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