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