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