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