* de.po: Update.
[official-gcc.git] / gcc / fortran / check.c
blobc22bfa965eba355cc6b6afcd6e1b1a794cae26c5
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 expr2 is constant, then check that the value is less than
299 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
301 static bool
302 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
303 gfc_expr *expr2, bool or_equal)
305 int i2, i3;
307 if (expr2->expr_type == EXPR_CONSTANT)
309 gfc_extract_int (expr2, &i2);
310 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
312 /* For ISHFT[C], check that |shift| <= bit_size(i). */
313 if (arg2 == NULL)
315 if (i2 < 0)
316 i2 = -i2;
318 if (i2 > gfc_integer_kinds[i3].bit_size)
320 gfc_error ("The absolute value of SHIFT at %L must be less "
321 "than or equal to BIT_SIZE(%qs)",
322 &expr2->where, arg1);
323 return false;
327 if (or_equal)
329 if (i2 > gfc_integer_kinds[i3].bit_size)
331 gfc_error ("%qs at %L must be less than "
332 "or equal to BIT_SIZE(%qs)",
333 arg2, &expr2->where, arg1);
334 return false;
337 else
339 if (i2 >= gfc_integer_kinds[i3].bit_size)
341 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
342 arg2, &expr2->where, arg1);
343 return false;
348 return true;
352 /* If expr is constant, then check that the value is less than or equal
353 to the bit_size of the kind k. */
355 static bool
356 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
358 int i, val;
360 if (expr->expr_type != EXPR_CONSTANT)
361 return true;
363 i = gfc_validate_kind (BT_INTEGER, k, false);
364 gfc_extract_int (expr, &val);
366 if (val > gfc_integer_kinds[i].bit_size)
368 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
369 "INTEGER(KIND=%d)", arg, &expr->where, k);
370 return false;
373 return true;
377 /* If expr2 and expr3 are constants, then check that the value is less than
378 or equal to bit_size(expr1). */
380 static bool
381 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
382 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
384 int i2, i3;
386 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
388 gfc_extract_int (expr2, &i2);
389 gfc_extract_int (expr3, &i3);
390 i2 += i3;
391 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
392 if (i2 > gfc_integer_kinds[i3].bit_size)
394 gfc_error ("%<%s + %s%> at %L must be less than or equal "
395 "to BIT_SIZE(%qs)",
396 arg2, arg3, &expr2->where, arg1);
397 return false;
401 return true;
404 /* Make sure two expressions have the same type. */
406 static bool
407 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
409 gfc_typespec *ets = &e->ts;
410 gfc_typespec *fts = &f->ts;
412 if (e->ts.type == BT_PROCEDURE && e->symtree->n.sym)
413 ets = &e->symtree->n.sym->ts;
414 if (f->ts.type == BT_PROCEDURE && f->symtree->n.sym)
415 fts = &f->symtree->n.sym->ts;
417 if (gfc_compare_types (ets, fts))
418 return true;
420 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
421 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
422 gfc_current_intrinsic, &f->where,
423 gfc_current_intrinsic_arg[n]->name);
425 return false;
429 /* Make sure that an expression has a certain (nonzero) rank. */
431 static bool
432 rank_check (gfc_expr *e, int n, int rank)
434 if (e->rank == rank)
435 return true;
437 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
438 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
439 &e->where, rank);
441 return false;
445 /* Make sure a variable expression is not an optional dummy argument. */
447 static bool
448 nonoptional_check (gfc_expr *e, int n)
450 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
452 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
453 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
454 &e->where);
457 /* TODO: Recursive check on nonoptional variables? */
459 return true;
463 /* Check for ALLOCATABLE attribute. */
465 static bool
466 allocatable_check (gfc_expr *e, int n)
468 symbol_attribute attr;
470 attr = gfc_variable_attr (e, NULL);
471 if (!attr.allocatable || attr.associate_var)
473 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
474 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
475 &e->where);
476 return false;
479 return true;
483 /* Check that an expression has a particular kind. */
485 static bool
486 kind_value_check (gfc_expr *e, int n, int k)
488 if (e->ts.kind == k)
489 return true;
491 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
492 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
493 &e->where, k);
495 return false;
499 /* Make sure an expression is a variable. */
501 static bool
502 variable_check (gfc_expr *e, int n, bool allow_proc)
504 if (e->expr_type == EXPR_VARIABLE
505 && e->symtree->n.sym->attr.intent == INTENT_IN
506 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
507 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
509 gfc_ref *ref;
510 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
511 && CLASS_DATA (e->symtree->n.sym)
512 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
513 : e->symtree->n.sym->attr.pointer;
515 for (ref = e->ref; ref; ref = ref->next)
517 if (pointer && ref->type == REF_COMPONENT)
518 break;
519 if (ref->type == REF_COMPONENT
520 && ((ref->u.c.component->ts.type == BT_CLASS
521 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
522 || (ref->u.c.component->ts.type != BT_CLASS
523 && ref->u.c.component->attr.pointer)))
524 break;
527 if (!ref)
529 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
530 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
531 gfc_current_intrinsic, &e->where);
532 return false;
536 if (e->expr_type == EXPR_VARIABLE
537 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
538 && (allow_proc || !e->symtree->n.sym->attr.function))
539 return true;
541 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
542 && e->symtree->n.sym == e->symtree->n.sym->result)
544 gfc_namespace *ns;
545 for (ns = gfc_current_ns; ns; ns = ns->parent)
546 if (ns->proc_name == e->symtree->n.sym)
547 return true;
550 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
551 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
553 return false;
557 /* Check the common DIM parameter for correctness. */
559 static bool
560 dim_check (gfc_expr *dim, int n, bool optional)
562 if (dim == NULL)
563 return true;
565 if (!type_check (dim, n, BT_INTEGER))
566 return false;
568 if (!scalar_check (dim, n))
569 return false;
571 if (!optional && !nonoptional_check (dim, n))
572 return false;
574 return true;
578 /* If a coarray DIM parameter is a constant, make sure that it is greater than
579 zero and less than or equal to the corank of the given array. */
581 static bool
582 dim_corank_check (gfc_expr *dim, gfc_expr *array)
584 int corank;
586 gcc_assert (array->expr_type == EXPR_VARIABLE);
588 if (dim->expr_type != EXPR_CONSTANT)
589 return true;
591 if (array->ts.type == BT_CLASS)
592 return true;
594 corank = gfc_get_corank (array);
596 if (mpz_cmp_ui (dim->value.integer, 1) < 0
597 || mpz_cmp_ui (dim->value.integer, corank) > 0)
599 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
600 "codimension index", gfc_current_intrinsic, &dim->where);
602 return false;
605 return true;
609 /* If a DIM parameter is a constant, make sure that it is greater than
610 zero and less than or equal to the rank of the given array. If
611 allow_assumed is zero then dim must be less than the rank of the array
612 for assumed size arrays. */
614 static bool
615 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
617 gfc_array_ref *ar;
618 int rank;
620 if (dim == NULL)
621 return true;
623 if (dim->expr_type != EXPR_CONSTANT)
624 return true;
626 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
627 && array->value.function.isym->id == GFC_ISYM_SPREAD)
628 rank = array->rank + 1;
629 else
630 rank = array->rank;
632 /* Assumed-rank array. */
633 if (rank == -1)
634 rank = GFC_MAX_DIMENSIONS;
636 if (array->expr_type == EXPR_VARIABLE)
638 ar = gfc_find_array_ref (array);
639 if (ar->as->type == AS_ASSUMED_SIZE
640 && !allow_assumed
641 && ar->type != AR_ELEMENT
642 && ar->type != AR_SECTION)
643 rank--;
646 if (mpz_cmp_ui (dim->value.integer, 1) < 0
647 || mpz_cmp_ui (dim->value.integer, rank) > 0)
649 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
650 "dimension index", gfc_current_intrinsic, &dim->where);
652 return false;
655 return true;
659 /* Compare the size of a along dimension ai with the size of b along
660 dimension bi, returning 0 if they are known not to be identical,
661 and 1 if they are identical, or if this cannot be determined. */
663 static int
664 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
666 mpz_t a_size, b_size;
667 int ret;
669 gcc_assert (a->rank > ai);
670 gcc_assert (b->rank > bi);
672 ret = 1;
674 if (gfc_array_dimen_size (a, ai, &a_size))
676 if (gfc_array_dimen_size (b, bi, &b_size))
678 if (mpz_cmp (a_size, b_size) != 0)
679 ret = 0;
681 mpz_clear (b_size);
683 mpz_clear (a_size);
685 return ret;
688 /* Calculate the length of a character variable, including substrings.
689 Strip away parentheses if necessary. Return -1 if no length could
690 be determined. */
692 static long
693 gfc_var_strlen (const gfc_expr *a)
695 gfc_ref *ra;
697 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
698 a = a->value.op.op1;
700 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
703 if (ra)
705 long start_a, end_a;
707 if (!ra->u.ss.end)
708 return -1;
710 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
711 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
713 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
714 : 1;
715 end_a = mpz_get_si (ra->u.ss.end->value.integer);
716 return (end_a < start_a) ? 0 : end_a - start_a + 1;
718 else if (ra->u.ss.start
719 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
720 return 1;
721 else
722 return -1;
725 if (a->ts.u.cl && a->ts.u.cl->length
726 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
727 return mpz_get_si (a->ts.u.cl->length->value.integer);
728 else if (a->expr_type == EXPR_CONSTANT
729 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
730 return a->value.character.length;
731 else
732 return -1;
736 /* Check whether two character expressions have the same length;
737 returns true if they have or if the length cannot be determined,
738 otherwise return false and raise a gfc_error. */
740 bool
741 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
743 long len_a, len_b;
745 len_a = gfc_var_strlen(a);
746 len_b = gfc_var_strlen(b);
748 if (len_a == -1 || len_b == -1 || len_a == len_b)
749 return true;
750 else
752 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
753 len_a, len_b, name, &a->where);
754 return false;
759 /***** Check functions *****/
761 /* Check subroutine suitable for intrinsics taking a real argument and
762 a kind argument for the result. */
764 static bool
765 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
767 if (!type_check (a, 0, BT_REAL))
768 return false;
769 if (!kind_check (kind, 1, type))
770 return false;
772 return true;
776 /* Check subroutine suitable for ceiling, floor and nint. */
778 bool
779 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
781 return check_a_kind (a, kind, BT_INTEGER);
785 /* Check subroutine suitable for aint, anint. */
787 bool
788 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
790 return check_a_kind (a, kind, BT_REAL);
794 bool
795 gfc_check_abs (gfc_expr *a)
797 if (!numeric_check (a, 0))
798 return false;
800 return true;
804 bool
805 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
807 if (!type_check (a, 0, BT_INTEGER))
808 return false;
809 if (!kind_check (kind, 1, BT_CHARACTER))
810 return false;
812 return true;
816 bool
817 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
819 if (!type_check (name, 0, BT_CHARACTER)
820 || !scalar_check (name, 0))
821 return false;
822 if (!kind_value_check (name, 0, gfc_default_character_kind))
823 return false;
825 if (!type_check (mode, 1, BT_CHARACTER)
826 || !scalar_check (mode, 1))
827 return false;
828 if (!kind_value_check (mode, 1, gfc_default_character_kind))
829 return false;
831 return true;
835 bool
836 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
838 if (!logical_array_check (mask, 0))
839 return false;
841 if (!dim_check (dim, 1, false))
842 return false;
844 if (!dim_rank_check (dim, mask, 0))
845 return false;
847 return true;
851 bool
852 gfc_check_allocated (gfc_expr *array)
854 /* Tests on allocated components of coarrays need to detour the check to
855 argument of the _caf_get. */
856 if (flag_coarray == GFC_FCOARRAY_LIB && array->expr_type == EXPR_FUNCTION
857 && array->value.function.isym
858 && array->value.function.isym->id == GFC_ISYM_CAF_GET)
860 array = array->value.function.actual->expr;
861 if (!array->ref)
862 return false;
865 if (!variable_check (array, 0, false))
866 return false;
867 if (!allocatable_check (array, 0))
868 return false;
870 return true;
874 /* Common check function where the first argument must be real or
875 integer and the second argument must be the same as the first. */
877 bool
878 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
880 if (!int_or_real_check (a, 0))
881 return false;
883 if (a->ts.type != p->ts.type)
885 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
886 "have the same type", gfc_current_intrinsic_arg[0]->name,
887 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
888 &p->where);
889 return false;
892 if (a->ts.kind != p->ts.kind)
894 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
895 &p->where))
896 return false;
899 return true;
903 bool
904 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
906 if (!double_check (x, 0) || !double_check (y, 1))
907 return false;
909 return true;
913 bool
914 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
916 symbol_attribute attr1, attr2;
917 int i;
918 bool t;
919 locus *where;
921 where = &pointer->where;
923 if (pointer->expr_type == EXPR_NULL)
924 goto null_arg;
926 attr1 = gfc_expr_attr (pointer);
928 if (!attr1.pointer && !attr1.proc_pointer)
930 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
931 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
932 &pointer->where);
933 return false;
936 /* F2008, C1242. */
937 if (attr1.pointer && gfc_is_coindexed (pointer))
939 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
940 "coindexed", gfc_current_intrinsic_arg[0]->name,
941 gfc_current_intrinsic, &pointer->where);
942 return false;
945 /* Target argument is optional. */
946 if (target == NULL)
947 return true;
949 where = &target->where;
950 if (target->expr_type == EXPR_NULL)
951 goto null_arg;
953 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
954 attr2 = gfc_expr_attr (target);
955 else
957 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
958 "or target VARIABLE or FUNCTION",
959 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
960 &target->where);
961 return false;
964 if (attr1.pointer && !attr2.pointer && !attr2.target)
966 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
967 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
968 gfc_current_intrinsic, &target->where);
969 return false;
972 /* F2008, C1242. */
973 if (attr1.pointer && gfc_is_coindexed (target))
975 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
976 "coindexed", gfc_current_intrinsic_arg[1]->name,
977 gfc_current_intrinsic, &target->where);
978 return false;
981 t = true;
982 if (!same_type_check (pointer, 0, target, 1))
983 t = false;
984 if (!rank_check (target, 0, pointer->rank))
985 t = false;
986 if (target->rank > 0)
988 for (i = 0; i < target->rank; i++)
989 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
991 gfc_error ("Array section with a vector subscript at %L shall not "
992 "be the target of a pointer",
993 &target->where);
994 t = false;
995 break;
998 return t;
1000 null_arg:
1002 gfc_error ("NULL pointer at %L is not permitted as actual argument "
1003 "of %qs intrinsic function", where, gfc_current_intrinsic);
1004 return false;
1009 bool
1010 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
1012 /* gfc_notify_std would be a waste of time as the return value
1013 is seemingly used only for the generic resolution. The error
1014 will be: Too many arguments. */
1015 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
1016 return false;
1018 return gfc_check_atan2 (y, x);
1022 bool
1023 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
1025 if (!type_check (y, 0, BT_REAL))
1026 return false;
1027 if (!same_type_check (y, 0, x, 1))
1028 return false;
1030 return true;
1034 static bool
1035 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1036 gfc_expr *stat, int stat_no)
1038 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1039 return false;
1041 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1042 && !(atom->ts.type == BT_LOGICAL
1043 && atom->ts.kind == gfc_atomic_logical_kind))
1045 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1046 "integer of ATOMIC_INT_KIND or a logical of "
1047 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1048 return false;
1051 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1053 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1054 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1055 return false;
1058 if (atom->ts.type != value->ts.type)
1060 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1061 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1062 gfc_current_intrinsic, &value->where,
1063 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1064 return false;
1067 if (stat != NULL)
1069 if (!type_check (stat, stat_no, BT_INTEGER))
1070 return false;
1071 if (!scalar_check (stat, stat_no))
1072 return false;
1073 if (!variable_check (stat, stat_no, false))
1074 return false;
1075 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1076 return false;
1078 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1079 gfc_current_intrinsic, &stat->where))
1080 return false;
1083 return true;
1087 bool
1088 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1090 if (atom->expr_type == EXPR_FUNCTION
1091 && atom->value.function.isym
1092 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1093 atom = atom->value.function.actual->expr;
1095 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1097 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1098 "definable", gfc_current_intrinsic, &atom->where);
1099 return false;
1102 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1106 bool
1107 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1109 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1111 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1112 "integer of ATOMIC_INT_KIND", &atom->where,
1113 gfc_current_intrinsic);
1114 return false;
1117 return gfc_check_atomic_def (atom, value, stat);
1121 bool
1122 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1124 if (atom->expr_type == EXPR_FUNCTION
1125 && atom->value.function.isym
1126 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1127 atom = atom->value.function.actual->expr;
1129 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1131 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1132 "definable", gfc_current_intrinsic, &value->where);
1133 return false;
1136 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1140 bool
1141 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1142 gfc_expr *new_val, gfc_expr *stat)
1144 if (atom->expr_type == EXPR_FUNCTION
1145 && atom->value.function.isym
1146 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1147 atom = atom->value.function.actual->expr;
1149 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1150 return false;
1152 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1153 return false;
1155 if (!same_type_check (atom, 0, old, 1))
1156 return false;
1158 if (!same_type_check (atom, 0, compare, 2))
1159 return false;
1161 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1163 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1164 "definable", gfc_current_intrinsic, &atom->where);
1165 return false;
1168 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1170 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1171 "definable", gfc_current_intrinsic, &old->where);
1172 return false;
1175 return true;
1178 bool
1179 gfc_check_event_query (gfc_expr *event, gfc_expr *count, gfc_expr *stat)
1181 if (event->ts.type != BT_DERIVED
1182 || event->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
1183 || event->ts.u.derived->intmod_sym_id != ISOFORTRAN_EVENT_TYPE)
1185 gfc_error ("EVENT argument at %L to the intrinsic EVENT_QUERY "
1186 "shall be of type EVENT_TYPE", &event->where);
1187 return false;
1190 if (!scalar_check (event, 0))
1191 return false;
1193 if (!gfc_check_vardef_context (count, false, false, false, NULL))
1195 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1196 "shall be definable", &count->where);
1197 return false;
1200 if (!type_check (count, 1, BT_INTEGER))
1201 return false;
1203 int i = gfc_validate_kind (BT_INTEGER, count->ts.kind, false);
1204 int j = gfc_validate_kind (BT_INTEGER, gfc_default_integer_kind, false);
1206 if (gfc_integer_kinds[i].range < gfc_integer_kinds[j].range)
1208 gfc_error ("COUNT argument of the EVENT_QUERY intrinsic function at %L "
1209 "shall have at least the range of the default integer",
1210 &count->where);
1211 return false;
1214 if (stat != NULL)
1216 if (!type_check (stat, 2, BT_INTEGER))
1217 return false;
1218 if (!scalar_check (stat, 2))
1219 return false;
1220 if (!variable_check (stat, 2, false))
1221 return false;
1223 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1224 gfc_current_intrinsic, &stat->where))
1225 return false;
1228 return true;
1232 bool
1233 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1234 gfc_expr *stat)
1236 if (atom->expr_type == EXPR_FUNCTION
1237 && atom->value.function.isym
1238 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1239 atom = atom->value.function.actual->expr;
1241 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1243 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1244 "integer of ATOMIC_INT_KIND", &atom->where,
1245 gfc_current_intrinsic);
1246 return false;
1249 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1250 return false;
1252 if (!scalar_check (old, 2))
1253 return false;
1255 if (!same_type_check (atom, 0, old, 2))
1256 return false;
1258 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1260 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1261 "definable", gfc_current_intrinsic, &atom->where);
1262 return false;
1265 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1267 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1268 "definable", gfc_current_intrinsic, &old->where);
1269 return false;
1272 return true;
1276 /* BESJN and BESYN functions. */
1278 bool
1279 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1281 if (!type_check (n, 0, BT_INTEGER))
1282 return false;
1283 if (n->expr_type == EXPR_CONSTANT)
1285 int i;
1286 gfc_extract_int (n, &i);
1287 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1288 "N at %L", &n->where))
1289 return false;
1292 if (!type_check (x, 1, BT_REAL))
1293 return false;
1295 return true;
1299 /* Transformational version of the Bessel JN and YN functions. */
1301 bool
1302 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1304 if (!type_check (n1, 0, BT_INTEGER))
1305 return false;
1306 if (!scalar_check (n1, 0))
1307 return false;
1308 if (!nonnegative_check ("N1", n1))
1309 return false;
1311 if (!type_check (n2, 1, BT_INTEGER))
1312 return false;
1313 if (!scalar_check (n2, 1))
1314 return false;
1315 if (!nonnegative_check ("N2", n2))
1316 return false;
1318 if (!type_check (x, 2, BT_REAL))
1319 return false;
1320 if (!scalar_check (x, 2))
1321 return false;
1323 return true;
1327 bool
1328 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1330 if (!type_check (i, 0, BT_INTEGER))
1331 return false;
1333 if (!type_check (j, 1, BT_INTEGER))
1334 return false;
1336 return true;
1340 bool
1341 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1343 if (!type_check (i, 0, BT_INTEGER))
1344 return false;
1346 if (!type_check (pos, 1, BT_INTEGER))
1347 return false;
1349 if (!nonnegative_check ("pos", pos))
1350 return false;
1352 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1353 return false;
1355 return true;
1359 bool
1360 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1362 if (!type_check (i, 0, BT_INTEGER))
1363 return false;
1364 if (!kind_check (kind, 1, BT_CHARACTER))
1365 return false;
1367 return true;
1371 bool
1372 gfc_check_chdir (gfc_expr *dir)
1374 if (!type_check (dir, 0, BT_CHARACTER))
1375 return false;
1376 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1377 return false;
1379 return true;
1383 bool
1384 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1386 if (!type_check (dir, 0, BT_CHARACTER))
1387 return false;
1388 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1389 return false;
1391 if (status == NULL)
1392 return true;
1394 if (!type_check (status, 1, BT_INTEGER))
1395 return false;
1396 if (!scalar_check (status, 1))
1397 return false;
1399 return true;
1403 bool
1404 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1406 if (!type_check (name, 0, BT_CHARACTER))
1407 return false;
1408 if (!kind_value_check (name, 0, gfc_default_character_kind))
1409 return false;
1411 if (!type_check (mode, 1, BT_CHARACTER))
1412 return false;
1413 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1414 return false;
1416 return true;
1420 bool
1421 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1423 if (!type_check (name, 0, BT_CHARACTER))
1424 return false;
1425 if (!kind_value_check (name, 0, gfc_default_character_kind))
1426 return false;
1428 if (!type_check (mode, 1, BT_CHARACTER))
1429 return false;
1430 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1431 return false;
1433 if (status == NULL)
1434 return true;
1436 if (!type_check (status, 2, BT_INTEGER))
1437 return false;
1439 if (!scalar_check (status, 2))
1440 return false;
1442 return true;
1446 bool
1447 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1449 if (!numeric_check (x, 0))
1450 return false;
1452 if (y != NULL)
1454 if (!numeric_check (y, 1))
1455 return false;
1457 if (x->ts.type == BT_COMPLEX)
1459 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1460 "present if %<x%> is COMPLEX",
1461 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1462 &y->where);
1463 return false;
1466 if (y->ts.type == BT_COMPLEX)
1468 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1469 "of either REAL or INTEGER",
1470 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1471 &y->where);
1472 return false;
1477 if (!kind_check (kind, 2, BT_COMPLEX))
1478 return false;
1480 if (!kind && warn_conversion
1481 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1482 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1483 "COMPLEX(%d) at %L might lose precision, consider using "
1484 "the KIND argument", gfc_typename (&x->ts),
1485 gfc_default_real_kind, &x->where);
1486 else if (y && !kind && warn_conversion
1487 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1488 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1489 "COMPLEX(%d) at %L might lose precision, consider using "
1490 "the KIND argument", gfc_typename (&y->ts),
1491 gfc_default_real_kind, &y->where);
1492 return true;
1496 static bool
1497 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1498 gfc_expr *errmsg, bool co_reduce)
1500 if (!variable_check (a, 0, false))
1501 return false;
1503 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1504 "INTENT(INOUT)"))
1505 return false;
1507 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1508 if (gfc_has_vector_subscript (a))
1510 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1511 "subroutine %s shall not have a vector subscript",
1512 &a->where, gfc_current_intrinsic);
1513 return false;
1516 if (gfc_is_coindexed (a))
1518 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1519 "coindexed", &a->where, gfc_current_intrinsic);
1520 return false;
1523 if (image_idx != NULL)
1525 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1526 return false;
1527 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1528 return false;
1531 if (stat != NULL)
1533 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1534 return false;
1535 if (!scalar_check (stat, co_reduce ? 3 : 2))
1536 return false;
1537 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1538 return false;
1539 if (stat->ts.kind != 4)
1541 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1542 "variable", &stat->where);
1543 return false;
1547 if (errmsg != NULL)
1549 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1550 return false;
1551 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1552 return false;
1553 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1554 return false;
1555 if (errmsg->ts.kind != 1)
1557 gfc_error ("The errmsg= argument at %L must be a default-kind "
1558 "character variable", &errmsg->where);
1559 return false;
1563 if (flag_coarray == GFC_FCOARRAY_NONE)
1565 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1566 &a->where);
1567 return false;
1570 return true;
1574 bool
1575 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1576 gfc_expr *errmsg)
1578 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1580 gfc_error ("Support for the A argument at %L which is polymorphic A "
1581 "argument or has allocatable components is not yet "
1582 "implemented", &a->where);
1583 return false;
1585 return check_co_collective (a, source_image, stat, errmsg, false);
1589 bool
1590 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1591 gfc_expr *stat, gfc_expr *errmsg)
1593 symbol_attribute attr;
1594 gfc_formal_arglist *formal;
1595 gfc_symbol *sym;
1597 if (a->ts.type == BT_CLASS)
1599 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1600 &a->where);
1601 return false;
1604 if (gfc_expr_attr (a).alloc_comp)
1606 gfc_error ("Support for the A argument at %L with allocatable components"
1607 " is not yet implemented", &a->where);
1608 return false;
1611 if (!check_co_collective (a, result_image, stat, errmsg, true))
1612 return false;
1614 if (!gfc_resolve_expr (op))
1615 return false;
1617 attr = gfc_expr_attr (op);
1618 if (!attr.pure || !attr.function)
1620 gfc_error ("OPERATOR argument at %L must be a PURE function",
1621 &op->where);
1622 return false;
1625 if (attr.intrinsic)
1627 /* None of the intrinsics fulfills the criteria of taking two arguments,
1628 returning the same type and kind as the arguments and being permitted
1629 as actual argument. */
1630 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1631 op->symtree->n.sym->name, &op->where);
1632 return false;
1635 if (gfc_is_proc_ptr_comp (op))
1637 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1638 sym = comp->ts.interface;
1640 else
1641 sym = op->symtree->n.sym;
1643 formal = sym->formal;
1645 if (!formal || !formal->next || formal->next->next)
1647 gfc_error ("The function passed as OPERATOR at %L shall have two "
1648 "arguments", &op->where);
1649 return false;
1652 if (sym->result->ts.type == BT_UNKNOWN)
1653 gfc_set_default_type (sym->result, 0, NULL);
1655 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1657 gfc_error ("A argument at %L has type %s but the function passed as "
1658 "OPERATOR at %L returns %s",
1659 &a->where, gfc_typename (&a->ts), &op->where,
1660 gfc_typename (&sym->result->ts));
1661 return false;
1663 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1664 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1666 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1667 "%s and %s but shall have type %s", &op->where,
1668 gfc_typename (&formal->sym->ts),
1669 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1670 return false;
1672 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1673 || formal->next->sym->as || formal->sym->attr.allocatable
1674 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1675 || formal->next->sym->attr.pointer)
1677 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1678 "nonallocatable nonpointer arguments and return a "
1679 "nonallocatable nonpointer scalar", &op->where);
1680 return false;
1683 if (formal->sym->attr.value != formal->next->sym->attr.value)
1685 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1686 "attribute either for none or both arguments", &op->where);
1687 return false;
1690 if (formal->sym->attr.target != formal->next->sym->attr.target)
1692 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1693 "attribute either for none or both arguments", &op->where);
1694 return false;
1697 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1699 gfc_error ("The function passed as OPERATOR at %L shall have the "
1700 "ASYNCHRONOUS attribute either for none or both arguments",
1701 &op->where);
1702 return false;
1705 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1707 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1708 "OPTIONAL attribute for either of the arguments", &op->where);
1709 return false;
1712 if (a->ts.type == BT_CHARACTER)
1714 gfc_charlen *cl;
1715 unsigned long actual_size, formal_size1, formal_size2, result_size;
1717 cl = a->ts.u.cl;
1718 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1719 ? mpz_get_ui (cl->length->value.integer) : 0;
1721 cl = formal->sym->ts.u.cl;
1722 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1723 ? mpz_get_ui (cl->length->value.integer) : 0;
1725 cl = formal->next->sym->ts.u.cl;
1726 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1727 ? mpz_get_ui (cl->length->value.integer) : 0;
1729 cl = sym->ts.u.cl;
1730 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1731 ? mpz_get_ui (cl->length->value.integer) : 0;
1733 if (actual_size
1734 && ((formal_size1 && actual_size != formal_size1)
1735 || (formal_size2 && actual_size != formal_size2)))
1737 gfc_error ("The character length of the A argument at %L and of the "
1738 "arguments of the OPERATOR at %L shall be the same",
1739 &a->where, &op->where);
1740 return false;
1742 if (actual_size && result_size && actual_size != result_size)
1744 gfc_error ("The character length of the A argument at %L and of the "
1745 "function result of the OPERATOR at %L shall be the same",
1746 &a->where, &op->where);
1747 return false;
1751 return true;
1755 bool
1756 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1757 gfc_expr *errmsg)
1759 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1760 && a->ts.type != BT_CHARACTER)
1762 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1763 "integer, real or character",
1764 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1765 &a->where);
1766 return false;
1768 return check_co_collective (a, result_image, stat, errmsg, false);
1772 bool
1773 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1774 gfc_expr *errmsg)
1776 if (!numeric_check (a, 0))
1777 return false;
1778 return check_co_collective (a, result_image, stat, errmsg, false);
1782 bool
1783 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1785 if (!int_or_real_check (x, 0))
1786 return false;
1787 if (!scalar_check (x, 0))
1788 return false;
1790 if (!int_or_real_check (y, 1))
1791 return false;
1792 if (!scalar_check (y, 1))
1793 return false;
1795 return true;
1799 bool
1800 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1802 if (!logical_array_check (mask, 0))
1803 return false;
1804 if (!dim_check (dim, 1, false))
1805 return false;
1806 if (!dim_rank_check (dim, mask, 0))
1807 return false;
1808 if (!kind_check (kind, 2, BT_INTEGER))
1809 return false;
1810 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1811 "with KIND argument at %L",
1812 gfc_current_intrinsic, &kind->where))
1813 return false;
1815 return true;
1819 bool
1820 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1822 if (!array_check (array, 0))
1823 return false;
1825 if (!type_check (shift, 1, BT_INTEGER))
1826 return false;
1828 if (!dim_check (dim, 2, true))
1829 return false;
1831 if (!dim_rank_check (dim, array, false))
1832 return false;
1834 if (array->rank == 1 || shift->rank == 0)
1836 if (!scalar_check (shift, 1))
1837 return false;
1839 else if (shift->rank == array->rank - 1)
1841 int d;
1842 if (!dim)
1843 d = 1;
1844 else if (dim->expr_type == EXPR_CONSTANT)
1845 gfc_extract_int (dim, &d);
1846 else
1847 d = -1;
1849 if (d > 0)
1851 int i, j;
1852 for (i = 0, j = 0; i < array->rank; i++)
1853 if (i != d - 1)
1855 if (!identical_dimen_shape (array, i, shift, j))
1857 gfc_error ("%qs argument of %qs intrinsic at %L has "
1858 "invalid shape in dimension %d (%ld/%ld)",
1859 gfc_current_intrinsic_arg[1]->name,
1860 gfc_current_intrinsic, &shift->where, i + 1,
1861 mpz_get_si (array->shape[i]),
1862 mpz_get_si (shift->shape[j]));
1863 return false;
1866 j += 1;
1870 else
1872 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1873 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1874 gfc_current_intrinsic, &shift->where, array->rank - 1);
1875 return false;
1878 return true;
1882 bool
1883 gfc_check_ctime (gfc_expr *time)
1885 if (!scalar_check (time, 0))
1886 return false;
1888 if (!type_check (time, 0, BT_INTEGER))
1889 return false;
1891 return true;
1895 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1897 if (!double_check (y, 0) || !double_check (x, 1))
1898 return false;
1900 return true;
1903 bool
1904 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1906 if (!numeric_check (x, 0))
1907 return false;
1909 if (y != NULL)
1911 if (!numeric_check (y, 1))
1912 return false;
1914 if (x->ts.type == BT_COMPLEX)
1916 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1917 "present if %<x%> is COMPLEX",
1918 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1919 &y->where);
1920 return false;
1923 if (y->ts.type == BT_COMPLEX)
1925 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1926 "of either REAL or INTEGER",
1927 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1928 &y->where);
1929 return false;
1933 return true;
1937 bool
1938 gfc_check_dble (gfc_expr *x)
1940 if (!numeric_check (x, 0))
1941 return false;
1943 return true;
1947 bool
1948 gfc_check_digits (gfc_expr *x)
1950 if (!int_or_real_check (x, 0))
1951 return false;
1953 return true;
1957 bool
1958 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1960 switch (vector_a->ts.type)
1962 case BT_LOGICAL:
1963 if (!type_check (vector_b, 1, BT_LOGICAL))
1964 return false;
1965 break;
1967 case BT_INTEGER:
1968 case BT_REAL:
1969 case BT_COMPLEX:
1970 if (!numeric_check (vector_b, 1))
1971 return false;
1972 break;
1974 default:
1975 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
1976 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1977 gfc_current_intrinsic, &vector_a->where);
1978 return false;
1981 if (!rank_check (vector_a, 0, 1))
1982 return false;
1984 if (!rank_check (vector_b, 1, 1))
1985 return false;
1987 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1989 gfc_error ("Different shape for arguments %qs and %qs at %L for "
1990 "intrinsic %<dot_product%>",
1991 gfc_current_intrinsic_arg[0]->name,
1992 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1993 return false;
1996 return true;
2000 bool
2001 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
2003 if (!type_check (x, 0, BT_REAL)
2004 || !type_check (y, 1, BT_REAL))
2005 return false;
2007 if (x->ts.kind != gfc_default_real_kind)
2009 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2010 "real", gfc_current_intrinsic_arg[0]->name,
2011 gfc_current_intrinsic, &x->where);
2012 return false;
2015 if (y->ts.kind != gfc_default_real_kind)
2017 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
2018 "real", gfc_current_intrinsic_arg[1]->name,
2019 gfc_current_intrinsic, &y->where);
2020 return false;
2023 return true;
2027 bool
2028 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
2030 if (!type_check (i, 0, BT_INTEGER))
2031 return false;
2033 if (!type_check (j, 1, BT_INTEGER))
2034 return false;
2036 if (i->is_boz && j->is_boz)
2038 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
2039 "constants", &i->where, &j->where);
2040 return false;
2043 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
2044 return false;
2046 if (!type_check (shift, 2, BT_INTEGER))
2047 return false;
2049 if (!nonnegative_check ("SHIFT", shift))
2050 return false;
2052 if (i->is_boz)
2054 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
2055 return false;
2056 i->ts.kind = j->ts.kind;
2058 else
2060 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
2061 return false;
2062 j->ts.kind = i->ts.kind;
2065 return true;
2069 bool
2070 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
2071 gfc_expr *dim)
2073 if (!array_check (array, 0))
2074 return false;
2076 if (!type_check (shift, 1, BT_INTEGER))
2077 return false;
2079 if (!dim_check (dim, 3, true))
2080 return false;
2082 if (!dim_rank_check (dim, array, false))
2083 return false;
2085 if (array->rank == 1 || shift->rank == 0)
2087 if (!scalar_check (shift, 1))
2088 return false;
2090 else if (shift->rank == array->rank - 1)
2092 int d;
2093 if (!dim)
2094 d = 1;
2095 else if (dim->expr_type == EXPR_CONSTANT)
2096 gfc_extract_int (dim, &d);
2097 else
2098 d = -1;
2100 if (d > 0)
2102 int i, j;
2103 for (i = 0, j = 0; i < array->rank; i++)
2104 if (i != d - 1)
2106 if (!identical_dimen_shape (array, i, shift, j))
2108 gfc_error ("%qs argument of %qs intrinsic at %L has "
2109 "invalid shape in dimension %d (%ld/%ld)",
2110 gfc_current_intrinsic_arg[1]->name,
2111 gfc_current_intrinsic, &shift->where, i + 1,
2112 mpz_get_si (array->shape[i]),
2113 mpz_get_si (shift->shape[j]));
2114 return false;
2117 j += 1;
2121 else
2123 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2124 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2125 gfc_current_intrinsic, &shift->where, array->rank - 1);
2126 return false;
2129 if (boundary != NULL)
2131 if (!same_type_check (array, 0, boundary, 2))
2132 return false;
2134 if (array->rank == 1 || boundary->rank == 0)
2136 if (!scalar_check (boundary, 2))
2137 return false;
2139 else if (boundary->rank == array->rank - 1)
2141 if (!gfc_check_conformance (shift, boundary,
2142 "arguments '%s' and '%s' for "
2143 "intrinsic %s",
2144 gfc_current_intrinsic_arg[1]->name,
2145 gfc_current_intrinsic_arg[2]->name,
2146 gfc_current_intrinsic))
2147 return false;
2149 else
2151 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2152 "rank %d or be a scalar",
2153 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2154 &shift->where, array->rank - 1);
2155 return false;
2159 return true;
2162 bool
2163 gfc_check_float (gfc_expr *a)
2165 if (!type_check (a, 0, BT_INTEGER))
2166 return false;
2168 if ((a->ts.kind != gfc_default_integer_kind)
2169 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2170 "kind argument to %s intrinsic at %L",
2171 gfc_current_intrinsic, &a->where))
2172 return false;
2174 return true;
2177 /* A single complex argument. */
2179 bool
2180 gfc_check_fn_c (gfc_expr *a)
2182 if (!type_check (a, 0, BT_COMPLEX))
2183 return false;
2185 return true;
2188 /* A single real argument. */
2190 bool
2191 gfc_check_fn_r (gfc_expr *a)
2193 if (!type_check (a, 0, BT_REAL))
2194 return false;
2196 return true;
2199 /* A single double argument. */
2201 bool
2202 gfc_check_fn_d (gfc_expr *a)
2204 if (!double_check (a, 0))
2205 return false;
2207 return true;
2210 /* A single real or complex argument. */
2212 bool
2213 gfc_check_fn_rc (gfc_expr *a)
2215 if (!real_or_complex_check (a, 0))
2216 return false;
2218 return true;
2222 bool
2223 gfc_check_fn_rc2008 (gfc_expr *a)
2225 if (!real_or_complex_check (a, 0))
2226 return false;
2228 if (a->ts.type == BT_COMPLEX
2229 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2230 "of %qs intrinsic at %L",
2231 gfc_current_intrinsic_arg[0]->name,
2232 gfc_current_intrinsic, &a->where))
2233 return false;
2235 return true;
2239 bool
2240 gfc_check_fnum (gfc_expr *unit)
2242 if (!type_check (unit, 0, BT_INTEGER))
2243 return false;
2245 if (!scalar_check (unit, 0))
2246 return false;
2248 return true;
2252 bool
2253 gfc_check_huge (gfc_expr *x)
2255 if (!int_or_real_check (x, 0))
2256 return false;
2258 return true;
2262 bool
2263 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2265 if (!type_check (x, 0, BT_REAL))
2266 return false;
2267 if (!same_type_check (x, 0, y, 1))
2268 return false;
2270 return true;
2274 /* Check that the single argument is an integer. */
2276 bool
2277 gfc_check_i (gfc_expr *i)
2279 if (!type_check (i, 0, BT_INTEGER))
2280 return false;
2282 return true;
2286 bool
2287 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2289 if (!type_check (i, 0, BT_INTEGER))
2290 return false;
2292 if (!type_check (j, 1, BT_INTEGER))
2293 return false;
2295 if (i->ts.kind != j->ts.kind)
2297 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2298 &i->where))
2299 return false;
2302 return true;
2306 bool
2307 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2309 if (!type_check (i, 0, BT_INTEGER))
2310 return false;
2312 if (!type_check (pos, 1, BT_INTEGER))
2313 return false;
2315 if (!type_check (len, 2, BT_INTEGER))
2316 return false;
2318 if (!nonnegative_check ("pos", pos))
2319 return false;
2321 if (!nonnegative_check ("len", len))
2322 return false;
2324 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2325 return false;
2327 return true;
2331 bool
2332 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2334 int i;
2336 if (!type_check (c, 0, BT_CHARACTER))
2337 return false;
2339 if (!kind_check (kind, 1, BT_INTEGER))
2340 return false;
2342 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2343 "with KIND argument at %L",
2344 gfc_current_intrinsic, &kind->where))
2345 return false;
2347 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2349 gfc_expr *start;
2350 gfc_expr *end;
2351 gfc_ref *ref;
2353 /* Substring references don't have the charlength set. */
2354 ref = c->ref;
2355 while (ref && ref->type != REF_SUBSTRING)
2356 ref = ref->next;
2358 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2360 if (!ref)
2362 /* Check that the argument is length one. Non-constant lengths
2363 can't be checked here, so assume they are ok. */
2364 if (c->ts.u.cl && c->ts.u.cl->length)
2366 /* If we already have a length for this expression then use it. */
2367 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2368 return true;
2369 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2371 else
2372 return true;
2374 else
2376 start = ref->u.ss.start;
2377 end = ref->u.ss.end;
2379 gcc_assert (start);
2380 if (end == NULL || end->expr_type != EXPR_CONSTANT
2381 || start->expr_type != EXPR_CONSTANT)
2382 return true;
2384 i = mpz_get_si (end->value.integer) + 1
2385 - mpz_get_si (start->value.integer);
2388 else
2389 return true;
2391 if (i != 1)
2393 gfc_error ("Argument of %s at %L must be of length one",
2394 gfc_current_intrinsic, &c->where);
2395 return false;
2398 return true;
2402 bool
2403 gfc_check_idnint (gfc_expr *a)
2405 if (!double_check (a, 0))
2406 return false;
2408 return true;
2412 bool
2413 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2415 if (!type_check (i, 0, BT_INTEGER))
2416 return false;
2418 if (!type_check (j, 1, BT_INTEGER))
2419 return false;
2421 if (i->ts.kind != j->ts.kind)
2423 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2424 &i->where))
2425 return false;
2428 return true;
2432 bool
2433 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2434 gfc_expr *kind)
2436 if (!type_check (string, 0, BT_CHARACTER)
2437 || !type_check (substring, 1, BT_CHARACTER))
2438 return false;
2440 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2441 return false;
2443 if (!kind_check (kind, 3, BT_INTEGER))
2444 return false;
2445 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2446 "with KIND argument at %L",
2447 gfc_current_intrinsic, &kind->where))
2448 return false;
2450 if (string->ts.kind != substring->ts.kind)
2452 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2453 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2454 gfc_current_intrinsic, &substring->where,
2455 gfc_current_intrinsic_arg[0]->name);
2456 return false;
2459 return true;
2463 bool
2464 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2466 if (!numeric_check (x, 0))
2467 return false;
2469 if (!kind_check (kind, 1, BT_INTEGER))
2470 return false;
2472 return true;
2476 bool
2477 gfc_check_intconv (gfc_expr *x)
2479 if (!numeric_check (x, 0))
2480 return false;
2482 return true;
2486 bool
2487 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2489 if (!type_check (i, 0, BT_INTEGER))
2490 return false;
2492 if (!type_check (j, 1, BT_INTEGER))
2493 return false;
2495 if (i->ts.kind != j->ts.kind)
2497 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2498 &i->where))
2499 return false;
2502 return true;
2506 bool
2507 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2509 if (!type_check (i, 0, BT_INTEGER)
2510 || !type_check (shift, 1, BT_INTEGER))
2511 return false;
2513 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2514 return false;
2516 return true;
2520 bool
2521 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2523 if (!type_check (i, 0, BT_INTEGER)
2524 || !type_check (shift, 1, BT_INTEGER))
2525 return false;
2527 if (size != NULL)
2529 int i2, i3;
2531 if (!type_check (size, 2, BT_INTEGER))
2532 return false;
2534 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2535 return false;
2537 if (size->expr_type == EXPR_CONSTANT)
2539 gfc_extract_int (size, &i3);
2540 if (i3 <= 0)
2542 gfc_error ("SIZE at %L must be positive", &size->where);
2543 return false;
2546 if (shift->expr_type == EXPR_CONSTANT)
2548 gfc_extract_int (shift, &i2);
2549 if (i2 < 0)
2550 i2 = -i2;
2552 if (i2 > i3)
2554 gfc_error ("The absolute value of SHIFT at %L must be less "
2555 "than or equal to SIZE at %L", &shift->where,
2556 &size->where);
2557 return false;
2562 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2563 return false;
2565 return true;
2569 bool
2570 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2572 if (!type_check (pid, 0, BT_INTEGER))
2573 return false;
2575 if (!type_check (sig, 1, BT_INTEGER))
2576 return false;
2578 return true;
2582 bool
2583 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2585 if (!type_check (pid, 0, BT_INTEGER))
2586 return false;
2588 if (!scalar_check (pid, 0))
2589 return false;
2591 if (!type_check (sig, 1, BT_INTEGER))
2592 return false;
2594 if (!scalar_check (sig, 1))
2595 return false;
2597 if (status == NULL)
2598 return true;
2600 if (!type_check (status, 2, BT_INTEGER))
2601 return false;
2603 if (!scalar_check (status, 2))
2604 return false;
2606 return true;
2610 bool
2611 gfc_check_kind (gfc_expr *x)
2613 if (gfc_bt_struct (x->ts.type) || x->ts.type == BT_CLASS)
2615 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2616 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2617 gfc_current_intrinsic, &x->where);
2618 return false;
2620 if (x->ts.type == BT_PROCEDURE)
2622 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2623 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2624 &x->where);
2625 return false;
2628 return true;
2632 bool
2633 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2635 if (!array_check (array, 0))
2636 return false;
2638 if (!dim_check (dim, 1, false))
2639 return false;
2641 if (!dim_rank_check (dim, array, 1))
2642 return false;
2644 if (!kind_check (kind, 2, BT_INTEGER))
2645 return false;
2646 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2647 "with KIND argument at %L",
2648 gfc_current_intrinsic, &kind->where))
2649 return false;
2651 return true;
2655 bool
2656 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2658 if (flag_coarray == GFC_FCOARRAY_NONE)
2660 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2661 return false;
2664 if (!coarray_check (coarray, 0))
2665 return false;
2667 if (dim != NULL)
2669 if (!dim_check (dim, 1, false))
2670 return false;
2672 if (!dim_corank_check (dim, coarray))
2673 return false;
2676 if (!kind_check (kind, 2, BT_INTEGER))
2677 return false;
2679 return true;
2683 bool
2684 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2686 if (!type_check (s, 0, BT_CHARACTER))
2687 return false;
2689 if (!kind_check (kind, 1, BT_INTEGER))
2690 return false;
2691 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2692 "with KIND argument at %L",
2693 gfc_current_intrinsic, &kind->where))
2694 return false;
2696 return true;
2700 bool
2701 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2703 if (!type_check (a, 0, BT_CHARACTER))
2704 return false;
2705 if (!kind_value_check (a, 0, gfc_default_character_kind))
2706 return false;
2708 if (!type_check (b, 1, BT_CHARACTER))
2709 return false;
2710 if (!kind_value_check (b, 1, gfc_default_character_kind))
2711 return false;
2713 return true;
2717 bool
2718 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2720 if (!type_check (path1, 0, BT_CHARACTER))
2721 return false;
2722 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2723 return false;
2725 if (!type_check (path2, 1, BT_CHARACTER))
2726 return false;
2727 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2728 return false;
2730 return true;
2734 bool
2735 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2737 if (!type_check (path1, 0, BT_CHARACTER))
2738 return false;
2739 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2740 return false;
2742 if (!type_check (path2, 1, BT_CHARACTER))
2743 return false;
2744 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2745 return false;
2747 if (status == NULL)
2748 return true;
2750 if (!type_check (status, 2, BT_INTEGER))
2751 return false;
2753 if (!scalar_check (status, 2))
2754 return false;
2756 return true;
2760 bool
2761 gfc_check_loc (gfc_expr *expr)
2763 return variable_check (expr, 0, true);
2767 bool
2768 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2770 if (!type_check (path1, 0, BT_CHARACTER))
2771 return false;
2772 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2773 return false;
2775 if (!type_check (path2, 1, BT_CHARACTER))
2776 return false;
2777 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2778 return false;
2780 return true;
2784 bool
2785 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2787 if (!type_check (path1, 0, BT_CHARACTER))
2788 return false;
2789 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2790 return false;
2792 if (!type_check (path2, 1, BT_CHARACTER))
2793 return false;
2794 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2795 return false;
2797 if (status == NULL)
2798 return true;
2800 if (!type_check (status, 2, BT_INTEGER))
2801 return false;
2803 if (!scalar_check (status, 2))
2804 return false;
2806 return true;
2810 bool
2811 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2813 if (!type_check (a, 0, BT_LOGICAL))
2814 return false;
2815 if (!kind_check (kind, 1, BT_LOGICAL))
2816 return false;
2818 return true;
2822 /* Min/max family. */
2824 static bool
2825 min_max_args (gfc_actual_arglist *args)
2827 gfc_actual_arglist *arg;
2828 int i, j, nargs, *nlabels, nlabelless;
2829 bool a1 = false, a2 = false;
2831 if (args == NULL || args->next == NULL)
2833 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
2834 gfc_current_intrinsic, gfc_current_intrinsic_where);
2835 return false;
2838 if (!args->name)
2839 a1 = true;
2841 if (!args->next->name)
2842 a2 = true;
2844 nargs = 0;
2845 for (arg = args; arg; arg = arg->next)
2846 if (arg->name)
2847 nargs++;
2849 if (nargs == 0)
2850 return true;
2852 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2853 nlabelless = 0;
2854 nlabels = XALLOCAVEC (int, nargs);
2855 for (arg = args, i = 0; arg; arg = arg->next, i++)
2856 if (arg->name)
2858 int n;
2859 char *endp;
2861 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2862 goto unknown;
2863 n = strtol (&arg->name[1], &endp, 10);
2864 if (endp[0] != '\0')
2865 goto unknown;
2866 if (n <= 0)
2867 goto unknown;
2868 if (n <= nlabelless)
2869 goto duplicate;
2870 nlabels[i] = n;
2871 if (n == 1)
2872 a1 = true;
2873 if (n == 2)
2874 a2 = true;
2876 else
2877 nlabelless++;
2879 if (!a1 || !a2)
2881 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
2882 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2883 gfc_current_intrinsic_where);
2884 return false;
2887 /* Check for duplicates. */
2888 for (i = 0; i < nargs; i++)
2889 for (j = i + 1; j < nargs; j++)
2890 if (nlabels[i] == nlabels[j])
2891 goto duplicate;
2893 return true;
2895 duplicate:
2896 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
2897 &arg->expr->where, gfc_current_intrinsic);
2898 return false;
2900 unknown:
2901 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
2902 &arg->expr->where, gfc_current_intrinsic);
2903 return false;
2907 static bool
2908 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2910 gfc_actual_arglist *arg, *tmp;
2911 gfc_expr *x;
2912 int m, n;
2914 if (!min_max_args (arglist))
2915 return false;
2917 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2919 x = arg->expr;
2920 if (x->ts.type != type || x->ts.kind != kind)
2922 if (x->ts.type == type)
2924 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2925 "kinds at %L", &x->where))
2926 return false;
2928 else
2930 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
2931 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2932 gfc_basic_typename (type), kind);
2933 return false;
2937 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2938 if (!gfc_check_conformance (tmp->expr, x,
2939 "arguments 'a%d' and 'a%d' for "
2940 "intrinsic '%s'", m, n,
2941 gfc_current_intrinsic))
2942 return false;
2945 return true;
2949 bool
2950 gfc_check_min_max (gfc_actual_arglist *arg)
2952 gfc_expr *x;
2954 if (!min_max_args (arg))
2955 return false;
2957 x = arg->expr;
2959 if (x->ts.type == BT_CHARACTER)
2961 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2962 "with CHARACTER argument at %L",
2963 gfc_current_intrinsic, &x->where))
2964 return false;
2966 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2968 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
2969 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2970 return false;
2973 return check_rest (x->ts.type, x->ts.kind, arg);
2977 bool
2978 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2980 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2984 bool
2985 gfc_check_min_max_real (gfc_actual_arglist *arg)
2987 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2991 bool
2992 gfc_check_min_max_double (gfc_actual_arglist *arg)
2994 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2998 /* End of min/max family. */
3000 bool
3001 gfc_check_malloc (gfc_expr *size)
3003 if (!type_check (size, 0, BT_INTEGER))
3004 return false;
3006 if (!scalar_check (size, 0))
3007 return false;
3009 return true;
3013 bool
3014 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
3016 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
3018 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3019 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
3020 gfc_current_intrinsic, &matrix_a->where);
3021 return false;
3024 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
3026 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
3027 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
3028 gfc_current_intrinsic, &matrix_b->where);
3029 return false;
3032 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
3033 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
3035 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
3036 gfc_current_intrinsic, &matrix_a->where,
3037 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
3038 return false;
3041 switch (matrix_a->rank)
3043 case 1:
3044 if (!rank_check (matrix_b, 1, 2))
3045 return false;
3046 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
3047 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
3049 gfc_error ("Different shape on dimension 1 for arguments %qs "
3050 "and %qs at %L for intrinsic matmul",
3051 gfc_current_intrinsic_arg[0]->name,
3052 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3053 return false;
3055 break;
3057 case 2:
3058 if (matrix_b->rank != 2)
3060 if (!rank_check (matrix_b, 1, 1))
3061 return false;
3063 /* matrix_b has rank 1 or 2 here. Common check for the cases
3064 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
3065 - matrix_a has shape (n,m) and matrix_b has shape (m). */
3066 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
3068 gfc_error ("Different shape on dimension 2 for argument %qs and "
3069 "dimension 1 for argument %qs at %L for intrinsic "
3070 "matmul", gfc_current_intrinsic_arg[0]->name,
3071 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
3072 return false;
3074 break;
3076 default:
3077 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
3078 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3079 gfc_current_intrinsic, &matrix_a->where);
3080 return false;
3083 return true;
3087 /* Whoever came up with this interface was probably on something.
3088 The possibilities for the occupation of the second and third
3089 parameters are:
3091 Arg #2 Arg #3
3092 NULL NULL
3093 DIM NULL
3094 MASK NULL
3095 NULL MASK minloc(array, mask=m)
3096 DIM MASK
3098 I.e. in the case of minloc(array,mask), mask will be in the second
3099 position of the argument list and we'll have to fix that up. */
3101 bool
3102 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3104 gfc_expr *a, *m, *d;
3106 a = ap->expr;
3107 if (!int_or_real_check (a, 0) || !array_check (a, 0))
3108 return false;
3110 d = ap->next->expr;
3111 m = ap->next->next->expr;
3113 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3114 && ap->next->name == NULL)
3116 m = d;
3117 d = NULL;
3118 ap->next->expr = NULL;
3119 ap->next->next->expr = m;
3122 if (!dim_check (d, 1, false))
3123 return false;
3125 if (!dim_rank_check (d, a, 0))
3126 return false;
3128 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3129 return false;
3131 if (m != NULL
3132 && !gfc_check_conformance (a, m,
3133 "arguments '%s' and '%s' for intrinsic %s",
3134 gfc_current_intrinsic_arg[0]->name,
3135 gfc_current_intrinsic_arg[2]->name,
3136 gfc_current_intrinsic))
3137 return false;
3139 return true;
3143 /* Similar to minloc/maxloc, the argument list might need to be
3144 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3145 difference is that MINLOC/MAXLOC take an additional KIND argument.
3146 The possibilities are:
3148 Arg #2 Arg #3
3149 NULL NULL
3150 DIM NULL
3151 MASK NULL
3152 NULL MASK minval(array, mask=m)
3153 DIM MASK
3155 I.e. in the case of minval(array,mask), mask will be in the second
3156 position of the argument list and we'll have to fix that up. */
3158 static bool
3159 check_reduction (gfc_actual_arglist *ap)
3161 gfc_expr *a, *m, *d;
3163 a = ap->expr;
3164 d = ap->next->expr;
3165 m = ap->next->next->expr;
3167 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3168 && ap->next->name == NULL)
3170 m = d;
3171 d = NULL;
3172 ap->next->expr = NULL;
3173 ap->next->next->expr = m;
3176 if (!dim_check (d, 1, false))
3177 return false;
3179 if (!dim_rank_check (d, a, 0))
3180 return false;
3182 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3183 return false;
3185 if (m != NULL
3186 && !gfc_check_conformance (a, m,
3187 "arguments '%s' and '%s' for intrinsic %s",
3188 gfc_current_intrinsic_arg[0]->name,
3189 gfc_current_intrinsic_arg[2]->name,
3190 gfc_current_intrinsic))
3191 return false;
3193 return true;
3197 bool
3198 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3200 if (!int_or_real_check (ap->expr, 0)
3201 || !array_check (ap->expr, 0))
3202 return false;
3204 return check_reduction (ap);
3208 bool
3209 gfc_check_product_sum (gfc_actual_arglist *ap)
3211 if (!numeric_check (ap->expr, 0)
3212 || !array_check (ap->expr, 0))
3213 return false;
3215 return check_reduction (ap);
3219 /* For IANY, IALL and IPARITY. */
3221 bool
3222 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3224 int k;
3226 if (!type_check (i, 0, BT_INTEGER))
3227 return false;
3229 if (!nonnegative_check ("I", i))
3230 return false;
3232 if (!kind_check (kind, 1, BT_INTEGER))
3233 return false;
3235 if (kind)
3236 gfc_extract_int (kind, &k);
3237 else
3238 k = gfc_default_integer_kind;
3240 if (!less_than_bitsizekind ("I", i, k))
3241 return false;
3243 return true;
3247 bool
3248 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3250 if (ap->expr->ts.type != BT_INTEGER)
3252 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3253 gfc_current_intrinsic_arg[0]->name,
3254 gfc_current_intrinsic, &ap->expr->where);
3255 return false;
3258 if (!array_check (ap->expr, 0))
3259 return false;
3261 return check_reduction (ap);
3265 bool
3266 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3268 if (!same_type_check (tsource, 0, fsource, 1))
3269 return false;
3271 if (!type_check (mask, 2, BT_LOGICAL))
3272 return false;
3274 if (tsource->ts.type == BT_CHARACTER)
3275 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3277 return true;
3281 bool
3282 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3284 if (!type_check (i, 0, BT_INTEGER))
3285 return false;
3287 if (!type_check (j, 1, BT_INTEGER))
3288 return false;
3290 if (!type_check (mask, 2, BT_INTEGER))
3291 return false;
3293 if (!same_type_check (i, 0, j, 1))
3294 return false;
3296 if (!same_type_check (i, 0, mask, 2))
3297 return false;
3299 return true;
3303 bool
3304 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3306 if (!variable_check (from, 0, false))
3307 return false;
3308 if (!allocatable_check (from, 0))
3309 return false;
3310 if (gfc_is_coindexed (from))
3312 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3313 "coindexed", &from->where);
3314 return false;
3317 if (!variable_check (to, 1, false))
3318 return false;
3319 if (!allocatable_check (to, 1))
3320 return false;
3321 if (gfc_is_coindexed (to))
3323 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3324 "coindexed", &to->where);
3325 return false;
3328 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3330 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3331 "polymorphic if FROM is polymorphic",
3332 &to->where);
3333 return false;
3336 if (!same_type_check (to, 1, from, 0))
3337 return false;
3339 if (to->rank != from->rank)
3341 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3342 "must have the same rank %d/%d", &to->where, from->rank,
3343 to->rank);
3344 return false;
3347 /* IR F08/0040; cf. 12-006A. */
3348 if (gfc_get_corank (to) != gfc_get_corank (from))
3350 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3351 "must have the same corank %d/%d", &to->where,
3352 gfc_get_corank (from), gfc_get_corank (to));
3353 return false;
3356 /* This is based losely on F2003 12.4.1.7. It is intended to prevent
3357 the likes of to = sym->cmp1->cmp2 and from = sym->cmp1, where cmp1
3358 and cmp2 are allocatable. After the allocation is transferred,
3359 the 'to' chain is broken by the nullification of the 'from'. A bit
3360 of reflection reveals that this can only occur for derived types
3361 with recursive allocatable components. */
3362 if (to->expr_type == EXPR_VARIABLE && from->expr_type == EXPR_VARIABLE
3363 && !strcmp (to->symtree->n.sym->name, from->symtree->n.sym->name))
3365 gfc_ref *to_ref, *from_ref;
3366 to_ref = to->ref;
3367 from_ref = from->ref;
3368 bool aliasing = true;
3370 for (; from_ref && to_ref;
3371 from_ref = from_ref->next, to_ref = to_ref->next)
3373 if (to_ref->type != from->ref->type)
3374 aliasing = false;
3375 else if (to_ref->type == REF_ARRAY
3376 && to_ref->u.ar.type != AR_FULL
3377 && from_ref->u.ar.type != AR_FULL)
3378 /* Play safe; assume sections and elements are different. */
3379 aliasing = false;
3380 else if (to_ref->type == REF_COMPONENT
3381 && to_ref->u.c.component != from_ref->u.c.component)
3382 aliasing = false;
3384 if (!aliasing)
3385 break;
3388 if (aliasing)
3390 gfc_error ("The FROM and TO arguments at %L violate aliasing "
3391 "restrictions (F2003 12.4.1.7)", &to->where);
3392 return false;
3396 /* CLASS arguments: Make sure the vtab of from is present. */
3397 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3398 gfc_find_vtab (&from->ts);
3400 return true;
3404 bool
3405 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3407 if (!type_check (x, 0, BT_REAL))
3408 return false;
3410 if (!type_check (s, 1, BT_REAL))
3411 return false;
3413 if (s->expr_type == EXPR_CONSTANT)
3415 if (mpfr_sgn (s->value.real) == 0)
3417 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3418 &s->where);
3419 return false;
3423 return true;
3427 bool
3428 gfc_check_new_line (gfc_expr *a)
3430 if (!type_check (a, 0, BT_CHARACTER))
3431 return false;
3433 return true;
3437 bool
3438 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3440 if (!type_check (array, 0, BT_REAL))
3441 return false;
3443 if (!array_check (array, 0))
3444 return false;
3446 if (!dim_rank_check (dim, array, false))
3447 return false;
3449 return true;
3452 bool
3453 gfc_check_null (gfc_expr *mold)
3455 symbol_attribute attr;
3457 if (mold == NULL)
3458 return true;
3460 if (!variable_check (mold, 0, true))
3461 return false;
3463 attr = gfc_variable_attr (mold, NULL);
3465 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3467 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3468 "ALLOCATABLE or procedure pointer",
3469 gfc_current_intrinsic_arg[0]->name,
3470 gfc_current_intrinsic, &mold->where);
3471 return false;
3474 if (attr.allocatable
3475 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3476 "allocatable MOLD at %L", &mold->where))
3477 return false;
3479 /* F2008, C1242. */
3480 if (gfc_is_coindexed (mold))
3482 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3483 "coindexed", gfc_current_intrinsic_arg[0]->name,
3484 gfc_current_intrinsic, &mold->where);
3485 return false;
3488 return true;
3492 bool
3493 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3495 if (!array_check (array, 0))
3496 return false;
3498 if (!type_check (mask, 1, BT_LOGICAL))
3499 return false;
3501 if (!gfc_check_conformance (array, mask,
3502 "arguments '%s' and '%s' for intrinsic '%s'",
3503 gfc_current_intrinsic_arg[0]->name,
3504 gfc_current_intrinsic_arg[1]->name,
3505 gfc_current_intrinsic))
3506 return false;
3508 if (vector != NULL)
3510 mpz_t array_size, vector_size;
3511 bool have_array_size, have_vector_size;
3513 if (!same_type_check (array, 0, vector, 2))
3514 return false;
3516 if (!rank_check (vector, 2, 1))
3517 return false;
3519 /* VECTOR requires at least as many elements as MASK
3520 has .TRUE. values. */
3521 have_array_size = gfc_array_size(array, &array_size);
3522 have_vector_size = gfc_array_size(vector, &vector_size);
3524 if (have_vector_size
3525 && (mask->expr_type == EXPR_ARRAY
3526 || (mask->expr_type == EXPR_CONSTANT
3527 && have_array_size)))
3529 int mask_true_values = 0;
3531 if (mask->expr_type == EXPR_ARRAY)
3533 gfc_constructor *mask_ctor;
3534 mask_ctor = gfc_constructor_first (mask->value.constructor);
3535 while (mask_ctor)
3537 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3539 mask_true_values = 0;
3540 break;
3543 if (mask_ctor->expr->value.logical)
3544 mask_true_values++;
3546 mask_ctor = gfc_constructor_next (mask_ctor);
3549 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3550 mask_true_values = mpz_get_si (array_size);
3552 if (mpz_get_si (vector_size) < mask_true_values)
3554 gfc_error ("%qs argument of %qs intrinsic at %L must "
3555 "provide at least as many elements as there "
3556 "are .TRUE. values in %qs (%ld/%d)",
3557 gfc_current_intrinsic_arg[2]->name,
3558 gfc_current_intrinsic, &vector->where,
3559 gfc_current_intrinsic_arg[1]->name,
3560 mpz_get_si (vector_size), mask_true_values);
3561 return false;
3565 if (have_array_size)
3566 mpz_clear (array_size);
3567 if (have_vector_size)
3568 mpz_clear (vector_size);
3571 return true;
3575 bool
3576 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3578 if (!type_check (mask, 0, BT_LOGICAL))
3579 return false;
3581 if (!array_check (mask, 0))
3582 return false;
3584 if (!dim_rank_check (dim, mask, false))
3585 return false;
3587 return true;
3591 bool
3592 gfc_check_precision (gfc_expr *x)
3594 if (!real_or_complex_check (x, 0))
3595 return false;
3597 return true;
3601 bool
3602 gfc_check_present (gfc_expr *a)
3604 gfc_symbol *sym;
3606 if (!variable_check (a, 0, true))
3607 return false;
3609 sym = a->symtree->n.sym;
3610 if (!sym->attr.dummy)
3612 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3613 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3614 gfc_current_intrinsic, &a->where);
3615 return false;
3618 if (!sym->attr.optional)
3620 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3621 "an OPTIONAL dummy variable",
3622 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3623 &a->where);
3624 return false;
3627 /* 13.14.82 PRESENT(A)
3628 ......
3629 Argument. A shall be the name of an optional dummy argument that is
3630 accessible in the subprogram in which the PRESENT function reference
3631 appears... */
3633 if (a->ref != NULL
3634 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3635 && (a->ref->u.ar.type == AR_FULL
3636 || (a->ref->u.ar.type == AR_ELEMENT
3637 && a->ref->u.ar.as->rank == 0))))
3639 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3640 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3641 gfc_current_intrinsic, &a->where, sym->name);
3642 return false;
3645 return true;
3649 bool
3650 gfc_check_radix (gfc_expr *x)
3652 if (!int_or_real_check (x, 0))
3653 return false;
3655 return true;
3659 bool
3660 gfc_check_range (gfc_expr *x)
3662 if (!numeric_check (x, 0))
3663 return false;
3665 return true;
3669 bool
3670 gfc_check_rank (gfc_expr *a)
3672 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3673 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3675 bool is_variable = true;
3677 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3678 if (a->expr_type == EXPR_FUNCTION)
3679 is_variable = a->value.function.esym
3680 ? a->value.function.esym->result->attr.pointer
3681 : a->symtree->n.sym->result->attr.pointer;
3683 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3684 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3685 || !is_variable)
3687 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3688 "object", &a->where);
3689 return false;
3692 return true;
3696 /* real, float, sngl. */
3697 bool
3698 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3700 if (!numeric_check (a, 0))
3701 return false;
3703 if (!kind_check (kind, 1, BT_REAL))
3704 return false;
3706 return true;
3710 bool
3711 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3713 if (!type_check (path1, 0, BT_CHARACTER))
3714 return false;
3715 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3716 return false;
3718 if (!type_check (path2, 1, BT_CHARACTER))
3719 return false;
3720 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3721 return false;
3723 return true;
3727 bool
3728 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3730 if (!type_check (path1, 0, BT_CHARACTER))
3731 return false;
3732 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3733 return false;
3735 if (!type_check (path2, 1, BT_CHARACTER))
3736 return false;
3737 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3738 return false;
3740 if (status == NULL)
3741 return true;
3743 if (!type_check (status, 2, BT_INTEGER))
3744 return false;
3746 if (!scalar_check (status, 2))
3747 return false;
3749 return true;
3753 bool
3754 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3756 if (!type_check (x, 0, BT_CHARACTER))
3757 return false;
3759 if (!scalar_check (x, 0))
3760 return false;
3762 if (!type_check (y, 0, BT_INTEGER))
3763 return false;
3765 if (!scalar_check (y, 1))
3766 return false;
3768 return true;
3772 bool
3773 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3774 gfc_expr *pad, gfc_expr *order)
3776 mpz_t size;
3777 mpz_t nelems;
3778 int shape_size;
3780 if (!array_check (source, 0))
3781 return false;
3783 if (!rank_check (shape, 1, 1))
3784 return false;
3786 if (!type_check (shape, 1, BT_INTEGER))
3787 return false;
3789 if (!gfc_array_size (shape, &size))
3791 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3792 "array of constant size", &shape->where);
3793 return false;
3796 shape_size = mpz_get_ui (size);
3797 mpz_clear (size);
3799 if (shape_size <= 0)
3801 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3802 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3803 &shape->where);
3804 return false;
3806 else if (shape_size > GFC_MAX_DIMENSIONS)
3808 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
3809 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3810 return false;
3812 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3814 gfc_expr *e;
3815 int i, extent;
3816 for (i = 0; i < shape_size; ++i)
3818 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3819 if (e->expr_type != EXPR_CONSTANT)
3820 continue;
3822 gfc_extract_int (e, &extent);
3823 if (extent < 0)
3825 gfc_error ("%qs argument of %qs intrinsic at %L has "
3826 "negative element (%d)",
3827 gfc_current_intrinsic_arg[1]->name,
3828 gfc_current_intrinsic, &e->where, extent);
3829 return false;
3833 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
3834 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
3835 && shape->ref->u.ar.as
3836 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
3837 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
3838 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
3839 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
3840 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
3842 int i, extent;
3843 gfc_expr *e, *v;
3845 v = shape->symtree->n.sym->value;
3847 for (i = 0; i < shape_size; i++)
3849 e = gfc_constructor_lookup_expr (v->value.constructor, i);
3850 if (e == NULL)
3851 break;
3853 gfc_extract_int (e, &extent);
3855 if (extent < 0)
3857 gfc_error ("Element %d of actual argument of RESHAPE at %L "
3858 "cannot be negative", i + 1, &shape->where);
3859 return false;
3864 if (pad != NULL)
3866 if (!same_type_check (source, 0, pad, 2))
3867 return false;
3869 if (!array_check (pad, 2))
3870 return false;
3873 if (order != NULL)
3875 if (!array_check (order, 3))
3876 return false;
3878 if (!type_check (order, 3, BT_INTEGER))
3879 return false;
3881 if (order->expr_type == EXPR_ARRAY && gfc_is_constant_expr (order))
3883 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3884 gfc_expr *e;
3886 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3887 perm[i] = 0;
3889 gfc_array_size (order, &size);
3890 order_size = mpz_get_ui (size);
3891 mpz_clear (size);
3893 if (order_size != shape_size)
3895 gfc_error ("%qs argument of %qs intrinsic at %L "
3896 "has wrong number of elements (%d/%d)",
3897 gfc_current_intrinsic_arg[3]->name,
3898 gfc_current_intrinsic, &order->where,
3899 order_size, shape_size);
3900 return false;
3903 for (i = 1; i <= order_size; ++i)
3905 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3906 if (e->expr_type != EXPR_CONSTANT)
3907 continue;
3909 gfc_extract_int (e, &dim);
3911 if (dim < 1 || dim > order_size)
3913 gfc_error ("%qs argument of %qs intrinsic at %L "
3914 "has out-of-range dimension (%d)",
3915 gfc_current_intrinsic_arg[3]->name,
3916 gfc_current_intrinsic, &e->where, dim);
3917 return false;
3920 if (perm[dim-1] != 0)
3922 gfc_error ("%qs argument of %qs intrinsic at %L has "
3923 "invalid permutation of dimensions (dimension "
3924 "%qd duplicated)",
3925 gfc_current_intrinsic_arg[3]->name,
3926 gfc_current_intrinsic, &e->where, dim);
3927 return false;
3930 perm[dim-1] = 1;
3935 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3936 && gfc_is_constant_expr (shape)
3937 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3938 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3940 /* Check the match in size between source and destination. */
3941 if (gfc_array_size (source, &nelems))
3943 gfc_constructor *c;
3944 bool test;
3947 mpz_init_set_ui (size, 1);
3948 for (c = gfc_constructor_first (shape->value.constructor);
3949 c; c = gfc_constructor_next (c))
3950 mpz_mul (size, size, c->expr->value.integer);
3952 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3953 mpz_clear (nelems);
3954 mpz_clear (size);
3956 if (test)
3958 gfc_error ("Without padding, there are not enough elements "
3959 "in the intrinsic RESHAPE source at %L to match "
3960 "the shape", &source->where);
3961 return false;
3966 return true;
3970 bool
3971 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3973 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3975 gfc_error ("%qs argument of %qs intrinsic at %L "
3976 "cannot be of type %s",
3977 gfc_current_intrinsic_arg[0]->name,
3978 gfc_current_intrinsic,
3979 &a->where, gfc_typename (&a->ts));
3980 return false;
3983 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3985 gfc_error ("%qs argument of %qs intrinsic at %L "
3986 "must be of an extensible type",
3987 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3988 &a->where);
3989 return false;
3992 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3994 gfc_error ("%qs argument of %qs intrinsic at %L "
3995 "cannot be of type %s",
3996 gfc_current_intrinsic_arg[0]->name,
3997 gfc_current_intrinsic,
3998 &b->where, gfc_typename (&b->ts));
3999 return false;
4002 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
4004 gfc_error ("%qs argument of %qs intrinsic at %L "
4005 "must be of an extensible type",
4006 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4007 &b->where);
4008 return false;
4011 return true;
4015 bool
4016 gfc_check_scale (gfc_expr *x, gfc_expr *i)
4018 if (!type_check (x, 0, BT_REAL))
4019 return false;
4021 if (!type_check (i, 1, BT_INTEGER))
4022 return false;
4024 return true;
4028 bool
4029 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4031 if (!type_check (x, 0, BT_CHARACTER))
4032 return false;
4034 if (!type_check (y, 1, BT_CHARACTER))
4035 return false;
4037 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4038 return false;
4040 if (!kind_check (kind, 3, BT_INTEGER))
4041 return false;
4042 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4043 "with KIND argument at %L",
4044 gfc_current_intrinsic, &kind->where))
4045 return false;
4047 if (!same_type_check (x, 0, y, 1))
4048 return false;
4050 return true;
4054 bool
4055 gfc_check_secnds (gfc_expr *r)
4057 if (!type_check (r, 0, BT_REAL))
4058 return false;
4060 if (!kind_value_check (r, 0, 4))
4061 return false;
4063 if (!scalar_check (r, 0))
4064 return false;
4066 return true;
4070 bool
4071 gfc_check_selected_char_kind (gfc_expr *name)
4073 if (!type_check (name, 0, BT_CHARACTER))
4074 return false;
4076 if (!kind_value_check (name, 0, gfc_default_character_kind))
4077 return false;
4079 if (!scalar_check (name, 0))
4080 return false;
4082 return true;
4086 bool
4087 gfc_check_selected_int_kind (gfc_expr *r)
4089 if (!type_check (r, 0, BT_INTEGER))
4090 return false;
4092 if (!scalar_check (r, 0))
4093 return false;
4095 return true;
4099 bool
4100 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
4102 if (p == NULL && r == NULL
4103 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
4104 " neither %<P%> nor %<R%> argument at %L",
4105 gfc_current_intrinsic_where))
4106 return false;
4108 if (p)
4110 if (!type_check (p, 0, BT_INTEGER))
4111 return false;
4113 if (!scalar_check (p, 0))
4114 return false;
4117 if (r)
4119 if (!type_check (r, 1, BT_INTEGER))
4120 return false;
4122 if (!scalar_check (r, 1))
4123 return false;
4126 if (radix)
4128 if (!type_check (radix, 1, BT_INTEGER))
4129 return false;
4131 if (!scalar_check (radix, 1))
4132 return false;
4134 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4135 "RADIX argument at %L", gfc_current_intrinsic,
4136 &radix->where))
4137 return false;
4140 return true;
4144 bool
4145 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4147 if (!type_check (x, 0, BT_REAL))
4148 return false;
4150 if (!type_check (i, 1, BT_INTEGER))
4151 return false;
4153 return true;
4157 bool
4158 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4160 gfc_array_ref *ar;
4162 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4163 return true;
4165 ar = gfc_find_array_ref (source);
4167 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4169 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4170 "an assumed size array", &source->where);
4171 return false;
4174 if (!kind_check (kind, 1, BT_INTEGER))
4175 return false;
4176 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4177 "with KIND argument at %L",
4178 gfc_current_intrinsic, &kind->where))
4179 return false;
4181 return true;
4185 bool
4186 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4188 if (!type_check (i, 0, BT_INTEGER))
4189 return false;
4191 if (!type_check (shift, 0, BT_INTEGER))
4192 return false;
4194 if (!nonnegative_check ("SHIFT", shift))
4195 return false;
4197 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4198 return false;
4200 return true;
4204 bool
4205 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4207 if (!int_or_real_check (a, 0))
4208 return false;
4210 if (!same_type_check (a, 0, b, 1))
4211 return false;
4213 return true;
4217 bool
4218 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4220 if (!array_check (array, 0))
4221 return false;
4223 if (!dim_check (dim, 1, true))
4224 return false;
4226 if (!dim_rank_check (dim, array, 0))
4227 return false;
4229 if (!kind_check (kind, 2, BT_INTEGER))
4230 return false;
4231 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4232 "with KIND argument at %L",
4233 gfc_current_intrinsic, &kind->where))
4234 return false;
4237 return true;
4241 bool
4242 gfc_check_sizeof (gfc_expr *arg)
4244 if (arg->ts.type == BT_PROCEDURE)
4246 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4247 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4248 &arg->where);
4249 return false;
4252 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4253 if (arg->ts.type == BT_ASSUMED
4254 && (arg->symtree->n.sym->as == NULL
4255 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4256 && arg->symtree->n.sym->as->type != AS_DEFERRED
4257 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4259 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4260 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4261 &arg->where);
4262 return false;
4265 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4266 && arg->symtree->n.sym->as != NULL
4267 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4268 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4270 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4271 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4272 gfc_current_intrinsic, &arg->where);
4273 return false;
4276 return true;
4280 /* Check whether an expression is interoperable. When returning false,
4281 msg is set to a string telling why the expression is not interoperable,
4282 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4283 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4284 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4285 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4286 are permitted. */
4288 static bool
4289 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4291 *msg = NULL;
4293 if (expr->ts.type == BT_CLASS)
4295 *msg = "Expression is polymorphic";
4296 return false;
4299 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4300 && !expr->ts.u.derived->ts.is_iso_c)
4302 *msg = "Expression is a noninteroperable derived type";
4303 return false;
4306 if (expr->ts.type == BT_PROCEDURE)
4308 *msg = "Procedure unexpected as argument";
4309 return false;
4312 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4314 int i;
4315 for (i = 0; gfc_logical_kinds[i].kind; i++)
4316 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4317 return true;
4318 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4319 return false;
4322 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4323 && expr->ts.kind != 1)
4325 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4326 return false;
4329 if (expr->ts.type == BT_CHARACTER) {
4330 if (expr->ts.deferred)
4332 /* TS 29113 allows deferred-length strings as dummy arguments,
4333 but it is not an interoperable type. */
4334 *msg = "Expression shall not be a deferred-length string";
4335 return false;
4338 if (expr->ts.u.cl && expr->ts.u.cl->length
4339 && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
4340 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4342 if (!c_loc && expr->ts.u.cl
4343 && (!expr->ts.u.cl->length
4344 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4345 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4347 *msg = "Type shall have a character length of 1";
4348 return false;
4352 /* Note: The following checks are about interoperatable variables, Fortran
4353 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4354 is allowed, e.g. assumed-shape arrays with TS 29113. */
4356 if (gfc_is_coarray (expr))
4358 *msg = "Coarrays are not interoperable";
4359 return false;
4362 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4364 gfc_array_ref *ar = gfc_find_array_ref (expr);
4365 if (ar->type != AR_FULL)
4367 *msg = "Only whole-arrays are interoperable";
4368 return false;
4370 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4371 && ar->as->type != AS_ASSUMED_SIZE)
4373 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4374 return false;
4378 return true;
4382 bool
4383 gfc_check_c_sizeof (gfc_expr *arg)
4385 const char *msg;
4387 if (!is_c_interoperable (arg, &msg, false, false))
4389 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4390 "interoperable data entity: %s",
4391 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4392 &arg->where, msg);
4393 return false;
4396 if (arg->ts.type == BT_ASSUMED)
4398 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4399 "TYPE(*)",
4400 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4401 &arg->where);
4402 return false;
4405 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4406 && arg->symtree->n.sym->as != NULL
4407 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4408 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4410 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4411 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4412 gfc_current_intrinsic, &arg->where);
4413 return false;
4416 return true;
4420 bool
4421 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4423 if (c_ptr_1->ts.type != BT_DERIVED
4424 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4425 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4426 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4428 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4429 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4430 return false;
4433 if (!scalar_check (c_ptr_1, 0))
4434 return false;
4436 if (c_ptr_2
4437 && (c_ptr_2->ts.type != BT_DERIVED
4438 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4439 || (c_ptr_1->ts.u.derived->intmod_sym_id
4440 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4442 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4443 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4444 gfc_typename (&c_ptr_1->ts),
4445 gfc_typename (&c_ptr_2->ts));
4446 return false;
4449 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4450 return false;
4452 return true;
4456 bool
4457 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4459 symbol_attribute attr;
4460 const char *msg;
4462 if (cptr->ts.type != BT_DERIVED
4463 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4464 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4466 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4467 "type TYPE(C_PTR)", &cptr->where);
4468 return false;
4471 if (!scalar_check (cptr, 0))
4472 return false;
4474 attr = gfc_expr_attr (fptr);
4476 if (!attr.pointer)
4478 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4479 &fptr->where);
4480 return false;
4483 if (fptr->ts.type == BT_CLASS)
4485 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4486 &fptr->where);
4487 return false;
4490 if (gfc_is_coindexed (fptr))
4492 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4493 "coindexed", &fptr->where);
4494 return false;
4497 if (fptr->rank == 0 && shape)
4499 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4500 "FPTR", &fptr->where);
4501 return false;
4503 else if (fptr->rank && !shape)
4505 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4506 "FPTR at %L", &fptr->where);
4507 return false;
4510 if (shape && !rank_check (shape, 2, 1))
4511 return false;
4513 if (shape && !type_check (shape, 2, BT_INTEGER))
4514 return false;
4516 if (shape)
4518 mpz_t size;
4519 if (gfc_array_size (shape, &size))
4521 if (mpz_cmp_ui (size, fptr->rank) != 0)
4523 mpz_clear (size);
4524 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4525 "size as the RANK of FPTR", &shape->where);
4526 return false;
4528 mpz_clear (size);
4532 if (fptr->ts.type == BT_CLASS)
4534 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4535 return false;
4538 if (!is_c_interoperable (fptr, &msg, false, true))
4539 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4540 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4542 return true;
4546 bool
4547 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4549 symbol_attribute attr;
4551 if (cptr->ts.type != BT_DERIVED
4552 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4553 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4555 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4556 "type TYPE(C_FUNPTR)", &cptr->where);
4557 return false;
4560 if (!scalar_check (cptr, 0))
4561 return false;
4563 attr = gfc_expr_attr (fptr);
4565 if (!attr.proc_pointer)
4567 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4568 "pointer", &fptr->where);
4569 return false;
4572 if (gfc_is_coindexed (fptr))
4574 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4575 "coindexed", &fptr->where);
4576 return false;
4579 if (!attr.is_bind_c)
4580 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4581 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4583 return true;
4587 bool
4588 gfc_check_c_funloc (gfc_expr *x)
4590 symbol_attribute attr;
4592 if (gfc_is_coindexed (x))
4594 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4595 "coindexed", &x->where);
4596 return false;
4599 attr = gfc_expr_attr (x);
4601 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4602 && x->symtree->n.sym == x->symtree->n.sym->result)
4604 gfc_namespace *ns = gfc_current_ns;
4606 for (ns = gfc_current_ns; ns; ns = ns->parent)
4607 if (x->symtree->n.sym == ns->proc_name)
4609 gfc_error ("Function result %qs at %L is invalid as X argument "
4610 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4611 return false;
4615 if (attr.flavor != FL_PROCEDURE)
4617 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4618 "or a procedure pointer", &x->where);
4619 return false;
4622 if (!attr.is_bind_c)
4623 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4624 "at %L to C_FUNLOC", &x->where);
4625 return true;
4629 bool
4630 gfc_check_c_loc (gfc_expr *x)
4632 symbol_attribute attr;
4633 const char *msg;
4635 if (gfc_is_coindexed (x))
4637 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4638 return false;
4641 if (x->ts.type == BT_CLASS)
4643 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4644 &x->where);
4645 return false;
4648 attr = gfc_expr_attr (x);
4650 if (!attr.pointer
4651 && (x->expr_type != EXPR_VARIABLE || !attr.target
4652 || attr.flavor == FL_PARAMETER))
4654 gfc_error ("Argument X at %L to C_LOC shall have either "
4655 "the POINTER or the TARGET attribute", &x->where);
4656 return false;
4659 if (x->ts.type == BT_CHARACTER
4660 && gfc_var_strlen (x) == 0)
4662 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4663 "string", &x->where);
4664 return false;
4667 if (!is_c_interoperable (x, &msg, true, false))
4669 if (x->ts.type == BT_CLASS)
4671 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4672 &x->where);
4673 return false;
4676 if (x->rank
4677 && !gfc_notify_std (GFC_STD_F2008_TS,
4678 "Noninteroperable array at %L as"
4679 " argument to C_LOC: %s", &x->where, msg))
4680 return false;
4682 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4684 gfc_array_ref *ar = gfc_find_array_ref (x);
4686 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4687 && !attr.allocatable
4688 && !gfc_notify_std (GFC_STD_F2008,
4689 "Array of interoperable type at %L "
4690 "to C_LOC which is nonallocatable and neither "
4691 "assumed size nor explicit size", &x->where))
4692 return false;
4693 else if (ar->type != AR_FULL
4694 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4695 "to C_LOC", &x->where))
4696 return false;
4699 return true;
4703 bool
4704 gfc_check_sleep_sub (gfc_expr *seconds)
4706 if (!type_check (seconds, 0, BT_INTEGER))
4707 return false;
4709 if (!scalar_check (seconds, 0))
4710 return false;
4712 return true;
4715 bool
4716 gfc_check_sngl (gfc_expr *a)
4718 if (!type_check (a, 0, BT_REAL))
4719 return false;
4721 if ((a->ts.kind != gfc_default_double_kind)
4722 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4723 "REAL argument to %s intrinsic at %L",
4724 gfc_current_intrinsic, &a->where))
4725 return false;
4727 return true;
4730 bool
4731 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4733 if (source->rank >= GFC_MAX_DIMENSIONS)
4735 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4736 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4737 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4739 return false;
4742 if (dim == NULL)
4743 return false;
4745 if (!dim_check (dim, 1, false))
4746 return false;
4748 /* dim_rank_check() does not apply here. */
4749 if (dim
4750 && dim->expr_type == EXPR_CONSTANT
4751 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4752 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4754 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4755 "dimension index", gfc_current_intrinsic_arg[1]->name,
4756 gfc_current_intrinsic, &dim->where);
4757 return false;
4760 if (!type_check (ncopies, 2, BT_INTEGER))
4761 return false;
4763 if (!scalar_check (ncopies, 2))
4764 return false;
4766 return true;
4770 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4771 functions). */
4773 bool
4774 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4776 if (!type_check (unit, 0, BT_INTEGER))
4777 return false;
4779 if (!scalar_check (unit, 0))
4780 return false;
4782 if (!type_check (c, 1, BT_CHARACTER))
4783 return false;
4784 if (!kind_value_check (c, 1, gfc_default_character_kind))
4785 return false;
4787 if (status == NULL)
4788 return true;
4790 if (!type_check (status, 2, BT_INTEGER)
4791 || !kind_value_check (status, 2, gfc_default_integer_kind)
4792 || !scalar_check (status, 2))
4793 return false;
4795 return true;
4799 bool
4800 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4802 return gfc_check_fgetputc_sub (unit, c, NULL);
4806 bool
4807 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4809 if (!type_check (c, 0, BT_CHARACTER))
4810 return false;
4811 if (!kind_value_check (c, 0, gfc_default_character_kind))
4812 return false;
4814 if (status == NULL)
4815 return true;
4817 if (!type_check (status, 1, BT_INTEGER)
4818 || !kind_value_check (status, 1, gfc_default_integer_kind)
4819 || !scalar_check (status, 1))
4820 return false;
4822 return true;
4826 bool
4827 gfc_check_fgetput (gfc_expr *c)
4829 return gfc_check_fgetput_sub (c, NULL);
4833 bool
4834 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4836 if (!type_check (unit, 0, BT_INTEGER))
4837 return false;
4839 if (!scalar_check (unit, 0))
4840 return false;
4842 if (!type_check (offset, 1, BT_INTEGER))
4843 return false;
4845 if (!scalar_check (offset, 1))
4846 return false;
4848 if (!type_check (whence, 2, BT_INTEGER))
4849 return false;
4851 if (!scalar_check (whence, 2))
4852 return false;
4854 if (status == NULL)
4855 return true;
4857 if (!type_check (status, 3, BT_INTEGER))
4858 return false;
4860 if (!kind_value_check (status, 3, 4))
4861 return false;
4863 if (!scalar_check (status, 3))
4864 return false;
4866 return true;
4871 bool
4872 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4874 if (!type_check (unit, 0, BT_INTEGER))
4875 return false;
4877 if (!scalar_check (unit, 0))
4878 return false;
4880 if (!type_check (array, 1, BT_INTEGER)
4881 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4882 return false;
4884 if (!array_check (array, 1))
4885 return false;
4887 return true;
4891 bool
4892 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4894 if (!type_check (unit, 0, BT_INTEGER))
4895 return false;
4897 if (!scalar_check (unit, 0))
4898 return false;
4900 if (!type_check (array, 1, BT_INTEGER)
4901 || !kind_value_check (array, 1, gfc_default_integer_kind))
4902 return false;
4904 if (!array_check (array, 1))
4905 return false;
4907 if (status == NULL)
4908 return true;
4910 if (!type_check (status, 2, BT_INTEGER)
4911 || !kind_value_check (status, 2, gfc_default_integer_kind))
4912 return false;
4914 if (!scalar_check (status, 2))
4915 return false;
4917 return true;
4921 bool
4922 gfc_check_ftell (gfc_expr *unit)
4924 if (!type_check (unit, 0, BT_INTEGER))
4925 return false;
4927 if (!scalar_check (unit, 0))
4928 return false;
4930 return true;
4934 bool
4935 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4937 if (!type_check (unit, 0, BT_INTEGER))
4938 return false;
4940 if (!scalar_check (unit, 0))
4941 return false;
4943 if (!type_check (offset, 1, BT_INTEGER))
4944 return false;
4946 if (!scalar_check (offset, 1))
4947 return false;
4949 return true;
4953 bool
4954 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4956 if (!type_check (name, 0, BT_CHARACTER))
4957 return false;
4958 if (!kind_value_check (name, 0, gfc_default_character_kind))
4959 return false;
4961 if (!type_check (array, 1, BT_INTEGER)
4962 || !kind_value_check (array, 1, gfc_default_integer_kind))
4963 return false;
4965 if (!array_check (array, 1))
4966 return false;
4968 return true;
4972 bool
4973 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4975 if (!type_check (name, 0, BT_CHARACTER))
4976 return false;
4977 if (!kind_value_check (name, 0, gfc_default_character_kind))
4978 return false;
4980 if (!type_check (array, 1, BT_INTEGER)
4981 || !kind_value_check (array, 1, gfc_default_integer_kind))
4982 return false;
4984 if (!array_check (array, 1))
4985 return false;
4987 if (status == NULL)
4988 return true;
4990 if (!type_check (status, 2, BT_INTEGER)
4991 || !kind_value_check (array, 1, gfc_default_integer_kind))
4992 return false;
4994 if (!scalar_check (status, 2))
4995 return false;
4997 return true;
5001 bool
5002 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
5004 mpz_t nelems;
5006 if (flag_coarray == GFC_FCOARRAY_NONE)
5008 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5009 return false;
5012 if (!coarray_check (coarray, 0))
5013 return false;
5015 if (sub->rank != 1)
5017 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
5018 gfc_current_intrinsic_arg[1]->name, &sub->where);
5019 return false;
5022 if (gfc_array_size (sub, &nelems))
5024 int corank = gfc_get_corank (coarray);
5026 if (mpz_cmp_ui (nelems, corank) != 0)
5028 gfc_error ("The number of array elements of the SUB argument to "
5029 "IMAGE_INDEX at %L shall be %d (corank) not %d",
5030 &sub->where, corank, (int) mpz_get_si (nelems));
5031 mpz_clear (nelems);
5032 return false;
5034 mpz_clear (nelems);
5037 return true;
5041 bool
5042 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
5044 if (flag_coarray == GFC_FCOARRAY_NONE)
5046 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5047 return false;
5050 if (distance)
5052 if (!type_check (distance, 0, BT_INTEGER))
5053 return false;
5055 if (!nonnegative_check ("DISTANCE", distance))
5056 return false;
5058 if (!scalar_check (distance, 0))
5059 return false;
5061 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5062 "NUM_IMAGES at %L", &distance->where))
5063 return false;
5066 if (failed)
5068 if (!type_check (failed, 1, BT_LOGICAL))
5069 return false;
5071 if (!scalar_check (failed, 1))
5072 return false;
5074 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
5075 "NUM_IMAGES at %L", &distance->where))
5076 return false;
5079 return true;
5083 bool
5084 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
5086 if (flag_coarray == GFC_FCOARRAY_NONE)
5088 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5089 return false;
5092 if (coarray == NULL && dim == NULL && distance == NULL)
5093 return true;
5095 if (dim != NULL && coarray == NULL)
5097 gfc_error ("DIM argument without COARRAY argument not allowed for "
5098 "THIS_IMAGE intrinsic at %L", &dim->where);
5099 return false;
5102 if (distance && (coarray || dim))
5104 gfc_error ("The DISTANCE argument may not be specified together with the "
5105 "COARRAY or DIM argument in intrinsic at %L",
5106 &distance->where);
5107 return false;
5110 /* Assume that we have "this_image (distance)". */
5111 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
5113 if (dim)
5115 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
5116 &coarray->where);
5117 return false;
5119 distance = coarray;
5122 if (distance)
5124 if (!type_check (distance, 2, BT_INTEGER))
5125 return false;
5127 if (!nonnegative_check ("DISTANCE", distance))
5128 return false;
5130 if (!scalar_check (distance, 2))
5131 return false;
5133 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5134 "THIS_IMAGE at %L", &distance->where))
5135 return false;
5137 return true;
5140 if (!coarray_check (coarray, 0))
5141 return false;
5143 if (dim != NULL)
5145 if (!dim_check (dim, 1, false))
5146 return false;
5148 if (!dim_corank_check (dim, coarray))
5149 return false;
5152 return true;
5155 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5156 by gfc_simplify_transfer. Return false if we cannot do so. */
5158 bool
5159 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5160 size_t *source_size, size_t *result_size,
5161 size_t *result_length_p)
5163 size_t result_elt_size;
5165 if (source->expr_type == EXPR_FUNCTION)
5166 return false;
5168 if (size && size->expr_type != EXPR_CONSTANT)
5169 return false;
5171 /* Calculate the size of the source. */
5172 *source_size = gfc_target_expr_size (source);
5173 if (*source_size == 0)
5174 return false;
5176 /* Determine the size of the element. */
5177 result_elt_size = gfc_element_size (mold);
5178 if (result_elt_size == 0)
5179 return false;
5181 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5183 int result_length;
5185 if (size)
5186 result_length = (size_t)mpz_get_ui (size->value.integer);
5187 else
5189 result_length = *source_size / result_elt_size;
5190 if (result_length * result_elt_size < *source_size)
5191 result_length += 1;
5194 *result_size = result_length * result_elt_size;
5195 if (result_length_p)
5196 *result_length_p = result_length;
5198 else
5199 *result_size = result_elt_size;
5201 return true;
5205 bool
5206 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5208 size_t source_size;
5209 size_t result_size;
5211 if (mold->ts.type == BT_HOLLERITH)
5213 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5214 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5215 return false;
5218 if (size != NULL)
5220 if (!type_check (size, 2, BT_INTEGER))
5221 return false;
5223 if (!scalar_check (size, 2))
5224 return false;
5226 if (!nonoptional_check (size, 2))
5227 return false;
5230 if (!warn_surprising)
5231 return true;
5233 /* If we can't calculate the sizes, we cannot check any more.
5234 Return true for that case. */
5236 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5237 &result_size, NULL))
5238 return true;
5240 if (source_size < result_size)
5241 gfc_warning (OPT_Wsurprising,
5242 "Intrinsic TRANSFER at %L has partly undefined result: "
5243 "source size %ld < result size %ld", &source->where,
5244 (long) source_size, (long) result_size);
5246 return true;
5250 bool
5251 gfc_check_transpose (gfc_expr *matrix)
5253 if (!rank_check (matrix, 0, 2))
5254 return false;
5256 return true;
5260 bool
5261 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5263 if (!array_check (array, 0))
5264 return false;
5266 if (!dim_check (dim, 1, false))
5267 return false;
5269 if (!dim_rank_check (dim, array, 0))
5270 return false;
5272 if (!kind_check (kind, 2, BT_INTEGER))
5273 return false;
5274 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5275 "with KIND argument at %L",
5276 gfc_current_intrinsic, &kind->where))
5277 return false;
5279 return true;
5283 bool
5284 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5286 if (flag_coarray == GFC_FCOARRAY_NONE)
5288 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5289 return false;
5292 if (!coarray_check (coarray, 0))
5293 return false;
5295 if (dim != NULL)
5297 if (!dim_check (dim, 1, false))
5298 return false;
5300 if (!dim_corank_check (dim, coarray))
5301 return false;
5304 if (!kind_check (kind, 2, BT_INTEGER))
5305 return false;
5307 return true;
5311 bool
5312 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5314 mpz_t vector_size;
5316 if (!rank_check (vector, 0, 1))
5317 return false;
5319 if (!array_check (mask, 1))
5320 return false;
5322 if (!type_check (mask, 1, BT_LOGICAL))
5323 return false;
5325 if (!same_type_check (vector, 0, field, 2))
5326 return false;
5328 if (mask->expr_type == EXPR_ARRAY
5329 && gfc_array_size (vector, &vector_size))
5331 int mask_true_count = 0;
5332 gfc_constructor *mask_ctor;
5333 mask_ctor = gfc_constructor_first (mask->value.constructor);
5334 while (mask_ctor)
5336 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5338 mask_true_count = 0;
5339 break;
5342 if (mask_ctor->expr->value.logical)
5343 mask_true_count++;
5345 mask_ctor = gfc_constructor_next (mask_ctor);
5348 if (mpz_get_si (vector_size) < mask_true_count)
5350 gfc_error ("%qs argument of %qs intrinsic at %L must "
5351 "provide at least as many elements as there "
5352 "are .TRUE. values in %qs (%ld/%d)",
5353 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5354 &vector->where, gfc_current_intrinsic_arg[1]->name,
5355 mpz_get_si (vector_size), mask_true_count);
5356 return false;
5359 mpz_clear (vector_size);
5362 if (mask->rank != field->rank && field->rank != 0)
5364 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5365 "the same rank as %qs or be a scalar",
5366 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5367 &field->where, gfc_current_intrinsic_arg[1]->name);
5368 return false;
5371 if (mask->rank == field->rank)
5373 int i;
5374 for (i = 0; i < field->rank; i++)
5375 if (! identical_dimen_shape (mask, i, field, i))
5377 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5378 "must have identical shape.",
5379 gfc_current_intrinsic_arg[2]->name,
5380 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5381 &field->where);
5385 return true;
5389 bool
5390 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5392 if (!type_check (x, 0, BT_CHARACTER))
5393 return false;
5395 if (!same_type_check (x, 0, y, 1))
5396 return false;
5398 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5399 return false;
5401 if (!kind_check (kind, 3, BT_INTEGER))
5402 return false;
5403 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5404 "with KIND argument at %L",
5405 gfc_current_intrinsic, &kind->where))
5406 return false;
5408 return true;
5412 bool
5413 gfc_check_trim (gfc_expr *x)
5415 if (!type_check (x, 0, BT_CHARACTER))
5416 return false;
5418 if (!scalar_check (x, 0))
5419 return false;
5421 return true;
5425 bool
5426 gfc_check_ttynam (gfc_expr *unit)
5428 if (!scalar_check (unit, 0))
5429 return false;
5431 if (!type_check (unit, 0, BT_INTEGER))
5432 return false;
5434 return true;
5438 /* Common check function for the half a dozen intrinsics that have a
5439 single real argument. */
5441 bool
5442 gfc_check_x (gfc_expr *x)
5444 if (!type_check (x, 0, BT_REAL))
5445 return false;
5447 return true;
5451 /************* Check functions for intrinsic subroutines *************/
5453 bool
5454 gfc_check_cpu_time (gfc_expr *time)
5456 if (!scalar_check (time, 0))
5457 return false;
5459 if (!type_check (time, 0, BT_REAL))
5460 return false;
5462 if (!variable_check (time, 0, false))
5463 return false;
5465 return true;
5469 bool
5470 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5471 gfc_expr *zone, gfc_expr *values)
5473 if (date != NULL)
5475 if (!type_check (date, 0, BT_CHARACTER))
5476 return false;
5477 if (!kind_value_check (date, 0, gfc_default_character_kind))
5478 return false;
5479 if (!scalar_check (date, 0))
5480 return false;
5481 if (!variable_check (date, 0, false))
5482 return false;
5485 if (time != NULL)
5487 if (!type_check (time, 1, BT_CHARACTER))
5488 return false;
5489 if (!kind_value_check (time, 1, gfc_default_character_kind))
5490 return false;
5491 if (!scalar_check (time, 1))
5492 return false;
5493 if (!variable_check (time, 1, false))
5494 return false;
5497 if (zone != NULL)
5499 if (!type_check (zone, 2, BT_CHARACTER))
5500 return false;
5501 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5502 return false;
5503 if (!scalar_check (zone, 2))
5504 return false;
5505 if (!variable_check (zone, 2, false))
5506 return false;
5509 if (values != NULL)
5511 if (!type_check (values, 3, BT_INTEGER))
5512 return false;
5513 if (!array_check (values, 3))
5514 return false;
5515 if (!rank_check (values, 3, 1))
5516 return false;
5517 if (!variable_check (values, 3, false))
5518 return false;
5521 return true;
5525 bool
5526 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5527 gfc_expr *to, gfc_expr *topos)
5529 if (!type_check (from, 0, BT_INTEGER))
5530 return false;
5532 if (!type_check (frompos, 1, BT_INTEGER))
5533 return false;
5535 if (!type_check (len, 2, BT_INTEGER))
5536 return false;
5538 if (!same_type_check (from, 0, to, 3))
5539 return false;
5541 if (!variable_check (to, 3, false))
5542 return false;
5544 if (!type_check (topos, 4, BT_INTEGER))
5545 return false;
5547 if (!nonnegative_check ("frompos", frompos))
5548 return false;
5550 if (!nonnegative_check ("topos", topos))
5551 return false;
5553 if (!nonnegative_check ("len", len))
5554 return false;
5556 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5557 return false;
5559 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5560 return false;
5562 return true;
5566 bool
5567 gfc_check_random_number (gfc_expr *harvest)
5569 if (!type_check (harvest, 0, BT_REAL))
5570 return false;
5572 if (!variable_check (harvest, 0, false))
5573 return false;
5575 return true;
5579 bool
5580 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5582 unsigned int nargs = 0, seed_size;
5583 locus *where = NULL;
5584 mpz_t put_size, get_size;
5586 /* Keep the number of bytes in sync with master_state in
5587 libgfortran/intrinsics/random.c. +1 due to the integer p which is
5588 part of the state too. */
5589 seed_size = 128 / gfc_default_integer_kind + 1;
5591 if (size != NULL)
5593 if (size->expr_type != EXPR_VARIABLE
5594 || !size->symtree->n.sym->attr.optional)
5595 nargs++;
5597 if (!scalar_check (size, 0))
5598 return false;
5600 if (!type_check (size, 0, BT_INTEGER))
5601 return false;
5603 if (!variable_check (size, 0, false))
5604 return false;
5606 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5607 return false;
5610 if (put != NULL)
5612 if (put->expr_type != EXPR_VARIABLE
5613 || !put->symtree->n.sym->attr.optional)
5615 nargs++;
5616 where = &put->where;
5619 if (!array_check (put, 1))
5620 return false;
5622 if (!rank_check (put, 1, 1))
5623 return false;
5625 if (!type_check (put, 1, BT_INTEGER))
5626 return false;
5628 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5629 return false;
5631 if (gfc_array_size (put, &put_size)
5632 && mpz_get_ui (put_size) < seed_size)
5633 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5634 "too small (%i/%i)",
5635 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5636 where, (int) mpz_get_ui (put_size), seed_size);
5639 if (get != NULL)
5641 if (get->expr_type != EXPR_VARIABLE
5642 || !get->symtree->n.sym->attr.optional)
5644 nargs++;
5645 where = &get->where;
5648 if (!array_check (get, 2))
5649 return false;
5651 if (!rank_check (get, 2, 1))
5652 return false;
5654 if (!type_check (get, 2, BT_INTEGER))
5655 return false;
5657 if (!variable_check (get, 2, false))
5658 return false;
5660 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5661 return false;
5663 if (gfc_array_size (get, &get_size)
5664 && mpz_get_ui (get_size) < seed_size)
5665 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5666 "too small (%i/%i)",
5667 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5668 where, (int) mpz_get_ui (get_size), seed_size);
5671 /* RANDOM_SEED may not have more than one non-optional argument. */
5672 if (nargs > 1)
5673 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5675 return true;
5678 bool
5679 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5681 gfc_expr *e;
5682 int len, i;
5683 int num_percent, nargs;
5685 e = a->expr;
5686 if (e->expr_type != EXPR_CONSTANT)
5687 return true;
5689 len = e->value.character.length;
5690 if (e->value.character.string[len-1] != '\0')
5691 gfc_internal_error ("fe_runtime_error string must be null terminated");
5693 num_percent = 0;
5694 for (i=0; i<len-1; i++)
5695 if (e->value.character.string[i] == '%')
5696 num_percent ++;
5698 nargs = 0;
5699 for (; a; a = a->next)
5700 nargs ++;
5702 if (nargs -1 != num_percent)
5703 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5704 nargs, num_percent++);
5706 return true;
5709 bool
5710 gfc_check_second_sub (gfc_expr *time)
5712 if (!scalar_check (time, 0))
5713 return false;
5715 if (!type_check (time, 0, BT_REAL))
5716 return false;
5718 if (!kind_value_check (time, 0, 4))
5719 return false;
5721 return true;
5725 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5726 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5727 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5728 count_max are all optional arguments */
5730 bool
5731 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5732 gfc_expr *count_max)
5734 if (count != NULL)
5736 if (!scalar_check (count, 0))
5737 return false;
5739 if (!type_check (count, 0, BT_INTEGER))
5740 return false;
5742 if (count->ts.kind != gfc_default_integer_kind
5743 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5744 "SYSTEM_CLOCK at %L has non-default kind",
5745 &count->where))
5746 return false;
5748 if (!variable_check (count, 0, false))
5749 return false;
5752 if (count_rate != NULL)
5754 if (!scalar_check (count_rate, 1))
5755 return false;
5757 if (!variable_check (count_rate, 1, false))
5758 return false;
5760 if (count_rate->ts.type == BT_REAL)
5762 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5763 "SYSTEM_CLOCK at %L", &count_rate->where))
5764 return false;
5766 else
5768 if (!type_check (count_rate, 1, BT_INTEGER))
5769 return false;
5771 if (count_rate->ts.kind != gfc_default_integer_kind
5772 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5773 "SYSTEM_CLOCK at %L has non-default kind",
5774 &count_rate->where))
5775 return false;
5780 if (count_max != NULL)
5782 if (!scalar_check (count_max, 2))
5783 return false;
5785 if (!type_check (count_max, 2, BT_INTEGER))
5786 return false;
5788 if (count_max->ts.kind != gfc_default_integer_kind
5789 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5790 "SYSTEM_CLOCK at %L has non-default kind",
5791 &count_max->where))
5792 return false;
5794 if (!variable_check (count_max, 2, false))
5795 return false;
5798 return true;
5802 bool
5803 gfc_check_irand (gfc_expr *x)
5805 if (x == NULL)
5806 return true;
5808 if (!scalar_check (x, 0))
5809 return false;
5811 if (!type_check (x, 0, BT_INTEGER))
5812 return false;
5814 if (!kind_value_check (x, 0, 4))
5815 return false;
5817 return true;
5821 bool
5822 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5824 if (!scalar_check (seconds, 0))
5825 return false;
5826 if (!type_check (seconds, 0, BT_INTEGER))
5827 return false;
5829 if (!int_or_proc_check (handler, 1))
5830 return false;
5831 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5832 return false;
5834 if (status == NULL)
5835 return true;
5837 if (!scalar_check (status, 2))
5838 return false;
5839 if (!type_check (status, 2, BT_INTEGER))
5840 return false;
5841 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5842 return false;
5844 return true;
5848 bool
5849 gfc_check_rand (gfc_expr *x)
5851 if (x == NULL)
5852 return true;
5854 if (!scalar_check (x, 0))
5855 return false;
5857 if (!type_check (x, 0, BT_INTEGER))
5858 return false;
5860 if (!kind_value_check (x, 0, 4))
5861 return false;
5863 return true;
5867 bool
5868 gfc_check_srand (gfc_expr *x)
5870 if (!scalar_check (x, 0))
5871 return false;
5873 if (!type_check (x, 0, BT_INTEGER))
5874 return false;
5876 if (!kind_value_check (x, 0, 4))
5877 return false;
5879 return true;
5883 bool
5884 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5886 if (!scalar_check (time, 0))
5887 return false;
5888 if (!type_check (time, 0, BT_INTEGER))
5889 return false;
5891 if (!type_check (result, 1, BT_CHARACTER))
5892 return false;
5893 if (!kind_value_check (result, 1, gfc_default_character_kind))
5894 return false;
5896 return true;
5900 bool
5901 gfc_check_dtime_etime (gfc_expr *x)
5903 if (!array_check (x, 0))
5904 return false;
5906 if (!rank_check (x, 0, 1))
5907 return false;
5909 if (!variable_check (x, 0, false))
5910 return false;
5912 if (!type_check (x, 0, BT_REAL))
5913 return false;
5915 if (!kind_value_check (x, 0, 4))
5916 return false;
5918 return true;
5922 bool
5923 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5925 if (!array_check (values, 0))
5926 return false;
5928 if (!rank_check (values, 0, 1))
5929 return false;
5931 if (!variable_check (values, 0, false))
5932 return false;
5934 if (!type_check (values, 0, BT_REAL))
5935 return false;
5937 if (!kind_value_check (values, 0, 4))
5938 return false;
5940 if (!scalar_check (time, 1))
5941 return false;
5943 if (!type_check (time, 1, BT_REAL))
5944 return false;
5946 if (!kind_value_check (time, 1, 4))
5947 return false;
5949 return true;
5953 bool
5954 gfc_check_fdate_sub (gfc_expr *date)
5956 if (!type_check (date, 0, BT_CHARACTER))
5957 return false;
5958 if (!kind_value_check (date, 0, gfc_default_character_kind))
5959 return false;
5961 return true;
5965 bool
5966 gfc_check_gerror (gfc_expr *msg)
5968 if (!type_check (msg, 0, BT_CHARACTER))
5969 return false;
5970 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5971 return false;
5973 return true;
5977 bool
5978 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5980 if (!type_check (cwd, 0, BT_CHARACTER))
5981 return false;
5982 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5983 return false;
5985 if (status == NULL)
5986 return true;
5988 if (!scalar_check (status, 1))
5989 return false;
5991 if (!type_check (status, 1, BT_INTEGER))
5992 return false;
5994 return true;
5998 bool
5999 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
6001 if (!type_check (pos, 0, BT_INTEGER))
6002 return false;
6004 if (pos->ts.kind > gfc_default_integer_kind)
6006 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
6007 "not wider than the default kind (%d)",
6008 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6009 &pos->where, gfc_default_integer_kind);
6010 return false;
6013 if (!type_check (value, 1, BT_CHARACTER))
6014 return false;
6015 if (!kind_value_check (value, 1, gfc_default_character_kind))
6016 return false;
6018 return true;
6022 bool
6023 gfc_check_getlog (gfc_expr *msg)
6025 if (!type_check (msg, 0, BT_CHARACTER))
6026 return false;
6027 if (!kind_value_check (msg, 0, gfc_default_character_kind))
6028 return false;
6030 return true;
6034 bool
6035 gfc_check_exit (gfc_expr *status)
6037 if (status == NULL)
6038 return true;
6040 if (!type_check (status, 0, BT_INTEGER))
6041 return false;
6043 if (!scalar_check (status, 0))
6044 return false;
6046 return true;
6050 bool
6051 gfc_check_flush (gfc_expr *unit)
6053 if (unit == NULL)
6054 return true;
6056 if (!type_check (unit, 0, BT_INTEGER))
6057 return false;
6059 if (!scalar_check (unit, 0))
6060 return false;
6062 return true;
6066 bool
6067 gfc_check_free (gfc_expr *i)
6069 if (!type_check (i, 0, BT_INTEGER))
6070 return false;
6072 if (!scalar_check (i, 0))
6073 return false;
6075 return true;
6079 bool
6080 gfc_check_hostnm (gfc_expr *name)
6082 if (!type_check (name, 0, BT_CHARACTER))
6083 return false;
6084 if (!kind_value_check (name, 0, gfc_default_character_kind))
6085 return false;
6087 return true;
6091 bool
6092 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
6094 if (!type_check (name, 0, BT_CHARACTER))
6095 return false;
6096 if (!kind_value_check (name, 0, gfc_default_character_kind))
6097 return false;
6099 if (status == NULL)
6100 return true;
6102 if (!scalar_check (status, 1))
6103 return false;
6105 if (!type_check (status, 1, BT_INTEGER))
6106 return false;
6108 return true;
6112 bool
6113 gfc_check_itime_idate (gfc_expr *values)
6115 if (!array_check (values, 0))
6116 return false;
6118 if (!rank_check (values, 0, 1))
6119 return false;
6121 if (!variable_check (values, 0, false))
6122 return false;
6124 if (!type_check (values, 0, BT_INTEGER))
6125 return false;
6127 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6128 return false;
6130 return true;
6134 bool
6135 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6137 if (!type_check (time, 0, BT_INTEGER))
6138 return false;
6140 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6141 return false;
6143 if (!scalar_check (time, 0))
6144 return false;
6146 if (!array_check (values, 1))
6147 return false;
6149 if (!rank_check (values, 1, 1))
6150 return false;
6152 if (!variable_check (values, 1, false))
6153 return false;
6155 if (!type_check (values, 1, BT_INTEGER))
6156 return false;
6158 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6159 return false;
6161 return true;
6165 bool
6166 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6168 if (!scalar_check (unit, 0))
6169 return false;
6171 if (!type_check (unit, 0, BT_INTEGER))
6172 return false;
6174 if (!type_check (name, 1, BT_CHARACTER))
6175 return false;
6176 if (!kind_value_check (name, 1, gfc_default_character_kind))
6177 return false;
6179 return true;
6183 bool
6184 gfc_check_isatty (gfc_expr *unit)
6186 if (unit == NULL)
6187 return false;
6189 if (!type_check (unit, 0, BT_INTEGER))
6190 return false;
6192 if (!scalar_check (unit, 0))
6193 return false;
6195 return true;
6199 bool
6200 gfc_check_isnan (gfc_expr *x)
6202 if (!type_check (x, 0, BT_REAL))
6203 return false;
6205 return true;
6209 bool
6210 gfc_check_perror (gfc_expr *string)
6212 if (!type_check (string, 0, BT_CHARACTER))
6213 return false;
6214 if (!kind_value_check (string, 0, gfc_default_character_kind))
6215 return false;
6217 return true;
6221 bool
6222 gfc_check_umask (gfc_expr *mask)
6224 if (!type_check (mask, 0, BT_INTEGER))
6225 return false;
6227 if (!scalar_check (mask, 0))
6228 return false;
6230 return true;
6234 bool
6235 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6237 if (!type_check (mask, 0, BT_INTEGER))
6238 return false;
6240 if (!scalar_check (mask, 0))
6241 return false;
6243 if (old == NULL)
6244 return true;
6246 if (!scalar_check (old, 1))
6247 return false;
6249 if (!type_check (old, 1, BT_INTEGER))
6250 return false;
6252 return true;
6256 bool
6257 gfc_check_unlink (gfc_expr *name)
6259 if (!type_check (name, 0, BT_CHARACTER))
6260 return false;
6261 if (!kind_value_check (name, 0, gfc_default_character_kind))
6262 return false;
6264 return true;
6268 bool
6269 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6271 if (!type_check (name, 0, BT_CHARACTER))
6272 return false;
6273 if (!kind_value_check (name, 0, gfc_default_character_kind))
6274 return false;
6276 if (status == NULL)
6277 return true;
6279 if (!scalar_check (status, 1))
6280 return false;
6282 if (!type_check (status, 1, BT_INTEGER))
6283 return false;
6285 return true;
6289 bool
6290 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6292 if (!scalar_check (number, 0))
6293 return false;
6294 if (!type_check (number, 0, BT_INTEGER))
6295 return false;
6297 if (!int_or_proc_check (handler, 1))
6298 return false;
6299 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6300 return false;
6302 return true;
6306 bool
6307 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6309 if (!scalar_check (number, 0))
6310 return false;
6311 if (!type_check (number, 0, BT_INTEGER))
6312 return false;
6314 if (!int_or_proc_check (handler, 1))
6315 return false;
6316 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6317 return false;
6319 if (status == NULL)
6320 return true;
6322 if (!type_check (status, 2, BT_INTEGER))
6323 return false;
6324 if (!scalar_check (status, 2))
6325 return false;
6327 return true;
6331 bool
6332 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6334 if (!type_check (cmd, 0, BT_CHARACTER))
6335 return false;
6336 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6337 return false;
6339 if (!scalar_check (status, 1))
6340 return false;
6342 if (!type_check (status, 1, BT_INTEGER))
6343 return false;
6345 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6346 return false;
6348 return true;
6352 /* This is used for the GNU intrinsics AND, OR and XOR. */
6353 bool
6354 gfc_check_and (gfc_expr *i, gfc_expr *j)
6356 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6358 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6359 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6360 gfc_current_intrinsic, &i->where);
6361 return false;
6364 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6366 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6367 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6368 gfc_current_intrinsic, &j->where);
6369 return false;
6372 if (i->ts.type != j->ts.type)
6374 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6375 "have the same type", gfc_current_intrinsic_arg[0]->name,
6376 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6377 &j->where);
6378 return false;
6381 if (!scalar_check (i, 0))
6382 return false;
6384 if (!scalar_check (j, 1))
6385 return false;
6387 return true;
6391 bool
6392 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6395 if (a->expr_type == EXPR_NULL)
6397 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6398 "argument to STORAGE_SIZE, because it returns a "
6399 "disassociated pointer", &a->where);
6400 return false;
6403 if (a->ts.type == BT_ASSUMED)
6405 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6406 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6407 &a->where);
6408 return false;
6411 if (a->ts.type == BT_PROCEDURE)
6413 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6414 "procedure", gfc_current_intrinsic_arg[0]->name,
6415 gfc_current_intrinsic, &a->where);
6416 return false;
6419 if (kind == NULL)
6420 return true;
6422 if (!type_check (kind, 1, BT_INTEGER))
6423 return false;
6425 if (!scalar_check (kind, 1))
6426 return false;
6428 if (kind->expr_type != EXPR_CONSTANT)
6430 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6431 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6432 &kind->where);
6433 return false;
6436 return true;