]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/check.c
re PR fortran/68153 (ICE for intrinsic reshape with negative dim in effective shape)
[thirdparty/gcc.git] / gcc / fortran / check.c
1 /* Check functions
2 Copyright (C) 2002-2015 Free Software Foundation, Inc.
3 Contributed by Andy Vaught & Katherine Holcomb
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21
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. */
27
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"
36
37
38 /* Make sure an expression is a scalar. */
39
40 static bool
41 scalar_check (gfc_expr *e, int n)
42 {
43 if (e->rank == 0)
44 return true;
45
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);
49
50 return false;
51 }
52
53
54 /* Check the type of an expression. */
55
56 static bool
57 type_check (gfc_expr *e, int n, bt type)
58 {
59 if (e->ts.type == type)
60 return true;
61
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));
65
66 return false;
67 }
68
69
70 /* Check that the expression is a numeric type. */
71
72 static bool
73 numeric_check (gfc_expr *e, int n)
74 {
75 if (gfc_numeric_ts (&e->ts))
76 return true;
77
78 /* If the expression has not got a type, check if its namespace can
79 offer a default type. */
80 if ((e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
81 && e->symtree->n.sym->ts.type == BT_UNKNOWN
82 && gfc_set_default_type (e->symtree->n.sym, 0, e->symtree->n.sym->ns)
83 && gfc_numeric_ts (&e->symtree->n.sym->ts))
84 {
85 e->ts = e->symtree->n.sym->ts;
86 return true;
87 }
88
89 gfc_error ("%qs argument of %qs intrinsic at %L must be a numeric type",
90 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
91 &e->where);
92
93 return false;
94 }
95
96
97 /* Check that an expression is integer or real. */
98
99 static bool
100 int_or_real_check (gfc_expr *e, int n)
101 {
102 if (e->ts.type != BT_INTEGER && e->ts.type != BT_REAL)
103 {
104 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
105 "or REAL", gfc_current_intrinsic_arg[n]->name,
106 gfc_current_intrinsic, &e->where);
107 return false;
108 }
109
110 return true;
111 }
112
113
114 /* Check that an expression is real or complex. */
115
116 static bool
117 real_or_complex_check (gfc_expr *e, int n)
118 {
119 if (e->ts.type != BT_REAL && e->ts.type != BT_COMPLEX)
120 {
121 gfc_error ("%qs argument of %qs intrinsic at %L must be REAL "
122 "or COMPLEX", gfc_current_intrinsic_arg[n]->name,
123 gfc_current_intrinsic, &e->where);
124 return false;
125 }
126
127 return true;
128 }
129
130
131 /* Check that an expression is INTEGER or PROCEDURE. */
132
133 static bool
134 int_or_proc_check (gfc_expr *e, int n)
135 {
136 if (e->ts.type != BT_INTEGER && e->ts.type != BT_PROCEDURE)
137 {
138 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
139 "or PROCEDURE", gfc_current_intrinsic_arg[n]->name,
140 gfc_current_intrinsic, &e->where);
141 return false;
142 }
143
144 return true;
145 }
146
147
148 /* Check that the expression is an optional constant integer
149 and that it specifies a valid kind for that type. */
150
151 static bool
152 kind_check (gfc_expr *k, int n, bt type)
153 {
154 int kind;
155
156 if (k == NULL)
157 return true;
158
159 if (!type_check (k, n, BT_INTEGER))
160 return false;
161
162 if (!scalar_check (k, n))
163 return false;
164
165 if (!gfc_check_init_expr (k))
166 {
167 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
168 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
169 &k->where);
170 return false;
171 }
172
173 if (gfc_extract_int (k, &kind) != NULL
174 || gfc_validate_kind (type, kind, true) < 0)
175 {
176 gfc_error ("Invalid kind for %s at %L", gfc_basic_typename (type),
177 &k->where);
178 return false;
179 }
180
181 return true;
182 }
183
184
185 /* Make sure the expression is a double precision real. */
186
187 static bool
188 double_check (gfc_expr *d, int n)
189 {
190 if (!type_check (d, n, BT_REAL))
191 return false;
192
193 if (d->ts.kind != gfc_default_double_kind)
194 {
195 gfc_error ("%qs argument of %qs intrinsic at %L must be double "
196 "precision", gfc_current_intrinsic_arg[n]->name,
197 gfc_current_intrinsic, &d->where);
198 return false;
199 }
200
201 return true;
202 }
203
204
205 static bool
206 coarray_check (gfc_expr *e, int n)
207 {
208 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
209 && CLASS_DATA (e)->attr.codimension
210 && CLASS_DATA (e)->as->corank)
211 {
212 gfc_add_class_array_ref (e);
213 return true;
214 }
215
216 if (!gfc_is_coarray (e))
217 {
218 gfc_error ("Expected coarray variable as %qs argument to the %s "
219 "intrinsic at %L", gfc_current_intrinsic_arg[n]->name,
220 gfc_current_intrinsic, &e->where);
221 return false;
222 }
223
224 return true;
225 }
226
227
228 /* Make sure the expression is a logical array. */
229
230 static bool
231 logical_array_check (gfc_expr *array, int n)
232 {
233 if (array->ts.type != BT_LOGICAL || array->rank == 0)
234 {
235 gfc_error ("%qs argument of %qs intrinsic at %L must be a logical "
236 "array", gfc_current_intrinsic_arg[n]->name,
237 gfc_current_intrinsic, &array->where);
238 return false;
239 }
240
241 return true;
242 }
243
244
245 /* Make sure an expression is an array. */
246
247 static bool
248 array_check (gfc_expr *e, int n)
249 {
250 if (e->ts.type == BT_CLASS && gfc_expr_attr (e).class_ok
251 && CLASS_DATA (e)->attr.dimension
252 && CLASS_DATA (e)->as->rank)
253 {
254 gfc_add_class_array_ref (e);
255 return true;
256 }
257
258 if (e->rank != 0 && e->ts.type != BT_PROCEDURE)
259 return true;
260
261 gfc_error ("%qs argument of %qs intrinsic at %L must be an array",
262 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
263 &e->where);
264
265 return false;
266 }
267
268
269 /* If expr is a constant, then check to ensure that it is greater than
270 of equal to zero. */
271
272 static bool
273 nonnegative_check (const char *arg, gfc_expr *expr)
274 {
275 int i;
276
277 if (expr->expr_type == EXPR_CONSTANT)
278 {
279 gfc_extract_int (expr, &i);
280 if (i < 0)
281 {
282 gfc_error ("%qs at %L must be nonnegative", arg, &expr->where);
283 return false;
284 }
285 }
286
287 return true;
288 }
289
290
291 /* If expr2 is constant, then check that the value is less than
292 (less than or equal to, if 'or_equal' is true) bit_size(expr1). */
293
294 static bool
295 less_than_bitsize1 (const char *arg1, gfc_expr *expr1, const char *arg2,
296 gfc_expr *expr2, bool or_equal)
297 {
298 int i2, i3;
299
300 if (expr2->expr_type == EXPR_CONSTANT)
301 {
302 gfc_extract_int (expr2, &i2);
303 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
304
305 /* For ISHFT[C], check that |shift| <= bit_size(i). */
306 if (arg2 == NULL)
307 {
308 if (i2 < 0)
309 i2 = -i2;
310
311 if (i2 > gfc_integer_kinds[i3].bit_size)
312 {
313 gfc_error ("The absolute value of SHIFT at %L must be less "
314 "than or equal to BIT_SIZE(%qs)",
315 &expr2->where, arg1);
316 return false;
317 }
318 }
319
320 if (or_equal)
321 {
322 if (i2 > gfc_integer_kinds[i3].bit_size)
323 {
324 gfc_error ("%qs at %L must be less than "
325 "or equal to BIT_SIZE(%qs)",
326 arg2, &expr2->where, arg1);
327 return false;
328 }
329 }
330 else
331 {
332 if (i2 >= gfc_integer_kinds[i3].bit_size)
333 {
334 gfc_error ("%qs at %L must be less than BIT_SIZE(%qs)",
335 arg2, &expr2->where, arg1);
336 return false;
337 }
338 }
339 }
340
341 return true;
342 }
343
344
345 /* If expr is constant, then check that the value is less than or equal
346 to the bit_size of the kind k. */
347
348 static bool
349 less_than_bitsizekind (const char *arg, gfc_expr *expr, int k)
350 {
351 int i, val;
352
353 if (expr->expr_type != EXPR_CONSTANT)
354 return true;
355
356 i = gfc_validate_kind (BT_INTEGER, k, false);
357 gfc_extract_int (expr, &val);
358
359 if (val > gfc_integer_kinds[i].bit_size)
360 {
361 gfc_error ("%qs at %L must be less than or equal to the BIT_SIZE of "
362 "INTEGER(KIND=%d)", arg, &expr->where, k);
363 return false;
364 }
365
366 return true;
367 }
368
369
370 /* If expr2 and expr3 are constants, then check that the value is less than
371 or equal to bit_size(expr1). */
372
373 static bool
374 less_than_bitsize2 (const char *arg1, gfc_expr *expr1, const char *arg2,
375 gfc_expr *expr2, const char *arg3, gfc_expr *expr3)
376 {
377 int i2, i3;
378
379 if (expr2->expr_type == EXPR_CONSTANT && expr3->expr_type == EXPR_CONSTANT)
380 {
381 gfc_extract_int (expr2, &i2);
382 gfc_extract_int (expr3, &i3);
383 i2 += i3;
384 i3 = gfc_validate_kind (BT_INTEGER, expr1->ts.kind, false);
385 if (i2 > gfc_integer_kinds[i3].bit_size)
386 {
387 gfc_error ("%<%s + %s%> at %L must be less than or equal "
388 "to BIT_SIZE(%qs)",
389 arg2, arg3, &expr2->where, arg1);
390 return false;
391 }
392 }
393
394 return true;
395 }
396
397 /* Make sure two expressions have the same type. */
398
399 static bool
400 same_type_check (gfc_expr *e, int n, gfc_expr *f, int m)
401 {
402 if (gfc_compare_types (&e->ts, &f->ts))
403 return true;
404
405 gfc_error ("%qs argument of %qs intrinsic at %L must be the same type "
406 "and kind as %qs", gfc_current_intrinsic_arg[m]->name,
407 gfc_current_intrinsic, &f->where,
408 gfc_current_intrinsic_arg[n]->name);
409
410 return false;
411 }
412
413
414 /* Make sure that an expression has a certain (nonzero) rank. */
415
416 static bool
417 rank_check (gfc_expr *e, int n, int rank)
418 {
419 if (e->rank == rank)
420 return true;
421
422 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank %d",
423 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
424 &e->where, rank);
425
426 return false;
427 }
428
429
430 /* Make sure a variable expression is not an optional dummy argument. */
431
432 static bool
433 nonoptional_check (gfc_expr *e, int n)
434 {
435 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.optional)
436 {
437 gfc_error ("%qs argument of %qs intrinsic at %L must not be OPTIONAL",
438 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
439 &e->where);
440 }
441
442 /* TODO: Recursive check on nonoptional variables? */
443
444 return true;
445 }
446
447
448 /* Check for ALLOCATABLE attribute. */
449
450 static bool
451 allocatable_check (gfc_expr *e, int n)
452 {
453 symbol_attribute attr;
454
455 attr = gfc_variable_attr (e, NULL);
456 if (!attr.allocatable || attr.associate_var)
457 {
458 gfc_error ("%qs argument of %qs intrinsic at %L must be ALLOCATABLE",
459 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
460 &e->where);
461 return false;
462 }
463
464 return true;
465 }
466
467
468 /* Check that an expression has a particular kind. */
469
470 static bool
471 kind_value_check (gfc_expr *e, int n, int k)
472 {
473 if (e->ts.kind == k)
474 return true;
475
476 gfc_error ("%qs argument of %qs intrinsic at %L must be of kind %d",
477 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic,
478 &e->where, k);
479
480 return false;
481 }
482
483
484 /* Make sure an expression is a variable. */
485
486 static bool
487 variable_check (gfc_expr *e, int n, bool allow_proc)
488 {
489 if (e->expr_type == EXPR_VARIABLE
490 && e->symtree->n.sym->attr.intent == INTENT_IN
491 && (gfc_current_intrinsic_arg[n]->intent == INTENT_OUT
492 || gfc_current_intrinsic_arg[n]->intent == INTENT_INOUT))
493 {
494 gfc_ref *ref;
495 bool pointer = e->symtree->n.sym->ts.type == BT_CLASS
496 && CLASS_DATA (e->symtree->n.sym)
497 ? CLASS_DATA (e->symtree->n.sym)->attr.class_pointer
498 : e->symtree->n.sym->attr.pointer;
499
500 for (ref = e->ref; ref; ref = ref->next)
501 {
502 if (pointer && ref->type == REF_COMPONENT)
503 break;
504 if (ref->type == REF_COMPONENT
505 && ((ref->u.c.component->ts.type == BT_CLASS
506 && CLASS_DATA (ref->u.c.component)->attr.class_pointer)
507 || (ref->u.c.component->ts.type != BT_CLASS
508 && ref->u.c.component->attr.pointer)))
509 break;
510 }
511
512 if (!ref)
513 {
514 gfc_error ("%qs argument of %qs intrinsic at %L cannot be "
515 "INTENT(IN)", gfc_current_intrinsic_arg[n]->name,
516 gfc_current_intrinsic, &e->where);
517 return false;
518 }
519 }
520
521 if (e->expr_type == EXPR_VARIABLE
522 && e->symtree->n.sym->attr.flavor != FL_PARAMETER
523 && (allow_proc || !e->symtree->n.sym->attr.function))
524 return true;
525
526 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.function
527 && e->symtree->n.sym == e->symtree->n.sym->result)
528 {
529 gfc_namespace *ns;
530 for (ns = gfc_current_ns; ns; ns = ns->parent)
531 if (ns->proc_name == e->symtree->n.sym)
532 return true;
533 }
534
535 gfc_error ("%qs argument of %qs intrinsic at %L must be a variable",
536 gfc_current_intrinsic_arg[n]->name, gfc_current_intrinsic, &e->where);
537
538 return false;
539 }
540
541
542 /* Check the common DIM parameter for correctness. */
543
544 static bool
545 dim_check (gfc_expr *dim, int n, bool optional)
546 {
547 if (dim == NULL)
548 return true;
549
550 if (!type_check (dim, n, BT_INTEGER))
551 return false;
552
553 if (!scalar_check (dim, n))
554 return false;
555
556 if (!optional && !nonoptional_check (dim, n))
557 return false;
558
559 return true;
560 }
561
562
563 /* If a coarray DIM parameter is a constant, make sure that it is greater than
564 zero and less than or equal to the corank of the given array. */
565
566 static bool
567 dim_corank_check (gfc_expr *dim, gfc_expr *array)
568 {
569 int corank;
570
571 gcc_assert (array->expr_type == EXPR_VARIABLE);
572
573 if (dim->expr_type != EXPR_CONSTANT)
574 return true;
575
576 if (array->ts.type == BT_CLASS)
577 return true;
578
579 corank = gfc_get_corank (array);
580
581 if (mpz_cmp_ui (dim->value.integer, 1) < 0
582 || mpz_cmp_ui (dim->value.integer, corank) > 0)
583 {
584 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
585 "codimension index", gfc_current_intrinsic, &dim->where);
586
587 return false;
588 }
589
590 return true;
591 }
592
593
594 /* If a DIM parameter is a constant, make sure that it is greater than
595 zero and less than or equal to the rank of the given array. If
596 allow_assumed is zero then dim must be less than the rank of the array
597 for assumed size arrays. */
598
599 static bool
600 dim_rank_check (gfc_expr *dim, gfc_expr *array, int allow_assumed)
601 {
602 gfc_array_ref *ar;
603 int rank;
604
605 if (dim == NULL)
606 return true;
607
608 if (dim->expr_type != EXPR_CONSTANT)
609 return true;
610
611 if (array->expr_type == EXPR_FUNCTION && array->value.function.isym
612 && array->value.function.isym->id == GFC_ISYM_SPREAD)
613 rank = array->rank + 1;
614 else
615 rank = array->rank;
616
617 /* Assumed-rank array. */
618 if (rank == -1)
619 rank = GFC_MAX_DIMENSIONS;
620
621 if (array->expr_type == EXPR_VARIABLE)
622 {
623 ar = gfc_find_array_ref (array);
624 if (ar->as->type == AS_ASSUMED_SIZE
625 && !allow_assumed
626 && ar->type != AR_ELEMENT
627 && ar->type != AR_SECTION)
628 rank--;
629 }
630
631 if (mpz_cmp_ui (dim->value.integer, 1) < 0
632 || mpz_cmp_ui (dim->value.integer, rank) > 0)
633 {
634 gfc_error ("%<dim%> argument of %qs intrinsic at %L is not a valid "
635 "dimension index", gfc_current_intrinsic, &dim->where);
636
637 return false;
638 }
639
640 return true;
641 }
642
643
644 /* Compare the size of a along dimension ai with the size of b along
645 dimension bi, returning 0 if they are known not to be identical,
646 and 1 if they are identical, or if this cannot be determined. */
647
648 static int
649 identical_dimen_shape (gfc_expr *a, int ai, gfc_expr *b, int bi)
650 {
651 mpz_t a_size, b_size;
652 int ret;
653
654 gcc_assert (a->rank > ai);
655 gcc_assert (b->rank > bi);
656
657 ret = 1;
658
659 if (gfc_array_dimen_size (a, ai, &a_size))
660 {
661 if (gfc_array_dimen_size (b, bi, &b_size))
662 {
663 if (mpz_cmp (a_size, b_size) != 0)
664 ret = 0;
665
666 mpz_clear (b_size);
667 }
668 mpz_clear (a_size);
669 }
670 return ret;
671 }
672
673 /* Calculate the length of a character variable, including substrings.
674 Strip away parentheses if necessary. Return -1 if no length could
675 be determined. */
676
677 static long
678 gfc_var_strlen (const gfc_expr *a)
679 {
680 gfc_ref *ra;
681
682 while (a->expr_type == EXPR_OP && a->value.op.op == INTRINSIC_PARENTHESES)
683 a = a->value.op.op1;
684
685 for (ra = a->ref; ra != NULL && ra->type != REF_SUBSTRING; ra = ra->next)
686 ;
687
688 if (ra)
689 {
690 long start_a, end_a;
691
692 if (!ra->u.ss.end)
693 return -1;
694
695 if ((!ra->u.ss.start || ra->u.ss.start->expr_type == EXPR_CONSTANT)
696 && ra->u.ss.end->expr_type == EXPR_CONSTANT)
697 {
698 start_a = ra->u.ss.start ? mpz_get_si (ra->u.ss.start->value.integer)
699 : 1;
700 end_a = mpz_get_si (ra->u.ss.end->value.integer);
701 return (end_a < start_a) ? 0 : end_a - start_a + 1;
702 }
703 else if (ra->u.ss.start
704 && gfc_dep_compare_expr (ra->u.ss.start, ra->u.ss.end) == 0)
705 return 1;
706 else
707 return -1;
708 }
709
710 if (a->ts.u.cl && a->ts.u.cl->length
711 && a->ts.u.cl->length->expr_type == EXPR_CONSTANT)
712 return mpz_get_si (a->ts.u.cl->length->value.integer);
713 else if (a->expr_type == EXPR_CONSTANT
714 && (a->ts.u.cl == NULL || a->ts.u.cl->length == NULL))
715 return a->value.character.length;
716 else
717 return -1;
718
719 }
720
721 /* Check whether two character expressions have the same length;
722 returns true if they have or if the length cannot be determined,
723 otherwise return false and raise a gfc_error. */
724
725 bool
726 gfc_check_same_strlen (const gfc_expr *a, const gfc_expr *b, const char *name)
727 {
728 long len_a, len_b;
729
730 len_a = gfc_var_strlen(a);
731 len_b = gfc_var_strlen(b);
732
733 if (len_a == -1 || len_b == -1 || len_a == len_b)
734 return true;
735 else
736 {
737 gfc_error ("Unequal character lengths (%ld/%ld) in %s at %L",
738 len_a, len_b, name, &a->where);
739 return false;
740 }
741 }
742
743
744 /***** Check functions *****/
745
746 /* Check subroutine suitable for intrinsics taking a real argument and
747 a kind argument for the result. */
748
749 static bool
750 check_a_kind (gfc_expr *a, gfc_expr *kind, bt type)
751 {
752 if (!type_check (a, 0, BT_REAL))
753 return false;
754 if (!kind_check (kind, 1, type))
755 return false;
756
757 return true;
758 }
759
760
761 /* Check subroutine suitable for ceiling, floor and nint. */
762
763 bool
764 gfc_check_a_ikind (gfc_expr *a, gfc_expr *kind)
765 {
766 return check_a_kind (a, kind, BT_INTEGER);
767 }
768
769
770 /* Check subroutine suitable for aint, anint. */
771
772 bool
773 gfc_check_a_xkind (gfc_expr *a, gfc_expr *kind)
774 {
775 return check_a_kind (a, kind, BT_REAL);
776 }
777
778
779 bool
780 gfc_check_abs (gfc_expr *a)
781 {
782 if (!numeric_check (a, 0))
783 return false;
784
785 return true;
786 }
787
788
789 bool
790 gfc_check_achar (gfc_expr *a, gfc_expr *kind)
791 {
792 if (!type_check (a, 0, BT_INTEGER))
793 return false;
794 if (!kind_check (kind, 1, BT_CHARACTER))
795 return false;
796
797 return true;
798 }
799
800
801 bool
802 gfc_check_access_func (gfc_expr *name, gfc_expr *mode)
803 {
804 if (!type_check (name, 0, BT_CHARACTER)
805 || !scalar_check (name, 0))
806 return false;
807 if (!kind_value_check (name, 0, gfc_default_character_kind))
808 return false;
809
810 if (!type_check (mode, 1, BT_CHARACTER)
811 || !scalar_check (mode, 1))
812 return false;
813 if (!kind_value_check (mode, 1, gfc_default_character_kind))
814 return false;
815
816 return true;
817 }
818
819
820 bool
821 gfc_check_all_any (gfc_expr *mask, gfc_expr *dim)
822 {
823 if (!logical_array_check (mask, 0))
824 return false;
825
826 if (!dim_check (dim, 1, false))
827 return false;
828
829 if (!dim_rank_check (dim, mask, 0))
830 return false;
831
832 return true;
833 }
834
835
836 bool
837 gfc_check_allocated (gfc_expr *array)
838 {
839 if (!variable_check (array, 0, false))
840 return false;
841 if (!allocatable_check (array, 0))
842 return false;
843
844 return true;
845 }
846
847
848 /* Common check function where the first argument must be real or
849 integer and the second argument must be the same as the first. */
850
851 bool
852 gfc_check_a_p (gfc_expr *a, gfc_expr *p)
853 {
854 if (!int_or_real_check (a, 0))
855 return false;
856
857 if (a->ts.type != p->ts.type)
858 {
859 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
860 "have the same type", gfc_current_intrinsic_arg[0]->name,
861 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
862 &p->where);
863 return false;
864 }
865
866 if (a->ts.kind != p->ts.kind)
867 {
868 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
869 &p->where))
870 return false;
871 }
872
873 return true;
874 }
875
876
877 bool
878 gfc_check_x_yd (gfc_expr *x, gfc_expr *y)
879 {
880 if (!double_check (x, 0) || !double_check (y, 1))
881 return false;
882
883 return true;
884 }
885
886
887 bool
888 gfc_check_associated (gfc_expr *pointer, gfc_expr *target)
889 {
890 symbol_attribute attr1, attr2;
891 int i;
892 bool t;
893 locus *where;
894
895 where = &pointer->where;
896
897 if (pointer->expr_type == EXPR_NULL)
898 goto null_arg;
899
900 attr1 = gfc_expr_attr (pointer);
901
902 if (!attr1.pointer && !attr1.proc_pointer)
903 {
904 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER",
905 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
906 &pointer->where);
907 return false;
908 }
909
910 /* F2008, C1242. */
911 if (attr1.pointer && gfc_is_coindexed (pointer))
912 {
913 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
914 "coindexed", gfc_current_intrinsic_arg[0]->name,
915 gfc_current_intrinsic, &pointer->where);
916 return false;
917 }
918
919 /* Target argument is optional. */
920 if (target == NULL)
921 return true;
922
923 where = &target->where;
924 if (target->expr_type == EXPR_NULL)
925 goto null_arg;
926
927 if (target->expr_type == EXPR_VARIABLE || target->expr_type == EXPR_FUNCTION)
928 attr2 = gfc_expr_attr (target);
929 else
930 {
931 gfc_error ("%qs argument of %qs intrinsic at %L must be a pointer "
932 "or target VARIABLE or FUNCTION",
933 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
934 &target->where);
935 return false;
936 }
937
938 if (attr1.pointer && !attr2.pointer && !attr2.target)
939 {
940 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER "
941 "or a TARGET", gfc_current_intrinsic_arg[1]->name,
942 gfc_current_intrinsic, &target->where);
943 return false;
944 }
945
946 /* F2008, C1242. */
947 if (attr1.pointer && gfc_is_coindexed (target))
948 {
949 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
950 "coindexed", gfc_current_intrinsic_arg[1]->name,
951 gfc_current_intrinsic, &target->where);
952 return false;
953 }
954
955 t = true;
956 if (!same_type_check (pointer, 0, target, 1))
957 t = false;
958 if (!rank_check (target, 0, pointer->rank))
959 t = false;
960 if (target->rank > 0)
961 {
962 for (i = 0; i < target->rank; i++)
963 if (target->ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
964 {
965 gfc_error ("Array section with a vector subscript at %L shall not "
966 "be the target of a pointer",
967 &target->where);
968 t = false;
969 break;
970 }
971 }
972 return t;
973
974 null_arg:
975
976 gfc_error ("NULL pointer at %L is not permitted as actual argument "
977 "of %qs intrinsic function", where, gfc_current_intrinsic);
978 return false;
979
980 }
981
982
983 bool
984 gfc_check_atan_2 (gfc_expr *y, gfc_expr *x)
985 {
986 /* gfc_notify_std would be a waste of time as the return value
987 is seemingly used only for the generic resolution. The error
988 will be: Too many arguments. */
989 if ((gfc_option.allow_std & GFC_STD_F2008) == 0)
990 return false;
991
992 return gfc_check_atan2 (y, x);
993 }
994
995
996 bool
997 gfc_check_atan2 (gfc_expr *y, gfc_expr *x)
998 {
999 if (!type_check (y, 0, BT_REAL))
1000 return false;
1001 if (!same_type_check (y, 0, x, 1))
1002 return false;
1003
1004 return true;
1005 }
1006
1007
1008 static bool
1009 gfc_check_atomic (gfc_expr *atom, int atom_no, gfc_expr *value, int val_no,
1010 gfc_expr *stat, int stat_no)
1011 {
1012 if (!scalar_check (atom, atom_no) || !scalar_check (value, val_no))
1013 return false;
1014
1015 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1016 && !(atom->ts.type == BT_LOGICAL
1017 && atom->ts.kind == gfc_atomic_logical_kind))
1018 {
1019 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1020 "integer of ATOMIC_INT_KIND or a logical of "
1021 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1022 return false;
1023 }
1024
1025 if (!gfc_is_coarray (atom) && !gfc_is_coindexed (atom))
1026 {
1027 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1028 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1029 return false;
1030 }
1031
1032 if (atom->ts.type != value->ts.type)
1033 {
1034 gfc_error ("%qs argument of %qs intrinsic at %L shall have the same "
1035 "type as %qs at %L", gfc_current_intrinsic_arg[val_no]->name,
1036 gfc_current_intrinsic, &value->where,
1037 gfc_current_intrinsic_arg[atom_no]->name, &atom->where);
1038 return false;
1039 }
1040
1041 if (stat != NULL)
1042 {
1043 if (!type_check (stat, stat_no, BT_INTEGER))
1044 return false;
1045 if (!scalar_check (stat, stat_no))
1046 return false;
1047 if (!variable_check (stat, stat_no, false))
1048 return false;
1049 if (!kind_value_check (stat, stat_no, gfc_default_integer_kind))
1050 return false;
1051
1052 if (!gfc_notify_std (GFC_STD_F2008_TS, "STAT= argument to %s at %L",
1053 gfc_current_intrinsic, &stat->where))
1054 return false;
1055 }
1056
1057 return true;
1058 }
1059
1060
1061 bool
1062 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1063 {
1064 if (atom->expr_type == EXPR_FUNCTION
1065 && atom->value.function.isym
1066 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1067 atom = atom->value.function.actual->expr;
1068
1069 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1070 {
1071 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1072 "definable", gfc_current_intrinsic, &atom->where);
1073 return false;
1074 }
1075
1076 return gfc_check_atomic (atom, 0, value, 1, stat, 2);
1077 }
1078
1079
1080 bool
1081 gfc_check_atomic_op (gfc_expr *atom, gfc_expr *value, gfc_expr *stat)
1082 {
1083 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1084 {
1085 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1086 "integer of ATOMIC_INT_KIND", &atom->where,
1087 gfc_current_intrinsic);
1088 return false;
1089 }
1090
1091 return gfc_check_atomic_def (atom, value, stat);
1092 }
1093
1094
1095 bool
1096 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom, gfc_expr *stat)
1097 {
1098 if (atom->expr_type == EXPR_FUNCTION
1099 && atom->value.function.isym
1100 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1101 atom = atom->value.function.actual->expr;
1102
1103 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1104 {
1105 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1106 "definable", gfc_current_intrinsic, &value->where);
1107 return false;
1108 }
1109
1110 return gfc_check_atomic (atom, 1, value, 0, stat, 2);
1111 }
1112
1113
1114 bool
1115 gfc_check_atomic_cas (gfc_expr *atom, gfc_expr *old, gfc_expr *compare,
1116 gfc_expr *new_val, gfc_expr *stat)
1117 {
1118 if (atom->expr_type == EXPR_FUNCTION
1119 && atom->value.function.isym
1120 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1121 atom = atom->value.function.actual->expr;
1122
1123 if (!gfc_check_atomic (atom, 0, new_val, 3, stat, 4))
1124 return false;
1125
1126 if (!scalar_check (old, 1) || !scalar_check (compare, 2))
1127 return false;
1128
1129 if (!same_type_check (atom, 0, old, 1))
1130 return false;
1131
1132 if (!same_type_check (atom, 0, compare, 2))
1133 return false;
1134
1135 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1136 {
1137 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1138 "definable", gfc_current_intrinsic, &atom->where);
1139 return false;
1140 }
1141
1142 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1143 {
1144 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1145 "definable", gfc_current_intrinsic, &old->where);
1146 return false;
1147 }
1148
1149 return true;
1150 }
1151
1152
1153 bool
1154 gfc_check_atomic_fetch_op (gfc_expr *atom, gfc_expr *value, gfc_expr *old,
1155 gfc_expr *stat)
1156 {
1157 if (atom->expr_type == EXPR_FUNCTION
1158 && atom->value.function.isym
1159 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1160 atom = atom->value.function.actual->expr;
1161
1162 if (atom->ts.type != BT_INTEGER || atom->ts.kind != gfc_atomic_int_kind)
1163 {
1164 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1165 "integer of ATOMIC_INT_KIND", &atom->where,
1166 gfc_current_intrinsic);
1167 return false;
1168 }
1169
1170 if (!gfc_check_atomic (atom, 0, value, 1, stat, 3))
1171 return false;
1172
1173 if (!scalar_check (old, 2))
1174 return false;
1175
1176 if (!same_type_check (atom, 0, old, 2))
1177 return false;
1178
1179 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1180 {
1181 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1182 "definable", gfc_current_intrinsic, &atom->where);
1183 return false;
1184 }
1185
1186 if (!gfc_check_vardef_context (old, false, false, false, NULL))
1187 {
1188 gfc_error ("OLD argument of the %s intrinsic function at %L shall be "
1189 "definable", gfc_current_intrinsic, &old->where);
1190 return false;
1191 }
1192
1193 return true;
1194 }
1195
1196
1197 /* BESJN and BESYN functions. */
1198
1199 bool
1200 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1201 {
1202 if (!type_check (n, 0, BT_INTEGER))
1203 return false;
1204 if (n->expr_type == EXPR_CONSTANT)
1205 {
1206 int i;
1207 gfc_extract_int (n, &i);
1208 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1209 "N at %L", &n->where))
1210 return false;
1211 }
1212
1213 if (!type_check (x, 1, BT_REAL))
1214 return false;
1215
1216 return true;
1217 }
1218
1219
1220 /* Transformational version of the Bessel JN and YN functions. */
1221
1222 bool
1223 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1224 {
1225 if (!type_check (n1, 0, BT_INTEGER))
1226 return false;
1227 if (!scalar_check (n1, 0))
1228 return false;
1229 if (!nonnegative_check ("N1", n1))
1230 return false;
1231
1232 if (!type_check (n2, 1, BT_INTEGER))
1233 return false;
1234 if (!scalar_check (n2, 1))
1235 return false;
1236 if (!nonnegative_check ("N2", n2))
1237 return false;
1238
1239 if (!type_check (x, 2, BT_REAL))
1240 return false;
1241 if (!scalar_check (x, 2))
1242 return false;
1243
1244 return true;
1245 }
1246
1247
1248 bool
1249 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1250 {
1251 if (!type_check (i, 0, BT_INTEGER))
1252 return false;
1253
1254 if (!type_check (j, 1, BT_INTEGER))
1255 return false;
1256
1257 return true;
1258 }
1259
1260
1261 bool
1262 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1263 {
1264 if (!type_check (i, 0, BT_INTEGER))
1265 return false;
1266
1267 if (!type_check (pos, 1, BT_INTEGER))
1268 return false;
1269
1270 if (!nonnegative_check ("pos", pos))
1271 return false;
1272
1273 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1274 return false;
1275
1276 return true;
1277 }
1278
1279
1280 bool
1281 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1282 {
1283 if (!type_check (i, 0, BT_INTEGER))
1284 return false;
1285 if (!kind_check (kind, 1, BT_CHARACTER))
1286 return false;
1287
1288 return true;
1289 }
1290
1291
1292 bool
1293 gfc_check_chdir (gfc_expr *dir)
1294 {
1295 if (!type_check (dir, 0, BT_CHARACTER))
1296 return false;
1297 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1298 return false;
1299
1300 return true;
1301 }
1302
1303
1304 bool
1305 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1306 {
1307 if (!type_check (dir, 0, BT_CHARACTER))
1308 return false;
1309 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1310 return false;
1311
1312 if (status == NULL)
1313 return true;
1314
1315 if (!type_check (status, 1, BT_INTEGER))
1316 return false;
1317 if (!scalar_check (status, 1))
1318 return false;
1319
1320 return true;
1321 }
1322
1323
1324 bool
1325 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1326 {
1327 if (!type_check (name, 0, BT_CHARACTER))
1328 return false;
1329 if (!kind_value_check (name, 0, gfc_default_character_kind))
1330 return false;
1331
1332 if (!type_check (mode, 1, BT_CHARACTER))
1333 return false;
1334 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1335 return false;
1336
1337 return true;
1338 }
1339
1340
1341 bool
1342 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1343 {
1344 if (!type_check (name, 0, BT_CHARACTER))
1345 return false;
1346 if (!kind_value_check (name, 0, gfc_default_character_kind))
1347 return false;
1348
1349 if (!type_check (mode, 1, BT_CHARACTER))
1350 return false;
1351 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1352 return false;
1353
1354 if (status == NULL)
1355 return true;
1356
1357 if (!type_check (status, 2, BT_INTEGER))
1358 return false;
1359
1360 if (!scalar_check (status, 2))
1361 return false;
1362
1363 return true;
1364 }
1365
1366
1367 bool
1368 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1369 {
1370 if (!numeric_check (x, 0))
1371 return false;
1372
1373 if (y != NULL)
1374 {
1375 if (!numeric_check (y, 1))
1376 return false;
1377
1378 if (x->ts.type == BT_COMPLEX)
1379 {
1380 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1381 "present if %<x%> is COMPLEX",
1382 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1383 &y->where);
1384 return false;
1385 }
1386
1387 if (y->ts.type == BT_COMPLEX)
1388 {
1389 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1390 "of either REAL or INTEGER",
1391 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1392 &y->where);
1393 return false;
1394 }
1395
1396 }
1397
1398 if (!kind_check (kind, 2, BT_COMPLEX))
1399 return false;
1400
1401 if (!kind && warn_conversion
1402 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1403 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1404 "COMPLEX(%d) at %L might lose precision, consider using "
1405 "the KIND argument", gfc_typename (&x->ts),
1406 gfc_default_real_kind, &x->where);
1407 else if (y && !kind && warn_conversion
1408 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1409 gfc_warning_now (OPT_Wconversion, "Conversion from %s to default-kind "
1410 "COMPLEX(%d) at %L might lose precision, consider using "
1411 "the KIND argument", gfc_typename (&y->ts),
1412 gfc_default_real_kind, &y->where);
1413 return true;
1414 }
1415
1416
1417 static bool
1418 check_co_collective (gfc_expr *a, gfc_expr *image_idx, gfc_expr *stat,
1419 gfc_expr *errmsg, bool co_reduce)
1420 {
1421 if (!variable_check (a, 0, false))
1422 return false;
1423
1424 if (!gfc_check_vardef_context (a, false, false, false, "argument 'A' with "
1425 "INTENT(INOUT)"))
1426 return false;
1427
1428 /* Fortran 2008, 12.5.2.4, paragraph 18. */
1429 if (gfc_has_vector_subscript (a))
1430 {
1431 gfc_error ("Argument %<A%> with INTENT(INOUT) at %L of the intrinsic "
1432 "subroutine %s shall not have a vector subscript",
1433 &a->where, gfc_current_intrinsic);
1434 return false;
1435 }
1436
1437 if (gfc_is_coindexed (a))
1438 {
1439 gfc_error ("The A argument at %L to the intrinsic %s shall not be "
1440 "coindexed", &a->where, gfc_current_intrinsic);
1441 return false;
1442 }
1443
1444 if (image_idx != NULL)
1445 {
1446 if (!type_check (image_idx, co_reduce ? 2 : 1, BT_INTEGER))
1447 return false;
1448 if (!scalar_check (image_idx, co_reduce ? 2 : 1))
1449 return false;
1450 }
1451
1452 if (stat != NULL)
1453 {
1454 if (!type_check (stat, co_reduce ? 3 : 2, BT_INTEGER))
1455 return false;
1456 if (!scalar_check (stat, co_reduce ? 3 : 2))
1457 return false;
1458 if (!variable_check (stat, co_reduce ? 3 : 2, false))
1459 return false;
1460 if (stat->ts.kind != 4)
1461 {
1462 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1463 "variable", &stat->where);
1464 return false;
1465 }
1466 }
1467
1468 if (errmsg != NULL)
1469 {
1470 if (!type_check (errmsg, co_reduce ? 4 : 3, BT_CHARACTER))
1471 return false;
1472 if (!scalar_check (errmsg, co_reduce ? 4 : 3))
1473 return false;
1474 if (!variable_check (errmsg, co_reduce ? 4 : 3, false))
1475 return false;
1476 if (errmsg->ts.kind != 1)
1477 {
1478 gfc_error ("The errmsg= argument at %L must be a default-kind "
1479 "character variable", &errmsg->where);
1480 return false;
1481 }
1482 }
1483
1484 if (flag_coarray == GFC_FCOARRAY_NONE)
1485 {
1486 gfc_fatal_error ("Coarrays disabled at %L, use %<-fcoarray=%> to enable",
1487 &a->where);
1488 return false;
1489 }
1490
1491 return true;
1492 }
1493
1494
1495 bool
1496 gfc_check_co_broadcast (gfc_expr *a, gfc_expr *source_image, gfc_expr *stat,
1497 gfc_expr *errmsg)
1498 {
1499 if (a->ts.type == BT_CLASS || gfc_expr_attr (a).alloc_comp)
1500 {
1501 gfc_error ("Support for the A argument at %L which is polymorphic A "
1502 "argument or has allocatable components is not yet "
1503 "implemented", &a->where);
1504 return false;
1505 }
1506 return check_co_collective (a, source_image, stat, errmsg, false);
1507 }
1508
1509
1510 bool
1511 gfc_check_co_reduce (gfc_expr *a, gfc_expr *op, gfc_expr *result_image,
1512 gfc_expr *stat, gfc_expr *errmsg)
1513 {
1514 symbol_attribute attr;
1515 gfc_formal_arglist *formal;
1516 gfc_symbol *sym;
1517
1518 if (a->ts.type == BT_CLASS)
1519 {
1520 gfc_error ("The A argument at %L of CO_REDUCE shall not be polymorphic",
1521 &a->where);
1522 return false;
1523 }
1524
1525 if (gfc_expr_attr (a).alloc_comp)
1526 {
1527 gfc_error ("Support for the A argument at %L with allocatable components"
1528 " is not yet implemented", &a->where);
1529 return false;
1530 }
1531
1532 if (!check_co_collective (a, result_image, stat, errmsg, true))
1533 return false;
1534
1535 if (!gfc_resolve_expr (op))
1536 return false;
1537
1538 attr = gfc_expr_attr (op);
1539 if (!attr.pure || !attr.function)
1540 {
1541 gfc_error ("OPERATOR argument at %L must be a PURE function",
1542 &op->where);
1543 return false;
1544 }
1545
1546 if (attr.intrinsic)
1547 {
1548 /* None of the intrinsics fulfills the criteria of taking two arguments,
1549 returning the same type and kind as the arguments and being permitted
1550 as actual argument. */
1551 gfc_error ("Intrinsic function %s at %L is not permitted for CO_REDUCE",
1552 op->symtree->n.sym->name, &op->where);
1553 return false;
1554 }
1555
1556 if (gfc_is_proc_ptr_comp (op))
1557 {
1558 gfc_component *comp = gfc_get_proc_ptr_comp (op);
1559 sym = comp->ts.interface;
1560 }
1561 else
1562 sym = op->symtree->n.sym;
1563
1564 formal = sym->formal;
1565
1566 if (!formal || !formal->next || formal->next->next)
1567 {
1568 gfc_error ("The function passed as OPERATOR at %L shall have two "
1569 "arguments", &op->where);
1570 return false;
1571 }
1572
1573 if (sym->result->ts.type == BT_UNKNOWN)
1574 gfc_set_default_type (sym->result, 0, NULL);
1575
1576 if (!gfc_compare_types (&a->ts, &sym->result->ts))
1577 {
1578 gfc_error ("A argument at %L has type %s but the function passed as "
1579 "OPERATOR at %L returns %s",
1580 &a->where, gfc_typename (&a->ts), &op->where,
1581 gfc_typename (&sym->result->ts));
1582 return false;
1583 }
1584 if (!gfc_compare_types (&a->ts, &formal->sym->ts)
1585 || !gfc_compare_types (&a->ts, &formal->next->sym->ts))
1586 {
1587 gfc_error ("The function passed as OPERATOR at %L has arguments of type "
1588 "%s and %s but shall have type %s", &op->where,
1589 gfc_typename (&formal->sym->ts),
1590 gfc_typename (&formal->next->sym->ts), gfc_typename (&a->ts));
1591 return false;
1592 }
1593 if (op->rank || attr.allocatable || attr.pointer || formal->sym->as
1594 || formal->next->sym->as || formal->sym->attr.allocatable
1595 || formal->next->sym->attr.allocatable || formal->sym->attr.pointer
1596 || formal->next->sym->attr.pointer)
1597 {
1598 gfc_error ("The function passed as OPERATOR at %L shall have scalar "
1599 "nonallocatable nonpointer arguments and return a "
1600 "nonallocatable nonpointer scalar", &op->where);
1601 return false;
1602 }
1603
1604 if (formal->sym->attr.value != formal->next->sym->attr.value)
1605 {
1606 gfc_error ("The function passed as OPERATOR at %L shall have the VALUE "
1607 "attribute either for none or both arguments", &op->where);
1608 return false;
1609 }
1610
1611 if (formal->sym->attr.target != formal->next->sym->attr.target)
1612 {
1613 gfc_error ("The function passed as OPERATOR at %L shall have the TARGET "
1614 "attribute either for none or both arguments", &op->where);
1615 return false;
1616 }
1617
1618 if (formal->sym->attr.asynchronous != formal->next->sym->attr.asynchronous)
1619 {
1620 gfc_error ("The function passed as OPERATOR at %L shall have the "
1621 "ASYNCHRONOUS attribute either for none or both arguments",
1622 &op->where);
1623 return false;
1624 }
1625
1626 if (formal->sym->attr.optional || formal->next->sym->attr.optional)
1627 {
1628 gfc_error ("The function passed as OPERATOR at %L shall not have the "
1629 "OPTIONAL attribute for either of the arguments", &op->where);
1630 return false;
1631 }
1632
1633 if (a->ts.type == BT_CHARACTER)
1634 {
1635 gfc_charlen *cl;
1636 unsigned long actual_size, formal_size1, formal_size2, result_size;
1637
1638 cl = a->ts.u.cl;
1639 actual_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1640 ? mpz_get_ui (cl->length->value.integer) : 0;
1641
1642 cl = formal->sym->ts.u.cl;
1643 formal_size1 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1644 ? mpz_get_ui (cl->length->value.integer) : 0;
1645
1646 cl = formal->next->sym->ts.u.cl;
1647 formal_size2 = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1648 ? mpz_get_ui (cl->length->value.integer) : 0;
1649
1650 cl = sym->ts.u.cl;
1651 result_size = cl && cl->length && cl->length->expr_type == EXPR_CONSTANT
1652 ? mpz_get_ui (cl->length->value.integer) : 0;
1653
1654 if (actual_size
1655 && ((formal_size1 && actual_size != formal_size1)
1656 || (formal_size2 && actual_size != formal_size2)))
1657 {
1658 gfc_error ("The character length of the A argument at %L and of the "
1659 "arguments of the OPERATOR at %L shall be the same",
1660 &a->where, &op->where);
1661 return false;
1662 }
1663 if (actual_size && result_size && actual_size != result_size)
1664 {
1665 gfc_error ("The character length of the A argument at %L and of the "
1666 "function result of the OPERATOR at %L shall be the same",
1667 &a->where, &op->where);
1668 return false;
1669 }
1670 }
1671
1672 return true;
1673 }
1674
1675
1676 bool
1677 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1678 gfc_expr *errmsg)
1679 {
1680 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1681 && a->ts.type != BT_CHARACTER)
1682 {
1683 gfc_error ("%qs argument of %qs intrinsic at %L shall be of type "
1684 "integer, real or character",
1685 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1686 &a->where);
1687 return false;
1688 }
1689 return check_co_collective (a, result_image, stat, errmsg, false);
1690 }
1691
1692
1693 bool
1694 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1695 gfc_expr *errmsg)
1696 {
1697 if (!numeric_check (a, 0))
1698 return false;
1699 return check_co_collective (a, result_image, stat, errmsg, false);
1700 }
1701
1702
1703 bool
1704 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1705 {
1706 if (!int_or_real_check (x, 0))
1707 return false;
1708 if (!scalar_check (x, 0))
1709 return false;
1710
1711 if (!int_or_real_check (y, 1))
1712 return false;
1713 if (!scalar_check (y, 1))
1714 return false;
1715
1716 return true;
1717 }
1718
1719
1720 bool
1721 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1722 {
1723 if (!logical_array_check (mask, 0))
1724 return false;
1725 if (!dim_check (dim, 1, false))
1726 return false;
1727 if (!dim_rank_check (dim, mask, 0))
1728 return false;
1729 if (!kind_check (kind, 2, BT_INTEGER))
1730 return false;
1731 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
1732 "with KIND argument at %L",
1733 gfc_current_intrinsic, &kind->where))
1734 return false;
1735
1736 return true;
1737 }
1738
1739
1740 bool
1741 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1742 {
1743 if (!array_check (array, 0))
1744 return false;
1745
1746 if (!type_check (shift, 1, BT_INTEGER))
1747 return false;
1748
1749 if (!dim_check (dim, 2, true))
1750 return false;
1751
1752 if (!dim_rank_check (dim, array, false))
1753 return false;
1754
1755 if (array->rank == 1 || shift->rank == 0)
1756 {
1757 if (!scalar_check (shift, 1))
1758 return false;
1759 }
1760 else if (shift->rank == array->rank - 1)
1761 {
1762 int d;
1763 if (!dim)
1764 d = 1;
1765 else if (dim->expr_type == EXPR_CONSTANT)
1766 gfc_extract_int (dim, &d);
1767 else
1768 d = -1;
1769
1770 if (d > 0)
1771 {
1772 int i, j;
1773 for (i = 0, j = 0; i < array->rank; i++)
1774 if (i != d - 1)
1775 {
1776 if (!identical_dimen_shape (array, i, shift, j))
1777 {
1778 gfc_error ("%qs argument of %qs intrinsic at %L has "
1779 "invalid shape in dimension %d (%ld/%ld)",
1780 gfc_current_intrinsic_arg[1]->name,
1781 gfc_current_intrinsic, &shift->where, i + 1,
1782 mpz_get_si (array->shape[i]),
1783 mpz_get_si (shift->shape[j]));
1784 return false;
1785 }
1786
1787 j += 1;
1788 }
1789 }
1790 }
1791 else
1792 {
1793 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
1794 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1795 gfc_current_intrinsic, &shift->where, array->rank - 1);
1796 return false;
1797 }
1798
1799 return true;
1800 }
1801
1802
1803 bool
1804 gfc_check_ctime (gfc_expr *time)
1805 {
1806 if (!scalar_check (time, 0))
1807 return false;
1808
1809 if (!type_check (time, 0, BT_INTEGER))
1810 return false;
1811
1812 return true;
1813 }
1814
1815
1816 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1817 {
1818 if (!double_check (y, 0) || !double_check (x, 1))
1819 return false;
1820
1821 return true;
1822 }
1823
1824 bool
1825 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1826 {
1827 if (!numeric_check (x, 0))
1828 return false;
1829
1830 if (y != NULL)
1831 {
1832 if (!numeric_check (y, 1))
1833 return false;
1834
1835 if (x->ts.type == BT_COMPLEX)
1836 {
1837 gfc_error ("%qs argument of %qs intrinsic at %L must not be "
1838 "present if %<x%> is COMPLEX",
1839 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1840 &y->where);
1841 return false;
1842 }
1843
1844 if (y->ts.type == BT_COMPLEX)
1845 {
1846 gfc_error ("%qs argument of %qs intrinsic at %L must have a type "
1847 "of either REAL or INTEGER",
1848 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1849 &y->where);
1850 return false;
1851 }
1852 }
1853
1854 return true;
1855 }
1856
1857
1858 bool
1859 gfc_check_dble (gfc_expr *x)
1860 {
1861 if (!numeric_check (x, 0))
1862 return false;
1863
1864 return true;
1865 }
1866
1867
1868 bool
1869 gfc_check_digits (gfc_expr *x)
1870 {
1871 if (!int_or_real_check (x, 0))
1872 return false;
1873
1874 return true;
1875 }
1876
1877
1878 bool
1879 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1880 {
1881 switch (vector_a->ts.type)
1882 {
1883 case BT_LOGICAL:
1884 if (!type_check (vector_b, 1, BT_LOGICAL))
1885 return false;
1886 break;
1887
1888 case BT_INTEGER:
1889 case BT_REAL:
1890 case BT_COMPLEX:
1891 if (!numeric_check (vector_b, 1))
1892 return false;
1893 break;
1894
1895 default:
1896 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
1897 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1898 gfc_current_intrinsic, &vector_a->where);
1899 return false;
1900 }
1901
1902 if (!rank_check (vector_a, 0, 1))
1903 return false;
1904
1905 if (!rank_check (vector_b, 1, 1))
1906 return false;
1907
1908 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1909 {
1910 gfc_error ("Different shape for arguments %qs and %qs at %L for "
1911 "intrinsic %<dot_product%>",
1912 gfc_current_intrinsic_arg[0]->name,
1913 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1914 return false;
1915 }
1916
1917 return true;
1918 }
1919
1920
1921 bool
1922 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
1923 {
1924 if (!type_check (x, 0, BT_REAL)
1925 || !type_check (y, 1, BT_REAL))
1926 return false;
1927
1928 if (x->ts.kind != gfc_default_real_kind)
1929 {
1930 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
1931 "real", gfc_current_intrinsic_arg[0]->name,
1932 gfc_current_intrinsic, &x->where);
1933 return false;
1934 }
1935
1936 if (y->ts.kind != gfc_default_real_kind)
1937 {
1938 gfc_error ("%qs argument of %qs intrinsic at %L must be default "
1939 "real", gfc_current_intrinsic_arg[1]->name,
1940 gfc_current_intrinsic, &y->where);
1941 return false;
1942 }
1943
1944 return true;
1945 }
1946
1947
1948 bool
1949 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
1950 {
1951 if (!type_check (i, 0, BT_INTEGER))
1952 return false;
1953
1954 if (!type_check (j, 1, BT_INTEGER))
1955 return false;
1956
1957 if (i->is_boz && j->is_boz)
1958 {
1959 gfc_error ("%<I%> at %L and %<J%>' at %L cannot both be BOZ literal "
1960 "constants", &i->where, &j->where);
1961 return false;
1962 }
1963
1964 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
1965 return false;
1966
1967 if (!type_check (shift, 2, BT_INTEGER))
1968 return false;
1969
1970 if (!nonnegative_check ("SHIFT", shift))
1971 return false;
1972
1973 if (i->is_boz)
1974 {
1975 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
1976 return false;
1977 i->ts.kind = j->ts.kind;
1978 }
1979 else
1980 {
1981 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
1982 return false;
1983 j->ts.kind = i->ts.kind;
1984 }
1985
1986 return true;
1987 }
1988
1989
1990 bool
1991 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
1992 gfc_expr *dim)
1993 {
1994 if (!array_check (array, 0))
1995 return false;
1996
1997 if (!type_check (shift, 1, BT_INTEGER))
1998 return false;
1999
2000 if (!dim_check (dim, 3, true))
2001 return false;
2002
2003 if (!dim_rank_check (dim, array, false))
2004 return false;
2005
2006 if (array->rank == 1 || shift->rank == 0)
2007 {
2008 if (!scalar_check (shift, 1))
2009 return false;
2010 }
2011 else if (shift->rank == array->rank - 1)
2012 {
2013 int d;
2014 if (!dim)
2015 d = 1;
2016 else if (dim->expr_type == EXPR_CONSTANT)
2017 gfc_extract_int (dim, &d);
2018 else
2019 d = -1;
2020
2021 if (d > 0)
2022 {
2023 int i, j;
2024 for (i = 0, j = 0; i < array->rank; i++)
2025 if (i != d - 1)
2026 {
2027 if (!identical_dimen_shape (array, i, shift, j))
2028 {
2029 gfc_error ("%qs argument of %qs intrinsic at %L has "
2030 "invalid shape in dimension %d (%ld/%ld)",
2031 gfc_current_intrinsic_arg[1]->name,
2032 gfc_current_intrinsic, &shift->where, i + 1,
2033 mpz_get_si (array->shape[i]),
2034 mpz_get_si (shift->shape[j]));
2035 return false;
2036 }
2037
2038 j += 1;
2039 }
2040 }
2041 }
2042 else
2043 {
2044 gfc_error ("%qs argument of intrinsic %qs at %L of must have rank "
2045 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
2046 gfc_current_intrinsic, &shift->where, array->rank - 1);
2047 return false;
2048 }
2049
2050 if (boundary != NULL)
2051 {
2052 if (!same_type_check (array, 0, boundary, 2))
2053 return false;
2054
2055 if (array->rank == 1 || boundary->rank == 0)
2056 {
2057 if (!scalar_check (boundary, 2))
2058 return false;
2059 }
2060 else if (boundary->rank == array->rank - 1)
2061 {
2062 if (!gfc_check_conformance (shift, boundary,
2063 "arguments '%s' and '%s' for "
2064 "intrinsic %s",
2065 gfc_current_intrinsic_arg[1]->name,
2066 gfc_current_intrinsic_arg[2]->name,
2067 gfc_current_intrinsic))
2068 return false;
2069 }
2070 else
2071 {
2072 gfc_error ("%qs argument of intrinsic %qs at %L of must have "
2073 "rank %d or be a scalar",
2074 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
2075 &shift->where, array->rank - 1);
2076 return false;
2077 }
2078 }
2079
2080 return true;
2081 }
2082
2083 bool
2084 gfc_check_float (gfc_expr *a)
2085 {
2086 if (!type_check (a, 0, BT_INTEGER))
2087 return false;
2088
2089 if ((a->ts.kind != gfc_default_integer_kind)
2090 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
2091 "kind argument to %s intrinsic at %L",
2092 gfc_current_intrinsic, &a->where))
2093 return false;
2094
2095 return true;
2096 }
2097
2098 /* A single complex argument. */
2099
2100 bool
2101 gfc_check_fn_c (gfc_expr *a)
2102 {
2103 if (!type_check (a, 0, BT_COMPLEX))
2104 return false;
2105
2106 return true;
2107 }
2108
2109 /* A single real argument. */
2110
2111 bool
2112 gfc_check_fn_r (gfc_expr *a)
2113 {
2114 if (!type_check (a, 0, BT_REAL))
2115 return false;
2116
2117 return true;
2118 }
2119
2120 /* A single double argument. */
2121
2122 bool
2123 gfc_check_fn_d (gfc_expr *a)
2124 {
2125 if (!double_check (a, 0))
2126 return false;
2127
2128 return true;
2129 }
2130
2131 /* A single real or complex argument. */
2132
2133 bool
2134 gfc_check_fn_rc (gfc_expr *a)
2135 {
2136 if (!real_or_complex_check (a, 0))
2137 return false;
2138
2139 return true;
2140 }
2141
2142
2143 bool
2144 gfc_check_fn_rc2008 (gfc_expr *a)
2145 {
2146 if (!real_or_complex_check (a, 0))
2147 return false;
2148
2149 if (a->ts.type == BT_COMPLEX
2150 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument %qs "
2151 "of %qs intrinsic at %L",
2152 gfc_current_intrinsic_arg[0]->name,
2153 gfc_current_intrinsic, &a->where))
2154 return false;
2155
2156 return true;
2157 }
2158
2159
2160 bool
2161 gfc_check_fnum (gfc_expr *unit)
2162 {
2163 if (!type_check (unit, 0, BT_INTEGER))
2164 return false;
2165
2166 if (!scalar_check (unit, 0))
2167 return false;
2168
2169 return true;
2170 }
2171
2172
2173 bool
2174 gfc_check_huge (gfc_expr *x)
2175 {
2176 if (!int_or_real_check (x, 0))
2177 return false;
2178
2179 return true;
2180 }
2181
2182
2183 bool
2184 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
2185 {
2186 if (!type_check (x, 0, BT_REAL))
2187 return false;
2188 if (!same_type_check (x, 0, y, 1))
2189 return false;
2190
2191 return true;
2192 }
2193
2194
2195 /* Check that the single argument is an integer. */
2196
2197 bool
2198 gfc_check_i (gfc_expr *i)
2199 {
2200 if (!type_check (i, 0, BT_INTEGER))
2201 return false;
2202
2203 return true;
2204 }
2205
2206
2207 bool
2208 gfc_check_iand (gfc_expr *i, gfc_expr *j)
2209 {
2210 if (!type_check (i, 0, BT_INTEGER))
2211 return false;
2212
2213 if (!type_check (j, 1, BT_INTEGER))
2214 return false;
2215
2216 if (i->ts.kind != j->ts.kind)
2217 {
2218 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2219 &i->where))
2220 return false;
2221 }
2222
2223 return true;
2224 }
2225
2226
2227 bool
2228 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
2229 {
2230 if (!type_check (i, 0, BT_INTEGER))
2231 return false;
2232
2233 if (!type_check (pos, 1, BT_INTEGER))
2234 return false;
2235
2236 if (!type_check (len, 2, BT_INTEGER))
2237 return false;
2238
2239 if (!nonnegative_check ("pos", pos))
2240 return false;
2241
2242 if (!nonnegative_check ("len", len))
2243 return false;
2244
2245 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
2246 return false;
2247
2248 return true;
2249 }
2250
2251
2252 bool
2253 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
2254 {
2255 int i;
2256
2257 if (!type_check (c, 0, BT_CHARACTER))
2258 return false;
2259
2260 if (!kind_check (kind, 1, BT_INTEGER))
2261 return false;
2262
2263 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2264 "with KIND argument at %L",
2265 gfc_current_intrinsic, &kind->where))
2266 return false;
2267
2268 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
2269 {
2270 gfc_expr *start;
2271 gfc_expr *end;
2272 gfc_ref *ref;
2273
2274 /* Substring references don't have the charlength set. */
2275 ref = c->ref;
2276 while (ref && ref->type != REF_SUBSTRING)
2277 ref = ref->next;
2278
2279 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
2280
2281 if (!ref)
2282 {
2283 /* Check that the argument is length one. Non-constant lengths
2284 can't be checked here, so assume they are ok. */
2285 if (c->ts.u.cl && c->ts.u.cl->length)
2286 {
2287 /* If we already have a length for this expression then use it. */
2288 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
2289 return true;
2290 i = mpz_get_si (c->ts.u.cl->length->value.integer);
2291 }
2292 else
2293 return true;
2294 }
2295 else
2296 {
2297 start = ref->u.ss.start;
2298 end = ref->u.ss.end;
2299
2300 gcc_assert (start);
2301 if (end == NULL || end->expr_type != EXPR_CONSTANT
2302 || start->expr_type != EXPR_CONSTANT)
2303 return true;
2304
2305 i = mpz_get_si (end->value.integer) + 1
2306 - mpz_get_si (start->value.integer);
2307 }
2308 }
2309 else
2310 return true;
2311
2312 if (i != 1)
2313 {
2314 gfc_error ("Argument of %s at %L must be of length one",
2315 gfc_current_intrinsic, &c->where);
2316 return false;
2317 }
2318
2319 return true;
2320 }
2321
2322
2323 bool
2324 gfc_check_idnint (gfc_expr *a)
2325 {
2326 if (!double_check (a, 0))
2327 return false;
2328
2329 return true;
2330 }
2331
2332
2333 bool
2334 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2335 {
2336 if (!type_check (i, 0, BT_INTEGER))
2337 return false;
2338
2339 if (!type_check (j, 1, BT_INTEGER))
2340 return false;
2341
2342 if (i->ts.kind != j->ts.kind)
2343 {
2344 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2345 &i->where))
2346 return false;
2347 }
2348
2349 return true;
2350 }
2351
2352
2353 bool
2354 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2355 gfc_expr *kind)
2356 {
2357 if (!type_check (string, 0, BT_CHARACTER)
2358 || !type_check (substring, 1, BT_CHARACTER))
2359 return false;
2360
2361 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2362 return false;
2363
2364 if (!kind_check (kind, 3, BT_INTEGER))
2365 return false;
2366 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2367 "with KIND argument at %L",
2368 gfc_current_intrinsic, &kind->where))
2369 return false;
2370
2371 if (string->ts.kind != substring->ts.kind)
2372 {
2373 gfc_error ("%qs argument of %qs intrinsic at %L must be the same "
2374 "kind as %qs", gfc_current_intrinsic_arg[1]->name,
2375 gfc_current_intrinsic, &substring->where,
2376 gfc_current_intrinsic_arg[0]->name);
2377 return false;
2378 }
2379
2380 return true;
2381 }
2382
2383
2384 bool
2385 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2386 {
2387 if (!numeric_check (x, 0))
2388 return false;
2389
2390 if (!kind_check (kind, 1, BT_INTEGER))
2391 return false;
2392
2393 return true;
2394 }
2395
2396
2397 bool
2398 gfc_check_intconv (gfc_expr *x)
2399 {
2400 if (!numeric_check (x, 0))
2401 return false;
2402
2403 return true;
2404 }
2405
2406
2407 bool
2408 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2409 {
2410 if (!type_check (i, 0, BT_INTEGER))
2411 return false;
2412
2413 if (!type_check (j, 1, BT_INTEGER))
2414 return false;
2415
2416 if (i->ts.kind != j->ts.kind)
2417 {
2418 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2419 &i->where))
2420 return false;
2421 }
2422
2423 return true;
2424 }
2425
2426
2427 bool
2428 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2429 {
2430 if (!type_check (i, 0, BT_INTEGER)
2431 || !type_check (shift, 1, BT_INTEGER))
2432 return false;
2433
2434 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2435 return false;
2436
2437 return true;
2438 }
2439
2440
2441 bool
2442 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2443 {
2444 if (!type_check (i, 0, BT_INTEGER)
2445 || !type_check (shift, 1, BT_INTEGER))
2446 return false;
2447
2448 if (size != NULL)
2449 {
2450 int i2, i3;
2451
2452 if (!type_check (size, 2, BT_INTEGER))
2453 return false;
2454
2455 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2456 return false;
2457
2458 if (size->expr_type == EXPR_CONSTANT)
2459 {
2460 gfc_extract_int (size, &i3);
2461 if (i3 <= 0)
2462 {
2463 gfc_error ("SIZE at %L must be positive", &size->where);
2464 return false;
2465 }
2466
2467 if (shift->expr_type == EXPR_CONSTANT)
2468 {
2469 gfc_extract_int (shift, &i2);
2470 if (i2 < 0)
2471 i2 = -i2;
2472
2473 if (i2 > i3)
2474 {
2475 gfc_error ("The absolute value of SHIFT at %L must be less "
2476 "than or equal to SIZE at %L", &shift->where,
2477 &size->where);
2478 return false;
2479 }
2480 }
2481 }
2482 }
2483 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2484 return false;
2485
2486 return true;
2487 }
2488
2489
2490 bool
2491 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2492 {
2493 if (!type_check (pid, 0, BT_INTEGER))
2494 return false;
2495
2496 if (!type_check (sig, 1, BT_INTEGER))
2497 return false;
2498
2499 return true;
2500 }
2501
2502
2503 bool
2504 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2505 {
2506 if (!type_check (pid, 0, BT_INTEGER))
2507 return false;
2508
2509 if (!scalar_check (pid, 0))
2510 return false;
2511
2512 if (!type_check (sig, 1, BT_INTEGER))
2513 return false;
2514
2515 if (!scalar_check (sig, 1))
2516 return false;
2517
2518 if (status == NULL)
2519 return true;
2520
2521 if (!type_check (status, 2, BT_INTEGER))
2522 return false;
2523
2524 if (!scalar_check (status, 2))
2525 return false;
2526
2527 return true;
2528 }
2529
2530
2531 bool
2532 gfc_check_kind (gfc_expr *x)
2533 {
2534 if (x->ts.type == BT_DERIVED || x->ts.type == BT_CLASS)
2535 {
2536 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
2537 "intrinsic type", gfc_current_intrinsic_arg[0]->name,
2538 gfc_current_intrinsic, &x->where);
2539 return false;
2540 }
2541 if (x->ts.type == BT_PROCEDURE)
2542 {
2543 gfc_error ("%qs argument of %qs intrinsic at %L must be a data entity",
2544 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
2545 &x->where);
2546 return false;
2547 }
2548
2549 return true;
2550 }
2551
2552
2553 bool
2554 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2555 {
2556 if (!array_check (array, 0))
2557 return false;
2558
2559 if (!dim_check (dim, 1, false))
2560 return false;
2561
2562 if (!dim_rank_check (dim, array, 1))
2563 return false;
2564
2565 if (!kind_check (kind, 2, BT_INTEGER))
2566 return false;
2567 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2568 "with KIND argument at %L",
2569 gfc_current_intrinsic, &kind->where))
2570 return false;
2571
2572 return true;
2573 }
2574
2575
2576 bool
2577 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2578 {
2579 if (flag_coarray == GFC_FCOARRAY_NONE)
2580 {
2581 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
2582 return false;
2583 }
2584
2585 if (!coarray_check (coarray, 0))
2586 return false;
2587
2588 if (dim != NULL)
2589 {
2590 if (!dim_check (dim, 1, false))
2591 return false;
2592
2593 if (!dim_corank_check (dim, coarray))
2594 return false;
2595 }
2596
2597 if (!kind_check (kind, 2, BT_INTEGER))
2598 return false;
2599
2600 return true;
2601 }
2602
2603
2604 bool
2605 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2606 {
2607 if (!type_check (s, 0, BT_CHARACTER))
2608 return false;
2609
2610 if (!kind_check (kind, 1, BT_INTEGER))
2611 return false;
2612 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2613 "with KIND argument at %L",
2614 gfc_current_intrinsic, &kind->where))
2615 return false;
2616
2617 return true;
2618 }
2619
2620
2621 bool
2622 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2623 {
2624 if (!type_check (a, 0, BT_CHARACTER))
2625 return false;
2626 if (!kind_value_check (a, 0, gfc_default_character_kind))
2627 return false;
2628
2629 if (!type_check (b, 1, BT_CHARACTER))
2630 return false;
2631 if (!kind_value_check (b, 1, gfc_default_character_kind))
2632 return false;
2633
2634 return true;
2635 }
2636
2637
2638 bool
2639 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2640 {
2641 if (!type_check (path1, 0, BT_CHARACTER))
2642 return false;
2643 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2644 return false;
2645
2646 if (!type_check (path2, 1, BT_CHARACTER))
2647 return false;
2648 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2649 return false;
2650
2651 return true;
2652 }
2653
2654
2655 bool
2656 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2657 {
2658 if (!type_check (path1, 0, BT_CHARACTER))
2659 return false;
2660 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2661 return false;
2662
2663 if (!type_check (path2, 1, BT_CHARACTER))
2664 return false;
2665 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2666 return false;
2667
2668 if (status == NULL)
2669 return true;
2670
2671 if (!type_check (status, 2, BT_INTEGER))
2672 return false;
2673
2674 if (!scalar_check (status, 2))
2675 return false;
2676
2677 return true;
2678 }
2679
2680
2681 bool
2682 gfc_check_loc (gfc_expr *expr)
2683 {
2684 return variable_check (expr, 0, true);
2685 }
2686
2687
2688 bool
2689 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2690 {
2691 if (!type_check (path1, 0, BT_CHARACTER))
2692 return false;
2693 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2694 return false;
2695
2696 if (!type_check (path2, 1, BT_CHARACTER))
2697 return false;
2698 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2699 return false;
2700
2701 return true;
2702 }
2703
2704
2705 bool
2706 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2707 {
2708 if (!type_check (path1, 0, BT_CHARACTER))
2709 return false;
2710 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2711 return false;
2712
2713 if (!type_check (path2, 1, BT_CHARACTER))
2714 return false;
2715 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2716 return false;
2717
2718 if (status == NULL)
2719 return true;
2720
2721 if (!type_check (status, 2, BT_INTEGER))
2722 return false;
2723
2724 if (!scalar_check (status, 2))
2725 return false;
2726
2727 return true;
2728 }
2729
2730
2731 bool
2732 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2733 {
2734 if (!type_check (a, 0, BT_LOGICAL))
2735 return false;
2736 if (!kind_check (kind, 1, BT_LOGICAL))
2737 return false;
2738
2739 return true;
2740 }
2741
2742
2743 /* Min/max family. */
2744
2745 static bool
2746 min_max_args (gfc_actual_arglist *args)
2747 {
2748 gfc_actual_arglist *arg;
2749 int i, j, nargs, *nlabels, nlabelless;
2750 bool a1 = false, a2 = false;
2751
2752 if (args == NULL || args->next == NULL)
2753 {
2754 gfc_error ("Intrinsic %qs at %L must have at least two arguments",
2755 gfc_current_intrinsic, gfc_current_intrinsic_where);
2756 return false;
2757 }
2758
2759 if (!args->name)
2760 a1 = true;
2761
2762 if (!args->next->name)
2763 a2 = true;
2764
2765 nargs = 0;
2766 for (arg = args; arg; arg = arg->next)
2767 if (arg->name)
2768 nargs++;
2769
2770 if (nargs == 0)
2771 return true;
2772
2773 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2774 nlabelless = 0;
2775 nlabels = XALLOCAVEC (int, nargs);
2776 for (arg = args, i = 0; arg; arg = arg->next, i++)
2777 if (arg->name)
2778 {
2779 int n;
2780 char *endp;
2781
2782 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2783 goto unknown;
2784 n = strtol (&arg->name[1], &endp, 10);
2785 if (endp[0] != '\0')
2786 goto unknown;
2787 if (n <= 0)
2788 goto unknown;
2789 if (n <= nlabelless)
2790 goto duplicate;
2791 nlabels[i] = n;
2792 if (n == 1)
2793 a1 = true;
2794 if (n == 2)
2795 a2 = true;
2796 }
2797 else
2798 nlabelless++;
2799
2800 if (!a1 || !a2)
2801 {
2802 gfc_error ("Missing %qs argument to the %s intrinsic at %L",
2803 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2804 gfc_current_intrinsic_where);
2805 return false;
2806 }
2807
2808 /* Check for duplicates. */
2809 for (i = 0; i < nargs; i++)
2810 for (j = i + 1; j < nargs; j++)
2811 if (nlabels[i] == nlabels[j])
2812 goto duplicate;
2813
2814 return true;
2815
2816 duplicate:
2817 gfc_error ("Duplicate argument %qs at %L to intrinsic %s", arg->name,
2818 &arg->expr->where, gfc_current_intrinsic);
2819 return false;
2820
2821 unknown:
2822 gfc_error ("Unknown argument %qs at %L to intrinsic %s", arg->name,
2823 &arg->expr->where, gfc_current_intrinsic);
2824 return false;
2825 }
2826
2827
2828 static bool
2829 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2830 {
2831 gfc_actual_arglist *arg, *tmp;
2832 gfc_expr *x;
2833 int m, n;
2834
2835 if (!min_max_args (arglist))
2836 return false;
2837
2838 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2839 {
2840 x = arg->expr;
2841 if (x->ts.type != type || x->ts.kind != kind)
2842 {
2843 if (x->ts.type == type)
2844 {
2845 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2846 "kinds at %L", &x->where))
2847 return false;
2848 }
2849 else
2850 {
2851 gfc_error ("%<a%d%> argument of %qs intrinsic at %L must be "
2852 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2853 gfc_basic_typename (type), kind);
2854 return false;
2855 }
2856 }
2857
2858 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2859 if (!gfc_check_conformance (tmp->expr, x,
2860 "arguments 'a%d' and 'a%d' for "
2861 "intrinsic '%s'", m, n,
2862 gfc_current_intrinsic))
2863 return false;
2864 }
2865
2866 return true;
2867 }
2868
2869
2870 bool
2871 gfc_check_min_max (gfc_actual_arglist *arg)
2872 {
2873 gfc_expr *x;
2874
2875 if (!min_max_args (arg))
2876 return false;
2877
2878 x = arg->expr;
2879
2880 if (x->ts.type == BT_CHARACTER)
2881 {
2882 if (!gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
2883 "with CHARACTER argument at %L",
2884 gfc_current_intrinsic, &x->where))
2885 return false;
2886 }
2887 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2888 {
2889 gfc_error ("%<a1%> argument of %qs intrinsic at %L must be INTEGER, "
2890 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2891 return false;
2892 }
2893
2894 return check_rest (x->ts.type, x->ts.kind, arg);
2895 }
2896
2897
2898 bool
2899 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2900 {
2901 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2902 }
2903
2904
2905 bool
2906 gfc_check_min_max_real (gfc_actual_arglist *arg)
2907 {
2908 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2909 }
2910
2911
2912 bool
2913 gfc_check_min_max_double (gfc_actual_arglist *arg)
2914 {
2915 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2916 }
2917
2918
2919 /* End of min/max family. */
2920
2921 bool
2922 gfc_check_malloc (gfc_expr *size)
2923 {
2924 if (!type_check (size, 0, BT_INTEGER))
2925 return false;
2926
2927 if (!scalar_check (size, 0))
2928 return false;
2929
2930 return true;
2931 }
2932
2933
2934 bool
2935 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
2936 {
2937 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
2938 {
2939 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2940 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2941 gfc_current_intrinsic, &matrix_a->where);
2942 return false;
2943 }
2944
2945 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
2946 {
2947 gfc_error ("%qs argument of %qs intrinsic at %L must be numeric "
2948 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
2949 gfc_current_intrinsic, &matrix_b->where);
2950 return false;
2951 }
2952
2953 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
2954 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
2955 {
2956 gfc_error ("Argument types of %qs intrinsic at %L must match (%s/%s)",
2957 gfc_current_intrinsic, &matrix_a->where,
2958 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
2959 return false;
2960 }
2961
2962 switch (matrix_a->rank)
2963 {
2964 case 1:
2965 if (!rank_check (matrix_b, 1, 2))
2966 return false;
2967 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
2968 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
2969 {
2970 gfc_error ("Different shape on dimension 1 for arguments %qs "
2971 "and %qs at %L for intrinsic matmul",
2972 gfc_current_intrinsic_arg[0]->name,
2973 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2974 return false;
2975 }
2976 break;
2977
2978 case 2:
2979 if (matrix_b->rank != 2)
2980 {
2981 if (!rank_check (matrix_b, 1, 1))
2982 return false;
2983 }
2984 /* matrix_b has rank 1 or 2 here. Common check for the cases
2985 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
2986 - matrix_a has shape (n,m) and matrix_b has shape (m). */
2987 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
2988 {
2989 gfc_error ("Different shape on dimension 2 for argument %qs and "
2990 "dimension 1 for argument %qs at %L for intrinsic "
2991 "matmul", gfc_current_intrinsic_arg[0]->name,
2992 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2993 return false;
2994 }
2995 break;
2996
2997 default:
2998 gfc_error ("%qs argument of %qs intrinsic at %L must be of rank "
2999 "1 or 2", gfc_current_intrinsic_arg[0]->name,
3000 gfc_current_intrinsic, &matrix_a->where);
3001 return false;
3002 }
3003
3004 return true;
3005 }
3006
3007
3008 /* Whoever came up with this interface was probably on something.
3009 The possibilities for the occupation of the second and third
3010 parameters are:
3011
3012 Arg #2 Arg #3
3013 NULL NULL
3014 DIM NULL
3015 MASK NULL
3016 NULL MASK minloc(array, mask=m)
3017 DIM MASK
3018
3019 I.e. in the case of minloc(array,mask), mask will be in the second
3020 position of the argument list and we'll have to fix that up. */
3021
3022 bool
3023 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
3024 {
3025 gfc_expr *a, *m, *d;
3026
3027 a = ap->expr;
3028 if (!int_or_real_check (a, 0) || !array_check (a, 0))
3029 return false;
3030
3031 d = ap->next->expr;
3032 m = ap->next->next->expr;
3033
3034 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3035 && ap->next->name == NULL)
3036 {
3037 m = d;
3038 d = NULL;
3039 ap->next->expr = NULL;
3040 ap->next->next->expr = m;
3041 }
3042
3043 if (!dim_check (d, 1, false))
3044 return false;
3045
3046 if (!dim_rank_check (d, a, 0))
3047 return false;
3048
3049 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3050 return false;
3051
3052 if (m != NULL
3053 && !gfc_check_conformance (a, m,
3054 "arguments '%s' and '%s' for intrinsic %s",
3055 gfc_current_intrinsic_arg[0]->name,
3056 gfc_current_intrinsic_arg[2]->name,
3057 gfc_current_intrinsic))
3058 return false;
3059
3060 return true;
3061 }
3062
3063
3064 /* Similar to minloc/maxloc, the argument list might need to be
3065 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
3066 difference is that MINLOC/MAXLOC take an additional KIND argument.
3067 The possibilities are:
3068
3069 Arg #2 Arg #3
3070 NULL NULL
3071 DIM NULL
3072 MASK NULL
3073 NULL MASK minval(array, mask=m)
3074 DIM MASK
3075
3076 I.e. in the case of minval(array,mask), mask will be in the second
3077 position of the argument list and we'll have to fix that up. */
3078
3079 static bool
3080 check_reduction (gfc_actual_arglist *ap)
3081 {
3082 gfc_expr *a, *m, *d;
3083
3084 a = ap->expr;
3085 d = ap->next->expr;
3086 m = ap->next->next->expr;
3087
3088 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
3089 && ap->next->name == NULL)
3090 {
3091 m = d;
3092 d = NULL;
3093 ap->next->expr = NULL;
3094 ap->next->next->expr = m;
3095 }
3096
3097 if (!dim_check (d, 1, false))
3098 return false;
3099
3100 if (!dim_rank_check (d, a, 0))
3101 return false;
3102
3103 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
3104 return false;
3105
3106 if (m != NULL
3107 && !gfc_check_conformance (a, m,
3108 "arguments '%s' and '%s' for intrinsic %s",
3109 gfc_current_intrinsic_arg[0]->name,
3110 gfc_current_intrinsic_arg[2]->name,
3111 gfc_current_intrinsic))
3112 return false;
3113
3114 return true;
3115 }
3116
3117
3118 bool
3119 gfc_check_minval_maxval (gfc_actual_arglist *ap)
3120 {
3121 if (!int_or_real_check (ap->expr, 0)
3122 || !array_check (ap->expr, 0))
3123 return false;
3124
3125 return check_reduction (ap);
3126 }
3127
3128
3129 bool
3130 gfc_check_product_sum (gfc_actual_arglist *ap)
3131 {
3132 if (!numeric_check (ap->expr, 0)
3133 || !array_check (ap->expr, 0))
3134 return false;
3135
3136 return check_reduction (ap);
3137 }
3138
3139
3140 /* For IANY, IALL and IPARITY. */
3141
3142 bool
3143 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
3144 {
3145 int k;
3146
3147 if (!type_check (i, 0, BT_INTEGER))
3148 return false;
3149
3150 if (!nonnegative_check ("I", i))
3151 return false;
3152
3153 if (!kind_check (kind, 1, BT_INTEGER))
3154 return false;
3155
3156 if (kind)
3157 gfc_extract_int (kind, &k);
3158 else
3159 k = gfc_default_integer_kind;
3160
3161 if (!less_than_bitsizekind ("I", i, k))
3162 return false;
3163
3164 return true;
3165 }
3166
3167
3168 bool
3169 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
3170 {
3171 if (ap->expr->ts.type != BT_INTEGER)
3172 {
3173 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER",
3174 gfc_current_intrinsic_arg[0]->name,
3175 gfc_current_intrinsic, &ap->expr->where);
3176 return false;
3177 }
3178
3179 if (!array_check (ap->expr, 0))
3180 return false;
3181
3182 return check_reduction (ap);
3183 }
3184
3185
3186 bool
3187 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
3188 {
3189 if (!same_type_check (tsource, 0, fsource, 1))
3190 return false;
3191
3192 if (!type_check (mask, 2, BT_LOGICAL))
3193 return false;
3194
3195 if (tsource->ts.type == BT_CHARACTER)
3196 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
3197
3198 return true;
3199 }
3200
3201
3202 bool
3203 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
3204 {
3205 if (!type_check (i, 0, BT_INTEGER))
3206 return false;
3207
3208 if (!type_check (j, 1, BT_INTEGER))
3209 return false;
3210
3211 if (!type_check (mask, 2, BT_INTEGER))
3212 return false;
3213
3214 if (!same_type_check (i, 0, j, 1))
3215 return false;
3216
3217 if (!same_type_check (i, 0, mask, 2))
3218 return false;
3219
3220 return true;
3221 }
3222
3223
3224 bool
3225 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
3226 {
3227 if (!variable_check (from, 0, false))
3228 return false;
3229 if (!allocatable_check (from, 0))
3230 return false;
3231 if (gfc_is_coindexed (from))
3232 {
3233 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
3234 "coindexed", &from->where);
3235 return false;
3236 }
3237
3238 if (!variable_check (to, 1, false))
3239 return false;
3240 if (!allocatable_check (to, 1))
3241 return false;
3242 if (gfc_is_coindexed (to))
3243 {
3244 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
3245 "coindexed", &to->where);
3246 return false;
3247 }
3248
3249 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
3250 {
3251 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
3252 "polymorphic if FROM is polymorphic",
3253 &to->where);
3254 return false;
3255 }
3256
3257 if (!same_type_check (to, 1, from, 0))
3258 return false;
3259
3260 if (to->rank != from->rank)
3261 {
3262 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3263 "must have the same rank %d/%d", &to->where, from->rank,
3264 to->rank);
3265 return false;
3266 }
3267
3268 /* IR F08/0040; cf. 12-006A. */
3269 if (gfc_get_corank (to) != gfc_get_corank (from))
3270 {
3271 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
3272 "must have the same corank %d/%d", &to->where,
3273 gfc_get_corank (from), gfc_get_corank (to));
3274 return false;
3275 }
3276
3277 /* CLASS arguments: Make sure the vtab of from is present. */
3278 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
3279 gfc_find_vtab (&from->ts);
3280
3281 return true;
3282 }
3283
3284
3285 bool
3286 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
3287 {
3288 if (!type_check (x, 0, BT_REAL))
3289 return false;
3290
3291 if (!type_check (s, 1, BT_REAL))
3292 return false;
3293
3294 if (s->expr_type == EXPR_CONSTANT)
3295 {
3296 if (mpfr_sgn (s->value.real) == 0)
3297 {
3298 gfc_error ("Argument %<S%> of NEAREST at %L shall not be zero",
3299 &s->where);
3300 return false;
3301 }
3302 }
3303
3304 return true;
3305 }
3306
3307
3308 bool
3309 gfc_check_new_line (gfc_expr *a)
3310 {
3311 if (!type_check (a, 0, BT_CHARACTER))
3312 return false;
3313
3314 return true;
3315 }
3316
3317
3318 bool
3319 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
3320 {
3321 if (!type_check (array, 0, BT_REAL))
3322 return false;
3323
3324 if (!array_check (array, 0))
3325 return false;
3326
3327 if (!dim_rank_check (dim, array, false))
3328 return false;
3329
3330 return true;
3331 }
3332
3333 bool
3334 gfc_check_null (gfc_expr *mold)
3335 {
3336 symbol_attribute attr;
3337
3338 if (mold == NULL)
3339 return true;
3340
3341 if (!variable_check (mold, 0, true))
3342 return false;
3343
3344 attr = gfc_variable_attr (mold, NULL);
3345
3346 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3347 {
3348 gfc_error ("%qs argument of %qs intrinsic at %L must be a POINTER, "
3349 "ALLOCATABLE or procedure pointer",
3350 gfc_current_intrinsic_arg[0]->name,
3351 gfc_current_intrinsic, &mold->where);
3352 return false;
3353 }
3354
3355 if (attr.allocatable
3356 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3357 "allocatable MOLD at %L", &mold->where))
3358 return false;
3359
3360 /* F2008, C1242. */
3361 if (gfc_is_coindexed (mold))
3362 {
3363 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
3364 "coindexed", gfc_current_intrinsic_arg[0]->name,
3365 gfc_current_intrinsic, &mold->where);
3366 return false;
3367 }
3368
3369 return true;
3370 }
3371
3372
3373 bool
3374 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3375 {
3376 if (!array_check (array, 0))
3377 return false;
3378
3379 if (!type_check (mask, 1, BT_LOGICAL))
3380 return false;
3381
3382 if (!gfc_check_conformance (array, mask,
3383 "arguments '%s' and '%s' for intrinsic '%s'",
3384 gfc_current_intrinsic_arg[0]->name,
3385 gfc_current_intrinsic_arg[1]->name,
3386 gfc_current_intrinsic))
3387 return false;
3388
3389 if (vector != NULL)
3390 {
3391 mpz_t array_size, vector_size;
3392 bool have_array_size, have_vector_size;
3393
3394 if (!same_type_check (array, 0, vector, 2))
3395 return false;
3396
3397 if (!rank_check (vector, 2, 1))
3398 return false;
3399
3400 /* VECTOR requires at least as many elements as MASK
3401 has .TRUE. values. */
3402 have_array_size = gfc_array_size(array, &array_size);
3403 have_vector_size = gfc_array_size(vector, &vector_size);
3404
3405 if (have_vector_size
3406 && (mask->expr_type == EXPR_ARRAY
3407 || (mask->expr_type == EXPR_CONSTANT
3408 && have_array_size)))
3409 {
3410 int mask_true_values = 0;
3411
3412 if (mask->expr_type == EXPR_ARRAY)
3413 {
3414 gfc_constructor *mask_ctor;
3415 mask_ctor = gfc_constructor_first (mask->value.constructor);
3416 while (mask_ctor)
3417 {
3418 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3419 {
3420 mask_true_values = 0;
3421 break;
3422 }
3423
3424 if (mask_ctor->expr->value.logical)
3425 mask_true_values++;
3426
3427 mask_ctor = gfc_constructor_next (mask_ctor);
3428 }
3429 }
3430 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3431 mask_true_values = mpz_get_si (array_size);
3432
3433 if (mpz_get_si (vector_size) < mask_true_values)
3434 {
3435 gfc_error ("%qs argument of %qs intrinsic at %L must "
3436 "provide at least as many elements as there "
3437 "are .TRUE. values in %qs (%ld/%d)",
3438 gfc_current_intrinsic_arg[2]->name,
3439 gfc_current_intrinsic, &vector->where,
3440 gfc_current_intrinsic_arg[1]->name,
3441 mpz_get_si (vector_size), mask_true_values);
3442 return false;
3443 }
3444 }
3445
3446 if (have_array_size)
3447 mpz_clear (array_size);
3448 if (have_vector_size)
3449 mpz_clear (vector_size);
3450 }
3451
3452 return true;
3453 }
3454
3455
3456 bool
3457 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3458 {
3459 if (!type_check (mask, 0, BT_LOGICAL))
3460 return false;
3461
3462 if (!array_check (mask, 0))
3463 return false;
3464
3465 if (!dim_rank_check (dim, mask, false))
3466 return false;
3467
3468 return true;
3469 }
3470
3471
3472 bool
3473 gfc_check_precision (gfc_expr *x)
3474 {
3475 if (!real_or_complex_check (x, 0))
3476 return false;
3477
3478 return true;
3479 }
3480
3481
3482 bool
3483 gfc_check_present (gfc_expr *a)
3484 {
3485 gfc_symbol *sym;
3486
3487 if (!variable_check (a, 0, true))
3488 return false;
3489
3490 sym = a->symtree->n.sym;
3491 if (!sym->attr.dummy)
3492 {
3493 gfc_error ("%qs argument of %qs intrinsic at %L must be of a "
3494 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3495 gfc_current_intrinsic, &a->where);
3496 return false;
3497 }
3498
3499 if (!sym->attr.optional)
3500 {
3501 gfc_error ("%qs argument of %qs intrinsic at %L must be of "
3502 "an OPTIONAL dummy variable",
3503 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3504 &a->where);
3505 return false;
3506 }
3507
3508 /* 13.14.82 PRESENT(A)
3509 ......
3510 Argument. A shall be the name of an optional dummy argument that is
3511 accessible in the subprogram in which the PRESENT function reference
3512 appears... */
3513
3514 if (a->ref != NULL
3515 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3516 && (a->ref->u.ar.type == AR_FULL
3517 || (a->ref->u.ar.type == AR_ELEMENT
3518 && a->ref->u.ar.as->rank == 0))))
3519 {
3520 gfc_error ("%qs argument of %qs intrinsic at %L must not be a "
3521 "subobject of %qs", gfc_current_intrinsic_arg[0]->name,
3522 gfc_current_intrinsic, &a->where, sym->name);
3523 return false;
3524 }
3525
3526 return true;
3527 }
3528
3529
3530 bool
3531 gfc_check_radix (gfc_expr *x)
3532 {
3533 if (!int_or_real_check (x, 0))
3534 return false;
3535
3536 return true;
3537 }
3538
3539
3540 bool
3541 gfc_check_range (gfc_expr *x)
3542 {
3543 if (!numeric_check (x, 0))
3544 return false;
3545
3546 return true;
3547 }
3548
3549
3550 bool
3551 gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
3552 {
3553 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3554 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3555
3556 bool is_variable = true;
3557
3558 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3559 if (a->expr_type == EXPR_FUNCTION)
3560 is_variable = a->value.function.esym
3561 ? a->value.function.esym->result->attr.pointer
3562 : a->symtree->n.sym->result->attr.pointer;
3563
3564 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3565 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3566 || !is_variable)
3567 {
3568 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3569 "object", &a->where);
3570 return false;
3571 }
3572
3573 return true;
3574 }
3575
3576
3577 /* real, float, sngl. */
3578 bool
3579 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3580 {
3581 if (!numeric_check (a, 0))
3582 return false;
3583
3584 if (!kind_check (kind, 1, BT_REAL))
3585 return false;
3586
3587 return true;
3588 }
3589
3590
3591 bool
3592 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3593 {
3594 if (!type_check (path1, 0, BT_CHARACTER))
3595 return false;
3596 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3597 return false;
3598
3599 if (!type_check (path2, 1, BT_CHARACTER))
3600 return false;
3601 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3602 return false;
3603
3604 return true;
3605 }
3606
3607
3608 bool
3609 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3610 {
3611 if (!type_check (path1, 0, BT_CHARACTER))
3612 return false;
3613 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3614 return false;
3615
3616 if (!type_check (path2, 1, BT_CHARACTER))
3617 return false;
3618 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3619 return false;
3620
3621 if (status == NULL)
3622 return true;
3623
3624 if (!type_check (status, 2, BT_INTEGER))
3625 return false;
3626
3627 if (!scalar_check (status, 2))
3628 return false;
3629
3630 return true;
3631 }
3632
3633
3634 bool
3635 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3636 {
3637 if (!type_check (x, 0, BT_CHARACTER))
3638 return false;
3639
3640 if (!scalar_check (x, 0))
3641 return false;
3642
3643 if (!type_check (y, 0, BT_INTEGER))
3644 return false;
3645
3646 if (!scalar_check (y, 1))
3647 return false;
3648
3649 return true;
3650 }
3651
3652
3653 bool
3654 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3655 gfc_expr *pad, gfc_expr *order)
3656 {
3657 mpz_t size;
3658 mpz_t nelems;
3659 int shape_size;
3660
3661 if (!array_check (source, 0))
3662 return false;
3663
3664 if (!rank_check (shape, 1, 1))
3665 return false;
3666
3667 if (!type_check (shape, 1, BT_INTEGER))
3668 return false;
3669
3670 if (!gfc_array_size (shape, &size))
3671 {
3672 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L must be an "
3673 "array of constant size", &shape->where);
3674 return false;
3675 }
3676
3677 shape_size = mpz_get_ui (size);
3678 mpz_clear (size);
3679
3680 if (shape_size <= 0)
3681 {
3682 gfc_error ("%qs argument of %qs intrinsic at %L is empty",
3683 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3684 &shape->where);
3685 return false;
3686 }
3687 else if (shape_size > GFC_MAX_DIMENSIONS)
3688 {
3689 gfc_error ("%<shape%> argument of %<reshape%> intrinsic at %L has more "
3690 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3691 return false;
3692 }
3693 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3694 {
3695 gfc_expr *e;
3696 int i, extent;
3697 for (i = 0; i < shape_size; ++i)
3698 {
3699 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3700 if (e->expr_type != EXPR_CONSTANT)
3701 continue;
3702
3703 gfc_extract_int (e, &extent);
3704 if (extent < 0)
3705 {
3706 gfc_error ("%qs argument of %qs intrinsic at %L has "
3707 "negative element (%d)",
3708 gfc_current_intrinsic_arg[1]->name,
3709 gfc_current_intrinsic, &e->where, extent);
3710 return false;
3711 }
3712 }
3713 }
3714 else if (shape->expr_type == EXPR_VARIABLE && shape->ref
3715 && shape->ref->u.ar.type == AR_FULL && shape->ref->u.ar.dimen == 1
3716 && shape->ref->u.ar.as
3717 && shape->ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
3718 && shape->ref->u.ar.as->lower[0]->ts.type == BT_INTEGER
3719 && shape->ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT
3720 && shape->ref->u.ar.as->upper[0]->ts.type == BT_INTEGER
3721 && shape->symtree->n.sym->attr.flavor == FL_PARAMETER)
3722 {
3723 int i, extent;
3724 gfc_expr *e, *v;
3725
3726 v = shape->symtree->n.sym->value;
3727
3728 for (i = 0; i < shape_size; i++)
3729 {
3730 e = gfc_constructor_lookup_expr (v->value.constructor, i);
3731 if (e == NULL)
3732 break;
3733
3734 gfc_extract_int (e, &extent);
3735
3736 if (extent < 0)
3737 {
3738 gfc_error ("Element %d of actual argument of RESHAPE at %L "
3739 "cannot be negative", i + 1, &shape->where);
3740 return false;
3741 }
3742 }
3743 }
3744
3745 if (pad != NULL)
3746 {
3747 if (!same_type_check (source, 0, pad, 2))
3748 return false;
3749
3750 if (!array_check (pad, 2))
3751 return false;
3752 }
3753
3754 if (order != NULL)
3755 {
3756 if (!array_check (order, 3))
3757 return false;
3758
3759 if (!type_check (order, 3, BT_INTEGER))
3760 return false;
3761
3762 if (order->expr_type == EXPR_ARRAY)
3763 {
3764 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3765 gfc_expr *e;
3766
3767 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3768 perm[i] = 0;
3769
3770 gfc_array_size (order, &size);
3771 order_size = mpz_get_ui (size);
3772 mpz_clear (size);
3773
3774 if (order_size != shape_size)
3775 {
3776 gfc_error ("%qs argument of %qs intrinsic at %L "
3777 "has wrong number of elements (%d/%d)",
3778 gfc_current_intrinsic_arg[3]->name,
3779 gfc_current_intrinsic, &order->where,
3780 order_size, shape_size);
3781 return false;
3782 }
3783
3784 for (i = 1; i <= order_size; ++i)
3785 {
3786 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3787 if (e->expr_type != EXPR_CONSTANT)
3788 continue;
3789
3790 gfc_extract_int (e, &dim);
3791
3792 if (dim < 1 || dim > order_size)
3793 {
3794 gfc_error ("%qs argument of %qs intrinsic at %L "
3795 "has out-of-range dimension (%d)",
3796 gfc_current_intrinsic_arg[3]->name,
3797 gfc_current_intrinsic, &e->where, dim);
3798 return false;
3799 }
3800
3801 if (perm[dim-1] != 0)
3802 {
3803 gfc_error ("%qs argument of %qs intrinsic at %L has "
3804 "invalid permutation of dimensions (dimension "
3805 "%<%d%> duplicated)",
3806 gfc_current_intrinsic_arg[3]->name,
3807 gfc_current_intrinsic, &e->where, dim);
3808 return false;
3809 }
3810
3811 perm[dim-1] = 1;
3812 }
3813 }
3814 }
3815
3816 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3817 && gfc_is_constant_expr (shape)
3818 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3819 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3820 {
3821 /* Check the match in size between source and destination. */
3822 if (gfc_array_size (source, &nelems))
3823 {
3824 gfc_constructor *c;
3825 bool test;
3826
3827
3828 mpz_init_set_ui (size, 1);
3829 for (c = gfc_constructor_first (shape->value.constructor);
3830 c; c = gfc_constructor_next (c))
3831 mpz_mul (size, size, c->expr->value.integer);
3832
3833 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3834 mpz_clear (nelems);
3835 mpz_clear (size);
3836
3837 if (test)
3838 {
3839 gfc_error ("Without padding, there are not enough elements "
3840 "in the intrinsic RESHAPE source at %L to match "
3841 "the shape", &source->where);
3842 return false;
3843 }
3844 }
3845 }
3846
3847 return true;
3848 }
3849
3850
3851 bool
3852 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3853 {
3854 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3855 {
3856 gfc_error ("%qs argument of %qs intrinsic at %L "
3857 "cannot be of type %s",
3858 gfc_current_intrinsic_arg[0]->name,
3859 gfc_current_intrinsic,
3860 &a->where, gfc_typename (&a->ts));
3861 return false;
3862 }
3863
3864 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3865 {
3866 gfc_error ("%qs argument of %qs intrinsic at %L "
3867 "must be of an extensible type",
3868 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3869 &a->where);
3870 return false;
3871 }
3872
3873 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3874 {
3875 gfc_error ("%qs argument of %qs intrinsic at %L "
3876 "cannot be of type %s",
3877 gfc_current_intrinsic_arg[0]->name,
3878 gfc_current_intrinsic,
3879 &b->where, gfc_typename (&b->ts));
3880 return false;
3881 }
3882
3883 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
3884 {
3885 gfc_error ("%qs argument of %qs intrinsic at %L "
3886 "must be of an extensible type",
3887 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3888 &b->where);
3889 return false;
3890 }
3891
3892 return true;
3893 }
3894
3895
3896 bool
3897 gfc_check_scale (gfc_expr *x, gfc_expr *i)
3898 {
3899 if (!type_check (x, 0, BT_REAL))
3900 return false;
3901
3902 if (!type_check (i, 1, BT_INTEGER))
3903 return false;
3904
3905 return true;
3906 }
3907
3908
3909 bool
3910 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
3911 {
3912 if (!type_check (x, 0, BT_CHARACTER))
3913 return false;
3914
3915 if (!type_check (y, 1, BT_CHARACTER))
3916 return false;
3917
3918 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
3919 return false;
3920
3921 if (!kind_check (kind, 3, BT_INTEGER))
3922 return false;
3923 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
3924 "with KIND argument at %L",
3925 gfc_current_intrinsic, &kind->where))
3926 return false;
3927
3928 if (!same_type_check (x, 0, y, 1))
3929 return false;
3930
3931 return true;
3932 }
3933
3934
3935 bool
3936 gfc_check_secnds (gfc_expr *r)
3937 {
3938 if (!type_check (r, 0, BT_REAL))
3939 return false;
3940
3941 if (!kind_value_check (r, 0, 4))
3942 return false;
3943
3944 if (!scalar_check (r, 0))
3945 return false;
3946
3947 return true;
3948 }
3949
3950
3951 bool
3952 gfc_check_selected_char_kind (gfc_expr *name)
3953 {
3954 if (!type_check (name, 0, BT_CHARACTER))
3955 return false;
3956
3957 if (!kind_value_check (name, 0, gfc_default_character_kind))
3958 return false;
3959
3960 if (!scalar_check (name, 0))
3961 return false;
3962
3963 return true;
3964 }
3965
3966
3967 bool
3968 gfc_check_selected_int_kind (gfc_expr *r)
3969 {
3970 if (!type_check (r, 0, BT_INTEGER))
3971 return false;
3972
3973 if (!scalar_check (r, 0))
3974 return false;
3975
3976 return true;
3977 }
3978
3979
3980 bool
3981 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
3982 {
3983 if (p == NULL && r == NULL
3984 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
3985 " neither %<P%> nor %<R%> argument at %L",
3986 gfc_current_intrinsic_where))
3987 return false;
3988
3989 if (p)
3990 {
3991 if (!type_check (p, 0, BT_INTEGER))
3992 return false;
3993
3994 if (!scalar_check (p, 0))
3995 return false;
3996 }
3997
3998 if (r)
3999 {
4000 if (!type_check (r, 1, BT_INTEGER))
4001 return false;
4002
4003 if (!scalar_check (r, 1))
4004 return false;
4005 }
4006
4007 if (radix)
4008 {
4009 if (!type_check (radix, 1, BT_INTEGER))
4010 return false;
4011
4012 if (!scalar_check (radix, 1))
4013 return false;
4014
4015 if (!gfc_notify_std (GFC_STD_F2008, "%qs intrinsic with "
4016 "RADIX argument at %L", gfc_current_intrinsic,
4017 &radix->where))
4018 return false;
4019 }
4020
4021 return true;
4022 }
4023
4024
4025 bool
4026 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
4027 {
4028 if (!type_check (x, 0, BT_REAL))
4029 return false;
4030
4031 if (!type_check (i, 1, BT_INTEGER))
4032 return false;
4033
4034 return true;
4035 }
4036
4037
4038 bool
4039 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
4040 {
4041 gfc_array_ref *ar;
4042
4043 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
4044 return true;
4045
4046 ar = gfc_find_array_ref (source);
4047
4048 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
4049 {
4050 gfc_error ("%<source%> argument of %<shape%> intrinsic at %L must not be "
4051 "an assumed size array", &source->where);
4052 return false;
4053 }
4054
4055 if (!kind_check (kind, 1, BT_INTEGER))
4056 return false;
4057 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4058 "with KIND argument at %L",
4059 gfc_current_intrinsic, &kind->where))
4060 return false;
4061
4062 return true;
4063 }
4064
4065
4066 bool
4067 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
4068 {
4069 if (!type_check (i, 0, BT_INTEGER))
4070 return false;
4071
4072 if (!type_check (shift, 0, BT_INTEGER))
4073 return false;
4074
4075 if (!nonnegative_check ("SHIFT", shift))
4076 return false;
4077
4078 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
4079 return false;
4080
4081 return true;
4082 }
4083
4084
4085 bool
4086 gfc_check_sign (gfc_expr *a, gfc_expr *b)
4087 {
4088 if (!int_or_real_check (a, 0))
4089 return false;
4090
4091 if (!same_type_check (a, 0, b, 1))
4092 return false;
4093
4094 return true;
4095 }
4096
4097
4098 bool
4099 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4100 {
4101 if (!array_check (array, 0))
4102 return false;
4103
4104 if (!dim_check (dim, 1, true))
4105 return false;
4106
4107 if (!dim_rank_check (dim, array, 0))
4108 return false;
4109
4110 if (!kind_check (kind, 2, BT_INTEGER))
4111 return false;
4112 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
4113 "with KIND argument at %L",
4114 gfc_current_intrinsic, &kind->where))
4115 return false;
4116
4117
4118 return true;
4119 }
4120
4121
4122 bool
4123 gfc_check_sizeof (gfc_expr *arg)
4124 {
4125 if (arg->ts.type == BT_PROCEDURE)
4126 {
4127 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a procedure",
4128 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4129 &arg->where);
4130 return false;
4131 }
4132
4133 /* TYPE(*) is acceptable if and only if it uses an array descriptor. */
4134 if (arg->ts.type == BT_ASSUMED
4135 && (arg->symtree->n.sym->as == NULL
4136 || (arg->symtree->n.sym->as->type != AS_ASSUMED_SHAPE
4137 && arg->symtree->n.sym->as->type != AS_DEFERRED
4138 && arg->symtree->n.sym->as->type != AS_ASSUMED_RANK)))
4139 {
4140 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
4141 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4142 &arg->where);
4143 return false;
4144 }
4145
4146 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4147 && arg->symtree->n.sym->as != NULL
4148 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4149 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4150 {
4151 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4152 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4153 gfc_current_intrinsic, &arg->where);
4154 return false;
4155 }
4156
4157 return true;
4158 }
4159
4160
4161 /* Check whether an expression is interoperable. When returning false,
4162 msg is set to a string telling why the expression is not interoperable,
4163 otherwise, it is set to NULL. The msg string can be used in diagnostics.
4164 If c_loc is true, character with len > 1 are allowed (cf. Fortran
4165 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
4166 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
4167 are permitted. */
4168
4169 static bool
4170 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
4171 {
4172 *msg = NULL;
4173
4174 if (expr->ts.type == BT_CLASS)
4175 {
4176 *msg = "Expression is polymorphic";
4177 return false;
4178 }
4179
4180 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
4181 && !expr->ts.u.derived->ts.is_iso_c)
4182 {
4183 *msg = "Expression is a noninteroperable derived type";
4184 return false;
4185 }
4186
4187 if (expr->ts.type == BT_PROCEDURE)
4188 {
4189 *msg = "Procedure unexpected as argument";
4190 return false;
4191 }
4192
4193 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
4194 {
4195 int i;
4196 for (i = 0; gfc_logical_kinds[i].kind; i++)
4197 if (gfc_logical_kinds[i].kind == expr->ts.kind)
4198 return true;
4199 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
4200 return false;
4201 }
4202
4203 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
4204 && expr->ts.kind != 1)
4205 {
4206 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
4207 return false;
4208 }
4209
4210 if (expr->ts.type == BT_CHARACTER) {
4211 if (expr->ts.deferred)
4212 {
4213 /* TS 29113 allows deferred-length strings as dummy arguments,
4214 but it is not an interoperable type. */
4215 *msg = "Expression shall not be a deferred-length string";
4216 return false;
4217 }
4218
4219 if (expr->ts.u.cl && expr->ts.u.cl->length
4220 && !gfc_simplify_expr (expr, 0))
4221 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
4222
4223 if (!c_loc && expr->ts.u.cl
4224 && (!expr->ts.u.cl->length
4225 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
4226 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
4227 {
4228 *msg = "Type shall have a character length of 1";
4229 return false;
4230 }
4231 }
4232
4233 /* Note: The following checks are about interoperatable variables, Fortran
4234 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
4235 is allowed, e.g. assumed-shape arrays with TS 29113. */
4236
4237 if (gfc_is_coarray (expr))
4238 {
4239 *msg = "Coarrays are not interoperable";
4240 return false;
4241 }
4242
4243 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
4244 {
4245 gfc_array_ref *ar = gfc_find_array_ref (expr);
4246 if (ar->type != AR_FULL)
4247 {
4248 *msg = "Only whole-arrays are interoperable";
4249 return false;
4250 }
4251 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
4252 && ar->as->type != AS_ASSUMED_SIZE)
4253 {
4254 *msg = "Only explicit-size and assumed-size arrays are interoperable";
4255 return false;
4256 }
4257 }
4258
4259 return true;
4260 }
4261
4262
4263 bool
4264 gfc_check_c_sizeof (gfc_expr *arg)
4265 {
4266 const char *msg;
4267
4268 if (!is_c_interoperable (arg, &msg, false, false))
4269 {
4270 gfc_error ("%qs argument of %qs intrinsic at %L must be an "
4271 "interoperable data entity: %s",
4272 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4273 &arg->where, msg);
4274 return false;
4275 }
4276
4277 if (arg->ts.type == BT_ASSUMED)
4278 {
4279 gfc_error ("%qs argument of %qs intrinsic at %L shall not be "
4280 "TYPE(*)",
4281 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4282 &arg->where);
4283 return false;
4284 }
4285
4286 if (arg->rank && arg->expr_type == EXPR_VARIABLE
4287 && arg->symtree->n.sym->as != NULL
4288 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
4289 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
4290 {
4291 gfc_error ("%qs argument of %qs intrinsic at %L shall not be an "
4292 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
4293 gfc_current_intrinsic, &arg->where);
4294 return false;
4295 }
4296
4297 return true;
4298 }
4299
4300
4301 bool
4302 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
4303 {
4304 if (c_ptr_1->ts.type != BT_DERIVED
4305 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4306 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
4307 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
4308 {
4309 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
4310 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
4311 return false;
4312 }
4313
4314 if (!scalar_check (c_ptr_1, 0))
4315 return false;
4316
4317 if (c_ptr_2
4318 && (c_ptr_2->ts.type != BT_DERIVED
4319 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4320 || (c_ptr_1->ts.u.derived->intmod_sym_id
4321 != c_ptr_2->ts.u.derived->intmod_sym_id)))
4322 {
4323 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
4324 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
4325 gfc_typename (&c_ptr_1->ts),
4326 gfc_typename (&c_ptr_2->ts));
4327 return false;
4328 }
4329
4330 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
4331 return false;
4332
4333 return true;
4334 }
4335
4336
4337 bool
4338 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
4339 {
4340 symbol_attribute attr;
4341 const char *msg;
4342
4343 if (cptr->ts.type != BT_DERIVED
4344 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4345 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
4346 {
4347 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
4348 "type TYPE(C_PTR)", &cptr->where);
4349 return false;
4350 }
4351
4352 if (!scalar_check (cptr, 0))
4353 return false;
4354
4355 attr = gfc_expr_attr (fptr);
4356
4357 if (!attr.pointer)
4358 {
4359 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4360 &fptr->where);
4361 return false;
4362 }
4363
4364 if (fptr->ts.type == BT_CLASS)
4365 {
4366 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4367 &fptr->where);
4368 return false;
4369 }
4370
4371 if (gfc_is_coindexed (fptr))
4372 {
4373 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4374 "coindexed", &fptr->where);
4375 return false;
4376 }
4377
4378 if (fptr->rank == 0 && shape)
4379 {
4380 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4381 "FPTR", &fptr->where);
4382 return false;
4383 }
4384 else if (fptr->rank && !shape)
4385 {
4386 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4387 "FPTR at %L", &fptr->where);
4388 return false;
4389 }
4390
4391 if (shape && !rank_check (shape, 2, 1))
4392 return false;
4393
4394 if (shape && !type_check (shape, 2, BT_INTEGER))
4395 return false;
4396
4397 if (shape)
4398 {
4399 mpz_t size;
4400 if (gfc_array_size (shape, &size))
4401 {
4402 if (mpz_cmp_ui (size, fptr->rank) != 0)
4403 {
4404 mpz_clear (size);
4405 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4406 "size as the RANK of FPTR", &shape->where);
4407 return false;
4408 }
4409 mpz_clear (size);
4410 }
4411 }
4412
4413 if (fptr->ts.type == BT_CLASS)
4414 {
4415 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4416 return false;
4417 }
4418
4419 if (!is_c_interoperable (fptr, &msg, false, true))
4420 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4421 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4422
4423 return true;
4424 }
4425
4426
4427 bool
4428 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4429 {
4430 symbol_attribute attr;
4431
4432 if (cptr->ts.type != BT_DERIVED
4433 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4434 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4435 {
4436 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4437 "type TYPE(C_FUNPTR)", &cptr->where);
4438 return false;
4439 }
4440
4441 if (!scalar_check (cptr, 0))
4442 return false;
4443
4444 attr = gfc_expr_attr (fptr);
4445
4446 if (!attr.proc_pointer)
4447 {
4448 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4449 "pointer", &fptr->where);
4450 return false;
4451 }
4452
4453 if (gfc_is_coindexed (fptr))
4454 {
4455 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4456 "coindexed", &fptr->where);
4457 return false;
4458 }
4459
4460 if (!attr.is_bind_c)
4461 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4462 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4463
4464 return true;
4465 }
4466
4467
4468 bool
4469 gfc_check_c_funloc (gfc_expr *x)
4470 {
4471 symbol_attribute attr;
4472
4473 if (gfc_is_coindexed (x))
4474 {
4475 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4476 "coindexed", &x->where);
4477 return false;
4478 }
4479
4480 attr = gfc_expr_attr (x);
4481
4482 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4483 && x->symtree->n.sym == x->symtree->n.sym->result)
4484 {
4485 gfc_namespace *ns = gfc_current_ns;
4486
4487 for (ns = gfc_current_ns; ns; ns = ns->parent)
4488 if (x->symtree->n.sym == ns->proc_name)
4489 {
4490 gfc_error ("Function result %qs at %L is invalid as X argument "
4491 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4492 return false;
4493 }
4494 }
4495
4496 if (attr.flavor != FL_PROCEDURE)
4497 {
4498 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4499 "or a procedure pointer", &x->where);
4500 return false;
4501 }
4502
4503 if (!attr.is_bind_c)
4504 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4505 "at %L to C_FUNLOC", &x->where);
4506 return true;
4507 }
4508
4509
4510 bool
4511 gfc_check_c_loc (gfc_expr *x)
4512 {
4513 symbol_attribute attr;
4514 const char *msg;
4515
4516 if (gfc_is_coindexed (x))
4517 {
4518 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4519 return false;
4520 }
4521
4522 if (x->ts.type == BT_CLASS)
4523 {
4524 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4525 &x->where);
4526 return false;
4527 }
4528
4529 attr = gfc_expr_attr (x);
4530
4531 if (!attr.pointer
4532 && (x->expr_type != EXPR_VARIABLE || !attr.target
4533 || attr.flavor == FL_PARAMETER))
4534 {
4535 gfc_error ("Argument X at %L to C_LOC shall have either "
4536 "the POINTER or the TARGET attribute", &x->where);
4537 return false;
4538 }
4539
4540 if (x->ts.type == BT_CHARACTER
4541 && gfc_var_strlen (x) == 0)
4542 {
4543 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4544 "string", &x->where);
4545 return false;
4546 }
4547
4548 if (!is_c_interoperable (x, &msg, true, false))
4549 {
4550 if (x->ts.type == BT_CLASS)
4551 {
4552 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4553 &x->where);
4554 return false;
4555 }
4556
4557 if (x->rank
4558 && !gfc_notify_std (GFC_STD_F2008_TS,
4559 "Noninteroperable array at %L as"
4560 " argument to C_LOC: %s", &x->where, msg))
4561 return false;
4562 }
4563 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4564 {
4565 gfc_array_ref *ar = gfc_find_array_ref (x);
4566
4567 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4568 && !attr.allocatable
4569 && !gfc_notify_std (GFC_STD_F2008,
4570 "Array of interoperable type at %L "
4571 "to C_LOC which is nonallocatable and neither "
4572 "assumed size nor explicit size", &x->where))
4573 return false;
4574 else if (ar->type != AR_FULL
4575 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4576 "to C_LOC", &x->where))
4577 return false;
4578 }
4579
4580 return true;
4581 }
4582
4583
4584 bool
4585 gfc_check_sleep_sub (gfc_expr *seconds)
4586 {
4587 if (!type_check (seconds, 0, BT_INTEGER))
4588 return false;
4589
4590 if (!scalar_check (seconds, 0))
4591 return false;
4592
4593 return true;
4594 }
4595
4596 bool
4597 gfc_check_sngl (gfc_expr *a)
4598 {
4599 if (!type_check (a, 0, BT_REAL))
4600 return false;
4601
4602 if ((a->ts.kind != gfc_default_double_kind)
4603 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4604 "REAL argument to %s intrinsic at %L",
4605 gfc_current_intrinsic, &a->where))
4606 return false;
4607
4608 return true;
4609 }
4610
4611 bool
4612 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4613 {
4614 if (source->rank >= GFC_MAX_DIMENSIONS)
4615 {
4616 gfc_error ("%qs argument of %qs intrinsic at %L must be less "
4617 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4618 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4619
4620 return false;
4621 }
4622
4623 if (dim == NULL)
4624 return false;
4625
4626 if (!dim_check (dim, 1, false))
4627 return false;
4628
4629 /* dim_rank_check() does not apply here. */
4630 if (dim
4631 && dim->expr_type == EXPR_CONSTANT
4632 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4633 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4634 {
4635 gfc_error ("%qs argument of %qs intrinsic at %L is not a valid "
4636 "dimension index", gfc_current_intrinsic_arg[1]->name,
4637 gfc_current_intrinsic, &dim->where);
4638 return false;
4639 }
4640
4641 if (!type_check (ncopies, 2, BT_INTEGER))
4642 return false;
4643
4644 if (!scalar_check (ncopies, 2))
4645 return false;
4646
4647 return true;
4648 }
4649
4650
4651 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4652 functions). */
4653
4654 bool
4655 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4656 {
4657 if (!type_check (unit, 0, BT_INTEGER))
4658 return false;
4659
4660 if (!scalar_check (unit, 0))
4661 return false;
4662
4663 if (!type_check (c, 1, BT_CHARACTER))
4664 return false;
4665 if (!kind_value_check (c, 1, gfc_default_character_kind))
4666 return false;
4667
4668 if (status == NULL)
4669 return true;
4670
4671 if (!type_check (status, 2, BT_INTEGER)
4672 || !kind_value_check (status, 2, gfc_default_integer_kind)
4673 || !scalar_check (status, 2))
4674 return false;
4675
4676 return true;
4677 }
4678
4679
4680 bool
4681 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4682 {
4683 return gfc_check_fgetputc_sub (unit, c, NULL);
4684 }
4685
4686
4687 bool
4688 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4689 {
4690 if (!type_check (c, 0, BT_CHARACTER))
4691 return false;
4692 if (!kind_value_check (c, 0, gfc_default_character_kind))
4693 return false;
4694
4695 if (status == NULL)
4696 return true;
4697
4698 if (!type_check (status, 1, BT_INTEGER)
4699 || !kind_value_check (status, 1, gfc_default_integer_kind)
4700 || !scalar_check (status, 1))
4701 return false;
4702
4703 return true;
4704 }
4705
4706
4707 bool
4708 gfc_check_fgetput (gfc_expr *c)
4709 {
4710 return gfc_check_fgetput_sub (c, NULL);
4711 }
4712
4713
4714 bool
4715 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4716 {
4717 if (!type_check (unit, 0, BT_INTEGER))
4718 return false;
4719
4720 if (!scalar_check (unit, 0))
4721 return false;
4722
4723 if (!type_check (offset, 1, BT_INTEGER))
4724 return false;
4725
4726 if (!scalar_check (offset, 1))
4727 return false;
4728
4729 if (!type_check (whence, 2, BT_INTEGER))
4730 return false;
4731
4732 if (!scalar_check (whence, 2))
4733 return false;
4734
4735 if (status == NULL)
4736 return true;
4737
4738 if (!type_check (status, 3, BT_INTEGER))
4739 return false;
4740
4741 if (!kind_value_check (status, 3, 4))
4742 return false;
4743
4744 if (!scalar_check (status, 3))
4745 return false;
4746
4747 return true;
4748 }
4749
4750
4751
4752 bool
4753 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4754 {
4755 if (!type_check (unit, 0, BT_INTEGER))
4756 return false;
4757
4758 if (!scalar_check (unit, 0))
4759 return false;
4760
4761 if (!type_check (array, 1, BT_INTEGER)
4762 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4763 return false;
4764
4765 if (!array_check (array, 1))
4766 return false;
4767
4768 return true;
4769 }
4770
4771
4772 bool
4773 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4774 {
4775 if (!type_check (unit, 0, BT_INTEGER))
4776 return false;
4777
4778 if (!scalar_check (unit, 0))
4779 return false;
4780
4781 if (!type_check (array, 1, BT_INTEGER)
4782 || !kind_value_check (array, 1, gfc_default_integer_kind))
4783 return false;
4784
4785 if (!array_check (array, 1))
4786 return false;
4787
4788 if (status == NULL)
4789 return true;
4790
4791 if (!type_check (status, 2, BT_INTEGER)
4792 || !kind_value_check (status, 2, gfc_default_integer_kind))
4793 return false;
4794
4795 if (!scalar_check (status, 2))
4796 return false;
4797
4798 return true;
4799 }
4800
4801
4802 bool
4803 gfc_check_ftell (gfc_expr *unit)
4804 {
4805 if (!type_check (unit, 0, BT_INTEGER))
4806 return false;
4807
4808 if (!scalar_check (unit, 0))
4809 return false;
4810
4811 return true;
4812 }
4813
4814
4815 bool
4816 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4817 {
4818 if (!type_check (unit, 0, BT_INTEGER))
4819 return false;
4820
4821 if (!scalar_check (unit, 0))
4822 return false;
4823
4824 if (!type_check (offset, 1, BT_INTEGER))
4825 return false;
4826
4827 if (!scalar_check (offset, 1))
4828 return false;
4829
4830 return true;
4831 }
4832
4833
4834 bool
4835 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4836 {
4837 if (!type_check (name, 0, BT_CHARACTER))
4838 return false;
4839 if (!kind_value_check (name, 0, gfc_default_character_kind))
4840 return false;
4841
4842 if (!type_check (array, 1, BT_INTEGER)
4843 || !kind_value_check (array, 1, gfc_default_integer_kind))
4844 return false;
4845
4846 if (!array_check (array, 1))
4847 return false;
4848
4849 return true;
4850 }
4851
4852
4853 bool
4854 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4855 {
4856 if (!type_check (name, 0, BT_CHARACTER))
4857 return false;
4858 if (!kind_value_check (name, 0, gfc_default_character_kind))
4859 return false;
4860
4861 if (!type_check (array, 1, BT_INTEGER)
4862 || !kind_value_check (array, 1, gfc_default_integer_kind))
4863 return false;
4864
4865 if (!array_check (array, 1))
4866 return false;
4867
4868 if (status == NULL)
4869 return true;
4870
4871 if (!type_check (status, 2, BT_INTEGER)
4872 || !kind_value_check (array, 1, gfc_default_integer_kind))
4873 return false;
4874
4875 if (!scalar_check (status, 2))
4876 return false;
4877
4878 return true;
4879 }
4880
4881
4882 bool
4883 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
4884 {
4885 mpz_t nelems;
4886
4887 if (flag_coarray == GFC_FCOARRAY_NONE)
4888 {
4889 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
4890 return false;
4891 }
4892
4893 if (!coarray_check (coarray, 0))
4894 return false;
4895
4896 if (sub->rank != 1)
4897 {
4898 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
4899 gfc_current_intrinsic_arg[1]->name, &sub->where);
4900 return false;
4901 }
4902
4903 if (gfc_array_size (sub, &nelems))
4904 {
4905 int corank = gfc_get_corank (coarray);
4906
4907 if (mpz_cmp_ui (nelems, corank) != 0)
4908 {
4909 gfc_error ("The number of array elements of the SUB argument to "
4910 "IMAGE_INDEX at %L shall be %d (corank) not %d",
4911 &sub->where, corank, (int) mpz_get_si (nelems));
4912 mpz_clear (nelems);
4913 return false;
4914 }
4915 mpz_clear (nelems);
4916 }
4917
4918 return true;
4919 }
4920
4921
4922 bool
4923 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
4924 {
4925 if (flag_coarray == GFC_FCOARRAY_NONE)
4926 {
4927 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
4928 return false;
4929 }
4930
4931 if (distance)
4932 {
4933 if (!type_check (distance, 0, BT_INTEGER))
4934 return false;
4935
4936 if (!nonnegative_check ("DISTANCE", distance))
4937 return false;
4938
4939 if (!scalar_check (distance, 0))
4940 return false;
4941
4942 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
4943 "NUM_IMAGES at %L", &distance->where))
4944 return false;
4945 }
4946
4947 if (failed)
4948 {
4949 if (!type_check (failed, 1, BT_LOGICAL))
4950 return false;
4951
4952 if (!scalar_check (failed, 1))
4953 return false;
4954
4955 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
4956 "NUM_IMAGES at %L", &distance->where))
4957 return false;
4958 }
4959
4960 return true;
4961 }
4962
4963
4964 bool
4965 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
4966 {
4967 if (flag_coarray == GFC_FCOARRAY_NONE)
4968 {
4969 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
4970 return false;
4971 }
4972
4973 if (coarray == NULL && dim == NULL && distance == NULL)
4974 return true;
4975
4976 if (dim != NULL && coarray == NULL)
4977 {
4978 gfc_error ("DIM argument without COARRAY argument not allowed for "
4979 "THIS_IMAGE intrinsic at %L", &dim->where);
4980 return false;
4981 }
4982
4983 if (distance && (coarray || dim))
4984 {
4985 gfc_error ("The DISTANCE argument may not be specified together with the "
4986 "COARRAY or DIM argument in intrinsic at %L",
4987 &distance->where);
4988 return false;
4989 }
4990
4991 /* Assume that we have "this_image (distance)". */
4992 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
4993 {
4994 if (dim)
4995 {
4996 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
4997 &coarray->where);
4998 return false;
4999 }
5000 distance = coarray;
5001 }
5002
5003 if (distance)
5004 {
5005 if (!type_check (distance, 2, BT_INTEGER))
5006 return false;
5007
5008 if (!nonnegative_check ("DISTANCE", distance))
5009 return false;
5010
5011 if (!scalar_check (distance, 2))
5012 return false;
5013
5014 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
5015 "THIS_IMAGE at %L", &distance->where))
5016 return false;
5017
5018 return true;
5019 }
5020
5021 if (!coarray_check (coarray, 0))
5022 return false;
5023
5024 if (dim != NULL)
5025 {
5026 if (!dim_check (dim, 1, false))
5027 return false;
5028
5029 if (!dim_corank_check (dim, coarray))
5030 return false;
5031 }
5032
5033 return true;
5034 }
5035
5036 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
5037 by gfc_simplify_transfer. Return false if we cannot do so. */
5038
5039 bool
5040 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
5041 size_t *source_size, size_t *result_size,
5042 size_t *result_length_p)
5043 {
5044 size_t result_elt_size;
5045
5046 if (source->expr_type == EXPR_FUNCTION)
5047 return false;
5048
5049 if (size && size->expr_type != EXPR_CONSTANT)
5050 return false;
5051
5052 /* Calculate the size of the source. */
5053 *source_size = gfc_target_expr_size (source);
5054 if (*source_size == 0)
5055 return false;
5056
5057 /* Determine the size of the element. */
5058 result_elt_size = gfc_element_size (mold);
5059 if (result_elt_size == 0)
5060 return false;
5061
5062 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
5063 {
5064 int result_length;
5065
5066 if (size)
5067 result_length = (size_t)mpz_get_ui (size->value.integer);
5068 else
5069 {
5070 result_length = *source_size / result_elt_size;
5071 if (result_length * result_elt_size < *source_size)
5072 result_length += 1;
5073 }
5074
5075 *result_size = result_length * result_elt_size;
5076 if (result_length_p)
5077 *result_length_p = result_length;
5078 }
5079 else
5080 *result_size = result_elt_size;
5081
5082 return true;
5083 }
5084
5085
5086 bool
5087 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
5088 {
5089 size_t source_size;
5090 size_t result_size;
5091
5092 if (mold->ts.type == BT_HOLLERITH)
5093 {
5094 gfc_error ("%<MOLD%> argument of %<TRANSFER%> intrinsic at %L must not be"
5095 " %s", &mold->where, gfc_basic_typename (BT_HOLLERITH));
5096 return false;
5097 }
5098
5099 if (size != NULL)
5100 {
5101 if (!type_check (size, 2, BT_INTEGER))
5102 return false;
5103
5104 if (!scalar_check (size, 2))
5105 return false;
5106
5107 if (!nonoptional_check (size, 2))
5108 return false;
5109 }
5110
5111 if (!warn_surprising)
5112 return true;
5113
5114 /* If we can't calculate the sizes, we cannot check any more.
5115 Return true for that case. */
5116
5117 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
5118 &result_size, NULL))
5119 return true;
5120
5121 if (source_size < result_size)
5122 gfc_warning (0, "Intrinsic TRANSFER at %L has partly undefined result: "
5123 "source size %ld < result size %ld", &source->where,
5124 (long) source_size, (long) result_size);
5125
5126 return true;
5127 }
5128
5129
5130 bool
5131 gfc_check_transpose (gfc_expr *matrix)
5132 {
5133 if (!rank_check (matrix, 0, 2))
5134 return false;
5135
5136 return true;
5137 }
5138
5139
5140 bool
5141 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
5142 {
5143 if (!array_check (array, 0))
5144 return false;
5145
5146 if (!dim_check (dim, 1, false))
5147 return false;
5148
5149 if (!dim_rank_check (dim, array, 0))
5150 return false;
5151
5152 if (!kind_check (kind, 2, BT_INTEGER))
5153 return false;
5154 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5155 "with KIND argument at %L",
5156 gfc_current_intrinsic, &kind->where))
5157 return false;
5158
5159 return true;
5160 }
5161
5162
5163 bool
5164 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
5165 {
5166 if (flag_coarray == GFC_FCOARRAY_NONE)
5167 {
5168 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
5169 return false;
5170 }
5171
5172 if (!coarray_check (coarray, 0))
5173 return false;
5174
5175 if (dim != NULL)
5176 {
5177 if (!dim_check (dim, 1, false))
5178 return false;
5179
5180 if (!dim_corank_check (dim, coarray))
5181 return false;
5182 }
5183
5184 if (!kind_check (kind, 2, BT_INTEGER))
5185 return false;
5186
5187 return true;
5188 }
5189
5190
5191 bool
5192 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
5193 {
5194 mpz_t vector_size;
5195
5196 if (!rank_check (vector, 0, 1))
5197 return false;
5198
5199 if (!array_check (mask, 1))
5200 return false;
5201
5202 if (!type_check (mask, 1, BT_LOGICAL))
5203 return false;
5204
5205 if (!same_type_check (vector, 0, field, 2))
5206 return false;
5207
5208 if (mask->expr_type == EXPR_ARRAY
5209 && gfc_array_size (vector, &vector_size))
5210 {
5211 int mask_true_count = 0;
5212 gfc_constructor *mask_ctor;
5213 mask_ctor = gfc_constructor_first (mask->value.constructor);
5214 while (mask_ctor)
5215 {
5216 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
5217 {
5218 mask_true_count = 0;
5219 break;
5220 }
5221
5222 if (mask_ctor->expr->value.logical)
5223 mask_true_count++;
5224
5225 mask_ctor = gfc_constructor_next (mask_ctor);
5226 }
5227
5228 if (mpz_get_si (vector_size) < mask_true_count)
5229 {
5230 gfc_error ("%qs argument of %qs intrinsic at %L must "
5231 "provide at least as many elements as there "
5232 "are .TRUE. values in %qs (%ld/%d)",
5233 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5234 &vector->where, gfc_current_intrinsic_arg[1]->name,
5235 mpz_get_si (vector_size), mask_true_count);
5236 return false;
5237 }
5238
5239 mpz_clear (vector_size);
5240 }
5241
5242 if (mask->rank != field->rank && field->rank != 0)
5243 {
5244 gfc_error ("%qs argument of %qs intrinsic at %L must have "
5245 "the same rank as %qs or be a scalar",
5246 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5247 &field->where, gfc_current_intrinsic_arg[1]->name);
5248 return false;
5249 }
5250
5251 if (mask->rank == field->rank)
5252 {
5253 int i;
5254 for (i = 0; i < field->rank; i++)
5255 if (! identical_dimen_shape (mask, i, field, i))
5256 {
5257 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L "
5258 "must have identical shape.",
5259 gfc_current_intrinsic_arg[2]->name,
5260 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5261 &field->where);
5262 }
5263 }
5264
5265 return true;
5266 }
5267
5268
5269 bool
5270 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
5271 {
5272 if (!type_check (x, 0, BT_CHARACTER))
5273 return false;
5274
5275 if (!same_type_check (x, 0, y, 1))
5276 return false;
5277
5278 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
5279 return false;
5280
5281 if (!kind_check (kind, 3, BT_INTEGER))
5282 return false;
5283 if (kind && !gfc_notify_std (GFC_STD_F2003, "%qs intrinsic "
5284 "with KIND argument at %L",
5285 gfc_current_intrinsic, &kind->where))
5286 return false;
5287
5288 return true;
5289 }
5290
5291
5292 bool
5293 gfc_check_trim (gfc_expr *x)
5294 {
5295 if (!type_check (x, 0, BT_CHARACTER))
5296 return false;
5297
5298 if (!scalar_check (x, 0))
5299 return false;
5300
5301 return true;
5302 }
5303
5304
5305 bool
5306 gfc_check_ttynam (gfc_expr *unit)
5307 {
5308 if (!scalar_check (unit, 0))
5309 return false;
5310
5311 if (!type_check (unit, 0, BT_INTEGER))
5312 return false;
5313
5314 return true;
5315 }
5316
5317
5318 /* Common check function for the half a dozen intrinsics that have a
5319 single real argument. */
5320
5321 bool
5322 gfc_check_x (gfc_expr *x)
5323 {
5324 if (!type_check (x, 0, BT_REAL))
5325 return false;
5326
5327 return true;
5328 }
5329
5330
5331 /************* Check functions for intrinsic subroutines *************/
5332
5333 bool
5334 gfc_check_cpu_time (gfc_expr *time)
5335 {
5336 if (!scalar_check (time, 0))
5337 return false;
5338
5339 if (!type_check (time, 0, BT_REAL))
5340 return false;
5341
5342 if (!variable_check (time, 0, false))
5343 return false;
5344
5345 return true;
5346 }
5347
5348
5349 bool
5350 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
5351 gfc_expr *zone, gfc_expr *values)
5352 {
5353 if (date != NULL)
5354 {
5355 if (!type_check (date, 0, BT_CHARACTER))
5356 return false;
5357 if (!kind_value_check (date, 0, gfc_default_character_kind))
5358 return false;
5359 if (!scalar_check (date, 0))
5360 return false;
5361 if (!variable_check (date, 0, false))
5362 return false;
5363 }
5364
5365 if (time != NULL)
5366 {
5367 if (!type_check (time, 1, BT_CHARACTER))
5368 return false;
5369 if (!kind_value_check (time, 1, gfc_default_character_kind))
5370 return false;
5371 if (!scalar_check (time, 1))
5372 return false;
5373 if (!variable_check (time, 1, false))
5374 return false;
5375 }
5376
5377 if (zone != NULL)
5378 {
5379 if (!type_check (zone, 2, BT_CHARACTER))
5380 return false;
5381 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5382 return false;
5383 if (!scalar_check (zone, 2))
5384 return false;
5385 if (!variable_check (zone, 2, false))
5386 return false;
5387 }
5388
5389 if (values != NULL)
5390 {
5391 if (!type_check (values, 3, BT_INTEGER))
5392 return false;
5393 if (!array_check (values, 3))
5394 return false;
5395 if (!rank_check (values, 3, 1))
5396 return false;
5397 if (!variable_check (values, 3, false))
5398 return false;
5399 }
5400
5401 return true;
5402 }
5403
5404
5405 bool
5406 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5407 gfc_expr *to, gfc_expr *topos)
5408 {
5409 if (!type_check (from, 0, BT_INTEGER))
5410 return false;
5411
5412 if (!type_check (frompos, 1, BT_INTEGER))
5413 return false;
5414
5415 if (!type_check (len, 2, BT_INTEGER))
5416 return false;
5417
5418 if (!same_type_check (from, 0, to, 3))
5419 return false;
5420
5421 if (!variable_check (to, 3, false))
5422 return false;
5423
5424 if (!type_check (topos, 4, BT_INTEGER))
5425 return false;
5426
5427 if (!nonnegative_check ("frompos", frompos))
5428 return false;
5429
5430 if (!nonnegative_check ("topos", topos))
5431 return false;
5432
5433 if (!nonnegative_check ("len", len))
5434 return false;
5435
5436 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5437 return false;
5438
5439 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5440 return false;
5441
5442 return true;
5443 }
5444
5445
5446 bool
5447 gfc_check_random_number (gfc_expr *harvest)
5448 {
5449 if (!type_check (harvest, 0, BT_REAL))
5450 return false;
5451
5452 if (!variable_check (harvest, 0, false))
5453 return false;
5454
5455 return true;
5456 }
5457
5458
5459 bool
5460 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5461 {
5462 unsigned int nargs = 0, kiss_size;
5463 locus *where = NULL;
5464 mpz_t put_size, get_size;
5465 bool have_gfc_real_16; /* Try and mimic HAVE_GFC_REAL_16 in libgfortran. */
5466
5467 have_gfc_real_16 = gfc_validate_kind (BT_REAL, 16, true) != -1;
5468
5469 /* Keep the number of bytes in sync with kiss_size in
5470 libgfortran/intrinsics/random.c. */
5471 kiss_size = (have_gfc_real_16 ? 48 : 32) / gfc_default_integer_kind;
5472
5473 if (size != NULL)
5474 {
5475 if (size->expr_type != EXPR_VARIABLE
5476 || !size->symtree->n.sym->attr.optional)
5477 nargs++;
5478
5479 if (!scalar_check (size, 0))
5480 return false;
5481
5482 if (!type_check (size, 0, BT_INTEGER))
5483 return false;
5484
5485 if (!variable_check (size, 0, false))
5486 return false;
5487
5488 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5489 return false;
5490 }
5491
5492 if (put != NULL)
5493 {
5494 if (put->expr_type != EXPR_VARIABLE
5495 || !put->symtree->n.sym->attr.optional)
5496 {
5497 nargs++;
5498 where = &put->where;
5499 }
5500
5501 if (!array_check (put, 1))
5502 return false;
5503
5504 if (!rank_check (put, 1, 1))
5505 return false;
5506
5507 if (!type_check (put, 1, BT_INTEGER))
5508 return false;
5509
5510 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5511 return false;
5512
5513 if (gfc_array_size (put, &put_size)
5514 && mpz_get_ui (put_size) < kiss_size)
5515 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5516 "too small (%i/%i)",
5517 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5518 where, (int) mpz_get_ui (put_size), kiss_size);
5519 }
5520
5521 if (get != NULL)
5522 {
5523 if (get->expr_type != EXPR_VARIABLE
5524 || !get->symtree->n.sym->attr.optional)
5525 {
5526 nargs++;
5527 where = &get->where;
5528 }
5529
5530 if (!array_check (get, 2))
5531 return false;
5532
5533 if (!rank_check (get, 2, 1))
5534 return false;
5535
5536 if (!type_check (get, 2, BT_INTEGER))
5537 return false;
5538
5539 if (!variable_check (get, 2, false))
5540 return false;
5541
5542 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5543 return false;
5544
5545 if (gfc_array_size (get, &get_size)
5546 && mpz_get_ui (get_size) < kiss_size)
5547 gfc_error ("Size of %qs argument of %qs intrinsic at %L "
5548 "too small (%i/%i)",
5549 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5550 where, (int) mpz_get_ui (get_size), kiss_size);
5551 }
5552
5553 /* RANDOM_SEED may not have more than one non-optional argument. */
5554 if (nargs > 1)
5555 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5556
5557 return true;
5558 }
5559
5560 bool
5561 gfc_check_fe_runtime_error (gfc_actual_arglist *a)
5562 {
5563 gfc_expr *e;
5564 int len, i;
5565 int num_percent, nargs;
5566
5567 e = a->expr;
5568 if (e->expr_type != EXPR_CONSTANT)
5569 return true;
5570
5571 len = e->value.character.length;
5572 if (e->value.character.string[len-1] != '\0')
5573 gfc_internal_error ("fe_runtime_error string must be null terminated");
5574
5575 num_percent = 0;
5576 for (i=0; i<len-1; i++)
5577 if (e->value.character.string[i] == '%')
5578 num_percent ++;
5579
5580 nargs = 0;
5581 for (; a; a = a->next)
5582 nargs ++;
5583
5584 if (nargs -1 != num_percent)
5585 gfc_internal_error ("fe_runtime_error: Wrong number of arguments (%d instead of %d)",
5586 nargs, num_percent++);
5587
5588 return true;
5589 }
5590
5591 bool
5592 gfc_check_second_sub (gfc_expr *time)
5593 {
5594 if (!scalar_check (time, 0))
5595 return false;
5596
5597 if (!type_check (time, 0, BT_REAL))
5598 return false;
5599
5600 if (!kind_value_check (time, 0, 4))
5601 return false;
5602
5603 return true;
5604 }
5605
5606
5607 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5608 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5609 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5610 count_max are all optional arguments */
5611
5612 bool
5613 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5614 gfc_expr *count_max)
5615 {
5616 if (count != NULL)
5617 {
5618 if (!scalar_check (count, 0))
5619 return false;
5620
5621 if (!type_check (count, 0, BT_INTEGER))
5622 return false;
5623
5624 if (count->ts.kind != gfc_default_integer_kind
5625 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5626 "SYSTEM_CLOCK at %L has non-default kind",
5627 &count->where))
5628 return false;
5629
5630 if (!variable_check (count, 0, false))
5631 return false;
5632 }
5633
5634 if (count_rate != NULL)
5635 {
5636 if (!scalar_check (count_rate, 1))
5637 return false;
5638
5639 if (!variable_check (count_rate, 1, false))
5640 return false;
5641
5642 if (count_rate->ts.type == BT_REAL)
5643 {
5644 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5645 "SYSTEM_CLOCK at %L", &count_rate->where))
5646 return false;
5647 }
5648 else
5649 {
5650 if (!type_check (count_rate, 1, BT_INTEGER))
5651 return false;
5652
5653 if (count_rate->ts.kind != gfc_default_integer_kind
5654 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5655 "SYSTEM_CLOCK at %L has non-default kind",
5656 &count_rate->where))
5657 return false;
5658 }
5659
5660 }
5661
5662 if (count_max != NULL)
5663 {
5664 if (!scalar_check (count_max, 2))
5665 return false;
5666
5667 if (!type_check (count_max, 2, BT_INTEGER))
5668 return false;
5669
5670 if (count_max->ts.kind != gfc_default_integer_kind
5671 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5672 "SYSTEM_CLOCK at %L has non-default kind",
5673 &count_max->where))
5674 return false;
5675
5676 if (!variable_check (count_max, 2, false))
5677 return false;
5678 }
5679
5680 return true;
5681 }
5682
5683
5684 bool
5685 gfc_check_irand (gfc_expr *x)
5686 {
5687 if (x == NULL)
5688 return true;
5689
5690 if (!scalar_check (x, 0))
5691 return false;
5692
5693 if (!type_check (x, 0, BT_INTEGER))
5694 return false;
5695
5696 if (!kind_value_check (x, 0, 4))
5697 return false;
5698
5699 return true;
5700 }
5701
5702
5703 bool
5704 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5705 {
5706 if (!scalar_check (seconds, 0))
5707 return false;
5708 if (!type_check (seconds, 0, BT_INTEGER))
5709 return false;
5710
5711 if (!int_or_proc_check (handler, 1))
5712 return false;
5713 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5714 return false;
5715
5716 if (status == NULL)
5717 return true;
5718
5719 if (!scalar_check (status, 2))
5720 return false;
5721 if (!type_check (status, 2, BT_INTEGER))
5722 return false;
5723 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5724 return false;
5725
5726 return true;
5727 }
5728
5729
5730 bool
5731 gfc_check_rand (gfc_expr *x)
5732 {
5733 if (x == NULL)
5734 return true;
5735
5736 if (!scalar_check (x, 0))
5737 return false;
5738
5739 if (!type_check (x, 0, BT_INTEGER))
5740 return false;
5741
5742 if (!kind_value_check (x, 0, 4))
5743 return false;
5744
5745 return true;
5746 }
5747
5748
5749 bool
5750 gfc_check_srand (gfc_expr *x)
5751 {
5752 if (!scalar_check (x, 0))
5753 return false;
5754
5755 if (!type_check (x, 0, BT_INTEGER))
5756 return false;
5757
5758 if (!kind_value_check (x, 0, 4))
5759 return false;
5760
5761 return true;
5762 }
5763
5764
5765 bool
5766 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5767 {
5768 if (!scalar_check (time, 0))
5769 return false;
5770 if (!type_check (time, 0, BT_INTEGER))
5771 return false;
5772
5773 if (!type_check (result, 1, BT_CHARACTER))
5774 return false;
5775 if (!kind_value_check (result, 1, gfc_default_character_kind))
5776 return false;
5777
5778 return true;
5779 }
5780
5781
5782 bool
5783 gfc_check_dtime_etime (gfc_expr *x)
5784 {
5785 if (!array_check (x, 0))
5786 return false;
5787
5788 if (!rank_check (x, 0, 1))
5789 return false;
5790
5791 if (!variable_check (x, 0, false))
5792 return false;
5793
5794 if (!type_check (x, 0, BT_REAL))
5795 return false;
5796
5797 if (!kind_value_check (x, 0, 4))
5798 return false;
5799
5800 return true;
5801 }
5802
5803
5804 bool
5805 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5806 {
5807 if (!array_check (values, 0))
5808 return false;
5809
5810 if (!rank_check (values, 0, 1))
5811 return false;
5812
5813 if (!variable_check (values, 0, false))
5814 return false;
5815
5816 if (!type_check (values, 0, BT_REAL))
5817 return false;
5818
5819 if (!kind_value_check (values, 0, 4))
5820 return false;
5821
5822 if (!scalar_check (time, 1))
5823 return false;
5824
5825 if (!type_check (time, 1, BT_REAL))
5826 return false;
5827
5828 if (!kind_value_check (time, 1, 4))
5829 return false;
5830
5831 return true;
5832 }
5833
5834
5835 bool
5836 gfc_check_fdate_sub (gfc_expr *date)
5837 {
5838 if (!type_check (date, 0, BT_CHARACTER))
5839 return false;
5840 if (!kind_value_check (date, 0, gfc_default_character_kind))
5841 return false;
5842
5843 return true;
5844 }
5845
5846
5847 bool
5848 gfc_check_gerror (gfc_expr *msg)
5849 {
5850 if (!type_check (msg, 0, BT_CHARACTER))
5851 return false;
5852 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5853 return false;
5854
5855 return true;
5856 }
5857
5858
5859 bool
5860 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5861 {
5862 if (!type_check (cwd, 0, BT_CHARACTER))
5863 return false;
5864 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5865 return false;
5866
5867 if (status == NULL)
5868 return true;
5869
5870 if (!scalar_check (status, 1))
5871 return false;
5872
5873 if (!type_check (status, 1, BT_INTEGER))
5874 return false;
5875
5876 return true;
5877 }
5878
5879
5880 bool
5881 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
5882 {
5883 if (!type_check (pos, 0, BT_INTEGER))
5884 return false;
5885
5886 if (pos->ts.kind > gfc_default_integer_kind)
5887 {
5888 gfc_error ("%qs argument of %qs intrinsic at %L must be of a kind "
5889 "not wider than the default kind (%d)",
5890 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5891 &pos->where, gfc_default_integer_kind);
5892 return false;
5893 }
5894
5895 if (!type_check (value, 1, BT_CHARACTER))
5896 return false;
5897 if (!kind_value_check (value, 1, gfc_default_character_kind))
5898 return false;
5899
5900 return true;
5901 }
5902
5903
5904 bool
5905 gfc_check_getlog (gfc_expr *msg)
5906 {
5907 if (!type_check (msg, 0, BT_CHARACTER))
5908 return false;
5909 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5910 return false;
5911
5912 return true;
5913 }
5914
5915
5916 bool
5917 gfc_check_exit (gfc_expr *status)
5918 {
5919 if (status == NULL)
5920 return true;
5921
5922 if (!type_check (status, 0, BT_INTEGER))
5923 return false;
5924
5925 if (!scalar_check (status, 0))
5926 return false;
5927
5928 return true;
5929 }
5930
5931
5932 bool
5933 gfc_check_flush (gfc_expr *unit)
5934 {
5935 if (unit == NULL)
5936 return true;
5937
5938 if (!type_check (unit, 0, BT_INTEGER))
5939 return false;
5940
5941 if (!scalar_check (unit, 0))
5942 return false;
5943
5944 return true;
5945 }
5946
5947
5948 bool
5949 gfc_check_free (gfc_expr *i)
5950 {
5951 if (!type_check (i, 0, BT_INTEGER))
5952 return false;
5953
5954 if (!scalar_check (i, 0))
5955 return false;
5956
5957 return true;
5958 }
5959
5960
5961 bool
5962 gfc_check_hostnm (gfc_expr *name)
5963 {
5964 if (!type_check (name, 0, BT_CHARACTER))
5965 return false;
5966 if (!kind_value_check (name, 0, gfc_default_character_kind))
5967 return false;
5968
5969 return true;
5970 }
5971
5972
5973 bool
5974 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
5975 {
5976 if (!type_check (name, 0, BT_CHARACTER))
5977 return false;
5978 if (!kind_value_check (name, 0, gfc_default_character_kind))
5979 return false;
5980
5981 if (status == NULL)
5982 return true;
5983
5984 if (!scalar_check (status, 1))
5985 return false;
5986
5987 if (!type_check (status, 1, BT_INTEGER))
5988 return false;
5989
5990 return true;
5991 }
5992
5993
5994 bool
5995 gfc_check_itime_idate (gfc_expr *values)
5996 {
5997 if (!array_check (values, 0))
5998 return false;
5999
6000 if (!rank_check (values, 0, 1))
6001 return false;
6002
6003 if (!variable_check (values, 0, false))
6004 return false;
6005
6006 if (!type_check (values, 0, BT_INTEGER))
6007 return false;
6008
6009 if (!kind_value_check (values, 0, gfc_default_integer_kind))
6010 return false;
6011
6012 return true;
6013 }
6014
6015
6016 bool
6017 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
6018 {
6019 if (!type_check (time, 0, BT_INTEGER))
6020 return false;
6021
6022 if (!kind_value_check (time, 0, gfc_default_integer_kind))
6023 return false;
6024
6025 if (!scalar_check (time, 0))
6026 return false;
6027
6028 if (!array_check (values, 1))
6029 return false;
6030
6031 if (!rank_check (values, 1, 1))
6032 return false;
6033
6034 if (!variable_check (values, 1, false))
6035 return false;
6036
6037 if (!type_check (values, 1, BT_INTEGER))
6038 return false;
6039
6040 if (!kind_value_check (values, 1, gfc_default_integer_kind))
6041 return false;
6042
6043 return true;
6044 }
6045
6046
6047 bool
6048 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
6049 {
6050 if (!scalar_check (unit, 0))
6051 return false;
6052
6053 if (!type_check (unit, 0, BT_INTEGER))
6054 return false;
6055
6056 if (!type_check (name, 1, BT_CHARACTER))
6057 return false;
6058 if (!kind_value_check (name, 1, gfc_default_character_kind))
6059 return false;
6060
6061 return true;
6062 }
6063
6064
6065 bool
6066 gfc_check_isatty (gfc_expr *unit)
6067 {
6068 if (unit == NULL)
6069 return false;
6070
6071 if (!type_check (unit, 0, BT_INTEGER))
6072 return false;
6073
6074 if (!scalar_check (unit, 0))
6075 return false;
6076
6077 return true;
6078 }
6079
6080
6081 bool
6082 gfc_check_isnan (gfc_expr *x)
6083 {
6084 if (!type_check (x, 0, BT_REAL))
6085 return false;
6086
6087 return true;
6088 }
6089
6090
6091 bool
6092 gfc_check_perror (gfc_expr *string)
6093 {
6094 if (!type_check (string, 0, BT_CHARACTER))
6095 return false;
6096 if (!kind_value_check (string, 0, gfc_default_character_kind))
6097 return false;
6098
6099 return true;
6100 }
6101
6102
6103 bool
6104 gfc_check_umask (gfc_expr *mask)
6105 {
6106 if (!type_check (mask, 0, BT_INTEGER))
6107 return false;
6108
6109 if (!scalar_check (mask, 0))
6110 return false;
6111
6112 return true;
6113 }
6114
6115
6116 bool
6117 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
6118 {
6119 if (!type_check (mask, 0, BT_INTEGER))
6120 return false;
6121
6122 if (!scalar_check (mask, 0))
6123 return false;
6124
6125 if (old == NULL)
6126 return true;
6127
6128 if (!scalar_check (old, 1))
6129 return false;
6130
6131 if (!type_check (old, 1, BT_INTEGER))
6132 return false;
6133
6134 return true;
6135 }
6136
6137
6138 bool
6139 gfc_check_unlink (gfc_expr *name)
6140 {
6141 if (!type_check (name, 0, BT_CHARACTER))
6142 return false;
6143 if (!kind_value_check (name, 0, gfc_default_character_kind))
6144 return false;
6145
6146 return true;
6147 }
6148
6149
6150 bool
6151 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
6152 {
6153 if (!type_check (name, 0, BT_CHARACTER))
6154 return false;
6155 if (!kind_value_check (name, 0, gfc_default_character_kind))
6156 return false;
6157
6158 if (status == NULL)
6159 return true;
6160
6161 if (!scalar_check (status, 1))
6162 return false;
6163
6164 if (!type_check (status, 1, BT_INTEGER))
6165 return false;
6166
6167 return true;
6168 }
6169
6170
6171 bool
6172 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
6173 {
6174 if (!scalar_check (number, 0))
6175 return false;
6176 if (!type_check (number, 0, BT_INTEGER))
6177 return false;
6178
6179 if (!int_or_proc_check (handler, 1))
6180 return false;
6181 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6182 return false;
6183
6184 return true;
6185 }
6186
6187
6188 bool
6189 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
6190 {
6191 if (!scalar_check (number, 0))
6192 return false;
6193 if (!type_check (number, 0, BT_INTEGER))
6194 return false;
6195
6196 if (!int_or_proc_check (handler, 1))
6197 return false;
6198 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
6199 return false;
6200
6201 if (status == NULL)
6202 return true;
6203
6204 if (!type_check (status, 2, BT_INTEGER))
6205 return false;
6206 if (!scalar_check (status, 2))
6207 return false;
6208
6209 return true;
6210 }
6211
6212
6213 bool
6214 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
6215 {
6216 if (!type_check (cmd, 0, BT_CHARACTER))
6217 return false;
6218 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
6219 return false;
6220
6221 if (!scalar_check (status, 1))
6222 return false;
6223
6224 if (!type_check (status, 1, BT_INTEGER))
6225 return false;
6226
6227 if (!kind_value_check (status, 1, gfc_default_integer_kind))
6228 return false;
6229
6230 return true;
6231 }
6232
6233
6234 /* This is used for the GNU intrinsics AND, OR and XOR. */
6235 bool
6236 gfc_check_and (gfc_expr *i, gfc_expr *j)
6237 {
6238 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
6239 {
6240 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6241 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
6242 gfc_current_intrinsic, &i->where);
6243 return false;
6244 }
6245
6246 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
6247 {
6248 gfc_error ("%qs argument of %qs intrinsic at %L must be INTEGER "
6249 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
6250 gfc_current_intrinsic, &j->where);
6251 return false;
6252 }
6253
6254 if (i->ts.type != j->ts.type)
6255 {
6256 gfc_error ("%qs and %qs arguments of %qs intrinsic at %L must "
6257 "have the same type", gfc_current_intrinsic_arg[0]->name,
6258 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6259 &j->where);
6260 return false;
6261 }
6262
6263 if (!scalar_check (i, 0))
6264 return false;
6265
6266 if (!scalar_check (j, 1))
6267 return false;
6268
6269 return true;
6270 }
6271
6272
6273 bool
6274 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
6275 {
6276
6277 if (a->expr_type == EXPR_NULL)
6278 {
6279 gfc_error ("Intrinsic function NULL at %L cannot be an actual "
6280 "argument to STORAGE_SIZE, because it returns a "
6281 "disassociated pointer", &a->where);
6282 return false;
6283 }
6284
6285 if (a->ts.type == BT_ASSUMED)
6286 {
6287 gfc_error ("%qs argument of %qs intrinsic at %L shall not be TYPE(*)",
6288 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
6289 &a->where);
6290 return false;
6291 }
6292
6293 if (a->ts.type == BT_PROCEDURE)
6294 {
6295 gfc_error ("%qs argument of %qs intrinsic at %L shall not be a "
6296 "procedure", gfc_current_intrinsic_arg[0]->name,
6297 gfc_current_intrinsic, &a->where);
6298 return false;
6299 }
6300
6301 if (kind == NULL)
6302 return true;
6303
6304 if (!type_check (kind, 1, BT_INTEGER))
6305 return false;
6306
6307 if (!scalar_check (kind, 1))
6308 return false;
6309
6310 if (kind->expr_type != EXPR_CONSTANT)
6311 {
6312 gfc_error ("%qs argument of %qs intrinsic at %L must be a constant",
6313 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
6314 &kind->where);
6315 return false;
6316 }
6317
6318 return true;
6319 }