* ggc.h (empty_string): Delete.
[official-gcc.git] / gcc / fortran / check.c
blob45bc68ef7ad46575716dd2374c2afdb805175f1b
1 /* Check functions
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 /* These functions check to see if an argument list is compatible with
23 a particular intrinsic function or subroutine. Presence of
24 required arguments has already been established, the argument list
25 has been sorted into the right order and has NULL arguments in the
26 correct places for missing optional arguments. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "options.h"
32 #include "gfortran.h"
33 #include "intrinsic.h"
34 #include "constructor.h"
35 #include "target-memory.h"
38 /* Make sure an expression is a scalar. */
40 static bool
41 scalar_check (gfc_expr *e, int n)
43 if (e->rank == 0)
44 return true;
46 gfc_error ("%qs argument of %qs intrinsic at %L must be a scalar",
47 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
48 &e->where);
50 return false;
54 /* Check the type of an expression. */
56 static bool
57 type_check (gfc_expr *e, int n, bt type)
59 if (e->ts.type == type)
60 return true;
62 gfc_error ("%qs argument of %qs intrinsic at %L must be %s",
63 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
64 &e->where, gfc_basic_typename (type));
66 return false;
70 /* Check that the expression is a numeric type. */
72 static bool
73 numeric_check (gfc_expr *e, int n)
75 /* Users sometime use a subroutine designator as an actual argument to
76 an intrinsic subprogram that expects an argument with a numeric type. */
77 if (e->symtree && e->symtree->n.sym->attr.subroutine)
78 goto error;
80 if (gfc_numeric_ts (&e->ts))
81 return true;
83 /* If the expression has not got a type, check if its namespace can
84 offer a default type. */
85 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
86 && e->symtree->n.sym->ts.type == BT_UNKNOWN
87 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
88 && gfc_numeric_ts (&e->symtree->n.sym->ts))
90 e->ts = e->symtree->n.sym->ts;
91 return true;
94 error:
96 gfc_error ("%qs argument of %qs intrinsic at %L must have a numeric type",
97 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
98 &e->where);
100 return false;
104 /* Check that an expression is integer or real. */
106 static bool
107 int_or_real_check (gfc_expr *e, int n)
109 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
111 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
112 "or REAL", gfc_current_intrinsic_arg[n]->name,
113 gfc_current_intrinsic, &e->where);
114 return false;
117 return true;
121 /* Check that an expression is real or complex. */
123 static bool
124 real_or_complex_check (gfc_expr *e, int n)
126 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
128 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
129 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
130 gfc_current_intrinsic, &e->where);
131 return false;
134 return true;
138 /* Check that an expression is INTEGER or PROCEDURE. */
140 static bool
141 int_or_proc_check (gfc_expr *e, int n)
143 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
145 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
146 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
147 gfc_current_intrinsic, &e->where);
148 return false;
151 return true;
155 /* Check that the expression is an optional constant integer
156 and that it specifies a valid kind for that type. */
158 static bool
159 kind_check (gfc_expr *k, int n, bt type)
161 int kind;
163 if (k == NULL)
164 return true;
166 if (!type_check (k, n, BT_INTEGER))
167 return false;
169 if (!scalar_check (k, n))
170 return false;
172 if (!gfc_check_init_expr (k))
174 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
175 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
176 &k->where);
177 return false;
180 if (gfc_extract_int (k, &kind)
181 || gfc_validate_kind (type, kind, true) < 0)
183 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
184 &k->where);
185 return false;
188 return true;
192 /* Make sure the expression is a double precision real. */
194 static bool
195 double_check (gfc_expr *d, int n)
197 if (!type_check (d, n, BT_REAL))
198 return false;
200 if (d->ts.kind != gfc_default_double_kind)
202 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
203 "precision", gfc_current_intrinsic_arg[n]->name,
204 gfc_current_intrinsic, &d->where);
205 return false;
208 return true;
212 static bool
213 coarray_check (gfc_expr *e, int n)
215 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
216 && CLASS_DATA (e)->attr.codimension
217 && CLASS_DATA (e)->as->corank)
219 gfc_add_class_array_ref (e);
220 return true;
223 if (!gfc_is_coarray (e))
225 gfc_error ("Expected coarray variable as %qs argument to the %s "
226 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
227 gfc_current_intrinsic, &e->where);
228 return false;
231 return true;
235 /* Make sure the expression is a logical array. */
237 static bool
238 logical_array_check (gfc_expr *array, int n)
240 if (array->ts.type != BT_LOGICAL || array->rank == 0)
242 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
243 "array", gfc_current_intrinsic_arg[n]->name,
244 gfc_current_intrinsic, &array->where);
245 return false;
248 return true;
252 /* Make sure an expression is an array. */
254 static bool
255 array_check (gfc_expr *e, int n)
257 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
258 && CLASS_DATA (e)->attr.dimension
259 && CLASS_DATA (e)->as->rank)
261 gfc_add_class_array_ref (e);
262 return true;
265 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
266 return true;
268 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
269 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
270 &e->where);
272 return false;
276 /* If expr is a constant, then check to ensure that it is greater than
277 of equal to zero. */
279 static bool
280 nonnegative_check (const char *arg, gfc_expr *expr)
282 int i;
284 if (expr->expr_type == EXPR_CONSTANT)
286 gfc_extract_int (expr, &i);
287 if (i < 0)
289 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
290 return false;
294 return true;
298 /* If expr is a constant, then check to ensure that it is greater than zero. */
300 static bool
301 positive_check (int n, gfc_expr *expr)
303 int i;
305 if (expr->expr_type == EXPR_CONSTANT)
307 gfc_extract_int (expr, &i);
308 if (i <= 0)
310 gfc_error ("%qs argument of %qs intrinsic at %L must be positive",
311 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
312 &expr->where);
313 return false;
317 return true;
321 /* If expr2 is constant, then check that the value is less than
322 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
324 static bool
325 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
326 gfc_expr *expr2, bool or_equal)
328 int i2, i3;
330 if (expr2->expr_type == EXPR_CONSTANT)
332 gfc_extract_int (expr2, &i2);
333 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
335 /* For ISHFT[C], check that |shift| <= bit_size(i). */
336 if (arg2 == NULL)
338 if (i2 < 0)
339 i2 = -i2;
341 if (i2 > gfc_integer_kinds[i3].bit_size)
343 gfc_error ("The absolute value of SHIFT at %L must be less "
344 "than or equal to BIT_SIZE(%qs)",
345 &expr2->where, arg1);
346 return false;
350 if (or_equal)
352 if (i2 > gfc_integer_kinds[i3].bit_size)
354 gfc_error ("%qs at %L must be less than "
355 "or equal to BIT_SIZE(%qs)",
356 arg2, &expr2->where, arg1);
357 return false;
360 else
362 if (i2 >= gfc_integer_kinds[i3].bit_size)
364 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
365 arg2, &expr2->where, arg1);
366 return false;
371 return true;
375 /* If expr is constant, then check that the value is less than or equal
376 to the bit_size of the kind k. */
378 static bool
379 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
381 int i, val;
383 if (expr->expr_type != EXPR_CONSTANT)
384 return true;
386 i = gfc_validate_kind (BT_INTEGER, k, false);
387 gfc_extract_int (expr, &val);
389 if (val > gfc_integer_kinds[i].bit_size)
391 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
392 "INTEGER(KIND=%d)", arg, &expr->where, k);
393 return false;
396 return true;
400 /* If expr2 and expr3 are constants, then check that the value is less than
401 or equal to bit_size(expr1). */
403 static bool
404 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
405 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
407 int i2, i3;
409 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
411 gfc_extract_int (expr2, &i2);
412 gfc_extract_int (expr3, &i3);
413 i2 += i3;
414 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
415 if (i2 > gfc_integer_kinds[i3].bit_size)
417 gfc_error ("%<%s + %s%> at %L must be less than or equal "
418 "to BIT_SIZE(%qs)",
419 arg2, arg3, &expr2->where, arg1);
420 return false;
424 return true;
427 /* Make sure two expressions have the same type. */
429 static bool
430 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
432 gfc_typespec *ets = &e->ts;
433 gfc_typespec *fts = &f->ts;
435 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
436 ets = &e->symtree->n.sym->ts;
437 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
438 fts = &f->symtree->n.sym->ts;
440 if (gfc_compare_types (ets, fts))
441 return true;
443 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
444 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
445 gfc_current_intrinsic, &f->where,
446 gfc_current_intrinsic_arg[n]->name);
448 return false;
452 /* Make sure that an expression has a certain (nonzero) rank. */
454 static bool
455 rank_check (gfc_expr *e, int n, int rank)
457 if (e->rank == rank)
458 return true;
460 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
461 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
462 &e->where, rank);
464 return false;
468 /* Make sure a variable expression is not an optional dummy argument. */
470 static bool
471 nonoptional_check (gfc_expr *e, int n)
473 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
475 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
476 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
477 &e->where);
480 /* TODO: Recursive check on nonoptional variables? */
482 return true;
486 /* Check for ALLOCATABLE attribute. */
488 static bool
489 allocatable_check (gfc_expr *e, int n)
491 symbol_attribute attr;
493 attr = gfc_variable_attr (e, NULL);
494 if (!attr.allocatable || attr.associate_var)
496 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
497 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
498 &e->where);
499 return false;
502 return true;
506 /* Check that an expression has a particular kind. */
508 static bool
509 kind_value_check (gfc_expr *e, int n, int k)
511 if (e->ts.kind == k)
512 return true;
514 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
515 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
516 &e->where, k);
518 return false;
522 /* Make sure an expression is a variable. */
524 static bool
525 variable_check (gfc_expr *e, int n, bool allow_proc)
527 if (e->expr_type == EXPR_VARIABLE
528 && e->symtree->n.sym->attr.intent == INTENT_IN
529 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
530 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
532 gfc_ref *ref;
533 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
534 && CLASS_DATA (e->symtree->n.sym)
535 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
536 : e->symtree->n.sym->attr.pointer;
538 for (ref = e->ref; ref; ref = ref->next)
540 if (pointer && ref->type == REF_COMPONENT)
541 break;
542 if (ref->type == REF_COMPONENT
543 && ((ref->u.c.component->ts.type == BT_CLASS
544 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
545 || (ref->u.c.component->ts.type != BT_CLASS
546 && ref->u.c.component->attr.pointer)))
547 break;
550 if (!ref)
552 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
553 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
554 gfc_current_intrinsic, &e->where);
555 return false;
559 if (e->expr_type == EXPR_VARIABLE
560 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
561 && (allow_proc || !e->symtree->n.sym->attr.function))
562 return true;
564 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
565 && e->symtree->n.sym == e->symtree->n.sym->result)
567 gfc_namespace *ns;
568 for (ns = gfc_current_ns; ns; ns = ns->parent)
569 if (ns->proc_name == e->symtree->n.sym)
570 return true;
573 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
574 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
576 return false;
580 /* Check the common DIM parameter for correctness. */
582 static bool
583 dim_check (gfc_expr *dim, int n, bool optional)
585 if (dim == NULL)
586 return true;
588 if (!type_check (dim, n, BT_INTEGER))
589 return false;
591 if (!scalar_check (dim, n))
592 return false;
594 if (!optional && !nonoptional_check (dim, n))
595 return false;
597 return true;
601 /* If a coarray DIM parameter is a constant, make sure that it is greater than
602 zero and less than or equal to the corank of the given array. */
604 static bool
605 dim_corank_check (gfc_expr *dim, gfc_expr *array)
607 int corank;
609 gcc_assert (array->expr_type == EXPR_VARIABLE);
611 if (dim->expr_type != EXPR_CONSTANT)
612 return true;
614 if (array->ts.type == BT_CLASS)
615 return true;
617 corank = gfc_get_corank (array);
619 if (mpz_cmp_ui (dim->value.integer, 1) < 0
620 || mpz_cmp_ui (dim->value.integer, corank) > 0)
622 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
623 "codimension index", gfc_current_intrinsic, &dim->where);
625 return false;
628 return true;
632 /* If a DIM parameter is a constant, make sure that it is greater than
633 zero and less than or equal to the rank of the given array. If
634 allow_assumed is zero then dim must be less than the rank of the array
635 for assumed size arrays. */
637 static bool
638 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
640 gfc_array_ref *ar;
641 int rank;
643 if (dim == NULL)
644 return true;
646 if (dim->expr_type != EXPR_CONSTANT)
647 return true;
649 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
650 && array->value.function.isym->id == GFC_ISYM_SPREAD)
651 rank = array->rank + 1;
652 else
653 rank = array->rank;
655 /* Assumed-rank array. */
656 if (rank == -1)
657 rank = GFC_MAX_DIMENSIONS;
659 if (array->expr_type == EXPR_VARIABLE)
661 ar = gfc_find_array_ref (array);
662 if (ar->as->type == AS_ASSUMED_SIZE
663 && !allow_assumed
664 && ar->type != AR_ELEMENT
665 && ar->type != AR_SECTION)
666 rank--;
669 if (mpz_cmp_ui (dim->value.integer, 1) < 0
670 || mpz_cmp_ui (dim->value.integer, rank) > 0)
672 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
673 "dimension index", gfc_current_intrinsic, &dim->where);
675 return false;
678 return true;
682 /* Compare the size of a along dimension ai with the size of b along
683 dimension bi, returning 0 if they are known not to be identical,
684 and 1 if they are identical, or if this cannot be determined. */
686 static int
687 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
689 mpz_t a_size, b_size;
690 int ret;
692 gcc_assert (a->rank > ai);
693 gcc_assert (b->rank > bi);
695 ret = 1;
697 if (gfc_array_dimen_size (a, ai, &a_size))
699 if (gfc_array_dimen_size (b, bi, &b_size))
701 if (mpz_cmp (a_size, b_size) != 0)
702 ret = 0;
704 mpz_clear (b_size);
706 mpz_clear (a_size);
708 return ret;
711 /* Calculate the length of a character variable, including substrings.
712 Strip away parentheses if necessary. Return -1 if no length could
713 be determined. */
715 static long
716 gfc_var_strlen (const gfc_expr *a)
718 gfc_ref *ra;
720 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
721 a = a->value.op.op1;
723 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
726 if (ra)
728 long start_a, end_a;
730 if (!ra->u.ss.end)
731 return -1;
733 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
734 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
736 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
737 : 1;
738 end_a = mpz_get_si (ra->u.ss.end->value.integer);
739 return (end_a < start_a) ? 0 : end_a - start_a + 1;
741 else if (ra->u.ss.start
742 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
743 return 1;
744 else
745 return -1;
748 if (a->ts.u.cl && a->ts.u.cl->length
749 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
750 return mpz_get_si (a->ts.u.cl->length->value.integer);
751 else if (a->expr_type == EXPR_CONSTANT
752 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
753 return a->value.character.length;
754 else
755 return -1;
759 /* Check whether two character expressions have the same length;
760 returns true if they have or if the length cannot be determined,
761 otherwise return false and raise a gfc_error. */
763 bool
764 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
766 long len_a, len_b;
768 len_a = gfc_var_strlen(a);
769 len_b = gfc_var_strlen(b);
771 if (len_a == -1 || len_b == -1 || len_a == len_b)
772 return true;
773 else
775 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
776 len_a, len_b, name, &a->where);
777 return false;
782 /***** Check functions *****/
784 /* Check subroutine suitable for intrinsics taking a real argument and
785 a kind argument for the result. */
787 static bool
788 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
790 if (!type_check (a, 0, BT_REAL))
791 return false;
792 if (!kind_check (kind, 1, type))
793 return false;
795 return true;
799 /* Check subroutine suitable for ceiling, floor and nint. */
801 bool
802 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
804 return check_a_kind (a, kind, BT_INTEGER);
808 /* Check subroutine suitable for aint, anint. */
810 bool
811 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
813 return check_a_kind (a, kind, BT_REAL);
817 bool
818 gfc_check_abs (gfc_expr *a)
820 if (!numeric_check (a, 0))
821 return false;
823 return true;
827 bool
828 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
830 if (!type_check (a, 0, BT_INTEGER))
831 return false;
832 if (!kind_check (kind, 1, BT_CHARACTER))
833 return false;
835 return true;
839 bool
840 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
842 if (!type_check (name, 0, BT_CHARACTER)
843 || !scalar_check (name, 0))
844 return false;
845 if (!kind_value_check (name, 0, gfc_default_character_kind))
846 return false;
848 if (!type_check (mode, 1, BT_CHARACTER)
849 || !scalar_check (mode, 1))
850 return false;
851 if (!kind_value_check (mode, 1, gfc_default_character_kind))
852 return false;
854 return true;
858 bool
859 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
861 if (!logical_array_check (mask, 0))
862 return false;
864 if (!dim_check (dim, 1, false))
865 return false;
867 if (!dim_rank_check (dim, mask, 0))
868 return false;
870 return true;
874 bool
875 gfc_check_allocated (gfc_expr *array)
877 /* Tests on allocated components of coarrays need to detour the check to
878 argument of the _caf_get. */
879 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
880 && array->value.function.isym
881 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
883 array = array->value.function.actual->expr;
884 if (!array->ref)
885 return false;
888 if (!variable_check (array, 0, false))
889 return false;
890 if (!allocatable_check (array, 0))
891 return false;
893 return true;
897 /* Common check function where the first argument must be real or
898 integer and the second argument must be the same as the first. */
900 bool
901 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
903 if (!int_or_real_check (a, 0))
904 return false;
906 if (a->ts.type != p->ts.type)
908 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
909 "have the same type", gfc_current_intrinsic_arg[0]->name,
910 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
911 &p->where);
912 return false;
915 if (a->ts.kind != p->ts.kind)
917 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
918 &p->where))
919 return false;
922 return true;
926 bool
927 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
929 if (!double_check (x, 0) || !double_check (y, 1))
930 return false;
932 return true;
936 bool
937 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
939 symbol_attribute attr1, attr2;
940 int i;
941 bool t;
942 locus *where;
944 where = &pointer->where;
946 if (pointer->expr_type == EXPR_NULL)
947 goto null_arg;
949 attr1 = gfc_expr_attr (pointer);
951 if (!attr1.pointer && !attr1.proc_pointer)
953 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
954 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
955 &pointer->where);
956 return false;
959 /* F2008, C1242. */
960 if (attr1.pointer && gfc_is_coindexed (pointer))
962 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
963 "coindexed", gfc_current_intrinsic_arg[0]->name,
964 gfc_current_intrinsic, &pointer->where);
965 return false;
968 /* Target argument is optional. */
969 if (target == NULL)
970 return true;
972 where = &target->where;
973 if (target->expr_type == EXPR_NULL)
974 goto null_arg;
976 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
977 attr2 = gfc_expr_attr (target);
978 else
980 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
981 "or target VARIABLE or FUNCTION",
982 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
983 &target->where);
984 return false;
987 if (attr1.pointer && !attr2.pointer && !attr2.target)
989 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
990 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
991 gfc_current_intrinsic, &target->where);
992 return false;
995 /* F2008, C1242. */
996 if (attr1.pointer && gfc_is_coindexed (target))
998 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
999 "coindexed", gfc_current_intrinsic_arg[1]->name,
1000 gfc_current_intrinsic, &target->where);
1001 return false;
1004 t = true;
1005 if (!same_type_check (pointer, 0, target, 1))
1006 t = false;
1007 if (!rank_check (target, 0, pointer->rank))
1008 t = false;
1009 if (target->rank > 0)
1011 for (i = 0; i < target->rank; i++)
1012 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
1014 gfc_error ("Array section with a vector subscript at %L shall not "
1015 "be the target of a pointer",
1016 &target->where);
1017 t = false;
1018 break;
1021 return t;
1023 null_arg:
1025 gfc_error ("NULL pointer at %L is not permitted as actual argument "
1026 "of %qs intrinsic function", where, gfc_current_intrinsic);
1027 return false;
1032 bool
1033 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1035 /* gfc_notify_std would be a waste of time as the return value
1036 is seemingly used only for the generic resolution. The error
1037 will be: Too many arguments. */
1038 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1039 return false;
1041 return gfc_check_atan2 (y, x);
1045 bool
1046 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1048 if (!type_check (y, 0, BT_REAL))
1049 return false;
1050 if (!same_type_check (y, 0, x, 1))
1051 return false;
1053 return true;
1057 static bool
1058 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1059 gfc_expr *stat, int stat_no)
1061 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1062 return false;
1064 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1065 && !(atom->ts.type == BT_LOGICAL
1066 && atom->ts.kind == gfc_atomic_logical_kind))
1068 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1069 "integer of ATOMIC_INT_KIND or a logical of "
1070 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1071 return false;
1074 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1076 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1077 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1078 return false;
1081 if (atom->ts.type != value->ts.type)
1083 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1084 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1085 gfc_current_intrinsic, &value->where,
1086 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1087 return false;
1090 if (stat != NULL)
1092 if (!type_check (stat, stat_no, BT_INTEGER))
1093 return false;
1094 if (!scalar_check (stat, stat_no))
1095 return false;
1096 if (!variable_check (stat, stat_no, false))
1097 return false;
1098 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1099 return false;
1101 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1102 gfc_current_intrinsic, &stat->where))
1103 return false;
1106 return true;
1110 bool
1111 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1113 if (atom->expr_type == EXPR_FUNCTION
1114 && atom->value.function.isym
1115 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1116 atom = atom->value.function.actual->expr;
1118 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1120 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1121 "definable", gfc_current_intrinsic, &atom->where);
1122 return false;
1125 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1129 bool
1130 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1132 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1134 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1135 "integer of ATOMIC_INT_KIND", &atom->where,
1136 gfc_current_intrinsic);
1137 return false;
1140 return gfc_check_atomic_def (atom, value, stat);
1144 bool
1145 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1147 if (atom->expr_type == EXPR_FUNCTION
1148 && atom->value.function.isym
1149 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1150 atom = atom->value.function.actual->expr;
1152 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1154 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1155 "definable", gfc_current_intrinsic, &value->where);
1156 return false;
1159 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1163 bool
1164 gfc_check_image_status (gfc_expr *image, gfc_expr *team)
1166 /* IMAGE has to be a positive, scalar integer. */
1167 if (!type_check (image, 0, BT_INTEGER) || !scalar_check (image, 0)
1168 || !positive_check (0, image))
1169 return false;
1171 if (team)
1173 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1174 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1175 &team->where);
1176 return false;
1178 return true;
1182 bool
1183 gfc_check_failed_or_stopped_images (gfc_expr *team, gfc_expr *kind)
1185 if (team)
1187 gfc_error ("%qs argument of %qs intrinsic at %L not yet supported",
1188 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1189 &team->where);
1190 return false;
1193 if (kind)
1195 int k;
1197 if (!type_check (kind, 1, BT_INTEGER) || !scalar_check (kind, 1)
1198 || !positive_check (1, kind))
1199 return false;
1201 /* Get the kind, reporting error on non-constant or overflow. */
1202 gfc_current_locus = kind->where;
1203 if (gfc_extract_int (kind, &k, 1))
1204 return false;
1205 if (gfc_validate_kind (BT_INTEGER, k, true) == -1)
1207 gfc_error ("%qs argument of %qs intrinsic at %L shall specify a "
1208 "valid integer kind", gfc_current_intrinsic_arg[1]->name,
1209 gfc_current_intrinsic, &kind->where);
1210 return false;
1213 return true;
1217 bool
1218 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1219 gfc_expr *new_val, gfc_expr *stat)
1221 if (atom->expr_type == EXPR_FUNCTION
1222 && atom->value.function.isym
1223 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1224 atom = atom->value.function.actual->expr;
1226 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1227 return false;
1229 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1230 return false;
1232 if (!same_type_check (atom, 0, old, 1))
1233 return false;
1235 if (!same_type_check (atom, 0, compare, 2))
1236 return false;
1238 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1240 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1241 "definable", gfc_current_intrinsic, &atom->where);
1242 return false;
1245 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1247 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1248 "definable", gfc_current_intrinsic, &old->where);
1249 return false;
1252 return true;
1255 bool
1256 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1258 if (event->ts.type != BT_DERIVED
1259 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1260 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1262 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1263 "shall be of type EVENT_TYPE", &event->where);
1264 return false;
1267 if (!scalar_check (event, 0))
1268 return false;
1270 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1272 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1273 "shall be definable", &count->where);
1274 return false;
1277 if (!type_check (count, 1, BT_INTEGER))
1278 return false;
1280 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1281 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1283 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1285 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1286 "shall have at least the range of the default integer",
1287 &count->where);
1288 return false;
1291 if (stat != NULL)
1293 if (!type_check (stat, 2, BT_INTEGER))
1294 return false;
1295 if (!scalar_check (stat, 2))
1296 return false;
1297 if (!variable_check (stat, 2, false))
1298 return false;
1300 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1301 gfc_current_intrinsic, &stat->where))
1302 return false;
1305 return true;
1309 bool
1310 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1311 gfc_expr *stat)
1313 if (atom->expr_type == EXPR_FUNCTION
1314 && atom->value.function.isym
1315 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1316 atom = atom->value.function.actual->expr;
1318 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1320 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1321 "integer of ATOMIC_INT_KIND", &atom->where,
1322 gfc_current_intrinsic);
1323 return false;
1326 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1327 return false;
1329 if (!scalar_check (old, 2))
1330 return false;
1332 if (!same_type_check (atom, 0, old, 2))
1333 return false;
1335 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1337 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1338 "definable", gfc_current_intrinsic, &atom->where);
1339 return false;
1342 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1344 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1345 "definable", gfc_current_intrinsic, &old->where);
1346 return false;
1349 return true;
1353 /* BESJN and BESYN functions. */
1355 bool
1356 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1358 if (!type_check (n, 0, BT_INTEGER))
1359 return false;
1360 if (n->expr_type == EXPR_CONSTANT)
1362 int i;
1363 gfc_extract_int (n, &i);
1364 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1365 "N at %L", &n->where))
1366 return false;
1369 if (!type_check (x, 1, BT_REAL))
1370 return false;
1372 return true;
1376 /* Transformational version of the Bessel JN and YN functions. */
1378 bool
1379 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1381 if (!type_check (n1, 0, BT_INTEGER))
1382 return false;
1383 if (!scalar_check (n1, 0))
1384 return false;
1385 if (!nonnegative_check ("N1", n1))
1386 return false;
1388 if (!type_check (n2, 1, BT_INTEGER))
1389 return false;
1390 if (!scalar_check (n2, 1))
1391 return false;
1392 if (!nonnegative_check ("N2", n2))
1393 return false;
1395 if (!type_check (x, 2, BT_REAL))
1396 return false;
1397 if (!scalar_check (x, 2))
1398 return false;
1400 return true;
1404 bool
1405 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1407 if (!type_check (i, 0, BT_INTEGER))
1408 return false;
1410 if (!type_check (j, 1, BT_INTEGER))
1411 return false;
1413 return true;
1417 bool
1418 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1420 if (!type_check (i, 0, BT_INTEGER))
1421 return false;
1423 if (!type_check (pos, 1, BT_INTEGER))
1424 return false;
1426 if (!nonnegative_check ("pos", pos))
1427 return false;
1429 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1430 return false;
1432 return true;
1436 bool
1437 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1439 if (!type_check (i, 0, BT_INTEGER))
1440 return false;
1441 if (!kind_check (kind, 1, BT_CHARACTER))
1442 return false;
1444 return true;
1448 bool
1449 gfc_check_chdir (gfc_expr *dir)
1451 if (!type_check (dir, 0, BT_CHARACTER))
1452 return false;
1453 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1454 return false;
1456 return true;
1460 bool
1461 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1463 if (!type_check (dir, 0, BT_CHARACTER))
1464 return false;
1465 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1466 return false;
1468 if (status == NULL)
1469 return true;
1471 if (!type_check (status, 1, BT_INTEGER))
1472 return false;
1473 if (!scalar_check (status, 1))
1474 return false;
1476 return true;
1480 bool
1481 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1483 if (!type_check (name, 0, BT_CHARACTER))
1484 return false;
1485 if (!kind_value_check (name, 0, gfc_default_character_kind))
1486 return false;
1488 if (!type_check (mode, 1, BT_CHARACTER))
1489 return false;
1490 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1491 return false;
1493 return true;
1497 bool
1498 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1500 if (!type_check (name, 0, BT_CHARACTER))
1501 return false;
1502 if (!kind_value_check (name, 0, gfc_default_character_kind))
1503 return false;
1505 if (!type_check (mode, 1, BT_CHARACTER))
1506 return false;
1507 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1508 return false;
1510 if (status == NULL)
1511 return true;
1513 if (!type_check (status, 2, BT_INTEGER))
1514 return false;
1516 if (!scalar_check (status, 2))
1517 return false;
1519 return true;
1523 bool
1524 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1526 if (!numeric_check (x, 0))
1527 return false;
1529 if (y != NULL)
1531 if (!numeric_check (y, 1))
1532 return false;
1534 if (x->ts.type == BT_COMPLEX)
1536 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1537 "present if %<x%> is COMPLEX",
1538 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1539 &y->where);
1540 return false;
1543 if (y->ts.type == BT_COMPLEX)
1545 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1546 "of either REAL or INTEGER",
1547 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1548 &y->where);
1549 return false;
1554 if (!kind_check (kind, 2, BT_COMPLEX))
1555 return false;
1557 if (!kind && warn_conversion
1558 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1559 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1560 "COMPLEX(%d) at %L might lose precision, consider using "
1561 "the KIND argument", gfc_typename (&x->ts),
1562 gfc_default_real_kind, &x->where);
1563 else if (y && !kind && warn_conversion
1564 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1565 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1566 "COMPLEX(%d) at %L might lose precision, consider using "
1567 "the KIND argument", gfc_typename (&y->ts),
1568 gfc_default_real_kind, &y->where);
1569 return true;
1573 static bool
1574 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1575 gfc_expr *errmsg, bool co_reduce)
1577 if (!variable_check (a, 0, false))
1578 return false;
1580 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1581 "INTENT(INOUT)"))
1582 return false;
1584 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1585 if (gfc_has_vector_subscript (a))
1587 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1588 "subroutine %s shall not have a vector subscript",
1589 &a->where, gfc_current_intrinsic);
1590 return false;
1593 if (gfc_is_coindexed (a))
1595 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1596 "coindexed", &a->where, gfc_current_intrinsic);
1597 return false;
1600 if (image_idx != NULL)
1602 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1603 return false;
1604 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1605 return false;
1608 if (stat != NULL)
1610 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1611 return false;
1612 if (!scalar_check (stat, co_reduce ? 3 : 2))
1613 return false;
1614 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1615 return false;
1616 if (stat->ts.kind != 4)
1618 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1619 "variable", &stat->where);
1620 return false;
1624 if (errmsg != NULL)
1626 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1627 return false;
1628 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1629 return false;
1630 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1631 return false;
1632 if (errmsg->ts.kind != 1)
1634 gfc_error ("The errmsg= argument at %L must be a default-kind "
1635 "character variable", &errmsg->where);
1636 return false;
1640 if (flag_coarray == GFC_FCOARRAY_NONE)
1642 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1643 &a->where);
1644 return false;
1647 return true;
1651 bool
1652 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1653 gfc_expr *errmsg)
1655 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1657 gfc_error ("Support for the A argument at %L which is polymorphic A "
1658 "argument or has allocatable components is not yet "
1659 "implemented", &a->where);
1660 return false;
1662 return check_co_collective (a, source_image, stat, errmsg, false);
1666 bool
1667 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1668 gfc_expr *stat, gfc_expr *errmsg)
1670 symbol_attribute attr;
1671 gfc_formal_arglist *formal;
1672 gfc_symbol *sym;
1674 if (a->ts.type == BT_CLASS)
1676 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1677 &a->where);
1678 return false;
1681 if (gfc_expr_attr (a).alloc_comp)
1683 gfc_error ("Support for the A argument at %L with allocatable components"
1684 " is not yet implemented", &a->where);
1685 return false;
1688 if (!check_co_collective (a, result_image, stat, errmsg, true))
1689 return false;
1691 if (!gfc_resolve_expr (op))
1692 return false;
1694 attr = gfc_expr_attr (op);
1695 if (!attr.pure || !attr.function)
1697 gfc_error ("OPERATOR argument at %L must be a PURE function",
1698 &op->where);
1699 return false;
1702 if (attr.intrinsic)
1704 /* None of the intrinsics fulfills the criteria of taking two arguments,
1705 returning the same type and kind as the arguments and being permitted
1706 as actual argument. */
1707 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1708 op->symtree->n.sym->name, &op->where);
1709 return false;
1712 if (gfc_is_proc_ptr_comp (op))
1714 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1715 sym = comp->ts.interface;
1717 else
1718 sym = op->symtree->n.sym;
1720 formal = sym->formal;
1722 if (!formal || !formal->next || formal->next->next)
1724 gfc_error ("The function passed as OPERATOR at %L shall have two "
1725 "arguments", &op->where);
1726 return false;
1729 if (sym->result->ts.type == BT_UNKNOWN)
1730 gfc_set_default_type (sym->result, 0, NULL);
1732 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1734 gfc_error ("A argument at %L has type %s but the function passed as "
1735 "OPERATOR at %L returns %s",
1736 &a->where, gfc_typename (&a->ts), &op->where,
1737 gfc_typename (&sym->result->ts));
1738 return false;
1740 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1741 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1743 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1744 "%s and %s but shall have type %s", &op->where,
1745 gfc_typename (&formal->sym->ts),
1746 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1747 return false;
1749 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1750 || formal->next->sym->as || formal->sym->attr.allocatable
1751 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1752 || formal->next->sym->attr.pointer)
1754 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1755 "nonallocatable nonpointer arguments and return a "
1756 "nonallocatable nonpointer scalar", &op->where);
1757 return false;
1760 if (formal->sym->attr.value != formal->next->sym->attr.value)
1762 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1763 "attribute either for none or both arguments", &op->where);
1764 return false;
1767 if (formal->sym->attr.target != formal->next->sym->attr.target)
1769 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1770 "attribute either for none or both arguments", &op->where);
1771 return false;
1774 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1776 gfc_error ("The function passed as OPERATOR at %L shall have the "
1777 "ASYNCHRONOUS attribute either for none or both arguments",
1778 &op->where);
1779 return false;
1782 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1784 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1785 "OPTIONAL attribute for either of the arguments", &op->where);
1786 return false;
1789 if (a->ts.type == BT_CHARACTER)
1791 gfc_charlen *cl;
1792 unsigned long actual_size, formal_size1, formal_size2, result_size;
1794 cl = a->ts.u.cl;
1795 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1796 ? mpz_get_ui (cl->length->value.integer) : 0;
1798 cl = formal->sym->ts.u.cl;
1799 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1800 ? mpz_get_ui (cl->length->value.integer) : 0;
1802 cl = formal->next->sym->ts.u.cl;
1803 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1804 ? mpz_get_ui (cl->length->value.integer) : 0;
1806 cl = sym->ts.u.cl;
1807 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1808 ? mpz_get_ui (cl->length->value.integer) : 0;
1810 if (actual_size
1811 && ((formal_size1 && actual_size != formal_size1)
1812 || (formal_size2 && actual_size != formal_size2)))
1814 gfc_error ("The character length of the A argument at %L and of the "
1815 "arguments of the OPERATOR at %L shall be the same",
1816 &a->where, &op->where);
1817 return false;
1819 if (actual_size && result_size && actual_size != result_size)
1821 gfc_error ("The character length of the A argument at %L and of the "
1822 "function result of the OPERATOR at %L shall be the same",
1823 &a->where, &op->where);
1824 return false;
1828 return true;
1832 bool
1833 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1834 gfc_expr *errmsg)
1836 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1837 && a->ts.type != BT_CHARACTER)
1839 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1840 "integer, real or character",
1841 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1842 &a->where);
1843 return false;
1845 return check_co_collective (a, result_image, stat, errmsg, false);
1849 bool
1850 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1851 gfc_expr *errmsg)
1853 if (!numeric_check (a, 0))
1854 return false;
1855 return check_co_collective (a, result_image, stat, errmsg, false);
1859 bool
1860 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1862 if (!int_or_real_check (x, 0))
1863 return false;
1864 if (!scalar_check (x, 0))
1865 return false;
1867 if (!int_or_real_check (y, 1))
1868 return false;
1869 if (!scalar_check (y, 1))
1870 return false;
1872 return true;
1876 bool
1877 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1879 if (!logical_array_check (mask, 0))
1880 return false;
1881 if (!dim_check (dim, 1, false))
1882 return false;
1883 if (!dim_rank_check (dim, mask, 0))
1884 return false;
1885 if (!kind_check (kind, 2, BT_INTEGER))
1886 return false;
1887 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1888 "with KIND argument at %L",
1889 gfc_current_intrinsic, &kind->where))
1890 return false;
1892 return true;
1896 bool
1897 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1899 if (!array_check (array, 0))
1900 return false;
1902 if (!type_check (shift, 1, BT_INTEGER))
1903 return false;
1905 if (!dim_check (dim, 2, true))
1906 return false;
1908 if (!dim_rank_check (dim, array, false))
1909 return false;
1911 if (array->rank == 1 || shift->rank == 0)
1913 if (!scalar_check (shift, 1))
1914 return false;
1916 else if (shift->rank == array->rank - 1)
1918 int d;
1919 if (!dim)
1920 d = 1;
1921 else if (dim->expr_type == EXPR_CONSTANT)
1922 gfc_extract_int (dim, &d);
1923 else
1924 d = -1;
1926 if (d > 0)
1928 int i, j;
1929 for (i = 0, j = 0; i < array->rank; i++)
1930 if (i != d - 1)
1932 if (!identical_dimen_shape (array, i, shift, j))
1934 gfc_error ("%qs argument of %qs intrinsic at %L has "
1935 "invalid shape in dimension %d (%ld/%ld)",
1936 gfc_current_intrinsic_arg[1]->name,
1937 gfc_current_intrinsic, &shift->where, i + 1,
1938 mpz_get_si (array->shape[i]),
1939 mpz_get_si (shift->shape[j]));
1940 return false;
1943 j += 1;
1947 else
1949 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1950 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1951 gfc_current_intrinsic, &shift->where, array->rank - 1);
1952 return false;
1955 return true;
1959 bool
1960 gfc_check_ctime (gfc_expr *time)
1962 if (!scalar_check (time, 0))
1963 return false;
1965 if (!type_check (time, 0, BT_INTEGER))
1966 return false;
1968 return true;
1972 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1974 if (!double_check (y, 0) || !double_check (x, 1))
1975 return false;
1977 return true;
1980 bool
1981 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1983 if (!numeric_check (x, 0))
1984 return false;
1986 if (y != NULL)
1988 if (!numeric_check (y, 1))
1989 return false;
1991 if (x->ts.type == BT_COMPLEX)
1993 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1994 "present if %<x%> is COMPLEX",
1995 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1996 &y->where);
1997 return false;
2000 if (y->ts.type == BT_COMPLEX)
2002 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
2003 "of either REAL or INTEGER",
2004 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2005 &y->where);
2006 return false;
2010 return true;
2014 bool
2015 gfc_check_dble (gfc_expr *x)
2017 if (!numeric_check (x, 0))
2018 return false;
2020 return true;
2024 bool
2025 gfc_check_digits (gfc_expr *x)
2027 if (!int_or_real_check (x, 0))
2028 return false;
2030 return true;
2034 bool
2035 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
2037 switch (vector_a->ts.type)
2039 case BT_LOGICAL:
2040 if (!type_check (vector_b, 1, BT_LOGICAL))
2041 return false;
2042 break;
2044 case BT_INTEGER:
2045 case BT_REAL:
2046 case BT_COMPLEX:
2047 if (!numeric_check (vector_b, 1))
2048 return false;
2049 break;
2051 default:
2052 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2053 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2054 gfc_current_intrinsic, &vector_a->where);
2055 return false;
2058 if (!rank_check (vector_a, 0, 1))
2059 return false;
2061 if (!rank_check (vector_b, 1, 1))
2062 return false;
2064 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
2066 gfc_error ("Different shape for arguments %qs and %qs at %L for "
2067 "intrinsic %<dot_product%>",
2068 gfc_current_intrinsic_arg[0]->name,
2069 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
2070 return false;
2073 return true;
2077 bool
2078 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2080 if (!type_check (x, 0, BT_REAL)
2081 || !type_check (y, 1, BT_REAL))
2082 return false;
2084 if (x->ts.kind != gfc_default_real_kind)
2086 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2087 "real", gfc_current_intrinsic_arg[0]->name,
2088 gfc_current_intrinsic, &x->where);
2089 return false;
2092 if (y->ts.kind != gfc_default_real_kind)
2094 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2095 "real", gfc_current_intrinsic_arg[1]->name,
2096 gfc_current_intrinsic, &y->where);
2097 return false;
2100 return true;
2104 bool
2105 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2107 if (!type_check (i, 0, BT_INTEGER))
2108 return false;
2110 if (!type_check (j, 1, BT_INTEGER))
2111 return false;
2113 if (i->is_boz && j->is_boz)
2115 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2116 "constants", &i->where, &j->where);
2117 return false;
2120 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2121 return false;
2123 if (!type_check (shift, 2, BT_INTEGER))
2124 return false;
2126 if (!nonnegative_check ("SHIFT", shift))
2127 return false;
2129 if (i->is_boz)
2131 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2132 return false;
2133 i->ts.kind = j->ts.kind;
2135 else
2137 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2138 return false;
2139 j->ts.kind = i->ts.kind;
2142 return true;
2146 bool
2147 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2148 gfc_expr *dim)
2150 if (!array_check (array, 0))
2151 return false;
2153 if (!type_check (shift, 1, BT_INTEGER))
2154 return false;
2156 if (!dim_check (dim, 3, true))
2157 return false;
2159 if (!dim_rank_check (dim, array, false))
2160 return false;
2162 if (array->rank == 1 || shift->rank == 0)
2164 if (!scalar_check (shift, 1))
2165 return false;
2167 else if (shift->rank == array->rank - 1)
2169 int d;
2170 if (!dim)
2171 d = 1;
2172 else if (dim->expr_type == EXPR_CONSTANT)
2173 gfc_extract_int (dim, &d);
2174 else
2175 d = -1;
2177 if (d > 0)
2179 int i, j;
2180 for (i = 0, j = 0; i < array->rank; i++)
2181 if (i != d - 1)
2183 if (!identical_dimen_shape (array, i, shift, j))
2185 gfc_error ("%qs argument of %qs intrinsic at %L has "
2186 "invalid shape in dimension %d (%ld/%ld)",
2187 gfc_current_intrinsic_arg[1]->name,
2188 gfc_current_intrinsic, &shift->where, i + 1,
2189 mpz_get_si (array->shape[i]),
2190 mpz_get_si (shift->shape[j]));
2191 return false;
2194 j += 1;
2198 else
2200 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2201 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2202 gfc_current_intrinsic, &shift->where, array->rank - 1);
2203 return false;
2206 if (boundary != NULL)
2208 if (!same_type_check (array, 0, boundary, 2))
2209 return false;
2211 if (array->rank == 1 || boundary->rank == 0)
2213 if (!scalar_check (boundary, 2))
2214 return false;
2216 else if (boundary->rank == array->rank - 1)
2218 if (!gfc_check_conformance (shift, boundary,
2219 "arguments '%s' and '%s' for "
2220 "intrinsic %s",
2221 gfc_current_intrinsic_arg[1]->name,
2222 gfc_current_intrinsic_arg[2]->name,
2223 gfc_current_intrinsic))
2224 return false;
2226 else
2228 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2229 "rank %d or be a scalar",
2230 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2231 &shift->where, array->rank - 1);
2232 return false;
2236 return true;
2239 bool
2240 gfc_check_float (gfc_expr *a)
2242 if (!type_check (a, 0, BT_INTEGER))
2243 return false;
2245 if ((a->ts.kind != gfc_default_integer_kind)
2246 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2247 "kind argument to %s intrinsic at %L",
2248 gfc_current_intrinsic, &a->where))
2249 return false;
2251 return true;
2254 /* A single complex argument. */
2256 bool
2257 gfc_check_fn_c (gfc_expr *a)
2259 if (!type_check (a, 0, BT_COMPLEX))
2260 return false;
2262 return true;
2265 /* A single real argument. */
2267 bool
2268 gfc_check_fn_r (gfc_expr *a)
2270 if (!type_check (a, 0, BT_REAL))
2271 return false;
2273 return true;
2276 /* A single double argument. */
2278 bool
2279 gfc_check_fn_d (gfc_expr *a)
2281 if (!double_check (a, 0))
2282 return false;
2284 return true;
2287 /* A single real or complex argument. */
2289 bool
2290 gfc_check_fn_rc (gfc_expr *a)
2292 if (!real_or_complex_check (a, 0))
2293 return false;
2295 return true;
2299 bool
2300 gfc_check_fn_rc2008 (gfc_expr *a)
2302 if (!real_or_complex_check (a, 0))
2303 return false;
2305 if (a->ts.type == BT_COMPLEX
2306 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2307 "of %qs intrinsic at %L",
2308 gfc_current_intrinsic_arg[0]->name,
2309 gfc_current_intrinsic, &a->where))
2310 return false;
2312 return true;
2316 bool
2317 gfc_check_fnum (gfc_expr *unit)
2319 if (!type_check (unit, 0, BT_INTEGER))
2320 return false;
2322 if (!scalar_check (unit, 0))
2323 return false;
2325 return true;
2329 bool
2330 gfc_check_huge (gfc_expr *x)
2332 if (!int_or_real_check (x, 0))
2333 return false;
2335 return true;
2339 bool
2340 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2342 if (!type_check (x, 0, BT_REAL))
2343 return false;
2344 if (!same_type_check (x, 0, y, 1))
2345 return false;
2347 return true;
2351 /* Check that the single argument is an integer. */
2353 bool
2354 gfc_check_i (gfc_expr *i)
2356 if (!type_check (i, 0, BT_INTEGER))
2357 return false;
2359 return true;
2363 bool
2364 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2366 if (!type_check (i, 0, BT_INTEGER))
2367 return false;
2369 if (!type_check (j, 1, BT_INTEGER))
2370 return false;
2372 if (i->ts.kind != j->ts.kind)
2374 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2375 &i->where))
2376 return false;
2379 return true;
2383 bool
2384 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2386 if (!type_check (i, 0, BT_INTEGER))
2387 return false;
2389 if (!type_check (pos, 1, BT_INTEGER))
2390 return false;
2392 if (!type_check (len, 2, BT_INTEGER))
2393 return false;
2395 if (!nonnegative_check ("pos", pos))
2396 return false;
2398 if (!nonnegative_check ("len", len))
2399 return false;
2401 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2402 return false;
2404 return true;
2408 bool
2409 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2411 int i;
2413 if (!type_check (c, 0, BT_CHARACTER))
2414 return false;
2416 if (!kind_check (kind, 1, BT_INTEGER))
2417 return false;
2419 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2420 "with KIND argument at %L",
2421 gfc_current_intrinsic, &kind->where))
2422 return false;
2424 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2426 gfc_expr *start;
2427 gfc_expr *end;
2428 gfc_ref *ref;
2430 /* Substring references don't have the charlength set. */
2431 ref = c->ref;
2432 while (ref && ref->type != REF_SUBSTRING)
2433 ref = ref->next;
2435 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2437 if (!ref)
2439 /* Check that the argument is length one. Non-constant lengths
2440 can't be checked here, so assume they are ok. */
2441 if (c->ts.u.cl && c->ts.u.cl->length)
2443 /* If we already have a length for this expression then use it. */
2444 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2445 return true;
2446 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2448 else
2449 return true;
2451 else
2453 start = ref->u.ss.start;
2454 end = ref->u.ss.end;
2456 gcc_assert (start);
2457 if (end == NULL || end->expr_type != EXPR_CONSTANT
2458 || start->expr_type != EXPR_CONSTANT)
2459 return true;
2461 i = mpz_get_si (end->value.integer) + 1
2462 - mpz_get_si (start->value.integer);
2465 else
2466 return true;
2468 if (i != 1)
2470 gfc_error ("Argument of %s at %L must be of length one",
2471 gfc_current_intrinsic, &c->where);
2472 return false;
2475 return true;
2479 bool
2480 gfc_check_idnint (gfc_expr *a)
2482 if (!double_check (a, 0))
2483 return false;
2485 return true;
2489 bool
2490 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2492 if (!type_check (i, 0, BT_INTEGER))
2493 return false;
2495 if (!type_check (j, 1, BT_INTEGER))
2496 return false;
2498 if (i->ts.kind != j->ts.kind)
2500 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2501 &i->where))
2502 return false;
2505 return true;
2509 bool
2510 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2511 gfc_expr *kind)
2513 if (!type_check (string, 0, BT_CHARACTER)
2514 || !type_check (substring, 1, BT_CHARACTER))
2515 return false;
2517 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2518 return false;
2520 if (!kind_check (kind, 3, BT_INTEGER))
2521 return false;
2522 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2523 "with KIND argument at %L",
2524 gfc_current_intrinsic, &kind->where))
2525 return false;
2527 if (string->ts.kind != substring->ts.kind)
2529 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2530 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2531 gfc_current_intrinsic, &substring->where,
2532 gfc_current_intrinsic_arg[0]->name);
2533 return false;
2536 return true;
2540 bool
2541 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2543 if (!numeric_check (x, 0))
2544 return false;
2546 if (!kind_check (kind, 1, BT_INTEGER))
2547 return false;
2549 return true;
2553 bool
2554 gfc_check_intconv (gfc_expr *x)
2556 if (!numeric_check (x, 0))
2557 return false;
2559 return true;
2563 bool
2564 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2566 if (!type_check (i, 0, BT_INTEGER))
2567 return false;
2569 if (!type_check (j, 1, BT_INTEGER))
2570 return false;
2572 if (i->ts.kind != j->ts.kind)
2574 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2575 &i->where))
2576 return false;
2579 return true;
2583 bool
2584 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2586 if (!type_check (i, 0, BT_INTEGER)
2587 || !type_check (shift, 1, BT_INTEGER))
2588 return false;
2590 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2591 return false;
2593 return true;
2597 bool
2598 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2600 if (!type_check (i, 0, BT_INTEGER)
2601 || !type_check (shift, 1, BT_INTEGER))
2602 return false;
2604 if (size != NULL)
2606 int i2, i3;
2608 if (!type_check (size, 2, BT_INTEGER))
2609 return false;
2611 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2612 return false;
2614 if (size->expr_type == EXPR_CONSTANT)
2616 gfc_extract_int (size, &i3);
2617 if (i3 <= 0)
2619 gfc_error ("SIZE at %L must be positive", &size->where);
2620 return false;
2623 if (shift->expr_type == EXPR_CONSTANT)
2625 gfc_extract_int (shift, &i2);
2626 if (i2 < 0)
2627 i2 = -i2;
2629 if (i2 > i3)
2631 gfc_error ("The absolute value of SHIFT at %L must be less "
2632 "than or equal to SIZE at %L", &shift->where,
2633 &size->where);
2634 return false;
2639 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2640 return false;
2642 return true;
2646 bool
2647 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2649 if (!type_check (pid, 0, BT_INTEGER))
2650 return false;
2652 if (!type_check (sig, 1, BT_INTEGER))
2653 return false;
2655 return true;
2659 bool
2660 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2662 if (!type_check (pid, 0, BT_INTEGER))
2663 return false;
2665 if (!scalar_check (pid, 0))
2666 return false;
2668 if (!type_check (sig, 1, BT_INTEGER))
2669 return false;
2671 if (!scalar_check (sig, 1))
2672 return false;
2674 if (status == NULL)
2675 return true;
2677 if (!type_check (status, 2, BT_INTEGER))
2678 return false;
2680 if (!scalar_check (status, 2))
2681 return false;
2683 return true;
2687 bool
2688 gfc_check_kind (gfc_expr *x)
2690 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2692 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2693 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2694 gfc_current_intrinsic, &x->where);
2695 return false;
2697 if (x->ts.type == BT_PROCEDURE)
2699 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2700 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2701 &x->where);
2702 return false;
2705 return true;
2709 bool
2710 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2712 if (!array_check (array, 0))
2713 return false;
2715 if (!dim_check (dim, 1, false))
2716 return false;
2718 if (!dim_rank_check (dim, array, 1))
2719 return false;
2721 if (!kind_check (kind, 2, BT_INTEGER))
2722 return false;
2723 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2724 "with KIND argument at %L",
2725 gfc_current_intrinsic, &kind->where))
2726 return false;
2728 return true;
2732 bool
2733 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2735 if (flag_coarray == GFC_FCOARRAY_NONE)
2737 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2738 return false;
2741 if (!coarray_check (coarray, 0))
2742 return false;
2744 if (dim != NULL)
2746 if (!dim_check (dim, 1, false))
2747 return false;
2749 if (!dim_corank_check (dim, coarray))
2750 return false;
2753 if (!kind_check (kind, 2, BT_INTEGER))
2754 return false;
2756 return true;
2760 bool
2761 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2763 if (!type_check (s, 0, BT_CHARACTER))
2764 return false;
2766 if (!kind_check (kind, 1, BT_INTEGER))
2767 return false;
2768 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2769 "with KIND argument at %L",
2770 gfc_current_intrinsic, &kind->where))
2771 return false;
2773 return true;
2777 bool
2778 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2780 if (!type_check (a, 0, BT_CHARACTER))
2781 return false;
2782 if (!kind_value_check (a, 0, gfc_default_character_kind))
2783 return false;
2785 if (!type_check (b, 1, BT_CHARACTER))
2786 return false;
2787 if (!kind_value_check (b, 1, gfc_default_character_kind))
2788 return false;
2790 return true;
2794 bool
2795 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2797 if (!type_check (path1, 0, BT_CHARACTER))
2798 return false;
2799 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2800 return false;
2802 if (!type_check (path2, 1, BT_CHARACTER))
2803 return false;
2804 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2805 return false;
2807 return true;
2811 bool
2812 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2814 if (!type_check (path1, 0, BT_CHARACTER))
2815 return false;
2816 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2817 return false;
2819 if (!type_check (path2, 1, BT_CHARACTER))
2820 return false;
2821 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2822 return false;
2824 if (status == NULL)
2825 return true;
2827 if (!type_check (status, 2, BT_INTEGER))
2828 return false;
2830 if (!scalar_check (status, 2))
2831 return false;
2833 return true;
2837 bool
2838 gfc_check_loc (gfc_expr *expr)
2840 return variable_check (expr, 0, true);
2844 bool
2845 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2847 if (!type_check (path1, 0, BT_CHARACTER))
2848 return false;
2849 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2850 return false;
2852 if (!type_check (path2, 1, BT_CHARACTER))
2853 return false;
2854 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2855 return false;
2857 return true;
2861 bool
2862 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2864 if (!type_check (path1, 0, BT_CHARACTER))
2865 return false;
2866 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2867 return false;
2869 if (!type_check (path2, 1, BT_CHARACTER))
2870 return false;
2871 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2872 return false;
2874 if (status == NULL)
2875 return true;
2877 if (!type_check (status, 2, BT_INTEGER))
2878 return false;
2880 if (!scalar_check (status, 2))
2881 return false;
2883 return true;
2887 bool
2888 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2890 if (!type_check (a, 0, BT_LOGICAL))
2891 return false;
2892 if (!kind_check (kind, 1, BT_LOGICAL))
2893 return false;
2895 return true;
2899 /* Min/max family. */
2901 static bool
2902 min_max_args (gfc_actual_arglist *args)
2904 gfc_actual_arglist *arg;
2905 int i, j, nargs, *nlabels, nlabelless;
2906 bool a1 = false, a2 = false;
2908 if (args == NULL || args->next == NULL)
2910 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
2911 gfc_current_intrinsic, gfc_current_intrinsic_where);
2912 return false;
2915 if (!args->name)
2916 a1 = true;
2918 if (!args->next->name)
2919 a2 = true;
2921 nargs = 0;
2922 for (arg = args; arg; arg = arg->next)
2923 if (arg->name)
2924 nargs++;
2926 if (nargs == 0)
2927 return true;
2929 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2930 nlabelless = 0;
2931 nlabels = XALLOCAVEC (int, nargs);
2932 for (arg = args, i = 0; arg; arg = arg->next, i++)
2933 if (arg->name)
2935 int n;
2936 char *endp;
2938 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2939 goto unknown;
2940 n = strtol (&arg->name[1], &endp, 10);
2941 if (endp[0] != '\0')
2942 goto unknown;
2943 if (n <= 0)
2944 goto unknown;
2945 if (n <= nlabelless)
2946 goto duplicate;
2947 nlabels[i] = n;
2948 if (n == 1)
2949 a1 = true;
2950 if (n == 2)
2951 a2 = true;
2953 else
2954 nlabelless++;
2956 if (!a1 || !a2)
2958 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
2959 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2960 gfc_current_intrinsic_where);
2961 return false;
2964 /* Check for duplicates. */
2965 for (i = 0; i < nargs; i++)
2966 for (j = i + 1; j < nargs; j++)
2967 if (nlabels[i] == nlabels[j])
2968 goto duplicate;
2970 return true;
2972 duplicate:
2973 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
2974 &arg->expr->where, gfc_current_intrinsic);
2975 return false;
2977 unknown:
2978 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
2979 &arg->expr->where, gfc_current_intrinsic);
2980 return false;
2984 static bool
2985 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2987 gfc_actual_arglist *arg, *tmp;
2988 gfc_expr *x;
2989 int m, n;
2991 if (!min_max_args (arglist))
2992 return false;
2994 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2996 x = arg->expr;
2997 if (x->ts.type != type || x->ts.kind != kind)
2999 if (x->ts.type == type)
3001 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
3002 "kinds at %L", &x->where))
3003 return false;
3005 else
3007 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
3008 "%s(%d)", n, gfc_current_intrinsic, &x->where,
3009 gfc_basic_typename (type), kind);
3010 return false;
3014 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
3015 if (!gfc_check_conformance (tmp->expr, x,
3016 "arguments 'a%d' and 'a%d' for "
3017 "intrinsic '%s'", m, n,
3018 gfc_current_intrinsic))
3019 return false;
3022 return true;
3026 bool
3027 gfc_check_min_max (gfc_actual_arglist *arg)
3029 gfc_expr *x;
3031 if (!min_max_args (arg))
3032 return false;
3034 x = arg->expr;
3036 if (x->ts.type == BT_CHARACTER)
3038 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3039 "with CHARACTER argument at %L",
3040 gfc_current_intrinsic, &x->where))
3041 return false;
3043 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
3045 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
3046 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
3047 return false;
3050 return check_rest (x->ts.type, x->ts.kind, arg);
3054 bool
3055 gfc_check_min_max_integer (gfc_actual_arglist *arg)
3057 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
3061 bool
3062 gfc_check_min_max_real (gfc_actual_arglist *arg)
3064 return check_rest (BT_REAL, gfc_default_real_kind, arg);
3068 bool
3069 gfc_check_min_max_double (gfc_actual_arglist *arg)
3071 return check_rest (BT_REAL, gfc_default_double_kind, arg);
3075 /* End of min/max family. */
3077 bool
3078 gfc_check_malloc (gfc_expr *size)
3080 if (!type_check (size, 0, BT_INTEGER))
3081 return false;
3083 if (!scalar_check (size, 0))
3084 return false;
3086 return true;
3090 bool
3091 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3093 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3095 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3096 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3097 gfc_current_intrinsic, &matrix_a->where);
3098 return false;
3101 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3103 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3104 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3105 gfc_current_intrinsic, &matrix_b->where);
3106 return false;
3109 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3110 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3112 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3113 gfc_current_intrinsic, &matrix_a->where,
3114 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3115 return false;
3118 switch (matrix_a->rank)
3120 case 1:
3121 if (!rank_check (matrix_b, 1, 2))
3122 return false;
3123 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3124 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3126 gfc_error ("Different shape on dimension 1 for arguments %qs "
3127 "and %qs at %L for intrinsic matmul",
3128 gfc_current_intrinsic_arg[0]->name,
3129 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3130 return false;
3132 break;
3134 case 2:
3135 if (matrix_b->rank != 2)
3137 if (!rank_check (matrix_b, 1, 1))
3138 return false;
3140 /* matrix_b has rank 1 or 2 here. Common check for the cases
3141 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3142 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3143 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3145 gfc_error ("Different shape on dimension 2 for argument %qs and "
3146 "dimension 1 for argument %qs at %L for intrinsic "
3147 "matmul", gfc_current_intrinsic_arg[0]->name,
3148 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3149 return false;
3151 break;
3153 default:
3154 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3155 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3156 gfc_current_intrinsic, &matrix_a->where);
3157 return false;
3160 return true;
3164 /* Whoever came up with this interface was probably on something.
3165 The possibilities for the occupation of the second and third
3166 parameters are:
3168 Arg #2 Arg #3
3169 NULL NULL
3170 DIM NULL
3171 MASK NULL
3172 NULL MASK minloc(array, mask=m)
3173 DIM MASK
3175 I.e. in the case of minloc(array,mask), mask will be in the second
3176 position of the argument list and we'll have to fix that up. */
3178 bool
3179 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3181 gfc_expr *a, *m, *d;
3183 a = ap->expr;
3184 if (!int_or_real_check (a, 0) || !array_check (a, 0))
3185 return false;
3187 d = ap->next->expr;
3188 m = ap->next->next->expr;
3190 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3191 && ap->next->name == NULL)
3193 m = d;
3194 d = NULL;
3195 ap->next->expr = NULL;
3196 ap->next->next->expr = m;
3199 if (!dim_check (d, 1, false))
3200 return false;
3202 if (!dim_rank_check (d, a, 0))
3203 return false;
3205 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3206 return false;
3208 if (m != NULL
3209 && !gfc_check_conformance (a, m,
3210 "arguments '%s' and '%s' for intrinsic %s",
3211 gfc_current_intrinsic_arg[0]->name,
3212 gfc_current_intrinsic_arg[2]->name,
3213 gfc_current_intrinsic))
3214 return false;
3216 return true;
3220 /* Similar to minloc/maxloc, the argument list might need to be
3221 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3222 difference is that MINLOC/MAXLOC take an additional KIND argument.
3223 The possibilities are:
3225 Arg #2 Arg #3
3226 NULL NULL
3227 DIM NULL
3228 MASK NULL
3229 NULL MASK minval(array, mask=m)
3230 DIM MASK
3232 I.e. in the case of minval(array,mask), mask will be in the second
3233 position of the argument list and we'll have to fix that up. */
3235 static bool
3236 check_reduction (gfc_actual_arglist *ap)
3238 gfc_expr *a, *m, *d;
3240 a = ap->expr;
3241 d = ap->next->expr;
3242 m = ap->next->next->expr;
3244 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3245 && ap->next->name == NULL)
3247 m = d;
3248 d = NULL;
3249 ap->next->expr = NULL;
3250 ap->next->next->expr = m;
3253 if (!dim_check (d, 1, false))
3254 return false;
3256 if (!dim_rank_check (d, a, 0))
3257 return false;
3259 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3260 return false;
3262 if (m != NULL
3263 && !gfc_check_conformance (a, m,
3264 "arguments '%s' and '%s' for intrinsic %s",
3265 gfc_current_intrinsic_arg[0]->name,
3266 gfc_current_intrinsic_arg[2]->name,
3267 gfc_current_intrinsic))
3268 return false;
3270 return true;
3274 bool
3275 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3277 if (!int_or_real_check (ap->expr, 0)
3278 || !array_check (ap->expr, 0))
3279 return false;
3281 return check_reduction (ap);
3285 bool
3286 gfc_check_product_sum (gfc_actual_arglist *ap)
3288 if (!numeric_check (ap->expr, 0)
3289 || !array_check (ap->expr, 0))
3290 return false;
3292 return check_reduction (ap);
3296 /* For IANY, IALL and IPARITY. */
3298 bool
3299 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3301 int k;
3303 if (!type_check (i, 0, BT_INTEGER))
3304 return false;
3306 if (!nonnegative_check ("I", i))
3307 return false;
3309 if (!kind_check (kind, 1, BT_INTEGER))
3310 return false;
3312 if (kind)
3313 gfc_extract_int (kind, &k);
3314 else
3315 k = gfc_default_integer_kind;
3317 if (!less_than_bitsizekind ("I", i, k))
3318 return false;
3320 return true;
3324 bool
3325 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3327 if (ap->expr->ts.type != BT_INTEGER)
3329 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3330 gfc_current_intrinsic_arg[0]->name,
3331 gfc_current_intrinsic, &ap->expr->where);
3332 return false;
3335 if (!array_check (ap->expr, 0))
3336 return false;
3338 return check_reduction (ap);
3342 bool
3343 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3345 if (!same_type_check (tsource, 0, fsource, 1))
3346 return false;
3348 if (!type_check (mask, 2, BT_LOGICAL))
3349 return false;
3351 if (tsource->ts.type == BT_CHARACTER)
3352 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3354 return true;
3358 bool
3359 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3361 if (!type_check (i, 0, BT_INTEGER))
3362 return false;
3364 if (!type_check (j, 1, BT_INTEGER))
3365 return false;
3367 if (!type_check (mask, 2, BT_INTEGER))
3368 return false;
3370 if (!same_type_check (i, 0, j, 1))
3371 return false;
3373 if (!same_type_check (i, 0, mask, 2))
3374 return false;
3376 return true;
3380 bool
3381 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3383 if (!variable_check (from, 0, false))
3384 return false;
3385 if (!allocatable_check (from, 0))
3386 return false;
3387 if (gfc_is_coindexed (from))
3389 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3390 "coindexed", &from->where);
3391 return false;
3394 if (!variable_check (to, 1, false))
3395 return false;
3396 if (!allocatable_check (to, 1))
3397 return false;
3398 if (gfc_is_coindexed (to))
3400 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3401 "coindexed", &to->where);
3402 return false;
3405 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3407 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3408 "polymorphic if FROM is polymorphic",
3409 &to->where);
3410 return false;
3413 if (!same_type_check (to, 1, from, 0))
3414 return false;
3416 if (to->rank != from->rank)
3418 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3419 "must have the same rank %d/%d", &to->where, from->rank,
3420 to->rank);
3421 return false;
3424 /* IR F08/0040; cf. 12-006A. */
3425 if (gfc_get_corank (to) != gfc_get_corank (from))
3427 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3428 "must have the same corank %d/%d", &to->where,
3429 gfc_get_corank (from), gfc_get_corank (to));
3430 return false;
3433 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3434 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3435 and cmp2 are allocatable. After the allocation is transferred,
3436 the 'to' chain is broken by the nullification of the 'from'. A bit
3437 of reflection reveals that this can only occur for derived types
3438 with recursive allocatable components. */
3439 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3440 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3442 gfc_ref *to_ref, *from_ref;
3443 to_ref = to->ref;
3444 from_ref = from->ref;
3445 bool aliasing = true;
3447 for (; from_ref && to_ref;
3448 from_ref = from_ref->next, to_ref = to_ref->next)
3450 if (to_ref->type != from->ref->type)
3451 aliasing = false;
3452 else if (to_ref->type == REF_ARRAY
3453 && to_ref->u.ar.type != AR_FULL
3454 && from_ref->u.ar.type != AR_FULL)
3455 /* Play safe; assume sections and elements are different. */
3456 aliasing = false;
3457 else if (to_ref->type == REF_COMPONENT
3458 && to_ref->u.c.component != from_ref->u.c.component)
3459 aliasing = false;
3461 if (!aliasing)
3462 break;
3465 if (aliasing)
3467 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3468 "restrictions (F2003 12.4.1.7)", &to->where);
3469 return false;
3473 /* CLASS arguments: Make sure the vtab of from is present. */
3474 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3475 gfc_find_vtab (&from->ts);
3477 return true;
3481 bool
3482 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3484 if (!type_check (x, 0, BT_REAL))
3485 return false;
3487 if (!type_check (s, 1, BT_REAL))
3488 return false;
3490 if (s->expr_type == EXPR_CONSTANT)
3492 if (mpfr_sgn (s->value.real) == 0)
3494 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3495 &s->where);
3496 return false;
3500 return true;
3504 bool
3505 gfc_check_new_line (gfc_expr *a)
3507 if (!type_check (a, 0, BT_CHARACTER))
3508 return false;
3510 return true;
3514 bool
3515 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3517 if (!type_check (array, 0, BT_REAL))
3518 return false;
3520 if (!array_check (array, 0))
3521 return false;
3523 if (!dim_rank_check (dim, array, false))
3524 return false;
3526 return true;
3529 bool
3530 gfc_check_null (gfc_expr *mold)
3532 symbol_attribute attr;
3534 if (mold == NULL)
3535 return true;
3537 if (!variable_check (mold, 0, true))
3538 return false;
3540 attr = gfc_variable_attr (mold, NULL);
3542 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3544 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3545 "ALLOCATABLE or procedure pointer",
3546 gfc_current_intrinsic_arg[0]->name,
3547 gfc_current_intrinsic, &mold->where);
3548 return false;
3551 if (attr.allocatable
3552 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3553 "allocatable MOLD at %L", &mold->where))
3554 return false;
3556 /* F2008, C1242. */
3557 if (gfc_is_coindexed (mold))
3559 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3560 "coindexed", gfc_current_intrinsic_arg[0]->name,
3561 gfc_current_intrinsic, &mold->where);
3562 return false;
3565 return true;
3569 bool
3570 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3572 if (!array_check (array, 0))
3573 return false;
3575 if (!type_check (mask, 1, BT_LOGICAL))
3576 return false;
3578 if (!gfc_check_conformance (array, mask,
3579 "arguments '%s' and '%s' for intrinsic '%s'",
3580 gfc_current_intrinsic_arg[0]->name,
3581 gfc_current_intrinsic_arg[1]->name,
3582 gfc_current_intrinsic))
3583 return false;
3585 if (vector != NULL)
3587 mpz_t array_size, vector_size;
3588 bool have_array_size, have_vector_size;
3590 if (!same_type_check (array, 0, vector, 2))
3591 return false;
3593 if (!rank_check (vector, 2, 1))
3594 return false;
3596 /* VECTOR requires at least as many elements as MASK
3597 has .TRUE. values. */
3598 have_array_size = gfc_array_size(array, &array_size);
3599 have_vector_size = gfc_array_size(vector, &vector_size);
3601 if (have_vector_size
3602 && (mask->expr_type == EXPR_ARRAY
3603 || (mask->expr_type == EXPR_CONSTANT
3604 && have_array_size)))
3606 int mask_true_values = 0;
3608 if (mask->expr_type == EXPR_ARRAY)
3610 gfc_constructor *mask_ctor;
3611 mask_ctor = gfc_constructor_first (mask->value.constructor);
3612 while (mask_ctor)
3614 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3616 mask_true_values = 0;
3617 break;
3620 if (mask_ctor->expr->value.logical)
3621 mask_true_values++;
3623 mask_ctor = gfc_constructor_next (mask_ctor);
3626 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3627 mask_true_values = mpz_get_si (array_size);
3629 if (mpz_get_si (vector_size) < mask_true_values)
3631 gfc_error ("%qs argument of %qs intrinsic at %L must "
3632 "provide at least as many elements as there "
3633 "are .TRUE. values in %qs (%ld/%d)",
3634 gfc_current_intrinsic_arg[2]->name,
3635 gfc_current_intrinsic, &vector->where,
3636 gfc_current_intrinsic_arg[1]->name,
3637 mpz_get_si (vector_size), mask_true_values);
3638 return false;
3642 if (have_array_size)
3643 mpz_clear (array_size);
3644 if (have_vector_size)
3645 mpz_clear (vector_size);
3648 return true;
3652 bool
3653 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3655 if (!type_check (mask, 0, BT_LOGICAL))
3656 return false;
3658 if (!array_check (mask, 0))
3659 return false;
3661 if (!dim_rank_check (dim, mask, false))
3662 return false;
3664 return true;
3668 bool
3669 gfc_check_precision (gfc_expr *x)
3671 if (!real_or_complex_check (x, 0))
3672 return false;
3674 return true;
3678 bool
3679 gfc_check_present (gfc_expr *a)
3681 gfc_symbol *sym;
3683 if (!variable_check (a, 0, true))
3684 return false;
3686 sym = a->symtree->n.sym;
3687 if (!sym->attr.dummy)
3689 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3690 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3691 gfc_current_intrinsic, &a->where);
3692 return false;
3695 if (!sym->attr.optional)
3697 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3698 "an OPTIONAL dummy variable",
3699 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3700 &a->where);
3701 return false;
3704 /* 13.14.82 PRESENT(A)
3705 ......
3706 Argument. A shall be the name of an optional dummy argument that is
3707 accessible in the subprogram in which the PRESENT function reference
3708 appears... */
3710 if (a->ref != NULL
3711 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3712 && (a->ref->u.ar.type == AR_FULL
3713 || (a->ref->u.ar.type == AR_ELEMENT
3714 && a->ref->u.ar.as->rank == 0))))
3716 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3717 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3718 gfc_current_intrinsic, &a->where, sym->name);
3719 return false;
3722 return true;
3726 bool
3727 gfc_check_radix (gfc_expr *x)
3729 if (!int_or_real_check (x, 0))
3730 return false;
3732 return true;
3736 bool
3737 gfc_check_range (gfc_expr *x)
3739 if (!numeric_check (x, 0))
3740 return false;
3742 return true;
3746 bool
3747 gfc_check_rank (gfc_expr *a)
3749 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3750 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3752 bool is_variable = true;
3754 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3755 if (a->expr_type == EXPR_FUNCTION)
3756 is_variable = a->value.function.esym
3757 ? a->value.function.esym->result->attr.pointer
3758 : a->symtree->n.sym->result->attr.pointer;
3760 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3761 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3762 || !is_variable)
3764 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3765 "object", &a->where);
3766 return false;
3769 return true;
3773 /* real, float, sngl. */
3774 bool
3775 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3777 if (!numeric_check (a, 0))
3778 return false;
3780 if (!kind_check (kind, 1, BT_REAL))
3781 return false;
3783 return true;
3787 bool
3788 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3790 if (!type_check (path1, 0, BT_CHARACTER))
3791 return false;
3792 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3793 return false;
3795 if (!type_check (path2, 1, BT_CHARACTER))
3796 return false;
3797 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3798 return false;
3800 return true;
3804 bool
3805 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3807 if (!type_check (path1, 0, BT_CHARACTER))
3808 return false;
3809 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3810 return false;
3812 if (!type_check (path2, 1, BT_CHARACTER))
3813 return false;
3814 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3815 return false;
3817 if (status == NULL)
3818 return true;
3820 if (!type_check (status, 2, BT_INTEGER))
3821 return false;
3823 if (!scalar_check (status, 2))
3824 return false;
3826 return true;
3830 bool
3831 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3833 if (!type_check (x, 0, BT_CHARACTER))
3834 return false;
3836 if (!scalar_check (x, 0))
3837 return false;
3839 if (!type_check (y, 0, BT_INTEGER))
3840 return false;
3842 if (!scalar_check (y, 1))
3843 return false;
3845 return true;
3849 bool
3850 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3851 gfc_expr *pad, gfc_expr *order)
3853 mpz_t size;
3854 mpz_t nelems;
3855 int shape_size;
3857 if (!array_check (source, 0))
3858 return false;
3860 if (!rank_check (shape, 1, 1))
3861 return false;
3863 if (!type_check (shape, 1, BT_INTEGER))
3864 return false;
3866 if (!gfc_array_size (shape, &size))
3868 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3869 "array of constant size", &shape->where);
3870 return false;
3873 shape_size = mpz_get_ui (size);
3874 mpz_clear (size);
3876 if (shape_size <= 0)
3878 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3879 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3880 &shape->where);
3881 return false;
3883 else if (shape_size > GFC_MAX_DIMENSIONS)
3885 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
3886 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3887 return false;
3889 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3891 gfc_expr *e;
3892 int i, extent;
3893 for (i = 0; i < shape_size; ++i)
3895 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3896 if (e->expr_type != EXPR_CONSTANT)
3897 continue;
3899 gfc_extract_int (e, &extent);
3900 if (extent < 0)
3902 gfc_error ("%qs argument of %qs intrinsic at %L has "
3903 "negative element (%d)",
3904 gfc_current_intrinsic_arg[1]->name,
3905 gfc_current_intrinsic, &e->where, extent);
3906 return false;
3910 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
3911 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
3912 && shape->ref->u.ar.as
3913 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
3914 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
3915 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
3916 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
3917 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
3919 int i, extent;
3920 gfc_expr *e, *v;
3922 v = shape->symtree->n.sym->value;
3924 for (i = 0; i < shape_size; i++)
3926 e = gfc_constructor_lookup_expr (v->value.constructor, i);
3927 if (e == NULL)
3928 break;
3930 gfc_extract_int (e, &extent);
3932 if (extent < 0)
3934 gfc_error ("Element %d of actual argument of RESHAPE at %L "
3935 "cannot be negative", i + 1, &shape->where);
3936 return false;
3941 if (pad != NULL)
3943 if (!same_type_check (source, 0, pad, 2))
3944 return false;
3946 if (!array_check (pad, 2))
3947 return false;
3950 if (order != NULL)
3952 if (!array_check (order, 3))
3953 return false;
3955 if (!type_check (order, 3, BT_INTEGER))
3956 return false;
3958 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
3960 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3961 gfc_expr *e;
3963 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3964 perm[i] = 0;
3966 gfc_array_size (order, &size);
3967 order_size = mpz_get_ui (size);
3968 mpz_clear (size);
3970 if (order_size != shape_size)
3972 gfc_error ("%qs argument of %qs intrinsic at %L "
3973 "has wrong number of elements (%d/%d)",
3974 gfc_current_intrinsic_arg[3]->name,
3975 gfc_current_intrinsic, &order->where,
3976 order_size, shape_size);
3977 return false;
3980 for (i = 1; i <= order_size; ++i)
3982 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3983 if (e->expr_type != EXPR_CONSTANT)
3984 continue;
3986 gfc_extract_int (e, &dim);
3988 if (dim < 1 || dim > order_size)
3990 gfc_error ("%qs argument of %qs intrinsic at %L "
3991 "has out-of-range dimension (%d)",
3992 gfc_current_intrinsic_arg[3]->name,
3993 gfc_current_intrinsic, &e->where, dim);
3994 return false;
3997 if (perm[dim-1] != 0)
3999 gfc_error ("%qs argument of %qs intrinsic at %L has "
4000 "invalid permutation of dimensions (dimension "
4001 "%qd duplicated)",
4002 gfc_current_intrinsic_arg[3]->name,
4003 gfc_current_intrinsic, &e->where, dim);
4004 return false;
4007 perm[dim-1] = 1;
4012 if (pad == NULL && shape->expr_type == EXPR_ARRAY
4013 && gfc_is_constant_expr (shape)
4014 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
4015 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
4017 /* Check the match in size between source and destination. */
4018 if (gfc_array_size (source, &nelems))
4020 gfc_constructor *c;
4021 bool test;
4024 mpz_init_set_ui (size, 1);
4025 for (c = gfc_constructor_first (shape->value.constructor);
4026 c; c = gfc_constructor_next (c))
4027 mpz_mul (size, size, c->expr->value.integer);
4029 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
4030 mpz_clear (nelems);
4031 mpz_clear (size);
4033 if (test)
4035 gfc_error ("Without padding, there are not enough elements "
4036 "in the intrinsic RESHAPE source at %L to match "
4037 "the shape", &source->where);
4038 return false;
4043 return true;
4047 bool
4048 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
4050 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
4052 gfc_error ("%qs argument of %qs intrinsic at %L "
4053 "cannot be of type %s",
4054 gfc_current_intrinsic_arg[0]->name,
4055 gfc_current_intrinsic,
4056 &a->where, gfc_typename (&a->ts));
4057 return false;
4060 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
4062 gfc_error ("%qs argument of %qs intrinsic at %L "
4063 "must be of an extensible type",
4064 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4065 &a->where);
4066 return false;
4069 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
4071 gfc_error ("%qs argument of %qs intrinsic at %L "
4072 "cannot be of type %s",
4073 gfc_current_intrinsic_arg[0]->name,
4074 gfc_current_intrinsic,
4075 &b->where, gfc_typename (&b->ts));
4076 return false;
4079 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4081 gfc_error ("%qs argument of %qs intrinsic at %L "
4082 "must be of an extensible type",
4083 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4084 &b->where);
4085 return false;
4088 return true;
4092 bool
4093 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4095 if (!type_check (x, 0, BT_REAL))
4096 return false;
4098 if (!type_check (i, 1, BT_INTEGER))
4099 return false;
4101 return true;
4105 bool
4106 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4108 if (!type_check (x, 0, BT_CHARACTER))
4109 return false;
4111 if (!type_check (y, 1, BT_CHARACTER))
4112 return false;
4114 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4115 return false;
4117 if (!kind_check (kind, 3, BT_INTEGER))
4118 return false;
4119 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4120 "with KIND argument at %L",
4121 gfc_current_intrinsic, &kind->where))
4122 return false;
4124 if (!same_type_check (x, 0, y, 1))
4125 return false;
4127 return true;
4131 bool
4132 gfc_check_secnds (gfc_expr *r)
4134 if (!type_check (r, 0, BT_REAL))
4135 return false;
4137 if (!kind_value_check (r, 0, 4))
4138 return false;
4140 if (!scalar_check (r, 0))
4141 return false;
4143 return true;
4147 bool
4148 gfc_check_selected_char_kind (gfc_expr *name)
4150 if (!type_check (name, 0, BT_CHARACTER))
4151 return false;
4153 if (!kind_value_check (name, 0, gfc_default_character_kind))
4154 return false;
4156 if (!scalar_check (name, 0))
4157 return false;
4159 return true;
4163 bool
4164 gfc_check_selected_int_kind (gfc_expr *r)
4166 if (!type_check (r, 0, BT_INTEGER))
4167 return false;
4169 if (!scalar_check (r, 0))
4170 return false;
4172 return true;
4176 bool
4177 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4179 if (p == NULL && r == NULL
4180 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4181 " neither %<P%> nor %<R%> argument at %L",
4182 gfc_current_intrinsic_where))
4183 return false;
4185 if (p)
4187 if (!type_check (p, 0, BT_INTEGER))
4188 return false;
4190 if (!scalar_check (p, 0))
4191 return false;
4194 if (r)
4196 if (!type_check (r, 1, BT_INTEGER))
4197 return false;
4199 if (!scalar_check (r, 1))
4200 return false;
4203 if (radix)
4205 if (!type_check (radix, 1, BT_INTEGER))
4206 return false;
4208 if (!scalar_check (radix, 1))
4209 return false;
4211 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4212 "RADIX argument at %L", gfc_current_intrinsic,
4213 &radix->where))
4214 return false;
4217 return true;
4221 bool
4222 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4224 if (!type_check (x, 0, BT_REAL))
4225 return false;
4227 if (!type_check (i, 1, BT_INTEGER))
4228 return false;
4230 return true;
4234 bool
4235 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4237 gfc_array_ref *ar;
4239 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4240 return true;
4242 ar = gfc_find_array_ref (source);
4244 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4246 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4247 "an assumed size array", &source->where);
4248 return false;
4251 if (!kind_check (kind, 1, BT_INTEGER))
4252 return false;
4253 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4254 "with KIND argument at %L",
4255 gfc_current_intrinsic, &kind->where))
4256 return false;
4258 return true;
4262 bool
4263 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4265 if (!type_check (i, 0, BT_INTEGER))
4266 return false;
4268 if (!type_check (shift, 0, BT_INTEGER))
4269 return false;
4271 if (!nonnegative_check ("SHIFT", shift))
4272 return false;
4274 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4275 return false;
4277 return true;
4281 bool
4282 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4284 if (!int_or_real_check (a, 0))
4285 return false;
4287 if (!same_type_check (a, 0, b, 1))
4288 return false;
4290 return true;
4294 bool
4295 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4297 if (!array_check (array, 0))
4298 return false;
4300 if (!dim_check (dim, 1, true))
4301 return false;
4303 if (!dim_rank_check (dim, array, 0))
4304 return false;
4306 if (!kind_check (kind, 2, BT_INTEGER))
4307 return false;
4308 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4309 "with KIND argument at %L",
4310 gfc_current_intrinsic, &kind->where))
4311 return false;
4314 return true;
4318 bool
4319 gfc_check_sizeof (gfc_expr *arg)
4321 if (arg->ts.type == BT_PROCEDURE)
4323 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4324 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4325 &arg->where);
4326 return false;
4329 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4330 if (arg->ts.type == BT_ASSUMED
4331 && (arg->symtree->n.sym->as == NULL
4332 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4333 && arg->symtree->n.sym->as->type != AS_DEFERRED
4334 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4336 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4337 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4338 &arg->where);
4339 return false;
4342 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4343 && arg->symtree->n.sym->as != NULL
4344 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4345 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4347 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4348 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4349 gfc_current_intrinsic, &arg->where);
4350 return false;
4353 return true;
4357 /* Check whether an expression is interoperable. When returning false,
4358 msg is set to a string telling why the expression is not interoperable,
4359 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4360 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4361 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4362 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4363 are permitted. */
4365 static bool
4366 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4368 *msg = NULL;
4370 if (expr->ts.type == BT_CLASS)
4372 *msg = "Expression is polymorphic";
4373 return false;
4376 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4377 && !expr->ts.u.derived->ts.is_iso_c)
4379 *msg = "Expression is a noninteroperable derived type";
4380 return false;
4383 if (expr->ts.type == BT_PROCEDURE)
4385 *msg = "Procedure unexpected as argument";
4386 return false;
4389 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4391 int i;
4392 for (i = 0; gfc_logical_kinds[i].kind; i++)
4393 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4394 return true;
4395 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4396 return false;
4399 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4400 && expr->ts.kind != 1)
4402 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4403 return false;
4406 if (expr->ts.type == BT_CHARACTER) {
4407 if (expr->ts.deferred)
4409 /* TS 29113 allows deferred-length strings as dummy arguments,
4410 but it is not an interoperable type. */
4411 *msg = "Expression shall not be a deferred-length string";
4412 return false;
4415 if (expr->ts.u.cl && expr->ts.u.cl->length
4416 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4417 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4419 if (!c_loc && expr->ts.u.cl
4420 && (!expr->ts.u.cl->length
4421 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4422 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4424 *msg = "Type shall have a character length of 1";
4425 return false;
4429 /* Note: The following checks are about interoperatable variables, Fortran
4430 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4431 is allowed, e.g. assumed-shape arrays with TS 29113. */
4433 if (gfc_is_coarray (expr))
4435 *msg = "Coarrays are not interoperable";
4436 return false;
4439 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4441 gfc_array_ref *ar = gfc_find_array_ref (expr);
4442 if (ar->type != AR_FULL)
4444 *msg = "Only whole-arrays are interoperable";
4445 return false;
4447 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4448 && ar->as->type != AS_ASSUMED_SIZE)
4450 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4451 return false;
4455 return true;
4459 bool
4460 gfc_check_c_sizeof (gfc_expr *arg)
4462 const char *msg;
4464 if (!is_c_interoperable (arg, &msg, false, false))
4466 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4467 "interoperable data entity: %s",
4468 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4469 &arg->where, msg);
4470 return false;
4473 if (arg->ts.type == BT_ASSUMED)
4475 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4476 "TYPE(*)",
4477 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4478 &arg->where);
4479 return false;
4482 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4483 && arg->symtree->n.sym->as != NULL
4484 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4485 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4487 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4488 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4489 gfc_current_intrinsic, &arg->where);
4490 return false;
4493 return true;
4497 bool
4498 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4500 if (c_ptr_1->ts.type != BT_DERIVED
4501 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4502 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4503 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4505 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4506 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4507 return false;
4510 if (!scalar_check (c_ptr_1, 0))
4511 return false;
4513 if (c_ptr_2
4514 && (c_ptr_2->ts.type != BT_DERIVED
4515 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4516 || (c_ptr_1->ts.u.derived->intmod_sym_id
4517 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4519 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4520 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4521 gfc_typename (&c_ptr_1->ts),
4522 gfc_typename (&c_ptr_2->ts));
4523 return false;
4526 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4527 return false;
4529 return true;
4533 bool
4534 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4536 symbol_attribute attr;
4537 const char *msg;
4539 if (cptr->ts.type != BT_DERIVED
4540 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4541 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4543 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4544 "type TYPE(C_PTR)", &cptr->where);
4545 return false;
4548 if (!scalar_check (cptr, 0))
4549 return false;
4551 attr = gfc_expr_attr (fptr);
4553 if (!attr.pointer)
4555 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4556 &fptr->where);
4557 return false;
4560 if (fptr->ts.type == BT_CLASS)
4562 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4563 &fptr->where);
4564 return false;
4567 if (gfc_is_coindexed (fptr))
4569 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4570 "coindexed", &fptr->where);
4571 return false;
4574 if (fptr->rank == 0 && shape)
4576 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4577 "FPTR", &fptr->where);
4578 return false;
4580 else if (fptr->rank && !shape)
4582 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4583 "FPTR at %L", &fptr->where);
4584 return false;
4587 if (shape && !rank_check (shape, 2, 1))
4588 return false;
4590 if (shape && !type_check (shape, 2, BT_INTEGER))
4591 return false;
4593 if (shape)
4595 mpz_t size;
4596 if (gfc_array_size (shape, &size))
4598 if (mpz_cmp_ui (size, fptr->rank) != 0)
4600 mpz_clear (size);
4601 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4602 "size as the RANK of FPTR", &shape->where);
4603 return false;
4605 mpz_clear (size);
4609 if (fptr->ts.type == BT_CLASS)
4611 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4612 return false;
4615 if (!is_c_interoperable (fptr, &msg, false, true))
4616 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4617 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4619 return true;
4623 bool
4624 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4626 symbol_attribute attr;
4628 if (cptr->ts.type != BT_DERIVED
4629 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4630 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4632 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4633 "type TYPE(C_FUNPTR)", &cptr->where);
4634 return false;
4637 if (!scalar_check (cptr, 0))
4638 return false;
4640 attr = gfc_expr_attr (fptr);
4642 if (!attr.proc_pointer)
4644 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4645 "pointer", &fptr->where);
4646 return false;
4649 if (gfc_is_coindexed (fptr))
4651 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4652 "coindexed", &fptr->where);
4653 return false;
4656 if (!attr.is_bind_c)
4657 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4658 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4660 return true;
4664 bool
4665 gfc_check_c_funloc (gfc_expr *x)
4667 symbol_attribute attr;
4669 if (gfc_is_coindexed (x))
4671 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4672 "coindexed", &x->where);
4673 return false;
4676 attr = gfc_expr_attr (x);
4678 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4679 && x->symtree->n.sym == x->symtree->n.sym->result)
4681 gfc_namespace *ns = gfc_current_ns;
4683 for (ns = gfc_current_ns; ns; ns = ns->parent)
4684 if (x->symtree->n.sym == ns->proc_name)
4686 gfc_error ("Function result %qs at %L is invalid as X argument "
4687 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4688 return false;
4692 if (attr.flavor != FL_PROCEDURE)
4694 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4695 "or a procedure pointer", &x->where);
4696 return false;
4699 if (!attr.is_bind_c)
4700 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4701 "at %L to C_FUNLOC", &x->where);
4702 return true;
4706 bool
4707 gfc_check_c_loc (gfc_expr *x)
4709 symbol_attribute attr;
4710 const char *msg;
4712 if (gfc_is_coindexed (x))
4714 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4715 return false;
4718 if (x->ts.type == BT_CLASS)
4720 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4721 &x->where);
4722 return false;
4725 attr = gfc_expr_attr (x);
4727 if (!attr.pointer
4728 && (x->expr_type != EXPR_VARIABLE || !attr.target
4729 || attr.flavor == FL_PARAMETER))
4731 gfc_error ("Argument X at %L to C_LOC shall have either "
4732 "the POINTER or the TARGET attribute", &x->where);
4733 return false;
4736 if (x->ts.type == BT_CHARACTER
4737 && gfc_var_strlen (x) == 0)
4739 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4740 "string", &x->where);
4741 return false;
4744 if (!is_c_interoperable (x, &msg, true, false))
4746 if (x->ts.type == BT_CLASS)
4748 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4749 &x->where);
4750 return false;
4753 if (x->rank
4754 && !gfc_notify_std (GFC_STD_F2008_TS,
4755 "Noninteroperable array at %L as"
4756 " argument to C_LOC: %s", &x->where, msg))
4757 return false;
4759 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4761 gfc_array_ref *ar = gfc_find_array_ref (x);
4763 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4764 && !attr.allocatable
4765 && !gfc_notify_std (GFC_STD_F2008,
4766 "Array of interoperable type at %L "
4767 "to C_LOC which is nonallocatable and neither "
4768 "assumed size nor explicit size", &x->where))
4769 return false;
4770 else if (ar->type != AR_FULL
4771 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4772 "to C_LOC", &x->where))
4773 return false;
4776 return true;
4780 bool
4781 gfc_check_sleep_sub (gfc_expr *seconds)
4783 if (!type_check (seconds, 0, BT_INTEGER))
4784 return false;
4786 if (!scalar_check (seconds, 0))
4787 return false;
4789 return true;
4792 bool
4793 gfc_check_sngl (gfc_expr *a)
4795 if (!type_check (a, 0, BT_REAL))
4796 return false;
4798 if ((a->ts.kind != gfc_default_double_kind)
4799 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4800 "REAL argument to %s intrinsic at %L",
4801 gfc_current_intrinsic, &a->where))
4802 return false;
4804 return true;
4807 bool
4808 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4810 if (source->rank >= GFC_MAX_DIMENSIONS)
4812 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4813 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4814 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4816 return false;
4819 if (dim == NULL)
4820 return false;
4822 if (!dim_check (dim, 1, false))
4823 return false;
4825 /* dim_rank_check() does not apply here. */
4826 if (dim
4827 && dim->expr_type == EXPR_CONSTANT
4828 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4829 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4831 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4832 "dimension index", gfc_current_intrinsic_arg[1]->name,
4833 gfc_current_intrinsic, &dim->where);
4834 return false;
4837 if (!type_check (ncopies, 2, BT_INTEGER))
4838 return false;
4840 if (!scalar_check (ncopies, 2))
4841 return false;
4843 return true;
4847 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4848 functions). */
4850 bool
4851 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4853 if (!type_check (unit, 0, BT_INTEGER))
4854 return false;
4856 if (!scalar_check (unit, 0))
4857 return false;
4859 if (!type_check (c, 1, BT_CHARACTER))
4860 return false;
4861 if (!kind_value_check (c, 1, gfc_default_character_kind))
4862 return false;
4864 if (status == NULL)
4865 return true;
4867 if (!type_check (status, 2, BT_INTEGER)
4868 || !kind_value_check (status, 2, gfc_default_integer_kind)
4869 || !scalar_check (status, 2))
4870 return false;
4872 return true;
4876 bool
4877 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4879 return gfc_check_fgetputc_sub (unit, c, NULL);
4883 bool
4884 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4886 if (!type_check (c, 0, BT_CHARACTER))
4887 return false;
4888 if (!kind_value_check (c, 0, gfc_default_character_kind))
4889 return false;
4891 if (status == NULL)
4892 return true;
4894 if (!type_check (status, 1, BT_INTEGER)
4895 || !kind_value_check (status, 1, gfc_default_integer_kind)
4896 || !scalar_check (status, 1))
4897 return false;
4899 return true;
4903 bool
4904 gfc_check_fgetput (gfc_expr *c)
4906 return gfc_check_fgetput_sub (c, NULL);
4910 bool
4911 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4913 if (!type_check (unit, 0, BT_INTEGER))
4914 return false;
4916 if (!scalar_check (unit, 0))
4917 return false;
4919 if (!type_check (offset, 1, BT_INTEGER))
4920 return false;
4922 if (!scalar_check (offset, 1))
4923 return false;
4925 if (!type_check (whence, 2, BT_INTEGER))
4926 return false;
4928 if (!scalar_check (whence, 2))
4929 return false;
4931 if (status == NULL)
4932 return true;
4934 if (!type_check (status, 3, BT_INTEGER))
4935 return false;
4937 if (!kind_value_check (status, 3, 4))
4938 return false;
4940 if (!scalar_check (status, 3))
4941 return false;
4943 return true;
4948 bool
4949 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4951 if (!type_check (unit, 0, BT_INTEGER))
4952 return false;
4954 if (!scalar_check (unit, 0))
4955 return false;
4957 if (!type_check (array, 1, BT_INTEGER)
4958 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4959 return false;
4961 if (!array_check (array, 1))
4962 return false;
4964 return true;
4968 bool
4969 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4971 if (!type_check (unit, 0, BT_INTEGER))
4972 return false;
4974 if (!scalar_check (unit, 0))
4975 return false;
4977 if (!type_check (array, 1, BT_INTEGER)
4978 || !kind_value_check (array, 1, gfc_default_integer_kind))
4979 return false;
4981 if (!array_check (array, 1))
4982 return false;
4984 if (status == NULL)
4985 return true;
4987 if (!type_check (status, 2, BT_INTEGER)
4988 || !kind_value_check (status, 2, gfc_default_integer_kind))
4989 return false;
4991 if (!scalar_check (status, 2))
4992 return false;
4994 return true;
4998 bool
4999 gfc_check_ftell (gfc_expr *unit)
5001 if (!type_check (unit, 0, BT_INTEGER))
5002 return false;
5004 if (!scalar_check (unit, 0))
5005 return false;
5007 return true;
5011 bool
5012 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
5014 if (!type_check (unit, 0, BT_INTEGER))
5015 return false;
5017 if (!scalar_check (unit, 0))
5018 return false;
5020 if (!type_check (offset, 1, BT_INTEGER))
5021 return false;
5023 if (!scalar_check (offset, 1))
5024 return false;
5026 return true;
5030 bool
5031 gfc_check_stat (gfc_expr *name, gfc_expr *array)
5033 if (!type_check (name, 0, BT_CHARACTER))
5034 return false;
5035 if (!kind_value_check (name, 0, gfc_default_character_kind))
5036 return false;
5038 if (!type_check (array, 1, BT_INTEGER)
5039 || !kind_value_check (array, 1, gfc_default_integer_kind))
5040 return false;
5042 if (!array_check (array, 1))
5043 return false;
5045 return true;
5049 bool
5050 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
5052 if (!type_check (name, 0, BT_CHARACTER))
5053 return false;
5054 if (!kind_value_check (name, 0, gfc_default_character_kind))
5055 return false;
5057 if (!type_check (array, 1, BT_INTEGER)
5058 || !kind_value_check (array, 1, gfc_default_integer_kind))
5059 return false;
5061 if (!array_check (array, 1))
5062 return false;
5064 if (status == NULL)
5065 return true;
5067 if (!type_check (status, 2, BT_INTEGER)
5068 || !kind_value_check (array, 1, gfc_default_integer_kind))
5069 return false;
5071 if (!scalar_check (status, 2))
5072 return false;
5074 return true;
5078 bool
5079 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5081 mpz_t nelems;
5083 if (flag_coarray == GFC_FCOARRAY_NONE)
5085 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5086 return false;
5089 if (!coarray_check (coarray, 0))
5090 return false;
5092 if (sub->rank != 1)
5094 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5095 gfc_current_intrinsic_arg[1]->name, &sub->where);
5096 return false;
5099 if (gfc_array_size (sub, &nelems))
5101 int corank = gfc_get_corank (coarray);
5103 if (mpz_cmp_ui (nelems, corank) != 0)
5105 gfc_error ("The number of array elements of the SUB argument to "
5106 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5107 &sub->where, corank, (int) mpz_get_si (nelems));
5108 mpz_clear (nelems);
5109 return false;
5111 mpz_clear (nelems);
5114 return true;
5118 bool
5119 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5121 if (flag_coarray == GFC_FCOARRAY_NONE)
5123 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5124 return false;
5127 if (distance)
5129 if (!type_check (distance, 0, BT_INTEGER))
5130 return false;
5132 if (!nonnegative_check ("DISTANCE", distance))
5133 return false;
5135 if (!scalar_check (distance, 0))
5136 return false;
5138 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5139 "NUM_IMAGES at %L", &distance->where))
5140 return false;
5143 if (failed)
5145 if (!type_check (failed, 1, BT_LOGICAL))
5146 return false;
5148 if (!scalar_check (failed, 1))
5149 return false;
5151 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
5152 "NUM_IMAGES at %L", &distance->where))
5153 return false;
5156 return true;
5160 bool
5161 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5163 if (flag_coarray == GFC_FCOARRAY_NONE)
5165 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5166 return false;
5169 if (coarray == NULL && dim == NULL && distance == NULL)
5170 return true;
5172 if (dim != NULL && coarray == NULL)
5174 gfc_error ("DIM argument without COARRAY argument not allowed for "
5175 "THIS_IMAGE intrinsic at %L", &dim->where);
5176 return false;
5179 if (distance && (coarray || dim))
5181 gfc_error ("The DISTANCE argument may not be specified together with the "
5182 "COARRAY or DIM argument in intrinsic at %L",
5183 &distance->where);
5184 return false;
5187 /* Assume that we have "this_image (distance)". */
5188 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5190 if (dim)
5192 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5193 &coarray->where);
5194 return false;
5196 distance = coarray;
5199 if (distance)
5201 if (!type_check (distance, 2, BT_INTEGER))
5202 return false;
5204 if (!nonnegative_check ("DISTANCE", distance))
5205 return false;
5207 if (!scalar_check (distance, 2))
5208 return false;
5210 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5211 "THIS_IMAGE at %L", &distance->where))
5212 return false;
5214 return true;
5217 if (!coarray_check (coarray, 0))
5218 return false;
5220 if (dim != NULL)
5222 if (!dim_check (dim, 1, false))
5223 return false;
5225 if (!dim_corank_check (dim, coarray))
5226 return false;
5229 return true;
5232 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5233 by gfc_simplify_transfer. Return false if we cannot do so. */
5235 bool
5236 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5237 size_t *source_size, size_t *result_size,
5238 size_t *result_length_p)
5240 size_t result_elt_size;
5242 if (source->expr_type == EXPR_FUNCTION)
5243 return false;
5245 if (size && size->expr_type != EXPR_CONSTANT)
5246 return false;
5248 /* Calculate the size of the source. */
5249 *source_size = gfc_target_expr_size (source);
5250 if (*source_size == 0)
5251 return false;
5253 /* Determine the size of the element. */
5254 result_elt_size = gfc_element_size (mold);
5255 if (result_elt_size == 0)
5256 return false;
5258 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5260 int result_length;
5262 if (size)
5263 result_length = (size_t)mpz_get_ui (size->value.integer);
5264 else
5266 result_length = *source_size / result_elt_size;
5267 if (result_length * result_elt_size < *source_size)
5268 result_length += 1;
5271 *result_size = result_length * result_elt_size;
5272 if (result_length_p)
5273 *result_length_p = result_length;
5275 else
5276 *result_size = result_elt_size;
5278 return true;
5282 bool
5283 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5285 size_t source_size;
5286 size_t result_size;
5288 if (mold->ts.type == BT_HOLLERITH)
5290 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5291 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5292 return false;
5295 if (size != NULL)
5297 if (!type_check (size, 2, BT_INTEGER))
5298 return false;
5300 if (!scalar_check (size, 2))
5301 return false;
5303 if (!nonoptional_check (size, 2))
5304 return false;
5307 if (!warn_surprising)
5308 return true;
5310 /* If we can't calculate the sizes, we cannot check any more.
5311 Return true for that case. */
5313 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5314 &result_size, NULL))
5315 return true;
5317 if (source_size < result_size)
5318 gfc_warning (OPT_Wsurprising,
5319 "Intrinsic TRANSFER at %L has partly undefined result: "
5320 "source size %ld < result size %ld", &source->where,
5321 (long) source_size, (long) result_size);
5323 return true;
5327 bool
5328 gfc_check_transpose (gfc_expr *matrix)
5330 if (!rank_check (matrix, 0, 2))
5331 return false;
5333 return true;
5337 bool
5338 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5340 if (!array_check (array, 0))
5341 return false;
5343 if (!dim_check (dim, 1, false))
5344 return false;
5346 if (!dim_rank_check (dim, array, 0))
5347 return false;
5349 if (!kind_check (kind, 2, BT_INTEGER))
5350 return false;
5351 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5352 "with KIND argument at %L",
5353 gfc_current_intrinsic, &kind->where))
5354 return false;
5356 return true;
5360 bool
5361 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5363 if (flag_coarray == GFC_FCOARRAY_NONE)
5365 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5366 return false;
5369 if (!coarray_check (coarray, 0))
5370 return false;
5372 if (dim != NULL)
5374 if (!dim_check (dim, 1, false))
5375 return false;
5377 if (!dim_corank_check (dim, coarray))
5378 return false;
5381 if (!kind_check (kind, 2, BT_INTEGER))
5382 return false;
5384 return true;
5388 bool
5389 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5391 mpz_t vector_size;
5393 if (!rank_check (vector, 0, 1))
5394 return false;
5396 if (!array_check (mask, 1))
5397 return false;
5399 if (!type_check (mask, 1, BT_LOGICAL))
5400 return false;
5402 if (!same_type_check (vector, 0, field, 2))
5403 return false;
5405 if (mask->expr_type == EXPR_ARRAY
5406 && gfc_array_size (vector, &vector_size))
5408 int mask_true_count = 0;
5409 gfc_constructor *mask_ctor;
5410 mask_ctor = gfc_constructor_first (mask->value.constructor);
5411 while (mask_ctor)
5413 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5415 mask_true_count = 0;
5416 break;
5419 if (mask_ctor->expr->value.logical)
5420 mask_true_count++;
5422 mask_ctor = gfc_constructor_next (mask_ctor);
5425 if (mpz_get_si (vector_size) < mask_true_count)
5427 gfc_error ("%qs argument of %qs intrinsic at %L must "
5428 "provide at least as many elements as there "
5429 "are .TRUE. values in %qs (%ld/%d)",
5430 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5431 &vector->where, gfc_current_intrinsic_arg[1]->name,
5432 mpz_get_si (vector_size), mask_true_count);
5433 return false;
5436 mpz_clear (vector_size);
5439 if (mask->rank != field->rank && field->rank != 0)
5441 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5442 "the same rank as %qs or be a scalar",
5443 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5444 &field->where, gfc_current_intrinsic_arg[1]->name);
5445 return false;
5448 if (mask->rank == field->rank)
5450 int i;
5451 for (i = 0; i < field->rank; i++)
5452 if (! identical_dimen_shape (mask, i, field, i))
5454 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5455 "must have identical shape.",
5456 gfc_current_intrinsic_arg[2]->name,
5457 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5458 &field->where);
5462 return true;
5466 bool
5467 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5469 if (!type_check (x, 0, BT_CHARACTER))
5470 return false;
5472 if (!same_type_check (x, 0, y, 1))
5473 return false;
5475 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5476 return false;
5478 if (!kind_check (kind, 3, BT_INTEGER))
5479 return false;
5480 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5481 "with KIND argument at %L",
5482 gfc_current_intrinsic, &kind->where))
5483 return false;
5485 return true;
5489 bool
5490 gfc_check_trim (gfc_expr *x)
5492 if (!type_check (x, 0, BT_CHARACTER))
5493 return false;
5495 if (!scalar_check (x, 0))
5496 return false;
5498 return true;
5502 bool
5503 gfc_check_ttynam (gfc_expr *unit)
5505 if (!scalar_check (unit, 0))
5506 return false;
5508 if (!type_check (unit, 0, BT_INTEGER))
5509 return false;
5511 return true;
5515 /* Common check function for the half a dozen intrinsics that have a
5516 single real argument. */
5518 bool
5519 gfc_check_x (gfc_expr *x)
5521 if (!type_check (x, 0, BT_REAL))
5522 return false;
5524 return true;
5528 /************* Check functions for intrinsic subroutines *************/
5530 bool
5531 gfc_check_cpu_time (gfc_expr *time)
5533 if (!scalar_check (time, 0))
5534 return false;
5536 if (!type_check (time, 0, BT_REAL))
5537 return false;
5539 if (!variable_check (time, 0, false))
5540 return false;
5542 return true;
5546 bool
5547 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5548 gfc_expr *zone, gfc_expr *values)
5550 if (date != NULL)
5552 if (!type_check (date, 0, BT_CHARACTER))
5553 return false;
5554 if (!kind_value_check (date, 0, gfc_default_character_kind))
5555 return false;
5556 if (!scalar_check (date, 0))
5557 return false;
5558 if (!variable_check (date, 0, false))
5559 return false;
5562 if (time != NULL)
5564 if (!type_check (time, 1, BT_CHARACTER))
5565 return false;
5566 if (!kind_value_check (time, 1, gfc_default_character_kind))
5567 return false;
5568 if (!scalar_check (time, 1))
5569 return false;
5570 if (!variable_check (time, 1, false))
5571 return false;
5574 if (zone != NULL)
5576 if (!type_check (zone, 2, BT_CHARACTER))
5577 return false;
5578 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5579 return false;
5580 if (!scalar_check (zone, 2))
5581 return false;
5582 if (!variable_check (zone, 2, false))
5583 return false;
5586 if (values != NULL)
5588 if (!type_check (values, 3, BT_INTEGER))
5589 return false;
5590 if (!array_check (values, 3))
5591 return false;
5592 if (!rank_check (values, 3, 1))
5593 return false;
5594 if (!variable_check (values, 3, false))
5595 return false;
5598 return true;
5602 bool
5603 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5604 gfc_expr *to, gfc_expr *topos)
5606 if (!type_check (from, 0, BT_INTEGER))
5607 return false;
5609 if (!type_check (frompos, 1, BT_INTEGER))
5610 return false;
5612 if (!type_check (len, 2, BT_INTEGER))
5613 return false;
5615 if (!same_type_check (from, 0, to, 3))
5616 return false;
5618 if (!variable_check (to, 3, false))
5619 return false;
5621 if (!type_check (topos, 4, BT_INTEGER))
5622 return false;
5624 if (!nonnegative_check ("frompos", frompos))
5625 return false;
5627 if (!nonnegative_check ("topos", topos))
5628 return false;
5630 if (!nonnegative_check ("len", len))
5631 return false;
5633 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5634 return false;
5636 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5637 return false;
5639 return true;
5643 bool
5644 gfc_check_random_number (gfc_expr *harvest)
5646 if (!type_check (harvest, 0, BT_REAL))
5647 return false;
5649 if (!variable_check (harvest, 0, false))
5650 return false;
5652 return true;
5656 bool
5657 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5659 unsigned int nargs = 0, seed_size;
5660 locus *where = NULL;
5661 mpz_t put_size, get_size;
5663 /* Keep the number of bytes in sync with master_state in
5664 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5665 part of the state too. */
5666 seed_size = 128 / gfc_default_integer_kind + 1;
5668 if (size != NULL)
5670 if (size->expr_type != EXPR_VARIABLE
5671 || !size->symtree->n.sym->attr.optional)
5672 nargs++;
5674 if (!scalar_check (size, 0))
5675 return false;
5677 if (!type_check (size, 0, BT_INTEGER))
5678 return false;
5680 if (!variable_check (size, 0, false))
5681 return false;
5683 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5684 return false;
5687 if (put != NULL)
5689 if (put->expr_type != EXPR_VARIABLE
5690 || !put->symtree->n.sym->attr.optional)
5692 nargs++;
5693 where = &put->where;
5696 if (!array_check (put, 1))
5697 return false;
5699 if (!rank_check (put, 1, 1))
5700 return false;
5702 if (!type_check (put, 1, BT_INTEGER))
5703 return false;
5705 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5706 return false;
5708 if (gfc_array_size (put, &put_size)
5709 && mpz_get_ui (put_size) < seed_size)
5710 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5711 "too small (%i/%i)",
5712 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5713 where, (int) mpz_get_ui (put_size), seed_size);
5716 if (get != NULL)
5718 if (get->expr_type != EXPR_VARIABLE
5719 || !get->symtree->n.sym->attr.optional)
5721 nargs++;
5722 where = &get->where;
5725 if (!array_check (get, 2))
5726 return false;
5728 if (!rank_check (get, 2, 1))
5729 return false;
5731 if (!type_check (get, 2, BT_INTEGER))
5732 return false;
5734 if (!variable_check (get, 2, false))
5735 return false;
5737 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5738 return false;
5740 if (gfc_array_size (get, &get_size)
5741 && mpz_get_ui (get_size) < seed_size)
5742 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5743 "too small (%i/%i)",
5744 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5745 where, (int) mpz_get_ui (get_size), seed_size);
5748 /* RANDOM_SEED may not have more than one non-optional argument. */
5749 if (nargs > 1)
5750 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5752 return true;
5755 bool
5756 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5758 gfc_expr *e;
5759 int len, i;
5760 int num_percent, nargs;
5762 e = a->expr;
5763 if (e->expr_type != EXPR_CONSTANT)
5764 return true;
5766 len = e->value.character.length;
5767 if (e->value.character.string[len-1] != '\0')
5768 gfc_internal_error ("fe_runtime_error string must be null terminated");
5770 num_percent = 0;
5771 for (i=0; i<len-1; i++)
5772 if (e->value.character.string[i] == '%')
5773 num_percent ++;
5775 nargs = 0;
5776 for (; a; a = a->next)
5777 nargs ++;
5779 if (nargs -1 != num_percent)
5780 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5781 nargs, num_percent++);
5783 return true;
5786 bool
5787 gfc_check_second_sub (gfc_expr *time)
5789 if (!scalar_check (time, 0))
5790 return false;
5792 if (!type_check (time, 0, BT_REAL))
5793 return false;
5795 if (!kind_value_check (time, 0, 4))
5796 return false;
5798 return true;
5802 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5803 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5804 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5805 count_max are all optional arguments */
5807 bool
5808 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5809 gfc_expr *count_max)
5811 if (count != NULL)
5813 if (!scalar_check (count, 0))
5814 return false;
5816 if (!type_check (count, 0, BT_INTEGER))
5817 return false;
5819 if (count->ts.kind != gfc_default_integer_kind
5820 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5821 "SYSTEM_CLOCK at %L has non-default kind",
5822 &count->where))
5823 return false;
5825 if (!variable_check (count, 0, false))
5826 return false;
5829 if (count_rate != NULL)
5831 if (!scalar_check (count_rate, 1))
5832 return false;
5834 if (!variable_check (count_rate, 1, false))
5835 return false;
5837 if (count_rate->ts.type == BT_REAL)
5839 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5840 "SYSTEM_CLOCK at %L", &count_rate->where))
5841 return false;
5843 else
5845 if (!type_check (count_rate, 1, BT_INTEGER))
5846 return false;
5848 if (count_rate->ts.kind != gfc_default_integer_kind
5849 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5850 "SYSTEM_CLOCK at %L has non-default kind",
5851 &count_rate->where))
5852 return false;
5857 if (count_max != NULL)
5859 if (!scalar_check (count_max, 2))
5860 return false;
5862 if (!type_check (count_max, 2, BT_INTEGER))
5863 return false;
5865 if (count_max->ts.kind != gfc_default_integer_kind
5866 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5867 "SYSTEM_CLOCK at %L has non-default kind",
5868 &count_max->where))
5869 return false;
5871 if (!variable_check (count_max, 2, false))
5872 return false;
5875 return true;
5879 bool
5880 gfc_check_irand (gfc_expr *x)
5882 if (x == NULL)
5883 return true;
5885 if (!scalar_check (x, 0))
5886 return false;
5888 if (!type_check (x, 0, BT_INTEGER))
5889 return false;
5891 if (!kind_value_check (x, 0, 4))
5892 return false;
5894 return true;
5898 bool
5899 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5901 if (!scalar_check (seconds, 0))
5902 return false;
5903 if (!type_check (seconds, 0, BT_INTEGER))
5904 return false;
5906 if (!int_or_proc_check (handler, 1))
5907 return false;
5908 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5909 return false;
5911 if (status == NULL)
5912 return true;
5914 if (!scalar_check (status, 2))
5915 return false;
5916 if (!type_check (status, 2, BT_INTEGER))
5917 return false;
5918 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5919 return false;
5921 return true;
5925 bool
5926 gfc_check_rand (gfc_expr *x)
5928 if (x == NULL)
5929 return true;
5931 if (!scalar_check (x, 0))
5932 return false;
5934 if (!type_check (x, 0, BT_INTEGER))
5935 return false;
5937 if (!kind_value_check (x, 0, 4))
5938 return false;
5940 return true;
5944 bool
5945 gfc_check_srand (gfc_expr *x)
5947 if (!scalar_check (x, 0))
5948 return false;
5950 if (!type_check (x, 0, BT_INTEGER))
5951 return false;
5953 if (!kind_value_check (x, 0, 4))
5954 return false;
5956 return true;
5960 bool
5961 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5963 if (!scalar_check (time, 0))
5964 return false;
5965 if (!type_check (time, 0, BT_INTEGER))
5966 return false;
5968 if (!type_check (result, 1, BT_CHARACTER))
5969 return false;
5970 if (!kind_value_check (result, 1, gfc_default_character_kind))
5971 return false;
5973 return true;
5977 bool
5978 gfc_check_dtime_etime (gfc_expr *x)
5980 if (!array_check (x, 0))
5981 return false;
5983 if (!rank_check (x, 0, 1))
5984 return false;
5986 if (!variable_check (x, 0, false))
5987 return false;
5989 if (!type_check (x, 0, BT_REAL))
5990 return false;
5992 if (!kind_value_check (x, 0, 4))
5993 return false;
5995 return true;
5999 bool
6000 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
6002 if (!array_check (values, 0))
6003 return false;
6005 if (!rank_check (values, 0, 1))
6006 return false;
6008 if (!variable_check (values, 0, false))
6009 return false;
6011 if (!type_check (values, 0, BT_REAL))
6012 return false;
6014 if (!kind_value_check (values, 0, 4))
6015 return false;
6017 if (!scalar_check (time, 1))
6018 return false;
6020 if (!type_check (time, 1, BT_REAL))
6021 return false;
6023 if (!kind_value_check (time, 1, 4))
6024 return false;
6026 return true;
6030 bool
6031 gfc_check_fdate_sub (gfc_expr *date)
6033 if (!type_check (date, 0, BT_CHARACTER))
6034 return false;
6035 if (!kind_value_check (date, 0, gfc_default_character_kind))
6036 return false;
6038 return true;
6042 bool
6043 gfc_check_gerror (gfc_expr *msg)
6045 if (!type_check (msg, 0, BT_CHARACTER))
6046 return false;
6047 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6048 return false;
6050 return true;
6054 bool
6055 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
6057 if (!type_check (cwd, 0, BT_CHARACTER))
6058 return false;
6059 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
6060 return false;
6062 if (status == NULL)
6063 return true;
6065 if (!scalar_check (status, 1))
6066 return false;
6068 if (!type_check (status, 1, BT_INTEGER))
6069 return false;
6071 return true;
6075 bool
6076 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
6078 if (!type_check (pos, 0, BT_INTEGER))
6079 return false;
6081 if (pos->ts.kind > gfc_default_integer_kind)
6083 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
6084 "not wider than the default kind (%d)",
6085 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6086 &pos->where, gfc_default_integer_kind);
6087 return false;
6090 if (!type_check (value, 1, BT_CHARACTER))
6091 return false;
6092 if (!kind_value_check (value, 1, gfc_default_character_kind))
6093 return false;
6095 return true;
6099 bool
6100 gfc_check_getlog (gfc_expr *msg)
6102 if (!type_check (msg, 0, BT_CHARACTER))
6103 return false;
6104 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6105 return false;
6107 return true;
6111 bool
6112 gfc_check_exit (gfc_expr *status)
6114 if (status == NULL)
6115 return true;
6117 if (!type_check (status, 0, BT_INTEGER))
6118 return false;
6120 if (!scalar_check (status, 0))
6121 return false;
6123 return true;
6127 bool
6128 gfc_check_flush (gfc_expr *unit)
6130 if (unit == NULL)
6131 return true;
6133 if (!type_check (unit, 0, BT_INTEGER))
6134 return false;
6136 if (!scalar_check (unit, 0))
6137 return false;
6139 return true;
6143 bool
6144 gfc_check_free (gfc_expr *i)
6146 if (!type_check (i, 0, BT_INTEGER))
6147 return false;
6149 if (!scalar_check (i, 0))
6150 return false;
6152 return true;
6156 bool
6157 gfc_check_hostnm (gfc_expr *name)
6159 if (!type_check (name, 0, BT_CHARACTER))
6160 return false;
6161 if (!kind_value_check (name, 0, gfc_default_character_kind))
6162 return false;
6164 return true;
6168 bool
6169 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6171 if (!type_check (name, 0, BT_CHARACTER))
6172 return false;
6173 if (!kind_value_check (name, 0, gfc_default_character_kind))
6174 return false;
6176 if (status == NULL)
6177 return true;
6179 if (!scalar_check (status, 1))
6180 return false;
6182 if (!type_check (status, 1, BT_INTEGER))
6183 return false;
6185 return true;
6189 bool
6190 gfc_check_itime_idate (gfc_expr *values)
6192 if (!array_check (values, 0))
6193 return false;
6195 if (!rank_check (values, 0, 1))
6196 return false;
6198 if (!variable_check (values, 0, false))
6199 return false;
6201 if (!type_check (values, 0, BT_INTEGER))
6202 return false;
6204 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6205 return false;
6207 return true;
6211 bool
6212 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6214 if (!type_check (time, 0, BT_INTEGER))
6215 return false;
6217 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6218 return false;
6220 if (!scalar_check (time, 0))
6221 return false;
6223 if (!array_check (values, 1))
6224 return false;
6226 if (!rank_check (values, 1, 1))
6227 return false;
6229 if (!variable_check (values, 1, false))
6230 return false;
6232 if (!type_check (values, 1, BT_INTEGER))
6233 return false;
6235 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6236 return false;
6238 return true;
6242 bool
6243 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6245 if (!scalar_check (unit, 0))
6246 return false;
6248 if (!type_check (unit, 0, BT_INTEGER))
6249 return false;
6251 if (!type_check (name, 1, BT_CHARACTER))
6252 return false;
6253 if (!kind_value_check (name, 1, gfc_default_character_kind))
6254 return false;
6256 return true;
6260 bool
6261 gfc_check_isatty (gfc_expr *unit)
6263 if (unit == NULL)
6264 return false;
6266 if (!type_check (unit, 0, BT_INTEGER))
6267 return false;
6269 if (!scalar_check (unit, 0))
6270 return false;
6272 return true;
6276 bool
6277 gfc_check_isnan (gfc_expr *x)
6279 if (!type_check (x, 0, BT_REAL))
6280 return false;
6282 return true;
6286 bool
6287 gfc_check_perror (gfc_expr *string)
6289 if (!type_check (string, 0, BT_CHARACTER))
6290 return false;
6291 if (!kind_value_check (string, 0, gfc_default_character_kind))
6292 return false;
6294 return true;
6298 bool
6299 gfc_check_umask (gfc_expr *mask)
6301 if (!type_check (mask, 0, BT_INTEGER))
6302 return false;
6304 if (!scalar_check (mask, 0))
6305 return false;
6307 return true;
6311 bool
6312 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6314 if (!type_check (mask, 0, BT_INTEGER))
6315 return false;
6317 if (!scalar_check (mask, 0))
6318 return false;
6320 if (old == NULL)
6321 return true;
6323 if (!scalar_check (old, 1))
6324 return false;
6326 if (!type_check (old, 1, BT_INTEGER))
6327 return false;
6329 return true;
6333 bool
6334 gfc_check_unlink (gfc_expr *name)
6336 if (!type_check (name, 0, BT_CHARACTER))
6337 return false;
6338 if (!kind_value_check (name, 0, gfc_default_character_kind))
6339 return false;
6341 return true;
6345 bool
6346 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6348 if (!type_check (name, 0, BT_CHARACTER))
6349 return false;
6350 if (!kind_value_check (name, 0, gfc_default_character_kind))
6351 return false;
6353 if (status == NULL)
6354 return true;
6356 if (!scalar_check (status, 1))
6357 return false;
6359 if (!type_check (status, 1, BT_INTEGER))
6360 return false;
6362 return true;
6366 bool
6367 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6369 if (!scalar_check (number, 0))
6370 return false;
6371 if (!type_check (number, 0, BT_INTEGER))
6372 return false;
6374 if (!int_or_proc_check (handler, 1))
6375 return false;
6376 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6377 return false;
6379 return true;
6383 bool
6384 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6386 if (!scalar_check (number, 0))
6387 return false;
6388 if (!type_check (number, 0, BT_INTEGER))
6389 return false;
6391 if (!int_or_proc_check (handler, 1))
6392 return false;
6393 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6394 return false;
6396 if (status == NULL)
6397 return true;
6399 if (!type_check (status, 2, BT_INTEGER))
6400 return false;
6401 if (!scalar_check (status, 2))
6402 return false;
6404 return true;
6408 bool
6409 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6411 if (!type_check (cmd, 0, BT_CHARACTER))
6412 return false;
6413 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6414 return false;
6416 if (!scalar_check (status, 1))
6417 return false;
6419 if (!type_check (status, 1, BT_INTEGER))
6420 return false;
6422 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6423 return false;
6425 return true;
6429 /* This is used for the GNU intrinsics AND, OR and XOR. */
6430 bool
6431 gfc_check_and (gfc_expr *i, gfc_expr *j)
6433 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6435 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6436 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6437 gfc_current_intrinsic, &i->where);
6438 return false;
6441 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6443 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6444 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6445 gfc_current_intrinsic, &j->where);
6446 return false;
6449 if (i->ts.type != j->ts.type)
6451 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6452 "have the same type", gfc_current_intrinsic_arg[0]->name,
6453 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6454 &j->where);
6455 return false;
6458 if (!scalar_check (i, 0))
6459 return false;
6461 if (!scalar_check (j, 1))
6462 return false;
6464 return true;
6468 bool
6469 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6472 if (a->expr_type == EXPR_NULL)
6474 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6475 "argument to STORAGE_SIZE, because it returns a "
6476 "disassociated pointer", &a->where);
6477 return false;
6480 if (a->ts.type == BT_ASSUMED)
6482 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6483 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6484 &a->where);
6485 return false;
6488 if (a->ts.type == BT_PROCEDURE)
6490 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6491 "procedure", gfc_current_intrinsic_arg[0]->name,
6492 gfc_current_intrinsic, &a->where);
6493 return false;
6496 if (kind == NULL)
6497 return true;
6499 if (!type_check (kind, 1, BT_INTEGER))
6500 return false;
6502 if (!scalar_check (kind, 1))
6503 return false;
6505 if (kind->expr_type != EXPR_CONSTANT)
6507 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6508 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6509 &kind->where);
6510 return false;
6513 return true;