]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/fortran/check.c
check.c (gfc_check_atomic, [...]): Use argument for GFC_ISYM_CAF_GET.
[thirdparty/gcc.git] / gcc / fortran / check.c
1 /* Check functions
2 Copyright (C) 2002-2014 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 "flags.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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' 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('%s')",
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 ("'%s' at %L must be less than "
325 "or equal to BIT_SIZE('%s')",
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 ("'%s' at %L must be less than BIT_SIZE('%s')",
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 ("'%s' 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('%s')",
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 ("'%s' argument of '%s' intrinsic at %L must be the same type "
406 "and kind as '%s'", 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 '%s' 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 '%s' 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 ("'%s' and '%s' arguments of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 ("'%s' argument of '%s' 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 '%s' 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, gfc_expr *value)
1010 {
1011 if (atom->expr_type == EXPR_FUNCTION
1012 && atom->value.function.isym
1013 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1014 atom = atom->value.function.actual->expr;
1015
1016 if (!(atom->ts.type == BT_INTEGER && atom->ts.kind == gfc_atomic_int_kind)
1017 && !(atom->ts.type == BT_LOGICAL
1018 && atom->ts.kind == gfc_atomic_logical_kind))
1019 {
1020 gfc_error ("ATOM argument at %L to intrinsic function %s shall be an "
1021 "integer of ATOMIC_INT_KIND or a logical of "
1022 "ATOMIC_LOGICAL_KIND", &atom->where, gfc_current_intrinsic);
1023 return false;
1024 }
1025
1026 if (!gfc_expr_attr (atom).codimension)
1027 {
1028 gfc_error ("ATOM argument at %L of the %s intrinsic function shall be a "
1029 "coarray or coindexed", &atom->where, gfc_current_intrinsic);
1030 return false;
1031 }
1032
1033 if (atom->ts.type != value->ts.type)
1034 {
1035 gfc_error ("ATOM and VALUE argument of the %s intrinsic function shall "
1036 "have the same type at %L", gfc_current_intrinsic,
1037 &value->where);
1038 return false;
1039 }
1040
1041 return true;
1042 }
1043
1044
1045 bool
1046 gfc_check_atomic_def (gfc_expr *atom, gfc_expr *value)
1047 {
1048 if (atom->expr_type == EXPR_FUNCTION
1049 && atom->value.function.isym
1050 && atom->value.function.isym->id == GFC_ISYM_CAF_GET)
1051 atom = atom->value.function.actual->expr;
1052
1053 if (!scalar_check (atom, 0) || !scalar_check (value, 1))
1054 return false;
1055
1056 if (!gfc_check_vardef_context (atom, false, false, false, NULL))
1057 {
1058 gfc_error ("ATOM argument of the %s intrinsic function at %L shall be "
1059 "definable", gfc_current_intrinsic, &atom->where);
1060 return false;
1061 }
1062
1063 return gfc_check_atomic (atom, value);
1064 }
1065
1066
1067 bool
1068 gfc_check_atomic_ref (gfc_expr *value, gfc_expr *atom)
1069 {
1070 if (!scalar_check (value, 0) || !scalar_check (atom, 1))
1071 return false;
1072
1073 if (!gfc_check_vardef_context (value, false, false, false, NULL))
1074 {
1075 gfc_error ("VALUE argument of the %s intrinsic function at %L shall be "
1076 "definable", gfc_current_intrinsic, &value->where);
1077 return false;
1078 }
1079
1080 return gfc_check_atomic (atom, value);
1081 }
1082
1083
1084 /* BESJN and BESYN functions. */
1085
1086 bool
1087 gfc_check_besn (gfc_expr *n, gfc_expr *x)
1088 {
1089 if (!type_check (n, 0, BT_INTEGER))
1090 return false;
1091 if (n->expr_type == EXPR_CONSTANT)
1092 {
1093 int i;
1094 gfc_extract_int (n, &i);
1095 if (i < 0 && !gfc_notify_std (GFC_STD_GNU, "Negative argument "
1096 "N at %L", &n->where))
1097 return false;
1098 }
1099
1100 if (!type_check (x, 1, BT_REAL))
1101 return false;
1102
1103 return true;
1104 }
1105
1106
1107 /* Transformational version of the Bessel JN and YN functions. */
1108
1109 bool
1110 gfc_check_bessel_n2 (gfc_expr *n1, gfc_expr *n2, gfc_expr *x)
1111 {
1112 if (!type_check (n1, 0, BT_INTEGER))
1113 return false;
1114 if (!scalar_check (n1, 0))
1115 return false;
1116 if (!nonnegative_check ("N1", n1))
1117 return false;
1118
1119 if (!type_check (n2, 1, BT_INTEGER))
1120 return false;
1121 if (!scalar_check (n2, 1))
1122 return false;
1123 if (!nonnegative_check ("N2", n2))
1124 return false;
1125
1126 if (!type_check (x, 2, BT_REAL))
1127 return false;
1128 if (!scalar_check (x, 2))
1129 return false;
1130
1131 return true;
1132 }
1133
1134
1135 bool
1136 gfc_check_bge_bgt_ble_blt (gfc_expr *i, gfc_expr *j)
1137 {
1138 if (!type_check (i, 0, BT_INTEGER))
1139 return false;
1140
1141 if (!type_check (j, 1, BT_INTEGER))
1142 return false;
1143
1144 return true;
1145 }
1146
1147
1148 bool
1149 gfc_check_bitfcn (gfc_expr *i, gfc_expr *pos)
1150 {
1151 if (!type_check (i, 0, BT_INTEGER))
1152 return false;
1153
1154 if (!type_check (pos, 1, BT_INTEGER))
1155 return false;
1156
1157 if (!nonnegative_check ("pos", pos))
1158 return false;
1159
1160 if (!less_than_bitsize1 ("i", i, "pos", pos, false))
1161 return false;
1162
1163 return true;
1164 }
1165
1166
1167 bool
1168 gfc_check_char (gfc_expr *i, gfc_expr *kind)
1169 {
1170 if (!type_check (i, 0, BT_INTEGER))
1171 return false;
1172 if (!kind_check (kind, 1, BT_CHARACTER))
1173 return false;
1174
1175 return true;
1176 }
1177
1178
1179 bool
1180 gfc_check_chdir (gfc_expr *dir)
1181 {
1182 if (!type_check (dir, 0, BT_CHARACTER))
1183 return false;
1184 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1185 return false;
1186
1187 return true;
1188 }
1189
1190
1191 bool
1192 gfc_check_chdir_sub (gfc_expr *dir, gfc_expr *status)
1193 {
1194 if (!type_check (dir, 0, BT_CHARACTER))
1195 return false;
1196 if (!kind_value_check (dir, 0, gfc_default_character_kind))
1197 return false;
1198
1199 if (status == NULL)
1200 return true;
1201
1202 if (!type_check (status, 1, BT_INTEGER))
1203 return false;
1204 if (!scalar_check (status, 1))
1205 return false;
1206
1207 return true;
1208 }
1209
1210
1211 bool
1212 gfc_check_chmod (gfc_expr *name, gfc_expr *mode)
1213 {
1214 if (!type_check (name, 0, BT_CHARACTER))
1215 return false;
1216 if (!kind_value_check (name, 0, gfc_default_character_kind))
1217 return false;
1218
1219 if (!type_check (mode, 1, BT_CHARACTER))
1220 return false;
1221 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1222 return false;
1223
1224 return true;
1225 }
1226
1227
1228 bool
1229 gfc_check_chmod_sub (gfc_expr *name, gfc_expr *mode, gfc_expr *status)
1230 {
1231 if (!type_check (name, 0, BT_CHARACTER))
1232 return false;
1233 if (!kind_value_check (name, 0, gfc_default_character_kind))
1234 return false;
1235
1236 if (!type_check (mode, 1, BT_CHARACTER))
1237 return false;
1238 if (!kind_value_check (mode, 1, gfc_default_character_kind))
1239 return false;
1240
1241 if (status == NULL)
1242 return true;
1243
1244 if (!type_check (status, 2, BT_INTEGER))
1245 return false;
1246
1247 if (!scalar_check (status, 2))
1248 return false;
1249
1250 return true;
1251 }
1252
1253
1254 bool
1255 gfc_check_cmplx (gfc_expr *x, gfc_expr *y, gfc_expr *kind)
1256 {
1257 if (!numeric_check (x, 0))
1258 return false;
1259
1260 if (y != NULL)
1261 {
1262 if (!numeric_check (y, 1))
1263 return false;
1264
1265 if (x->ts.type == BT_COMPLEX)
1266 {
1267 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1268 "present if 'x' is COMPLEX",
1269 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1270 &y->where);
1271 return false;
1272 }
1273
1274 if (y->ts.type == BT_COMPLEX)
1275 {
1276 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1277 "of either REAL or INTEGER",
1278 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1279 &y->where);
1280 return false;
1281 }
1282
1283 }
1284
1285 if (!kind_check (kind, 2, BT_COMPLEX))
1286 return false;
1287
1288 if (!kind && gfc_option.gfc_warn_conversion
1289 && x->ts.type == BT_REAL && x->ts.kind > gfc_default_real_kind)
1290 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1291 "might lose precision, consider using the KIND argument",
1292 gfc_typename (&x->ts), gfc_default_real_kind, &x->where);
1293 else if (y && !kind && gfc_option.gfc_warn_conversion
1294 && y->ts.type == BT_REAL && y->ts.kind > gfc_default_real_kind)
1295 gfc_warning_now ("Conversion from %s to default-kind COMPLEX(%d) at %L "
1296 "might lose precision, consider using the KIND argument",
1297 gfc_typename (&y->ts), gfc_default_real_kind, &y->where);
1298
1299 return true;
1300 }
1301
1302
1303 static bool
1304 check_co_minmaxsum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1305 gfc_expr *errmsg)
1306 {
1307 if (!variable_check (a, 0, false))
1308 return false;
1309
1310 if (result_image != NULL)
1311 {
1312 if (!type_check (result_image, 1, BT_INTEGER))
1313 return false;
1314 if (!scalar_check (result_image, 1))
1315 return false;
1316 }
1317
1318 if (stat != NULL)
1319 {
1320 if (!type_check (stat, 2, BT_INTEGER))
1321 return false;
1322 if (!scalar_check (stat, 2))
1323 return false;
1324 if (!variable_check (stat, 2, false))
1325 return false;
1326 if (stat->ts.kind != 4)
1327 {
1328 gfc_error ("The stat= argument at %L must be a kind=4 integer "
1329 "variable", &stat->where);
1330 return false;
1331 }
1332 }
1333
1334 if (errmsg != NULL)
1335 {
1336 if (!type_check (errmsg, 3, BT_CHARACTER))
1337 return false;
1338 if (!scalar_check (errmsg, 3))
1339 return false;
1340 if (!variable_check (errmsg, 3, false))
1341 return false;
1342 if (errmsg->ts.kind != 1)
1343 {
1344 gfc_error ("The errmsg= argument at %L must be a default-kind "
1345 "character variable", &errmsg->where);
1346 return false;
1347 }
1348 }
1349
1350 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
1351 {
1352 gfc_fatal_error ("Coarrays disabled at %L, use -fcoarray= to enable",
1353 &a->where);
1354 return false;
1355 }
1356
1357 return true;
1358 }
1359
1360
1361 bool
1362 gfc_check_co_minmax (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1363 gfc_expr *errmsg)
1364 {
1365 if (a->ts.type != BT_INTEGER && a->ts.type != BT_REAL
1366 && a->ts.type != BT_CHARACTER)
1367 {
1368 gfc_error ("'%s' argument of '%s' intrinsic at %L shall be of type "
1369 "integer, real or character",
1370 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
1371 &a->where);
1372 return false;
1373 }
1374 return check_co_minmaxsum (a, result_image, stat, errmsg);
1375 }
1376
1377
1378 bool
1379 gfc_check_co_sum (gfc_expr *a, gfc_expr *result_image, gfc_expr *stat,
1380 gfc_expr *errmsg)
1381 {
1382 if (!numeric_check (a, 0))
1383 return false;
1384 return check_co_minmaxsum (a, result_image, stat, errmsg);
1385 }
1386
1387
1388 bool
1389 gfc_check_complex (gfc_expr *x, gfc_expr *y)
1390 {
1391 if (!int_or_real_check (x, 0))
1392 return false;
1393 if (!scalar_check (x, 0))
1394 return false;
1395
1396 if (!int_or_real_check (y, 1))
1397 return false;
1398 if (!scalar_check (y, 1))
1399 return false;
1400
1401 return true;
1402 }
1403
1404
1405 bool
1406 gfc_check_count (gfc_expr *mask, gfc_expr *dim, gfc_expr *kind)
1407 {
1408 if (!logical_array_check (mask, 0))
1409 return false;
1410 if (!dim_check (dim, 1, false))
1411 return false;
1412 if (!dim_rank_check (dim, mask, 0))
1413 return false;
1414 if (!kind_check (kind, 2, BT_INTEGER))
1415 return false;
1416 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1417 "with KIND argument at %L",
1418 gfc_current_intrinsic, &kind->where))
1419 return false;
1420
1421 return true;
1422 }
1423
1424
1425 bool
1426 gfc_check_cshift (gfc_expr *array, gfc_expr *shift, gfc_expr *dim)
1427 {
1428 if (!array_check (array, 0))
1429 return false;
1430
1431 if (!type_check (shift, 1, BT_INTEGER))
1432 return false;
1433
1434 if (!dim_check (dim, 2, true))
1435 return false;
1436
1437 if (!dim_rank_check (dim, array, false))
1438 return false;
1439
1440 if (array->rank == 1 || shift->rank == 0)
1441 {
1442 if (!scalar_check (shift, 1))
1443 return false;
1444 }
1445 else if (shift->rank == array->rank - 1)
1446 {
1447 int d;
1448 if (!dim)
1449 d = 1;
1450 else if (dim->expr_type == EXPR_CONSTANT)
1451 gfc_extract_int (dim, &d);
1452 else
1453 d = -1;
1454
1455 if (d > 0)
1456 {
1457 int i, j;
1458 for (i = 0, j = 0; i < array->rank; i++)
1459 if (i != d - 1)
1460 {
1461 if (!identical_dimen_shape (array, i, shift, j))
1462 {
1463 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1464 "invalid shape in dimension %d (%ld/%ld)",
1465 gfc_current_intrinsic_arg[1]->name,
1466 gfc_current_intrinsic, &shift->where, i + 1,
1467 mpz_get_si (array->shape[i]),
1468 mpz_get_si (shift->shape[j]));
1469 return false;
1470 }
1471
1472 j += 1;
1473 }
1474 }
1475 }
1476 else
1477 {
1478 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1479 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1480 gfc_current_intrinsic, &shift->where, array->rank - 1);
1481 return false;
1482 }
1483
1484 return true;
1485 }
1486
1487
1488 bool
1489 gfc_check_ctime (gfc_expr *time)
1490 {
1491 if (!scalar_check (time, 0))
1492 return false;
1493
1494 if (!type_check (time, 0, BT_INTEGER))
1495 return false;
1496
1497 return true;
1498 }
1499
1500
1501 bool gfc_check_datan2 (gfc_expr *y, gfc_expr *x)
1502 {
1503 if (!double_check (y, 0) || !double_check (x, 1))
1504 return false;
1505
1506 return true;
1507 }
1508
1509 bool
1510 gfc_check_dcmplx (gfc_expr *x, gfc_expr *y)
1511 {
1512 if (!numeric_check (x, 0))
1513 return false;
1514
1515 if (y != NULL)
1516 {
1517 if (!numeric_check (y, 1))
1518 return false;
1519
1520 if (x->ts.type == BT_COMPLEX)
1521 {
1522 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be "
1523 "present if 'x' is COMPLEX",
1524 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1525 &y->where);
1526 return false;
1527 }
1528
1529 if (y->ts.type == BT_COMPLEX)
1530 {
1531 gfc_error ("'%s' argument of '%s' intrinsic at %L must have a type "
1532 "of either REAL or INTEGER",
1533 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1534 &y->where);
1535 return false;
1536 }
1537 }
1538
1539 return true;
1540 }
1541
1542
1543 bool
1544 gfc_check_dble (gfc_expr *x)
1545 {
1546 if (!numeric_check (x, 0))
1547 return false;
1548
1549 return true;
1550 }
1551
1552
1553 bool
1554 gfc_check_digits (gfc_expr *x)
1555 {
1556 if (!int_or_real_check (x, 0))
1557 return false;
1558
1559 return true;
1560 }
1561
1562
1563 bool
1564 gfc_check_dot_product (gfc_expr *vector_a, gfc_expr *vector_b)
1565 {
1566 switch (vector_a->ts.type)
1567 {
1568 case BT_LOGICAL:
1569 if (!type_check (vector_b, 1, BT_LOGICAL))
1570 return false;
1571 break;
1572
1573 case BT_INTEGER:
1574 case BT_REAL:
1575 case BT_COMPLEX:
1576 if (!numeric_check (vector_b, 1))
1577 return false;
1578 break;
1579
1580 default:
1581 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
1582 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
1583 gfc_current_intrinsic, &vector_a->where);
1584 return false;
1585 }
1586
1587 if (!rank_check (vector_a, 0, 1))
1588 return false;
1589
1590 if (!rank_check (vector_b, 1, 1))
1591 return false;
1592
1593 if (! identical_dimen_shape (vector_a, 0, vector_b, 0))
1594 {
1595 gfc_error ("Different shape for arguments '%s' and '%s' at %L for "
1596 "intrinsic 'dot_product'", gfc_current_intrinsic_arg[0]->name,
1597 gfc_current_intrinsic_arg[1]->name, &vector_a->where);
1598 return false;
1599 }
1600
1601 return true;
1602 }
1603
1604
1605 bool
1606 gfc_check_dprod (gfc_expr *x, gfc_expr *y)
1607 {
1608 if (!type_check (x, 0, BT_REAL)
1609 || !type_check (y, 1, BT_REAL))
1610 return false;
1611
1612 if (x->ts.kind != gfc_default_real_kind)
1613 {
1614 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1615 "real", gfc_current_intrinsic_arg[0]->name,
1616 gfc_current_intrinsic, &x->where);
1617 return false;
1618 }
1619
1620 if (y->ts.kind != gfc_default_real_kind)
1621 {
1622 gfc_error ("'%s' argument of '%s' intrinsic at %L must be default "
1623 "real", gfc_current_intrinsic_arg[1]->name,
1624 gfc_current_intrinsic, &y->where);
1625 return false;
1626 }
1627
1628 return true;
1629 }
1630
1631
1632 bool
1633 gfc_check_dshift (gfc_expr *i, gfc_expr *j, gfc_expr *shift)
1634 {
1635 if (!type_check (i, 0, BT_INTEGER))
1636 return false;
1637
1638 if (!type_check (j, 1, BT_INTEGER))
1639 return false;
1640
1641 if (i->is_boz && j->is_boz)
1642 {
1643 gfc_error ("'I' at %L and 'J' at %L cannot both be BOZ literal "
1644 "constants", &i->where, &j->where);
1645 return false;
1646 }
1647
1648 if (!i->is_boz && !j->is_boz && !same_type_check (i, 0, j, 1))
1649 return false;
1650
1651 if (!type_check (shift, 2, BT_INTEGER))
1652 return false;
1653
1654 if (!nonnegative_check ("SHIFT", shift))
1655 return false;
1656
1657 if (i->is_boz)
1658 {
1659 if (!less_than_bitsize1 ("J", j, "SHIFT", shift, true))
1660 return false;
1661 i->ts.kind = j->ts.kind;
1662 }
1663 else
1664 {
1665 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
1666 return false;
1667 j->ts.kind = i->ts.kind;
1668 }
1669
1670 return true;
1671 }
1672
1673
1674 bool
1675 gfc_check_eoshift (gfc_expr *array, gfc_expr *shift, gfc_expr *boundary,
1676 gfc_expr *dim)
1677 {
1678 if (!array_check (array, 0))
1679 return false;
1680
1681 if (!type_check (shift, 1, BT_INTEGER))
1682 return false;
1683
1684 if (!dim_check (dim, 3, true))
1685 return false;
1686
1687 if (!dim_rank_check (dim, array, false))
1688 return false;
1689
1690 if (array->rank == 1 || shift->rank == 0)
1691 {
1692 if (!scalar_check (shift, 1))
1693 return false;
1694 }
1695 else if (shift->rank == array->rank - 1)
1696 {
1697 int d;
1698 if (!dim)
1699 d = 1;
1700 else if (dim->expr_type == EXPR_CONSTANT)
1701 gfc_extract_int (dim, &d);
1702 else
1703 d = -1;
1704
1705 if (d > 0)
1706 {
1707 int i, j;
1708 for (i = 0, j = 0; i < array->rank; i++)
1709 if (i != d - 1)
1710 {
1711 if (!identical_dimen_shape (array, i, shift, j))
1712 {
1713 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
1714 "invalid shape in dimension %d (%ld/%ld)",
1715 gfc_current_intrinsic_arg[1]->name,
1716 gfc_current_intrinsic, &shift->where, i + 1,
1717 mpz_get_si (array->shape[i]),
1718 mpz_get_si (shift->shape[j]));
1719 return false;
1720 }
1721
1722 j += 1;
1723 }
1724 }
1725 }
1726 else
1727 {
1728 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have rank "
1729 "%d or be a scalar", gfc_current_intrinsic_arg[1]->name,
1730 gfc_current_intrinsic, &shift->where, array->rank - 1);
1731 return false;
1732 }
1733
1734 if (boundary != NULL)
1735 {
1736 if (!same_type_check (array, 0, boundary, 2))
1737 return false;
1738
1739 if (array->rank == 1 || boundary->rank == 0)
1740 {
1741 if (!scalar_check (boundary, 2))
1742 return false;
1743 }
1744 else if (boundary->rank == array->rank - 1)
1745 {
1746 if (!gfc_check_conformance (shift, boundary,
1747 "arguments '%s' and '%s' for "
1748 "intrinsic %s",
1749 gfc_current_intrinsic_arg[1]->name,
1750 gfc_current_intrinsic_arg[2]->name,
1751 gfc_current_intrinsic))
1752 return false;
1753 }
1754 else
1755 {
1756 gfc_error ("'%s' argument of intrinsic '%s' at %L of must have "
1757 "rank %d or be a scalar",
1758 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
1759 &shift->where, array->rank - 1);
1760 return false;
1761 }
1762 }
1763
1764 return true;
1765 }
1766
1767 bool
1768 gfc_check_float (gfc_expr *a)
1769 {
1770 if (!type_check (a, 0, BT_INTEGER))
1771 return false;
1772
1773 if ((a->ts.kind != gfc_default_integer_kind)
1774 && !gfc_notify_std (GFC_STD_GNU, "non-default INTEGER "
1775 "kind argument to %s intrinsic at %L",
1776 gfc_current_intrinsic, &a->where))
1777 return false;
1778
1779 return true;
1780 }
1781
1782 /* A single complex argument. */
1783
1784 bool
1785 gfc_check_fn_c (gfc_expr *a)
1786 {
1787 if (!type_check (a, 0, BT_COMPLEX))
1788 return false;
1789
1790 return true;
1791 }
1792
1793 /* A single real argument. */
1794
1795 bool
1796 gfc_check_fn_r (gfc_expr *a)
1797 {
1798 if (!type_check (a, 0, BT_REAL))
1799 return false;
1800
1801 return true;
1802 }
1803
1804 /* A single double argument. */
1805
1806 bool
1807 gfc_check_fn_d (gfc_expr *a)
1808 {
1809 if (!double_check (a, 0))
1810 return false;
1811
1812 return true;
1813 }
1814
1815 /* A single real or complex argument. */
1816
1817 bool
1818 gfc_check_fn_rc (gfc_expr *a)
1819 {
1820 if (!real_or_complex_check (a, 0))
1821 return false;
1822
1823 return true;
1824 }
1825
1826
1827 bool
1828 gfc_check_fn_rc2008 (gfc_expr *a)
1829 {
1830 if (!real_or_complex_check (a, 0))
1831 return false;
1832
1833 if (a->ts.type == BT_COMPLEX
1834 && !gfc_notify_std (GFC_STD_F2008, "COMPLEX argument '%s' "
1835 "of '%s' intrinsic at %L",
1836 gfc_current_intrinsic_arg[0]->name,
1837 gfc_current_intrinsic, &a->where))
1838 return false;
1839
1840 return true;
1841 }
1842
1843
1844 bool
1845 gfc_check_fnum (gfc_expr *unit)
1846 {
1847 if (!type_check (unit, 0, BT_INTEGER))
1848 return false;
1849
1850 if (!scalar_check (unit, 0))
1851 return false;
1852
1853 return true;
1854 }
1855
1856
1857 bool
1858 gfc_check_huge (gfc_expr *x)
1859 {
1860 if (!int_or_real_check (x, 0))
1861 return false;
1862
1863 return true;
1864 }
1865
1866
1867 bool
1868 gfc_check_hypot (gfc_expr *x, gfc_expr *y)
1869 {
1870 if (!type_check (x, 0, BT_REAL))
1871 return false;
1872 if (!same_type_check (x, 0, y, 1))
1873 return false;
1874
1875 return true;
1876 }
1877
1878
1879 /* Check that the single argument is an integer. */
1880
1881 bool
1882 gfc_check_i (gfc_expr *i)
1883 {
1884 if (!type_check (i, 0, BT_INTEGER))
1885 return false;
1886
1887 return true;
1888 }
1889
1890
1891 bool
1892 gfc_check_iand (gfc_expr *i, gfc_expr *j)
1893 {
1894 if (!type_check (i, 0, BT_INTEGER))
1895 return false;
1896
1897 if (!type_check (j, 1, BT_INTEGER))
1898 return false;
1899
1900 if (i->ts.kind != j->ts.kind)
1901 {
1902 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
1903 &i->where))
1904 return false;
1905 }
1906
1907 return true;
1908 }
1909
1910
1911 bool
1912 gfc_check_ibits (gfc_expr *i, gfc_expr *pos, gfc_expr *len)
1913 {
1914 if (!type_check (i, 0, BT_INTEGER))
1915 return false;
1916
1917 if (!type_check (pos, 1, BT_INTEGER))
1918 return false;
1919
1920 if (!type_check (len, 2, BT_INTEGER))
1921 return false;
1922
1923 if (!nonnegative_check ("pos", pos))
1924 return false;
1925
1926 if (!nonnegative_check ("len", len))
1927 return false;
1928
1929 if (!less_than_bitsize2 ("i", i, "pos", pos, "len", len))
1930 return false;
1931
1932 return true;
1933 }
1934
1935
1936 bool
1937 gfc_check_ichar_iachar (gfc_expr *c, gfc_expr *kind)
1938 {
1939 int i;
1940
1941 if (!type_check (c, 0, BT_CHARACTER))
1942 return false;
1943
1944 if (!kind_check (kind, 1, BT_INTEGER))
1945 return false;
1946
1947 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
1948 "with KIND argument at %L",
1949 gfc_current_intrinsic, &kind->where))
1950 return false;
1951
1952 if (c->expr_type == EXPR_VARIABLE || c->expr_type == EXPR_SUBSTRING)
1953 {
1954 gfc_expr *start;
1955 gfc_expr *end;
1956 gfc_ref *ref;
1957
1958 /* Substring references don't have the charlength set. */
1959 ref = c->ref;
1960 while (ref && ref->type != REF_SUBSTRING)
1961 ref = ref->next;
1962
1963 gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
1964
1965 if (!ref)
1966 {
1967 /* Check that the argument is length one. Non-constant lengths
1968 can't be checked here, so assume they are ok. */
1969 if (c->ts.u.cl && c->ts.u.cl->length)
1970 {
1971 /* If we already have a length for this expression then use it. */
1972 if (c->ts.u.cl->length->expr_type != EXPR_CONSTANT)
1973 return true;
1974 i = mpz_get_si (c->ts.u.cl->length->value.integer);
1975 }
1976 else
1977 return true;
1978 }
1979 else
1980 {
1981 start = ref->u.ss.start;
1982 end = ref->u.ss.end;
1983
1984 gcc_assert (start);
1985 if (end == NULL || end->expr_type != EXPR_CONSTANT
1986 || start->expr_type != EXPR_CONSTANT)
1987 return true;
1988
1989 i = mpz_get_si (end->value.integer) + 1
1990 - mpz_get_si (start->value.integer);
1991 }
1992 }
1993 else
1994 return true;
1995
1996 if (i != 1)
1997 {
1998 gfc_error ("Argument of %s at %L must be of length one",
1999 gfc_current_intrinsic, &c->where);
2000 return false;
2001 }
2002
2003 return true;
2004 }
2005
2006
2007 bool
2008 gfc_check_idnint (gfc_expr *a)
2009 {
2010 if (!double_check (a, 0))
2011 return false;
2012
2013 return true;
2014 }
2015
2016
2017 bool
2018 gfc_check_ieor (gfc_expr *i, gfc_expr *j)
2019 {
2020 if (!type_check (i, 0, BT_INTEGER))
2021 return false;
2022
2023 if (!type_check (j, 1, BT_INTEGER))
2024 return false;
2025
2026 if (i->ts.kind != j->ts.kind)
2027 {
2028 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2029 &i->where))
2030 return false;
2031 }
2032
2033 return true;
2034 }
2035
2036
2037 bool
2038 gfc_check_index (gfc_expr *string, gfc_expr *substring, gfc_expr *back,
2039 gfc_expr *kind)
2040 {
2041 if (!type_check (string, 0, BT_CHARACTER)
2042 || !type_check (substring, 1, BT_CHARACTER))
2043 return false;
2044
2045 if (back != NULL && !type_check (back, 2, BT_LOGICAL))
2046 return false;
2047
2048 if (!kind_check (kind, 3, BT_INTEGER))
2049 return false;
2050 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2051 "with KIND argument at %L",
2052 gfc_current_intrinsic, &kind->where))
2053 return false;
2054
2055 if (string->ts.kind != substring->ts.kind)
2056 {
2057 gfc_error ("'%s' argument of '%s' intrinsic at %L must be the same "
2058 "kind as '%s'", gfc_current_intrinsic_arg[1]->name,
2059 gfc_current_intrinsic, &substring->where,
2060 gfc_current_intrinsic_arg[0]->name);
2061 return false;
2062 }
2063
2064 return true;
2065 }
2066
2067
2068 bool
2069 gfc_check_int (gfc_expr *x, gfc_expr *kind)
2070 {
2071 if (!numeric_check (x, 0))
2072 return false;
2073
2074 if (!kind_check (kind, 1, BT_INTEGER))
2075 return false;
2076
2077 return true;
2078 }
2079
2080
2081 bool
2082 gfc_check_intconv (gfc_expr *x)
2083 {
2084 if (!numeric_check (x, 0))
2085 return false;
2086
2087 return true;
2088 }
2089
2090
2091 bool
2092 gfc_check_ior (gfc_expr *i, gfc_expr *j)
2093 {
2094 if (!type_check (i, 0, BT_INTEGER))
2095 return false;
2096
2097 if (!type_check (j, 1, BT_INTEGER))
2098 return false;
2099
2100 if (i->ts.kind != j->ts.kind)
2101 {
2102 if (!gfc_notify_std (GFC_STD_GNU, "Different type kinds at %L",
2103 &i->where))
2104 return false;
2105 }
2106
2107 return true;
2108 }
2109
2110
2111 bool
2112 gfc_check_ishft (gfc_expr *i, gfc_expr *shift)
2113 {
2114 if (!type_check (i, 0, BT_INTEGER)
2115 || !type_check (shift, 1, BT_INTEGER))
2116 return false;
2117
2118 if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2119 return false;
2120
2121 return true;
2122 }
2123
2124
2125 bool
2126 gfc_check_ishftc (gfc_expr *i, gfc_expr *shift, gfc_expr *size)
2127 {
2128 if (!type_check (i, 0, BT_INTEGER)
2129 || !type_check (shift, 1, BT_INTEGER))
2130 return false;
2131
2132 if (size != NULL)
2133 {
2134 int i2, i3;
2135
2136 if (!type_check (size, 2, BT_INTEGER))
2137 return false;
2138
2139 if (!less_than_bitsize1 ("I", i, "SIZE", size, true))
2140 return false;
2141
2142 if (size->expr_type == EXPR_CONSTANT)
2143 {
2144 gfc_extract_int (size, &i3);
2145 if (i3 <= 0)
2146 {
2147 gfc_error ("SIZE at %L must be positive", &size->where);
2148 return false;
2149 }
2150
2151 if (shift->expr_type == EXPR_CONSTANT)
2152 {
2153 gfc_extract_int (shift, &i2);
2154 if (i2 < 0)
2155 i2 = -i2;
2156
2157 if (i2 > i3)
2158 {
2159 gfc_error ("The absolute value of SHIFT at %L must be less "
2160 "than or equal to SIZE at %L", &shift->where,
2161 &size->where);
2162 return false;
2163 }
2164 }
2165 }
2166 }
2167 else if (!less_than_bitsize1 ("I", i, NULL, shift, true))
2168 return false;
2169
2170 return true;
2171 }
2172
2173
2174 bool
2175 gfc_check_kill (gfc_expr *pid, gfc_expr *sig)
2176 {
2177 if (!type_check (pid, 0, BT_INTEGER))
2178 return false;
2179
2180 if (!type_check (sig, 1, BT_INTEGER))
2181 return false;
2182
2183 return true;
2184 }
2185
2186
2187 bool
2188 gfc_check_kill_sub (gfc_expr *pid, gfc_expr *sig, gfc_expr *status)
2189 {
2190 if (!type_check (pid, 0, BT_INTEGER))
2191 return false;
2192
2193 if (!scalar_check (pid, 0))
2194 return false;
2195
2196 if (!type_check (sig, 1, BT_INTEGER))
2197 return false;
2198
2199 if (!scalar_check (sig, 1))
2200 return false;
2201
2202 if (status == NULL)
2203 return true;
2204
2205 if (!type_check (status, 2, BT_INTEGER))
2206 return false;
2207
2208 if (!scalar_check (status, 2))
2209 return false;
2210
2211 return true;
2212 }
2213
2214
2215 bool
2216 gfc_check_kind (gfc_expr *x)
2217 {
2218 if (x->ts.type == BT_DERIVED)
2219 {
2220 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a "
2221 "non-derived type", gfc_current_intrinsic_arg[0]->name,
2222 gfc_current_intrinsic, &x->where);
2223 return false;
2224 }
2225
2226 return true;
2227 }
2228
2229
2230 bool
2231 gfc_check_lbound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
2232 {
2233 if (!array_check (array, 0))
2234 return false;
2235
2236 if (!dim_check (dim, 1, false))
2237 return false;
2238
2239 if (!dim_rank_check (dim, array, 1))
2240 return false;
2241
2242 if (!kind_check (kind, 2, BT_INTEGER))
2243 return false;
2244 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2245 "with KIND argument at %L",
2246 gfc_current_intrinsic, &kind->where))
2247 return false;
2248
2249 return true;
2250 }
2251
2252
2253 bool
2254 gfc_check_lcobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
2255 {
2256 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
2257 {
2258 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
2259 return false;
2260 }
2261
2262 if (!coarray_check (coarray, 0))
2263 return false;
2264
2265 if (dim != NULL)
2266 {
2267 if (!dim_check (dim, 1, false))
2268 return false;
2269
2270 if (!dim_corank_check (dim, coarray))
2271 return false;
2272 }
2273
2274 if (!kind_check (kind, 2, BT_INTEGER))
2275 return false;
2276
2277 return true;
2278 }
2279
2280
2281 bool
2282 gfc_check_len_lentrim (gfc_expr *s, gfc_expr *kind)
2283 {
2284 if (!type_check (s, 0, BT_CHARACTER))
2285 return false;
2286
2287 if (!kind_check (kind, 1, BT_INTEGER))
2288 return false;
2289 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2290 "with KIND argument at %L",
2291 gfc_current_intrinsic, &kind->where))
2292 return false;
2293
2294 return true;
2295 }
2296
2297
2298 bool
2299 gfc_check_lge_lgt_lle_llt (gfc_expr *a, gfc_expr *b)
2300 {
2301 if (!type_check (a, 0, BT_CHARACTER))
2302 return false;
2303 if (!kind_value_check (a, 0, gfc_default_character_kind))
2304 return false;
2305
2306 if (!type_check (b, 1, BT_CHARACTER))
2307 return false;
2308 if (!kind_value_check (b, 1, gfc_default_character_kind))
2309 return false;
2310
2311 return true;
2312 }
2313
2314
2315 bool
2316 gfc_check_link (gfc_expr *path1, gfc_expr *path2)
2317 {
2318 if (!type_check (path1, 0, BT_CHARACTER))
2319 return false;
2320 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2321 return false;
2322
2323 if (!type_check (path2, 1, BT_CHARACTER))
2324 return false;
2325 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2326 return false;
2327
2328 return true;
2329 }
2330
2331
2332 bool
2333 gfc_check_link_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2334 {
2335 if (!type_check (path1, 0, BT_CHARACTER))
2336 return false;
2337 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2338 return false;
2339
2340 if (!type_check (path2, 1, BT_CHARACTER))
2341 return false;
2342 if (!kind_value_check (path2, 0, gfc_default_character_kind))
2343 return false;
2344
2345 if (status == NULL)
2346 return true;
2347
2348 if (!type_check (status, 2, BT_INTEGER))
2349 return false;
2350
2351 if (!scalar_check (status, 2))
2352 return false;
2353
2354 return true;
2355 }
2356
2357
2358 bool
2359 gfc_check_loc (gfc_expr *expr)
2360 {
2361 return variable_check (expr, 0, true);
2362 }
2363
2364
2365 bool
2366 gfc_check_symlnk (gfc_expr *path1, gfc_expr *path2)
2367 {
2368 if (!type_check (path1, 0, BT_CHARACTER))
2369 return false;
2370 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2371 return false;
2372
2373 if (!type_check (path2, 1, BT_CHARACTER))
2374 return false;
2375 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2376 return false;
2377
2378 return true;
2379 }
2380
2381
2382 bool
2383 gfc_check_symlnk_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
2384 {
2385 if (!type_check (path1, 0, BT_CHARACTER))
2386 return false;
2387 if (!kind_value_check (path1, 0, gfc_default_character_kind))
2388 return false;
2389
2390 if (!type_check (path2, 1, BT_CHARACTER))
2391 return false;
2392 if (!kind_value_check (path2, 1, gfc_default_character_kind))
2393 return false;
2394
2395 if (status == NULL)
2396 return true;
2397
2398 if (!type_check (status, 2, BT_INTEGER))
2399 return false;
2400
2401 if (!scalar_check (status, 2))
2402 return false;
2403
2404 return true;
2405 }
2406
2407
2408 bool
2409 gfc_check_logical (gfc_expr *a, gfc_expr *kind)
2410 {
2411 if (!type_check (a, 0, BT_LOGICAL))
2412 return false;
2413 if (!kind_check (kind, 1, BT_LOGICAL))
2414 return false;
2415
2416 return true;
2417 }
2418
2419
2420 /* Min/max family. */
2421
2422 static bool
2423 min_max_args (gfc_actual_arglist *args)
2424 {
2425 gfc_actual_arglist *arg;
2426 int i, j, nargs, *nlabels, nlabelless;
2427 bool a1 = false, a2 = false;
2428
2429 if (args == NULL || args->next == NULL)
2430 {
2431 gfc_error ("Intrinsic '%s' at %L must have at least two arguments",
2432 gfc_current_intrinsic, gfc_current_intrinsic_where);
2433 return false;
2434 }
2435
2436 if (!args->name)
2437 a1 = true;
2438
2439 if (!args->next->name)
2440 a2 = true;
2441
2442 nargs = 0;
2443 for (arg = args; arg; arg = arg->next)
2444 if (arg->name)
2445 nargs++;
2446
2447 if (nargs == 0)
2448 return true;
2449
2450 /* Note: Having a keywordless argument after an "arg=" is checked before. */
2451 nlabelless = 0;
2452 nlabels = XALLOCAVEC (int, nargs);
2453 for (arg = args, i = 0; arg; arg = arg->next, i++)
2454 if (arg->name)
2455 {
2456 int n;
2457 char *endp;
2458
2459 if (arg->name[0] != 'a' || arg->name[1] < '1' || arg->name[1] > '9')
2460 goto unknown;
2461 n = strtol (&arg->name[1], &endp, 10);
2462 if (endp[0] != '\0')
2463 goto unknown;
2464 if (n <= 0)
2465 goto unknown;
2466 if (n <= nlabelless)
2467 goto duplicate;
2468 nlabels[i] = n;
2469 if (n == 1)
2470 a1 = true;
2471 if (n == 2)
2472 a2 = true;
2473 }
2474 else
2475 nlabelless++;
2476
2477 if (!a1 || !a2)
2478 {
2479 gfc_error ("Missing '%s' argument to the %s intrinsic at %L",
2480 !a1 ? "a1" : "a2", gfc_current_intrinsic,
2481 gfc_current_intrinsic_where);
2482 return false;
2483 }
2484
2485 /* Check for duplicates. */
2486 for (i = 0; i < nargs; i++)
2487 for (j = i + 1; j < nargs; j++)
2488 if (nlabels[i] == nlabels[j])
2489 goto duplicate;
2490
2491 return true;
2492
2493 duplicate:
2494 gfc_error ("Duplicate argument '%s' at %L to intrinsic %s", arg->name,
2495 &arg->expr->where, gfc_current_intrinsic);
2496 return false;
2497
2498 unknown:
2499 gfc_error ("Unknown argument '%s' at %L to intrinsic %s", arg->name,
2500 &arg->expr->where, gfc_current_intrinsic);
2501 return false;
2502 }
2503
2504
2505 static bool
2506 check_rest (bt type, int kind, gfc_actual_arglist *arglist)
2507 {
2508 gfc_actual_arglist *arg, *tmp;
2509 gfc_expr *x;
2510 int m, n;
2511
2512 if (!min_max_args (arglist))
2513 return false;
2514
2515 for (arg = arglist, n=1; arg; arg = arg->next, n++)
2516 {
2517 x = arg->expr;
2518 if (x->ts.type != type || x->ts.kind != kind)
2519 {
2520 if (x->ts.type == type)
2521 {
2522 if (!gfc_notify_std (GFC_STD_GNU, "Different type "
2523 "kinds at %L", &x->where))
2524 return false;
2525 }
2526 else
2527 {
2528 gfc_error ("'a%d' argument of '%s' intrinsic at %L must be "
2529 "%s(%d)", n, gfc_current_intrinsic, &x->where,
2530 gfc_basic_typename (type), kind);
2531 return false;
2532 }
2533 }
2534
2535 for (tmp = arglist, m=1; tmp != arg; tmp = tmp->next, m++)
2536 if (!gfc_check_conformance (tmp->expr, x,
2537 "arguments 'a%d' and 'a%d' for "
2538 "intrinsic '%s'", m, n,
2539 gfc_current_intrinsic))
2540 return false;
2541 }
2542
2543 return true;
2544 }
2545
2546
2547 bool
2548 gfc_check_min_max (gfc_actual_arglist *arg)
2549 {
2550 gfc_expr *x;
2551
2552 if (!min_max_args (arg))
2553 return false;
2554
2555 x = arg->expr;
2556
2557 if (x->ts.type == BT_CHARACTER)
2558 {
2559 if (!gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
2560 "with CHARACTER argument at %L",
2561 gfc_current_intrinsic, &x->where))
2562 return false;
2563 }
2564 else if (x->ts.type != BT_INTEGER && x->ts.type != BT_REAL)
2565 {
2566 gfc_error ("'a1' argument of '%s' intrinsic at %L must be INTEGER, "
2567 "REAL or CHARACTER", gfc_current_intrinsic, &x->where);
2568 return false;
2569 }
2570
2571 return check_rest (x->ts.type, x->ts.kind, arg);
2572 }
2573
2574
2575 bool
2576 gfc_check_min_max_integer (gfc_actual_arglist *arg)
2577 {
2578 return check_rest (BT_INTEGER, gfc_default_integer_kind, arg);
2579 }
2580
2581
2582 bool
2583 gfc_check_min_max_real (gfc_actual_arglist *arg)
2584 {
2585 return check_rest (BT_REAL, gfc_default_real_kind, arg);
2586 }
2587
2588
2589 bool
2590 gfc_check_min_max_double (gfc_actual_arglist *arg)
2591 {
2592 return check_rest (BT_REAL, gfc_default_double_kind, arg);
2593 }
2594
2595
2596 /* End of min/max family. */
2597
2598 bool
2599 gfc_check_malloc (gfc_expr *size)
2600 {
2601 if (!type_check (size, 0, BT_INTEGER))
2602 return false;
2603
2604 if (!scalar_check (size, 0))
2605 return false;
2606
2607 return true;
2608 }
2609
2610
2611 bool
2612 gfc_check_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
2613 {
2614 if ((matrix_a->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_a->ts))
2615 {
2616 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2617 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
2618 gfc_current_intrinsic, &matrix_a->where);
2619 return false;
2620 }
2621
2622 if ((matrix_b->ts.type != BT_LOGICAL) && !gfc_numeric_ts (&matrix_b->ts))
2623 {
2624 gfc_error ("'%s' argument of '%s' intrinsic at %L must be numeric "
2625 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
2626 gfc_current_intrinsic, &matrix_b->where);
2627 return false;
2628 }
2629
2630 if ((matrix_a->ts.type == BT_LOGICAL && gfc_numeric_ts (&matrix_b->ts))
2631 || (gfc_numeric_ts (&matrix_a->ts) && matrix_b->ts.type == BT_LOGICAL))
2632 {
2633 gfc_error ("Argument types of '%s' intrinsic at %L must match (%s/%s)",
2634 gfc_current_intrinsic, &matrix_a->where,
2635 gfc_typename(&matrix_a->ts), gfc_typename(&matrix_b->ts));
2636 return false;
2637 }
2638
2639 switch (matrix_a->rank)
2640 {
2641 case 1:
2642 if (!rank_check (matrix_b, 1, 2))
2643 return false;
2644 /* Check for case matrix_a has shape(m), matrix_b has shape (m, k). */
2645 if (!identical_dimen_shape (matrix_a, 0, matrix_b, 0))
2646 {
2647 gfc_error ("Different shape on dimension 1 for arguments '%s' "
2648 "and '%s' at %L for intrinsic matmul",
2649 gfc_current_intrinsic_arg[0]->name,
2650 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2651 return false;
2652 }
2653 break;
2654
2655 case 2:
2656 if (matrix_b->rank != 2)
2657 {
2658 if (!rank_check (matrix_b, 1, 1))
2659 return false;
2660 }
2661 /* matrix_b has rank 1 or 2 here. Common check for the cases
2662 - matrix_a has shape (n,m) and matrix_b has shape (m, k)
2663 - matrix_a has shape (n,m) and matrix_b has shape (m). */
2664 if (!identical_dimen_shape (matrix_a, 1, matrix_b, 0))
2665 {
2666 gfc_error ("Different shape on dimension 2 for argument '%s' and "
2667 "dimension 1 for argument '%s' at %L for intrinsic "
2668 "matmul", gfc_current_intrinsic_arg[0]->name,
2669 gfc_current_intrinsic_arg[1]->name, &matrix_a->where);
2670 return false;
2671 }
2672 break;
2673
2674 default:
2675 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of rank "
2676 "1 or 2", gfc_current_intrinsic_arg[0]->name,
2677 gfc_current_intrinsic, &matrix_a->where);
2678 return false;
2679 }
2680
2681 return true;
2682 }
2683
2684
2685 /* Whoever came up with this interface was probably on something.
2686 The possibilities for the occupation of the second and third
2687 parameters are:
2688
2689 Arg #2 Arg #3
2690 NULL NULL
2691 DIM NULL
2692 MASK NULL
2693 NULL MASK minloc(array, mask=m)
2694 DIM MASK
2695
2696 I.e. in the case of minloc(array,mask), mask will be in the second
2697 position of the argument list and we'll have to fix that up. */
2698
2699 bool
2700 gfc_check_minloc_maxloc (gfc_actual_arglist *ap)
2701 {
2702 gfc_expr *a, *m, *d;
2703
2704 a = ap->expr;
2705 if (!int_or_real_check (a, 0) || !array_check (a, 0))
2706 return false;
2707
2708 d = ap->next->expr;
2709 m = ap->next->next->expr;
2710
2711 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2712 && ap->next->name == NULL)
2713 {
2714 m = d;
2715 d = NULL;
2716 ap->next->expr = NULL;
2717 ap->next->next->expr = m;
2718 }
2719
2720 if (!dim_check (d, 1, false))
2721 return false;
2722
2723 if (!dim_rank_check (d, a, 0))
2724 return false;
2725
2726 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2727 return false;
2728
2729 if (m != NULL
2730 && !gfc_check_conformance (a, m,
2731 "arguments '%s' and '%s' for intrinsic %s",
2732 gfc_current_intrinsic_arg[0]->name,
2733 gfc_current_intrinsic_arg[2]->name,
2734 gfc_current_intrinsic))
2735 return false;
2736
2737 return true;
2738 }
2739
2740
2741 /* Similar to minloc/maxloc, the argument list might need to be
2742 reordered for the MINVAL, MAXVAL, PRODUCT, and SUM intrinsics. The
2743 difference is that MINLOC/MAXLOC take an additional KIND argument.
2744 The possibilities are:
2745
2746 Arg #2 Arg #3
2747 NULL NULL
2748 DIM NULL
2749 MASK NULL
2750 NULL MASK minval(array, mask=m)
2751 DIM MASK
2752
2753 I.e. in the case of minval(array,mask), mask will be in the second
2754 position of the argument list and we'll have to fix that up. */
2755
2756 static bool
2757 check_reduction (gfc_actual_arglist *ap)
2758 {
2759 gfc_expr *a, *m, *d;
2760
2761 a = ap->expr;
2762 d = ap->next->expr;
2763 m = ap->next->next->expr;
2764
2765 if (m == NULL && d != NULL && d->ts.type == BT_LOGICAL
2766 && ap->next->name == NULL)
2767 {
2768 m = d;
2769 d = NULL;
2770 ap->next->expr = NULL;
2771 ap->next->next->expr = m;
2772 }
2773
2774 if (!dim_check (d, 1, false))
2775 return false;
2776
2777 if (!dim_rank_check (d, a, 0))
2778 return false;
2779
2780 if (m != NULL && !type_check (m, 2, BT_LOGICAL))
2781 return false;
2782
2783 if (m != NULL
2784 && !gfc_check_conformance (a, m,
2785 "arguments '%s' and '%s' for intrinsic %s",
2786 gfc_current_intrinsic_arg[0]->name,
2787 gfc_current_intrinsic_arg[2]->name,
2788 gfc_current_intrinsic))
2789 return false;
2790
2791 return true;
2792 }
2793
2794
2795 bool
2796 gfc_check_minval_maxval (gfc_actual_arglist *ap)
2797 {
2798 if (!int_or_real_check (ap->expr, 0)
2799 || !array_check (ap->expr, 0))
2800 return false;
2801
2802 return check_reduction (ap);
2803 }
2804
2805
2806 bool
2807 gfc_check_product_sum (gfc_actual_arglist *ap)
2808 {
2809 if (!numeric_check (ap->expr, 0)
2810 || !array_check (ap->expr, 0))
2811 return false;
2812
2813 return check_reduction (ap);
2814 }
2815
2816
2817 /* For IANY, IALL and IPARITY. */
2818
2819 bool
2820 gfc_check_mask (gfc_expr *i, gfc_expr *kind)
2821 {
2822 int k;
2823
2824 if (!type_check (i, 0, BT_INTEGER))
2825 return false;
2826
2827 if (!nonnegative_check ("I", i))
2828 return false;
2829
2830 if (!kind_check (kind, 1, BT_INTEGER))
2831 return false;
2832
2833 if (kind)
2834 gfc_extract_int (kind, &k);
2835 else
2836 k = gfc_default_integer_kind;
2837
2838 if (!less_than_bitsizekind ("I", i, k))
2839 return false;
2840
2841 return true;
2842 }
2843
2844
2845 bool
2846 gfc_check_transf_bit_intrins (gfc_actual_arglist *ap)
2847 {
2848 if (ap->expr->ts.type != BT_INTEGER)
2849 {
2850 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER",
2851 gfc_current_intrinsic_arg[0]->name,
2852 gfc_current_intrinsic, &ap->expr->where);
2853 return false;
2854 }
2855
2856 if (!array_check (ap->expr, 0))
2857 return false;
2858
2859 return check_reduction (ap);
2860 }
2861
2862
2863 bool
2864 gfc_check_merge (gfc_expr *tsource, gfc_expr *fsource, gfc_expr *mask)
2865 {
2866 if (!same_type_check (tsource, 0, fsource, 1))
2867 return false;
2868
2869 if (!type_check (mask, 2, BT_LOGICAL))
2870 return false;
2871
2872 if (tsource->ts.type == BT_CHARACTER)
2873 return gfc_check_same_strlen (tsource, fsource, "MERGE intrinsic");
2874
2875 return true;
2876 }
2877
2878
2879 bool
2880 gfc_check_merge_bits (gfc_expr *i, gfc_expr *j, gfc_expr *mask)
2881 {
2882 if (!type_check (i, 0, BT_INTEGER))
2883 return false;
2884
2885 if (!type_check (j, 1, BT_INTEGER))
2886 return false;
2887
2888 if (!type_check (mask, 2, BT_INTEGER))
2889 return false;
2890
2891 if (!same_type_check (i, 0, j, 1))
2892 return false;
2893
2894 if (!same_type_check (i, 0, mask, 2))
2895 return false;
2896
2897 return true;
2898 }
2899
2900
2901 bool
2902 gfc_check_move_alloc (gfc_expr *from, gfc_expr *to)
2903 {
2904 if (!variable_check (from, 0, false))
2905 return false;
2906 if (!allocatable_check (from, 0))
2907 return false;
2908 if (gfc_is_coindexed (from))
2909 {
2910 gfc_error ("The FROM argument to MOVE_ALLOC at %L shall not be "
2911 "coindexed", &from->where);
2912 return false;
2913 }
2914
2915 if (!variable_check (to, 1, false))
2916 return false;
2917 if (!allocatable_check (to, 1))
2918 return false;
2919 if (gfc_is_coindexed (to))
2920 {
2921 gfc_error ("The TO argument to MOVE_ALLOC at %L shall not be "
2922 "coindexed", &to->where);
2923 return false;
2924 }
2925
2926 if (from->ts.type == BT_CLASS && to->ts.type == BT_DERIVED)
2927 {
2928 gfc_error ("The TO arguments in MOVE_ALLOC at %L must be "
2929 "polymorphic if FROM is polymorphic",
2930 &to->where);
2931 return false;
2932 }
2933
2934 if (!same_type_check (to, 1, from, 0))
2935 return false;
2936
2937 if (to->rank != from->rank)
2938 {
2939 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
2940 "must have the same rank %d/%d", &to->where, from->rank,
2941 to->rank);
2942 return false;
2943 }
2944
2945 /* IR F08/0040; cf. 12-006A. */
2946 if (gfc_get_corank (to) != gfc_get_corank (from))
2947 {
2948 gfc_error ("The FROM and TO arguments of the MOVE_ALLOC intrinsic at %L "
2949 "must have the same corank %d/%d", &to->where,
2950 gfc_get_corank (from), gfc_get_corank (to));
2951 return false;
2952 }
2953
2954 /* CLASS arguments: Make sure the vtab of from is present. */
2955 if (to->ts.type == BT_CLASS && !UNLIMITED_POLY (from))
2956 gfc_find_vtab (&from->ts);
2957
2958 return true;
2959 }
2960
2961
2962 bool
2963 gfc_check_nearest (gfc_expr *x, gfc_expr *s)
2964 {
2965 if (!type_check (x, 0, BT_REAL))
2966 return false;
2967
2968 if (!type_check (s, 1, BT_REAL))
2969 return false;
2970
2971 if (s->expr_type == EXPR_CONSTANT)
2972 {
2973 if (mpfr_sgn (s->value.real) == 0)
2974 {
2975 gfc_error ("Argument 'S' of NEAREST at %L shall not be zero",
2976 &s->where);
2977 return false;
2978 }
2979 }
2980
2981 return true;
2982 }
2983
2984
2985 bool
2986 gfc_check_new_line (gfc_expr *a)
2987 {
2988 if (!type_check (a, 0, BT_CHARACTER))
2989 return false;
2990
2991 return true;
2992 }
2993
2994
2995 bool
2996 gfc_check_norm2 (gfc_expr *array, gfc_expr *dim)
2997 {
2998 if (!type_check (array, 0, BT_REAL))
2999 return false;
3000
3001 if (!array_check (array, 0))
3002 return false;
3003
3004 if (!dim_rank_check (dim, array, false))
3005 return false;
3006
3007 return true;
3008 }
3009
3010 bool
3011 gfc_check_null (gfc_expr *mold)
3012 {
3013 symbol_attribute attr;
3014
3015 if (mold == NULL)
3016 return true;
3017
3018 if (!variable_check (mold, 0, true))
3019 return false;
3020
3021 attr = gfc_variable_attr (mold, NULL);
3022
3023 if (!attr.pointer && !attr.proc_pointer && !attr.allocatable)
3024 {
3025 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a POINTER, "
3026 "ALLOCATABLE or procedure pointer",
3027 gfc_current_intrinsic_arg[0]->name,
3028 gfc_current_intrinsic, &mold->where);
3029 return false;
3030 }
3031
3032 if (attr.allocatable
3033 && !gfc_notify_std (GFC_STD_F2003, "NULL intrinsic with "
3034 "allocatable MOLD at %L", &mold->where))
3035 return false;
3036
3037 /* F2008, C1242. */
3038 if (gfc_is_coindexed (mold))
3039 {
3040 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
3041 "coindexed", gfc_current_intrinsic_arg[0]->name,
3042 gfc_current_intrinsic, &mold->where);
3043 return false;
3044 }
3045
3046 return true;
3047 }
3048
3049
3050 bool
3051 gfc_check_pack (gfc_expr *array, gfc_expr *mask, gfc_expr *vector)
3052 {
3053 if (!array_check (array, 0))
3054 return false;
3055
3056 if (!type_check (mask, 1, BT_LOGICAL))
3057 return false;
3058
3059 if (!gfc_check_conformance (array, mask,
3060 "arguments '%s' and '%s' for intrinsic '%s'",
3061 gfc_current_intrinsic_arg[0]->name,
3062 gfc_current_intrinsic_arg[1]->name,
3063 gfc_current_intrinsic))
3064 return false;
3065
3066 if (vector != NULL)
3067 {
3068 mpz_t array_size, vector_size;
3069 bool have_array_size, have_vector_size;
3070
3071 if (!same_type_check (array, 0, vector, 2))
3072 return false;
3073
3074 if (!rank_check (vector, 2, 1))
3075 return false;
3076
3077 /* VECTOR requires at least as many elements as MASK
3078 has .TRUE. values. */
3079 have_array_size = gfc_array_size(array, &array_size);
3080 have_vector_size = gfc_array_size(vector, &vector_size);
3081
3082 if (have_vector_size
3083 && (mask->expr_type == EXPR_ARRAY
3084 || (mask->expr_type == EXPR_CONSTANT
3085 && have_array_size)))
3086 {
3087 int mask_true_values = 0;
3088
3089 if (mask->expr_type == EXPR_ARRAY)
3090 {
3091 gfc_constructor *mask_ctor;
3092 mask_ctor = gfc_constructor_first (mask->value.constructor);
3093 while (mask_ctor)
3094 {
3095 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
3096 {
3097 mask_true_values = 0;
3098 break;
3099 }
3100
3101 if (mask_ctor->expr->value.logical)
3102 mask_true_values++;
3103
3104 mask_ctor = gfc_constructor_next (mask_ctor);
3105 }
3106 }
3107 else if (mask->expr_type == EXPR_CONSTANT && mask->value.logical)
3108 mask_true_values = mpz_get_si (array_size);
3109
3110 if (mpz_get_si (vector_size) < mask_true_values)
3111 {
3112 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
3113 "provide at least as many elements as there "
3114 "are .TRUE. values in '%s' (%ld/%d)",
3115 gfc_current_intrinsic_arg[2]->name,
3116 gfc_current_intrinsic, &vector->where,
3117 gfc_current_intrinsic_arg[1]->name,
3118 mpz_get_si (vector_size), mask_true_values);
3119 return false;
3120 }
3121 }
3122
3123 if (have_array_size)
3124 mpz_clear (array_size);
3125 if (have_vector_size)
3126 mpz_clear (vector_size);
3127 }
3128
3129 return true;
3130 }
3131
3132
3133 bool
3134 gfc_check_parity (gfc_expr *mask, gfc_expr *dim)
3135 {
3136 if (!type_check (mask, 0, BT_LOGICAL))
3137 return false;
3138
3139 if (!array_check (mask, 0))
3140 return false;
3141
3142 if (!dim_rank_check (dim, mask, false))
3143 return false;
3144
3145 return true;
3146 }
3147
3148
3149 bool
3150 gfc_check_precision (gfc_expr *x)
3151 {
3152 if (!real_or_complex_check (x, 0))
3153 return false;
3154
3155 return true;
3156 }
3157
3158
3159 bool
3160 gfc_check_present (gfc_expr *a)
3161 {
3162 gfc_symbol *sym;
3163
3164 if (!variable_check (a, 0, true))
3165 return false;
3166
3167 sym = a->symtree->n.sym;
3168 if (!sym->attr.dummy)
3169 {
3170 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a "
3171 "dummy variable", gfc_current_intrinsic_arg[0]->name,
3172 gfc_current_intrinsic, &a->where);
3173 return false;
3174 }
3175
3176 if (!sym->attr.optional)
3177 {
3178 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of "
3179 "an OPTIONAL dummy variable",
3180 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3181 &a->where);
3182 return false;
3183 }
3184
3185 /* 13.14.82 PRESENT(A)
3186 ......
3187 Argument. A shall be the name of an optional dummy argument that is
3188 accessible in the subprogram in which the PRESENT function reference
3189 appears... */
3190
3191 if (a->ref != NULL
3192 && !(a->ref->next == NULL && a->ref->type == REF_ARRAY
3193 && (a->ref->u.ar.type == AR_FULL
3194 || (a->ref->u.ar.type == AR_ELEMENT
3195 && a->ref->u.ar.as->rank == 0))))
3196 {
3197 gfc_error ("'%s' argument of '%s' intrinsic at %L must not be a "
3198 "subobject of '%s'", gfc_current_intrinsic_arg[0]->name,
3199 gfc_current_intrinsic, &a->where, sym->name);
3200 return false;
3201 }
3202
3203 return true;
3204 }
3205
3206
3207 bool
3208 gfc_check_radix (gfc_expr *x)
3209 {
3210 if (!int_or_real_check (x, 0))
3211 return false;
3212
3213 return true;
3214 }
3215
3216
3217 bool
3218 gfc_check_range (gfc_expr *x)
3219 {
3220 if (!numeric_check (x, 0))
3221 return false;
3222
3223 return true;
3224 }
3225
3226
3227 bool
3228 gfc_check_rank (gfc_expr *a ATTRIBUTE_UNUSED)
3229 {
3230 /* Any data object is allowed; a "data object" is a "constant (4.1.3),
3231 variable (6), or subobject of a constant (2.4.3.2.3)" (F2008, 1.3.45). */
3232
3233 bool is_variable = true;
3234
3235 /* Functions returning pointers are regarded as variable, cf. F2008, R602. */
3236 if (a->expr_type == EXPR_FUNCTION)
3237 is_variable = a->value.function.esym
3238 ? a->value.function.esym->result->attr.pointer
3239 : a->symtree->n.sym->result->attr.pointer;
3240
3241 if (a->expr_type == EXPR_OP || a->expr_type == EXPR_NULL
3242 || a->expr_type == EXPR_COMPCALL|| a->expr_type == EXPR_PPC
3243 || !is_variable)
3244 {
3245 gfc_error ("The argument of the RANK intrinsic at %L must be a data "
3246 "object", &a->where);
3247 return false;
3248 }
3249
3250 return true;
3251 }
3252
3253
3254 /* real, float, sngl. */
3255 bool
3256 gfc_check_real (gfc_expr *a, gfc_expr *kind)
3257 {
3258 if (!numeric_check (a, 0))
3259 return false;
3260
3261 if (!kind_check (kind, 1, BT_REAL))
3262 return false;
3263
3264 return true;
3265 }
3266
3267
3268 bool
3269 gfc_check_rename (gfc_expr *path1, gfc_expr *path2)
3270 {
3271 if (!type_check (path1, 0, BT_CHARACTER))
3272 return false;
3273 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3274 return false;
3275
3276 if (!type_check (path2, 1, BT_CHARACTER))
3277 return false;
3278 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3279 return false;
3280
3281 return true;
3282 }
3283
3284
3285 bool
3286 gfc_check_rename_sub (gfc_expr *path1, gfc_expr *path2, gfc_expr *status)
3287 {
3288 if (!type_check (path1, 0, BT_CHARACTER))
3289 return false;
3290 if (!kind_value_check (path1, 0, gfc_default_character_kind))
3291 return false;
3292
3293 if (!type_check (path2, 1, BT_CHARACTER))
3294 return false;
3295 if (!kind_value_check (path2, 1, gfc_default_character_kind))
3296 return false;
3297
3298 if (status == NULL)
3299 return true;
3300
3301 if (!type_check (status, 2, BT_INTEGER))
3302 return false;
3303
3304 if (!scalar_check (status, 2))
3305 return false;
3306
3307 return true;
3308 }
3309
3310
3311 bool
3312 gfc_check_repeat (gfc_expr *x, gfc_expr *y)
3313 {
3314 if (!type_check (x, 0, BT_CHARACTER))
3315 return false;
3316
3317 if (!scalar_check (x, 0))
3318 return false;
3319
3320 if (!type_check (y, 0, BT_INTEGER))
3321 return false;
3322
3323 if (!scalar_check (y, 1))
3324 return false;
3325
3326 return true;
3327 }
3328
3329
3330 bool
3331 gfc_check_reshape (gfc_expr *source, gfc_expr *shape,
3332 gfc_expr *pad, gfc_expr *order)
3333 {
3334 mpz_t size;
3335 mpz_t nelems;
3336 int shape_size;
3337
3338 if (!array_check (source, 0))
3339 return false;
3340
3341 if (!rank_check (shape, 1, 1))
3342 return false;
3343
3344 if (!type_check (shape, 1, BT_INTEGER))
3345 return false;
3346
3347 if (!gfc_array_size (shape, &size))
3348 {
3349 gfc_error ("'shape' argument of 'reshape' intrinsic at %L must be an "
3350 "array of constant size", &shape->where);
3351 return false;
3352 }
3353
3354 shape_size = mpz_get_ui (size);
3355 mpz_clear (size);
3356
3357 if (shape_size <= 0)
3358 {
3359 gfc_error ("'%s' argument of '%s' intrinsic at %L is empty",
3360 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3361 &shape->where);
3362 return false;
3363 }
3364 else if (shape_size > GFC_MAX_DIMENSIONS)
3365 {
3366 gfc_error ("'shape' argument of 'reshape' intrinsic at %L has more "
3367 "than %d elements", &shape->where, GFC_MAX_DIMENSIONS);
3368 return false;
3369 }
3370 else if (shape->expr_type == EXPR_ARRAY && gfc_is_constant_expr (shape))
3371 {
3372 gfc_expr *e;
3373 int i, extent;
3374 for (i = 0; i < shape_size; ++i)
3375 {
3376 e = gfc_constructor_lookup_expr (shape->value.constructor, i);
3377 if (e->expr_type != EXPR_CONSTANT)
3378 continue;
3379
3380 gfc_extract_int (e, &extent);
3381 if (extent < 0)
3382 {
3383 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3384 "negative element (%d)",
3385 gfc_current_intrinsic_arg[1]->name,
3386 gfc_current_intrinsic, &e->where, extent);
3387 return false;
3388 }
3389 }
3390 }
3391
3392 if (pad != NULL)
3393 {
3394 if (!same_type_check (source, 0, pad, 2))
3395 return false;
3396
3397 if (!array_check (pad, 2))
3398 return false;
3399 }
3400
3401 if (order != NULL)
3402 {
3403 if (!array_check (order, 3))
3404 return false;
3405
3406 if (!type_check (order, 3, BT_INTEGER))
3407 return false;
3408
3409 if (order->expr_type == EXPR_ARRAY)
3410 {
3411 int i, order_size, dim, perm[GFC_MAX_DIMENSIONS];
3412 gfc_expr *e;
3413
3414 for (i = 0; i < GFC_MAX_DIMENSIONS; ++i)
3415 perm[i] = 0;
3416
3417 gfc_array_size (order, &size);
3418 order_size = mpz_get_ui (size);
3419 mpz_clear (size);
3420
3421 if (order_size != shape_size)
3422 {
3423 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3424 "has wrong number of elements (%d/%d)",
3425 gfc_current_intrinsic_arg[3]->name,
3426 gfc_current_intrinsic, &order->where,
3427 order_size, shape_size);
3428 return false;
3429 }
3430
3431 for (i = 1; i <= order_size; ++i)
3432 {
3433 e = gfc_constructor_lookup_expr (order->value.constructor, i-1);
3434 if (e->expr_type != EXPR_CONSTANT)
3435 continue;
3436
3437 gfc_extract_int (e, &dim);
3438
3439 if (dim < 1 || dim > order_size)
3440 {
3441 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3442 "has out-of-range dimension (%d)",
3443 gfc_current_intrinsic_arg[3]->name,
3444 gfc_current_intrinsic, &e->where, dim);
3445 return false;
3446 }
3447
3448 if (perm[dim-1] != 0)
3449 {
3450 gfc_error ("'%s' argument of '%s' intrinsic at %L has "
3451 "invalid permutation of dimensions (dimension "
3452 "'%d' duplicated)",
3453 gfc_current_intrinsic_arg[3]->name,
3454 gfc_current_intrinsic, &e->where, dim);
3455 return false;
3456 }
3457
3458 perm[dim-1] = 1;
3459 }
3460 }
3461 }
3462
3463 if (pad == NULL && shape->expr_type == EXPR_ARRAY
3464 && gfc_is_constant_expr (shape)
3465 && !(source->expr_type == EXPR_VARIABLE && source->symtree->n.sym->as
3466 && source->symtree->n.sym->as->type == AS_ASSUMED_SIZE))
3467 {
3468 /* Check the match in size between source and destination. */
3469 if (gfc_array_size (source, &nelems))
3470 {
3471 gfc_constructor *c;
3472 bool test;
3473
3474
3475 mpz_init_set_ui (size, 1);
3476 for (c = gfc_constructor_first (shape->value.constructor);
3477 c; c = gfc_constructor_next (c))
3478 mpz_mul (size, size, c->expr->value.integer);
3479
3480 test = mpz_cmp (nelems, size) < 0 && mpz_cmp_ui (size, 0) > 0;
3481 mpz_clear (nelems);
3482 mpz_clear (size);
3483
3484 if (test)
3485 {
3486 gfc_error ("Without padding, there are not enough elements "
3487 "in the intrinsic RESHAPE source at %L to match "
3488 "the shape", &source->where);
3489 return false;
3490 }
3491 }
3492 }
3493
3494 return true;
3495 }
3496
3497
3498 bool
3499 gfc_check_same_type_as (gfc_expr *a, gfc_expr *b)
3500 {
3501 if (a->ts.type != BT_DERIVED && a->ts.type != BT_CLASS)
3502 {
3503 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3504 "cannot be of type %s",
3505 gfc_current_intrinsic_arg[0]->name,
3506 gfc_current_intrinsic,
3507 &a->where, gfc_typename (&a->ts));
3508 return false;
3509 }
3510
3511 if (!(gfc_type_is_extensible (a->ts.u.derived) || UNLIMITED_POLY (a)))
3512 {
3513 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3514 "must be of an extensible type",
3515 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3516 &a->where);
3517 return false;
3518 }
3519
3520 if (b->ts.type != BT_DERIVED && b->ts.type != BT_CLASS)
3521 {
3522 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3523 "cannot be of type %s",
3524 gfc_current_intrinsic_arg[0]->name,
3525 gfc_current_intrinsic,
3526 &b->where, gfc_typename (&b->ts));
3527 return false;
3528 }
3529
3530 if (!(gfc_type_is_extensible (b->ts.u.derived) || UNLIMITED_POLY (b)))
3531 {
3532 gfc_error ("'%s' argument of '%s' intrinsic at %L "
3533 "must be of an extensible type",
3534 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
3535 &b->where);
3536 return false;
3537 }
3538
3539 return true;
3540 }
3541
3542
3543 bool
3544 gfc_check_scale (gfc_expr *x, gfc_expr *i)
3545 {
3546 if (!type_check (x, 0, BT_REAL))
3547 return false;
3548
3549 if (!type_check (i, 1, BT_INTEGER))
3550 return false;
3551
3552 return true;
3553 }
3554
3555
3556 bool
3557 gfc_check_scan (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
3558 {
3559 if (!type_check (x, 0, BT_CHARACTER))
3560 return false;
3561
3562 if (!type_check (y, 1, BT_CHARACTER))
3563 return false;
3564
3565 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
3566 return false;
3567
3568 if (!kind_check (kind, 3, BT_INTEGER))
3569 return false;
3570 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3571 "with KIND argument at %L",
3572 gfc_current_intrinsic, &kind->where))
3573 return false;
3574
3575 if (!same_type_check (x, 0, y, 1))
3576 return false;
3577
3578 return true;
3579 }
3580
3581
3582 bool
3583 gfc_check_secnds (gfc_expr *r)
3584 {
3585 if (!type_check (r, 0, BT_REAL))
3586 return false;
3587
3588 if (!kind_value_check (r, 0, 4))
3589 return false;
3590
3591 if (!scalar_check (r, 0))
3592 return false;
3593
3594 return true;
3595 }
3596
3597
3598 bool
3599 gfc_check_selected_char_kind (gfc_expr *name)
3600 {
3601 if (!type_check (name, 0, BT_CHARACTER))
3602 return false;
3603
3604 if (!kind_value_check (name, 0, gfc_default_character_kind))
3605 return false;
3606
3607 if (!scalar_check (name, 0))
3608 return false;
3609
3610 return true;
3611 }
3612
3613
3614 bool
3615 gfc_check_selected_int_kind (gfc_expr *r)
3616 {
3617 if (!type_check (r, 0, BT_INTEGER))
3618 return false;
3619
3620 if (!scalar_check (r, 0))
3621 return false;
3622
3623 return true;
3624 }
3625
3626
3627 bool
3628 gfc_check_selected_real_kind (gfc_expr *p, gfc_expr *r, gfc_expr *radix)
3629 {
3630 if (p == NULL && r == NULL
3631 && !gfc_notify_std (GFC_STD_F2008, "SELECTED_REAL_KIND with"
3632 " neither 'P' nor 'R' argument at %L",
3633 gfc_current_intrinsic_where))
3634 return false;
3635
3636 if (p)
3637 {
3638 if (!type_check (p, 0, BT_INTEGER))
3639 return false;
3640
3641 if (!scalar_check (p, 0))
3642 return false;
3643 }
3644
3645 if (r)
3646 {
3647 if (!type_check (r, 1, BT_INTEGER))
3648 return false;
3649
3650 if (!scalar_check (r, 1))
3651 return false;
3652 }
3653
3654 if (radix)
3655 {
3656 if (!type_check (radix, 1, BT_INTEGER))
3657 return false;
3658
3659 if (!scalar_check (radix, 1))
3660 return false;
3661
3662 if (!gfc_notify_std (GFC_STD_F2008, "'%s' intrinsic with "
3663 "RADIX argument at %L", gfc_current_intrinsic,
3664 &radix->where))
3665 return false;
3666 }
3667
3668 return true;
3669 }
3670
3671
3672 bool
3673 gfc_check_set_exponent (gfc_expr *x, gfc_expr *i)
3674 {
3675 if (!type_check (x, 0, BT_REAL))
3676 return false;
3677
3678 if (!type_check (i, 1, BT_INTEGER))
3679 return false;
3680
3681 return true;
3682 }
3683
3684
3685 bool
3686 gfc_check_shape (gfc_expr *source, gfc_expr *kind)
3687 {
3688 gfc_array_ref *ar;
3689
3690 if (source->rank == 0 || source->expr_type != EXPR_VARIABLE)
3691 return true;
3692
3693 ar = gfc_find_array_ref (source);
3694
3695 if (ar->as && ar->as->type == AS_ASSUMED_SIZE && ar->type == AR_FULL)
3696 {
3697 gfc_error ("'source' argument of 'shape' intrinsic at %L must not be "
3698 "an assumed size array", &source->where);
3699 return false;
3700 }
3701
3702 if (!kind_check (kind, 1, BT_INTEGER))
3703 return false;
3704 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3705 "with KIND argument at %L",
3706 gfc_current_intrinsic, &kind->where))
3707 return false;
3708
3709 return true;
3710 }
3711
3712
3713 bool
3714 gfc_check_shift (gfc_expr *i, gfc_expr *shift)
3715 {
3716 if (!type_check (i, 0, BT_INTEGER))
3717 return false;
3718
3719 if (!type_check (shift, 0, BT_INTEGER))
3720 return false;
3721
3722 if (!nonnegative_check ("SHIFT", shift))
3723 return false;
3724
3725 if (!less_than_bitsize1 ("I", i, "SHIFT", shift, true))
3726 return false;
3727
3728 return true;
3729 }
3730
3731
3732 bool
3733 gfc_check_sign (gfc_expr *a, gfc_expr *b)
3734 {
3735 if (!int_or_real_check (a, 0))
3736 return false;
3737
3738 if (!same_type_check (a, 0, b, 1))
3739 return false;
3740
3741 return true;
3742 }
3743
3744
3745 bool
3746 gfc_check_size (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
3747 {
3748 if (!array_check (array, 0))
3749 return false;
3750
3751 if (!dim_check (dim, 1, true))
3752 return false;
3753
3754 if (!dim_rank_check (dim, array, 0))
3755 return false;
3756
3757 if (!kind_check (kind, 2, BT_INTEGER))
3758 return false;
3759 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
3760 "with KIND argument at %L",
3761 gfc_current_intrinsic, &kind->where))
3762 return false;
3763
3764
3765 return true;
3766 }
3767
3768
3769 bool
3770 gfc_check_sizeof (gfc_expr *arg)
3771 {
3772 if (arg->ts.type == BT_PROCEDURE)
3773 {
3774 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a procedure",
3775 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3776 &arg->where);
3777 return false;
3778 }
3779
3780 if (arg->ts.type == BT_ASSUMED)
3781 {
3782 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
3783 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3784 &arg->where);
3785 return false;
3786 }
3787
3788 if (arg->rank && arg->expr_type == EXPR_VARIABLE
3789 && arg->symtree->n.sym->as != NULL
3790 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
3791 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
3792 {
3793 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
3794 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
3795 gfc_current_intrinsic, &arg->where);
3796 return false;
3797 }
3798
3799 return true;
3800 }
3801
3802
3803 /* Check whether an expression is interoperable. When returning false,
3804 msg is set to a string telling why the expression is not interoperable,
3805 otherwise, it is set to NULL. The msg string can be used in diagnostics.
3806 If c_loc is true, character with len > 1 are allowed (cf. Fortran
3807 2003corr5); additionally, assumed-shape/assumed-rank/deferred-shape
3808 arrays are permitted. And if c_f_ptr is true, deferred-shape arrays
3809 are permitted. */
3810
3811 static bool
3812 is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr)
3813 {
3814 *msg = NULL;
3815
3816 if (expr->ts.type == BT_CLASS)
3817 {
3818 *msg = "Expression is polymorphic";
3819 return false;
3820 }
3821
3822 if (expr->ts.type == BT_DERIVED && !expr->ts.u.derived->attr.is_bind_c
3823 && !expr->ts.u.derived->ts.is_iso_c)
3824 {
3825 *msg = "Expression is a noninteroperable derived type";
3826 return false;
3827 }
3828
3829 if (expr->ts.type == BT_PROCEDURE)
3830 {
3831 *msg = "Procedure unexpected as argument";
3832 return false;
3833 }
3834
3835 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_LOGICAL)
3836 {
3837 int i;
3838 for (i = 0; gfc_logical_kinds[i].kind; i++)
3839 if (gfc_logical_kinds[i].kind == expr->ts.kind)
3840 return true;
3841 *msg = "Extension to use a non-C_Bool-kind LOGICAL";
3842 return false;
3843 }
3844
3845 if (gfc_notification_std (GFC_STD_GNU) && expr->ts.type == BT_CHARACTER
3846 && expr->ts.kind != 1)
3847 {
3848 *msg = "Extension to use a non-C_CHAR-kind CHARACTER";
3849 return false;
3850 }
3851
3852 if (expr->ts.type == BT_CHARACTER) {
3853 if (expr->ts.deferred)
3854 {
3855 /* TS 29113 allows deferred-length strings as dummy arguments,
3856 but it is not an interoperable type. */
3857 *msg = "Expression shall not be a deferred-length string";
3858 return false;
3859 }
3860
3861 if (expr->ts.u.cl && expr->ts.u.cl->length
3862 && !gfc_simplify_expr (expr, 0))
3863 gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");
3864
3865 if (!c_loc && expr->ts.u.cl
3866 && (!expr->ts.u.cl->length
3867 || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
3868 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
3869 {
3870 *msg = "Type shall have a character length of 1";
3871 return false;
3872 }
3873 }
3874
3875 /* Note: The following checks are about interoperatable variables, Fortran
3876 15.3.5/15.3.6. In intrinsics like C_LOC or in procedure interface, more
3877 is allowed, e.g. assumed-shape arrays with TS 29113. */
3878
3879 if (gfc_is_coarray (expr))
3880 {
3881 *msg = "Coarrays are not interoperable";
3882 return false;
3883 }
3884
3885 if (!c_loc && expr->rank > 0 && expr->expr_type != EXPR_ARRAY)
3886 {
3887 gfc_array_ref *ar = gfc_find_array_ref (expr);
3888 if (ar->type != AR_FULL)
3889 {
3890 *msg = "Only whole-arrays are interoperable";
3891 return false;
3892 }
3893 if (!c_f_ptr && ar->as->type != AS_EXPLICIT
3894 && ar->as->type != AS_ASSUMED_SIZE)
3895 {
3896 *msg = "Only explicit-size and assumed-size arrays are interoperable";
3897 return false;
3898 }
3899 }
3900
3901 return true;
3902 }
3903
3904
3905 bool
3906 gfc_check_c_sizeof (gfc_expr *arg)
3907 {
3908 const char *msg;
3909
3910 if (!is_c_interoperable (arg, &msg, false, false))
3911 {
3912 gfc_error ("'%s' argument of '%s' intrinsic at %L must be an "
3913 "interoperable data entity: %s",
3914 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3915 &arg->where, msg);
3916 return false;
3917 }
3918
3919 if (arg->ts.type == BT_ASSUMED)
3920 {
3921 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be "
3922 "TYPE(*)",
3923 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
3924 &arg->where);
3925 return false;
3926 }
3927
3928 if (arg->rank && arg->expr_type == EXPR_VARIABLE
3929 && arg->symtree->n.sym->as != NULL
3930 && arg->symtree->n.sym->as->type == AS_ASSUMED_SIZE && arg->ref
3931 && arg->ref->type == REF_ARRAY && arg->ref->u.ar.type == AR_FULL)
3932 {
3933 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be an "
3934 "assumed-size array", gfc_current_intrinsic_arg[0]->name,
3935 gfc_current_intrinsic, &arg->where);
3936 return false;
3937 }
3938
3939 return true;
3940 }
3941
3942
3943 bool
3944 gfc_check_c_associated (gfc_expr *c_ptr_1, gfc_expr *c_ptr_2)
3945 {
3946 if (c_ptr_1->ts.type != BT_DERIVED
3947 || c_ptr_1->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3948 || (c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR
3949 && c_ptr_1->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR))
3950 {
3951 gfc_error ("Argument C_PTR_1 at %L to C_ASSOCIATED shall have the "
3952 "type TYPE(C_PTR) or TYPE(C_FUNPTR)", &c_ptr_1->where);
3953 return false;
3954 }
3955
3956 if (!scalar_check (c_ptr_1, 0))
3957 return false;
3958
3959 if (c_ptr_2
3960 && (c_ptr_2->ts.type != BT_DERIVED
3961 || c_ptr_2->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3962 || (c_ptr_1->ts.u.derived->intmod_sym_id
3963 != c_ptr_2->ts.u.derived->intmod_sym_id)))
3964 {
3965 gfc_error ("Argument C_PTR_2 at %L to C_ASSOCIATED shall have the "
3966 "same type as C_PTR_1: %s instead of %s", &c_ptr_1->where,
3967 gfc_typename (&c_ptr_1->ts),
3968 gfc_typename (&c_ptr_2->ts));
3969 return false;
3970 }
3971
3972 if (c_ptr_2 && !scalar_check (c_ptr_2, 1))
3973 return false;
3974
3975 return true;
3976 }
3977
3978
3979 bool
3980 gfc_check_c_f_pointer (gfc_expr *cptr, gfc_expr *fptr, gfc_expr *shape)
3981 {
3982 symbol_attribute attr;
3983 const char *msg;
3984
3985 if (cptr->ts.type != BT_DERIVED
3986 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
3987 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_PTR)
3988 {
3989 gfc_error ("Argument CPTR at %L to C_F_POINTER shall have the "
3990 "type TYPE(C_PTR)", &cptr->where);
3991 return false;
3992 }
3993
3994 if (!scalar_check (cptr, 0))
3995 return false;
3996
3997 attr = gfc_expr_attr (fptr);
3998
3999 if (!attr.pointer)
4000 {
4001 gfc_error ("Argument FPTR at %L to C_F_POINTER must be a pointer",
4002 &fptr->where);
4003 return false;
4004 }
4005
4006 if (fptr->ts.type == BT_CLASS)
4007 {
4008 gfc_error ("FPTR argument at %L to C_F_POINTER shall not be polymorphic",
4009 &fptr->where);
4010 return false;
4011 }
4012
4013 if (gfc_is_coindexed (fptr))
4014 {
4015 gfc_error ("Argument FPTR at %L to C_F_POINTER shall not be "
4016 "coindexed", &fptr->where);
4017 return false;
4018 }
4019
4020 if (fptr->rank == 0 && shape)
4021 {
4022 gfc_error ("Unexpected SHAPE argument at %L to C_F_POINTER with scalar "
4023 "FPTR", &fptr->where);
4024 return false;
4025 }
4026 else if (fptr->rank && !shape)
4027 {
4028 gfc_error ("Expected SHAPE argument to C_F_POINTER with array "
4029 "FPTR at %L", &fptr->where);
4030 return false;
4031 }
4032
4033 if (shape && !rank_check (shape, 2, 1))
4034 return false;
4035
4036 if (shape && !type_check (shape, 2, BT_INTEGER))
4037 return false;
4038
4039 if (shape)
4040 {
4041 mpz_t size;
4042 if (gfc_array_size (shape, &size))
4043 {
4044 if (mpz_cmp_ui (size, fptr->rank) != 0)
4045 {
4046 mpz_clear (size);
4047 gfc_error ("SHAPE argument at %L to C_F_POINTER must have the same "
4048 "size as the RANK of FPTR", &shape->where);
4049 return false;
4050 }
4051 mpz_clear (size);
4052 }
4053 }
4054
4055 if (fptr->ts.type == BT_CLASS)
4056 {
4057 gfc_error ("Polymorphic FPTR at %L to C_F_POINTER", &fptr->where);
4058 return false;
4059 }
4060
4061 if (!is_c_interoperable (fptr, &msg, false, true))
4062 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable array FPTR "
4063 "at %L to C_F_POINTER: %s", &fptr->where, msg);
4064
4065 return true;
4066 }
4067
4068
4069 bool
4070 gfc_check_c_f_procpointer (gfc_expr *cptr, gfc_expr *fptr)
4071 {
4072 symbol_attribute attr;
4073
4074 if (cptr->ts.type != BT_DERIVED
4075 || cptr->ts.u.derived->from_intmod != INTMOD_ISO_C_BINDING
4076 || cptr->ts.u.derived->intmod_sym_id != ISOCBINDING_FUNPTR)
4077 {
4078 gfc_error ("Argument CPTR at %L to C_F_PROCPOINTER shall have the "
4079 "type TYPE(C_FUNPTR)", &cptr->where);
4080 return false;
4081 }
4082
4083 if (!scalar_check (cptr, 0))
4084 return false;
4085
4086 attr = gfc_expr_attr (fptr);
4087
4088 if (!attr.proc_pointer)
4089 {
4090 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall be a procedure "
4091 "pointer", &fptr->where);
4092 return false;
4093 }
4094
4095 if (gfc_is_coindexed (fptr))
4096 {
4097 gfc_error ("Argument FPTR at %L to C_F_PROCPOINTER shall not be "
4098 "coindexed", &fptr->where);
4099 return false;
4100 }
4101
4102 if (!attr.is_bind_c)
4103 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4104 "pointer at %L to C_F_PROCPOINTER", &fptr->where);
4105
4106 return true;
4107 }
4108
4109
4110 bool
4111 gfc_check_c_funloc (gfc_expr *x)
4112 {
4113 symbol_attribute attr;
4114
4115 if (gfc_is_coindexed (x))
4116 {
4117 gfc_error ("Argument X at %L to C_FUNLOC shall not be "
4118 "coindexed", &x->where);
4119 return false;
4120 }
4121
4122 attr = gfc_expr_attr (x);
4123
4124 if (attr.function && !attr.proc_pointer && x->expr_type == EXPR_VARIABLE
4125 && x->symtree->n.sym == x->symtree->n.sym->result)
4126 {
4127 gfc_namespace *ns = gfc_current_ns;
4128
4129 for (ns = gfc_current_ns; ns; ns = ns->parent)
4130 if (x->symtree->n.sym == ns->proc_name)
4131 {
4132 gfc_error ("Function result '%s' at %L is invalid as X argument "
4133 "to C_FUNLOC", x->symtree->n.sym->name, &x->where);
4134 return false;
4135 }
4136 }
4137
4138 if (attr.flavor != FL_PROCEDURE)
4139 {
4140 gfc_error ("Argument X at %L to C_FUNLOC shall be a procedure "
4141 "or a procedure pointer", &x->where);
4142 return false;
4143 }
4144
4145 if (!attr.is_bind_c)
4146 return gfc_notify_std (GFC_STD_F2008_TS, "Noninteroperable procedure "
4147 "at %L to C_FUNLOC", &x->where);
4148 return true;
4149 }
4150
4151
4152 bool
4153 gfc_check_c_loc (gfc_expr *x)
4154 {
4155 symbol_attribute attr;
4156 const char *msg;
4157
4158 if (gfc_is_coindexed (x))
4159 {
4160 gfc_error ("Argument X at %L to C_LOC shall not be coindexed", &x->where);
4161 return false;
4162 }
4163
4164 if (x->ts.type == BT_CLASS)
4165 {
4166 gfc_error ("X argument at %L to C_LOC shall not be polymorphic",
4167 &x->where);
4168 return false;
4169 }
4170
4171 attr = gfc_expr_attr (x);
4172
4173 if (!attr.pointer
4174 && (x->expr_type != EXPR_VARIABLE || !attr.target
4175 || attr.flavor == FL_PARAMETER))
4176 {
4177 gfc_error ("Argument X at %L to C_LOC shall have either "
4178 "the POINTER or the TARGET attribute", &x->where);
4179 return false;
4180 }
4181
4182 if (x->ts.type == BT_CHARACTER
4183 && gfc_var_strlen (x) == 0)
4184 {
4185 gfc_error ("Argument X at %L to C_LOC shall be not be a zero-sized "
4186 "string", &x->where);
4187 return false;
4188 }
4189
4190 if (!is_c_interoperable (x, &msg, true, false))
4191 {
4192 if (x->ts.type == BT_CLASS)
4193 {
4194 gfc_error ("Argument at %L to C_LOC shall not be polymorphic",
4195 &x->where);
4196 return false;
4197 }
4198
4199 if (x->rank
4200 && !gfc_notify_std (GFC_STD_F2008_TS,
4201 "Noninteroperable array at %L as"
4202 " argument to C_LOC: %s", &x->where, msg))
4203 return false;
4204 }
4205 else if (x->rank > 0 && gfc_notification_std (GFC_STD_F2008))
4206 {
4207 gfc_array_ref *ar = gfc_find_array_ref (x);
4208
4209 if (ar->as->type != AS_EXPLICIT && ar->as->type != AS_ASSUMED_SIZE
4210 && !attr.allocatable
4211 && !gfc_notify_std (GFC_STD_F2008,
4212 "Array of interoperable type at %L "
4213 "to C_LOC which is nonallocatable and neither "
4214 "assumed size nor explicit size", &x->where))
4215 return false;
4216 else if (ar->type != AR_FULL
4217 && !gfc_notify_std (GFC_STD_F2008, "Array section at %L "
4218 "to C_LOC", &x->where))
4219 return false;
4220 }
4221
4222 return true;
4223 }
4224
4225
4226 bool
4227 gfc_check_sleep_sub (gfc_expr *seconds)
4228 {
4229 if (!type_check (seconds, 0, BT_INTEGER))
4230 return false;
4231
4232 if (!scalar_check (seconds, 0))
4233 return false;
4234
4235 return true;
4236 }
4237
4238 bool
4239 gfc_check_sngl (gfc_expr *a)
4240 {
4241 if (!type_check (a, 0, BT_REAL))
4242 return false;
4243
4244 if ((a->ts.kind != gfc_default_double_kind)
4245 && !gfc_notify_std (GFC_STD_GNU, "non double precision "
4246 "REAL argument to %s intrinsic at %L",
4247 gfc_current_intrinsic, &a->where))
4248 return false;
4249
4250 return true;
4251 }
4252
4253 bool
4254 gfc_check_spread (gfc_expr *source, gfc_expr *dim, gfc_expr *ncopies)
4255 {
4256 if (source->rank >= GFC_MAX_DIMENSIONS)
4257 {
4258 gfc_error ("'%s' argument of '%s' intrinsic at %L must be less "
4259 "than rank %d", gfc_current_intrinsic_arg[0]->name,
4260 gfc_current_intrinsic, &source->where, GFC_MAX_DIMENSIONS);
4261
4262 return false;
4263 }
4264
4265 if (dim == NULL)
4266 return false;
4267
4268 if (!dim_check (dim, 1, false))
4269 return false;
4270
4271 /* dim_rank_check() does not apply here. */
4272 if (dim
4273 && dim->expr_type == EXPR_CONSTANT
4274 && (mpz_cmp_ui (dim->value.integer, 1) < 0
4275 || mpz_cmp_ui (dim->value.integer, source->rank + 1) > 0))
4276 {
4277 gfc_error ("'%s' argument of '%s' intrinsic at %L is not a valid "
4278 "dimension index", gfc_current_intrinsic_arg[1]->name,
4279 gfc_current_intrinsic, &dim->where);
4280 return false;
4281 }
4282
4283 if (!type_check (ncopies, 2, BT_INTEGER))
4284 return false;
4285
4286 if (!scalar_check (ncopies, 2))
4287 return false;
4288
4289 return true;
4290 }
4291
4292
4293 /* Functions for checking FGETC, FPUTC, FGET and FPUT (subroutines and
4294 functions). */
4295
4296 bool
4297 gfc_check_fgetputc_sub (gfc_expr *unit, gfc_expr *c, gfc_expr *status)
4298 {
4299 if (!type_check (unit, 0, BT_INTEGER))
4300 return false;
4301
4302 if (!scalar_check (unit, 0))
4303 return false;
4304
4305 if (!type_check (c, 1, BT_CHARACTER))
4306 return false;
4307 if (!kind_value_check (c, 1, gfc_default_character_kind))
4308 return false;
4309
4310 if (status == NULL)
4311 return true;
4312
4313 if (!type_check (status, 2, BT_INTEGER)
4314 || !kind_value_check (status, 2, gfc_default_integer_kind)
4315 || !scalar_check (status, 2))
4316 return false;
4317
4318 return true;
4319 }
4320
4321
4322 bool
4323 gfc_check_fgetputc (gfc_expr *unit, gfc_expr *c)
4324 {
4325 return gfc_check_fgetputc_sub (unit, c, NULL);
4326 }
4327
4328
4329 bool
4330 gfc_check_fgetput_sub (gfc_expr *c, gfc_expr *status)
4331 {
4332 if (!type_check (c, 0, BT_CHARACTER))
4333 return false;
4334 if (!kind_value_check (c, 0, gfc_default_character_kind))
4335 return false;
4336
4337 if (status == NULL)
4338 return true;
4339
4340 if (!type_check (status, 1, BT_INTEGER)
4341 || !kind_value_check (status, 1, gfc_default_integer_kind)
4342 || !scalar_check (status, 1))
4343 return false;
4344
4345 return true;
4346 }
4347
4348
4349 bool
4350 gfc_check_fgetput (gfc_expr *c)
4351 {
4352 return gfc_check_fgetput_sub (c, NULL);
4353 }
4354
4355
4356 bool
4357 gfc_check_fseek_sub (gfc_expr *unit, gfc_expr *offset, gfc_expr *whence, gfc_expr *status)
4358 {
4359 if (!type_check (unit, 0, BT_INTEGER))
4360 return false;
4361
4362 if (!scalar_check (unit, 0))
4363 return false;
4364
4365 if (!type_check (offset, 1, BT_INTEGER))
4366 return false;
4367
4368 if (!scalar_check (offset, 1))
4369 return false;
4370
4371 if (!type_check (whence, 2, BT_INTEGER))
4372 return false;
4373
4374 if (!scalar_check (whence, 2))
4375 return false;
4376
4377 if (status == NULL)
4378 return true;
4379
4380 if (!type_check (status, 3, BT_INTEGER))
4381 return false;
4382
4383 if (!kind_value_check (status, 3, 4))
4384 return false;
4385
4386 if (!scalar_check (status, 3))
4387 return false;
4388
4389 return true;
4390 }
4391
4392
4393
4394 bool
4395 gfc_check_fstat (gfc_expr *unit, gfc_expr *array)
4396 {
4397 if (!type_check (unit, 0, BT_INTEGER))
4398 return false;
4399
4400 if (!scalar_check (unit, 0))
4401 return false;
4402
4403 if (!type_check (array, 1, BT_INTEGER)
4404 || !kind_value_check (unit, 0, gfc_default_integer_kind))
4405 return false;
4406
4407 if (!array_check (array, 1))
4408 return false;
4409
4410 return true;
4411 }
4412
4413
4414 bool
4415 gfc_check_fstat_sub (gfc_expr *unit, gfc_expr *array, gfc_expr *status)
4416 {
4417 if (!type_check (unit, 0, BT_INTEGER))
4418 return false;
4419
4420 if (!scalar_check (unit, 0))
4421 return false;
4422
4423 if (!type_check (array, 1, BT_INTEGER)
4424 || !kind_value_check (array, 1, gfc_default_integer_kind))
4425 return false;
4426
4427 if (!array_check (array, 1))
4428 return false;
4429
4430 if (status == NULL)
4431 return true;
4432
4433 if (!type_check (status, 2, BT_INTEGER)
4434 || !kind_value_check (status, 2, gfc_default_integer_kind))
4435 return false;
4436
4437 if (!scalar_check (status, 2))
4438 return false;
4439
4440 return true;
4441 }
4442
4443
4444 bool
4445 gfc_check_ftell (gfc_expr *unit)
4446 {
4447 if (!type_check (unit, 0, BT_INTEGER))
4448 return false;
4449
4450 if (!scalar_check (unit, 0))
4451 return false;
4452
4453 return true;
4454 }
4455
4456
4457 bool
4458 gfc_check_ftell_sub (gfc_expr *unit, gfc_expr *offset)
4459 {
4460 if (!type_check (unit, 0, BT_INTEGER))
4461 return false;
4462
4463 if (!scalar_check (unit, 0))
4464 return false;
4465
4466 if (!type_check (offset, 1, BT_INTEGER))
4467 return false;
4468
4469 if (!scalar_check (offset, 1))
4470 return false;
4471
4472 return true;
4473 }
4474
4475
4476 bool
4477 gfc_check_stat (gfc_expr *name, gfc_expr *array)
4478 {
4479 if (!type_check (name, 0, BT_CHARACTER))
4480 return false;
4481 if (!kind_value_check (name, 0, gfc_default_character_kind))
4482 return false;
4483
4484 if (!type_check (array, 1, BT_INTEGER)
4485 || !kind_value_check (array, 1, gfc_default_integer_kind))
4486 return false;
4487
4488 if (!array_check (array, 1))
4489 return false;
4490
4491 return true;
4492 }
4493
4494
4495 bool
4496 gfc_check_stat_sub (gfc_expr *name, gfc_expr *array, gfc_expr *status)
4497 {
4498 if (!type_check (name, 0, BT_CHARACTER))
4499 return false;
4500 if (!kind_value_check (name, 0, gfc_default_character_kind))
4501 return false;
4502
4503 if (!type_check (array, 1, BT_INTEGER)
4504 || !kind_value_check (array, 1, gfc_default_integer_kind))
4505 return false;
4506
4507 if (!array_check (array, 1))
4508 return false;
4509
4510 if (status == NULL)
4511 return true;
4512
4513 if (!type_check (status, 2, BT_INTEGER)
4514 || !kind_value_check (array, 1, gfc_default_integer_kind))
4515 return false;
4516
4517 if (!scalar_check (status, 2))
4518 return false;
4519
4520 return true;
4521 }
4522
4523
4524 bool
4525 gfc_check_image_index (gfc_expr *coarray, gfc_expr *sub)
4526 {
4527 mpz_t nelems;
4528
4529 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4530 {
4531 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4532 return false;
4533 }
4534
4535 if (!coarray_check (coarray, 0))
4536 return false;
4537
4538 if (sub->rank != 1)
4539 {
4540 gfc_error ("%s argument to IMAGE_INDEX must be a rank one array at %L",
4541 gfc_current_intrinsic_arg[1]->name, &sub->where);
4542 return false;
4543 }
4544
4545 if (gfc_array_size (sub, &nelems))
4546 {
4547 int corank = gfc_get_corank (coarray);
4548
4549 if (mpz_cmp_ui (nelems, corank) != 0)
4550 {
4551 gfc_error ("The number of array elements of the SUB argument to "
4552 "IMAGE_INDEX at %L shall be %d (corank) not %d",
4553 &sub->where, corank, (int) mpz_get_si (nelems));
4554 mpz_clear (nelems);
4555 return false;
4556 }
4557 mpz_clear (nelems);
4558 }
4559
4560 return true;
4561 }
4562
4563
4564 bool
4565 gfc_check_num_images (gfc_expr *distance, gfc_expr *failed)
4566 {
4567 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4568 {
4569 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4570 return false;
4571 }
4572
4573 if (distance)
4574 {
4575 if (!type_check (distance, 0, BT_INTEGER))
4576 return false;
4577
4578 if (!nonnegative_check ("DISTANCE", distance))
4579 return false;
4580
4581 if (!scalar_check (distance, 0))
4582 return false;
4583
4584 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
4585 "NUM_IMAGES at %L", &distance->where))
4586 return false;
4587 }
4588
4589 if (failed)
4590 {
4591 if (!type_check (failed, 1, BT_LOGICAL))
4592 return false;
4593
4594 if (!scalar_check (failed, 1))
4595 return false;
4596
4597 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAILED= argument to "
4598 "NUM_IMAGES at %L", &distance->where))
4599 return false;
4600 }
4601
4602 return true;
4603 }
4604
4605
4606 bool
4607 gfc_check_this_image (gfc_expr *coarray, gfc_expr *dim, gfc_expr *distance)
4608 {
4609 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4610 {
4611 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4612 return false;
4613 }
4614
4615 if (coarray == NULL && dim == NULL && distance == NULL)
4616 return true;
4617
4618 if (dim != NULL && coarray == NULL)
4619 {
4620 gfc_error ("DIM argument without COARRAY argument not allowed for "
4621 "THIS_IMAGE intrinsic at %L", &dim->where);
4622 return false;
4623 }
4624
4625 if (distance && (coarray || dim))
4626 {
4627 gfc_error ("The DISTANCE argument may not be specified together with the "
4628 "COARRAY or DIM argument in intrinsic at %L",
4629 &distance->where);
4630 return false;
4631 }
4632
4633 /* Assume that we have "this_image (distance)". */
4634 if (coarray && !gfc_is_coarray (coarray) && coarray->ts.type == BT_INTEGER)
4635 {
4636 if (dim)
4637 {
4638 gfc_error ("Unexpected DIM argument with noncoarray argument at %L",
4639 &coarray->where);
4640 return false;
4641 }
4642 distance = coarray;
4643 }
4644
4645 if (distance)
4646 {
4647 if (!type_check (distance, 2, BT_INTEGER))
4648 return false;
4649
4650 if (!nonnegative_check ("DISTANCE", distance))
4651 return false;
4652
4653 if (!scalar_check (distance, 2))
4654 return false;
4655
4656 if (!gfc_notify_std (GFC_STD_F2008_TS, "DISTANCE= argument to "
4657 "THIS_IMAGE at %L", &distance->where))
4658 return false;
4659
4660 return true;
4661 }
4662
4663 if (!coarray_check (coarray, 0))
4664 return false;
4665
4666 if (dim != NULL)
4667 {
4668 if (!dim_check (dim, 1, false))
4669 return false;
4670
4671 if (!dim_corank_check (dim, coarray))
4672 return false;
4673 }
4674
4675 return true;
4676 }
4677
4678 /* Calculate the sizes for transfer, used by gfc_check_transfer and also
4679 by gfc_simplify_transfer. Return false if we cannot do so. */
4680
4681 bool
4682 gfc_calculate_transfer_sizes (gfc_expr *source, gfc_expr *mold, gfc_expr *size,
4683 size_t *source_size, size_t *result_size,
4684 size_t *result_length_p)
4685 {
4686 size_t result_elt_size;
4687
4688 if (source->expr_type == EXPR_FUNCTION)
4689 return false;
4690
4691 if (size && size->expr_type != EXPR_CONSTANT)
4692 return false;
4693
4694 /* Calculate the size of the source. */
4695 *source_size = gfc_target_expr_size (source);
4696 if (*source_size == 0)
4697 return false;
4698
4699 /* Determine the size of the element. */
4700 result_elt_size = gfc_element_size (mold);
4701 if (result_elt_size == 0)
4702 return false;
4703
4704 if (mold->expr_type == EXPR_ARRAY || mold->rank || size)
4705 {
4706 int result_length;
4707
4708 if (size)
4709 result_length = (size_t)mpz_get_ui (size->value.integer);
4710 else
4711 {
4712 result_length = *source_size / result_elt_size;
4713 if (result_length * result_elt_size < *source_size)
4714 result_length += 1;
4715 }
4716
4717 *result_size = result_length * result_elt_size;
4718 if (result_length_p)
4719 *result_length_p = result_length;
4720 }
4721 else
4722 *result_size = result_elt_size;
4723
4724 return true;
4725 }
4726
4727
4728 bool
4729 gfc_check_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
4730 {
4731 size_t source_size;
4732 size_t result_size;
4733
4734 if (mold->ts.type == BT_HOLLERITH)
4735 {
4736 gfc_error ("'MOLD' argument of 'TRANSFER' intrinsic at %L must not be %s",
4737 &mold->where, gfc_basic_typename (BT_HOLLERITH));
4738 return false;
4739 }
4740
4741 if (size != NULL)
4742 {
4743 if (!type_check (size, 2, BT_INTEGER))
4744 return false;
4745
4746 if (!scalar_check (size, 2))
4747 return false;
4748
4749 if (!nonoptional_check (size, 2))
4750 return false;
4751 }
4752
4753 if (!gfc_option.warn_surprising)
4754 return true;
4755
4756 /* If we can't calculate the sizes, we cannot check any more.
4757 Return true for that case. */
4758
4759 if (!gfc_calculate_transfer_sizes (source, mold, size, &source_size,
4760 &result_size, NULL))
4761 return true;
4762
4763 if (source_size < result_size)
4764 gfc_warning("Intrinsic TRANSFER at %L has partly undefined result: "
4765 "source size %ld < result size %ld", &source->where,
4766 (long) source_size, (long) result_size);
4767
4768 return true;
4769 }
4770
4771
4772 bool
4773 gfc_check_transpose (gfc_expr *matrix)
4774 {
4775 if (!rank_check (matrix, 0, 2))
4776 return false;
4777
4778 return true;
4779 }
4780
4781
4782 bool
4783 gfc_check_ubound (gfc_expr *array, gfc_expr *dim, gfc_expr *kind)
4784 {
4785 if (!array_check (array, 0))
4786 return false;
4787
4788 if (!dim_check (dim, 1, false))
4789 return false;
4790
4791 if (!dim_rank_check (dim, array, 0))
4792 return false;
4793
4794 if (!kind_check (kind, 2, BT_INTEGER))
4795 return false;
4796 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
4797 "with KIND argument at %L",
4798 gfc_current_intrinsic, &kind->where))
4799 return false;
4800
4801 return true;
4802 }
4803
4804
4805 bool
4806 gfc_check_ucobound (gfc_expr *coarray, gfc_expr *dim, gfc_expr *kind)
4807 {
4808 if (gfc_option.coarray == GFC_FCOARRAY_NONE)
4809 {
4810 gfc_fatal_error ("Coarrays disabled at %C, use -fcoarray= to enable");
4811 return false;
4812 }
4813
4814 if (!coarray_check (coarray, 0))
4815 return false;
4816
4817 if (dim != NULL)
4818 {
4819 if (!dim_check (dim, 1, false))
4820 return false;
4821
4822 if (!dim_corank_check (dim, coarray))
4823 return false;
4824 }
4825
4826 if (!kind_check (kind, 2, BT_INTEGER))
4827 return false;
4828
4829 return true;
4830 }
4831
4832
4833 bool
4834 gfc_check_unpack (gfc_expr *vector, gfc_expr *mask, gfc_expr *field)
4835 {
4836 mpz_t vector_size;
4837
4838 if (!rank_check (vector, 0, 1))
4839 return false;
4840
4841 if (!array_check (mask, 1))
4842 return false;
4843
4844 if (!type_check (mask, 1, BT_LOGICAL))
4845 return false;
4846
4847 if (!same_type_check (vector, 0, field, 2))
4848 return false;
4849
4850 if (mask->expr_type == EXPR_ARRAY
4851 && gfc_array_size (vector, &vector_size))
4852 {
4853 int mask_true_count = 0;
4854 gfc_constructor *mask_ctor;
4855 mask_ctor = gfc_constructor_first (mask->value.constructor);
4856 while (mask_ctor)
4857 {
4858 if (mask_ctor->expr->expr_type != EXPR_CONSTANT)
4859 {
4860 mask_true_count = 0;
4861 break;
4862 }
4863
4864 if (mask_ctor->expr->value.logical)
4865 mask_true_count++;
4866
4867 mask_ctor = gfc_constructor_next (mask_ctor);
4868 }
4869
4870 if (mpz_get_si (vector_size) < mask_true_count)
4871 {
4872 gfc_error ("'%s' argument of '%s' intrinsic at %L must "
4873 "provide at least as many elements as there "
4874 "are .TRUE. values in '%s' (%ld/%d)",
4875 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
4876 &vector->where, gfc_current_intrinsic_arg[1]->name,
4877 mpz_get_si (vector_size), mask_true_count);
4878 return false;
4879 }
4880
4881 mpz_clear (vector_size);
4882 }
4883
4884 if (mask->rank != field->rank && field->rank != 0)
4885 {
4886 gfc_error ("'%s' argument of '%s' intrinsic at %L must have "
4887 "the same rank as '%s' or be a scalar",
4888 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
4889 &field->where, gfc_current_intrinsic_arg[1]->name);
4890 return false;
4891 }
4892
4893 if (mask->rank == field->rank)
4894 {
4895 int i;
4896 for (i = 0; i < field->rank; i++)
4897 if (! identical_dimen_shape (mask, i, field, i))
4898 {
4899 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L "
4900 "must have identical shape.",
4901 gfc_current_intrinsic_arg[2]->name,
4902 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
4903 &field->where);
4904 }
4905 }
4906
4907 return true;
4908 }
4909
4910
4911 bool
4912 gfc_check_verify (gfc_expr *x, gfc_expr *y, gfc_expr *z, gfc_expr *kind)
4913 {
4914 if (!type_check (x, 0, BT_CHARACTER))
4915 return false;
4916
4917 if (!same_type_check (x, 0, y, 1))
4918 return false;
4919
4920 if (z != NULL && !type_check (z, 2, BT_LOGICAL))
4921 return false;
4922
4923 if (!kind_check (kind, 3, BT_INTEGER))
4924 return false;
4925 if (kind && !gfc_notify_std (GFC_STD_F2003, "'%s' intrinsic "
4926 "with KIND argument at %L",
4927 gfc_current_intrinsic, &kind->where))
4928 return false;
4929
4930 return true;
4931 }
4932
4933
4934 bool
4935 gfc_check_trim (gfc_expr *x)
4936 {
4937 if (!type_check (x, 0, BT_CHARACTER))
4938 return false;
4939
4940 if (!scalar_check (x, 0))
4941 return false;
4942
4943 return true;
4944 }
4945
4946
4947 bool
4948 gfc_check_ttynam (gfc_expr *unit)
4949 {
4950 if (!scalar_check (unit, 0))
4951 return false;
4952
4953 if (!type_check (unit, 0, BT_INTEGER))
4954 return false;
4955
4956 return true;
4957 }
4958
4959
4960 /* Common check function for the half a dozen intrinsics that have a
4961 single real argument. */
4962
4963 bool
4964 gfc_check_x (gfc_expr *x)
4965 {
4966 if (!type_check (x, 0, BT_REAL))
4967 return false;
4968
4969 return true;
4970 }
4971
4972
4973 /************* Check functions for intrinsic subroutines *************/
4974
4975 bool
4976 gfc_check_cpu_time (gfc_expr *time)
4977 {
4978 if (!scalar_check (time, 0))
4979 return false;
4980
4981 if (!type_check (time, 0, BT_REAL))
4982 return false;
4983
4984 if (!variable_check (time, 0, false))
4985 return false;
4986
4987 return true;
4988 }
4989
4990
4991 bool
4992 gfc_check_date_and_time (gfc_expr *date, gfc_expr *time,
4993 gfc_expr *zone, gfc_expr *values)
4994 {
4995 if (date != NULL)
4996 {
4997 if (!type_check (date, 0, BT_CHARACTER))
4998 return false;
4999 if (!kind_value_check (date, 0, gfc_default_character_kind))
5000 return false;
5001 if (!scalar_check (date, 0))
5002 return false;
5003 if (!variable_check (date, 0, false))
5004 return false;
5005 }
5006
5007 if (time != NULL)
5008 {
5009 if (!type_check (time, 1, BT_CHARACTER))
5010 return false;
5011 if (!kind_value_check (time, 1, gfc_default_character_kind))
5012 return false;
5013 if (!scalar_check (time, 1))
5014 return false;
5015 if (!variable_check (time, 1, false))
5016 return false;
5017 }
5018
5019 if (zone != NULL)
5020 {
5021 if (!type_check (zone, 2, BT_CHARACTER))
5022 return false;
5023 if (!kind_value_check (zone, 2, gfc_default_character_kind))
5024 return false;
5025 if (!scalar_check (zone, 2))
5026 return false;
5027 if (!variable_check (zone, 2, false))
5028 return false;
5029 }
5030
5031 if (values != NULL)
5032 {
5033 if (!type_check (values, 3, BT_INTEGER))
5034 return false;
5035 if (!array_check (values, 3))
5036 return false;
5037 if (!rank_check (values, 3, 1))
5038 return false;
5039 if (!variable_check (values, 3, false))
5040 return false;
5041 }
5042
5043 return true;
5044 }
5045
5046
5047 bool
5048 gfc_check_mvbits (gfc_expr *from, gfc_expr *frompos, gfc_expr *len,
5049 gfc_expr *to, gfc_expr *topos)
5050 {
5051 if (!type_check (from, 0, BT_INTEGER))
5052 return false;
5053
5054 if (!type_check (frompos, 1, BT_INTEGER))
5055 return false;
5056
5057 if (!type_check (len, 2, BT_INTEGER))
5058 return false;
5059
5060 if (!same_type_check (from, 0, to, 3))
5061 return false;
5062
5063 if (!variable_check (to, 3, false))
5064 return false;
5065
5066 if (!type_check (topos, 4, BT_INTEGER))
5067 return false;
5068
5069 if (!nonnegative_check ("frompos", frompos))
5070 return false;
5071
5072 if (!nonnegative_check ("topos", topos))
5073 return false;
5074
5075 if (!nonnegative_check ("len", len))
5076 return false;
5077
5078 if (!less_than_bitsize2 ("from", from, "frompos", frompos, "len", len))
5079 return false;
5080
5081 if (!less_than_bitsize2 ("to", to, "topos", topos, "len", len))
5082 return false;
5083
5084 return true;
5085 }
5086
5087
5088 bool
5089 gfc_check_random_number (gfc_expr *harvest)
5090 {
5091 if (!type_check (harvest, 0, BT_REAL))
5092 return false;
5093
5094 if (!variable_check (harvest, 0, false))
5095 return false;
5096
5097 return true;
5098 }
5099
5100
5101 bool
5102 gfc_check_random_seed (gfc_expr *size, gfc_expr *put, gfc_expr *get)
5103 {
5104 unsigned int nargs = 0, kiss_size;
5105 locus *where = NULL;
5106 mpz_t put_size, get_size;
5107 bool have_gfc_real_16; /* Try and mimic HAVE_GFC_REAL_16 in libgfortran. */
5108
5109 have_gfc_real_16 = gfc_validate_kind (BT_REAL, 16, true) != -1;
5110
5111 /* Keep the number of bytes in sync with kiss_size in
5112 libgfortran/intrinsics/random.c. */
5113 kiss_size = (have_gfc_real_16 ? 48 : 32) / gfc_default_integer_kind;
5114
5115 if (size != NULL)
5116 {
5117 if (size->expr_type != EXPR_VARIABLE
5118 || !size->symtree->n.sym->attr.optional)
5119 nargs++;
5120
5121 if (!scalar_check (size, 0))
5122 return false;
5123
5124 if (!type_check (size, 0, BT_INTEGER))
5125 return false;
5126
5127 if (!variable_check (size, 0, false))
5128 return false;
5129
5130 if (!kind_value_check (size, 0, gfc_default_integer_kind))
5131 return false;
5132 }
5133
5134 if (put != NULL)
5135 {
5136 if (put->expr_type != EXPR_VARIABLE
5137 || !put->symtree->n.sym->attr.optional)
5138 {
5139 nargs++;
5140 where = &put->where;
5141 }
5142
5143 if (!array_check (put, 1))
5144 return false;
5145
5146 if (!rank_check (put, 1, 1))
5147 return false;
5148
5149 if (!type_check (put, 1, BT_INTEGER))
5150 return false;
5151
5152 if (!kind_value_check (put, 1, gfc_default_integer_kind))
5153 return false;
5154
5155 if (gfc_array_size (put, &put_size)
5156 && mpz_get_ui (put_size) < kiss_size)
5157 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
5158 "too small (%i/%i)",
5159 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5160 where, (int) mpz_get_ui (put_size), kiss_size);
5161 }
5162
5163 if (get != NULL)
5164 {
5165 if (get->expr_type != EXPR_VARIABLE
5166 || !get->symtree->n.sym->attr.optional)
5167 {
5168 nargs++;
5169 where = &get->where;
5170 }
5171
5172 if (!array_check (get, 2))
5173 return false;
5174
5175 if (!rank_check (get, 2, 1))
5176 return false;
5177
5178 if (!type_check (get, 2, BT_INTEGER))
5179 return false;
5180
5181 if (!variable_check (get, 2, false))
5182 return false;
5183
5184 if (!kind_value_check (get, 2, gfc_default_integer_kind))
5185 return false;
5186
5187 if (gfc_array_size (get, &get_size)
5188 && mpz_get_ui (get_size) < kiss_size)
5189 gfc_error ("Size of '%s' argument of '%s' intrinsic at %L "
5190 "too small (%i/%i)",
5191 gfc_current_intrinsic_arg[2]->name, gfc_current_intrinsic,
5192 where, (int) mpz_get_ui (get_size), kiss_size);
5193 }
5194
5195 /* RANDOM_SEED may not have more than one non-optional argument. */
5196 if (nargs > 1)
5197 gfc_error ("Too many arguments to %s at %L", gfc_current_intrinsic, where);
5198
5199 return true;
5200 }
5201
5202
5203 bool
5204 gfc_check_second_sub (gfc_expr *time)
5205 {
5206 if (!scalar_check (time, 0))
5207 return false;
5208
5209 if (!type_check (time, 0, BT_REAL))
5210 return false;
5211
5212 if (!kind_value_check (time, 0, 4))
5213 return false;
5214
5215 return true;
5216 }
5217
5218
5219 /* COUNT and COUNT_MAX of SYSTEM_CLOCK are scalar, default-kind integer
5220 variables in Fortran 95. In Fortran 2003 and later, they can be of any
5221 kind, and COUNT_RATE can be of type real. Note, count, count_rate, and
5222 count_max are all optional arguments */
5223
5224 bool
5225 gfc_check_system_clock (gfc_expr *count, gfc_expr *count_rate,
5226 gfc_expr *count_max)
5227 {
5228 if (count != NULL)
5229 {
5230 if (!scalar_check (count, 0))
5231 return false;
5232
5233 if (!type_check (count, 0, BT_INTEGER))
5234 return false;
5235
5236 if (count->ts.kind != gfc_default_integer_kind
5237 && !gfc_notify_std (GFC_STD_F2003, "COUNT argument to "
5238 "SYSTEM_CLOCK at %L has non-default kind",
5239 &count->where))
5240 return false;
5241
5242 if (!variable_check (count, 0, false))
5243 return false;
5244 }
5245
5246 if (count_rate != NULL)
5247 {
5248 if (!scalar_check (count_rate, 1))
5249 return false;
5250
5251 if (!variable_check (count_rate, 1, false))
5252 return false;
5253
5254 if (count_rate->ts.type == BT_REAL)
5255 {
5256 if (!gfc_notify_std (GFC_STD_F2003, "Real COUNT_RATE argument to "
5257 "SYSTEM_CLOCK at %L", &count_rate->where))
5258 return false;
5259 }
5260 else
5261 {
5262 if (!type_check (count_rate, 1, BT_INTEGER))
5263 return false;
5264
5265 if (count_rate->ts.kind != gfc_default_integer_kind
5266 && !gfc_notify_std (GFC_STD_F2003, "COUNT_RATE argument to "
5267 "SYSTEM_CLOCK at %L has non-default kind",
5268 &count_rate->where))
5269 return false;
5270 }
5271
5272 }
5273
5274 if (count_max != NULL)
5275 {
5276 if (!scalar_check (count_max, 2))
5277 return false;
5278
5279 if (!type_check (count_max, 2, BT_INTEGER))
5280 return false;
5281
5282 if (count_max->ts.kind != gfc_default_integer_kind
5283 && !gfc_notify_std (GFC_STD_F2003, "COUNT_MAX argument to "
5284 "SYSTEM_CLOCK at %L has non-default kind",
5285 &count_max->where))
5286 return false;
5287
5288 if (!variable_check (count_max, 2, false))
5289 return false;
5290 }
5291
5292 return true;
5293 }
5294
5295
5296 bool
5297 gfc_check_irand (gfc_expr *x)
5298 {
5299 if (x == NULL)
5300 return true;
5301
5302 if (!scalar_check (x, 0))
5303 return false;
5304
5305 if (!type_check (x, 0, BT_INTEGER))
5306 return false;
5307
5308 if (!kind_value_check (x, 0, 4))
5309 return false;
5310
5311 return true;
5312 }
5313
5314
5315 bool
5316 gfc_check_alarm_sub (gfc_expr *seconds, gfc_expr *handler, gfc_expr *status)
5317 {
5318 if (!scalar_check (seconds, 0))
5319 return false;
5320 if (!type_check (seconds, 0, BT_INTEGER))
5321 return false;
5322
5323 if (!int_or_proc_check (handler, 1))
5324 return false;
5325 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5326 return false;
5327
5328 if (status == NULL)
5329 return true;
5330
5331 if (!scalar_check (status, 2))
5332 return false;
5333 if (!type_check (status, 2, BT_INTEGER))
5334 return false;
5335 if (!kind_value_check (status, 2, gfc_default_integer_kind))
5336 return false;
5337
5338 return true;
5339 }
5340
5341
5342 bool
5343 gfc_check_rand (gfc_expr *x)
5344 {
5345 if (x == NULL)
5346 return true;
5347
5348 if (!scalar_check (x, 0))
5349 return false;
5350
5351 if (!type_check (x, 0, BT_INTEGER))
5352 return false;
5353
5354 if (!kind_value_check (x, 0, 4))
5355 return false;
5356
5357 return true;
5358 }
5359
5360
5361 bool
5362 gfc_check_srand (gfc_expr *x)
5363 {
5364 if (!scalar_check (x, 0))
5365 return false;
5366
5367 if (!type_check (x, 0, BT_INTEGER))
5368 return false;
5369
5370 if (!kind_value_check (x, 0, 4))
5371 return false;
5372
5373 return true;
5374 }
5375
5376
5377 bool
5378 gfc_check_ctime_sub (gfc_expr *time, gfc_expr *result)
5379 {
5380 if (!scalar_check (time, 0))
5381 return false;
5382 if (!type_check (time, 0, BT_INTEGER))
5383 return false;
5384
5385 if (!type_check (result, 1, BT_CHARACTER))
5386 return false;
5387 if (!kind_value_check (result, 1, gfc_default_character_kind))
5388 return false;
5389
5390 return true;
5391 }
5392
5393
5394 bool
5395 gfc_check_dtime_etime (gfc_expr *x)
5396 {
5397 if (!array_check (x, 0))
5398 return false;
5399
5400 if (!rank_check (x, 0, 1))
5401 return false;
5402
5403 if (!variable_check (x, 0, false))
5404 return false;
5405
5406 if (!type_check (x, 0, BT_REAL))
5407 return false;
5408
5409 if (!kind_value_check (x, 0, 4))
5410 return false;
5411
5412 return true;
5413 }
5414
5415
5416 bool
5417 gfc_check_dtime_etime_sub (gfc_expr *values, gfc_expr *time)
5418 {
5419 if (!array_check (values, 0))
5420 return false;
5421
5422 if (!rank_check (values, 0, 1))
5423 return false;
5424
5425 if (!variable_check (values, 0, false))
5426 return false;
5427
5428 if (!type_check (values, 0, BT_REAL))
5429 return false;
5430
5431 if (!kind_value_check (values, 0, 4))
5432 return false;
5433
5434 if (!scalar_check (time, 1))
5435 return false;
5436
5437 if (!type_check (time, 1, BT_REAL))
5438 return false;
5439
5440 if (!kind_value_check (time, 1, 4))
5441 return false;
5442
5443 return true;
5444 }
5445
5446
5447 bool
5448 gfc_check_fdate_sub (gfc_expr *date)
5449 {
5450 if (!type_check (date, 0, BT_CHARACTER))
5451 return false;
5452 if (!kind_value_check (date, 0, gfc_default_character_kind))
5453 return false;
5454
5455 return true;
5456 }
5457
5458
5459 bool
5460 gfc_check_gerror (gfc_expr *msg)
5461 {
5462 if (!type_check (msg, 0, BT_CHARACTER))
5463 return false;
5464 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5465 return false;
5466
5467 return true;
5468 }
5469
5470
5471 bool
5472 gfc_check_getcwd_sub (gfc_expr *cwd, gfc_expr *status)
5473 {
5474 if (!type_check (cwd, 0, BT_CHARACTER))
5475 return false;
5476 if (!kind_value_check (cwd, 0, gfc_default_character_kind))
5477 return false;
5478
5479 if (status == NULL)
5480 return true;
5481
5482 if (!scalar_check (status, 1))
5483 return false;
5484
5485 if (!type_check (status, 1, BT_INTEGER))
5486 return false;
5487
5488 return true;
5489 }
5490
5491
5492 bool
5493 gfc_check_getarg (gfc_expr *pos, gfc_expr *value)
5494 {
5495 if (!type_check (pos, 0, BT_INTEGER))
5496 return false;
5497
5498 if (pos->ts.kind > gfc_default_integer_kind)
5499 {
5500 gfc_error ("'%s' argument of '%s' intrinsic at %L must be of a kind "
5501 "not wider than the default kind (%d)",
5502 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5503 &pos->where, gfc_default_integer_kind);
5504 return false;
5505 }
5506
5507 if (!type_check (value, 1, BT_CHARACTER))
5508 return false;
5509 if (!kind_value_check (value, 1, gfc_default_character_kind))
5510 return false;
5511
5512 return true;
5513 }
5514
5515
5516 bool
5517 gfc_check_getlog (gfc_expr *msg)
5518 {
5519 if (!type_check (msg, 0, BT_CHARACTER))
5520 return false;
5521 if (!kind_value_check (msg, 0, gfc_default_character_kind))
5522 return false;
5523
5524 return true;
5525 }
5526
5527
5528 bool
5529 gfc_check_exit (gfc_expr *status)
5530 {
5531 if (status == NULL)
5532 return true;
5533
5534 if (!type_check (status, 0, BT_INTEGER))
5535 return false;
5536
5537 if (!scalar_check (status, 0))
5538 return false;
5539
5540 return true;
5541 }
5542
5543
5544 bool
5545 gfc_check_flush (gfc_expr *unit)
5546 {
5547 if (unit == NULL)
5548 return true;
5549
5550 if (!type_check (unit, 0, BT_INTEGER))
5551 return false;
5552
5553 if (!scalar_check (unit, 0))
5554 return false;
5555
5556 return true;
5557 }
5558
5559
5560 bool
5561 gfc_check_free (gfc_expr *i)
5562 {
5563 if (!type_check (i, 0, BT_INTEGER))
5564 return false;
5565
5566 if (!scalar_check (i, 0))
5567 return false;
5568
5569 return true;
5570 }
5571
5572
5573 bool
5574 gfc_check_hostnm (gfc_expr *name)
5575 {
5576 if (!type_check (name, 0, BT_CHARACTER))
5577 return false;
5578 if (!kind_value_check (name, 0, gfc_default_character_kind))
5579 return false;
5580
5581 return true;
5582 }
5583
5584
5585 bool
5586 gfc_check_hostnm_sub (gfc_expr *name, gfc_expr *status)
5587 {
5588 if (!type_check (name, 0, BT_CHARACTER))
5589 return false;
5590 if (!kind_value_check (name, 0, gfc_default_character_kind))
5591 return false;
5592
5593 if (status == NULL)
5594 return true;
5595
5596 if (!scalar_check (status, 1))
5597 return false;
5598
5599 if (!type_check (status, 1, BT_INTEGER))
5600 return false;
5601
5602 return true;
5603 }
5604
5605
5606 bool
5607 gfc_check_itime_idate (gfc_expr *values)
5608 {
5609 if (!array_check (values, 0))
5610 return false;
5611
5612 if (!rank_check (values, 0, 1))
5613 return false;
5614
5615 if (!variable_check (values, 0, false))
5616 return false;
5617
5618 if (!type_check (values, 0, BT_INTEGER))
5619 return false;
5620
5621 if (!kind_value_check (values, 0, gfc_default_integer_kind))
5622 return false;
5623
5624 return true;
5625 }
5626
5627
5628 bool
5629 gfc_check_ltime_gmtime (gfc_expr *time, gfc_expr *values)
5630 {
5631 if (!type_check (time, 0, BT_INTEGER))
5632 return false;
5633
5634 if (!kind_value_check (time, 0, gfc_default_integer_kind))
5635 return false;
5636
5637 if (!scalar_check (time, 0))
5638 return false;
5639
5640 if (!array_check (values, 1))
5641 return false;
5642
5643 if (!rank_check (values, 1, 1))
5644 return false;
5645
5646 if (!variable_check (values, 1, false))
5647 return false;
5648
5649 if (!type_check (values, 1, BT_INTEGER))
5650 return false;
5651
5652 if (!kind_value_check (values, 1, gfc_default_integer_kind))
5653 return false;
5654
5655 return true;
5656 }
5657
5658
5659 bool
5660 gfc_check_ttynam_sub (gfc_expr *unit, gfc_expr *name)
5661 {
5662 if (!scalar_check (unit, 0))
5663 return false;
5664
5665 if (!type_check (unit, 0, BT_INTEGER))
5666 return false;
5667
5668 if (!type_check (name, 1, BT_CHARACTER))
5669 return false;
5670 if (!kind_value_check (name, 1, gfc_default_character_kind))
5671 return false;
5672
5673 return true;
5674 }
5675
5676
5677 bool
5678 gfc_check_isatty (gfc_expr *unit)
5679 {
5680 if (unit == NULL)
5681 return false;
5682
5683 if (!type_check (unit, 0, BT_INTEGER))
5684 return false;
5685
5686 if (!scalar_check (unit, 0))
5687 return false;
5688
5689 return true;
5690 }
5691
5692
5693 bool
5694 gfc_check_isnan (gfc_expr *x)
5695 {
5696 if (!type_check (x, 0, BT_REAL))
5697 return false;
5698
5699 return true;
5700 }
5701
5702
5703 bool
5704 gfc_check_perror (gfc_expr *string)
5705 {
5706 if (!type_check (string, 0, BT_CHARACTER))
5707 return false;
5708 if (!kind_value_check (string, 0, gfc_default_character_kind))
5709 return false;
5710
5711 return true;
5712 }
5713
5714
5715 bool
5716 gfc_check_umask (gfc_expr *mask)
5717 {
5718 if (!type_check (mask, 0, BT_INTEGER))
5719 return false;
5720
5721 if (!scalar_check (mask, 0))
5722 return false;
5723
5724 return true;
5725 }
5726
5727
5728 bool
5729 gfc_check_umask_sub (gfc_expr *mask, gfc_expr *old)
5730 {
5731 if (!type_check (mask, 0, BT_INTEGER))
5732 return false;
5733
5734 if (!scalar_check (mask, 0))
5735 return false;
5736
5737 if (old == NULL)
5738 return true;
5739
5740 if (!scalar_check (old, 1))
5741 return false;
5742
5743 if (!type_check (old, 1, BT_INTEGER))
5744 return false;
5745
5746 return true;
5747 }
5748
5749
5750 bool
5751 gfc_check_unlink (gfc_expr *name)
5752 {
5753 if (!type_check (name, 0, BT_CHARACTER))
5754 return false;
5755 if (!kind_value_check (name, 0, gfc_default_character_kind))
5756 return false;
5757
5758 return true;
5759 }
5760
5761
5762 bool
5763 gfc_check_unlink_sub (gfc_expr *name, gfc_expr *status)
5764 {
5765 if (!type_check (name, 0, BT_CHARACTER))
5766 return false;
5767 if (!kind_value_check (name, 0, gfc_default_character_kind))
5768 return false;
5769
5770 if (status == NULL)
5771 return true;
5772
5773 if (!scalar_check (status, 1))
5774 return false;
5775
5776 if (!type_check (status, 1, BT_INTEGER))
5777 return false;
5778
5779 return true;
5780 }
5781
5782
5783 bool
5784 gfc_check_signal (gfc_expr *number, gfc_expr *handler)
5785 {
5786 if (!scalar_check (number, 0))
5787 return false;
5788 if (!type_check (number, 0, BT_INTEGER))
5789 return false;
5790
5791 if (!int_or_proc_check (handler, 1))
5792 return false;
5793 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5794 return false;
5795
5796 return true;
5797 }
5798
5799
5800 bool
5801 gfc_check_signal_sub (gfc_expr *number, gfc_expr *handler, gfc_expr *status)
5802 {
5803 if (!scalar_check (number, 0))
5804 return false;
5805 if (!type_check (number, 0, BT_INTEGER))
5806 return false;
5807
5808 if (!int_or_proc_check (handler, 1))
5809 return false;
5810 if (handler->ts.type == BT_INTEGER && !scalar_check (handler, 1))
5811 return false;
5812
5813 if (status == NULL)
5814 return true;
5815
5816 if (!type_check (status, 2, BT_INTEGER))
5817 return false;
5818 if (!scalar_check (status, 2))
5819 return false;
5820
5821 return true;
5822 }
5823
5824
5825 bool
5826 gfc_check_system_sub (gfc_expr *cmd, gfc_expr *status)
5827 {
5828 if (!type_check (cmd, 0, BT_CHARACTER))
5829 return false;
5830 if (!kind_value_check (cmd, 0, gfc_default_character_kind))
5831 return false;
5832
5833 if (!scalar_check (status, 1))
5834 return false;
5835
5836 if (!type_check (status, 1, BT_INTEGER))
5837 return false;
5838
5839 if (!kind_value_check (status, 1, gfc_default_integer_kind))
5840 return false;
5841
5842 return true;
5843 }
5844
5845
5846 /* This is used for the GNU intrinsics AND, OR and XOR. */
5847 bool
5848 gfc_check_and (gfc_expr *i, gfc_expr *j)
5849 {
5850 if (i->ts.type != BT_INTEGER && i->ts.type != BT_LOGICAL)
5851 {
5852 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5853 "or LOGICAL", gfc_current_intrinsic_arg[0]->name,
5854 gfc_current_intrinsic, &i->where);
5855 return false;
5856 }
5857
5858 if (j->ts.type != BT_INTEGER && j->ts.type != BT_LOGICAL)
5859 {
5860 gfc_error ("'%s' argument of '%s' intrinsic at %L must be INTEGER "
5861 "or LOGICAL", gfc_current_intrinsic_arg[1]->name,
5862 gfc_current_intrinsic, &j->where);
5863 return false;
5864 }
5865
5866 if (i->ts.type != j->ts.type)
5867 {
5868 gfc_error ("'%s' and '%s' arguments of '%s' intrinsic at %L must "
5869 "have the same type", gfc_current_intrinsic_arg[0]->name,
5870 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5871 &j->where);
5872 return false;
5873 }
5874
5875 if (!scalar_check (i, 0))
5876 return false;
5877
5878 if (!scalar_check (j, 1))
5879 return false;
5880
5881 return true;
5882 }
5883
5884
5885 bool
5886 gfc_check_storage_size (gfc_expr *a, gfc_expr *kind)
5887 {
5888 if (a->ts.type == BT_ASSUMED)
5889 {
5890 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be TYPE(*)",
5891 gfc_current_intrinsic_arg[0]->name, gfc_current_intrinsic,
5892 &a->where);
5893 return false;
5894 }
5895
5896 if (a->ts.type == BT_PROCEDURE)
5897 {
5898 gfc_error ("'%s' argument of '%s' intrinsic at %L shall not be a "
5899 "procedure", gfc_current_intrinsic_arg[0]->name,
5900 gfc_current_intrinsic, &a->where);
5901 return false;
5902 }
5903
5904 if (kind == NULL)
5905 return true;
5906
5907 if (!type_check (kind, 1, BT_INTEGER))
5908 return false;
5909
5910 if (!scalar_check (kind, 1))
5911 return false;
5912
5913 if (kind->expr_type != EXPR_CONSTANT)
5914 {
5915 gfc_error ("'%s' argument of '%s' intrinsic at %L must be a constant",
5916 gfc_current_intrinsic_arg[1]->name, gfc_current_intrinsic,
5917 &kind->where);
5918 return false;
5919 }
5920
5921 return true;
5922 }