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