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