compiler: enable escape analysis for runtime
[official-gcc.git] / gcc / fortran / check.c
bloba2c8b520d80168a94ddc0ab4ed3761fb94dd8570
1 /* Check functions
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* These functions check to see if an argument list is compatible with
23 a particular intrinsic function or subroutine. Presence of
24 required arguments has already been established, the argument list
25 has been sorted into the right order and has NULL arguments in the
26 correct places for missing optional arguments. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "options.h"
32 #include "gfortran.h"
33 #include "intrinsic.h"
34 #include "constructor.h"
35 #include "target-memory.h"
38 /* Make sure an expression is a scalar. */
40 static bool
41 scalar_check (gfc_expr *e, int n)
43 if (e->rank == 0)
44 return true;
46 gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
47 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
48 &e->where);
50 return false;
54 /* Check the type of an expression. */
56 static bool
57 type_check (gfc_expr *e, int n, bt type)
59 if (e->ts.type == type)
60 return true;
62 gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
63 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
64 &e->where, gfc_basic_typename (type));
66 return false;
70 /* Check that the expression is a numeric type. */
72 static bool
73 numeric_check (gfc_expr *e, int n)
75 /* Users sometime use a subroutine designator as an actual argument to
76 an intrinsic subprogram that expects an argument with a numeric type. */
77 if (e->symtree && e->symtree->n.sym->attr.subroutine)
78 goto error;
80 if (gfc_numeric_ts (&e->ts))
81 return true;
83 /* If the expression has not got a type, check if its namespace can
84 offer a default type. */
85 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
86 && e->symtree->n.sym->ts.type == BT_UNKNOWN
87 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
88 && gfc_numeric_ts (&e->symtree->n.sym->ts))
90 e->ts = e->symtree->n.sym->ts;
91 return true;
94 error:
96 gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
97 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
98 &e->where);
100 return false;
104 /* Check that an expression is integer or real. */
106 static bool
107 int_or_real_check (gfc_expr *e, int n)
109 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
111 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
112 "or REAL", gfc_current_intrinsic_arg[n]->name,
113 gfc_current_intrinsic, &e->where);
114 return false;
117 return true;
120 /* Check that an expression is integer or real; allow character for
121 F2003 or later. */
123 static bool
124 int_or_real_or_char_check_f2003 (gfc_expr *e, int n)
126 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
128 if (e->ts.type == BT_CHARACTER)
129 return gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Character for "
130 "%qs argument of %qs intrinsic at %L",
131 gfc_current_intrinsic_arg[n]->name,
132 gfc_current_intrinsic, &e->where);
133 else
135 if (gfc_option.allow_std & GFC_STD_F2003)
136 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
137 "or REAL or CHARACTER",
138 gfc_current_intrinsic_arg[n]->name,
139 gfc_current_intrinsic, &e->where);
140 else
141 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
142 "or REAL", gfc_current_intrinsic_arg[n]->name,
143 gfc_current_intrinsic, &e->where);
145 return false;
148 return true;
152 /* Check that an expression is real or complex. */
154 static bool
155 real_or_complex_check (gfc_expr *e, int n)
157 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
159 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
160 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
161 gfc_current_intrinsic, &e->where);
162 return false;
165 return true;
169 /* Check that an expression is INTEGER or PROCEDURE. */
171 static bool
172 int_or_proc_check (gfc_expr *e, int n)
174 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
176 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
177 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
178 gfc_current_intrinsic, &e->where);
179 return false;
182 return true;
186 /* Check that the expression is an optional constant integer
187 and that it specifies a valid kind for that type. */
189 static bool
190 kind_check (gfc_expr *k, int n, bt type)
192 int kind;
194 if (k == NULL)
195 return true;
197 if (!type_check (k, n, BT_INTEGER))
198 return false;
200 if (!scalar_check (k, n))
201 return false;
203 if (!gfc_check_init_expr (k))
205 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
206 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
207 &k->where);
208 return false;
211 if (gfc_extract_int (k, &kind)
212 || gfc_validate_kind (type, kind, true) < 0)
214 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
215 &k->where);
216 return false;
219 return true;
223 /* Make sure the expression is a double precision real. */
225 static bool
226 double_check (gfc_expr *d, int n)
228 if (!type_check (d, n, BT_REAL))
229 return false;
231 if (d->ts.kind != gfc_default_double_kind)
233 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
234 "precision", gfc_current_intrinsic_arg[n]->name,
235 gfc_current_intrinsic, &d->where);
236 return false;
239 return true;
243 static bool
244 coarray_check (gfc_expr *e, int n)
246 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
247 && CLASS_DATA (e)->attr.codimension
248 && CLASS_DATA (e)->as->corank)
250 gfc_add_class_array_ref (e);
251 return true;
254 if (!gfc_is_coarray (e))
256 gfc_error ("Expected coarray variable as %qs argument to the %s "
257 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
258 gfc_current_intrinsic, &e->where);
259 return false;
262 return true;
266 /* Make sure the expression is a logical array. */
268 static bool
269 logical_array_check (gfc_expr *array, int n)
271 if (array->ts.type != BT_LOGICAL || array->rank == 0)
273 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
274 "array", gfc_current_intrinsic_arg[n]->name,
275 gfc_current_intrinsic, &array->where);
276 return false;
279 return true;
283 /* Make sure an expression is an array. */
285 static bool
286 array_check (gfc_expr *e, int n)
288 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
289 && CLASS_DATA (e)->attr.dimension
290 && CLASS_DATA (e)->as->rank)
292 gfc_add_class_array_ref (e);
293 return true;
296 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
297 return true;
299 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
300 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
301 &e->where);
303 return false;
307 /* If expr is a constant, then check to ensure that it is greater than
308 of equal to zero. */
310 static bool
311 nonnegative_check (const char *arg, gfc_expr *expr)
313 int i;
315 if (expr->expr_type == EXPR_CONSTANT)
317 gfc_extract_int (expr, &i);
318 if (i < 0)
320 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
321 return false;
325 return true;
329 /* If expr is a constant, then check to ensure that it is greater than zero. */
331 static bool
332 positive_check (int n, gfc_expr *expr)
334 int i;
336 if (expr->expr_type == EXPR_CONSTANT)
338 gfc_extract_int (expr, &i);
339 if (i <= 0)
341 gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
342 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
343 &expr->where);
344 return false;
348 return true;
352 /* If expr2 is constant, then check that the value is less than
353 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
355 static bool
356 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
357 gfc_expr *expr2, bool or_equal)
359 int i2, i3;
361 if (expr2->expr_type == EXPR_CONSTANT)
363 gfc_extract_int (expr2, &i2);
364 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
366 /* For ISHFT[C], check that |shift| <= bit_size(i). */
367 if (arg2 == NULL)
369 if (i2 < 0)
370 i2 = -i2;
372 if (i2 > gfc_integer_kinds[i3].bit_size)
374 gfc_error ("The absolute value of SHIFT at %L must be less "
375 "than or equal to BIT_SIZE(%qs)",
376 &expr2->where, arg1);
377 return false;
381 if (or_equal)
383 if (i2 > gfc_integer_kinds[i3].bit_size)
385 gfc_error ("%qs at %L must be less than "
386 "or equal to BIT_SIZE(%qs)",
387 arg2, &expr2->where, arg1);
388 return false;
391 else
393 if (i2 >= gfc_integer_kinds[i3].bit_size)
395 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
396 arg2, &expr2->where, arg1);
397 return false;
402 return true;
406 /* If expr is constant, then check that the value is less than or equal
407 to the bit_size of the kind k. */
409 static bool
410 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
412 int i, val;
414 if (expr->expr_type != EXPR_CONSTANT)
415 return true;
417 i = gfc_validate_kind (BT_INTEGER, k, false);
418 gfc_extract_int (expr, &val);
420 if (val > gfc_integer_kinds[i].bit_size)
422 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
423 "INTEGER(KIND=%d)", arg, &expr->where, k);
424 return false;
427 return true;
431 /* If expr2 and expr3 are constants, then check that the value is less than
432 or equal to bit_size(expr1). */
434 static bool
435 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
436 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
438 int i2, i3;
440 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
442 gfc_extract_int (expr2, &i2);
443 gfc_extract_int (expr3, &i3);
444 i2 += i3;
445 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
446 if (i2 > gfc_integer_kinds[i3].bit_size)
448 gfc_error ("%<%s + %s%> at %L must be less than or equal "
449 "to BIT_SIZE(%qs)",
450 arg2, arg3, &expr2->where, arg1);
451 return false;
455 return true;
458 /* Make sure two expressions have the same type. */
460 static bool
461 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m, bool assoc = false)
463 gfc_typespec *ets = &e->ts;
464 gfc_typespec *fts = &f->ts;
466 if (assoc)
468 /* Procedure pointer component expressions have the type of the interface
469 procedure. If they are being tested for association with a procedure
470 pointer (ie. not a component), the type of the procedure must be
471 determined. */
472 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
473 ets = &e->symtree->n.sym->ts;
474 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
475 fts = &f->symtree->n.sym->ts;
478 if (gfc_compare_types (ets, fts))
479 return true;
481 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
482 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
483 gfc_current_intrinsic, &f->where,
484 gfc_current_intrinsic_arg[n]->name);
486 return false;
490 /* Make sure that an expression has a certain (nonzero) rank. */
492 static bool
493 rank_check (gfc_expr *e, int n, int rank)
495 if (e->rank == rank)
496 return true;
498 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
499 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
500 &e->where, rank);
502 return false;
506 /* Make sure a variable expression is not an optional dummy argument. */
508 static bool
509 nonoptional_check (gfc_expr *e, int n)
511 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
513 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
514 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
515 &e->where);
518 /* TODO: Recursive check on nonoptional variables? */
520 return true;
524 /* Check for ALLOCATABLE attribute. */
526 static bool
527 allocatable_check (gfc_expr *e, int n)
529 symbol_attribute attr;
531 attr = gfc_variable_attr (e, NULL);
532 if (!attr.allocatable || attr.associate_var)
534 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
535 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
536 &e->where);
537 return false;
540 return true;
544 /* Check that an expression has a particular kind. */
546 static bool
547 kind_value_check (gfc_expr *e, int n, int k)
549 if (e->ts.kind == k)
550 return true;
552 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
553 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
554 &e->where, k);
556 return false;
560 /* Make sure an expression is a variable. */
562 static bool
563 variable_check (gfc_expr *e, int n, bool allow_proc)
565 if (e->expr_type == EXPR_VARIABLE
566 && e->symtree->n.sym->attr.intent == INTENT_IN
567 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
568 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
570 gfc_ref *ref;
571 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
572 && CLASS_DATA (e->symtree->n.sym)
573 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
574 : e->symtree->n.sym->attr.pointer;
576 for (ref = e->ref; ref; ref = ref->next)
578 if (pointer && ref->type == REF_COMPONENT)
579 break;
580 if (ref->type == REF_COMPONENT
581 && ((ref->u.c.component->ts.type == BT_CLASS
582 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
583 || (ref->u.c.component->ts.type != BT_CLASS
584 && ref->u.c.component->attr.pointer)))
585 break;
588 if (!ref)
590 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
591 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
592 gfc_current_intrinsic, &e->where);
593 return false;
597 if (e->expr_type == EXPR_VARIABLE
598 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
599 && (allow_proc || !e->symtree->n.sym->attr.function))
600 return true;
602 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
603 && e->symtree->n.sym == e->symtree->n.sym->result)
605 gfc_namespace *ns;
606 for (ns = gfc_current_ns; ns; ns = ns->parent)
607 if (ns->proc_name == e->symtree->n.sym)
608 return true;
611 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
612 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
614 return false;
618 /* Check the common DIM parameter for correctness. */
620 static bool
621 dim_check (gfc_expr *dim, int n, bool optional)
623 if (dim == NULL)
624 return true;
626 if (!type_check (dim, n, BT_INTEGER))
627 return false;
629 if (!scalar_check (dim, n))
630 return false;
632 if (!optional && !nonoptional_check (dim, n))
633 return false;
635 return true;
639 /* If a coarray DIM parameter is a constant, make sure that it is greater than
640 zero and less than or equal to the corank of the given array. */
642 static bool
643 dim_corank_check (gfc_expr *dim, gfc_expr *array)
645 int corank;
647 gcc_assert (array->expr_type == EXPR_VARIABLE);
649 if (dim->expr_type != EXPR_CONSTANT)
650 return true;
652 if (array->ts.type == BT_CLASS)
653 return true;
655 corank = gfc_get_corank (array);
657 if (mpz_cmp_ui (dim->value.integer, 1) < 0
658 || mpz_cmp_ui (dim->value.integer, corank) > 0)
660 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
661 "codimension index", gfc_current_intrinsic, &dim->where);
663 return false;
666 return true;
670 /* If a DIM parameter is a constant, make sure that it is greater than
671 zero and less than or equal to the rank of the given array. If
672 allow_assumed is zero then dim must be less than the rank of the array
673 for assumed size arrays. */
675 static bool
676 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
678 gfc_array_ref *ar;
679 int rank;
681 if (dim == NULL)
682 return true;
684 if (dim->expr_type != EXPR_CONSTANT)
685 return true;
687 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
688 && array->value.function.isym->id == GFC_ISYM_SPREAD)
689 rank = array->rank + 1;
690 else
691 rank = array->rank;
693 /* Assumed-rank array. */
694 if (rank == -1)
695 rank = GFC_MAX_DIMENSIONS;
697 if (array->expr_type == EXPR_VARIABLE)
699 ar = gfc_find_array_ref (array);
700 if (ar->as->type == AS_ASSUMED_SIZE
701 && !allow_assumed
702 && ar->type != AR_ELEMENT
703 && ar->type != AR_SECTION)
704 rank--;
707 if (mpz_cmp_ui (dim->value.integer, 1) < 0
708 || mpz_cmp_ui (dim->value.integer, rank) > 0)
710 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
711 "dimension index", gfc_current_intrinsic, &dim->where);
713 return false;
716 return true;
720 /* Compare the size of a along dimension ai with the size of b along
721 dimension bi, returning 0 if they are known not to be identical,
722 and 1 if they are identical, or if this cannot be determined. */
724 static int
725 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
727 mpz_t a_size, b_size;
728 int ret;
730 gcc_assert (a->rank > ai);
731 gcc_assert (b->rank > bi);
733 ret = 1;
735 if (gfc_array_dimen_size (a, ai, &a_size))
737 if (gfc_array_dimen_size (b, bi, &b_size))
739 if (mpz_cmp (a_size, b_size) != 0)
740 ret = 0;
742 mpz_clear (b_size);
744 mpz_clear (a_size);
746 return ret;
749 /* Calculate the length of a character variable, including substrings.
750 Strip away parentheses if necessary. Return -1 if no length could
751 be determined. */
753 static long
754 gfc_var_strlen (const gfc_expr *a)
756 gfc_ref *ra;
758 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
759 a = a->value.op.op1;
761 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
764 if (ra)
766 long start_a, end_a;
768 if (!ra->u.ss.end)
769 return -1;
771 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
772 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
774 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
775 : 1;
776 end_a = mpz_get_si (ra->u.ss.end->value.integer);
777 return (end_a < start_a) ? 0 : end_a - start_a + 1;
779 else if (ra->u.ss.start
780 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
781 return 1;
782 else
783 return -1;
786 if (a->ts.u.cl && a->ts.u.cl->length
787 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
788 return mpz_get_si (a->ts.u.cl->length->value.integer);
789 else if (a->expr_type == EXPR_CONSTANT
790 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
791 return a->value.character.length;
792 else
793 return -1;
797 /* Check whether two character expressions have the same length;
798 returns true if they have or if the length cannot be determined,
799 otherwise return false and raise a gfc_error. */
801 bool
802 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
804 long len_a, len_b;
806 len_a = gfc_var_strlen(a);
807 len_b = gfc_var_strlen(b);
809 if (len_a == -1 || len_b == -1 || len_a == len_b)
810 return true;
811 else
813 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
814 len_a, len_b, name, &a->where);
815 return false;
820 /***** Check functions *****/
822 /* Check subroutine suitable for intrinsics taking a real argument and
823 a kind argument for the result. */
825 static bool
826 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
828 if (!type_check (a, 0, BT_REAL))
829 return false;
830 if (!kind_check (kind, 1, type))
831 return false;
833 return true;
837 /* Check subroutine suitable for ceiling, floor and nint. */
839 bool
840 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
842 return check_a_kind (a, kind, BT_INTEGER);
846 /* Check subroutine suitable for aint, anint. */
848 bool
849 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
851 return check_a_kind (a, kind, BT_REAL);
855 bool
856 gfc_check_abs (gfc_expr *a)
858 if (!numeric_check (a, 0))
859 return false;
861 return true;
865 bool
866 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
868 if (!type_check (a, 0, BT_INTEGER))
869 return false;
870 if (!kind_check (kind, 1, BT_CHARACTER))
871 return false;
873 return true;
877 bool
878 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
880 if (!type_check (name, 0, BT_CHARACTER)
881 || !scalar_check (name, 0))
882 return false;
883 if (!kind_value_check (name, 0, gfc_default_character_kind))
884 return false;
886 if (!type_check (mode, 1, BT_CHARACTER)
887 || !scalar_check (mode, 1))
888 return false;
889 if (!kind_value_check (mode, 1, gfc_default_character_kind))
890 return false;
892 return true;
896 bool
897 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
899 if (!logical_array_check (mask, 0))
900 return false;
902 if (!dim_check (dim, 1, false))
903 return false;
905 if (!dim_rank_check (dim, mask, 0))
906 return false;
908 return true;
912 bool
913 gfc_check_allocated (gfc_expr *array)
915 /* Tests on allocated components of coarrays need to detour the check to
916 argument of the _caf_get. */
917 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
918 && array->value.function.isym
919 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
921 array = array->value.function.actual->expr;
922 if (!array->ref)
923 return false;
926 if (!variable_check (array, 0, false))
927 return false;
928 if (!allocatable_check (array, 0))
929 return false;
931 return true;
935 /* Common check function where the first argument must be real or
936 integer and the second argument must be the same as the first. */
938 bool
939 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
941 if (!int_or_real_check (a, 0))
942 return false;
944 if (a->ts.type != p->ts.type)
946 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
947 "have the same type", gfc_current_intrinsic_arg[0]->name,
948 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
949 &p->where);
950 return false;
953 if (a->ts.kind != p->ts.kind)
955 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
956 &p->where))
957 return false;
960 return true;
964 bool
965 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
967 if (!double_check (x, 0) || !double_check (y, 1))
968 return false;
970 return true;
974 bool
975 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
977 symbol_attribute attr1, attr2;
978 int i;
979 bool t;
980 locus *where;
982 where = &pointer->where;
984 if (pointer->expr_type == EXPR_NULL)
985 goto null_arg;
987 attr1 = gfc_expr_attr (pointer);
989 if (!attr1.pointer && !attr1.proc_pointer)
991 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
992 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
993 &pointer->where);
994 return false;
997 /* F2008, C1242. */
998 if (attr1.pointer && gfc_is_coindexed (pointer))
1000 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1001 "coindexed", gfc_current_intrinsic_arg[0]->name,
1002 gfc_current_intrinsic, &pointer->where);
1003 return false;
1006 /* Target argument is optional. */
1007 if (target == NULL)
1008 return true;
1010 where = &target->where;
1011 if (target->expr_type == EXPR_NULL)
1012 goto null_arg;
1014 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
1015 attr2 = gfc_expr_attr (target);
1016 else
1018 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
1019 "or target VARIABLE or FUNCTION",
1020 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1021 &target->where);
1022 return false;
1025 if (attr1.pointer && !attr2.pointer && !attr2.target)
1027 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
1028 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
1029 gfc_current_intrinsic, &target->where);
1030 return false;
1033 /* F2008, C1242. */
1034 if (attr1.pointer && gfc_is_coindexed (target))
1036 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
1037 "coindexed", gfc_current_intrinsic_arg[1]->name,
1038 gfc_current_intrinsic, &target->where);
1039 return false;
1042 t = true;
1043 if (!same_type_check (pointer, 0, target, 1, true))
1044 t = false;
1045 if (!rank_check (target, 0, pointer->rank))
1046 t = false;
1047 if (target->rank > 0)
1049 for (i = 0; i < target->rank; i++)
1050 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1052 gfc_error ("Array section with a vector subscript at %L shall not "
1053 "be the target of a pointer",
1054 &target->where);
1055 t = false;
1056 break;
1059 return t;
1061 null_arg:
1063 gfc_error ("NULL pointer at %L is not permitted as actual argument "
1064 "of %qs intrinsic function", where, gfc_current_intrinsic);
1065 return false;
1070 bool
1071 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1073 /* gfc_notify_std would be a waste of time as the return value
1074 is seemingly used only for the generic resolution. The error
1075 will be: Too many arguments. */
1076 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1077 return false;
1079 return gfc_check_atan2 (y, x);
1083 bool
1084 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1086 if (!type_check (y, 0, BT_REAL))
1087 return false;
1088 if (!same_type_check (y, 0, x, 1))
1089 return false;
1091 return true;
1095 static bool
1096 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1097 gfc_expr *stat, int stat_no)
1099 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1100 return false;
1102 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1103 && !(atom->ts.type == BT_LOGICAL
1104 && atom->ts.kind == gfc_atomic_logical_kind))
1106 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1107 "integer of ATOMIC_INT_KIND or a logical of "
1108 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1109 return false;
1112 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1114 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1115 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1116 return false;
1119 if (atom->ts.type != value->ts.type)
1121 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1122 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1123 gfc_current_intrinsic, &value->where,
1124 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1125 return false;
1128 if (stat != NULL)
1130 if (!type_check (stat, stat_no, BT_INTEGER))
1131 return false;
1132 if (!scalar_check (stat, stat_no))
1133 return false;
1134 if (!variable_check (stat, stat_no, false))
1135 return false;
1136 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1137 return false;
1139 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1140 gfc_current_intrinsic, &stat->where))
1141 return false;
1144 return true;
1148 bool
1149 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1151 if (atom->expr_type == EXPR_FUNCTION
1152 && atom->value.function.isym
1153 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1154 atom = atom->value.function.actual->expr;
1156 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1158 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1159 "definable", gfc_current_intrinsic, &atom->where);
1160 return false;
1163 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1167 bool
1168 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1170 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1172 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1173 "integer of ATOMIC_INT_KIND", &atom->where,
1174 gfc_current_intrinsic);
1175 return false;
1178 return gfc_check_atomic_def (atom, value, stat);
1182 bool
1183 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1185 if (atom->expr_type == EXPR_FUNCTION
1186 && atom->value.function.isym
1187 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1188 atom = atom->value.function.actual->expr;
1190 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1192 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1193 "definable", gfc_current_intrinsic, &value->where);
1194 return false;
1197 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1201 bool
1202 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1204 /* IMAGE has to be a positive, scalar integer. */
1205 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1206 || !positive_check (0, image))
1207 return false;
1209 if (team)
1211 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1212 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1213 &team->where);
1214 return false;
1216 return true;
1220 bool
1221 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1223 if (team)
1225 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1226 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1227 &team->where);
1228 return false;
1231 if (kind)
1233 int k;
1235 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1236 || !positive_check (1, kind))
1237 return false;
1239 /* Get the kind, reporting error on non-constant or overflow. */
1240 gfc_current_locus = kind->where;
1241 if (gfc_extract_int (kind, &k, 1))
1242 return false;
1243 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1245 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1246 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1247 gfc_current_intrinsic, &kind->where);
1248 return false;
1251 return true;
1255 bool
1256 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1257 gfc_expr *new_val, gfc_expr *stat)
1259 if (atom->expr_type == EXPR_FUNCTION
1260 && atom->value.function.isym
1261 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1262 atom = atom->value.function.actual->expr;
1264 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1265 return false;
1267 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1268 return false;
1270 if (!same_type_check (atom, 0, old, 1))
1271 return false;
1273 if (!same_type_check (atom, 0, compare, 2))
1274 return false;
1276 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1278 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1279 "definable", gfc_current_intrinsic, &atom->where);
1280 return false;
1283 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1285 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1286 "definable", gfc_current_intrinsic, &old->where);
1287 return false;
1290 return true;
1293 bool
1294 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1296 if (event->ts.type != BT_DERIVED
1297 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1298 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1300 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1301 "shall be of type EVENT_TYPE", &event->where);
1302 return false;
1305 if (!scalar_check (event, 0))
1306 return false;
1308 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1310 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1311 "shall be definable", &count->where);
1312 return false;
1315 if (!type_check (count, 1, BT_INTEGER))
1316 return false;
1318 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1319 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1321 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1323 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1324 "shall have at least the range of the default integer",
1325 &count->where);
1326 return false;
1329 if (stat != NULL)
1331 if (!type_check (stat, 2, BT_INTEGER))
1332 return false;
1333 if (!scalar_check (stat, 2))
1334 return false;
1335 if (!variable_check (stat, 2, false))
1336 return false;
1338 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1339 gfc_current_intrinsic, &stat->where))
1340 return false;
1343 return true;
1347 bool
1348 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1349 gfc_expr *stat)
1351 if (atom->expr_type == EXPR_FUNCTION
1352 && atom->value.function.isym
1353 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1354 atom = atom->value.function.actual->expr;
1356 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1358 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1359 "integer of ATOMIC_INT_KIND", &atom->where,
1360 gfc_current_intrinsic);
1361 return false;
1364 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1365 return false;
1367 if (!scalar_check (old, 2))
1368 return false;
1370 if (!same_type_check (atom, 0, old, 2))
1371 return false;
1373 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1375 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1376 "definable", gfc_current_intrinsic, &atom->where);
1377 return false;
1380 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1382 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1383 "definable", gfc_current_intrinsic, &old->where);
1384 return false;
1387 return true;
1391 /* BESJN and BESYN functions. */
1393 bool
1394 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1396 if (!type_check (n, 0, BT_INTEGER))
1397 return false;
1398 if (n->expr_type == EXPR_CONSTANT)
1400 int i;
1401 gfc_extract_int (n, &i);
1402 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1403 "N at %L", &n->where))
1404 return false;
1407 if (!type_check (x, 1, BT_REAL))
1408 return false;
1410 return true;
1414 /* Transformational version of the Bessel JN and YN functions. */
1416 bool
1417 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1419 if (!type_check (n1, 0, BT_INTEGER))
1420 return false;
1421 if (!scalar_check (n1, 0))
1422 return false;
1423 if (!nonnegative_check ("N1", n1))
1424 return false;
1426 if (!type_check (n2, 1, BT_INTEGER))
1427 return false;
1428 if (!scalar_check (n2, 1))
1429 return false;
1430 if (!nonnegative_check ("N2", n2))
1431 return false;
1433 if (!type_check (x, 2, BT_REAL))
1434 return false;
1435 if (!scalar_check (x, 2))
1436 return false;
1438 return true;
1442 bool
1443 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1445 if (!type_check (i, 0, BT_INTEGER))
1446 return false;
1448 if (!type_check (j, 1, BT_INTEGER))
1449 return false;
1451 return true;
1455 bool
1456 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1458 if (!type_check (i, 0, BT_INTEGER))
1459 return false;
1461 if (!type_check (pos, 1, BT_INTEGER))
1462 return false;
1464 if (!nonnegative_check ("pos", pos))
1465 return false;
1467 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1468 return false;
1470 return true;
1474 bool
1475 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1477 if (!type_check (i, 0, BT_INTEGER))
1478 return false;
1479 if (!kind_check (kind, 1, BT_CHARACTER))
1480 return false;
1482 return true;
1486 bool
1487 gfc_check_chdir (gfc_expr *dir)
1489 if (!type_check (dir, 0, BT_CHARACTER))
1490 return false;
1491 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1492 return false;
1494 return true;
1498 bool
1499 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1501 if (!type_check (dir, 0, BT_CHARACTER))
1502 return false;
1503 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1504 return false;
1506 if (status == NULL)
1507 return true;
1509 if (!type_check (status, 1, BT_INTEGER))
1510 return false;
1511 if (!scalar_check (status, 1))
1512 return false;
1514 return true;
1518 bool
1519 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1521 if (!type_check (name, 0, BT_CHARACTER))
1522 return false;
1523 if (!kind_value_check (name, 0, gfc_default_character_kind))
1524 return false;
1526 if (!type_check (mode, 1, BT_CHARACTER))
1527 return false;
1528 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1529 return false;
1531 return true;
1535 bool
1536 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1538 if (!type_check (name, 0, BT_CHARACTER))
1539 return false;
1540 if (!kind_value_check (name, 0, gfc_default_character_kind))
1541 return false;
1543 if (!type_check (mode, 1, BT_CHARACTER))
1544 return false;
1545 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1546 return false;
1548 if (status == NULL)
1549 return true;
1551 if (!type_check (status, 2, BT_INTEGER))
1552 return false;
1554 if (!scalar_check (status, 2))
1555 return false;
1557 return true;
1561 bool
1562 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1564 if (!numeric_check (x, 0))
1565 return false;
1567 if (y != NULL)
1569 if (!numeric_check (y, 1))
1570 return false;
1572 if (x->ts.type == BT_COMPLEX)
1574 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1575 "present if %<x%> is COMPLEX",
1576 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1577 &y->where);
1578 return false;
1581 if (y->ts.type == BT_COMPLEX)
1583 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1584 "of either REAL or INTEGER",
1585 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1586 &y->where);
1587 return false;
1592 if (!kind_check (kind, 2, BT_COMPLEX))
1593 return false;
1595 if (!kind && warn_conversion
1596 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1597 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1598 "COMPLEX(%d) at %L might lose precision, consider using "
1599 "the KIND argument", gfc_typename (&x->ts),
1600 gfc_default_real_kind, &x->where);
1601 else if (y && !kind && warn_conversion
1602 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1603 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1604 "COMPLEX(%d) at %L might lose precision, consider using "
1605 "the KIND argument", gfc_typename (&y->ts),
1606 gfc_default_real_kind, &y->where);
1607 return true;
1611 static bool
1612 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1613 gfc_expr *errmsg, bool co_reduce)
1615 if (!variable_check (a, 0, false))
1616 return false;
1618 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1619 "INTENT(INOUT)"))
1620 return false;
1622 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1623 if (gfc_has_vector_subscript (a))
1625 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1626 "subroutine %s shall not have a vector subscript",
1627 &a->where, gfc_current_intrinsic);
1628 return false;
1631 if (gfc_is_coindexed (a))
1633 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1634 "coindexed", &a->where, gfc_current_intrinsic);
1635 return false;
1638 if (image_idx != NULL)
1640 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1641 return false;
1642 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1643 return false;
1646 if (stat != NULL)
1648 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1649 return false;
1650 if (!scalar_check (stat, co_reduce ? 3 : 2))
1651 return false;
1652 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1653 return false;
1654 if (stat->ts.kind != 4)
1656 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1657 "variable", &stat->where);
1658 return false;
1662 if (errmsg != NULL)
1664 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1665 return false;
1666 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1667 return false;
1668 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1669 return false;
1670 if (errmsg->ts.kind != 1)
1672 gfc_error ("The errmsg= argument at %L must be a default-kind "
1673 "character variable", &errmsg->where);
1674 return false;
1678 if (flag_coarray == GFC_FCOARRAY_NONE)
1680 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1681 &a->where);
1682 return false;
1685 return true;
1689 bool
1690 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1691 gfc_expr *errmsg)
1693 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1695 gfc_error ("Support for the A argument at %L which is polymorphic A "
1696 "argument or has allocatable components is not yet "
1697 "implemented", &a->where);
1698 return false;
1700 return check_co_collective (a, source_image, stat, errmsg, false);
1704 bool
1705 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1706 gfc_expr *stat, gfc_expr *errmsg)
1708 symbol_attribute attr;
1709 gfc_formal_arglist *formal;
1710 gfc_symbol *sym;
1712 if (a->ts.type == BT_CLASS)
1714 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1715 &a->where);
1716 return false;
1719 if (gfc_expr_attr (a).alloc_comp)
1721 gfc_error ("Support for the A argument at %L with allocatable components"
1722 " is not yet implemented", &a->where);
1723 return false;
1726 if (!check_co_collective (a, result_image, stat, errmsg, true))
1727 return false;
1729 if (!gfc_resolve_expr (op))
1730 return false;
1732 attr = gfc_expr_attr (op);
1733 if (!attr.pure || !attr.function)
1735 gfc_error ("OPERATOR argument at %L must be a PURE function",
1736 &op->where);
1737 return false;
1740 if (attr.intrinsic)
1742 /* None of the intrinsics fulfills the criteria of taking two arguments,
1743 returning the same type and kind as the arguments and being permitted
1744 as actual argument. */
1745 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1746 op->symtree->n.sym->name, &op->where);
1747 return false;
1750 if (gfc_is_proc_ptr_comp (op))
1752 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1753 sym = comp->ts.interface;
1755 else
1756 sym = op->symtree->n.sym;
1758 formal = sym->formal;
1760 if (!formal || !formal->next || formal->next->next)
1762 gfc_error ("The function passed as OPERATOR at %L shall have two "
1763 "arguments", &op->where);
1764 return false;
1767 if (sym->result->ts.type == BT_UNKNOWN)
1768 gfc_set_default_type (sym->result, 0, NULL);
1770 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1772 gfc_error ("The A argument at %L has type %s but the function passed as "
1773 "OPERATOR at %L returns %s",
1774 &a->where, gfc_typename (&a->ts), &op->where,
1775 gfc_typename (&sym->result->ts));
1776 return false;
1778 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1779 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1781 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1782 "%s and %s but shall have type %s", &op->where,
1783 gfc_typename (&formal->sym->ts),
1784 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1785 return false;
1787 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1788 || formal->next->sym->as || formal->sym->attr.allocatable
1789 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1790 || formal->next->sym->attr.pointer)
1792 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1793 "nonallocatable nonpointer arguments and return a "
1794 "nonallocatable nonpointer scalar", &op->where);
1795 return false;
1798 if (formal->sym->attr.value != formal->next->sym->attr.value)
1800 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1801 "attribute either for none or both arguments", &op->where);
1802 return false;
1805 if (formal->sym->attr.target != formal->next->sym->attr.target)
1807 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1808 "attribute either for none or both arguments", &op->where);
1809 return false;
1812 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1814 gfc_error ("The function passed as OPERATOR at %L shall have the "
1815 "ASYNCHRONOUS attribute either for none or both arguments",
1816 &op->where);
1817 return false;
1820 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1822 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1823 "OPTIONAL attribute for either of the arguments", &op->where);
1824 return false;
1827 if (a->ts.type == BT_CHARACTER)
1829 gfc_charlen *cl;
1830 unsigned long actual_size, formal_size1, formal_size2, result_size;
1832 cl = a->ts.u.cl;
1833 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1834 ? mpz_get_ui (cl->length->value.integer) : 0;
1836 cl = formal->sym->ts.u.cl;
1837 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1838 ? mpz_get_ui (cl->length->value.integer) : 0;
1840 cl = formal->next->sym->ts.u.cl;
1841 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1842 ? mpz_get_ui (cl->length->value.integer) : 0;
1844 cl = sym->ts.u.cl;
1845 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1846 ? mpz_get_ui (cl->length->value.integer) : 0;
1848 if (actual_size
1849 && ((formal_size1 && actual_size != formal_size1)
1850 || (formal_size2 && actual_size != formal_size2)))
1852 gfc_error ("The character length of the A argument at %L and of the "
1853 "arguments of the OPERATOR at %L shall be the same",
1854 &a->where, &op->where);
1855 return false;
1857 if (actual_size && result_size && actual_size != result_size)
1859 gfc_error ("The character length of the A argument at %L and of the "
1860 "function result of the OPERATOR at %L shall be the same",
1861 &a->where, &op->where);
1862 return false;
1866 return true;
1870 bool
1871 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1872 gfc_expr *errmsg)
1874 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1875 && a->ts.type != BT_CHARACTER)
1877 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1878 "integer, real or character",
1879 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1880 &a->where);
1881 return false;
1883 return check_co_collective (a, result_image, stat, errmsg, false);
1887 bool
1888 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1889 gfc_expr *errmsg)
1891 if (!numeric_check (a, 0))
1892 return false;
1893 return check_co_collective (a, result_image, stat, errmsg, false);
1897 bool
1898 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1900 if (!int_or_real_check (x, 0))
1901 return false;
1902 if (!scalar_check (x, 0))
1903 return false;
1905 if (!int_or_real_check (y, 1))
1906 return false;
1907 if (!scalar_check (y, 1))
1908 return false;
1910 return true;
1914 bool
1915 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1917 if (!logical_array_check (mask, 0))
1918 return false;
1919 if (!dim_check (dim, 1, false))
1920 return false;
1921 if (!dim_rank_check (dim, mask, 0))
1922 return false;
1923 if (!kind_check (kind, 2, BT_INTEGER))
1924 return false;
1925 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1926 "with KIND argument at %L",
1927 gfc_current_intrinsic, &kind->where))
1928 return false;
1930 return true;
1934 bool
1935 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1937 if (!array_check (array, 0))
1938 return false;
1940 if (!type_check (shift, 1, BT_INTEGER))
1941 return false;
1943 if (!dim_check (dim, 2, true))
1944 return false;
1946 if (!dim_rank_check (dim, array, false))
1947 return false;
1949 if (array->rank == 1 || shift->rank == 0)
1951 if (!scalar_check (shift, 1))
1952 return false;
1954 else if (shift->rank == array->rank - 1)
1956 int d;
1957 if (!dim)
1958 d = 1;
1959 else if (dim->expr_type == EXPR_CONSTANT)
1960 gfc_extract_int (dim, &d);
1961 else
1962 d = -1;
1964 if (d > 0)
1966 int i, j;
1967 for (i = 0, j = 0; i < array->rank; i++)
1968 if (i != d - 1)
1970 if (!identical_dimen_shape (array, i, shift, j))
1972 gfc_error ("%qs argument of %qs intrinsic at %L has "
1973 "invalid shape in dimension %d (%ld/%ld)",
1974 gfc_current_intrinsic_arg[1]->name,
1975 gfc_current_intrinsic, &shift->where, i + 1,
1976 mpz_get_si (array->shape[i]),
1977 mpz_get_si (shift->shape[j]));
1978 return false;
1981 j += 1;
1985 else
1987 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1988 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1989 gfc_current_intrinsic, &shift->where, array->rank - 1);
1990 return false;
1993 return true;
1997 bool
1998 gfc_check_ctime (gfc_expr *time)
2000 if (!scalar_check (time, 0))
2001 return false;
2003 if (!type_check (time, 0, BT_INTEGER))
2004 return false;
2006 return true;
2010 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
2012 if (!double_check (y, 0) || !double_check (x, 1))
2013 return false;
2015 return true;
2018 bool
2019 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
2021 if (!numeric_check (x, 0))
2022 return false;
2024 if (y != NULL)
2026 if (!numeric_check (y, 1))
2027 return false;
2029 if (x->ts.type == BT_COMPLEX)
2031 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
2032 "present if %<x%> is COMPLEX",
2033 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2034 &y->where);
2035 return false;
2038 if (y->ts.type == BT_COMPLEX)
2040 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2041 "of either REAL or INTEGER",
2042 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2043 &y->where);
2044 return false;
2048 return true;
2052 bool
2053 gfc_check_dble (gfc_expr *x)
2055 if (!numeric_check (x, 0))
2056 return false;
2058 return true;
2062 bool
2063 gfc_check_digits (gfc_expr *x)
2065 if (!int_or_real_check (x, 0))
2066 return false;
2068 return true;
2072 bool
2073 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2075 switch (vector_a->ts.type)
2077 case BT_LOGICAL:
2078 if (!type_check (vector_b, 1, BT_LOGICAL))
2079 return false;
2080 break;
2082 case BT_INTEGER:
2083 case BT_REAL:
2084 case BT_COMPLEX:
2085 if (!numeric_check (vector_b, 1))
2086 return false;
2087 break;
2089 default:
2090 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2091 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2092 gfc_current_intrinsic, &vector_a->where);
2093 return false;
2096 if (!rank_check (vector_a, 0, 1))
2097 return false;
2099 if (!rank_check (vector_b, 1, 1))
2100 return false;
2102 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2104 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2105 "intrinsic %<dot_product%>",
2106 gfc_current_intrinsic_arg[0]->name,
2107 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2108 return false;
2111 return true;
2115 bool
2116 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2118 if (!type_check (x, 0, BT_REAL)
2119 || !type_check (y, 1, BT_REAL))
2120 return false;
2122 if (x->ts.kind != gfc_default_real_kind)
2124 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2125 "real", gfc_current_intrinsic_arg[0]->name,
2126 gfc_current_intrinsic, &x->where);
2127 return false;
2130 if (y->ts.kind != gfc_default_real_kind)
2132 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2133 "real", gfc_current_intrinsic_arg[1]->name,
2134 gfc_current_intrinsic, &y->where);
2135 return false;
2138 return true;
2142 bool
2143 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2145 if (!type_check (i, 0, BT_INTEGER))
2146 return false;
2148 if (!type_check (j, 1, BT_INTEGER))
2149 return false;
2151 if (i->is_boz && j->is_boz)
2153 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2154 "constants", &i->where, &j->where);
2155 return false;
2158 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2159 return false;
2161 if (!type_check (shift, 2, BT_INTEGER))
2162 return false;
2164 if (!nonnegative_check ("SHIFT", shift))
2165 return false;
2167 if (i->is_boz)
2169 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2170 return false;
2171 i->ts.kind = j->ts.kind;
2173 else
2175 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2176 return false;
2177 j->ts.kind = i->ts.kind;
2180 return true;
2184 bool
2185 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2186 gfc_expr *dim)
2188 int d;
2190 if (!array_check (array, 0))
2191 return false;
2193 if (!type_check (shift, 1, BT_INTEGER))
2194 return false;
2196 if (!dim_check (dim, 3, true))
2197 return false;
2199 if (!dim_rank_check (dim, array, false))
2200 return false;
2202 if (!dim)
2203 d = 1;
2204 else if (dim->expr_type == EXPR_CONSTANT)
2205 gfc_extract_int (dim, &d);
2206 else
2207 d = -1;
2209 if (array->rank == 1 || shift->rank == 0)
2211 if (!scalar_check (shift, 1))
2212 return false;
2214 else if (shift->rank == array->rank - 1)
2216 if (d > 0)
2218 int i, j;
2219 for (i = 0, j = 0; i < array->rank; i++)
2220 if (i != d - 1)
2222 if (!identical_dimen_shape (array, i, shift, j))
2224 gfc_error ("%qs argument of %qs intrinsic at %L has "
2225 "invalid shape in dimension %d (%ld/%ld)",
2226 gfc_current_intrinsic_arg[1]->name,
2227 gfc_current_intrinsic, &shift->where, i + 1,
2228 mpz_get_si (array->shape[i]),
2229 mpz_get_si (shift->shape[j]));
2230 return false;
2233 j += 1;
2237 else
2239 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2240 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2241 gfc_current_intrinsic, &shift->where, array->rank - 1);
2242 return false;
2245 if (boundary != NULL)
2247 if (!same_type_check (array, 0, boundary, 2))
2248 return false;
2250 /* Reject unequal string lengths and emit a better error message than
2251 gfc_check_same_strlen would. */
2252 if (array->ts.type == BT_CHARACTER)
2254 ssize_t len_a, len_b;
2256 len_a = gfc_var_strlen (array);
2257 len_b = gfc_var_strlen (boundary);
2258 if (len_a != -1 && len_b != -1 && len_a != len_b)
2260 gfc_error ("%qs must be of same type and kind as %qs at %L in %qs",
2261 gfc_current_intrinsic_arg[2]->name,
2262 gfc_current_intrinsic_arg[0]->name,
2263 &boundary->where, gfc_current_intrinsic);
2264 return false;
2268 if (array->rank == 1 || boundary->rank == 0)
2270 if (!scalar_check (boundary, 2))
2271 return false;
2273 else if (boundary->rank == array->rank - 1)
2275 if (d > 0)
2277 int i,j;
2278 for (i = 0, j = 0; i < array->rank; i++)
2280 if (i != d - 1)
2282 if (!identical_dimen_shape (array, i, boundary, j))
2284 gfc_error ("%qs argument of %qs intrinsic at %L has "
2285 "invalid shape in dimension %d (%ld/%ld)",
2286 gfc_current_intrinsic_arg[2]->name,
2287 gfc_current_intrinsic, &shift->where, i+1,
2288 mpz_get_si (array->shape[i]),
2289 mpz_get_si (boundary->shape[j]));
2290 return false;
2292 j += 1;
2297 else
2299 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2300 "rank %d or be a scalar",
2301 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2302 &shift->where, array->rank - 1);
2303 return false;
2306 else
2308 switch (array->ts.type)
2310 case BT_INTEGER:
2311 case BT_LOGICAL:
2312 case BT_REAL:
2313 case BT_COMPLEX:
2314 case BT_CHARACTER:
2315 break;
2317 default:
2318 gfc_error ("Missing %qs argument to %qs intrinsic at %L for %qs "
2319 "of type %qs", gfc_current_intrinsic_arg[2]->name,
2320 gfc_current_intrinsic, &array->where,
2321 gfc_current_intrinsic_arg[0]->name,
2322 gfc_typename (&array->ts));
2323 return false;
2327 return true;
2330 bool
2331 gfc_check_float (gfc_expr *a)
2333 if (!type_check (a, 0, BT_INTEGER))
2334 return false;
2336 if ((a->ts.kind != gfc_default_integer_kind)
2337 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2338 "kind argument to %s intrinsic at %L",
2339 gfc_current_intrinsic, &a->where))
2340 return false;
2342 return true;
2345 /* A single complex argument. */
2347 bool
2348 gfc_check_fn_c (gfc_expr *a)
2350 if (!type_check (a, 0, BT_COMPLEX))
2351 return false;
2353 return true;
2357 /* A single real argument. */
2359 bool
2360 gfc_check_fn_r (gfc_expr *a)
2362 if (!type_check (a, 0, BT_REAL))
2363 return false;
2365 return true;
2368 /* A single double argument. */
2370 bool
2371 gfc_check_fn_d (gfc_expr *a)
2373 if (!double_check (a, 0))
2374 return false;
2376 return true;
2379 /* A single real or complex argument. */
2381 bool
2382 gfc_check_fn_rc (gfc_expr *a)
2384 if (!real_or_complex_check (a, 0))
2385 return false;
2387 return true;
2391 bool
2392 gfc_check_fn_rc2008 (gfc_expr *a)
2394 if (!real_or_complex_check (a, 0))
2395 return false;
2397 if (a->ts.type == BT_COMPLEX
2398 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2399 "of %qs intrinsic at %L",
2400 gfc_current_intrinsic_arg[0]->name,
2401 gfc_current_intrinsic, &a->where))
2402 return false;
2404 return true;
2408 bool
2409 gfc_check_fnum (gfc_expr *unit)
2411 if (!type_check (unit, 0, BT_INTEGER))
2412 return false;
2414 if (!scalar_check (unit, 0))
2415 return false;
2417 return true;
2421 bool
2422 gfc_check_huge (gfc_expr *x)
2424 if (!int_or_real_check (x, 0))
2425 return false;
2427 return true;
2431 bool
2432 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2434 if (!type_check (x, 0, BT_REAL))
2435 return false;
2436 if (!same_type_check (x, 0, y, 1))
2437 return false;
2439 return true;
2443 /* Check that the single argument is an integer. */
2445 bool
2446 gfc_check_i (gfc_expr *i)
2448 if (!type_check (i, 0, BT_INTEGER))
2449 return false;
2451 return true;
2455 bool
2456 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2458 if (!type_check (i, 0, BT_INTEGER))
2459 return false;
2461 if (!type_check (j, 1, BT_INTEGER))
2462 return false;
2464 if (i->ts.kind != j->ts.kind)
2466 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2467 &i->where))
2468 return false;
2471 return true;
2475 bool
2476 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2478 if (!type_check (i, 0, BT_INTEGER))
2479 return false;
2481 if (!type_check (pos, 1, BT_INTEGER))
2482 return false;
2484 if (!type_check (len, 2, BT_INTEGER))
2485 return false;
2487 if (!nonnegative_check ("pos", pos))
2488 return false;
2490 if (!nonnegative_check ("len", len))
2491 return false;
2493 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2494 return false;
2496 return true;
2500 bool
2501 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2503 int i;
2505 if (!type_check (c, 0, BT_CHARACTER))
2506 return false;
2508 if (!kind_check (kind, 1, BT_INTEGER))
2509 return false;
2511 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2512 "with KIND argument at %L",
2513 gfc_current_intrinsic, &kind->where))
2514 return false;
2516 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2518 gfc_expr *start;
2519 gfc_expr *end;
2520 gfc_ref *ref;
2522 /* Substring references don't have the charlength set. */
2523 ref = c->ref;
2524 while (ref && ref->type != REF_SUBSTRING)
2525 ref = ref->next;
2527 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2529 if (!ref)
2531 /* Check that the argument is length one. Non-constant lengths
2532 can't be checked here, so assume they are ok. */
2533 if (c->ts.u.cl && c->ts.u.cl->length)
2535 /* If we already have a length for this expression then use it. */
2536 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2537 return true;
2538 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2540 else
2541 return true;
2543 else
2545 start = ref->u.ss.start;
2546 end = ref->u.ss.end;
2548 gcc_assert (start);
2549 if (end == NULL || end->expr_type != EXPR_CONSTANT
2550 || start->expr_type != EXPR_CONSTANT)
2551 return true;
2553 i = mpz_get_si (end->value.integer) + 1
2554 - mpz_get_si (start->value.integer);
2557 else
2558 return true;
2560 if (i != 1)
2562 gfc_error ("Argument of %s at %L must be of length one",
2563 gfc_current_intrinsic, &c->where);
2564 return false;
2567 return true;
2571 bool
2572 gfc_check_idnint (gfc_expr *a)
2574 if (!double_check (a, 0))
2575 return false;
2577 return true;
2581 bool
2582 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2584 if (!type_check (i, 0, BT_INTEGER))
2585 return false;
2587 if (!type_check (j, 1, BT_INTEGER))
2588 return false;
2590 if (i->ts.kind != j->ts.kind)
2592 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2593 &i->where))
2594 return false;
2597 return true;
2601 bool
2602 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2603 gfc_expr *kind)
2605 if (!type_check (string, 0, BT_CHARACTER)
2606 || !type_check (substring, 1, BT_CHARACTER))
2607 return false;
2609 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2610 return false;
2612 if (!kind_check (kind, 3, BT_INTEGER))
2613 return false;
2614 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2615 "with KIND argument at %L",
2616 gfc_current_intrinsic, &kind->where))
2617 return false;
2619 if (string->ts.kind != substring->ts.kind)
2621 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2622 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2623 gfc_current_intrinsic, &substring->where,
2624 gfc_current_intrinsic_arg[0]->name);
2625 return false;
2628 return true;
2632 bool
2633 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2635 if (!numeric_check (x, 0))
2636 return false;
2638 if (!kind_check (kind, 1, BT_INTEGER))
2639 return false;
2641 return true;
2645 bool
2646 gfc_check_intconv (gfc_expr *x)
2648 if (!numeric_check (x, 0))
2649 return false;
2651 return true;
2655 bool
2656 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2658 if (!type_check (i, 0, BT_INTEGER))
2659 return false;
2661 if (!type_check (j, 1, BT_INTEGER))
2662 return false;
2664 if (i->ts.kind != j->ts.kind)
2666 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2667 &i->where))
2668 return false;
2671 return true;
2675 bool
2676 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2678 if (!type_check (i, 0, BT_INTEGER)
2679 || !type_check (shift, 1, BT_INTEGER))
2680 return false;
2682 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2683 return false;
2685 return true;
2689 bool
2690 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2692 if (!type_check (i, 0, BT_INTEGER)
2693 || !type_check (shift, 1, BT_INTEGER))
2694 return false;
2696 if (size != NULL)
2698 int i2, i3;
2700 if (!type_check (size, 2, BT_INTEGER))
2701 return false;
2703 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2704 return false;
2706 if (size->expr_type == EXPR_CONSTANT)
2708 gfc_extract_int (size, &i3);
2709 if (i3 <= 0)
2711 gfc_error ("SIZE at %L must be positive", &size->where);
2712 return false;
2715 if (shift->expr_type == EXPR_CONSTANT)
2717 gfc_extract_int (shift, &i2);
2718 if (i2 < 0)
2719 i2 = -i2;
2721 if (i2 > i3)
2723 gfc_error ("The absolute value of SHIFT at %L must be less "
2724 "than or equal to SIZE at %L", &shift->where,
2725 &size->where);
2726 return false;
2731 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2732 return false;
2734 return true;
2738 bool
2739 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2741 if (!type_check (pid, 0, BT_INTEGER))
2742 return false;
2744 if (!type_check (sig, 1, BT_INTEGER))
2745 return false;
2747 return true;
2751 bool
2752 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2754 if (!type_check (pid, 0, BT_INTEGER))
2755 return false;
2757 if (!scalar_check (pid, 0))
2758 return false;
2760 if (!type_check (sig, 1, BT_INTEGER))
2761 return false;
2763 if (!scalar_check (sig, 1))
2764 return false;
2766 if (status == NULL)
2767 return true;
2769 if (!type_check (status, 2, BT_INTEGER))
2770 return false;
2772 if (!scalar_check (status, 2))
2773 return false;
2775 return true;
2779 bool
2780 gfc_check_kind (gfc_expr *x)
2782 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2784 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2785 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2786 gfc_current_intrinsic, &x->where);
2787 return false;
2789 if (x->ts.type == BT_PROCEDURE)
2791 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2792 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2793 &x->where);
2794 return false;
2797 return true;
2801 bool
2802 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2804 if (!array_check (array, 0))
2805 return false;
2807 if (!dim_check (dim, 1, false))
2808 return false;
2810 if (!dim_rank_check (dim, array, 1))
2811 return false;
2813 if (!kind_check (kind, 2, BT_INTEGER))
2814 return false;
2815 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2816 "with KIND argument at %L",
2817 gfc_current_intrinsic, &kind->where))
2818 return false;
2820 return true;
2824 bool
2825 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2827 if (flag_coarray == GFC_FCOARRAY_NONE)
2829 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2830 return false;
2833 if (!coarray_check (coarray, 0))
2834 return false;
2836 if (dim != NULL)
2838 if (!dim_check (dim, 1, false))
2839 return false;
2841 if (!dim_corank_check (dim, coarray))
2842 return false;
2845 if (!kind_check (kind, 2, BT_INTEGER))
2846 return false;
2848 return true;
2852 bool
2853 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2855 if (!type_check (s, 0, BT_CHARACTER))
2856 return false;
2858 if (!kind_check (kind, 1, BT_INTEGER))
2859 return false;
2860 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2861 "with KIND argument at %L",
2862 gfc_current_intrinsic, &kind->where))
2863 return false;
2865 return true;
2869 bool
2870 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2872 if (!type_check (a, 0, BT_CHARACTER))
2873 return false;
2874 if (!kind_value_check (a, 0, gfc_default_character_kind))
2875 return false;
2877 if (!type_check (b, 1, BT_CHARACTER))
2878 return false;
2879 if (!kind_value_check (b, 1, gfc_default_character_kind))
2880 return false;
2882 return true;
2886 bool
2887 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2889 if (!type_check (path1, 0, BT_CHARACTER))
2890 return false;
2891 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2892 return false;
2894 if (!type_check (path2, 1, BT_CHARACTER))
2895 return false;
2896 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2897 return false;
2899 return true;
2903 bool
2904 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2906 if (!type_check (path1, 0, BT_CHARACTER))
2907 return false;
2908 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2909 return false;
2911 if (!type_check (path2, 1, BT_CHARACTER))
2912 return false;
2913 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2914 return false;
2916 if (status == NULL)
2917 return true;
2919 if (!type_check (status, 2, BT_INTEGER))
2920 return false;
2922 if (!scalar_check (status, 2))
2923 return false;
2925 return true;
2929 bool
2930 gfc_check_loc (gfc_expr *expr)
2932 return variable_check (expr, 0, true);
2936 bool
2937 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2939 if (!type_check (path1, 0, BT_CHARACTER))
2940 return false;
2941 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2942 return false;
2944 if (!type_check (path2, 1, BT_CHARACTER))
2945 return false;
2946 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2947 return false;
2949 return true;
2953 bool
2954 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2956 if (!type_check (path1, 0, BT_CHARACTER))
2957 return false;
2958 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2959 return false;
2961 if (!type_check (path2, 1, BT_CHARACTER))
2962 return false;
2963 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2964 return false;
2966 if (status == NULL)
2967 return true;
2969 if (!type_check (status, 2, BT_INTEGER))
2970 return false;
2972 if (!scalar_check (status, 2))
2973 return false;
2975 return true;
2979 bool
2980 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2982 if (!type_check (a, 0, BT_LOGICAL))
2983 return false;
2984 if (!kind_check (kind, 1, BT_LOGICAL))
2985 return false;
2987 return true;
2991 /* Min/max family. */
2993 static bool
2994 min_max_args (gfc_actual_arglist *args)
2996 gfc_actual_arglist *arg;
2997 int i, j, nargs, *nlabels, nlabelless;
2998 bool a1 = false, a2 = false;
3000 if (args == NULL || args->next == NULL)
3002 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
3003 gfc_current_intrinsic, gfc_current_intrinsic_where);
3004 return false;
3007 if (!args->name)
3008 a1 = true;
3010 if (!args->next->name)
3011 a2 = true;
3013 nargs = 0;
3014 for (arg = args; arg; arg = arg->next)
3015 if (arg->name)
3016 nargs++;
3018 if (nargs == 0)
3019 return true;
3021 /* Note: Having a keywordless argument after an "arg=" is checked before. */
3022 nlabelless = 0;
3023 nlabels = XALLOCAVEC (int, nargs);
3024 for (arg = args, i = 0; arg; arg = arg->next, i++)
3025 if (arg->name)
3027 int n;
3028 char *endp;
3030 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
3031 goto unknown;
3032 n = strtol (&arg->name[1], &endp, 10);
3033 if (endp[0] != '\0')
3034 goto unknown;
3035 if (n <= 0)
3036 goto unknown;
3037 if (n <= nlabelless)
3038 goto duplicate;
3039 nlabels[i] = n;
3040 if (n == 1)
3041 a1 = true;
3042 if (n == 2)
3043 a2 = true;
3045 else
3046 nlabelless++;
3048 if (!a1 || !a2)
3050 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
3051 !a1 ? "a1" : "a2", gfc_current_intrinsic,
3052 gfc_current_intrinsic_where);
3053 return false;
3056 /* Check for duplicates. */
3057 for (i = 0; i < nargs; i++)
3058 for (j = i + 1; j < nargs; j++)
3059 if (nlabels[i] == nlabels[j])
3060 goto duplicate;
3062 return true;
3064 duplicate:
3065 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
3066 &arg->expr->where, gfc_current_intrinsic);
3067 return false;
3069 unknown:
3070 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
3071 &arg->expr->where, gfc_current_intrinsic);
3072 return false;
3076 static bool
3077 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
3079 gfc_actual_arglist *arg, *tmp;
3080 gfc_expr *x;
3081 int m, n;
3083 if (!min_max_args (arglist))
3084 return false;
3086 for (arg = arglist, n=1; arg; arg = arg->next, n++)
3088 x = arg->expr;
3089 if (x->ts.type != type || x->ts.kind != kind)
3091 if (x->ts.type == type)
3093 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3094 "kinds at %L", &x->where))
3095 return false;
3097 else
3099 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3100 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3101 gfc_basic_typename (type), kind);
3102 return false;
3106 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3107 if (!gfc_check_conformance (tmp->expr, x,
3108 "arguments 'a%d' and 'a%d' for "
3109 "intrinsic '%s'", m, n,
3110 gfc_current_intrinsic))
3111 return false;
3114 return true;
3118 bool
3119 gfc_check_min_max (gfc_actual_arglist *arg)
3121 gfc_expr *x;
3123 if (!min_max_args (arg))
3124 return false;
3126 x = arg->expr;
3128 if (x->ts.type == BT_CHARACTER)
3130 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3131 "with CHARACTER argument at %L",
3132 gfc_current_intrinsic, &x->where))
3133 return false;
3135 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3137 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3138 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3139 return false;
3142 return check_rest (x->ts.type, x->ts.kind, arg);
3146 bool
3147 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3149 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3153 bool
3154 gfc_check_min_max_real (gfc_actual_arglist *arg)
3156 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3160 bool
3161 gfc_check_min_max_double (gfc_actual_arglist *arg)
3163 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3167 /* End of min/max family. */
3169 bool
3170 gfc_check_malloc (gfc_expr *size)
3172 if (!type_check (size, 0, BT_INTEGER))
3173 return false;
3175 if (!scalar_check (size, 0))
3176 return false;
3178 return true;
3182 bool
3183 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3185 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3187 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3188 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3189 gfc_current_intrinsic, &matrix_a->where);
3190 return false;
3193 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3195 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3196 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3197 gfc_current_intrinsic, &matrix_b->where);
3198 return false;
3201 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3202 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3204 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3205 gfc_current_intrinsic, &matrix_a->where,
3206 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3207 return false;
3210 switch (matrix_a->rank)
3212 case 1:
3213 if (!rank_check (matrix_b, 1, 2))
3214 return false;
3215 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3216 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3218 gfc_error ("Different shape on dimension 1 for arguments %qs "
3219 "and %qs at %L for intrinsic matmul",
3220 gfc_current_intrinsic_arg[0]->name,
3221 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3222 return false;
3224 break;
3226 case 2:
3227 if (matrix_b->rank != 2)
3229 if (!rank_check (matrix_b, 1, 1))
3230 return false;
3232 /* matrix_b has rank 1 or 2 here. Common check for the cases
3233 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3234 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3235 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3237 gfc_error ("Different shape on dimension 2 for argument %qs and "
3238 "dimension 1 for argument %qs at %L for intrinsic "
3239 "matmul", gfc_current_intrinsic_arg[0]->name,
3240 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3241 return false;
3243 break;
3245 default:
3246 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3247 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3248 gfc_current_intrinsic, &matrix_a->where);
3249 return false;
3252 return true;
3256 /* Whoever came up with this interface was probably on something.
3257 The possibilities for the occupation of the second and third
3258 parameters are:
3260 Arg #2 Arg #3
3261 NULL NULL
3262 DIM NULL
3263 MASK NULL
3264 NULL MASK minloc(array, mask=m)
3265 DIM MASK
3267 I.e. in the case of minloc(array,mask), mask will be in the second
3268 position of the argument list and we'll have to fix that up. Also,
3269 add the BACK argument if that isn't present. */
3271 bool
3272 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3274 gfc_expr *a, *m, *d, *k, *b;
3276 a = ap->expr;
3277 if (!int_or_real_or_char_check_f2003 (a, 0) || !array_check (a, 0))
3278 return false;
3280 d = ap->next->expr;
3281 m = ap->next->next->expr;
3282 k = ap->next->next->next->expr;
3283 b = ap->next->next->next->next->expr;
3285 if (b)
3287 if (!type_check (b, 4, BT_LOGICAL) || !scalar_check (b,4))
3288 return false;
3290 /* TODO: Remove this once BACK is actually implemented. */
3291 if (b->expr_type != EXPR_CONSTANT || b->value.logical != 0)
3293 gfc_error ("BACK argument to %qs intrinsic not yet "
3294 "implemented", gfc_current_intrinsic);
3295 return false;
3298 else
3300 b = gfc_get_logical_expr (gfc_default_logical_kind, NULL, 0);
3301 ap->next->next->next->next->expr = b;
3304 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3305 && ap->next->name == NULL)
3307 m = d;
3308 d = NULL;
3309 ap->next->expr = NULL;
3310 ap->next->next->expr = m;
3313 if (!dim_check (d, 1, false))
3314 return false;
3316 if (!dim_rank_check (d, a, 0))
3317 return false;
3319 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3320 return false;
3322 if (m != NULL
3323 && !gfc_check_conformance (a, m,
3324 "arguments '%s' and '%s' for intrinsic %s",
3325 gfc_current_intrinsic_arg[0]->name,
3326 gfc_current_intrinsic_arg[2]->name,
3327 gfc_current_intrinsic))
3328 return false;
3330 if (!kind_check (k, 1, BT_INTEGER))
3331 return false;
3333 return true;
3337 /* Similar to minloc/maxloc, the argument list might need to be
3338 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3339 difference is that MINLOC/MAXLOC take an additional KIND argument.
3340 The possibilities are:
3342 Arg #2 Arg #3
3343 NULL NULL
3344 DIM NULL
3345 MASK NULL
3346 NULL MASK minval(array, mask=m)
3347 DIM MASK
3349 I.e. in the case of minval(array,mask), mask will be in the second
3350 position of the argument list and we'll have to fix that up. */
3352 static bool
3353 check_reduction (gfc_actual_arglist *ap)
3355 gfc_expr *a, *m, *d;
3357 a = ap->expr;
3358 d = ap->next->expr;
3359 m = ap->next->next->expr;
3361 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3362 && ap->next->name == NULL)
3364 m = d;
3365 d = NULL;
3366 ap->next->expr = NULL;
3367 ap->next->next->expr = m;
3370 if (!dim_check (d, 1, false))
3371 return false;
3373 if (!dim_rank_check (d, a, 0))
3374 return false;
3376 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3377 return false;
3379 if (m != NULL
3380 && !gfc_check_conformance (a, m,
3381 "arguments '%s' and '%s' for intrinsic %s",
3382 gfc_current_intrinsic_arg[0]->name,
3383 gfc_current_intrinsic_arg[2]->name,
3384 gfc_current_intrinsic))
3385 return false;
3387 return true;
3391 bool
3392 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3394 if (!int_or_real_or_char_check_f2003 (ap->expr, 0)
3395 || !array_check (ap->expr, 0))
3396 return false;
3398 return check_reduction (ap);
3402 bool
3403 gfc_check_product_sum (gfc_actual_arglist *ap)
3405 if (!numeric_check (ap->expr, 0)
3406 || !array_check (ap->expr, 0))
3407 return false;
3409 return check_reduction (ap);
3413 /* For IANY, IALL and IPARITY. */
3415 bool
3416 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3418 int k;
3420 if (!type_check (i, 0, BT_INTEGER))
3421 return false;
3423 if (!nonnegative_check ("I", i))
3424 return false;
3426 if (!kind_check (kind, 1, BT_INTEGER))
3427 return false;
3429 if (kind)
3430 gfc_extract_int (kind, &k);
3431 else
3432 k = gfc_default_integer_kind;
3434 if (!less_than_bitsizekind ("I", i, k))
3435 return false;
3437 return true;
3441 bool
3442 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3444 if (ap->expr->ts.type != BT_INTEGER)
3446 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3447 gfc_current_intrinsic_arg[0]->name,
3448 gfc_current_intrinsic, &ap->expr->where);
3449 return false;
3452 if (!array_check (ap->expr, 0))
3453 return false;
3455 return check_reduction (ap);
3459 bool
3460 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3462 if (!same_type_check (tsource, 0, fsource, 1))
3463 return false;
3465 if (!type_check (mask, 2, BT_LOGICAL))
3466 return false;
3468 if (tsource->ts.type == BT_CHARACTER)
3469 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3471 return true;
3475 bool
3476 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3478 if (!type_check (i, 0, BT_INTEGER))
3479 return false;
3481 if (!type_check (j, 1, BT_INTEGER))
3482 return false;
3484 if (!type_check (mask, 2, BT_INTEGER))
3485 return false;
3487 if (!same_type_check (i, 0, j, 1))
3488 return false;
3490 if (!same_type_check (i, 0, mask, 2))
3491 return false;
3493 return true;
3497 bool
3498 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3500 if (!variable_check (from, 0, false))
3501 return false;
3502 if (!allocatable_check (from, 0))
3503 return false;
3504 if (gfc_is_coindexed (from))
3506 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3507 "coindexed", &from->where);
3508 return false;
3511 if (!variable_check (to, 1, false))
3512 return false;
3513 if (!allocatable_check (to, 1))
3514 return false;
3515 if (gfc_is_coindexed (to))
3517 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3518 "coindexed", &to->where);
3519 return false;
3522 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3524 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3525 "polymorphic if FROM is polymorphic",
3526 &to->where);
3527 return false;
3530 if (!same_type_check (to, 1, from, 0))
3531 return false;
3533 if (to->rank != from->rank)
3535 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3536 "must have the same rank %d/%d", &to->where, from->rank,
3537 to->rank);
3538 return false;
3541 /* IR F08/0040; cf. 12-006A. */
3542 if (gfc_get_corank (to) != gfc_get_corank (from))
3544 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3545 "must have the same corank %d/%d", &to->where,
3546 gfc_get_corank (from), gfc_get_corank (to));
3547 return false;
3550 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3551 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3552 and cmp2 are allocatable. After the allocation is transferred,
3553 the 'to' chain is broken by the nullification of the 'from'. A bit
3554 of reflection reveals that this can only occur for derived types
3555 with recursive allocatable components. */
3556 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3557 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3559 gfc_ref *to_ref, *from_ref;
3560 to_ref = to->ref;
3561 from_ref = from->ref;
3562 bool aliasing = true;
3564 for (; from_ref && to_ref;
3565 from_ref = from_ref->next, to_ref = to_ref->next)
3567 if (to_ref->type != from->ref->type)
3568 aliasing = false;
3569 else if (to_ref->type == REF_ARRAY
3570 && to_ref->u.ar.type != AR_FULL
3571 && from_ref->u.ar.type != AR_FULL)
3572 /* Play safe; assume sections and elements are different. */
3573 aliasing = false;
3574 else if (to_ref->type == REF_COMPONENT
3575 && to_ref->u.c.component != from_ref->u.c.component)
3576 aliasing = false;
3578 if (!aliasing)
3579 break;
3582 if (aliasing)
3584 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3585 "restrictions (F2003 12.4.1.7)", &to->where);
3586 return false;
3590 /* CLASS arguments: Make sure the vtab of from is present. */
3591 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3592 gfc_find_vtab (&from->ts);
3594 return true;
3598 bool
3599 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3601 if (!type_check (x, 0, BT_REAL))
3602 return false;
3604 if (!type_check (s, 1, BT_REAL))
3605 return false;
3607 if (s->expr_type == EXPR_CONSTANT)
3609 if (mpfr_sgn (s->value.real) == 0)
3611 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3612 &s->where);
3613 return false;
3617 return true;
3621 bool
3622 gfc_check_new_line (gfc_expr *a)
3624 if (!type_check (a, 0, BT_CHARACTER))
3625 return false;
3627 return true;
3631 bool
3632 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3634 if (!type_check (array, 0, BT_REAL))
3635 return false;
3637 if (!array_check (array, 0))
3638 return false;
3640 if (!dim_rank_check (dim, array, false))
3641 return false;
3643 return true;
3646 bool
3647 gfc_check_null (gfc_expr *mold)
3649 symbol_attribute attr;
3651 if (mold == NULL)
3652 return true;
3654 if (!variable_check (mold, 0, true))
3655 return false;
3657 attr = gfc_variable_attr (mold, NULL);
3659 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3661 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3662 "ALLOCATABLE or procedure pointer",
3663 gfc_current_intrinsic_arg[0]->name,
3664 gfc_current_intrinsic, &mold->where);
3665 return false;
3668 if (attr.allocatable
3669 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3670 "allocatable MOLD at %L", &mold->where))
3671 return false;
3673 /* F2008, C1242. */
3674 if (gfc_is_coindexed (mold))
3676 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3677 "coindexed", gfc_current_intrinsic_arg[0]->name,
3678 gfc_current_intrinsic, &mold->where);
3679 return false;
3682 return true;
3686 bool
3687 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3689 if (!array_check (array, 0))
3690 return false;
3692 if (!type_check (mask, 1, BT_LOGICAL))
3693 return false;
3695 if (!gfc_check_conformance (array, mask,
3696 "arguments '%s' and '%s' for intrinsic '%s'",
3697 gfc_current_intrinsic_arg[0]->name,
3698 gfc_current_intrinsic_arg[1]->name,
3699 gfc_current_intrinsic))
3700 return false;
3702 if (vector != NULL)
3704 mpz_t array_size, vector_size;
3705 bool have_array_size, have_vector_size;
3707 if (!same_type_check (array, 0, vector, 2))
3708 return false;
3710 if (!rank_check (vector, 2, 1))
3711 return false;
3713 /* VECTOR requires at least as many elements as MASK
3714 has .TRUE. values. */
3715 have_array_size = gfc_array_size(array, &array_size);
3716 have_vector_size = gfc_array_size(vector, &vector_size);
3718 if (have_vector_size
3719 && (mask->expr_type == EXPR_ARRAY
3720 || (mask->expr_type == EXPR_CONSTANT
3721 && have_array_size)))
3723 int mask_true_values = 0;
3725 if (mask->expr_type == EXPR_ARRAY)
3727 gfc_constructor *mask_ctor;
3728 mask_ctor = gfc_constructor_first (mask->value.constructor);
3729 while (mask_ctor)
3731 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3733 mask_true_values = 0;
3734 break;
3737 if (mask_ctor->expr->value.logical)
3738 mask_true_values++;
3740 mask_ctor = gfc_constructor_next (mask_ctor);
3743 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3744 mask_true_values = mpz_get_si (array_size);
3746 if (mpz_get_si (vector_size) < mask_true_values)
3748 gfc_error ("%qs argument of %qs intrinsic at %L must "
3749 "provide at least as many elements as there "
3750 "are .TRUE. values in %qs (%ld/%d)",
3751 gfc_current_intrinsic_arg[2]->name,
3752 gfc_current_intrinsic, &vector->where,
3753 gfc_current_intrinsic_arg[1]->name,
3754 mpz_get_si (vector_size), mask_true_values);
3755 return false;
3759 if (have_array_size)
3760 mpz_clear (array_size);
3761 if (have_vector_size)
3762 mpz_clear (vector_size);
3765 return true;
3769 bool
3770 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3772 if (!type_check (mask, 0, BT_LOGICAL))
3773 return false;
3775 if (!array_check (mask, 0))
3776 return false;
3778 if (!dim_rank_check (dim, mask, false))
3779 return false;
3781 return true;
3785 bool
3786 gfc_check_precision (gfc_expr *x)
3788 if (!real_or_complex_check (x, 0))
3789 return false;
3791 return true;
3795 bool
3796 gfc_check_present (gfc_expr *a)
3798 gfc_symbol *sym;
3800 if (!variable_check (a, 0, true))
3801 return false;
3803 sym = a->symtree->n.sym;
3804 if (!sym->attr.dummy)
3806 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3807 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3808 gfc_current_intrinsic, &a->where);
3809 return false;
3812 if (!sym->attr.optional)
3814 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3815 "an OPTIONAL dummy variable",
3816 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3817 &a->where);
3818 return false;
3821 /* 13.14.82 PRESENT(A)
3822 ......
3823 Argument. A shall be the name of an optional dummy argument that is
3824 accessible in the subprogram in which the PRESENT function reference
3825 appears... */
3827 if (a->ref != NULL
3828 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3829 && (a->ref->u.ar.type == AR_FULL
3830 || (a->ref->u.ar.type == AR_ELEMENT
3831 && a->ref->u.ar.as->rank == 0))))
3833 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3834 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3835 gfc_current_intrinsic, &a->where, sym->name);
3836 return false;
3839 return true;
3843 bool
3844 gfc_check_radix (gfc_expr *x)
3846 if (!int_or_real_check (x, 0))
3847 return false;
3849 return true;
3853 bool
3854 gfc_check_range (gfc_expr *x)
3856 if (!numeric_check (x, 0))
3857 return false;
3859 return true;
3863 bool
3864 gfc_check_rank (gfc_expr *a)
3866 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3867 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3869 bool is_variable = true;
3871 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3872 if (a->expr_type == EXPR_FUNCTION)
3873 is_variable = a->value.function.esym
3874 ? a->value.function.esym->result->attr.pointer
3875 : a->symtree->n.sym->result->attr.pointer;
3877 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3878 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3879 || !is_variable)
3881 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3882 "object", &a->where);
3883 return false;
3886 return true;
3890 /* real, float, sngl. */
3891 bool
3892 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3894 if (!numeric_check (a, 0))
3895 return false;
3897 if (!kind_check (kind, 1, BT_REAL))
3898 return false;
3900 return true;
3904 bool
3905 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3907 if (!type_check (path1, 0, BT_CHARACTER))
3908 return false;
3909 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3910 return false;
3912 if (!type_check (path2, 1, BT_CHARACTER))
3913 return false;
3914 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3915 return false;
3917 return true;
3921 bool
3922 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3924 if (!type_check (path1, 0, BT_CHARACTER))
3925 return false;
3926 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3927 return false;
3929 if (!type_check (path2, 1, BT_CHARACTER))
3930 return false;
3931 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3932 return false;
3934 if (status == NULL)
3935 return true;
3937 if (!type_check (status, 2, BT_INTEGER))
3938 return false;
3940 if (!scalar_check (status, 2))
3941 return false;
3943 return true;
3947 bool
3948 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3950 if (!type_check (x, 0, BT_CHARACTER))
3951 return false;
3953 if (!scalar_check (x, 0))
3954 return false;
3956 if (!type_check (y, 0, BT_INTEGER))
3957 return false;
3959 if (!scalar_check (y, 1))
3960 return false;
3962 return true;
3966 bool
3967 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3968 gfc_expr *pad, gfc_expr *order)
3970 mpz_t size;
3971 mpz_t nelems;
3972 int shape_size;
3974 if (!array_check (source, 0))
3975 return false;
3977 if (!rank_check (shape, 1, 1))
3978 return false;
3980 if (!type_check (shape, 1, BT_INTEGER))
3981 return false;
3983 if (!gfc_array_size (shape, &size))
3985 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3986 "array of constant size", &shape->where);
3987 return false;
3990 shape_size = mpz_get_ui (size);
3991 mpz_clear (size);
3993 if (shape_size <= 0)
3995 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3996 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3997 &shape->where);
3998 return false;
4000 else if (shape_size > GFC_MAX_DIMENSIONS)
4002 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
4003 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
4004 return false;
4006 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
4008 gfc_expr *e;
4009 int i, extent;
4010 for (i = 0; i < shape_size; ++i)
4012 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
4013 if (e->expr_type != EXPR_CONSTANT)
4014 continue;
4016 gfc_extract_int (e, &extent);
4017 if (extent < 0)
4019 gfc_error ("%qs argument of %qs intrinsic at %L has "
4020 "negative element (%d)",
4021 gfc_current_intrinsic_arg[1]->name,
4022 gfc_current_intrinsic, &e->where, extent);
4023 return false;
4027 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
4028 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
4029 && shape->ref->u.ar.as
4030 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
4031 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
4032 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
4033 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
4034 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
4036 int i, extent;
4037 gfc_expr *e, *v;
4039 v = shape->symtree->n.sym->value;
4041 for (i = 0; i < shape_size; i++)
4043 e = gfc_constructor_lookup_expr (v->value.constructor, i);
4044 if (e == NULL)
4045 break;
4047 gfc_extract_int (e, &extent);
4049 if (extent < 0)
4051 gfc_error ("Element %d of actual argument of RESHAPE at %L "
4052 "cannot be negative", i + 1, &shape->where);
4053 return false;
4058 if (pad != NULL)
4060 if (!same_type_check (source, 0, pad, 2))
4061 return false;
4063 if (!array_check (pad, 2))
4064 return false;
4067 if (order != NULL)
4069 if (!array_check (order, 3))
4070 return false;
4072 if (!type_check (order, 3, BT_INTEGER))
4073 return false;
4075 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
4077 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
4078 gfc_expr *e;
4080 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
4081 perm[i] = 0;
4083 gfc_array_size (order, &size);
4084 order_size = mpz_get_ui (size);
4085 mpz_clear (size);
4087 if (order_size != shape_size)
4089 gfc_error ("%qs argument of %qs intrinsic at %L "
4090 "has wrong number of elements (%d/%d)",
4091 gfc_current_intrinsic_arg[3]->name,
4092 gfc_current_intrinsic, &order->where,
4093 order_size, shape_size);
4094 return false;
4097 for (i = 1; i <= order_size; ++i)
4099 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
4100 if (e->expr_type != EXPR_CONSTANT)
4101 continue;
4103 gfc_extract_int (e, &dim);
4105 if (dim < 1 || dim > order_size)
4107 gfc_error ("%qs argument of %qs intrinsic at %L "
4108 "has out-of-range dimension (%d)",
4109 gfc_current_intrinsic_arg[3]->name,
4110 gfc_current_intrinsic, &e->where, dim);
4111 return false;
4114 if (perm[dim-1] != 0)
4116 gfc_error ("%qs argument of %qs intrinsic at %L has "
4117 "invalid permutation of dimensions (dimension "
4118 "%qd duplicated)",
4119 gfc_current_intrinsic_arg[3]->name,
4120 gfc_current_intrinsic, &e->where, dim);
4121 return false;
4124 perm[dim-1] = 1;
4129 if (pad == NULL && shape->expr_type == EXPR_ARRAY
4130 && gfc_is_constant_expr (shape)
4131 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4132 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4134 /* Check the match in size between source and destination. */
4135 if (gfc_array_size (source, &nelems))
4137 gfc_constructor *c;
4138 bool test;
4141 mpz_init_set_ui (size, 1);
4142 for (c = gfc_constructor_first (shape->value.constructor);
4143 c; c = gfc_constructor_next (c))
4144 mpz_mul (size, size, c->expr->value.integer);
4146 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4147 mpz_clear (nelems);
4148 mpz_clear (size);
4150 if (test)
4152 gfc_error ("Without padding, there are not enough elements "
4153 "in the intrinsic RESHAPE source at %L to match "
4154 "the shape", &source->where);
4155 return false;
4160 return true;
4164 bool
4165 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4167 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4169 gfc_error ("%qs argument of %qs intrinsic at %L "
4170 "cannot be of type %s",
4171 gfc_current_intrinsic_arg[0]->name,
4172 gfc_current_intrinsic,
4173 &a->where, gfc_typename (&a->ts));
4174 return false;
4177 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4179 gfc_error ("%qs argument of %qs intrinsic at %L "
4180 "must be of an extensible type",
4181 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4182 &a->where);
4183 return false;
4186 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4188 gfc_error ("%qs argument of %qs intrinsic at %L "
4189 "cannot be of type %s",
4190 gfc_current_intrinsic_arg[0]->name,
4191 gfc_current_intrinsic,
4192 &b->where, gfc_typename (&b->ts));
4193 return false;
4196 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4198 gfc_error ("%qs argument of %qs intrinsic at %L "
4199 "must be of an extensible type",
4200 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4201 &b->where);
4202 return false;
4205 return true;
4209 bool
4210 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4212 if (!type_check (x, 0, BT_REAL))
4213 return false;
4215 if (!type_check (i, 1, BT_INTEGER))
4216 return false;
4218 return true;
4222 bool
4223 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4225 if (!type_check (x, 0, BT_CHARACTER))
4226 return false;
4228 if (!type_check (y, 1, BT_CHARACTER))
4229 return false;
4231 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4232 return false;
4234 if (!kind_check (kind, 3, BT_INTEGER))
4235 return false;
4236 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4237 "with KIND argument at %L",
4238 gfc_current_intrinsic, &kind->where))
4239 return false;
4241 if (!same_type_check (x, 0, y, 1))
4242 return false;
4244 return true;
4248 bool
4249 gfc_check_secnds (gfc_expr *r)
4251 if (!type_check (r, 0, BT_REAL))
4252 return false;
4254 if (!kind_value_check (r, 0, 4))
4255 return false;
4257 if (!scalar_check (r, 0))
4258 return false;
4260 return true;
4264 bool
4265 gfc_check_selected_char_kind (gfc_expr *name)
4267 if (!type_check (name, 0, BT_CHARACTER))
4268 return false;
4270 if (!kind_value_check (name, 0, gfc_default_character_kind))
4271 return false;
4273 if (!scalar_check (name, 0))
4274 return false;
4276 return true;
4280 bool
4281 gfc_check_selected_int_kind (gfc_expr *r)
4283 if (!type_check (r, 0, BT_INTEGER))
4284 return false;
4286 if (!scalar_check (r, 0))
4287 return false;
4289 return true;
4293 bool
4294 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4296 if (p == NULL && r == NULL
4297 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4298 " neither %<P%> nor %<R%> argument at %L",
4299 gfc_current_intrinsic_where))
4300 return false;
4302 if (p)
4304 if (!type_check (p, 0, BT_INTEGER))
4305 return false;
4307 if (!scalar_check (p, 0))
4308 return false;
4311 if (r)
4313 if (!type_check (r, 1, BT_INTEGER))
4314 return false;
4316 if (!scalar_check (r, 1))
4317 return false;
4320 if (radix)
4322 if (!type_check (radix, 1, BT_INTEGER))
4323 return false;
4325 if (!scalar_check (radix, 1))
4326 return false;
4328 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4329 "RADIX argument at %L", gfc_current_intrinsic,
4330 &radix->where))
4331 return false;
4334 return true;
4338 bool
4339 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4341 if (!type_check (x, 0, BT_REAL))
4342 return false;
4344 if (!type_check (i, 1, BT_INTEGER))
4345 return false;
4347 return true;
4351 bool
4352 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4354 gfc_array_ref *ar;
4356 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4357 return true;
4359 ar = gfc_find_array_ref (source);
4361 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4363 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4364 "an assumed size array", &source->where);
4365 return false;
4368 if (!kind_check (kind, 1, BT_INTEGER))
4369 return false;
4370 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4371 "with KIND argument at %L",
4372 gfc_current_intrinsic, &kind->where))
4373 return false;
4375 return true;
4379 bool
4380 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4382 if (!type_check (i, 0, BT_INTEGER))
4383 return false;
4385 if (!type_check (shift, 0, BT_INTEGER))
4386 return false;
4388 if (!nonnegative_check ("SHIFT", shift))
4389 return false;
4391 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4392 return false;
4394 return true;
4398 bool
4399 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4401 if (!int_or_real_check (a, 0))
4402 return false;
4404 if (!same_type_check (a, 0, b, 1))
4405 return false;
4407 return true;
4411 bool
4412 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4414 if (!array_check (array, 0))
4415 return false;
4417 if (!dim_check (dim, 1, true))
4418 return false;
4420 if (!dim_rank_check (dim, array, 0))
4421 return false;
4423 if (!kind_check (kind, 2, BT_INTEGER))
4424 return false;
4425 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4426 "with KIND argument at %L",
4427 gfc_current_intrinsic, &kind->where))
4428 return false;
4431 return true;
4435 bool
4436 gfc_check_sizeof (gfc_expr *arg)
4438 if (arg->ts.type == BT_PROCEDURE)
4440 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4441 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4442 &arg->where);
4443 return false;
4446 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4447 if (arg->ts.type == BT_ASSUMED
4448 && (arg->symtree->n.sym->as == NULL
4449 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4450 && arg->symtree->n.sym->as->type != AS_DEFERRED
4451 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4453 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4454 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4455 &arg->where);
4456 return false;
4459 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4460 && arg->symtree->n.sym->as != NULL
4461 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4462 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4464 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4465 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4466 gfc_current_intrinsic, &arg->where);
4467 return false;
4470 return true;
4474 /* Check whether an expression is interoperable. When returning false,
4475 msg is set to a string telling why the expression is not interoperable,
4476 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4477 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4478 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4479 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4480 are permitted. */
4482 static bool
4483 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4485 *msg = NULL;
4487 if (expr->ts.type == BT_CLASS)
4489 *msg = "Expression is polymorphic";
4490 return false;
4493 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4494 && !expr->ts.u.derived->ts.is_iso_c)
4496 *msg = "Expression is a noninteroperable derived type";
4497 return false;
4500 if (expr->ts.type == BT_PROCEDURE)
4502 *msg = "Procedure unexpected as argument";
4503 return false;
4506 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4508 int i;
4509 for (i = 0; gfc_logical_kinds[i].kind; i++)
4510 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4511 return true;
4512 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4513 return false;
4516 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4517 && expr->ts.kind != 1)
4519 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4520 return false;
4523 if (expr->ts.type == BT_CHARACTER) {
4524 if (expr->ts.deferred)
4526 /* TS 29113 allows deferred-length strings as dummy arguments,
4527 but it is not an interoperable type. */
4528 *msg = "Expression shall not be a deferred-length string";
4529 return false;
4532 if (expr->ts.u.cl && expr->ts.u.cl->length
4533 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4534 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4536 if (!c_loc && expr->ts.u.cl
4537 && (!expr->ts.u.cl->length
4538 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4539 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4541 *msg = "Type shall have a character length of 1";
4542 return false;
4546 /* Note: The following checks are about interoperatable variables, Fortran
4547 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4548 is allowed, e.g. assumed-shape arrays with TS 29113. */
4550 if (gfc_is_coarray (expr))
4552 *msg = "Coarrays are not interoperable";
4553 return false;
4556 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4558 gfc_array_ref *ar = gfc_find_array_ref (expr);
4559 if (ar->type != AR_FULL)
4561 *msg = "Only whole-arrays are interoperable";
4562 return false;
4564 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4565 && ar->as->type != AS_ASSUMED_SIZE)
4567 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4568 return false;
4572 return true;
4576 bool
4577 gfc_check_c_sizeof (gfc_expr *arg)
4579 const char *msg;
4581 if (!is_c_interoperable (arg, &msg, false, false))
4583 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4584 "interoperable data entity: %s",
4585 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4586 &arg->where, msg);
4587 return false;
4590 if (arg->ts.type == BT_ASSUMED)
4592 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4593 "TYPE(*)",
4594 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4595 &arg->where);
4596 return false;
4599 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4600 && arg->symtree->n.sym->as != NULL
4601 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4602 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4604 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4605 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4606 gfc_current_intrinsic, &arg->where);
4607 return false;
4610 return true;
4614 bool
4615 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4617 if (c_ptr_1->ts.type != BT_DERIVED
4618 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4619 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4620 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4622 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4623 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4624 return false;
4627 if (!scalar_check (c_ptr_1, 0))
4628 return false;
4630 if (c_ptr_2
4631 && (c_ptr_2->ts.type != BT_DERIVED
4632 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4633 || (c_ptr_1->ts.u.derived->intmod_sym_id
4634 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4636 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4637 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4638 gfc_typename (&c_ptr_1->ts),
4639 gfc_typename (&c_ptr_2->ts));
4640 return false;
4643 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4644 return false;
4646 return true;
4650 bool
4651 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4653 symbol_attribute attr;
4654 const char *msg;
4656 if (cptr->ts.type != BT_DERIVED
4657 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4658 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4660 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4661 "type TYPE(C_PTR)", &cptr->where);
4662 return false;
4665 if (!scalar_check (cptr, 0))
4666 return false;
4668 attr = gfc_expr_attr (fptr);
4670 if (!attr.pointer)
4672 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4673 &fptr->where);
4674 return false;
4677 if (fptr->ts.type == BT_CLASS)
4679 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4680 &fptr->where);
4681 return false;
4684 if (gfc_is_coindexed (fptr))
4686 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4687 "coindexed", &fptr->where);
4688 return false;
4691 if (fptr->rank == 0 && shape)
4693 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4694 "FPTR", &fptr->where);
4695 return false;
4697 else if (fptr->rank && !shape)
4699 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4700 "FPTR at %L", &fptr->where);
4701 return false;
4704 if (shape && !rank_check (shape, 2, 1))
4705 return false;
4707 if (shape && !type_check (shape, 2, BT_INTEGER))
4708 return false;
4710 if (shape)
4712 mpz_t size;
4713 if (gfc_array_size (shape, &size))
4715 if (mpz_cmp_ui (size, fptr->rank) != 0)
4717 mpz_clear (size);
4718 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4719 "size as the RANK of FPTR", &shape->where);
4720 return false;
4722 mpz_clear (size);
4726 if (fptr->ts.type == BT_CLASS)
4728 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4729 return false;
4732 if (!is_c_interoperable (fptr, &msg, false, true))
4733 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4734 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4736 return true;
4740 bool
4741 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4743 symbol_attribute attr;
4745 if (cptr->ts.type != BT_DERIVED
4746 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4747 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4749 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4750 "type TYPE(C_FUNPTR)", &cptr->where);
4751 return false;
4754 if (!scalar_check (cptr, 0))
4755 return false;
4757 attr = gfc_expr_attr (fptr);
4759 if (!attr.proc_pointer)
4761 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4762 "pointer", &fptr->where);
4763 return false;
4766 if (gfc_is_coindexed (fptr))
4768 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4769 "coindexed", &fptr->where);
4770 return false;
4773 if (!attr.is_bind_c)
4774 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4775 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4777 return true;
4781 bool
4782 gfc_check_c_funloc (gfc_expr *x)
4784 symbol_attribute attr;
4786 if (gfc_is_coindexed (x))
4788 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4789 "coindexed", &x->where);
4790 return false;
4793 attr = gfc_expr_attr (x);
4795 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4796 && x->symtree->n.sym == x->symtree->n.sym->result)
4798 gfc_namespace *ns = gfc_current_ns;
4800 for (ns = gfc_current_ns; ns; ns = ns->parent)
4801 if (x->symtree->n.sym == ns->proc_name)
4803 gfc_error ("Function result %qs at %L is invalid as X argument "
4804 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4805 return false;
4809 if (attr.flavor != FL_PROCEDURE)
4811 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4812 "or a procedure pointer", &x->where);
4813 return false;
4816 if (!attr.is_bind_c)
4817 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4818 "at %L to C_FUNLOC", &x->where);
4819 return true;
4823 bool
4824 gfc_check_c_loc (gfc_expr *x)
4826 symbol_attribute attr;
4827 const char *msg;
4829 if (gfc_is_coindexed (x))
4831 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4832 return false;
4835 if (x->ts.type == BT_CLASS)
4837 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4838 &x->where);
4839 return false;
4842 attr = gfc_expr_attr (x);
4844 if (!attr.pointer
4845 && (x->expr_type != EXPR_VARIABLE || !attr.target
4846 || attr.flavor == FL_PARAMETER))
4848 gfc_error ("Argument X at %L to C_LOC shall have either "
4849 "the POINTER or the TARGET attribute", &x->where);
4850 return false;
4853 if (x->ts.type == BT_CHARACTER
4854 && gfc_var_strlen (x) == 0)
4856 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4857 "string", &x->where);
4858 return false;
4861 if (!is_c_interoperable (x, &msg, true, false))
4863 if (x->ts.type == BT_CLASS)
4865 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4866 &x->where);
4867 return false;
4870 if (x->rank
4871 && !gfc_notify_std (GFC_STD_F2008_TS,
4872 "Noninteroperable array at %L as"
4873 " argument to C_LOC: %s", &x->where, msg))
4874 return false;
4876 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4878 gfc_array_ref *ar = gfc_find_array_ref (x);
4880 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4881 && !attr.allocatable
4882 && !gfc_notify_std (GFC_STD_F2008,
4883 "Array of interoperable type at %L "
4884 "to C_LOC which is nonallocatable and neither "
4885 "assumed size nor explicit size", &x->where))
4886 return false;
4887 else if (ar->type != AR_FULL
4888 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4889 "to C_LOC", &x->where))
4890 return false;
4893 return true;
4897 bool
4898 gfc_check_sleep_sub (gfc_expr *seconds)
4900 if (!type_check (seconds, 0, BT_INTEGER))
4901 return false;
4903 if (!scalar_check (seconds, 0))
4904 return false;
4906 return true;
4909 bool
4910 gfc_check_sngl (gfc_expr *a)
4912 if (!type_check (a, 0, BT_REAL))
4913 return false;
4915 if ((a->ts.kind != gfc_default_double_kind)
4916 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4917 "REAL argument to %s intrinsic at %L",
4918 gfc_current_intrinsic, &a->where))
4919 return false;
4921 return true;
4924 bool
4925 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4927 if (source->rank >= GFC_MAX_DIMENSIONS)
4929 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4930 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4931 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4933 return false;
4936 if (dim == NULL)
4937 return false;
4939 if (!dim_check (dim, 1, false))
4940 return false;
4942 /* dim_rank_check() does not apply here. */
4943 if (dim
4944 && dim->expr_type == EXPR_CONSTANT
4945 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4946 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4948 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4949 "dimension index", gfc_current_intrinsic_arg[1]->name,
4950 gfc_current_intrinsic, &dim->where);
4951 return false;
4954 if (!type_check (ncopies, 2, BT_INTEGER))
4955 return false;
4957 if (!scalar_check (ncopies, 2))
4958 return false;
4960 return true;
4964 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4965 functions). */
4967 bool
4968 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4970 if (!type_check (unit, 0, BT_INTEGER))
4971 return false;
4973 if (!scalar_check (unit, 0))
4974 return false;
4976 if (!type_check (c, 1, BT_CHARACTER))
4977 return false;
4978 if (!kind_value_check (c, 1, gfc_default_character_kind))
4979 return false;
4981 if (status == NULL)
4982 return true;
4984 if (!type_check (status, 2, BT_INTEGER)
4985 || !kind_value_check (status, 2, gfc_default_integer_kind)
4986 || !scalar_check (status, 2))
4987 return false;
4989 return true;
4993 bool
4994 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4996 return gfc_check_fgetputc_sub (unit, c, NULL);
5000 bool
5001 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
5003 if (!type_check (c, 0, BT_CHARACTER))
5004 return false;
5005 if (!kind_value_check (c, 0, gfc_default_character_kind))
5006 return false;
5008 if (status == NULL)
5009 return true;
5011 if (!type_check (status, 1, BT_INTEGER)
5012 || !kind_value_check (status, 1, gfc_default_integer_kind)
5013 || !scalar_check (status, 1))
5014 return false;
5016 return true;
5020 bool
5021 gfc_check_fgetput (gfc_expr *c)
5023 return gfc_check_fgetput_sub (c, NULL);
5027 bool
5028 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
5030 if (!type_check (unit, 0, BT_INTEGER))
5031 return false;
5033 if (!scalar_check (unit, 0))
5034 return false;
5036 if (!type_check (offset, 1, BT_INTEGER))
5037 return false;
5039 if (!scalar_check (offset, 1))
5040 return false;
5042 if (!type_check (whence, 2, BT_INTEGER))
5043 return false;
5045 if (!scalar_check (whence, 2))
5046 return false;
5048 if (status == NULL)
5049 return true;
5051 if (!type_check (status, 3, BT_INTEGER))
5052 return false;
5054 if (!kind_value_check (status, 3, 4))
5055 return false;
5057 if (!scalar_check (status, 3))
5058 return false;
5060 return true;
5065 bool
5066 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
5068 if (!type_check (unit, 0, BT_INTEGER))
5069 return false;
5071 if (!scalar_check (unit, 0))
5072 return false;
5074 if (!type_check (array, 1, BT_INTEGER)
5075 || !kind_value_check (unit, 0, gfc_default_integer_kind))
5076 return false;
5078 if (!array_check (array, 1))
5079 return false;
5081 return true;
5085 bool
5086 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
5088 if (!type_check (unit, 0, BT_INTEGER))
5089 return false;
5091 if (!scalar_check (unit, 0))
5092 return false;
5094 if (!type_check (array, 1, BT_INTEGER)
5095 || !kind_value_check (array, 1, gfc_default_integer_kind))
5096 return false;
5098 if (!array_check (array, 1))
5099 return false;
5101 if (status == NULL)
5102 return true;
5104 if (!type_check (status, 2, BT_INTEGER)
5105 || !kind_value_check (status, 2, gfc_default_integer_kind))
5106 return false;
5108 if (!scalar_check (status, 2))
5109 return false;
5111 return true;
5115 bool
5116 gfc_check_ftell (gfc_expr *unit)
5118 if (!type_check (unit, 0, BT_INTEGER))
5119 return false;
5121 if (!scalar_check (unit, 0))
5122 return false;
5124 return true;
5128 bool
5129 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5131 if (!type_check (unit, 0, BT_INTEGER))
5132 return false;
5134 if (!scalar_check (unit, 0))
5135 return false;
5137 if (!type_check (offset, 1, BT_INTEGER))
5138 return false;
5140 if (!scalar_check (offset, 1))
5141 return false;
5143 return true;
5147 bool
5148 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5150 if (!type_check (name, 0, BT_CHARACTER))
5151 return false;
5152 if (!kind_value_check (name, 0, gfc_default_character_kind))
5153 return false;
5155 if (!type_check (array, 1, BT_INTEGER)
5156 || !kind_value_check (array, 1, gfc_default_integer_kind))
5157 return false;
5159 if (!array_check (array, 1))
5160 return false;
5162 return true;
5166 bool
5167 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5169 if (!type_check (name, 0, BT_CHARACTER))
5170 return false;
5171 if (!kind_value_check (name, 0, gfc_default_character_kind))
5172 return false;
5174 if (!type_check (array, 1, BT_INTEGER)
5175 || !kind_value_check (array, 1, gfc_default_integer_kind))
5176 return false;
5178 if (!array_check (array, 1))
5179 return false;
5181 if (status == NULL)
5182 return true;
5184 if (!type_check (status, 2, BT_INTEGER)
5185 || !kind_value_check (array, 1, gfc_default_integer_kind))
5186 return false;
5188 if (!scalar_check (status, 2))
5189 return false;
5191 return true;
5195 bool
5196 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5198 mpz_t nelems;
5200 if (flag_coarray == GFC_FCOARRAY_NONE)
5202 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5203 return false;
5206 if (!coarray_check (coarray, 0))
5207 return false;
5209 if (sub->rank != 1)
5211 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5212 gfc_current_intrinsic_arg[1]->name, &sub->where);
5213 return false;
5216 if (gfc_array_size (sub, &nelems))
5218 int corank = gfc_get_corank (coarray);
5220 if (mpz_cmp_ui (nelems, corank) != 0)
5222 gfc_error ("The number of array elements of the SUB argument to "
5223 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5224 &sub->where, corank, (int) mpz_get_si (nelems));
5225 mpz_clear (nelems);
5226 return false;
5228 mpz_clear (nelems);
5231 return true;
5235 bool
5236 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5238 if (flag_coarray == GFC_FCOARRAY_NONE)
5240 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5241 return false;
5244 if (distance)
5246 if (!type_check (distance, 0, BT_INTEGER))
5247 return false;
5249 if (!nonnegative_check ("DISTANCE", distance))
5250 return false;
5252 if (!scalar_check (distance, 0))
5253 return false;
5255 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5256 "NUM_IMAGES at %L", &distance->where))
5257 return false;
5260 if (failed)
5262 if (!type_check (failed, 1, BT_LOGICAL))
5263 return false;
5265 if (!scalar_check (failed, 1))
5266 return false;
5268 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
5269 "NUM_IMAGES at %L", &failed->where))
5270 return false;
5273 return true;
5277 bool
5278 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5280 if (flag_coarray == GFC_FCOARRAY_NONE)
5282 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5283 return false;
5286 if (coarray == NULL && dim == NULL && distance == NULL)
5287 return true;
5289 if (dim != NULL && coarray == NULL)
5291 gfc_error ("DIM argument without COARRAY argument not allowed for "
5292 "THIS_IMAGE intrinsic at %L", &dim->where);
5293 return false;
5296 if (distance && (coarray || dim))
5298 gfc_error ("The DISTANCE argument may not be specified together with the "
5299 "COARRAY or DIM argument in intrinsic at %L",
5300 &distance->where);
5301 return false;
5304 /* Assume that we have "this_image (distance)". */
5305 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5307 if (dim)
5309 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5310 &coarray->where);
5311 return false;
5313 distance = coarray;
5316 if (distance)
5318 if (!type_check (distance, 2, BT_INTEGER))
5319 return false;
5321 if (!nonnegative_check ("DISTANCE", distance))
5322 return false;
5324 if (!scalar_check (distance, 2))
5325 return false;
5327 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5328 "THIS_IMAGE at %L", &distance->where))
5329 return false;
5331 return true;
5334 if (!coarray_check (coarray, 0))
5335 return false;
5337 if (dim != NULL)
5339 if (!dim_check (dim, 1, false))
5340 return false;
5342 if (!dim_corank_check (dim, coarray))
5343 return false;
5346 return true;
5349 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5350 by gfc_simplify_transfer. Return false if we cannot do so. */
5352 bool
5353 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5354 size_t *source_size, size_t *result_size,
5355 size_t *result_length_p)
5357 size_t result_elt_size;
5359 if (source->expr_type == EXPR_FUNCTION)
5360 return false;
5362 if (size && size->expr_type != EXPR_CONSTANT)
5363 return false;
5365 /* Calculate the size of the source. */
5366 *source_size = gfc_target_expr_size (source);
5367 if (*source_size == 0)
5368 return false;
5370 /* Determine the size of the element. */
5371 result_elt_size = gfc_element_size (mold);
5372 if (result_elt_size == 0)
5373 return false;
5375 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5377 int result_length;
5379 if (size)
5380 result_length = (size_t)mpz_get_ui (size->value.integer);
5381 else
5383 result_length = *source_size / result_elt_size;
5384 if (result_length * result_elt_size < *source_size)
5385 result_length += 1;
5388 *result_size = result_length * result_elt_size;
5389 if (result_length_p)
5390 *result_length_p = result_length;
5392 else
5393 *result_size = result_elt_size;
5395 return true;
5399 bool
5400 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5402 size_t source_size;
5403 size_t result_size;
5405 if (mold->ts.type == BT_HOLLERITH)
5407 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5408 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5409 return false;
5412 if (size != NULL)
5414 if (!type_check (size, 2, BT_INTEGER))
5415 return false;
5417 if (!scalar_check (size, 2))
5418 return false;
5420 if (!nonoptional_check (size, 2))
5421 return false;
5424 if (!warn_surprising)
5425 return true;
5427 /* If we can't calculate the sizes, we cannot check any more.
5428 Return true for that case. */
5430 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5431 &result_size, NULL))
5432 return true;
5434 if (source_size < result_size)
5435 gfc_warning (OPT_Wsurprising,
5436 "Intrinsic TRANSFER at %L has partly undefined result: "
5437 "source size %ld < result size %ld", &source->where,
5438 (long) source_size, (long) result_size);
5440 return true;
5444 bool
5445 gfc_check_transpose (gfc_expr *matrix)
5447 if (!rank_check (matrix, 0, 2))
5448 return false;
5450 return true;
5454 bool
5455 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5457 if (!array_check (array, 0))
5458 return false;
5460 if (!dim_check (dim, 1, false))
5461 return false;
5463 if (!dim_rank_check (dim, array, 0))
5464 return false;
5466 if (!kind_check (kind, 2, BT_INTEGER))
5467 return false;
5468 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5469 "with KIND argument at %L",
5470 gfc_current_intrinsic, &kind->where))
5471 return false;
5473 return true;
5477 bool
5478 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5480 if (flag_coarray == GFC_FCOARRAY_NONE)
5482 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5483 return false;
5486 if (!coarray_check (coarray, 0))
5487 return false;
5489 if (dim != NULL)
5491 if (!dim_check (dim, 1, false))
5492 return false;
5494 if (!dim_corank_check (dim, coarray))
5495 return false;
5498 if (!kind_check (kind, 2, BT_INTEGER))
5499 return false;
5501 return true;
5505 bool
5506 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5508 mpz_t vector_size;
5510 if (!rank_check (vector, 0, 1))
5511 return false;
5513 if (!array_check (mask, 1))
5514 return false;
5516 if (!type_check (mask, 1, BT_LOGICAL))
5517 return false;
5519 if (!same_type_check (vector, 0, field, 2))
5520 return false;
5522 if (mask->expr_type == EXPR_ARRAY
5523 && gfc_array_size (vector, &vector_size))
5525 int mask_true_count = 0;
5526 gfc_constructor *mask_ctor;
5527 mask_ctor = gfc_constructor_first (mask->value.constructor);
5528 while (mask_ctor)
5530 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5532 mask_true_count = 0;
5533 break;
5536 if (mask_ctor->expr->value.logical)
5537 mask_true_count++;
5539 mask_ctor = gfc_constructor_next (mask_ctor);
5542 if (mpz_get_si (vector_size) < mask_true_count)
5544 gfc_error ("%qs argument of %qs intrinsic at %L must "
5545 "provide at least as many elements as there "
5546 "are .TRUE. values in %qs (%ld/%d)",
5547 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5548 &vector->where, gfc_current_intrinsic_arg[1]->name,
5549 mpz_get_si (vector_size), mask_true_count);
5550 return false;
5553 mpz_clear (vector_size);
5556 if (mask->rank != field->rank && field->rank != 0)
5558 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5559 "the same rank as %qs or be a scalar",
5560 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5561 &field->where, gfc_current_intrinsic_arg[1]->name);
5562 return false;
5565 if (mask->rank == field->rank)
5567 int i;
5568 for (i = 0; i < field->rank; i++)
5569 if (! identical_dimen_shape (mask, i, field, i))
5571 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5572 "must have identical shape.",
5573 gfc_current_intrinsic_arg[2]->name,
5574 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5575 &field->where);
5579 return true;
5583 bool
5584 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5586 if (!type_check (x, 0, BT_CHARACTER))
5587 return false;
5589 if (!same_type_check (x, 0, y, 1))
5590 return false;
5592 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5593 return false;
5595 if (!kind_check (kind, 3, BT_INTEGER))
5596 return false;
5597 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5598 "with KIND argument at %L",
5599 gfc_current_intrinsic, &kind->where))
5600 return false;
5602 return true;
5606 bool
5607 gfc_check_trim (gfc_expr *x)
5609 if (!type_check (x, 0, BT_CHARACTER))
5610 return false;
5612 if (!scalar_check (x, 0))
5613 return false;
5615 return true;
5619 bool
5620 gfc_check_ttynam (gfc_expr *unit)
5622 if (!scalar_check (unit, 0))
5623 return false;
5625 if (!type_check (unit, 0, BT_INTEGER))
5626 return false;
5628 return true;
5632 /************* Check functions for intrinsic subroutines *************/
5634 bool
5635 gfc_check_cpu_time (gfc_expr *time)
5637 if (!scalar_check (time, 0))
5638 return false;
5640 if (!type_check (time, 0, BT_REAL))
5641 return false;
5643 if (!variable_check (time, 0, false))
5644 return false;
5646 return true;
5650 bool
5651 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5652 gfc_expr *zone, gfc_expr *values)
5654 if (date != NULL)
5656 if (!type_check (date, 0, BT_CHARACTER))
5657 return false;
5658 if (!kind_value_check (date, 0, gfc_default_character_kind))
5659 return false;
5660 if (!scalar_check (date, 0))
5661 return false;
5662 if (!variable_check (date, 0, false))
5663 return false;
5666 if (time != NULL)
5668 if (!type_check (time, 1, BT_CHARACTER))
5669 return false;
5670 if (!kind_value_check (time, 1, gfc_default_character_kind))
5671 return false;
5672 if (!scalar_check (time, 1))
5673 return false;
5674 if (!variable_check (time, 1, false))
5675 return false;
5678 if (zone != NULL)
5680 if (!type_check (zone, 2, BT_CHARACTER))
5681 return false;
5682 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5683 return false;
5684 if (!scalar_check (zone, 2))
5685 return false;
5686 if (!variable_check (zone, 2, false))
5687 return false;
5690 if (values != NULL)
5692 if (!type_check (values, 3, BT_INTEGER))
5693 return false;
5694 if (!array_check (values, 3))
5695 return false;
5696 if (!rank_check (values, 3, 1))
5697 return false;
5698 if (!variable_check (values, 3, false))
5699 return false;
5702 return true;
5706 bool
5707 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5708 gfc_expr *to, gfc_expr *topos)
5710 if (!type_check (from, 0, BT_INTEGER))
5711 return false;
5713 if (!type_check (frompos, 1, BT_INTEGER))
5714 return false;
5716 if (!type_check (len, 2, BT_INTEGER))
5717 return false;
5719 if (!same_type_check (from, 0, to, 3))
5720 return false;
5722 if (!variable_check (to, 3, false))
5723 return false;
5725 if (!type_check (topos, 4, BT_INTEGER))
5726 return false;
5728 if (!nonnegative_check ("frompos", frompos))
5729 return false;
5731 if (!nonnegative_check ("topos", topos))
5732 return false;
5734 if (!nonnegative_check ("len", len))
5735 return false;
5737 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5738 return false;
5740 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5741 return false;
5743 return true;
5747 bool
5748 gfc_check_random_number (gfc_expr *harvest)
5750 if (!type_check (harvest, 0, BT_REAL))
5751 return false;
5753 if (!variable_check (harvest, 0, false))
5754 return false;
5756 return true;
5760 bool
5761 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5763 unsigned int nargs = 0, seed_size;
5764 locus *where = NULL;
5765 mpz_t put_size, get_size;
5767 /* Keep the number of bytes in sync with master_state in
5768 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5769 part of the state too. */
5770 seed_size = 128 / gfc_default_integer_kind + 1;
5772 if (size != NULL)
5774 if (size->expr_type != EXPR_VARIABLE
5775 || !size->symtree->n.sym->attr.optional)
5776 nargs++;
5778 if (!scalar_check (size, 0))
5779 return false;
5781 if (!type_check (size, 0, BT_INTEGER))
5782 return false;
5784 if (!variable_check (size, 0, false))
5785 return false;
5787 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5788 return false;
5791 if (put != NULL)
5793 if (put->expr_type != EXPR_VARIABLE
5794 || !put->symtree->n.sym->attr.optional)
5796 nargs++;
5797 where = &put->where;
5800 if (!array_check (put, 1))
5801 return false;
5803 if (!rank_check (put, 1, 1))
5804 return false;
5806 if (!type_check (put, 1, BT_INTEGER))
5807 return false;
5809 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5810 return false;
5812 if (gfc_array_size (put, &put_size)
5813 && mpz_get_ui (put_size) < seed_size)
5814 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5815 "too small (%i/%i)",
5816 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5817 where, (int) mpz_get_ui (put_size), seed_size);
5820 if (get != NULL)
5822 if (get->expr_type != EXPR_VARIABLE
5823 || !get->symtree->n.sym->attr.optional)
5825 nargs++;
5826 where = &get->where;
5829 if (!array_check (get, 2))
5830 return false;
5832 if (!rank_check (get, 2, 1))
5833 return false;
5835 if (!type_check (get, 2, BT_INTEGER))
5836 return false;
5838 if (!variable_check (get, 2, false))
5839 return false;
5841 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5842 return false;
5844 if (gfc_array_size (get, &get_size)
5845 && mpz_get_ui (get_size) < seed_size)
5846 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5847 "too small (%i/%i)",
5848 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5849 where, (int) mpz_get_ui (get_size), seed_size);
5852 /* RANDOM_SEED may not have more than one non-optional argument. */
5853 if (nargs > 1)
5854 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5856 return true;
5859 bool
5860 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5862 gfc_expr *e;
5863 int len, i;
5864 int num_percent, nargs;
5866 e = a->expr;
5867 if (e->expr_type != EXPR_CONSTANT)
5868 return true;
5870 len = e->value.character.length;
5871 if (e->value.character.string[len-1] != '\0')
5872 gfc_internal_error ("fe_runtime_error string must be null terminated");
5874 num_percent = 0;
5875 for (i=0; i<len-1; i++)
5876 if (e->value.character.string[i] == '%')
5877 num_percent ++;
5879 nargs = 0;
5880 for (; a; a = a->next)
5881 nargs ++;
5883 if (nargs -1 != num_percent)
5884 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5885 nargs, num_percent++);
5887 return true;
5890 bool
5891 gfc_check_second_sub (gfc_expr *time)
5893 if (!scalar_check (time, 0))
5894 return false;
5896 if (!type_check (time, 0, BT_REAL))
5897 return false;
5899 if (!kind_value_check (time, 0, 4))
5900 return false;
5902 return true;
5906 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5907 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5908 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5909 count_max are all optional arguments */
5911 bool
5912 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5913 gfc_expr *count_max)
5915 if (count != NULL)
5917 if (!scalar_check (count, 0))
5918 return false;
5920 if (!type_check (count, 0, BT_INTEGER))
5921 return false;
5923 if (count->ts.kind != gfc_default_integer_kind
5924 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5925 "SYSTEM_CLOCK at %L has non-default kind",
5926 &count->where))
5927 return false;
5929 if (!variable_check (count, 0, false))
5930 return false;
5933 if (count_rate != NULL)
5935 if (!scalar_check (count_rate, 1))
5936 return false;
5938 if (!variable_check (count_rate, 1, false))
5939 return false;
5941 if (count_rate->ts.type == BT_REAL)
5943 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5944 "SYSTEM_CLOCK at %L", &count_rate->where))
5945 return false;
5947 else
5949 if (!type_check (count_rate, 1, BT_INTEGER))
5950 return false;
5952 if (count_rate->ts.kind != gfc_default_integer_kind
5953 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5954 "SYSTEM_CLOCK at %L has non-default kind",
5955 &count_rate->where))
5956 return false;
5961 if (count_max != NULL)
5963 if (!scalar_check (count_max, 2))
5964 return false;
5966 if (!type_check (count_max, 2, BT_INTEGER))
5967 return false;
5969 if (count_max->ts.kind != gfc_default_integer_kind
5970 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5971 "SYSTEM_CLOCK at %L has non-default kind",
5972 &count_max->where))
5973 return false;
5975 if (!variable_check (count_max, 2, false))
5976 return false;
5979 return true;
5983 bool
5984 gfc_check_irand (gfc_expr *x)
5986 if (x == NULL)
5987 return true;
5989 if (!scalar_check (x, 0))
5990 return false;
5992 if (!type_check (x, 0, BT_INTEGER))
5993 return false;
5995 if (!kind_value_check (x, 0, 4))
5996 return false;
5998 return true;
6002 bool
6003 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
6005 if (!scalar_check (seconds, 0))
6006 return false;
6007 if (!type_check (seconds, 0, BT_INTEGER))
6008 return false;
6010 if (!int_or_proc_check (handler, 1))
6011 return false;
6012 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6013 return false;
6015 if (status == NULL)
6016 return true;
6018 if (!scalar_check (status, 2))
6019 return false;
6020 if (!type_check (status, 2, BT_INTEGER))
6021 return false;
6022 if (!kind_value_check (status, 2, gfc_default_integer_kind))
6023 return false;
6025 return true;
6029 bool
6030 gfc_check_rand (gfc_expr *x)
6032 if (x == NULL)
6033 return true;
6035 if (!scalar_check (x, 0))
6036 return false;
6038 if (!type_check (x, 0, BT_INTEGER))
6039 return false;
6041 if (!kind_value_check (x, 0, 4))
6042 return false;
6044 return true;
6048 bool
6049 gfc_check_srand (gfc_expr *x)
6051 if (!scalar_check (x, 0))
6052 return false;
6054 if (!type_check (x, 0, BT_INTEGER))
6055 return false;
6057 if (!kind_value_check (x, 0, 4))
6058 return false;
6060 return true;
6064 bool
6065 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
6067 if (!scalar_check (time, 0))
6068 return false;
6069 if (!type_check (time, 0, BT_INTEGER))
6070 return false;
6072 if (!type_check (result, 1, BT_CHARACTER))
6073 return false;
6074 if (!kind_value_check (result, 1, gfc_default_character_kind))
6075 return false;
6077 return true;
6081 bool
6082 gfc_check_dtime_etime (gfc_expr *x)
6084 if (!array_check (x, 0))
6085 return false;
6087 if (!rank_check (x, 0, 1))
6088 return false;
6090 if (!variable_check (x, 0, false))
6091 return false;
6093 if (!type_check (x, 0, BT_REAL))
6094 return false;
6096 if (!kind_value_check (x, 0, 4))
6097 return false;
6099 return true;
6103 bool
6104 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
6106 if (!array_check (values, 0))
6107 return false;
6109 if (!rank_check (values, 0, 1))
6110 return false;
6112 if (!variable_check (values, 0, false))
6113 return false;
6115 if (!type_check (values, 0, BT_REAL))
6116 return false;
6118 if (!kind_value_check (values, 0, 4))
6119 return false;
6121 if (!scalar_check (time, 1))
6122 return false;
6124 if (!type_check (time, 1, BT_REAL))
6125 return false;
6127 if (!kind_value_check (time, 1, 4))
6128 return false;
6130 return true;
6134 bool
6135 gfc_check_fdate_sub (gfc_expr *date)
6137 if (!type_check (date, 0, BT_CHARACTER))
6138 return false;
6139 if (!kind_value_check (date, 0, gfc_default_character_kind))
6140 return false;
6142 return true;
6146 bool
6147 gfc_check_gerror (gfc_expr *msg)
6149 if (!type_check (msg, 0, BT_CHARACTER))
6150 return false;
6151 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6152 return false;
6154 return true;
6158 bool
6159 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
6161 if (!type_check (cwd, 0, BT_CHARACTER))
6162 return false;
6163 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
6164 return false;
6166 if (status == NULL)
6167 return true;
6169 if (!scalar_check (status, 1))
6170 return false;
6172 if (!type_check (status, 1, BT_INTEGER))
6173 return false;
6175 return true;
6179 bool
6180 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
6182 if (!type_check (pos, 0, BT_INTEGER))
6183 return false;
6185 if (pos->ts.kind > gfc_default_integer_kind)
6187 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
6188 "not wider than the default kind (%d)",
6189 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6190 &pos->where, gfc_default_integer_kind);
6191 return false;
6194 if (!type_check (value, 1, BT_CHARACTER))
6195 return false;
6196 if (!kind_value_check (value, 1, gfc_default_character_kind))
6197 return false;
6199 return true;
6203 bool
6204 gfc_check_getlog (gfc_expr *msg)
6206 if (!type_check (msg, 0, BT_CHARACTER))
6207 return false;
6208 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6209 return false;
6211 return true;
6215 bool
6216 gfc_check_exit (gfc_expr *status)
6218 if (status == NULL)
6219 return true;
6221 if (!type_check (status, 0, BT_INTEGER))
6222 return false;
6224 if (!scalar_check (status, 0))
6225 return false;
6227 return true;
6231 bool
6232 gfc_check_flush (gfc_expr *unit)
6234 if (unit == NULL)
6235 return true;
6237 if (!type_check (unit, 0, BT_INTEGER))
6238 return false;
6240 if (!scalar_check (unit, 0))
6241 return false;
6243 return true;
6247 bool
6248 gfc_check_free (gfc_expr *i)
6250 if (!type_check (i, 0, BT_INTEGER))
6251 return false;
6253 if (!scalar_check (i, 0))
6254 return false;
6256 return true;
6260 bool
6261 gfc_check_hostnm (gfc_expr *name)
6263 if (!type_check (name, 0, BT_CHARACTER))
6264 return false;
6265 if (!kind_value_check (name, 0, gfc_default_character_kind))
6266 return false;
6268 return true;
6272 bool
6273 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6275 if (!type_check (name, 0, BT_CHARACTER))
6276 return false;
6277 if (!kind_value_check (name, 0, gfc_default_character_kind))
6278 return false;
6280 if (status == NULL)
6281 return true;
6283 if (!scalar_check (status, 1))
6284 return false;
6286 if (!type_check (status, 1, BT_INTEGER))
6287 return false;
6289 return true;
6293 bool
6294 gfc_check_itime_idate (gfc_expr *values)
6296 if (!array_check (values, 0))
6297 return false;
6299 if (!rank_check (values, 0, 1))
6300 return false;
6302 if (!variable_check (values, 0, false))
6303 return false;
6305 if (!type_check (values, 0, BT_INTEGER))
6306 return false;
6308 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6309 return false;
6311 return true;
6315 bool
6316 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6318 if (!type_check (time, 0, BT_INTEGER))
6319 return false;
6321 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6322 return false;
6324 if (!scalar_check (time, 0))
6325 return false;
6327 if (!array_check (values, 1))
6328 return false;
6330 if (!rank_check (values, 1, 1))
6331 return false;
6333 if (!variable_check (values, 1, false))
6334 return false;
6336 if (!type_check (values, 1, BT_INTEGER))
6337 return false;
6339 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6340 return false;
6342 return true;
6346 bool
6347 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6349 if (!scalar_check (unit, 0))
6350 return false;
6352 if (!type_check (unit, 0, BT_INTEGER))
6353 return false;
6355 if (!type_check (name, 1, BT_CHARACTER))
6356 return false;
6357 if (!kind_value_check (name, 1, gfc_default_character_kind))
6358 return false;
6360 return true;
6364 bool
6365 gfc_check_isatty (gfc_expr *unit)
6367 if (unit == NULL)
6368 return false;
6370 if (!type_check (unit, 0, BT_INTEGER))
6371 return false;
6373 if (!scalar_check (unit, 0))
6374 return false;
6376 return true;
6380 bool
6381 gfc_check_isnan (gfc_expr *x)
6383 if (!type_check (x, 0, BT_REAL))
6384 return false;
6386 return true;
6390 bool
6391 gfc_check_perror (gfc_expr *string)
6393 if (!type_check (string, 0, BT_CHARACTER))
6394 return false;
6395 if (!kind_value_check (string, 0, gfc_default_character_kind))
6396 return false;
6398 return true;
6402 bool
6403 gfc_check_umask (gfc_expr *mask)
6405 if (!type_check (mask, 0, BT_INTEGER))
6406 return false;
6408 if (!scalar_check (mask, 0))
6409 return false;
6411 return true;
6415 bool
6416 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6418 if (!type_check (mask, 0, BT_INTEGER))
6419 return false;
6421 if (!scalar_check (mask, 0))
6422 return false;
6424 if (old == NULL)
6425 return true;
6427 if (!scalar_check (old, 1))
6428 return false;
6430 if (!type_check (old, 1, BT_INTEGER))
6431 return false;
6433 return true;
6437 bool
6438 gfc_check_unlink (gfc_expr *name)
6440 if (!type_check (name, 0, BT_CHARACTER))
6441 return false;
6442 if (!kind_value_check (name, 0, gfc_default_character_kind))
6443 return false;
6445 return true;
6449 bool
6450 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6452 if (!type_check (name, 0, BT_CHARACTER))
6453 return false;
6454 if (!kind_value_check (name, 0, gfc_default_character_kind))
6455 return false;
6457 if (status == NULL)
6458 return true;
6460 if (!scalar_check (status, 1))
6461 return false;
6463 if (!type_check (status, 1, BT_INTEGER))
6464 return false;
6466 return true;
6470 bool
6471 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6473 if (!scalar_check (number, 0))
6474 return false;
6475 if (!type_check (number, 0, BT_INTEGER))
6476 return false;
6478 if (!int_or_proc_check (handler, 1))
6479 return false;
6480 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6481 return false;
6483 return true;
6487 bool
6488 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6490 if (!scalar_check (number, 0))
6491 return false;
6492 if (!type_check (number, 0, BT_INTEGER))
6493 return false;
6495 if (!int_or_proc_check (handler, 1))
6496 return false;
6497 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6498 return false;
6500 if (status == NULL)
6501 return true;
6503 if (!type_check (status, 2, BT_INTEGER))
6504 return false;
6505 if (!scalar_check (status, 2))
6506 return false;
6508 return true;
6512 bool
6513 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6515 if (!type_check (cmd, 0, BT_CHARACTER))
6516 return false;
6517 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6518 return false;
6520 if (!scalar_check (status, 1))
6521 return false;
6523 if (!type_check (status, 1, BT_INTEGER))
6524 return false;
6526 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6527 return false;
6529 return true;
6533 /* This is used for the GNU intrinsics AND, OR and XOR. */
6534 bool
6535 gfc_check_and (gfc_expr *i, gfc_expr *j)
6537 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6539 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6540 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6541 gfc_current_intrinsic, &i->where);
6542 return false;
6545 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6547 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6548 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6549 gfc_current_intrinsic, &j->where);
6550 return false;
6553 if (i->ts.type != j->ts.type)
6555 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6556 "have the same type", gfc_current_intrinsic_arg[0]->name,
6557 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6558 &j->where);
6559 return false;
6562 if (!scalar_check (i, 0))
6563 return false;
6565 if (!scalar_check (j, 1))
6566 return false;
6568 return true;
6572 bool
6573 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6576 if (a->expr_type == EXPR_NULL)
6578 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6579 "argument to STORAGE_SIZE, because it returns a "
6580 "disassociated pointer", &a->where);
6581 return false;
6584 if (a->ts.type == BT_ASSUMED)
6586 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6587 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6588 &a->where);
6589 return false;
6592 if (a->ts.type == BT_PROCEDURE)
6594 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6595 "procedure", gfc_current_intrinsic_arg[0]->name,
6596 gfc_current_intrinsic, &a->where);
6597 return false;
6600 if (kind == NULL)
6601 return true;
6603 if (!type_check (kind, 1, BT_INTEGER))
6604 return false;
6606 if (!scalar_check (kind, 1))
6607 return false;
6609 if (kind->expr_type != EXPR_CONSTANT)
6611 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6612 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6613 &kind->where);
6614 return false;
6617 return true;