]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/resolve.c
2014-12-17 Tobias Burnus <burnus@net-b.de>
[thirdparty/gcc.git] / gcc / fortran / resolve.c
CommitLineData
69b1505f 1/* Perform type resolution on the various structures.
3aea1f79 2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4ee9c684 3 Contributed by Andy Vaught
4
c84b470d 5This file is part of GCC.
4ee9c684 6
c84b470d 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
bdabe786 9Software Foundation; either version 3, or (at your option) any later
c84b470d 10version.
4ee9c684 11
c84b470d 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.
4ee9c684 16
17You should have received a copy of the GNU General Public License
bdabe786 18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
4ee9c684 20
21#include "config.h"
7436502b 22#include "system.h"
e4d6c7fc 23#include "coretypes.h"
b6abe79c 24#include "flags.h"
4ee9c684 25#include "gfortran.h"
82efdb2e 26#include "obstack.h"
27#include "bitmap.h"
4ee9c684 28#include "arith.h" /* For gfc_compare_expr(). */
018ef8b8 29#include "dependency.h"
cbbac028 30#include "data.h"
9ba02d19 31#include "target-memory.h" /* for gfc_simplify_transfer */
126387b5 32#include "constructor.h"
7436502b 33
9e25b302 34/* Types used in equivalence statements. */
35
36typedef enum seq_type
37{
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39}
40seq_type;
4ee9c684 41
82efdb2e 42/* Stack to keep track of the nesting of blocks as we move through the
c3f3b68d 43 code. See resolve_branch() and gfc_resolve_code(). */
4ee9c684 44
45typedef struct code_stack
46{
8581350b 47 struct gfc_code *head, *current;
4ee9c684 48 struct code_stack *prev;
82efdb2e 49
50 /* This bitmap keeps track of the targets valid for a branch from
8581350b 51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
82efdb2e 53 bitmap reachable_labels;
4ee9c684 54}
55code_stack;
56
57static code_stack *cs_base = NULL;
58
59
55ea8666 60/* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
4ee9c684 61
62static int forall_flag;
8b8cc022 63int gfc_do_concurrent_flag;
4ee9c684 64
f00f6dd6 65/* True when we are resolving an expression that is an actual argument to
66 a procedure. */
67static bool actual_arg = false;
68/* True when we are resolving an expression that is the first actual argument
69 to a procedure. */
70static bool first_actual_arg = false;
71
8c2d8d6d 72
764f1175 73/* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
74
75static int omp_workshare_flag;
76
ea13b9b7 77/* Nonzero if we are processing a formal arglist. The corresponding function
78 resets the flag each time that it is read. */
79static int formal_arg_flag = 0;
80
c6b395dd 81/* True if we are resolving a specification expression. */
be844014 82static bool specification_expr = false;
c6b395dd 83
84/* The id of the last entry seen. */
85static int current_entry_id;
86
82efdb2e 87/* We use bitmaps to determine if a branch target is valid. */
88static bitmap_obstack labels_obstack;
89
e97ac7c0 90/* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91static bool inquiry_argument = false;
92
f00f6dd6 93
ea13b9b7 94int
95gfc_is_formal_arg (void)
96{
97 return formal_arg_flag;
98}
99
9386f343 100/* Is the symbol host associated? */
101static bool
102is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
103{
104 for (ns = ns->parent; ns; ns = ns->parent)
d6463863 105 {
9386f343 106 if (sym->ns == ns)
107 return true;
108 }
109
110 return false;
111}
ac5f2650 112
113/* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
114 an ABSTRACT derived-type. If where is not NULL, an error message with that
115 locus is printed, optionally using name. */
116
60e19868 117static bool
ac5f2650 118resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
119{
eeebe20b 120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
ac5f2650 121 {
122 if (where)
123 {
124 if (name)
0d2b3c9c 125 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
eeebe20b 126 name, where, ts->u.derived->name);
ac5f2650 127 else
0d2b3c9c 128 gfc_error ("ABSTRACT type %qs used at %L",
eeebe20b 129 ts->u.derived->name, where);
ac5f2650 130 }
131
60e19868 132 return false;
ac5f2650 133 }
134
60e19868 135 return true;
ac5f2650 136}
137
138
60e19868 139static bool
ea996e99 140check_proc_interface (gfc_symbol *ifc, locus *where)
f161695e 141{
87863b31 142 /* Several checks for F08:C1216. */
87863b31 143 if (ifc->attr.procedure)
f161695e 144 {
0d2b3c9c 145 gfc_error ("Interface %qs at %L is declared "
ea996e99 146 "in a later PROCEDURE statement", ifc->name, where);
60e19868 147 return false;
f161695e 148 }
87863b31 149 if (ifc->generic)
150 {
151 /* For generic interfaces, check if there is
152 a specific procedure with the same name. */
153 gfc_interface *gen = ifc->generic;
154 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
155 gen = gen->next;
156 if (!gen)
157 {
0d2b3c9c 158 gfc_error ("Interface %qs at %L may not be generic",
ea996e99 159 ifc->name, where);
60e19868 160 return false;
87863b31 161 }
162 }
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
164 {
0d2b3c9c 165 gfc_error ("Interface %qs at %L may not be a statement function",
ea996e99 166 ifc->name, where);
60e19868 167 return false;
87863b31 168 }
169 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
170 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
171 ifc->attr.intrinsic = 1;
172 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
173 {
0d2b3c9c 174 gfc_error ("Intrinsic procedure %qs not allowed in "
ea996e99 175 "PROCEDURE statement at %L", ifc->name, where);
60e19868 176 return false;
ea996e99 177 }
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
179 {
0d2b3c9c 180 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
60e19868 181 return false;
87863b31 182 }
60e19868 183 return true;
ea996e99 184}
185
186
187static void resolve_symbol (gfc_symbol *sym);
188
189
190/* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
191
60e19868 192static bool
ea996e99 193resolve_procedure_interface (gfc_symbol *sym)
194{
195 gfc_symbol *ifc = sym->ts.interface;
196
197 if (!ifc)
60e19868 198 return true;
ea996e99 199
200 if (ifc == sym)
201 {
0d2b3c9c 202 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
ea996e99 203 sym->name, &sym->declared_at);
60e19868 204 return false;
ea996e99 205 }
60e19868 206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
f161695e 208
87863b31 209 if (ifc->attr.if_source || ifc->attr.intrinsic)
f161695e 210 {
ea996e99 211 /* Resolve interface and copy attributes. */
f161695e 212 resolve_symbol (ifc);
f161695e 213 if (ifc->attr.intrinsic)
68c6e05c 214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
f161695e 215
216 if (ifc->result)
c6337549 217 {
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
220 }
d6463863 221 else
f161695e 222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
f161695e 226
227 sym->attr.allocatable = ifc->attr.allocatable;
228 sym->attr.pointer = ifc->attr.pointer;
229 sym->attr.pure = ifc->attr.pure;
230 sym->attr.elemental = ifc->attr.elemental;
231 sym->attr.dimension = ifc->attr.dimension;
232 sym->attr.contiguous = ifc->attr.contiguous;
233 sym->attr.recursive = ifc->attr.recursive;
234 sym->attr.always_explicit = ifc->attr.always_explicit;
235 sym->attr.ext_attr |= ifc->attr.ext_attr;
1ebb2958 236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
cc786707 237 sym->attr.class_ok = ifc->attr.class_ok;
f161695e 238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
f161695e 240 /* Copy char length. */
241 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
242 {
243 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
f161695e 244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
60e19868 245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
f161695e 247 }
248 }
f161695e 249
60e19868 250 return true;
f161695e 251}
252
253
4ee9c684 254/* Resolve types of formal argument lists. These have to be done early so that
255 the formal argument lists of module procedures can be copied to the
256 containing module before the individual procedures are resolved
257 individually. We also resolve argument lists of procedures in interface
258 blocks because they are self-contained scoping units.
259
260 Since a dummy argument cannot be a non-dummy procedure, the only
261 resort left for untyped names are the IMPLICIT types. */
262
263static void
1bcc6eb8 264resolve_formal_arglist (gfc_symbol *proc)
4ee9c684 265{
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
be844014 268 bool saved_specification_expr;
4ee9c684 269 int i;
270
4ee9c684 271 if (proc->result != NULL)
272 sym = proc->result;
273 else
274 sym = proc;
275
276 if (gfc_elemental (proc)
277 || sym->attr.pointer || sym->attr.allocatable
f00f6dd6 278 || (sym->as && sym->as->rank != 0))
36efa756 279 {
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
282 }
4ee9c684 283
ea13b9b7 284 formal_arg_flag = 1;
285
4ee9c684 286 for (f = proc->formal; f; f = f->next)
287 {
bc118adb 288 gfc_array_spec *as;
4ee9c684 289
1dbb2336 290 sym = f->sym;
291
4ee9c684 292 if (sym == NULL)
293 {
1bcc6eb8 294 /* Alternate return placeholder. */
4ee9c684 295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
0d2b3c9c 297 "%qs at %L is not allowed", proc->name,
4ee9c684 298 &proc->declared_at);
1bcc6eb8 299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
0d2b3c9c 301 "%qs at %L is not allowed", proc->name,
1bcc6eb8 302 &proc->declared_at);
4ee9c684 303 continue;
304 }
87863b31 305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
60e19868 306 && !resolve_procedure_interface (sym))
87863b31 307 return;
4ee9c684 308
2d472c22 309 if (strcmp (proc->name, sym->name) == 0)
310 {
311 gfc_error ("Self-referential argument "
0d2b3c9c 312 "%qs at %L is not allowed", sym->name,
2d472c22 313 &proc->declared_at);
314 return;
315 }
316
4ee9c684 317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
319
4aff5851 320 if (sym->attr.subroutine || sym->attr.external)
5684c61c 321 {
4aff5851 322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
5684c61c 324 }
4aff5851 325 else
4ee9c684 326 {
4aff5851 327 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
328 && (!sym->attr.function || sym->result == sym))
329 gfc_set_default_type (sym, 1, sym->ns);
4ee9c684 330 }
331
bc118adb 332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
334
be844014 335 saved_specification_expr = specification_expr;
336 specification_expr = true;
bc118adb 337 gfc_resolve_array_spec (as, 0);
be844014 338 specification_expr = saved_specification_expr;
4ee9c684 339
340 /* We can't tell if an array with dimension (:) is assumed or deferred
1bcc6eb8 341 shape until we know if it has the pointer or allocatable attributes.
4ee9c684 342 */
bc118adb 343 if (as && as->rank > 0 && as->type == AS_DEFERRED
344 && ((sym->ts.type != BT_CLASS
345 && !(sym->attr.pointer || sym->attr.allocatable))
346 || (sym->ts.type == BT_CLASS
347 && !(CLASS_DATA (sym)->attr.class_pointer
348 || CLASS_DATA (sym)->attr.allocatable)))
d6a853a7 349 && sym->attr.flavor != FL_PROCEDURE)
1bcc6eb8 350 {
bc118adb 351 as->type = AS_ASSUMED_SHAPE;
352 for (i = 0; i < as->rank; i++)
353 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1bcc6eb8 354 }
4ee9c684 355
bc118adb 356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
f00f6dd6 357 || (as && as->type == AS_ASSUMED_RANK)
1bcc6eb8 358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
bc118adb 359 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
360 && (CLASS_DATA (sym)->attr.class_pointer
361 || CLASS_DATA (sym)->attr.allocatable
362 || CLASS_DATA (sym)->attr.target))
1bcc6eb8 363 || sym->attr.optional)
36efa756 364 {
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
368 }
4ee9c684 369
370 /* If the flavor is unknown at this point, it has to be a variable.
1bcc6eb8 371 A procedure specification would have already set the type. */
4ee9c684 372
373 if (sym->attr.flavor == FL_UNKNOWN)
950683ed 374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
4ee9c684 375
4aff5851 376 if (gfc_pure (proc))
4ee9c684 377 {
4aff5851 378 if (sym->attr.flavor == FL_PROCEDURE)
59b292ba 379 {
4aff5851 380 /* F08:C1279. */
381 if (!gfc_pure (sym))
382 {
0d2b3c9c 383 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
4aff5851 384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
386 }
59b292ba 387 }
4aff5851 388 else if (!sym->attr.pointer)
59b292ba 389 {
4aff5851 390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
391 {
392 if (sym->attr.value)
0d2b3c9c 393 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
394 " of pure function %qs at %L with VALUE "
4aff5851 395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
0d2b3c9c 398 gfc_error ("Argument %qs of pure function %qs at %L must "
4aff5851 399 "be INTENT(IN) or VALUE", sym->name, proc->name,
400 &sym->declared_at);
401 }
402
403 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
404 {
405 if (sym->attr.value)
0d2b3c9c 406 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
407 " of pure subroutine %qs at %L with VALUE "
4aff5851 408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
0d2b3c9c 411 gfc_error ("Argument %qs of pure subroutine %qs at %L "
4aff5851 412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
415 }
59b292ba 416 }
4ee9c684 417 }
418
4aff5851 419 if (proc->attr.implicit_pure)
8b0a2e85 420 {
4aff5851 421 if (sym->attr.flavor == FL_PROCEDURE)
422 {
60e19868 423 if (!gfc_pure (sym))
4aff5851 424 proc->attr.implicit_pure = 0;
425 }
426 else if (!sym->attr.pointer)
427 {
b410f5d1 428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
4aff5851 430 proc->attr.implicit_pure = 0;
8b0a2e85 431
b410f5d1 432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
4aff5851 434 proc->attr.implicit_pure = 0;
435 }
8b0a2e85 436 }
437
4ee9c684 438 if (gfc_elemental (proc))
439 {
5684c61c 440 /* F08:C1289. */
de1cb551 441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
aff518b0 444 {
0d2b3c9c 445 gfc_error ("Coarray dummy argument %qs at %L to elemental "
aff518b0 446 "procedure", sym->name, &sym->declared_at);
447 continue;
448 }
449
de1cb551 450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
4ee9c684 452 {
0d2b3c9c 453 gfc_error ("Argument %qs of elemental procedure at %L must "
1bcc6eb8 454 "be scalar", sym->name, &sym->declared_at);
4ee9c684 455 continue;
456 }
457
de1cb551 458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
4e4ea00b 461 {
0d2b3c9c 462 gfc_error ("Argument %qs of elemental procedure at %L cannot "
4e4ea00b 463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
466 }
467
4289983a 468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
4ee9c684 471 {
716da296 472 gfc_error ("Argument %qs of elemental procedure at %L cannot "
1bcc6eb8 473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
4ee9c684 475 continue;
476 }
1d0109d5 477
478 if (sym->attr.flavor == FL_PROCEDURE)
479 {
716da296 480 gfc_error ("Dummy procedure %qs not allowed in elemental "
481 "procedure %qs at %L", sym->name, proc->name,
1d0109d5 482 &sym->declared_at);
483 continue;
484 }
4e4ea00b 485
8bd19a21 486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
4e4ea00b 488 {
716da296 489 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
8bd19a21 490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
4e4ea00b 492 &sym->declared_at);
493 continue;
494 }
4ee9c684 495 }
496
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
1bcc6eb8 499 {
500 if (sym->as != NULL)
501 {
716da296 502 gfc_error ("Argument %qs of statement function at %L must "
1bcc6eb8 503 "be scalar", sym->name, &sym->declared_at);
504 continue;
505 }
506
507 if (sym->ts.type == BT_CHARACTER)
508 {
eeebe20b 509 gfc_charlen *cl = sym->ts.u.cl;
1bcc6eb8 510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
511 {
716da296 512 gfc_error ("Character-valued argument %qs of statement "
1bcc6eb8 513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
516 }
517 }
518 }
4ee9c684 519 }
ea13b9b7 520 formal_arg_flag = 0;
4ee9c684 521}
522
523
524/* Work function called when searching for symbols that have argument lists
525 associated with them. */
526
527static void
1bcc6eb8 528find_arglists (gfc_symbol *sym)
4ee9c684 529{
c2958b6b 530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
07f0c434 531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
4ee9c684 532 return;
533
534 resolve_formal_arglist (sym);
535}
536
537
538/* Given a namespace, resolve all formal argument lists within the namespace.
539 */
540
541static void
1bcc6eb8 542resolve_formal_arglists (gfc_namespace *ns)
4ee9c684 543{
4ee9c684 544 if (ns == NULL)
545 return;
546
547 gfc_traverse_ns (ns, find_arglists);
548}
549
550
1b716045 551static void
1bcc6eb8 552resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
1b716045 553{
60e19868 554 bool t;
fc243266 555
f86aea04 556 /* If this namespace is not a function or an entry master function,
557 ignore it. */
558 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
559 || sym->attr.entry_master)
1b716045 560 return;
561
e311e7c3 562 /* Try to find out of what the return type is. */
969eb27f 563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
1b716045 564 {
04d2ca03 565 t = gfc_set_default_type (sym->result, 0, ns);
1b716045 566
60e19868 567 if (!t && !sym->result->attr.untyped)
0e633d82 568 {
04d2ca03 569 if (sym->result == sym)
716da296 570 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
04d2ca03 571 sym->name, &sym->declared_at);
1e057e9b 572 else if (!sym->result->attr.proc_pointer)
716da296 573 gfc_error ("Result %qs of contained function %qs at %L has "
04d2ca03 574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
0e633d82 577 }
1b716045 578 }
976d903a 579
d6463863 580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
1bcc6eb8 581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
2576a2df 583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
976d903a 585
04d2ca03 586 if (sym->result->ts.type == BT_CHARACTER)
976d903a 587 {
eeebe20b 588 gfc_charlen *cl = sym->result->ts.u.cl;
617125a6 589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
2576a2df 590 {
591 /* See if this is a module-procedure and adapt error message
592 accordingly. */
593 bool module_proc;
594 gcc_assert (ns->parent && ns->parent->proc_name);
595 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
596
716da296 597 gfc_error ("Character-valued %s %qs at %L must not be"
2576a2df 598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
602 }
976d903a 603 }
1b716045 604}
605
606
607/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
b14e2757 608 introduce duplicates. */
1b716045 609
610static void
611merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
612{
613 gfc_formal_arglist *f, *new_arglist;
614 gfc_symbol *new_sym;
615
616 for (; new_args != NULL; new_args = new_args->next)
617 {
618 new_sym = new_args->sym;
fc243266 619 /* See if this arg is already in the formal argument list. */
1b716045 620 for (f = proc->formal; f; f = f->next)
621 {
622 if (new_sym == f->sym)
623 break;
624 }
625
626 if (f)
627 continue;
628
629 /* Add a new argument. Argument order is not important. */
630 new_arglist = gfc_get_formal_arglist ();
631 new_arglist->sym = new_sym;
632 new_arglist->next = proc->formal;
633 proc->formal = new_arglist;
634 }
635}
636
637
2872c066 638/* Flag the arguments that are not present in all entries. */
639
640static void
641check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
642{
643 gfc_formal_arglist *f, *head;
644 head = new_args;
645
646 for (f = proc->formal; f; f = f->next)
647 {
648 if (f->sym == NULL)
649 continue;
650
651 for (new_args = head; new_args; new_args = new_args->next)
652 {
653 if (new_args->sym == f->sym)
654 break;
655 }
656
657 if (new_args)
658 continue;
659
660 f->sym->attr.not_always_present = 1;
661 }
662}
663
664
1b716045 665/* Resolve alternate entry points. If a symbol has multiple entry points we
666 create a new master symbol for the main routine, and turn the existing
667 symbol into an entry point. */
668
669static void
1bcc6eb8 670resolve_entries (gfc_namespace *ns)
1b716045 671{
672 gfc_namespace *old_ns;
673 gfc_code *c;
674 gfc_symbol *proc;
675 gfc_entry_list *el;
676 char name[GFC_MAX_SYMBOL_LEN + 1];
677 static int master_count = 0;
678
679 if (ns->proc_name == NULL)
680 return;
681
682 /* No need to do anything if this procedure doesn't have alternate entry
683 points. */
684 if (!ns->entries)
685 return;
686
687 /* We may already have resolved alternate entry points. */
688 if (ns->proc_name->attr.entry_master)
689 return;
690
b14e2757 691 /* If this isn't a procedure something has gone horribly wrong. */
22d678e8 692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
fc243266 693
1b716045 694 /* Remember the current namespace. */
695 old_ns = gfc_current_ns;
696
697 gfc_current_ns = ns;
698
699 /* Add the main entry point to the list of entry points. */
700 el = gfc_get_entry_list ();
701 el->sym = ns->proc_name;
702 el->id = 0;
703 el->next = ns->entries;
704 ns->entries = el;
705 ns->proc_name->attr.entry = 1;
706
d77f260f 707 /* If it is a module function, it needs to be in the right namespace
708 so that gfc_get_fake_result_decl can gather up the results. The
709 need for this arose in get_proc_name, where these beasts were
710 left in their own namespace, to keep prior references linked to
711 the entry declaration.*/
712 if (ns->proc_name->attr.function
1bcc6eb8 713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
d77f260f 714 el->sym->ns = ns;
715
b7b0917e 716 /* Do the same for entries where the master is not a module
717 procedure. These are retained in the module namespace because
718 of the module procedure declaration. */
719 for (el = el->next; el; el = el->next)
720 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
721 && el->sym->attr.mod_proc)
722 el->sym->ns = ns;
723 el = ns->entries;
724
1b716045 725 /* Add an entry statement for it. */
f1ab83c6 726 c = gfc_get_code (EXEC_ENTRY);
1b716045 727 c->ext.entry = el;
728 c->next = ns->code;
729 ns->code = c;
730
731 /* Create a new symbol for the master function. */
732 /* Give the internal function a unique name (within this file).
377df5f3 733 Also include the function name so the user has some hope of figuring
734 out what is going on. */
1b716045 735 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
736 master_count++, ns->proc_name->name);
1b716045 737 gfc_get_ha_symbol (name, &proc);
22d678e8 738 gcc_assert (proc != NULL);
1b716045 739
950683ed 740 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
1b716045 741 if (ns->proc_name->attr.subroutine)
950683ed 742 gfc_add_subroutine (&proc->attr, proc->name, NULL);
1b716045 743 else
744 {
c6871095 745 gfc_symbol *sym;
746 gfc_typespec *ts, *fts;
d45fced7 747 gfc_array_spec *as, *fas;
950683ed 748 gfc_add_function (&proc->attr, proc->name, NULL);
c6871095 749 proc->result = proc;
d45fced7 750 fas = ns->entries->sym->as;
751 fas = fas ? fas : ns->entries->sym->result->as;
c6871095 752 fts = &ns->entries->sym->result->ts;
753 if (fts->type == BT_UNKNOWN)
64e93293 754 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
c6871095 755 for (el = ns->entries->next; el; el = el->next)
756 {
757 ts = &el->sym->result->ts;
d45fced7 758 as = el->sym->as;
759 as = as ? as : el->sym->result->as;
c6871095 760 if (ts->type == BT_UNKNOWN)
64e93293 761 ts = gfc_get_default_type (el->sym->result->name, NULL);
d45fced7 762
c6871095 763 if (! gfc_compare_types (ts, fts)
764 || (el->sym->result->attr.dimension
765 != ns->entries->sym->result->attr.dimension)
766 || (el->sym->result->attr.pointer
767 != ns->entries->sym->result->attr.pointer))
768 break;
82190250 769 else if (as && fas && ns->entries->sym->result != el->sym->result
770 && gfc_compare_array_spec (as, fas) == 0)
407c9b80 771 gfc_error ("Function %s at %L has entries with mismatched "
d45fced7 772 "array specifications", ns->entries->sym->name,
773 &ns->entries->sym->declared_at);
407c9b80 774 /* The characteristics need to match and thus both need to have
775 the same string length, i.e. both len=*, or both len=4.
776 Having both len=<variable> is also possible, but difficult to
777 check at compile time. */
eeebe20b 778 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
779 && (((ts->u.cl->length && !fts->u.cl->length)
780 ||(!ts->u.cl->length && fts->u.cl->length))
781 || (ts->u.cl->length
782 && ts->u.cl->length->expr_type
783 != fts->u.cl->length->expr_type)
784 || (ts->u.cl->length
785 && ts->u.cl->length->expr_type == EXPR_CONSTANT
786 && mpz_cmp (ts->u.cl->length->value.integer,
787 fts->u.cl->length->value.integer) != 0)))
f25dbbf7 788 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
407c9b80 789 "entries returning variables of different "
790 "string lengths", ns->entries->sym->name,
791 &ns->entries->sym->declared_at);
c6871095 792 }
793
794 if (el == NULL)
795 {
796 sym = ns->entries->sym->result;
797 /* All result types the same. */
798 proc->ts = *fts;
799 if (sym->attr.dimension)
800 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
801 if (sym->attr.pointer)
802 gfc_add_pointer (&proc->attr, NULL);
803 }
804 else
805 {
89d91d02 806 /* Otherwise the result will be passed through a union by
c6871095 807 reference. */
808 proc->attr.mixed_entry_master = 1;
809 for (el = ns->entries; el; el = el->next)
810 {
811 sym = el->sym->result;
812 if (sym->attr.dimension)
1bcc6eb8 813 {
814 if (el == ns->entries)
815 gfc_error ("FUNCTION result %s can't be an array in "
816 "FUNCTION %s at %L", sym->name,
817 ns->entries->sym->name, &sym->declared_at);
818 else
819 gfc_error ("ENTRY result %s can't be an array in "
820 "FUNCTION %s at %L", sym->name,
821 ns->entries->sym->name, &sym->declared_at);
822 }
c6871095 823 else if (sym->attr.pointer)
1bcc6eb8 824 {
825 if (el == ns->entries)
826 gfc_error ("FUNCTION result %s can't be a POINTER in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
829 else
830 gfc_error ("ENTRY result %s can't be a POINTER in "
831 "FUNCTION %s at %L", sym->name,
832 ns->entries->sym->name, &sym->declared_at);
833 }
c6871095 834 else
835 {
836 ts = &sym->ts;
837 if (ts->type == BT_UNKNOWN)
64e93293 838 ts = gfc_get_default_type (sym->name, NULL);
c6871095 839 switch (ts->type)
840 {
841 case BT_INTEGER:
842 if (ts->kind == gfc_default_integer_kind)
843 sym = NULL;
844 break;
845 case BT_REAL:
846 if (ts->kind == gfc_default_real_kind
847 || ts->kind == gfc_default_double_kind)
848 sym = NULL;
849 break;
850 case BT_COMPLEX:
851 if (ts->kind == gfc_default_complex_kind)
852 sym = NULL;
853 break;
854 case BT_LOGICAL:
855 if (ts->kind == gfc_default_logical_kind)
856 sym = NULL;
857 break;
0e633d82 858 case BT_UNKNOWN:
859 /* We will issue error elsewhere. */
860 sym = NULL;
861 break;
c6871095 862 default:
863 break;
864 }
865 if (sym)
1bcc6eb8 866 {
867 if (el == ns->entries)
868 gfc_error ("FUNCTION result %s can't be of type %s "
869 "in FUNCTION %s at %L", sym->name,
870 gfc_typename (ts), ns->entries->sym->name,
871 &sym->declared_at);
872 else
873 gfc_error ("ENTRY result %s can't be of type %s "
874 "in FUNCTION %s at %L", sym->name,
875 gfc_typename (ts), ns->entries->sym->name,
876 &sym->declared_at);
877 }
c6871095 878 }
879 }
880 }
1b716045 881 }
882 proc->attr.access = ACCESS_PRIVATE;
883 proc->attr.entry_master = 1;
884
885 /* Merge all the entry point arguments. */
886 for (el = ns->entries; el; el = el->next)
887 merge_argument_lists (proc, el->sym->formal);
888
2872c066 889 /* Check the master formal arguments for any that are not
890 present in all entry points. */
891 for (el = ns->entries; el; el = el->next)
892 check_argument_lists (proc, el->sym->formal);
893
377df5f3 894 /* Use the master function for the function body. */
1b716045 895 ns->proc_name = proc;
896
377df5f3 897 /* Finalize the new symbols. */
1b716045 898 gfc_commit_symbols ();
899
900 /* Restore the original namespace. */
901 gfc_current_ns = old_ns;
902}
903
904
1f2d591b 905/* Resolve common variables. */
4750071e 906static void
1f2d591b 907resolve_common_vars (gfc_symbol *sym, bool named_common)
4750071e 908{
1f2d591b 909 gfc_symbol *csym = sym;
4750071e 910
1f2d591b 911 for (; csym; csym = csym->common_next)
59c8fc77 912 {
1f2d591b 913 if (csym->value || csym->attr.data)
914 {
915 if (!csym->ns->is_block_data)
0d2b3c9c 916 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1f2d591b 917 "but only in BLOCK DATA initialization is "
918 "allowed", csym->name, &csym->declared_at);
919 else if (!named_common)
0d2b3c9c 920 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1f2d591b 921 "in a blank COMMON but initialization is only "
922 "allowed in named common blocks", csym->name,
923 &csym->declared_at);
924 }
925
a90fe829 926 if (UNLIMITED_POLY (csym))
bf79c656 927 gfc_error_now ("%qs in cannot appear in COMMON at %L "
a90fe829 928 "[F2008:C5100]", csym->name, &csym->declared_at);
929
ec530640 930 if (csym->ts.type != BT_DERIVED)
931 continue;
932
eeebe20b 933 if (!(csym->ts.u.derived->attr.sequence
934 || csym->ts.u.derived->attr.is_bind_c))
bf79c656 935 gfc_error_now ("Derived type variable %qs in COMMON at %L "
ec530640 936 "has neither the SEQUENCE nor the BIND(C) "
937 "attribute", csym->name, &csym->declared_at);
eeebe20b 938 if (csym->ts.u.derived->attr.alloc_comp)
bf79c656 939 gfc_error_now ("Derived type variable %qs in COMMON at %L "
ec530640 940 "has an ultimate component that is "
941 "allocatable", csym->name, &csym->declared_at);
08262510 942 if (gfc_has_default_initializer (csym->ts.u.derived))
bf79c656 943 gfc_error_now ("Derived type variable %qs in COMMON at %L "
ec530640 944 "may not have default initializer", csym->name,
945 &csym->declared_at);
bb80cc66 946
947 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
948 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
59c8fc77 949 }
1f2d591b 950}
951
952/* Resolve common blocks. */
953static void
954resolve_common_blocks (gfc_symtree *common_root)
955{
956 gfc_symbol *sym;
7a914593 957 gfc_gsymbol * gsym;
1f2d591b 958
959 if (common_root == NULL)
960 return;
961
962 if (common_root->left)
963 resolve_common_blocks (common_root->left);
964 if (common_root->right)
965 resolve_common_blocks (common_root->right);
966
967 resolve_common_vars (common_root->n.common->head, true);
4750071e 968
7a914593 969 /* The common name is a global name - in Fortran 2003 also if it has a
970 C binding name, since Fortran 2008 only the C binding name is a global
971 identifier. */
972 if (!common_root->n.common->binding_label
973 || gfc_notification_std (GFC_STD_F2008))
974 {
975 gsym = gfc_find_gsymbol (gfc_gsym_root,
976 common_root->n.common->name);
977
978 if (gsym && gfc_notification_std (GFC_STD_F2008)
979 && gsym->type == GSYM_COMMON
980 && ((common_root->n.common->binding_label
981 && (!gsym->binding_label
982 || strcmp (common_root->n.common->binding_label,
983 gsym->binding_label) != 0))
984 || (!common_root->n.common->binding_label
985 && gsym->binding_label)))
986 {
716da296 987 gfc_error_1 ("In Fortran 2003 COMMON '%s' block at %L is a global "
7a914593 988 "identifier and must thus have the same binding name "
989 "as the same-named COMMON block at %L: %s vs %s",
990 common_root->n.common->name, &common_root->n.common->where,
991 &gsym->where,
992 common_root->n.common->binding_label
993 ? common_root->n.common->binding_label : "(blank)",
994 gsym->binding_label ? gsym->binding_label : "(blank)");
995 return;
996 }
997
998 if (gsym && gsym->type != GSYM_COMMON
999 && !common_root->n.common->binding_label)
1000 {
716da296 1001 gfc_error_1 ("COMMON block '%s' at %L uses the same global identifier "
7a914593 1002 "as entity at %L",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where);
1005 return;
1006 }
1007 if (gsym && gsym->type != GSYM_COMMON)
1008 {
716da296 1009 gfc_error_1 ("Fortran 2008: COMMON block '%s' with binding label at "
7a914593 1010 "%L sharing the identifier with global non-COMMON-block "
1011 "entity at %L", common_root->n.common->name,
1012 &common_root->n.common->where, &gsym->where);
1013 return;
1014 }
1015 if (!gsym)
1016 {
1017 gsym = gfc_get_gsymbol (common_root->n.common->name);
1018 gsym->type = GSYM_COMMON;
1019 gsym->where = common_root->n.common->where;
1020 gsym->defined = 1;
1021 }
1022 gsym->used = 1;
1023 }
1024
1025 if (common_root->n.common->binding_label)
1026 {
1027 gsym = gfc_find_gsymbol (gfc_gsym_root,
1028 common_root->n.common->binding_label);
1029 if (gsym && gsym->type != GSYM_COMMON)
1030 {
716da296 1031 gfc_error_1 ("COMMON block at %L with binding label %s uses the same "
7a914593 1032 "global identifier as entity at %L",
1033 &common_root->n.common->where,
1034 common_root->n.common->binding_label, &gsym->where);
1035 return;
1036 }
1037 if (!gsym)
1038 {
1039 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1040 gsym->type = GSYM_COMMON;
1041 gsym->where = common_root->n.common->where;
1042 gsym->defined = 1;
1043 }
1044 gsym->used = 1;
1045 }
1046
59c8fc77 1047 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1048 if (sym == NULL)
1049 return;
1050
1051 if (sym->attr.flavor == FL_PARAMETER)
716da296 1052 gfc_error_1 ("COMMON block '%s' at %L is used as PARAMETER at %L",
59c8fc77 1053 sym->name, &common_root->n.common->where, &sym->declared_at);
1054
092781dc 1055 if (sym->attr.external)
716da296 1056 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
092781dc 1057 sym->name, &common_root->n.common->where);
1058
59c8fc77 1059 if (sym->attr.intrinsic)
716da296 1060 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
59c8fc77 1061 sym->name, &common_root->n.common->where);
1062 else if (sym->attr.result
20859373 1063 || gfc_is_function_return_value (sym, gfc_current_ns))
0d2b3c9c 1064 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
59c8fc77 1065 "that is also a function result", sym->name,
1066 &common_root->n.common->where);
1067 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1068 && sym->attr.proc != PROC_ST_FUNCTION)
0d2b3c9c 1069 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
59c8fc77 1070 "that is also a global procedure", sym->name,
1071 &common_root->n.common->where);
4750071e 1072}
1073
1074
4ee9c684 1075/* Resolve contained function types. Because contained functions can call one
1076 another, they have to be worked out before any of the contained procedures
1077 can be resolved.
1078
1079 The good news is that if a function doesn't already have a type, the only
1080 way it can get one is through an IMPLICIT type or a RESULT variable, because
1081 by definition contained functions are contained namespace they're contained
1082 in, not in a sibling or parent namespace. */
1083
1084static void
1bcc6eb8 1085resolve_contained_functions (gfc_namespace *ns)
4ee9c684 1086{
4ee9c684 1087 gfc_namespace *child;
1b716045 1088 gfc_entry_list *el;
4ee9c684 1089
1090 resolve_formal_arglists (ns);
1091
1092 for (child = ns->contained; child; child = child->sibling)
1093 {
1b716045 1094 /* Resolve alternate entry points first. */
fc243266 1095 resolve_entries (child);
4ee9c684 1096
1b716045 1097 /* Then check function return types. */
1098 resolve_contained_fntype (child->proc_name, child);
1099 for (el = child->entries; el; el = el->next)
1100 resolve_contained_fntype (el->sym, child);
4ee9c684 1101 }
1102}
1103
1104
60e19868 1105static bool resolve_fl_derived0 (gfc_symbol *sym);
f959368d 1106
1107
4ee9c684 1108/* Resolve all of the elements of a structure constructor and make sure that
23d075f4 1109 the types are correct. The 'init' flag indicates that the given
1110 constructor is an initializer. */
4ee9c684 1111
60e19868 1112static bool
23d075f4 1113resolve_structure_cons (gfc_expr *expr, int init)
4ee9c684 1114{
1115 gfc_constructor *cons;
1116 gfc_component *comp;
60e19868 1117 bool t;
2294b616 1118 symbol_attribute a;
4ee9c684 1119
60e19868 1120 t = true;
ec2c6976 1121
1122 if (expr->ts.type == BT_DERIVED)
f959368d 1123 resolve_fl_derived0 (expr->ts.u.derived);
ec2c6976 1124
126387b5 1125 cons = gfc_constructor_first (expr->value.constructor);
4ee9c684 1126
c2958b6b 1127 /* A constructor may have references if it is the result of substituting a
1128 parameter variable. In this case we just pull out the component we
1129 want. */
1130 if (expr->ref)
1131 comp = expr->ref->u.c.sym->components;
1132 else
1133 comp = expr->ts.u.derived->components;
1134
126387b5 1135 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
4ee9c684 1136 {
4516f139 1137 int rank;
1138
1bcc6eb8 1139 if (!cons->expr)
73f88052 1140 continue;
4ee9c684 1141
60e19868 1142 if (!gfc_resolve_expr (cons->expr))
4ee9c684 1143 {
60e19868 1144 t = false;
4ee9c684 1145 continue;
1146 }
1147
4516f139 1148 rank = comp->as ? comp->as->rank : 0;
1149 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
3be2b8d5 1150 && (comp->attr.allocatable || cons->expr->rank))
2294b616 1151 {
74e666d3 1152 gfc_error ("The rank of the element in the structure "
2294b616 1153 "constructor at %L does not match that of the "
1154 "component (%d/%d)", &cons->expr->where,
4516f139 1155 cons->expr->rank, rank);
60e19868 1156 t = false;
2294b616 1157 }
1158
4ee9c684 1159 /* If we don't have the right type, try to convert it. */
1160
23d075f4 1161 if (!comp->attr.proc_pointer &&
1162 !gfc_compare_types (&cons->expr->ts, &comp->ts))
840e5aa1 1163 {
607ae689 1164 if (strcmp (comp->name, "_extends") == 0)
09c509ed 1165 {
607ae689 1166 /* Can afford to be brutal with the _extends initializer.
09c509ed 1167 The derived type can get lost because it is PRIVATE
1168 but it is not usage constrained by the standard. */
1169 cons->expr->ts = comp->ts;
09c509ed 1170 }
1171 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
16f7554b 1172 {
1173 gfc_error ("The element in the structure constructor at %L, "
716da296 1174 "for pointer component %qs, is %s but should be %s",
16f7554b 1175 &cons->expr->where, comp->name,
1176 gfc_basic_typename (cons->expr->ts.type),
1177 gfc_basic_typename (comp->ts.type));
60e19868 1178 t = false;
16f7554b 1179 }
840e5aa1 1180 else
16f7554b 1181 {
60e19868 1182 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1183 if (t)
16f7554b 1184 t = t2;
1185 }
840e5aa1 1186 }
2294b616 1187
459a1bdc 1188 /* For strings, the length of the constructor should be the same as
1189 the one of the structure, ensure this if the lengths are known at
1190 compile time and when we are dealing with PARAMETER or structure
1191 constructors. */
1192 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1193 && comp->ts.u.cl->length
1194 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1195 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1196 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
0c806cea 1197 && cons->expr->rank != 0
459a1bdc 1198 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1199 comp->ts.u.cl->length->value.integer) != 0)
1200 {
1201 if (cons->expr->expr_type == EXPR_VARIABLE
1202 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1203 {
1204 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1205 to make use of the gfc_resolve_character_array_constructor
1206 machinery. The expression is later simplified away to
1207 an array of string literals. */
1208 gfc_expr *para = cons->expr;
1209 cons->expr = gfc_get_expr ();
1210 cons->expr->ts = para->ts;
1211 cons->expr->where = para->where;
1212 cons->expr->expr_type = EXPR_ARRAY;
1213 cons->expr->rank = para->rank;
1214 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1215 gfc_constructor_append_expr (&cons->expr->value.constructor,
1216 para, &cons->expr->where);
1217 }
1218 if (cons->expr->expr_type == EXPR_ARRAY)
1219 {
1220 gfc_constructor *p;
1221 p = gfc_constructor_first (cons->expr->value.constructor);
1222 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1223 {
5c3a6ccb 1224 gfc_charlen *cl, *cl2;
1225
1226 cl2 = NULL;
1227 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1228 {
1229 if (cl == cons->expr->ts.u.cl)
1230 break;
1231 cl2 = cl;
1232 }
1233
1234 gcc_assert (cl);
1235
1236 if (cl2)
1237 cl2->next = cl->next;
1238
1239 gfc_free_expr (cl->length);
434f0922 1240 free (cl);
459a1bdc 1241 }
1242
5c3a6ccb 1243 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
459a1bdc 1244 cons->expr->ts.u.cl->length_from_typespec = true;
1245 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1246 gfc_resolve_character_array_constructor (cons->expr);
1247 }
1248 }
1249
9277c291 1250 if (cons->expr->expr_type == EXPR_NULL
64e93293 1251 && !(comp->attr.pointer || comp->attr.allocatable
07f0c434 1252 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1de1b1a9 1253 || (comp->ts.type == BT_CLASS
a33fbb6f 1254 && (CLASS_DATA (comp)->attr.class_pointer
50b4b37b 1255 || CLASS_DATA (comp)->attr.allocatable))))
9277c291 1256 {
60e19868 1257 t = false;
74e666d3 1258 gfc_error ("The NULL in the structure constructor at %L is "
716da296 1259 "being applied to component %qs, which is neither "
9277c291 1260 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1261 comp->name);
1262 }
1263
74e666d3 1264 if (comp->attr.proc_pointer && comp->ts.interface)
1265 {
1266 /* Check procedure pointer interface. */
1267 gfc_symbol *s2 = NULL;
1268 gfc_component *c2;
1269 const char *name;
1270 char err[200];
1271
b3961d7b 1272 c2 = gfc_get_proc_ptr_comp (cons->expr);
1273 if (c2)
74e666d3 1274 {
1275 s2 = c2->ts.interface;
1276 name = c2->name;
1277 }
1278 else if (cons->expr->expr_type == EXPR_FUNCTION)
1279 {
1280 s2 = cons->expr->symtree->n.sym->result;
1281 name = cons->expr->symtree->n.sym->result->name;
1282 }
1283 else if (cons->expr->expr_type != EXPR_NULL)
1284 {
1285 s2 = cons->expr->symtree->n.sym;
1286 name = cons->expr->symtree->n.sym->name;
1287 }
1288
1289 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
bfc1ce10 1290 err, sizeof (err), NULL, NULL))
74e666d3 1291 {
1292 gfc_error ("Interface mismatch for procedure-pointer component "
716da296 1293 "%qs in structure constructor at %L: %s",
74e666d3 1294 comp->name, &cons->expr->where, err);
60e19868 1295 return false;
74e666d3 1296 }
1297 }
1298
d720de1e 1299 if (!comp->attr.pointer || comp->attr.proc_pointer
1300 || cons->expr->expr_type == EXPR_NULL)
2294b616 1301 continue;
1302
1303 a = gfc_expr_attr (cons->expr);
1304
1305 if (!a.pointer && !a.target)
1306 {
60e19868 1307 t = false;
74e666d3 1308 gfc_error ("The element in the structure constructor at %L, "
716da296 1309 "for pointer component %qs should be a POINTER or "
2294b616 1310 "a TARGET", &cons->expr->where, comp->name);
1311 }
895e6dfa 1312
23d075f4 1313 if (init)
1314 {
1315 /* F08:C461. Additional checks for pointer initialization. */
1316 if (a.allocatable)
1317 {
60e19868 1318 t = false;
23d075f4 1319 gfc_error ("Pointer initialization target at %L "
1320 "must not be ALLOCATABLE ", &cons->expr->where);
1321 }
1322 if (!a.save)
1323 {
60e19868 1324 t = false;
23d075f4 1325 gfc_error ("Pointer initialization target at %L "
1326 "must have the SAVE attribute", &cons->expr->where);
1327 }
1328 }
1329
895e6dfa 1330 /* F2003, C1272 (3). */
c77badf3 1331 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1332 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1333 || gfc_is_coindexed (cons->expr));
1334 if (impure && gfc_pure (NULL))
895e6dfa 1335 {
60e19868 1336 t = false;
74e666d3 1337 gfc_error ("Invalid expression in the structure constructor for "
716da296 1338 "pointer component %qs at %L in PURE procedure",
e97ac7c0 1339 comp->name, &cons->expr->where);
895e6dfa 1340 }
23d075f4 1341
c77badf3 1342 if (impure)
1343 gfc_unset_implicit_pure (NULL);
4ee9c684 1344 }
1345
1346 return t;
1347}
1348
1349
4ee9c684 1350/****************** Expression name resolution ******************/
1351
1352/* Returns 0 if a symbol was not declared with a type or
1089cf27 1353 attribute declaration statement, nonzero otherwise. */
4ee9c684 1354
1355static int
1bcc6eb8 1356was_declared (gfc_symbol *sym)
4ee9c684 1357{
1358 symbol_attribute a;
1359
1360 a = sym->attr;
1361
1362 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1363 return 1;
1364
644564ff 1365 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1bcc6eb8 1366 || a.optional || a.pointer || a.save || a.target || a.volatile_
738928be 1367 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
aff518b0 1368 || a.asynchronous || a.codimension)
4ee9c684 1369 return 1;
1370
1371 return 0;
1372}
1373
1374
1375/* Determine if a symbol is generic or not. */
1376
1377static int
1bcc6eb8 1378generic_sym (gfc_symbol *sym)
4ee9c684 1379{
1380 gfc_symbol *s;
1381
1382 if (sym->attr.generic ||
1383 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1384 return 1;
1385
1386 if (was_declared (sym) || sym->ns->parent == NULL)
1387 return 0;
1388
1389 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
d6463863 1390
21411f94 1391 if (s != NULL)
1392 {
1393 if (s == sym)
1394 return 0;
1395 else
1396 return generic_sym (s);
1397 }
4ee9c684 1398
21411f94 1399 return 0;
4ee9c684 1400}
1401
1402
1403/* Determine if a symbol is specific or not. */
1404
1405static int
1bcc6eb8 1406specific_sym (gfc_symbol *sym)
4ee9c684 1407{
1408 gfc_symbol *s;
1409
1410 if (sym->attr.if_source == IFSRC_IFBODY
1411 || sym->attr.proc == PROC_MODULE
1412 || sym->attr.proc == PROC_INTERNAL
1413 || sym->attr.proc == PROC_ST_FUNCTION
1bcc6eb8 1414 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
4ee9c684 1415 || sym->attr.external)
1416 return 1;
1417
1418 if (was_declared (sym) || sym->ns->parent == NULL)
1419 return 0;
1420
1421 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1422
1423 return (s == NULL) ? 0 : specific_sym (s);
1424}
1425
1426
1427/* Figure out if the procedure is specific, generic or unknown. */
1428
1429typedef enum
1430{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1431proc_type;
1432
1433static proc_type
1bcc6eb8 1434procedure_kind (gfc_symbol *sym)
4ee9c684 1435{
4ee9c684 1436 if (generic_sym (sym))
1437 return PTYPE_GENERIC;
1438
1439 if (specific_sym (sym))
1440 return PTYPE_SPECIFIC;
1441
1442 return PTYPE_UNKNOWN;
1443}
1444
6bfab0c0 1445/* Check references to assumed size arrays. The flag need_full_assumed_size
179eba08 1446 is nonzero when matching actual arguments. */
6bfab0c0 1447
1448static int need_full_assumed_size = 0;
1449
1450static bool
1bcc6eb8 1451check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
6bfab0c0 1452{
1bcc6eb8 1453 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
6bfab0c0 1454 return false;
1455
590c3166 1456 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1457 What should it be? */
72cf5e11 1458 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
c3e2d7e5 1459 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
590c3166 1460 && (e->ref->u.ar.type == AR_FULL))
6bfab0c0 1461 {
1462 gfc_error ("The upper bound in the last dimension must "
1463 "appear in the reference to the assumed size "
716da296 1464 "array %qs at %L", sym->name, &e->where);
6bfab0c0 1465 return true;
1466 }
1467 return false;
1468}
1469
1470
1471/* Look for bad assumed size array references in argument expressions
1472 of elemental and array valued intrinsic procedures. Since this is
1473 called from procedure resolution functions, it only recurses at
1474 operators. */
1475
1476static bool
1477resolve_assumed_size_actual (gfc_expr *e)
1478{
1479 if (e == NULL)
1480 return false;
1481
1482 switch (e->expr_type)
1483 {
1484 case EXPR_VARIABLE:
1bcc6eb8 1485 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
6bfab0c0 1486 return true;
1487 break;
1488
1489 case EXPR_OP:
1490 if (resolve_assumed_size_actual (e->value.op.op1)
1bcc6eb8 1491 || resolve_assumed_size_actual (e->value.op.op2))
6bfab0c0 1492 return true;
1493 break;
1494
1495 default:
1496 break;
1497 }
1498 return false;
1499}
1500
4ee9c684 1501
f6f6f726 1502/* Check a generic procedure, passed as an actual argument, to see if
1503 there is a matching specific name. If none, it is an error, and if
1504 more than one, the reference is ambiguous. */
1505static int
1506count_specific_procs (gfc_expr *e)
1507{
1508 int n;
1509 gfc_interface *p;
1510 gfc_symbol *sym;
d6463863 1511
f6f6f726 1512 n = 0;
1513 sym = e->symtree->n.sym;
1514
1515 for (p = sym->generic; p; p = p->next)
1516 if (strcmp (sym->name, p->sym->name) == 0)
1517 {
1518 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1519 sym->name);
1520 n++;
1521 }
1522
1523 if (n > 1)
716da296 1524 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
f6f6f726 1525 &e->where);
1526
1527 if (n == 0)
716da296 1528 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
f6f6f726 1529 "argument at %L", sym->name, &e->where);
1530
1531 return n;
1532}
1533
76279446 1534
4fafe6df 1535/* See if a call to sym could possibly be a not allowed RECURSION because of
df084314 1536 a missing RECURSIVE declaration. This means that either sym is the current
4fafe6df 1537 context itself, or sym is the parent of a contained procedure calling its
1538 non-RECURSIVE containing procedure.
1539 This also works if sym is an ENTRY. */
1540
1541static bool
1542is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1543{
1544 gfc_symbol* proc_sym;
1545 gfc_symbol* context_proc;
6a7084d7 1546 gfc_namespace* real_context;
4fafe6df 1547
c2958b6b 1548 if (sym->attr.flavor == FL_PROGRAM
1549 || sym->attr.flavor == FL_DERIVED)
595aea75 1550 return false;
1551
4fafe6df 1552 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1553
1554 /* If we've got an ENTRY, find real procedure. */
1555 if (sym->attr.entry && sym->ns->entries)
1556 proc_sym = sym->ns->entries->sym;
1557 else
1558 proc_sym = sym;
1559
1560 /* If sym is RECURSIVE, all is well of course. */
829d7a08 1561 if (proc_sym->attr.recursive || flag_recursive)
4fafe6df 1562 return false;
1563
6a7084d7 1564 /* Find the context procedure's "real" symbol if it has entries.
1565 We look for a procedure symbol, so recurse on the parents if we don't
1566 find one (like in case of a BLOCK construct). */
1567 for (real_context = context; ; real_context = real_context->parent)
1568 {
1569 /* We should find something, eventually! */
1570 gcc_assert (real_context);
1571
1572 context_proc = (real_context->entries ? real_context->entries->sym
1573 : real_context->proc_name);
1574
1575 /* In some special cases, there may not be a proc_name, like for this
1576 invalid code:
1577 real(bad_kind()) function foo () ...
1578 when checking the call to bad_kind ().
1579 In these cases, we simply return here and assume that the
1580 call is ok. */
1581 if (!context_proc)
1582 return false;
1583
1584 if (context_proc->attr.flavor != FL_LABEL)
1585 break;
1586 }
4fafe6df 1587
1588 /* A call from sym's body to itself is recursion, of course. */
1589 if (context_proc == proc_sym)
1590 return true;
1591
1592 /* The same is true if context is a contained procedure and sym the
1593 containing one. */
1594 if (context_proc->attr.contained)
1595 {
1596 gfc_symbol* parent_proc;
1597
1598 gcc_assert (context->parent);
1599 parent_proc = (context->parent->entries ? context->parent->entries->sym
1600 : context->parent->proc_name);
1601
1602 if (parent_proc == proc_sym)
1603 return true;
1604 }
1605
1606 return false;
1607}
1608
1609
180a5dc0 1610/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1611 its typespec and formal argument list. */
1612
60e19868 1613bool
68c6e05c 1614gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
180a5dc0 1615{
75471ad0 1616 gfc_intrinsic_sym* isym = NULL;
2eb87b8c 1617 const char* symstd;
1618
1619 if (sym->formal)
60e19868 1620 return true;
2eb87b8c 1621
e0ac4206 1622 /* Already resolved. */
1623 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
60e19868 1624 return true;
e0ac4206 1625
2eb87b8c 1626 /* We already know this one is an intrinsic, so we don't call
1627 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1628 gfc_find_subroutine directly to check whether it is a function or
1629 subroutine. */
1630
07f0c434 1631 if (sym->intmod_sym_id && sym->attr.subroutine)
1632 {
1633 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1634 isym = gfc_intrinsic_subroutine_by_id (id);
1635 }
1636 else if (sym->intmod_sym_id)
1637 {
1638 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1639 isym = gfc_intrinsic_function_by_id (id);
1640 }
1d7b6409 1641 else if (!sym->attr.subroutine)
75471ad0 1642 isym = gfc_find_function (sym->name);
1643
07f0c434 1644 if (isym && !sym->attr.subroutine)
180a5dc0 1645 {
8290d53f 1646 if (sym->ts.type != BT_UNKNOWN && warn_surprising
2eb87b8c 1647 && !sym->attr.implicit_type)
4166acc7 1648 gfc_warning (OPT_Wsurprising,
1649 "Type specified for intrinsic function %qs at %L is"
2eb87b8c 1650 " ignored", sym->name, &sym->declared_at);
1651
180a5dc0 1652 if (!sym->attr.function &&
60e19868 1653 !gfc_add_function(&sym->attr, sym->name, loc))
1654 return false;
2eb87b8c 1655
180a5dc0 1656 sym->ts = isym->ts;
1657 }
07f0c434 1658 else if (isym || (isym = gfc_find_subroutine (sym->name)))
180a5dc0 1659 {
2eb87b8c 1660 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1661 {
716da296 1662 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
2eb87b8c 1663 " specifier", sym->name, &sym->declared_at);
60e19868 1664 return false;
2eb87b8c 1665 }
1666
180a5dc0 1667 if (!sym->attr.subroutine &&
60e19868 1668 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1669 return false;
180a5dc0 1670 }
2eb87b8c 1671 else
1672 {
716da296 1673 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
2eb87b8c 1674 &sym->declared_at);
60e19868 1675 return false;
2eb87b8c 1676 }
1677
4b36c1ce 1678 gfc_copy_formal_args_intr (sym, isym, NULL);
2eb87b8c 1679
9b0e3203 1680 sym->attr.pure = isym->pure;
1681 sym->attr.elemental = isym->elemental;
1682
2eb87b8c 1683 /* Check it is actually available in the standard settings. */
60e19868 1684 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
2eb87b8c 1685 {
0d2b3c9c 1686 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1687 "available in the current standard settings but %s. Use "
1688 "an appropriate %<-std=*%> option or enable "
1689 "%<-fall-intrinsics%> in order to use it.",
2eb87b8c 1690 sym->name, &sym->declared_at, symstd);
60e19868 1691 return false;
2eb87b8c 1692 }
1693
60e19868 1694 return true;
180a5dc0 1695}
1696
1697
76279446 1698/* Resolve a procedure expression, like passing it to a called procedure or as
1699 RHS for a procedure pointer assignment. */
1700
60e19868 1701static bool
76279446 1702resolve_procedure_expression (gfc_expr* expr)
1703{
1704 gfc_symbol* sym;
1705
4fafe6df 1706 if (expr->expr_type != EXPR_VARIABLE)
60e19868 1707 return true;
76279446 1708 gcc_assert (expr->symtree);
4fafe6df 1709
76279446 1710 sym = expr->symtree->n.sym;
180a5dc0 1711
1712 if (sym->attr.intrinsic)
68c6e05c 1713 gfc_resolve_intrinsic (sym, &expr->where);
180a5dc0 1714
4fafe6df 1715 if (sym->attr.flavor != FL_PROCEDURE
1716 || (sym->attr.function && sym->result == sym))
60e19868 1717 return true;
76279446 1718
1719 /* A non-RECURSIVE procedure that is used as procedure expression within its
1720 own body is in danger of being called recursively. */
4fafe6df 1721 if (is_illegal_recursion (sym, gfc_current_ns))
4166acc7 1722 gfc_warning ("Non-RECURSIVE procedure %qs at %L is possibly calling"
76279446 1723 " itself recursively. Declare it RECURSIVE or use"
4166acc7 1724 " %<-frecursive%>", sym->name, &expr->where);
d6463863 1725
60e19868 1726 return true;
76279446 1727}
1728
1729
4ee9c684 1730/* Resolve an actual argument list. Most of the time, this is just
1731 resolving the expressions in the list.
1732 The exception is that we sometimes have to decide whether arguments
1733 that look like procedure arguments are really simple variable
1734 references. */
1735
60e19868 1736static bool
f6f6f726 1737resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1738 bool no_formal_args)
4ee9c684 1739{
1740 gfc_symbol *sym;
1741 gfc_symtree *parent_st;
1742 gfc_expr *e;
f175008f 1743 gfc_component *comp;
67170043 1744 int save_need_full_assumed_size;
60e19868 1745 bool return_value = false;
f00f6dd6 1746 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
e97ac7c0 1747
f00f6dd6 1748 actual_arg = true;
1749 first_actual_arg = true;
8c2d8d6d 1750
4ee9c684 1751 for (; arg; arg = arg->next)
1752 {
4ee9c684 1753 e = arg->expr;
1754 if (e == NULL)
1bcc6eb8 1755 {
1756 /* Check the label is a valid branching target. */
1757 if (arg->label)
1758 {
1759 if (arg->label->defined == ST_LABEL_UNKNOWN)
1760 {
1761 gfc_error ("Label %d referenced at %L is never defined",
1762 arg->label->value, &arg->label->where);
f00f6dd6 1763 goto cleanup;
1bcc6eb8 1764 }
1765 }
f00f6dd6 1766 first_actual_arg = false;
1bcc6eb8 1767 continue;
1768 }
4ee9c684 1769
fe5c28d2 1770 if (e->expr_type == EXPR_VARIABLE
f6f6f726 1771 && e->symtree->n.sym->attr.generic
1772 && no_formal_args
1773 && count_specific_procs (e) != 1)
f00f6dd6 1774 goto cleanup;
920901ac 1775
4ee9c684 1776 if (e->ts.type != BT_PROCEDURE)
1777 {
67170043 1778 save_need_full_assumed_size = need_full_assumed_size;
590c3166 1779 if (e->expr_type != EXPR_VARIABLE)
67170043 1780 need_full_assumed_size = 0;
60e19868 1781 if (!gfc_resolve_expr (e))
f00f6dd6 1782 goto cleanup;
67170043 1783 need_full_assumed_size = save_need_full_assumed_size;
8d7cdc4d 1784 goto argument_list;
4ee9c684 1785 }
1786
1bcc6eb8 1787 /* See if the expression node should really be a variable reference. */
4ee9c684 1788
1789 sym = e->symtree->n.sym;
1790
1791 if (sym->attr.flavor == FL_PROCEDURE
1792 || sym->attr.intrinsic
1793 || sym->attr.external)
1794 {
37e0271a 1795 int actual_ok;
4ee9c684 1796
94a286ff 1797 /* If a procedure is not already determined to be something else
1798 check if it is intrinsic. */
87863b31 1799 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
94a286ff 1800 sym->attr.intrinsic = 1;
1801
693c40a7 1802 if (sym->attr.proc == PROC_ST_FUNCTION)
1803 {
716da296 1804 gfc_error ("Statement function %qs at %L is not allowed as an "
693c40a7 1805 "actual argument", sym->name, &e->where);
1806 }
1807
1bcc6eb8 1808 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1809 sym->attr.subroutine);
37e0271a 1810 if (sym->attr.intrinsic && actual_ok == 0)
1811 {
716da296 1812 gfc_error ("Intrinsic %qs at %L is not allowed as an "
37e0271a 1813 "actual argument", sym->name, &e->where);
1814 }
37e0271a 1815
693c40a7 1816 if (sym->attr.contained && !sym->attr.use_assoc
1817 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1818 {
0d2b3c9c 1819 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
080819af 1820 " used as actual argument at %L",
60e19868 1821 sym->name, &e->where))
f00f6dd6 1822 goto cleanup;
693c40a7 1823 }
1824
1825 if (sym->attr.elemental && !sym->attr.intrinsic)
1826 {
716da296 1827 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
1bcc6eb8 1828 "allowed as an actual argument at %L", sym->name,
693c40a7 1829 &e->where);
1830 }
fc61d1a9 1831
096d4ad9 1832 /* Check if a generic interface has a specific procedure
1833 with the same name before emitting an error. */
f6f6f726 1834 if (sym->attr.generic && count_specific_procs (e) != 1)
f00f6dd6 1835 goto cleanup;
1836
f6f6f726 1837 /* Just in case a specific was found for the expression. */
1838 sym = e->symtree->n.sym;
d95efb59 1839
4ee9c684 1840 /* If the symbol is the function that names the current (or
1841 parent) scope, then we really have a variable reference. */
1842
20859373 1843 if (gfc_is_function_return_value (sym, sym->ns))
4ee9c684 1844 goto got_variable;
1845
be6157d7 1846 /* If all else fails, see if we have a specific intrinsic. */
f0127d87 1847 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
be6157d7 1848 {
1849 gfc_intrinsic_sym *isym;
f7d7a083 1850
be6157d7 1851 isym = gfc_find_function (sym->name);
1852 if (isym == NULL || !isym->specific)
1853 {
1854 gfc_error ("Unable to find a specific INTRINSIC procedure "
716da296 1855 "for the reference %qs at %L", sym->name,
be6157d7 1856 &e->where);
f00f6dd6 1857 goto cleanup;
be6157d7 1858 }
1859 sym->ts = isym->ts;
f7d7a083 1860 sym->attr.intrinsic = 1;
f0127d87 1861 sym->attr.function = 1;
be6157d7 1862 }
76279446 1863
60e19868 1864 if (!gfc_resolve_expr (e))
f00f6dd6 1865 goto cleanup;
8d7cdc4d 1866 goto argument_list;
4ee9c684 1867 }
1868
1869 /* See if the name is a module procedure in a parent unit. */
1870
1871 if (was_declared (sym) || sym->ns->parent == NULL)
1872 goto got_variable;
1873
1874 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1875 {
716da296 1876 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
f00f6dd6 1877 goto cleanup;
4ee9c684 1878 }
1879
1880 if (parent_st == NULL)
1881 goto got_variable;
1882
1883 sym = parent_st->n.sym;
1884 e->symtree = parent_st; /* Point to the right thing. */
1885
1886 if (sym->attr.flavor == FL_PROCEDURE
1887 || sym->attr.intrinsic
1888 || sym->attr.external)
1889 {
60e19868 1890 if (!gfc_resolve_expr (e))
f00f6dd6 1891 goto cleanup;
8d7cdc4d 1892 goto argument_list;
4ee9c684 1893 }
1894
1895 got_variable:
1896 e->expr_type = EXPR_VARIABLE;
1897 e->ts = sym->ts;
3a19c063 1898 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1899 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1900 && CLASS_DATA (sym)->as))
4ee9c684 1901 {
3a19c063 1902 e->rank = sym->ts.type == BT_CLASS
1903 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
4ee9c684 1904 e->ref = gfc_get_ref ();
1905 e->ref->type = REF_ARRAY;
1906 e->ref->u.ar.type = AR_FULL;
3a19c063 1907 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1908 ? CLASS_DATA (sym)->as : sym->as;
4ee9c684 1909 }
8d7cdc4d 1910
18f801dd 1911 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1912 primary.c (match_actual_arg). If above code determines that it
1913 is a variable instead, it needs to be resolved as it was not
1914 done at the beginning of this function. */
67170043 1915 save_need_full_assumed_size = need_full_assumed_size;
590c3166 1916 if (e->expr_type != EXPR_VARIABLE)
67170043 1917 need_full_assumed_size = 0;
60e19868 1918 if (!gfc_resolve_expr (e))
f00f6dd6 1919 goto cleanup;
67170043 1920 need_full_assumed_size = save_need_full_assumed_size;
18f801dd 1921
8d7cdc4d 1922 argument_list:
1923 /* Check argument list functions %VAL, %LOC and %REF. There is
1924 nothing to do for %REF. */
1925 if (arg->name && arg->name[0] == '%')
1926 {
1927 if (strncmp ("%VAL", arg->name, 4) == 0)
1928 {
1929 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1930 {
1931 gfc_error ("By-value argument at %L is not of numeric "
1932 "type", &e->where);
f00f6dd6 1933 goto cleanup;
8d7cdc4d 1934 }
1935
1936 if (e->rank)
1937 {
1938 gfc_error ("By-value argument at %L cannot be an array or "
1939 "an array section", &e->where);
f00f6dd6 1940 goto cleanup;
8d7cdc4d 1941 }
1942
1943 /* Intrinsics are still PROC_UNKNOWN here. However,
1944 since same file external procedures are not resolvable
1945 in gfortran, it is a good deal easier to leave them to
1946 intrinsic.c. */
b8128c7b 1947 if (ptype != PROC_UNKNOWN
1948 && ptype != PROC_DUMMY
ed05a89c 1949 && ptype != PROC_EXTERNAL
1950 && ptype != PROC_MODULE)
8d7cdc4d 1951 {
1952 gfc_error ("By-value argument at %L is not allowed "
1953 "in this context", &e->where);
f00f6dd6 1954 goto cleanup;
8d7cdc4d 1955 }
8d7cdc4d 1956 }
1957
1958 /* Statement functions have already been excluded above. */
1959 else if (strncmp ("%LOC", arg->name, 4) == 0
1bcc6eb8 1960 && e->ts.type == BT_PROCEDURE)
8d7cdc4d 1961 {
1962 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1963 {
1964 gfc_error ("Passing internal procedure at %L by location "
1965 "not allowed", &e->where);
f00f6dd6 1966 goto cleanup;
8d7cdc4d 1967 }
1968 }
1969 }
e97ac7c0 1970
f175008f 1971 comp = gfc_get_proc_ptr_comp(e);
1972 if (comp && comp->attr.elemental)
1973 {
1974 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
1975 "allowed as an actual argument at %L", comp->name,
1976 &e->where);
1977 }
1978
e97ac7c0 1979 /* Fortran 2008, C1237. */
1980 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
f00f6dd6 1981 && gfc_has_ultimate_pointer (e))
1982 {
1983 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
e97ac7c0 1984 "component", &e->where);
f00f6dd6 1985 goto cleanup;
1986 }
1987
1988 first_actual_arg = false;
4ee9c684 1989 }
1990
60e19868 1991 return_value = true;
f00f6dd6 1992
1993cleanup:
1994 actual_arg = actual_arg_sav;
1995 first_actual_arg = first_actual_arg_sav;
1996
1997 return return_value;
4ee9c684 1998}
1999
2000
d7b90372 2001/* Do the checks of the actual argument list that are specific to elemental
2002 procedures. If called with c == NULL, we have a function, otherwise if
2003 expr == NULL, we have a subroutine. */
1bcc6eb8 2004
60e19868 2005static bool
d7b90372 2006resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2007{
2008 gfc_actual_arglist *arg0;
2009 gfc_actual_arglist *arg;
2010 gfc_symbol *esym = NULL;
2011 gfc_intrinsic_sym *isym = NULL;
2012 gfc_expr *e = NULL;
2013 gfc_intrinsic_arg *iformal = NULL;
2014 gfc_formal_arglist *eformal = NULL;
2015 bool formal_optional = false;
2016 bool set_by_optional = false;
2017 int i;
2018 int rank = 0;
2019
2020 /* Is this an elemental procedure? */
2021 if (expr && expr->value.function.actual != NULL)
2022 {
2023 if (expr->value.function.esym != NULL
1bcc6eb8 2024 && expr->value.function.esym->attr.elemental)
d7b90372 2025 {
2026 arg0 = expr->value.function.actual;
2027 esym = expr->value.function.esym;
2028 }
2029 else if (expr->value.function.isym != NULL
1bcc6eb8 2030 && expr->value.function.isym->elemental)
d7b90372 2031 {
2032 arg0 = expr->value.function.actual;
2033 isym = expr->value.function.isym;
2034 }
2035 else
60e19868 2036 return true;
d7b90372 2037 }
88ce30eb 2038 else if (c && c->ext.actual != NULL)
d7b90372 2039 {
2040 arg0 = c->ext.actual;
d6463863 2041
88ce30eb 2042 if (c->resolved_sym)
2043 esym = c->resolved_sym;
2044 else
2045 esym = c->symtree->n.sym;
2046 gcc_assert (esym);
2047
2048 if (!esym->attr.elemental)
60e19868 2049 return true;
d7b90372 2050 }
2051 else
60e19868 2052 return true;
d7b90372 2053
2054 /* The rank of an elemental is the rank of its array argument(s). */
2055 for (arg = arg0; arg; arg = arg->next)
2056 {
f00f6dd6 2057 if (arg->expr != NULL && arg->expr->rank != 0)
d7b90372 2058 {
2059 rank = arg->expr->rank;
2060 if (arg->expr->expr_type == EXPR_VARIABLE
1bcc6eb8 2061 && arg->expr->symtree->n.sym->attr.optional)
d7b90372 2062 set_by_optional = true;
2063
2064 /* Function specific; set the result rank and shape. */
2065 if (expr)
2066 {
2067 expr->rank = rank;
2068 if (!expr->shape && arg->expr->shape)
2069 {
2070 expr->shape = gfc_get_shape (rank);
2071 for (i = 0; i < rank; i++)
2072 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2073 }
2074 }
2075 break;
2076 }
2077 }
2078
2079 /* If it is an array, it shall not be supplied as an actual argument
2080 to an elemental procedure unless an array of the same rank is supplied
2081 as an actual argument corresponding to a nonoptional dummy argument of
2082 that elemental procedure(12.4.1.5). */
2083 formal_optional = false;
2084 if (isym)
2085 iformal = isym->formal;
2086 else
2087 eformal = esym->formal;
2088
2089 for (arg = arg0; arg; arg = arg->next)
2090 {
2091 if (eformal)
2092 {
2093 if (eformal->sym && eformal->sym->attr.optional)
2094 formal_optional = true;
2095 eformal = eformal->next;
2096 }
2097 else if (isym && iformal)
2098 {
2099 if (iformal->optional)
2100 formal_optional = true;
2101 iformal = iformal->next;
2102 }
2103 else if (isym)
2104 formal_optional = true;
2105
b6abe79c 2106 if (pedantic && arg->expr != NULL
1bcc6eb8 2107 && arg->expr->expr_type == EXPR_VARIABLE
2108 && arg->expr->symtree->n.sym->attr.optional
2109 && formal_optional
2110 && arg->expr->rank
2111 && (set_by_optional || arg->expr->rank != rank)
55cb4417 2112 && !(isym && isym->id == GFC_ISYM_CONVERSION))
d7b90372 2113 {
4166acc7 2114 gfc_warning ("%qs at %L is an array and OPTIONAL; IF IT IS "
b6abe79c 2115 "MISSING, it cannot be the actual argument of an "
1bcc6eb8 2116 "ELEMENTAL procedure unless there is a non-optional "
b6abe79c 2117 "argument with the same rank (12.4.1.5)",
2118 arg->expr->symtree->n.sym->name, &arg->expr->where);
d7b90372 2119 }
2120 }
2121
2122 for (arg = arg0; arg; arg = arg->next)
2123 {
2124 if (arg->expr == NULL || arg->expr->rank == 0)
2125 continue;
2126
2127 /* Being elemental, the last upper bound of an assumed size array
2128 argument must be present. */
2129 if (resolve_assumed_size_actual (arg->expr))
60e19868 2130 return false;
d7b90372 2131
aadb5322 2132 /* Elemental procedure's array actual arguments must conform. */
d7b90372 2133 if (e != NULL)
2134 {
60e19868 2135 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2136 return false;
d7b90372 2137 }
2138 else
2139 e = arg->expr;
2140 }
2141
f6c9396c 2142 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2143 is an array, the intent inout/out variable needs to be also an array. */
2144 if (rank > 0 && esym && expr == NULL)
2145 for (eformal = esym->formal, arg = arg0; arg && eformal;
2146 arg = arg->next, eformal = eformal->next)
2147 if ((eformal->sym->attr.intent == INTENT_OUT
2148 || eformal->sym->attr.intent == INTENT_INOUT)
2149 && arg->expr && arg->expr->rank == 0)
2150 {
716da296 2151 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2152 "ELEMENTAL subroutine %qs is a scalar, but another "
f6c9396c 2153 "actual argument is an array", &arg->expr->where,
2154 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2155 : "INOUT", eformal->sym->name, esym->name);
60e19868 2156 return false;
f6c9396c 2157 }
60e19868 2158 return true;
d7b90372 2159}
2160
2161
858f9894 2162/* This function does the checking of references to global procedures
2163 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2164 77 and 95 standards. It checks for a gsymbol for the name, making
2165 one if it does not already exist. If it already exists, then the
2166 reference being resolved must correspond to the type of gsymbol.
fc243266 2167 Otherwise, the new symbol is equipped with the attributes of the
858f9894 2168 reference. The corresponding code that is called in creating
83aeedb9 2169 global entities is parse.c.
2170
2171 In addition, for all but -std=legacy, the gsymbols are used to
2172 check the interfaces of external procedures from the same file.
2173 The namespace of the gsymbol is resolved and then, once this is
2174 done the interface is checked. */
858f9894 2175
7ea64434 2176
2177static bool
2178not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2179{
2180 if (!gsym_ns->proc_name->attr.recursive)
2181 return true;
2182
2183 if (sym->ns == gsym_ns)
2184 return false;
2185
2186 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2187 return false;
2188
2189 return true;
2190}
2191
2192static bool
2193not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2194{
2195 if (gsym_ns->entries)
2196 {
2197 gfc_entry_list *entry = gsym_ns->entries;
2198
2199 for (; entry; entry = entry->next)
2200 {
2201 if (strcmp (sym->name, entry->sym->name) == 0)
2202 {
2203 if (strcmp (gsym_ns->proc_name->name,
2204 sym->ns->proc_name->name) == 0)
2205 return false;
2206
2207 if (sym->ns->parent
2208 && strcmp (gsym_ns->proc_name->name,
2209 sym->ns->parent->proc_name->name) == 0)
2210 return false;
2211 }
2212 }
2213 }
2214 return true;
2215}
2216
b596030c 2217
2218/* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2219
2220bool
2221gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2222{
2223 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2224
2225 for ( ; arg; arg = arg->next)
2226 {
2227 if (!arg->sym)
2228 continue;
2229
2230 if (arg->sym->attr.allocatable) /* (2a) */
2231 {
2232 strncpy (errmsg, _("allocatable argument"), err_len);
2233 return true;
2234 }
2235 else if (arg->sym->attr.asynchronous)
2236 {
2237 strncpy (errmsg, _("asynchronous argument"), err_len);
2238 return true;
2239 }
2240 else if (arg->sym->attr.optional)
2241 {
2242 strncpy (errmsg, _("optional argument"), err_len);
2243 return true;
2244 }
2245 else if (arg->sym->attr.pointer)
2246 {
2247 strncpy (errmsg, _("pointer argument"), err_len);
2248 return true;
2249 }
2250 else if (arg->sym->attr.target)
2251 {
2252 strncpy (errmsg, _("target argument"), err_len);
2253 return true;
2254 }
2255 else if (arg->sym->attr.value)
2256 {
2257 strncpy (errmsg, _("value argument"), err_len);
2258 return true;
2259 }
2260 else if (arg->sym->attr.volatile_)
2261 {
2262 strncpy (errmsg, _("volatile argument"), err_len);
2263 return true;
2264 }
2265 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2266 {
2267 strncpy (errmsg, _("assumed-shape argument"), err_len);
2268 return true;
2269 }
2270 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2271 {
2272 strncpy (errmsg, _("assumed-rank argument"), err_len);
2273 return true;
2274 }
2275 else if (arg->sym->attr.codimension) /* (2c) */
2276 {
2277 strncpy (errmsg, _("coarray argument"), err_len);
2278 return true;
2279 }
2280 else if (false) /* (2d) TODO: parametrized derived type */
2281 {
2282 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2283 return true;
2284 }
2285 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2286 {
2287 strncpy (errmsg, _("polymorphic argument"), err_len);
2288 return true;
2289 }
fa76a552 2290 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2291 {
2292 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2293 return true;
2294 }
b596030c 2295 else if (arg->sym->ts.type == BT_ASSUMED)
2296 {
2297 /* As assumed-type is unlimited polymorphic (cf. above).
2298 See also TS 29113, Note 6.1. */
2299 strncpy (errmsg, _("assumed-type argument"), err_len);
2300 return true;
2301 }
2302 }
2303
2304 if (sym->attr.function)
2305 {
2306 gfc_symbol *res = sym->result ? sym->result : sym;
2307
2308 if (res->attr.dimension) /* (3a) */
2309 {
2310 strncpy (errmsg, _("array result"), err_len);
2311 return true;
2312 }
2313 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2314 {
2315 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2316 return true;
2317 }
2318 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2319 && res->ts.u.cl->length
2320 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2321 {
2322 strncpy (errmsg, _("result with non-constant character length"), err_len);
2323 return true;
2324 }
2325 }
2326
9b0e3203 2327 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
b596030c 2328 {
2329 strncpy (errmsg, _("elemental procedure"), err_len);
2330 return true;
2331 }
2332 else if (sym->attr.is_bind_c) /* (5) */
2333 {
2334 strncpy (errmsg, _("bind(c) procedure"), err_len);
2335 return true;
2336 }
2337
2338 return false;
2339}
2340
2341
b49755cf 2342static void
83aeedb9 2343resolve_global_procedure (gfc_symbol *sym, locus *where,
2344 gfc_actual_arglist **actual, int sub)
858f9894 2345{
2346 gfc_gsymbol * gsym;
83aeedb9 2347 gfc_namespace *ns;
8458f4ca 2348 enum gfc_symbol_type type;
b596030c 2349 char reason[200];
858f9894 2350
2351 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2352
da5c730d 2353 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
858f9894 2354
2355 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
cbbac028 2356 gfc_global_used (gsym, where);
858f9894 2357
044bbd71 2358 if ((sym->attr.if_source == IFSRC_UNKNOWN
2359 || sym->attr.if_source == IFSRC_IFBODY)
2360 && gsym->type != GSYM_UNKNOWN
cf3654f0 2361 && !gsym->binding_label
044bbd71 2362 && gsym->ns
2363 && gsym->ns->resolved != -1
2364 && gsym->ns->proc_name
2365 && not_in_recursive (sym, gsym->ns)
2366 && not_entry_self_reference (sym, gsym->ns))
83aeedb9 2367 {
99e32e83 2368 gfc_symbol *def_sym;
2369
7e33d332 2370 /* Resolve the gsymbol namespace if needed. */
83aeedb9 2371 if (!gsym->ns->resolved)
7ea64434 2372 {
2373 gfc_dt_list *old_dt_list;
b6740dda 2374 struct gfc_omp_saved_state old_omp_state;
7ea64434 2375
2376 /* Stash away derived types so that the backend_decls do not
2377 get mixed up. */
2378 old_dt_list = gfc_derived_types;
2379 gfc_derived_types = NULL;
b6740dda 2380 /* And stash away openmp state. */
2381 gfc_omp_save_and_clear_state (&old_omp_state);
7ea64434 2382
2383 gfc_resolve (gsym->ns);
2384
2385 /* Store the new derived types with the global namespace. */
2386 if (gfc_derived_types)
2387 gsym->ns->derived_types = gfc_derived_types;
2388
2389 /* Restore the derived types of this namespace. */
2390 gfc_derived_types = old_dt_list;
b6740dda 2391 /* And openmp state. */
2392 gfc_omp_restore_state (&old_omp_state);
7ea64434 2393 }
2394
7e33d332 2395 /* Make sure that translation for the gsymbol occurs before
2396 the procedure currently being resolved. */
2397 ns = gfc_global_ns_list;
2398 for (; ns && ns != gsym->ns; ns = ns->sibling)
2399 {
2400 if (ns->sibling == gsym->ns)
2401 {
2402 ns->sibling = gsym->ns->sibling;
2403 gsym->ns->sibling = gfc_global_ns_list;
2404 gfc_global_ns_list = gsym->ns;
2405 break;
2406 }
2407 }
2408
99e32e83 2409 def_sym = gsym->ns->proc_name;
c8b913ab 2410
2411 /* This can happen if a binding name has been specified. */
2412 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2413 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2414
99e32e83 2415 if (def_sym->attr.entry_master)
2416 {
2417 gfc_entry_list *entry;
2418 for (entry = gsym->ns->entries; entry; entry = entry->next)
2419 if (strcmp (entry->sym->name, sym->name) == 0)
2420 {
2421 def_sym = entry->sym;
2422 break;
2423 }
2424 }
2425
b596030c 2426 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
d5156c99 2427 {
716da296 2428 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
b596030c 2429 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2430 gfc_typename (&def_sym->ts));
2431 goto done;
d5156c99 2432 }
2433
b596030c 2434 if (sym->attr.if_source == IFSRC_UNKNOWN
2435 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
d5156c99 2436 {
716da296 2437 gfc_error ("Explicit interface required for %qs at %L: %s",
b596030c 2438 sym->name, &sym->declared_at, reason);
2439 goto done;
ab0a1ed6 2440 }
2441
b596030c 2442 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2443 /* Turn erros into warnings with -std=gnu and -std=legacy. */
fbc62641 2444 gfc_errors_to_warnings (true);
ab0a1ed6 2445
b596030c 2446 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2447 reason, sizeof(reason), NULL, NULL))
080819af 2448 {
716da296 2449 gfc_error ("Interface mismatch in global procedure %qs at %L: %s ",
b596030c 2450 sym->name, &sym->declared_at, reason);
2451 goto done;
d5156c99 2452 }
2453
044bbd71 2454 if (!pedantic
d5156c99 2455 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2456 && !(gfc_option.warn_std & GFC_STD_GNU)))
fbc62641 2457 gfc_errors_to_warnings (true);
83aeedb9 2458
d6463863 2459 if (sym->attr.if_source != IFSRC_IFBODY)
d896f9b3 2460 gfc_procedure_use (def_sym, actual, where);
83aeedb9 2461 }
080819af 2462
b596030c 2463done:
fbc62641 2464 gfc_errors_to_warnings (false);
83aeedb9 2465
858f9894 2466 if (gsym->type == GSYM_UNKNOWN)
2467 {
2468 gsym->type = type;
2469 gsym->where = *where;
2470 }
2471
2472 gsym->used = 1;
2473}
018ef8b8 2474
1bcc6eb8 2475
4ee9c684 2476/************* Function resolution *************/
2477
2478/* Resolve a function call known to be generic.
2479 Section 14.1.2.4.1. */
2480
2481static match
1bcc6eb8 2482resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
4ee9c684 2483{
2484 gfc_symbol *s;
2485
2486 if (sym->attr.generic)
2487 {
1bcc6eb8 2488 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
4ee9c684 2489 if (s != NULL)
2490 {
2491 expr->value.function.name = s->name;
2492 expr->value.function.esym = s;
10b07432 2493
2494 if (s->ts.type != BT_UNKNOWN)
2495 expr->ts = s->ts;
2496 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2497 expr->ts = s->result->ts;
2498
4ee9c684 2499 if (s->as != NULL)
2500 expr->rank = s->as->rank;
10b07432 2501 else if (s->result != NULL && s->result->as != NULL)
2502 expr->rank = s->result->as->rank;
2503
fd149f95 2504 gfc_set_sym_referenced (expr->value.function.esym);
2505
4ee9c684 2506 return MATCH_YES;
2507 }
2508
1bcc6eb8 2509 /* TODO: Need to search for elemental references in generic
2510 interface. */
4ee9c684 2511 }
2512
2513 if (sym->attr.intrinsic)
2514 return gfc_intrinsic_func_interface (expr, 0);
2515
2516 return MATCH_NO;
2517}
2518
2519
60e19868 2520static bool
1bcc6eb8 2521resolve_generic_f (gfc_expr *expr)
4ee9c684 2522{
2523 gfc_symbol *sym;
2524 match m;
c2958b6b 2525 gfc_interface *intr = NULL;
4ee9c684 2526
2527 sym = expr->symtree->n.sym;
2528
2529 for (;;)
2530 {
2531 m = resolve_generic_f0 (expr, sym);
2532 if (m == MATCH_YES)
60e19868 2533 return true;
4ee9c684 2534 else if (m == MATCH_ERROR)
60e19868 2535 return false;
4ee9c684 2536
2537generic:
c2958b6b 2538 if (!intr)
2539 for (intr = sym->generic; intr; intr = intr->next)
2540 if (intr->sym->attr.flavor == FL_DERIVED)
2541 break;
2542
4ee9c684 2543 if (sym->ns->parent == NULL)
2544 break;
2545 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2546
2547 if (sym == NULL)
2548 break;
2549 if (!generic_sym (sym))
2550 goto generic;
2551 }
2552
3186f695 2553 /* Last ditch attempt. See if the reference is to an intrinsic
2554 that possesses a matching interface. 14.1.2.4 */
c2958b6b 2555 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
4ee9c684 2556 {
716da296 2557 gfc_error ("There is no specific function for the generic %qs "
c2958b6b 2558 "at %L", expr->symtree->n.sym->name, &expr->where);
60e19868 2559 return false;
4ee9c684 2560 }
2561
c2958b6b 2562 if (intr)
2563 {
080819af 2564 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
60e19868 2565 NULL, false))
2566 return false;
c2958b6b 2567 return resolve_structure_cons (expr, 0);
2568 }
2569
4ee9c684 2570 m = gfc_intrinsic_func_interface (expr, 0);
2571 if (m == MATCH_YES)
60e19868 2572 return true;
c2958b6b 2573
4ee9c684 2574 if (m == MATCH_NO)
716da296 2575 gfc_error ("Generic function %qs at %L is not consistent with a "
1bcc6eb8 2576 "specific intrinsic interface", expr->symtree->n.sym->name,
2577 &expr->where);
4ee9c684 2578
60e19868 2579 return false;
4ee9c684 2580}
2581
2582
2583/* Resolve a function call known to be specific. */
2584
2585static match
1bcc6eb8 2586resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
4ee9c684 2587{
2588 match m;
2589
2590 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2591 {
2592 if (sym->attr.dummy)
2593 {
2594 sym->attr.proc = PROC_DUMMY;
2595 goto found;
2596 }
2597
2598 sym->attr.proc = PROC_EXTERNAL;
2599 goto found;
2600 }
2601
2602 if (sym->attr.proc == PROC_MODULE
2603 || sym->attr.proc == PROC_ST_FUNCTION
2604 || sym->attr.proc == PROC_INTERNAL)
2605 goto found;
2606
2607 if (sym->attr.intrinsic)
2608 {
2609 m = gfc_intrinsic_func_interface (expr, 1);
2610 if (m == MATCH_YES)
2611 return MATCH_YES;
2612 if (m == MATCH_NO)
716da296 2613 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
1bcc6eb8 2614 "with an intrinsic", sym->name, &expr->where);
4ee9c684 2615
2616 return MATCH_ERROR;
2617 }
2618
2619 return MATCH_NO;
2620
2621found:
2622 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2623
eee4a6d8 2624 if (sym->result)
2625 expr->ts = sym->result->ts;
2626 else
2627 expr->ts = sym->ts;
4ee9c684 2628 expr->value.function.name = sym->name;
2629 expr->value.function.esym = sym;
94228add 2630 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2631 expr->rank = CLASS_DATA (sym)->as->rank;
2632 else if (sym->as != NULL)
4ee9c684 2633 expr->rank = sym->as->rank;
2634
2635 return MATCH_YES;
2636}
2637
2638
60e19868 2639static bool
1bcc6eb8 2640resolve_specific_f (gfc_expr *expr)
4ee9c684 2641{
2642 gfc_symbol *sym;
2643 match m;
2644
2645 sym = expr->symtree->n.sym;
2646
2647 for (;;)
2648 {
2649 m = resolve_specific_f0 (sym, expr);
2650 if (m == MATCH_YES)
60e19868 2651 return true;
4ee9c684 2652 if (m == MATCH_ERROR)
60e19868 2653 return false;
4ee9c684 2654
2655 if (sym->ns->parent == NULL)
2656 break;
2657
2658 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2659
2660 if (sym == NULL)
2661 break;
2662 }
2663
716da296 2664 gfc_error ("Unable to resolve the specific function %qs at %L",
4ee9c684 2665 expr->symtree->n.sym->name, &expr->where);
2666
60e19868 2667 return true;
4ee9c684 2668}
2669
2670
2671/* Resolve a procedure call not known to be generic nor specific. */
2672
60e19868 2673static bool
1bcc6eb8 2674resolve_unknown_f (gfc_expr *expr)
4ee9c684 2675{
2676 gfc_symbol *sym;
2677 gfc_typespec *ts;
2678
2679 sym = expr->symtree->n.sym;
2680
2681 if (sym->attr.dummy)
2682 {
2683 sym->attr.proc = PROC_DUMMY;
2684 expr->value.function.name = sym->name;
2685 goto set_type;
2686 }
2687
2688 /* See if we have an intrinsic function reference. */
2689
a34926ba 2690 if (gfc_is_intrinsic (sym, 0, expr->where))
4ee9c684 2691 {
2692 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
60e19868 2693 return true;
2694 return false;
4ee9c684 2695 }
2696
2697 /* The reference is to an external name. */
2698
2699 sym->attr.proc = PROC_EXTERNAL;
2700 expr->value.function.name = sym->name;
2701 expr->value.function.esym = expr->symtree->n.sym;
2702
2703 if (sym->as != NULL)
2704 expr->rank = sym->as->rank;
2705
2706 /* Type of the expression is either the type of the symbol or the
2707 default type of the symbol. */
2708
2709set_type:
2710 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2711
2712 if (sym->ts.type != BT_UNKNOWN)
2713 expr->ts = sym->ts;
2714 else
2715 {
64e93293 2716 ts = gfc_get_default_type (sym->name, sym->ns);
4ee9c684 2717
2718 if (ts->type == BT_UNKNOWN)
2719 {
716da296 2720 gfc_error ("Function %qs at %L has no IMPLICIT type",
4ee9c684 2721 sym->name, &expr->where);
60e19868 2722 return false;
4ee9c684 2723 }
2724 else
2725 expr->ts = *ts;
2726 }
2727
60e19868 2728 return true;
4ee9c684 2729}
2730
2731
6b98ca3d 2732/* Return true, if the symbol is an external procedure. */
2733static bool
2734is_external_proc (gfc_symbol *sym)
2735{
2736 if (!sym->attr.dummy && !sym->attr.contained
87863b31 2737 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
6b98ca3d 2738 && sym->attr.proc != PROC_ST_FUNCTION
d6df670a 2739 && !sym->attr.proc_pointer
6b98ca3d 2740 && !sym->attr.use_assoc
2741 && sym->name)
2742 return true;
a34926ba 2743
2744 return false;
6b98ca3d 2745}
2746
2747
36ae04f2 2748/* Figure out if a function reference is pure or not. Also set the name
2749 of the function for a potential error message. Return nonzero if the
4ee9c684 2750 function is PURE, zero if not. */
1acb400a 2751static int
2752pure_stmt_function (gfc_expr *, gfc_symbol *);
4ee9c684 2753
2754static int
1bcc6eb8 2755pure_function (gfc_expr *e, const char **name)
4ee9c684 2756{
2757 int pure;
0252ef5c 2758 gfc_component *comp;
4ee9c684 2759
77423564 2760 *name = NULL;
2761
ac8d4d79 2762 if (e->symtree != NULL
2763 && e->symtree->n.sym != NULL
2764 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
1acb400a 2765 return pure_stmt_function (e, e->symtree->n.sym);
ac8d4d79 2766
0252ef5c 2767 comp = gfc_get_proc_ptr_comp (e);
2768 if (comp)
2769 {
2770 pure = gfc_pure (comp->ts.interface);
2771 *name = comp->name;
2772 }
2773 else if (e->value.function.esym)
4ee9c684 2774 {
2775 pure = gfc_pure (e->value.function.esym);
2776 *name = e->value.function.esym->name;
2777 }
2778 else if (e->value.function.isym)
2779 {
2780 pure = e->value.function.isym->pure
1bcc6eb8 2781 || e->value.function.isym->elemental;
4ee9c684 2782 *name = e->value.function.isym->name;
2783 }
2784 else
2785 {
2786 /* Implicit functions are not pure. */
2787 pure = 0;
2788 *name = e->value.function.name;
2789 }
2790
2791 return pure;
2792}
2793
2794
1acb400a 2795static bool
2796impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2797 int *f ATTRIBUTE_UNUSED)
2798{
2799 const char *name;
2800
2801 /* Don't bother recursing into other statement functions
2802 since they will be checked individually for purity. */
2803 if (e->expr_type != EXPR_FUNCTION
2804 || !e->symtree
2805 || e->symtree->n.sym == sym
2806 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2807 return false;
2808
2809 return pure_function (e, &name) ? false : true;
2810}
2811
2812
2813static int
2814pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2815{
2816 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2817}
2818
2819
d41c8ae0 2820/* Check if an impure function is allowed in the current context. */
0252ef5c 2821
2822static bool check_pure_function (gfc_expr *e)
2823{
2824 const char *name = NULL;
2825 if (!pure_function (e, &name) && name)
2826 {
2827 if (forall_flag)
2828 {
d41c8ae0 2829 gfc_error ("Reference to impure function %qs at %L inside a "
0252ef5c 2830 "FORALL %s", name, &e->where,
2831 forall_flag == 2 ? "mask" : "block");
2832 return false;
2833 }
2834 else if (gfc_do_concurrent_flag)
2835 {
d41c8ae0 2836 gfc_error ("Reference to impure function %qs at %L inside a "
0252ef5c 2837 "DO CONCURRENT %s", name, &e->where,
2838 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2839 return false;
2840 }
2841 else if (gfc_pure (NULL))
2842 {
d41c8ae0 2843 gfc_error ("Reference to impure function %qs at %L "
0252ef5c 2844 "within a PURE procedure", name, &e->where);
2845 return false;
2846 }
2847 gfc_unset_implicit_pure (NULL);
2848 }
2849 return true;
2850}
2851
2852
4ee9c684 2853/* Resolve a function call, which means resolving the arguments, then figuring
2854 out which entity the name refers to. */
4ee9c684 2855
60e19868 2856static bool
1bcc6eb8 2857resolve_function (gfc_expr *expr)
4ee9c684 2858{
2859 gfc_actual_arglist *arg;
1bcc6eb8 2860 gfc_symbol *sym;
60e19868 2861 bool t;
6bfab0c0 2862 int temp;
8d7cdc4d 2863 procedure_type p = PROC_INTRINSIC;
f6f6f726 2864 bool no_formal_args;
6bfab0c0 2865
5e8cd291 2866 sym = NULL;
2867 if (expr->symtree)
2868 sym = expr->symtree->n.sym;
2869
ffe221be 2870 /* If this is a procedure pointer component, it has already been resolved. */
b3961d7b 2871 if (gfc_is_proc_ptr_comp (expr))
60e19868 2872 return true;
b3961d7b 2873
1c0e6696 2874 if (sym && sym->attr.intrinsic
60e19868 2875 && !gfc_resolve_intrinsic (sym, &expr->where))
2876 return false;
1c0e6696 2877
f6d3042b 2878 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
be6157d7 2879 {
716da296 2880 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
60e19868 2881 return false;
be6157d7 2882 }
2883
f3c5115b 2884 /* If this ia a deferred TBP with an abstract interface (which may
b652cb7e 2885 of course be referenced), expr->value.function.esym will be set. */
2886 if (sym && sym->attr.abstract && !expr->value.function.esym)
94fa7146 2887 {
716da296 2888 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
94fa7146 2889 sym->name, &expr->where);
60e19868 2890 return false;
94fa7146 2891 }
2892
6bfab0c0 2893 /* Switch off assumed size checking and do this again for certain kinds
2894 of procedure, once the procedure itself is resolved. */
2895 need_full_assumed_size++;
4ee9c684 2896
8d7cdc4d 2897 if (expr->symtree && expr->symtree->n.sym)
2898 p = expr->symtree->n.sym->attr.proc;
2899
e97ac7c0 2900 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2901 inquiry_argument = true;
6777213b 2902 no_formal_args = sym && is_external_proc (sym)
2903 && gfc_sym_get_dummy_args (sym) == NULL;
e97ac7c0 2904
080819af 2905 if (!resolve_actual_arglist (expr->value.function.actual,
60e19868 2906 p, no_formal_args))
e97ac7c0 2907 {
2908 inquiry_argument = false;
60e19868 2909 return false;
e97ac7c0 2910 }
4ee9c684 2911
e97ac7c0 2912 inquiry_argument = false;
d6463863 2913
c5d33754 2914 /* Resume assumed_size checking. */
6bfab0c0 2915 need_full_assumed_size--;
2916
83aeedb9 2917 /* If the procedure is external, check for usage. */
2918 if (sym && is_external_proc (sym))
2919 resolve_global_procedure (sym, &expr->where,
2920 &expr->value.function.actual, 0);
2921
5e8cd291 2922 if (sym && sym->ts.type == BT_CHARACTER
eeebe20b 2923 && sym->ts.u.cl
2924 && sym->ts.u.cl->length == NULL
1bcc6eb8 2925 && !sym->attr.dummy
617125a6 2926 && !sym->ts.deferred
1bcc6eb8 2927 && expr->value.function.esym == NULL
2928 && !sym->attr.contained)
5e8cd291 2929 {
5e8cd291 2930 /* Internal procedures are taken care of in resolve_contained_fntype. */
716da296 2931 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
e4981f6e 2932 "be used at %L since it is not a dummy argument",
2933 sym->name, &expr->where);
60e19868 2934 return false;
5e8cd291 2935 }
2936
1bcc6eb8 2937 /* See if function is already resolved. */
4ee9c684 2938
c3f3b68d 2939 if (expr->value.function.name != NULL
2940 || expr->value.function.isym != NULL)
4ee9c684 2941 {
2942 if (expr->ts.type == BT_UNKNOWN)
5e8cd291 2943 expr->ts = sym->ts;
60e19868 2944 t = true;
4ee9c684 2945 }
2946 else
2947 {
2948 /* Apply the rules of section 14.1.2. */
2949
5e8cd291 2950 switch (procedure_kind (sym))
4ee9c684 2951 {
2952 case PTYPE_GENERIC:
2953 t = resolve_generic_f (expr);
2954 break;
2955
2956 case PTYPE_SPECIFIC:
2957 t = resolve_specific_f (expr);
2958 break;
2959
2960 case PTYPE_UNKNOWN:
2961 t = resolve_unknown_f (expr);
2962 break;
2963
2964 default:
2965 gfc_internal_error ("resolve_function(): bad function type");
2966 }
2967 }
2968
2969 /* If the expression is still a function (it might have simplified),
2970 then we check to see if we are calling an elemental function. */
2971
2972 if (expr->expr_type != EXPR_FUNCTION)
2973 return t;
2974
6bfab0c0 2975 temp = need_full_assumed_size;
2976 need_full_assumed_size = 0;
2977
60e19868 2978 if (!resolve_elemental_actual (expr, NULL))
2979 return false;
6bfab0c0 2980
764f1175 2981 if (omp_workshare_flag
2982 && expr->value.function.esym
2983 && ! gfc_elemental (expr->value.function.esym))
2984 {
716da296 2985 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
1bcc6eb8 2986 "in WORKSHARE construct", expr->value.function.esym->name,
764f1175 2987 &expr->where);
60e19868 2988 t = false;
764f1175 2989 }
4ee9c684 2990
55cb4417 2991#define GENERIC_ID expr->value.function.isym->id
6bfab0c0 2992 else if (expr->value.function.actual != NULL
1bcc6eb8 2993 && expr->value.function.isym != NULL
2994 && GENERIC_ID != GFC_ISYM_LBOUND
4921ab8a 2995 && GENERIC_ID != GFC_ISYM_LCOBOUND
2996 && GENERIC_ID != GFC_ISYM_UCOBOUND
1bcc6eb8 2997 && GENERIC_ID != GFC_ISYM_LEN
2998 && GENERIC_ID != GFC_ISYM_LOC
07f0c434 2999 && GENERIC_ID != GFC_ISYM_C_LOC
1bcc6eb8 3000 && GENERIC_ID != GFC_ISYM_PRESENT)
6bfab0c0 3001 {
cb8e3560 3002 /* Array intrinsics must also have the last upper bound of an
179eba08 3003 assumed size array argument. UBOUND and SIZE have to be
6bfab0c0 3004 excluded from the check if the second argument is anything
3005 than a constant. */
fc243266 3006
6bfab0c0 3007 for (arg = expr->value.function.actual; arg; arg = arg->next)
3008 {
e2018d20 3009 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
f782f3fd 3010 && arg == expr->value.function.actual
e2018d20 3011 && arg->next != NULL && arg->next->expr)
ac8d4d79 3012 {
3013 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3014 break;
3015
60e19868 3016 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
e2018d20 3017 break;
3018
ac8d4d79 3019 if ((int)mpz_get_si (arg->next->expr->value.integer)
3020 < arg->expr->rank)
3021 break;
3022 }
fc243266 3023
6bfab0c0 3024 if (arg->expr != NULL
1bcc6eb8 3025 && arg->expr->rank > 0
3026 && resolve_assumed_size_actual (arg->expr))
60e19868 3027 return false;
6bfab0c0 3028 }
3029 }
48a3e2a8 3030#undef GENERIC_ID
6bfab0c0 3031
3032 need_full_assumed_size = temp;
3033
0252ef5c 3034 if (!check_pure_function(expr))
3035 t = false;
8b0a2e85 3036
ff9cd459 3037 /* Functions without the RECURSIVE attribution are not allowed to
3038 * call themselves. */
3039 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3040 {
4fafe6df 3041 gfc_symbol *esym;
ff9cd459 3042 esym = expr->value.function.esym;
ff9cd459 3043
4fafe6df 3044 if (is_illegal_recursion (esym, gfc_current_ns))
ff9cd459 3045 {
4fafe6df 3046 if (esym->attr.entry && esym->ns->entries)
716da296 3047 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3048 " function %qs is not RECURSIVE",
4fafe6df 3049 esym->name, &expr->where, esym->ns->entries->sym->name);
3050 else
716da296 3051 gfc_error ("Function %qs at %L cannot be called recursively, as it"
4fafe6df 3052 " is not RECURSIVE", esym->name, &expr->where);
3053
60e19868 3054 t = false;
ff9cd459 3055 }
3056 }
3057
2c35ee8c 3058 /* Character lengths of use associated functions may contains references to
3059 symbols not referenced from the current program unit otherwise. Make sure
3060 those symbols are marked as referenced. */
3061
fc243266 3062 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
2c35ee8c 3063 && expr->value.function.esym->attr.use_assoc)
3064 {
eeebe20b 3065 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
2c35ee8c 3066 }
3067
ac8d4d79 3068 /* Make sure that the expression has a typespec that works. */
3069 if (expr->ts.type == BT_UNKNOWN)
3070 {
3071 if (expr->symtree->n.sym->result
1e057e9b 3072 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3073 && !expr->symtree->n.sym->result->attr.proc_pointer)
ac8d4d79 3074 expr->ts = expr->symtree->n.sym->result->ts;
ac8d4d79 3075 }
3076
4ee9c684 3077 return t;
3078}
3079
3080
3081/************* Subroutine resolution *************/
3082
0252ef5c 3083static bool
3084pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
4ee9c684 3085{
4ee9c684 3086 if (gfc_pure (sym))
0252ef5c 3087 return true;
4ee9c684 3088
3089 if (forall_flag)
0252ef5c 3090 {
3091 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3092 name, loc);
3093 return false;
3094 }
8b8cc022 3095 else if (gfc_do_concurrent_flag)
0252ef5c 3096 {
3097 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3098 "PURE", name, loc);
3099 return false;
3100 }
4ee9c684 3101 else if (gfc_pure (NULL))
0252ef5c 3102 {
3103 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3104 return false;
3105 }
95f9d51f 3106
c77badf3 3107 gfc_unset_implicit_pure (NULL);
0252ef5c 3108 return true;
4ee9c684 3109}
3110
3111
3112static match
1bcc6eb8 3113resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
4ee9c684 3114{
3115 gfc_symbol *s;
3116
3117 if (sym->attr.generic)
3118 {
3119 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3120 if (s != NULL)
3121 {
1bcc6eb8 3122 c->resolved_sym = s;
0252ef5c 3123 if (!pure_subroutine (s, s->name, &c->loc))
3124 return MATCH_ERROR;
4ee9c684 3125 return MATCH_YES;
3126 }
3127
3128 /* TODO: Need to search for elemental references in generic interface. */
3129 }
3130
3131 if (sym->attr.intrinsic)
3132 return gfc_intrinsic_sub_interface (c, 0);
3133
3134 return MATCH_NO;
3135}
3136
3137
60e19868 3138static bool
1bcc6eb8 3139resolve_generic_s (gfc_code *c)
4ee9c684 3140{
3141 gfc_symbol *sym;
3142 match m;
3143
3144 sym = c->symtree->n.sym;
3145
e8325fb3 3146 for (;;)
4ee9c684 3147 {
e8325fb3 3148 m = resolve_generic_s0 (c, sym);
3149 if (m == MATCH_YES)
60e19868 3150 return true;
e8325fb3 3151 else if (m == MATCH_ERROR)
60e19868 3152 return false;
e8325fb3 3153
3154generic:
3155 if (sym->ns->parent == NULL)
3156 break;
4ee9c684 3157 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
e8325fb3 3158
3159 if (sym == NULL)
3160 break;
3161 if (!generic_sym (sym))
3162 goto generic;
4ee9c684 3163 }
3164
3186f695 3165 /* Last ditch attempt. See if the reference is to an intrinsic
3166 that possesses a matching interface. 14.1.2.4 */
e8325fb3 3167 sym = c->symtree->n.sym;
3186f695 3168
a34926ba 3169 if (!gfc_is_intrinsic (sym, 1, c->loc))
4ee9c684 3170 {
716da296 3171 gfc_error ("There is no specific subroutine for the generic %qs at %L",
1bcc6eb8 3172 sym->name, &c->loc);
60e19868 3173 return false;
4ee9c684 3174 }
3175
3176 m = gfc_intrinsic_sub_interface (c, 0);
3177 if (m == MATCH_YES)
60e19868 3178 return true;
4ee9c684 3179 if (m == MATCH_NO)
716da296 3180 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
4ee9c684 3181 "intrinsic subroutine interface", sym->name, &c->loc);
3182
60e19868 3183 return false;
4ee9c684 3184}
3185
3186
3187/* Resolve a subroutine call known to be specific. */
3188
3189static match
1bcc6eb8 3190resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
4ee9c684 3191{
3192 match m;
3193
3194 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3195 {
3196 if (sym->attr.dummy)
3197 {
3198 sym->attr.proc = PROC_DUMMY;
3199 goto found;
3200 }
3201
3202 sym->attr.proc = PROC_EXTERNAL;
3203 goto found;
3204 }
3205
3206 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3207 goto found;
3208
3209 if (sym->attr.intrinsic)
3210 {
3211 m = gfc_intrinsic_sub_interface (c, 1);
3212 if (m == MATCH_YES)
3213 return MATCH_YES;
3214 if (m == MATCH_NO)
716da296 3215 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
4ee9c684 3216 "with an intrinsic", sym->name, &c->loc);
3217
3218 return MATCH_ERROR;
3219 }
3220
3221 return MATCH_NO;
3222
3223found:
3224 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3225
3226 c->resolved_sym = sym;
0252ef5c 3227 if (!pure_subroutine (sym, sym->name, &c->loc))
3228 return MATCH_ERROR;
4ee9c684 3229
3230 return MATCH_YES;
3231}
3232
3233
60e19868 3234static bool
1bcc6eb8 3235resolve_specific_s (gfc_code *c)
4ee9c684 3236{
3237 gfc_symbol *sym;
3238 match m;
3239
3240 sym = c->symtree->n.sym;
3241
e8325fb3 3242 for (;;)
4ee9c684 3243 {
3244 m = resolve_specific_s0 (c, sym);
3245 if (m == MATCH_YES)
60e19868 3246 return true;
4ee9c684 3247 if (m == MATCH_ERROR)
60e19868 3248 return false;
e8325fb3 3249
3250 if (sym->ns->parent == NULL)
3251 break;
3252
3253 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3254
3255 if (sym == NULL)
3256 break;
4ee9c684 3257 }
3258
e8325fb3 3259 sym = c->symtree->n.sym;
716da296 3260 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
4ee9c684 3261 sym->name, &c->loc);
3262
60e19868 3263 return false;
4ee9c684 3264}
3265
3266
3267/* Resolve a subroutine call not known to be generic nor specific. */
3268
60e19868 3269static bool
1bcc6eb8 3270resolve_unknown_s (gfc_code *c)
4ee9c684 3271{
3272 gfc_symbol *sym;
3273
3274 sym = c->symtree->n.sym;
3275
3276 if (sym->attr.dummy)
3277 {
3278 sym->attr.proc = PROC_DUMMY;
3279 goto found;
3280 }
3281
3282 /* See if we have an intrinsic function reference. */
3283
a34926ba 3284 if (gfc_is_intrinsic (sym, 1, c->loc))
4ee9c684 3285 {
3286 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
60e19868 3287 return true;
3288 return false;
4ee9c684 3289 }
3290
3291 /* The reference is to an external name. */
3292
3293found:
3294 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3295
3296 c->resolved_sym = sym;
3297
0252ef5c 3298 return pure_subroutine (sym, sym->name, &c->loc);
4ee9c684 3299}
3300
3301
3302/* Resolve a subroutine call. Although it was tempting to use the same code
3303 for functions, subroutines and functions are stored differently and this
3304 makes things awkward. */
3305
60e19868 3306static bool
1bcc6eb8 3307resolve_call (gfc_code *c)
4ee9c684 3308{
60e19868 3309 bool t;
8d7cdc4d 3310 procedure_type ptype = PROC_INTRINSIC;
fe5c28d2 3311 gfc_symbol *csym, *sym;
f6f6f726 3312 bool no_formal_args;
3313
3314 csym = c->symtree ? c->symtree->n.sym : NULL;
4ee9c684 3315
f6f6f726 3316 if (csym && csym->ts.type != BT_UNKNOWN)
693c40a7 3317 {
716da296 3318 gfc_error_1 ("'%s' at %L has a type, which is not consistent with "
f6f6f726 3319 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
60e19868 3320 return false;
693c40a7 3321 }
3322
fe5c28d2 3323 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3324 {
0f5aadbe 3325 gfc_symtree *st;
830f3dcc 3326 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
0f5aadbe 3327 sym = st ? st->n.sym : NULL;
fe5c28d2 3328 if (sym && csym != sym
3329 && sym->ns == gfc_current_ns
3330 && sym->attr.flavor == FL_PROCEDURE
3331 && sym->attr.contained)
3332 {
3333 sym->refs++;
0f5aadbe 3334 if (csym->attr.generic)
3335 c->symtree->n.sym = sym;
3336 else
3337 c->symtree = st;
3338 csym = c->symtree->n.sym;
fe5c28d2 3339 }
3340 }
3341
9d2264a0 3342 /* If this ia a deferred TBP, c->expr1 will be set. */
3343 if (!c->expr1 && csym)
f3c5115b 3344 {
9d2264a0 3345 if (csym->attr.abstract)
3346 {
716da296 3347 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
9d2264a0 3348 csym->name, &c->loc);
60e19868 3349 return false;
9d2264a0 3350 }
f3c5115b 3351
9d2264a0 3352 /* Subroutines without the RECURSIVE attribution are not allowed to
3353 call themselves. */
3354 if (is_illegal_recursion (csym, gfc_current_ns))
3355 {
3356 if (csym->attr.entry && csym->ns->entries)
716da296 3357 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3358 "as subroutine %qs is not RECURSIVE",
9d2264a0 3359 csym->name, &c->loc, csym->ns->entries->sym->name);
3360 else
716da296 3361 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
9d2264a0 3362 "as it is not RECURSIVE", csym->name, &c->loc);
4fafe6df 3363
60e19868 3364 t = false;
9d2264a0 3365 }
ff9cd459 3366 }
3367
6bfab0c0 3368 /* Switch off assumed size checking and do this again for certain kinds
3369 of procedure, once the procedure itself is resolved. */
3370 need_full_assumed_size++;
3371
f6f6f726 3372 if (csym)
3373 ptype = csym->attr.proc;
8d7cdc4d 3374
6777213b 3375 no_formal_args = csym && is_external_proc (csym)
3376 && gfc_sym_get_dummy_args (csym) == NULL;
60e19868 3377 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3378 return false;
4ee9c684 3379
f6d0e37a 3380 /* Resume assumed_size checking. */
6bfab0c0 3381 need_full_assumed_size--;
3382
83aeedb9 3383 /* If external, check for usage. */
3384 if (csym && is_external_proc (csym))
3385 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3386
60e19868 3387 t = true;
018ef8b8 3388 if (c->resolved_sym == NULL)
7a2a9daf 3389 {
3390 c->resolved_isym = NULL;
3391 switch (procedure_kind (csym))
3392 {
3393 case PTYPE_GENERIC:
3394 t = resolve_generic_s (c);
3395 break;
4ee9c684 3396
7a2a9daf 3397 case PTYPE_SPECIFIC:
3398 t = resolve_specific_s (c);
3399 break;
4ee9c684 3400
7a2a9daf 3401 case PTYPE_UNKNOWN:
3402 t = resolve_unknown_s (c);
3403 break;
4ee9c684 3404
7a2a9daf 3405 default:
3406 gfc_internal_error ("resolve_subroutine(): bad function type");
3407 }
3408 }
4ee9c684 3409
d7b90372 3410 /* Some checks of elemental subroutine actual arguments. */
60e19868 3411 if (!resolve_elemental_actual (NULL, c))
3412 return false;
6bfab0c0 3413
4ee9c684 3414 return t;
3415}
3416
1bcc6eb8 3417
e0cf8f9c 3418/* Compare the shapes of two arrays that have non-NULL shapes. If both
60e19868 3419 op1->shape and op2->shape are non-NULL return true if their shapes
3420 match. If both op1->shape and op2->shape are non-NULL return false
e0cf8f9c 3421 if their shapes do not match. If either op1->shape or op2->shape is
60e19868 3422 NULL, return true. */
e0cf8f9c 3423
60e19868 3424static bool
1bcc6eb8 3425compare_shapes (gfc_expr *op1, gfc_expr *op2)
e0cf8f9c 3426{
60e19868 3427 bool t;
e0cf8f9c 3428 int i;
3429
60e19868 3430 t = true;
fc243266 3431
e0cf8f9c 3432 if (op1->shape != NULL && op2->shape != NULL)
3433 {
3434 for (i = 0; i < op1->rank; i++)
3435 {
3436 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3437 {
716da296 3438 gfc_error_1 ("Shapes for operands at %L and %L are not conformable",
e0cf8f9c 3439 &op1->where, &op2->where);
60e19868 3440 t = false;
e0cf8f9c 3441 break;
3442 }
3443 }
3444 }
3445
3446 return t;
3447}
4ee9c684 3448
1bcc6eb8 3449
4ee9c684 3450/* Resolve an operator expression node. This can involve replacing the
3451 operation with a user defined function call. */
3452
60e19868 3453static bool
1bcc6eb8 3454resolve_operator (gfc_expr *e)
4ee9c684 3455{
3456 gfc_expr *op1, *op2;
3457 char msg[200];
cecd43a5 3458 bool dual_locus_error;
60e19868 3459 bool t;
4ee9c684 3460
3461 /* Resolve all subnodes-- give them types. */
3462
dcb1b019 3463 switch (e->value.op.op)
4ee9c684 3464 {
3465 default:
60e19868 3466 if (!gfc_resolve_expr (e->value.op.op2))
3467 return false;
4ee9c684 3468
3469 /* Fall through... */
3470
3471 case INTRINSIC_NOT:
3472 case INTRINSIC_UPLUS:
3473 case INTRINSIC_UMINUS:
42b215cc 3474 case INTRINSIC_PARENTHESES:
60e19868 3475 if (!gfc_resolve_expr (e->value.op.op1))
3476 return false;
4ee9c684 3477 break;
3478 }
3479
3480 /* Typecheck the new node. */
3481
9b773341 3482 op1 = e->value.op.op1;
3483 op2 = e->value.op.op2;
cecd43a5 3484 dual_locus_error = false;
4ee9c684 3485
0dc7e13b 3486 if ((op1 && op1->expr_type == EXPR_NULL)
3487 || (op2 && op2->expr_type == EXPR_NULL))
3488 {
3489 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3490 goto bad_op;
3491 }
3492
dcb1b019 3493 switch (e->value.op.op)
4ee9c684 3494 {
3495 case INTRINSIC_UPLUS:
3496 case INTRINSIC_UMINUS:
3497 if (op1->ts.type == BT_INTEGER
3498 || op1->ts.type == BT_REAL
3499 || op1->ts.type == BT_COMPLEX)
3500 {
3501 e->ts = op1->ts;
3502 break;
3503 }
3504
41481754 3505 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
dcb1b019 3506 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
4ee9c684 3507 goto bad_op;
3508
3509 case INTRINSIC_PLUS:
3510 case INTRINSIC_MINUS:
3511 case INTRINSIC_TIMES:
3512 case INTRINSIC_DIVIDE:
3513 case INTRINSIC_POWER:
3514 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3515 {
8c2c51e8 3516 gfc_type_convert_binary (e, 1);
4ee9c684 3517 break;
3518 }
3519
3520 sprintf (msg,
41481754 3521 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
dcb1b019 3522 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
4ee9c684 3523 gfc_typename (&op2->ts));
3524 goto bad_op;
3525
3526 case INTRINSIC_CONCAT:
b44437b9 3527 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3528 && op1->ts.kind == op2->ts.kind)
4ee9c684 3529 {
3530 e->ts.type = BT_CHARACTER;
3531 e->ts.kind = op1->ts.kind;
3532 break;
3533 }
3534
3535 sprintf (msg,
41481754 3536 _("Operands of string concatenation operator at %%L are %s/%s"),
4ee9c684 3537 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3538 goto bad_op;
3539
3540 case INTRINSIC_AND:
3541 case INTRINSIC_OR:
3542 case INTRINSIC_EQV:
3543 case INTRINSIC_NEQV:
3544 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3545 {
3546 e->ts.type = BT_LOGICAL;
3547 e->ts.kind = gfc_kind_max (op1, op2);
1bcc6eb8 3548 if (op1->ts.kind < e->ts.kind)
3549 gfc_convert_type (op1, &e->ts, 2);
3550 else if (op2->ts.kind < e->ts.kind)
3551 gfc_convert_type (op2, &e->ts, 2);
4ee9c684 3552 break;
3553 }
3554
41481754 3555 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
dcb1b019 3556 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
4ee9c684 3557 gfc_typename (&op2->ts));
3558
3559 goto bad_op;
3560
3561 case INTRINSIC_NOT:
3562 if (op1->ts.type == BT_LOGICAL)
3563 {
3564 e->ts.type = BT_LOGICAL;
3565 e->ts.kind = op1->ts.kind;
3566 break;
3567 }
3568
f47957c7 3569 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
4ee9c684 3570 gfc_typename (&op1->ts));
3571 goto bad_op;
3572
3573 case INTRINSIC_GT:
f47957c7 3574 case INTRINSIC_GT_OS:
4ee9c684 3575 case INTRINSIC_GE:
f47957c7 3576 case INTRINSIC_GE_OS:
4ee9c684 3577 case INTRINSIC_LT:
f47957c7 3578 case INTRINSIC_LT_OS:
4ee9c684 3579 case INTRINSIC_LE:
f47957c7 3580 case INTRINSIC_LE_OS:
4ee9c684 3581 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3582 {
41481754 3583 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
4ee9c684 3584 goto bad_op;
3585 }
3586
3587 /* Fall through... */
3588
3589 case INTRINSIC_EQ:
f47957c7 3590 case INTRINSIC_EQ_OS:
4ee9c684 3591 case INTRINSIC_NE:
f47957c7 3592 case INTRINSIC_NE_OS:
b44437b9 3593 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3594 && op1->ts.kind == op2->ts.kind)
4ee9c684 3595 {
3596 e->ts.type = BT_LOGICAL;
b8a891cb 3597 e->ts.kind = gfc_default_logical_kind;
4ee9c684 3598 break;
3599 }
3600
3601 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3602 {
8c2c51e8 3603 gfc_type_convert_binary (e, 1);
4ee9c684 3604
3605 e->ts.type = BT_LOGICAL;
b8a891cb 3606 e->ts.kind = gfc_default_logical_kind;
02dcd1b8 3607
8290d53f 3608 if (warn_compare_reals)
02dcd1b8 3609 {
3610 gfc_intrinsic_op op = e->value.op.op;
3611
3612 /* Type conversion has made sure that the types of op1 and op2
3613 agree, so it is only necessary to check the first one. */
3614 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3615 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3616 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3617 {
3618 const char *msg;
3619
3620 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3621 msg = "Equality comparison for %s at %L";
3622 else
3623 msg = "Inequality comparison for %s at %L";
d6463863 3624
02dcd1b8 3625 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3626 }
3627 }
3628
4ee9c684 3629 break;
3630 }
3631
62a94956 3632 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
41481754 3633 sprintf (msg,
1bcc6eb8 3634 _("Logicals at %%L must be compared with %s instead of %s"),
d6463863 3635 (e->value.op.op == INTRINSIC_EQ
dcb1b019 3636 || e->value.op.op == INTRINSIC_EQ_OS)
3637 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
62a94956 3638 else
41481754 3639 sprintf (msg,
1bcc6eb8 3640 _("Operands of comparison operator '%s' at %%L are %s/%s"),
dcb1b019 3641 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
62a94956 3642 gfc_typename (&op2->ts));
4ee9c684 3643
3644 goto bad_op;
3645
3646 case INTRINSIC_USER:
dcb1b019 3647 if (e->value.op.uop->op == NULL)
8a4bda20 3648 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3649 else if (op2 == NULL)
41481754 3650 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
9b773341 3651 e->value.op.uop->name, gfc_typename (&op1->ts));
4ee9c684 3652 else
e1afc7ba 3653 {
3654 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3655 e->value.op.uop->name, gfc_typename (&op1->ts),
3656 gfc_typename (&op2->ts));
3657 e->value.op.uop->op->sym->attr.referenced = 1;
3658 }
4ee9c684 3659
3660 goto bad_op;
3661
42b215cc 3662 case INTRINSIC_PARENTHESES:
ce2aba1e 3663 e->ts = op1->ts;
3664 if (e->ts.type == BT_CHARACTER)
eeebe20b 3665 e->ts.u.cl = op1->ts.u.cl;
42b215cc 3666 break;
3667
4ee9c684 3668 default:
3669 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3670 }
3671
3672 /* Deal with arrayness of an operand through an operator. */
3673
60e19868 3674 t = true;
4ee9c684 3675
dcb1b019 3676 switch (e->value.op.op)
4ee9c684 3677 {
3678 case INTRINSIC_PLUS:
3679 case INTRINSIC_MINUS:
3680 case INTRINSIC_TIMES:
3681 case INTRINSIC_DIVIDE:
3682 case INTRINSIC_POWER:
3683 case INTRINSIC_CONCAT:
3684 case INTRINSIC_AND:
3685 case INTRINSIC_OR:
3686 case INTRINSIC_EQV:
3687 case INTRINSIC_NEQV:
3688 case INTRINSIC_EQ:
f47957c7 3689 case INTRINSIC_EQ_OS:
4ee9c684 3690 case INTRINSIC_NE:
f47957c7 3691 case INTRINSIC_NE_OS:
4ee9c684 3692 case INTRINSIC_GT:
f47957c7 3693 case INTRINSIC_GT_OS:
4ee9c684 3694 case INTRINSIC_GE:
f47957c7 3695 case INTRINSIC_GE_OS:
4ee9c684 3696 case INTRINSIC_LT:
f47957c7 3697 case INTRINSIC_LT_OS:
4ee9c684 3698 case INTRINSIC_LE:
f47957c7 3699 case INTRINSIC_LE_OS:
4ee9c684 3700
3701 if (op1->rank == 0 && op2->rank == 0)
3702 e->rank = 0;
3703
3704 if (op1->rank == 0 && op2->rank != 0)
3705 {
3706 e->rank = op2->rank;
3707
3708 if (e->shape == NULL)
3709 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3710 }
3711
3712 if (op1->rank != 0 && op2->rank == 0)
3713 {
3714 e->rank = op1->rank;
3715
3716 if (e->shape == NULL)
3717 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3718 }
3719
3720 if (op1->rank != 0 && op2->rank != 0)
3721 {
3722 if (op1->rank == op2->rank)
3723 {
3724 e->rank = op1->rank;
4ee9c684 3725 if (e->shape == NULL)
e0cf8f9c 3726 {
4c0165a9 3727 t = compare_shapes (op1, op2);
60e19868 3728 if (!t)
e0cf8f9c 3729 e->shape = NULL;
3730 else
4c0165a9 3731 e->shape = gfc_copy_shape (op1->shape, op1->rank);
e0cf8f9c 3732 }
4ee9c684 3733 }
3734 else
3735 {
1bcc6eb8 3736 /* Allow higher level expressions to work. */
4ee9c684 3737 e->rank = 0;
cecd43a5 3738
3739 /* Try user-defined operators, and otherwise throw an error. */
3740 dual_locus_error = true;
3741 sprintf (msg,
3742 _("Inconsistent ranks for operator at %%L and %%L"));
3743 goto bad_op;
4ee9c684 3744 }
3745 }
3746
3747 break;
3748
1e853e89 3749 case INTRINSIC_PARENTHESES:
4ee9c684 3750 case INTRINSIC_NOT:
3751 case INTRINSIC_UPLUS:
3752 case INTRINSIC_UMINUS:
1e853e89 3753 /* Simply copy arrayness attribute */
4ee9c684 3754 e->rank = op1->rank;
3755
3756 if (e->shape == NULL)
3757 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3758
4ee9c684 3759 break;
3760
3761 default:
3762 break;
3763 }
3764
3765 /* Attempt to simplify the expression. */
60e19868 3766 if (t)
9fb2e10d 3767 {
3768 t = gfc_simplify_expr (e, 0);
60e19868 3769 /* Some calls do not succeed in simplification and return false
69b1505f 3770 even though there is no error; e.g. variable references to
9fb2e10d 3771 PARAMETER arrays. */
3772 if (!gfc_is_constant_expr (e))
60e19868 3773 t = true;
9fb2e10d 3774 }
4ee9c684 3775 return t;
3776
3777bad_op:
e0cf8f9c 3778
7d034542 3779 {
63b9ead4 3780 match m = gfc_extend_expr (e);
3781 if (m == MATCH_YES)
60e19868 3782 return true;
63b9ead4 3783 if (m == MATCH_ERROR)
60e19868 3784 return false;
7d034542 3785 }
4ee9c684 3786
cecd43a5 3787 if (dual_locus_error)
3788 gfc_error (msg, &op1->where, &op2->where);
3789 else
3790 gfc_error (msg, &e->where);
e0cf8f9c 3791
60e19868 3792 return false;
4ee9c684 3793}
3794
3795
3796/************** Array resolution subroutines **************/
3797
4ee9c684 3798typedef enum
3799{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3800comparison;
3801
3802/* Compare two integer expressions. */
3803
3804static comparison
1bcc6eb8 3805compare_bound (gfc_expr *a, gfc_expr *b)
4ee9c684 3806{
3807 int i;
3808
3809 if (a == NULL || a->expr_type != EXPR_CONSTANT
3810 || b == NULL || b->expr_type != EXPR_CONSTANT)
3811 return CMP_UNKNOWN;
3812
89b10b15 3813 /* If either of the types isn't INTEGER, we must have
3814 raised an error earlier. */
3815
4ee9c684 3816 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
89b10b15 3817 return CMP_UNKNOWN;
4ee9c684 3818
3819 i = mpz_cmp (a->value.integer, b->value.integer);
3820
3821 if (i < 0)
3822 return CMP_LT;
3823 if (i > 0)
3824 return CMP_GT;
3825 return CMP_EQ;
3826}
3827
3828
3829/* Compare an integer expression with an integer. */
3830
3831static comparison
1bcc6eb8 3832compare_bound_int (gfc_expr *a, int b)
4ee9c684 3833{
3834 int i;
3835
3836 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3837 return CMP_UNKNOWN;
3838
3839 if (a->ts.type != BT_INTEGER)
3840 gfc_internal_error ("compare_bound_int(): Bad expression");
3841
3842 i = mpz_cmp_si (a->value.integer, b);
3843
3844 if (i < 0)
3845 return CMP_LT;
3846 if (i > 0)
3847 return CMP_GT;
3848 return CMP_EQ;
3849}
3850
3851
dd94eeca 3852/* Compare an integer expression with a mpz_t. */
3853
3854static comparison
1bcc6eb8 3855compare_bound_mpz_t (gfc_expr *a, mpz_t b)
dd94eeca 3856{
3857 int i;
3858
3859 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3860 return CMP_UNKNOWN;
3861
3862 if (a->ts.type != BT_INTEGER)
3863 gfc_internal_error ("compare_bound_int(): Bad expression");
3864
3865 i = mpz_cmp (a->value.integer, b);
3866
3867 if (i < 0)
3868 return CMP_LT;
3869 if (i > 0)
3870 return CMP_GT;
3871 return CMP_EQ;
3872}
3873
3874
d6463863 3875/* Compute the last value of a sequence given by a triplet.
dd94eeca 3876 Return 0 if it wasn't able to compute the last value, or if the
3877 sequence if empty, and 1 otherwise. */
3878
3879static int
1bcc6eb8 3880compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3881 gfc_expr *stride, mpz_t last)
dd94eeca 3882{
3883 mpz_t rem;
3884
3885 if (start == NULL || start->expr_type != EXPR_CONSTANT
3886 || end == NULL || end->expr_type != EXPR_CONSTANT
3887 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3888 return 0;
3889
3890 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3891 || (stride != NULL && stride->ts.type != BT_INTEGER))
3892 return 0;
3893
60e19868 3894 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
dd94eeca 3895 {
3896 if (compare_bound (start, end) == CMP_GT)
3897 return 0;
3898 mpz_set (last, end->value.integer);
3899 return 1;
3900 }
fc243266 3901
dd94eeca 3902 if (compare_bound_int (stride, 0) == CMP_GT)
3903 {
3904 /* Stride is positive */
3905 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3906 return 0;
3907 }
3908 else
3909 {
3910 /* Stride is negative */
3911 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3912 return 0;
3913 }
3914
3915 mpz_init (rem);
3916 mpz_sub (rem, end->value.integer, start->value.integer);
3917 mpz_tdiv_r (rem, rem, stride->value.integer);
3918 mpz_sub (last, end->value.integer, rem);
3919 mpz_clear (rem);
3920
3921 return 1;
3922}
3923
3924
4ee9c684 3925/* Compare a single dimension of an array reference to the array
3926 specification. */
3927
60e19868 3928static bool
1bcc6eb8 3929check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
4ee9c684 3930{
dd94eeca 3931 mpz_t last_value;
4ee9c684 3932
e97ac7c0 3933 if (ar->dimen_type[i] == DIMEN_STAR)
3934 {
3935 gcc_assert (ar->stride[i] == NULL);
3936 /* This implies [*] as [*:] and [*:3] are not possible. */
3937 if (ar->start[i] == NULL)
3938 {
3939 gcc_assert (ar->end[i] == NULL);
60e19868 3940 return true;
e97ac7c0 3941 }
3942 }
3943
4ee9c684 3944/* Given start, end and stride values, calculate the minimum and
b14e2757 3945 maximum referenced indexes. */
4ee9c684 3946
366f17ed 3947 switch (ar->dimen_type[i])
4ee9c684 3948 {
366f17ed 3949 case DIMEN_VECTOR:
076094b7 3950 case DIMEN_THIS_IMAGE:
4ee9c684 3951 break;
3952
e97ac7c0 3953 case DIMEN_STAR:
366f17ed 3954 case DIMEN_ELEMENT:
4ee9c684 3955 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
366f17ed 3956 {
e97ac7c0 3957 if (i < as->rank)
3958 gfc_warning ("Array reference at %L is out of bounds "
3959 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3960 mpz_get_si (ar->start[i]->value.integer),
3961 mpz_get_si (as->lower[i]->value.integer), i+1);
3962 else
3963 gfc_warning ("Array reference at %L is out of bounds "
3964 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3965 mpz_get_si (ar->start[i]->value.integer),
3966 mpz_get_si (as->lower[i]->value.integer),
3967 i + 1 - as->rank);
60e19868 3968 return true;
366f17ed 3969 }
4ee9c684 3970 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
366f17ed 3971 {
e97ac7c0 3972 if (i < as->rank)
3973 gfc_warning ("Array reference at %L is out of bounds "
3974 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3975 mpz_get_si (ar->start[i]->value.integer),
3976 mpz_get_si (as->upper[i]->value.integer), i+1);
3977 else
3978 gfc_warning ("Array reference at %L is out of bounds "
3979 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3980 mpz_get_si (ar->start[i]->value.integer),
3981 mpz_get_si (as->upper[i]->value.integer),
3982 i + 1 - as->rank);
60e19868 3983 return true;
366f17ed 3984 }
4ee9c684 3985
3986 break;
3987
366f17ed 3988 case DIMEN_RANGE:
8b3b28e3 3989 {
dd94eeca 3990#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3991#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3992
8b3b28e3 3993 comparison comp_start_end = compare_bound (AR_START, AR_END);
dd94eeca 3994
8b3b28e3 3995 /* Check for zero stride, which is not allowed. */
3996 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3997 {
3998 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
60e19868 3999 return false;
8b3b28e3 4000 }
4001
4002 /* if start == len || (stride > 0 && start < len)
4003 || (stride < 0 && start > len),
4004 then the array section contains at least one element. In this
4005 case, there is an out-of-bounds access if
4006 (start < lower || start > upper). */
4007 if (compare_bound (AR_START, AR_END) == CMP_EQ
4008 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4009 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4010 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4011 && comp_start_end == CMP_GT))
4012 {
366f17ed 4013 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4014 {
4015 gfc_warning ("Lower array reference at %L is out of bounds "
4016 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4017 mpz_get_si (AR_START->value.integer),
4018 mpz_get_si (as->lower[i]->value.integer), i+1);
60e19868 4019 return true;
366f17ed 4020 }
4021 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4022 {
4023 gfc_warning ("Lower array reference at %L is out of bounds "
4024 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4025 mpz_get_si (AR_START->value.integer),
4026 mpz_get_si (as->upper[i]->value.integer), i+1);
60e19868 4027 return true;
366f17ed 4028 }
8b3b28e3 4029 }
4030
4031 /* If we can compute the highest index of the array section,
4032 then it also has to be between lower and upper. */
4033 mpz_init (last_value);
4034 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4035 last_value))
4036 {
366f17ed 4037 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4038 {
4039 gfc_warning ("Upper array reference at %L is out of bounds "
4040 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4041 mpz_get_si (last_value),
4042 mpz_get_si (as->lower[i]->value.integer), i+1);
4043 mpz_clear (last_value);
60e19868 4044 return true;
366f17ed 4045 }
4046 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
8b3b28e3 4047 {
366f17ed 4048 gfc_warning ("Upper array reference at %L is out of bounds "
4049 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4050 mpz_get_si (last_value),
4051 mpz_get_si (as->upper[i]->value.integer), i+1);
8b3b28e3 4052 mpz_clear (last_value);
60e19868 4053 return true;
8b3b28e3 4054 }
4055 }
4056 mpz_clear (last_value);
dd94eeca 4057
4058#undef AR_START
4059#undef AR_END
8b3b28e3 4060 }
4ee9c684 4061 break;
4062
4063 default:
4064 gfc_internal_error ("check_dimension(): Bad array reference");
4065 }
4066
60e19868 4067 return true;
4ee9c684 4068}
4069
4070
4071/* Compare an array reference with an array specification. */
4072
60e19868 4073static bool
1bcc6eb8 4074compare_spec_to_ref (gfc_array_ref *ar)
4ee9c684 4075{
4076 gfc_array_spec *as;
4077 int i;
4078
4079 as = ar->as;
4080 i = as->rank - 1;
4081 /* TODO: Full array sections are only allowed as actual parameters. */
4082 if (as->type == AS_ASSUMED_SIZE
4083 && (/*ar->type == AR_FULL
1bcc6eb8 4084 ||*/ (ar->type == AR_SECTION
4085 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
4ee9c684 4086 {
1bcc6eb8 4087 gfc_error ("Rightmost upper bound of assumed size array section "
4088 "not specified at %L", &ar->where);
60e19868 4089 return false;
4ee9c684 4090 }
4091
4092 if (ar->type == AR_FULL)
60e19868 4093 return true;
4ee9c684 4094
4095 if (as->rank != ar->dimen)
4096 {
4097 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4098 &ar->where, ar->dimen, as->rank);
60e19868 4099 return false;
4ee9c684 4100 }
4101
e97ac7c0 4102 /* ar->codimen == 0 is a local array. */
4103 if (as->corank != ar->codimen && ar->codimen != 0)
4104 {
4105 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4106 &ar->where, ar->codimen, as->corank);
60e19868 4107 return false;
e97ac7c0 4108 }
4109
4ee9c684 4110 for (i = 0; i < as->rank; i++)
60e19868 4111 if (!check_dimension (i, ar, as))
4112 return false;
4ee9c684 4113
e97ac7c0 4114 /* Local access has no coarray spec. */
4115 if (ar->codimen != 0)
4116 for (i = as->rank; i < as->rank + as->corank; i++)
4117 {
076094b7 4118 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4119 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
e97ac7c0 4120 {
4121 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4122 i + 1 - as->rank, &ar->where);
60e19868 4123 return false;
e97ac7c0 4124 }
60e19868 4125 if (!check_dimension (i, ar, as))
4126 return false;
e97ac7c0 4127 }
4128
60e19868 4129 return true;
4ee9c684 4130}
4131
4132
4133/* Resolve one part of an array index. */
4134
60e19868 4135static bool
e2ab564d 4136gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4137 int force_index_integer_kind)
4ee9c684 4138{
4139 gfc_typespec ts;
4140
4141 if (index == NULL)
60e19868 4142 return true;
4ee9c684 4143
60e19868 4144 if (!gfc_resolve_expr (index))
4145 return false;
4ee9c684 4146
ae634447 4147 if (check_scalar && index->rank != 0)
4ee9c684 4148 {
ae634447 4149 gfc_error ("Array index at %L must be scalar", &index->where);
60e19868 4150 return false;
4ee9c684 4151 }
4152
ae634447 4153 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
4ee9c684 4154 {
7e221851 4155 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4156 &index->where, gfc_basic_typename (index->ts.type));
60e19868 4157 return false;
4ee9c684 4158 }
4159
ae634447 4160 if (index->ts.type == BT_REAL)
080819af 4161 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
60e19868 4162 &index->where))
4163 return false;
ae634447 4164
e2ab564d 4165 if ((index->ts.kind != gfc_index_integer_kind
4166 && force_index_integer_kind)
ae634447 4167 || index->ts.type != BT_INTEGER)
4ee9c684 4168 {
900c3ad8 4169 gfc_clear_ts (&ts);
4ee9c684 4170 ts.type = BT_INTEGER;
4171 ts.kind = gfc_index_integer_kind;
4172
4173 gfc_convert_type_warn (index, &ts, 2, 0);
4174 }
4175
60e19868 4176 return true;
4ee9c684 4177}
4178
e2ab564d 4179/* Resolve one part of an array index. */
4180
60e19868 4181bool
e2ab564d 4182gfc_resolve_index (gfc_expr *index, int check_scalar)
4183{
4184 return gfc_resolve_index_1 (index, check_scalar, 1);
4185}
4186
02ddb12a 4187/* Resolve a dim argument to an intrinsic function. */
4188
60e19868 4189bool
02ddb12a 4190gfc_resolve_dim_arg (gfc_expr *dim)
4191{
4192 if (dim == NULL)
60e19868 4193 return true;
02ddb12a 4194
60e19868 4195 if (!gfc_resolve_expr (dim))
4196 return false;
02ddb12a 4197
4198 if (dim->rank != 0)
4199 {
4200 gfc_error ("Argument dim at %L must be scalar", &dim->where);
60e19868 4201 return false;
fc243266 4202
02ddb12a 4203 }
24146844 4204
02ddb12a 4205 if (dim->ts.type != BT_INTEGER)
4206 {
4207 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
60e19868 4208 return false;
02ddb12a 4209 }
24146844 4210
02ddb12a 4211 if (dim->ts.kind != gfc_index_integer_kind)
4212 {
4213 gfc_typespec ts;
4214
1fb1e0c8 4215 gfc_clear_ts (&ts);
02ddb12a 4216 ts.type = BT_INTEGER;
4217 ts.kind = gfc_index_integer_kind;
4218
4219 gfc_convert_type_warn (dim, &ts, 2, 0);
4220 }
4221
60e19868 4222 return true;
02ddb12a 4223}
4ee9c684 4224
4225/* Given an expression that contains array references, update those array
4226 references to point to the right array specifications. While this is
4227 filled in during matching, this information is difficult to save and load
4228 in a module, so we take care of it here.
4229
4230 The idea here is that the original array reference comes from the
4231 base symbol. We traverse the list of reference structures, setting
4232 the stored reference to references. Component references can
4233 provide an additional array specification. */
4234
4235static void
1bcc6eb8 4236find_array_spec (gfc_expr *e)
4ee9c684 4237{
4238 gfc_array_spec *as;
4239 gfc_component *c;
4240 gfc_ref *ref;
4241
1de1b1a9 4242 if (e->symtree->n.sym->ts.type == BT_CLASS)
50b4b37b 4243 as = CLASS_DATA (e->symtree->n.sym)->as;
1de1b1a9 4244 else
4245 as = e->symtree->n.sym->as;
4ee9c684 4246
4247 for (ref = e->ref; ref; ref = ref->next)
4248 switch (ref->type)
4249 {
4250 case REF_ARRAY:
4251 if (as == NULL)
4252 gfc_internal_error ("find_array_spec(): Missing spec");
4253
4254 ref->u.ar.as = as;
4255 as = NULL;
4256 break;
4257
4258 case REF_COMPONENT:
3794f7ca 4259 c = ref->u.c.component;
3be2b8d5 4260 if (c->attr.dimension)
4ee9c684 4261 {
4262 if (as != NULL)
4263 gfc_internal_error ("find_array_spec(): unused as(1)");
4264 as = c->as;
4265 }
4266
4ee9c684 4267 break;
4268
4269 case REF_SUBSTRING:
4270 break;
4271 }
4272
4273 if (as != NULL)
4274 gfc_internal_error ("find_array_spec(): unused as(2)");
4275}
4276
4277
4278/* Resolve an array reference. */
4279
60e19868 4280static bool
1bcc6eb8 4281resolve_array_ref (gfc_array_ref *ar)
4ee9c684 4282{
4283 int i, check_scalar;
947f2aa1 4284 gfc_expr *e;
4ee9c684 4285
e97ac7c0 4286 for (i = 0; i < ar->dimen + ar->codimen; i++)
4ee9c684 4287 {
4288 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4289
e2ab564d 4290 /* Do not force gfc_index_integer_kind for the start. We can
4291 do fine with any integer kind. This avoids temporary arrays
4292 created for indexing with a vector. */
60e19868 4293 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4294 return false;
4295 if (!gfc_resolve_index (ar->end[i], check_scalar))
4296 return false;
4297 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4298 return false;
4ee9c684 4299
947f2aa1 4300 e = ar->start[i];
4301
4ee9c684 4302 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
947f2aa1 4303 switch (e->rank)
4ee9c684 4304 {
4305 case 0:
4306 ar->dimen_type[i] = DIMEN_ELEMENT;
4307 break;
4308
4309 case 1:
4310 ar->dimen_type[i] = DIMEN_VECTOR;
947f2aa1 4311 if (e->expr_type == EXPR_VARIABLE
1bcc6eb8 4312 && e->symtree->n.sym->ts.type == BT_DERIVED)
947f2aa1 4313 ar->start[i] = gfc_get_parentheses (e);
4ee9c684 4314 break;
4315
4316 default:
4317 gfc_error ("Array index at %L is an array of rank %d",
947f2aa1 4318 &ar->c_where[i], e->rank);
60e19868 4319 return false;
4ee9c684 4320 }
ec849f11 4321
4322 /* Fill in the upper bound, which may be lower than the
4323 specified one for something like a(2:10:5), which is
4324 identical to a(2:7:5). Only relevant for strides not equal
5a3a7e74 4325 to one. Don't try a division by zero. */
ec849f11 4326 if (ar->dimen_type[i] == DIMEN_RANGE
4327 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5a3a7e74 4328 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4329 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
ec849f11 4330 {
4331 mpz_t size, end;
4332
60e19868 4333 if (gfc_ref_dimen_size (ar, i, &size, &end))
ec849f11 4334 {
4335 if (ar->end[i] == NULL)
4336 {
4337 ar->end[i] =
4338 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4339 &ar->where);
4340 mpz_set (ar->end[i]->value.integer, end);
4341 }
4342 else if (ar->end[i]->ts.type == BT_INTEGER
4343 && ar->end[i]->expr_type == EXPR_CONSTANT)
4344 {
4345 mpz_set (ar->end[i]->value.integer, end);
4346 }
4347 else
4348 gcc_unreachable ();
4349
4350 mpz_clear (size);
4351 mpz_clear (end);
4352 }
4353 }
4ee9c684 4354 }
4355
5733df58 4356 if (ar->type == AR_FULL)
4357 {
4358 if (ar->as->rank == 0)
4359 ar->type = AR_ELEMENT;
4360
4361 /* Make sure array is the same as array(:,:), this way
4362 we don't need to special case all the time. */
4363 ar->dimen = ar->as->rank;
4364 for (i = 0; i < ar->dimen; i++)
4365 {
4366 ar->dimen_type[i] = DIMEN_RANGE;
4367
4368 gcc_assert (ar->start[i] == NULL);
4369 gcc_assert (ar->end[i] == NULL);
4370 gcc_assert (ar->stride[i] == NULL);
4371 }
4372 }
e97ac7c0 4373
4ee9c684 4374 /* If the reference type is unknown, figure out what kind it is. */
4375
4376 if (ar->type == AR_UNKNOWN)
4377 {
4378 ar->type = AR_ELEMENT;
4379 for (i = 0; i < ar->dimen; i++)
4380 if (ar->dimen_type[i] == DIMEN_RANGE
4381 || ar->dimen_type[i] == DIMEN_VECTOR)
4382 {
4383 ar->type = AR_SECTION;
4384 break;
4385 }
4386 }
4387
60e19868 4388 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4389 return false;
4ee9c684 4390
97f98d6e 4391 if (ar->as->corank && ar->codimen == 0)
4392 {
4393 int n;
4394 ar->codimen = ar->as->corank;
4395 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4396 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4397 }
4398
60e19868 4399 return true;
4ee9c684 4400}
4401
4402
60e19868 4403static bool
1bcc6eb8 4404resolve_substring (gfc_ref *ref)
4ee9c684 4405{
7ad1f5f6 4406 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4407
4ee9c684 4408 if (ref->u.ss.start != NULL)
4409 {
60e19868 4410 if (!gfc_resolve_expr (ref->u.ss.start))
4411 return false;
4ee9c684 4412
4413 if (ref->u.ss.start->ts.type != BT_INTEGER)
4414 {
4415 gfc_error ("Substring start index at %L must be of type INTEGER",
4416 &ref->u.ss.start->where);
60e19868 4417 return false;
4ee9c684 4418 }
4419
4420 if (ref->u.ss.start->rank != 0)
4421 {
4422 gfc_error ("Substring start index at %L must be scalar",
4423 &ref->u.ss.start->where);
60e19868 4424 return false;
4ee9c684 4425 }
4426
d1664d35 4427 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4428 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4429 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4ee9c684 4430 {
4431 gfc_error ("Substring start index at %L is less than one",
4432 &ref->u.ss.start->where);
60e19868 4433 return false;
4ee9c684 4434 }
4435 }
4436
4437 if (ref->u.ss.end != NULL)
4438 {
60e19868 4439 if (!gfc_resolve_expr (ref->u.ss.end))
4440 return false;
4ee9c684 4441
4442 if (ref->u.ss.end->ts.type != BT_INTEGER)
4443 {
4444 gfc_error ("Substring end index at %L must be of type INTEGER",
4445 &ref->u.ss.end->where);
60e19868 4446 return false;
4ee9c684 4447 }
4448
4449 if (ref->u.ss.end->rank != 0)
4450 {
4451 gfc_error ("Substring end index at %L must be scalar",
4452 &ref->u.ss.end->where);
60e19868 4453 return false;
4ee9c684 4454 }
4455
4456 if (ref->u.ss.length != NULL
d1664d35 4457 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4458 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4459 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4ee9c684 4460 {
d1664d35 4461 gfc_error ("Substring end index at %L exceeds the string length",
4ee9c684 4462 &ref->u.ss.start->where);
60e19868 4463 return false;
4ee9c684 4464 }
7ad1f5f6 4465
4466 if (compare_bound_mpz_t (ref->u.ss.end,
4467 gfc_integer_kinds[k].huge) == CMP_GT
4468 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4469 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4470 {
4471 gfc_error ("Substring end index at %L is too large",
4472 &ref->u.ss.end->where);
60e19868 4473 return false;
7ad1f5f6 4474 }
4ee9c684 4475 }
4476
60e19868 4477 return true;
4ee9c684 4478}
4479
4480
0ff77f4e 4481/* This function supplies missing substring charlens. */
4482
4483void
4484gfc_resolve_substring_charlen (gfc_expr *e)
4485{
4486 gfc_ref *char_ref;
4487 gfc_expr *start, *end;
4488
4489 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4490 if (char_ref->type == REF_SUBSTRING)
4491 break;
4492
4493 if (!char_ref)
4494 return;
4495
4496 gcc_assert (char_ref->next == NULL);
4497
eeebe20b 4498 if (e->ts.u.cl)
0ff77f4e 4499 {
eeebe20b 4500 if (e->ts.u.cl->length)
4501 gfc_free_expr (e->ts.u.cl->length);
0ff77f4e 4502 else if (e->expr_type == EXPR_VARIABLE
4503 && e->symtree->n.sym->attr.dummy)
4504 return;
4505 }
4506
4507 e->ts.type = BT_CHARACTER;
4508 e->ts.kind = gfc_default_character_kind;
4509
eeebe20b 4510 if (!e->ts.u.cl)
d270ce52 4511 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
0ff77f4e 4512
4513 if (char_ref->u.ss.start)
4514 start = gfc_copy_expr (char_ref->u.ss.start);
4515 else
126387b5 4516 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
0ff77f4e 4517
4518 if (char_ref->u.ss.end)
4519 end = gfc_copy_expr (char_ref->u.ss.end);
4520 else if (e->expr_type == EXPR_VARIABLE)
eeebe20b 4521 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
0ff77f4e 4522 else
4523 end = NULL;
4524
4525 if (!start || !end)
1f947744 4526 {
4527 gfc_free_expr (start);
4528 gfc_free_expr (end);
4529 return;
4530 }
0ff77f4e 4531
4532 /* Length = (end - start +1). */
eeebe20b 4533 e->ts.u.cl->length = gfc_subtract (end, start);
126387b5 4534 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4535 gfc_get_int_expr (gfc_default_integer_kind,
4536 NULL, 1));
0ff77f4e 4537
eeebe20b 4538 e->ts.u.cl->length->ts.type = BT_INTEGER;
4539 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
0ff77f4e 4540
4541 /* Make sure that the length is simplified. */
eeebe20b 4542 gfc_simplify_expr (e->ts.u.cl->length, 1);
4543 gfc_resolve_expr (e->ts.u.cl->length);
0ff77f4e 4544}
4545
4546
4ee9c684 4547/* Resolve subtype references. */
4548
60e19868 4549static bool
1bcc6eb8 4550resolve_ref (gfc_expr *expr)
4ee9c684 4551{
4552 int current_part_dimension, n_components, seen_part_dimension;
4553 gfc_ref *ref;
4554
4555 for (ref = expr->ref; ref; ref = ref->next)
4556 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4557 {
4558 find_array_spec (expr);
4559 break;
4560 }
4561
4562 for (ref = expr->ref; ref; ref = ref->next)
4563 switch (ref->type)
4564 {
4565 case REF_ARRAY:
60e19868 4566 if (!resolve_array_ref (&ref->u.ar))
4567 return false;
4ee9c684 4568 break;
4569
4570 case REF_COMPONENT:
4571 break;
4572
4573 case REF_SUBSTRING:
60e19868 4574 if (!resolve_substring (ref))
4575 return false;
4ee9c684 4576 break;
4577 }
4578
4579 /* Check constraints on part references. */
4580
4581 current_part_dimension = 0;
4582 seen_part_dimension = 0;
4583 n_components = 0;
4584
4585 for (ref = expr->ref; ref; ref = ref->next)
4586 {
4587 switch (ref->type)
4588 {
4589 case REF_ARRAY:
4590 switch (ref->u.ar.type)
4591 {
4592 case AR_FULL:
e97ac7c0 4593 /* Coarray scalar. */
4594 if (ref->u.ar.as->rank == 0)
4595 {
4596 current_part_dimension = 0;
4597 break;
4598 }
4599 /* Fall through. */
4ee9c684 4600 case AR_SECTION:
4601 current_part_dimension = 1;
4602 break;
4603
4604 case AR_ELEMENT:
4605 current_part_dimension = 0;
4606 break;
4607
4608 case AR_UNKNOWN:
4609 gfc_internal_error ("resolve_ref(): Bad array reference");
4610 }
4611
4612 break;
4613
4614 case REF_COMPONENT:
8df9e01e 4615 if (current_part_dimension || seen_part_dimension)
4ee9c684 4616 {
4d9b926d 4617 /* F03:C614. */
4618 if (ref->u.c.component->attr.pointer
49dcd9d0 4619 || ref->u.c.component->attr.proc_pointer
4620 || (ref->u.c.component->ts.type == BT_CLASS
4621 && CLASS_DATA (ref->u.c.component)->attr.pointer))
1bcc6eb8 4622 {
4623 gfc_error ("Component to the right of a part reference "
4624 "with nonzero rank must not have the POINTER "
4625 "attribute at %L", &expr->where);
60e19868 4626 return false;
8df9e01e 4627 }
49dcd9d0 4628 else if (ref->u.c.component->attr.allocatable
4629 || (ref->u.c.component->ts.type == BT_CLASS
4630 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4631
1bcc6eb8 4632 {
4633 gfc_error ("Component to the right of a part reference "
4634 "with nonzero rank must not have the ALLOCATABLE "
4635 "attribute at %L", &expr->where);
60e19868 4636 return false;
8df9e01e 4637 }
4ee9c684 4638 }
4639
4640 n_components++;
4641 break;
4642
4643 case REF_SUBSTRING:
4644 break;
4645 }
4646
4647 if (((ref->type == REF_COMPONENT && n_components > 1)
4648 || ref->next == NULL)
1bcc6eb8 4649 && current_part_dimension
4ee9c684 4650 && seen_part_dimension)
4651 {
4ee9c684 4652 gfc_error ("Two or more part references with nonzero rank must "
4653 "not be specified at %L", &expr->where);
60e19868 4654 return false;
4ee9c684 4655 }
4656
4657 if (ref->type == REF_COMPONENT)
4658 {
4659 if (current_part_dimension)
4660 seen_part_dimension = 1;
4661
1bcc6eb8 4662 /* reset to make sure */
4ee9c684 4663 current_part_dimension = 0;
4664 }
4665 }
4666
60e19868 4667 return true;
4ee9c684 4668}
4669
4670
4671/* Given an expression, determine its shape. This is easier than it sounds.
b14e2757 4672 Leaves the shape array NULL if it is not possible to determine the shape. */
4ee9c684 4673
4674static void
1bcc6eb8 4675expression_shape (gfc_expr *e)
4ee9c684 4676{
4677 mpz_t array[GFC_MAX_DIMENSIONS];
4678 int i;
4679
f00f6dd6 4680 if (e->rank <= 0 || e->shape != NULL)
4ee9c684 4681 return;
4682
4683 for (i = 0; i < e->rank; i++)
60e19868 4684 if (!gfc_array_dimen_size (e, i, &array[i]))
4ee9c684 4685 goto fail;
4686
4687 e->shape = gfc_get_shape (e->rank);
4688
4689 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4690
4691 return;
4692
4693fail:
4694 for (i--; i >= 0; i--)
4695 mpz_clear (array[i]);
4696}
4697
4698
4699/* Given a variable expression node, compute the rank of the expression by
4700 examining the base symbol and any reference structures it may have. */
4701
4702static void
1bcc6eb8 4703expression_rank (gfc_expr *e)
4ee9c684 4704{
4705 gfc_ref *ref;
4706 int i, rank;
4707
6ad53a0c 4708 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4709 could lead to serious confusion... */
4710 gcc_assert (e->expr_type != EXPR_COMPCALL);
4711
4ee9c684 4712 if (e->ref == NULL)
4713 {
4714 if (e->expr_type == EXPR_ARRAY)
4715 goto done;
b14e2757 4716 /* Constructors can have a rank different from one via RESHAPE(). */
4ee9c684 4717
4718 if (e->symtree == NULL)
4719 {
4720 e->rank = 0;
4721 goto done;
4722 }
4723
4724 e->rank = (e->symtree->n.sym->as == NULL)
1bcc6eb8 4725 ? 0 : e->symtree->n.sym->as->rank;
4ee9c684 4726 goto done;
4727 }
4728
4729 rank = 0;
4730
4731 for (ref = e->ref; ref; ref = ref->next)
4732 {
45ade45a 4733 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4734 && ref->u.c.component->attr.function && !ref->next)
4735 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4736
4ee9c684 4737 if (ref->type != REF_ARRAY)
4738 continue;
4739
4740 if (ref->u.ar.type == AR_FULL)
4741 {
4742 rank = ref->u.ar.as->rank;
4743 break;
4744 }
4745
4746 if (ref->u.ar.type == AR_SECTION)
4747 {
1bcc6eb8 4748 /* Figure out the rank of the section. */
4ee9c684 4749 if (rank != 0)
4750 gfc_internal_error ("expression_rank(): Two array specs");
4751
4752 for (i = 0; i < ref->u.ar.dimen; i++)
4753 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4754 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4755 rank++;
4756
4757 break;
4758 }
4759 }
4760
4761 e->rank = rank;
4762
4763done:
4764 expression_shape (e);
4765}
4766
4767
8879941c 4768static void
4769add_caf_get_intrinsic (gfc_expr *e)
4770{
4771 gfc_expr *wrapper, *tmp_expr;
4772 gfc_ref *ref;
4773 int n;
4774
4775 for (ref = e->ref; ref; ref = ref->next)
4776 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4777 break;
4778 if (ref == NULL)
4779 return;
4780
4781 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4782 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4783 return;
4784
4785 tmp_expr = XCNEW (gfc_expr);
4786 *tmp_expr = *e;
4787 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4788 "caf_get", tmp_expr->where, 1, tmp_expr);
4789 wrapper->ts = e->ts;
4790 wrapper->rank = e->rank;
4791 if (e->rank)
4792 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4793 *e = *wrapper;
4794 free (wrapper);
4795}
4796
4797
4798static void
4799remove_caf_get_intrinsic (gfc_expr *e)
4800{
4801 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4802 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4803 gfc_expr *e2 = e->value.function.actual->expr;
5f4a118e 4804 e->value.function.actual->expr = NULL;
8879941c 4805 gfc_free_actual_arglist (e->value.function.actual);
4806 gfc_free_shape (&e->shape, e->rank);
4807 *e = *e2;
4808 free (e2);
4809}
4810
4811
4ee9c684 4812/* Resolve a variable expression. */
4813
60e19868 4814static bool
1bcc6eb8 4815resolve_variable (gfc_expr *e)
4ee9c684 4816{
4817 gfc_symbol *sym;
60e19868 4818 bool t;
c6b395dd 4819
60e19868 4820 t = true;
4ee9c684 4821
d95efb59 4822 if (e->symtree == NULL)
60e19868 4823 return false;
7b82374f 4824 sym = e->symtree->n.sym;
4825
fa76a552 4826 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4827 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4828 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4829 {
4830 if (!actual_arg || inquiry_argument)
4831 {
4832 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4833 "be used as actual argument", sym->name, &e->where);
4834 return false;
4835 }
4836 }
8c2d8d6d 4837 /* TS 29113, 407b. */
fa76a552 4838 else if (e->ts.type == BT_ASSUMED)
8c2d8d6d 4839 {
f00f6dd6 4840 if (!actual_arg)
4841 {
4842 gfc_error ("Assumed-type variable %s at %L may only be used "
4843 "as actual argument", sym->name, &e->where);
60e19868 4844 return false;
f00f6dd6 4845 }
4846 else if (inquiry_argument && !first_actual_arg)
4847 {
4848 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4849 for all inquiry functions in resolve_function; the reason is
4850 that the function-name resolution happens too late in that
4851 function. */
4852 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4853 "an inquiry function shall be the first argument",
4854 sym->name, &e->where);
60e19868 4855 return false;
f00f6dd6 4856 }
4857 }
f00f6dd6 4858 /* TS 29113, C535b. */
fa76a552 4859 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4860 && CLASS_DATA (sym)->as
4861 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4862 || (sym->ts.type != BT_CLASS && sym->as
4863 && sym->as->type == AS_ASSUMED_RANK))
f00f6dd6 4864 {
4865 if (!actual_arg)
4866 {
4867 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4868 "actual argument", sym->name, &e->where);
60e19868 4869 return false;
f00f6dd6 4870 }
4871 else if (inquiry_argument && !first_actual_arg)
4872 {
4873 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4874 for all inquiry functions in resolve_function; the reason is
4875 that the function-name resolution happens too late in that
4876 function. */
4877 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4878 "to an inquiry function shall be the first argument",
4879 sym->name, &e->where);
60e19868 4880 return false;
f00f6dd6 4881 }
8c2d8d6d 4882 }
4883
fa76a552 4884 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
8c2d8d6d 4885 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
f00f6dd6 4886 && e->ref->next == NULL))
fa76a552 4887 {
4888 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4889 "a subobject reference", sym->name, &e->ref->u.ar.where);
4890 return false;
4891 }
4892 /* TS 29113, 407b. */
4893 else if (e->ts.type == BT_ASSUMED && e->ref
4894 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4895 && e->ref->next == NULL))
8c2d8d6d 4896 {
f00f6dd6 4897 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4898 "reference", sym->name, &e->ref->u.ar.where);
60e19868 4899 return false;
8c2d8d6d 4900 }
4901
f00f6dd6 4902 /* TS 29113, C535b. */
4903 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4904 && CLASS_DATA (sym)->as
4905 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4906 || (sym->ts.type != BT_CLASS && sym->as
4907 && sym->as->type == AS_ASSUMED_RANK))
4908 && e->ref
4909 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4910 && e->ref->next == NULL))
4911 {
4912 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4913 "reference", sym->name, &e->ref->u.ar.where);
60e19868 4914 return false;
f00f6dd6 4915 }
4916
4917
cf92f151 4918 /* If this is an associate-name, it may be parsed with an array reference
49dcd9d0 4919 in error even though the target is scalar. Fail directly in this case.
4920 TODO Understand why class scalar expressions must be excluded. */
4921 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4922 {
4923 if (sym->ts.type == BT_CLASS)
4924 gfc_fix_class_refs (e);
4925 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
60e19868 4926 return false;
49dcd9d0 4927 }
7b82374f 4928
c2958b6b 4929 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4930 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4931
7b82374f 4932 /* On the other hand, the parser may not have known this is an array;
4933 in this case, we have to add a FULL reference. */
4934 if (sym->assoc && sym->attr.dimension && !e->ref)
4935 {
4936 e->ref = gfc_get_ref ();
4937 e->ref->type = REF_ARRAY;
4938 e->ref->u.ar.type = AR_FULL;
4939 e->ref->u.ar.dimen = 0;
4940 }
4ee9c684 4941
60e19868 4942 if (e->ref && !resolve_ref (e))
4943 return false;
06367b6e 4944
1e057e9b 4945 if (sym->attr.flavor == FL_PROCEDURE
4946 && (!sym->attr.function
4947 || (sym->attr.function && sym->result
4948 && sym->result->attr.proc_pointer
4949 && !sym->result->attr.function)))
4ee9c684 4950 {
4951 e->ts.type = BT_PROCEDURE;
76279446 4952 goto resolve_procedure;
4ee9c684 4953 }
4954
4955 if (sym->ts.type != BT_UNKNOWN)
4956 gfc_variable_attr (e, &e->ts);
4957 else
4958 {
4959 /* Must be a simple variable reference. */
60e19868 4960 if (!gfc_set_default_type (sym, 1, sym->ns))
4961 return false;
4ee9c684 4962 e->ts = sym->ts;
4963 }
4964
6bfab0c0 4965 if (check_assumed_size_reference (sym, e))
60e19868 4966 return false;
6bfab0c0 4967
c3f3b68d 4968 /* Deal with forward references to entries during gfc_resolve_code, to
c6b395dd 4969 satisfy, at least partially, 12.5.2.5. */
4970 if (gfc_current_ns->entries
1bcc6eb8 4971 && current_entry_id == sym->entry_id
4972 && cs_base
4973 && cs_base->current
4974 && cs_base->current->op != EXEC_ENTRY)
c6b395dd 4975 {
4976 gfc_entry_list *entry;
4977 gfc_formal_arglist *formal;
4978 int n;
be844014 4979 bool seen, saved_specification_expr;
c6b395dd 4980
4981 /* If the symbol is a dummy... */
583d9bb8 4982 if (sym->attr.dummy && sym->ns == gfc_current_ns)
c6b395dd 4983 {
4984 entry = gfc_current_ns->entries;
4985 seen = false;
4986
4987 /* ...test if the symbol is a parameter of previous entries. */
4988 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4989 for (formal = entry->sym->formal; formal; formal = formal->next)
4990 {
4991 if (formal->sym && sym->name == formal->sym->name)
16e3c896 4992 {
4993 seen = true;
4994 break;
4995 }
c6b395dd 4996 }
4997
4998 /* If it has not been seen as a dummy, this is an error. */
4999 if (!seen)
5000 {
5001 if (specification_expr)
0d2b3c9c 5002 gfc_error ("Variable %qs, used in a specification expression"
583d9bb8 5003 ", is referenced at %L before the ENTRY statement "
c6b395dd 5004 "in which it is a parameter",
5005 sym->name, &cs_base->current->loc);
5006 else
0d2b3c9c 5007 gfc_error ("Variable %qs is used at %L before the ENTRY "
c6b395dd 5008 "statement in which it is a parameter",
5009 sym->name, &cs_base->current->loc);
60e19868 5010 t = false;
c6b395dd 5011 }
5012 }
5013
5014 /* Now do the same check on the specification expressions. */
be844014 5015 saved_specification_expr = specification_expr;
5016 specification_expr = true;
c6b395dd 5017 if (sym->ts.type == BT_CHARACTER
60e19868 5018 && !gfc_resolve_expr (sym->ts.u.cl->length))
5019 t = false;
c6b395dd 5020
5021 if (sym->as)
5022 for (n = 0; n < sym->as->rank; n++)
5023 {
60e19868 5024 if (!gfc_resolve_expr (sym->as->lower[n]))
5025 t = false;
5026 if (!gfc_resolve_expr (sym->as->upper[n]))
5027 t = false;
c6b395dd 5028 }
be844014 5029 specification_expr = saved_specification_expr;
c6b395dd 5030
60e19868 5031 if (t)
c6b395dd 5032 /* Update the symbol's entry level. */
5033 sym->entry_id = current_entry_id + 1;
5034 }
5035
e20c5d83 5036 /* If a symbol has been host_associated mark it. This is used latter,
5037 to identify if aliasing is possible via host association. */
5038 if (sym->attr.flavor == FL_VARIABLE
5039 && gfc_current_ns->parent
5040 && (gfc_current_ns->parent == sym->ns
5041 || (gfc_current_ns->parent->parent
5042 && gfc_current_ns->parent->parent == sym->ns)))
5043 sym->attr.host_assoc = 1;
5044
76279446 5045resolve_procedure:
60e19868 5046 if (t && !resolve_procedure_expression (e))
5047 t = false;
76279446 5048
e97ac7c0 5049 /* F2008, C617 and C1229. */
5050 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5051 && gfc_is_coindexed (e))
5052 {
5053 gfc_ref *ref, *ref2 = NULL;
5054
e97ac7c0 5055 for (ref = e->ref; ref; ref = ref->next)
5056 {
5057 if (ref->type == REF_COMPONENT)
5058 ref2 = ref;
5059 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5060 break;
5061 }
5062
5063 for ( ; ref; ref = ref->next)
5064 if (ref->type == REF_COMPONENT)
5065 break;
5066
63bbb2db 5067 /* Expression itself is not coindexed object. */
5068 if (ref && e->ts.type == BT_CLASS)
5069 {
5070 gfc_error ("Polymorphic subobject of coindexed object at %L",
5071 &e->where);
60e19868 5072 t = false;
63bbb2db 5073 }
5074
e97ac7c0 5075 /* Expression itself is coindexed object. */
5076 if (ref == NULL)
5077 {
5078 gfc_component *c;
5079 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5080 for ( ; c; c = c->next)
5081 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5082 {
5083 gfc_error ("Coindexed object with polymorphic allocatable "
5084 "subcomponent at %L", &e->where);
60e19868 5085 t = false;
e97ac7c0 5086 break;
5087 }
5088 }
5089 }
5090
8879941c 5091 if (t)
5092 expression_rank (e);
5093
4fe73152 5094 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
8879941c 5095 add_caf_get_intrinsic (e);
5096
c6b395dd 5097 return t;
4ee9c684 5098}
5099
5100
659c6e2f 5101/* Checks to see that the correct symbol has been host associated.
5102 The only situation where this arises is that in which a twice
5103 contained function is parsed after the host association is made.
f6eeace7 5104 Therefore, on detecting this, change the symbol in the expression
5105 and convert the array reference into an actual arglist if the old
5106 symbol is a variable. */
659c6e2f 5107static bool
5108check_host_association (gfc_expr *e)
5109{
5110 gfc_symbol *sym, *old_sym;
f6eeace7 5111 gfc_symtree *st;
659c6e2f 5112 int n;
f6eeace7 5113 gfc_ref *ref;
5390b584 5114 gfc_actual_arglist *arg, *tail = NULL;
e520a5e1 5115 bool retval = e->expr_type == EXPR_FUNCTION;
659c6e2f 5116
499335e4 5117 /* If the expression is the result of substitution in
5118 interface.c(gfc_extend_expr) because there is no way in
5119 which the host association can be wrong. */
5120 if (e->symtree == NULL
5121 || e->symtree->n.sym == NULL
5122 || e->user_operator)
e520a5e1 5123 return retval;
659c6e2f 5124
5125 old_sym = e->symtree->n.sym;
e520a5e1 5126
659c6e2f 5127 if (gfc_current_ns->parent
659c6e2f 5128 && old_sym->ns != gfc_current_ns)
5129 {
f6eeace7 5130 /* Use the 'USE' name so that renamed module symbols are
5131 correctly handled. */
6f307f34 5132 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
f6eeace7 5133
d5aa86b8 5134 if (sym && old_sym != sym
fe5c28d2 5135 && sym->ts.type == old_sym->ts.type
d5aa86b8 5136 && sym->attr.flavor == FL_PROCEDURE
5137 && sym->attr.contained)
659c6e2f 5138 {
f6eeace7 5139 /* Clear the shape, since it might not be valid. */
642aa6bf 5140 gfc_free_shape (&e->shape, e->rank);
659c6e2f 5141
e4b33af4 5142 /* Give the expression the right symtree! */
5143 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5144 gcc_assert (st != NULL);
659c6e2f 5145
e4b33af4 5146 if (old_sym->attr.flavor == FL_PROCEDURE
5147 || e->expr_type == EXPR_FUNCTION)
5148 {
f6eeace7 5149 /* Original was function so point to the new symbol, since
5150 the actual argument list is already attached to the
293d72e0 5151 expression. */
f6eeace7 5152 e->value.function.esym = NULL;
5153 e->symtree = st;
5154 }
5155 else
5156 {
5157 /* Original was variable so convert array references into
5158 an actual arglist. This does not need any checking now
945c743f 5159 since resolve_function will take care of it. */
f6eeace7 5160 e->value.function.actual = NULL;
5161 e->expr_type = EXPR_FUNCTION;
5162 e->symtree = st;
659c6e2f 5163
f6eeace7 5164 /* Ambiguity will not arise if the array reference is not
5165 the last reference. */
5166 for (ref = e->ref; ref; ref = ref->next)
5167 if (ref->type == REF_ARRAY && ref->next == NULL)
5168 break;
5169
5170 gcc_assert (ref->type == REF_ARRAY);
5171
5172 /* Grab the start expressions from the array ref and
5173 copy them into actual arguments. */
5174 for (n = 0; n < ref->u.ar.dimen; n++)
5175 {
5176 arg = gfc_get_actual_arglist ();
5177 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5178 if (e->value.function.actual == NULL)
5179 tail = e->value.function.actual = arg;
5180 else
5181 {
5182 tail->next = arg;
5183 tail = arg;
5184 }
5185 }
659c6e2f 5186
f6eeace7 5187 /* Dump the reference list and set the rank. */
5188 gfc_free_ref_list (e->ref);
5189 e->ref = NULL;
5190 e->rank = sym->as ? sym->as->rank : 0;
5191 }
5192
5193 gfc_resolve_expr (e);
5194 sym->refs++;
659c6e2f 5195 }
5196 }
e520a5e1 5197 /* This might have changed! */
659c6e2f 5198 return e->expr_type == EXPR_FUNCTION;
5199}
5200
5201
0ff77f4e 5202static void
5203gfc_resolve_character_operator (gfc_expr *e)
5204{
5205 gfc_expr *op1 = e->value.op.op1;
5206 gfc_expr *op2 = e->value.op.op2;
5207 gfc_expr *e1 = NULL;
5208 gfc_expr *e2 = NULL;
5209
dcb1b019 5210 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
0ff77f4e 5211
eeebe20b 5212 if (op1->ts.u.cl && op1->ts.u.cl->length)
5213 e1 = gfc_copy_expr (op1->ts.u.cl->length);
0ff77f4e 5214 else if (op1->expr_type == EXPR_CONSTANT)
126387b5 5215 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5216 op1->value.character.length);
0ff77f4e 5217
eeebe20b 5218 if (op2->ts.u.cl && op2->ts.u.cl->length)
5219 e2 = gfc_copy_expr (op2->ts.u.cl->length);
0ff77f4e 5220 else if (op2->expr_type == EXPR_CONSTANT)
126387b5 5221 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5222 op2->value.character.length);
0ff77f4e 5223
d270ce52 5224 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
0ff77f4e 5225
5226 if (!e1 || !e2)
ebbbec49 5227 {
5228 gfc_free_expr (e1);
5229 gfc_free_expr (e2);
d6463863 5230
ebbbec49 5231 return;
5232 }
0ff77f4e 5233
eeebe20b 5234 e->ts.u.cl->length = gfc_add (e1, e2);
5235 e->ts.u.cl->length->ts.type = BT_INTEGER;
5236 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5237 gfc_simplify_expr (e->ts.u.cl->length, 0);
5238 gfc_resolve_expr (e->ts.u.cl->length);
0ff77f4e 5239
5240 return;
5241}
5242
5243
5244/* Ensure that an character expression has a charlen and, if possible, a
5245 length expression. */
5246
5247static void
5248fixup_charlen (gfc_expr *e)
5249{
5250 /* The cases fall through so that changes in expression type and the need
5251 for multiple fixes are picked up. In all circumstances, a charlen should
5252 be available for the middle end to hang a backend_decl on. */
5253 switch (e->expr_type)
5254 {
5255 case EXPR_OP:
5256 gfc_resolve_character_operator (e);
5257
5258 case EXPR_ARRAY:
5259 if (e->expr_type == EXPR_ARRAY)
5260 gfc_resolve_character_array_constructor (e);
5261
5262 case EXPR_SUBSTRING:
eeebe20b 5263 if (!e->ts.u.cl && e->ref)
0ff77f4e 5264 gfc_resolve_substring_charlen (e);
5265
5266 default:
eeebe20b 5267 if (!e->ts.u.cl)
d270ce52 5268 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
0ff77f4e 5269
5270 break;
5271 }
5272}
5273
5274
930fe1de 5275/* Update an actual argument to include the passed-object for type-bound
5276 procedures at the right position. */
5277
5278static gfc_actual_arglist*
fe9b08a2 5279update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5280 const char *name)
930fe1de 5281{
540483f2 5282 gcc_assert (argpos > 0);
5283
930fe1de 5284 if (argpos == 1)
5285 {
5286 gfc_actual_arglist* result;
5287
5288 result = gfc_get_actual_arglist ();
5289 result->expr = po;
5290 result->next = lst;
fe9b08a2 5291 if (name)
5292 result->name = name;
930fe1de 5293
5294 return result;
5295 }
5296
fe9b08a2 5297 if (lst)
5298 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5299 else
5300 lst = update_arglist_pass (NULL, po, argpos - 1, name);
930fe1de 5301 return lst;
5302}
5303
5304
e2f06a48 5305/* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
930fe1de 5306
e2f06a48 5307static gfc_expr*
5308extract_compcall_passed_object (gfc_expr* e)
930fe1de 5309{
5310 gfc_expr* po;
930fe1de 5311
e2f06a48 5312 gcc_assert (e->expr_type == EXPR_COMPCALL);
930fe1de 5313
7d034542 5314 if (e->value.compcall.base_object)
5315 po = gfc_copy_expr (e->value.compcall.base_object);
5316 else
5317 {
5318 po = gfc_get_expr ();
5319 po->expr_type = EXPR_VARIABLE;
5320 po->symtree = e->symtree;
5321 po->ref = gfc_copy_ref (e->ref);
aea8962c 5322 po->where = e->where;
7d034542 5323 }
930fe1de 5324
60e19868 5325 if (!gfc_resolve_expr (po))
e2f06a48 5326 return NULL;
5327
5328 return po;
5329}
5330
5331
5332/* Update the arglist of an EXPR_COMPCALL expression to include the
5333 passed-object. */
5334
60e19868 5335static bool
e2f06a48 5336update_compcall_arglist (gfc_expr* e)
5337{
5338 gfc_expr* po;
5339 gfc_typebound_proc* tbp;
5340
5341 tbp = e->value.compcall.tbp;
5342
540483f2 5343 if (tbp->error)
60e19868 5344 return false;
540483f2 5345
e2f06a48 5346 po = extract_compcall_passed_object (e);
5347 if (!po)
60e19868 5348 return false;
e2f06a48 5349
7d034542 5350 if (tbp->nopass || e->value.compcall.ignore_pass)
930fe1de 5351 {
5352 gfc_free_expr (po);
60e19868 5353 return true;
930fe1de 5354 }
5355
5356 gcc_assert (tbp->pass_arg_num > 0);
5357 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
fe9b08a2 5358 tbp->pass_arg_num,
5359 tbp->pass_arg);
5360
60e19868 5361 return true;
fe9b08a2 5362}
5363
5364
5365/* Extract the passed object from a PPC call (a copy of it). */
5366
5367static gfc_expr*
5368extract_ppc_passed_object (gfc_expr *e)
5369{
5370 gfc_expr *po;
5371 gfc_ref **ref;
5372
5373 po = gfc_get_expr ();
5374 po->expr_type = EXPR_VARIABLE;
5375 po->symtree = e->symtree;
5376 po->ref = gfc_copy_ref (e->ref);
aea8962c 5377 po->where = e->where;
fe9b08a2 5378
5379 /* Remove PPC reference. */
5380 ref = &po->ref;
5381 while ((*ref)->next)
aea8962c 5382 ref = &(*ref)->next;
fe9b08a2 5383 gfc_free_ref_list (*ref);
5384 *ref = NULL;
5385
60e19868 5386 if (!gfc_resolve_expr (po))
fe9b08a2 5387 return NULL;
5388
5389 return po;
5390}
5391
5392
5393/* Update the actual arglist of a procedure pointer component to include the
5394 passed-object. */
5395
60e19868 5396static bool
fe9b08a2 5397update_ppc_arglist (gfc_expr* e)
5398{
5399 gfc_expr* po;
5400 gfc_component *ppc;
5401 gfc_typebound_proc* tb;
5402
b3961d7b 5403 ppc = gfc_get_proc_ptr_comp (e);
5404 if (!ppc)
60e19868 5405 return false;
fe9b08a2 5406
5407 tb = ppc->tb;
5408
5409 if (tb->error)
60e19868 5410 return false;
fe9b08a2 5411 else if (tb->nopass)
60e19868 5412 return true;
fe9b08a2 5413
5414 po = extract_ppc_passed_object (e);
5415 if (!po)
60e19868 5416 return false;
fe9b08a2 5417
23d37e37 5418 /* F08:R739. */
f00f6dd6 5419 if (po->rank != 0)
fe9b08a2 5420 {
5421 gfc_error ("Passed-object at %L must be scalar", &e->where);
60e19868 5422 return false;
fe9b08a2 5423 }
5424
23d37e37 5425 /* F08:C611. */
5426 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5427 {
5428 gfc_error ("Base object for procedure-pointer component call at %L is of"
0d2b3c9c 5429 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
60e19868 5430 return false;
23d37e37 5431 }
5432
fe9b08a2 5433 gcc_assert (tb->pass_arg_num > 0);
5434 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5435 tb->pass_arg_num,
5436 tb->pass_arg);
930fe1de 5437
60e19868 5438 return true;
930fe1de 5439}
5440
5441
61c3b81d 5442/* Check that the object a TBP is called on is valid, i.e. it must not be
5443 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5444
60e19868 5445static bool
61c3b81d 5446check_typebound_baseobject (gfc_expr* e)
5447{
5448 gfc_expr* base;
60e19868 5449 bool return_value = false;
61c3b81d 5450
5451 base = extract_compcall_passed_object (e);
5452 if (!base)
60e19868 5453 return false;
61c3b81d 5454
1de1b1a9 5455 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
8337b324 5456
cc786707 5457 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
60e19868 5458 return false;
cc786707 5459
23d37e37 5460 /* F08:C611. */
8337b324 5461 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
61c3b81d 5462 {
5463 gfc_error ("Base object for type-bound procedure call at %L is of"
0d2b3c9c 5464 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
0e0b4034 5465 goto cleanup;
61c3b81d 5466 }
5467
23d37e37 5468 /* F08:C1230. If the procedure called is NOPASS,
5469 the base object must be scalar. */
f00f6dd6 5470 if (e->value.compcall.tbp->nopass && base->rank != 0)
4b68c8f7 5471 {
5472 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5473 " be scalar", &e->where);
0e0b4034 5474 goto cleanup;
4b68c8f7 5475 }
5476
60e19868 5477 return_value = true;
0e0b4034 5478
5479cleanup:
5480 gfc_free_expr (base);
5481 return return_value;
61c3b81d 5482}
5483
5484
930fe1de 5485/* Resolve a call to a type-bound procedure, either function or subroutine,
5486 statically from the data in an EXPR_COMPCALL expression. The adapted
5487 arglist and the target-procedure symtree are returned. */
5488
60e19868 5489static bool
930fe1de 5490resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5491 gfc_actual_arglist** actual)
5492{
5493 gcc_assert (e->expr_type == EXPR_COMPCALL);
e2f06a48 5494 gcc_assert (!e->value.compcall.tbp->is_generic);
930fe1de 5495
5496 /* Update the actual arglist for PASS. */
60e19868 5497 if (!update_compcall_arglist (e))
5498 return false;
930fe1de 5499
5500 *actual = e->value.compcall.actual;
e2f06a48 5501 *target = e->value.compcall.tbp->u.specific;
930fe1de 5502
5503 gfc_free_ref_list (e->ref);
5504 e->ref = NULL;
5505 e->value.compcall.actual = NULL;
5506
9749851b 5507 /* If we find a deferred typebound procedure, check for derived types
ad65d2f7 5508 that an overriding typebound procedure has not been missed. */
5509 if (e->value.compcall.name
5510 && !e->value.compcall.tbp->non_overridable
5511 && e->value.compcall.base_object
5512 && e->value.compcall.base_object->ts.type == BT_DERIVED)
9749851b 5513 {
5514 gfc_symtree *st;
5515 gfc_symbol *derived;
5516
5517 /* Use the derived type of the base_object. */
5518 derived = e->value.compcall.base_object->ts.u.derived;
5519 st = NULL;
5520
df084314 5521 /* If necessary, go through the inheritance chain. */
9749851b 5522 while (!st && derived)
5523 {
5524 /* Look for the typebound procedure 'name'. */
5525 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5526 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5527 e->value.compcall.name);
5528 if (!st)
5529 derived = gfc_get_derived_super_type (derived);
5530 }
5531
5532 /* Now find the specific name in the derived type namespace. */
5533 if (st && st->n.tb && st->n.tb->u.specific)
5534 gfc_find_sym_tree (st->n.tb->u.specific->name,
5535 derived->ns, 1, &st);
5536 if (st)
5537 *target = st;
5538 }
60e19868 5539 return true;
930fe1de 5540}
5541
5542
89132055 5543/* Get the ultimate declared type from an expression. In addition,
5544 return the last class/derived type reference and the copy of the
24980a98 5545 reference list. If check_types is set true, derived types are
5546 identified as well as class references. */
89132055 5547static gfc_symbol*
5548get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
24980a98 5549 gfc_expr *e, bool check_types)
89132055 5550{
5551 gfc_symbol *declared;
5552 gfc_ref *ref;
5553
5554 declared = NULL;
5555 if (class_ref)
5556 *class_ref = NULL;
5557 if (new_ref)
5558 *new_ref = gfc_copy_ref (e->ref);
5559
5560 for (ref = e->ref; ref; ref = ref->next)
5561 {
5562 if (ref->type != REF_COMPONENT)
5563 continue;
5564
24980a98 5565 if ((ref->u.c.component->ts.type == BT_CLASS
5566 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5567 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
89132055 5568 {
5569 declared = ref->u.c.component->ts.u.derived;
5570 if (class_ref)
5571 *class_ref = ref;
5572 }
5573 }
5574
5575 if (declared == NULL)
5576 declared = e->symtree->n.sym->ts.u.derived;
5577
5578 return declared;
5579}
5580
5581
e2f06a48 5582/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5583 which of the specific bindings (if any) matches the arglist and transform
5584 the expression into a call of that binding. */
5585
60e19868 5586static bool
09c509ed 5587resolve_typebound_generic_call (gfc_expr* e, const char **name)
e2f06a48 5588{
5589 gfc_typebound_proc* genproc;
5590 const char* genname;
89132055 5591 gfc_symtree *st;
5592 gfc_symbol *derived;
e2f06a48 5593
5594 gcc_assert (e->expr_type == EXPR_COMPCALL);
5595 genname = e->value.compcall.name;
5596 genproc = e->value.compcall.tbp;
5597
5598 if (!genproc->is_generic)
60e19868 5599 return true;
e2f06a48 5600
5601 /* Try the bindings on this type and in the inheritance hierarchy. */
5602 for (; genproc; genproc = genproc->overridden)
5603 {
5604 gfc_tbp_generic* g;
5605
5606 gcc_assert (genproc->is_generic);
5607 for (g = genproc->u.generic; g; g = g->next)
5608 {
5609 gfc_symbol* target;
5610 gfc_actual_arglist* args;
5611 bool matches;
5612
5613 gcc_assert (g->specific);
540483f2 5614
5615 if (g->specific->error)
5616 continue;
5617
e2f06a48 5618 target = g->specific->u.specific->n.sym;
5619
5620 /* Get the right arglist by handling PASS/NOPASS. */
5621 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5622 if (!g->specific->nopass)
5623 {
5624 gfc_expr* po;
5625 po = extract_compcall_passed_object (e);
5626 if (!po)
1f947744 5627 {
5628 gfc_free_actual_arglist (args);
60e19868 5629 return false;
1f947744 5630 }
e2f06a48 5631
540483f2 5632 gcc_assert (g->specific->pass_arg_num > 0);
5633 gcc_assert (!g->specific->error);
fe9b08a2 5634 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5635 g->specific->pass_arg);
e2f06a48 5636 }
e3154a05 5637 resolve_actual_arglist (args, target->attr.proc,
6777213b 5638 is_external_proc (target)
5639 && gfc_sym_get_dummy_args (target) == NULL);
e2f06a48 5640
5641 /* Check if this arglist matches the formal. */
e3154a05 5642 matches = gfc_arglist_matches_symbol (&args, target);
e2f06a48 5643
5644 /* Clean up and break out of the loop if we've found it. */
5645 gfc_free_actual_arglist (args);
5646 if (matches)
5647 {
5648 e->value.compcall.tbp = g->specific;
217ca056 5649 genname = g->specific_st->name;
09c509ed 5650 /* Pass along the name for CLASS methods, where the vtab
5651 procedure pointer component has to be referenced. */
5652 if (name)
217ca056 5653 *name = genname;
e2f06a48 5654 goto success;
5655 }
5656 }
5657 }
5658
5659 /* Nothing matching found! */
5660 gfc_error ("Found no matching specific binding for the call to the GENERIC"
0d2b3c9c 5661 " %qs at %L", genname, &e->where);
60e19868 5662 return false;
e2f06a48 5663
5664success:
89132055 5665 /* Make sure that we have the right specific instance for the name. */
24980a98 5666 derived = get_declared_from_expr (NULL, NULL, e, true);
89132055 5667
d6a853a7 5668 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
89132055 5669 if (st)
5670 e->value.compcall.tbp = st->n.tb;
5671
60e19868 5672 return true;
e2f06a48 5673}
5674
5675
930fe1de 5676/* Resolve a call to a type-bound subroutine. */
5677
60e19868 5678static bool
0bbb91a9 5679resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
930fe1de 5680{
5681 gfc_actual_arglist* newactual;
5682 gfc_symtree* target;
5683
e2f06a48 5684 /* Check that's really a SUBROUTINE. */
578d3f19 5685 if (!c->expr1->value.compcall.tbp->subroutine)
e2f06a48 5686 {
0d2b3c9c 5687 gfc_error ("%qs at %L should be a SUBROUTINE",
578d3f19 5688 c->expr1->value.compcall.name, &c->loc);
60e19868 5689 return false;
e2f06a48 5690 }
5691
60e19868 5692 if (!check_typebound_baseobject (c->expr1))
5693 return false;
61c3b81d 5694
09c509ed 5695 /* Pass along the name for CLASS methods, where the vtab
5696 procedure pointer component has to be referenced. */
5697 if (name)
5698 *name = c->expr1->value.compcall.name;
5699
60e19868 5700 if (!resolve_typebound_generic_call (c->expr1, name))
5701 return false;
e2f06a48 5702
0bbb91a9 5703 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5704 if (overridable)
5705 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5706
930fe1de 5707 /* Transform into an ordinary EXEC_CALL for now. */
5708
60e19868 5709 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5710 return false;
930fe1de 5711
5712 c->ext.actual = newactual;
5713 c->symtree = target;
7d034542 5714 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
930fe1de 5715
578d3f19 5716 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
d94c1385 5717
578d3f19 5718 gfc_free_expr (c->expr1);
d94c1385 5719 c->expr1 = gfc_get_expr ();
5720 c->expr1->expr_type = EXPR_FUNCTION;
5721 c->expr1->symtree = target;
5722 c->expr1->where = c->loc;
930fe1de 5723
5724 return resolve_call (c);
5725}
5726
5727
09c509ed 5728/* Resolve a component-call expression. */
60e19868 5729static bool
09c509ed 5730resolve_compcall (gfc_expr* e, const char **name)
930fe1de 5731{
5732 gfc_actual_arglist* newactual;
5733 gfc_symtree* target;
5734
e2f06a48 5735 /* Check that's really a FUNCTION. */
09c509ed 5736 if (!e->value.compcall.tbp->function)
e2f06a48 5737 {
0d2b3c9c 5738 gfc_error ("%qs at %L should be a FUNCTION",
e2f06a48 5739 e->value.compcall.name, &e->where);
60e19868 5740 return false;
e2f06a48 5741 }
5742
7d034542 5743 /* These must not be assign-calls! */
5744 gcc_assert (!e->value.compcall.assign);
5745
60e19868 5746 if (!check_typebound_baseobject (e))
5747 return false;
61c3b81d 5748
09c509ed 5749 /* Pass along the name for CLASS methods, where the vtab
5750 procedure pointer component has to be referenced. */
5751 if (name)
5752 *name = e->value.compcall.name;
5753
60e19868 5754 if (!resolve_typebound_generic_call (e, name))
5755 return false;
6ad53a0c 5756 gcc_assert (!e->value.compcall.tbp->is_generic);
5757
5758 /* Take the rank from the function's symbol. */
5759 if (e->value.compcall.tbp->u.specific->n.sym->as)
5760 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
e2f06a48 5761
5762 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
930fe1de 5763 arglist to the TBP's binding target. */
5764
60e19868 5765 if (!resolve_typebound_static (e, &target, &newactual))
5766 return false;
930fe1de 5767
5768 e->value.function.actual = newactual;
b652cb7e 5769 e->value.function.name = NULL;
88a37d69 5770 e->value.function.esym = target->n.sym;
e2f06a48 5771 e->value.function.isym = NULL;
930fe1de 5772 e->symtree = target;
e3154a05 5773 e->ts = target->n.sym->ts;
930fe1de 5774 e->expr_type = EXPR_FUNCTION;
5775
09c509ed 5776 /* Resolution is not necessary if this is a class subroutine; this
5777 function only has to identify the specific proc. Resolution of
5778 the call will be done next in resolve_typebound_call. */
5779 return gfc_resolve_expr (e);
f3f303c6 5780}
5781
5782
9ce53a40 5783static bool resolve_fl_derived (gfc_symbol *sym);
5784
f3f303c6 5785
09c509ed 5786/* Resolve a typebound function, or 'method'. First separate all
5787 the non-CLASS references by calling resolve_compcall directly. */
ae925cc0 5788
60e19868 5789static bool
ae925cc0 5790resolve_typebound_function (gfc_expr* e)
d94c1385 5791{
09c509ed 5792 gfc_symbol *declared;
5793 gfc_component *c;
f3f303c6 5794 gfc_ref *new_ref;
5795 gfc_ref *class_ref;
5796 gfc_symtree *st;
09c509ed 5797 const char *name;
09c509ed 5798 gfc_typespec ts;
abca3541 5799 gfc_expr *expr;
6df74ab4 5800 bool overridable;
f3f303c6 5801
5802 st = e->symtree;
abca3541 5803
5804 /* Deal with typebound operators for CLASS objects. */
5805 expr = e->value.compcall.base_object;
6df74ab4 5806 overridable = !e->value.compcall.tbp->non_overridable;
0f8ad762 5807 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
abca3541 5808 {
24980a98 5809 /* If the base_object is not a variable, the corresponding actual
5810 argument expression must be stored in e->base_expression so
5811 that the corresponding tree temporary can be used as the base
5812 object in gfc_conv_procedure_call. */
5813 if (expr->expr_type != EXPR_VARIABLE)
5814 {
5815 gfc_actual_arglist *args;
5816
5817 for (args= e->value.function.actual; args; args = args->next)
5818 {
5819 if (expr == args->expr)
5820 expr = args->expr;
5821 }
5822 }
5823
abca3541 5824 /* Since the typebound operators are generic, we have to ensure
5825 that any delays in resolution are corrected and that the vtab
5826 is present. */
0f8ad762 5827 ts = expr->ts;
abca3541 5828 declared = ts.u.derived;
607ae689 5829 c = gfc_find_component (declared, "_vptr", true, true);
abca3541 5830 if (c->ts.u.derived == NULL)
5831 c->ts.u.derived = gfc_find_derived_vtab (declared);
5832
60e19868 5833 if (!resolve_compcall (e, &name))
5834 return false;
abca3541 5835
5836 /* Use the generic name if it is there. */
5837 name = name ? name : e->value.function.esym->name;
5838 e->symtree = expr->symtree;
e0931d1e 5839 e->ref = gfc_copy_ref (expr->ref);
24980a98 5840 get_declared_from_expr (&class_ref, NULL, e, false);
5841
5842 /* Trim away the extraneous references that emerge from nested
5843 use of interface.c (extend_expr). */
5844 if (class_ref && class_ref->next)
5845 {
5846 gfc_free_ref_list (class_ref->next);
5847 class_ref->next = NULL;
5848 }
5849 else if (e->ref && !class_ref)
5850 {
5851 gfc_free_ref_list (e->ref);
5852 e->ref = NULL;
5853 }
5854
607ae689 5855 gfc_add_vptr_component (e);
abca3541 5856 gfc_add_component_ref (e, name);
5857 e->value.function.esym = NULL;
24980a98 5858 if (expr->expr_type != EXPR_VARIABLE)
5859 e->base_expr = expr;
60e19868 5860 return true;
abca3541 5861 }
5862
ae925cc0 5863 if (st == NULL)
09c509ed 5864 return resolve_compcall (e, NULL);
d94c1385 5865
60e19868 5866 if (!resolve_ref (e))
5867 return false;
ae44f506 5868
f3f303c6 5869 /* Get the CLASS declared type. */
24980a98 5870 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
080819af 5871
9ce53a40 5872 if (!resolve_fl_derived (declared))
5873 return false;
d94c1385 5874
f3f303c6 5875 /* Weed out cases of the ultimate component being a derived type. */
ae925cc0 5876 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
09c509ed 5877 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
f3f303c6 5878 {
5879 gfc_free_ref_list (new_ref);
09c509ed 5880 return resolve_compcall (e, NULL);
191c342b 5881 }
5882
607ae689 5883 c = gfc_find_component (declared, "_data", true, true);
09c509ed 5884 declared = c->ts.u.derived;
d94c1385 5885
09c509ed 5886 /* Treat the call as if it is a typebound procedure, in order to roll
5887 out the correct name for the specific function. */
60e19868 5888 if (!resolve_compcall (e, &name))
1f947744 5889 {
5890 gfc_free_ref_list (new_ref);
60e19868 5891 return false;
1f947744 5892 }
09c509ed 5893 ts = e->ts;
d94c1385 5894
6df74ab4 5895 if (overridable)
5896 {
5897 /* Convert the expression to a procedure pointer component call. */
5898 e->value.function.esym = NULL;
5899 e->symtree = st;
d94c1385 5900
d6463863 5901 if (new_ref)
6df74ab4 5902 e->ref = new_ref;
d94c1385 5903
6df74ab4 5904 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5905 gfc_add_vptr_component (e);
5906 gfc_add_component_ref (e, name);
5907
5908 /* Recover the typespec for the expression. This is really only
5909 necessary for generic procedures, where the additional call
5910 to gfc_add_component_ref seems to throw the collection of the
5911 correct typespec. */
5912 e->ts = ts;
5913 }
f48281e6 5914 else if (new_ref)
5915 gfc_free_ref_list (new_ref);
f3f303c6 5916
60e19868 5917 return true;
d94c1385 5918}
5919
09c509ed 5920/* Resolve a typebound subroutine, or 'method'. First separate all
5921 the non-CLASS references by calling resolve_typebound_call
5922 directly. */
ae925cc0 5923
60e19868 5924static bool
ae925cc0 5925resolve_typebound_subroutine (gfc_code *code)
d94c1385 5926{
abca3541 5927 gfc_symbol *declared;
5928 gfc_component *c;
f3f303c6 5929 gfc_ref *new_ref;
5930 gfc_ref *class_ref;
5931 gfc_symtree *st;
09c509ed 5932 const char *name;
5933 gfc_typespec ts;
abca3541 5934 gfc_expr *expr;
6df74ab4 5935 bool overridable;
f3f303c6 5936
5937 st = code->expr1->symtree;
abca3541 5938
5939 /* Deal with typebound operators for CLASS objects. */
5940 expr = code->expr1->value.compcall.base_object;
6df74ab4 5941 overridable = !code->expr1->value.compcall.tbp->non_overridable;
4cc3facb 5942 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
abca3541 5943 {
24980a98 5944 /* If the base_object is not a variable, the corresponding actual
5945 argument expression must be stored in e->base_expression so
5946 that the corresponding tree temporary can be used as the base
5947 object in gfc_conv_procedure_call. */
5948 if (expr->expr_type != EXPR_VARIABLE)
5949 {
5950 gfc_actual_arglist *args;
5951
5952 args= code->expr1->value.function.actual;
5953 for (; args; args = args->next)
5954 if (expr == args->expr)
5955 expr = args->expr;
5956 }
5957
abca3541 5958 /* Since the typebound operators are generic, we have to ensure
5959 that any delays in resolution are corrected and that the vtab
5960 is present. */
4cc3facb 5961 declared = expr->ts.u.derived;
607ae689 5962 c = gfc_find_component (declared, "_vptr", true, true);
abca3541 5963 if (c->ts.u.derived == NULL)
5964 c->ts.u.derived = gfc_find_derived_vtab (declared);
5965
0bbb91a9 5966 if (!resolve_typebound_call (code, &name, NULL))
60e19868 5967 return false;
abca3541 5968
5969 /* Use the generic name if it is there. */
5970 name = name ? name : code->expr1->value.function.esym->name;
5971 code->expr1->symtree = expr->symtree;
4cc3facb 5972 code->expr1->ref = gfc_copy_ref (expr->ref);
24980a98 5973
5974 /* Trim away the extraneous references that emerge from nested
5975 use of interface.c (extend_expr). */
5976 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5977 if (class_ref && class_ref->next)
5978 {
5979 gfc_free_ref_list (class_ref->next);
5980 class_ref->next = NULL;
5981 }
5982 else if (code->expr1->ref && !class_ref)
5983 {
5984 gfc_free_ref_list (code->expr1->ref);
5985 code->expr1->ref = NULL;
5986 }
5987
5988 /* Now use the procedure in the vtable. */
607ae689 5989 gfc_add_vptr_component (code->expr1);
abca3541 5990 gfc_add_component_ref (code->expr1, name);
5991 code->expr1->value.function.esym = NULL;
24980a98 5992 if (expr->expr_type != EXPR_VARIABLE)
5993 code->expr1->base_expr = expr;
60e19868 5994 return true;
abca3541 5995 }
5996
ae925cc0 5997 if (st == NULL)
0bbb91a9 5998 return resolve_typebound_call (code, NULL, NULL);
d94c1385 5999
60e19868 6000 if (!resolve_ref (code->expr1))
6001 return false;
ae44f506 6002
f3f303c6 6003 /* Get the CLASS declared type. */
24980a98 6004 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
d94c1385 6005
f3f303c6 6006 /* Weed out cases of the ultimate component being a derived type. */
ae925cc0 6007 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
09c509ed 6008 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
f3f303c6 6009 {
6010 gfc_free_ref_list (new_ref);
0bbb91a9 6011 return resolve_typebound_call (code, NULL, NULL);
217ca056 6012 }
d94c1385 6013
0bbb91a9 6014 if (!resolve_typebound_call (code, &name, &overridable))
1f947744 6015 {
6016 gfc_free_ref_list (new_ref);
60e19868 6017 return false;
1f947744 6018 }
09c509ed 6019 ts = code->expr1->ts;
d94c1385 6020
6df74ab4 6021 if (overridable)
6022 {
6023 /* Convert the expression to a procedure pointer component call. */
6024 code->expr1->value.function.esym = NULL;
6025 code->expr1->symtree = st;
d94c1385 6026
6df74ab4 6027 if (new_ref)
6028 code->expr1->ref = new_ref;
6029
6030 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6031 gfc_add_vptr_component (code->expr1);
6032 gfc_add_component_ref (code->expr1, name);
d94c1385 6033
6df74ab4 6034 /* Recover the typespec for the expression. This is really only
6035 necessary for generic procedures, where the additional call
6036 to gfc_add_component_ref seems to throw the collection of the
6037 correct typespec. */
6038 code->expr1->ts = ts;
6039 }
68cc384e 6040 else if (new_ref)
6041 gfc_free_ref_list (new_ref);
f3f303c6 6042
60e19868 6043 return true;
930fe1de 6044}
6045
6046
64e93293 6047/* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6048
60e19868 6049static bool
64e93293 6050resolve_ppc_call (gfc_code* c)
6051{
6052 gfc_component *comp;
1de1b1a9 6053
b3961d7b 6054 comp = gfc_get_proc_ptr_comp (c->expr1);
6055 gcc_assert (comp != NULL);
64e93293 6056
578d3f19 6057 c->resolved_sym = c->expr1->symtree->n.sym;
6058 c->expr1->expr_type = EXPR_VARIABLE;
64e93293 6059
6060 if (!comp->attr.subroutine)
578d3f19 6061 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
64e93293 6062
60e19868 6063 if (!resolve_ref (c->expr1))
6064 return false;
9f65c497 6065
60e19868 6066 if (!update_ppc_arglist (c->expr1))
6067 return false;
fe9b08a2 6068
6069 c->ext.actual = c->expr1->value.compcall.actual;
6070
080819af 6071 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6072 !(comp->ts.interface
60e19868 6073 && comp->ts.interface->formal)))
6074 return false;
64e93293 6075
0252ef5c 6076 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6077 return false;
6078
a84cb1a9 6079 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
64e93293 6080
60e19868 6081 return true;
64e93293 6082}
6083
6084
6085/* Resolve a Function Call to a Procedure Pointer Component (Function). */
6086
60e19868 6087static bool
64e93293 6088resolve_expr_ppc (gfc_expr* e)
6089{
6090 gfc_component *comp;
1de1b1a9 6091
b3961d7b 6092 comp = gfc_get_proc_ptr_comp (e);
6093 gcc_assert (comp != NULL);
64e93293 6094
6095 /* Convert to EXPR_FUNCTION. */
6096 e->expr_type = EXPR_FUNCTION;
6097 e->value.function.isym = NULL;
6098 e->value.function.actual = e->value.compcall.actual;
6099 e->ts = comp->ts;
85d1c108 6100 if (comp->as != NULL)
6101 e->rank = comp->as->rank;
64e93293 6102
6103 if (!comp->attr.function)
6104 gfc_add_function (&comp->attr, comp->name, &e->where);
6105
60e19868 6106 if (!resolve_ref (e))
6107 return false;
9f65c497 6108
080819af 6109 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6110 !(comp->ts.interface
60e19868 6111 && comp->ts.interface->formal)))
6112 return false;
64e93293 6113
60e19868 6114 if (!update_ppc_arglist (e))
6115 return false;
fe9b08a2 6116
0252ef5c 6117 if (!check_pure_function(e))
6118 return false;
6119
a84cb1a9 6120 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
64e93293 6121
60e19868 6122 return true;
64e93293 6123}
6124
6125
c315461d 6126static bool
6127gfc_is_expandable_expr (gfc_expr *e)
6128{
6129 gfc_constructor *con;
6130
6131 if (e->expr_type == EXPR_ARRAY)
6132 {
6133 /* Traverse the constructor looking for variables that are flavor
6134 parameter. Parameters must be expanded since they are fully used at
6135 compile time. */
126387b5 6136 con = gfc_constructor_first (e->value.constructor);
6137 for (; con; con = gfc_constructor_next (con))
c315461d 6138 {
6139 if (con->expr->expr_type == EXPR_VARIABLE
126387b5 6140 && con->expr->symtree
6141 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
c315461d 6142 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6143 return true;
6144 if (con->expr->expr_type == EXPR_ARRAY
126387b5 6145 && gfc_is_expandable_expr (con->expr))
c315461d 6146 return true;
6147 }
6148 }
6149
6150 return false;
6151}
6152
4ee9c684 6153/* Resolve an expression. That is, make sure that types of operands agree
6154 with their operators, intrinsic operators are converted to function calls
6155 for overloaded types and unresolved function references are resolved. */
6156
60e19868 6157bool
1bcc6eb8 6158gfc_resolve_expr (gfc_expr *e)
4ee9c684 6159{
60e19868 6160 bool t;
f00f6dd6 6161 bool inquiry_save, actual_arg_save, first_actual_arg_save;
4ee9c684 6162
6163 if (e == NULL)
60e19868 6164 return true;
4ee9c684 6165
e97ac7c0 6166 /* inquiry_argument only applies to variables. */
6167 inquiry_save = inquiry_argument;
f00f6dd6 6168 actual_arg_save = actual_arg;
6169 first_actual_arg_save = first_actual_arg;
6170
e97ac7c0 6171 if (e->expr_type != EXPR_VARIABLE)
f00f6dd6 6172 {
6173 inquiry_argument = false;
6174 actual_arg = false;
6175 first_actual_arg = false;
6176 }
e97ac7c0 6177
4ee9c684 6178 switch (e->expr_type)
6179 {
6180 case EXPR_OP:
6181 t = resolve_operator (e);
6182 break;
6183
6184 case EXPR_FUNCTION:
4ee9c684 6185 case EXPR_VARIABLE:
659c6e2f 6186
6187 if (check_host_association (e))
6188 t = resolve_function (e);
6189 else
8879941c 6190 t = resolve_variable (e);
0ff77f4e 6191
eeebe20b 6192 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
71f1bb94 6193 && e->ref->type != REF_SUBSTRING)
0ff77f4e 6194 gfc_resolve_substring_charlen (e);
6195
4ee9c684 6196 break;
6197
930fe1de 6198 case EXPR_COMPCALL:
ae925cc0 6199 t = resolve_typebound_function (e);
930fe1de 6200 break;
6201
4ee9c684 6202 case EXPR_SUBSTRING:
6203 t = resolve_ref (e);
6204 break;
6205
6206 case EXPR_CONSTANT:
6207 case EXPR_NULL:
60e19868 6208 t = true;
4ee9c684 6209 break;
6210
64e93293 6211 case EXPR_PPC:
6212 t = resolve_expr_ppc (e);
6213 break;
6214
4ee9c684 6215 case EXPR_ARRAY:
60e19868 6216 t = false;
6217 if (!resolve_ref (e))
4ee9c684 6218 break;
6219
6220 t = gfc_resolve_array_constructor (e);
6221 /* Also try to expand a constructor. */
60e19868 6222 if (t)
4ee9c684 6223 {
6224 expression_rank (e);
c315461d 6225 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
58b069a0 6226 gfc_expand_constructor (e, false);
4ee9c684 6227 }
35d9c496 6228
1bcc6eb8 6229 /* This provides the opportunity for the length of constructors with
a0527218 6230 character valued function elements to propagate the string length
1bcc6eb8 6231 to the expression. */
60e19868 6232 if (t && e->ts.type == BT_CHARACTER)
c315461d 6233 {
6234 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
d6463863 6235 here rather then add a duplicate test for it above. */
58b069a0 6236 gfc_expand_constructor (e, false);
c315461d 6237 t = gfc_resolve_character_array_constructor (e);
6238 }
4ee9c684 6239
6240 break;
6241
6242 case EXPR_STRUCTURE:
6243 t = resolve_ref (e);
60e19868 6244 if (!t)
4ee9c684 6245 break;
6246
23d075f4 6247 t = resolve_structure_cons (e, 0);
60e19868 6248 if (!t)
4ee9c684 6249 break;
6250
6251 t = gfc_simplify_expr (e, 0);
6252 break;
6253
6254 default:
6255 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6256 }
6257
60e19868 6258 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
0ff77f4e 6259 fixup_charlen (e);
6260
e97ac7c0 6261 inquiry_argument = inquiry_save;
f00f6dd6 6262 actual_arg = actual_arg_save;
6263 first_actual_arg = first_actual_arg_save;
e97ac7c0 6264
4ee9c684 6265 return t;
6266}
6267
6268
290001b9 6269/* Resolve an expression from an iterator. They must be scalar and have
6270 INTEGER or (optionally) REAL type. */
4ee9c684 6271
60e19868 6272static bool
1bcc6eb8 6273gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6274 const char *name_msgid)
4ee9c684 6275{
60e19868 6276 if (!gfc_resolve_expr (expr))
6277 return false;
4ee9c684 6278
290001b9 6279 if (expr->rank != 0)
4ee9c684 6280 {
41481754 6281 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
60e19868 6282 return false;
4ee9c684 6283 }
6284
0e70e3df 6285 if (expr->ts.type != BT_INTEGER)
4ee9c684 6286 {
0e70e3df 6287 if (expr->ts.type == BT_REAL)
6288 {
6289 if (real_ok)
6290 return gfc_notify_std (GFC_STD_F95_DEL,
f25dbbf7 6291 "%s at %L must be integer",
0e70e3df 6292 _(name_msgid), &expr->where);
6293 else
6294 {
6295 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6296 &expr->where);
60e19868 6297 return false;
0e70e3df 6298 }
6299 }
41481754 6300 else
0e70e3df 6301 {
6302 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
60e19868 6303 return false;
0e70e3df 6304 }
4ee9c684 6305 }
60e19868 6306 return true;
290001b9 6307}
6308
6309
6310/* Resolve the expressions in an iterator structure. If REAL_OK is
091c5975 6311 false allow only INTEGER type iterators, otherwise allow REAL types.
6312 Set own_scope to true for ac-implied-do and data-implied-do as those
6313 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
290001b9 6314
60e19868 6315bool
091c5975 6316gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
290001b9 6317{
60e19868 6318 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6319 return false;
4ee9c684 6320
080819af 6321 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
60e19868 6322 _("iterator variable")))
6323 return false;
4ee9c684 6324
080819af 6325 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
60e19868 6326 "Start expression in DO loop"))
6327 return false;
4ee9c684 6328
080819af 6329 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
60e19868 6330 "End expression in DO loop"))
6331 return false;
4ee9c684 6332
080819af 6333 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
60e19868 6334 "Step expression in DO loop"))
6335 return false;
4ee9c684 6336
290001b9 6337 if (iter->step->expr_type == EXPR_CONSTANT)
4ee9c684 6338 {
290001b9 6339 if ((iter->step->ts.type == BT_INTEGER
6340 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6341 || (iter->step->ts.type == BT_REAL
6342 && mpfr_sgn (iter->step->value.real) == 0))
6343 {
6344 gfc_error ("Step expression in DO loop at %L cannot be zero",
6345 &iter->step->where);
60e19868 6346 return false;
290001b9 6347 }
4ee9c684 6348 }
6349
290001b9 6350 /* Convert start, end, and step to the same type as var. */
6351 if (iter->start->ts.kind != iter->var->ts.kind
6352 || iter->start->ts.type != iter->var->ts.type)
6353 gfc_convert_type (iter->start, &iter->var->ts, 2);
6354
6355 if (iter->end->ts.kind != iter->var->ts.kind
6356 || iter->end->ts.type != iter->var->ts.type)
6357 gfc_convert_type (iter->end, &iter->var->ts, 2);
6358
6359 if (iter->step->ts.kind != iter->var->ts.kind
6360 || iter->step->ts.type != iter->var->ts.type)
6361 gfc_convert_type (iter->step, &iter->var->ts, 2);
4ee9c684 6362
f4084ad8 6363 if (iter->start->expr_type == EXPR_CONSTANT
6364 && iter->end->expr_type == EXPR_CONSTANT
6365 && iter->step->expr_type == EXPR_CONSTANT)
6366 {
6367 int sgn, cmp;
6368 if (iter->start->ts.type == BT_INTEGER)
6369 {
6370 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6371 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6372 }
6373 else
6374 {
6375 sgn = mpfr_sgn (iter->step->value.real);
6376 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6377 }
8290d53f 6378 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
4166acc7 6379 gfc_warning (OPT_Wzerotrip,
6380 "DO loop at %L will be executed zero times",
f4084ad8 6381 &iter->step->where);
6382 }
6383
60e19868 6384 return true;
4ee9c684 6385}
6386
6387
791d4123 6388/* Traversal function for find_forall_index. f == 2 signals that
6389 that variable itself is not to be checked - only the references. */
8a49b30e 6390
791d4123 6391static bool
6392forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8a49b30e 6393{
1acb400a 6394 if (expr->expr_type != EXPR_VARIABLE)
6395 return false;
d6463863 6396
791d4123 6397 /* A scalar assignment */
6398 if (!expr->ref || *f == 1)
8a49b30e 6399 {
791d4123 6400 if (expr->symtree->n.sym == sym)
6401 return true;
6402 else
6403 return false;
6404 }
8a49b30e 6405
791d4123 6406 if (*f == 2)
6407 *f = 1;
6408 return false;
6409}
8a49b30e 6410
8a49b30e 6411
791d4123 6412/* Check whether the FORALL index appears in the expression or not.
60e19868 6413 Returns true if SYM is found in EXPR. */
8a49b30e 6414
60e19868 6415bool
791d4123 6416find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6417{
6418 if (gfc_traverse_expr (expr, sym, forall_index, f))
60e19868 6419 return true;
791d4123 6420 else
60e19868 6421 return false;
8a49b30e 6422}
6423
6424
f70e9da7 6425/* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6426 to be a scalar INTEGER variable. The subscripts and stride are scalar
8a49b30e 6427 INTEGERs, and if stride is a constant it must be nonzero.
6428 Furthermore "A subscript or stride in a forall-triplet-spec shall
6429 not contain a reference to any index-name in the
6430 forall-triplet-spec-list in which it appears." (7.5.4.1) */
4ee9c684 6431
6432static void
8a49b30e 6433resolve_forall_iterators (gfc_forall_iterator *it)
4ee9c684 6434{
8a49b30e 6435 gfc_forall_iterator *iter, *iter2;
6436
6437 for (iter = it; iter; iter = iter->next)
4ee9c684 6438 {
60e19868 6439 if (gfc_resolve_expr (iter->var)
f70e9da7 6440 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6441 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
4ee9c684 6442 &iter->var->where);
6443
60e19868 6444 if (gfc_resolve_expr (iter->start)
f70e9da7 6445 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6446 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
4ee9c684 6447 &iter->start->where);
6448 if (iter->var->ts.kind != iter->start->ts.kind)
046ab75f 6449 gfc_convert_type (iter->start, &iter->var->ts, 1);
4ee9c684 6450
60e19868 6451 if (gfc_resolve_expr (iter->end)
f70e9da7 6452 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6453 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
4ee9c684 6454 &iter->end->where);
6455 if (iter->var->ts.kind != iter->end->ts.kind)
046ab75f 6456 gfc_convert_type (iter->end, &iter->var->ts, 1);
4ee9c684 6457
60e19868 6458 if (gfc_resolve_expr (iter->stride))
f70e9da7 6459 {
6460 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6461 gfc_error ("FORALL stride expression at %L must be a scalar %s",
1bcc6eb8 6462 &iter->stride->where, "INTEGER");
f70e9da7 6463
6464 if (iter->stride->expr_type == EXPR_CONSTANT
60e19868 6465 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
f70e9da7 6466 gfc_error ("FORALL stride expression at %L cannot be zero",
6467 &iter->stride->where);
6468 }
4ee9c684 6469 if (iter->var->ts.kind != iter->stride->ts.kind)
046ab75f 6470 gfc_convert_type (iter->stride, &iter->var->ts, 1);
4ee9c684 6471 }
8a49b30e 6472
6473 for (iter = it; iter; iter = iter->next)
6474 for (iter2 = iter; iter2; iter2 = iter2->next)
6475 {
60e19868 6476 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6477 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6478 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
0d2b3c9c 6479 gfc_error ("FORALL index %qs may not appear in triplet "
8a49b30e 6480 "specification at %L", iter->var->symtree->name,
6481 &iter2->start->where);
6482 }
4ee9c684 6483}
6484
6485
ba9448b4 6486/* Given a pointer to a symbol that is a derived type, see if it's
6487 inaccessible, i.e. if it's defined in another module and the components are
6488 PRIVATE. The search is recursive if necessary. Returns zero if no
6489 inaccessible components are found, nonzero otherwise. */
6490
6491static int
6492derived_inaccessible (gfc_symbol *sym)
6493{
6494 gfc_component *c;
6495
28b240bf 6496 if (sym->attr.use_assoc && sym->attr.private_comp)
ba9448b4 6497 return 1;
6498
6499 for (c = sym->components; c; c = c->next)
6500 {
eeebe20b 6501 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
1bcc6eb8 6502 return 1;
ba9448b4 6503 }
6504
6505 return 0;
6506}
6507
6508
4ee9c684 6509/* Resolve the argument of a deallocate expression. The expression must be
6510 a pointer or a full array. */
6511
60e19868 6512static bool
1bcc6eb8 6513resolve_deallocate_expr (gfc_expr *e)
4ee9c684 6514{
6515 symbol_attribute attr;
7725f40e 6516 int allocatable, pointer;
4ee9c684 6517 gfc_ref *ref;
1de1b1a9 6518 gfc_symbol *sym;
6519 gfc_component *c;
a90fe829 6520 bool unlimited;
4ee9c684 6521
60e19868 6522 if (!gfc_resolve_expr (e))
6523 return false;
4ee9c684 6524
4ee9c684 6525 if (e->expr_type != EXPR_VARIABLE)
6526 goto bad;
6527
1de1b1a9 6528 sym = e->symtree->n.sym;
a90fe829 6529 unlimited = UNLIMITED_POLY(sym);
1de1b1a9 6530
6531 if (sym->ts.type == BT_CLASS)
6532 {
50b4b37b 6533 allocatable = CLASS_DATA (sym)->attr.allocatable;
a33fbb6f 6534 pointer = CLASS_DATA (sym)->attr.class_pointer;
1de1b1a9 6535 }
6536 else
6537 {
6538 allocatable = sym->attr.allocatable;
6539 pointer = sym->attr.pointer;
6540 }
4ee9c684 6541 for (ref = e->ref; ref; ref = ref->next)
2bec85dc 6542 {
2bec85dc 6543 switch (ref->type)
1bcc6eb8 6544 {
6545 case REF_ARRAY:
0d3bb1de 6546 if (ref->u.ar.type != AR_FULL
6547 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6548 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
2bec85dc 6549 allocatable = 0;
6550 break;
4ee9c684 6551
1bcc6eb8 6552 case REF_COMPONENT:
1de1b1a9 6553 c = ref->u.c.component;
6554 if (c->ts.type == BT_CLASS)
6555 {
50b4b37b 6556 allocatable = CLASS_DATA (c)->attr.allocatable;
a33fbb6f 6557 pointer = CLASS_DATA (c)->attr.class_pointer;
1de1b1a9 6558 }
6559 else
6560 {
6561 allocatable = c->attr.allocatable;
6562 pointer = c->attr.pointer;
6563 }
2bec85dc 6564 break;
4ee9c684 6565
1bcc6eb8 6566 case REF_SUBSTRING:
2bec85dc 6567 allocatable = 0;
6568 break;
1bcc6eb8 6569 }
2bec85dc 6570 }
6571
6572 attr = gfc_expr_attr (e);
6573
a90fe829 6574 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
4ee9c684 6575 {
6576 bad:
e9d30d9c 6577 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6578 &e->where);
60e19868 6579 return false;
4ee9c684 6580 }
6581
8e5ab246 6582 /* F2008, C644. */
6583 if (gfc_is_coindexed (e))
6584 {
6585 gfc_error ("Coindexed allocatable object at %L", &e->where);
60e19868 6586 return false;
8e5ab246 6587 }
6588
7725f40e 6589 if (pointer
080819af 6590 && !gfc_check_vardef_context (e, true, true, false,
60e19868 6591 _("DEALLOCATE object")))
6592 return false;
080819af 6593 if (!gfc_check_vardef_context (e, false, true, false,
60e19868 6594 _("DEALLOCATE object")))
6595 return false;
7d19e94d 6596
60e19868 6597 return true;
4ee9c684 6598}
6599
1bcc6eb8 6600
1acb400a 6601/* Returns true if the expression e contains a reference to the symbol sym. */
f277dc2c 6602static bool
1acb400a 6603sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
f277dc2c 6604{
1acb400a 6605 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6606 return true;
f277dc2c 6607
1acb400a 6608 return false;
6609}
f277dc2c 6610
fd6481cf 6611bool
6612gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
1acb400a 6613{
6614 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
f277dc2c 6615}
6616
4ee9c684 6617
427ecbfa 6618/* Given the expression node e for an allocatable/pointer of derived type to be
6619 allocated, get the expression node to be initialized afterwards (needed for
2294b616 6620 derived types with default initializers, and derived types with allocatable
6621 components that need nullification.) */
427ecbfa 6622
1de1b1a9 6623gfc_expr *
6624gfc_expr_to_initialize (gfc_expr *e)
427ecbfa 6625{
6626 gfc_expr *result;
6627 gfc_ref *ref;
6628 int i;
6629
6630 result = gfc_copy_expr (e);
6631
6632 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6633 for (ref = result->ref; ref; ref = ref->next)
6634 if (ref->type == REF_ARRAY && ref->next == NULL)
6635 {
1bcc6eb8 6636 ref->u.ar.type = AR_FULL;
427ecbfa 6637
1bcc6eb8 6638 for (i = 0; i < ref->u.ar.dimen; i++)
6639 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
427ecbfa 6640
1bcc6eb8 6641 break;
427ecbfa 6642 }
6643
a9031a3e 6644 gfc_free_shape (&result->shape, result->rank);
6645
6646 /* Recalculate rank, shape, etc. */
6647 gfc_resolve_expr (result);
427ecbfa 6648 return result;
6649}
6650
6651
7725f40e 6652/* If the last ref of an expression is an array ref, return a copy of the
6653 expression with that one removed. Otherwise, a copy of the original
6654 expression. This is used for allocate-expressions and pointer assignment
6655 LHS, where there may be an array specification that needs to be stripped
6656 off when using gfc_check_vardef_context. */
6657
6658static gfc_expr*
6659remove_last_array_ref (gfc_expr* e)
6660{
6661 gfc_expr* e2;
6662 gfc_ref** r;
6663
6664 e2 = gfc_copy_expr (e);
6665 for (r = &e2->ref; *r; r = &(*r)->next)
6666 if ((*r)->type == REF_ARRAY && !(*r)->next)
6667 {
6668 gfc_free_ref_list (*r);
6669 *r = NULL;
6670 break;
6671 }
6672
6673 return e2;
6674}
6675
6676
af675571 6677/* Used in resolve_allocate_expr to check that a allocation-object and
d6463863 6678 a source-expr are conformable. This does not catch all possible
af675571 6679 cases; in particular a runtime checking is needed. */
6680
60e19868 6681static bool
af675571 6682conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6683{
ad0fe61d 6684 gfc_ref *tail;
6685 for (tail = e2->ref; tail && tail->next; tail = tail->next);
d6463863 6686
af675571 6687 /* First compare rank. */
9a454437 6688 if ((tail && e1->rank != tail->u.ar.as->rank)
6689 || (!tail && e1->rank != e2->rank))
af675571 6690 {
6691 gfc_error ("Source-expr at %L must be scalar or have the "
6692 "same rank as the allocate-object at %L",
6693 &e1->where, &e2->where);
60e19868 6694 return false;
af675571 6695 }
6696
6697 if (e1->shape)
6698 {
6699 int i;
6700 mpz_t s;
6701
6702 mpz_init (s);
6703
6704 for (i = 0; i < e1->rank; i++)
6705 {
7431b56c 6706 if (tail->u.ar.start[i] == NULL)
6707 break;
6708
ad0fe61d 6709 if (tail->u.ar.end[i])
af675571 6710 {
ad0fe61d 6711 mpz_set (s, tail->u.ar.end[i]->value.integer);
6712 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
af675571 6713 mpz_add_ui (s, s, 1);
6714 }
6715 else
6716 {
ad0fe61d 6717 mpz_set (s, tail->u.ar.start[i]->value.integer);
af675571 6718 }
6719
6720 if (mpz_cmp (e1->shape[i], s) != 0)
6721 {
716da296 6722 gfc_error_1 ("Source-expr at %L and allocate-object at %L must "
af675571 6723 "have the same shape", &e1->where, &e2->where);
6724 mpz_clear (s);
60e19868 6725 return false;
af675571 6726 }
6727 }
6728
6729 mpz_clear (s);
6730 }
6731
60e19868 6732 return true;
af675571 6733}
6734
6735
4ee9c684 6736/* Resolve the expression in an ALLOCATE statement, doing the additional
6737 checks to see whether the expression is OK or not. The expression must
6738 have a trailing array reference that gives the size of the array. */
6739
60e19868 6740static bool
1bcc6eb8 6741resolve_allocate_expr (gfc_expr *e, gfc_code *code)
4ee9c684 6742{
7725f40e 6743 int i, pointer, allocatable, dimension, is_abstract;
e97ac7c0 6744 int codimension;
6861b092 6745 bool coindexed;
a90fe829 6746 bool unlimited;
4ee9c684 6747 symbol_attribute attr;
6748 gfc_ref *ref, *ref2;
7725f40e 6749 gfc_expr *e2;
4ee9c684 6750 gfc_array_ref *ar;
2e67d2c9 6751 gfc_symbol *sym = NULL;
f277dc2c 6752 gfc_alloc *a;
1de1b1a9 6753 gfc_component *c;
60e19868 6754 bool t;
2bec85dc 6755
df084314 6756 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
e97ac7c0 6757 checking of coarrays. */
6758 for (ref = e->ref; ref; ref = ref->next)
6759 if (ref->next == NULL)
6760 break;
6761
6762 if (ref && ref->type == REF_ARRAY)
6763 ref->u.ar.in_allocate = true;
6764
60e19868 6765 if (!gfc_resolve_expr (e))
e97ac7c0 6766 goto failure;
4ee9c684 6767
6768 /* Make sure the expression is allocatable or a pointer. If it is
6769 pointer, the next-to-last reference must be a pointer. */
6770
6771 ref2 = NULL;
1de1b1a9 6772 if (e->symtree)
6773 sym = e->symtree->n.sym;
4ee9c684 6774
bb1fa9b5 6775 /* Check whether ultimate component is abstract and CLASS. */
6776 is_abstract = 0;
6777
a90fe829 6778 /* Is the allocate-object unlimited polymorphic? */
6779 unlimited = UNLIMITED_POLY(e);
6780
4ee9c684 6781 if (e->expr_type != EXPR_VARIABLE)
6782 {
6783 allocatable = 0;
4ee9c684 6784 attr = gfc_expr_attr (e);
6785 pointer = attr.pointer;
6786 dimension = attr.dimension;
e97ac7c0 6787 codimension = attr.codimension;
4ee9c684 6788 }
6789 else
6790 {
fd23cc08 6791 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
1de1b1a9 6792 {
50b4b37b 6793 allocatable = CLASS_DATA (sym)->attr.allocatable;
a33fbb6f 6794 pointer = CLASS_DATA (sym)->attr.class_pointer;
50b4b37b 6795 dimension = CLASS_DATA (sym)->attr.dimension;
6796 codimension = CLASS_DATA (sym)->attr.codimension;
6797 is_abstract = CLASS_DATA (sym)->attr.abstract;
1de1b1a9 6798 }
6799 else
6800 {
6801 allocatable = sym->attr.allocatable;
6802 pointer = sym->attr.pointer;
6803 dimension = sym->attr.dimension;
e97ac7c0 6804 codimension = sym->attr.codimension;
1de1b1a9 6805 }
4ee9c684 6806
6861b092 6807 coindexed = false;
6808
4ee9c684 6809 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
1bcc6eb8 6810 {
2bec85dc 6811 switch (ref->type)
6812 {
6813 case REF_ARRAY:
6861b092 6814 if (ref->u.ar.codimen > 0)
6815 {
6816 int n;
6817 for (n = ref->u.ar.dimen;
6818 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6819 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6820 {
6821 coindexed = true;
6822 break;
6823 }
6824 }
6825
1bcc6eb8 6826 if (ref->next != NULL)
6827 pointer = 0;
6828 break;
2bec85dc 6829
6830 case REF_COMPONENT:
e97ac7c0 6831 /* F2008, C644. */
6861b092 6832 if (coindexed)
e97ac7c0 6833 {
6834 gfc_error ("Coindexed allocatable object at %L",
6835 &e->where);
6836 goto failure;
6837 }
6838
1de1b1a9 6839 c = ref->u.c.component;
6840 if (c->ts.type == BT_CLASS)
6841 {
50b4b37b 6842 allocatable = CLASS_DATA (c)->attr.allocatable;
a33fbb6f 6843 pointer = CLASS_DATA (c)->attr.class_pointer;
50b4b37b 6844 dimension = CLASS_DATA (c)->attr.dimension;
6845 codimension = CLASS_DATA (c)->attr.codimension;
6846 is_abstract = CLASS_DATA (c)->attr.abstract;
1de1b1a9 6847 }
6848 else
6849 {
6850 allocatable = c->attr.allocatable;
6851 pointer = c->attr.pointer;
6852 dimension = c->attr.dimension;
e97ac7c0 6853 codimension = c->attr.codimension;
bb1fa9b5 6854 is_abstract = c->attr.abstract;
1de1b1a9 6855 }
1bcc6eb8 6856 break;
2bec85dc 6857
6858 case REF_SUBSTRING:
1bcc6eb8 6859 allocatable = 0;
6860 pointer = 0;
6861 break;
2bec85dc 6862 }
930fe1de 6863 }
4ee9c684 6864 }
6865
c73f762d 6866 /* Check for F08:C628. */
a90fe829 6867 if (allocatable == 0 && pointer == 0 && !unlimited)
4ee9c684 6868 {
e9d30d9c 6869 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6870 &e->where);
e97ac7c0 6871 goto failure;
4ee9c684 6872 }
6873
af675571 6874 /* Some checks for the SOURCE tag. */
6875 if (code->expr3)
6876 {
6877 /* Check F03:C631. */
6878 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6879 {
716da296 6880 gfc_error_1 ("Type of entity at %L is type incompatible with "
6881 "source-expr at %L", &e->where, &code->expr3->where);
e97ac7c0 6882 goto failure;
af675571 6883 }
6884
6885 /* Check F03:C632 and restriction following Note 6.18. */
9a454437 6886 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
e97ac7c0 6887 goto failure;
af675571 6888
6889 /* Check F03:C633. */
a90fe829 6890 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
af675571 6891 {
716da296 6892 gfc_error_1 ("The allocate-object at %L and the source-expr at %L "
af675571 6893 "shall have the same kind type parameter",
6894 &e->where, &code->expr3->where);
e97ac7c0 6895 goto failure;
af675571 6896 }
c135f087 6897
6898 /* Check F2008, C642. */
6899 if (code->expr3->ts.type == BT_DERIVED
50fefeb7 6900 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
c135f087 6901 || (code->expr3->ts.u.derived->from_intmod
6902 == INTMOD_ISO_FORTRAN_ENV
6903 && code->expr3->ts.u.derived->intmod_sym_id
6904 == ISOFORTRAN_LOCK_TYPE)))
6905 {
716da296 6906 gfc_error_1 ("The source-expr at %L shall neither be of type "
c135f087 6907 "LOCK_TYPE nor have a LOCK_TYPE component if "
6908 "allocate-object at %L is a coarray",
6909 &code->expr3->where, &e->where);
6910 goto failure;
6911 }
af675571 6912 }
de622904 6913
6914 /* Check F08:C629. */
6915 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6916 && !code->expr3)
bb1fa9b5 6917 {
6918 gcc_assert (e->ts.type == BT_CLASS);
6919 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
de622904 6920 "type-spec or source-expr", sym->name, &e->where);
e97ac7c0 6921 goto failure;
bb1fa9b5 6922 }
6923
ee7e7076 6924 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6925 {
6926 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6927 code->ext.alloc.ts.u.cl->length);
6928 if (cmp == 1 || cmp == -1 || cmp == -3)
6929 {
6930 gfc_error ("Allocating %s at %L with type-spec requires the same "
6931 "character-length parameter as in the declaration",
6932 sym->name, &e->where);
6933 goto failure;
6934 }
6935 }
6936
7725f40e 6937 /* In the variable definition context checks, gfc_expr_attr is used
6938 on the expression. This is fooled by the array specification
6939 present in e, thus we have to eliminate that one temporarily. */
6940 e2 = remove_last_array_ref (e);
60e19868 6941 t = true;
6942 if (t && pointer)
080819af 6943 t = gfc_check_vardef_context (e2, true, true, false,
60e19868 6944 _("ALLOCATE object"));
6945 if (t)
080819af 6946 t = gfc_check_vardef_context (e2, false, true, false,
60e19868 6947 _("ALLOCATE object"));
7725f40e 6948 gfc_free_expr (e2);
60e19868 6949 if (!t)
7725f40e 6950 goto failure;
7d19e94d 6951
fd23cc08 6952 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6953 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6954 {
6955 /* For class arrays, the initialization with SOURCE is done
6956 using _copy and trans_call. It is convenient to exploit that
6957 when the allocated type is different from the declared type but
6958 no SOURCE exists by setting expr3. */
d6463863 6959 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
fd23cc08 6960 }
6961 else if (!code->expr3)
e5f2c160 6962 {
6963 /* Set up default initializer if needed. */
6964 gfc_typespec ts;
be318c4b 6965 gfc_expr *init_e;
e5f2c160 6966
6967 if (code->ext.alloc.ts.type == BT_DERIVED)
6968 ts = code->ext.alloc.ts;
6969 else
6970 ts = e->ts;
6971
6972 if (ts.type == BT_CLASS)
6973 ts = ts.u.derived->components->ts;
6974
be318c4b 6975 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
e5f2c160 6976 {
f1ab83c6 6977 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
7bca694e 6978 init_st->loc = code->loc;
7bca694e 6979 init_st->expr1 = gfc_expr_to_initialize (e);
6980 init_st->expr2 = init_e;
6981 init_st->next = code->next;
6982 code->next = init_st;
e5f2c160 6983 }
6984 }
6985 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6986 {
6987 /* Default initialization via MOLD (non-polymorphic). */
6988 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6989 gfc_resolve_expr (rhs);
6990 gfc_free_expr (code->expr3);
6991 code->expr3 = rhs;
6992 }
6993
a90fe829 6994 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
224db79a 6995 {
6996 /* Make sure the vtab symbol is present when
6997 the module variables are generated. */
6998 gfc_typespec ts = e->ts;
6999 if (code->expr3)
7000 ts = code->expr3->ts;
7001 else if (code->ext.alloc.ts.type == BT_DERIVED)
7002 ts = code->ext.alloc.ts;
a90fe829 7003
224db79a 7004 gfc_find_derived_vtab (ts.u.derived);
a90fe829 7005
7006 if (dimension)
7007 e = gfc_expr_to_initialize (e);
7008 }
7009 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7010 {
7011 /* Again, make sure the vtab symbol is present when
7012 the module variables are generated. */
7013 gfc_typespec *ts = NULL;
7014 if (code->expr3)
7015 ts = &code->expr3->ts;
7016 else
7017 ts = &code->ext.alloc.ts;
7018
7019 gcc_assert (ts);
7020
25014fa7 7021 gfc_find_vtab (ts);
a90fe829 7022
fd23cc08 7023 if (dimension)
7024 e = gfc_expr_to_initialize (e);
224db79a 7025 }
7026
76608b4a 7027 if (dimension == 0 && codimension == 0)
e97ac7c0 7028 goto success;
4ee9c684 7029
df084314 7030 /* Make sure the last reference node is an array specification. */
4ee9c684 7031
7725f40e 7032 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
e97ac7c0 7033 || (dimension && ref2->u.ar.dimen == 0))
4ee9c684 7034 {
7035 gfc_error ("Array specification required in ALLOCATE statement "
7036 "at %L", &e->where);
e97ac7c0 7037 goto failure;
4ee9c684 7038 }
7039
4ee9c684 7040 /* Make sure that the array section reference makes sense in the
7041 context of an ALLOCATE specification. */
7042
7043 ar = &ref2->u.ar;
7044
076094b7 7045 if (codimension)
7046 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7047 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7048 {
7049 gfc_error ("Coarray specification required in ALLOCATE statement "
7050 "at %L", &e->where);
7051 goto failure;
7052 }
e97ac7c0 7053
4ee9c684 7054 for (i = 0; i < ar->dimen; i++)
f277dc2c 7055 {
7056 if (ref2->u.ar.type == AR_ELEMENT)
7057 goto check_symbols;
4ee9c684 7058
f277dc2c 7059 switch (ar->dimen_type[i])
7060 {
7061 case DIMEN_ELEMENT:
4ee9c684 7062 break;
7063
f277dc2c 7064 case DIMEN_RANGE:
7065 if (ar->start[i] != NULL
7066 && ar->end[i] != NULL
7067 && ar->stride[i] == NULL)
7068 break;
4ee9c684 7069
f277dc2c 7070 /* Fall Through... */
7071
7072 case DIMEN_UNKNOWN:
7073 case DIMEN_VECTOR:
e97ac7c0 7074 case DIMEN_STAR:
076094b7 7075 case DIMEN_THIS_IMAGE:
f277dc2c 7076 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7077 &e->where);
e97ac7c0 7078 goto failure;
f277dc2c 7079 }
7080
7081check_symbols:
1de1b1a9 7082 for (a = code->ext.alloc.list; a; a = a->next)
f277dc2c 7083 {
7084 sym = a->expr->symtree->n.sym;
bd7c4cff 7085
7086 /* TODO - check derived type components. */
449db53c 7087 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
bd7c4cff 7088 continue;
7089
fd6481cf 7090 if ((ar->start[i] != NULL
7091 && gfc_find_sym_in_expr (sym, ar->start[i]))
7092 || (ar->end[i] != NULL
7093 && gfc_find_sym_in_expr (sym, ar->end[i])))
f277dc2c 7094 {
0d2b3c9c 7095 gfc_error ("%qs must not appear in the array specification at "
f277dc2c 7096 "%L in the same ALLOCATE statement where it is "
7097 "itself allocated", sym->name, &ar->where);
e97ac7c0 7098 goto failure;
f277dc2c 7099 }
7100 }
7101 }
4ee9c684 7102
e97ac7c0 7103 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7104 {
7105 if (ar->dimen_type[i] == DIMEN_ELEMENT
7106 || ar->dimen_type[i] == DIMEN_RANGE)
7107 {
7108 if (i == (ar->dimen + ar->codimen - 1))
7109 {
7110 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7111 "statement at %L", &e->where);
7112 goto failure;
7113 }
b3034d83 7114 continue;
e97ac7c0 7115 }
7116
7117 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7118 && ar->stride[i] == NULL)
7119 break;
7120
7121 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7122 &e->where);
7123 goto failure;
7124 }
7125
e97ac7c0 7126success:
60e19868 7127 return true;
e97ac7c0 7128
7129failure:
60e19868 7130 return false;
4ee9c684 7131}
7132
a9e7fd6a 7133static void
7134resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7135{
e9d30d9c 7136 gfc_expr *stat, *errmsg, *pe, *qe;
7137 gfc_alloc *a, *p, *q;
7138
7725f40e 7139 stat = code->expr1;
7140 errmsg = code->expr2;
a9e7fd6a 7141
e9d30d9c 7142 /* Check the stat variable. */
7143 if (stat)
a9e7fd6a 7144 {
080819af 7145 gfc_check_vardef_context (stat, false, false, false,
60e19868 7146 _("STAT variable"));
a9e7fd6a 7147
b6bbfb84 7148 if ((stat->ts.type != BT_INTEGER
7149 && !(stat->ref && (stat->ref->type == REF_ARRAY
7150 || stat->ref->type == REF_COMPONENT)))
7151 || stat->rank > 0)
e9d30d9c 7152 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7153 "variable", &stat->where);
7154
1de1b1a9 7155 for (p = code->ext.alloc.list; p; p = p->next)
e9d30d9c 7156 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
7e6fae0b 7157 {
7158 gfc_ref *ref1, *ref2;
7159 bool found = true;
7160
7161 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7162 ref1 = ref1->next, ref2 = ref2->next)
7163 {
7164 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7165 continue;
7166 if (ref1->u.c.component->name != ref2->u.c.component->name)
7167 {
7168 found = false;
7169 break;
7170 }
7171 }
7172
7173 if (found)
7174 {
7175 gfc_error ("Stat-variable at %L shall not be %sd within "
7176 "the same %s statement", &stat->where, fcn, fcn);
7177 break;
7178 }
7179 }
a9e7fd6a 7180 }
7181
e9d30d9c 7182 /* Check the errmsg variable. */
7183 if (errmsg)
7184 {
7185 if (!stat)
7186 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7187 &errmsg->where);
7188
091c5975 7189 gfc_check_vardef_context (errmsg, false, false, false,
7190 _("ERRMSG variable"));
e9d30d9c 7191
b6bbfb84 7192 if ((errmsg->ts.type != BT_CHARACTER
7193 && !(errmsg->ref
7194 && (errmsg->ref->type == REF_ARRAY
7195 || errmsg->ref->type == REF_COMPONENT)))
7196 || errmsg->rank > 0 )
e9d30d9c 7197 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7198 "variable", &errmsg->where);
7199
1de1b1a9 7200 for (p = code->ext.alloc.list; p; p = p->next)
e9d30d9c 7201 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
7e6fae0b 7202 {
7203 gfc_ref *ref1, *ref2;
7204 bool found = true;
7205
7206 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7207 ref1 = ref1->next, ref2 = ref2->next)
7208 {
7209 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7210 continue;
7211 if (ref1->u.c.component->name != ref2->u.c.component->name)
7212 {
7213 found = false;
7214 break;
7215 }
7216 }
7217
7218 if (found)
7219 {
7220 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7221 "the same %s statement", &errmsg->where, fcn, fcn);
7222 break;
7223 }
7224 }
e9d30d9c 7225 }
7226
cbe0bc01 7227 /* Check that an allocate-object appears only once in the statement. */
7228
1de1b1a9 7229 for (p = code->ext.alloc.list; p; p = p->next)
e9d30d9c 7230 {
7231 pe = p->expr;
66d1dccf 7232 for (q = p->next; q; q = q->next)
e9d30d9c 7233 {
66d1dccf 7234 qe = q->expr;
7235 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
e9d30d9c 7236 {
66d1dccf 7237 /* This is a potential collision. */
7238 gfc_ref *pr = pe->ref;
7239 gfc_ref *qr = qe->ref;
d6463863 7240
66d1dccf 7241 /* Follow the references until
7242 a) They start to differ, in which case there is no error;
7243 you can deallocate a%b and a%c in a single statement
7244 b) Both of them stop, which is an error
7245 c) One of them stops, which is also an error. */
7246 while (1)
7247 {
7248 if (pr == NULL && qr == NULL)
7249 {
716da296 7250 gfc_error_1 ("Allocate-object at %L also appears at %L",
7251 &pe->where, &qe->where);
66d1dccf 7252 break;
7253 }
7254 else if (pr != NULL && qr == NULL)
7255 {
716da296 7256 gfc_error_1 ("Allocate-object at %L is subobject of"
7257 " object at %L", &pe->where, &qe->where);
66d1dccf 7258 break;
7259 }
7260 else if (pr == NULL && qr != NULL)
7261 {
716da296 7262 gfc_error_1 ("Allocate-object at %L is subobject of"
7263 " object at %L", &qe->where, &pe->where);
66d1dccf 7264 break;
7265 }
7266 /* Here, pr != NULL && qr != NULL */
7267 gcc_assert(pr->type == qr->type);
7268 if (pr->type == REF_ARRAY)
7269 {
7270 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7271 which are legal. */
7272 gcc_assert (qr->type == REF_ARRAY);
7273
7274 if (pr->next && qr->next)
7275 {
ed90efe5 7276 int i;
66d1dccf 7277 gfc_array_ref *par = &(pr->u.ar);
7278 gfc_array_ref *qar = &(qr->u.ar);
ed90efe5 7279
7280 for (i=0; i<par->dimen; i++)
7281 {
7282 if ((par->start[i] != NULL
7283 || qar->start[i] != NULL)
7284 && gfc_dep_compare_expr (par->start[i],
7285 qar->start[i]) != 0)
7286 goto break_label;
7287 }
66d1dccf 7288 }
7289 }
7290 else
7291 {
7292 if (pr->u.c.component->name != qr->u.c.component->name)
7293 break;
7294 }
d6463863 7295
66d1dccf 7296 pr = pr->next;
7297 qr = qr->next;
7298 }
ed90efe5 7299 break_label:
7300 ;
e9d30d9c 7301 }
7302 }
7303 }
a9e7fd6a 7304
7305 if (strcmp (fcn, "ALLOCATE") == 0)
7306 {
1de1b1a9 7307 for (a = code->ext.alloc.list; a; a = a->next)
a9e7fd6a 7308 resolve_allocate_expr (a->expr, code);
7309 }
7310 else
7311 {
1de1b1a9 7312 for (a = code->ext.alloc.list; a; a = a->next)
a9e7fd6a 7313 resolve_deallocate_expr (a->expr);
7314 }
7315}
4ee9c684 7316
e9d30d9c 7317
4ee9c684 7318/************ SELECT CASE resolution subroutines ************/
7319
7320/* Callback function for our mergesort variant. Determines interval
7321 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
d6463863 7322 op1 > op2. Assumes we're not dealing with the default case.
ea87c3d2 7323 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7324 There are nine situations to check. */
4ee9c684 7325
7326static int
1bcc6eb8 7327compare_cases (const gfc_case *op1, const gfc_case *op2)
4ee9c684 7328{
ea87c3d2 7329 int retval;
4ee9c684 7330
ea87c3d2 7331 if (op1->low == NULL) /* op1 = (:L) */
4ee9c684 7332 {
ea87c3d2 7333 /* op2 = (:N), so overlap. */
7334 retval = 0;
7335 /* op2 = (M:) or (M:N), L < M */
7336 if (op2->low != NULL
134eab89 7337 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
ea87c3d2 7338 retval = -1;
4ee9c684 7339 }
ea87c3d2 7340 else if (op1->high == NULL) /* op1 = (K:) */
4ee9c684 7341 {
ea87c3d2 7342 /* op2 = (M:), so overlap. */
7343 retval = 0;
7344 /* op2 = (:N) or (M:N), K > N */
7345 if (op2->high != NULL
134eab89 7346 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
ea87c3d2 7347 retval = 1;
4ee9c684 7348 }
ea87c3d2 7349 else /* op1 = (K:L) */
4ee9c684 7350 {
ea87c3d2 7351 if (op2->low == NULL) /* op2 = (:N), K > N */
134eab89 7352 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7353 ? 1 : 0;
ea87c3d2 7354 else if (op2->high == NULL) /* op2 = (M:), L < M */
134eab89 7355 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7356 ? -1 : 0;
1bcc6eb8 7357 else /* op2 = (M:N) */
7358 {
ea87c3d2 7359 retval = 0;
1bcc6eb8 7360 /* L < M */
134eab89 7361 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
ea87c3d2 7362 retval = -1;
1bcc6eb8 7363 /* K > N */
134eab89 7364 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
ea87c3d2 7365 retval = 1;
4ee9c684 7366 }
7367 }
ea87c3d2 7368
7369 return retval;
4ee9c684 7370}
7371
7372
7373/* Merge-sort a double linked case list, detecting overlap in the
7374 process. LIST is the head of the double linked case list before it
7375 is sorted. Returns the head of the sorted list if we don't see any
7376 overlap, or NULL otherwise. */
7377
7378static gfc_case *
1bcc6eb8 7379check_case_overlap (gfc_case *list)
4ee9c684 7380{
7381 gfc_case *p, *q, *e, *tail;
7382 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7383
7384 /* If the passed list was empty, return immediately. */
7385 if (!list)
7386 return NULL;
7387
7388 overlap_seen = 0;
7389 insize = 1;
7390
7391 /* Loop unconditionally. The only exit from this loop is a return
7392 statement, when we've finished sorting the case list. */
7393 for (;;)
7394 {
7395 p = list;
7396 list = NULL;
7397 tail = NULL;
7398
7399 /* Count the number of merges we do in this pass. */
7400 nmerges = 0;
7401
7402 /* Loop while there exists a merge to be done. */
7403 while (p)
7404 {
7405 int i;
7406
7407 /* Count this merge. */
7408 nmerges++;
7409
c2849191 7410 /* Cut the list in two pieces by stepping INSIZE places
1bcc6eb8 7411 forward in the list, starting from P. */
4ee9c684 7412 psize = 0;
7413 q = p;
7414 for (i = 0; i < insize; i++)
7415 {
7416 psize++;
7417 q = q->right;
7418 if (!q)
7419 break;
7420 }
7421 qsize = insize;
7422
7423 /* Now we have two lists. Merge them! */
7424 while (psize > 0 || (qsize > 0 && q != NULL))
7425 {
4ee9c684 7426 /* See from which the next case to merge comes from. */
7427 if (psize == 0)
7428 {
7429 /* P is empty so the next case must come from Q. */
7430 e = q;
7431 q = q->right;
7432 qsize--;
7433 }
7434 else if (qsize == 0 || q == NULL)
7435 {
7436 /* Q is empty. */
7437 e = p;
7438 p = p->right;
7439 psize--;
7440 }
7441 else
7442 {
7443 cmp = compare_cases (p, q);
7444 if (cmp < 0)
7445 {
7446 /* The whole case range for P is less than the
1bcc6eb8 7447 one for Q. */
4ee9c684 7448 e = p;
7449 p = p->right;
7450 psize--;
7451 }
7452 else if (cmp > 0)
7453 {
7454 /* The whole case range for Q is greater than
1bcc6eb8 7455 the case range for P. */
4ee9c684 7456 e = q;
7457 q = q->right;
7458 qsize--;
7459 }
7460 else
7461 {
7462 /* The cases overlap, or they are the same
7463 element in the list. Either way, we must
7464 issue an error and get the next case from P. */
7465 /* FIXME: Sort P and Q by line number. */
716da296 7466 gfc_error_1 ("CASE label at %L overlaps with CASE "
4ee9c684 7467 "label at %L", &p->where, &q->where);
7468 overlap_seen = 1;
7469 e = p;
7470 p = p->right;
7471 psize--;
7472 }
7473 }
7474
7475 /* Add the next element to the merged list. */
7476 if (tail)
7477 tail->right = e;
7478 else
7479 list = e;
7480 e->left = tail;
7481 tail = e;
7482 }
7483
7484 /* P has now stepped INSIZE places along, and so has Q. So
1bcc6eb8 7485 they're the same. */
4ee9c684 7486 p = q;
7487 }
7488 tail->right = NULL;
7489
7490 /* If we have done only one merge or none at all, we've
1bcc6eb8 7491 finished sorting the cases. */
4ee9c684 7492 if (nmerges <= 1)
1bcc6eb8 7493 {
4ee9c684 7494 if (!overlap_seen)
7495 return list;
7496 else
7497 return NULL;
7498 }
7499
7500 /* Otherwise repeat, merging lists twice the size. */
7501 insize *= 2;
7502 }
7503}
7504
7505
c2849191 7506/* Check to see if an expression is suitable for use in a CASE statement.
7507 Makes sure that all case expressions are scalar constants of the same
60e19868 7508 type. Return false if anything is wrong. */
4ee9c684 7509
60e19868 7510static bool
1bcc6eb8 7511validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
4ee9c684 7512{
60e19868 7513 if (e == NULL) return true;
4ee9c684 7514
c2849191 7515 if (e->ts.type != case_expr->ts.type)
4ee9c684 7516 {
7517 gfc_error ("Expression in CASE statement at %L must be of type %s",
c2849191 7518 &e->where, gfc_basic_typename (case_expr->ts.type));
60e19868 7519 return false;
4ee9c684 7520 }
7521
c2849191 7522 /* C805 (R808) For a given case-construct, each case-value shall be of
7523 the same type as case-expr. For character type, length differences
7524 are allowed, but the kind type parameters shall be the same. */
7525
7526 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
4ee9c684 7527 {
b44437b9 7528 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7529 &e->where, case_expr->ts.kind);
60e19868 7530 return false;
4ee9c684 7531 }
7532
c58db196 7533 /* Convert the case value kind to that of case expression kind,
7534 if needed */
7535
c2849191 7536 if (e->ts.kind != case_expr->ts.kind)
7537 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7538
4ee9c684 7539 if (e->rank != 0)
7540 {
7541 gfc_error ("Expression in CASE statement at %L must be scalar",
7542 &e->where);
60e19868 7543 return false;
4ee9c684 7544 }
7545
60e19868 7546 return true;
4ee9c684 7547}
7548
7549
7550/* Given a completely parsed select statement, we:
7551
7552 - Validate all expressions and code within the SELECT.
7553 - Make sure that the selection expression is not of the wrong type.
7554 - Make sure that no case ranges overlap.
7555 - Eliminate unreachable cases and unreachable code resulting from
7556 removing case labels.
7557
7558 The standard does allow unreachable cases, e.g. CASE (5:3). But
7559 they are a hassle for code generation, and to prevent that, we just
7560 cut them out here. This is not necessary for overlapping cases
7561 because they are illegal and we never even try to generate code.
7562
7563 We have the additional caveat that a SELECT construct could have
231e961a 7564 been a computed GOTO in the source code. Fortunately we can fairly
4ee9c684 7565 easily work around that here: The case_expr for a "real" SELECT CASE
7566 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7567 we have to do is make sure that the case_expr is a scalar integer
7568 expression. */
7569
7570static void
c58ba4b2 7571resolve_select (gfc_code *code, bool select_type)
4ee9c684 7572{
7573 gfc_code *body;
7574 gfc_expr *case_expr;
7575 gfc_case *cp, *default_case, *tail, *head;
7576 int seen_unreachable;
94a286ff 7577 int seen_logical;
4ee9c684 7578 int ncases;
7579 bt type;
60e19868 7580 bool t;
4ee9c684 7581
578d3f19 7582 if (code->expr1 == NULL)
4ee9c684 7583 {
7584 /* This was actually a computed GOTO statement. */
7585 case_expr = code->expr2;
1bcc6eb8 7586 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
4ee9c684 7587 gfc_error ("Selection expression in computed GOTO statement "
7588 "at %L must be a scalar integer expression",
7589 &case_expr->where);
7590
7591 /* Further checking is not necessary because this SELECT was built
7592 by the compiler, so it should always be OK. Just move the
7593 case_expr from expr2 to expr so that we can handle computed
7594 GOTOs as normal SELECTs from here on. */
578d3f19 7595 code->expr1 = code->expr2;
4ee9c684 7596 code->expr2 = NULL;
7597 return;
7598 }
7599
578d3f19 7600 case_expr = code->expr1;
4ee9c684 7601 type = case_expr->ts.type;
c58ba4b2 7602
7603 /* F08:C830. */
4ee9c684 7604 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7605 {
7606 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7607 &case_expr->where, gfc_typename (&case_expr->ts));
7608
7609 /* Punt. Going on here just produce more garbage error messages. */
7610 return;
7611 }
7612
c58ba4b2 7613 /* F08:R842. */
7614 if (!select_type && case_expr->rank != 0)
7615 {
7616 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7617 "expression", &case_expr->where);
7618
7619 /* Punt. */
7620 return;
7621 }
7622
c58db196 7623 /* Raise a warning if an INTEGER case value exceeds the range of
7624 the case-expr. Later, all expressions will be promoted to the
7625 largest kind of all case-labels. */
7626
7627 if (type == BT_INTEGER)
7628 for (body = code->block; body; body = body->block)
030b7e6d 7629 for (cp = body->ext.block.case_list; cp; cp = cp->next)
c58db196 7630 {
7631 if (cp->low
7632 && gfc_check_integer_range (cp->low->value.integer,
7633 case_expr->ts.kind) != ARITH_OK)
7634 gfc_warning ("Expression in CASE statement at %L is "
7635 "not in the range of %s", &cp->low->where,
7636 gfc_typename (&case_expr->ts));
7637
7638 if (cp->high
7639 && cp->low != cp->high
7640 && gfc_check_integer_range (cp->high->value.integer,
7641 case_expr->ts.kind) != ARITH_OK)
7642 gfc_warning ("Expression in CASE statement at %L is "
7643 "not in the range of %s", &cp->high->where,
7644 gfc_typename (&case_expr->ts));
7645 }
7646
c2849191 7647 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7648 of the SELECT CASE expression and its CASE values. Walk the lists
7649 of case values, and if we find a mismatch, promote case_expr to
7650 the appropriate kind. */
7651
7652 if (type == BT_LOGICAL || type == BT_INTEGER)
7653 {
7654 for (body = code->block; body; body = body->block)
7655 {
7656 /* Walk the case label list. */
030b7e6d 7657 for (cp = body->ext.block.case_list; cp; cp = cp->next)
c2849191 7658 {
7659 /* Intercept the DEFAULT case. It does not have a kind. */
7660 if (cp->low == NULL && cp->high == NULL)
7661 continue;
7662
fc243266 7663 /* Unreachable case ranges are discarded, so ignore. */
c2849191 7664 if (cp->low != NULL && cp->high != NULL
7665 && cp->low != cp->high
134eab89 7666 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
c2849191 7667 continue;
7668
c2849191 7669 if (cp->low != NULL
7670 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7671 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7672
7673 if (cp->high != NULL
7674 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
fc243266 7675 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
c2849191 7676 }
7677 }
7678 }
7679
4ee9c684 7680 /* Assume there is no DEFAULT case. */
7681 default_case = NULL;
7682 head = tail = NULL;
7683 ncases = 0;
94a286ff 7684 seen_logical = 0;
4ee9c684 7685
7686 for (body = code->block; body; body = body->block)
7687 {
7688 /* Assume the CASE list is OK, and all CASE labels can be matched. */
60e19868 7689 t = true;
4ee9c684 7690 seen_unreachable = 0;
7691
7692 /* Walk the case label list, making sure that all case labels
1bcc6eb8 7693 are legal. */
030b7e6d 7694 for (cp = body->ext.block.case_list; cp; cp = cp->next)
4ee9c684 7695 {
7696 /* Count the number of cases in the whole construct. */
7697 ncases++;
7698
7699 /* Intercept the DEFAULT case. */
7700 if (cp->low == NULL && cp->high == NULL)
7701 {
7702 if (default_case != NULL)
1bcc6eb8 7703 {
716da296 7704 gfc_error_1 ("The DEFAULT CASE at %L cannot be followed "
4ee9c684 7705 "by a second DEFAULT CASE at %L",
7706 &default_case->where, &cp->where);
60e19868 7707 t = false;
4ee9c684 7708 break;
7709 }
7710 else
7711 {
7712 default_case = cp;
7713 continue;
7714 }
7715 }
7716
7717 /* Deal with single value cases and case ranges. Errors are
1bcc6eb8 7718 issued from the validation function. */
60e19868 7719 if (!validate_case_label_expr (cp->low, case_expr)
7720 || !validate_case_label_expr (cp->high, case_expr))
4ee9c684 7721 {
60e19868 7722 t = false;
4ee9c684 7723 break;
7724 }
7725
7726 if (type == BT_LOGICAL
7727 && ((cp->low == NULL || cp->high == NULL)
7728 || cp->low != cp->high))
7729 {
1bcc6eb8 7730 gfc_error ("Logical range in CASE statement at %L is not "
7731 "allowed", &cp->low->where);
60e19868 7732 t = false;
4ee9c684 7733 break;
7734 }
7735
94a286ff 7736 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7737 {
7738 int value;
7739 value = cp->low->value.logical == 0 ? 2 : 1;
7740 if (value & seen_logical)
7741 {
c58db196 7742 gfc_error ("Constant logical value in CASE statement "
94a286ff 7743 "is repeated at %L",
7744 &cp->low->where);
60e19868 7745 t = false;
94a286ff 7746 break;
7747 }
7748 seen_logical |= value;
7749 }
7750
4ee9c684 7751 if (cp->low != NULL && cp->high != NULL
7752 && cp->low != cp->high
134eab89 7753 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
4ee9c684 7754 {
8290d53f 7755 if (warn_surprising)
4166acc7 7756 gfc_warning (OPT_Wsurprising,
7757 "Range specification at %L can never be matched",
7758 &cp->where);
4ee9c684 7759
7760 cp->unreachable = 1;
7761 seen_unreachable = 1;
7762 }
7763 else
7764 {
7765 /* If the case range can be matched, it can also overlap with
7766 other cases. To make sure it does not, we put it in a
7767 double linked list here. We sort that with a merge sort
7768 later on to detect any overlapping cases. */
7769 if (!head)
1bcc6eb8 7770 {
4ee9c684 7771 head = tail = cp;
7772 head->right = head->left = NULL;
7773 }
7774 else
1bcc6eb8 7775 {
4ee9c684 7776 tail->right = cp;
7777 tail->right->left = tail;
7778 tail = tail->right;
7779 tail->right = NULL;
7780 }
7781 }
7782 }
7783
7784 /* It there was a failure in the previous case label, give up
7785 for this case label list. Continue with the next block. */
60e19868 7786 if (!t)
4ee9c684 7787 continue;
7788
7789 /* See if any case labels that are unreachable have been seen.
7790 If so, we eliminate them. This is a bit of a kludge because
7791 the case lists for a single case statement (label) is a
7792 single forward linked lists. */
7793 if (seen_unreachable)
7794 {
7795 /* Advance until the first case in the list is reachable. */
030b7e6d 7796 while (body->ext.block.case_list != NULL
7797 && body->ext.block.case_list->unreachable)
4ee9c684 7798 {
030b7e6d 7799 gfc_case *n = body->ext.block.case_list;
7800 body->ext.block.case_list = body->ext.block.case_list->next;
4ee9c684 7801 n->next = NULL;
7802 gfc_free_case_list (n);
7803 }
7804
7805 /* Strip all other unreachable cases. */
030b7e6d 7806 if (body->ext.block.case_list)
4ee9c684 7807 {
b249d458 7808 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
4ee9c684 7809 {
7810 if (cp->next->unreachable)
7811 {
7812 gfc_case *n = cp->next;
7813 cp->next = cp->next->next;
7814 n->next = NULL;
7815 gfc_free_case_list (n);
7816 }
7817 }
7818 }
7819 }
7820 }
7821
7822 /* See if there were overlapping cases. If the check returns NULL,
7823 there was overlap. In that case we don't do anything. If head
7824 is non-NULL, we prepend the DEFAULT case. The sorted list can
7825 then used during code generation for SELECT CASE constructs with
7826 a case expression of a CHARACTER type. */
7827 if (head)
7828 {
7829 head = check_case_overlap (head);
7830
7831 /* Prepend the default_case if it is there. */
7832 if (head != NULL && default_case)
7833 {
7834 default_case->left = NULL;
7835 default_case->right = head;
7836 head->left = default_case;
7837 }
7838 }
7839
7840 /* Eliminate dead blocks that may be the result if we've seen
7841 unreachable case labels for a block. */
7842 for (body = code; body && body->block; body = body->block)
7843 {
030b7e6d 7844 if (body->block->ext.block.case_list == NULL)
1bcc6eb8 7845 {
4ee9c684 7846 /* Cut the unreachable block from the code chain. */
7847 gfc_code *c = body->block;
7848 body->block = c->block;
7849
7850 /* Kill the dead block, but not the blocks below it. */
7851 c->block = NULL;
7852 gfc_free_statements (c);
1bcc6eb8 7853 }
4ee9c684 7854 }
7855
7856 /* More than two cases is legal but insane for logical selects.
7857 Issue a warning for it. */
8290d53f 7858 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
4166acc7 7859 gfc_warning (OPT_Wsurprising,
7860 "Logical SELECT CASE block at %L has more that two cases",
4ee9c684 7861 &code->loc);
7862}
7863
7864
1de1b1a9 7865/* Check if a derived type is extensible. */
7866
7867bool
7868gfc_type_is_extensible (gfc_symbol *sym)
7869{
a90fe829 7870 return !(sym->attr.is_bind_c || sym->attr.sequence
7871 || (sym->attr.is_class
7872 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
1de1b1a9 7873}
7874
7875
49dcd9d0 7876/* Resolve an associate-name: Resolve target and ensure the type-spec is
cf92f151 7877 correct as well as possibly the array-spec. */
7878
7879static void
7880resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7881{
7882 gfc_expr* target;
cf92f151 7883
7884 gcc_assert (sym->assoc);
7885 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7886
7887 /* If this is for SELECT TYPE, the target may not yet be set. In that
7888 case, return. Resolution will be called later manually again when
7889 this is done. */
7890 target = sym->assoc->target;
7891 if (!target)
7892 return;
7893 gcc_assert (!sym->assoc->dangling);
7894
60e19868 7895 if (resolve_target && !gfc_resolve_expr (target))
cf92f151 7896 return;
7897
7898 /* For variable targets, we get some attributes from the target. */
7899 if (target->expr_type == EXPR_VARIABLE)
7900 {
7901 gfc_symbol* tsym;
7902
7903 gcc_assert (target->symtree);
7904 tsym = target->symtree->n.sym;
7905
7906 sym->attr.asynchronous = tsym->attr.asynchronous;
7907 sym->attr.volatile_ = tsym->attr.volatile_;
7908
3a19c063 7909 sym->attr.target = tsym->attr.target
7910 || gfc_expr_attr (target).pointer;
b664a6ed 7911 if (is_subref_array (target))
7912 sym->attr.subref_array_pointer = 1;
cf92f151 7913 }
7914
62aa667d 7915 /* Get type if this was not already set. Note that it can be
7916 some other type than the target in case this is a SELECT TYPE
7917 selector! So we must not update when the type is already there. */
7918 if (sym->ts.type == BT_UNKNOWN)
7919 sym->ts = target->ts;
cf92f151 7920 gcc_assert (sym->ts.type != BT_UNKNOWN);
7921
7922 /* See if this is a valid association-to-variable. */
7725f40e 7923 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7924 && !gfc_has_vector_subscript (target));
cf92f151 7925
7926 /* Finally resolve if this is an array or not. */
3a19c063 7927 if (sym->attr.dimension && target->rank == 0)
cf92f151 7928 {
0d2b3c9c 7929 gfc_error ("Associate-name %qs at %L is used as array",
cf92f151 7930 sym->name, &sym->declared_at);
7931 sym->attr.dimension = 0;
7932 return;
7933 }
49dcd9d0 7934
7935 /* We cannot deal with class selectors that need temporaries. */
7936 if (target->ts.type == BT_CLASS
7937 && gfc_ref_needs_temporary_p (target->ref))
7938 {
7939 gfc_error ("CLASS selector at %L needs a temporary which is not "
7940 "yet implemented", &target->where);
7941 return;
7942 }
7943
7944 if (target->ts.type != BT_CLASS && target->rank > 0)
cf92f151 7945 sym->attr.dimension = 1;
49dcd9d0 7946 else if (target->ts.type == BT_CLASS)
7947 gfc_fix_class_refs (target);
7948
7949 /* The associate-name will have a correct type by now. Make absolutely
7950 sure that it has not picked up a dimension attribute. */
7951 if (sym->ts.type == BT_CLASS)
7952 sym->attr.dimension = 0;
cf92f151 7953
7954 if (sym->attr.dimension)
7955 {
7956 sym->as = gfc_get_array_spec ();
7957 sym->as->rank = target->rank;
7958 sym->as->type = AS_DEFERRED;
102abea2 7959 sym->as->corank = gfc_get_corank (target);
cf92f151 7960 }
20bdca22 7961
7962 /* Mark this as an associate variable. */
7963 sym->attr.associate_var = 1;
7964
7965 /* If the target is a good class object, so is the associate variable. */
7966 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7967 sym->attr.class_ok = 1;
cf92f151 7968}
7969
7970
1de1b1a9 7971/* Resolve a SELECT TYPE statement. */
7972
7973static void
7725f40e 7974resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
1de1b1a9 7975{
7976 gfc_symbol *selector_type;
bdfbc762 7977 gfc_code *body, *new_st, *if_st, *tail;
7978 gfc_code *class_is = NULL, *default_case = NULL;
7979 gfc_case *c;
1de1b1a9 7980 gfc_symtree *st;
7981 char name[GFC_MAX_SYMBOL_LEN];
cd62bad7 7982 gfc_namespace *ns;
bdfbc762 7983 int error = 0;
a90fe829 7984 int charlen = 0;
cd62bad7 7985
d18a512a 7986 ns = code->ext.block.ns;
cd62bad7 7987 gfc_resolve (ns);
1de1b1a9 7988
f0ea8570 7989 /* Check for F03:C813. */
7990 if (code->expr1->ts.type != BT_CLASS
7991 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7992 {
7993 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7994 "at %L", &code->loc);
7995 return;
7996 }
7997
1e22193c 7998 if (!code->expr1->symtree->n.sym->attr.class_ok)
7999 return;
8000
cd62bad7 8001 if (code->expr2)
f0ea8570 8002 {
8003 if (code->expr1->symtree->n.sym->attr.untyped)
8004 code->expr1->symtree->n.sym->ts = code->expr2->ts;
50b4b37b 8005 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
2082cd0b 8006
8007 /* F2008: C803 The selector expression must not be coindexed. */
8008 if (gfc_is_coindexed (code->expr2))
8009 {
8010 gfc_error ("Selector at %L must not be coindexed",
8011 &code->expr2->where);
8012 return;
8013 }
8014
f0ea8570 8015 }
cd62bad7 8016 else
2082cd0b 8017 {
8018 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8019
8020 if (gfc_is_coindexed (code->expr1))
8021 {
8022 gfc_error ("Selector at %L must not be coindexed",
8023 &code->expr1->where);
8024 return;
8025 }
8026 }
1de1b1a9 8027
1de1b1a9 8028 /* Loop over TYPE IS / CLASS IS cases. */
8029 for (body = code->block; body; body = body->block)
8030 {
030b7e6d 8031 c = body->ext.block.case_list;
1de1b1a9 8032
8033 /* Check F03:C815. */
8034 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
a90fe829 8035 && !selector_type->attr.unlimited_polymorphic
1de1b1a9 8036 && !gfc_type_is_extensible (c->ts.u.derived))
8037 {
0d2b3c9c 8038 gfc_error ("Derived type %qs at %L must be extensible",
1de1b1a9 8039 c->ts.u.derived->name, &c->where);
bdfbc762 8040 error++;
1de1b1a9 8041 continue;
8042 }
8043
8044 /* Check F03:C816. */
3facfa3c 8045 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8046 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8047 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
1de1b1a9 8048 {
3facfa3c 8049 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
0d2b3c9c 8050 gfc_error ("Derived type %qs at %L must be an extension of %qs",
3facfa3c 8051 c->ts.u.derived->name, &c->where, selector_type->name);
8052 else
0d2b3c9c 8053 gfc_error ("Unexpected intrinsic type %qs at %L",
3facfa3c 8054 gfc_basic_typename (c->ts.type), &c->where);
bdfbc762 8055 error++;
1de1b1a9 8056 continue;
8057 }
8058
a90fe829 8059 /* Check F03:C814. */
8060 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8061 {
8062 gfc_error ("The type-spec at %L shall specify that each length "
8063 "type parameter is assumed", &c->where);
8064 error++;
8065 continue;
8066 }
8067
1de1b1a9 8068 /* Intercept the DEFAULT case. */
8069 if (c->ts.type == BT_UNKNOWN)
8070 {
8071 /* Check F03:C818. */
bdfbc762 8072 if (default_case)
8073 {
716da296 8074 gfc_error_1 ("The DEFAULT CASE at %L cannot be followed "
bdfbc762 8075 "by a second DEFAULT CASE at %L",
030b7e6d 8076 &default_case->ext.block.case_list->where, &c->where);
bdfbc762 8077 error++;
8078 continue;
8079 }
62aa667d 8080
8081 default_case = body;
1de1b1a9 8082 }
8083 }
d6463863 8084
cf92f151 8085 if (error > 0)
bdfbc762 8086 return;
1de1b1a9 8087
cf92f151 8088 /* Transform SELECT TYPE statement to BLOCK and associate selector to
0c3f80cf 8089 target if present. If there are any EXIT statements referring to the
8090 SELECT TYPE construct, this is no problem because the gfc_code
8091 reference stays the same and EXIT is equally possible from the BLOCK
8092 it is changed to. */
cf92f151 8093 code->op = EXEC_BLOCK;
cd62bad7 8094 if (code->expr2)
8095 {
cf92f151 8096 gfc_association_list* assoc;
8097
8098 assoc = gfc_get_association_list ();
8099 assoc->st = code->expr1->symtree;
8100 assoc->target = gfc_copy_expr (code->expr2);
fd23cc08 8101 assoc->target->where = code->expr2->where;
cf92f151 8102 /* assoc->variable will be set by resolve_assoc_var. */
d6463863 8103
cf92f151 8104 code->ext.block.assoc = assoc;
8105 code->expr1->symtree->n.sym->assoc = assoc;
8106
8107 resolve_assoc_var (code->expr1->symtree->n.sym, false);
cd62bad7 8108 }
cf92f151 8109 else
8110 code->ext.block.assoc = NULL;
cd62bad7 8111
cf92f151 8112 /* Add EXEC_SELECT to switch on type. */
f1ab83c6 8113 new_st = gfc_get_code (code->op);
cd62bad7 8114 new_st->expr1 = code->expr1;
8115 new_st->expr2 = code->expr2;
8116 new_st->block = code->block;
cf92f151 8117 code->expr1 = code->expr2 = NULL;
8118 code->block = NULL;
cd62bad7 8119 if (!ns->code)
8120 ns->code = new_st;
8121 else
8122 ns->code->next = new_st;
cd62bad7 8123 code = new_st;
1de1b1a9 8124 code->op = EXEC_SELECT;
a90fe829 8125
607ae689 8126 gfc_add_vptr_component (code->expr1);
8127 gfc_add_hash_component (code->expr1);
1de1b1a9 8128
8129 /* Loop over TYPE IS / CLASS IS cases. */
8130 for (body = code->block; body; body = body->block)
8131 {
030b7e6d 8132 c = body->ext.block.case_list;
126387b5 8133
1de1b1a9 8134 if (c->ts.type == BT_DERIVED)
126387b5 8135 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8136 c->ts.u.derived->hash_value);
a90fe829 8137 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8138 {
8139 gfc_symbol *ivtab;
8140 gfc_expr *e;
8141
25014fa7 8142 ivtab = gfc_find_vtab (&c->ts);
a552d912 8143 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
a90fe829 8144 e = CLASS_DATA (ivtab)->initializer;
8145 c->low = c->high = gfc_copy_expr (e);
8146 }
126387b5 8147
bdfbc762 8148 else if (c->ts.type == BT_UNKNOWN)
1de1b1a9 8149 continue;
126387b5 8150
cf92f151 8151 /* Associate temporary to selector. This should only be done
8152 when this case is actually true, so build a new ASSOCIATE
8153 that does precisely this here (instead of using the
8154 'global' one). */
8155
bdfbc762 8156 if (c->ts.type == BT_CLASS)
607ae689 8157 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
a90fe829 8158 else if (c->ts.type == BT_DERIVED)
607ae689 8159 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
a90fe829 8160 else if (c->ts.type == BT_CHARACTER)
8161 {
8162 if (c->ts.u.cl && c->ts.u.cl->length
8163 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8164 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8165 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8166 charlen, c->ts.kind);
8167 }
8168 else
8169 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8170 c->ts.kind);
8171
cd62bad7 8172 st = gfc_find_symtree (ns->sym_root, name);
cf92f151 8173 gcc_assert (st->n.sym->assoc);
8174 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
fd23cc08 8175 st->n.sym->assoc->target->where = code->expr1->where;
a90fe829 8176 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
607ae689 8177 gfc_add_data_component (st->n.sym->assoc->target);
cf92f151 8178
f1ab83c6 8179 new_st = gfc_get_code (EXEC_BLOCK);
cf92f151 8180 new_st->ext.block.ns = gfc_build_block_ns (ns);
8181 new_st->ext.block.ns->code = body->next;
8182 body->next = new_st;
8183
8184 /* Chain in the new list only if it is marked as dangling. Otherwise
8185 there is a CASE label overlap and this is already used. Just ignore,
df084314 8186 the error is diagnosed elsewhere. */
cf92f151 8187 if (st->n.sym->assoc->dangling)
bdfbc762 8188 {
cf92f151 8189 new_st->ext.block.assoc = st->n.sym->assoc;
8190 st->n.sym->assoc->dangling = 0;
bdfbc762 8191 }
cf92f151 8192
8193 resolve_assoc_var (st->n.sym, false);
1de1b1a9 8194 }
d6463863 8195
bdfbc762 8196 /* Take out CLASS IS cases for separate treatment. */
8197 body = code;
8198 while (body && body->block)
8199 {
030b7e6d 8200 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
bdfbc762 8201 {
8202 /* Add to class_is list. */
8203 if (class_is == NULL)
d6463863 8204 {
bdfbc762 8205 class_is = body->block;
8206 tail = class_is;
8207 }
8208 else
8209 {
8210 for (tail = class_is; tail->block; tail = tail->block) ;
8211 tail->block = body->block;
8212 tail = tail->block;
8213 }
8214 /* Remove from EXEC_SELECT list. */
8215 body->block = body->block->block;
8216 tail->block = NULL;
8217 }
8218 else
8219 body = body->block;
8220 }
1de1b1a9 8221
bdfbc762 8222 if (class_is)
1de1b1a9 8223 {
bdfbc762 8224 gfc_symbol *vtab;
d6463863 8225
bdfbc762 8226 if (!default_case)
8227 {
8228 /* Add a default case to hold the CLASS IS cases. */
8229 for (tail = code; tail->block; tail = tail->block) ;
f1ab83c6 8230 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
bdfbc762 8231 tail = tail->block;
030b7e6d 8232 tail->ext.block.case_list = gfc_get_case ();
8233 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
bdfbc762 8234 tail->next = NULL;
8235 default_case = tail;
8236 }
09c509ed 8237
bdfbc762 8238 /* More than one CLASS IS block? */
8239 if (class_is->block)
1de1b1a9 8240 {
bdfbc762 8241 gfc_code **c1,*c2;
8242 bool swapped;
8243 /* Sort CLASS IS blocks by extension level. */
8244 do
8245 {
8246 swapped = false;
8247 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8248 {
8249 c2 = (*c1)->block;
8250 /* F03:C817 (check for doubles). */
030b7e6d 8251 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8252 == c2->ext.block.case_list->ts.u.derived->hash_value)
bdfbc762 8253 {
8254 gfc_error ("Double CLASS IS block in SELECT TYPE "
030b7e6d 8255 "statement at %L",
8256 &c2->ext.block.case_list->where);
bdfbc762 8257 return;
8258 }
030b7e6d 8259 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8260 < c2->ext.block.case_list->ts.u.derived->attr.extension)
bdfbc762 8261 {
8262 /* Swap. */
8263 (*c1)->block = c2->block;
8264 c2->block = *c1;
8265 *c1 = c2;
8266 swapped = true;
8267 }
8268 }
8269 }
8270 while (swapped);
1de1b1a9 8271 }
d6463863 8272
bdfbc762 8273 /* Generate IF chain. */
f1ab83c6 8274 if_st = gfc_get_code (EXEC_IF);
bdfbc762 8275 new_st = if_st;
8276 for (body = class_is; body; body = body->block)
8277 {
f1ab83c6 8278 new_st->block = gfc_get_code (EXEC_IF);
bdfbc762 8279 new_st = new_st->block;
bdfbc762 8280 /* Set up IF condition: Call _gfortran_is_extension_of. */
8281 new_st->expr1 = gfc_get_expr ();
8282 new_st->expr1->expr_type = EXPR_FUNCTION;
8283 new_st->expr1->ts.type = BT_LOGICAL;
8284 new_st->expr1->ts.kind = 4;
8285 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8286 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8287 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8288 /* Set up arguments. */
8289 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8290 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
f8d40ab5 8291 new_st->expr1->value.function.actual->expr->where = code->loc;
607ae689 8292 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
030b7e6d 8293 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
bdfbc762 8294 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8295 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8296 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8297 new_st->next = body->next;
8298 }
8299 if (default_case->next)
8300 {
f1ab83c6 8301 new_st->block = gfc_get_code (EXEC_IF);
bdfbc762 8302 new_st = new_st->block;
bdfbc762 8303 new_st->next = default_case->next;
8304 }
d6463863 8305
bdfbc762 8306 /* Replace CLASS DEFAULT code by the IF chain. */
8307 default_case->next = if_st;
1de1b1a9 8308 }
8309
7725f40e 8310 /* Resolve the internal code. This can not be done earlier because
8311 it requires that the sym->assoc of selectors is set already. */
8312 gfc_current_ns = ns;
8313 gfc_resolve_blocks (code->block, gfc_current_ns);
8314 gfc_current_ns = old_ns;
1de1b1a9 8315
c58ba4b2 8316 resolve_select (code, true);
1de1b1a9 8317}
8318
8319
2978704c 8320/* Resolve a transfer statement. This is making sure that:
8321 -- a derived type being transferred has only non-pointer components
d6463863 8322 -- a derived type being transferred doesn't have private components, unless
ba9448b4 8323 it's being transferred from the module where the type was defined
2978704c 8324 -- we're not trying to transfer a whole assumed size array. */
8325
8326static void
1bcc6eb8 8327resolve_transfer (gfc_code *code)
2978704c 8328{
8329 gfc_typespec *ts;
8330 gfc_symbol *sym;
8331 gfc_ref *ref;
8332 gfc_expr *exp;
8333
578d3f19 8334 exp = code->expr1;
2978704c 8335
c0c208f9 8336 while (exp != NULL && exp->expr_type == EXPR_OP
8337 && exp->value.op.op == INTRINSIC_PARENTHESES)
8338 exp = exp->value.op.op1;
8339
93d1352a 8340 if (exp && exp->expr_type == EXPR_NULL
8341 && code->ext.dt)
5b6e6354 8342 {
93d1352a 8343 gfc_error ("Invalid context for NULL () intrinsic at %L",
8344 &exp->where);
5b6e6354 8345 return;
8346 }
8347
c0c208f9 8348 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8349 && exp->expr_type != EXPR_FUNCTION))
2978704c 8350 return;
8351
6f73755f 8352 /* If we are reading, the variable will be changed. Note that
8353 code->ext.dt may be NULL if the TRANSFER is related to
8354 an INQUIRE statement -- but in this case, we are not reading, either. */
8355 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
080819af 8356 && !gfc_check_vardef_context (exp, false, false, false,
60e19868 8357 _("item in READ")))
6f73755f 8358 return;
8359
2978704c 8360 sym = exp->symtree->n.sym;
8361 ts = &sym->ts;
8362
8363 /* Go to actual component transferred. */
8811c470 8364 for (ref = exp->ref; ref; ref = ref->next)
2978704c 8365 if (ref->type == REF_COMPONENT)
8366 ts = &ref->u.c.component->ts;
8367
7e2dcd7e 8368 if (ts->type == BT_CLASS)
8369 {
8370 /* FIXME: Test for defined input/output. */
8371 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8372 "it is processed by a defined input/output procedure",
8373 &code->loc);
8374 return;
8375 }
8376
2978704c 8377 if (ts->type == BT_DERIVED)
8378 {
8379 /* Check that transferred derived type doesn't contain POINTER
8380 components. */
eeebe20b 8381 if (ts->u.derived->attr.pointer_comp)
2978704c 8382 {
945c743f 8383 gfc_error ("Data transfer element at %L cannot have POINTER "
8384 "components unless it is processed by a defined "
8385 "input/output procedure", &code->loc);
2978704c 8386 return;
8387 }
8388
b839d579 8389 /* F08:C935. */
8390 if (ts->u.derived->attr.proc_pointer_comp)
8391 {
8392 gfc_error ("Data transfer element at %L cannot have "
8393 "procedure pointer components", &code->loc);
8394 return;
8395 }
8396
eeebe20b 8397 if (ts->u.derived->attr.alloc_comp)
2294b616 8398 {
945c743f 8399 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8400 "components unless it is processed by a defined "
8401 "input/output procedure", &code->loc);
2294b616 8402 return;
8403 }
8404
07f0c434 8405 /* C_PTR and C_FUNPTR have private components which means they can not
8406 be printed. However, if -std=gnu and not -pedantic, allow
8407 the component to be printed to help debugging. */
8408 if (ts->u.derived->ts.f90_type == BT_VOID)
8409 {
60e19868 8410 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8411 "cannot have PRIVATE components", &code->loc))
07f0c434 8412 return;
8413 }
8414 else if (derived_inaccessible (ts->u.derived))
2978704c 8415 {
8416 gfc_error ("Data transfer element at %L cannot have "
8417 "PRIVATE components",&code->loc);
8418 return;
8419 }
8420 }
8421
12c86104 8422 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
2978704c 8423 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8424 {
8425 gfc_error ("Data transfer element at %L cannot be a full reference to "
8426 "an assumed-size array", &code->loc);
8427 return;
8428 }
8429}
8430
8431
4ee9c684 8432/*********** Toplevel code resolution subroutines ***********/
8433
82efdb2e 8434/* Find the set of labels that are reachable from this block. We also
8581350b 8435 record the last statement in each block. */
d6463863 8436
82efdb2e 8437static void
8581350b 8438find_reachable_labels (gfc_code *block)
82efdb2e 8439{
8440 gfc_code *c;
8441
8442 if (!block)
8443 return;
8444
8445 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8446
8581350b 8447 /* Collect labels in this block. We don't keep those corresponding
8448 to END {IF|SELECT}, these are checked in resolve_branch by going
8449 up through the code_stack. */
82efdb2e 8450 for (c = block; c; c = c->next)
8451 {
045b8fbb 8452 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
82efdb2e 8453 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
82efdb2e 8454 }
8455
8456 /* Merge with labels from parent block. */
8457 if (cs_base->prev)
8458 {
8459 gcc_assert (cs_base->prev->reachable_labels);
8460 bitmap_ior_into (cs_base->reachable_labels,
8461 cs_base->prev->reachable_labels);
8462 }
8463}
8464
c6cd3066 8465
3f73d66e 8466static void
8467resolve_lock_unlock (gfc_code *code)
8468{
5f4a118e 8469 if (code->expr1->expr_type == EXPR_FUNCTION
8470 && code->expr1->value.function.isym
8471 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8472 remove_caf_get_intrinsic (code->expr1);
8473
c135f087 8474 if (code->expr1->ts.type != BT_DERIVED
8475 || code->expr1->expr_type != EXPR_VARIABLE
8476 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8477 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8478 || code->expr1->rank != 0
50fefeb7 8479 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8480 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8481 &code->expr1->where);
3f73d66e 8482
8483 /* Check STAT. */
8484 if (code->expr2
8485 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8486 || code->expr2->expr_type != EXPR_VARIABLE))
8487 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8488 &code->expr2->where);
8489
c135f087 8490 if (code->expr2
080819af 8491 && !gfc_check_vardef_context (code->expr2, false, false, false,
60e19868 8492 _("STAT variable")))
c135f087 8493 return;
8494
3f73d66e 8495 /* Check ERRMSG. */
8496 if (code->expr3
8497 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8498 || code->expr3->expr_type != EXPR_VARIABLE))
8499 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8500 &code->expr3->where);
8501
c135f087 8502 if (code->expr3
080819af 8503 && !gfc_check_vardef_context (code->expr3, false, false, false,
60e19868 8504 _("ERRMSG variable")))
c135f087 8505 return;
8506
3f73d66e 8507 /* Check ACQUIRED_LOCK. */
8508 if (code->expr4
8509 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8510 || code->expr4->expr_type != EXPR_VARIABLE))
8511 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8512 "variable", &code->expr4->where);
c135f087 8513
8514 if (code->expr4
080819af 8515 && !gfc_check_vardef_context (code->expr4, false, false, false,
60e19868 8516 _("ACQUIRED_LOCK variable")))
c135f087 8517 return;
3f73d66e 8518}
8519
8520
498b946e 8521static void
8522resolve_critical (gfc_code *code)
8523{
8524 gfc_symtree *symtree;
8525 gfc_symbol *lock_type;
8526 char name[GFC_MAX_SYMBOL_LEN];
8527 static int serial = 0;
8528
4fe73152 8529 if (flag_coarray != GFC_FCOARRAY_LIB)
498b946e 8530 return;
8531
fd9b5fab 8532 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8533 GFC_PREFIX ("lock_type"));
498b946e 8534 if (symtree)
8535 lock_type = symtree->n.sym;
8536 else
8537 {
fd9b5fab 8538 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8539 false) != 0)
498b946e 8540 gcc_unreachable ();
8541 lock_type = symtree->n.sym;
8542 lock_type->attr.flavor = FL_DERIVED;
8543 lock_type->attr.zero_comp = 1;
8544 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8545 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8546 }
8547
fd9b5fab 8548 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
498b946e 8549 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8550 gcc_unreachable ();
8551
8552 code->resolved_sym = symtree->n.sym;
8553 symtree->n.sym->attr.flavor = FL_VARIABLE;
8554 symtree->n.sym->attr.referenced = 1;
8555 symtree->n.sym->attr.artificial = 1;
8556 symtree->n.sym->attr.codimension = 1;
8557 symtree->n.sym->ts.type = BT_DERIVED;
8558 symtree->n.sym->ts.u.derived = lock_type;
8559 symtree->n.sym->as = gfc_get_array_spec ();
8560 symtree->n.sym->as->corank = 1;
8561 symtree->n.sym->as->type = AS_EXPLICIT;
8562 symtree->n.sym->as->cotype = AS_EXPLICIT;
8563 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8564 NULL, 1);
8565}
8566
8567
c6cd3066 8568static void
8569resolve_sync (gfc_code *code)
8570{
8571 /* Check imageset. The * case matches expr1 == NULL. */
8572 if (code->expr1)
8573 {
8574 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8575 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8576 "INTEGER expression", &code->expr1->where);
8577 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8578 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8579 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8580 &code->expr1->where);
8581 else if (code->expr1->expr_type == EXPR_ARRAY
60e19868 8582 && gfc_simplify_expr (code->expr1, 0))
c6cd3066 8583 {
8584 gfc_constructor *cons;
126387b5 8585 cons = gfc_constructor_first (code->expr1->value.constructor);
8586 for (; cons; cons = gfc_constructor_next (cons))
c6cd3066 8587 if (cons->expr->expr_type == EXPR_CONSTANT
8588 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8589 gfc_error ("Imageset argument at %L must between 1 and "
8590 "num_images()", &cons->expr->where);
8591 }
8592 }
8593
8594 /* Check STAT. */
8595 if (code->expr2
8596 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8597 || code->expr2->expr_type != EXPR_VARIABLE))
8598 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8599 &code->expr2->where);
8600
8601 /* Check ERRMSG. */
8602 if (code->expr3
8603 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8604 || code->expr3->expr_type != EXPR_VARIABLE))
8605 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8606 &code->expr3->where);
8607}
8608
8609
8581350b 8610/* Given a branch to a label, see if the branch is conforming.
82efdb2e 8611 The code node describes where the branch is located. */
4ee9c684 8612
8613static void
1bcc6eb8 8614resolve_branch (gfc_st_label *label, gfc_code *code)
4ee9c684 8615{
4ee9c684 8616 code_stack *stack;
4ee9c684 8617
8618 if (label == NULL)
8619 return;
4ee9c684 8620
8621 /* Step one: is this a valid branching target? */
8622
82efdb2e 8623 if (label->defined == ST_LABEL_UNKNOWN)
4ee9c684 8624 {
82efdb2e 8625 gfc_error ("Label %d referenced at %L is never defined", label->value,
8626 &label->where);
4ee9c684 8627 return;
8628 }
8629
2c46015e 8630 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
4ee9c684 8631 {
716da296 8632 gfc_error_1 ("Statement at %L is not a valid branch target statement "
82efdb2e 8633 "for the branch statement at %L", &label->where, &code->loc);
4ee9c684 8634 return;
8635 }
8636
8637 /* Step two: make sure this branch is not a branch to itself ;-) */
8638
8639 if (code->here == label)
8640 {
05035f4d 8641 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
4ee9c684 8642 return;
8643 }
8644
82efdb2e 8645 /* Step three: See if the label is in the same block as the
8646 branching statement. The hard work has been done by setting up
8647 the bitmap reachable_labels. */
4ee9c684 8648
8581350b 8649 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
c6cd3066 8650 {
8651 /* Check now whether there is a CRITICAL construct; if so, check
8652 whether the label is still visible outside of the CRITICAL block,
8653 which is invalid. */
8654 for (stack = cs_base; stack; stack = stack->prev)
55ea8666 8655 {
8656 if (stack->current->op == EXEC_CRITICAL
8657 && bitmap_bit_p (stack->reachable_labels, label->value))
716da296 8658 gfc_error_1 ("GOTO statement at %L leaves CRITICAL construct for "
55ea8666 8659 "label at %L", &code->loc, &label->where);
8660 else if (stack->current->op == EXEC_DO_CONCURRENT
8661 && bitmap_bit_p (stack->reachable_labels, label->value))
716da296 8662 gfc_error_1 ("GOTO statement at %L leaves DO CONCURRENT construct "
55ea8666 8663 "for label at %L", &code->loc, &label->where);
8664 }
c6cd3066 8665
8666 return;
8667 }
4ee9c684 8668
8581350b 8669 /* Step four: If we haven't found the label in the bitmap, it may
8670 still be the label of the END of the enclosing block, in which
8671 case we find it by going up the code_stack. */
4ee9c684 8672
82efdb2e 8673 for (stack = cs_base; stack; stack = stack->prev)
c6cd3066 8674 {
8675 if (stack->current->next && stack->current->next->here == label)
8676 break;
8677 if (stack->current->op == EXEC_CRITICAL)
8678 {
8679 /* Note: A label at END CRITICAL does not leave the CRITICAL
8680 construct as END CRITICAL is still part of it. */
716da296 8681 gfc_error_1 ("GOTO statement at %L leaves CRITICAL construct for label"
c6cd3066 8682 " at %L", &code->loc, &label->where);
8683 return;
8684 }
55ea8666 8685 else if (stack->current->op == EXEC_DO_CONCURRENT)
8686 {
716da296 8687 gfc_error_1 ("GOTO statement at %L leaves DO CONCURRENT construct for "
55ea8666 8688 "label at %L", &code->loc, &label->where);
8689 return;
8690 }
c6cd3066 8691 }
4ee9c684 8692
8581350b 8693 if (stack)
82efdb2e 8694 {
045b8fbb 8695 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
8581350b 8696 return;
4ee9c684 8697 }
82efdb2e 8698
8581350b 8699 /* The label is not in an enclosing block, so illegal. This was
8700 allowed in Fortran 66, so we allow it as extension. No
8701 further checks are necessary in this case. */
0d2b3c9c 8702 gfc_notify_std_1 (GFC_STD_LEGACY, "Label at %L is not in the same block "
8581350b 8703 "as the GOTO statement at %L", &label->where,
8704 &code->loc);
8705 return;
4ee9c684 8706}
8707
8708
8709/* Check whether EXPR1 has the same shape as EXPR2. */
8710
60e19868 8711static bool
4ee9c684 8712resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8713{
8714 mpz_t shape[GFC_MAX_DIMENSIONS];
8715 mpz_t shape2[GFC_MAX_DIMENSIONS];
60e19868 8716 bool result = false;
4ee9c684 8717 int i;
8718
8719 /* Compare the rank. */
8720 if (expr1->rank != expr2->rank)
8721 return result;
8722
8723 /* Compare the size of each dimension. */
8724 for (i=0; i<expr1->rank; i++)
8725 {
60e19868 8726 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
1bcc6eb8 8727 goto ignore;
4ee9c684 8728
60e19868 8729 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
1bcc6eb8 8730 goto ignore;
4ee9c684 8731
8732 if (mpz_cmp (shape[i], shape2[i]))
1bcc6eb8 8733 goto over;
4ee9c684 8734 }
8735
8736 /* When either of the two expression is an assumed size array, we
8737 ignore the comparison of dimension sizes. */
8738ignore:
60e19868 8739 result = true;
4ee9c684 8740
8741over:
a9031a3e 8742 gfc_clear_shape (shape, i);
8743 gfc_clear_shape (shape2, i);
4ee9c684 8744 return result;
8745}
8746
8747
8748/* Check whether a WHERE assignment target or a WHERE mask expression
8749 has the same shape as the outmost WHERE mask expression. */
8750
8751static void
8752resolve_where (gfc_code *code, gfc_expr *mask)
8753{
8754 gfc_code *cblock;
8755 gfc_code *cnext;
8756 gfc_expr *e = NULL;
8757
8758 cblock = code->block;
8759
8760 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8761 In case of nested WHERE, only the outmost one is stored. */
8762 if (mask == NULL) /* outmost WHERE */
578d3f19 8763 e = cblock->expr1;
4ee9c684 8764 else /* inner WHERE */
8765 e = mask;
8766
8767 while (cblock)
8768 {
578d3f19 8769 if (cblock->expr1)
1bcc6eb8 8770 {
8771 /* Check if the mask-expr has a consistent shape with the
8772 outmost WHERE mask-expr. */
60e19868 8773 if (!resolve_where_shape (cblock->expr1, e))
1bcc6eb8 8774 gfc_error ("WHERE mask at %L has inconsistent shape",
578d3f19 8775 &cblock->expr1->where);
1bcc6eb8 8776 }
4ee9c684 8777
8778 /* the assignment statement of a WHERE statement, or the first
1bcc6eb8 8779 statement in where-body-construct of a WHERE construct */
4ee9c684 8780 cnext = cblock->next;
8781 while (cnext)
1bcc6eb8 8782 {
8783 switch (cnext->op)
8784 {
8785 /* WHERE assignment statement */
8786 case EXEC_ASSIGN:
8787
8788 /* Check shape consistent for WHERE assignment target. */
60e19868 8789 if (e && !resolve_where_shape (cnext->expr1, e))
1bcc6eb8 8790 gfc_error ("WHERE assignment target at %L has "
578d3f19 8791 "inconsistent shape", &cnext->expr1->where);
1bcc6eb8 8792 break;
8793
d6463863 8794
74f588f2 8795 case EXEC_ASSIGN_CALL:
8796 resolve_call (cnext);
5a82d68e 8797 if (!cnext->resolved_sym->attr.elemental)
847fe274 8798 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5a82d68e 8799 &cnext->ext.actual->expr->where);
74f588f2 8800 break;
8801
1bcc6eb8 8802 /* WHERE or WHERE construct is part of a where-body-construct */
8803 case EXEC_WHERE:
8804 resolve_where (cnext, e);
8805 break;
8806
8807 default:
8808 gfc_error ("Unsupported statement inside WHERE at %L",
8809 &cnext->loc);
8810 }
8811 /* the next statement within the same where-body-construct */
8812 cnext = cnext->next;
4ee9c684 8813 }
8814 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8815 cblock = cblock->block;
8816 }
8817}
8818
8819
4ee9c684 8820/* Resolve assignment in FORALL construct.
8821 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8822 FORALL index variables. */
8823
8824static void
8825gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8826{
8827 int n;
8828
8829 for (n = 0; n < nvar; n++)
8830 {
8831 gfc_symbol *forall_index;
8832
8833 forall_index = var_expr[n]->symtree->n.sym;
8834
8835 /* Check whether the assignment target is one of the FORALL index
1bcc6eb8 8836 variable. */
578d3f19 8837 if ((code->expr1->expr_type == EXPR_VARIABLE)
8838 && (code->expr1->symtree->n.sym == forall_index))
1bcc6eb8 8839 gfc_error ("Assignment to a FORALL index variable at %L",
578d3f19 8840 &code->expr1->where);
4ee9c684 8841 else
1bcc6eb8 8842 {
8843 /* If one of the FORALL index variables doesn't appear in the
fe5c28d2 8844 assignment variable, then there could be a many-to-one
8845 assignment. Emit a warning rather than an error because the
8846 mask could be resolving this problem. */
60e19868 8847 if (!find_forall_index (code->expr1, forall_index, 0))
4166acc7 8848 gfc_warning ("The FORALL with index %qs is not used on the "
fe5c28d2 8849 "left side of the assignment at %L and so might "
8850 "cause multiple assignment to this object",
578d3f19 8851 var_expr[n]->symtree->name, &code->expr1->where);
1bcc6eb8 8852 }
4ee9c684 8853 }
8854}
8855
8856
8857/* Resolve WHERE statement in FORALL construct. */
8858
8859static void
1bcc6eb8 8860gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8861 gfc_expr **var_expr)
8862{
4ee9c684 8863 gfc_code *cblock;
8864 gfc_code *cnext;
8865
8866 cblock = code->block;
8867 while (cblock)
8868 {
8869 /* the assignment statement of a WHERE statement, or the first
1bcc6eb8 8870 statement in where-body-construct of a WHERE construct */
4ee9c684 8871 cnext = cblock->next;
8872 while (cnext)
1bcc6eb8 8873 {
8874 switch (cnext->op)
8875 {
8876 /* WHERE assignment statement */
8877 case EXEC_ASSIGN:
8878 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8879 break;
d6463863 8880
74f588f2 8881 /* WHERE operator assignment statement */
8882 case EXEC_ASSIGN_CALL:
8883 resolve_call (cnext);
5a82d68e 8884 if (!cnext->resolved_sym->attr.elemental)
847fe274 8885 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
5a82d68e 8886 &cnext->ext.actual->expr->where);
74f588f2 8887 break;
1bcc6eb8 8888
8889 /* WHERE or WHERE construct is part of a where-body-construct */
8890 case EXEC_WHERE:
8891 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8892 break;
8893
8894 default:
8895 gfc_error ("Unsupported statement inside WHERE at %L",
8896 &cnext->loc);
8897 }
8898 /* the next statement within the same where-body-construct */
8899 cnext = cnext->next;
8900 }
4ee9c684 8901 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8902 cblock = cblock->block;
8903 }
8904}
8905
8906
8907/* Traverse the FORALL body to check whether the following errors exist:
8908 1. For assignment, check if a many-to-one assignment happens.
8909 2. For WHERE statement, check the WHERE body to see if there is any
8910 many-to-one assignment. */
8911
8912static void
8913gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8914{
8915 gfc_code *c;
8916
8917 c = code->block->next;
8918 while (c)
8919 {
8920 switch (c->op)
1bcc6eb8 8921 {
8922 case EXEC_ASSIGN:
8923 case EXEC_POINTER_ASSIGN:
8924 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8925 break;
8926
74f588f2 8927 case EXEC_ASSIGN_CALL:
8928 resolve_call (c);
8929 break;
8930
1bcc6eb8 8931 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8932 there is no need to handle it here. */
8933 case EXEC_FORALL:
8934 break;
8935 case EXEC_WHERE:
8936 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8937 break;
8938 default:
8939 break;
8940 }
4ee9c684 8941 /* The next statement in the FORALL body. */
8942 c = c->next;
8943 }
8944}
8945
8946
7046511a 8947/* Counts the number of iterators needed inside a forall construct, including
d6463863 8948 nested forall constructs. This is used to allocate the needed memory
7046511a 8949 in gfc_resolve_forall. */
8950
d6463863 8951static int
7046511a 8952gfc_count_forall_iterators (gfc_code *code)
8953{
8954 int max_iters, sub_iters, current_iters;
8955 gfc_forall_iterator *fa;
8956
8957 gcc_assert(code->op == EXEC_FORALL);
8958 max_iters = 0;
8959 current_iters = 0;
8960
8961 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8962 current_iters ++;
d6463863 8963
7046511a 8964 code = code->block->next;
8965
8966 while (code)
d6463863 8967 {
7046511a 8968 if (code->op == EXEC_FORALL)
8969 {
8970 sub_iters = gfc_count_forall_iterators (code);
8971 if (sub_iters > max_iters)
8972 max_iters = sub_iters;
8973 }
8974 code = code->next;
8975 }
8976
8977 return current_iters + max_iters;
8978}
8979
8980
4ee9c684 8981/* Given a FORALL construct, first resolve the FORALL iterator, then call
8982 gfc_resolve_forall_body to resolve the FORALL body. */
8983
4ee9c684 8984static void
8985gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8986{
8987 static gfc_expr **var_expr;
8988 static int total_var = 0;
8989 static int nvar = 0;
7046511a 8990 int old_nvar, tmp;
4ee9c684 8991 gfc_forall_iterator *fa;
4ee9c684 8992 int i;
8993
7046511a 8994 old_nvar = nvar;
8995
4ee9c684 8996 /* Start to resolve a FORALL construct */
8997 if (forall_save == 0)
8998 {
8999 /* Count the total number of FORALL index in the nested FORALL
7046511a 9000 construct in order to allocate the VAR_EXPR with proper size. */
9001 total_var = gfc_count_forall_iterators (code);
4ee9c684 9002
b14e2757 9003 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
dfa3fb6a 9004 var_expr = XCNEWVEC (gfc_expr *, total_var);
4ee9c684 9005 }
9006
9007 /* The information about FORALL iterator, including FORALL index start, end
9008 and stride. The FORALL index can not appear in start, end or stride. */
9009 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9010 {
9011 /* Check if any outer FORALL index name is the same as the current
1bcc6eb8 9012 one. */
4ee9c684 9013 for (i = 0; i < nvar; i++)
1bcc6eb8 9014 {
9015 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9016 {
9017 gfc_error ("An outer FORALL construct already has an index "
9018 "with this name %L", &fa->var->where);
9019 }
9020 }
4ee9c684 9021
9022 /* Record the current FORALL index. */
9023 var_expr[nvar] = gfc_copy_expr (fa->var);
9024
4ee9c684 9025 nvar++;
7046511a 9026
9027 /* No memory leak. */
9028 gcc_assert (nvar <= total_var);
4ee9c684 9029 }
9030
9031 /* Resolve the FORALL body. */
9032 gfc_resolve_forall_body (code, nvar, var_expr);
9033
9034 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
764f1175 9035 gfc_resolve_blocks (code->block, ns);
4ee9c684 9036
7046511a 9037 tmp = nvar;
9038 nvar = old_nvar;
9039 /* Free only the VAR_EXPRs allocated in this frame. */
9040 for (i = nvar; i < tmp; i++)
9041 gfc_free_expr (var_expr[i]);
4ee9c684 9042
7046511a 9043 if (nvar == 0)
9044 {
9045 /* We are in the outermost FORALL construct. */
9046 gcc_assert (forall_save == 0);
9047
9048 /* VAR_EXPR is not needed any more. */
434f0922 9049 free (var_expr);
7046511a 9050 total_var = 0;
9051 }
4ee9c684 9052}
9053
9054
6a7084d7 9055/* Resolve a BLOCK construct statement. */
9056
9057static void
9058resolve_block_construct (gfc_code* code)
9059{
d18a512a 9060 /* Resolve the BLOCK's namespace. */
9061 gfc_resolve (code->ext.block.ns);
7b82374f 9062
9063 /* For an ASSOCIATE block, the associations (and their targets) are already
cf92f151 9064 resolved during resolve_symbol. */
6a7084d7 9065}
9066
9067
9068/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
4ee9c684 9069 DO code nodes. */
9070
764f1175 9071void
1bcc6eb8 9072gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
4ee9c684 9073{
60e19868 9074 bool t;
4ee9c684 9075
9076 for (; b; b = b->block)
9077 {
578d3f19 9078 t = gfc_resolve_expr (b->expr1);
60e19868 9079 if (!gfc_resolve_expr (b->expr2))
9080 t = false;
4ee9c684 9081
9082 switch (b->op)
9083 {
9084 case EXEC_IF:
60e19868 9085 if (t && b->expr1 != NULL
578d3f19 9086 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
1bcc6eb8 9087 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
578d3f19 9088 &b->expr1->where);
4ee9c684 9089 break;
9090
9091 case EXEC_WHERE:
60e19868 9092 if (t
578d3f19 9093 && b->expr1 != NULL
9094 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
1bcc6eb8 9095 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
578d3f19 9096 &b->expr1->where);
4ee9c684 9097 break;
9098
1bcc6eb8 9099 case EXEC_GOTO:
13b33c16 9100 resolve_branch (b->label1, b);
1bcc6eb8 9101 break;
4ee9c684 9102
6a7084d7 9103 case EXEC_BLOCK:
9104 resolve_block_construct (b);
9105 break;
9106
4ee9c684 9107 case EXEC_SELECT:
1de1b1a9 9108 case EXEC_SELECT_TYPE:
4ee9c684 9109 case EXEC_FORALL:
9110 case EXEC_DO:
9111 case EXEC_DO_WHILE:
55ea8666 9112 case EXEC_DO_CONCURRENT:
c6cd3066 9113 case EXEC_CRITICAL:
60c514ba 9114 case EXEC_READ:
9115 case EXEC_WRITE:
9116 case EXEC_IOLENGTH:
ff6af856 9117 case EXEC_WAIT:
4ee9c684 9118 break;
9119
764f1175 9120 case EXEC_OMP_ATOMIC:
9121 case EXEC_OMP_CRITICAL:
691447ab 9122 case EXEC_OMP_DISTRIBUTE:
9123 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9124 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9125 case EXEC_OMP_DISTRIBUTE_SIMD:
764f1175 9126 case EXEC_OMP_DO:
15b28553 9127 case EXEC_OMP_DO_SIMD:
764f1175 9128 case EXEC_OMP_MASTER:
9129 case EXEC_OMP_ORDERED:
9130 case EXEC_OMP_PARALLEL:
9131 case EXEC_OMP_PARALLEL_DO:
15b28553 9132 case EXEC_OMP_PARALLEL_DO_SIMD:
764f1175 9133 case EXEC_OMP_PARALLEL_SECTIONS:
9134 case EXEC_OMP_PARALLEL_WORKSHARE:
9135 case EXEC_OMP_SECTIONS:
15b28553 9136 case EXEC_OMP_SIMD:
764f1175 9137 case EXEC_OMP_SINGLE:
691447ab 9138 case EXEC_OMP_TARGET:
9139 case EXEC_OMP_TARGET_DATA:
9140 case EXEC_OMP_TARGET_TEAMS:
9141 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9142 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9143 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9144 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9145 case EXEC_OMP_TARGET_UPDATE:
fd6481cf 9146 case EXEC_OMP_TASK:
15b28553 9147 case EXEC_OMP_TASKGROUP:
fd6481cf 9148 case EXEC_OMP_TASKWAIT:
2169f33b 9149 case EXEC_OMP_TASKYIELD:
691447ab 9150 case EXEC_OMP_TEAMS:
9151 case EXEC_OMP_TEAMS_DISTRIBUTE:
9152 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9153 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9154 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
764f1175 9155 case EXEC_OMP_WORKSHARE:
9156 break;
9157
4ee9c684 9158 default:
6a7084d7 9159 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
4ee9c684 9160 }
9161
c3f3b68d 9162 gfc_resolve_code (b->next, ns);
4ee9c684 9163 }
9164}
9165
9166
c94189f2 9167/* Does everything to resolve an ordinary assignment. Returns true
69b1505f 9168 if this is an interface assignment. */
c94189f2 9169static bool
9170resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9171{
9172 bool rval = false;
9173 gfc_expr *lhs;
9174 gfc_expr *rhs;
9175 int llen = 0;
9176 int rlen = 0;
9177 int n;
9178 gfc_ref *ref;
94ce0fae 9179 symbol_attribute attr;
c94189f2 9180
60e19868 9181 if (gfc_extend_assign (code, ns))
c94189f2 9182 {
7d034542 9183 gfc_expr** rhsptr;
9184
9185 if (code->op == EXEC_ASSIGN_CALL)
c94189f2 9186 {
7d034542 9187 lhs = code->ext.actual->expr;
9188 rhsptr = &code->ext.actual->next->expr;
7d034542 9189 }
9190 else
9191 {
9192 gfc_actual_arglist* args;
9193 gfc_typebound_proc* tbp;
9194
9195 gcc_assert (code->op == EXEC_COMPCALL);
9196
9197 args = code->expr1->value.compcall.actual;
9198 lhs = args->expr;
9199 rhsptr = &args->next->expr;
9200
9201 tbp = code->expr1->value.compcall.tbp;
9202 gcc_assert (!tbp->is_generic);
c94189f2 9203 }
9204
9205 /* Make a temporary rhs when there is a default initializer
9206 and rhs is the same symbol as the lhs. */
7d034542 9207 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9208 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
08262510 9209 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7d034542 9210 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9211 *rhsptr = gfc_get_parentheses (*rhsptr);
c94189f2 9212
9213 return true;
9214 }
9215
578d3f19 9216 lhs = code->expr1;
c94189f2 9217 rhs = code->expr2;
9218
9ba02d19 9219 if (rhs->is_boz
60e19868 9220 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
080819af 9221 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
60e19868 9222 &code->loc))
9ba02d19 9223 return false;
9224
9225 /* Handle the case of a BOZ literal on the RHS. */
9226 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9227 {
073c96a6 9228 int rc;
8290d53f 9229 if (warn_surprising)
4166acc7 9230 gfc_warning (OPT_Wsurprising,
9231 "BOZ literal at %L is bitwise transferred "
9232 "non-integer symbol %qs", &code->loc,
9ba02d19 9233 lhs->symtree->n.sym->name);
9234
cb989427 9235 if (!gfc_convert_boz (rhs, &lhs->ts))
9236 return false;
073c96a6 9237 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9238 {
9239 if (rc == ARITH_UNDERFLOW)
9240 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9241 ". This check can be disabled with the option "
0d2b3c9c 9242 "%<-fno-range-check%>", &rhs->where);
073c96a6 9243 else if (rc == ARITH_OVERFLOW)
9244 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9245 ". This check can be disabled with the option "
0d2b3c9c 9246 "%<-fno-range-check%>", &rhs->where);
073c96a6 9247 else if (rc == ARITH_NAN)
9248 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9249 ". This check can be disabled with the option "
0d2b3c9c 9250 "%<-fno-range-check%>", &rhs->where);
073c96a6 9251 return false;
9252 }
9ba02d19 9253 }
9254
c94189f2 9255 if (lhs->ts.type == BT_CHARACTER
fed21cc2 9256 && warn_character_truncation)
c94189f2 9257 {
eeebe20b 9258 if (lhs->ts.u.cl != NULL
9259 && lhs->ts.u.cl->length != NULL
9260 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9261 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
c94189f2 9262
9263 if (rhs->expr_type == EXPR_CONSTANT)
9264 rlen = rhs->value.character.length;
9265
eeebe20b 9266 else if (rhs->ts.u.cl != NULL
7d034542 9267 && rhs->ts.u.cl->length != NULL
eeebe20b 9268 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9269 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
c94189f2 9270
9271 if (rlen && llen && rlen > llen)
bf79c656 9272 gfc_warning_now (OPT_Wcharacter_truncation,
9273 "CHARACTER expression will be truncated "
9274 "in assignment (%d/%d) at %L",
9275 llen, rlen, &code->loc);
c94189f2 9276 }
9277
9278 /* Ensure that a vector index expression for the lvalue is evaluated
1acb400a 9279 to a temporary if the lvalue symbol is referenced in it. */
c94189f2 9280 if (lhs->rank)
9281 {
9282 for (ref = lhs->ref; ref; ref= ref->next)
9283 if (ref->type == REF_ARRAY)
9284 {
9285 for (n = 0; n < ref->u.ar.dimen; n++)
1acb400a 9286 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
fd6481cf 9287 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9288 ref->u.ar.start[n]))
c94189f2 9289 ref->u.ar.start[n]
9290 = gfc_get_parentheses (ref->u.ar.start[n]);
9291 }
9292 }
9293
9294 if (gfc_pure (NULL))
9295 {
c94189f2 9296 if (lhs->ts.type == BT_DERIVED
9297 && lhs->expr_type == EXPR_VARIABLE
eeebe20b 9298 && lhs->ts.u.derived->attr.pointer_comp
895e6dfa 9299 && rhs->expr_type == EXPR_VARIABLE
e97ac7c0 9300 && (gfc_impure_variable (rhs->symtree->n.sym)
9301 || gfc_is_coindexed (rhs)))
9302 {
9303 /* F2008, C1283. */
9304 if (gfc_is_coindexed (rhs))
9305 gfc_error ("Coindexed expression at %L is assigned to "
9306 "a derived type variable with a POINTER "
9307 "component in a PURE procedure",
9308 &rhs->where);
9309 else
9310 gfc_error ("The impure variable at %L is assigned to "
9311 "a derived type variable with a POINTER "
9312 "component in a PURE procedure (12.6)",
9313 &rhs->where);
9314 return rval;
9315 }
9316
9317 /* Fortran 2008, C1283. */
9318 if (gfc_is_coindexed (lhs))
c94189f2 9319 {
e97ac7c0 9320 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9321 "procedure", &rhs->where);
c94189f2 9322 return rval;
9323 }
9324 }
9325
8b0a2e85 9326 if (gfc_implicit_pure (NULL))
9327 {
9328 if (lhs->expr_type == EXPR_VARIABLE
9329 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9330 && lhs->symtree->n.sym->ns != gfc_current_ns)
57cb78c6 9331 gfc_unset_implicit_pure (NULL);
8b0a2e85 9332
9333 if (lhs->ts.type == BT_DERIVED
9334 && lhs->expr_type == EXPR_VARIABLE
9335 && lhs->ts.u.derived->attr.pointer_comp
9336 && rhs->expr_type == EXPR_VARIABLE
9337 && (gfc_impure_variable (rhs->symtree->n.sym)
9338 || gfc_is_coindexed (rhs)))
57cb78c6 9339 gfc_unset_implicit_pure (NULL);
8b0a2e85 9340
9341 /* Fortran 2008, C1283. */
9342 if (gfc_is_coindexed (lhs))
57cb78c6 9343 gfc_unset_implicit_pure (NULL);
8b0a2e85 9344 }
9345
94ce0fae 9346 /* F2008, 7.2.1.2. */
9347 attr = gfc_expr_attr (lhs);
9348 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9349 {
9350 if (attr.codimension)
9351 {
9352 gfc_error ("Assignment to polymorphic coarray at %L is not "
9353 "permitted", &lhs->where);
9354 return false;
9355 }
9356 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9357 "polymorphic variable at %L", &lhs->where))
9358 return false;
eb106faf 9359 if (!flag_realloc_lhs)
94ce0fae 9360 {
9361 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
0d2b3c9c 9362 "requires %<-frealloc-lhs%>", &lhs->where);
94ce0fae 9363 return false;
9364 }
9365 /* See PR 43366. */
9366 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9367 "is not yet supported", &lhs->where);
9368 return false;
9369 }
9370 else if (lhs->ts.type == BT_CLASS)
0ed65c4e 9371 {
94ce0fae 9372 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9373 "assignment at %L - check that there is a matching specific "
9374 "subroutine for '=' operator", &lhs->where);
0ed65c4e 9375 return false;
9376 }
9377
8879941c 9378 bool lhs_coindexed = gfc_is_coindexed (lhs);
9379
e97ac7c0 9380 /* F2008, Section 7.2.1.2. */
8879941c 9381 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
e97ac7c0 9382 {
b0b557ae 9383 gfc_error ("Coindexed variable must not have an allocatable ultimate "
e97ac7c0 9384 "component in assignment at %L", &lhs->where);
9385 return false;
9386 }
9387
c94189f2 9388 gfc_check_assign (lhs, rhs, 1);
8879941c 9389
080819af 9390 /* Assign the 'data' of a class object to a derived type. */
9391 if (lhs->ts.type == BT_DERIVED
9392 && rhs->ts.type == BT_CLASS)
9393 gfc_add_data_component (rhs);
9394
5f4a118e 9395 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9396 Additionally, insert this code when the RHS is a CAF as we then use the
9397 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
a10fb10a 9398 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9399 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9400 path. */
4fe73152 9401 if (flag_coarray == GFC_FCOARRAY_LIB
5f4a118e 9402 && (lhs_coindexed
9403 || (code->expr2->expr_type == EXPR_FUNCTION
9404 && code->expr2->value.function.isym
9405 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
a10fb10a 9406 && (code->expr1->rank == 0 || code->expr2->rank != 0)
5f4a118e 9407 && !gfc_expr_attr (rhs).allocatable
9408 && !gfc_has_vector_subscript (rhs))))
9409 {
9410 if (code->expr2->expr_type == EXPR_FUNCTION
9411 && code->expr2->value.function.isym
9412 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9413 remove_caf_get_intrinsic (code->expr2);
8879941c 9414 code->op = EXEC_CALL;
9415 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9416 code->resolved_sym = code->symtree->n.sym;
9417 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9418 code->resolved_sym->attr.intrinsic = 1;
9419 code->resolved_sym->attr.subroutine = 1;
9420 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9421 gfc_commit_symbol (code->resolved_sym);
9422 code->ext.actual = gfc_get_actual_arglist ();
9423 code->ext.actual->expr = lhs;
9424 code->ext.actual->next = gfc_get_actual_arglist ();
9425 code->ext.actual->next->expr = rhs;
9426 code->expr1 = NULL;
9427 code->expr2 = NULL;
9428 }
9429
c94189f2 9430 return false;
9431}
9432
6a7084d7 9433
d6463863 9434/* Add a component reference onto an expression. */
9435
9436static void
9437add_comp_ref (gfc_expr *e, gfc_component *c)
9438{
9439 gfc_ref **ref;
9440 ref = &(e->ref);
9441 while (*ref)
9442 ref = &((*ref)->next);
9443 *ref = gfc_get_ref ();
9444 (*ref)->type = REF_COMPONENT;
9445 (*ref)->u.c.sym = e->ts.u.derived;
9446 (*ref)->u.c.component = c;
9447 e->ts = c->ts;
9448
9449 /* Add a full array ref, as necessary. */
9450 if (c->as)
9451 {
9452 gfc_add_full_array_ref (e, c->as);
9453 e->rank = c->as->rank;
9454 }
9455}
9456
9457
9458/* Build an assignment. Keep the argument 'op' for future use, so that
9459 pointer assignments can be made. */
9460
9461static gfc_code *
9462build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9463 gfc_component *comp1, gfc_component *comp2, locus loc)
9464{
9465 gfc_code *this_code;
9466
f1ab83c6 9467 this_code = gfc_get_code (op);
d6463863 9468 this_code->next = NULL;
9469 this_code->expr1 = gfc_copy_expr (expr1);
9470 this_code->expr2 = gfc_copy_expr (expr2);
9471 this_code->loc = loc;
9472 if (comp1 && comp2)
9473 {
9474 add_comp_ref (this_code->expr1, comp1);
9475 add_comp_ref (this_code->expr2, comp2);
9476 }
9477
9478 return this_code;
9479}
9480
9481
9482/* Makes a temporary variable expression based on the characteristics of
9483 a given variable expression. */
9484
9485static gfc_expr*
9486get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9487{
9488 static int serial = 0;
9489 char name[GFC_MAX_SYMBOL_LEN];
9490 gfc_symtree *tmp;
9491 gfc_array_spec *as;
9492 gfc_array_ref *aref;
9493 gfc_ref *ref;
9494
fa003ed3 9495 sprintf (name, GFC_PREFIX("DA%d"), serial++);
d6463863 9496 gfc_get_sym_tree (name, ns, &tmp, false);
9497 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9498
9499 as = NULL;
9500 ref = NULL;
9501 aref = NULL;
9502
9503 /* This function could be expanded to support other expression type
9504 but this is not needed here. */
9505 gcc_assert (e->expr_type == EXPR_VARIABLE);
9506
9507 /* Obtain the arrayspec for the temporary. */
9508 if (e->rank)
9509 {
9510 aref = gfc_find_array_ref (e);
9511 if (e->expr_type == EXPR_VARIABLE
9512 && e->symtree->n.sym->as == aref->as)
9513 as = aref->as;
9514 else
9515 {
9516 for (ref = e->ref; ref; ref = ref->next)
9517 if (ref->type == REF_COMPONENT
9518 && ref->u.c.component->as == aref->as)
9519 {
9520 as = aref->as;
9521 break;
9522 }
9523 }
9524 }
9525
9526 /* Add the attributes and the arrayspec to the temporary. */
9527 tmp->n.sym->attr = gfc_expr_attr (e);
0e1d2bb3 9528 tmp->n.sym->attr.function = 0;
9529 tmp->n.sym->attr.result = 0;
9530 tmp->n.sym->attr.flavor = FL_VARIABLE;
9531
d6463863 9532 if (as)
9533 {
9534 tmp->n.sym->as = gfc_copy_array_spec (as);
9535 if (!ref)
9536 ref = e->ref;
9537 if (as->type == AS_DEFERRED)
9538 tmp->n.sym->attr.allocatable = 1;
9539 }
9540 else
9541 tmp->n.sym->attr.dimension = 0;
9542
9543 gfc_set_sym_referenced (tmp->n.sym);
d989a9cc 9544 gfc_commit_symbol (tmp->n.sym);
d6463863 9545 e = gfc_lval_expr_from_sym (tmp->n.sym);
9546
9547 /* Should the lhs be a section, use its array ref for the
9548 temporary expression. */
9549 if (aref && aref->type != AR_FULL)
9550 {
9551 gfc_free_ref_list (e->ref);
9552 e->ref = gfc_copy_ref (ref);
9553 }
9554 return e;
9555}
9556
9557
9558/* Add one line of code to the code chain, making sure that 'head' and
9559 'tail' are appropriately updated. */
9560
9561static void
9562add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9563{
9564 gcc_assert (this_code);
9565 if (*head == NULL)
9566 *head = *tail = *this_code;
9567 else
9568 *tail = gfc_append_code (*tail, *this_code);
9569 *this_code = NULL;
9570}
9571
9572
9573/* Counts the potential number of part array references that would
9574 result from resolution of typebound defined assignments. */
9575
9576static int
9577nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9578{
9579 gfc_component *c;
9580 int c_depth = 0, t_depth;
9581
9582 for (c= derived->components; c; c = c->next)
9583 {
9584 if ((c->ts.type != BT_DERIVED
9585 || c->attr.pointer
9586 || c->attr.allocatable
9587 || c->attr.proc_pointer_comp
9588 || c->attr.class_pointer
9589 || c->attr.proc_pointer)
9590 && !c->attr.defined_assign_comp)
9591 continue;
9592
9593 if (c->as && c_depth == 0)
9594 c_depth = 1;
9595
9596 if (c->ts.u.derived->attr.defined_assign_comp)
9597 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9598 c->as ? 1 : 0);
9599 else
9600 t_depth = 0;
9601
9602 c_depth = t_depth > c_depth ? t_depth : c_depth;
9603 }
9604 return depth + c_depth;
9605}
9606
9607
9608/* Implement 7.2.1.3 of the F08 standard:
9609 "An intrinsic assignment where the variable is of derived type is
9610 performed as if each component of the variable were assigned from the
9611 corresponding component of expr using pointer assignment (7.2.2) for
9612 each pointer component, defined assignment for each nonpointer
9613 nonallocatable component of a type that has a type-bound defined
9614 assignment consistent with the component, intrinsic assignment for
9615 each other nonpointer nonallocatable component, ..."
9616
9617 The pointer assignments are taken care of by the intrinsic
9618 assignment of the structure itself. This function recursively adds
9619 defined assignments where required. The recursion is accomplished
c3f3b68d 9620 by calling gfc_resolve_code.
d6463863 9621
9622 When the lhs in a defined assignment has intent INOUT, we need a
9623 temporary for the lhs. In pseudo-code:
9624
9625 ! Only call function lhs once.
9626 if (lhs is not a constant or an variable)
9627 temp_x = expr2
9628 expr2 => temp_x
9629 ! Do the intrinsic assignment
9630 expr1 = expr2
9631 ! Now do the defined assignments
9632 do over components with typebound defined assignment [%cmp]
9633 #if one component's assignment procedure is INOUT
9634 t1 = expr1
9635 #if expr2 non-variable
9636 temp_x = expr2
9637 expr2 => temp_x
9638 # endif
9639 expr1 = expr2
9640 # for each cmp
9641 t1%cmp {defined=} expr2%cmp
9642 expr1%cmp = t1%cmp
9643 #else
9644 expr1 = expr2
9645
9646 # for each cmp
9647 expr1%cmp {defined=} expr2%cmp
9648 #endif
9649 */
9650
9651/* The temporary assignments have to be put on top of the additional
9652 code to avoid the result being changed by the intrinsic assignment.
9653 */
9654static int component_assignment_level = 0;
9655static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9656
9657static void
9658generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9659{
9660 gfc_component *comp1, *comp2;
9661 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9662 gfc_expr *t1;
9663 int error_count, depth;
9664
9665 gfc_get_errors (NULL, &error_count);
9666
9667 /* Filter out continuing processing after an error. */
9668 if (error_count
9669 || (*code)->expr1->ts.type != BT_DERIVED
9670 || (*code)->expr2->ts.type != BT_DERIVED)
9671 return;
9672
9673 /* TODO: Handle more than one part array reference in assignments. */
9674 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9675 (*code)->expr1->rank ? 1 : 0);
9676 if (depth > 1)
9677 {
37a09d86 9678 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
d6463863 9679 "done because multiple part array references would "
9680 "occur in intermediate expressions.", &(*code)->loc);
9681 return;
9682 }
9683
9684 component_assignment_level++;
9685
9686 /* Create a temporary so that functions get called only once. */
9687 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9688 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9689 {
9690 gfc_expr *tmp_expr;
9691
9692 /* Assign the rhs to the temporary. */
9693 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9694 this_code = build_assignment (EXEC_ASSIGN,
9695 tmp_expr, (*code)->expr2,
9696 NULL, NULL, (*code)->loc);
9697 /* Add the code and substitute the rhs expression. */
9698 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9699 gfc_free_expr ((*code)->expr2);
9700 (*code)->expr2 = tmp_expr;
9701 }
9702
9703 /* Do the intrinsic assignment. This is not needed if the lhs is one
9704 of the temporaries generated here, since the intrinsic assignment
9705 to the final result already does this. */
9706 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9707 {
9708 this_code = build_assignment (EXEC_ASSIGN,
9709 (*code)->expr1, (*code)->expr2,
9710 NULL, NULL, (*code)->loc);
9711 add_code_to_chain (&this_code, &head, &tail);
9712 }
9713
9714 comp1 = (*code)->expr1->ts.u.derived->components;
9715 comp2 = (*code)->expr2->ts.u.derived->components;
9716
9717 t1 = NULL;
9718 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9719 {
9720 bool inout = false;
9721
9722 /* The intrinsic assignment does the right thing for pointers
9723 of all kinds and allocatable components. */
9724 if (comp1->ts.type != BT_DERIVED
9725 || comp1->attr.pointer
9726 || comp1->attr.allocatable
9727 || comp1->attr.proc_pointer_comp
9728 || comp1->attr.class_pointer
9729 || comp1->attr.proc_pointer)
9730 continue;
9731
9732 /* Make an assigment for this component. */
d6463863 9733 this_code = build_assignment (EXEC_ASSIGN,
9734 (*code)->expr1, (*code)->expr2,
9735 comp1, comp2, (*code)->loc);
9736
9737 /* Convert the assignment if there is a defined assignment for
c3f3b68d 9738 this type. Otherwise, using the call from gfc_resolve_code,
d6463863 9739 recurse into its components. */
c3f3b68d 9740 gfc_resolve_code (this_code, ns);
d6463863 9741
9742 if (this_code->op == EXEC_ASSIGN_CALL)
9743 {
6777213b 9744 gfc_formal_arglist *dummy_args;
d6463863 9745 gfc_symbol *rsym;
9746 /* Check that there is a typebound defined assignment. If not,
9747 then this must be a module defined assignment. We cannot
9748 use the defined_assign_comp attribute here because it must
9749 be this derived type that has the defined assignment and not
9750 a parent type. */
9751 if (!(comp1->ts.u.derived->f2k_derived
9752 && comp1->ts.u.derived->f2k_derived
9753 ->tb_op[INTRINSIC_ASSIGN]))
9754 {
9755 gfc_free_statements (this_code);
9756 this_code = NULL;
9757 continue;
9758 }
9759
9760 /* If the first argument of the subroutine has intent INOUT
9761 a temporary must be generated and used instead. */
9762 rsym = this_code->resolved_sym;
6777213b 9763 dummy_args = gfc_sym_get_dummy_args (rsym);
9764 if (dummy_args
9765 && dummy_args->sym->attr.intent == INTENT_INOUT)
d6463863 9766 {
9767 gfc_code *temp_code;
9768 inout = true;
9769
9770 /* Build the temporary required for the assignment and put
9771 it at the head of the generated code. */
9772 if (!t1)
9773 {
9774 t1 = get_temp_from_expr ((*code)->expr1, ns);
9775 temp_code = build_assignment (EXEC_ASSIGN,
9776 t1, (*code)->expr1,
9777 NULL, NULL, (*code)->loc);
991e1cc6 9778
e50c06f2 9779 /* For allocatable LHS, check whether it is allocated. Note
9780 that allocatable components with defined assignment are
9781 not yet support. See PR 57696. */
9782 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
991e1cc6 9783 {
9784 gfc_code *block;
e50c06f2 9785 gfc_expr *e =
9786 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
991e1cc6 9787 block = gfc_get_code (EXEC_IF);
9788 block->block = gfc_get_code (EXEC_IF);
9789 block->block->expr1
9790 = gfc_build_intrinsic_call (ns,
e50c06f2 9791 GFC_ISYM_ALLOCATED, "allocated",
9792 (*code)->loc, 1, e);
991e1cc6 9793 block->block->next = temp_code;
9794 temp_code = block;
9795 }
d6463863 9796 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9797 }
9798
9799 /* Replace the first actual arg with the component of the
9800 temporary. */
9801 gfc_free_expr (this_code->ext.actual->expr);
9802 this_code->ext.actual->expr = gfc_copy_expr (t1);
9803 add_comp_ref (this_code->ext.actual->expr, comp1);
991e1cc6 9804
e50c06f2 9805 /* If the LHS variable is allocatable and wasn't allocated and
9806 the temporary is allocatable, pointer assign the address of
9807 the freshly allocated LHS to the temporary. */
9808 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9809 && gfc_expr_attr ((*code)->expr1).allocatable)
991e1cc6 9810 {
9811 gfc_code *block;
a6a783dd 9812 gfc_expr *cond;
9813
9814 cond = gfc_get_expr ();
991e1cc6 9815 cond->ts.type = BT_LOGICAL;
9816 cond->ts.kind = gfc_default_logical_kind;
9817 cond->expr_type = EXPR_OP;
9818 cond->where = (*code)->loc;
9819 cond->value.op.op = INTRINSIC_NOT;
9820 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
e50c06f2 9821 GFC_ISYM_ALLOCATED, "allocated",
9822 (*code)->loc, 1, gfc_copy_expr (t1));
991e1cc6 9823 block = gfc_get_code (EXEC_IF);
9824 block->block = gfc_get_code (EXEC_IF);
9825 block->block->expr1 = cond;
9826 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9827 t1, (*code)->expr1,
9828 NULL, NULL, (*code)->loc);
9829 add_code_to_chain (&block, &head, &tail);
9830 }
d6463863 9831 }
a6a783dd 9832 }
d6463863 9833 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9834 {
9835 /* Don't add intrinsic assignments since they are already
9836 effected by the intrinsic assignment of the structure. */
9837 gfc_free_statements (this_code);
9838 this_code = NULL;
9839 continue;
9840 }
9841
9842 add_code_to_chain (&this_code, &head, &tail);
9843
9844 if (t1 && inout)
9845 {
9846 /* Transfer the value to the final result. */
9847 this_code = build_assignment (EXEC_ASSIGN,
9848 (*code)->expr1, t1,
9849 comp1, comp2, (*code)->loc);
9850 add_code_to_chain (&this_code, &head, &tail);
9851 }
9852 }
9853
d6463863 9854 /* Put the temporary assignments at the top of the generated code. */
9855 if (tmp_head && component_assignment_level == 1)
9856 {
9857 gfc_append_code (tmp_head, head);
9858 head = tmp_head;
9859 tmp_head = tmp_tail = NULL;
9860 }
9861
a6a783dd 9862 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9863 // not accidentally deallocated. Hence, nullify t1.
9864 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9865 && gfc_expr_attr ((*code)->expr1).allocatable)
9866 {
9867 gfc_code *block;
9868 gfc_expr *cond;
9869 gfc_expr *e;
9870
9871 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9872 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9873 (*code)->loc, 2, gfc_copy_expr (t1), e);
9874 block = gfc_get_code (EXEC_IF);
9875 block->block = gfc_get_code (EXEC_IF);
9876 block->block->expr1 = cond;
9877 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9878 t1, gfc_get_null_expr (&(*code)->loc),
9879 NULL, NULL, (*code)->loc);
9880 gfc_append_code (tail, block);
9881 tail = block;
9882 }
9883
d6463863 9884 /* Now attach the remaining code chain to the input code. Step on
9885 to the end of the new code since resolution is complete. */
9886 gcc_assert ((*code)->op == EXEC_ASSIGN);
9887 tail->next = (*code)->next;
9888 /* Overwrite 'code' because this would place the intrinsic assignment
9889 before the temporary for the lhs is created. */
9890 gfc_free_expr ((*code)->expr1);
9891 gfc_free_expr ((*code)->expr2);
9892 **code = *head;
a6a783dd 9893 if (head != tail)
9894 free (head);
d6463863 9895 *code = tail;
9896
9897 component_assignment_level--;
9898}
9899
9900
4ee9c684 9901/* Given a block of code, recursively resolve everything pointed to by this
9902 code block. */
9903
c3f3b68d 9904void
9905gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
4ee9c684 9906{
764f1175 9907 int omp_workshare_save;
55ea8666 9908 int forall_save, do_concurrent_save;
4ee9c684 9909 code_stack frame;
60e19868 9910 bool t;
4ee9c684 9911
9912 frame.prev = cs_base;
9913 frame.head = code;
9914 cs_base = &frame;
9915
8581350b 9916 find_reachable_labels (code);
82efdb2e 9917
4ee9c684 9918 for (; code; code = code->next)
9919 {
9920 frame.current = code;
94a286ff 9921 forall_save = forall_flag;
8b8cc022 9922 do_concurrent_save = gfc_do_concurrent_flag;
4ee9c684 9923
9924 if (code->op == EXEC_FORALL)
9925 {
4ee9c684 9926 forall_flag = 1;
764f1175 9927 gfc_resolve_forall (code, ns, forall_save);
94a286ff 9928 forall_flag = 2;
764f1175 9929 }
9930 else if (code->block)
9931 {
9932 omp_workshare_save = -1;
9933 switch (code->op)
9934 {
9935 case EXEC_OMP_PARALLEL_WORKSHARE:
9936 omp_workshare_save = omp_workshare_flag;
9937 omp_workshare_flag = 1;
9938 gfc_resolve_omp_parallel_blocks (code, ns);
9939 break;
9940 case EXEC_OMP_PARALLEL:
9941 case EXEC_OMP_PARALLEL_DO:
15b28553 9942 case EXEC_OMP_PARALLEL_DO_SIMD:
764f1175 9943 case EXEC_OMP_PARALLEL_SECTIONS:
691447ab 9944 case EXEC_OMP_TARGET_TEAMS:
9945 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9946 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9947 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9948 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
fd6481cf 9949 case EXEC_OMP_TASK:
691447ab 9950 case EXEC_OMP_TEAMS:
9951 case EXEC_OMP_TEAMS_DISTRIBUTE:
9952 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9953 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9954 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
764f1175 9955 omp_workshare_save = omp_workshare_flag;
9956 omp_workshare_flag = 0;
9957 gfc_resolve_omp_parallel_blocks (code, ns);
9958 break;
691447ab 9959 case EXEC_OMP_DISTRIBUTE:
9960 case EXEC_OMP_DISTRIBUTE_SIMD:
764f1175 9961 case EXEC_OMP_DO:
15b28553 9962 case EXEC_OMP_DO_SIMD:
9963 case EXEC_OMP_SIMD:
764f1175 9964 gfc_resolve_omp_do_blocks (code, ns);
9965 break;
c4cec8b1 9966 case EXEC_SELECT_TYPE:
7725f40e 9967 /* Blocks are handled in resolve_select_type because we have
9968 to transform the SELECT TYPE into ASSOCIATE first. */
c4cec8b1 9969 break;
55ea8666 9970 case EXEC_DO_CONCURRENT:
8b8cc022 9971 gfc_do_concurrent_flag = 1;
55ea8666 9972 gfc_resolve_blocks (code->block, ns);
8b8cc022 9973 gfc_do_concurrent_flag = 2;
55ea8666 9974 break;
764f1175 9975 case EXEC_OMP_WORKSHARE:
9976 omp_workshare_save = omp_workshare_flag;
9977 omp_workshare_flag = 1;
df084314 9978 /* FALL THROUGH */
764f1175 9979 default:
9980 gfc_resolve_blocks (code->block, ns);
9981 break;
9982 }
4ee9c684 9983
764f1175 9984 if (omp_workshare_save != -1)
9985 omp_workshare_flag = omp_workshare_save;
9986 }
4ee9c684 9987
60e19868 9988 t = true;
64e93293 9989 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
578d3f19 9990 t = gfc_resolve_expr (code->expr1);
94a286ff 9991 forall_flag = forall_save;
8b8cc022 9992 gfc_do_concurrent_flag = do_concurrent_save;
94a286ff 9993
60e19868 9994 if (!gfc_resolve_expr (code->expr2))
9995 t = false;
4ee9c684 9996
af675571 9997 if (code->op == EXEC_ALLOCATE
60e19868 9998 && !gfc_resolve_expr (code->expr3))
9999 t = false;
af675571 10000
4ee9c684 10001 switch (code->op)
10002 {
10003 case EXEC_NOP:
8581350b 10004 case EXEC_END_BLOCK:
045b8fbb 10005 case EXEC_END_NESTED_BLOCK:
4ee9c684 10006 case EXEC_CYCLE:
4ee9c684 10007 case EXEC_PAUSE:
10008 case EXEC_STOP:
c6cd3066 10009 case EXEC_ERROR_STOP:
4ee9c684 10010 case EXEC_EXIT:
10011 case EXEC_CONTINUE:
10012 case EXEC_DT_END:
7d034542 10013 case EXEC_ASSIGN_CALL:
498b946e 10014 break;
10015
c6cd3066 10016 case EXEC_CRITICAL:
498b946e 10017 resolve_critical (code);
c6cd3066 10018 break;
10019
10020 case EXEC_SYNC_ALL:
10021 case EXEC_SYNC_IMAGES:
10022 case EXEC_SYNC_MEMORY:
10023 resolve_sync (code);
c6b395dd 10024 break;
10025
3f73d66e 10026 case EXEC_LOCK:
10027 case EXEC_UNLOCK:
10028 resolve_lock_unlock (code);
10029 break;
10030
1b716045 10031 case EXEC_ENTRY:
c6b395dd 10032 /* Keep track of which entry we are up to. */
10033 current_entry_id = code->ext.entry->id;
4ee9c684 10034 break;
10035
10036 case EXEC_WHERE:
10037 resolve_where (code, NULL);
10038 break;
10039
10040 case EXEC_GOTO:
578d3f19 10041 if (code->expr1 != NULL)
836fa030 10042 {
578d3f19 10043 if (code->expr1->ts.type != BT_INTEGER)
1bcc6eb8 10044 gfc_error ("ASSIGNED GOTO statement at %L requires an "
578d3f19 10045 "INTEGER variable", &code->expr1->where);
10046 else if (code->expr1->symtree->n.sym->attr.assign != 1)
716da296 10047 gfc_error ("Variable %qs has not been assigned a target "
578d3f19 10048 "label at %L", code->expr1->symtree->n.sym->name,
10049 &code->expr1->where);
836fa030 10050 }
10051 else
13b33c16 10052 resolve_branch (code->label1, code);
4ee9c684 10053 break;
10054
10055 case EXEC_RETURN:
578d3f19 10056 if (code->expr1 != NULL
10057 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
947f2aa1 10058 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
578d3f19 10059 "INTEGER return specifier", &code->expr1->where);
4ee9c684 10060 break;
10061
b9cd8c56 10062 case EXEC_INIT_ASSIGN:
9286e713 10063 case EXEC_END_PROCEDURE:
b9cd8c56 10064 break;
10065
4ee9c684 10066 case EXEC_ASSIGN:
60e19868 10067 if (!t)
4ee9c684 10068 break;
10069
5f4a118e 10070 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
293d72e0 10071 the LHS. */
8879941c 10072 if (code->expr1->expr_type == EXPR_FUNCTION
10073 && code->expr1->value.function.isym
10074 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10075 remove_caf_get_intrinsic (code->expr1);
10076
080819af 10077 if (!gfc_check_vardef_context (code->expr1, false, false, false,
60e19868 10078 _("assignment")))
7725f40e 10079 break;
10080
c94189f2 10081 if (resolve_ordinary_assign (code, ns))
56b411d9 10082 {
10083 if (code->op == EXEC_COMPCALL)
10084 goto compcall;
10085 else
10086 goto call;
10087 }
d6463863 10088
10089 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
8879941c 10090 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
d6463863 10091 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10092 generate_component_assignments (&code, ns);
10093
4ee9c684 10094 break;
10095
10096 case EXEC_LABEL_ASSIGN:
13b33c16 10097 if (code->label1->defined == ST_LABEL_UNKNOWN)
1bcc6eb8 10098 gfc_error ("Label %d referenced at %L is never defined",
13b33c16 10099 code->label1->value, &code->label1->where);
60e19868 10100 if (t
578d3f19 10101 && (code->expr1->expr_type != EXPR_VARIABLE
10102 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10103 || code->expr1->symtree->n.sym->ts.kind
1bcc6eb8 10104 != gfc_default_integer_kind
578d3f19 10105 || code->expr1->symtree->n.sym->as != NULL))
f6c25b7d 10106 gfc_error ("ASSIGN statement at %L requires a scalar "
578d3f19 10107 "default INTEGER variable", &code->expr1->where);
4ee9c684 10108 break;
10109
10110 case EXEC_POINTER_ASSIGN:
7725f40e 10111 {
10112 gfc_expr* e;
4ee9c684 10113
60e19868 10114 if (!t)
7725f40e 10115 break;
10116
10117 /* This is both a variable definition and pointer assignment
10118 context, so check both of them. For rank remapping, a final
10119 array ref may be present on the LHS and fool gfc_expr_attr
10120 used in gfc_check_vardef_context. Remove it. */
10121 e = remove_last_array_ref (code->expr1);
091c5975 10122 t = gfc_check_vardef_context (e, true, false, false,
c135f087 10123 _("pointer assignment"));
60e19868 10124 if (t)
091c5975 10125 t = gfc_check_vardef_context (e, false, false, false,
c135f087 10126 _("pointer assignment"));
7725f40e 10127 gfc_free_expr (e);
60e19868 10128 if (!t)
7725f40e 10129 break;
10130
10131 gfc_check_pointer_assign (code->expr1, code->expr2);
10132 break;
10133 }
4ee9c684 10134
10135 case EXEC_ARITHMETIC_IF:
60e19868 10136 if (t
578d3f19 10137 && code->expr1->ts.type != BT_INTEGER
10138 && code->expr1->ts.type != BT_REAL)
4ee9c684 10139 gfc_error ("Arithmetic IF statement at %L requires a numeric "
578d3f19 10140 "expression", &code->expr1->where);
4ee9c684 10141
13b33c16 10142 resolve_branch (code->label1, code);
4ee9c684 10143 resolve_branch (code->label2, code);
10144 resolve_branch (code->label3, code);
10145 break;
10146
10147 case EXEC_IF:
60e19868 10148 if (t && code->expr1 != NULL
578d3f19 10149 && (code->expr1->ts.type != BT_LOGICAL
10150 || code->expr1->rank != 0))
4ee9c684 10151 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
578d3f19 10152 &code->expr1->where);
4ee9c684 10153 break;
10154
10155 case EXEC_CALL:
10156 call:
10157 resolve_call (code);
10158 break;
10159
930fe1de 10160 case EXEC_COMPCALL:
56b411d9 10161 compcall:
ae925cc0 10162 resolve_typebound_subroutine (code);
930fe1de 10163 break;
10164
64e93293 10165 case EXEC_CALL_PPC:
6a7084d7 10166 resolve_ppc_call (code);
64e93293 10167 break;
10168
4ee9c684 10169 case EXEC_SELECT:
10170 /* Select is complicated. Also, a SELECT construct could be
10171 a transformed computed GOTO. */
c58ba4b2 10172 resolve_select (code, false);
4ee9c684 10173 break;
10174
1de1b1a9 10175 case EXEC_SELECT_TYPE:
7725f40e 10176 resolve_select_type (code, ns);
1de1b1a9 10177 break;
10178
6a7084d7 10179 case EXEC_BLOCK:
7b82374f 10180 resolve_block_construct (code);
6a7084d7 10181 break;
10182
4ee9c684 10183 case EXEC_DO:
10184 if (code->ext.iterator != NULL)
764f1175 10185 {
10186 gfc_iterator *iter = code->ext.iterator;
60e19868 10187 if (gfc_resolve_iterator (iter, true, false))
764f1175 10188 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10189 }
4ee9c684 10190 break;
10191
10192 case EXEC_DO_WHILE:
578d3f19 10193 if (code->expr1 == NULL)
c3f3b68d 10194 gfc_internal_error ("gfc_resolve_code(): No expression on "
10195 "DO WHILE");
60e19868 10196 if (t
578d3f19 10197 && (code->expr1->rank != 0
10198 || code->expr1->ts.type != BT_LOGICAL))
4ee9c684 10199 gfc_error ("Exit condition of DO WHILE loop at %L must be "
578d3f19 10200 "a scalar LOGICAL expression", &code->expr1->where);
4ee9c684 10201 break;
10202
10203 case EXEC_ALLOCATE:
60e19868 10204 if (t)
a9e7fd6a 10205 resolve_allocate_deallocate (code, "ALLOCATE");
4ee9c684 10206
10207 break;
10208
10209 case EXEC_DEALLOCATE:
60e19868 10210 if (t)
a9e7fd6a 10211 resolve_allocate_deallocate (code, "DEALLOCATE");
4ee9c684 10212
10213 break;
10214
10215 case EXEC_OPEN:
60e19868 10216 if (!gfc_resolve_open (code->ext.open))
4ee9c684 10217 break;
10218
10219 resolve_branch (code->ext.open->err, code);
10220 break;
10221
10222 case EXEC_CLOSE:
60e19868 10223 if (!gfc_resolve_close (code->ext.close))
4ee9c684 10224 break;
10225
10226 resolve_branch (code->ext.close->err, code);
10227 break;
10228
10229 case EXEC_BACKSPACE:
10230 case EXEC_ENDFILE:
10231 case EXEC_REWIND:
6c306f90 10232 case EXEC_FLUSH:
60e19868 10233 if (!gfc_resolve_filepos (code->ext.filepos))
4ee9c684 10234 break;
10235
10236 resolve_branch (code->ext.filepos->err, code);
10237 break;
10238
10239 case EXEC_INQUIRE:
60e19868 10240 if (!gfc_resolve_inquire (code->ext.inquire))
6799e2f8 10241 break;
10242
10243 resolve_branch (code->ext.inquire->err, code);
10244 break;
10245
10246 case EXEC_IOLENGTH:
22d678e8 10247 gcc_assert (code->ext.inquire != NULL);
60e19868 10248 if (!gfc_resolve_inquire (code->ext.inquire))
4ee9c684 10249 break;
10250
10251 resolve_branch (code->ext.inquire->err, code);
10252 break;
10253
ff6af856 10254 case EXEC_WAIT:
60e19868 10255 if (!gfc_resolve_wait (code->ext.wait))
ff6af856 10256 break;
10257
10258 resolve_branch (code->ext.wait->err, code);
10259 resolve_branch (code->ext.wait->end, code);
10260 resolve_branch (code->ext.wait->eor, code);
10261 break;
10262
4ee9c684 10263 case EXEC_READ:
10264 case EXEC_WRITE:
60e19868 10265 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
4ee9c684 10266 break;
10267
10268 resolve_branch (code->ext.dt->err, code);
10269 resolve_branch (code->ext.dt->end, code);
10270 resolve_branch (code->ext.dt->eor, code);
10271 break;
10272
2978704c 10273 case EXEC_TRANSFER:
10274 resolve_transfer (code);
10275 break;
10276
55ea8666 10277 case EXEC_DO_CONCURRENT:
4ee9c684 10278 case EXEC_FORALL:
10279 resolve_forall_iterators (code->ext.forall_iterator);
10280
7e2dcd7e 10281 if (code->expr1 != NULL
10282 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10283 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
578d3f19 10284 "expression", &code->expr1->where);
4ee9c684 10285 break;
10286
764f1175 10287 case EXEC_OMP_ATOMIC:
10288 case EXEC_OMP_BARRIER:
15b28553 10289 case EXEC_OMP_CANCEL:
10290 case EXEC_OMP_CANCELLATION_POINT:
764f1175 10291 case EXEC_OMP_CRITICAL:
10292 case EXEC_OMP_FLUSH:
691447ab 10293 case EXEC_OMP_DISTRIBUTE:
10294 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10295 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10296 case EXEC_OMP_DISTRIBUTE_SIMD:
764f1175 10297 case EXEC_OMP_DO:
15b28553 10298 case EXEC_OMP_DO_SIMD:
764f1175 10299 case EXEC_OMP_MASTER:
10300 case EXEC_OMP_ORDERED:
10301 case EXEC_OMP_SECTIONS:
15b28553 10302 case EXEC_OMP_SIMD:
764f1175 10303 case EXEC_OMP_SINGLE:
691447ab 10304 case EXEC_OMP_TARGET:
10305 case EXEC_OMP_TARGET_DATA:
10306 case EXEC_OMP_TARGET_TEAMS:
10307 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10308 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10309 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10310 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10311 case EXEC_OMP_TARGET_UPDATE:
10312 case EXEC_OMP_TASK:
15b28553 10313 case EXEC_OMP_TASKGROUP:
fd6481cf 10314 case EXEC_OMP_TASKWAIT:
2169f33b 10315 case EXEC_OMP_TASKYIELD:
691447ab 10316 case EXEC_OMP_TEAMS:
10317 case EXEC_OMP_TEAMS_DISTRIBUTE:
10318 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10319 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10320 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
764f1175 10321 case EXEC_OMP_WORKSHARE:
10322 gfc_resolve_omp_directive (code, ns);
10323 break;
10324
10325 case EXEC_OMP_PARALLEL:
10326 case EXEC_OMP_PARALLEL_DO:
15b28553 10327 case EXEC_OMP_PARALLEL_DO_SIMD:
764f1175 10328 case EXEC_OMP_PARALLEL_SECTIONS:
10329 case EXEC_OMP_PARALLEL_WORKSHARE:
10330 omp_workshare_save = omp_workshare_flag;
10331 omp_workshare_flag = 0;
10332 gfc_resolve_omp_directive (code, ns);
10333 omp_workshare_flag = omp_workshare_save;
10334 break;
10335
4ee9c684 10336 default:
c3f3b68d 10337 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
4ee9c684 10338 }
10339 }
10340
10341 cs_base = frame.prev;
10342}
10343
10344
10345/* Resolve initial values and make sure they are compatible with
10346 the variable. */
10347
10348static void
1bcc6eb8 10349resolve_values (gfc_symbol *sym)
4ee9c684 10350{
60e19868 10351 bool t;
23d075f4 10352
98976ae1 10353 if (sym->value == NULL)
4ee9c684 10354 return;
10355
23d075f4 10356 if (sym->value->expr_type == EXPR_STRUCTURE)
10357 t= resolve_structure_cons (sym->value, 1);
d6463863 10358 else
23d075f4 10359 t = gfc_resolve_expr (sym->value);
10360
60e19868 10361 if (!t)
4ee9c684 10362 return;
10363
16f7554b 10364 gfc_check_assign_symbol (sym, NULL, sym->value);
4ee9c684 10365}
10366
10367
c5d33754 10368/* Verify any BIND(C) derived types in the namespace so we can report errors
10369 for them once, rather than for each variable declared of that type. */
10370
10371static void
10372resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10373{
10374 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10375 && derived_sym->attr.is_bind_c == 1)
10376 verify_bind_c_derived_type (derived_sym);
d6463863 10377
c5d33754 10378 return;
10379}
10380
10381
d6463863 10382/* Verify that any binding labels used in a given namespace do not collide
c8b913ab 10383 with the names or binding labels of any global symbols. Multiple INTERFACE
10384 for the same procedure are permitted. */
c5d33754 10385
10386static void
10387gfc_verify_binding_labels (gfc_symbol *sym)
10388{
c8b913ab 10389 gfc_gsymbol *gsym;
10390 const char *module;
10391
10392 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10393 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10394 return;
10395
10396 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10397
10398 if (sym->module)
10399 module = sym->module;
10400 else if (sym->ns && sym->ns->proc_name
10401 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10402 module = sym->ns->proc_name->name;
10403 else if (sym->ns && sym->ns->parent
10404 && sym->ns && sym->ns->parent->proc_name
10405 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10406 module = sym->ns->parent->proc_name->name;
10407 else
10408 module = NULL;
d6463863 10409
c8b913ab 10410 if (!gsym
10411 || (!gsym->defined
10412 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
c5d33754 10413 {
c8b913ab 10414 if (!gsym)
10415 gsym = gfc_get_gsymbol (sym->binding_label);
10416 gsym->where = sym->declared_at;
10417 gsym->sym_name = sym->name;
10418 gsym->binding_label = sym->binding_label;
c8b913ab 10419 gsym->ns = sym->ns;
10420 gsym->mod_name = module;
10421 if (sym->attr.function)
10422 gsym->type = GSYM_FUNCTION;
10423 else if (sym->attr.subroutine)
10424 gsym->type = GSYM_SUBROUTINE;
10425 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10426 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10427 return;
10428 }
10429
10430 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10431 {
716da296 10432 gfc_error_1 ("Variable %s with binding label %s at %L uses the same global "
c8b913ab 10433 "identifier as entity at %L", sym->name,
10434 sym->binding_label, &sym->declared_at, &gsym->where);
10435 /* Clear the binding label to prevent checking multiple times. */
10436 sym->binding_label = NULL;
c5d33754 10437
c5d33754 10438 }
c8b913ab 10439 else if (sym->attr.flavor == FL_VARIABLE
10440 && (strcmp (module, gsym->mod_name) != 0
10441 || strcmp (sym->name, gsym->sym_name) != 0))
10442 {
10443 /* This can only happen if the variable is defined in a module - if it
10444 isn't the same module, reject it. */
716da296 10445 gfc_error_1 ("Variable %s from module %s with binding label %s at %L uses "
10446 "the same global identifier as entity at %L from module %s",
c8b913ab 10447 sym->name, module, sym->binding_label,
10448 &sym->declared_at, &gsym->where, gsym->mod_name);
10449 sym->binding_label = NULL;
10450 }
10451 else if ((sym->attr.function || sym->attr.subroutine)
10452 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10453 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10454 && sym != gsym->ns->proc_name
a52fbc53 10455 && (module != gsym->mod_name
10456 || strcmp (gsym->sym_name, sym->name) != 0
c8b913ab 10457 || (module && strcmp (module, gsym->mod_name) != 0)))
10458 {
a52fbc53 10459 /* Print an error if the procedure is defined multiple times; we have to
c8b913ab 10460 exclude references to the same procedure via module association or
10461 multiple checks for the same procedure. */
716da296 10462 gfc_error_1 ("Procedure %s with binding label %s at %L uses the same "
c8b913ab 10463 "global identifier as entity at %L", sym->name,
10464 sym->binding_label, &sym->declared_at, &gsym->where);
10465 sym->binding_label = NULL;
10466 }
c5d33754 10467}
10468
10469
693c40a7 10470/* Resolve an index expression. */
10471
60e19868 10472static bool
1bcc6eb8 10473resolve_index_expr (gfc_expr *e)
693c40a7 10474{
60e19868 10475 if (!gfc_resolve_expr (e))
10476 return false;
693c40a7 10477
60e19868 10478 if (!gfc_simplify_expr (e, 0))
10479 return false;
693c40a7 10480
60e19868 10481 if (!gfc_specification_expr (e))
10482 return false;
693c40a7 10483
60e19868 10484 return true;
693c40a7 10485}
10486
3e715c81 10487
ac42ecbd 10488/* Resolve a charlen structure. */
10489
60e19868 10490static bool
ac42ecbd 10491resolve_charlen (gfc_charlen *cl)
10492{
7ad1f5f6 10493 int i, k;
be844014 10494 bool saved_specification_expr;
2fe2caa6 10495
ac42ecbd 10496 if (cl->resolved)
60e19868 10497 return true;
ac42ecbd 10498
10499 cl->resolved = 1;
be844014 10500 saved_specification_expr = specification_expr;
10501 specification_expr = true;
c6b395dd 10502
71f58d94 10503 if (cl->length_from_typespec)
c6b395dd 10504 {
60e19868 10505 if (!gfc_resolve_expr (cl->length))
be844014 10506 {
10507 specification_expr = saved_specification_expr;
60e19868 10508 return false;
be844014 10509 }
71f58d94 10510
60e19868 10511 if (!gfc_simplify_expr (cl->length, 0))
be844014 10512 {
10513 specification_expr = saved_specification_expr;
60e19868 10514 return false;
be844014 10515 }
71f58d94 10516 }
10517 else
10518 {
71f58d94 10519
60e19868 10520 if (!resolve_index_expr (cl->length))
71f58d94 10521 {
be844014 10522 specification_expr = saved_specification_expr;
60e19868 10523 return false;
71f58d94 10524 }
c6b395dd 10525 }
ac42ecbd 10526
2fe2caa6 10527 /* "If the character length parameter value evaluates to a negative
10528 value, the length of character entities declared is zero." */
a6eea7ca 10529 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
2fe2caa6 10530 {
8290d53f 10531 if (warn_surprising)
4166acc7 10532 gfc_warning_now (OPT_Wsurprising,
10533 "CHARACTER variable at %L has negative length %d,"
7c967940 10534 " the length has been set to zero",
10535 &cl->length->where, i);
126387b5 10536 gfc_replace_expr (cl->length,
10537 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
2fe2caa6 10538 }
10539
7ad1f5f6 10540 /* Check that the character length is not too large. */
10541 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10542 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10543 && cl->length->ts.type == BT_INTEGER
10544 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10545 {
10546 gfc_error ("String length at %L is too large", &cl->length->where);
be844014 10547 specification_expr = saved_specification_expr;
60e19868 10548 return false;
7ad1f5f6 10549 }
10550
be844014 10551 specification_expr = saved_specification_expr;
60e19868 10552 return true;
693c40a7 10553}
10554
10555
f6d0e37a 10556/* Test for non-constant shape arrays. */
199bf9f5 10557
10558static bool
10559is_non_constant_shape_array (gfc_symbol *sym)
10560{
10561 gfc_expr *e;
10562 int i;
c6b395dd 10563 bool not_constant;
199bf9f5 10564
c6b395dd 10565 not_constant = false;
199bf9f5 10566 if (sym->as != NULL)
10567 {
10568 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10569 has not been simplified; parameter array references. Do the
10570 simplification now. */
aff518b0 10571 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
199bf9f5 10572 {
10573 e = sym->as->lower[i];
60e19868 10574 if (e && (!resolve_index_expr(e)
1bcc6eb8 10575 || !gfc_is_constant_expr (e)))
c6b395dd 10576 not_constant = true;
199bf9f5 10577 e = sym->as->upper[i];
60e19868 10578 if (e && (!resolve_index_expr(e)
1bcc6eb8 10579 || !gfc_is_constant_expr (e)))
c6b395dd 10580 not_constant = true;
199bf9f5 10581 }
10582 }
c6b395dd 10583 return not_constant;
199bf9f5 10584}
10585
a28eb9a8 10586/* Given a symbol and an initialization expression, add code to initialize
10587 the symbol to the function entry. */
b9cd8c56 10588static void
a28eb9a8 10589build_init_assign (gfc_symbol *sym, gfc_expr *init)
b9cd8c56 10590{
10591 gfc_expr *lval;
b9cd8c56 10592 gfc_code *init_st;
10593 gfc_namespace *ns = sym->ns;
10594
b9cd8c56 10595 /* Search for the function namespace if this is a contained
10596 function without an explicit result. */
10597 if (sym->attr.function && sym == sym->result
1bcc6eb8 10598 && sym->name != sym->ns->proc_name->name)
b9cd8c56 10599 {
10600 ns = ns->contained;
10601 for (;ns; ns = ns->sibling)
10602 if (strcmp (ns->proc_name->name, sym->name) == 0)
10603 break;
10604 }
10605
10606 if (ns == NULL)
10607 {
10608 gfc_free_expr (init);
10609 return;
10610 }
10611
10612 /* Build an l-value expression for the result. */
1e853e89 10613 lval = gfc_lval_expr_from_sym (sym);
b9cd8c56 10614
10615 /* Add the code at scope entry. */
f1ab83c6 10616 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
b9cd8c56 10617 init_st->next = ns->code;
10618 ns->code = init_st;
10619
10620 /* Assign the default initializer to the l-value. */
10621 init_st->loc = sym->declared_at;
578d3f19 10622 init_st->expr1 = lval;
b9cd8c56 10623 init_st->expr2 = init;
10624}
10625
a28eb9a8 10626/* Assign the default initializer to a derived type variable or result. */
10627
10628static void
10629apply_default_init (gfc_symbol *sym)
10630{
10631 gfc_expr *init = NULL;
10632
10633 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10634 return;
10635
eeebe20b 10636 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
a28eb9a8 10637 init = gfc_default_initializer (&sym->ts);
10638
4c33a6fa 10639 if (init == NULL && sym->ts.type != BT_CLASS)
a28eb9a8 10640 return;
10641
10642 build_init_assign (sym, init);
bc9d38fe 10643 sym->attr.referenced = 1;
a28eb9a8 10644}
10645
10646/* Build an initializer for a local integer, real, complex, logical, or
10647 character variable, based on the command line flags finit-local-zero,
d6463863 10648 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
a28eb9a8 10649 null if the symbol should not have a default initialization. */
10650static gfc_expr *
10651build_default_init_expr (gfc_symbol *sym)
10652{
10653 int char_len;
10654 gfc_expr *init_expr;
10655 int i;
a28eb9a8 10656
10657 /* These symbols should never have a default initialization. */
cb44834a 10658 if (sym->attr.allocatable
a28eb9a8 10659 || sym->attr.external
10660 || sym->attr.dummy
10661 || sym->attr.pointer
10662 || sym->attr.in_equivalence
10663 || sym->attr.in_common
10664 || sym->attr.data
10665 || sym->module
10666 || sym->attr.cray_pointee
e8802ff7 10667 || sym->attr.cray_pointer
10668 || sym->assoc)
a28eb9a8 10669 return NULL;
10670
10671 /* Now we'll try to build an initializer expression. */
126387b5 10672 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10673 &sym->declared_at);
10674
a28eb9a8 10675 /* We will only initialize integers, reals, complex, logicals, and
10676 characters, and only if the corresponding command-line flags
10677 were set. Otherwise, we free init_expr and return null. */
10678 switch (sym->ts.type)
d6463863 10679 {
a28eb9a8 10680 case BT_INTEGER:
10681 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
d6463863 10682 mpz_set_si (init_expr->value.integer,
a28eb9a8 10683 gfc_option.flag_init_integer_value);
10684 else
10685 {
10686 gfc_free_expr (init_expr);
10687 init_expr = NULL;
10688 }
10689 break;
10690
10691 case BT_REAL:
4fe73152 10692 switch (flag_init_real)
a28eb9a8 10693 {
2b6bc4f2 10694 case GFC_INIT_REAL_SNAN:
10695 init_expr->is_snan = 1;
10696 /* Fall through. */
a28eb9a8 10697 case GFC_INIT_REAL_NAN:
10698 mpfr_set_nan (init_expr->value.real);
10699 break;
10700
10701 case GFC_INIT_REAL_INF:
10702 mpfr_set_inf (init_expr->value.real, 1);
10703 break;
10704
10705 case GFC_INIT_REAL_NEG_INF:
10706 mpfr_set_inf (init_expr->value.real, -1);
10707 break;
10708
10709 case GFC_INIT_REAL_ZERO:
10710 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10711 break;
10712
10713 default:
10714 gfc_free_expr (init_expr);
10715 init_expr = NULL;
10716 break;
10717 }
10718 break;
d6463863 10719
a28eb9a8 10720 case BT_COMPLEX:
4fe73152 10721 switch (flag_init_real)
a28eb9a8 10722 {
2b6bc4f2 10723 case GFC_INIT_REAL_SNAN:
10724 init_expr->is_snan = 1;
10725 /* Fall through. */
a28eb9a8 10726 case GFC_INIT_REAL_NAN:
f8e9f06c 10727 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10728 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
a28eb9a8 10729 break;
10730
10731 case GFC_INIT_REAL_INF:
f8e9f06c 10732 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10733 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
a28eb9a8 10734 break;
10735
10736 case GFC_INIT_REAL_NEG_INF:
f8e9f06c 10737 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10738 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
a28eb9a8 10739 break;
10740
10741 case GFC_INIT_REAL_ZERO:
f8e9f06c 10742 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
a28eb9a8 10743 break;
10744
10745 default:
10746 gfc_free_expr (init_expr);
10747 init_expr = NULL;
10748 break;
10749 }
10750 break;
d6463863 10751
a28eb9a8 10752 case BT_LOGICAL:
10753 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10754 init_expr->value.logical = 0;
10755 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10756 init_expr->value.logical = 1;
10757 else
10758 {
10759 gfc_free_expr (init_expr);
10760 init_expr = NULL;
10761 }
10762 break;
d6463863 10763
a28eb9a8 10764 case BT_CHARACTER:
d6463863 10765 /* For characters, the length must be constant in order to
a28eb9a8 10766 create a default initializer. */
10767 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
eeebe20b 10768 && sym->ts.u.cl->length
10769 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
a28eb9a8 10770 {
eeebe20b 10771 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
a28eb9a8 10772 init_expr->value.character.length = char_len;
c32f863c 10773 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
a28eb9a8 10774 for (i = 0; i < char_len; i++)
c32f863c 10775 init_expr->value.character.string[i]
10776 = (unsigned char) gfc_option.flag_init_character_value;
a28eb9a8 10777 }
10778 else
10779 {
10780 gfc_free_expr (init_expr);
10781 init_expr = NULL;
10782 }
3831e585 10783 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
eb106faf 10784 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
3831e585 10785 {
10786 gfc_actual_arglist *arg;
10787 init_expr = gfc_get_expr ();
10788 init_expr->where = sym->declared_at;
10789 init_expr->ts = sym->ts;
10790 init_expr->expr_type = EXPR_FUNCTION;
10791 init_expr->value.function.isym =
10792 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10793 init_expr->value.function.name = "repeat";
10794 arg = gfc_get_actual_arglist ();
10795 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10796 NULL, 1);
10797 arg->expr->value.character.string[0]
10798 = gfc_option.flag_init_character_value;
10799 arg->next = gfc_get_actual_arglist ();
10800 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10801 init_expr->value.function.actual = arg;
10802 }
a28eb9a8 10803 break;
d6463863 10804
a28eb9a8 10805 default:
10806 gfc_free_expr (init_expr);
10807 init_expr = NULL;
10808 }
10809 return init_expr;
10810}
10811
10812/* Add an initialization expression to a local variable. */
10813static void
10814apply_default_init_local (gfc_symbol *sym)
10815{
10816 gfc_expr *init = NULL;
10817
10818 /* The symbol should be a variable or a function return value. */
10819 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10820 || (sym->attr.function && sym->result != sym))
10821 return;
10822
10823 /* Try to build the initializer expression. If we can't initialize
10824 this symbol, then init will be NULL. */
10825 init = build_default_init_expr (sym);
10826 if (init == NULL)
10827 return;
10828
3831e585 10829 /* For saved variables, we don't want to add an initializer at function
10830 entry, so we just add a static initializer. Note that automatic variables
dbbb84de 10831 are stack allocated even with -fno-automatic; we have also to exclude
10832 result variable, which are also nonstatic. */
d6463863 10833 if (sym->attr.save || sym->ns->save_all
eb106faf 10834 || (flag_max_stack_var_size == 0 && !sym->attr.result
e275db47 10835 && !sym->ns->proc_name->attr.recursive
3831e585 10836 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
a28eb9a8 10837 {
10838 /* Don't clobber an existing initializer! */
10839 gcc_assert (sym->value == NULL);
10840 sym->value = init;
10841 return;
10842 }
10843
10844 build_init_assign (sym, init);
10845}
b9cd8c56 10846
3e715c81 10847
f6d0e37a 10848/* Resolution of common features of flavors variable and procedure. */
693c40a7 10849
60e19868 10850static bool
693c40a7 10851resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10852{
5c3604f9 10853 gfc_array_spec *as;
10854
5c3604f9 10855 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10856 as = CLASS_DATA (sym)->as;
10857 else
10858 as = sym->as;
10859
693c40a7 10860 /* Constraints on deferred shape variable. */
5c3604f9 10861 if (as == NULL || as->type != AS_DEFERRED)
693c40a7 10862 {
5c3604f9 10863 bool pointer, allocatable, dimension;
10864
10865 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
693c40a7 10866 {
5c3604f9 10867 pointer = CLASS_DATA (sym)->attr.class_pointer;
10868 allocatable = CLASS_DATA (sym)->attr.allocatable;
10869 dimension = CLASS_DATA (sym)->attr.dimension;
10870 }
10871 else
10872 {
461db9e3 10873 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
5c3604f9 10874 allocatable = sym->attr.allocatable;
10875 dimension = sym->attr.dimension;
10876 }
10877
10878 if (allocatable)
10879 {
f00f6dd6 10880 if (dimension && as->type != AS_ASSUMED_RANK)
eb67c215 10881 {
0d2b3c9c 10882 gfc_error ("Allocatable array %qs at %L must have a deferred "
f00f6dd6 10883 "shape or assumed rank", sym->name, &sym->declared_at);
60e19868 10884 return false;
eb67c215 10885 }
60e19868 10886 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
0d2b3c9c 10887 "%qs at %L may not be ALLOCATABLE",
60e19868 10888 sym->name, &sym->declared_at))
10889 return false;
693c40a7 10890 }
10891
f00f6dd6 10892 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
693c40a7 10893 {
0d2b3c9c 10894 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
f00f6dd6 10895 "assumed rank", sym->name, &sym->declared_at);
60e19868 10896 return false;
693c40a7 10897 }
693c40a7 10898 }
10899 else
10900 {
1de1b1a9 10901 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
d6a853a7 10902 && sym->ts.type != BT_CLASS && !sym->assoc)
693c40a7 10903 {
0d2b3c9c 10904 gfc_error ("Array %qs at %L cannot have a deferred shape",
693c40a7 10905 sym->name, &sym->declared_at);
60e19868 10906 return false;
693c40a7 10907 }
10908 }
1e4299bb 10909
10910 /* Constraints on polymorphic variables. */
10911 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10912 {
10913 /* F03:C502. */
a33fbb6f 10914 if (sym->attr.class_ok
a90fe829 10915 && !sym->attr.select_type_temporary
60e19868 10916 && !UNLIMITED_POLY (sym)
a33fbb6f 10917 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
1e4299bb 10918 {
0d2b3c9c 10919 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
50b4b37b 10920 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10921 &sym->declared_at);
60e19868 10922 return false;
1e4299bb 10923 }
10924
10925 /* F03:C509. */
cf92f151 10926 /* Assume that use associated symbols were checked in the module ns.
10927 Class-variables that are associate-names are also something special
10928 and excepted from the test. */
10929 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
1e4299bb 10930 {
0d2b3c9c 10931 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
1e4299bb 10932 "or pointer", sym->name, &sym->declared_at);
60e19868 10933 return false;
1e4299bb 10934 }
10935 }
d6463863 10936
60e19868 10937 return true;
693c40a7 10938}
10939
1bcc6eb8 10940
ec530640 10941/* Additional checks for symbols with flavor variable and derived
10942 type. To be called from resolve_fl_variable. */
10943
60e19868 10944static bool
71f1bb94 10945resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
ec530640 10946{
1de1b1a9 10947 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
ec530640 10948
10949 /* Check to see if a derived type is blocked from being host
10950 associated by the presence of another class I symbol in the same
10951 namespace. 14.6.1.3 of the standard and the discussion on
10952 comp.lang.fortran. */
eeebe20b 10953 if (sym->ns != sym->ts.u.derived->ns
ec530640 10954 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10955 {
10956 gfc_symbol *s;
eeebe20b 10957 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
c2958b6b 10958 if (s && s->attr.generic)
10959 s = gfc_find_dt_in_generic (s);
7cc3311d 10960 if (s && s->attr.flavor != FL_DERIVED)
ec530640 10961 {
716da296 10962 gfc_error_1 ("The type '%s' cannot be host associated at %L "
ec530640 10963 "because it is blocked by an incompatible object "
10964 "of the same name declared at %L",
eeebe20b 10965 sym->ts.u.derived->name, &sym->declared_at,
ec530640 10966 &s->declared_at);
60e19868 10967 return false;
ec530640 10968 }
10969 }
10970
10971 /* 4th constraint in section 11.3: "If an object of a type for which
10972 component-initialization is specified (R429) appears in the
10973 specification-part of a module and does not have the ALLOCATABLE
10974 or POINTER attribute, the object shall have the SAVE attribute."
10975
10976 The check for initializers is performed with
08262510 10977 gfc_has_default_initializer because gfc_default_initializer generates
ec530640 10978 a hidden default for allocatable components. */
71f1bb94 10979 if (!(sym->value || no_init_flag) && sym->ns->proc_name
ec530640 10980 && sym->ns->proc_name->attr.flavor == FL_MODULE
10981 && !sym->ns->save_all && !sym->attr.save
10982 && !sym->attr.pointer && !sym->attr.allocatable
08262510 10983 && gfc_has_default_initializer (sym->ts.u.derived)
60e19868 10984 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
0d2b3c9c 10985 "%qs at %L, needed due to the default "
60e19868 10986 "initialization", sym->name, &sym->declared_at))
10987 return false;
ec530640 10988
10989 /* Assign default initializer. */
10990 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
71f1bb94 10991 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
ec530640 10992 {
10993 sym->value = gfc_default_initializer (&sym->ts);
10994 }
10995
60e19868 10996 return true;
ec530640 10997}
10998
10999
693c40a7 11000/* Resolve symbols with flavor variable. */
11001
60e19868 11002static bool
693c40a7 11003resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11004{
71f1bb94 11005 int no_init_flag, automatic_flag;
693c40a7 11006 gfc_expr *e;
1bcc6eb8 11007 const char *auto_save_msg;
be844014 11008 bool saved_specification_expr;
c6b395dd 11009
0d2b3c9c 11010 auto_save_msg = "Automatic object %qs at %L cannot have the "
c6b395dd 11011 "SAVE attribute";
693c40a7 11012
60e19868 11013 if (!resolve_fl_var_and_proc (sym, mp_flag))
11014 return false;
ac42ecbd 11015
c6b395dd 11016 /* Set this flag to check that variables are parameters of all entries.
11017 This check is effected by the call to gfc_resolve_expr through
11018 is_non_constant_shape_array. */
be844014 11019 saved_specification_expr = specification_expr;
11020 specification_expr = true;
c6b395dd 11021
a51743b4 11022 if (sym->ns->proc_name
11023 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11024 || sym->ns->proc_name->attr.is_main_program)
11025 && !sym->attr.use_assoc
1bcc6eb8 11026 && !sym->attr.allocatable
11027 && !sym->attr.pointer
11028 && is_non_constant_shape_array (sym))
693c40a7 11029 {
a51743b4 11030 /* The shape of a main program or module array needs to be
11031 constant. */
11032 gfc_error ("The module or main program array '%s' at %L must "
11033 "have constant shape", sym->name, &sym->declared_at);
be844014 11034 specification_expr = saved_specification_expr;
60e19868 11035 return false;
693c40a7 11036 }
11037
3e715c81 11038 /* Constraints on deferred type parameter. */
b14b82d9 11039 if (sym->ts.deferred
11040 && !(sym->attr.pointer
11041 || sym->attr.allocatable
11042 || sym->attr.omp_udr_artificial_var))
3e715c81 11043 {
0d2b3c9c 11044 gfc_error ("Entity %qs at %L has a deferred type parameter and "
3e715c81 11045 "requires either the pointer or allocatable attribute",
11046 sym->name, &sym->declared_at);
be844014 11047 specification_expr = saved_specification_expr;
60e19868 11048 return false;
3e715c81 11049 }
11050
693c40a7 11051 if (sym->ts.type == BT_CHARACTER)
11052 {
11053 /* Make sure that character string variables with assumed length are
11054 dummy arguments. */
eeebe20b 11055 e = sym->ts.u.cl->length;
3e715c81 11056 if (e == NULL && !sym->attr.dummy && !sym->attr.result
b14b82d9 11057 && !sym->ts.deferred && !sym->attr.select_type_temporary
11058 && !sym->attr.omp_udr_artificial_var)
693c40a7 11059 {
11060 gfc_error ("Entity with assumed character length at %L must be a "
11061 "dummy argument or a PARAMETER", &sym->declared_at);
be844014 11062 specification_expr = saved_specification_expr;
60e19868 11063 return false;
693c40a7 11064 }
11065
23d075f4 11066 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
c6b395dd 11067 {
11068 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
be844014 11069 specification_expr = saved_specification_expr;
60e19868 11070 return false;
c6b395dd 11071 }
11072
693c40a7 11073 if (!gfc_is_constant_expr (e)
1bcc6eb8 11074 && !(e->expr_type == EXPR_VARIABLE
487e0ef4 11075 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11076 {
11077 if (!sym->attr.use_assoc && sym->ns->proc_name
11078 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11079 || sym->ns->proc_name->attr.is_main_program))
11080 {
11081 gfc_error ("'%s' at %L must have constant character length "
11082 "in this context", sym->name, &sym->declared_at);
be844014 11083 specification_expr = saved_specification_expr;
60e19868 11084 return false;
487e0ef4 11085 }
11086 if (sym->attr.in_common)
11087 {
0d2b3c9c 11088 gfc_error ("COMMON variable %qs at %L must have constant "
487e0ef4 11089 "character length", sym->name, &sym->declared_at);
be844014 11090 specification_expr = saved_specification_expr;
60e19868 11091 return false;
487e0ef4 11092 }
693c40a7 11093 }
11094 }
11095
a28eb9a8 11096 if (sym->value == NULL && sym->attr.referenced)
11097 apply_default_init_local (sym); /* Try to apply a default initialization. */
11098
71f1bb94 11099 /* Determine if the symbol may not have an initializer. */
11100 no_init_flag = automatic_flag = 0;
693c40a7 11101 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
71f1bb94 11102 || sym->attr.intrinsic || sym->attr.result)
11103 no_init_flag = 1;
aff518b0 11104 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
71f1bb94 11105 && is_non_constant_shape_array (sym))
693c40a7 11106 {
71f1bb94 11107 no_init_flag = automatic_flag = 1;
c6b395dd 11108
bc5d6438 11109 /* Also, they must not have the SAVE attribute.
11110 SAVE_IMPLICIT is checked below. */
7c7db7f6 11111 if (sym->as && sym->attr.codimension)
11112 {
11113 int corank = sym->as->corank;
11114 sym->as->corank = 0;
11115 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11116 sym->as->corank = corank;
11117 }
11118 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
c6b395dd 11119 {
11120 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
be844014 11121 specification_expr = saved_specification_expr;
60e19868 11122 return false;
c6b395dd 11123 }
ec530640 11124 }
693c40a7 11125
99b87279 11126 /* Ensure that any initializer is simplified. */
11127 if (sym->value)
11128 gfc_simplify_expr (sym->value, 1);
11129
693c40a7 11130 /* Reject illegal initializers. */
71f1bb94 11131 if (!sym->mark && sym->value)
693c40a7 11132 {
ea108448 11133 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11134 && CLASS_DATA (sym)->attr.allocatable))
0d2b3c9c 11135 gfc_error ("Allocatable %qs at %L cannot have an initializer",
693c40a7 11136 sym->name, &sym->declared_at);
11137 else if (sym->attr.external)
0d2b3c9c 11138 gfc_error ("External %qs at %L cannot have an initializer",
693c40a7 11139 sym->name, &sym->declared_at);
3f9b9add 11140 else if (sym->attr.dummy
11141 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
0d2b3c9c 11142 gfc_error ("Dummy %qs at %L cannot have an initializer",
693c40a7 11143 sym->name, &sym->declared_at);
11144 else if (sym->attr.intrinsic)
0d2b3c9c 11145 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
693c40a7 11146 sym->name, &sym->declared_at);
11147 else if (sym->attr.result)
0d2b3c9c 11148 gfc_error ("Function result %qs at %L cannot have an initializer",
693c40a7 11149 sym->name, &sym->declared_at);
71f1bb94 11150 else if (automatic_flag)
0d2b3c9c 11151 gfc_error ("Automatic array %qs at %L cannot have an initializer",
693c40a7 11152 sym->name, &sym->declared_at);
3f9b9add 11153 else
11154 goto no_init_error;
be844014 11155 specification_expr = saved_specification_expr;
60e19868 11156 return false;
693c40a7 11157 }
11158
3f9b9add 11159no_init_error:
1de1b1a9 11160 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
be844014 11161 {
60e19868 11162 bool res = resolve_fl_variable_derived (sym, no_init_flag);
be844014 11163 specification_expr = saved_specification_expr;
11164 return res;
11165 }
693c40a7 11166
be844014 11167 specification_expr = saved_specification_expr;
60e19868 11168 return true;
693c40a7 11169}
11170
11171
11172/* Resolve a procedure. */
11173
60e19868 11174static bool
693c40a7 11175resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11176{
11177 gfc_formal_arglist *arg;
11178
11179 if (sym->attr.function
60e19868 11180 && !resolve_fl_var_and_proc (sym, mp_flag))
11181 return false;
ac42ecbd 11182
994f2db2 11183 if (sym->ts.type == BT_CHARACTER)
693c40a7 11184 {
eeebe20b 11185 gfc_charlen *cl = sym->ts.u.cl;
0d71b2ca 11186
11187 if (cl && cl->length && gfc_is_constant_expr (cl->length)
60e19868 11188 && !resolve_charlen (cl))
11189 return false;
0d71b2ca 11190
f5daae0b 11191 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11192 && sym->attr.proc == PROC_ST_FUNCTION)
994f2db2 11193 {
0d2b3c9c 11194 gfc_error ("Character-valued statement function %qs at %L must "
f5daae0b 11195 "have constant length", sym->name, &sym->declared_at);
60e19868 11196 return false;
1bcc6eb8 11197 }
693c40a7 11198 }
11199
62838b07 11200 /* Ensure that derived type for are not of a private type. Internal
69b1505f 11201 module procedures are excluded by 2.2.3.3 - i.e., they are not
179eba08 11202 externally accessible and can access all the objects accessible in
f6d0e37a 11203 the host. */
62838b07 11204 if (!(sym->ns->parent
1bcc6eb8 11205 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
924d51fd 11206 && gfc_check_symbol_access (sym))
693c40a7 11207 {
195c8d50 11208 gfc_interface *iface;
11209
6777213b 11210 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
693c40a7 11211 {
11212 if (arg->sym
1bcc6eb8 11213 && arg->sym->ts.type == BT_DERIVED
eeebe20b 11214 && !arg->sym->ts.u.derived->attr.use_assoc
924d51fd 11215 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
0d2b3c9c 11216 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
60e19868 11217 "and cannot be a dummy argument"
0d2b3c9c 11218 " of %qs, which is PUBLIC at %L",
080819af 11219 arg->sym->name, sym->name,
60e19868 11220 &sym->declared_at))
693c40a7 11221 {
693c40a7 11222 /* Stop this message from recurring. */
eeebe20b 11223 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
60e19868 11224 return false;
693c40a7 11225 }
11226 }
195c8d50 11227
f47957c7 11228 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11229 PRIVATE to the containing module. */
11230 for (iface = sym->generic; iface; iface = iface->next)
11231 {
6777213b 11232 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
f47957c7 11233 {
11234 if (arg->sym
11235 && arg->sym->ts.type == BT_DERIVED
eeebe20b 11236 && !arg->sym->ts.u.derived->attr.use_assoc
924d51fd 11237 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
0d2b3c9c 11238 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11239 "PUBLIC interface %qs at %L "
11240 "takes dummy arguments of %qs which "
080819af 11241 "is PRIVATE", iface->sym->name,
11242 sym->name, &iface->sym->declared_at,
60e19868 11243 gfc_typename(&arg->sym->ts)))
f47957c7 11244 {
f47957c7 11245 /* Stop this message from recurring. */
eeebe20b 11246 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
60e19868 11247 return false;
f47957c7 11248 }
11249 }
11250 }
693c40a7 11251 }
11252
cad0ddcf 11253 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11254 && !sym->attr.proc_pointer)
12a05fa5 11255 {
0d2b3c9c 11256 gfc_error ("Function %qs at %L cannot have an initializer",
12a05fa5 11257 sym->name, &sym->declared_at);
60e19868 11258 return false;
12a05fa5 11259 }
11260
8e2caf1e 11261 /* An external symbol may not have an initializer because it is taken to be
cad0ddcf 11262 a procedure. Exception: Procedure Pointers. */
11263 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
693c40a7 11264 {
0d2b3c9c 11265 gfc_error ("External object %qs at %L may not have an initializer",
693c40a7 11266 sym->name, &sym->declared_at);
60e19868 11267 return false;
693c40a7 11268 }
11269
94a286ff 11270 /* An elemental function is required to return a scalar 12.7.1 */
11271 if (sym->attr.elemental && sym->attr.function && sym->as)
11272 {
0d2b3c9c 11273 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
94a286ff 11274 "result", sym->name, &sym->declared_at);
11275 /* Reset so that the error only occurs once. */
11276 sym->attr.elemental = 0;
60e19868 11277 return false;
94a286ff 11278 }
11279
7ef67f66 11280 if (sym->attr.proc == PROC_ST_FUNCTION
11281 && (sym->attr.allocatable || sym->attr.pointer))
11282 {
0d2b3c9c 11283 gfc_error ("Statement function %qs at %L may not have pointer or "
7ef67f66 11284 "allocatable attribute", sym->name, &sym->declared_at);
60e19868 11285 return false;
7ef67f66 11286 }
11287
693c40a7 11288 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11289 char-len-param shall not be array-valued, pointer-valued, recursive
11290 or pure. ....snip... A character value of * may only be used in the
11291 following ways: (i) Dummy arg of procedure - dummy associates with
11292 actual length; (ii) To declare a named constant; or (iii) External
11293 function - but length must be declared in calling scoping unit. */
11294 if (sym->attr.function
11820978 11295 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
eeebe20b 11296 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
693c40a7 11297 {
11298 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
1bcc6eb8 11299 || (sym->attr.recursive) || (sym->attr.pure))
693c40a7 11300 {
11301 if (sym->as && sym->as->rank)
0d2b3c9c 11302 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
693c40a7 11303 "array-valued", sym->name, &sym->declared_at);
11304
11305 if (sym->attr.pointer)
0d2b3c9c 11306 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
693c40a7 11307 "pointer-valued", sym->name, &sym->declared_at);
11308
11309 if (sym->attr.pure)
0d2b3c9c 11310 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
693c40a7 11311 "pure", sym->name, &sym->declared_at);
11312
11313 if (sym->attr.recursive)
0d2b3c9c 11314 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
693c40a7 11315 "recursive", sym->name, &sym->declared_at);
11316
60e19868 11317 return false;
693c40a7 11318 }
11319
11320 /* Appendix B.2 of the standard. Contained functions give an
50145844 11321 error anyway. Deferred character length is an F2003 feature.
11322 Don't warn on intrinsic conversion functions, which start
11323 with two underscores. */
11324 if (!sym->attr.contained && !sym->ts.deferred
11325 && (sym->name[0] != '_' || sym->name[1] != '_'))
f25dbbf7 11326 gfc_notify_std (GFC_STD_F95_OBS,
0d2b3c9c 11327 "CHARACTER(*) function %qs at %L",
693c40a7 11328 sym->name, &sym->declared_at);
11329 }
c5d33754 11330
9b0e3203 11331 /* F2008, C1218. */
11332 if (sym->attr.elemental)
11333 {
11334 if (sym->attr.proc_pointer)
11335 {
0d2b3c9c 11336 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
9b0e3203 11337 sym->name, &sym->declared_at);
11338 return false;
11339 }
11340 if (sym->attr.dummy)
11341 {
0d2b3c9c 11342 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
9b0e3203 11343 sym->name, &sym->declared_at);
11344 return false;
11345 }
11346 }
11347
c5d33754 11348 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11349 {
11350 gfc_formal_arglist *curr_arg;
e4eda3ec 11351 int has_non_interop_arg = 0;
c5d33754 11352
080819af 11353 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
60e19868 11354 sym->common_block))
c5d33754 11355 {
11356 /* Clear these to prevent looking at them again if there was an
11357 error. */
11358 sym->attr.is_bind_c = 0;
11359 sym->attr.is_c_interop = 0;
11360 sym->ts.is_c_interop = 0;
11361 }
11362 else
11363 {
11364 /* So far, no errors have been found. */
11365 sym->attr.is_c_interop = 1;
11366 sym->ts.is_c_interop = 1;
11367 }
d6463863 11368
6777213b 11369 curr_arg = gfc_sym_get_dummy_args (sym);
c5d33754 11370 while (curr_arg != NULL)
11371 {
11372 /* Skip implicitly typed dummy args here. */
e4eda3ec 11373 if (curr_arg->sym->attr.implicit_type == 0)
60e19868 11374 if (!gfc_verify_c_interop_param (curr_arg->sym))
e4eda3ec 11375 /* If something is found to fail, record the fact so we
11376 can mark the symbol for the procedure as not being
11377 BIND(C) to try and prevent multiple errors being
11378 reported. */
11379 has_non_interop_arg = 1;
d6463863 11380
c5d33754 11381 curr_arg = curr_arg->next;
11382 }
e4eda3ec 11383
11384 /* See if any of the arguments were not interoperable and if so, clear
11385 the procedure symbol to prevent duplicate error messages. */
11386 if (has_non_interop_arg != 0)
11387 {
11388 sym->attr.is_c_interop = 0;
11389 sym->ts.is_c_interop = 0;
11390 sym->attr.is_bind_c = 0;
11391 }
c5d33754 11392 }
d6463863 11393
1e057e9b 11394 if (!sym->attr.proc_pointer)
14fdcdea 11395 {
1e057e9b 11396 if (sym->attr.save == SAVE_EXPLICIT)
11397 {
11398 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
0d2b3c9c 11399 "in %qs at %L", sym->name, &sym->declared_at);
60e19868 11400 return false;
1e057e9b 11401 }
11402 if (sym->attr.intent)
11403 {
11404 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
0d2b3c9c 11405 "in %qs at %L", sym->name, &sym->declared_at);
60e19868 11406 return false;
1e057e9b 11407 }
11408 if (sym->attr.subroutine && sym->attr.result)
11409 {
11410 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
0d2b3c9c 11411 "in %qs at %L", sym->name, &sym->declared_at);
60e19868 11412 return false;
1e057e9b 11413 }
11414 if (sym->attr.external && sym->attr.function
11415 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11416 || sym->attr.contained))
11417 {
11418 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
0d2b3c9c 11419 "in %qs at %L", sym->name, &sym->declared_at);
60e19868 11420 return false;
1e057e9b 11421 }
11422 if (strcmp ("ppr@", sym->name) == 0)
11423 {
0d2b3c9c 11424 gfc_error ("Procedure pointer result %qs at %L "
1e057e9b 11425 "is missing the pointer attribute",
11426 sym->ns->proc_name->name, &sym->declared_at);
60e19868 11427 return false;
1e057e9b 11428 }
14fdcdea 11429 }
11430
60e19868 11431 return true;
ac42ecbd 11432}
11433
11434
223f0f57 11435/* Resolve a list of finalizer procedures. That is, after they have hopefully
11436 been defined and we now know their defined arguments, check that they fulfill
11437 the requirements of the standard for procedures used as finalizers. */
11438
60e19868 11439static bool
baf14602 11440gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
223f0f57 11441{
11442 gfc_finalizer* list;
11443 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
60e19868 11444 bool result = true;
223f0f57 11445 bool seen_scalar = false;
baf14602 11446 gfc_symbol *vtab;
11447 gfc_component *c;
f3348d97 11448 gfc_symbol *parent = gfc_get_derived_super_type (derived);
11449
11450 if (parent)
11451 gfc_resolve_finalizers (parent, finalizable);
223f0f57 11452
baf14602 11453 /* Return early when not finalizable. Additionally, ensure that derived-type
11454 components have a their finalizables resolved. */
223f0f57 11455 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
baf14602 11456 {
11457 bool has_final = false;
11458 for (c = derived->components; c; c = c->next)
11459 if (c->ts.type == BT_DERIVED
11460 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
11461 {
11462 bool has_final2 = false;
11463 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
11464 return false; /* Error. */
11465 has_final = has_final || has_final2;
11466 }
11467 if (!has_final)
11468 {
11469 if (finalizable)
11470 *finalizable = false;
11471 return true;
11472 }
11473 }
223f0f57 11474
11475 /* Walk over the list of finalizer-procedures, check them, and if any one
11476 does not fit in with the standard's definition, print an error and remove
11477 it from the list. */
11478 prev_link = &derived->f2k_derived->finalizers;
11479 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11480 {
6777213b 11481 gfc_formal_arglist *dummy_args;
223f0f57 11482 gfc_symbol* arg;
11483 gfc_finalizer* i;
11484 int my_rank;
11485
e449e4dd 11486 /* Skip this finalizer if we already resolved it. */
11487 if (list->proc_tree)
11488 {
11489 prev_link = &(list->next);
11490 continue;
11491 }
11492
223f0f57 11493 /* Check this exists and is a SUBROUTINE. */
e449e4dd 11494 if (!list->proc_sym->attr.subroutine)
223f0f57 11495 {
0d2b3c9c 11496 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
e449e4dd 11497 list->proc_sym->name, &list->where);
223f0f57 11498 goto error;
11499 }
11500
11501 /* We should have exactly one argument. */
6777213b 11502 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11503 if (!dummy_args || dummy_args->next)
223f0f57 11504 {
11505 gfc_error ("FINAL procedure at %L must have exactly one argument",
11506 &list->where);
11507 goto error;
11508 }
6777213b 11509 arg = dummy_args->sym;
223f0f57 11510
11511 /* This argument must be of our type. */
eeebe20b 11512 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
223f0f57 11513 {
0d2b3c9c 11514 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
223f0f57 11515 &arg->declared_at, derived->name);
11516 goto error;
11517 }
11518
11519 /* It must neither be a pointer nor allocatable nor optional. */
11520 if (arg->attr.pointer)
11521 {
11522 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11523 &arg->declared_at);
11524 goto error;
11525 }
11526 if (arg->attr.allocatable)
11527 {
11528 gfc_error ("Argument of FINAL procedure at %L must not be"
11529 " ALLOCATABLE", &arg->declared_at);
11530 goto error;
11531 }
11532 if (arg->attr.optional)
11533 {
11534 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11535 &arg->declared_at);
11536 goto error;
11537 }
11538
11539 /* It must not be INTENT(OUT). */
11540 if (arg->attr.intent == INTENT_OUT)
11541 {
11542 gfc_error ("Argument of FINAL procedure at %L must not be"
11543 " INTENT(OUT)", &arg->declared_at);
11544 goto error;
11545 }
11546
11547 /* Warn if the procedure is non-scalar and not assumed shape. */
8290d53f 11548 if (warn_surprising && arg->as && arg->as->rank != 0
223f0f57 11549 && arg->as->type != AS_ASSUMED_SHAPE)
4166acc7 11550 gfc_warning (OPT_Wsurprising,
11551 "Non-scalar FINAL procedure at %L should have assumed"
223f0f57 11552 " shape argument", &arg->declared_at);
11553
11554 /* Check that it does not match in kind and rank with a FINAL procedure
11555 defined earlier. To really loop over the *earlier* declarations,
11556 we need to walk the tail of the list as new ones were pushed at the
11557 front. */
11558 /* TODO: Handle kind parameters once they are implemented. */
11559 my_rank = (arg->as ? arg->as->rank : 0);
11560 for (i = list->next; i; i = i->next)
11561 {
6777213b 11562 gfc_formal_arglist *dummy_args;
11563
223f0f57 11564 /* Argument list might be empty; that is an error signalled earlier,
11565 but we nevertheless continued resolving. */
6777213b 11566 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11567 if (dummy_args)
223f0f57 11568 {
6777213b 11569 gfc_symbol* i_arg = dummy_args->sym;
223f0f57 11570 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11571 if (i_rank == my_rank)
11572 {
0d2b3c9c 11573 gfc_error ("FINAL procedure %qs declared at %L has the same"
11574 " rank (%d) as %qs",
d6463863 11575 list->proc_sym->name, &list->where, my_rank,
e449e4dd 11576 i->proc_sym->name);
223f0f57 11577 goto error;
11578 }
11579 }
11580 }
11581
11582 /* Is this the/a scalar finalizer procedure? */
11583 if (!arg->as || arg->as->rank == 0)
11584 seen_scalar = true;
11585
e449e4dd 11586 /* Find the symtree for this procedure. */
11587 gcc_assert (!list->proc_tree);
11588 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11589
223f0f57 11590 prev_link = &list->next;
11591 continue;
11592
69b1505f 11593 /* Remove wrong nodes immediately from the list so we don't risk any
223f0f57 11594 troubles in the future when they might fail later expectations. */
11595error:
223f0f57 11596 i = list;
11597 *prev_link = list->next;
11598 gfc_free_finalizer (i);
baf14602 11599 result = false;
223f0f57 11600 }
11601
baf14602 11602 if (result == false)
11603 return false;
11604
223f0f57 11605 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11606 were nodes in the list, must have been for arrays. It is surely a good
11607 idea to have a scalar version there if there's something to finalize. */
8290d53f 11608 if (warn_surprising && result && !seen_scalar)
4166acc7 11609 gfc_warning (OPT_Wsurprising,
11610 "Only array FINAL procedures declared for derived type %qs"
223f0f57 11611 " defined at %L, suggest also scalar one",
11612 derived->name, &derived->declared_at);
11613
baf14602 11614 vtab = gfc_find_derived_vtab (derived);
11615 c = vtab->ts.u.derived->components->next->next->next->next->next;
11616 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
11617
11618 if (finalizable)
11619 *finalizable = true;
11620
11621 return true;
223f0f57 11622}
11623
11624
e2f06a48 11625/* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11626
60e19868 11627static bool
e2f06a48 11628check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11629 const char* generic_name, locus where)
11630{
bfc1ce10 11631 gfc_symbol *sym1, *sym2;
11632 const char *pass1, *pass2;
2ab6a21d 11633 gfc_formal_arglist *dummy_args;
e2f06a48 11634
11635 gcc_assert (t1->specific && t2->specific);
11636 gcc_assert (!t1->specific->is_generic);
11637 gcc_assert (!t2->specific->is_generic);
5c0f7d99 11638 gcc_assert (t1->is_operator == t2->is_operator);
e2f06a48 11639
11640 sym1 = t1->specific->u.specific->n.sym;
11641 sym2 = t2->specific->u.specific->n.sym;
11642
1de1b1a9 11643 if (sym1 == sym2)
60e19868 11644 return true;
1de1b1a9 11645
e2f06a48 11646 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11647 if (sym1->attr.subroutine != sym2->attr.subroutine
11648 || sym1->attr.function != sym2->attr.function)
11649 {
0d2b3c9c 11650 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
11651 " GENERIC %qs at %L",
e2f06a48 11652 sym1->name, sym2->name, generic_name, &where);
60e19868 11653 return false;
e2f06a48 11654 }
11655
2ab6a21d 11656 /* Determine PASS arguments. */
bfc1ce10 11657 if (t1->specific->nopass)
11658 pass1 = NULL;
11659 else if (t1->specific->pass_arg)
11660 pass1 = t1->specific->pass_arg;
11661 else
2ab6a21d 11662 {
11663 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
11664 if (dummy_args)
11665 pass1 = dummy_args->sym->name;
11666 else
11667 pass1 = NULL;
11668 }
bfc1ce10 11669 if (t2->specific->nopass)
11670 pass2 = NULL;
11671 else if (t2->specific->pass_arg)
11672 pass2 = t2->specific->pass_arg;
11673 else
2ab6a21d 11674 {
11675 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
11676 if (dummy_args)
11677 pass2 = dummy_args->sym->name;
11678 else
11679 pass2 = NULL;
11680 }
11681
11682 /* Compare the interfaces. */
5c0f7d99 11683 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
bfc1ce10 11684 NULL, 0, pass1, pass2))
e2f06a48 11685 {
0d2b3c9c 11686 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
e2f06a48 11687 sym1->name, sym2->name, generic_name, &where);
60e19868 11688 return false;
e2f06a48 11689 }
11690
60e19868 11691 return true;
e2f06a48 11692}
11693
11694
a36eb9ee 11695/* Worker function for resolving a generic procedure binding; this is used to
11696 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11697
11698 The difference between those cases is finding possible inherited bindings
11699 that are overridden, as one has to look for them in tb_sym_root,
11700 tb_uop_root or tb_op, respectively. Thus the caller must already find
11701 the super-type and set p->overridden correctly. */
e2f06a48 11702
60e19868 11703static bool
a36eb9ee 11704resolve_tb_generic_targets (gfc_symbol* super_type,
11705 gfc_typebound_proc* p, const char* name)
e2f06a48 11706{
11707 gfc_tbp_generic* target;
11708 gfc_symtree* first_target;
e2f06a48 11709 gfc_symtree* inherited;
e2f06a48 11710
a36eb9ee 11711 gcc_assert (p && p->is_generic);
e2f06a48 11712
11713 /* Try to find the specific bindings for the symtrees in our target-list. */
a36eb9ee 11714 gcc_assert (p->u.generic);
11715 for (target = p->u.generic; target; target = target->next)
e2f06a48 11716 if (!target->specific)
11717 {
11718 gfc_typebound_proc* overridden_tbp;
11719 gfc_tbp_generic* g;
11720 const char* target_name;
11721
11722 target_name = target->specific_st->name;
11723
11724 /* Defined for this type directly. */
8b0c7315 11725 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
e2f06a48 11726 {
3323e9b1 11727 target->specific = target->specific_st->n.tb;
e2f06a48 11728 goto specific_found;
11729 }
11730
11731 /* Look for an inherited specific binding. */
11732 if (super_type)
11733 {
7d034542 11734 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11735 true, NULL);
e2f06a48 11736
11737 if (inherited)
11738 {
3323e9b1 11739 gcc_assert (inherited->n.tb);
11740 target->specific = inherited->n.tb;
e2f06a48 11741 goto specific_found;
11742 }
11743 }
11744
0d2b3c9c 11745 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
a36eb9ee 11746 " at %L", target_name, name, &p->where);
60e19868 11747 return false;
e2f06a48 11748
11749 /* Once we've found the specific binding, check it is not ambiguous with
11750 other specifics already found or inherited for the same GENERIC. */
11751specific_found:
11752 gcc_assert (target->specific);
11753
11754 /* This must really be a specific binding! */
11755 if (target->specific->is_generic)
11756 {
0d2b3c9c 11757 gfc_error ("GENERIC %qs at %L must target a specific binding,"
11758 " %qs is GENERIC, too", name, &p->where, target_name);
60e19868 11759 return false;
e2f06a48 11760 }
11761
11762 /* Check those already resolved on this type directly. */
a36eb9ee 11763 for (g = p->u.generic; g; g = g->next)
e2f06a48 11764 if (g != target && g->specific
60e19868 11765 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11766 return false;
e2f06a48 11767
11768 /* Check for ambiguity with inherited specific targets. */
a36eb9ee 11769 for (overridden_tbp = p->overridden; overridden_tbp;
e2f06a48 11770 overridden_tbp = overridden_tbp->overridden)
11771 if (overridden_tbp->is_generic)
11772 {
11773 for (g = overridden_tbp->u.generic; g; g = g->next)
11774 {
11775 gcc_assert (g->specific);
60e19868 11776 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11777 return false;
e2f06a48 11778 }
11779 }
11780 }
11781
11782 /* If we attempt to "overwrite" a specific binding, this is an error. */
a36eb9ee 11783 if (p->overridden && !p->overridden->is_generic)
e2f06a48 11784 {
0d2b3c9c 11785 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
a36eb9ee 11786 " the same name", name, &p->where);
60e19868 11787 return false;
e2f06a48 11788 }
11789
11790 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11791 all must have the same attributes here. */
a36eb9ee 11792 first_target = p->u.generic->specific->u.specific;
3323e9b1 11793 gcc_assert (first_target);
a36eb9ee 11794 p->subroutine = first_target->n.sym->attr.subroutine;
11795 p->function = first_target->n.sym->attr.function;
e2f06a48 11796
60e19868 11797 return true;
e2f06a48 11798}
11799
11800
a36eb9ee 11801/* Resolve a GENERIC procedure binding for a derived type. */
11802
60e19868 11803static bool
a36eb9ee 11804resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11805{
11806 gfc_symbol* super_type;
11807
11808 /* Find the overridden binding if any. */
11809 st->n.tb->overridden = NULL;
11810 super_type = gfc_get_derived_super_type (derived);
11811 if (super_type)
11812 {
11813 gfc_symtree* overridden;
7d034542 11814 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11815 true, NULL);
a36eb9ee 11816
11817 if (overridden && overridden->n.tb)
11818 st->n.tb->overridden = overridden->n.tb;
11819 }
11820
11821 /* Resolve using worker function. */
11822 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11823}
11824
11825
a00a2d4b 11826/* Retrieve the target-procedure of an operator binding and do some checks in
11827 common for intrinsic and user-defined type-bound operators. */
11828
11829static gfc_symbol*
11830get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11831{
11832 gfc_symbol* target_proc;
11833
11834 gcc_assert (target->specific && !target->specific->is_generic);
11835 target_proc = target->specific->u.specific->n.sym;
11836 gcc_assert (target_proc);
11837
8d891809 11838 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
a00a2d4b 11839 if (target->specific->nopass)
11840 {
11841 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11842 return NULL;
11843 }
11844
11845 return target_proc;
11846}
11847
11848
a36eb9ee 11849/* Resolve a type-bound intrinsic operator. */
11850
60e19868 11851static bool
a36eb9ee 11852resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11853 gfc_typebound_proc* p)
11854{
11855 gfc_symbol* super_type;
11856 gfc_tbp_generic* target;
d6463863 11857
a36eb9ee 11858 /* If there's already an error here, do nothing (but don't fail again). */
11859 if (p->error)
60e19868 11860 return true;
a36eb9ee 11861
11862 /* Operators should always be GENERIC bindings. */
11863 gcc_assert (p->is_generic);
11864
11865 /* Look for an overridden binding. */
11866 super_type = gfc_get_derived_super_type (derived);
11867 if (super_type && super_type->f2k_derived)
11868 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
7d034542 11869 op, true, NULL);
a36eb9ee 11870 else
11871 p->overridden = NULL;
11872
11873 /* Resolve general GENERIC properties using worker function. */
60e19868 11874 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
a36eb9ee 11875 goto error;
11876
11877 /* Check the targets to be procedures of correct interface. */
11878 for (target = p->u.generic; target; target = target->next)
11879 {
11880 gfc_symbol* target_proc;
11881
a00a2d4b 11882 target_proc = get_checked_tb_operator_target (target, p->where);
11883 if (!target_proc)
7d034542 11884 goto error;
a36eb9ee 11885
11886 if (!gfc_check_operator_interface (target_proc, op, p->where))
7d034542 11887 goto error;
fc028a49 11888
11889 /* Add target to non-typebound operator list. */
11890 if (!target->specific->deferred && !derived->attr.use_assoc
ed90efe5 11891 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
fc028a49 11892 {
11893 gfc_interface *head, *intr;
60e19868 11894 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11895 return false;
fc028a49 11896 head = derived->ns->op[op];
11897 intr = gfc_get_interface ();
11898 intr->sym = target_proc;
11899 intr->where = p->where;
11900 intr->next = head;
11901 derived->ns->op[op] = intr;
11902 }
a36eb9ee 11903 }
11904
60e19868 11905 return true;
a36eb9ee 11906
11907error:
11908 p->error = 1;
60e19868 11909 return false;
a36eb9ee 11910}
11911
11912
11913/* Resolve a type-bound user operator (tree-walker callback). */
7fd88f6e 11914
11915static gfc_symbol* resolve_bindings_derived;
60e19868 11916static bool resolve_bindings_result;
7fd88f6e 11917
60e19868 11918static bool check_uop_procedure (gfc_symbol* sym, locus where);
a36eb9ee 11919
11920static void
11921resolve_typebound_user_op (gfc_symtree* stree)
11922{
11923 gfc_symbol* super_type;
11924 gfc_tbp_generic* target;
11925
11926 gcc_assert (stree && stree->n.tb);
11927
11928 if (stree->n.tb->error)
11929 return;
11930
11931 /* Operators should always be GENERIC bindings. */
11932 gcc_assert (stree->n.tb->is_generic);
11933
11934 /* Find overridden procedure, if any. */
11935 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11936 if (super_type && super_type->f2k_derived)
11937 {
11938 gfc_symtree* overridden;
11939 overridden = gfc_find_typebound_user_op (super_type, NULL,
7d034542 11940 stree->name, true, NULL);
a36eb9ee 11941
11942 if (overridden && overridden->n.tb)
11943 stree->n.tb->overridden = overridden->n.tb;
11944 }
11945 else
11946 stree->n.tb->overridden = NULL;
11947
11948 /* Resolve basically using worker function. */
60e19868 11949 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
a36eb9ee 11950 goto error;
11951
11952 /* Check the targets to be functions of correct interface. */
11953 for (target = stree->n.tb->u.generic; target; target = target->next)
11954 {
11955 gfc_symbol* target_proc;
11956
a00a2d4b 11957 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11958 if (!target_proc)
11959 goto error;
a36eb9ee 11960
60e19868 11961 if (!check_uop_procedure (target_proc, stree->n.tb->where))
a36eb9ee 11962 goto error;
11963 }
11964
11965 return;
11966
11967error:
60e19868 11968 resolve_bindings_result = false;
a36eb9ee 11969 stree->n.tb->error = 1;
11970}
11971
11972
11973/* Resolve the type-bound procedures for a derived type. */
11974
7fd88f6e 11975static void
11976resolve_typebound_procedure (gfc_symtree* stree)
11977{
11978 gfc_symbol* proc;
11979 locus where;
11980 gfc_symbol* me_arg;
11981 gfc_symbol* super_type;
f8f35c46 11982 gfc_component* comp;
7fd88f6e 11983
3323e9b1 11984 gcc_assert (stree);
11985
11986 /* Undefined specific symbol from GENERIC target definition. */
11987 if (!stree->n.tb)
11988 return;
11989
11990 if (stree->n.tb->error)
7fd88f6e 11991 return;
11992
e2f06a48 11993 /* If this is a GENERIC binding, use that routine. */
3323e9b1 11994 if (stree->n.tb->is_generic)
e2f06a48 11995 {
60e19868 11996 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
e2f06a48 11997 goto error;
11998 return;
11999 }
12000
7fd88f6e 12001 /* Get the target-procedure to check it. */
3323e9b1 12002 gcc_assert (!stree->n.tb->is_generic);
12003 gcc_assert (stree->n.tb->u.specific);
12004 proc = stree->n.tb->u.specific->n.sym;
12005 where = stree->n.tb->where;
7fd88f6e 12006
12007 /* Default access should already be resolved from the parser. */
3323e9b1 12008 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
7fd88f6e 12009
ea996e99 12010 if (stree->n.tb->deferred)
7fd88f6e 12011 {
60e19868 12012 if (!check_proc_interface (proc, &where))
ea996e99 12013 goto error;
12014 }
12015 else
12016 {
12017 /* Check for F08:C465. */
12018 if ((!proc->attr.subroutine && !proc->attr.function)
12019 || (proc->attr.proc != PROC_MODULE
12020 && proc->attr.if_source != IFSRC_IFBODY)
12021 || proc->attr.abstract)
12022 {
0d2b3c9c 12023 gfc_error ("%qs must be a module procedure or an external procedure with"
ea996e99 12024 " an explicit interface at %L", proc->name, &where);
12025 goto error;
12026 }
7fd88f6e 12027 }
ea996e99 12028
3323e9b1 12029 stree->n.tb->subroutine = proc->attr.subroutine;
12030 stree->n.tb->function = proc->attr.function;
7fd88f6e 12031
12032 /* Find the super-type of the current derived type. We could do this once and
12033 store in a global if speed is needed, but as long as not I believe this is
12034 more readable and clearer. */
12035 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12036
e2f06a48 12037 /* If PASS, resolve and check arguments if not already resolved / loaded
12038 from a .mod file. */
3323e9b1 12039 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
7fd88f6e 12040 {
6777213b 12041 gfc_formal_arglist *dummy_args;
12042
12043 dummy_args = gfc_sym_get_dummy_args (proc);
3323e9b1 12044 if (stree->n.tb->pass_arg)
7fd88f6e 12045 {
6777213b 12046 gfc_formal_arglist *i;
7fd88f6e 12047
12048 /* If an explicit passing argument name is given, walk the arg-list
12049 and look for it. */
12050
12051 me_arg = NULL;
3323e9b1 12052 stree->n.tb->pass_arg_num = 1;
6777213b 12053 for (i = dummy_args; i; i = i->next)
7fd88f6e 12054 {
3323e9b1 12055 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
7fd88f6e 12056 {
12057 me_arg = i->sym;
12058 break;
12059 }
3323e9b1 12060 ++stree->n.tb->pass_arg_num;
7fd88f6e 12061 }
12062
12063 if (!me_arg)
12064 {
0d2b3c9c 12065 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12066 " argument %qs",
3323e9b1 12067 proc->name, stree->n.tb->pass_arg, &where,
12068 stree->n.tb->pass_arg);
7fd88f6e 12069 goto error;
12070 }
12071 }
12072 else
12073 {
12074 /* Otherwise, take the first one; there should in fact be at least
12075 one. */
3323e9b1 12076 stree->n.tb->pass_arg_num = 1;
6777213b 12077 if (!dummy_args)
7fd88f6e 12078 {
0d2b3c9c 12079 gfc_error ("Procedure %qs with PASS at %L must have at"
7fd88f6e 12080 " least one argument", proc->name, &where);
12081 goto error;
12082 }
6777213b 12083 me_arg = dummy_args->sym;
7fd88f6e 12084 }
12085
4b68c8f7 12086 /* Now check that the argument-type matches and the passed-object
12087 dummy argument is generally fine. */
12088
7fd88f6e 12089 gcc_assert (me_arg);
4b68c8f7 12090
1de1b1a9 12091 if (me_arg->ts.type != BT_CLASS)
7fd88f6e 12092 {
0d2b3c9c 12093 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
1de1b1a9 12094 " at %L", proc->name, &where);
7fd88f6e 12095 goto error;
12096 }
930fe1de 12097
50b4b37b 12098 if (CLASS_DATA (me_arg)->ts.u.derived
1de1b1a9 12099 != resolve_bindings_derived)
8ca7f89c 12100 {
0d2b3c9c 12101 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12102 " the derived-type %qs", me_arg->name, proc->name,
1de1b1a9 12103 me_arg->name, &where, resolve_bindings_derived->name);
8ca7f89c 12104 goto error;
12105 }
d6463863 12106
4b68c8f7 12107 gcc_assert (me_arg->ts.type == BT_CLASS);
f00f6dd6 12108 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
4b68c8f7 12109 {
0d2b3c9c 12110 gfc_error ("Passed-object dummy argument of %qs at %L must be"
4b68c8f7 12111 " scalar", proc->name, &where);
12112 goto error;
12113 }
50b4b37b 12114 if (CLASS_DATA (me_arg)->attr.allocatable)
4b68c8f7 12115 {
0d2b3c9c 12116 gfc_error ("Passed-object dummy argument of %qs at %L must not"
4b68c8f7 12117 " be ALLOCATABLE", proc->name, &where);
12118 goto error;
12119 }
50b4b37b 12120 if (CLASS_DATA (me_arg)->attr.class_pointer)
4b68c8f7 12121 {
0d2b3c9c 12122 gfc_error ("Passed-object dummy argument of %qs at %L must not"
4b68c8f7 12123 " be POINTER", proc->name, &where);
12124 goto error;
12125 }
7fd88f6e 12126 }
12127
12128 /* If we are extending some type, check that we don't override a procedure
12129 flagged NON_OVERRIDABLE. */
3323e9b1 12130 stree->n.tb->overridden = NULL;
7fd88f6e 12131 if (super_type)
12132 {
12133 gfc_symtree* overridden;
930fe1de 12134 overridden = gfc_find_typebound_proc (super_type, NULL,
7d034542 12135 stree->name, true, NULL);
7fd88f6e 12136
9fcec394 12137 if (overridden)
12138 {
12139 if (overridden->n.tb)
12140 stree->n.tb->overridden = overridden->n.tb;
e2f06a48 12141
60e19868 12142 if (!gfc_check_typebound_override (stree, overridden))
9fcec394 12143 goto error;
12144 }
7fd88f6e 12145 }
12146
f8f35c46 12147 /* See if there's a name collision with a component directly in this type. */
12148 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12149 if (!strcmp (comp->name, stree->name))
12150 {
0d2b3c9c 12151 gfc_error ("Procedure %qs at %L has the same name as a component of"
12152 " %qs",
f8f35c46 12153 stree->name, &where, resolve_bindings_derived->name);
12154 goto error;
12155 }
12156
12157 /* Try to find a name collision with an inherited component. */
12158 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12159 {
0d2b3c9c 12160 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12161 " component of %qs",
f8f35c46 12162 stree->name, &where, resolve_bindings_derived->name);
12163 goto error;
12164 }
12165
3323e9b1 12166 stree->n.tb->error = 0;
7fd88f6e 12167 return;
12168
12169error:
60e19868 12170 resolve_bindings_result = false;
3323e9b1 12171 stree->n.tb->error = 1;
7fd88f6e 12172}
12173
ec2c6976 12174
60e19868 12175static bool
7fd88f6e 12176resolve_typebound_procedures (gfc_symbol* derived)
12177{
a36eb9ee 12178 int op;
f959368d 12179 gfc_symbol* super_type;
a36eb9ee 12180
3323e9b1 12181 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
60e19868 12182 return true;
d6463863 12183
f959368d 12184 super_type = gfc_get_derived_super_type (derived);
12185 if (super_type)
d82a3a02 12186 resolve_symbol (super_type);
7fd88f6e 12187
12188 resolve_bindings_derived = derived;
60e19868 12189 resolve_bindings_result = true;
a36eb9ee 12190
12191 if (derived->f2k_derived->tb_sym_root)
12192 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12193 &resolve_typebound_procedure);
12194
a36eb9ee 12195 if (derived->f2k_derived->tb_uop_root)
12196 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12197 &resolve_typebound_user_op);
12198
12199 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12200 {
12201 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
080819af 12202 if (p && !resolve_typebound_intrinsic_op (derived,
60e19868 12203 (gfc_intrinsic_op)op, p))
12204 resolve_bindings_result = false;
a36eb9ee 12205 }
7fd88f6e 12206
12207 return resolve_bindings_result;
12208}
12209
12210
00be3612 12211/* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12212 to give all identical derived types the same backend_decl. */
12213static void
12214add_dt_to_dt_list (gfc_symbol *derived)
12215{
12216 gfc_dt_list *dt_list;
12217
12218 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12219 if (derived == dt_list->derived)
84e25a66 12220 return;
00be3612 12221
84e25a66 12222 dt_list = gfc_get_dt_list ();
12223 dt_list->next = gfc_derived_types;
12224 dt_list->derived = derived;
12225 gfc_derived_types = dt_list;
00be3612 12226}
12227
12228
61c3b81d 12229/* Ensure that a derived-type is really not abstract, meaning that every
12230 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12231
60e19868 12232static bool
61c3b81d 12233ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12234{
12235 if (!st)
60e19868 12236 return true;
61c3b81d 12237
60e19868 12238 if (!ensure_not_abstract_walker (sub, st->left))
12239 return false;
12240 if (!ensure_not_abstract_walker (sub, st->right))
12241 return false;
61c3b81d 12242
3323e9b1 12243 if (st->n.tb && st->n.tb->deferred)
61c3b81d 12244 {
12245 gfc_symtree* overriding;
7d034542 12246 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
cf4b41d8 12247 if (!overriding)
60e19868 12248 return false;
cf4b41d8 12249 gcc_assert (overriding->n.tb);
3323e9b1 12250 if (overriding->n.tb->deferred)
61c3b81d 12251 {
0d2b3c9c 12252 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12253 " %qs is DEFERRED and not overridden",
61c3b81d 12254 sub->name, &sub->declared_at, st->name);
60e19868 12255 return false;
61c3b81d 12256 }
12257 }
12258
60e19868 12259 return true;
61c3b81d 12260}
12261
60e19868 12262static bool
61c3b81d 12263ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12264{
12265 /* The algorithm used here is to recursively travel up the ancestry of sub
12266 and for each ancestor-type, check all bindings. If any of them is
12267 DEFERRED, look it up starting from sub and see if the found (overriding)
12268 binding is not DEFERRED.
12269 This is not the most efficient way to do this, but it should be ok and is
12270 clearer than something sophisticated. */
12271
a96bd516 12272 gcc_assert (ancestor && !sub->attr.abstract);
d6463863 12273
a96bd516 12274 if (!ancestor->attr.abstract)
60e19868 12275 return true;
61c3b81d 12276
12277 /* Walk bindings of this ancestor. */
12278 if (ancestor->f2k_derived)
12279 {
60e19868 12280 bool t;
3323e9b1 12281 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
60e19868 12282 if (!t)
12283 return false;
61c3b81d 12284 }
12285
12286 /* Find next ancestor type and recurse on it. */
12287 ancestor = gfc_get_derived_super_type (ancestor);
12288 if (ancestor)
12289 return ensure_not_abstract (sub, ancestor);
12290
60e19868 12291 return true;
61c3b81d 12292}
12293
12294
d6463863 12295/* This check for typebound defined assignments is done recursively
12296 since the order in which derived types are resolved is not always in
12297 order of the declarations. */
12298
12299static void
12300check_defined_assignments (gfc_symbol *derived)
12301{
12302 gfc_component *c;
12303
12304 for (c = derived->components; c; c = c->next)
12305 {
12306 if (c->ts.type != BT_DERIVED
12307 || c->attr.pointer
12308 || c->attr.allocatable
12309 || c->attr.proc_pointer_comp
12310 || c->attr.class_pointer
12311 || c->attr.proc_pointer)
12312 continue;
12313
12314 if (c->ts.u.derived->attr.defined_assign_comp
12315 || (c->ts.u.derived->f2k_derived
12316 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12317 {
12318 derived->attr.defined_assign_comp = 1;
12319 return;
12320 }
12321
12322 check_defined_assignments (c->ts.u.derived);
12323 if (c->ts.u.derived->attr.defined_assign_comp)
12324 {
12325 derived->attr.defined_assign_comp = 1;
12326 return;
12327 }
12328 }
12329}
12330
12331
f959368d 12332/* Resolve the components of a derived type. This does not have to wait until
12333 resolution stage, but can be done as soon as the dt declaration has been
12334 parsed. */
ac42ecbd 12335
60e19868 12336static bool
f959368d 12337resolve_fl_derived0 (gfc_symbol *sym)
ac42ecbd 12338{
f8f35c46 12339 gfc_symbol* super_type;
ac42ecbd 12340 gfc_component *c;
12341
a90fe829 12342 if (sym->attr.unlimited_polymorphic)
60e19868 12343 return true;
a90fe829 12344
f8f35c46 12345 super_type = gfc_get_derived_super_type (sym);
12346
293d72e0 12347 /* F2008, C432. */
aff518b0 12348 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12349 {
0d2b3c9c 12350 gfc_error ("As extending type %qs at %L has a coarray component, "
12351 "parent type %qs shall also have one", sym->name,
aff518b0 12352 &sym->declared_at, super_type->name);
60e19868 12353 return false;
aff518b0 12354 }
12355
e2f06a48 12356 /* Ensure the extended type gets resolved before we do. */
60e19868 12357 if (super_type && !resolve_fl_derived0 (super_type))
12358 return false;
e2f06a48 12359
ac5f2650 12360 /* An ABSTRACT type must be extensible. */
1de1b1a9 12361 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
ac5f2650 12362 {
0d2b3c9c 12363 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
ac5f2650 12364 sym->name, &sym->declared_at);
60e19868 12365 return false;
ac5f2650 12366 }
12367
5c3604f9 12368 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12369 : sym->components;
12370
12371 for ( ; c != NULL; c = c->next)
ac42ecbd 12372 {
8a96d642 12373 if (c->attr.artificial)
12374 continue;
12375
aff518b0 12376 /* F2008, C442. */
fd23cc08 12377 if ((!sym->attr.is_class || c != sym->components)
12378 && c->attr.codimension
e97ac7c0 12379 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
aff518b0 12380 {
716da296 12381 gfc_error ("Coarray component %qs at %L must be allocatable with "
aff518b0 12382 "deferred shape", c->name, &c->loc);
60e19868 12383 return false;
aff518b0 12384 }
12385
12386 /* F2008, C443. */
12387 if (c->attr.codimension && c->ts.type == BT_DERIVED
12388 && c->ts.u.derived->ts.is_iso_c)
12389 {
716da296 12390 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
aff518b0 12391 "shall not be a coarray", c->name, &c->loc);
60e19868 12392 return false;
aff518b0 12393 }
12394
12395 /* F2008, C444. */
12396 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
2d640d61 12397 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12398 || c->attr.allocatable))
aff518b0 12399 {
716da296 12400 gfc_error ("Component %qs at %L with coarray component "
aff518b0 12401 "shall be a nonpointer, nonallocatable scalar",
12402 c->name, &c->loc);
60e19868 12403 return false;
aff518b0 12404 }
12405
b3c3927c 12406 /* F2008, C448. */
12407 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12408 {
716da296 12409 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
b3c3927c 12410 "is not an array pointer", c->name, &c->loc);
60e19868 12411 return false;
b3c3927c 12412 }
12413
64e93293 12414 if (c->attr.proc_pointer && c->ts.interface)
12415 {
ea996e99 12416 gfc_symbol *ifc = c->ts.interface;
64e93293 12417
ea996e99 12418 if (!sym->attr.vtype
60e19868 12419 && !check_proc_interface (ifc, &c->loc))
12420 return false;
64e93293 12421
ea996e99 12422 if (ifc->attr.if_source || ifc->attr.intrinsic)
12423 {
12424 /* Resolve interface and copy attributes. */
452a3743 12425 if (ifc->formal && !ifc->formal_ns)
12426 resolve_symbol (ifc);
64e93293 12427 if (ifc->attr.intrinsic)
68c6e05c 12428 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
64e93293 12429
12430 if (ifc->result)
ff70e443 12431 {
12432 c->ts = ifc->result->ts;
12433 c->attr.allocatable = ifc->result->attr.allocatable;
12434 c->attr.pointer = ifc->result->attr.pointer;
12435 c->attr.dimension = ifc->result->attr.dimension;
12436 c->as = gfc_copy_array_spec (ifc->result->as);
59d5ada4 12437 c->attr.class_ok = ifc->result->attr.class_ok;
ff70e443 12438 }
12439 else
d6463863 12440 {
ff70e443 12441 c->ts = ifc->ts;
12442 c->attr.allocatable = ifc->attr.allocatable;
12443 c->attr.pointer = ifc->attr.pointer;
12444 c->attr.dimension = ifc->attr.dimension;
12445 c->as = gfc_copy_array_spec (ifc->as);
59d5ada4 12446 c->attr.class_ok = ifc->attr.class_ok;
ff70e443 12447 }
64e93293 12448 c->ts.interface = ifc;
12449 c->attr.function = ifc->attr.function;
12450 c->attr.subroutine = ifc->attr.subroutine;
64e93293 12451
64e93293 12452 c->attr.pure = ifc->attr.pure;
12453 c->attr.elemental = ifc->attr.elemental;
64e93293 12454 c->attr.recursive = ifc->attr.recursive;
12455 c->attr.always_explicit = ifc->attr.always_explicit;
de0c4488 12456 c->attr.ext_attr |= ifc->attr.ext_attr;
64e93293 12457 /* Copy char length. */
eeebe20b 12458 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
64e93293 12459 {
cf4b41d8 12460 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
cf4b41d8 12461 if (cl->length && !cl->resolved
60e19868 12462 && !gfc_resolve_expr (cl->length))
12463 return false;
cf4b41d8 12464 c->ts.u.cl = cl;
64e93293 12465 }
12466 }
64e93293 12467 }
12468 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12469 {
ffe221be 12470 /* Since PPCs are not implicitly typed, a PPC without an explicit
12471 interface must be a subroutine. */
12472 gfc_add_subroutine (&c->attr, c->name, &c->loc);
64e93293 12473 }
12474
fe9b08a2 12475 /* Procedure pointer components: Check PASS arg. */
09c509ed 12476 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12477 && !sym->attr.vtype)
fe9b08a2 12478 {
12479 gfc_symbol* me_arg;
12480
12481 if (c->tb->pass_arg)
12482 {
12483 gfc_formal_arglist* i;
12484
12485 /* If an explicit passing argument name is given, walk the arg-list
12486 and look for it. */
12487
12488 me_arg = NULL;
12489 c->tb->pass_arg_num = 1;
6777213b 12490 for (i = c->ts.interface->formal; i; i = i->next)
fe9b08a2 12491 {
12492 if (!strcmp (i->sym->name, c->tb->pass_arg))
12493 {
12494 me_arg = i->sym;
12495 break;
12496 }
12497 c->tb->pass_arg_num++;
12498 }
12499
12500 if (!me_arg)
12501 {
716da296 12502 gfc_error ("Procedure pointer component %qs with PASS(%s) "
12503 "at %L has no argument %qs", c->name,
fe9b08a2 12504 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12505 c->tb->error = 1;
60e19868 12506 return false;
fe9b08a2 12507 }
12508 }
12509 else
12510 {
12511 /* Otherwise, take the first one; there should in fact be at least
12512 one. */
12513 c->tb->pass_arg_num = 1;
6777213b 12514 if (!c->ts.interface->formal)
fe9b08a2 12515 {
716da296 12516 gfc_error ("Procedure pointer component %qs with PASS at %L "
fe9b08a2 12517 "must have at least one argument",
12518 c->name, &c->loc);
12519 c->tb->error = 1;
60e19868 12520 return false;
fe9b08a2 12521 }
6777213b 12522 me_arg = c->ts.interface->formal->sym;
fe9b08a2 12523 }
12524
12525 /* Now check that the argument-type matches. */
12526 gcc_assert (me_arg);
1de1b1a9 12527 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12528 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12529 || (me_arg->ts.type == BT_CLASS
50b4b37b 12530 && CLASS_DATA (me_arg)->ts.u.derived != sym))
fe9b08a2 12531 {
716da296 12532 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12533 " the derived type %qs", me_arg->name, c->name,
fe9b08a2 12534 me_arg->name, &c->loc, sym->name);
12535 c->tb->error = 1;
60e19868 12536 return false;
fe9b08a2 12537 }
12538
12539 /* Check for C453. */
12540 if (me_arg->attr.dimension)
12541 {
716da296 12542 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
fe9b08a2 12543 "must be scalar", me_arg->name, c->name, me_arg->name,
12544 &c->loc);
12545 c->tb->error = 1;
60e19868 12546 return false;
fe9b08a2 12547 }
12548
12549 if (me_arg->attr.pointer)
12550 {
716da296 12551 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
fe9b08a2 12552 "may not have the POINTER attribute", me_arg->name,
12553 c->name, me_arg->name, &c->loc);
12554 c->tb->error = 1;
60e19868 12555 return false;
fe9b08a2 12556 }
12557
12558 if (me_arg->attr.allocatable)
12559 {
716da296 12560 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
fe9b08a2 12561 "may not be ALLOCATABLE", me_arg->name, c->name,
12562 me_arg->name, &c->loc);
12563 c->tb->error = 1;
60e19868 12564 return false;
fe9b08a2 12565 }
12566
1de1b1a9 12567 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
716da296 12568 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
1de1b1a9 12569 " at %L", c->name, &c->loc);
fe9b08a2 12570
12571 }
12572
ac5f2650 12573 /* Check type-spec if this is not the parent-type component. */
5c3604f9 12574 if (((sym->attr.is_class
12575 && (!sym->components->ts.u.derived->attr.extension
12576 || c != sym->components->ts.u.derived->components))
12577 || (!sym->attr.is_class
12578 && (!sym->attr.extension || c != sym->components)))
12579 && !sym->attr.vtype
60e19868 12580 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12581 return false;
ac5f2650 12582
1b4ad9da 12583 /* If this type is an extension, set the accessibility of the parent
12584 component. */
5c3604f9 12585 if (super_type
12586 && ((sym->attr.is_class
12587 && c == sym->components->ts.u.derived->components)
12588 || (!sym->attr.is_class && c == sym->components))
1b4ad9da 12589 && strcmp (super_type->name, c->name) == 0)
12590 c->attr.access = super_type->attr.access;
d6463863 12591
f8f35c46 12592 /* If this type is an extension, see if this component has the same name
12593 as an inherited type-bound procedure. */
35820014 12594 if (super_type && !sym->attr.is_class
7d034542 12595 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
f8f35c46 12596 {
716da296 12597 gfc_error ("Component %qs of %qs at %L has the same name as an"
f8f35c46 12598 " inherited type-bound procedure",
12599 c->name, sym->name, &c->loc);
60e19868 12600 return false;
f8f35c46 12601 }
12602
617125a6 12603 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12604 && !c->ts.deferred)
ac42ecbd 12605 {
eeebe20b 12606 if (c->ts.u.cl->length == NULL
60e19868 12607 || (!resolve_charlen(c->ts.u.cl))
eeebe20b 12608 || !gfc_is_constant_expr (c->ts.u.cl->length))
ac42ecbd 12609 {
716da296 12610 gfc_error ("Character length of component %qs needs to "
7698a624 12611 "be a constant specification expression at %L",
ac42ecbd 12612 c->name,
eeebe20b 12613 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
60e19868 12614 return false;
ac42ecbd 12615 }
12616 }
12617
617125a6 12618 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12619 && !c->attr.pointer && !c->attr.allocatable)
12620 {
716da296 12621 gfc_error ("Character component %qs of %qs at %L with deferred "
617125a6 12622 "length must be a POINTER or ALLOCATABLE",
12623 c->name, sym->name, &c->loc);
60e19868 12624 return false;
617125a6 12625 }
12626
13d7216c 12627 /* Add the hidden deferred length field. */
12628 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
12629 && !sym->attr.is_class)
12630 {
12631 char name[GFC_MAX_SYMBOL_LEN+9];
12632 gfc_component *strlen;
12633 sprintf (name, "_%s_length", c->name);
12634 strlen = gfc_find_component (sym, name, true, true);
12635 if (strlen == NULL)
12636 {
12637 if (!gfc_add_component (sym, name, &strlen))
12638 return false;
12639 strlen->ts.type = BT_INTEGER;
12640 strlen->ts.kind = gfc_charlen_int_kind;
12641 strlen->attr.access = ACCESS_PRIVATE;
12642 strlen->attr.deferred_parameter = 1;
12643 }
12644 }
12645
693c40a7 12646 if (c->ts.type == BT_DERIVED
1bcc6eb8 12647 && sym->component_access != ACCESS_PRIVATE
924d51fd 12648 && gfc_check_symbol_access (sym)
eeebe20b 12649 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12650 && !c->ts.u.derived->attr.use_assoc
924d51fd 12651 && !gfc_check_symbol_access (c->ts.u.derived)
0d2b3c9c 12652 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
60e19868 12653 "PRIVATE type and cannot be a component of "
0d2b3c9c 12654 "%qs, which is PUBLIC at %L", c->name,
60e19868 12655 sym->name, &sym->declared_at))
12656 return false;
693c40a7 12657
e63a44ce 12658 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12659 {
12660 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12661 "type %s", c->name, &c->loc, sym->name);
60e19868 12662 return false;
e63a44ce 12663 }
12664
3b99e4a8 12665 if (sym->attr.sequence)
12666 {
eeebe20b 12667 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
3b99e4a8 12668 {
12669 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12670 "not have the SEQUENCE attribute",
eeebe20b 12671 c->ts.u.derived->name, &sym->declared_at);
60e19868 12672 return false;
3b99e4a8 12673 }
12674 }
12675
c2958b6b 12676 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12677 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12678 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12679 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12680 CLASS_DATA (c)->ts.u.derived
12681 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12682
4c33a6fa 12683 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12684 && c->attr.pointer && c->ts.u.derived->components == NULL
eeebe20b 12685 && !c->ts.u.derived->attr.zero_comp)
540338c6 12686 {
716da296 12687 gfc_error ("The pointer component %qs of %qs at %L is a type "
540338c6 12688 "that has not been declared", c->name, sym->name,
12689 &c->loc);
60e19868 12690 return false;
540338c6 12691 }
12692
91cb3b66 12693 if (c->ts.type == BT_CLASS && c->attr.class_ok
12694 && CLASS_DATA (c)->attr.class_pointer
50b4b37b 12695 && CLASS_DATA (c)->ts.u.derived->components == NULL
a90fe829 12696 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12697 && !UNLIMITED_POLY (c))
35820014 12698 {
716da296 12699 gfc_error ("The pointer component %qs of %qs at %L is a type "
35820014 12700 "that has not been declared", c->name, sym->name,
12701 &c->loc);
60e19868 12702 return false;
35820014 12703 }
12704
8ca7f89c 12705 /* C437. */
91cb3b66 12706 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12707 && (!c->attr.class_ok
12708 || !(CLASS_DATA (c)->attr.class_pointer
12709 || CLASS_DATA (c)->attr.allocatable)))
8ca7f89c 12710 {
716da296 12711 gfc_error ("Component %qs with CLASS at %L must be allocatable "
8ca7f89c 12712 "or pointer", c->name, &c->loc);
0377ee78 12713 /* Prevent a recurrence of the error. */
12714 c->ts.type = BT_UNKNOWN;
60e19868 12715 return false;
8ca7f89c 12716 }
12717
00be3612 12718 /* Ensure that all the derived type components are put on the
12719 derived type list; even in formal namespaces, where derived type
12720 pointer components might not have been declared. */
12721 if (c->ts.type == BT_DERIVED
eeebe20b 12722 && c->ts.u.derived
12723 && c->ts.u.derived->components
3be2b8d5 12724 && c->attr.pointer
eeebe20b 12725 && sym != c->ts.u.derived)
12726 add_dt_to_dt_list (c->ts.u.derived);
00be3612 12727
080819af 12728 if (!gfc_resolve_array_spec (c->as,
12729 !(c->attr.pointer || c->attr.proc_pointer
60e19868 12730 || c->attr.allocatable)))
12731 return false;
16f7554b 12732
12733 if (c->initializer && !sym->attr.vtype
60e19868 12734 && !gfc_check_assign_symbol (sym, c, c->initializer))
12735 return false;
ac42ecbd 12736 }
fc243266 12737
d6463863 12738 check_defined_assignments (sym);
12739
12740 if (!sym->attr.defined_assign_comp && super_type)
12741 sym->attr.defined_assign_comp
12742 = super_type->attr.defined_assign_comp;
12743
61c3b81d 12744 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12745 all DEFERRED bindings are overridden. */
12746 if (super_type && super_type->attr.abstract && !sym->attr.abstract
c420ccbb 12747 && !sym->attr.is_class
60e19868 12748 && !ensure_not_abstract (sym, super_type))
12749 return false;
61c3b81d 12750
a9c39401 12751 /* Add derived type to the derived type list. */
00be3612 12752 add_dt_to_dt_list (sym);
a9c39401 12753
60e19868 12754 return true;
ac42ecbd 12755}
12756
693c40a7 12757
f959368d 12758/* The following procedure does the full resolution of a derived type,
12759 including resolution of all type-bound procedures (if present). In contrast
12760 to 'resolve_fl_derived0' this can only be done after the module has been
12761 parsed completely. */
12762
60e19868 12763static bool
f959368d 12764resolve_fl_derived (gfc_symbol *sym)
12765{
c2958b6b 12766 gfc_symbol *gen_dt = NULL;
12767
a90fe829 12768 if (sym->attr.unlimited_polymorphic)
60e19868 12769 return true;
a90fe829 12770
c2958b6b 12771 if (!sym->attr.is_class)
12772 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12773 if (gen_dt && gen_dt->generic && gen_dt->generic->next
ae79d05f 12774 && (!gen_dt->generic->sym->attr.use_assoc
12775 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
0d2b3c9c 12776 && !gfc_notify_std_1 (GFC_STD_F2003, "Generic name '%s' of function "
60e19868 12777 "'%s' at %L being the same name as derived "
080819af 12778 "type at %L", sym->name,
12779 gen_dt->generic->sym == sym
12780 ? gen_dt->generic->next->sym->name
12781 : gen_dt->generic->sym->name,
12782 gen_dt->generic->sym == sym
12783 ? &gen_dt->generic->next->sym->declared_at
12784 : &gen_dt->generic->sym->declared_at,
60e19868 12785 &sym->declared_at))
12786 return false;
c2958b6b 12787
8a96d642 12788 /* Resolve the finalizer procedures. */
baf14602 12789 if (!gfc_resolve_finalizers (sym, NULL))
60e19868 12790 return false;
d6463863 12791
f959368d 12792 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12793 {
12794 /* Fix up incomplete CLASS symbols. */
12795 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12796 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
a90fe829 12797
12798 /* Nothing more to do for unlimited polymorphic entities. */
12799 if (data->ts.u.derived->attr.unlimited_polymorphic)
60e19868 12800 return true;
a90fe829 12801 else if (vptr->ts.u.derived == NULL)
f959368d 12802 {
12803 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12804 gcc_assert (vtab);
12805 vptr->ts.u.derived = vtab->ts.u.derived;
12806 }
12807 }
d6463863 12808
60e19868 12809 if (!resolve_fl_derived0 (sym))
12810 return false;
d6463863 12811
f959368d 12812 /* Resolve the type-bound procedures. */
60e19868 12813 if (!resolve_typebound_procedures (sym))
12814 return false;
f959368d 12815
60e19868 12816 return true;
f959368d 12817}
12818
12819
60e19868 12820static bool
199bf9f5 12821resolve_fl_namelist (gfc_symbol *sym)
12822{
12823 gfc_namelist *nl;
12824 gfc_symbol *nlsym;
12825
9a956fc7 12826 for (nl = sym->namelist; nl; nl = nl->next)
12827 {
95fad61e 12828 /* Check again, the check in match only works if NAMELIST comes
12829 after the decl. */
12830 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12831 {
0d2b3c9c 12832 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
95fad61e 12833 "allowed", nl->sym->name, sym->name, &sym->declared_at);
60e19868 12834 return false;
95fad61e 12835 }
12836
9a956fc7 12837 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
0d2b3c9c 12838 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
12839 "with assumed shape in namelist %qs at %L",
60e19868 12840 nl->sym->name, sym->name, &sym->declared_at))
12841 return false;
9a956fc7 12842
95fad61e 12843 if (is_non_constant_shape_array (nl->sym)
0d2b3c9c 12844 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
12845 "with nonconstant shape in namelist %qs at %L",
60e19868 12846 nl->sym->name, sym->name, &sym->declared_at))
12847 return false;
9a956fc7 12848
95fad61e 12849 if (nl->sym->ts.type == BT_CHARACTER
12850 && (nl->sym->ts.u.cl->length == NULL
12851 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
0d2b3c9c 12852 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
60e19868 12853 "nonconstant character length in "
0d2b3c9c 12854 "namelist %qs at %L", nl->sym->name,
60e19868 12855 sym->name, &sym->declared_at))
12856 return false;
9a956fc7 12857
95fad61e 12858 /* FIXME: Once UDDTIO is implemented, the following can be
12859 removed. */
12860 if (nl->sym->ts.type == BT_CLASS)
9a956fc7 12861 {
0d2b3c9c 12862 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
95fad61e 12863 "polymorphic and requires a defined input/output "
12864 "procedure", nl->sym->name, sym->name, &sym->declared_at);
60e19868 12865 return false;
9a956fc7 12866 }
12867
95fad61e 12868 if (nl->sym->ts.type == BT_DERIVED
12869 && (nl->sym->ts.u.derived->attr.alloc_comp
12870 || nl->sym->ts.u.derived->attr.pointer_comp))
9a956fc7 12871 {
0d2b3c9c 12872 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
12873 "namelist %qs at %L with ALLOCATABLE "
080819af 12874 "or POINTER components", nl->sym->name,
60e19868 12875 sym->name, &sym->declared_at))
12876 return false;
95fad61e 12877
12878 /* FIXME: Once UDDTIO is implemented, the following can be
12879 removed. */
0d2b3c9c 12880 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
95fad61e 12881 "ALLOCATABLE or POINTER components and thus requires "
12882 "a defined input/output procedure", nl->sym->name,
12883 sym->name, &sym->declared_at);
60e19868 12884 return false;
9a956fc7 12885 }
12886 }
12887
199bf9f5 12888 /* Reject PRIVATE objects in a PUBLIC namelist. */
924d51fd 12889 if (gfc_check_symbol_access (sym))
199bf9f5 12890 {
12891 for (nl = sym->namelist; nl; nl = nl->next)
12892 {
28b240bf 12893 if (!nl->sym->attr.use_assoc
9386f343 12894 && !is_sym_host_assoc (nl->sym, sym->ns)
924d51fd 12895 && !gfc_check_symbol_access (nl->sym))
199bf9f5 12896 {
0d2b3c9c 12897 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
12898 "cannot be member of PUBLIC namelist %qs at %L",
135fdccf 12899 nl->sym->name, sym->name, &sym->declared_at);
60e19868 12900 return false;
135fdccf 12901 }
12902
28b240bf 12903 /* Types with private components that came here by USE-association. */
12904 if (nl->sym->ts.type == BT_DERIVED
eeebe20b 12905 && derived_inaccessible (nl->sym->ts.u.derived))
28b240bf 12906 {
0d2b3c9c 12907 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
12908 "components and cannot be member of namelist %qs at %L",
28b240bf 12909 nl->sym->name, sym->name, &sym->declared_at);
60e19868 12910 return false;
28b240bf 12911 }
12912
12913 /* Types with private components that are defined in the same module. */
135fdccf 12914 if (nl->sym->ts.type == BT_DERIVED
eeebe20b 12915 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
924d51fd 12916 && nl->sym->ts.u.derived->attr.private_comp)
135fdccf 12917 {
0d2b3c9c 12918 gfc_error ("NAMELIST object %qs has PRIVATE components and "
12919 "cannot be a member of PUBLIC namelist %qs at %L",
135fdccf 12920 nl->sym->name, sym->name, &sym->declared_at);
60e19868 12921 return false;
199bf9f5 12922 }
12923 }
12924 }
12925
135fdccf 12926
199bf9f5 12927 /* 14.1.2 A module or internal procedure represent local entities
bc055333 12928 of the same type as a namelist member and so are not allowed. */
199bf9f5 12929 for (nl = sym->namelist; nl; nl = nl->next)
12930 {
540338c6 12931 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12932 continue;
bc055333 12933
12934 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12935 if ((nl->sym == sym->ns->proc_name)
12936 ||
12937 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12938 continue;
12939
199bf9f5 12940 nlsym = NULL;
10281157 12941 if (nl->sym->name)
bc055333 12942 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
540338c6 12943 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12944 {
12945 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
0d2b3c9c 12946 "attribute in %qs at %L", nlsym->name,
540338c6 12947 &sym->declared_at);
60e19868 12948 return false;
540338c6 12949 }
199bf9f5 12950 }
12951
60e19868 12952 return true;
199bf9f5 12953}
12954
12955
60e19868 12956static bool
693c40a7 12957resolve_fl_parameter (gfc_symbol *sym)
12958{
12959 /* A parameter array's shape needs to be constant. */
d6463863 12960 if (sym->as != NULL
30ec1ccc 12961 && (sym->as->type == AS_DEFERRED
12962 || is_non_constant_shape_array (sym)))
693c40a7 12963 {
0d2b3c9c 12964 gfc_error ("Parameter array %qs at %L cannot be automatic "
30ec1ccc 12965 "or of deferred shape", sym->name, &sym->declared_at);
60e19868 12966 return false;
693c40a7 12967 }
12968
12969 /* Make sure a parameter that has been implicitly typed still
12970 matches the implicit type, since PARAMETER statements can precede
12971 IMPLICIT statements. */
12972 if (sym->attr.implicit_type
64e93293 12973 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12974 sym->ns)))
693c40a7 12975 {
0d2b3c9c 12976 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
693c40a7 12977 "later IMPLICIT type", sym->name, &sym->declared_at);
60e19868 12978 return false;
693c40a7 12979 }
12980
12981 /* Make sure the types of derived parameters are consistent. This
12982 type checking is deferred until resolution because the type may
12983 refer to a derived type from the host. */
98976ae1 12984 if (sym->ts.type == BT_DERIVED
1bcc6eb8 12985 && !gfc_compare_types (&sym->ts, &sym->value->ts))
693c40a7 12986 {
12987 gfc_error ("Incompatible derived type in PARAMETER at %L",
12988 &sym->value->where);
60e19868 12989 return false;
693c40a7 12990 }
60e19868 12991 return true;
693c40a7 12992}
12993
12994
4ee9c684 12995/* Do anything necessary to resolve a symbol. Right now, we just
12996 assume that an otherwise unknown symbol is a variable. This sort
12997 of thing commonly happens for symbols in module. */
12998
12999static void
1bcc6eb8 13000resolve_symbol (gfc_symbol *sym)
4ee9c684 13001{
9ef860ba 13002 int check_constant, mp_flag;
3d7e03fa 13003 gfc_symtree *symtree;
13004 gfc_symtree *this_symtree;
13005 gfc_namespace *ns;
13006 gfc_component *c;
5c3604f9 13007 symbol_attribute class_attr;
13008 gfc_array_spec *as;
be844014 13009 bool saved_specification_expr;
4ee9c684 13010
acd1bde2 13011 if (sym->resolved)
13012 return;
13013 sym->resolved = 1;
13014
8a96d642 13015 if (sym->attr.artificial)
13016 return;
13017
a90fe829 13018 if (sym->attr.unlimited_polymorphic)
13019 return;
13020
f455901f 13021 if (sym->attr.flavor == FL_UNKNOWN
13022 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13023 && !sym->attr.generic && !sym->attr.external
92b18bc0 13024 && sym->attr.if_source == IFSRC_UNKNOWN
13025 && sym->ts.type == BT_UNKNOWN))
4ee9c684 13026 {
e2c1060c 13027
13028 /* If we find that a flavorless symbol is an interface in one of the
13029 parent namespaces, find its symtree in this namespace, free the
13030 symbol and set the symtree to point to the interface symbol. */
13031 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13032 {
13033 symtree = gfc_find_symtree (ns->sym_root, sym->name);
9dff0fca 13034 if (symtree && (symtree->n.sym->generic ||
13035 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13036 && sym->ns->construct_entities)))
e2c1060c 13037 {
13038 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13039 sym->name);
7d16ae15 13040 gfc_release_symbol (sym);
e2c1060c 13041 symtree->n.sym->refs++;
13042 this_symtree->n.sym = symtree->n.sym;
13043 return;
13044 }
13045 }
13046
13047 /* Otherwise give it a flavor according to such attributes as
13048 it has. */
f455901f 13049 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13050 && sym->attr.intrinsic == 0)
4ee9c684 13051 sym->attr.flavor = FL_VARIABLE;
f455901f 13052 else if (sym->attr.flavor == FL_UNKNOWN)
4ee9c684 13053 {
13054 sym->attr.flavor = FL_PROCEDURE;
13055 if (sym->attr.dimension)
13056 sym->attr.function = 1;
13057 }
13058 }
13059
180a5dc0 13060 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13061 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13062
87863b31 13063 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
60e19868 13064 && !resolve_procedure_interface (sym))
f161695e 13065 return;
af1a34ee 13066
4e1f7cdd 13067 if (sym->attr.is_protected && !sym->attr.proc_pointer
13068 && (sym->attr.procedure || sym->attr.external))
13069 {
13070 if (sym->attr.external)
13071 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13072 "at %L", &sym->declared_at);
13073 else
13074 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13075 "at %L", &sym->declared_at);
13076
13077 return;
13078 }
13079
60e19868 13080 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
ac42ecbd 13081 return;
13082
4ee9c684 13083 /* Symbols that are module procedures with results (functions) have
13084 the types and array specification copied for type checking in
13085 procedures that call them, as well as for saving to a module
13086 file. These symbols can't stand the scrutiny that their results
13087 can. */
13088 mp_flag = (sym->result != NULL && sym->result != sym);
13089
d6463863 13090 /* Make sure that the intrinsic is consistent with its internal
13091 representation. This needs to be done before assigning a default
73764586 13092 type to avoid spurious warnings. */
2eb87b8c 13093 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
60e19868 13094 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
2eb87b8c 13095 return;
73764586 13096
cf92f151 13097 /* Resolve associate names. */
d18a512a 13098 if (sym->assoc)
cf92f151 13099 resolve_assoc_var (sym, true);
d18a512a 13100
4ee9c684 13101 /* Assign default type to symbols that need one and don't have one. */
13102 if (sym->ts.type == BT_UNKNOWN)
13103 {
13104 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
5c3604f9 13105 {
13106 gfc_set_default_type (sym, 1, NULL);
13107 }
4ee9c684 13108
e40ac2fe 13109 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13110 && !sym->attr.function && !sym->attr.subroutine
13111 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13112 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13113
4ee9c684 13114 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13115 {
10387833 13116 /* The specific case of an external procedure should emit an error
13117 in the case that there is no implicit type. */
4ee9c684 13118 if (!mp_flag)
10387833 13119 gfc_set_default_type (sym, sym->attr.external, NULL);
4ee9c684 13120 else
13121 {
1bcc6eb8 13122 /* Result may be in another namespace. */
4ee9c684 13123 resolve_symbol (sym->result);
13124
1e057e9b 13125 if (!sym->result->attr.proc_pointer)
13126 {
13127 sym->ts = sym->result->ts;
13128 sym->as = gfc_copy_array_spec (sym->result->as);
13129 sym->attr.dimension = sym->result->attr.dimension;
13130 sym->attr.pointer = sym->result->attr.pointer;
13131 sym->attr.allocatable = sym->result->attr.allocatable;
b3c3927c 13132 sym->attr.contiguous = sym->result->attr.contiguous;
1e057e9b 13133 }
4ee9c684 13134 }
13135 }
13136 }
e9246410 13137 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
be844014 13138 {
13139 bool saved_specification_expr = specification_expr;
13140 specification_expr = true;
13141 gfc_resolve_array_spec (sym->result->as, false);
13142 specification_expr = saved_specification_expr;
13143 }
4ee9c684 13144
5c3604f9 13145 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13146 {
13147 as = CLASS_DATA (sym)->as;
13148 class_attr = CLASS_DATA (sym)->attr;
13149 class_attr.pointer = class_attr.class_pointer;
13150 }
13151 else
13152 {
13153 class_attr = sym->attr;
13154 as = sym->as;
13155 }
13156
293d72e0 13157 /* F2008, C530. */
5c3604f9 13158 if (sym->attr.contiguous
13159 && (!class_attr.dimension
8a96d642 13160 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13161 && !class_attr.pointer)))
5c3604f9 13162 {
0d2b3c9c 13163 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
8a96d642 13164 "array pointer or an assumed-shape or assumed-rank array",
13165 sym->name, &sym->declared_at);
5c3604f9 13166 return;
13167 }
13168
ef9d8353 13169 /* Assumed size arrays and assumed shape arrays must be dummy
ae2864a8 13170 arguments. Array-spec's of implied-shape should have been resolved to
13171 AS_EXPLICIT already. */
ef9d8353 13172
5c3604f9 13173 if (as)
4ee9c684 13174 {
5c3604f9 13175 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13176 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13177 || as->type == AS_ASSUMED_SHAPE)
461db9e3 13178 && !sym->attr.dummy && !sym->attr.select_type_temporary)
ae2864a8 13179 {
5c3604f9 13180 if (as->type == AS_ASSUMED_SIZE)
ae2864a8 13181 gfc_error ("Assumed size array at %L must be a dummy argument",
13182 &sym->declared_at);
13183 else
13184 gfc_error ("Assumed shape array at %L must be a dummy argument",
13185 &sym->declared_at);
13186 return;
13187 }
f00f6dd6 13188 /* TS 29113, C535a. */
461db9e3 13189 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13190 && !sym->attr.select_type_temporary)
f00f6dd6 13191 {
13192 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13193 &sym->declared_at);
13194 return;
13195 }
13196 if (as->type == AS_ASSUMED_RANK
13197 && (sym->attr.codimension || sym->attr.value))
13198 {
13199 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13200 "CODIMENSION attribute", &sym->declared_at);
13201 return;
13202 }
6a7528d5 13203 }
13204
4ee9c684 13205 /* Make sure symbols with known intent or optional are really dummy
13206 variable. Because of ENTRY statement, this has to be deferred
13207 until resolution time. */
13208
693c40a7 13209 if (!sym->attr.dummy
1bcc6eb8 13210 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
4ee9c684 13211 {
13212 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13213 return;
13214 }
13215
8f6339b6 13216 if (sym->attr.value && !sym->attr.dummy)
13217 {
0d2b3c9c 13218 gfc_error ("%qs at %L cannot have the VALUE attribute because "
bdeef0b1 13219 "it is not a dummy argument", sym->name, &sym->declared_at);
8f6339b6 13220 return;
13221 }
13222
bdeef0b1 13223 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13224 {
eeebe20b 13225 gfc_charlen *cl = sym->ts.u.cl;
bdeef0b1 13226 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13227 {
0d2b3c9c 13228 gfc_error ("Character dummy variable %qs at %L with VALUE "
bdeef0b1 13229 "attribute must have constant length",
13230 sym->name, &sym->declared_at);
13231 return;
13232 }
c5d33754 13233
13234 if (sym->ts.is_c_interop
13235 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13236 {
0d2b3c9c 13237 gfc_error ("C interoperable character dummy variable %qs at %L "
c5d33754 13238 "with VALUE attribute must have length one",
13239 sym->name, &sym->declared_at);
13240 return;
13241 }
13242 }
13243
c2958b6b 13244 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13245 && sym->ts.u.derived->attr.generic)
13246 {
13247 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13248 if (!sym->ts.u.derived)
13249 {
0d2b3c9c 13250 gfc_error ("The derived type %qs at %L is of type %qs, "
c2958b6b 13251 "which has not been defined", sym->name,
13252 &sym->declared_at, sym->ts.u.derived->name);
13253 sym->ts.type = BT_UNKNOWN;
13254 return;
13255 }
13256 }
13257
fa76a552 13258 /* Use the same constraints as TYPE(*), except for the type check
13259 and that only scalars and assumed-size arrays are permitted. */
13260 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13261 {
13262 if (!sym->attr.dummy)
13263 {
13264 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13265 "a dummy argument", sym->name, &sym->declared_at);
13266 return;
13267 }
13268
13269 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13270 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13271 && sym->ts.type != BT_COMPLEX)
13272 {
13273 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13274 "of type TYPE(*) or of an numeric intrinsic type",
13275 sym->name, &sym->declared_at);
13276 return;
13277 }
13278
13279 if (sym->attr.allocatable || sym->attr.codimension
13280 || sym->attr.pointer || sym->attr.value)
13281 {
13282 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13283 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13284 "attribute", sym->name, &sym->declared_at);
13285 return;
13286 }
13287
13288 if (sym->attr.intent == INTENT_OUT)
13289 {
13290 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13291 "have the INTENT(OUT) attribute",
13292 sym->name, &sym->declared_at);
13293 return;
13294 }
13295 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13296 {
13297 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13298 "either be a scalar or an assumed-size array",
13299 sym->name, &sym->declared_at);
13300 return;
13301 }
13302
13303 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13304 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13305 packing. */
13306 sym->ts.type = BT_ASSUMED;
13307 sym->as = gfc_get_array_spec ();
13308 sym->as->type = AS_ASSUMED_SIZE;
13309 sym->as->rank = 1;
13310 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13311 }
13312 else if (sym->ts.type == BT_ASSUMED)
d6463863 13313 {
8c2d8d6d 13314 /* TS 29113, C407a. */
13315 if (!sym->attr.dummy)
13316 {
13317 gfc_error ("Assumed type of variable %s at %L is only permitted "
13318 "for dummy variables", sym->name, &sym->declared_at);
13319 return;
13320 }
13321 if (sym->attr.allocatable || sym->attr.codimension
13322 || sym->attr.pointer || sym->attr.value)
13323 {
13324 gfc_error ("Assumed-type variable %s at %L may not have the "
13325 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13326 sym->name, &sym->declared_at);
13327 return;
13328 }
f00f6dd6 13329 if (sym->attr.intent == INTENT_OUT)
13330 {
13331 gfc_error ("Assumed-type variable %s at %L may not have the "
13332 "INTENT(OUT) attribute",
13333 sym->name, &sym->declared_at);
13334 return;
13335 }
8c2d8d6d 13336 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13337 {
13338 gfc_error ("Assumed-type variable %s at %L shall not be an "
13339 "explicit-shape array", sym->name, &sym->declared_at);
13340 return;
13341 }
13342 }
13343
c5d33754 13344 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13345 do this for something that was implicitly typed because that is handled
13346 in gfc_set_default_type. Handle dummy arguments and procedure
13347 definitions separately. Also, anything that is use associated is not
13348 handled here but instead is handled in the module it is declared in.
13349 Finally, derived type definitions are allowed to be BIND(C) since that
13350 only implies that they're interoperable, and they are checked fully for
13351 interoperability when a variable is declared of that type. */
13352 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13353 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13354 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13355 {
60e19868 13356 bool t = true;
d6463863 13357
c5d33754 13358 /* First, make sure the variable is declared at the
13359 module-level scope (J3/04-007, Section 15.3). */
13360 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13361 sym->attr.in_common == 0)
13362 {
716da296 13363 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
c5d33754 13364 "is neither a COMMON block nor declared at the "
13365 "module level scope", sym->name, &(sym->declared_at));
60e19868 13366 t = false;
c5d33754 13367 }
13368 else if (sym->common_head != NULL)
13369 {
13370 t = verify_com_block_vars_c_interop (sym->common_head);
13371 }
13372 else
13373 {
13374 /* If type() declaration, we need to verify that the components
13375 of the given type are all C interoperable, etc. */
13376 if (sym->ts.type == BT_DERIVED &&
eeebe20b 13377 sym->ts.u.derived->attr.is_c_interop != 1)
c5d33754 13378 {
13379 /* Make sure the user marked the derived type as BIND(C). If
13380 not, call the verify routine. This could print an error
13381 for the derived type more than once if multiple variables
13382 of that type are declared. */
eeebe20b 13383 if (sym->ts.u.derived->attr.is_bind_c != 1)
13384 verify_bind_c_derived_type (sym->ts.u.derived);
60e19868 13385 t = false;
c5d33754 13386 }
d6463863 13387
c5d33754 13388 /* Verify the variable itself as C interoperable if it
13389 is BIND(C). It is not possible for this to succeed if
13390 the verify_bind_c_derived_type failed, so don't have to handle
13391 any error returned by verify_bind_c_derived_type. */
13392 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13393 sym->common_block);
13394 }
13395
60e19868 13396 if (!t)
c5d33754 13397 {
13398 /* clear the is_bind_c flag to prevent reporting errors more than
13399 once if something failed. */
13400 sym->attr.is_bind_c = 0;
13401 return;
13402 }
bdeef0b1 13403 }
13404
40cf8078 13405 /* If a derived type symbol has reached this point, without its
13406 type being declared, we have an error. Notice that most
13407 conditions that produce undefined derived types have already
13408 been dealt with. However, the likes of:
13409 implicit type(t) (t) ..... call foo (t) will get us here if
13410 the type is not declared in the scope of the implicit
13411 statement. Change the type to BT_UNKNOWN, both because it is so
13412 and to prevent an ICE. */
c2958b6b 13413 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13414 && sym->ts.u.derived->components == NULL
eeebe20b 13415 && !sym->ts.u.derived->attr.zero_comp)
40cf8078 13416 {
0d2b3c9c 13417 gfc_error ("The derived type %qs at %L is of type %qs, "
7698a624 13418 "which has not been defined", sym->name,
eeebe20b 13419 &sym->declared_at, sym->ts.u.derived->name);
40cf8078 13420 sym->ts.type = BT_UNKNOWN;
13421 return;
13422 }
13423
9277c291 13424 /* Make sure that the derived type has been resolved and that the
13425 derived type is visible in the symbol's namespace, if it is a
13426 module function and is not PRIVATE. */
13427 if (sym->ts.type == BT_DERIVED
eeebe20b 13428 && sym->ts.u.derived->attr.use_assoc
fc12e066 13429 && sym->ns->proc_name
c2958b6b 13430 && sym->ns->proc_name->attr.flavor == FL_MODULE
60e19868 13431 && !resolve_fl_derived (sym->ts.u.derived))
c2958b6b 13432 return;
9277c291 13433
3a8f9efd 13434 /* Unless the derived-type declaration is use associated, Fortran 95
13435 does not allow public entries of private derived types.
13436 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13437 161 in 95-006r3. */
13438 if (sym->ts.type == BT_DERIVED
9d3724c2 13439 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
eeebe20b 13440 && !sym->ts.u.derived->attr.use_assoc
924d51fd 13441 && gfc_check_symbol_access (sym)
13442 && !gfc_check_symbol_access (sym->ts.u.derived)
0d2b3c9c 13443 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
13444 "derived type %qs",
080819af 13445 (sym->attr.flavor == FL_PARAMETER)
13446 ? "parameter" : "variable",
13447 sym->name, &sym->declared_at,
60e19868 13448 sym->ts.u.derived->name))
3a8f9efd 13449 return;
13450
c135f087 13451 /* F2008, C1302. */
13452 if (sym->ts.type == BT_DERIVED
50fefeb7 13453 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13454 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13455 || sym->ts.u.derived->attr.lock_comp)
13456 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
c135f087 13457 {
50fefeb7 13458 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13459 "type LOCK_TYPE must be a coarray", sym->name,
13460 &sym->declared_at);
c135f087 13461 return;
13462 }
13463
ea13b9b7 13464 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13465 default initialization is defined (5.1.2.4.4). */
13466 if (sym->ts.type == BT_DERIVED
1bcc6eb8 13467 && sym->attr.dummy
13468 && sym->attr.intent == INTENT_OUT
13469 && sym->as
13470 && sym->as->type == AS_ASSUMED_SIZE)
ea13b9b7 13471 {
eeebe20b 13472 for (c = sym->ts.u.derived->components; c; c = c->next)
ea13b9b7 13473 {
13474 if (c->initializer)
13475 {
0d2b3c9c 13476 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
ea13b9b7 13477 "ASSUMED SIZE and so cannot have a default initializer",
13478 sym->name, &sym->declared_at);
13479 return;
13480 }
13481 }
13482 }
13483
c135f087 13484 /* F2008, C542. */
13485 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13486 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
76fa5db4 13487 {
0d2b3c9c 13488 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
76fa5db4 13489 "INTENT(OUT)", sym->name, &sym->declared_at);
13490 return;
13491 }
c135f087 13492
76fa5db4 13493 /* F2008, C525. */
5c3604f9 13494 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13495 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13496 && CLASS_DATA (sym)->attr.coarray_comp))
13497 || class_attr.codimension)
76fa5db4 13498 && (sym->attr.result || sym->result == sym))
13499 {
0d2b3c9c 13500 gfc_error ("Function result %qs at %L shall not be a coarray or have "
76fa5db4 13501 "a coarray component", sym->name, &sym->declared_at);
13502 return;
13503 }
aff518b0 13504
13505 /* F2008, C524. */
13506 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13507 && sym->ts.u.derived->ts.is_iso_c)
76fa5db4 13508 {
0d2b3c9c 13509 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
76fa5db4 13510 "shall not be a coarray", sym->name, &sym->declared_at);
13511 return;
13512 }
aff518b0 13513
13514 /* F2008, C525. */
5c3604f9 13515 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13516 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13517 && CLASS_DATA (sym)->attr.coarray_comp))
13518 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13519 || class_attr.allocatable))
76fa5db4 13520 {
0d2b3c9c 13521 gfc_error ("Variable %qs at %L with coarray component shall be a "
2dec1d1b 13522 "nonpointer, nonallocatable scalar, which is not a coarray",
76fa5db4 13523 sym->name, &sym->declared_at);
13524 return;
13525 }
aff518b0 13526
13527 /* F2008, C526. The function-result case was handled above. */
5c3604f9 13528 if (class_attr.codimension
13529 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13530 || sym->attr.select_type_temporary
7c7db7f6 13531 || sym->ns->save_all
aff518b0 13532 || sym->ns->proc_name->attr.flavor == FL_MODULE
13533 || sym->ns->proc_name->attr.is_main_program
13534 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
76fa5db4 13535 {
0d2b3c9c 13536 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
76fa5db4 13537 "nor a dummy argument", sym->name, &sym->declared_at);
13538 return;
13539 }
5c3604f9 13540 /* F2008, C528. */
13541 else if (class_attr.codimension && !sym->attr.select_type_temporary
13542 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
76fa5db4 13543 {
0d2b3c9c 13544 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
76fa5db4 13545 "deferred shape", sym->name, &sym->declared_at);
13546 return;
13547 }
5c3604f9 13548 else if (class_attr.codimension && class_attr.allocatable && as
13549 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
76fa5db4 13550 {
0d2b3c9c 13551 gfc_error ("Allocatable coarray variable %qs at %L must have "
76fa5db4 13552 "deferred shape", sym->name, &sym->declared_at);
13553 return;
13554 }
aff518b0 13555
13556 /* F2008, C541. */
5c3604f9 13557 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13558 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13559 && CLASS_DATA (sym)->attr.coarray_comp))
13560 || (class_attr.codimension && class_attr.allocatable))
aff518b0 13561 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
76fa5db4 13562 {
0d2b3c9c 13563 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
76fa5db4 13564 "allocatable coarray or have coarray components",
13565 sym->name, &sym->declared_at);
13566 return;
13567 }
aff518b0 13568
5c3604f9 13569 if (class_attr.codimension && sym->attr.dummy
aff518b0 13570 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
76fa5db4 13571 {
0d2b3c9c 13572 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
13573 "procedure %qs", sym->name, &sym->declared_at,
76fa5db4 13574 sym->ns->proc_name->name);
13575 return;
13576 }
aff518b0 13577
3bbb71ba 13578 if (sym->ts.type == BT_LOGICAL
13579 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13580 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13581 && sym->ns->proc_name->attr.is_bind_c)))
13582 {
13583 int i;
13584 for (i = 0; gfc_logical_kinds[i].kind; i++)
13585 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13586 break;
13587 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
0d2b3c9c 13588 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
60e19868 13589 "%L with non-C_Bool kind in BIND(C) procedure "
0d2b3c9c 13590 "%qs", sym->name, &sym->declared_at,
60e19868 13591 sym->ns->proc_name->name))
3bbb71ba 13592 return;
13593 else if (!gfc_logical_kinds[i].c_bool
60e19868 13594 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
0d2b3c9c 13595 "%qs at %L with non-C_Bool kind in "
13596 "BIND(C) procedure %qs", sym->name,
080819af 13597 &sym->declared_at,
13598 sym->attr.function ? sym->name
60e19868 13599 : sym->ns->proc_name->name))
3bbb71ba 13600 return;
13601 }
13602
51d7446c 13603 switch (sym->attr.flavor)
d9b3f26b 13604 {
51d7446c 13605 case FL_VARIABLE:
60e19868 13606 if (!resolve_fl_variable (sym, mp_flag))
693c40a7 13607 return;
13608 break;
3d7e03fa 13609
693c40a7 13610 case FL_PROCEDURE:
60e19868 13611 if (!resolve_fl_procedure (sym, mp_flag))
693c40a7 13612 return;
51d7446c 13613 break;
13614
13615 case FL_NAMELIST:
60e19868 13616 if (!resolve_fl_namelist (sym))
199bf9f5 13617 return;
858f9894 13618 break;
13619
693c40a7 13620 case FL_PARAMETER:
60e19868 13621 if (!resolve_fl_parameter (sym))
693c40a7 13622 return;
840e5aa1 13623 break;
13624
51d7446c 13625 default:
13626 break;
d9b3f26b 13627 }
13628
4ee9c684 13629 /* Resolve array specifier. Check as well some constraints
b14e2757 13630 on COMMON blocks. */
4ee9c684 13631
13632 check_constant = sym->attr.in_common && !sym->attr.pointer;
e49f4c1f 13633
13634 /* Set the formal_arg_flag so that check_conflict will not throw
13635 an error for host associated variables in the specification
13636 expression for an array_valued function. */
13637 if (sym->attr.function && sym->as)
13638 formal_arg_flag = 1;
13639
be844014 13640 saved_specification_expr = specification_expr;
13641 specification_expr = true;
4ee9c684 13642 gfc_resolve_array_spec (sym->as, check_constant);
be844014 13643 specification_expr = saved_specification_expr;
4ee9c684 13644
e49f4c1f 13645 formal_arg_flag = 0;
13646
9ef860ba 13647 /* Resolve formal namespaces. */
f8fc09fd 13648 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
daaffbae 13649 && !sym->attr.contained && !sym->attr.intrinsic)
9ef860ba 13650 gfc_resolve (sym->formal_ns);
764f1175 13651
452a3743 13652 /* Make sure the formal namespace is present. */
13653 if (sym->formal && !sym->formal_ns)
13654 {
13655 gfc_formal_arglist *formal = sym->formal;
13656 while (formal && !formal->sym)
13657 formal = formal->next;
13658
13659 if (formal)
13660 {
13661 sym->formal_ns = formal->sym->ns;
94544b87 13662 if (sym->ns != formal->sym->ns)
13663 sym->formal_ns->refs++;
452a3743 13664 }
13665 }
13666
764f1175 13667 /* Check threadprivate restrictions. */
bc5d6438 13668 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
764f1175 13669 && (!sym->attr.in_common
1bcc6eb8 13670 && sym->module == NULL
13671 && (sym->ns->proc_name == NULL
13672 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
764f1175 13673 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
b9cd8c56 13674
691447ab 13675 /* Check omp declare target restrictions. */
13676 if (sym->attr.omp_declare_target
13677 && sym->attr.flavor == FL_VARIABLE
13678 && !sym->attr.save
13679 && !sym->ns->save_all
13680 && (!sym->attr.in_common
13681 && sym->module == NULL
13682 && (sym->ns->proc_name == NULL
13683 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
0d2b3c9c 13684 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
691447ab 13685 sym->name, &sym->declared_at);
13686
b9cd8c56 13687 /* If we have come this far we can apply default-initializers, as
13688 described in 14.7.5, to those variables that have not already
13689 been assigned one. */
25dd7350 13690 if (sym->ts.type == BT_DERIVED
1bcc6eb8 13691 && !sym->value
13692 && !sym->attr.allocatable
13693 && !sym->attr.alloc_comp)
b9cd8c56 13694 {
13695 symbol_attribute *a = &sym->attr;
13696
13697 if ((!a->save && !a->dummy && !a->pointer
1bcc6eb8 13698 && !a->in_common && !a->use_assoc
bc9d38fe 13699 && (a->referenced || a->result)
1bcc6eb8 13700 && !(a->function && sym != sym->result))
c49db15e 13701 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
b9cd8c56 13702 apply_default_init (sym);
13703 }
ac5f2650 13704
4c33a6fa 13705 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13706 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
3518a35b 13707 && !CLASS_DATA (sym)->attr.class_pointer
13708 && !CLASS_DATA (sym)->attr.allocatable)
bc9d38fe 13709 apply_default_init (sym);
4c33a6fa 13710
ac5f2650 13711 /* If this symbol has a type-spec, check it. */
13712 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13713 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
60e19868 13714 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
ac5f2650 13715 return;
4ee9c684 13716}
13717
13718
4ee9c684 13719/************* Resolve DATA statements *************/
13720
13721static struct
13722{
13723 gfc_data_value *vnode;
7d74abfd 13724 mpz_t left;
4ee9c684 13725}
13726values;
13727
13728
13729/* Advance the values structure to point to the next value in the data list. */
13730
60e19868 13731static bool
4ee9c684 13732next_data_value (void)
13733{
7d74abfd 13734 while (mpz_cmp_ui (values.left, 0) == 0)
4ee9c684 13735 {
a733be02 13736
4ee9c684 13737 if (values.vnode->next == NULL)
60e19868 13738 return false;
4ee9c684 13739
13740 values.vnode = values.vnode->next;
7d74abfd 13741 mpz_set (values.left, values.vnode->repeat);
4ee9c684 13742 }
13743
60e19868 13744 return true;
4ee9c684 13745}
13746
13747
60e19868 13748static bool
1bcc6eb8 13749check_data_variable (gfc_data_variable *var, locus *where)
4ee9c684 13750{
13751 gfc_expr *e;
13752 mpz_t size;
13753 mpz_t offset;
60e19868 13754 bool t;
ef9d8353 13755 ar_type mark = AR_UNKNOWN;
4ee9c684 13756 int i;
13757 mpz_t section_index[GFC_MAX_DIMENSIONS];
13758 gfc_ref *ref;
13759 gfc_array_ref *ar;
95002c27 13760 gfc_symbol *sym;
13761 int has_pointer;
4ee9c684 13762
60e19868 13763 if (!gfc_resolve_expr (var->expr))
13764 return false;
4ee9c684 13765
13766 ar = NULL;
13767 mpz_init_set_si (offset, 0);
13768 e = var->expr;
13769
13770 if (e->expr_type != EXPR_VARIABLE)
13771 gfc_internal_error ("check_data_variable(): Bad expression");
13772
95002c27 13773 sym = e->symtree->n.sym;
13774
13775 if (sym->ns->is_block_data && !sym->attr.in_common)
693c40a7 13776 {
0d2b3c9c 13777 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
95002c27 13778 sym->name, &sym->declared_at);
693c40a7 13779 }
13780
95002c27 13781 if (e->ref == NULL && sym->as)
a7e5a90a 13782 {
0d2b3c9c 13783 gfc_error ("DATA array %qs at %L must be specified in a previous"
95002c27 13784 " declaration", sym->name, where);
60e19868 13785 return false;
a7e5a90a 13786 }
13787
95002c27 13788 has_pointer = sym->attr.pointer;
13789
076094b7 13790 if (gfc_is_coindexed (e))
13791 {
0d2b3c9c 13792 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
076094b7 13793 where);
60e19868 13794 return false;
076094b7 13795 }
13796
95002c27 13797 for (ref = e->ref; ref; ref = ref->next)
13798 {
13799 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13800 has_pointer = 1;
13801
13802 if (has_pointer
13803 && ref->type == REF_ARRAY
13804 && ref->u.ar.type != AR_FULL)
13805 {
0d2b3c9c 13806 gfc_error ("DATA element %qs at %L is a pointer and so must "
95002c27 13807 "be a full array", sym->name, where);
60e19868 13808 return false;
95002c27 13809 }
13810 }
13811
13812 if (e->rank == 0 || has_pointer)
5770ea9d 13813 {
13814 mpz_init_set_ui (size, 1);
13815 ref = NULL;
13816 }
4ee9c684 13817 else
13818 {
13819 ref = e->ref;
13820
13821 /* Find the array section reference. */
13822 for (ref = e->ref; ref; ref = ref->next)
13823 {
13824 if (ref->type != REF_ARRAY)
13825 continue;
13826 if (ref->u.ar.type == AR_ELEMENT)
13827 continue;
13828 break;
13829 }
22d678e8 13830 gcc_assert (ref);
4ee9c684 13831
231e961a 13832 /* Set marks according to the reference pattern. */
4ee9c684 13833 switch (ref->u.ar.type)
13834 {
13835 case AR_FULL:
ef9d8353 13836 mark = AR_FULL;
4ee9c684 13837 break;
13838
13839 case AR_SECTION:
1bcc6eb8 13840 ar = &ref->u.ar;
13841 /* Get the start position of array section. */
13842 gfc_get_section_index (ar, section_index, &offset);
13843 mark = AR_SECTION;
4ee9c684 13844 break;
13845
13846 default:
22d678e8 13847 gcc_unreachable ();
4ee9c684 13848 }
13849
60e19868 13850 if (!gfc_array_size (e, &size))
4ee9c684 13851 {
13852 gfc_error ("Nonconstant array section at %L in DATA statement",
13853 &e->where);
13854 mpz_clear (offset);
60e19868 13855 return false;
4ee9c684 13856 }
13857 }
13858
60e19868 13859 t = true;
4ee9c684 13860
13861 while (mpz_cmp_ui (size, 0) > 0)
13862 {
60e19868 13863 if (!next_data_value ())
4ee9c684 13864 {
13865 gfc_error ("DATA statement at %L has more variables than values",
13866 where);
60e19868 13867 t = false;
4ee9c684 13868 break;
13869 }
13870
13871 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
60e19868 13872 if (!t)
4ee9c684 13873 break;
13874
5770ea9d 13875 /* If we have more than one element left in the repeat count,
13876 and we have more than one element left in the target variable,
13877 then create a range assignment. */
7d74abfd 13878 /* FIXME: Only done for full arrays for now, since array sections
5770ea9d 13879 seem tricky. */
13880 if (mark == AR_FULL && ref && ref->next == NULL
7d74abfd 13881 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
5770ea9d 13882 {
13883 mpz_t range;
13884
7d74abfd 13885 if (mpz_cmp (size, values.left) >= 0)
5770ea9d 13886 {
7d74abfd 13887 mpz_init_set (range, values.left);
13888 mpz_sub (size, size, values.left);
13889 mpz_set_ui (values.left, 0);
5770ea9d 13890 }
13891 else
13892 {
13893 mpz_init_set (range, size);
7d74abfd 13894 mpz_sub (values.left, values.left, size);
5770ea9d 13895 mpz_set_ui (size, 0);
13896 }
13897
dffd0df7 13898 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13899 offset, &range);
5770ea9d 13900
13901 mpz_add (offset, offset, range);
13902 mpz_clear (range);
53ee5847 13903
60e19868 13904 if (!t)
53ee5847 13905 break;
5770ea9d 13906 }
13907
4ee9c684 13908 /* Assign initial value to symbol. */
5770ea9d 13909 else
13910 {
7d74abfd 13911 mpz_sub_ui (values.left, values.left, 1);
5770ea9d 13912 mpz_sub_ui (size, size, 1);
4ee9c684 13913
dffd0df7 13914 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13915 offset, NULL);
60e19868 13916 if (!t)
2f427a5c 13917 break;
4ee9c684 13918
5770ea9d 13919 if (mark == AR_FULL)
13920 mpz_add_ui (offset, offset, 1);
4ee9c684 13921
5770ea9d 13922 /* Modify the array section indexes and recalculate the offset
13923 for next element. */
13924 else if (mark == AR_SECTION)
13925 gfc_advance_section (section_index, ar, &offset);
13926 }
4ee9c684 13927 }
5770ea9d 13928
ef9d8353 13929 if (mark == AR_SECTION)
4ee9c684 13930 {
13931 for (i = 0; i < ar->dimen; i++)
1bcc6eb8 13932 mpz_clear (section_index[i]);
4ee9c684 13933 }
13934
13935 mpz_clear (size);
13936 mpz_clear (offset);
13937
13938 return t;
13939}
13940
13941
60e19868 13942static bool traverse_data_var (gfc_data_variable *, locus *);
4ee9c684 13943
13944/* Iterate over a list of elements in a DATA statement. */
13945
60e19868 13946static bool
1bcc6eb8 13947traverse_data_list (gfc_data_variable *var, locus *where)
4ee9c684 13948{
13949 mpz_t trip;
13950 iterator_stack frame;
1bfea7e8 13951 gfc_expr *e, *start, *end, *step;
60e19868 13952 bool retval = true;
4ee9c684 13953
13954 mpz_init (frame.value);
8ae2b304 13955 mpz_init (trip);
4ee9c684 13956
1bfea7e8 13957 start = gfc_copy_expr (var->iter.start);
13958 end = gfc_copy_expr (var->iter.end);
13959 step = gfc_copy_expr (var->iter.step);
13960
60e19868 13961 if (!gfc_simplify_expr (start, 1)
1bcc6eb8 13962 || start->expr_type != EXPR_CONSTANT)
1bfea7e8 13963 {
8ae2b304 13964 gfc_error ("start of implied-do loop at %L could not be "
13965 "simplified to a constant value", &start->where);
60e19868 13966 retval = false;
1bfea7e8 13967 goto cleanup;
13968 }
60e19868 13969 if (!gfc_simplify_expr (end, 1)
1bcc6eb8 13970 || end->expr_type != EXPR_CONSTANT)
1bfea7e8 13971 {
8ae2b304 13972 gfc_error ("end of implied-do loop at %L could not be "
13973 "simplified to a constant value", &start->where);
60e19868 13974 retval = false;
1bfea7e8 13975 goto cleanup;
13976 }
60e19868 13977 if (!gfc_simplify_expr (step, 1)
1bcc6eb8 13978 || step->expr_type != EXPR_CONSTANT)
1bfea7e8 13979 {
8ae2b304 13980 gfc_error ("step of implied-do loop at %L could not be "
13981 "simplified to a constant value", &start->where);
60e19868 13982 retval = false;
1bfea7e8 13983 goto cleanup;
13984 }
13985
8ae2b304 13986 mpz_set (trip, end->value.integer);
1bfea7e8 13987 mpz_sub (trip, trip, start->value.integer);
13988 mpz_add (trip, trip, step->value.integer);
4ee9c684 13989
1bfea7e8 13990 mpz_div (trip, trip, step->value.integer);
4ee9c684 13991
1bfea7e8 13992 mpz_set (frame.value, start->value.integer);
4ee9c684 13993
13994 frame.prev = iter_stack;
13995 frame.variable = var->iter.var->symtree;
13996 iter_stack = &frame;
13997
13998 while (mpz_cmp_ui (trip, 0) > 0)
13999 {
60e19868 14000 if (!traverse_data_var (var->list, where))
4ee9c684 14001 {
60e19868 14002 retval = false;
1bfea7e8 14003 goto cleanup;
4ee9c684 14004 }
14005
14006 e = gfc_copy_expr (var->expr);
60e19868 14007 if (!gfc_simplify_expr (e, 1))
1bfea7e8 14008 {
14009 gfc_free_expr (e);
60e19868 14010 retval = false;
1bfea7e8 14011 goto cleanup;
14012 }
4ee9c684 14013
1bfea7e8 14014 mpz_add (frame.value, frame.value, step->value.integer);
4ee9c684 14015
14016 mpz_sub_ui (trip, trip, 1);
14017 }
14018
1bfea7e8 14019cleanup:
4ee9c684 14020 mpz_clear (frame.value);
8ae2b304 14021 mpz_clear (trip);
4ee9c684 14022
1bfea7e8 14023 gfc_free_expr (start);
14024 gfc_free_expr (end);
14025 gfc_free_expr (step);
14026
4ee9c684 14027 iter_stack = frame.prev;
1bfea7e8 14028 return retval;
4ee9c684 14029}
14030
14031
14032/* Type resolve variables in the variable list of a DATA statement. */
14033
60e19868 14034static bool
1bcc6eb8 14035traverse_data_var (gfc_data_variable *var, locus *where)
4ee9c684 14036{
60e19868 14037 bool t;
4ee9c684 14038
14039 for (; var; var = var->next)
14040 {
14041 if (var->expr == NULL)
14042 t = traverse_data_list (var, where);
14043 else
14044 t = check_data_variable (var, where);
14045
60e19868 14046 if (!t)
14047 return false;
4ee9c684 14048 }
14049
60e19868 14050 return true;
4ee9c684 14051}
14052
14053
14054/* Resolve the expressions and iterators associated with a data statement.
14055 This is separate from the assignment checking because data lists should
14056 only be resolved once. */
14057
60e19868 14058static bool
1bcc6eb8 14059resolve_data_variables (gfc_data_variable *d)
4ee9c684 14060{
4ee9c684 14061 for (; d; d = d->next)
14062 {
14063 if (d->list == NULL)
14064 {
60e19868 14065 if (!gfc_resolve_expr (d->expr))
14066 return false;
4ee9c684 14067 }
14068 else
14069 {
60e19868 14070 if (!gfc_resolve_iterator (&d->iter, false, true))
14071 return false;
4ee9c684 14072
60e19868 14073 if (!resolve_data_variables (d->list))
14074 return false;
4ee9c684 14075 }
14076 }
14077
60e19868 14078 return true;
4ee9c684 14079}
14080
14081
14082/* Resolve a single DATA statement. We implement this by storing a pointer to
14083 the value list into static variables, and then recursively traversing the
14084 variables list, expanding iterators and such. */
14085
14086static void
7d74abfd 14087resolve_data (gfc_data *d)
4ee9c684 14088{
7d74abfd 14089
60e19868 14090 if (!resolve_data_variables (d->var))
4ee9c684 14091 return;
14092
14093 values.vnode = d->value;
7d74abfd 14094 if (d->value == NULL)
14095 mpz_set_ui (values.left, 0);
14096 else
14097 mpz_set (values.left, d->value->repeat);
4ee9c684 14098
60e19868 14099 if (!traverse_data_var (d->var, &d->where))
4ee9c684 14100 return;
14101
14102 /* At this point, we better not have any values left. */
14103
60e19868 14104 if (next_data_value ())
4ee9c684 14105 gfc_error ("DATA statement at %L has more values than variables",
14106 &d->where);
14107}
14108
14109
7ee0732d 14110/* 12.6 Constraint: In a pure subprogram any variable which is in common or
14111 accessed by host or use association, is a dummy argument to a pure function,
14112 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14113 is storage associated with any such variable, shall not be used in the
14114 following contexts: (clients of this function). */
14115
69b1505f 14116/* Determines if a variable is not 'pure', i.e., not assignable within a pure
1bcc6eb8 14117 procedure. Returns zero if assignment is OK, nonzero if there is a
14118 problem. */
4ee9c684 14119int
1bcc6eb8 14120gfc_impure_variable (gfc_symbol *sym)
4ee9c684 14121{
7ee0732d 14122 gfc_symbol *proc;
c4cec8b1 14123 gfc_namespace *ns;
7ee0732d 14124
4ee9c684 14125 if (sym->attr.use_assoc || sym->attr.in_common)
14126 return 1;
14127
c4cec8b1 14128 /* Check if the symbol's ns is inside the pure procedure. */
14129 for (ns = gfc_current_ns; ns; ns = ns->parent)
14130 {
14131 if (ns == sym->ns)
14132 break;
14133 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14134 return 1;
14135 }
4ee9c684 14136
7ee0732d 14137 proc = sym->ns->proc_name;
b410f5d1 14138 if (sym->attr.dummy
14139 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14140 || proc->attr.function))
7ee0732d 14141 return 1;
4ee9c684 14142
7ee0732d 14143 /* TODO: Sort out what can be storage associated, if anything, and include
14144 it here. In principle equivalences should be scanned but it does not
14145 seem to be possible to storage associate an impure variable this way. */
4ee9c684 14146 return 0;
14147}
14148
14149
c4cec8b1 14150/* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14151 current namespace is inside a pure procedure. */
4ee9c684 14152
14153int
1bcc6eb8 14154gfc_pure (gfc_symbol *sym)
4ee9c684 14155{
14156 symbol_attribute attr;
c4cec8b1 14157 gfc_namespace *ns;
4ee9c684 14158
14159 if (sym == NULL)
c4cec8b1 14160 {
14161 /* Check if the current namespace or one of its parents
14162 belongs to a pure procedure. */
14163 for (ns = gfc_current_ns; ns; ns = ns->parent)
14164 {
14165 sym = ns->proc_name;
14166 if (sym == NULL)
14167 return 0;
14168 attr = sym->attr;
4e4ea00b 14169 if (attr.flavor == FL_PROCEDURE && attr.pure)
c4cec8b1 14170 return 1;
14171 }
14172 return 0;
14173 }
4ee9c684 14174
14175 attr = sym->attr;
14176
4e4ea00b 14177 return attr.flavor == FL_PROCEDURE && attr.pure;
4ee9c684 14178}
14179
14180
8b0a2e85 14181/* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14182 checks if the current namespace is implicitly pure. Note that this
14183 function returns false for a PURE procedure. */
14184
14185int
14186gfc_implicit_pure (gfc_symbol *sym)
14187{
68218204 14188 gfc_namespace *ns;
8b0a2e85 14189
14190 if (sym == NULL)
14191 {
68218204 14192 /* Check if the current procedure is implicit_pure. Walk up
14193 the procedure list until we find a procedure. */
14194 for (ns = gfc_current_ns; ns; ns = ns->parent)
14195 {
14196 sym = ns->proc_name;
14197 if (sym == NULL)
14198 return 0;
d6463863 14199
68218204 14200 if (sym->attr.flavor == FL_PROCEDURE)
14201 break;
14202 }
8b0a2e85 14203 }
d6463863 14204
68218204 14205 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14206 && !sym->attr.pure;
8b0a2e85 14207}
14208
14209
c77badf3 14210void
14211gfc_unset_implicit_pure (gfc_symbol *sym)
14212{
14213 gfc_namespace *ns;
14214
14215 if (sym == NULL)
14216 {
14217 /* Check if the current procedure is implicit_pure. Walk up
14218 the procedure list until we find a procedure. */
14219 for (ns = gfc_current_ns; ns; ns = ns->parent)
14220 {
14221 sym = ns->proc_name;
14222 if (sym == NULL)
14223 return;
14224
14225 if (sym->attr.flavor == FL_PROCEDURE)
14226 break;
14227 }
14228 }
14229
14230 if (sym->attr.flavor == FL_PROCEDURE)
14231 sym->attr.implicit_pure = 0;
14232 else
14233 sym->attr.pure = 0;
14234}
14235
14236
4ee9c684 14237/* Test whether the current procedure is elemental or not. */
14238
14239int
1bcc6eb8 14240gfc_elemental (gfc_symbol *sym)
4ee9c684 14241{
14242 symbol_attribute attr;
14243
14244 if (sym == NULL)
14245 sym = gfc_current_ns->proc_name;
14246 if (sym == NULL)
14247 return 0;
14248 attr = sym->attr;
14249
14250 return attr.flavor == FL_PROCEDURE && attr.elemental;
14251}
14252
14253
14254/* Warn about unused labels. */
14255
14256static void
1bcc6eb8 14257warn_unused_fortran_label (gfc_st_label *label)
4ee9c684 14258{
3bd3b616 14259 if (label == NULL)
4ee9c684 14260 return;
14261
b6abe79c 14262 warn_unused_fortran_label (label->left);
4ee9c684 14263
3bd3b616 14264 if (label->defined == ST_LABEL_UNKNOWN)
14265 return;
4ee9c684 14266
3bd3b616 14267 switch (label->referenced)
14268 {
14269 case ST_LABEL_UNKNOWN:
14270 gfc_warning ("Label %d at %L defined but not used", label->value,
14271 &label->where);
14272 break;
4ee9c684 14273
3bd3b616 14274 case ST_LABEL_BAD_TARGET:
14275 gfc_warning ("Label %d at %L defined but cannot be used",
14276 label->value, &label->where);
14277 break;
4ee9c684 14278
3bd3b616 14279 default:
14280 break;
4ee9c684 14281 }
3bd3b616 14282
b6abe79c 14283 warn_unused_fortran_label (label->right);
4ee9c684 14284}
14285
14286
9e25b302 14287/* Returns the sequence type of a symbol or sequence. */
14288
14289static seq_type
14290sequence_type (gfc_typespec ts)
14291{
14292 seq_type result;
14293 gfc_component *c;
14294
14295 switch (ts.type)
14296 {
14297 case BT_DERIVED:
14298
eeebe20b 14299 if (ts.u.derived->components == NULL)
9e25b302 14300 return SEQ_NONDEFAULT;
14301
eeebe20b 14302 result = sequence_type (ts.u.derived->components->ts);
14303 for (c = ts.u.derived->components->next; c; c = c->next)
9e25b302 14304 if (sequence_type (c->ts) != result)
14305 return SEQ_MIXED;
14306
14307 return result;
14308
14309 case BT_CHARACTER:
14310 if (ts.kind != gfc_default_character_kind)
14311 return SEQ_NONDEFAULT;
14312
14313 return SEQ_CHARACTER;
14314
14315 case BT_INTEGER:
14316 if (ts.kind != gfc_default_integer_kind)
14317 return SEQ_NONDEFAULT;
14318
14319 return SEQ_NUMERIC;
14320
14321 case BT_REAL:
14322 if (!(ts.kind == gfc_default_real_kind
1bcc6eb8 14323 || ts.kind == gfc_default_double_kind))
9e25b302 14324 return SEQ_NONDEFAULT;
14325
14326 return SEQ_NUMERIC;
14327
14328 case BT_COMPLEX:
14329 if (ts.kind != gfc_default_complex_kind)
14330 return SEQ_NONDEFAULT;
14331
14332 return SEQ_NUMERIC;
14333
14334 case BT_LOGICAL:
14335 if (ts.kind != gfc_default_logical_kind)
14336 return SEQ_NONDEFAULT;
14337
14338 return SEQ_NUMERIC;
14339
14340 default:
14341 return SEQ_NONDEFAULT;
14342 }
14343}
14344
14345
4ee9c684 14346/* Resolve derived type EQUIVALENCE object. */
14347
60e19868 14348static bool
4ee9c684 14349resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14350{
4ee9c684 14351 gfc_component *c = derived->components;
14352
14353 if (!derived)
60e19868 14354 return true;
4ee9c684 14355
14356 /* Shall not be an object of nonsequence derived type. */
14357 if (!derived->attr.sequence)
14358 {
0d2b3c9c 14359 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
1bcc6eb8 14360 "attribute to be an EQUIVALENCE object", sym->name,
14361 &e->where);
60e19868 14362 return false;
4ee9c684 14363 }
14364
f6d0e37a 14365 /* Shall not have allocatable components. */
2294b616 14366 if (derived->attr.alloc_comp)
14367 {
0d2b3c9c 14368 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
1bcc6eb8 14369 "components to be an EQUIVALENCE object",sym->name,
14370 &e->where);
60e19868 14371 return false;
2294b616 14372 }
14373
08262510 14374 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
b4bcbcd7 14375 {
0d2b3c9c 14376 gfc_error ("Derived type variable %qs at %L with default "
b4bcbcd7 14377 "initialization cannot be in EQUIVALENCE with a variable "
14378 "in COMMON", sym->name, &e->where);
60e19868 14379 return false;
b4bcbcd7 14380 }
14381
4ee9c684 14382 for (; c ; c = c->next)
14383 {
eeebe20b 14384 if (c->ts.type == BT_DERIVED
60e19868 14385 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14386 return false;
fc243266 14387
4ee9c684 14388 /* Shall not be an object of sequence derived type containing a pointer
1bcc6eb8 14389 in the structure. */
3be2b8d5 14390 if (c->attr.pointer)
1bcc6eb8 14391 {
0d2b3c9c 14392 gfc_error ("Derived type variable %qs at %L with pointer "
1bcc6eb8 14393 "component(s) cannot be an EQUIVALENCE object",
14394 sym->name, &e->where);
60e19868 14395 return false;
1bcc6eb8 14396 }
4ee9c684 14397 }
60e19868 14398 return true;
4ee9c684 14399}
14400
14401
d6463863 14402/* Resolve equivalence object.
9e25b302 14403 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14404 an allocatable array, an object of nonsequence derived type, an object of
4ee9c684 14405 sequence derived type containing a pointer at any level of component
14406 selection, an automatic object, a function name, an entry name, a result
14407 name, a named constant, a structure component, or a subobject of any of
9e25b302 14408 the preceding objects. A substring shall not have length zero. A
14409 derived type shall not have components with default initialization nor
14410 shall two objects of an equivalence group be initialized.
3ea52af3 14411 Either all or none of the objects shall have an protected attribute.
9e25b302 14412 The simple constraints are done in symbol.c(check_conflict) and the rest
14413 are implemented here. */
4ee9c684 14414
14415static void
14416resolve_equivalence (gfc_equiv *eq)
14417{
14418 gfc_symbol *sym;
9e25b302 14419 gfc_symbol *first_sym;
4ee9c684 14420 gfc_expr *e;
14421 gfc_ref *r;
9e25b302 14422 locus *last_where = NULL;
14423 seq_type eq_type, last_eq_type;
14424 gfc_typespec *last_ts;
3ea52af3 14425 int object, cnt_protected;
9e25b302 14426 const char *msg;
14427
9e25b302 14428 last_ts = &eq->expr->symtree->n.sym->ts;
4ee9c684 14429
9e25b302 14430 first_sym = eq->expr->symtree->n.sym;
14431
3ea52af3 14432 cnt_protected = 0;
14433
9e25b302 14434 for (object = 1; eq; eq = eq->eq, object++)
4ee9c684 14435 {
14436 e = eq->expr;
a3c2bc5e 14437
14438 e->ts = e->symtree->n.sym->ts;
14439 /* match_varspec might not know yet if it is seeing
14440 array reference or substring reference, as it doesn't
14441 know the types. */
14442 if (e->ref && e->ref->type == REF_ARRAY)
14443 {
14444 gfc_ref *ref = e->ref;
14445 sym = e->symtree->n.sym;
14446
14447 if (sym->attr.dimension)
14448 {
14449 ref->u.ar.as = sym->as;
14450 ref = ref->next;
14451 }
14452
14453 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14454 if (e->ts.type == BT_CHARACTER
14455 && ref
14456 && ref->type == REF_ARRAY
14457 && ref->u.ar.dimen == 1
14458 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14459 && ref->u.ar.stride[0] == NULL)
14460 {
14461 gfc_expr *start = ref->u.ar.start[0];
14462 gfc_expr *end = ref->u.ar.end[0];
14463 void *mem = NULL;
14464
14465 /* Optimize away the (:) reference. */
14466 if (start == NULL && end == NULL)
14467 {
14468 if (e->ref == ref)
14469 e->ref = ref->next;
14470 else
14471 e->ref->next = ref->next;
14472 mem = ref;
14473 }
14474 else
14475 {
14476 ref->type = REF_SUBSTRING;
14477 if (start == NULL)
126387b5 14478 start = gfc_get_int_expr (gfc_default_integer_kind,
14479 NULL, 1);
a3c2bc5e 14480 ref->u.ss.start = start;
eeebe20b 14481 if (end == NULL && e->ts.u.cl)
14482 end = gfc_copy_expr (e->ts.u.cl->length);
a3c2bc5e 14483 ref->u.ss.end = end;
eeebe20b 14484 ref->u.ss.length = e->ts.u.cl;
14485 e->ts.u.cl = NULL;
a3c2bc5e 14486 }
14487 ref = ref->next;
434f0922 14488 free (mem);
a3c2bc5e 14489 }
14490
14491 /* Any further ref is an error. */
14492 if (ref)
14493 {
14494 gcc_assert (ref->type == REF_ARRAY);
14495 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14496 &ref->u.ar.where);
14497 continue;
14498 }
14499 }
14500
60e19868 14501 if (!gfc_resolve_expr (e))
1bcc6eb8 14502 continue;
4ee9c684 14503
14504 sym = e->symtree->n.sym;
4ee9c684 14505
41694d7c 14506 if (sym->attr.is_protected)
3ea52af3 14507 cnt_protected++;
14508 if (cnt_protected > 0 && cnt_protected != object)
14509 {
14510 gfc_error ("Either all or none of the objects in the "
14511 "EQUIVALENCE set at %L shall have the "
14512 "PROTECTED attribute",
14513 &e->where);
14514 break;
1bcc6eb8 14515 }
3ea52af3 14516
9e25b302 14517 /* Shall not equivalence common block variables in a PURE procedure. */
fc243266 14518 if (sym->ns->proc_name
1bcc6eb8 14519 && sym->ns->proc_name->attr.pure
14520 && sym->attr.in_common)
14521 {
0d2b3c9c 14522 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
14523 "object in the pure procedure %qs",
9e25b302 14524 sym->name, &e->where, sym->ns->proc_name->name);
1bcc6eb8 14525 break;
14526 }
fc243266 14527
14528 /* Shall not be a named constant. */
4ee9c684 14529 if (e->expr_type == EXPR_CONSTANT)
1bcc6eb8 14530 {
0d2b3c9c 14531 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
1bcc6eb8 14532 "object", sym->name, &e->where);
14533 continue;
14534 }
4ee9c684 14535
eeebe20b 14536 if (e->ts.type == BT_DERIVED
60e19868 14537 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
1bcc6eb8 14538 continue;
4ee9c684 14539
9e25b302 14540 /* Check that the types correspond correctly:
14541 Note 5.28:
14542 A numeric sequence structure may be equivalenced to another sequence
14543 structure, an object of default integer type, default real type, double
14544 precision real type, default logical type such that components of the
14545 structure ultimately only become associated to objects of the same
14546 kind. A character sequence structure may be equivalenced to an object
14547 of default character kind or another character sequence structure.
14548 Other objects may be equivalenced only to objects of the same type and
14549 kind parameters. */
14550
14551 /* Identical types are unconditionally OK. */
14552 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14553 goto identical_types;
14554
14555 last_eq_type = sequence_type (*last_ts);
14556 eq_type = sequence_type (sym->ts);
14557
14558 /* Since the pair of objects is not of the same type, mixed or
14559 non-default sequences can be rejected. */
14560
14561 msg = "Sequence %s with mixed components in EQUIVALENCE "
14562 "statement at %L with different type objects";
14563 if ((object ==2
1bcc6eb8 14564 && last_eq_type == SEQ_MIXED
60e19868 14565 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
1bcc6eb8 14566 || (eq_type == SEQ_MIXED
60e19868 14567 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
9e25b302 14568 continue;
14569
14570 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14571 "statement at %L with objects of different type";
14572 if ((object ==2
1bcc6eb8 14573 && last_eq_type == SEQ_NONDEFAULT
60e19868 14574 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
1bcc6eb8 14575 || (eq_type == SEQ_NONDEFAULT
60e19868 14576 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
9e25b302 14577 continue;
14578
0d2b3c9c 14579 msg ="Non-CHARACTER object %qs in default CHARACTER "
9e25b302 14580 "EQUIVALENCE statement at %L";
14581 if (last_eq_type == SEQ_CHARACTER
1bcc6eb8 14582 && eq_type != SEQ_CHARACTER
60e19868 14583 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
9e25b302 14584 continue;
14585
0d2b3c9c 14586 msg ="Non-NUMERIC object %qs in default NUMERIC "
9e25b302 14587 "EQUIVALENCE statement at %L";
14588 if (last_eq_type == SEQ_NUMERIC
1bcc6eb8 14589 && eq_type != SEQ_NUMERIC
60e19868 14590 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
9e25b302 14591 continue;
14592
14593 identical_types:
14594 last_ts =&sym->ts;
14595 last_where = &e->where;
14596
4ee9c684 14597 if (!e->ref)
1bcc6eb8 14598 continue;
4ee9c684 14599
14600 /* Shall not be an automatic array. */
14601 if (e->ref->type == REF_ARRAY
60e19868 14602 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
1bcc6eb8 14603 {
0d2b3c9c 14604 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
1bcc6eb8 14605 "an EQUIVALENCE object", sym->name, &e->where);
14606 continue;
14607 }
4ee9c684 14608
4ee9c684 14609 r = e->ref;
14610 while (r)
1bcc6eb8 14611 {
a3c2bc5e 14612 /* Shall not be a structure component. */
14613 if (r->type == REF_COMPONENT)
14614 {
0d2b3c9c 14615 gfc_error ("Structure component %qs at %L cannot be an "
a3c2bc5e 14616 "EQUIVALENCE object",
14617 r->u.c.component->name, &e->where);
14618 break;
14619 }
14620
14621 /* A substring shall not have length zero. */
14622 if (r->type == REF_SUBSTRING)
14623 {
14624 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14625 {
14626 gfc_error ("Substring at %L has length zero",
14627 &r->u.ss.start->where);
14628 break;
14629 }
14630 }
14631 r = r->next;
14632 }
fc243266 14633 }
14634}
0e633d82 14635
14636
f6d0e37a 14637/* Resolve function and ENTRY types, issue diagnostics if needed. */
0e633d82 14638
14639static void
1bcc6eb8 14640resolve_fntype (gfc_namespace *ns)
0e633d82 14641{
14642 gfc_entry_list *el;
14643 gfc_symbol *sym;
14644
14645 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14646 return;
14647
14648 /* If there are any entries, ns->proc_name is the entry master
14649 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14650 if (ns->entries)
14651 sym = ns->entries->sym;
14652 else
14653 sym = ns->proc_name;
14654 if (sym->result == sym
14655 && sym->ts.type == BT_UNKNOWN
60e19868 14656 && !gfc_set_default_type (sym, 0, NULL)
0e633d82 14657 && !sym->attr.untyped)
14658 {
0d2b3c9c 14659 gfc_error ("Function %qs at %L has no IMPLICIT type",
0e633d82 14660 sym->name, &sym->declared_at);
14661 sym->attr.untyped = 1;
14662 }
14663
eeebe20b 14664 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
9b3a552a 14665 && !sym->attr.contained
924d51fd 14666 && !gfc_check_symbol_access (sym->ts.u.derived)
14667 && gfc_check_symbol_access (sym))
669164e6 14668 {
0d2b3c9c 14669 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
14670 "%L of PRIVATE type %qs", sym->name,
eeebe20b 14671 &sym->declared_at, sym->ts.u.derived->name);
669164e6 14672 }
14673
cf4d6ace 14674 if (ns->entries)
0e633d82 14675 for (el = ns->entries->next; el; el = el->next)
14676 {
14677 if (el->sym->result == el->sym
14678 && el->sym->ts.type == BT_UNKNOWN
60e19868 14679 && !gfc_set_default_type (el->sym, 0, NULL)
0e633d82 14680 && !el->sym->attr.untyped)
14681 {
0d2b3c9c 14682 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
0e633d82 14683 el->sym->name, &el->sym->declared_at);
14684 el->sym->attr.untyped = 1;
14685 }
14686 }
14687}
14688
a36eb9ee 14689
e4981f6e 14690/* 12.3.2.1.1 Defined operators. */
14691
60e19868 14692static bool
a36eb9ee 14693check_uop_procedure (gfc_symbol *sym, locus where)
e4981f6e 14694{
e4981f6e 14695 gfc_formal_arglist *formal;
14696
a36eb9ee 14697 if (!sym->attr.function)
14698 {
0d2b3c9c 14699 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
a36eb9ee 14700 sym->name, &where);
60e19868 14701 return false;
a36eb9ee 14702 }
fc243266 14703
a36eb9ee 14704 if (sym->ts.type == BT_CHARACTER
eeebe20b 14705 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14706 && !(sym->result && sym->result->ts.u.cl
14707 && sym->result->ts.u.cl->length))
a36eb9ee 14708 {
0d2b3c9c 14709 gfc_error ("User operator procedure %qs at %L cannot be assumed "
a36eb9ee 14710 "character length", sym->name, &where);
60e19868 14711 return false;
a36eb9ee 14712 }
e4981f6e 14713
6777213b 14714 formal = gfc_sym_get_dummy_args (sym);
a36eb9ee 14715 if (!formal || !formal->sym)
e4981f6e 14716 {
0d2b3c9c 14717 gfc_error ("User operator procedure %qs at %L must have at least "
a36eb9ee 14718 "one argument", sym->name, &where);
60e19868 14719 return false;
a36eb9ee 14720 }
e4981f6e 14721
a36eb9ee 14722 if (formal->sym->attr.intent != INTENT_IN)
14723 {
14724 gfc_error ("First argument of operator interface at %L must be "
14725 "INTENT(IN)", &where);
60e19868 14726 return false;
a36eb9ee 14727 }
e4981f6e 14728
a36eb9ee 14729 if (formal->sym->attr.optional)
14730 {
14731 gfc_error ("First argument of operator interface at %L cannot be "
14732 "optional", &where);
60e19868 14733 return false;
a36eb9ee 14734 }
e4981f6e 14735
a36eb9ee 14736 formal = formal->next;
14737 if (!formal || !formal->sym)
60e19868 14738 return true;
e4981f6e 14739
a36eb9ee 14740 if (formal->sym->attr.intent != INTENT_IN)
14741 {
14742 gfc_error ("Second argument of operator interface at %L must be "
14743 "INTENT(IN)", &where);
60e19868 14744 return false;
a36eb9ee 14745 }
e4981f6e 14746
a36eb9ee 14747 if (formal->sym->attr.optional)
14748 {
14749 gfc_error ("Second argument of operator interface at %L cannot be "
14750 "optional", &where);
60e19868 14751 return false;
a36eb9ee 14752 }
e4981f6e 14753
a36eb9ee 14754 if (formal->next)
14755 {
14756 gfc_error ("Operator interface at %L must have, at most, two "
14757 "arguments", &where);
60e19868 14758 return false;
a36eb9ee 14759 }
e4981f6e 14760
60e19868 14761 return true;
a36eb9ee 14762}
e4981f6e 14763
a36eb9ee 14764static void
14765gfc_resolve_uops (gfc_symtree *symtree)
14766{
14767 gfc_interface *itr;
14768
14769 if (symtree == NULL)
14770 return;
14771
14772 gfc_resolve_uops (symtree->left);
14773 gfc_resolve_uops (symtree->right);
14774
14775 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14776 check_uop_procedure (itr->sym, itr->sym->declared_at);
e4981f6e 14777}
14778
0e633d82 14779
dd7622e6 14780/* Examine all of the expressions associated with a program unit,
14781 assign types to all intermediate expressions, make sure that all
14782 assignments are to compatible types and figure out which names
14783 refer to which functions or subroutines. It doesn't check code
c3f3b68d 14784 block, which is handled by gfc_resolve_code. */
4ee9c684 14785
dd7622e6 14786static void
1bcc6eb8 14787resolve_types (gfc_namespace *ns)
4ee9c684 14788{
dd7622e6 14789 gfc_namespace *n;
4ee9c684 14790 gfc_charlen *cl;
14791 gfc_data *d;
14792 gfc_equiv *eq;
5422d457 14793 gfc_namespace* old_ns = gfc_current_ns;
4ee9c684 14794
ac5f2650 14795 /* Check that all IMPLICIT types are ok. */
14796 if (!ns->seen_implicit_none)
14797 {
14798 unsigned letter;
14799 for (letter = 0; letter != GFC_LETTERS; ++letter)
14800 if (ns->set_flag[letter]
080819af 14801 && !resolve_typespec_used (&ns->default_type[letter],
60e19868 14802 &ns->implicit_loc[letter], NULL))
ac5f2650 14803 return;
14804 }
14805
5422d457 14806 gfc_current_ns = ns;
14807
da8ac1db 14808 resolve_entries (ns);
14809
1f2d591b 14810 resolve_common_vars (ns->blank_common.head, false);
4750071e 14811 resolve_common_blocks (ns->common_root);
14812
da8ac1db 14813 resolve_contained_functions (ns);
14814
d6a853a7 14815 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14816 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14817 resolve_formal_arglist (ns->proc_name);
14818
c5d33754 14819 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14820
2fe2caa6 14821 for (cl = ns->cl_list; cl; cl = cl->next)
14822 resolve_charlen (cl);
14823
4ee9c684 14824 gfc_traverse_ns (ns, resolve_symbol);
14825
0e633d82 14826 resolve_fntype (ns);
14827
4ee9c684 14828 for (n = ns->contained; n; n = n->sibling)
14829 {
14830 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
0d2b3c9c 14831 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
4ee9c684 14832 "also be PURE", n->proc_name->name,
14833 &n->proc_name->declared_at);
14834
dd7622e6 14835 resolve_types (n);
4ee9c684 14836 }
14837
14838 forall_flag = 0;
8b8cc022 14839 gfc_do_concurrent_flag = 0;
4ee9c684 14840 gfc_check_interfaces (ns);
14841
4ee9c684 14842 gfc_traverse_ns (ns, resolve_values);
14843
7f2e183b 14844 if (ns->save_all)
4ee9c684 14845 gfc_save_all (ns);
14846
14847 iter_stack = NULL;
14848 for (d = ns->data; d; d = d->next)
14849 resolve_data (d);
14850
14851 iter_stack = NULL;
14852 gfc_traverse_ns (ns, gfc_formalize_init_value);
14853
c5d33754 14854 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14855
4ee9c684 14856 for (eq = ns->equiv; eq; eq = eq->next)
14857 resolve_equivalence (eq);
14858
4ee9c684 14859 /* Warn about unused labels. */
fade3213 14860 if (warn_unused_label)
b6abe79c 14861 warn_unused_fortran_label (ns->st_labels);
e4981f6e 14862
14863 gfc_resolve_uops (ns->uop_root);
5422d457 14864
15b28553 14865 gfc_resolve_omp_declare_simd (ns);
14866
b14b82d9 14867 gfc_resolve_omp_udrs (ns->omp_udr_root);
14868
5422d457 14869 gfc_current_ns = old_ns;
dd7622e6 14870}
14871
14872
c3f3b68d 14873/* Call gfc_resolve_code recursively. */
dd7622e6 14874
14875static void
1bcc6eb8 14876resolve_codes (gfc_namespace *ns)
dd7622e6 14877{
14878 gfc_namespace *n;
83aeedb9 14879 bitmap_obstack old_obstack;
dd7622e6 14880
a97292cb 14881 if (ns->resolved == 1)
14882 return;
14883
dd7622e6 14884 for (n = ns->contained; n; n = n->sibling)
14885 resolve_codes (n);
14886
14887 gfc_current_ns = ns;
f30e488d 14888
14889 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14890 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14891 cs_base = NULL;
14892
c6b395dd 14893 /* Set to an out of range value. */
14894 current_entry_id = -1;
82efdb2e 14895
83aeedb9 14896 old_obstack = labels_obstack;
82efdb2e 14897 bitmap_obstack_initialize (&labels_obstack);
83aeedb9 14898
c3f3b68d 14899 gfc_resolve_code (ns->code, ns);
83aeedb9 14900
82efdb2e 14901 bitmap_obstack_release (&labels_obstack);
83aeedb9 14902 labels_obstack = old_obstack;
dd7622e6 14903}
14904
14905
14906/* This function is called after a complete program unit has been compiled.
14907 Its purpose is to examine all of the expressions associated with a program
14908 unit, assign types to all intermediate expressions, make sure that all
14909 assignments are to compatible types and figure out which names refer to
14910 which functions or subroutines. */
14911
14912void
1bcc6eb8 14913gfc_resolve (gfc_namespace *ns)
dd7622e6 14914{
14915 gfc_namespace *old_ns;
7ea64434 14916 code_stack *old_cs_base;
dd7622e6 14917
83aeedb9 14918 if (ns->resolved)
14919 return;
14920
7ea64434 14921 ns->resolved = -1;
dd7622e6 14922 old_ns = gfc_current_ns;
7ea64434 14923 old_cs_base = cs_base;
dd7622e6 14924
14925 resolve_types (ns);
d6463863 14926 component_assignment_level = 0;
dd7622e6 14927 resolve_codes (ns);
4ee9c684 14928
14929 gfc_current_ns = old_ns;
7ea64434 14930 cs_base = old_cs_base;
83aeedb9 14931 ns->resolved = 1;
5532a4d1 14932
14933 gfc_run_passes (ns);
4ee9c684 14934}