]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/resolve.c
Update copyright years in gcc/
[thirdparty/gcc.git] / gcc / fortran / resolve.c
CommitLineData
df2fba9e 1/* Perform type resolution on the various structures.
23a5b65a 2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Andy Vaught
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
9fc4d79b 10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
20
21#include "config.h"
d22e4895 22#include "system.h"
953bee7c 23#include "coretypes.h"
994c1cc0 24#include "flags.h"
6de9cd9a 25#include "gfortran.h"
0615f923
TS
26#include "obstack.h"
27#include "bitmap.h"
6de9cd9a 28#include "arith.h" /* For gfc_compare_expr(). */
1524f80b 29#include "dependency.h"
ca39e6f2 30#include "data.h"
00a4618b 31#include "target-memory.h" /* for gfc_simplify_transfer */
b7e75771 32#include "constructor.h"
d22e4895 33
e8ec07e1
PT
34/* Types used in equivalence statements. */
35
36typedef enum seq_type
37{
38 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
39}
40seq_type;
6de9cd9a 41
0615f923
TS
42/* Stack to keep track of the nesting of blocks as we move through the
43 code. See resolve_branch() and resolve_code(). */
6de9cd9a
DN
44
45typedef struct code_stack
46{
d80c695f 47 struct gfc_code *head, *current;
6de9cd9a 48 struct code_stack *prev;
0615f923
TS
49
50 /* This bitmap keeps track of the targets valid for a branch from
d80c695f
TS
51 inside this block except for END {IF|SELECT}s of enclosing
52 blocks. */
0615f923 53 bitmap reachable_labels;
6de9cd9a
DN
54}
55code_stack;
56
57static code_stack *cs_base = NULL;
58
59
8c6a85e3 60/* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
6de9cd9a
DN
61
62static int forall_flag;
ce96d372 63int gfc_do_concurrent_flag;
6de9cd9a 64
c62c6622
TB
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
45a69325 72
6c7a4dfd
JJ
73/* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
74
75static int omp_workshare_flag;
76
4213f93b
PT
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
0e9a445b 81/* True if we are resolving a specification expression. */
fd061185 82static bool specification_expr = false;
0e9a445b
PT
83
84/* The id of the last entry seen. */
85static int current_entry_id;
86
0615f923
TS
87/* We use bitmaps to determine if a branch target is valid. */
88static bitmap_obstack labels_obstack;
89
d3a9eea2
TB
90/* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
91static bool inquiry_argument = false;
92
c62c6622 93
4213f93b
PT
94int
95gfc_is_formal_arg (void)
96{
97 return formal_arg_flag;
98}
99
c867b7b6
PT
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)
4d382327 105 {
c867b7b6
PT
106 if (sym->ns == ns)
107 return true;
108 }
109
110 return false;
111}
52f49934
DK
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
524af0d6 117static bool
52f49934
DK
118resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
119{
bc21d315 120 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
52f49934
DK
121 {
122 if (where)
123 {
124 if (name)
125 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
bc21d315 126 name, where, ts->u.derived->name);
52f49934
DK
127 else
128 gfc_error ("ABSTRACT type '%s' used at %L",
bc21d315 129 ts->u.derived->name, where);
52f49934
DK
130 }
131
524af0d6 132 return false;
52f49934
DK
133 }
134
524af0d6 135 return true;
52f49934
DK
136}
137
138
524af0d6 139static bool
b6a45605 140check_proc_interface (gfc_symbol *ifc, locus *where)
2fcac97d 141{
0e8d854e 142 /* Several checks for F08:C1216. */
0e8d854e 143 if (ifc->attr.procedure)
2fcac97d 144 {
b6a45605
JW
145 gfc_error ("Interface '%s' at %L is declared "
146 "in a later PROCEDURE statement", ifc->name, where);
524af0d6 147 return false;
2fcac97d 148 }
0e8d854e
JW
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 {
158 gfc_error ("Interface '%s' at %L may not be generic",
b6a45605 159 ifc->name, where);
524af0d6 160 return false;
0e8d854e
JW
161 }
162 }
163 if (ifc->attr.proc == PROC_ST_FUNCTION)
164 {
165 gfc_error ("Interface '%s' at %L may not be a statement function",
b6a45605 166 ifc->name, where);
524af0d6 167 return false;
0e8d854e
JW
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 {
174 gfc_error ("Intrinsic procedure '%s' not allowed in "
b6a45605 175 "PROCEDURE statement at %L", ifc->name, where);
524af0d6 176 return false;
b6a45605
JW
177 }
178 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
179 {
180 gfc_error ("Interface '%s' at %L must be explicit", ifc->name, where);
524af0d6 181 return false;
0e8d854e 182 }
524af0d6 183 return true;
b6a45605
JW
184}
185
186
187static void resolve_symbol (gfc_symbol *sym);
188
189
190/* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
191
524af0d6 192static bool
b6a45605
JW
193resolve_procedure_interface (gfc_symbol *sym)
194{
195 gfc_symbol *ifc = sym->ts.interface;
196
197 if (!ifc)
524af0d6 198 return true;
b6a45605
JW
199
200 if (ifc == sym)
201 {
202 gfc_error ("PROCEDURE '%s' at %L may not be used as its own interface",
203 sym->name, &sym->declared_at);
524af0d6 204 return false;
b6a45605 205 }
524af0d6
JB
206 if (!check_proc_interface (ifc, &sym->declared_at))
207 return false;
2fcac97d 208
0e8d854e 209 if (ifc->attr.if_source || ifc->attr.intrinsic)
2fcac97d 210 {
b6a45605 211 /* Resolve interface and copy attributes. */
2fcac97d 212 resolve_symbol (ifc);
2fcac97d 213 if (ifc->attr.intrinsic)
2dda89a8 214 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
2fcac97d
JW
215
216 if (ifc->result)
c79bb355
JW
217 {
218 sym->ts = ifc->result->ts;
219 sym->result = sym;
220 }
4d382327 221 else
2fcac97d
JW
222 sym->ts = ifc->ts;
223 sym->ts.interface = ifc;
224 sym->attr.function = ifc->attr.function;
225 sym->attr.subroutine = ifc->attr.subroutine;
2fcac97d
JW
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;
8be3d7da 236 sym->attr.is_bind_c = ifc->attr.is_bind_c;
0b2d443b 237 sym->attr.class_ok = ifc->attr.class_ok;
2fcac97d
JW
238 /* Copy array spec. */
239 sym->as = gfc_copy_array_spec (ifc->as);
2fcac97d
JW
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);
2fcac97d 244 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
524af0d6
JB
245 && !gfc_resolve_expr (sym->ts.u.cl->length))
246 return false;
2fcac97d
JW
247 }
248 }
2fcac97d 249
524af0d6 250 return true;
2fcac97d
JW
251}
252
253
6de9cd9a
DN
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
edf1eac2 264resolve_formal_arglist (gfc_symbol *proc)
6de9cd9a
DN
265{
266 gfc_formal_arglist *f;
267 gfc_symbol *sym;
fd061185 268 bool saved_specification_expr;
6de9cd9a
DN
269 int i;
270
6de9cd9a
DN
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
c62c6622 278 || (sym->as && sym->as->rank != 0))
43e7fd21
FXC
279 {
280 proc->attr.always_explicit = 1;
281 sym->attr.always_explicit = 1;
282 }
6de9cd9a 283
4213f93b
PT
284 formal_arg_flag = 1;
285
6de9cd9a
DN
286 for (f = proc->formal; f; f = f->next)
287 {
3d333a28 288 gfc_array_spec *as;
6de9cd9a 289
6220bf43
TB
290 sym = f->sym;
291
6de9cd9a
DN
292 if (sym == NULL)
293 {
edf1eac2 294 /* Alternate return placeholder. */
6de9cd9a
DN
295 if (gfc_elemental (proc))
296 gfc_error ("Alternate return specifier in elemental subroutine "
297 "'%s' at %L is not allowed", proc->name,
298 &proc->declared_at);
edf1eac2
SK
299 if (proc->attr.function)
300 gfc_error ("Alternate return specifier in function "
301 "'%s' at %L is not allowed", proc->name,
302 &proc->declared_at);
6de9cd9a
DN
303 continue;
304 }
0e8d854e 305 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
524af0d6 306 && !resolve_procedure_interface (sym))
0e8d854e 307 return;
6de9cd9a 308
9281625b
BD
309 if (strcmp (proc->name, sym->name) == 0)
310 {
311 gfc_error ("Self-referential argument "
312 "'%s' at %L is not allowed", sym->name,
313 &proc->declared_at);
314 return;
315 }
316
6de9cd9a
DN
317 if (sym->attr.if_source != IFSRC_UNKNOWN)
318 resolve_formal_arglist (sym);
319
fe445bf7 320 if (sym->attr.subroutine || sym->attr.external)
4056cc1b 321 {
fe445bf7
JW
322 if (sym->attr.flavor == FL_UNKNOWN)
323 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
4056cc1b 324 }
fe445bf7 325 else
6de9cd9a 326 {
fe445bf7
JW
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);
6de9cd9a
DN
330 }
331
3d333a28
TB
332 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
333 ? CLASS_DATA (sym)->as : sym->as;
334
fd061185
TB
335 saved_specification_expr = specification_expr;
336 specification_expr = true;
3d333a28 337 gfc_resolve_array_spec (as, 0);
fd061185 338 specification_expr = saved_specification_expr;
6de9cd9a
DN
339
340 /* We can't tell if an array with dimension (:) is assumed or deferred
edf1eac2 341 shape until we know if it has the pointer or allocatable attributes.
6de9cd9a 342 */
3d333a28
TB
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)))
12578be7 349 && sym->attr.flavor != FL_PROCEDURE)
edf1eac2 350 {
3d333a28
TB
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);
edf1eac2 354 }
6de9cd9a 355
3d333a28 356 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
c62c6622 357 || (as && as->type == AS_ASSUMED_RANK)
edf1eac2 358 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
3d333a28
TB
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))
edf1eac2 363 || sym->attr.optional)
43e7fd21
FXC
364 {
365 proc->attr.always_explicit = 1;
366 if (proc->result)
367 proc->result->attr.always_explicit = 1;
368 }
6de9cd9a
DN
369
370 /* If the flavor is unknown at this point, it has to be a variable.
edf1eac2 371 A procedure specification would have already set the type. */
6de9cd9a
DN
372
373 if (sym->attr.flavor == FL_UNKNOWN)
231b2fcc 374 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
6de9cd9a 375
fe445bf7 376 if (gfc_pure (proc))
6de9cd9a 377 {
fe445bf7 378 if (sym->attr.flavor == FL_PROCEDURE)
a26e8df4 379 {
fe445bf7
JW
380 /* F08:C1279. */
381 if (!gfc_pure (sym))
382 {
383 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
384 "also be PURE", sym->name, &sym->declared_at);
385 continue;
386 }
a26e8df4 387 }
fe445bf7 388 else if (!sym->attr.pointer)
a26e8df4 389 {
fe445bf7
JW
390 if (proc->attr.function && sym->attr.intent != INTENT_IN)
391 {
392 if (sym->attr.value)
9717f7a1 393 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
fe445bf7
JW
394 " of pure function '%s' at %L with VALUE "
395 "attribute but without INTENT(IN)",
396 sym->name, proc->name, &sym->declared_at);
397 else
398 gfc_error ("Argument '%s' of pure function '%s' at %L must "
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)
9717f7a1 406 gfc_notify_std (GFC_STD_F2008, "Argument '%s'"
fe445bf7
JW
407 " of pure subroutine '%s' at %L with VALUE "
408 "attribute but without INTENT", sym->name,
409 proc->name, &sym->declared_at);
410 else
411 gfc_error ("Argument '%s' of pure subroutine '%s' at %L "
412 "must have its INTENT specified or have the "
413 "VALUE attribute", sym->name, proc->name,
414 &sym->declared_at);
415 }
a26e8df4 416 }
6de9cd9a
DN
417 }
418
fe445bf7 419 if (proc->attr.implicit_pure)
f1f39033 420 {
fe445bf7
JW
421 if (sym->attr.flavor == FL_PROCEDURE)
422 {
524af0d6 423 if (!gfc_pure (sym))
fe445bf7
JW
424 proc->attr.implicit_pure = 0;
425 }
426 else if (!sym->attr.pointer)
427 {
c915f8bc
TB
428 if (proc->attr.function && sym->attr.intent != INTENT_IN
429 && !sym->value)
fe445bf7 430 proc->attr.implicit_pure = 0;
f1f39033 431
c915f8bc
TB
432 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
433 && !sym->value)
fe445bf7
JW
434 proc->attr.implicit_pure = 0;
435 }
f1f39033
PT
436 }
437
6de9cd9a
DN
438 if (gfc_elemental (proc))
439 {
4056cc1b 440 /* F08:C1289. */
9775a921
TB
441 if (sym->attr.codimension
442 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
443 && CLASS_DATA (sym)->attr.codimension))
be59db2d
TB
444 {
445 gfc_error ("Coarray dummy argument '%s' at %L to elemental "
446 "procedure", sym->name, &sym->declared_at);
447 continue;
448 }
449
9775a921
TB
450 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
451 && CLASS_DATA (sym)->as))
6de9cd9a 452 {
edf1eac2
SK
453 gfc_error ("Argument '%s' of elemental procedure at %L must "
454 "be scalar", sym->name, &sym->declared_at);
6de9cd9a
DN
455 continue;
456 }
457
9775a921
TB
458 if (sym->attr.allocatable
459 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
460 && CLASS_DATA (sym)->attr.allocatable))
e6c14898
DK
461 {
462 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
463 "have the ALLOCATABLE attribute", sym->name,
464 &sym->declared_at);
465 continue;
466 }
467
c696c6f3
TB
468 if (sym->attr.pointer
469 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
470 && CLASS_DATA (sym)->attr.class_pointer))
6de9cd9a 471 {
edf1eac2
SK
472 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
473 "have the POINTER attribute", sym->name,
474 &sym->declared_at);
6de9cd9a
DN
475 continue;
476 }
242633d6
TB
477
478 if (sym->attr.flavor == FL_PROCEDURE)
479 {
480 gfc_error ("Dummy procedure '%s' not allowed in elemental "
481 "procedure '%s' at %L", sym->name, proc->name,
482 &sym->declared_at);
483 continue;
484 }
e6c14898 485
25ffd46f
TB
486 /* Fortran 2008 Corrigendum 1, C1290a. */
487 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
e6c14898
DK
488 {
489 gfc_error ("Argument '%s' of elemental procedure '%s' at %L must "
25ffd46f
TB
490 "have its INTENT specified or have the VALUE "
491 "attribute", sym->name, proc->name,
e6c14898
DK
492 &sym->declared_at);
493 continue;
494 }
6de9cd9a
DN
495 }
496
497 /* Each dummy shall be specified to be scalar. */
498 if (proc->attr.proc == PROC_ST_FUNCTION)
edf1eac2
SK
499 {
500 if (sym->as != NULL)
501 {
502 gfc_error ("Argument '%s' of statement function at %L must "
503 "be scalar", sym->name, &sym->declared_at);
504 continue;
505 }
506
507 if (sym->ts.type == BT_CHARACTER)
508 {
bc21d315 509 gfc_charlen *cl = sym->ts.u.cl;
edf1eac2
SK
510 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
511 {
512 gfc_error ("Character-valued argument '%s' of statement "
513 "function at %L must have constant length",
514 sym->name, &sym->declared_at);
515 continue;
516 }
517 }
518 }
6de9cd9a 519 }
4213f93b 520 formal_arg_flag = 0;
6de9cd9a
DN
521}
522
523
524/* Work function called when searching for symbols that have argument lists
525 associated with them. */
526
527static void
edf1eac2 528find_arglists (gfc_symbol *sym)
6de9cd9a 529{
c3f34952 530 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
cadddfdd 531 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
6de9cd9a
DN
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
edf1eac2 542resolve_formal_arglists (gfc_namespace *ns)
6de9cd9a 543{
6de9cd9a
DN
544 if (ns == NULL)
545 return;
546
547 gfc_traverse_ns (ns, find_arglists);
548}
549
550
3d79abbd 551static void
edf1eac2 552resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
3d79abbd 553{
524af0d6 554 bool t;
05c1e3a7 555
b5bf3e4d
TB
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)
3d79abbd
PB
560 return;
561
0dd973dd 562 /* Try to find out of what the return type is. */
f9909823 563 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
3d79abbd 564 {
c2de0c19 565 t = gfc_set_default_type (sym->result, 0, ns);
3d79abbd 566
524af0d6 567 if (!t && !sym->result->attr.untyped)
cf4d246b 568 {
c2de0c19
TB
569 if (sym->result == sym)
570 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
571 sym->name, &sym->declared_at);
3070bab4 572 else if (!sym->result->attr.proc_pointer)
c2de0c19
TB
573 gfc_error ("Result '%s' of contained function '%s' at %L has "
574 "no IMPLICIT type", sym->result->name, sym->name,
575 &sym->result->declared_at);
576 sym->result->attr.untyped = 1;
cf4d246b 577 }
3d79abbd 578 }
b95605fb 579
4d382327 580 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
edf1eac2
SK
581 type, lists the only ways a character length value of * can be used:
582 dummy arguments of procedures, named constants, and function results
6c19d9b5
DK
583 in external functions. Internal function results and results of module
584 procedures are not on this list, ergo, not permitted. */
b95605fb 585
c2de0c19 586 if (sym->result->ts.type == BT_CHARACTER)
b95605fb 587 {
bc21d315 588 gfc_charlen *cl = sym->result->ts.u.cl;
8d51f26f 589 if ((!cl || !cl->length) && !sym->result->ts.deferred)
6c19d9b5
DK
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
597 gfc_error ("Character-valued %s '%s' at %L must not be"
598 " assumed length",
599 module_proc ? _("module procedure")
600 : _("internal function"),
601 sym->name, &sym->declared_at);
602 }
b95605fb 603 }
3d79abbd
PB
604}
605
606
607/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
f7b529fa 608 introduce duplicates. */
3d79abbd
PB
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;
05c1e3a7 619 /* See if this arg is already in the formal argument list. */
3d79abbd
PB
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
54129a64
PT
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
3d79abbd
PB
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
edf1eac2 670resolve_entries (gfc_namespace *ns)
3d79abbd
PB
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
f7b529fa 691 /* If this isn't a procedure something has gone horribly wrong. */
6e45f57b 692 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
05c1e3a7 693
3d79abbd
PB
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
1a492601
PT
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
edf1eac2 713 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
1a492601
PT
714 el->sym->ns = ns;
715
08ee9e85
PT
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
3d79abbd 725 /* Add an entry statement for it. */
11e5274a 726 c = gfc_get_code (EXEC_ENTRY);
3d79abbd
PB
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).
7be7d41b
TS
733 Also include the function name so the user has some hope of figuring
734 out what is going on. */
3d79abbd
PB
735 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
736 master_count++, ns->proc_name->name);
3d79abbd 737 gfc_get_ha_symbol (name, &proc);
6e45f57b 738 gcc_assert (proc != NULL);
3d79abbd 739
231b2fcc 740 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
3d79abbd 741 if (ns->proc_name->attr.subroutine)
231b2fcc 742 gfc_add_subroutine (&proc->attr, proc->name, NULL);
3d79abbd
PB
743 else
744 {
d198b59a
JJ
745 gfc_symbol *sym;
746 gfc_typespec *ts, *fts;
5be38273 747 gfc_array_spec *as, *fas;
231b2fcc 748 gfc_add_function (&proc->attr, proc->name, NULL);
d198b59a 749 proc->result = proc;
5be38273
PT
750 fas = ns->entries->sym->as;
751 fas = fas ? fas : ns->entries->sym->result->as;
d198b59a
JJ
752 fts = &ns->entries->sym->result->ts;
753 if (fts->type == BT_UNKNOWN)
713485cc 754 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
d198b59a
JJ
755 for (el = ns->entries->next; el; el = el->next)
756 {
757 ts = &el->sym->result->ts;
5be38273
PT
758 as = el->sym->as;
759 as = as ? as : el->sym->result->as;
d198b59a 760 if (ts->type == BT_UNKNOWN)
713485cc 761 ts = gfc_get_default_type (el->sym->result->name, NULL);
5be38273 762
d198b59a
JJ
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;
f5d67ede
PT
769 else if (as && fas && ns->entries->sym->result != el->sym->result
770 && gfc_compare_array_spec (as, fas) == 0)
107d5ff6 771 gfc_error ("Function %s at %L has entries with mismatched "
5be38273
PT
772 "array specifications", ns->entries->sym->name,
773 &ns->entries->sym->declared_at);
107d5ff6
TB
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. */
bc21d315
JW
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)))
9717f7a1 788 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
107d5ff6
TB
789 "entries returning variables of different "
790 "string lengths", ns->entries->sym->name,
791 &ns->entries->sym->declared_at);
d198b59a
JJ
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 {
49de9e73 806 /* Otherwise the result will be passed through a union by
d198b59a
JJ
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)
edf1eac2
SK
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 }
d198b59a 823 else if (sym->attr.pointer)
edf1eac2
SK
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 }
d198b59a
JJ
834 else
835 {
836 ts = &sym->ts;
837 if (ts->type == BT_UNKNOWN)
713485cc 838 ts = gfc_get_default_type (sym->name, NULL);
d198b59a
JJ
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;
cf4d246b
JJ
858 case BT_UNKNOWN:
859 /* We will issue error elsewhere. */
860 sym = NULL;
861 break;
d198b59a
JJ
862 default:
863 break;
864 }
865 if (sym)
edf1eac2
SK
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 }
d198b59a
JJ
878 }
879 }
880 }
3d79abbd
PB
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
54129a64
PT
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
7be7d41b 894 /* Use the master function for the function body. */
3d79abbd
PB
895 ns->proc_name = proc;
896
7be7d41b 897 /* Finalize the new symbols. */
3d79abbd
PB
898 gfc_commit_symbols ();
899
900 /* Restore the original namespace. */
901 gfc_current_ns = old_ns;
902}
903
904
346ecba8 905/* Resolve common variables. */
ad22b1ff 906static void
346ecba8 907resolve_common_vars (gfc_symbol *sym, bool named_common)
ad22b1ff 908{
346ecba8 909 gfc_symbol *csym = sym;
ad22b1ff 910
346ecba8 911 for (; csym; csym = csym->common_next)
041cf987 912 {
346ecba8
TB
913 if (csym->value || csym->attr.data)
914 {
915 if (!csym->ns->is_block_data)
916 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
917 "but only in BLOCK DATA initialization is "
918 "allowed", csym->name, &csym->declared_at);
919 else if (!named_common)
920 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
921 "in a blank COMMON but initialization is only "
922 "allowed in named common blocks", csym->name,
923 &csym->declared_at);
924 }
925
8b704316
PT
926 if (UNLIMITED_POLY (csym))
927 gfc_error_now ("'%s' in cannot appear in COMMON at %L "
928 "[F2008:C5100]", csym->name, &csym->declared_at);
929
448d2cd2
TS
930 if (csym->ts.type != BT_DERIVED)
931 continue;
932
bc21d315
JW
933 if (!(csym->ts.u.derived->attr.sequence
934 || csym->ts.u.derived->attr.is_bind_c))
448d2cd2
TS
935 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
936 "has neither the SEQUENCE nor the BIND(C) "
937 "attribute", csym->name, &csym->declared_at);
bc21d315 938 if (csym->ts.u.derived->attr.alloc_comp)
448d2cd2
TS
939 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
940 "has an ultimate component that is "
941 "allocatable", csym->name, &csym->declared_at);
16e520b6 942 if (gfc_has_default_initializer (csym->ts.u.derived))
448d2cd2
TS
943 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
944 "may not have default initializer", csym->name,
945 &csym->declared_at);
6f9c9d6d
TB
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);
041cf987 949 }
346ecba8
TB
950}
951
952/* Resolve common blocks. */
953static void
954resolve_common_blocks (gfc_symtree *common_root)
955{
956 gfc_symbol *sym;
878cdb7b 957 gfc_gsymbol * gsym;
346ecba8
TB
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);
ad22b1ff 968
878cdb7b
TB
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 {
987 gfc_error ("In Fortran 2003 COMMON '%s' block at %L is a global "
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 {
1001 gfc_error ("COMMON block '%s' at %L uses the same global identifier "
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 {
1009 gfc_error ("Fortran 2008: COMMON block '%s' with binding label at "
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 {
1031 gfc_error ("COMMON block at %L with binding label %s uses the same "
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
041cf987
TB
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)
1052 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
1053 sym->name, &common_root->n.common->where, &sym->declared_at);
1054
ef71fdd9
JW
1055 if (sym->attr.external)
1056 gfc_error ("COMMON block '%s' at %L can not have the EXTERNAL attribute",
1057 sym->name, &common_root->n.common->where);
1058
041cf987
TB
1059 if (sym->attr.intrinsic)
1060 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
1061 sym->name, &common_root->n.common->where);
1062 else if (sym->attr.result
2d71b918 1063 || gfc_is_function_return_value (sym, gfc_current_ns))
9717f7a1 1064 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
041cf987
TB
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)
9717f7a1 1069 gfc_notify_std (GFC_STD_F2003, "COMMON block '%s' at %L "
041cf987
TB
1070 "that is also a global procedure", sym->name,
1071 &common_root->n.common->where);
ad22b1ff
TB
1072}
1073
1074
6de9cd9a
DN
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
edf1eac2 1085resolve_contained_functions (gfc_namespace *ns)
6de9cd9a 1086{
6de9cd9a 1087 gfc_namespace *child;
3d79abbd 1088 gfc_entry_list *el;
6de9cd9a
DN
1089
1090 resolve_formal_arglists (ns);
1091
1092 for (child = ns->contained; child; child = child->sibling)
1093 {
3d79abbd 1094 /* Resolve alternate entry points first. */
05c1e3a7 1095 resolve_entries (child);
6de9cd9a 1096
3d79abbd
PB
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);
6de9cd9a
DN
1101 }
1102}
1103
1104
524af0d6 1105static bool resolve_fl_derived0 (gfc_symbol *sym);
0291fa25
JW
1106
1107
6de9cd9a 1108/* Resolve all of the elements of a structure constructor and make sure that
80f95228
JW
1109 the types are correct. The 'init' flag indicates that the given
1110 constructor is an initializer. */
6de9cd9a 1111
524af0d6 1112static bool
80f95228 1113resolve_structure_cons (gfc_expr *expr, int init)
6de9cd9a
DN
1114{
1115 gfc_constructor *cons;
1116 gfc_component *comp;
524af0d6 1117 bool t;
5046aff5 1118 symbol_attribute a;
6de9cd9a 1119
524af0d6 1120 t = true;
bd48f123
JW
1121
1122 if (expr->ts.type == BT_DERIVED)
0291fa25 1123 resolve_fl_derived0 (expr->ts.u.derived);
bd48f123 1124
b7e75771 1125 cons = gfc_constructor_first (expr->value.constructor);
6de9cd9a 1126
c3f34952
TB
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
b7e75771 1135 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
6de9cd9a 1136 {
0df50e7a
FXC
1137 int rank;
1138
edf1eac2 1139 if (!cons->expr)
404d8401 1140 continue;
6de9cd9a 1141
524af0d6 1142 if (!gfc_resolve_expr (cons->expr))
6de9cd9a 1143 {
524af0d6 1144 t = false;
6de9cd9a
DN
1145 continue;
1146 }
1147
0df50e7a
FXC
1148 rank = comp->as ? comp->as->rank : 0;
1149 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
d4b7d0f0 1150 && (comp->attr.allocatable || cons->expr->rank))
5046aff5 1151 {
6a38e151 1152 gfc_error ("The rank of the element in the structure "
5046aff5
PT
1153 "constructor at %L does not match that of the "
1154 "component (%d/%d)", &cons->expr->where,
0df50e7a 1155 cons->expr->rank, rank);
524af0d6 1156 t = false;
5046aff5
PT
1157 }
1158
6de9cd9a
DN
1159 /* If we don't have the right type, try to convert it. */
1160
80f95228
JW
1161 if (!comp->attr.proc_pointer &&
1162 !gfc_compare_types (&cons->expr->ts, &comp->ts))
e0e85e06 1163 {
b04533af 1164 if (strcmp (comp->name, "_extends") == 0)
eece1eb9 1165 {
b04533af 1166 /* Can afford to be brutal with the _extends initializer.
eece1eb9
PT
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;
eece1eb9
PT
1170 }
1171 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
e35e87dc
TB
1172 {
1173 gfc_error ("The element in the structure constructor at %L, "
1174 "for pointer component '%s', is %s but should be %s",
1175 &cons->expr->where, comp->name,
1176 gfc_basic_typename (cons->expr->ts.type),
1177 gfc_basic_typename (comp->ts.type));
524af0d6 1178 t = false;
e35e87dc 1179 }
e0e85e06 1180 else
e35e87dc 1181 {
524af0d6
JB
1182 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1183 if (t)
e35e87dc
TB
1184 t = t2;
1185 }
e0e85e06 1186 }
5046aff5 1187
a48a9173
TB
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
083dd940 1197 && cons->expr->rank != 0
a48a9173
TB
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 {
c130efd5
TB
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);
cede9502 1240 free (cl);
a48a9173
TB
1241 }
1242
c130efd5 1243 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
a48a9173
TB
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
c1203a70 1250 if (cons->expr->expr_type == EXPR_NULL
713485cc 1251 && !(comp->attr.pointer || comp->attr.allocatable
cadddfdd 1252 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
cf2b3c22 1253 || (comp->ts.type == BT_CLASS
d40477b4 1254 && (CLASS_DATA (comp)->attr.class_pointer
7a08eda1 1255 || CLASS_DATA (comp)->attr.allocatable))))
c1203a70 1256 {
524af0d6 1257 t = false;
6a38e151 1258 gfc_error ("The NULL in the structure constructor at %L is "
c1203a70
PT
1259 "being applied to component '%s', which is neither "
1260 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1261 comp->name);
1262 }
1263
6a38e151
JW
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
2a573572
MM
1272 c2 = gfc_get_proc_ptr_comp (cons->expr);
1273 if (c2)
6a38e151
JW
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,
6f3ab30d 1290 err, sizeof (err), NULL, NULL))
6a38e151
JW
1291 {
1292 gfc_error ("Interface mismatch for procedure-pointer component "
1293 "'%s' in structure constructor at %L: %s",
1294 comp->name, &cons->expr->where, err);
524af0d6 1295 return false;
6a38e151
JW
1296 }
1297 }
1298
e8cd3983
JW
1299 if (!comp->attr.pointer || comp->attr.proc_pointer
1300 || cons->expr->expr_type == EXPR_NULL)
5046aff5
PT
1301 continue;
1302
1303 a = gfc_expr_attr (cons->expr);
1304
1305 if (!a.pointer && !a.target)
1306 {
524af0d6 1307 t = false;
6a38e151 1308 gfc_error ("The element in the structure constructor at %L, "
5046aff5
PT
1309 "for pointer component '%s' should be a POINTER or "
1310 "a TARGET", &cons->expr->where, comp->name);
1311 }
4eceddd7 1312
80f95228
JW
1313 if (init)
1314 {
1315 /* F08:C461. Additional checks for pointer initialization. */
1316 if (a.allocatable)
1317 {
524af0d6 1318 t = false;
80f95228
JW
1319 gfc_error ("Pointer initialization target at %L "
1320 "must not be ALLOCATABLE ", &cons->expr->where);
1321 }
1322 if (!a.save)
1323 {
524af0d6 1324 t = false;
80f95228
JW
1325 gfc_error ("Pointer initialization target at %L "
1326 "must have the SAVE attribute", &cons->expr->where);
1327 }
1328 }
1329
4eceddd7
TB
1330 /* F2003, C1272 (3). */
1331 if (gfc_pure (NULL) && cons->expr->expr_type == EXPR_VARIABLE
d3a9eea2
TB
1332 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1333 || gfc_is_coindexed (cons->expr)))
4eceddd7 1334 {
524af0d6 1335 t = false;
6a38e151 1336 gfc_error ("Invalid expression in the structure constructor for "
d3a9eea2
TB
1337 "pointer component '%s' at %L in PURE procedure",
1338 comp->name, &cons->expr->where);
4eceddd7 1339 }
80f95228 1340
f1f39033
PT
1341 if (gfc_implicit_pure (NULL)
1342 && cons->expr->expr_type == EXPR_VARIABLE
1343 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1344 || gfc_is_coindexed (cons->expr)))
1345 gfc_current_ns->proc_name->attr.implicit_pure = 0;
1346
6de9cd9a
DN
1347 }
1348
1349 return t;
1350}
1351
1352
6de9cd9a
DN
1353/****************** Expression name resolution ******************/
1354
1355/* Returns 0 if a symbol was not declared with a type or
4f613946 1356 attribute declaration statement, nonzero otherwise. */
6de9cd9a
DN
1357
1358static int
edf1eac2 1359was_declared (gfc_symbol *sym)
6de9cd9a
DN
1360{
1361 symbol_attribute a;
1362
1363 a = sym->attr;
1364
1365 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1366 return 1;
1367
9439ae41 1368 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
edf1eac2 1369 || a.optional || a.pointer || a.save || a.target || a.volatile_
1eee5628 1370 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
be59db2d 1371 || a.asynchronous || a.codimension)
6de9cd9a
DN
1372 return 1;
1373
1374 return 0;
1375}
1376
1377
1378/* Determine if a symbol is generic or not. */
1379
1380static int
edf1eac2 1381generic_sym (gfc_symbol *sym)
6de9cd9a
DN
1382{
1383 gfc_symbol *s;
1384
1385 if (sym->attr.generic ||
1386 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1387 return 1;
1388
1389 if (was_declared (sym) || sym->ns->parent == NULL)
1390 return 0;
1391
1392 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
4d382327 1393
6d023ec5
JD
1394 if (s != NULL)
1395 {
1396 if (s == sym)
1397 return 0;
1398 else
1399 return generic_sym (s);
1400 }
6de9cd9a 1401
6d023ec5 1402 return 0;
6de9cd9a
DN
1403}
1404
1405
1406/* Determine if a symbol is specific or not. */
1407
1408static int
edf1eac2 1409specific_sym (gfc_symbol *sym)
6de9cd9a
DN
1410{
1411 gfc_symbol *s;
1412
1413 if (sym->attr.if_source == IFSRC_IFBODY
1414 || sym->attr.proc == PROC_MODULE
1415 || sym->attr.proc == PROC_INTERNAL
1416 || sym->attr.proc == PROC_ST_FUNCTION
edf1eac2 1417 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
6de9cd9a
DN
1418 || sym->attr.external)
1419 return 1;
1420
1421 if (was_declared (sym) || sym->ns->parent == NULL)
1422 return 0;
1423
1424 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1425
1426 return (s == NULL) ? 0 : specific_sym (s);
1427}
1428
1429
1430/* Figure out if the procedure is specific, generic or unknown. */
1431
1432typedef enum
1433{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1434proc_type;
1435
1436static proc_type
edf1eac2 1437procedure_kind (gfc_symbol *sym)
6de9cd9a 1438{
6de9cd9a
DN
1439 if (generic_sym (sym))
1440 return PTYPE_GENERIC;
1441
1442 if (specific_sym (sym))
1443 return PTYPE_SPECIFIC;
1444
1445 return PTYPE_UNKNOWN;
1446}
1447
48474141 1448/* Check references to assumed size arrays. The flag need_full_assumed_size
b82feea5 1449 is nonzero when matching actual arguments. */
48474141
PT
1450
1451static int need_full_assumed_size = 0;
1452
1453static bool
edf1eac2 1454check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
48474141 1455{
edf1eac2 1456 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
48474141
PT
1457 return false;
1458
e0c68ce9
ILT
1459 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1460 What should it be? */
582f2176 1461 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
c52938ec 1462 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
e0c68ce9 1463 && (e->ref->u.ar.type == AR_FULL))
48474141
PT
1464 {
1465 gfc_error ("The upper bound in the last dimension must "
1466 "appear in the reference to the assumed size "
e25a0da3 1467 "array '%s' at %L", sym->name, &e->where);
48474141
PT
1468 return true;
1469 }
1470 return false;
1471}
1472
1473
1474/* Look for bad assumed size array references in argument expressions
1475 of elemental and array valued intrinsic procedures. Since this is
1476 called from procedure resolution functions, it only recurses at
1477 operators. */
1478
1479static bool
1480resolve_assumed_size_actual (gfc_expr *e)
1481{
1482 if (e == NULL)
1483 return false;
1484
1485 switch (e->expr_type)
1486 {
1487 case EXPR_VARIABLE:
edf1eac2 1488 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
48474141
PT
1489 return true;
1490 break;
1491
1492 case EXPR_OP:
1493 if (resolve_assumed_size_actual (e->value.op.op1)
edf1eac2 1494 || resolve_assumed_size_actual (e->value.op.op2))
48474141
PT
1495 return true;
1496 break;
1497
1498 default:
1499 break;
1500 }
1501 return false;
1502}
1503
6de9cd9a 1504
0b4e2af7
PT
1505/* Check a generic procedure, passed as an actual argument, to see if
1506 there is a matching specific name. If none, it is an error, and if
1507 more than one, the reference is ambiguous. */
1508static int
1509count_specific_procs (gfc_expr *e)
1510{
1511 int n;
1512 gfc_interface *p;
1513 gfc_symbol *sym;
4d382327 1514
0b4e2af7
PT
1515 n = 0;
1516 sym = e->symtree->n.sym;
1517
1518 for (p = sym->generic; p; p = p->next)
1519 if (strcmp (sym->name, p->sym->name) == 0)
1520 {
1521 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1522 sym->name);
1523 n++;
1524 }
1525
1526 if (n > 1)
1527 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1528 &e->where);
1529
1530 if (n == 0)
1531 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1532 "argument at %L", sym->name, &e->where);
1533
1534 return n;
1535}
1536
a03826d1 1537
1933ba0f 1538/* See if a call to sym could possibly be a not allowed RECURSION because of
eea58adb 1539 a missing RECURSIVE declaration. This means that either sym is the current
1933ba0f
DK
1540 context itself, or sym is the parent of a contained procedure calling its
1541 non-RECURSIVE containing procedure.
1542 This also works if sym is an ENTRY. */
1543
1544static bool
1545is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1546{
1547 gfc_symbol* proc_sym;
1548 gfc_symbol* context_proc;
9abe5e56 1549 gfc_namespace* real_context;
1933ba0f 1550
c3f34952
TB
1551 if (sym->attr.flavor == FL_PROGRAM
1552 || sym->attr.flavor == FL_DERIVED)
6f7e06ce
JD
1553 return false;
1554
1933ba0f
DK
1555 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1556
1557 /* If we've got an ENTRY, find real procedure. */
1558 if (sym->attr.entry && sym->ns->entries)
1559 proc_sym = sym->ns->entries->sym;
1560 else
1561 proc_sym = sym;
1562
1563 /* If sym is RECURSIVE, all is well of course. */
1564 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1565 return false;
1566
9abe5e56
DK
1567 /* Find the context procedure's "real" symbol if it has entries.
1568 We look for a procedure symbol, so recurse on the parents if we don't
1569 find one (like in case of a BLOCK construct). */
1570 for (real_context = context; ; real_context = real_context->parent)
1571 {
1572 /* We should find something, eventually! */
1573 gcc_assert (real_context);
1574
1575 context_proc = (real_context->entries ? real_context->entries->sym
1576 : real_context->proc_name);
1577
1578 /* In some special cases, there may not be a proc_name, like for this
1579 invalid code:
1580 real(bad_kind()) function foo () ...
1581 when checking the call to bad_kind ().
1582 In these cases, we simply return here and assume that the
1583 call is ok. */
1584 if (!context_proc)
1585 return false;
1586
1587 if (context_proc->attr.flavor != FL_LABEL)
1588 break;
1589 }
1933ba0f
DK
1590
1591 /* A call from sym's body to itself is recursion, of course. */
1592 if (context_proc == proc_sym)
1593 return true;
1594
1595 /* The same is true if context is a contained procedure and sym the
1596 containing one. */
1597 if (context_proc->attr.contained)
1598 {
1599 gfc_symbol* parent_proc;
1600
1601 gcc_assert (context->parent);
1602 parent_proc = (context->parent->entries ? context->parent->entries->sym
1603 : context->parent->proc_name);
1604
1605 if (parent_proc == proc_sym)
1606 return true;
1607 }
1608
1609 return false;
1610}
1611
1612
c73b6478
JW
1613/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1614 its typespec and formal argument list. */
1615
524af0d6 1616bool
2dda89a8 1617gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
c73b6478 1618{
d000aa67 1619 gfc_intrinsic_sym* isym = NULL;
f6038131
JW
1620 const char* symstd;
1621
1622 if (sym->formal)
524af0d6 1623 return true;
f6038131 1624
13157033
TB
1625 /* Already resolved. */
1626 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
524af0d6 1627 return true;
13157033 1628
f6038131
JW
1629 /* We already know this one is an intrinsic, so we don't call
1630 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1631 gfc_find_subroutine directly to check whether it is a function or
1632 subroutine. */
1633
cadddfdd
TB
1634 if (sym->intmod_sym_id && sym->attr.subroutine)
1635 {
1636 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1637 isym = gfc_intrinsic_subroutine_by_id (id);
1638 }
1639 else if (sym->intmod_sym_id)
1640 {
1641 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1642 isym = gfc_intrinsic_function_by_id (id);
1643 }
2b91eb32 1644 else if (!sym->attr.subroutine)
d000aa67
TB
1645 isym = gfc_find_function (sym->name);
1646
cadddfdd 1647 if (isym && !sym->attr.subroutine)
c73b6478 1648 {
f6038131
JW
1649 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1650 && !sym->attr.implicit_type)
1651 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1652 " ignored", sym->name, &sym->declared_at);
1653
c73b6478 1654 if (!sym->attr.function &&
524af0d6
JB
1655 !gfc_add_function(&sym->attr, sym->name, loc))
1656 return false;
f6038131 1657
c73b6478
JW
1658 sym->ts = isym->ts;
1659 }
cadddfdd 1660 else if (isym || (isym = gfc_find_subroutine (sym->name)))
c73b6478 1661 {
f6038131
JW
1662 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1663 {
1664 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1665 " specifier", sym->name, &sym->declared_at);
524af0d6 1666 return false;
f6038131
JW
1667 }
1668
c73b6478 1669 if (!sym->attr.subroutine &&
524af0d6
JB
1670 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1671 return false;
c73b6478 1672 }
f6038131
JW
1673 else
1674 {
1675 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1676 &sym->declared_at);
524af0d6 1677 return false;
f6038131
JW
1678 }
1679
1680 gfc_copy_formal_args_intr (sym, isym);
1681
019c0e5d
TB
1682 sym->attr.pure = isym->pure;
1683 sym->attr.elemental = isym->elemental;
1684
f6038131 1685 /* Check it is actually available in the standard settings. */
524af0d6 1686 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
f6038131
JW
1687 {
1688 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1689 " available in the current standard settings but %s. Use"
1690 " an appropriate -std=* option or enable -fall-intrinsics"
1691 " in order to use it.",
1692 sym->name, &sym->declared_at, symstd);
524af0d6 1693 return false;
f6038131
JW
1694 }
1695
524af0d6 1696 return true;
c73b6478
JW
1697}
1698
1699
a03826d1
DK
1700/* Resolve a procedure expression, like passing it to a called procedure or as
1701 RHS for a procedure pointer assignment. */
1702
524af0d6 1703static bool
a03826d1
DK
1704resolve_procedure_expression (gfc_expr* expr)
1705{
1706 gfc_symbol* sym;
1707
1933ba0f 1708 if (expr->expr_type != EXPR_VARIABLE)
524af0d6 1709 return true;
a03826d1 1710 gcc_assert (expr->symtree);
1933ba0f 1711
a03826d1 1712 sym = expr->symtree->n.sym;
c73b6478
JW
1713
1714 if (sym->attr.intrinsic)
2dda89a8 1715 gfc_resolve_intrinsic (sym, &expr->where);
c73b6478 1716
1933ba0f
DK
1717 if (sym->attr.flavor != FL_PROCEDURE
1718 || (sym->attr.function && sym->result == sym))
524af0d6 1719 return true;
a03826d1
DK
1720
1721 /* A non-RECURSIVE procedure that is used as procedure expression within its
1722 own body is in danger of being called recursively. */
1933ba0f 1723 if (is_illegal_recursion (sym, gfc_current_ns))
a03826d1
DK
1724 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1725 " itself recursively. Declare it RECURSIVE or use"
1726 " -frecursive", sym->name, &expr->where);
4d382327 1727
524af0d6 1728 return true;
a03826d1
DK
1729}
1730
1731
6de9cd9a
DN
1732/* Resolve an actual argument list. Most of the time, this is just
1733 resolving the expressions in the list.
1734 The exception is that we sometimes have to decide whether arguments
1735 that look like procedure arguments are really simple variable
1736 references. */
1737
524af0d6 1738static bool
0b4e2af7
PT
1739resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1740 bool no_formal_args)
6de9cd9a
DN
1741{
1742 gfc_symbol *sym;
1743 gfc_symtree *parent_st;
1744 gfc_expr *e;
5ad6345e 1745 int save_need_full_assumed_size;
524af0d6 1746 bool return_value = false;
c62c6622 1747 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
d3a9eea2 1748
c62c6622
TB
1749 actual_arg = true;
1750 first_actual_arg = true;
45a69325 1751
6de9cd9a
DN
1752 for (; arg; arg = arg->next)
1753 {
6de9cd9a
DN
1754 e = arg->expr;
1755 if (e == NULL)
edf1eac2
SK
1756 {
1757 /* Check the label is a valid branching target. */
1758 if (arg->label)
1759 {
1760 if (arg->label->defined == ST_LABEL_UNKNOWN)
1761 {
1762 gfc_error ("Label %d referenced at %L is never defined",
1763 arg->label->value, &arg->label->where);
c62c6622 1764 goto cleanup;
edf1eac2
SK
1765 }
1766 }
c62c6622 1767 first_actual_arg = false;
edf1eac2
SK
1768 continue;
1769 }
6de9cd9a 1770
67cec813 1771 if (e->expr_type == EXPR_VARIABLE
0b4e2af7
PT
1772 && e->symtree->n.sym->attr.generic
1773 && no_formal_args
1774 && count_specific_procs (e) != 1)
c62c6622 1775 goto cleanup;
27372c38 1776
6de9cd9a
DN
1777 if (e->ts.type != BT_PROCEDURE)
1778 {
5ad6345e 1779 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1780 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1781 need_full_assumed_size = 0;
524af0d6 1782 if (!gfc_resolve_expr (e))
c62c6622 1783 goto cleanup;
5ad6345e 1784 need_full_assumed_size = save_need_full_assumed_size;
7fcafa71 1785 goto argument_list;
6de9cd9a
DN
1786 }
1787
edf1eac2 1788 /* See if the expression node should really be a variable reference. */
6de9cd9a
DN
1789
1790 sym = e->symtree->n.sym;
1791
1792 if (sym->attr.flavor == FL_PROCEDURE
1793 || sym->attr.intrinsic
1794 || sym->attr.external)
1795 {
0e7e7e6e 1796 int actual_ok;
6de9cd9a 1797
d68bd5a8
PT
1798 /* If a procedure is not already determined to be something else
1799 check if it is intrinsic. */
0e8d854e 1800 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
d68bd5a8
PT
1801 sym->attr.intrinsic = 1;
1802
2ed8d224
PT
1803 if (sym->attr.proc == PROC_ST_FUNCTION)
1804 {
1805 gfc_error ("Statement function '%s' at %L is not allowed as an "
1806 "actual argument", sym->name, &e->where);
1807 }
1808
edf1eac2
SK
1809 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1810 sym->attr.subroutine);
0e7e7e6e
FXC
1811 if (sym->attr.intrinsic && actual_ok == 0)
1812 {
1813 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1814 "actual argument", sym->name, &e->where);
1815 }
0e7e7e6e 1816
2ed8d224
PT
1817 if (sym->attr.contained && !sym->attr.use_assoc
1818 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1819 {
524af0d6
JB
1820 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure '%s' is"
1821 " used as actual argument at %L",
1822 sym->name, &e->where))
c62c6622 1823 goto cleanup;
2ed8d224
PT
1824 }
1825
1826 if (sym->attr.elemental && !sym->attr.intrinsic)
1827 {
1828 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
edf1eac2 1829 "allowed as an actual argument at %L", sym->name,
2ed8d224
PT
1830 &e->where);
1831 }
781e1004 1832
36d3fb4c
PT
1833 /* Check if a generic interface has a specific procedure
1834 with the same name before emitting an error. */
0b4e2af7 1835 if (sym->attr.generic && count_specific_procs (e) != 1)
c62c6622
TB
1836 goto cleanup;
1837
0b4e2af7
PT
1838 /* Just in case a specific was found for the expression. */
1839 sym = e->symtree->n.sym;
3e978d30 1840
6de9cd9a
DN
1841 /* If the symbol is the function that names the current (or
1842 parent) scope, then we really have a variable reference. */
1843
2d71b918 1844 if (gfc_is_function_return_value (sym, sym->ns))
6de9cd9a
DN
1845 goto got_variable;
1846
20a037d5 1847 /* If all else fails, see if we have a specific intrinsic. */
26033479 1848 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
20a037d5
PT
1849 {
1850 gfc_intrinsic_sym *isym;
6cc309c9 1851
20a037d5
PT
1852 isym = gfc_find_function (sym->name);
1853 if (isym == NULL || !isym->specific)
1854 {
1855 gfc_error ("Unable to find a specific INTRINSIC procedure "
1856 "for the reference '%s' at %L", sym->name,
1857 &e->where);
c62c6622 1858 goto cleanup;
20a037d5
PT
1859 }
1860 sym->ts = isym->ts;
6cc309c9 1861 sym->attr.intrinsic = 1;
26033479 1862 sym->attr.function = 1;
20a037d5 1863 }
a03826d1 1864
524af0d6 1865 if (!gfc_resolve_expr (e))
c62c6622 1866 goto cleanup;
7fcafa71 1867 goto argument_list;
6de9cd9a
DN
1868 }
1869
1870 /* See if the name is a module procedure in a parent unit. */
1871
1872 if (was_declared (sym) || sym->ns->parent == NULL)
1873 goto got_variable;
1874
1875 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1876 {
1877 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
c62c6622 1878 goto cleanup;
6de9cd9a
DN
1879 }
1880
1881 if (parent_st == NULL)
1882 goto got_variable;
1883
1884 sym = parent_st->n.sym;
1885 e->symtree = parent_st; /* Point to the right thing. */
1886
1887 if (sym->attr.flavor == FL_PROCEDURE
1888 || sym->attr.intrinsic
1889 || sym->attr.external)
1890 {
524af0d6 1891 if (!gfc_resolve_expr (e))
c62c6622 1892 goto cleanup;
7fcafa71 1893 goto argument_list;
6de9cd9a
DN
1894 }
1895
1896 got_variable:
1897 e->expr_type = EXPR_VARIABLE;
1898 e->ts = sym->ts;
102344e2
TB
1899 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1900 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1901 && CLASS_DATA (sym)->as))
6de9cd9a 1902 {
102344e2
TB
1903 e->rank = sym->ts.type == BT_CLASS
1904 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
6de9cd9a
DN
1905 e->ref = gfc_get_ref ();
1906 e->ref->type = REF_ARRAY;
1907 e->ref->u.ar.type = AR_FULL;
102344e2
TB
1908 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1909 ? CLASS_DATA (sym)->as : sym->as;
6de9cd9a 1910 }
7fcafa71 1911
1b35264f
DF
1912 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1913 primary.c (match_actual_arg). If above code determines that it
1914 is a variable instead, it needs to be resolved as it was not
1915 done at the beginning of this function. */
5ad6345e 1916 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1917 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1918 need_full_assumed_size = 0;
524af0d6 1919 if (!gfc_resolve_expr (e))
c62c6622 1920 goto cleanup;
5ad6345e 1921 need_full_assumed_size = save_need_full_assumed_size;
1b35264f 1922
7fcafa71
PT
1923 argument_list:
1924 /* Check argument list functions %VAL, %LOC and %REF. There is
1925 nothing to do for %REF. */
1926 if (arg->name && arg->name[0] == '%')
1927 {
1928 if (strncmp ("%VAL", arg->name, 4) == 0)
1929 {
1930 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1931 {
1932 gfc_error ("By-value argument at %L is not of numeric "
1933 "type", &e->where);
c62c6622 1934 goto cleanup;
7fcafa71
PT
1935 }
1936
1937 if (e->rank)
1938 {
1939 gfc_error ("By-value argument at %L cannot be an array or "
1940 "an array section", &e->where);
c62c6622 1941 goto cleanup;
7fcafa71
PT
1942 }
1943
1944 /* Intrinsics are still PROC_UNKNOWN here. However,
1945 since same file external procedures are not resolvable
1946 in gfortran, it is a good deal easier to leave them to
1947 intrinsic.c. */
7193e30a
TB
1948 if (ptype != PROC_UNKNOWN
1949 && ptype != PROC_DUMMY
29ea08da
TB
1950 && ptype != PROC_EXTERNAL
1951 && ptype != PROC_MODULE)
7fcafa71
PT
1952 {
1953 gfc_error ("By-value argument at %L is not allowed "
1954 "in this context", &e->where);
c62c6622 1955 goto cleanup;
7fcafa71 1956 }
7fcafa71
PT
1957 }
1958
1959 /* Statement functions have already been excluded above. */
1960 else if (strncmp ("%LOC", arg->name, 4) == 0
edf1eac2 1961 && e->ts.type == BT_PROCEDURE)
7fcafa71
PT
1962 {
1963 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1964 {
1965 gfc_error ("Passing internal procedure at %L by location "
1966 "not allowed", &e->where);
c62c6622 1967 goto cleanup;
7fcafa71
PT
1968 }
1969 }
1970 }
d3a9eea2
TB
1971
1972 /* Fortran 2008, C1237. */
1973 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
c62c6622
TB
1974 && gfc_has_ultimate_pointer (e))
1975 {
1976 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
d3a9eea2 1977 "component", &e->where);
c62c6622
TB
1978 goto cleanup;
1979 }
1980
1981 first_actual_arg = false;
6de9cd9a
DN
1982 }
1983
524af0d6 1984 return_value = true;
c62c6622
TB
1985
1986cleanup:
1987 actual_arg = actual_arg_sav;
1988 first_actual_arg = first_actual_arg_sav;
1989
1990 return return_value;
6de9cd9a
DN
1991}
1992
1993
b8ea6dbc
PT
1994/* Do the checks of the actual argument list that are specific to elemental
1995 procedures. If called with c == NULL, we have a function, otherwise if
1996 expr == NULL, we have a subroutine. */
edf1eac2 1997
524af0d6 1998static bool
b8ea6dbc
PT
1999resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2000{
2001 gfc_actual_arglist *arg0;
2002 gfc_actual_arglist *arg;
2003 gfc_symbol *esym = NULL;
2004 gfc_intrinsic_sym *isym = NULL;
2005 gfc_expr *e = NULL;
2006 gfc_intrinsic_arg *iformal = NULL;
2007 gfc_formal_arglist *eformal = NULL;
2008 bool formal_optional = false;
2009 bool set_by_optional = false;
2010 int i;
2011 int rank = 0;
2012
2013 /* Is this an elemental procedure? */
2014 if (expr && expr->value.function.actual != NULL)
2015 {
2016 if (expr->value.function.esym != NULL
edf1eac2 2017 && expr->value.function.esym->attr.elemental)
b8ea6dbc
PT
2018 {
2019 arg0 = expr->value.function.actual;
2020 esym = expr->value.function.esym;
2021 }
2022 else if (expr->value.function.isym != NULL
edf1eac2 2023 && expr->value.function.isym->elemental)
b8ea6dbc
PT
2024 {
2025 arg0 = expr->value.function.actual;
2026 isym = expr->value.function.isym;
2027 }
2028 else
524af0d6 2029 return true;
b8ea6dbc 2030 }
dd9315de 2031 else if (c && c->ext.actual != NULL)
b8ea6dbc
PT
2032 {
2033 arg0 = c->ext.actual;
4d382327 2034
dd9315de
DK
2035 if (c->resolved_sym)
2036 esym = c->resolved_sym;
2037 else
2038 esym = c->symtree->n.sym;
2039 gcc_assert (esym);
2040
2041 if (!esym->attr.elemental)
524af0d6 2042 return true;
b8ea6dbc
PT
2043 }
2044 else
524af0d6 2045 return true;
b8ea6dbc
PT
2046
2047 /* The rank of an elemental is the rank of its array argument(s). */
2048 for (arg = arg0; arg; arg = arg->next)
2049 {
c62c6622 2050 if (arg->expr != NULL && arg->expr->rank != 0)
b8ea6dbc
PT
2051 {
2052 rank = arg->expr->rank;
2053 if (arg->expr->expr_type == EXPR_VARIABLE
edf1eac2 2054 && arg->expr->symtree->n.sym->attr.optional)
b8ea6dbc
PT
2055 set_by_optional = true;
2056
2057 /* Function specific; set the result rank and shape. */
2058 if (expr)
2059 {
2060 expr->rank = rank;
2061 if (!expr->shape && arg->expr->shape)
2062 {
2063 expr->shape = gfc_get_shape (rank);
2064 for (i = 0; i < rank; i++)
2065 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2066 }
2067 }
2068 break;
2069 }
2070 }
2071
2072 /* If it is an array, it shall not be supplied as an actual argument
2073 to an elemental procedure unless an array of the same rank is supplied
2074 as an actual argument corresponding to a nonoptional dummy argument of
2075 that elemental procedure(12.4.1.5). */
2076 formal_optional = false;
2077 if (isym)
2078 iformal = isym->formal;
2079 else
2080 eformal = esym->formal;
2081
2082 for (arg = arg0; arg; arg = arg->next)
2083 {
2084 if (eformal)
2085 {
2086 if (eformal->sym && eformal->sym->attr.optional)
2087 formal_optional = true;
2088 eformal = eformal->next;
2089 }
2090 else if (isym && iformal)
2091 {
2092 if (iformal->optional)
2093 formal_optional = true;
2094 iformal = iformal->next;
2095 }
2096 else if (isym)
2097 formal_optional = true;
2098
994c1cc0 2099 if (pedantic && arg->expr != NULL
edf1eac2
SK
2100 && arg->expr->expr_type == EXPR_VARIABLE
2101 && arg->expr->symtree->n.sym->attr.optional
2102 && formal_optional
2103 && arg->expr->rank
2104 && (set_by_optional || arg->expr->rank != rank)
cd5ecab6 2105 && !(isym && isym->id == GFC_ISYM_CONVERSION))
b8ea6dbc 2106 {
994c1cc0
SK
2107 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
2108 "MISSING, it cannot be the actual argument of an "
edf1eac2 2109 "ELEMENTAL procedure unless there is a non-optional "
994c1cc0
SK
2110 "argument with the same rank (12.4.1.5)",
2111 arg->expr->symtree->n.sym->name, &arg->expr->where);
b8ea6dbc
PT
2112 }
2113 }
2114
2115 for (arg = arg0; arg; arg = arg->next)
2116 {
2117 if (arg->expr == NULL || arg->expr->rank == 0)
2118 continue;
2119
2120 /* Being elemental, the last upper bound of an assumed size array
2121 argument must be present. */
2122 if (resolve_assumed_size_actual (arg->expr))
524af0d6 2123 return false;
b8ea6dbc 2124
3c7b91d3 2125 /* Elemental procedure's array actual arguments must conform. */
b8ea6dbc
PT
2126 if (e != NULL)
2127 {
524af0d6
JB
2128 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2129 return false;
b8ea6dbc
PT
2130 }
2131 else
2132 e = arg->expr;
2133 }
2134
4a965827
TB
2135 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2136 is an array, the intent inout/out variable needs to be also an array. */
2137 if (rank > 0 && esym && expr == NULL)
2138 for (eformal = esym->formal, arg = arg0; arg && eformal;
2139 arg = arg->next, eformal = eformal->next)
2140 if ((eformal->sym->attr.intent == INTENT_OUT
2141 || eformal->sym->attr.intent == INTENT_INOUT)
2142 && arg->expr && arg->expr->rank == 0)
2143 {
2144 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
2145 "ELEMENTAL subroutine '%s' is a scalar, but another "
2146 "actual argument is an array", &arg->expr->where,
2147 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2148 : "INOUT", eformal->sym->name, esym->name);
524af0d6 2149 return false;
4a965827 2150 }
524af0d6 2151 return true;
b8ea6dbc
PT
2152}
2153
2154
68ea355b
PT
2155/* This function does the checking of references to global procedures
2156 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2157 77 and 95 standards. It checks for a gsymbol for the name, making
2158 one if it does not already exist. If it already exists, then the
2159 reference being resolved must correspond to the type of gsymbol.
05c1e3a7 2160 Otherwise, the new symbol is equipped with the attributes of the
68ea355b 2161 reference. The corresponding code that is called in creating
71a7778c
PT
2162 global entities is parse.c.
2163
2164 In addition, for all but -std=legacy, the gsymbols are used to
2165 check the interfaces of external procedures from the same file.
2166 The namespace of the gsymbol is resolved and then, once this is
2167 done the interface is checked. */
68ea355b 2168
3af8d8cb
PT
2169
2170static bool
2171not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2172{
2173 if (!gsym_ns->proc_name->attr.recursive)
2174 return true;
2175
2176 if (sym->ns == gsym_ns)
2177 return false;
2178
2179 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2180 return false;
2181
2182 return true;
2183}
2184
2185static bool
2186not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2187{
2188 if (gsym_ns->entries)
2189 {
2190 gfc_entry_list *entry = gsym_ns->entries;
2191
2192 for (; entry; entry = entry->next)
2193 {
2194 if (strcmp (sym->name, entry->sym->name) == 0)
2195 {
2196 if (strcmp (gsym_ns->proc_name->name,
2197 sym->ns->proc_name->name) == 0)
2198 return false;
2199
2200 if (sym->ns->parent
2201 && strcmp (gsym_ns->proc_name->name,
2202 sym->ns->parent->proc_name->name) == 0)
2203 return false;
2204 }
2205 }
2206 }
2207 return true;
2208}
2209
96486998
JW
2210
2211/* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2212
2213bool
2214gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2215{
2216 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2217
2218 for ( ; arg; arg = arg->next)
2219 {
2220 if (!arg->sym)
2221 continue;
2222
2223 if (arg->sym->attr.allocatable) /* (2a) */
2224 {
2225 strncpy (errmsg, _("allocatable argument"), err_len);
2226 return true;
2227 }
2228 else if (arg->sym->attr.asynchronous)
2229 {
2230 strncpy (errmsg, _("asynchronous argument"), err_len);
2231 return true;
2232 }
2233 else if (arg->sym->attr.optional)
2234 {
2235 strncpy (errmsg, _("optional argument"), err_len);
2236 return true;
2237 }
2238 else if (arg->sym->attr.pointer)
2239 {
2240 strncpy (errmsg, _("pointer argument"), err_len);
2241 return true;
2242 }
2243 else if (arg->sym->attr.target)
2244 {
2245 strncpy (errmsg, _("target argument"), err_len);
2246 return true;
2247 }
2248 else if (arg->sym->attr.value)
2249 {
2250 strncpy (errmsg, _("value argument"), err_len);
2251 return true;
2252 }
2253 else if (arg->sym->attr.volatile_)
2254 {
2255 strncpy (errmsg, _("volatile argument"), err_len);
2256 return true;
2257 }
2258 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2259 {
2260 strncpy (errmsg, _("assumed-shape argument"), err_len);
2261 return true;
2262 }
2263 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2264 {
2265 strncpy (errmsg, _("assumed-rank argument"), err_len);
2266 return true;
2267 }
2268 else if (arg->sym->attr.codimension) /* (2c) */
2269 {
2270 strncpy (errmsg, _("coarray argument"), err_len);
2271 return true;
2272 }
2273 else if (false) /* (2d) TODO: parametrized derived type */
2274 {
2275 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2276 return true;
2277 }
2278 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2279 {
2280 strncpy (errmsg, _("polymorphic argument"), err_len);
2281 return true;
2282 }
e7ac6a7c
TB
2283 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2284 {
2285 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2286 return true;
2287 }
96486998
JW
2288 else if (arg->sym->ts.type == BT_ASSUMED)
2289 {
2290 /* As assumed-type is unlimited polymorphic (cf. above).
2291 See also TS 29113, Note 6.1. */
2292 strncpy (errmsg, _("assumed-type argument"), err_len);
2293 return true;
2294 }
2295 }
2296
2297 if (sym->attr.function)
2298 {
2299 gfc_symbol *res = sym->result ? sym->result : sym;
2300
2301 if (res->attr.dimension) /* (3a) */
2302 {
2303 strncpy (errmsg, _("array result"), err_len);
2304 return true;
2305 }
2306 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2307 {
2308 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2309 return true;
2310 }
2311 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2312 && res->ts.u.cl->length
2313 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2314 {
2315 strncpy (errmsg, _("result with non-constant character length"), err_len);
2316 return true;
2317 }
2318 }
2319
019c0e5d 2320 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
96486998
JW
2321 {
2322 strncpy (errmsg, _("elemental procedure"), err_len);
2323 return true;
2324 }
2325 else if (sym->attr.is_bind_c) /* (5) */
2326 {
2327 strncpy (errmsg, _("bind(c) procedure"), err_len);
2328 return true;
2329 }
2330
2331 return false;
2332}
2333
2334
ff604888 2335static void
71a7778c
PT
2336resolve_global_procedure (gfc_symbol *sym, locus *where,
2337 gfc_actual_arglist **actual, int sub)
68ea355b
PT
2338{
2339 gfc_gsymbol * gsym;
71a7778c 2340 gfc_namespace *ns;
32e8bb8e 2341 enum gfc_symbol_type type;
96486998 2342 char reason[200];
68ea355b
PT
2343
2344 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2345
f11de7c5 2346 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
68ea355b
PT
2347
2348 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
ca39e6f2 2349 gfc_global_used (gsym, where);
68ea355b 2350
9fa52231
TB
2351 if ((sym->attr.if_source == IFSRC_UNKNOWN
2352 || sym->attr.if_source == IFSRC_IFBODY)
2353 && gsym->type != GSYM_UNKNOWN
2354 && gsym->ns
2355 && gsym->ns->resolved != -1
2356 && gsym->ns->proc_name
2357 && not_in_recursive (sym, gsym->ns)
2358 && not_entry_self_reference (sym, gsym->ns))
71a7778c 2359 {
48a32c49
TB
2360 gfc_symbol *def_sym;
2361
cc9a4ca9 2362 /* Resolve the gsymbol namespace if needed. */
71a7778c 2363 if (!gsym->ns->resolved)
3af8d8cb
PT
2364 {
2365 gfc_dt_list *old_dt_list;
c7d3bb76 2366 struct gfc_omp_saved_state old_omp_state;
3af8d8cb
PT
2367
2368 /* Stash away derived types so that the backend_decls do not
2369 get mixed up. */
2370 old_dt_list = gfc_derived_types;
2371 gfc_derived_types = NULL;
c7d3bb76
JJ
2372 /* And stash away openmp state. */
2373 gfc_omp_save_and_clear_state (&old_omp_state);
3af8d8cb
PT
2374
2375 gfc_resolve (gsym->ns);
2376
2377 /* Store the new derived types with the global namespace. */
2378 if (gfc_derived_types)
2379 gsym->ns->derived_types = gfc_derived_types;
2380
2381 /* Restore the derived types of this namespace. */
2382 gfc_derived_types = old_dt_list;
c7d3bb76
JJ
2383 /* And openmp state. */
2384 gfc_omp_restore_state (&old_omp_state);
3af8d8cb
PT
2385 }
2386
cc9a4ca9
PT
2387 /* Make sure that translation for the gsymbol occurs before
2388 the procedure currently being resolved. */
2389 ns = gfc_global_ns_list;
2390 for (; ns && ns != gsym->ns; ns = ns->sibling)
2391 {
2392 if (ns->sibling == gsym->ns)
2393 {
2394 ns->sibling = gsym->ns->sibling;
2395 gsym->ns->sibling = gfc_global_ns_list;
2396 gfc_global_ns_list = gsym->ns;
2397 break;
2398 }
2399 }
2400
48a32c49 2401 def_sym = gsym->ns->proc_name;
77f8682b
TB
2402
2403 /* This can happen if a binding name has been specified. */
2404 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2405 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2406
48a32c49
TB
2407 if (def_sym->attr.entry_master)
2408 {
2409 gfc_entry_list *entry;
2410 for (entry = gsym->ns->entries; entry; entry = entry->next)
2411 if (strcmp (entry->sym->name, sym->name) == 0)
2412 {
2413 def_sym = entry->sym;
2414 break;
2415 }
2416 }
2417
96486998 2418 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
30145da5 2419 {
96486998
JW
2420 gfc_error ("Return type mismatch of function '%s' at %L (%s/%s)",
2421 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2422 gfc_typename (&def_sym->ts));
2423 goto done;
30145da5
DF
2424 }
2425
96486998
JW
2426 if (sym->attr.if_source == IFSRC_UNKNOWN
2427 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
30145da5 2428 {
96486998
JW
2429 gfc_error ("Explicit interface required for '%s' at %L: %s",
2430 sym->name, &sym->declared_at, reason);
2431 goto done;
1b1a6626
DF
2432 }
2433
96486998
JW
2434 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2435 /* Turn erros into warnings with -std=gnu and -std=legacy. */
2436 gfc_errors_to_warnings (1);
1b1a6626 2437
96486998
JW
2438 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2439 reason, sizeof(reason), NULL, NULL))
2440 {
2441 gfc_error ("Interface mismatch in global procedure '%s' at %L: %s ",
2442 sym->name, &sym->declared_at, reason);
2443 goto done;
30145da5
DF
2444 }
2445
9fa52231 2446 if (!pedantic
30145da5
DF
2447 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2448 && !(gfc_option.warn_std & GFC_STD_GNU)))
3af8d8cb 2449 gfc_errors_to_warnings (1);
71a7778c 2450
4d382327 2451 if (sym->attr.if_source != IFSRC_IFBODY)
fb55ca75 2452 gfc_procedure_use (def_sym, actual, where);
71a7778c 2453 }
96486998
JW
2454
2455done:
2456 gfc_errors_to_warnings (0);
71a7778c 2457
68ea355b
PT
2458 if (gsym->type == GSYM_UNKNOWN)
2459 {
2460 gsym->type = type;
2461 gsym->where = *where;
2462 }
2463
2464 gsym->used = 1;
2465}
1524f80b 2466
edf1eac2 2467
6de9cd9a
DN
2468/************* Function resolution *************/
2469
2470/* Resolve a function call known to be generic.
2471 Section 14.1.2.4.1. */
2472
2473static match
edf1eac2 2474resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
6de9cd9a
DN
2475{
2476 gfc_symbol *s;
2477
2478 if (sym->attr.generic)
2479 {
edf1eac2 2480 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
6de9cd9a
DN
2481 if (s != NULL)
2482 {
2483 expr->value.function.name = s->name;
2484 expr->value.function.esym = s;
f5f701ad
PT
2485
2486 if (s->ts.type != BT_UNKNOWN)
2487 expr->ts = s->ts;
2488 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2489 expr->ts = s->result->ts;
2490
6de9cd9a
DN
2491 if (s->as != NULL)
2492 expr->rank = s->as->rank;
f5f701ad
PT
2493 else if (s->result != NULL && s->result->as != NULL)
2494 expr->rank = s->result->as->rank;
2495
0a164a3c
PT
2496 gfc_set_sym_referenced (expr->value.function.esym);
2497
6de9cd9a
DN
2498 return MATCH_YES;
2499 }
2500
edf1eac2
SK
2501 /* TODO: Need to search for elemental references in generic
2502 interface. */
6de9cd9a
DN
2503 }
2504
2505 if (sym->attr.intrinsic)
2506 return gfc_intrinsic_func_interface (expr, 0);
2507
2508 return MATCH_NO;
2509}
2510
2511
524af0d6 2512static bool
edf1eac2 2513resolve_generic_f (gfc_expr *expr)
6de9cd9a
DN
2514{
2515 gfc_symbol *sym;
2516 match m;
c3f34952 2517 gfc_interface *intr = NULL;
6de9cd9a
DN
2518
2519 sym = expr->symtree->n.sym;
2520
2521 for (;;)
2522 {
2523 m = resolve_generic_f0 (expr, sym);
2524 if (m == MATCH_YES)
524af0d6 2525 return true;
6de9cd9a 2526 else if (m == MATCH_ERROR)
524af0d6 2527 return false;
6de9cd9a
DN
2528
2529generic:
c3f34952
TB
2530 if (!intr)
2531 for (intr = sym->generic; intr; intr = intr->next)
2532 if (intr->sym->attr.flavor == FL_DERIVED)
2533 break;
2534
6de9cd9a
DN
2535 if (sym->ns->parent == NULL)
2536 break;
2537 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2538
2539 if (sym == NULL)
2540 break;
2541 if (!generic_sym (sym))
2542 goto generic;
2543 }
2544
71f77fd7
PT
2545 /* Last ditch attempt. See if the reference is to an intrinsic
2546 that possesses a matching interface. 14.1.2.4 */
c3f34952 2547 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a 2548 {
c3f34952
TB
2549 gfc_error ("There is no specific function for the generic '%s' "
2550 "at %L", expr->symtree->n.sym->name, &expr->where);
524af0d6 2551 return false;
6de9cd9a
DN
2552 }
2553
c3f34952
TB
2554 if (intr)
2555 {
524af0d6
JB
2556 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2557 NULL, false))
2558 return false;
c3f34952
TB
2559 return resolve_structure_cons (expr, 0);
2560 }
2561
6de9cd9a
DN
2562 m = gfc_intrinsic_func_interface (expr, 0);
2563 if (m == MATCH_YES)
524af0d6 2564 return true;
c3f34952 2565
6de9cd9a 2566 if (m == MATCH_NO)
edf1eac2
SK
2567 gfc_error ("Generic function '%s' at %L is not consistent with a "
2568 "specific intrinsic interface", expr->symtree->n.sym->name,
2569 &expr->where);
6de9cd9a 2570
524af0d6 2571 return false;
6de9cd9a
DN
2572}
2573
2574
2575/* Resolve a function call known to be specific. */
2576
2577static match
edf1eac2 2578resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
6de9cd9a
DN
2579{
2580 match m;
2581
2582 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2583 {
2584 if (sym->attr.dummy)
2585 {
2586 sym->attr.proc = PROC_DUMMY;
2587 goto found;
2588 }
2589
2590 sym->attr.proc = PROC_EXTERNAL;
2591 goto found;
2592 }
2593
2594 if (sym->attr.proc == PROC_MODULE
2595 || sym->attr.proc == PROC_ST_FUNCTION
2596 || sym->attr.proc == PROC_INTERNAL)
2597 goto found;
2598
2599 if (sym->attr.intrinsic)
2600 {
2601 m = gfc_intrinsic_func_interface (expr, 1);
2602 if (m == MATCH_YES)
2603 return MATCH_YES;
2604 if (m == MATCH_NO)
edf1eac2
SK
2605 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2606 "with an intrinsic", sym->name, &expr->where);
6de9cd9a
DN
2607
2608 return MATCH_ERROR;
2609 }
2610
2611 return MATCH_NO;
2612
2613found:
2614 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2615
a7c0b11d
JW
2616 if (sym->result)
2617 expr->ts = sym->result->ts;
2618 else
2619 expr->ts = sym->ts;
6de9cd9a
DN
2620 expr->value.function.name = sym->name;
2621 expr->value.function.esym = sym;
36ad06d2
JW
2622 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2623 expr->rank = CLASS_DATA (sym)->as->rank;
2624 else if (sym->as != NULL)
6de9cd9a
DN
2625 expr->rank = sym->as->rank;
2626
2627 return MATCH_YES;
2628}
2629
2630
524af0d6 2631static bool
edf1eac2 2632resolve_specific_f (gfc_expr *expr)
6de9cd9a
DN
2633{
2634 gfc_symbol *sym;
2635 match m;
2636
2637 sym = expr->symtree->n.sym;
2638
2639 for (;;)
2640 {
2641 m = resolve_specific_f0 (sym, expr);
2642 if (m == MATCH_YES)
524af0d6 2643 return true;
6de9cd9a 2644 if (m == MATCH_ERROR)
524af0d6 2645 return false;
6de9cd9a
DN
2646
2647 if (sym->ns->parent == NULL)
2648 break;
2649
2650 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2651
2652 if (sym == NULL)
2653 break;
2654 }
2655
2656 gfc_error ("Unable to resolve the specific function '%s' at %L",
2657 expr->symtree->n.sym->name, &expr->where);
2658
524af0d6 2659 return true;
6de9cd9a
DN
2660}
2661
2662
2663/* Resolve a procedure call not known to be generic nor specific. */
2664
524af0d6 2665static bool
edf1eac2 2666resolve_unknown_f (gfc_expr *expr)
6de9cd9a
DN
2667{
2668 gfc_symbol *sym;
2669 gfc_typespec *ts;
2670
2671 sym = expr->symtree->n.sym;
2672
2673 if (sym->attr.dummy)
2674 {
2675 sym->attr.proc = PROC_DUMMY;
2676 expr->value.function.name = sym->name;
2677 goto set_type;
2678 }
2679
2680 /* See if we have an intrinsic function reference. */
2681
c3005b0f 2682 if (gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a
DN
2683 {
2684 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
524af0d6
JB
2685 return true;
2686 return false;
6de9cd9a
DN
2687 }
2688
2689 /* The reference is to an external name. */
2690
2691 sym->attr.proc = PROC_EXTERNAL;
2692 expr->value.function.name = sym->name;
2693 expr->value.function.esym = expr->symtree->n.sym;
2694
2695 if (sym->as != NULL)
2696 expr->rank = sym->as->rank;
2697
2698 /* Type of the expression is either the type of the symbol or the
2699 default type of the symbol. */
2700
2701set_type:
2702 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2703
2704 if (sym->ts.type != BT_UNKNOWN)
2705 expr->ts = sym->ts;
2706 else
2707 {
713485cc 2708 ts = gfc_get_default_type (sym->name, sym->ns);
6de9cd9a
DN
2709
2710 if (ts->type == BT_UNKNOWN)
2711 {
cf4d246b 2712 gfc_error ("Function '%s' at %L has no IMPLICIT type",
6de9cd9a 2713 sym->name, &expr->where);
524af0d6 2714 return false;
6de9cd9a
DN
2715 }
2716 else
2717 expr->ts = *ts;
2718 }
2719
524af0d6 2720 return true;
6de9cd9a
DN
2721}
2722
2723
e7c8ff56
PT
2724/* Return true, if the symbol is an external procedure. */
2725static bool
2726is_external_proc (gfc_symbol *sym)
2727{
2728 if (!sym->attr.dummy && !sym->attr.contained
0e8d854e 2729 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
e7c8ff56 2730 && sym->attr.proc != PROC_ST_FUNCTION
68d8db77 2731 && !sym->attr.proc_pointer
e7c8ff56
PT
2732 && !sym->attr.use_assoc
2733 && sym->name)
2734 return true;
c3005b0f
DK
2735
2736 return false;
e7c8ff56
PT
2737}
2738
2739
2054fc29
VR
2740/* Figure out if a function reference is pure or not. Also set the name
2741 of the function for a potential error message. Return nonzero if the
6de9cd9a 2742 function is PURE, zero if not. */
908a2235
PT
2743static int
2744pure_stmt_function (gfc_expr *, gfc_symbol *);
6de9cd9a
DN
2745
2746static int
edf1eac2 2747pure_function (gfc_expr *e, const char **name)
6de9cd9a
DN
2748{
2749 int pure;
2750
36f7dcae
PT
2751 *name = NULL;
2752
9ebe2d22
PT
2753 if (e->symtree != NULL
2754 && e->symtree->n.sym != NULL
2755 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
908a2235 2756 return pure_stmt_function (e, e->symtree->n.sym);
9ebe2d22 2757
6de9cd9a
DN
2758 if (e->value.function.esym)
2759 {
2760 pure = gfc_pure (e->value.function.esym);
2761 *name = e->value.function.esym->name;
2762 }
2763 else if (e->value.function.isym)
2764 {
2765 pure = e->value.function.isym->pure
edf1eac2 2766 || e->value.function.isym->elemental;
6de9cd9a
DN
2767 *name = e->value.function.isym->name;
2768 }
2769 else
2770 {
2771 /* Implicit functions are not pure. */
2772 pure = 0;
2773 *name = e->value.function.name;
2774 }
2775
2776 return pure;
2777}
2778
2779
908a2235
PT
2780static bool
2781impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2782 int *f ATTRIBUTE_UNUSED)
2783{
2784 const char *name;
2785
2786 /* Don't bother recursing into other statement functions
2787 since they will be checked individually for purity. */
2788 if (e->expr_type != EXPR_FUNCTION
2789 || !e->symtree
2790 || e->symtree->n.sym == sym
2791 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2792 return false;
2793
2794 return pure_function (e, &name) ? false : true;
2795}
2796
2797
2798static int
2799pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2800{
2801 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2802}
2803
2804
6de9cd9a
DN
2805/* Resolve a function call, which means resolving the arguments, then figuring
2806 out which entity the name refers to. */
6de9cd9a 2807
524af0d6 2808static bool
edf1eac2 2809resolve_function (gfc_expr *expr)
6de9cd9a
DN
2810{
2811 gfc_actual_arglist *arg;
edf1eac2 2812 gfc_symbol *sym;
6b25a558 2813 const char *name;
524af0d6 2814 bool t;
48474141 2815 int temp;
7fcafa71 2816 procedure_type p = PROC_INTRINSIC;
0b4e2af7 2817 bool no_formal_args;
48474141 2818
20236f90
PT
2819 sym = NULL;
2820 if (expr->symtree)
2821 sym = expr->symtree->n.sym;
2822
6c036626 2823 /* If this is a procedure pointer component, it has already been resolved. */
2a573572 2824 if (gfc_is_proc_ptr_comp (expr))
524af0d6 2825 return true;
2a573572 2826
2c68bc89 2827 if (sym && sym->attr.intrinsic
524af0d6
JB
2828 && !gfc_resolve_intrinsic (sym, &expr->where))
2829 return false;
2c68bc89 2830
726d8566 2831 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
20a037d5 2832 {
edf1eac2 2833 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
524af0d6 2834 return false;
20a037d5
PT
2835 }
2836
8bae6273 2837 /* If this ia a deferred TBP with an abstract interface (which may
b3d286ba
JW
2838 of course be referenced), expr->value.function.esym will be set. */
2839 if (sym && sym->attr.abstract && !expr->value.function.esym)
9e1d712c
TB
2840 {
2841 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2842 sym->name, &expr->where);
524af0d6 2843 return false;
9e1d712c
TB
2844 }
2845
48474141
PT
2846 /* Switch off assumed size checking and do this again for certain kinds
2847 of procedure, once the procedure itself is resolved. */
2848 need_full_assumed_size++;
6de9cd9a 2849
7fcafa71
PT
2850 if (expr->symtree && expr->symtree->n.sym)
2851 p = expr->symtree->n.sym->attr.proc;
2852
d3a9eea2
TB
2853 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2854 inquiry_argument = true;
4cbc9039
JW
2855 no_formal_args = sym && is_external_proc (sym)
2856 && gfc_sym_get_dummy_args (sym) == NULL;
d3a9eea2 2857
524af0d6
JB
2858 if (!resolve_actual_arglist (expr->value.function.actual,
2859 p, no_formal_args))
d3a9eea2
TB
2860 {
2861 inquiry_argument = false;
524af0d6 2862 return false;
d3a9eea2 2863 }
6de9cd9a 2864
d3a9eea2 2865 inquiry_argument = false;
4d382327 2866
a8b3b0b6 2867 /* Resume assumed_size checking. */
48474141
PT
2868 need_full_assumed_size--;
2869
71a7778c
PT
2870 /* If the procedure is external, check for usage. */
2871 if (sym && is_external_proc (sym))
2872 resolve_global_procedure (sym, &expr->where,
2873 &expr->value.function.actual, 0);
2874
20236f90 2875 if (sym && sym->ts.type == BT_CHARACTER
bc21d315
JW
2876 && sym->ts.u.cl
2877 && sym->ts.u.cl->length == NULL
edf1eac2 2878 && !sym->attr.dummy
8d51f26f 2879 && !sym->ts.deferred
edf1eac2
SK
2880 && expr->value.function.esym == NULL
2881 && !sym->attr.contained)
20236f90 2882 {
20236f90 2883 /* Internal procedures are taken care of in resolve_contained_fntype. */
0e3e65bc
PT
2884 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2885 "be used at %L since it is not a dummy argument",
2886 sym->name, &expr->where);
524af0d6 2887 return false;
20236f90
PT
2888 }
2889
edf1eac2 2890 /* See if function is already resolved. */
6de9cd9a
DN
2891
2892 if (expr->value.function.name != NULL)
2893 {
2894 if (expr->ts.type == BT_UNKNOWN)
20236f90 2895 expr->ts = sym->ts;
524af0d6 2896 t = true;
6de9cd9a
DN
2897 }
2898 else
2899 {
2900 /* Apply the rules of section 14.1.2. */
2901
20236f90 2902 switch (procedure_kind (sym))
6de9cd9a
DN
2903 {
2904 case PTYPE_GENERIC:
2905 t = resolve_generic_f (expr);
2906 break;
2907
2908 case PTYPE_SPECIFIC:
2909 t = resolve_specific_f (expr);
2910 break;
2911
2912 case PTYPE_UNKNOWN:
2913 t = resolve_unknown_f (expr);
2914 break;
2915
2916 default:
2917 gfc_internal_error ("resolve_function(): bad function type");
2918 }
2919 }
2920
2921 /* If the expression is still a function (it might have simplified),
2922 then we check to see if we are calling an elemental function. */
2923
2924 if (expr->expr_type != EXPR_FUNCTION)
2925 return t;
2926
48474141
PT
2927 temp = need_full_assumed_size;
2928 need_full_assumed_size = 0;
2929
524af0d6
JB
2930 if (!resolve_elemental_actual (expr, NULL))
2931 return false;
48474141 2932
6c7a4dfd
JJ
2933 if (omp_workshare_flag
2934 && expr->value.function.esym
2935 && ! gfc_elemental (expr->value.function.esym))
2936 {
edf1eac2
SK
2937 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2938 "in WORKSHARE construct", expr->value.function.esym->name,
6c7a4dfd 2939 &expr->where);
524af0d6 2940 t = false;
6c7a4dfd 2941 }
6de9cd9a 2942
cd5ecab6 2943#define GENERIC_ID expr->value.function.isym->id
48474141 2944 else if (expr->value.function.actual != NULL
edf1eac2
SK
2945 && expr->value.function.isym != NULL
2946 && GENERIC_ID != GFC_ISYM_LBOUND
2947 && GENERIC_ID != GFC_ISYM_LEN
2948 && GENERIC_ID != GFC_ISYM_LOC
cadddfdd 2949 && GENERIC_ID != GFC_ISYM_C_LOC
edf1eac2 2950 && GENERIC_ID != GFC_ISYM_PRESENT)
48474141 2951 {
fa951694 2952 /* Array intrinsics must also have the last upper bound of an
b82feea5 2953 assumed size array argument. UBOUND and SIZE have to be
48474141
PT
2954 excluded from the check if the second argument is anything
2955 than a constant. */
05c1e3a7 2956
48474141
PT
2957 for (arg = expr->value.function.actual; arg; arg = arg->next)
2958 {
7a687b22 2959 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
1634e53f 2960 && arg == expr->value.function.actual
7a687b22 2961 && arg->next != NULL && arg->next->expr)
9ebe2d22
PT
2962 {
2963 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2964 break;
2965
524af0d6 2966 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
7a687b22
TB
2967 break;
2968
9ebe2d22
PT
2969 if ((int)mpz_get_si (arg->next->expr->value.integer)
2970 < arg->expr->rank)
2971 break;
2972 }
05c1e3a7 2973
48474141 2974 if (arg->expr != NULL
edf1eac2
SK
2975 && arg->expr->rank > 0
2976 && resolve_assumed_size_actual (arg->expr))
524af0d6 2977 return false;
48474141
PT
2978 }
2979 }
4d4074e4 2980#undef GENERIC_ID
48474141
PT
2981
2982 need_full_assumed_size = temp;
36f7dcae 2983 name = NULL;
48474141 2984
5f20c93a 2985 if (!pure_function (expr, &name) && name)
6de9cd9a
DN
2986 {
2987 if (forall_flag)
2988 {
8c6a85e3 2989 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
edf1eac2
SK
2990 "FORALL %s", name, &expr->where,
2991 forall_flag == 2 ? "mask" : "block");
524af0d6 2992 t = false;
6de9cd9a 2993 }
ce96d372 2994 else if (gfc_do_concurrent_flag)
8c6a85e3
TB
2995 {
2996 gfc_error ("Reference to non-PURE function '%s' at %L inside a "
2997 "DO CONCURRENT %s", name, &expr->where,
ce96d372 2998 gfc_do_concurrent_flag == 2 ? "mask" : "block");
524af0d6 2999 t = false;
8c6a85e3 3000 }
6de9cd9a
DN
3001 else if (gfc_pure (NULL))
3002 {
3003 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
3004 "procedure within a PURE procedure", name, &expr->where);
524af0d6 3005 t = false;
6de9cd9a 3006 }
6de9cd9a 3007
3d2cea8c
TB
3008 if (gfc_implicit_pure (NULL))
3009 gfc_current_ns->proc_name->attr.implicit_pure = 0;
3010 }
f1f39033 3011
77f131ca
FXC
3012 /* Functions without the RECURSIVE attribution are not allowed to
3013 * call themselves. */
3014 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3015 {
1933ba0f 3016 gfc_symbol *esym;
77f131ca 3017 esym = expr->value.function.esym;
77f131ca 3018
1933ba0f 3019 if (is_illegal_recursion (esym, gfc_current_ns))
77f131ca 3020 {
1933ba0f
DK
3021 if (esym->attr.entry && esym->ns->entries)
3022 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3023 " function '%s' is not RECURSIVE",
3024 esym->name, &expr->where, esym->ns->entries->sym->name);
3025 else
3026 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
3027 " is not RECURSIVE", esym->name, &expr->where);
3028
524af0d6 3029 t = false;
77f131ca
FXC
3030 }
3031 }
3032
47992a4a
EE
3033 /* Character lengths of use associated functions may contains references to
3034 symbols not referenced from the current program unit otherwise. Make sure
3035 those symbols are marked as referenced. */
3036
05c1e3a7 3037 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
47992a4a
EE
3038 && expr->value.function.esym->attr.use_assoc)
3039 {
bc21d315 3040 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
47992a4a
EE
3041 }
3042
9ebe2d22
PT
3043 /* Make sure that the expression has a typespec that works. */
3044 if (expr->ts.type == BT_UNKNOWN)
3045 {
3046 if (expr->symtree->n.sym->result
3070bab4
JW
3047 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3048 && !expr->symtree->n.sym->result->attr.proc_pointer)
9ebe2d22 3049 expr->ts = expr->symtree->n.sym->result->ts;
9ebe2d22
PT
3050 }
3051
6de9cd9a
DN
3052 return t;
3053}
3054
3055
3056/************* Subroutine resolution *************/
3057
3058static void
edf1eac2 3059pure_subroutine (gfc_code *c, gfc_symbol *sym)
6de9cd9a 3060{
6de9cd9a
DN
3061 if (gfc_pure (sym))
3062 return;
3063
3064 if (forall_flag)
3065 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
3066 sym->name, &c->loc);
ce96d372 3067 else if (gfc_do_concurrent_flag)
8c6a85e3
TB
3068 gfc_error ("Subroutine call to '%s' in DO CONCURRENT block at %L is not "
3069 "PURE", sym->name, &c->loc);
6de9cd9a
DN
3070 else if (gfc_pure (NULL))
3071 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
3072 &c->loc);
3d2cea8c
TB
3073
3074 if (gfc_implicit_pure (NULL))
3075 gfc_current_ns->proc_name->attr.implicit_pure = 0;
6de9cd9a
DN
3076}
3077
3078
3079static match
edf1eac2 3080resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
3081{
3082 gfc_symbol *s;
3083
3084 if (sym->attr.generic)
3085 {
3086 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3087 if (s != NULL)
3088 {
edf1eac2 3089 c->resolved_sym = s;
6de9cd9a
DN
3090 pure_subroutine (c, s);
3091 return MATCH_YES;
3092 }
3093
3094 /* TODO: Need to search for elemental references in generic interface. */
3095 }
3096
3097 if (sym->attr.intrinsic)
3098 return gfc_intrinsic_sub_interface (c, 0);
3099
3100 return MATCH_NO;
3101}
3102
3103
524af0d6 3104static bool
edf1eac2 3105resolve_generic_s (gfc_code *c)
6de9cd9a
DN
3106{
3107 gfc_symbol *sym;
3108 match m;
3109
3110 sym = c->symtree->n.sym;
3111
8c086c9c 3112 for (;;)
6de9cd9a 3113 {
8c086c9c
PT
3114 m = resolve_generic_s0 (c, sym);
3115 if (m == MATCH_YES)
524af0d6 3116 return true;
8c086c9c 3117 else if (m == MATCH_ERROR)
524af0d6 3118 return false;
8c086c9c
PT
3119
3120generic:
3121 if (sym->ns->parent == NULL)
3122 break;
6de9cd9a 3123 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
8c086c9c
PT
3124
3125 if (sym == NULL)
3126 break;
3127 if (!generic_sym (sym))
3128 goto generic;
6de9cd9a
DN
3129 }
3130
71f77fd7
PT
3131 /* Last ditch attempt. See if the reference is to an intrinsic
3132 that possesses a matching interface. 14.1.2.4 */
8c086c9c 3133 sym = c->symtree->n.sym;
71f77fd7 3134
c3005b0f 3135 if (!gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a 3136 {
edf1eac2
SK
3137 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
3138 sym->name, &c->loc);
524af0d6 3139 return false;
6de9cd9a
DN
3140 }
3141
3142 m = gfc_intrinsic_sub_interface (c, 0);
3143 if (m == MATCH_YES)
524af0d6 3144 return true;
6de9cd9a
DN
3145 if (m == MATCH_NO)
3146 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
3147 "intrinsic subroutine interface", sym->name, &c->loc);
3148
524af0d6 3149 return false;
6de9cd9a
DN
3150}
3151
3152
3153/* Resolve a subroutine call known to be specific. */
3154
3155static match
edf1eac2 3156resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
3157{
3158 match m;
3159
3160 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3161 {
3162 if (sym->attr.dummy)
3163 {
3164 sym->attr.proc = PROC_DUMMY;
3165 goto found;
3166 }
3167
3168 sym->attr.proc = PROC_EXTERNAL;
3169 goto found;
3170 }
3171
3172 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3173 goto found;
3174
3175 if (sym->attr.intrinsic)
3176 {
3177 m = gfc_intrinsic_sub_interface (c, 1);
3178 if (m == MATCH_YES)
3179 return MATCH_YES;
3180 if (m == MATCH_NO)
3181 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3182 "with an intrinsic", sym->name, &c->loc);
3183
3184 return MATCH_ERROR;
3185 }
3186
3187 return MATCH_NO;
3188
3189found:
3190 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3191
3192 c->resolved_sym = sym;
3193 pure_subroutine (c, sym);
3194
3195 return MATCH_YES;
3196}
3197
3198
524af0d6 3199static bool
edf1eac2 3200resolve_specific_s (gfc_code *c)
6de9cd9a
DN
3201{
3202 gfc_symbol *sym;
3203 match m;
3204
3205 sym = c->symtree->n.sym;
3206
8c086c9c 3207 for (;;)
6de9cd9a
DN
3208 {
3209 m = resolve_specific_s0 (c, sym);
3210 if (m == MATCH_YES)
524af0d6 3211 return true;
6de9cd9a 3212 if (m == MATCH_ERROR)
524af0d6 3213 return false;
8c086c9c
PT
3214
3215 if (sym->ns->parent == NULL)
3216 break;
3217
3218 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3219
3220 if (sym == NULL)
3221 break;
6de9cd9a
DN
3222 }
3223
8c086c9c 3224 sym = c->symtree->n.sym;
6de9cd9a
DN
3225 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3226 sym->name, &c->loc);
3227
524af0d6 3228 return false;
6de9cd9a
DN
3229}
3230
3231
3232/* Resolve a subroutine call not known to be generic nor specific. */
3233
524af0d6 3234static bool
edf1eac2 3235resolve_unknown_s (gfc_code *c)
6de9cd9a
DN
3236{
3237 gfc_symbol *sym;
3238
3239 sym = c->symtree->n.sym;
3240
3241 if (sym->attr.dummy)
3242 {
3243 sym->attr.proc = PROC_DUMMY;
3244 goto found;
3245 }
3246
3247 /* See if we have an intrinsic function reference. */
3248
c3005b0f 3249 if (gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a
DN
3250 {
3251 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
524af0d6
JB
3252 return true;
3253 return false;
6de9cd9a
DN
3254 }
3255
3256 /* The reference is to an external name. */
3257
3258found:
3259 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3260
3261 c->resolved_sym = sym;
3262
3263 pure_subroutine (c, sym);
3264
524af0d6 3265 return true;
6de9cd9a
DN
3266}
3267
3268
3269/* Resolve a subroutine call. Although it was tempting to use the same code
3270 for functions, subroutines and functions are stored differently and this
3271 makes things awkward. */
3272
524af0d6 3273static bool
edf1eac2 3274resolve_call (gfc_code *c)
6de9cd9a 3275{
524af0d6 3276 bool t;
7fcafa71 3277 procedure_type ptype = PROC_INTRINSIC;
67cec813 3278 gfc_symbol *csym, *sym;
0b4e2af7
PT
3279 bool no_formal_args;
3280
3281 csym = c->symtree ? c->symtree->n.sym : NULL;
6de9cd9a 3282
0b4e2af7 3283 if (csym && csym->ts.type != BT_UNKNOWN)
2ed8d224
PT
3284 {
3285 gfc_error ("'%s' at %L has a type, which is not consistent with "
0b4e2af7 3286 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
524af0d6 3287 return false;
2ed8d224
PT
3288 }
3289
67cec813
PT
3290 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3291 {
79b1d36c 3292 gfc_symtree *st;
d932cea8 3293 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
79b1d36c 3294 sym = st ? st->n.sym : NULL;
67cec813
PT
3295 if (sym && csym != sym
3296 && sym->ns == gfc_current_ns
3297 && sym->attr.flavor == FL_PROCEDURE
3298 && sym->attr.contained)
3299 {
3300 sym->refs++;
79b1d36c
PT
3301 if (csym->attr.generic)
3302 c->symtree->n.sym = sym;
3303 else
3304 c->symtree = st;
3305 csym = c->symtree->n.sym;
67cec813
PT
3306 }
3307 }
3308
fdb1fa9e
JW
3309 /* If this ia a deferred TBP, c->expr1 will be set. */
3310 if (!c->expr1 && csym)
8bae6273 3311 {
fdb1fa9e
JW
3312 if (csym->attr.abstract)
3313 {
3314 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3315 csym->name, &c->loc);
524af0d6 3316 return false;
fdb1fa9e 3317 }
8bae6273 3318
fdb1fa9e
JW
3319 /* Subroutines without the RECURSIVE attribution are not allowed to
3320 call themselves. */
3321 if (is_illegal_recursion (csym, gfc_current_ns))
3322 {
3323 if (csym->attr.entry && csym->ns->entries)
3324 gfc_error ("ENTRY '%s' at %L cannot be called recursively, "
3325 "as subroutine '%s' is not RECURSIVE",
3326 csym->name, &c->loc, csym->ns->entries->sym->name);
3327 else
3328 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, "
3329 "as it is not RECURSIVE", csym->name, &c->loc);
1933ba0f 3330
524af0d6 3331 t = false;
fdb1fa9e 3332 }
77f131ca
FXC
3333 }
3334
48474141
PT
3335 /* Switch off assumed size checking and do this again for certain kinds
3336 of procedure, once the procedure itself is resolved. */
3337 need_full_assumed_size++;
3338
0b4e2af7
PT
3339 if (csym)
3340 ptype = csym->attr.proc;
7fcafa71 3341
4cbc9039
JW
3342 no_formal_args = csym && is_external_proc (csym)
3343 && gfc_sym_get_dummy_args (csym) == NULL;
524af0d6
JB
3344 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3345 return false;
6de9cd9a 3346
66e4ab31 3347 /* Resume assumed_size checking. */
48474141
PT
3348 need_full_assumed_size--;
3349
71a7778c
PT
3350 /* If external, check for usage. */
3351 if (csym && is_external_proc (csym))
3352 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3353
524af0d6 3354 t = true;
1524f80b 3355 if (c->resolved_sym == NULL)
12f681a0
DK
3356 {
3357 c->resolved_isym = NULL;
3358 switch (procedure_kind (csym))
3359 {
3360 case PTYPE_GENERIC:
3361 t = resolve_generic_s (c);
3362 break;
6de9cd9a 3363
12f681a0
DK
3364 case PTYPE_SPECIFIC:
3365 t = resolve_specific_s (c);
3366 break;
6de9cd9a 3367
12f681a0
DK
3368 case PTYPE_UNKNOWN:
3369 t = resolve_unknown_s (c);
3370 break;
6de9cd9a 3371
12f681a0
DK
3372 default:
3373 gfc_internal_error ("resolve_subroutine(): bad function type");
3374 }
3375 }
6de9cd9a 3376
b8ea6dbc 3377 /* Some checks of elemental subroutine actual arguments. */
524af0d6
JB
3378 if (!resolve_elemental_actual (NULL, c))
3379 return false;
48474141 3380
6de9cd9a
DN
3381 return t;
3382}
3383
edf1eac2 3384
2c5ed587 3385/* Compare the shapes of two arrays that have non-NULL shapes. If both
524af0d6
JB
3386 op1->shape and op2->shape are non-NULL return true if their shapes
3387 match. If both op1->shape and op2->shape are non-NULL return false
2c5ed587 3388 if their shapes do not match. If either op1->shape or op2->shape is
524af0d6 3389 NULL, return true. */
2c5ed587 3390
524af0d6 3391static bool
edf1eac2 3392compare_shapes (gfc_expr *op1, gfc_expr *op2)
2c5ed587 3393{
524af0d6 3394 bool t;
2c5ed587
SK
3395 int i;
3396
524af0d6 3397 t = true;
05c1e3a7 3398
2c5ed587
SK
3399 if (op1->shape != NULL && op2->shape != NULL)
3400 {
3401 for (i = 0; i < op1->rank; i++)
3402 {
3403 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3404 {
3405 gfc_error ("Shapes for operands at %L and %L are not conformable",
3406 &op1->where, &op2->where);
524af0d6 3407 t = false;
2c5ed587
SK
3408 break;
3409 }
3410 }
3411 }
3412
3413 return t;
3414}
6de9cd9a 3415
edf1eac2 3416
6de9cd9a
DN
3417/* Resolve an operator expression node. This can involve replacing the
3418 operation with a user defined function call. */
3419
524af0d6 3420static bool
edf1eac2 3421resolve_operator (gfc_expr *e)
6de9cd9a
DN
3422{
3423 gfc_expr *op1, *op2;
3424 char msg[200];
27189292 3425 bool dual_locus_error;
524af0d6 3426 bool t;
6de9cd9a
DN
3427
3428 /* Resolve all subnodes-- give them types. */
3429
a1ee985f 3430 switch (e->value.op.op)
6de9cd9a
DN
3431 {
3432 default:
524af0d6
JB
3433 if (!gfc_resolve_expr (e->value.op.op2))
3434 return false;
6de9cd9a
DN
3435
3436 /* Fall through... */
3437
3438 case INTRINSIC_NOT:
3439 case INTRINSIC_UPLUS:
3440 case INTRINSIC_UMINUS:
2414e1d6 3441 case INTRINSIC_PARENTHESES:
524af0d6
JB
3442 if (!gfc_resolve_expr (e->value.op.op1))
3443 return false;
6de9cd9a
DN
3444 break;
3445 }
3446
3447 /* Typecheck the new node. */
3448
58b03ab2
TS
3449 op1 = e->value.op.op1;
3450 op2 = e->value.op.op2;
27189292 3451 dual_locus_error = false;
6de9cd9a 3452
bb9e683e
TB
3453 if ((op1 && op1->expr_type == EXPR_NULL)
3454 || (op2 && op2->expr_type == EXPR_NULL))
3455 {
3456 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3457 goto bad_op;
3458 }
3459
a1ee985f 3460 switch (e->value.op.op)
6de9cd9a
DN
3461 {
3462 case INTRINSIC_UPLUS:
3463 case INTRINSIC_UMINUS:
3464 if (op1->ts.type == BT_INTEGER
3465 || op1->ts.type == BT_REAL
3466 || op1->ts.type == BT_COMPLEX)
3467 {
3468 e->ts = op1->ts;
3469 break;
3470 }
3471
31043f6c 3472 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
a1ee985f 3473 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
6de9cd9a
DN
3474 goto bad_op;
3475
3476 case INTRINSIC_PLUS:
3477 case INTRINSIC_MINUS:
3478 case INTRINSIC_TIMES:
3479 case INTRINSIC_DIVIDE:
3480 case INTRINSIC_POWER:
3481 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3482 {
dcea1b2f 3483 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3484 break;
3485 }
3486
3487 sprintf (msg,
31043f6c 3488 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
a1ee985f 3489 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3490 gfc_typename (&op2->ts));
3491 goto bad_op;
3492
3493 case INTRINSIC_CONCAT:
d393bbd7
FXC
3494 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3495 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3496 {
3497 e->ts.type = BT_CHARACTER;
3498 e->ts.kind = op1->ts.kind;
3499 break;
3500 }
3501
3502 sprintf (msg,
31043f6c 3503 _("Operands of string concatenation operator at %%L are %s/%s"),
6de9cd9a
DN
3504 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3505 goto bad_op;
3506
3507 case INTRINSIC_AND:
3508 case INTRINSIC_OR:
3509 case INTRINSIC_EQV:
3510 case INTRINSIC_NEQV:
3511 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3512 {
3513 e->ts.type = BT_LOGICAL;
3514 e->ts.kind = gfc_kind_max (op1, op2);
edf1eac2
SK
3515 if (op1->ts.kind < e->ts.kind)
3516 gfc_convert_type (op1, &e->ts, 2);
3517 else if (op2->ts.kind < e->ts.kind)
3518 gfc_convert_type (op2, &e->ts, 2);
6de9cd9a
DN
3519 break;
3520 }
3521
31043f6c 3522 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
a1ee985f 3523 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3524 gfc_typename (&op2->ts));
3525
3526 goto bad_op;
3527
3528 case INTRINSIC_NOT:
3529 if (op1->ts.type == BT_LOGICAL)
3530 {
3531 e->ts.type = BT_LOGICAL;
3532 e->ts.kind = op1->ts.kind;
3533 break;
3534 }
3535
3bed9dd0 3536 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
6de9cd9a
DN
3537 gfc_typename (&op1->ts));
3538 goto bad_op;
3539
3540 case INTRINSIC_GT:
3bed9dd0 3541 case INTRINSIC_GT_OS:
6de9cd9a 3542 case INTRINSIC_GE:
3bed9dd0 3543 case INTRINSIC_GE_OS:
6de9cd9a 3544 case INTRINSIC_LT:
3bed9dd0 3545 case INTRINSIC_LT_OS:
6de9cd9a 3546 case INTRINSIC_LE:
3bed9dd0 3547 case INTRINSIC_LE_OS:
6de9cd9a
DN
3548 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3549 {
31043f6c 3550 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
6de9cd9a
DN
3551 goto bad_op;
3552 }
3553
3554 /* Fall through... */
3555
3556 case INTRINSIC_EQ:
3bed9dd0 3557 case INTRINSIC_EQ_OS:
6de9cd9a 3558 case INTRINSIC_NE:
3bed9dd0 3559 case INTRINSIC_NE_OS:
d393bbd7
FXC
3560 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3561 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3562 {
3563 e->ts.type = BT_LOGICAL;
9d64df18 3564 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3565 break;
3566 }
3567
3568 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3569 {
dcea1b2f 3570 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3571
3572 e->ts.type = BT_LOGICAL;
9d64df18 3573 e->ts.kind = gfc_default_logical_kind;
cf21551e
TK
3574
3575 if (gfc_option.warn_compare_reals)
3576 {
3577 gfc_intrinsic_op op = e->value.op.op;
3578
3579 /* Type conversion has made sure that the types of op1 and op2
3580 agree, so it is only necessary to check the first one. */
3581 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3582 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3583 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3584 {
3585 const char *msg;
3586
3587 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3588 msg = "Equality comparison for %s at %L";
3589 else
3590 msg = "Inequality comparison for %s at %L";
4d382327 3591
cf21551e
TK
3592 gfc_warning (msg, gfc_typename (&op1->ts), &op1->where);
3593 }
3594 }
3595
6de9cd9a
DN
3596 break;
3597 }
3598
6a28f513 3599 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
31043f6c 3600 sprintf (msg,
edf1eac2 3601 _("Logicals at %%L must be compared with %s instead of %s"),
4d382327 3602 (e->value.op.op == INTRINSIC_EQ
a1ee985f
KG
3603 || e->value.op.op == INTRINSIC_EQ_OS)
3604 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
6a28f513 3605 else
31043f6c 3606 sprintf (msg,
edf1eac2 3607 _("Operands of comparison operator '%s' at %%L are %s/%s"),
a1ee985f 3608 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6a28f513 3609 gfc_typename (&op2->ts));
6de9cd9a
DN
3610
3611 goto bad_op;
3612
3613 case INTRINSIC_USER:
a1ee985f 3614 if (e->value.op.uop->op == NULL)
622af87f
DF
3615 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3616 else if (op2 == NULL)
31043f6c 3617 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
58b03ab2 3618 e->value.op.uop->name, gfc_typename (&op1->ts));
6de9cd9a 3619 else
7c1a49fa
TK
3620 {
3621 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
3622 e->value.op.uop->name, gfc_typename (&op1->ts),
3623 gfc_typename (&op2->ts));
3624 e->value.op.uop->op->sym->attr.referenced = 1;
3625 }
6de9cd9a
DN
3626
3627 goto bad_op;
3628
2414e1d6 3629 case INTRINSIC_PARENTHESES:
dcdc83a1
TS
3630 e->ts = op1->ts;
3631 if (e->ts.type == BT_CHARACTER)
bc21d315 3632 e->ts.u.cl = op1->ts.u.cl;
2414e1d6
TS
3633 break;
3634
6de9cd9a
DN
3635 default:
3636 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3637 }
3638
3639 /* Deal with arrayness of an operand through an operator. */
3640
524af0d6 3641 t = true;
6de9cd9a 3642
a1ee985f 3643 switch (e->value.op.op)
6de9cd9a
DN
3644 {
3645 case INTRINSIC_PLUS:
3646 case INTRINSIC_MINUS:
3647 case INTRINSIC_TIMES:
3648 case INTRINSIC_DIVIDE:
3649 case INTRINSIC_POWER:
3650 case INTRINSIC_CONCAT:
3651 case INTRINSIC_AND:
3652 case INTRINSIC_OR:
3653 case INTRINSIC_EQV:
3654 case INTRINSIC_NEQV:
3655 case INTRINSIC_EQ:
3bed9dd0 3656 case INTRINSIC_EQ_OS:
6de9cd9a 3657 case INTRINSIC_NE:
3bed9dd0 3658 case INTRINSIC_NE_OS:
6de9cd9a 3659 case INTRINSIC_GT:
3bed9dd0 3660 case INTRINSIC_GT_OS:
6de9cd9a 3661 case INTRINSIC_GE:
3bed9dd0 3662 case INTRINSIC_GE_OS:
6de9cd9a 3663 case INTRINSIC_LT:
3bed9dd0 3664 case INTRINSIC_LT_OS:
6de9cd9a 3665 case INTRINSIC_LE:
3bed9dd0 3666 case INTRINSIC_LE_OS:
6de9cd9a
DN
3667
3668 if (op1->rank == 0 && op2->rank == 0)
3669 e->rank = 0;
3670
3671 if (op1->rank == 0 && op2->rank != 0)
3672 {
3673 e->rank = op2->rank;
3674
3675 if (e->shape == NULL)
3676 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3677 }
3678
3679 if (op1->rank != 0 && op2->rank == 0)
3680 {
3681 e->rank = op1->rank;
3682
3683 if (e->shape == NULL)
3684 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3685 }
3686
3687 if (op1->rank != 0 && op2->rank != 0)
3688 {
3689 if (op1->rank == op2->rank)
3690 {
3691 e->rank = op1->rank;
6de9cd9a 3692 if (e->shape == NULL)
2c5ed587 3693 {
d1d7b044 3694 t = compare_shapes (op1, op2);
524af0d6 3695 if (!t)
2c5ed587
SK
3696 e->shape = NULL;
3697 else
d1d7b044 3698 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2c5ed587 3699 }
6de9cd9a
DN
3700 }
3701 else
3702 {
edf1eac2 3703 /* Allow higher level expressions to work. */
6de9cd9a 3704 e->rank = 0;
27189292
FXC
3705
3706 /* Try user-defined operators, and otherwise throw an error. */
3707 dual_locus_error = true;
3708 sprintf (msg,
3709 _("Inconsistent ranks for operator at %%L and %%L"));
3710 goto bad_op;
6de9cd9a
DN
3711 }
3712 }
3713
3714 break;
3715
08113c73 3716 case INTRINSIC_PARENTHESES:
6de9cd9a
DN
3717 case INTRINSIC_NOT:
3718 case INTRINSIC_UPLUS:
3719 case INTRINSIC_UMINUS:
08113c73 3720 /* Simply copy arrayness attribute */
6de9cd9a
DN
3721 e->rank = op1->rank;
3722
3723 if (e->shape == NULL)
3724 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3725
6de9cd9a
DN
3726 break;
3727
3728 default:
3729 break;
3730 }
3731
3732 /* Attempt to simplify the expression. */
524af0d6 3733 if (t)
dd5ecf41
PT
3734 {
3735 t = gfc_simplify_expr (e, 0);
524af0d6 3736 /* Some calls do not succeed in simplification and return false
df2fba9e 3737 even though there is no error; e.g. variable references to
dd5ecf41
PT
3738 PARAMETER arrays. */
3739 if (!gfc_is_constant_expr (e))
524af0d6 3740 t = true;
dd5ecf41 3741 }
6de9cd9a
DN
3742 return t;
3743
3744bad_op:
2c5ed587 3745
4a44a72d 3746 {
eaee02a5
JW
3747 match m = gfc_extend_expr (e);
3748 if (m == MATCH_YES)
524af0d6 3749 return true;
eaee02a5 3750 if (m == MATCH_ERROR)
524af0d6 3751 return false;
4a44a72d 3752 }
6de9cd9a 3753
27189292
FXC
3754 if (dual_locus_error)
3755 gfc_error (msg, &op1->where, &op2->where);
3756 else
3757 gfc_error (msg, &e->where);
2c5ed587 3758
524af0d6 3759 return false;
6de9cd9a
DN
3760}
3761
3762
3763/************** Array resolution subroutines **************/
3764
6de9cd9a
DN
3765typedef enum
3766{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3767comparison;
3768
3769/* Compare two integer expressions. */
3770
3771static comparison
edf1eac2 3772compare_bound (gfc_expr *a, gfc_expr *b)
6de9cd9a
DN
3773{
3774 int i;
3775
3776 if (a == NULL || a->expr_type != EXPR_CONSTANT
3777 || b == NULL || b->expr_type != EXPR_CONSTANT)
3778 return CMP_UNKNOWN;
3779
df80a455
TK
3780 /* If either of the types isn't INTEGER, we must have
3781 raised an error earlier. */
3782
6de9cd9a 3783 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
df80a455 3784 return CMP_UNKNOWN;
6de9cd9a
DN
3785
3786 i = mpz_cmp (a->value.integer, b->value.integer);
3787
3788 if (i < 0)
3789 return CMP_LT;
3790 if (i > 0)
3791 return CMP_GT;
3792 return CMP_EQ;
3793}
3794
3795
3796/* Compare an integer expression with an integer. */
3797
3798static comparison
edf1eac2 3799compare_bound_int (gfc_expr *a, int b)
6de9cd9a
DN
3800{
3801 int i;
3802
3803 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3804 return CMP_UNKNOWN;
3805
3806 if (a->ts.type != BT_INTEGER)
3807 gfc_internal_error ("compare_bound_int(): Bad expression");
3808
3809 i = mpz_cmp_si (a->value.integer, b);
3810
3811 if (i < 0)
3812 return CMP_LT;
3813 if (i > 0)
3814 return CMP_GT;
3815 return CMP_EQ;
3816}
3817
3818
0094f362
FXC
3819/* Compare an integer expression with a mpz_t. */
3820
3821static comparison
edf1eac2 3822compare_bound_mpz_t (gfc_expr *a, mpz_t b)
0094f362
FXC
3823{
3824 int i;
3825
3826 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3827 return CMP_UNKNOWN;
3828
3829 if (a->ts.type != BT_INTEGER)
3830 gfc_internal_error ("compare_bound_int(): Bad expression");
3831
3832 i = mpz_cmp (a->value.integer, b);
3833
3834 if (i < 0)
3835 return CMP_LT;
3836 if (i > 0)
3837 return CMP_GT;
3838 return CMP_EQ;
3839}
3840
3841
4d382327 3842/* Compute the last value of a sequence given by a triplet.
0094f362
FXC
3843 Return 0 if it wasn't able to compute the last value, or if the
3844 sequence if empty, and 1 otherwise. */
3845
3846static int
edf1eac2
SK
3847compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3848 gfc_expr *stride, mpz_t last)
0094f362
FXC
3849{
3850 mpz_t rem;
3851
3852 if (start == NULL || start->expr_type != EXPR_CONSTANT
3853 || end == NULL || end->expr_type != EXPR_CONSTANT
3854 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3855 return 0;
3856
3857 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3858 || (stride != NULL && stride->ts.type != BT_INTEGER))
3859 return 0;
3860
524af0d6 3861 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
0094f362
FXC
3862 {
3863 if (compare_bound (start, end) == CMP_GT)
3864 return 0;
3865 mpz_set (last, end->value.integer);
3866 return 1;
3867 }
05c1e3a7 3868
0094f362
FXC
3869 if (compare_bound_int (stride, 0) == CMP_GT)
3870 {
3871 /* Stride is positive */
3872 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3873 return 0;
3874 }
3875 else
3876 {
3877 /* Stride is negative */
3878 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3879 return 0;
3880 }
3881
3882 mpz_init (rem);
3883 mpz_sub (rem, end->value.integer, start->value.integer);
3884 mpz_tdiv_r (rem, rem, stride->value.integer);
3885 mpz_sub (last, end->value.integer, rem);
3886 mpz_clear (rem);
3887
3888 return 1;
3889}
3890
3891
6de9cd9a
DN
3892/* Compare a single dimension of an array reference to the array
3893 specification. */
3894
524af0d6 3895static bool
edf1eac2 3896check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
6de9cd9a 3897{
0094f362 3898 mpz_t last_value;
6de9cd9a 3899
d3a9eea2
TB
3900 if (ar->dimen_type[i] == DIMEN_STAR)
3901 {
3902 gcc_assert (ar->stride[i] == NULL);
3903 /* This implies [*] as [*:] and [*:3] are not possible. */
3904 if (ar->start[i] == NULL)
3905 {
3906 gcc_assert (ar->end[i] == NULL);
524af0d6 3907 return true;
d3a9eea2
TB
3908 }
3909 }
3910
6de9cd9a 3911/* Given start, end and stride values, calculate the minimum and
f7b529fa 3912 maximum referenced indexes. */
6de9cd9a 3913
1954a27b 3914 switch (ar->dimen_type[i])
6de9cd9a 3915 {
1954a27b 3916 case DIMEN_VECTOR:
a3935ffc 3917 case DIMEN_THIS_IMAGE:
6de9cd9a
DN
3918 break;
3919
d3a9eea2 3920 case DIMEN_STAR:
1954a27b 3921 case DIMEN_ELEMENT:
6de9cd9a 3922 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1954a27b 3923 {
d3a9eea2
TB
3924 if (i < as->rank)
3925 gfc_warning ("Array reference at %L is out of bounds "
3926 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3927 mpz_get_si (ar->start[i]->value.integer),
3928 mpz_get_si (as->lower[i]->value.integer), i+1);
3929 else
3930 gfc_warning ("Array reference at %L is out of bounds "
3931 "(%ld < %ld) in codimension %d", &ar->c_where[i],
3932 mpz_get_si (ar->start[i]->value.integer),
3933 mpz_get_si (as->lower[i]->value.integer),
3934 i + 1 - as->rank);
524af0d6 3935 return true;
1954a27b 3936 }
6de9cd9a 3937 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1954a27b 3938 {
d3a9eea2
TB
3939 if (i < as->rank)
3940 gfc_warning ("Array reference at %L is out of bounds "
3941 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3942 mpz_get_si (ar->start[i]->value.integer),
3943 mpz_get_si (as->upper[i]->value.integer), i+1);
3944 else
3945 gfc_warning ("Array reference at %L is out of bounds "
3946 "(%ld > %ld) in codimension %d", &ar->c_where[i],
3947 mpz_get_si (ar->start[i]->value.integer),
3948 mpz_get_si (as->upper[i]->value.integer),
3949 i + 1 - as->rank);
524af0d6 3950 return true;
1954a27b 3951 }
6de9cd9a
DN
3952
3953 break;
3954
1954a27b 3955 case DIMEN_RANGE:
d912240d 3956 {
0094f362
FXC
3957#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3958#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3959
d912240d 3960 comparison comp_start_end = compare_bound (AR_START, AR_END);
0094f362 3961
d912240d
FXC
3962 /* Check for zero stride, which is not allowed. */
3963 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3964 {
3965 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
524af0d6 3966 return false;
d912240d
FXC
3967 }
3968
3969 /* if start == len || (stride > 0 && start < len)
3970 || (stride < 0 && start > len),
3971 then the array section contains at least one element. In this
3972 case, there is an out-of-bounds access if
3973 (start < lower || start > upper). */
3974 if (compare_bound (AR_START, AR_END) == CMP_EQ
3975 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3976 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3977 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3978 && comp_start_end == CMP_GT))
3979 {
1954a27b
TB
3980 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3981 {
3982 gfc_warning ("Lower array reference at %L is out of bounds "
3983 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3984 mpz_get_si (AR_START->value.integer),
3985 mpz_get_si (as->lower[i]->value.integer), i+1);
524af0d6 3986 return true;
1954a27b
TB
3987 }
3988 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3989 {
3990 gfc_warning ("Lower array reference at %L is out of bounds "
3991 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3992 mpz_get_si (AR_START->value.integer),
3993 mpz_get_si (as->upper[i]->value.integer), i+1);
524af0d6 3994 return true;
1954a27b 3995 }
d912240d
FXC
3996 }
3997
3998 /* If we can compute the highest index of the array section,
3999 then it also has to be between lower and upper. */
4000 mpz_init (last_value);
4001 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4002 last_value))
4003 {
1954a27b
TB
4004 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4005 {
4006 gfc_warning ("Upper array reference at %L is out of bounds "
4007 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4008 mpz_get_si (last_value),
4009 mpz_get_si (as->lower[i]->value.integer), i+1);
4010 mpz_clear (last_value);
524af0d6 4011 return true;
1954a27b
TB
4012 }
4013 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
d912240d 4014 {
1954a27b
TB
4015 gfc_warning ("Upper array reference at %L is out of bounds "
4016 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4017 mpz_get_si (last_value),
4018 mpz_get_si (as->upper[i]->value.integer), i+1);
d912240d 4019 mpz_clear (last_value);
524af0d6 4020 return true;
d912240d
FXC
4021 }
4022 }
4023 mpz_clear (last_value);
0094f362
FXC
4024
4025#undef AR_START
4026#undef AR_END
d912240d 4027 }
6de9cd9a
DN
4028 break;
4029
4030 default:
4031 gfc_internal_error ("check_dimension(): Bad array reference");
4032 }
4033
524af0d6 4034 return true;
6de9cd9a
DN
4035}
4036
4037
4038/* Compare an array reference with an array specification. */
4039
524af0d6 4040static bool
edf1eac2 4041compare_spec_to_ref (gfc_array_ref *ar)
6de9cd9a
DN
4042{
4043 gfc_array_spec *as;
4044 int i;
4045
4046 as = ar->as;
4047 i = as->rank - 1;
4048 /* TODO: Full array sections are only allowed as actual parameters. */
4049 if (as->type == AS_ASSUMED_SIZE
4050 && (/*ar->type == AR_FULL
edf1eac2
SK
4051 ||*/ (ar->type == AR_SECTION
4052 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
6de9cd9a 4053 {
edf1eac2
SK
4054 gfc_error ("Rightmost upper bound of assumed size array section "
4055 "not specified at %L", &ar->where);
524af0d6 4056 return false;
6de9cd9a
DN
4057 }
4058
4059 if (ar->type == AR_FULL)
524af0d6 4060 return true;
6de9cd9a
DN
4061
4062 if (as->rank != ar->dimen)
4063 {
4064 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4065 &ar->where, ar->dimen, as->rank);
524af0d6 4066 return false;
6de9cd9a
DN
4067 }
4068
d3a9eea2
TB
4069 /* ar->codimen == 0 is a local array. */
4070 if (as->corank != ar->codimen && ar->codimen != 0)
4071 {
4072 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4073 &ar->where, ar->codimen, as->corank);
524af0d6 4074 return false;
d3a9eea2
TB
4075 }
4076
6de9cd9a 4077 for (i = 0; i < as->rank; i++)
524af0d6
JB
4078 if (!check_dimension (i, ar, as))
4079 return false;
6de9cd9a 4080
d3a9eea2
TB
4081 /* Local access has no coarray spec. */
4082 if (ar->codimen != 0)
4083 for (i = as->rank; i < as->rank + as->corank; i++)
4084 {
a3935ffc
TB
4085 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4086 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
d3a9eea2
TB
4087 {
4088 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4089 i + 1 - as->rank, &ar->where);
524af0d6 4090 return false;
d3a9eea2 4091 }
524af0d6
JB
4092 if (!check_dimension (i, ar, as))
4093 return false;
d3a9eea2
TB
4094 }
4095
524af0d6 4096 return true;
6de9cd9a
DN
4097}
4098
4099
4100/* Resolve one part of an array index. */
4101
524af0d6 4102static bool
92375a20
RG
4103gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4104 int force_index_integer_kind)
6de9cd9a
DN
4105{
4106 gfc_typespec ts;
4107
4108 if (index == NULL)
524af0d6 4109 return true;
6de9cd9a 4110
524af0d6
JB
4111 if (!gfc_resolve_expr (index))
4112 return false;
6de9cd9a 4113
ee943062 4114 if (check_scalar && index->rank != 0)
6de9cd9a 4115 {
ee943062 4116 gfc_error ("Array index at %L must be scalar", &index->where);
524af0d6 4117 return false;
6de9cd9a
DN
4118 }
4119
ee943062 4120 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
6de9cd9a 4121 {
acb388a0
JD
4122 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4123 &index->where, gfc_basic_typename (index->ts.type));
524af0d6 4124 return false;
6de9cd9a
DN
4125 }
4126
ee943062 4127 if (index->ts.type == BT_REAL)
524af0d6
JB
4128 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
4129 &index->where))
4130 return false;
ee943062 4131
92375a20
RG
4132 if ((index->ts.kind != gfc_index_integer_kind
4133 && force_index_integer_kind)
ee943062 4134 || index->ts.type != BT_INTEGER)
6de9cd9a 4135 {
810306f2 4136 gfc_clear_ts (&ts);
6de9cd9a
DN
4137 ts.type = BT_INTEGER;
4138 ts.kind = gfc_index_integer_kind;
4139
4140 gfc_convert_type_warn (index, &ts, 2, 0);
4141 }
4142
524af0d6 4143 return true;
6de9cd9a
DN
4144}
4145
92375a20
RG
4146/* Resolve one part of an array index. */
4147
524af0d6 4148bool
92375a20
RG
4149gfc_resolve_index (gfc_expr *index, int check_scalar)
4150{
4151 return gfc_resolve_index_1 (index, check_scalar, 1);
4152}
4153
bf302220
TK
4154/* Resolve a dim argument to an intrinsic function. */
4155
524af0d6 4156bool
bf302220
TK
4157gfc_resolve_dim_arg (gfc_expr *dim)
4158{
4159 if (dim == NULL)
524af0d6 4160 return true;
bf302220 4161
524af0d6
JB
4162 if (!gfc_resolve_expr (dim))
4163 return false;
bf302220
TK
4164
4165 if (dim->rank != 0)
4166 {
4167 gfc_error ("Argument dim at %L must be scalar", &dim->where);
524af0d6 4168 return false;
05c1e3a7 4169
bf302220 4170 }
33717d59 4171
bf302220
TK
4172 if (dim->ts.type != BT_INTEGER)
4173 {
4174 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
524af0d6 4175 return false;
bf302220 4176 }
33717d59 4177
bf302220
TK
4178 if (dim->ts.kind != gfc_index_integer_kind)
4179 {
4180 gfc_typespec ts;
4181
a79ff094 4182 gfc_clear_ts (&ts);
bf302220
TK
4183 ts.type = BT_INTEGER;
4184 ts.kind = gfc_index_integer_kind;
4185
4186 gfc_convert_type_warn (dim, &ts, 2, 0);
4187 }
4188
524af0d6 4189 return true;
bf302220 4190}
6de9cd9a
DN
4191
4192/* Given an expression that contains array references, update those array
4193 references to point to the right array specifications. While this is
4194 filled in during matching, this information is difficult to save and load
4195 in a module, so we take care of it here.
4196
4197 The idea here is that the original array reference comes from the
4198 base symbol. We traverse the list of reference structures, setting
4199 the stored reference to references. Component references can
4200 provide an additional array specification. */
4201
4202static void
edf1eac2 4203find_array_spec (gfc_expr *e)
6de9cd9a
DN
4204{
4205 gfc_array_spec *as;
4206 gfc_component *c;
4207 gfc_ref *ref;
4208
cf2b3c22 4209 if (e->symtree->n.sym->ts.type == BT_CLASS)
7a08eda1 4210 as = CLASS_DATA (e->symtree->n.sym)->as;
cf2b3c22
TB
4211 else
4212 as = e->symtree->n.sym->as;
6de9cd9a
DN
4213
4214 for (ref = e->ref; ref; ref = ref->next)
4215 switch (ref->type)
4216 {
4217 case REF_ARRAY:
4218 if (as == NULL)
4219 gfc_internal_error ("find_array_spec(): Missing spec");
4220
4221 ref->u.ar.as = as;
4222 as = NULL;
4223 break;
4224
4225 case REF_COMPONENT:
02139671 4226 c = ref->u.c.component;
d4b7d0f0 4227 if (c->attr.dimension)
6de9cd9a
DN
4228 {
4229 if (as != NULL)
4230 gfc_internal_error ("find_array_spec(): unused as(1)");
4231 as = c->as;
4232 }
4233
6de9cd9a
DN
4234 break;
4235
4236 case REF_SUBSTRING:
4237 break;
4238 }
4239
4240 if (as != NULL)
4241 gfc_internal_error ("find_array_spec(): unused as(2)");
4242}
4243
4244
4245/* Resolve an array reference. */
4246
524af0d6 4247static bool
edf1eac2 4248resolve_array_ref (gfc_array_ref *ar)
6de9cd9a
DN
4249{
4250 int i, check_scalar;
b6398823 4251 gfc_expr *e;
6de9cd9a 4252
d3a9eea2 4253 for (i = 0; i < ar->dimen + ar->codimen; i++)
6de9cd9a
DN
4254 {
4255 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4256
92375a20
RG
4257 /* Do not force gfc_index_integer_kind for the start. We can
4258 do fine with any integer kind. This avoids temporary arrays
4259 created for indexing with a vector. */
524af0d6
JB
4260 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4261 return false;
4262 if (!gfc_resolve_index (ar->end[i], check_scalar))
4263 return false;
4264 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4265 return false;
6de9cd9a 4266
b6398823
PT
4267 e = ar->start[i];
4268
6de9cd9a 4269 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
b6398823 4270 switch (e->rank)
6de9cd9a
DN
4271 {
4272 case 0:
4273 ar->dimen_type[i] = DIMEN_ELEMENT;
4274 break;
4275
4276 case 1:
4277 ar->dimen_type[i] = DIMEN_VECTOR;
b6398823 4278 if (e->expr_type == EXPR_VARIABLE
edf1eac2 4279 && e->symtree->n.sym->ts.type == BT_DERIVED)
b6398823 4280 ar->start[i] = gfc_get_parentheses (e);
6de9cd9a
DN
4281 break;
4282
4283 default:
4284 gfc_error ("Array index at %L is an array of rank %d",
b6398823 4285 &ar->c_where[i], e->rank);
524af0d6 4286 return false;
6de9cd9a 4287 }
ee247636
TK
4288
4289 /* Fill in the upper bound, which may be lower than the
4290 specified one for something like a(2:10:5), which is
4291 identical to a(2:7:5). Only relevant for strides not equal
2d27cb44 4292 to one. Don't try a division by zero. */
ee247636
TK
4293 if (ar->dimen_type[i] == DIMEN_RANGE
4294 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
2d27cb44
TK
4295 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4296 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
ee247636
TK
4297 {
4298 mpz_t size, end;
4299
524af0d6 4300 if (gfc_ref_dimen_size (ar, i, &size, &end))
ee247636
TK
4301 {
4302 if (ar->end[i] == NULL)
4303 {
4304 ar->end[i] =
4305 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4306 &ar->where);
4307 mpz_set (ar->end[i]->value.integer, end);
4308 }
4309 else if (ar->end[i]->ts.type == BT_INTEGER
4310 && ar->end[i]->expr_type == EXPR_CONSTANT)
4311 {
4312 mpz_set (ar->end[i]->value.integer, end);
4313 }
4314 else
4315 gcc_unreachable ();
4316
4317 mpz_clear (size);
4318 mpz_clear (end);
4319 }
4320 }
6de9cd9a
DN
4321 }
4322
5551a54e
MM
4323 if (ar->type == AR_FULL)
4324 {
4325 if (ar->as->rank == 0)
4326 ar->type = AR_ELEMENT;
4327
4328 /* Make sure array is the same as array(:,:), this way
4329 we don't need to special case all the time. */
4330 ar->dimen = ar->as->rank;
4331 for (i = 0; i < ar->dimen; i++)
4332 {
4333 ar->dimen_type[i] = DIMEN_RANGE;
4334
4335 gcc_assert (ar->start[i] == NULL);
4336 gcc_assert (ar->end[i] == NULL);
4337 gcc_assert (ar->stride[i] == NULL);
4338 }
4339 }
d3a9eea2 4340
6de9cd9a
DN
4341 /* If the reference type is unknown, figure out what kind it is. */
4342
4343 if (ar->type == AR_UNKNOWN)
4344 {
4345 ar->type = AR_ELEMENT;
4346 for (i = 0; i < ar->dimen; i++)
4347 if (ar->dimen_type[i] == DIMEN_RANGE
4348 || ar->dimen_type[i] == DIMEN_VECTOR)
4349 {
4350 ar->type = AR_SECTION;
4351 break;
4352 }
4353 }
4354
524af0d6
JB
4355 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4356 return false;
6de9cd9a 4357
b78a06b2
MM
4358 if (ar->as->corank && ar->codimen == 0)
4359 {
4360 int n;
4361 ar->codimen = ar->as->corank;
4362 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4363 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4364 }
4365
524af0d6 4366 return true;
6de9cd9a
DN
4367}
4368
4369
524af0d6 4370static bool
edf1eac2 4371resolve_substring (gfc_ref *ref)
6de9cd9a 4372{
b0c06816
FXC
4373 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4374
6de9cd9a
DN
4375 if (ref->u.ss.start != NULL)
4376 {
524af0d6
JB
4377 if (!gfc_resolve_expr (ref->u.ss.start))
4378 return false;
6de9cd9a
DN
4379
4380 if (ref->u.ss.start->ts.type != BT_INTEGER)
4381 {
4382 gfc_error ("Substring start index at %L must be of type INTEGER",
4383 &ref->u.ss.start->where);
524af0d6 4384 return false;
6de9cd9a
DN
4385 }
4386
4387 if (ref->u.ss.start->rank != 0)
4388 {
4389 gfc_error ("Substring start index at %L must be scalar",
4390 &ref->u.ss.start->where);
524af0d6 4391 return false;
6de9cd9a
DN
4392 }
4393
97bca513
FXC
4394 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4395 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4396 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a
DN
4397 {
4398 gfc_error ("Substring start index at %L is less than one",
4399 &ref->u.ss.start->where);
524af0d6 4400 return false;
6de9cd9a
DN
4401 }
4402 }
4403
4404 if (ref->u.ss.end != NULL)
4405 {
524af0d6
JB
4406 if (!gfc_resolve_expr (ref->u.ss.end))
4407 return false;
6de9cd9a
DN
4408
4409 if (ref->u.ss.end->ts.type != BT_INTEGER)
4410 {
4411 gfc_error ("Substring end index at %L must be of type INTEGER",
4412 &ref->u.ss.end->where);
524af0d6 4413 return false;
6de9cd9a
DN
4414 }
4415
4416 if (ref->u.ss.end->rank != 0)
4417 {
4418 gfc_error ("Substring end index at %L must be scalar",
4419 &ref->u.ss.end->where);
524af0d6 4420 return false;
6de9cd9a
DN
4421 }
4422
4423 if (ref->u.ss.length != NULL
97bca513
FXC
4424 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4425 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4426 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a 4427 {
97bca513 4428 gfc_error ("Substring end index at %L exceeds the string length",
6de9cd9a 4429 &ref->u.ss.start->where);
524af0d6 4430 return false;
6de9cd9a 4431 }
b0c06816
FXC
4432
4433 if (compare_bound_mpz_t (ref->u.ss.end,
4434 gfc_integer_kinds[k].huge) == CMP_GT
4435 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4436 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4437 {
4438 gfc_error ("Substring end index at %L is too large",
4439 &ref->u.ss.end->where);
524af0d6 4440 return false;
b0c06816 4441 }
6de9cd9a
DN
4442 }
4443
524af0d6 4444 return true;
6de9cd9a
DN
4445}
4446
4447
07368af0
PT
4448/* This function supplies missing substring charlens. */
4449
4450void
4451gfc_resolve_substring_charlen (gfc_expr *e)
4452{
4453 gfc_ref *char_ref;
4454 gfc_expr *start, *end;
4455
4456 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4457 if (char_ref->type == REF_SUBSTRING)
4458 break;
4459
4460 if (!char_ref)
4461 return;
4462
4463 gcc_assert (char_ref->next == NULL);
4464
bc21d315 4465 if (e->ts.u.cl)
07368af0 4466 {
bc21d315
JW
4467 if (e->ts.u.cl->length)
4468 gfc_free_expr (e->ts.u.cl->length);
07368af0
PT
4469 else if (e->expr_type == EXPR_VARIABLE
4470 && e->symtree->n.sym->attr.dummy)
4471 return;
4472 }
4473
4474 e->ts.type = BT_CHARACTER;
4475 e->ts.kind = gfc_default_character_kind;
4476
bc21d315 4477 if (!e->ts.u.cl)
b76e28c6 4478 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4479
4480 if (char_ref->u.ss.start)
4481 start = gfc_copy_expr (char_ref->u.ss.start);
4482 else
b7e75771 4483 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
07368af0
PT
4484
4485 if (char_ref->u.ss.end)
4486 end = gfc_copy_expr (char_ref->u.ss.end);
4487 else if (e->expr_type == EXPR_VARIABLE)
bc21d315 4488 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
07368af0
PT
4489 else
4490 end = NULL;
4491
4492 if (!start || !end)
efb63364
TB
4493 {
4494 gfc_free_expr (start);
4495 gfc_free_expr (end);
4496 return;
4497 }
07368af0
PT
4498
4499 /* Length = (end - start +1). */
bc21d315 4500 e->ts.u.cl->length = gfc_subtract (end, start);
b7e75771
JD
4501 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4502 gfc_get_int_expr (gfc_default_integer_kind,
4503 NULL, 1));
07368af0 4504
bc21d315
JW
4505 e->ts.u.cl->length->ts.type = BT_INTEGER;
4506 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
07368af0
PT
4507
4508 /* Make sure that the length is simplified. */
bc21d315
JW
4509 gfc_simplify_expr (e->ts.u.cl->length, 1);
4510 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4511}
4512
4513
6de9cd9a
DN
4514/* Resolve subtype references. */
4515
524af0d6 4516static bool
edf1eac2 4517resolve_ref (gfc_expr *expr)
6de9cd9a
DN
4518{
4519 int current_part_dimension, n_components, seen_part_dimension;
4520 gfc_ref *ref;
4521
4522 for (ref = expr->ref; ref; ref = ref->next)
4523 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4524 {
4525 find_array_spec (expr);
4526 break;
4527 }
4528
4529 for (ref = expr->ref; ref; ref = ref->next)
4530 switch (ref->type)
4531 {
4532 case REF_ARRAY:
524af0d6
JB
4533 if (!resolve_array_ref (&ref->u.ar))
4534 return false;
6de9cd9a
DN
4535 break;
4536
4537 case REF_COMPONENT:
4538 break;
4539
4540 case REF_SUBSTRING:
524af0d6
JB
4541 if (!resolve_substring (ref))
4542 return false;
6de9cd9a
DN
4543 break;
4544 }
4545
4546 /* Check constraints on part references. */
4547
4548 current_part_dimension = 0;
4549 seen_part_dimension = 0;
4550 n_components = 0;
4551
4552 for (ref = expr->ref; ref; ref = ref->next)
4553 {
4554 switch (ref->type)
4555 {
4556 case REF_ARRAY:
4557 switch (ref->u.ar.type)
4558 {
4559 case AR_FULL:
d3a9eea2
TB
4560 /* Coarray scalar. */
4561 if (ref->u.ar.as->rank == 0)
4562 {
4563 current_part_dimension = 0;
4564 break;
4565 }
4566 /* Fall through. */
6de9cd9a
DN
4567 case AR_SECTION:
4568 current_part_dimension = 1;
4569 break;
4570
4571 case AR_ELEMENT:
4572 current_part_dimension = 0;
4573 break;
4574
4575 case AR_UNKNOWN:
4576 gfc_internal_error ("resolve_ref(): Bad array reference");
4577 }
4578
4579 break;
4580
4581 case REF_COMPONENT:
51f824b6 4582 if (current_part_dimension || seen_part_dimension)
6de9cd9a 4583 {
ef2bbc8c
JW
4584 /* F03:C614. */
4585 if (ref->u.c.component->attr.pointer
8f75db9f
PT
4586 || ref->u.c.component->attr.proc_pointer
4587 || (ref->u.c.component->ts.type == BT_CLASS
4588 && CLASS_DATA (ref->u.c.component)->attr.pointer))
edf1eac2
SK
4589 {
4590 gfc_error ("Component to the right of a part reference "
4591 "with nonzero rank must not have the POINTER "
4592 "attribute at %L", &expr->where);
524af0d6 4593 return false;
51f824b6 4594 }
8f75db9f
PT
4595 else if (ref->u.c.component->attr.allocatable
4596 || (ref->u.c.component->ts.type == BT_CLASS
4597 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4598
edf1eac2
SK
4599 {
4600 gfc_error ("Component to the right of a part reference "
4601 "with nonzero rank must not have the ALLOCATABLE "
4602 "attribute at %L", &expr->where);
524af0d6 4603 return false;
51f824b6 4604 }
6de9cd9a
DN
4605 }
4606
4607 n_components++;
4608 break;
4609
4610 case REF_SUBSTRING:
4611 break;
4612 }
4613
4614 if (((ref->type == REF_COMPONENT && n_components > 1)
4615 || ref->next == NULL)
edf1eac2 4616 && current_part_dimension
6de9cd9a
DN
4617 && seen_part_dimension)
4618 {
6de9cd9a
DN
4619 gfc_error ("Two or more part references with nonzero rank must "
4620 "not be specified at %L", &expr->where);
524af0d6 4621 return false;
6de9cd9a
DN
4622 }
4623
4624 if (ref->type == REF_COMPONENT)
4625 {
4626 if (current_part_dimension)
4627 seen_part_dimension = 1;
4628
edf1eac2 4629 /* reset to make sure */
6de9cd9a
DN
4630 current_part_dimension = 0;
4631 }
4632 }
4633
524af0d6 4634 return true;
6de9cd9a
DN
4635}
4636
4637
4638/* Given an expression, determine its shape. This is easier than it sounds.
f7b529fa 4639 Leaves the shape array NULL if it is not possible to determine the shape. */
6de9cd9a
DN
4640
4641static void
edf1eac2 4642expression_shape (gfc_expr *e)
6de9cd9a
DN
4643{
4644 mpz_t array[GFC_MAX_DIMENSIONS];
4645 int i;
4646
c62c6622 4647 if (e->rank <= 0 || e->shape != NULL)
6de9cd9a
DN
4648 return;
4649
4650 for (i = 0; i < e->rank; i++)
524af0d6 4651 if (!gfc_array_dimen_size (e, i, &array[i]))
6de9cd9a
DN
4652 goto fail;
4653
4654 e->shape = gfc_get_shape (e->rank);
4655
4656 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4657
4658 return;
4659
4660fail:
4661 for (i--; i >= 0; i--)
4662 mpz_clear (array[i]);
4663}
4664
4665
4666/* Given a variable expression node, compute the rank of the expression by
4667 examining the base symbol and any reference structures it may have. */
4668
4669static void
edf1eac2 4670expression_rank (gfc_expr *e)
6de9cd9a
DN
4671{
4672 gfc_ref *ref;
4673 int i, rank;
4674
00ca6640
DK
4675 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4676 could lead to serious confusion... */
4677 gcc_assert (e->expr_type != EXPR_COMPCALL);
4678
6de9cd9a
DN
4679 if (e->ref == NULL)
4680 {
4681 if (e->expr_type == EXPR_ARRAY)
4682 goto done;
f7b529fa 4683 /* Constructors can have a rank different from one via RESHAPE(). */
6de9cd9a
DN
4684
4685 if (e->symtree == NULL)
4686 {
4687 e->rank = 0;
4688 goto done;
4689 }
4690
4691 e->rank = (e->symtree->n.sym->as == NULL)
edf1eac2 4692 ? 0 : e->symtree->n.sym->as->rank;
6de9cd9a
DN
4693 goto done;
4694 }
4695
4696 rank = 0;
4697
4698 for (ref = e->ref; ref; ref = ref->next)
4699 {
2d300fac
JW
4700 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4701 && ref->u.c.component->attr.function && !ref->next)
4702 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4703
6de9cd9a
DN
4704 if (ref->type != REF_ARRAY)
4705 continue;
4706
4707 if (ref->u.ar.type == AR_FULL)
4708 {
4709 rank = ref->u.ar.as->rank;
4710 break;
4711 }
4712
4713 if (ref->u.ar.type == AR_SECTION)
4714 {
edf1eac2 4715 /* Figure out the rank of the section. */
6de9cd9a
DN
4716 if (rank != 0)
4717 gfc_internal_error ("expression_rank(): Two array specs");
4718
4719 for (i = 0; i < ref->u.ar.dimen; i++)
4720 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4721 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4722 rank++;
4723
4724 break;
4725 }
4726 }
4727
4728 e->rank = rank;
4729
4730done:
4731 expression_shape (e);
4732}
4733
4734
4735/* Resolve a variable expression. */
4736
524af0d6 4737static bool
edf1eac2 4738resolve_variable (gfc_expr *e)
6de9cd9a
DN
4739{
4740 gfc_symbol *sym;
524af0d6 4741 bool t;
0e9a445b 4742
524af0d6 4743 t = true;
6de9cd9a 4744
3e978d30 4745 if (e->symtree == NULL)
524af0d6 4746 return false;
52bf62f9
DK
4747 sym = e->symtree->n.sym;
4748
e7ac6a7c
TB
4749 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4750 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4751 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4752 {
4753 if (!actual_arg || inquiry_argument)
4754 {
4755 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4756 "be used as actual argument", sym->name, &e->where);
4757 return false;
4758 }
4759 }
45a69325 4760 /* TS 29113, 407b. */
e7ac6a7c 4761 else if (e->ts.type == BT_ASSUMED)
45a69325 4762 {
c62c6622
TB
4763 if (!actual_arg)
4764 {
4765 gfc_error ("Assumed-type variable %s at %L may only be used "
4766 "as actual argument", sym->name, &e->where);
524af0d6 4767 return false;
c62c6622
TB
4768 }
4769 else if (inquiry_argument && !first_actual_arg)
4770 {
4771 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4772 for all inquiry functions in resolve_function; the reason is
4773 that the function-name resolution happens too late in that
4774 function. */
4775 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4776 "an inquiry function shall be the first argument",
4777 sym->name, &e->where);
524af0d6 4778 return false;
c62c6622
TB
4779 }
4780 }
c62c6622 4781 /* TS 29113, C535b. */
e7ac6a7c
TB
4782 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4783 && CLASS_DATA (sym)->as
4784 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4785 || (sym->ts.type != BT_CLASS && sym->as
4786 && sym->as->type == AS_ASSUMED_RANK))
c62c6622
TB
4787 {
4788 if (!actual_arg)
4789 {
4790 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4791 "actual argument", sym->name, &e->where);
524af0d6 4792 return false;
c62c6622
TB
4793 }
4794 else if (inquiry_argument && !first_actual_arg)
4795 {
4796 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4797 for all inquiry functions in resolve_function; the reason is
4798 that the function-name resolution happens too late in that
4799 function. */
4800 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4801 "to an inquiry function shall be the first argument",
4802 sym->name, &e->where);
524af0d6 4803 return false;
c62c6622 4804 }
45a69325
TB
4805 }
4806
e7ac6a7c 4807 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
45a69325 4808 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
c62c6622 4809 && e->ref->next == NULL))
e7ac6a7c
TB
4810 {
4811 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4812 "a subobject reference", sym->name, &e->ref->u.ar.where);
4813 return false;
4814 }
4815 /* TS 29113, 407b. */
4816 else if (e->ts.type == BT_ASSUMED && e->ref
4817 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4818 && e->ref->next == NULL))
45a69325 4819 {
c62c6622
TB
4820 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4821 "reference", sym->name, &e->ref->u.ar.where);
524af0d6 4822 return false;
45a69325
TB
4823 }
4824
c62c6622
TB
4825 /* TS 29113, C535b. */
4826 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4827 && CLASS_DATA (sym)->as
4828 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4829 || (sym->ts.type != BT_CLASS && sym->as
4830 && sym->as->type == AS_ASSUMED_RANK))
4831 && e->ref
4832 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4833 && e->ref->next == NULL))
4834 {
4835 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4836 "reference", sym->name, &e->ref->u.ar.where);
524af0d6 4837 return false;
c62c6622
TB
4838 }
4839
4840
3e78238a 4841 /* If this is an associate-name, it may be parsed with an array reference
8f75db9f
PT
4842 in error even though the target is scalar. Fail directly in this case.
4843 TODO Understand why class scalar expressions must be excluded. */
4844 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
4845 {
4846 if (sym->ts.type == BT_CLASS)
4847 gfc_fix_class_refs (e);
4848 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
524af0d6 4849 return false;
8f75db9f 4850 }
52bf62f9 4851
c3f34952
TB
4852 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
4853 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
4854
52bf62f9
DK
4855 /* On the other hand, the parser may not have known this is an array;
4856 in this case, we have to add a FULL reference. */
4857 if (sym->assoc && sym->attr.dimension && !e->ref)
4858 {
4859 e->ref = gfc_get_ref ();
4860 e->ref->type = REF_ARRAY;
4861 e->ref->u.ar.type = AR_FULL;
4862 e->ref->u.ar.dimen = 0;
4863 }
6de9cd9a 4864
524af0d6
JB
4865 if (e->ref && !resolve_ref (e))
4866 return false;
009e94d4 4867
3070bab4
JW
4868 if (sym->attr.flavor == FL_PROCEDURE
4869 && (!sym->attr.function
4870 || (sym->attr.function && sym->result
4871 && sym->result->attr.proc_pointer
4872 && !sym->result->attr.function)))
6de9cd9a
DN
4873 {
4874 e->ts.type = BT_PROCEDURE;
a03826d1 4875 goto resolve_procedure;
6de9cd9a
DN
4876 }
4877
4878 if (sym->ts.type != BT_UNKNOWN)
4879 gfc_variable_attr (e, &e->ts);
4880 else
4881 {
4882 /* Must be a simple variable reference. */
524af0d6
JB
4883 if (!gfc_set_default_type (sym, 1, sym->ns))
4884 return false;
6de9cd9a
DN
4885 e->ts = sym->ts;
4886 }
4887
48474141 4888 if (check_assumed_size_reference (sym, e))
524af0d6 4889 return false;
48474141 4890
0e9a445b
PT
4891 /* Deal with forward references to entries during resolve_code, to
4892 satisfy, at least partially, 12.5.2.5. */
4893 if (gfc_current_ns->entries
edf1eac2
SK
4894 && current_entry_id == sym->entry_id
4895 && cs_base
4896 && cs_base->current
4897 && cs_base->current->op != EXEC_ENTRY)
0e9a445b
PT
4898 {
4899 gfc_entry_list *entry;
4900 gfc_formal_arglist *formal;
4901 int n;
fd061185 4902 bool seen, saved_specification_expr;
0e9a445b
PT
4903
4904 /* If the symbol is a dummy... */
70365b5c 4905 if (sym->attr.dummy && sym->ns == gfc_current_ns)
0e9a445b
PT
4906 {
4907 entry = gfc_current_ns->entries;
4908 seen = false;
4909
4910 /* ...test if the symbol is a parameter of previous entries. */
4911 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4912 for (formal = entry->sym->formal; formal; formal = formal->next)
4913 {
4914 if (formal->sym && sym->name == formal->sym->name)
502af491
PCC
4915 {
4916 seen = true;
4917 break;
4918 }
0e9a445b
PT
4919 }
4920
4921 /* If it has not been seen as a dummy, this is an error. */
4922 if (!seen)
4923 {
4924 if (specification_expr)
70365b5c
TB
4925 gfc_error ("Variable '%s', used in a specification expression"
4926 ", is referenced at %L before the ENTRY statement "
0e9a445b
PT
4927 "in which it is a parameter",
4928 sym->name, &cs_base->current->loc);
4929 else
4930 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4931 "statement in which it is a parameter",
4932 sym->name, &cs_base->current->loc);
524af0d6 4933 t = false;
0e9a445b
PT
4934 }
4935 }
4936
4937 /* Now do the same check on the specification expressions. */
fd061185
TB
4938 saved_specification_expr = specification_expr;
4939 specification_expr = true;
0e9a445b 4940 if (sym->ts.type == BT_CHARACTER
524af0d6
JB
4941 && !gfc_resolve_expr (sym->ts.u.cl->length))
4942 t = false;
0e9a445b
PT
4943
4944 if (sym->as)
4945 for (n = 0; n < sym->as->rank; n++)
4946 {
524af0d6
JB
4947 if (!gfc_resolve_expr (sym->as->lower[n]))
4948 t = false;
4949 if (!gfc_resolve_expr (sym->as->upper[n]))
4950 t = false;
0e9a445b 4951 }
fd061185 4952 specification_expr = saved_specification_expr;
0e9a445b 4953
524af0d6 4954 if (t)
0e9a445b
PT
4955 /* Update the symbol's entry level. */
4956 sym->entry_id = current_entry_id + 1;
4957 }
4958
022e30c0
PT
4959 /* If a symbol has been host_associated mark it. This is used latter,
4960 to identify if aliasing is possible via host association. */
4961 if (sym->attr.flavor == FL_VARIABLE
4962 && gfc_current_ns->parent
4963 && (gfc_current_ns->parent == sym->ns
4964 || (gfc_current_ns->parent->parent
4965 && gfc_current_ns->parent->parent == sym->ns)))
4966 sym->attr.host_assoc = 1;
4967
a03826d1 4968resolve_procedure:
524af0d6
JB
4969 if (t && !resolve_procedure_expression (e))
4970 t = false;
a03826d1 4971
d3a9eea2
TB
4972 /* F2008, C617 and C1229. */
4973 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
4974 && gfc_is_coindexed (e))
4975 {
4976 gfc_ref *ref, *ref2 = NULL;
4977
d3a9eea2
TB
4978 for (ref = e->ref; ref; ref = ref->next)
4979 {
4980 if (ref->type == REF_COMPONENT)
4981 ref2 = ref;
4982 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4983 break;
4984 }
4985
4986 for ( ; ref; ref = ref->next)
4987 if (ref->type == REF_COMPONENT)
4988 break;
4989
a70de21f
TB
4990 /* Expression itself is not coindexed object. */
4991 if (ref && e->ts.type == BT_CLASS)
4992 {
4993 gfc_error ("Polymorphic subobject of coindexed object at %L",
4994 &e->where);
524af0d6 4995 t = false;
a70de21f
TB
4996 }
4997
d3a9eea2
TB
4998 /* Expression itself is coindexed object. */
4999 if (ref == NULL)
5000 {
5001 gfc_component *c;
5002 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5003 for ( ; c; c = c->next)
5004 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5005 {
5006 gfc_error ("Coindexed object with polymorphic allocatable "
5007 "subcomponent at %L", &e->where);
524af0d6 5008 t = false;
d3a9eea2
TB
5009 break;
5010 }
5011 }
5012 }
5013
0e9a445b 5014 return t;
6de9cd9a
DN
5015}
5016
5017
eb77cddf
PT
5018/* Checks to see that the correct symbol has been host associated.
5019 The only situation where this arises is that in which a twice
5020 contained function is parsed after the host association is made.
5b3b1d09
PT
5021 Therefore, on detecting this, change the symbol in the expression
5022 and convert the array reference into an actual arglist if the old
5023 symbol is a variable. */
eb77cddf
PT
5024static bool
5025check_host_association (gfc_expr *e)
5026{
5027 gfc_symbol *sym, *old_sym;
5b3b1d09 5028 gfc_symtree *st;
eb77cddf 5029 int n;
5b3b1d09 5030 gfc_ref *ref;
e4bf01a4 5031 gfc_actual_arglist *arg, *tail = NULL;
8de10a62 5032 bool retval = e->expr_type == EXPR_FUNCTION;
eb77cddf 5033
a1ab6660
PT
5034 /* If the expression is the result of substitution in
5035 interface.c(gfc_extend_expr) because there is no way in
5036 which the host association can be wrong. */
5037 if (e->symtree == NULL
5038 || e->symtree->n.sym == NULL
5039 || e->user_operator)
8de10a62 5040 return retval;
eb77cddf
PT
5041
5042 old_sym = e->symtree->n.sym;
8de10a62 5043
eb77cddf 5044 if (gfc_current_ns->parent
eb77cddf
PT
5045 && old_sym->ns != gfc_current_ns)
5046 {
5b3b1d09
PT
5047 /* Use the 'USE' name so that renamed module symbols are
5048 correctly handled. */
9be3684b 5049 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5b3b1d09 5050
a944c79a 5051 if (sym && old_sym != sym
67cec813 5052 && sym->ts.type == old_sym->ts.type
a944c79a
PT
5053 && sym->attr.flavor == FL_PROCEDURE
5054 && sym->attr.contained)
eb77cddf 5055 {
5b3b1d09 5056 /* Clear the shape, since it might not be valid. */
d54e80ce 5057 gfc_free_shape (&e->shape, e->rank);
eb77cddf 5058
1aafbf99
PT
5059 /* Give the expression the right symtree! */
5060 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5061 gcc_assert (st != NULL);
eb77cddf 5062
1aafbf99
PT
5063 if (old_sym->attr.flavor == FL_PROCEDURE
5064 || e->expr_type == EXPR_FUNCTION)
5065 {
5b3b1d09
PT
5066 /* Original was function so point to the new symbol, since
5067 the actual argument list is already attached to the
5068 expression. */
5069 e->value.function.esym = NULL;
5070 e->symtree = st;
5071 }
5072 else
5073 {
5074 /* Original was variable so convert array references into
5075 an actual arglist. This does not need any checking now
d8155bf5 5076 since resolve_function will take care of it. */
5b3b1d09
PT
5077 e->value.function.actual = NULL;
5078 e->expr_type = EXPR_FUNCTION;
5079 e->symtree = st;
eb77cddf 5080
5b3b1d09
PT
5081 /* Ambiguity will not arise if the array reference is not
5082 the last reference. */
5083 for (ref = e->ref; ref; ref = ref->next)
5084 if (ref->type == REF_ARRAY && ref->next == NULL)
5085 break;
5086
5087 gcc_assert (ref->type == REF_ARRAY);
5088
5089 /* Grab the start expressions from the array ref and
5090 copy them into actual arguments. */
5091 for (n = 0; n < ref->u.ar.dimen; n++)
5092 {
5093 arg = gfc_get_actual_arglist ();
5094 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5095 if (e->value.function.actual == NULL)
5096 tail = e->value.function.actual = arg;
5097 else
5098 {
5099 tail->next = arg;
5100 tail = arg;
5101 }
5102 }
eb77cddf 5103
5b3b1d09
PT
5104 /* Dump the reference list and set the rank. */
5105 gfc_free_ref_list (e->ref);
5106 e->ref = NULL;
5107 e->rank = sym->as ? sym->as->rank : 0;
5108 }
5109
5110 gfc_resolve_expr (e);
5111 sym->refs++;
eb77cddf
PT
5112 }
5113 }
8de10a62 5114 /* This might have changed! */
eb77cddf
PT
5115 return e->expr_type == EXPR_FUNCTION;
5116}
5117
5118
07368af0
PT
5119static void
5120gfc_resolve_character_operator (gfc_expr *e)
5121{
5122 gfc_expr *op1 = e->value.op.op1;
5123 gfc_expr *op2 = e->value.op.op2;
5124 gfc_expr *e1 = NULL;
5125 gfc_expr *e2 = NULL;
5126
a1ee985f 5127 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
07368af0 5128
bc21d315
JW
5129 if (op1->ts.u.cl && op1->ts.u.cl->length)
5130 e1 = gfc_copy_expr (op1->ts.u.cl->length);
07368af0 5131 else if (op1->expr_type == EXPR_CONSTANT)
b7e75771
JD
5132 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5133 op1->value.character.length);
07368af0 5134
bc21d315
JW
5135 if (op2->ts.u.cl && op2->ts.u.cl->length)
5136 e2 = gfc_copy_expr (op2->ts.u.cl->length);
07368af0 5137 else if (op2->expr_type == EXPR_CONSTANT)
b7e75771
JD
5138 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5139 op2->value.character.length);
07368af0 5140
b76e28c6 5141 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
5142
5143 if (!e1 || !e2)
d7920cf0
TB
5144 {
5145 gfc_free_expr (e1);
5146 gfc_free_expr (e2);
4d382327 5147
d7920cf0
TB
5148 return;
5149 }
07368af0 5150
bc21d315
JW
5151 e->ts.u.cl->length = gfc_add (e1, e2);
5152 e->ts.u.cl->length->ts.type = BT_INTEGER;
5153 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5154 gfc_simplify_expr (e->ts.u.cl->length, 0);
5155 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
5156
5157 return;
5158}
5159
5160
5161/* Ensure that an character expression has a charlen and, if possible, a
5162 length expression. */
5163
5164static void
5165fixup_charlen (gfc_expr *e)
5166{
5167 /* The cases fall through so that changes in expression type and the need
5168 for multiple fixes are picked up. In all circumstances, a charlen should
5169 be available for the middle end to hang a backend_decl on. */
5170 switch (e->expr_type)
5171 {
5172 case EXPR_OP:
5173 gfc_resolve_character_operator (e);
5174
5175 case EXPR_ARRAY:
5176 if (e->expr_type == EXPR_ARRAY)
5177 gfc_resolve_character_array_constructor (e);
5178
5179 case EXPR_SUBSTRING:
bc21d315 5180 if (!e->ts.u.cl && e->ref)
07368af0
PT
5181 gfc_resolve_substring_charlen (e);
5182
5183 default:
bc21d315 5184 if (!e->ts.u.cl)
b76e28c6 5185 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
5186
5187 break;
5188 }
5189}
5190
5191
8e1f752a
DK
5192/* Update an actual argument to include the passed-object for type-bound
5193 procedures at the right position. */
5194
5195static gfc_actual_arglist*
90661f26
JW
5196update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5197 const char *name)
8e1f752a 5198{
b82657f4
DK
5199 gcc_assert (argpos > 0);
5200
8e1f752a
DK
5201 if (argpos == 1)
5202 {
5203 gfc_actual_arglist* result;
5204
5205 result = gfc_get_actual_arglist ();
5206 result->expr = po;
5207 result->next = lst;
90661f26
JW
5208 if (name)
5209 result->name = name;
8e1f752a
DK
5210
5211 return result;
5212 }
5213
90661f26
JW
5214 if (lst)
5215 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5216 else
5217 lst = update_arglist_pass (NULL, po, argpos - 1, name);
8e1f752a
DK
5218 return lst;
5219}
5220
5221
e157f736 5222/* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
8e1f752a 5223
e157f736
DK
5224static gfc_expr*
5225extract_compcall_passed_object (gfc_expr* e)
8e1f752a
DK
5226{
5227 gfc_expr* po;
8e1f752a 5228
e157f736 5229 gcc_assert (e->expr_type == EXPR_COMPCALL);
8e1f752a 5230
4a44a72d
DK
5231 if (e->value.compcall.base_object)
5232 po = gfc_copy_expr (e->value.compcall.base_object);
5233 else
5234 {
5235 po = gfc_get_expr ();
5236 po->expr_type = EXPR_VARIABLE;
5237 po->symtree = e->symtree;
5238 po->ref = gfc_copy_ref (e->ref);
63894de2 5239 po->where = e->where;
4a44a72d 5240 }
8e1f752a 5241
524af0d6 5242 if (!gfc_resolve_expr (po))
e157f736
DK
5243 return NULL;
5244
5245 return po;
5246}
5247
5248
5249/* Update the arglist of an EXPR_COMPCALL expression to include the
5250 passed-object. */
5251
524af0d6 5252static bool
e157f736
DK
5253update_compcall_arglist (gfc_expr* e)
5254{
5255 gfc_expr* po;
5256 gfc_typebound_proc* tbp;
5257
5258 tbp = e->value.compcall.tbp;
5259
b82657f4 5260 if (tbp->error)
524af0d6 5261 return false;
b82657f4 5262
e157f736
DK
5263 po = extract_compcall_passed_object (e);
5264 if (!po)
524af0d6 5265 return false;
e157f736 5266
4a44a72d 5267 if (tbp->nopass || e->value.compcall.ignore_pass)
8e1f752a
DK
5268 {
5269 gfc_free_expr (po);
524af0d6 5270 return true;
8e1f752a
DK
5271 }
5272
5273 gcc_assert (tbp->pass_arg_num > 0);
5274 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
90661f26
JW
5275 tbp->pass_arg_num,
5276 tbp->pass_arg);
5277
524af0d6 5278 return true;
90661f26
JW
5279}
5280
5281
5282/* Extract the passed object from a PPC call (a copy of it). */
5283
5284static gfc_expr*
5285extract_ppc_passed_object (gfc_expr *e)
5286{
5287 gfc_expr *po;
5288 gfc_ref **ref;
5289
5290 po = gfc_get_expr ();
5291 po->expr_type = EXPR_VARIABLE;
5292 po->symtree = e->symtree;
5293 po->ref = gfc_copy_ref (e->ref);
63894de2 5294 po->where = e->where;
90661f26
JW
5295
5296 /* Remove PPC reference. */
5297 ref = &po->ref;
5298 while ((*ref)->next)
63894de2 5299 ref = &(*ref)->next;
90661f26
JW
5300 gfc_free_ref_list (*ref);
5301 *ref = NULL;
5302
524af0d6 5303 if (!gfc_resolve_expr (po))
90661f26
JW
5304 return NULL;
5305
5306 return po;
5307}
5308
5309
5310/* Update the actual arglist of a procedure pointer component to include the
5311 passed-object. */
5312
524af0d6 5313static bool
90661f26
JW
5314update_ppc_arglist (gfc_expr* e)
5315{
5316 gfc_expr* po;
5317 gfc_component *ppc;
5318 gfc_typebound_proc* tb;
5319
2a573572
MM
5320 ppc = gfc_get_proc_ptr_comp (e);
5321 if (!ppc)
524af0d6 5322 return false;
90661f26
JW
5323
5324 tb = ppc->tb;
5325
5326 if (tb->error)
524af0d6 5327 return false;
90661f26 5328 else if (tb->nopass)
524af0d6 5329 return true;
90661f26
JW
5330
5331 po = extract_ppc_passed_object (e);
5332 if (!po)
524af0d6 5333 return false;
90661f26 5334
8b29bd22 5335 /* F08:R739. */
c62c6622 5336 if (po->rank != 0)
90661f26
JW
5337 {
5338 gfc_error ("Passed-object at %L must be scalar", &e->where);
524af0d6 5339 return false;
90661f26
JW
5340 }
5341
8b29bd22
JW
5342 /* F08:C611. */
5343 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5344 {
5345 gfc_error ("Base object for procedure-pointer component call at %L is of"
5346 " ABSTRACT type '%s'", &e->where, po->ts.u.derived->name);
524af0d6 5347 return false;
8b29bd22
JW
5348 }
5349
90661f26
JW
5350 gcc_assert (tb->pass_arg_num > 0);
5351 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5352 tb->pass_arg_num,
5353 tb->pass_arg);
8e1f752a 5354
524af0d6 5355 return true;
8e1f752a
DK
5356}
5357
5358
b0e5fa94
DK
5359/* Check that the object a TBP is called on is valid, i.e. it must not be
5360 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5361
524af0d6 5362static bool
b0e5fa94
DK
5363check_typebound_baseobject (gfc_expr* e)
5364{
5365 gfc_expr* base;
524af0d6 5366 bool return_value = false;
b0e5fa94
DK
5367
5368 base = extract_compcall_passed_object (e);
5369 if (!base)
524af0d6 5370 return false;
b0e5fa94 5371
cf2b3c22 5372 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
e56817db 5373
0b2d443b 5374 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
524af0d6 5375 return false;
0b2d443b 5376
8b29bd22 5377 /* F08:C611. */
e56817db 5378 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
b0e5fa94
DK
5379 {
5380 gfc_error ("Base object for type-bound procedure call at %L is of"
bc21d315 5381 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
99b41d52 5382 goto cleanup;
b0e5fa94
DK
5383 }
5384
8b29bd22
JW
5385 /* F08:C1230. If the procedure called is NOPASS,
5386 the base object must be scalar. */
c62c6622 5387 if (e->value.compcall.tbp->nopass && base->rank != 0)
41a394bb
DK
5388 {
5389 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5390 " be scalar", &e->where);
99b41d52 5391 goto cleanup;
41a394bb
DK
5392 }
5393
524af0d6 5394 return_value = true;
99b41d52
MM
5395
5396cleanup:
5397 gfc_free_expr (base);
5398 return return_value;
b0e5fa94
DK
5399}
5400
5401
8e1f752a
DK
5402/* Resolve a call to a type-bound procedure, either function or subroutine,
5403 statically from the data in an EXPR_COMPCALL expression. The adapted
5404 arglist and the target-procedure symtree are returned. */
5405
524af0d6 5406static bool
8e1f752a
DK
5407resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5408 gfc_actual_arglist** actual)
5409{
5410 gcc_assert (e->expr_type == EXPR_COMPCALL);
e157f736 5411 gcc_assert (!e->value.compcall.tbp->is_generic);
8e1f752a
DK
5412
5413 /* Update the actual arglist for PASS. */
524af0d6
JB
5414 if (!update_compcall_arglist (e))
5415 return false;
8e1f752a
DK
5416
5417 *actual = e->value.compcall.actual;
e157f736 5418 *target = e->value.compcall.tbp->u.specific;
8e1f752a
DK
5419
5420 gfc_free_ref_list (e->ref);
5421 e->ref = NULL;
5422 e->value.compcall.actual = NULL;
5423
003e0ad6 5424 /* If we find a deferred typebound procedure, check for derived types
e3a2ec56
TB
5425 that an overriding typebound procedure has not been missed. */
5426 if (e->value.compcall.name
5427 && !e->value.compcall.tbp->non_overridable
5428 && e->value.compcall.base_object
5429 && e->value.compcall.base_object->ts.type == BT_DERIVED)
003e0ad6
PT
5430 {
5431 gfc_symtree *st;
5432 gfc_symbol *derived;
5433
5434 /* Use the derived type of the base_object. */
5435 derived = e->value.compcall.base_object->ts.u.derived;
5436 st = NULL;
5437
eea58adb 5438 /* If necessary, go through the inheritance chain. */
003e0ad6
PT
5439 while (!st && derived)
5440 {
5441 /* Look for the typebound procedure 'name'. */
5442 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5443 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5444 e->value.compcall.name);
5445 if (!st)
5446 derived = gfc_get_derived_super_type (derived);
5447 }
5448
5449 /* Now find the specific name in the derived type namespace. */
5450 if (st && st->n.tb && st->n.tb->u.specific)
5451 gfc_find_sym_tree (st->n.tb->u.specific->name,
5452 derived->ns, 1, &st);
5453 if (st)
5454 *target = st;
5455 }
524af0d6 5456 return true;
8e1f752a
DK
5457}
5458
5459
15d774f9
PT
5460/* Get the ultimate declared type from an expression. In addition,
5461 return the last class/derived type reference and the copy of the
94fae14b
PT
5462 reference list. If check_types is set true, derived types are
5463 identified as well as class references. */
15d774f9
PT
5464static gfc_symbol*
5465get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
94fae14b 5466 gfc_expr *e, bool check_types)
15d774f9
PT
5467{
5468 gfc_symbol *declared;
5469 gfc_ref *ref;
5470
5471 declared = NULL;
5472 if (class_ref)
5473 *class_ref = NULL;
5474 if (new_ref)
5475 *new_ref = gfc_copy_ref (e->ref);
5476
5477 for (ref = e->ref; ref; ref = ref->next)
5478 {
5479 if (ref->type != REF_COMPONENT)
5480 continue;
5481
94fae14b
PT
5482 if ((ref->u.c.component->ts.type == BT_CLASS
5483 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5484 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
15d774f9
PT
5485 {
5486 declared = ref->u.c.component->ts.u.derived;
5487 if (class_ref)
5488 *class_ref = ref;
5489 }
5490 }
5491
5492 if (declared == NULL)
5493 declared = e->symtree->n.sym->ts.u.derived;
5494
5495 return declared;
5496}
5497
5498
e157f736
DK
5499/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5500 which of the specific bindings (if any) matches the arglist and transform
5501 the expression into a call of that binding. */
5502
524af0d6 5503static bool
eece1eb9 5504resolve_typebound_generic_call (gfc_expr* e, const char **name)
e157f736
DK
5505{
5506 gfc_typebound_proc* genproc;
5507 const char* genname;
15d774f9
PT
5508 gfc_symtree *st;
5509 gfc_symbol *derived;
e157f736
DK
5510
5511 gcc_assert (e->expr_type == EXPR_COMPCALL);
5512 genname = e->value.compcall.name;
5513 genproc = e->value.compcall.tbp;
5514
5515 if (!genproc->is_generic)
524af0d6 5516 return true;
e157f736
DK
5517
5518 /* Try the bindings on this type and in the inheritance hierarchy. */
5519 for (; genproc; genproc = genproc->overridden)
5520 {
5521 gfc_tbp_generic* g;
5522
5523 gcc_assert (genproc->is_generic);
5524 for (g = genproc->u.generic; g; g = g->next)
5525 {
5526 gfc_symbol* target;
5527 gfc_actual_arglist* args;
5528 bool matches;
5529
5530 gcc_assert (g->specific);
b82657f4
DK
5531
5532 if (g->specific->error)
5533 continue;
5534
e157f736
DK
5535 target = g->specific->u.specific->n.sym;
5536
5537 /* Get the right arglist by handling PASS/NOPASS. */
5538 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5539 if (!g->specific->nopass)
5540 {
5541 gfc_expr* po;
5542 po = extract_compcall_passed_object (e);
5543 if (!po)
efb63364
TB
5544 {
5545 gfc_free_actual_arglist (args);
524af0d6 5546 return false;
efb63364 5547 }
e157f736 5548
b82657f4
DK
5549 gcc_assert (g->specific->pass_arg_num > 0);
5550 gcc_assert (!g->specific->error);
90661f26
JW
5551 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5552 g->specific->pass_arg);
e157f736 5553 }
f0ac18b7 5554 resolve_actual_arglist (args, target->attr.proc,
4cbc9039
JW
5555 is_external_proc (target)
5556 && gfc_sym_get_dummy_args (target) == NULL);
e157f736
DK
5557
5558 /* Check if this arglist matches the formal. */
f0ac18b7 5559 matches = gfc_arglist_matches_symbol (&args, target);
e157f736
DK
5560
5561 /* Clean up and break out of the loop if we've found it. */
5562 gfc_free_actual_arglist (args);
5563 if (matches)
5564 {
5565 e->value.compcall.tbp = g->specific;
ab7306ed 5566 genname = g->specific_st->name;
eece1eb9
PT
5567 /* Pass along the name for CLASS methods, where the vtab
5568 procedure pointer component has to be referenced. */
5569 if (name)
ab7306ed 5570 *name = genname;
e157f736
DK
5571 goto success;
5572 }
5573 }
5574 }
5575
5576 /* Nothing matching found! */
5577 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5578 " '%s' at %L", genname, &e->where);
524af0d6 5579 return false;
e157f736
DK
5580
5581success:
15d774f9 5582 /* Make sure that we have the right specific instance for the name. */
94fae14b 5583 derived = get_declared_from_expr (NULL, NULL, e, true);
15d774f9 5584
12578be7 5585 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
15d774f9
PT
5586 if (st)
5587 e->value.compcall.tbp = st->n.tb;
5588
524af0d6 5589 return true;
e157f736
DK
5590}
5591
5592
8e1f752a
DK
5593/* Resolve a call to a type-bound subroutine. */
5594
524af0d6 5595static bool
eece1eb9 5596resolve_typebound_call (gfc_code* c, const char **name)
8e1f752a
DK
5597{
5598 gfc_actual_arglist* newactual;
5599 gfc_symtree* target;
5600
e157f736 5601 /* Check that's really a SUBROUTINE. */
a513927a 5602 if (!c->expr1->value.compcall.tbp->subroutine)
e157f736
DK
5603 {
5604 gfc_error ("'%s' at %L should be a SUBROUTINE",
a513927a 5605 c->expr1->value.compcall.name, &c->loc);
524af0d6 5606 return false;
e157f736
DK
5607 }
5608
524af0d6
JB
5609 if (!check_typebound_baseobject (c->expr1))
5610 return false;
b0e5fa94 5611
eece1eb9
PT
5612 /* Pass along the name for CLASS methods, where the vtab
5613 procedure pointer component has to be referenced. */
5614 if (name)
5615 *name = c->expr1->value.compcall.name;
5616
524af0d6
JB
5617 if (!resolve_typebound_generic_call (c->expr1, name))
5618 return false;
e157f736 5619
8e1f752a
DK
5620 /* Transform into an ordinary EXEC_CALL for now. */
5621
524af0d6
JB
5622 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5623 return false;
8e1f752a
DK
5624
5625 c->ext.actual = newactual;
5626 c->symtree = target;
4a44a72d 5627 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
8e1f752a 5628
a513927a 5629 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7cf078dc 5630
a513927a 5631 gfc_free_expr (c->expr1);
7cf078dc
PT
5632 c->expr1 = gfc_get_expr ();
5633 c->expr1->expr_type = EXPR_FUNCTION;
5634 c->expr1->symtree = target;
5635 c->expr1->where = c->loc;
8e1f752a
DK
5636
5637 return resolve_call (c);
5638}
5639
5640
eece1eb9 5641/* Resolve a component-call expression. */
524af0d6 5642static bool
eece1eb9 5643resolve_compcall (gfc_expr* e, const char **name)
8e1f752a
DK
5644{
5645 gfc_actual_arglist* newactual;
5646 gfc_symtree* target;
5647
e157f736 5648 /* Check that's really a FUNCTION. */
eece1eb9 5649 if (!e->value.compcall.tbp->function)
e157f736
DK
5650 {
5651 gfc_error ("'%s' at %L should be a FUNCTION",
5652 e->value.compcall.name, &e->where);
524af0d6 5653 return false;
e157f736
DK
5654 }
5655
4a44a72d
DK
5656 /* These must not be assign-calls! */
5657 gcc_assert (!e->value.compcall.assign);
5658
524af0d6
JB
5659 if (!check_typebound_baseobject (e))
5660 return false;
b0e5fa94 5661
eece1eb9
PT
5662 /* Pass along the name for CLASS methods, where the vtab
5663 procedure pointer component has to be referenced. */
5664 if (name)
5665 *name = e->value.compcall.name;
5666
524af0d6
JB
5667 if (!resolve_typebound_generic_call (e, name))
5668 return false;
00ca6640
DK
5669 gcc_assert (!e->value.compcall.tbp->is_generic);
5670
5671 /* Take the rank from the function's symbol. */
5672 if (e->value.compcall.tbp->u.specific->n.sym->as)
5673 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
e157f736
DK
5674
5675 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
8e1f752a
DK
5676 arglist to the TBP's binding target. */
5677
524af0d6
JB
5678 if (!resolve_typebound_static (e, &target, &newactual))
5679 return false;
8e1f752a
DK
5680
5681 e->value.function.actual = newactual;
b3d286ba 5682 e->value.function.name = NULL;
37a40b53 5683 e->value.function.esym = target->n.sym;
e157f736 5684 e->value.function.isym = NULL;
8e1f752a 5685 e->symtree = target;
f0ac18b7 5686 e->ts = target->n.sym->ts;
8e1f752a
DK
5687 e->expr_type = EXPR_FUNCTION;
5688
eece1eb9
PT
5689 /* Resolution is not necessary if this is a class subroutine; this
5690 function only has to identify the specific proc. Resolution of
5691 the call will be done next in resolve_typebound_call. */
5692 return gfc_resolve_expr (e);
28188747
PT
5693}
5694
5695
f0051264
JW
5696static bool resolve_fl_derived (gfc_symbol *sym);
5697
28188747 5698
eece1eb9
PT
5699/* Resolve a typebound function, or 'method'. First separate all
5700 the non-CLASS references by calling resolve_compcall directly. */
6a943ee7 5701
524af0d6 5702static bool
6a943ee7 5703resolve_typebound_function (gfc_expr* e)
7cf078dc 5704{
eece1eb9
PT
5705 gfc_symbol *declared;
5706 gfc_component *c;
28188747
PT
5707 gfc_ref *new_ref;
5708 gfc_ref *class_ref;
5709 gfc_symtree *st;
eece1eb9 5710 const char *name;
eece1eb9 5711 gfc_typespec ts;
974df0f8 5712 gfc_expr *expr;
fd83db3d 5713 bool overridable;
28188747
PT
5714
5715 st = e->symtree;
974df0f8
PT
5716
5717 /* Deal with typebound operators for CLASS objects. */
5718 expr = e->value.compcall.base_object;
fd83db3d 5719 overridable = !e->value.compcall.tbp->non_overridable;
061e60bd 5720 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
974df0f8 5721 {
94fae14b
PT
5722 /* If the base_object is not a variable, the corresponding actual
5723 argument expression must be stored in e->base_expression so
5724 that the corresponding tree temporary can be used as the base
5725 object in gfc_conv_procedure_call. */
5726 if (expr->expr_type != EXPR_VARIABLE)
5727 {
5728 gfc_actual_arglist *args;
5729
5730 for (args= e->value.function.actual; args; args = args->next)
5731 {
5732 if (expr == args->expr)
5733 expr = args->expr;
5734 }
5735 }
5736
974df0f8
PT
5737 /* Since the typebound operators are generic, we have to ensure
5738 that any delays in resolution are corrected and that the vtab
5739 is present. */
061e60bd 5740 ts = expr->ts;
974df0f8 5741 declared = ts.u.derived;
b04533af 5742 c = gfc_find_component (declared, "_vptr", true, true);
974df0f8
PT
5743 if (c->ts.u.derived == NULL)
5744 c->ts.u.derived = gfc_find_derived_vtab (declared);
5745
524af0d6
JB
5746 if (!resolve_compcall (e, &name))
5747 return false;
974df0f8
PT
5748
5749 /* Use the generic name if it is there. */
5750 name = name ? name : e->value.function.esym->name;
5751 e->symtree = expr->symtree;
d3735479 5752 e->ref = gfc_copy_ref (expr->ref);
94fae14b
PT
5753 get_declared_from_expr (&class_ref, NULL, e, false);
5754
5755 /* Trim away the extraneous references that emerge from nested
5756 use of interface.c (extend_expr). */
5757 if (class_ref && class_ref->next)
5758 {
5759 gfc_free_ref_list (class_ref->next);
5760 class_ref->next = NULL;
5761 }
5762 else if (e->ref && !class_ref)
5763 {
5764 gfc_free_ref_list (e->ref);
5765 e->ref = NULL;
5766 }
5767
b04533af 5768 gfc_add_vptr_component (e);
974df0f8
PT
5769 gfc_add_component_ref (e, name);
5770 e->value.function.esym = NULL;
94fae14b
PT
5771 if (expr->expr_type != EXPR_VARIABLE)
5772 e->base_expr = expr;
524af0d6 5773 return true;
974df0f8
PT
5774 }
5775
6a943ee7 5776 if (st == NULL)
eece1eb9 5777 return resolve_compcall (e, NULL);
7cf078dc 5778
524af0d6
JB
5779 if (!resolve_ref (e))
5780 return false;
f1a0b754 5781
28188747 5782 /* Get the CLASS declared type. */
94fae14b 5783 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
f0051264
JW
5784
5785 if (!resolve_fl_derived (declared))
5786 return false;
7cf078dc 5787
28188747 5788 /* Weed out cases of the ultimate component being a derived type. */
6a943ee7 5789 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
eece1eb9 5790 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
28188747
PT
5791 {
5792 gfc_free_ref_list (new_ref);
eece1eb9 5793 return resolve_compcall (e, NULL);
f116b2fc
PT
5794 }
5795
b04533af 5796 c = gfc_find_component (declared, "_data", true, true);
eece1eb9 5797 declared = c->ts.u.derived;
7cf078dc 5798
eece1eb9
PT
5799 /* Treat the call as if it is a typebound procedure, in order to roll
5800 out the correct name for the specific function. */
524af0d6 5801 if (!resolve_compcall (e, &name))
efb63364
TB
5802 {
5803 gfc_free_ref_list (new_ref);
524af0d6 5804 return false;
efb63364 5805 }
eece1eb9 5806 ts = e->ts;
7cf078dc 5807
fd83db3d
JW
5808 if (overridable)
5809 {
5810 /* Convert the expression to a procedure pointer component call. */
5811 e->value.function.esym = NULL;
5812 e->symtree = st;
7cf078dc 5813
4d382327 5814 if (new_ref)
fd83db3d 5815 e->ref = new_ref;
7cf078dc 5816
fd83db3d
JW
5817 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5818 gfc_add_vptr_component (e);
5819 gfc_add_component_ref (e, name);
5820
5821 /* Recover the typespec for the expression. This is really only
5822 necessary for generic procedures, where the additional call
5823 to gfc_add_component_ref seems to throw the collection of the
5824 correct typespec. */
5825 e->ts = ts;
5826 }
36abe895
TB
5827 else if (new_ref)
5828 gfc_free_ref_list (new_ref);
28188747 5829
524af0d6 5830 return true;
7cf078dc
PT
5831}
5832
eece1eb9
PT
5833/* Resolve a typebound subroutine, or 'method'. First separate all
5834 the non-CLASS references by calling resolve_typebound_call
5835 directly. */
6a943ee7 5836
524af0d6 5837static bool
6a943ee7 5838resolve_typebound_subroutine (gfc_code *code)
7cf078dc 5839{
974df0f8
PT
5840 gfc_symbol *declared;
5841 gfc_component *c;
28188747
PT
5842 gfc_ref *new_ref;
5843 gfc_ref *class_ref;
5844 gfc_symtree *st;
eece1eb9
PT
5845 const char *name;
5846 gfc_typespec ts;
974df0f8 5847 gfc_expr *expr;
fd83db3d 5848 bool overridable;
28188747
PT
5849
5850 st = code->expr1->symtree;
974df0f8
PT
5851
5852 /* Deal with typebound operators for CLASS objects. */
5853 expr = code->expr1->value.compcall.base_object;
fd83db3d 5854 overridable = !code->expr1->value.compcall.tbp->non_overridable;
b6c77bcb 5855 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
974df0f8 5856 {
94fae14b
PT
5857 /* If the base_object is not a variable, the corresponding actual
5858 argument expression must be stored in e->base_expression so
5859 that the corresponding tree temporary can be used as the base
5860 object in gfc_conv_procedure_call. */
5861 if (expr->expr_type != EXPR_VARIABLE)
5862 {
5863 gfc_actual_arglist *args;
5864
5865 args= code->expr1->value.function.actual;
5866 for (; args; args = args->next)
5867 if (expr == args->expr)
5868 expr = args->expr;
5869 }
5870
974df0f8
PT
5871 /* Since the typebound operators are generic, we have to ensure
5872 that any delays in resolution are corrected and that the vtab
5873 is present. */
b6c77bcb 5874 declared = expr->ts.u.derived;
b04533af 5875 c = gfc_find_component (declared, "_vptr", true, true);
974df0f8
PT
5876 if (c->ts.u.derived == NULL)
5877 c->ts.u.derived = gfc_find_derived_vtab (declared);
5878
524af0d6
JB
5879 if (!resolve_typebound_call (code, &name))
5880 return false;
974df0f8
PT
5881
5882 /* Use the generic name if it is there. */
5883 name = name ? name : code->expr1->value.function.esym->name;
5884 code->expr1->symtree = expr->symtree;
b6c77bcb 5885 code->expr1->ref = gfc_copy_ref (expr->ref);
94fae14b
PT
5886
5887 /* Trim away the extraneous references that emerge from nested
5888 use of interface.c (extend_expr). */
5889 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
5890 if (class_ref && class_ref->next)
5891 {
5892 gfc_free_ref_list (class_ref->next);
5893 class_ref->next = NULL;
5894 }
5895 else if (code->expr1->ref && !class_ref)
5896 {
5897 gfc_free_ref_list (code->expr1->ref);
5898 code->expr1->ref = NULL;
5899 }
5900
5901 /* Now use the procedure in the vtable. */
b04533af 5902 gfc_add_vptr_component (code->expr1);
974df0f8
PT
5903 gfc_add_component_ref (code->expr1, name);
5904 code->expr1->value.function.esym = NULL;
94fae14b
PT
5905 if (expr->expr_type != EXPR_VARIABLE)
5906 code->expr1->base_expr = expr;
524af0d6 5907 return true;
974df0f8
PT
5908 }
5909
6a943ee7 5910 if (st == NULL)
eece1eb9 5911 return resolve_typebound_call (code, NULL);
7cf078dc 5912
524af0d6
JB
5913 if (!resolve_ref (code->expr1))
5914 return false;
f1a0b754 5915
28188747 5916 /* Get the CLASS declared type. */
94fae14b 5917 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7cf078dc 5918
28188747 5919 /* Weed out cases of the ultimate component being a derived type. */
6a943ee7 5920 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
eece1eb9 5921 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
28188747
PT
5922 {
5923 gfc_free_ref_list (new_ref);
eece1eb9 5924 return resolve_typebound_call (code, NULL);
ab7306ed 5925 }
7cf078dc 5926
524af0d6 5927 if (!resolve_typebound_call (code, &name))
efb63364
TB
5928 {
5929 gfc_free_ref_list (new_ref);
524af0d6 5930 return false;
efb63364 5931 }
eece1eb9 5932 ts = code->expr1->ts;
7cf078dc 5933
fd83db3d
JW
5934 if (overridable)
5935 {
5936 /* Convert the expression to a procedure pointer component call. */
5937 code->expr1->value.function.esym = NULL;
5938 code->expr1->symtree = st;
7cf078dc 5939
fd83db3d
JW
5940 if (new_ref)
5941 code->expr1->ref = new_ref;
5942
5943 /* '_vptr' points to the vtab, which contains the procedure pointers. */
5944 gfc_add_vptr_component (code->expr1);
5945 gfc_add_component_ref (code->expr1, name);
7cf078dc 5946
fd83db3d
JW
5947 /* Recover the typespec for the expression. This is really only
5948 necessary for generic procedures, where the additional call
5949 to gfc_add_component_ref seems to throw the collection of the
5950 correct typespec. */
5951 code->expr1->ts = ts;
5952 }
adede54c
TB
5953 else if (new_ref)
5954 gfc_free_ref_list (new_ref);
28188747 5955
524af0d6 5956 return true;
8e1f752a
DK
5957}
5958
5959
713485cc
JW
5960/* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5961
524af0d6 5962static bool
713485cc
JW
5963resolve_ppc_call (gfc_code* c)
5964{
5965 gfc_component *comp;
cf2b3c22 5966
2a573572
MM
5967 comp = gfc_get_proc_ptr_comp (c->expr1);
5968 gcc_assert (comp != NULL);
713485cc 5969
a513927a
SK
5970 c->resolved_sym = c->expr1->symtree->n.sym;
5971 c->expr1->expr_type = EXPR_VARIABLE;
713485cc
JW
5972
5973 if (!comp->attr.subroutine)
a513927a 5974 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
713485cc 5975
524af0d6
JB
5976 if (!resolve_ref (c->expr1))
5977 return false;
e35bbb23 5978
524af0d6
JB
5979 if (!update_ppc_arglist (c->expr1))
5980 return false;
90661f26
JW
5981
5982 c->ext.actual = c->expr1->value.compcall.actual;
5983
524af0d6
JB
5984 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5985 !(comp->ts.interface
5986 && comp->ts.interface->formal)))
5987 return false;
713485cc 5988
7e196f89 5989 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
713485cc 5990
524af0d6 5991 return true;
713485cc
JW
5992}
5993
5994
5995/* Resolve a Function Call to a Procedure Pointer Component (Function). */
5996
524af0d6 5997static bool
713485cc
JW
5998resolve_expr_ppc (gfc_expr* e)
5999{
6000 gfc_component *comp;
cf2b3c22 6001
2a573572
MM
6002 comp = gfc_get_proc_ptr_comp (e);
6003 gcc_assert (comp != NULL);
713485cc
JW
6004
6005 /* Convert to EXPR_FUNCTION. */
6006 e->expr_type = EXPR_FUNCTION;
6007 e->value.function.isym = NULL;
6008 e->value.function.actual = e->value.compcall.actual;
6009 e->ts = comp->ts;
c74b74a8
JW
6010 if (comp->as != NULL)
6011 e->rank = comp->as->rank;
713485cc
JW
6012
6013 if (!comp->attr.function)
6014 gfc_add_function (&comp->attr, comp->name, &e->where);
6015
524af0d6
JB
6016 if (!resolve_ref (e))
6017 return false;
e35bbb23 6018
524af0d6
JB
6019 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6020 !(comp->ts.interface
6021 && comp->ts.interface->formal)))
6022 return false;
713485cc 6023
524af0d6
JB
6024 if (!update_ppc_arglist (e))
6025 return false;
90661f26 6026
7e196f89 6027 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
713485cc 6028
524af0d6 6029 return true;
713485cc
JW
6030}
6031
6032
f2ff577a
JD
6033static bool
6034gfc_is_expandable_expr (gfc_expr *e)
6035{
6036 gfc_constructor *con;
6037
6038 if (e->expr_type == EXPR_ARRAY)
6039 {
6040 /* Traverse the constructor looking for variables that are flavor
6041 parameter. Parameters must be expanded since they are fully used at
6042 compile time. */
b7e75771
JD
6043 con = gfc_constructor_first (e->value.constructor);
6044 for (; con; con = gfc_constructor_next (con))
f2ff577a
JD
6045 {
6046 if (con->expr->expr_type == EXPR_VARIABLE
b7e75771
JD
6047 && con->expr->symtree
6048 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
f2ff577a
JD
6049 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6050 return true;
6051 if (con->expr->expr_type == EXPR_ARRAY
b7e75771 6052 && gfc_is_expandable_expr (con->expr))
f2ff577a
JD
6053 return true;
6054 }
6055 }
6056
6057 return false;
6058}
6059
6de9cd9a
DN
6060/* Resolve an expression. That is, make sure that types of operands agree
6061 with their operators, intrinsic operators are converted to function calls
6062 for overloaded types and unresolved function references are resolved. */
6063
524af0d6 6064bool
edf1eac2 6065gfc_resolve_expr (gfc_expr *e)
6de9cd9a 6066{
524af0d6 6067 bool t;
c62c6622 6068 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6de9cd9a
DN
6069
6070 if (e == NULL)
524af0d6 6071 return true;
6de9cd9a 6072
d3a9eea2
TB
6073 /* inquiry_argument only applies to variables. */
6074 inquiry_save = inquiry_argument;
c62c6622
TB
6075 actual_arg_save = actual_arg;
6076 first_actual_arg_save = first_actual_arg;
6077
d3a9eea2 6078 if (e->expr_type != EXPR_VARIABLE)
c62c6622
TB
6079 {
6080 inquiry_argument = false;
6081 actual_arg = false;
6082 first_actual_arg = false;
6083 }
d3a9eea2 6084
6de9cd9a
DN
6085 switch (e->expr_type)
6086 {
6087 case EXPR_OP:
6088 t = resolve_operator (e);
6089 break;
6090
6091 case EXPR_FUNCTION:
6de9cd9a 6092 case EXPR_VARIABLE:
eb77cddf
PT
6093
6094 if (check_host_association (e))
6095 t = resolve_function (e);
6096 else
6097 {
6098 t = resolve_variable (e);
524af0d6 6099 if (t)
eb77cddf
PT
6100 expression_rank (e);
6101 }
07368af0 6102
bc21d315 6103 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
9de88093 6104 && e->ref->type != REF_SUBSTRING)
07368af0
PT
6105 gfc_resolve_substring_charlen (e);
6106
6de9cd9a
DN
6107 break;
6108
8e1f752a 6109 case EXPR_COMPCALL:
6a943ee7 6110 t = resolve_typebound_function (e);
8e1f752a
DK
6111 break;
6112
6de9cd9a
DN
6113 case EXPR_SUBSTRING:
6114 t = resolve_ref (e);
6115 break;
6116
6117 case EXPR_CONSTANT:
6118 case EXPR_NULL:
524af0d6 6119 t = true;
6de9cd9a
DN
6120 break;
6121
713485cc
JW
6122 case EXPR_PPC:
6123 t = resolve_expr_ppc (e);
6124 break;
6125
6de9cd9a 6126 case EXPR_ARRAY:
524af0d6
JB
6127 t = false;
6128 if (!resolve_ref (e))
6de9cd9a
DN
6129 break;
6130
6131 t = gfc_resolve_array_constructor (e);
6132 /* Also try to expand a constructor. */
524af0d6 6133 if (t)
6de9cd9a
DN
6134 {
6135 expression_rank (e);
f2ff577a 6136 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
928f0490 6137 gfc_expand_constructor (e, false);
6de9cd9a 6138 }
1855915a 6139
edf1eac2 6140 /* This provides the opportunity for the length of constructors with
86bf520d 6141 character valued function elements to propagate the string length
edf1eac2 6142 to the expression. */
524af0d6 6143 if (t && e->ts.type == BT_CHARACTER)
f2ff577a
JD
6144 {
6145 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
4d382327 6146 here rather then add a duplicate test for it above. */
928f0490 6147 gfc_expand_constructor (e, false);
f2ff577a
JD
6148 t = gfc_resolve_character_array_constructor (e);
6149 }
6de9cd9a
DN
6150
6151 break;
6152
6153 case EXPR_STRUCTURE:
6154 t = resolve_ref (e);
524af0d6 6155 if (!t)
6de9cd9a
DN
6156 break;
6157
80f95228 6158 t = resolve_structure_cons (e, 0);
524af0d6 6159 if (!t)
6de9cd9a
DN
6160 break;
6161
6162 t = gfc_simplify_expr (e, 0);
6163 break;
6164
6165 default:
6166 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6167 }
6168
524af0d6 6169 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
07368af0
PT
6170 fixup_charlen (e);
6171
d3a9eea2 6172 inquiry_argument = inquiry_save;
c62c6622
TB
6173 actual_arg = actual_arg_save;
6174 first_actual_arg = first_actual_arg_save;
d3a9eea2 6175
6de9cd9a
DN
6176 return t;
6177}
6178
6179
8d5cfa27
SK
6180/* Resolve an expression from an iterator. They must be scalar and have
6181 INTEGER or (optionally) REAL type. */
6de9cd9a 6182
524af0d6 6183static bool
edf1eac2
SK
6184gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6185 const char *name_msgid)
6de9cd9a 6186{
524af0d6
JB
6187 if (!gfc_resolve_expr (expr))
6188 return false;
6de9cd9a 6189
8d5cfa27 6190 if (expr->rank != 0)
6de9cd9a 6191 {
31043f6c 6192 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
524af0d6 6193 return false;
6de9cd9a
DN
6194 }
6195
79e7840d 6196 if (expr->ts.type != BT_INTEGER)
6de9cd9a 6197 {
79e7840d
JD
6198 if (expr->ts.type == BT_REAL)
6199 {
6200 if (real_ok)
6201 return gfc_notify_std (GFC_STD_F95_DEL,
9717f7a1 6202 "%s at %L must be integer",
79e7840d
JD
6203 _(name_msgid), &expr->where);
6204 else
6205 {
6206 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6207 &expr->where);
524af0d6 6208 return false;
79e7840d
JD
6209 }
6210 }
31043f6c 6211 else
79e7840d
JD
6212 {
6213 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
524af0d6 6214 return false;
79e7840d 6215 }
6de9cd9a 6216 }
524af0d6 6217 return true;
8d5cfa27
SK
6218}
6219
6220
6221/* Resolve the expressions in an iterator structure. If REAL_OK is
57bf28ea
TB
6222 false allow only INTEGER type iterators, otherwise allow REAL types.
6223 Set own_scope to true for ac-implied-do and data-implied-do as those
6224 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8d5cfa27 6225
524af0d6 6226bool
57bf28ea 6227gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8d5cfa27 6228{
524af0d6
JB
6229 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6230 return false;
6de9cd9a 6231
524af0d6
JB
6232 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
6233 _("iterator variable")))
6234 return false;
6de9cd9a 6235
524af0d6
JB
6236 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
6237 "Start expression in DO loop"))
6238 return false;
6de9cd9a 6239
524af0d6
JB
6240 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
6241 "End expression in DO loop"))
6242 return false;
6de9cd9a 6243
524af0d6
JB
6244 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
6245 "Step expression in DO loop"))
6246 return false;
6de9cd9a 6247
8d5cfa27 6248 if (iter->step->expr_type == EXPR_CONSTANT)
6de9cd9a 6249 {
8d5cfa27
SK
6250 if ((iter->step->ts.type == BT_INTEGER
6251 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6252 || (iter->step->ts.type == BT_REAL
6253 && mpfr_sgn (iter->step->value.real) == 0))
6254 {
6255 gfc_error ("Step expression in DO loop at %L cannot be zero",
6256 &iter->step->where);
524af0d6 6257 return false;
8d5cfa27 6258 }
6de9cd9a
DN
6259 }
6260
8d5cfa27
SK
6261 /* Convert start, end, and step to the same type as var. */
6262 if (iter->start->ts.kind != iter->var->ts.kind
6263 || iter->start->ts.type != iter->var->ts.type)
6264 gfc_convert_type (iter->start, &iter->var->ts, 2);
6265
6266 if (iter->end->ts.kind != iter->var->ts.kind
6267 || iter->end->ts.type != iter->var->ts.type)
6268 gfc_convert_type (iter->end, &iter->var->ts, 2);
6269
6270 if (iter->step->ts.kind != iter->var->ts.kind
6271 || iter->step->ts.type != iter->var->ts.type)
6272 gfc_convert_type (iter->step, &iter->var->ts, 2);
6de9cd9a 6273
dc186969
TB
6274 if (iter->start->expr_type == EXPR_CONSTANT
6275 && iter->end->expr_type == EXPR_CONSTANT
6276 && iter->step->expr_type == EXPR_CONSTANT)
6277 {
6278 int sgn, cmp;
6279 if (iter->start->ts.type == BT_INTEGER)
6280 {
6281 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6282 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6283 }
6284 else
6285 {
6286 sgn = mpfr_sgn (iter->step->value.real);
6287 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6288 }
0e533e50
TK
6289 if (gfc_option.warn_zerotrip &&
6290 ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
6291 gfc_warning ("DO loop at %L will be executed zero times"
6292 " (use -Wno-zerotrip to suppress)",
dc186969
TB
6293 &iter->step->where);
6294 }
6295
524af0d6 6296 return true;
6de9cd9a
DN
6297}
6298
6299
640670c7
PT
6300/* Traversal function for find_forall_index. f == 2 signals that
6301 that variable itself is not to be checked - only the references. */
ac5ba373 6302
640670c7
PT
6303static bool
6304forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
ac5ba373 6305{
908a2235
PT
6306 if (expr->expr_type != EXPR_VARIABLE)
6307 return false;
4d382327 6308
640670c7
PT
6309 /* A scalar assignment */
6310 if (!expr->ref || *f == 1)
ac5ba373 6311 {
640670c7
PT
6312 if (expr->symtree->n.sym == sym)
6313 return true;
6314 else
6315 return false;
6316 }
ac5ba373 6317
640670c7
PT
6318 if (*f == 2)
6319 *f = 1;
6320 return false;
6321}
ac5ba373 6322
ac5ba373 6323
640670c7 6324/* Check whether the FORALL index appears in the expression or not.
524af0d6 6325 Returns true if SYM is found in EXPR. */
ac5ba373 6326
524af0d6 6327bool
640670c7
PT
6328find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6329{
6330 if (gfc_traverse_expr (expr, sym, forall_index, f))
524af0d6 6331 return true;
640670c7 6332 else
524af0d6 6333 return false;
ac5ba373
TS
6334}
6335
6336
1c54741a
SK
6337/* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6338 to be a scalar INTEGER variable. The subscripts and stride are scalar
ac5ba373
TS
6339 INTEGERs, and if stride is a constant it must be nonzero.
6340 Furthermore "A subscript or stride in a forall-triplet-spec shall
6341 not contain a reference to any index-name in the
6342 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6de9cd9a
DN
6343
6344static void
ac5ba373 6345resolve_forall_iterators (gfc_forall_iterator *it)
6de9cd9a 6346{
ac5ba373
TS
6347 gfc_forall_iterator *iter, *iter2;
6348
6349 for (iter = it; iter; iter = iter->next)
6de9cd9a 6350 {
524af0d6 6351 if (gfc_resolve_expr (iter->var)
1c54741a
SK
6352 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6353 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6de9cd9a
DN
6354 &iter->var->where);
6355
524af0d6 6356 if (gfc_resolve_expr (iter->start)
1c54741a
SK
6357 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6358 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6de9cd9a
DN
6359 &iter->start->where);
6360 if (iter->var->ts.kind != iter->start->ts.kind)
7298eef3 6361 gfc_convert_type (iter->start, &iter->var->ts, 1);
6de9cd9a 6362
524af0d6 6363 if (gfc_resolve_expr (iter->end)
1c54741a
SK
6364 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6365 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6de9cd9a
DN
6366 &iter->end->where);
6367 if (iter->var->ts.kind != iter->end->ts.kind)
7298eef3 6368 gfc_convert_type (iter->end, &iter->var->ts, 1);
6de9cd9a 6369
524af0d6 6370 if (gfc_resolve_expr (iter->stride))
1c54741a
SK
6371 {
6372 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6373 gfc_error ("FORALL stride expression at %L must be a scalar %s",
edf1eac2 6374 &iter->stride->where, "INTEGER");
1c54741a
SK
6375
6376 if (iter->stride->expr_type == EXPR_CONSTANT
524af0d6 6377 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
1c54741a
SK
6378 gfc_error ("FORALL stride expression at %L cannot be zero",
6379 &iter->stride->where);
6380 }
6de9cd9a 6381 if (iter->var->ts.kind != iter->stride->ts.kind)
7298eef3 6382 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6de9cd9a 6383 }
ac5ba373
TS
6384
6385 for (iter = it; iter; iter = iter->next)
6386 for (iter2 = iter; iter2; iter2 = iter2->next)
6387 {
524af0d6
JB
6388 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6389 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6390 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
ac5ba373
TS
6391 gfc_error ("FORALL index '%s' may not appear in triplet "
6392 "specification at %L", iter->var->symtree->name,
6393 &iter2->start->where);
6394 }
6de9cd9a
DN
6395}
6396
6397
8451584a
EE
6398/* Given a pointer to a symbol that is a derived type, see if it's
6399 inaccessible, i.e. if it's defined in another module and the components are
6400 PRIVATE. The search is recursive if necessary. Returns zero if no
6401 inaccessible components are found, nonzero otherwise. */
6402
6403static int
6404derived_inaccessible (gfc_symbol *sym)
6405{
6406 gfc_component *c;
6407
3dbf6538 6408 if (sym->attr.use_assoc && sym->attr.private_comp)
8451584a
EE
6409 return 1;
6410
6411 for (c = sym->components; c; c = c->next)
6412 {
bc21d315 6413 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
edf1eac2 6414 return 1;
8451584a
EE
6415 }
6416
6417 return 0;
6418}
6419
6420
6de9cd9a
DN
6421/* Resolve the argument of a deallocate expression. The expression must be
6422 a pointer or a full array. */
6423
524af0d6 6424static bool
edf1eac2 6425resolve_deallocate_expr (gfc_expr *e)
6de9cd9a
DN
6426{
6427 symbol_attribute attr;
8c91ab34 6428 int allocatable, pointer;
6de9cd9a 6429 gfc_ref *ref;
cf2b3c22
TB
6430 gfc_symbol *sym;
6431 gfc_component *c;
8b704316 6432 bool unlimited;
6de9cd9a 6433
524af0d6
JB
6434 if (!gfc_resolve_expr (e))
6435 return false;
6de9cd9a 6436
6de9cd9a
DN
6437 if (e->expr_type != EXPR_VARIABLE)
6438 goto bad;
6439
cf2b3c22 6440 sym = e->symtree->n.sym;
8b704316 6441 unlimited = UNLIMITED_POLY(sym);
cf2b3c22
TB
6442
6443 if (sym->ts.type == BT_CLASS)
6444 {
7a08eda1 6445 allocatable = CLASS_DATA (sym)->attr.allocatable;
d40477b4 6446 pointer = CLASS_DATA (sym)->attr.class_pointer;
cf2b3c22
TB
6447 }
6448 else
6449 {
6450 allocatable = sym->attr.allocatable;
6451 pointer = sym->attr.pointer;
6452 }
6de9cd9a 6453 for (ref = e->ref; ref; ref = ref->next)
f17facac 6454 {
f17facac 6455 switch (ref->type)
edf1eac2
SK
6456 {
6457 case REF_ARRAY:
badd9e69
TB
6458 if (ref->u.ar.type != AR_FULL
6459 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6460 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
f17facac
TB
6461 allocatable = 0;
6462 break;
6de9cd9a 6463
edf1eac2 6464 case REF_COMPONENT:
cf2b3c22
TB
6465 c = ref->u.c.component;
6466 if (c->ts.type == BT_CLASS)
6467 {
7a08eda1 6468 allocatable = CLASS_DATA (c)->attr.allocatable;
d40477b4 6469 pointer = CLASS_DATA (c)->attr.class_pointer;
cf2b3c22
TB
6470 }
6471 else
6472 {
6473 allocatable = c->attr.allocatable;
6474 pointer = c->attr.pointer;
6475 }
f17facac 6476 break;
6de9cd9a 6477
edf1eac2 6478 case REF_SUBSTRING:
f17facac
TB
6479 allocatable = 0;
6480 break;
edf1eac2 6481 }
f17facac
TB
6482 }
6483
6484 attr = gfc_expr_attr (e);
6485
8b704316 6486 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6de9cd9a
DN
6487 {
6488 bad:
3759634f
SK
6489 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6490 &e->where);
524af0d6 6491 return false;
6de9cd9a
DN
6492 }
6493
5aacb11e
TB
6494 /* F2008, C644. */
6495 if (gfc_is_coindexed (e))
6496 {
6497 gfc_error ("Coindexed allocatable object at %L", &e->where);
524af0d6 6498 return false;
5aacb11e
TB
6499 }
6500
8c91ab34 6501 if (pointer
524af0d6
JB
6502 && !gfc_check_vardef_context (e, true, true, false,
6503 _("DEALLOCATE object")))
6504 return false;
6505 if (!gfc_check_vardef_context (e, false, true, false,
6506 _("DEALLOCATE object")))
6507 return false;
aa08038d 6508
524af0d6 6509 return true;
6de9cd9a
DN
6510}
6511
edf1eac2 6512
908a2235 6513/* Returns true if the expression e contains a reference to the symbol sym. */
77726571 6514static bool
908a2235 6515sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
77726571 6516{
908a2235
PT
6517 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6518 return true;
77726571 6519
908a2235
PT
6520 return false;
6521}
77726571 6522
a68ab351
JJ
6523bool
6524gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
908a2235
PT
6525{
6526 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
77726571
PT
6527}
6528
6de9cd9a 6529
68577e56
EE
6530/* Given the expression node e for an allocatable/pointer of derived type to be
6531 allocated, get the expression node to be initialized afterwards (needed for
5046aff5
PT
6532 derived types with default initializers, and derived types with allocatable
6533 components that need nullification.) */
68577e56 6534
cf2b3c22
TB
6535gfc_expr *
6536gfc_expr_to_initialize (gfc_expr *e)
68577e56
EE
6537{
6538 gfc_expr *result;
6539 gfc_ref *ref;
6540 int i;
6541
6542 result = gfc_copy_expr (e);
6543
6544 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6545 for (ref = result->ref; ref; ref = ref->next)
6546 if (ref->type == REF_ARRAY && ref->next == NULL)
6547 {
edf1eac2 6548 ref->u.ar.type = AR_FULL;
68577e56 6549
edf1eac2
SK
6550 for (i = 0; i < ref->u.ar.dimen; i++)
6551 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
68577e56 6552
edf1eac2 6553 break;
68577e56
EE
6554 }
6555
7d7212ec
MM
6556 gfc_free_shape (&result->shape, result->rank);
6557
6558 /* Recalculate rank, shape, etc. */
6559 gfc_resolve_expr (result);
68577e56
EE
6560 return result;
6561}
6562
6563
8c91ab34
DK
6564/* If the last ref of an expression is an array ref, return a copy of the
6565 expression with that one removed. Otherwise, a copy of the original
6566 expression. This is used for allocate-expressions and pointer assignment
6567 LHS, where there may be an array specification that needs to be stripped
6568 off when using gfc_check_vardef_context. */
6569
6570static gfc_expr*
6571remove_last_array_ref (gfc_expr* e)
6572{
6573 gfc_expr* e2;
6574 gfc_ref** r;
6575
6576 e2 = gfc_copy_expr (e);
6577 for (r = &e2->ref; *r; r = &(*r)->next)
6578 if ((*r)->type == REF_ARRAY && !(*r)->next)
6579 {
6580 gfc_free_ref_list (*r);
6581 *r = NULL;
6582 break;
6583 }
6584
6585 return e2;
6586}
6587
6588
8460475b 6589/* Used in resolve_allocate_expr to check that a allocation-object and
4d382327 6590 a source-expr are conformable. This does not catch all possible
8460475b
JW
6591 cases; in particular a runtime checking is needed. */
6592
524af0d6 6593static bool
8460475b
JW
6594conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6595{
66051b60
JW
6596 gfc_ref *tail;
6597 for (tail = e2->ref; tail && tail->next; tail = tail->next);
4d382327 6598
8460475b 6599 /* First compare rank. */
2ccd6f72
JW
6600 if ((tail && e1->rank != tail->u.ar.as->rank)
6601 || (!tail && e1->rank != e2->rank))
8460475b
JW
6602 {
6603 gfc_error ("Source-expr at %L must be scalar or have the "
6604 "same rank as the allocate-object at %L",
6605 &e1->where, &e2->where);
524af0d6 6606 return false;
8460475b
JW
6607 }
6608
6609 if (e1->shape)
6610 {
6611 int i;
6612 mpz_t s;
6613
6614 mpz_init (s);
6615
6616 for (i = 0; i < e1->rank; i++)
6617 {
f0470cc5
TB
6618 if (tail->u.ar.start[i] == NULL)
6619 break;
6620
66051b60 6621 if (tail->u.ar.end[i])
8460475b 6622 {
66051b60
JW
6623 mpz_set (s, tail->u.ar.end[i]->value.integer);
6624 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8460475b
JW
6625 mpz_add_ui (s, s, 1);
6626 }
6627 else
6628 {
66051b60 6629 mpz_set (s, tail->u.ar.start[i]->value.integer);
8460475b
JW
6630 }
6631
6632 if (mpz_cmp (e1->shape[i], s) != 0)
6633 {
6634 gfc_error ("Source-expr at %L and allocate-object at %L must "
6635 "have the same shape", &e1->where, &e2->where);
6636 mpz_clear (s);
524af0d6 6637 return false;
8460475b
JW
6638 }
6639 }
6640
6641 mpz_clear (s);
6642 }
6643
524af0d6 6644 return true;
8460475b
JW
6645}
6646
6647
6de9cd9a
DN
6648/* Resolve the expression in an ALLOCATE statement, doing the additional
6649 checks to see whether the expression is OK or not. The expression must
6650 have a trailing array reference that gives the size of the array. */
6651
524af0d6 6652static bool
edf1eac2 6653resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6de9cd9a 6654{
8c91ab34 6655 int i, pointer, allocatable, dimension, is_abstract;
d3a9eea2 6656 int codimension;
c49eaa23 6657 bool coindexed;
8b704316 6658 bool unlimited;
6de9cd9a
DN
6659 symbol_attribute attr;
6660 gfc_ref *ref, *ref2;
8c91ab34 6661 gfc_expr *e2;
6de9cd9a 6662 gfc_array_ref *ar;
0d7d4951 6663 gfc_symbol *sym = NULL;
77726571 6664 gfc_alloc *a;
cf2b3c22 6665 gfc_component *c;
524af0d6 6666 bool t;
f17facac 6667
eea58adb 6668 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
d3a9eea2
TB
6669 checking of coarrays. */
6670 for (ref = e->ref; ref; ref = ref->next)
6671 if (ref->next == NULL)
6672 break;
6673
6674 if (ref && ref->type == REF_ARRAY)
6675 ref->u.ar.in_allocate = true;
6676
524af0d6 6677 if (!gfc_resolve_expr (e))
d3a9eea2 6678 goto failure;
6de9cd9a
DN
6679
6680 /* Make sure the expression is allocatable or a pointer. If it is
6681 pointer, the next-to-last reference must be a pointer. */
6682
6683 ref2 = NULL;
cf2b3c22
TB
6684 if (e->symtree)
6685 sym = e->symtree->n.sym;
6de9cd9a 6686
d0a9804e
TB
6687 /* Check whether ultimate component is abstract and CLASS. */
6688 is_abstract = 0;
6689
8b704316
PT
6690 /* Is the allocate-object unlimited polymorphic? */
6691 unlimited = UNLIMITED_POLY(e);
6692
6de9cd9a
DN
6693 if (e->expr_type != EXPR_VARIABLE)
6694 {
6695 allocatable = 0;
6de9cd9a
DN
6696 attr = gfc_expr_attr (e);
6697 pointer = attr.pointer;
6698 dimension = attr.dimension;
d3a9eea2 6699 codimension = attr.codimension;
6de9cd9a
DN
6700 }
6701 else
6702 {
c49ea23d 6703 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
cf2b3c22 6704 {
7a08eda1 6705 allocatable = CLASS_DATA (sym)->attr.allocatable;
d40477b4 6706 pointer = CLASS_DATA (sym)->attr.class_pointer;
7a08eda1
JW
6707 dimension = CLASS_DATA (sym)->attr.dimension;
6708 codimension = CLASS_DATA (sym)->attr.codimension;
6709 is_abstract = CLASS_DATA (sym)->attr.abstract;
cf2b3c22
TB
6710 }
6711 else
6712 {
6713 allocatable = sym->attr.allocatable;
6714 pointer = sym->attr.pointer;
6715 dimension = sym->attr.dimension;
d3a9eea2 6716 codimension = sym->attr.codimension;
cf2b3c22 6717 }
6de9cd9a 6718
c49eaa23
TB
6719 coindexed = false;
6720
6de9cd9a 6721 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
edf1eac2 6722 {
f17facac
TB
6723 switch (ref->type)
6724 {
6725 case REF_ARRAY:
c49eaa23
TB
6726 if (ref->u.ar.codimen > 0)
6727 {
6728 int n;
6729 for (n = ref->u.ar.dimen;
6730 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6731 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6732 {
6733 coindexed = true;
6734 break;
6735 }
6736 }
6737
edf1eac2
SK
6738 if (ref->next != NULL)
6739 pointer = 0;
6740 break;
f17facac
TB
6741
6742 case REF_COMPONENT:
d3a9eea2 6743 /* F2008, C644. */
c49eaa23 6744 if (coindexed)
d3a9eea2
TB
6745 {
6746 gfc_error ("Coindexed allocatable object at %L",
6747 &e->where);
6748 goto failure;
6749 }
6750
cf2b3c22
TB
6751 c = ref->u.c.component;
6752 if (c->ts.type == BT_CLASS)
6753 {
7a08eda1 6754 allocatable = CLASS_DATA (c)->attr.allocatable;
d40477b4 6755 pointer = CLASS_DATA (c)->attr.class_pointer;
7a08eda1
JW
6756 dimension = CLASS_DATA (c)->attr.dimension;
6757 codimension = CLASS_DATA (c)->attr.codimension;
6758 is_abstract = CLASS_DATA (c)->attr.abstract;
cf2b3c22
TB
6759 }
6760 else
6761 {
6762 allocatable = c->attr.allocatable;
6763 pointer = c->attr.pointer;
6764 dimension = c->attr.dimension;
d3a9eea2 6765 codimension = c->attr.codimension;
d0a9804e 6766 is_abstract = c->attr.abstract;
cf2b3c22 6767 }
edf1eac2 6768 break;
f17facac
TB
6769
6770 case REF_SUBSTRING:
edf1eac2
SK
6771 allocatable = 0;
6772 pointer = 0;
6773 break;
f17facac 6774 }
8e1f752a 6775 }
6de9cd9a
DN
6776 }
6777
98cf47d1 6778 /* Check for F08:C628. */
8b704316 6779 if (allocatable == 0 && pointer == 0 && !unlimited)
6de9cd9a 6780 {
3759634f
SK
6781 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6782 &e->where);
d3a9eea2 6783 goto failure;
6de9cd9a
DN
6784 }
6785
8460475b
JW
6786 /* Some checks for the SOURCE tag. */
6787 if (code->expr3)
6788 {
6789 /* Check F03:C631. */
6790 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6791 {
6792 gfc_error ("Type of entity at %L is type incompatible with "
6793 "source-expr at %L", &e->where, &code->expr3->where);
d3a9eea2 6794 goto failure;
8460475b
JW
6795 }
6796
6797 /* Check F03:C632 and restriction following Note 6.18. */
2ccd6f72 6798 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
d3a9eea2 6799 goto failure;
8460475b
JW
6800
6801 /* Check F03:C633. */
8b704316 6802 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
8460475b
JW
6803 {
6804 gfc_error ("The allocate-object at %L and the source-expr at %L "
6805 "shall have the same kind type parameter",
6806 &e->where, &code->expr3->where);
d3a9eea2 6807 goto failure;
8460475b 6808 }
fea54935
TB
6809
6810 /* Check F2008, C642. */
6811 if (code->expr3->ts.type == BT_DERIVED
3b6fa7a5 6812 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
fea54935
TB
6813 || (code->expr3->ts.u.derived->from_intmod
6814 == INTMOD_ISO_FORTRAN_ENV
6815 && code->expr3->ts.u.derived->intmod_sym_id
6816 == ISOFORTRAN_LOCK_TYPE)))
6817 {
6818 gfc_error ("The source-expr at %L shall neither be of type "
6819 "LOCK_TYPE nor have a LOCK_TYPE component if "
6820 "allocate-object at %L is a coarray",
6821 &code->expr3->where, &e->where);
6822 goto failure;
6823 }
8460475b 6824 }
94bff632
JW
6825
6826 /* Check F08:C629. */
6827 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
6828 && !code->expr3)
d0a9804e
TB
6829 {
6830 gcc_assert (e->ts.type == BT_CLASS);
6831 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
94bff632 6832 "type-spec or source-expr", sym->name, &e->where);
d3a9eea2 6833 goto failure;
d0a9804e
TB
6834 }
6835
2e0bffaf
TB
6836 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred)
6837 {
6838 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
6839 code->ext.alloc.ts.u.cl->length);
6840 if (cmp == 1 || cmp == -1 || cmp == -3)
6841 {
6842 gfc_error ("Allocating %s at %L with type-spec requires the same "
6843 "character-length parameter as in the declaration",
6844 sym->name, &e->where);
6845 goto failure;
6846 }
6847 }
6848
8c91ab34
DK
6849 /* In the variable definition context checks, gfc_expr_attr is used
6850 on the expression. This is fooled by the array specification
6851 present in e, thus we have to eliminate that one temporarily. */
6852 e2 = remove_last_array_ref (e);
524af0d6
JB
6853 t = true;
6854 if (t && pointer)
6855 t = gfc_check_vardef_context (e2, true, true, false,
6856 _("ALLOCATE object"));
6857 if (t)
6858 t = gfc_check_vardef_context (e2, false, true, false,
6859 _("ALLOCATE object"));
8c91ab34 6860 gfc_free_expr (e2);
524af0d6 6861 if (!t)
8c91ab34 6862 goto failure;
aa08038d 6863
c49ea23d
PT
6864 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
6865 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
6866 {
6867 /* For class arrays, the initialization with SOURCE is done
6868 using _copy and trans_call. It is convenient to exploit that
6869 when the allocated type is different from the declared type but
6870 no SOURCE exists by setting expr3. */
4d382327 6871 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
c49ea23d
PT
6872 }
6873 else if (!code->expr3)
b6ff8128
JW
6874 {
6875 /* Set up default initializer if needed. */
6876 gfc_typespec ts;
03d79dc3 6877 gfc_expr *init_e;
b6ff8128
JW
6878
6879 if (code->ext.alloc.ts.type == BT_DERIVED)
6880 ts = code->ext.alloc.ts;
6881 else
6882 ts = e->ts;
6883
6884 if (ts.type == BT_CLASS)
6885 ts = ts.u.derived->components->ts;
6886
03d79dc3 6887 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
b6ff8128 6888 {
11e5274a 6889 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
edd2b56a 6890 init_st->loc = code->loc;
edd2b56a
JW
6891 init_st->expr1 = gfc_expr_to_initialize (e);
6892 init_st->expr2 = init_e;
6893 init_st->next = code->next;
6894 code->next = init_st;
b6ff8128
JW
6895 }
6896 }
6897 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
6898 {
6899 /* Default initialization via MOLD (non-polymorphic). */
6900 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
6901 gfc_resolve_expr (rhs);
6902 gfc_free_expr (code->expr3);
6903 code->expr3 = rhs;
6904 }
6905
8b704316 6906 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
e10f52d0
JW
6907 {
6908 /* Make sure the vtab symbol is present when
6909 the module variables are generated. */
6910 gfc_typespec ts = e->ts;
6911 if (code->expr3)
6912 ts = code->expr3->ts;
6913 else if (code->ext.alloc.ts.type == BT_DERIVED)
6914 ts = code->ext.alloc.ts;
8b704316 6915
e10f52d0 6916 gfc_find_derived_vtab (ts.u.derived);
8b704316
PT
6917
6918 if (dimension)
6919 e = gfc_expr_to_initialize (e);
6920 }
6921 else if (unlimited && !UNLIMITED_POLY (code->expr3))
6922 {
6923 /* Again, make sure the vtab symbol is present when
6924 the module variables are generated. */
6925 gfc_typespec *ts = NULL;
6926 if (code->expr3)
6927 ts = &code->expr3->ts;
6928 else
6929 ts = &code->ext.alloc.ts;
6930
6931 gcc_assert (ts);
6932
7289d1c9 6933 gfc_find_vtab (ts);
8b704316 6934
c49ea23d
PT
6935 if (dimension)
6936 e = gfc_expr_to_initialize (e);
e10f52d0
JW
6937 }
6938
b21a544b 6939 if (dimension == 0 && codimension == 0)
d3a9eea2 6940 goto success;
6de9cd9a 6941
eea58adb 6942 /* Make sure the last reference node is an array specification. */
6de9cd9a 6943
8c91ab34 6944 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
d3a9eea2 6945 || (dimension && ref2->u.ar.dimen == 0))
6de9cd9a
DN
6946 {
6947 gfc_error ("Array specification required in ALLOCATE statement "
6948 "at %L", &e->where);
d3a9eea2 6949 goto failure;
6de9cd9a
DN
6950 }
6951
6de9cd9a
DN
6952 /* Make sure that the array section reference makes sense in the
6953 context of an ALLOCATE specification. */
6954
6955 ar = &ref2->u.ar;
6956
a3935ffc
TB
6957 if (codimension)
6958 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
6959 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
6960 {
6961 gfc_error ("Coarray specification required in ALLOCATE statement "
6962 "at %L", &e->where);
6963 goto failure;
6964 }
d3a9eea2 6965
6de9cd9a 6966 for (i = 0; i < ar->dimen; i++)
77726571
PT
6967 {
6968 if (ref2->u.ar.type == AR_ELEMENT)
6969 goto check_symbols;
6de9cd9a 6970
77726571
PT
6971 switch (ar->dimen_type[i])
6972 {
6973 case DIMEN_ELEMENT:
6de9cd9a
DN
6974 break;
6975
77726571
PT
6976 case DIMEN_RANGE:
6977 if (ar->start[i] != NULL
6978 && ar->end[i] != NULL
6979 && ar->stride[i] == NULL)
6980 break;
6de9cd9a 6981
77726571
PT
6982 /* Fall Through... */
6983
6984 case DIMEN_UNKNOWN:
6985 case DIMEN_VECTOR:
d3a9eea2 6986 case DIMEN_STAR:
a3935ffc 6987 case DIMEN_THIS_IMAGE:
77726571
PT
6988 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6989 &e->where);
d3a9eea2 6990 goto failure;
77726571
PT
6991 }
6992
6993check_symbols:
cf2b3c22 6994 for (a = code->ext.alloc.list; a; a = a->next)
77726571
PT
6995 {
6996 sym = a->expr->symtree->n.sym;
25e8cb2e
PT
6997
6998 /* TODO - check derived type components. */
6168891d 6999 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
25e8cb2e
PT
7000 continue;
7001
a68ab351
JJ
7002 if ((ar->start[i] != NULL
7003 && gfc_find_sym_in_expr (sym, ar->start[i]))
7004 || (ar->end[i] != NULL
7005 && gfc_find_sym_in_expr (sym, ar->end[i])))
77726571 7006 {
df2fba9e 7007 gfc_error ("'%s' must not appear in the array specification at "
77726571
PT
7008 "%L in the same ALLOCATE statement where it is "
7009 "itself allocated", sym->name, &ar->where);
d3a9eea2 7010 goto failure;
77726571
PT
7011 }
7012 }
7013 }
6de9cd9a 7014
d3a9eea2
TB
7015 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7016 {
7017 if (ar->dimen_type[i] == DIMEN_ELEMENT
7018 || ar->dimen_type[i] == DIMEN_RANGE)
7019 {
7020 if (i == (ar->dimen + ar->codimen - 1))
7021 {
7022 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7023 "statement at %L", &e->where);
7024 goto failure;
7025 }
c6423ef3 7026 continue;
d3a9eea2
TB
7027 }
7028
7029 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7030 && ar->stride[i] == NULL)
7031 break;
7032
7033 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7034 &e->where);
7035 goto failure;
7036 }
7037
d3a9eea2 7038success:
524af0d6 7039 return true;
d3a9eea2
TB
7040
7041failure:
524af0d6 7042 return false;
6de9cd9a
DN
7043}
7044
b9332b09
PT
7045static void
7046resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7047{
3759634f
SK
7048 gfc_expr *stat, *errmsg, *pe, *qe;
7049 gfc_alloc *a, *p, *q;
7050
8c91ab34
DK
7051 stat = code->expr1;
7052 errmsg = code->expr2;
b9332b09 7053
3759634f
SK
7054 /* Check the stat variable. */
7055 if (stat)
b9332b09 7056 {
524af0d6
JB
7057 gfc_check_vardef_context (stat, false, false, false,
7058 _("STAT variable"));
b9332b09 7059
6c145259
TK
7060 if ((stat->ts.type != BT_INTEGER
7061 && !(stat->ref && (stat->ref->type == REF_ARRAY
7062 || stat->ref->type == REF_COMPONENT)))
7063 || stat->rank > 0)
3759634f
SK
7064 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7065 "variable", &stat->where);
7066
cf2b3c22 7067 for (p = code->ext.alloc.list; p; p = p->next)
3759634f 7068 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
ddf58e42
TB
7069 {
7070 gfc_ref *ref1, *ref2;
7071 bool found = true;
7072
7073 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7074 ref1 = ref1->next, ref2 = ref2->next)
7075 {
7076 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7077 continue;
7078 if (ref1->u.c.component->name != ref2->u.c.component->name)
7079 {
7080 found = false;
7081 break;
7082 }
7083 }
7084
7085 if (found)
7086 {
7087 gfc_error ("Stat-variable at %L shall not be %sd within "
7088 "the same %s statement", &stat->where, fcn, fcn);
7089 break;
7090 }
7091 }
b9332b09
PT
7092 }
7093
3759634f
SK
7094 /* Check the errmsg variable. */
7095 if (errmsg)
7096 {
7097 if (!stat)
7098 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
7099 &errmsg->where);
7100
57bf28ea
TB
7101 gfc_check_vardef_context (errmsg, false, false, false,
7102 _("ERRMSG variable"));
3759634f 7103
6c145259
TK
7104 if ((errmsg->ts.type != BT_CHARACTER
7105 && !(errmsg->ref
7106 && (errmsg->ref->type == REF_ARRAY
7107 || errmsg->ref->type == REF_COMPONENT)))
7108 || errmsg->rank > 0 )
3759634f
SK
7109 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7110 "variable", &errmsg->where);
7111
cf2b3c22 7112 for (p = code->ext.alloc.list; p; p = p->next)
3759634f 7113 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
ddf58e42
TB
7114 {
7115 gfc_ref *ref1, *ref2;
7116 bool found = true;
7117
7118 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7119 ref1 = ref1->next, ref2 = ref2->next)
7120 {
7121 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7122 continue;
7123 if (ref1->u.c.component->name != ref2->u.c.component->name)
7124 {
7125 found = false;
7126 break;
7127 }
7128 }
7129
7130 if (found)
7131 {
7132 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7133 "the same %s statement", &errmsg->where, fcn, fcn);
7134 break;
7135 }
7136 }
3759634f
SK
7137 }
7138
c2092deb
TK
7139 /* Check that an allocate-object appears only once in the statement. */
7140
cf2b3c22 7141 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
7142 {
7143 pe = p->expr;
75fee9f2 7144 for (q = p->next; q; q = q->next)
3759634f 7145 {
75fee9f2
TK
7146 qe = q->expr;
7147 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
3759634f 7148 {
75fee9f2
TK
7149 /* This is a potential collision. */
7150 gfc_ref *pr = pe->ref;
7151 gfc_ref *qr = qe->ref;
4d382327 7152
75fee9f2
TK
7153 /* Follow the references until
7154 a) They start to differ, in which case there is no error;
7155 you can deallocate a%b and a%c in a single statement
7156 b) Both of them stop, which is an error
7157 c) One of them stops, which is also an error. */
7158 while (1)
7159 {
7160 if (pr == NULL && qr == NULL)
7161 {
7162 gfc_error ("Allocate-object at %L also appears at %L",
7163 &pe->where, &qe->where);
7164 break;
7165 }
7166 else if (pr != NULL && qr == NULL)
7167 {
7168 gfc_error ("Allocate-object at %L is subobject of"
7169 " object at %L", &pe->where, &qe->where);
7170 break;
7171 }
7172 else if (pr == NULL && qr != NULL)
7173 {
7174 gfc_error ("Allocate-object at %L is subobject of"
7175 " object at %L", &qe->where, &pe->where);
7176 break;
7177 }
7178 /* Here, pr != NULL && qr != NULL */
7179 gcc_assert(pr->type == qr->type);
7180 if (pr->type == REF_ARRAY)
7181 {
7182 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7183 which are legal. */
7184 gcc_assert (qr->type == REF_ARRAY);
7185
7186 if (pr->next && qr->next)
7187 {
474d486a 7188 int i;
75fee9f2
TK
7189 gfc_array_ref *par = &(pr->u.ar);
7190 gfc_array_ref *qar = &(qr->u.ar);
474d486a
PT
7191
7192 for (i=0; i<par->dimen; i++)
7193 {
7194 if ((par->start[i] != NULL
7195 || qar->start[i] != NULL)
7196 && gfc_dep_compare_expr (par->start[i],
7197 qar->start[i]) != 0)
7198 goto break_label;
7199 }
75fee9f2
TK
7200 }
7201 }
7202 else
7203 {
7204 if (pr->u.c.component->name != qr->u.c.component->name)
7205 break;
7206 }
4d382327 7207
75fee9f2
TK
7208 pr = pr->next;
7209 qr = qr->next;
7210 }
474d486a
PT
7211 break_label:
7212 ;
3759634f
SK
7213 }
7214 }
7215 }
b9332b09
PT
7216
7217 if (strcmp (fcn, "ALLOCATE") == 0)
7218 {
cf2b3c22 7219 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
7220 resolve_allocate_expr (a->expr, code);
7221 }
7222 else
7223 {
cf2b3c22 7224 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
7225 resolve_deallocate_expr (a->expr);
7226 }
7227}
6de9cd9a 7228
3759634f 7229
6de9cd9a
DN
7230/************ SELECT CASE resolution subroutines ************/
7231
7232/* Callback function for our mergesort variant. Determines interval
7233 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4d382327 7234 op1 > op2. Assumes we're not dealing with the default case.
c224550f
SK
7235 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7236 There are nine situations to check. */
6de9cd9a
DN
7237
7238static int
edf1eac2 7239compare_cases (const gfc_case *op1, const gfc_case *op2)
6de9cd9a 7240{
c224550f 7241 int retval;
6de9cd9a 7242
c224550f 7243 if (op1->low == NULL) /* op1 = (:L) */
6de9cd9a 7244 {
c224550f
SK
7245 /* op2 = (:N), so overlap. */
7246 retval = 0;
7247 /* op2 = (M:) or (M:N), L < M */
7248 if (op2->low != NULL
7b4c5f8b 7249 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 7250 retval = -1;
6de9cd9a 7251 }
c224550f 7252 else if (op1->high == NULL) /* op1 = (K:) */
6de9cd9a 7253 {
c224550f
SK
7254 /* op2 = (M:), so overlap. */
7255 retval = 0;
7256 /* op2 = (:N) or (M:N), K > N */
7257 if (op2->high != NULL
7b4c5f8b 7258 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 7259 retval = 1;
6de9cd9a 7260 }
c224550f 7261 else /* op1 = (K:L) */
6de9cd9a 7262 {
c224550f 7263 if (op2->low == NULL) /* op2 = (:N), K > N */
7b4c5f8b
TB
7264 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7265 ? 1 : 0;
c224550f 7266 else if (op2->high == NULL) /* op2 = (M:), L < M */
7b4c5f8b
TB
7267 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7268 ? -1 : 0;
edf1eac2
SK
7269 else /* op2 = (M:N) */
7270 {
c224550f 7271 retval = 0;
edf1eac2 7272 /* L < M */
7b4c5f8b 7273 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 7274 retval = -1;
edf1eac2 7275 /* K > N */
7b4c5f8b 7276 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 7277 retval = 1;
6de9cd9a
DN
7278 }
7279 }
c224550f
SK
7280
7281 return retval;
6de9cd9a
DN
7282}
7283
7284
7285/* Merge-sort a double linked case list, detecting overlap in the
7286 process. LIST is the head of the double linked case list before it
7287 is sorted. Returns the head of the sorted list if we don't see any
7288 overlap, or NULL otherwise. */
7289
7290static gfc_case *
edf1eac2 7291check_case_overlap (gfc_case *list)
6de9cd9a
DN
7292{
7293 gfc_case *p, *q, *e, *tail;
7294 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7295
7296 /* If the passed list was empty, return immediately. */
7297 if (!list)
7298 return NULL;
7299
7300 overlap_seen = 0;
7301 insize = 1;
7302
7303 /* Loop unconditionally. The only exit from this loop is a return
7304 statement, when we've finished sorting the case list. */
7305 for (;;)
7306 {
7307 p = list;
7308 list = NULL;
7309 tail = NULL;
7310
7311 /* Count the number of merges we do in this pass. */
7312 nmerges = 0;
7313
7314 /* Loop while there exists a merge to be done. */
7315 while (p)
7316 {
7317 int i;
7318
7319 /* Count this merge. */
7320 nmerges++;
7321
5352b89f 7322 /* Cut the list in two pieces by stepping INSIZE places
edf1eac2 7323 forward in the list, starting from P. */
6de9cd9a
DN
7324 psize = 0;
7325 q = p;
7326 for (i = 0; i < insize; i++)
7327 {
7328 psize++;
7329 q = q->right;
7330 if (!q)
7331 break;
7332 }
7333 qsize = insize;
7334
7335 /* Now we have two lists. Merge them! */
7336 while (psize > 0 || (qsize > 0 && q != NULL))
7337 {
6de9cd9a
DN
7338 /* See from which the next case to merge comes from. */
7339 if (psize == 0)
7340 {
7341 /* P is empty so the next case must come from Q. */
7342 e = q;
7343 q = q->right;
7344 qsize--;
7345 }
7346 else if (qsize == 0 || q == NULL)
7347 {
7348 /* Q is empty. */
7349 e = p;
7350 p = p->right;
7351 psize--;
7352 }
7353 else
7354 {
7355 cmp = compare_cases (p, q);
7356 if (cmp < 0)
7357 {
7358 /* The whole case range for P is less than the
edf1eac2 7359 one for Q. */
6de9cd9a
DN
7360 e = p;
7361 p = p->right;
7362 psize--;
7363 }
7364 else if (cmp > 0)
7365 {
7366 /* The whole case range for Q is greater than
edf1eac2 7367 the case range for P. */
6de9cd9a
DN
7368 e = q;
7369 q = q->right;
7370 qsize--;
7371 }
7372 else
7373 {
7374 /* The cases overlap, or they are the same
7375 element in the list. Either way, we must
7376 issue an error and get the next case from P. */
7377 /* FIXME: Sort P and Q by line number. */
7378 gfc_error ("CASE label at %L overlaps with CASE "
7379 "label at %L", &p->where, &q->where);
7380 overlap_seen = 1;
7381 e = p;
7382 p = p->right;
7383 psize--;
7384 }
7385 }
7386
7387 /* Add the next element to the merged list. */
7388 if (tail)
7389 tail->right = e;
7390 else
7391 list = e;
7392 e->left = tail;
7393 tail = e;
7394 }
7395
7396 /* P has now stepped INSIZE places along, and so has Q. So
edf1eac2 7397 they're the same. */
6de9cd9a
DN
7398 p = q;
7399 }
7400 tail->right = NULL;
7401
7402 /* If we have done only one merge or none at all, we've
edf1eac2 7403 finished sorting the cases. */
6de9cd9a 7404 if (nmerges <= 1)
edf1eac2 7405 {
6de9cd9a
DN
7406 if (!overlap_seen)
7407 return list;
7408 else
7409 return NULL;
7410 }
7411
7412 /* Otherwise repeat, merging lists twice the size. */
7413 insize *= 2;
7414 }
7415}
7416
7417
5352b89f
SK
7418/* Check to see if an expression is suitable for use in a CASE statement.
7419 Makes sure that all case expressions are scalar constants of the same
524af0d6 7420 type. Return false if anything is wrong. */
6de9cd9a 7421
524af0d6 7422static bool
edf1eac2 7423validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6de9cd9a 7424{
524af0d6 7425 if (e == NULL) return true;
6de9cd9a 7426
5352b89f 7427 if (e->ts.type != case_expr->ts.type)
6de9cd9a
DN
7428 {
7429 gfc_error ("Expression in CASE statement at %L must be of type %s",
5352b89f 7430 &e->where, gfc_basic_typename (case_expr->ts.type));
524af0d6 7431 return false;
6de9cd9a
DN
7432 }
7433
5352b89f
SK
7434 /* C805 (R808) For a given case-construct, each case-value shall be of
7435 the same type as case-expr. For character type, length differences
7436 are allowed, but the kind type parameters shall be the same. */
7437
7438 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6de9cd9a 7439 {
d393bbd7
FXC
7440 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7441 &e->where, case_expr->ts.kind);
524af0d6 7442 return false;
6de9cd9a
DN
7443 }
7444
ad1614a7
DF
7445 /* Convert the case value kind to that of case expression kind,
7446 if needed */
7447
5352b89f
SK
7448 if (e->ts.kind != case_expr->ts.kind)
7449 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7450
6de9cd9a
DN
7451 if (e->rank != 0)
7452 {
7453 gfc_error ("Expression in CASE statement at %L must be scalar",
7454 &e->where);
524af0d6 7455 return false;
6de9cd9a
DN
7456 }
7457
524af0d6 7458 return true;
6de9cd9a
DN
7459}
7460
7461
7462/* Given a completely parsed select statement, we:
7463
7464 - Validate all expressions and code within the SELECT.
7465 - Make sure that the selection expression is not of the wrong type.
7466 - Make sure that no case ranges overlap.
7467 - Eliminate unreachable cases and unreachable code resulting from
7468 removing case labels.
7469
7470 The standard does allow unreachable cases, e.g. CASE (5:3). But
7471 they are a hassle for code generation, and to prevent that, we just
7472 cut them out here. This is not necessary for overlapping cases
7473 because they are illegal and we never even try to generate code.
7474
7475 We have the additional caveat that a SELECT construct could have
1f2959f0 7476 been a computed GOTO in the source code. Fortunately we can fairly
6de9cd9a
DN
7477 easily work around that here: The case_expr for a "real" SELECT CASE
7478 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7479 we have to do is make sure that the case_expr is a scalar integer
7480 expression. */
7481
7482static void
ad3e2ad2 7483resolve_select (gfc_code *code, bool select_type)
6de9cd9a
DN
7484{
7485 gfc_code *body;
7486 gfc_expr *case_expr;
7487 gfc_case *cp, *default_case, *tail, *head;
7488 int seen_unreachable;
d68bd5a8 7489 int seen_logical;
6de9cd9a
DN
7490 int ncases;
7491 bt type;
524af0d6 7492 bool t;
6de9cd9a 7493
a513927a 7494 if (code->expr1 == NULL)
6de9cd9a
DN
7495 {
7496 /* This was actually a computed GOTO statement. */
7497 case_expr = code->expr2;
edf1eac2 7498 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6de9cd9a
DN
7499 gfc_error ("Selection expression in computed GOTO statement "
7500 "at %L must be a scalar integer expression",
7501 &case_expr->where);
7502
7503 /* Further checking is not necessary because this SELECT was built
7504 by the compiler, so it should always be OK. Just move the
7505 case_expr from expr2 to expr so that we can handle computed
7506 GOTOs as normal SELECTs from here on. */
a513927a 7507 code->expr1 = code->expr2;
6de9cd9a
DN
7508 code->expr2 = NULL;
7509 return;
7510 }
7511
a513927a 7512 case_expr = code->expr1;
6de9cd9a 7513 type = case_expr->ts.type;
ad3e2ad2
JW
7514
7515 /* F08:C830. */
6de9cd9a
DN
7516 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7517 {
7518 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7519 &case_expr->where, gfc_typename (&case_expr->ts));
7520
7521 /* Punt. Going on here just produce more garbage error messages. */
7522 return;
7523 }
7524
ad3e2ad2
JW
7525 /* F08:R842. */
7526 if (!select_type && case_expr->rank != 0)
7527 {
7528 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7529 "expression", &case_expr->where);
7530
7531 /* Punt. */
7532 return;
7533 }
7534
ad1614a7
DF
7535 /* Raise a warning if an INTEGER case value exceeds the range of
7536 the case-expr. Later, all expressions will be promoted to the
7537 largest kind of all case-labels. */
7538
7539 if (type == BT_INTEGER)
7540 for (body = code->block; body; body = body->block)
29a63d67 7541 for (cp = body->ext.block.case_list; cp; cp = cp->next)
ad1614a7
DF
7542 {
7543 if (cp->low
7544 && gfc_check_integer_range (cp->low->value.integer,
7545 case_expr->ts.kind) != ARITH_OK)
7546 gfc_warning ("Expression in CASE statement at %L is "
7547 "not in the range of %s", &cp->low->where,
7548 gfc_typename (&case_expr->ts));
7549
7550 if (cp->high
7551 && cp->low != cp->high
7552 && gfc_check_integer_range (cp->high->value.integer,
7553 case_expr->ts.kind) != ARITH_OK)
7554 gfc_warning ("Expression in CASE statement at %L is "
7555 "not in the range of %s", &cp->high->where,
7556 gfc_typename (&case_expr->ts));
7557 }
7558
5352b89f
SK
7559 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7560 of the SELECT CASE expression and its CASE values. Walk the lists
7561 of case values, and if we find a mismatch, promote case_expr to
7562 the appropriate kind. */
7563
7564 if (type == BT_LOGICAL || type == BT_INTEGER)
7565 {
7566 for (body = code->block; body; body = body->block)
7567 {
7568 /* Walk the case label list. */
29a63d67 7569 for (cp = body->ext.block.case_list; cp; cp = cp->next)
5352b89f
SK
7570 {
7571 /* Intercept the DEFAULT case. It does not have a kind. */
7572 if (cp->low == NULL && cp->high == NULL)
7573 continue;
7574
05c1e3a7 7575 /* Unreachable case ranges are discarded, so ignore. */
5352b89f
SK
7576 if (cp->low != NULL && cp->high != NULL
7577 && cp->low != cp->high
7b4c5f8b 7578 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5352b89f
SK
7579 continue;
7580
5352b89f
SK
7581 if (cp->low != NULL
7582 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7583 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7584
7585 if (cp->high != NULL
7586 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
05c1e3a7 7587 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5352b89f
SK
7588 }
7589 }
7590 }
7591
6de9cd9a
DN
7592 /* Assume there is no DEFAULT case. */
7593 default_case = NULL;
7594 head = tail = NULL;
7595 ncases = 0;
d68bd5a8 7596 seen_logical = 0;
6de9cd9a
DN
7597
7598 for (body = code->block; body; body = body->block)
7599 {
7600 /* Assume the CASE list is OK, and all CASE labels can be matched. */
524af0d6 7601 t = true;
6de9cd9a
DN
7602 seen_unreachable = 0;
7603
7604 /* Walk the case label list, making sure that all case labels
edf1eac2 7605 are legal. */
29a63d67 7606 for (cp = body->ext.block.case_list; cp; cp = cp->next)
6de9cd9a
DN
7607 {
7608 /* Count the number of cases in the whole construct. */
7609 ncases++;
7610
7611 /* Intercept the DEFAULT case. */
7612 if (cp->low == NULL && cp->high == NULL)
7613 {
7614 if (default_case != NULL)
edf1eac2 7615 {
6de9cd9a
DN
7616 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7617 "by a second DEFAULT CASE at %L",
7618 &default_case->where, &cp->where);
524af0d6 7619 t = false;
6de9cd9a
DN
7620 break;
7621 }
7622 else
7623 {
7624 default_case = cp;
7625 continue;
7626 }
7627 }
7628
7629 /* Deal with single value cases and case ranges. Errors are
edf1eac2 7630 issued from the validation function. */
524af0d6
JB
7631 if (!validate_case_label_expr (cp->low, case_expr)
7632 || !validate_case_label_expr (cp->high, case_expr))
6de9cd9a 7633 {
524af0d6 7634 t = false;
6de9cd9a
DN
7635 break;
7636 }
7637
7638 if (type == BT_LOGICAL
7639 && ((cp->low == NULL || cp->high == NULL)
7640 || cp->low != cp->high))
7641 {
edf1eac2
SK
7642 gfc_error ("Logical range in CASE statement at %L is not "
7643 "allowed", &cp->low->where);
524af0d6 7644 t = false;
6de9cd9a
DN
7645 break;
7646 }
7647
d68bd5a8
PT
7648 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7649 {
7650 int value;
7651 value = cp->low->value.logical == 0 ? 2 : 1;
7652 if (value & seen_logical)
7653 {
ad1614a7 7654 gfc_error ("Constant logical value in CASE statement "
d68bd5a8
PT
7655 "is repeated at %L",
7656 &cp->low->where);
524af0d6 7657 t = false;
d68bd5a8
PT
7658 break;
7659 }
7660 seen_logical |= value;
7661 }
7662
6de9cd9a
DN
7663 if (cp->low != NULL && cp->high != NULL
7664 && cp->low != cp->high
7b4c5f8b 7665 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6de9cd9a
DN
7666 {
7667 if (gfc_option.warn_surprising)
edf1eac2 7668 gfc_warning ("Range specification at %L can never "
6de9cd9a
DN
7669 "be matched", &cp->where);
7670
7671 cp->unreachable = 1;
7672 seen_unreachable = 1;
7673 }
7674 else
7675 {
7676 /* If the case range can be matched, it can also overlap with
7677 other cases. To make sure it does not, we put it in a
7678 double linked list here. We sort that with a merge sort
7679 later on to detect any overlapping cases. */
7680 if (!head)
edf1eac2 7681 {
6de9cd9a
DN
7682 head = tail = cp;
7683 head->right = head->left = NULL;
7684 }
7685 else
edf1eac2 7686 {
6de9cd9a
DN
7687 tail->right = cp;
7688 tail->right->left = tail;
7689 tail = tail->right;
7690 tail->right = NULL;
7691 }
7692 }
7693 }
7694
7695 /* It there was a failure in the previous case label, give up
7696 for this case label list. Continue with the next block. */
524af0d6 7697 if (!t)
6de9cd9a
DN
7698 continue;
7699
7700 /* See if any case labels that are unreachable have been seen.
7701 If so, we eliminate them. This is a bit of a kludge because
7702 the case lists for a single case statement (label) is a
7703 single forward linked lists. */
7704 if (seen_unreachable)
7705 {
7706 /* Advance until the first case in the list is reachable. */
29a63d67
TB
7707 while (body->ext.block.case_list != NULL
7708 && body->ext.block.case_list->unreachable)
6de9cd9a 7709 {
29a63d67
TB
7710 gfc_case *n = body->ext.block.case_list;
7711 body->ext.block.case_list = body->ext.block.case_list->next;
6de9cd9a
DN
7712 n->next = NULL;
7713 gfc_free_case_list (n);
7714 }
7715
7716 /* Strip all other unreachable cases. */
29a63d67 7717 if (body->ext.block.case_list)
6de9cd9a 7718 {
29a63d67 7719 for (cp = body->ext.block.case_list; cp->next; cp = cp->next)
6de9cd9a
DN
7720 {
7721 if (cp->next->unreachable)
7722 {
7723 gfc_case *n = cp->next;
7724 cp->next = cp->next->next;
7725 n->next = NULL;
7726 gfc_free_case_list (n);
7727 }
7728 }
7729 }
7730 }
7731 }
7732
7733 /* See if there were overlapping cases. If the check returns NULL,
7734 there was overlap. In that case we don't do anything. If head
7735 is non-NULL, we prepend the DEFAULT case. The sorted list can
7736 then used during code generation for SELECT CASE constructs with
7737 a case expression of a CHARACTER type. */
7738 if (head)
7739 {
7740 head = check_case_overlap (head);
7741
7742 /* Prepend the default_case if it is there. */
7743 if (head != NULL && default_case)
7744 {
7745 default_case->left = NULL;
7746 default_case->right = head;
7747 head->left = default_case;
7748 }
7749 }
7750
7751 /* Eliminate dead blocks that may be the result if we've seen
7752 unreachable case labels for a block. */
7753 for (body = code; body && body->block; body = body->block)
7754 {
29a63d67 7755 if (body->block->ext.block.case_list == NULL)
edf1eac2 7756 {
6de9cd9a
DN
7757 /* Cut the unreachable block from the code chain. */
7758 gfc_code *c = body->block;
7759 body->block = c->block;
7760
7761 /* Kill the dead block, but not the blocks below it. */
7762 c->block = NULL;
7763 gfc_free_statements (c);
edf1eac2 7764 }
6de9cd9a
DN
7765 }
7766
7767 /* More than two cases is legal but insane for logical selects.
7768 Issue a warning for it. */
7769 if (gfc_option.warn_surprising && type == BT_LOGICAL
7770 && ncases > 2)
7771 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
7772 &code->loc);
7773}
7774
7775
cf2b3c22
TB
7776/* Check if a derived type is extensible. */
7777
7778bool
7779gfc_type_is_extensible (gfc_symbol *sym)
7780{
8b704316
PT
7781 return !(sym->attr.is_bind_c || sym->attr.sequence
7782 || (sym->attr.is_class
7783 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
cf2b3c22
TB
7784}
7785
7786
8f75db9f 7787/* Resolve an associate-name: Resolve target and ensure the type-spec is
3e78238a
DK
7788 correct as well as possibly the array-spec. */
7789
7790static void
7791resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
7792{
7793 gfc_expr* target;
3e78238a
DK
7794
7795 gcc_assert (sym->assoc);
7796 gcc_assert (sym->attr.flavor == FL_VARIABLE);
7797
7798 /* If this is for SELECT TYPE, the target may not yet be set. In that
7799 case, return. Resolution will be called later manually again when
7800 this is done. */
7801 target = sym->assoc->target;
7802 if (!target)
7803 return;
7804 gcc_assert (!sym->assoc->dangling);
7805
524af0d6 7806 if (resolve_target && !gfc_resolve_expr (target))
3e78238a
DK
7807 return;
7808
7809 /* For variable targets, we get some attributes from the target. */
7810 if (target->expr_type == EXPR_VARIABLE)
7811 {
7812 gfc_symbol* tsym;
7813
7814 gcc_assert (target->symtree);
7815 tsym = target->symtree->n.sym;
7816
7817 sym->attr.asynchronous = tsym->attr.asynchronous;
7818 sym->attr.volatile_ = tsym->attr.volatile_;
7819
102344e2
TB
7820 sym->attr.target = tsym->attr.target
7821 || gfc_expr_attr (target).pointer;
3e78238a
DK
7822 }
7823
414e8be2
DK
7824 /* Get type if this was not already set. Note that it can be
7825 some other type than the target in case this is a SELECT TYPE
7826 selector! So we must not update when the type is already there. */
7827 if (sym->ts.type == BT_UNKNOWN)
7828 sym->ts = target->ts;
3e78238a
DK
7829 gcc_assert (sym->ts.type != BT_UNKNOWN);
7830
7831 /* See if this is a valid association-to-variable. */
8c91ab34
DK
7832 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
7833 && !gfc_has_vector_subscript (target));
3e78238a
DK
7834
7835 /* Finally resolve if this is an array or not. */
102344e2 7836 if (sym->attr.dimension && target->rank == 0)
3e78238a
DK
7837 {
7838 gfc_error ("Associate-name '%s' at %L is used as array",
7839 sym->name, &sym->declared_at);
7840 sym->attr.dimension = 0;
7841 return;
7842 }
8f75db9f
PT
7843
7844 /* We cannot deal with class selectors that need temporaries. */
7845 if (target->ts.type == BT_CLASS
7846 && gfc_ref_needs_temporary_p (target->ref))
7847 {
7848 gfc_error ("CLASS selector at %L needs a temporary which is not "
7849 "yet implemented", &target->where);
7850 return;
7851 }
7852
7853 if (target->ts.type != BT_CLASS && target->rank > 0)
3e78238a 7854 sym->attr.dimension = 1;
8f75db9f
PT
7855 else if (target->ts.type == BT_CLASS)
7856 gfc_fix_class_refs (target);
7857
7858 /* The associate-name will have a correct type by now. Make absolutely
7859 sure that it has not picked up a dimension attribute. */
7860 if (sym->ts.type == BT_CLASS)
7861 sym->attr.dimension = 0;
3e78238a
DK
7862
7863 if (sym->attr.dimension)
7864 {
7865 sym->as = gfc_get_array_spec ();
7866 sym->as->rank = target->rank;
7867 sym->as->type = AS_DEFERRED;
7868
7869 /* Target must not be coindexed, thus the associate-variable
7870 has no corank. */
7871 sym->as->corank = 0;
7872 }
aa271860
PT
7873
7874 /* Mark this as an associate variable. */
7875 sym->attr.associate_var = 1;
7876
7877 /* If the target is a good class object, so is the associate variable. */
7878 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
7879 sym->attr.class_ok = 1;
3e78238a
DK
7880}
7881
7882
cf2b3c22
TB
7883/* Resolve a SELECT TYPE statement. */
7884
7885static void
8c91ab34 7886resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
cf2b3c22
TB
7887{
7888 gfc_symbol *selector_type;
7c1dab0d
JW
7889 gfc_code *body, *new_st, *if_st, *tail;
7890 gfc_code *class_is = NULL, *default_case = NULL;
7891 gfc_case *c;
cf2b3c22
TB
7892 gfc_symtree *st;
7893 char name[GFC_MAX_SYMBOL_LEN];
93d76687 7894 gfc_namespace *ns;
7c1dab0d 7895 int error = 0;
8b704316 7896 int charlen = 0;
93d76687 7897
03af1e4c 7898 ns = code->ext.block.ns;
93d76687 7899 gfc_resolve (ns);
cf2b3c22 7900
f5dbb57c
JW
7901 /* Check for F03:C813. */
7902 if (code->expr1->ts.type != BT_CLASS
7903 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
7904 {
7905 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
7906 "at %L", &code->loc);
7907 return;
7908 }
7909
cd99c23c
TB
7910 if (!code->expr1->symtree->n.sym->attr.class_ok)
7911 return;
7912
93d76687 7913 if (code->expr2)
f5dbb57c
JW
7914 {
7915 if (code->expr1->symtree->n.sym->attr.untyped)
7916 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7a08eda1 7917 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
e4821cd8
PT
7918
7919 /* F2008: C803 The selector expression must not be coindexed. */
7920 if (gfc_is_coindexed (code->expr2))
7921 {
7922 gfc_error ("Selector at %L must not be coindexed",
7923 &code->expr2->where);
7924 return;
7925 }
7926
f5dbb57c 7927 }
93d76687 7928 else
e4821cd8
PT
7929 {
7930 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
7931
7932 if (gfc_is_coindexed (code->expr1))
7933 {
7934 gfc_error ("Selector at %L must not be coindexed",
7935 &code->expr1->where);
7936 return;
7937 }
7938 }
cf2b3c22 7939
cf2b3c22
TB
7940 /* Loop over TYPE IS / CLASS IS cases. */
7941 for (body = code->block; body; body = body->block)
7942 {
29a63d67 7943 c = body->ext.block.case_list;
cf2b3c22
TB
7944
7945 /* Check F03:C815. */
7946 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8b704316 7947 && !selector_type->attr.unlimited_polymorphic
cf2b3c22
TB
7948 && !gfc_type_is_extensible (c->ts.u.derived))
7949 {
7950 gfc_error ("Derived type '%s' at %L must be extensible",
7951 c->ts.u.derived->name, &c->where);
7c1dab0d 7952 error++;
cf2b3c22
TB
7953 continue;
7954 }
7955
7956 /* Check F03:C816. */
55d8631b
TB
7957 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
7958 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
7959 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
cf2b3c22 7960 {
55d8631b
TB
7961 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
7962 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
7963 c->ts.u.derived->name, &c->where, selector_type->name);
7964 else
7965 gfc_error ("Unexpected intrinsic type '%s' at %L",
7966 gfc_basic_typename (c->ts.type), &c->where);
7c1dab0d 7967 error++;
cf2b3c22
TB
7968 continue;
7969 }
7970
8b704316
PT
7971 /* Check F03:C814. */
7972 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
7973 {
7974 gfc_error ("The type-spec at %L shall specify that each length "
7975 "type parameter is assumed", &c->where);
7976 error++;
7977 continue;
7978 }
7979
cf2b3c22
TB
7980 /* Intercept the DEFAULT case. */
7981 if (c->ts.type == BT_UNKNOWN)
7982 {
7983 /* Check F03:C818. */
7c1dab0d
JW
7984 if (default_case)
7985 {
7986 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7987 "by a second DEFAULT CASE at %L",
29a63d67 7988 &default_case->ext.block.case_list->where, &c->where);
7c1dab0d
JW
7989 error++;
7990 continue;
7991 }
414e8be2
DK
7992
7993 default_case = body;
cf2b3c22
TB
7994 }
7995 }
4d382327 7996
3e78238a 7997 if (error > 0)
7c1dab0d 7998 return;
cf2b3c22 7999
3e78238a 8000 /* Transform SELECT TYPE statement to BLOCK and associate selector to
e5ca9693
DK
8001 target if present. If there are any EXIT statements referring to the
8002 SELECT TYPE construct, this is no problem because the gfc_code
8003 reference stays the same and EXIT is equally possible from the BLOCK
8004 it is changed to. */
3e78238a 8005 code->op = EXEC_BLOCK;
93d76687
JW
8006 if (code->expr2)
8007 {
3e78238a
DK
8008 gfc_association_list* assoc;
8009
8010 assoc = gfc_get_association_list ();
8011 assoc->st = code->expr1->symtree;
8012 assoc->target = gfc_copy_expr (code->expr2);
c49ea23d 8013 assoc->target->where = code->expr2->where;
3e78238a 8014 /* assoc->variable will be set by resolve_assoc_var. */
4d382327 8015
3e78238a
DK
8016 code->ext.block.assoc = assoc;
8017 code->expr1->symtree->n.sym->assoc = assoc;
8018
8019 resolve_assoc_var (code->expr1->symtree->n.sym, false);
93d76687 8020 }
3e78238a
DK
8021 else
8022 code->ext.block.assoc = NULL;
93d76687 8023
3e78238a 8024 /* Add EXEC_SELECT to switch on type. */
11e5274a 8025 new_st = gfc_get_code (code->op);
93d76687
JW
8026 new_st->expr1 = code->expr1;
8027 new_st->expr2 = code->expr2;
8028 new_st->block = code->block;
3e78238a
DK
8029 code->expr1 = code->expr2 = NULL;
8030 code->block = NULL;
93d76687
JW
8031 if (!ns->code)
8032 ns->code = new_st;
8033 else
8034 ns->code->next = new_st;
93d76687 8035 code = new_st;
cf2b3c22 8036 code->op = EXEC_SELECT;
8b704316 8037
b04533af
JW
8038 gfc_add_vptr_component (code->expr1);
8039 gfc_add_hash_component (code->expr1);
cf2b3c22
TB
8040
8041 /* Loop over TYPE IS / CLASS IS cases. */
8042 for (body = code->block; body; body = body->block)
8043 {
29a63d67 8044 c = body->ext.block.case_list;
b7e75771 8045
cf2b3c22 8046 if (c->ts.type == BT_DERIVED)
b7e75771
JD
8047 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8048 c->ts.u.derived->hash_value);
8b704316
PT
8049 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8050 {
8051 gfc_symbol *ivtab;
8052 gfc_expr *e;
8053
7289d1c9 8054 ivtab = gfc_find_vtab (&c->ts);
4038d0fb 8055 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8b704316
PT
8056 e = CLASS_DATA (ivtab)->initializer;
8057 c->low = c->high = gfc_copy_expr (e);
8058 }
b7e75771 8059
7c1dab0d 8060 else if (c->ts.type == BT_UNKNOWN)
cf2b3c22 8061 continue;
b7e75771 8062
3e78238a
DK
8063 /* Associate temporary to selector. This should only be done
8064 when this case is actually true, so build a new ASSOCIATE
8065 that does precisely this here (instead of using the
8066 'global' one). */
8067
7c1dab0d 8068 if (c->ts.type == BT_CLASS)
b04533af 8069 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8b704316 8070 else if (c->ts.type == BT_DERIVED)
b04533af 8071 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8b704316
PT
8072 else if (c->ts.type == BT_CHARACTER)
8073 {
8074 if (c->ts.u.cl && c->ts.u.cl->length
8075 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8076 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8077 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8078 charlen, c->ts.kind);
8079 }
8080 else
8081 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8082 c->ts.kind);
8083
93d76687 8084 st = gfc_find_symtree (ns->sym_root, name);
3e78238a
DK
8085 gcc_assert (st->n.sym->assoc);
8086 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
c49ea23d 8087 st->n.sym->assoc->target->where = code->expr1->where;
8b704316 8088 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
b04533af 8089 gfc_add_data_component (st->n.sym->assoc->target);
3e78238a 8090
11e5274a 8091 new_st = gfc_get_code (EXEC_BLOCK);
3e78238a
DK
8092 new_st->ext.block.ns = gfc_build_block_ns (ns);
8093 new_st->ext.block.ns->code = body->next;
8094 body->next = new_st;
8095
8096 /* Chain in the new list only if it is marked as dangling. Otherwise
8097 there is a CASE label overlap and this is already used. Just ignore,
eea58adb 8098 the error is diagnosed elsewhere. */
3e78238a 8099 if (st->n.sym->assoc->dangling)
7c1dab0d 8100 {
3e78238a
DK
8101 new_st->ext.block.assoc = st->n.sym->assoc;
8102 st->n.sym->assoc->dangling = 0;
7c1dab0d 8103 }
3e78238a
DK
8104
8105 resolve_assoc_var (st->n.sym, false);
cf2b3c22 8106 }
4d382327 8107
7c1dab0d
JW
8108 /* Take out CLASS IS cases for separate treatment. */
8109 body = code;
8110 while (body && body->block)
8111 {
29a63d67 8112 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
7c1dab0d
JW
8113 {
8114 /* Add to class_is list. */
8115 if (class_is == NULL)
4d382327 8116 {
7c1dab0d
JW
8117 class_is = body->block;
8118 tail = class_is;
8119 }
8120 else
8121 {
8122 for (tail = class_is; tail->block; tail = tail->block) ;
8123 tail->block = body->block;
8124 tail = tail->block;
8125 }
8126 /* Remove from EXEC_SELECT list. */
8127 body->block = body->block->block;
8128 tail->block = NULL;
8129 }
8130 else
8131 body = body->block;
8132 }
cf2b3c22 8133
7c1dab0d 8134 if (class_is)
cf2b3c22 8135 {
7c1dab0d 8136 gfc_symbol *vtab;
4d382327 8137
7c1dab0d
JW
8138 if (!default_case)
8139 {
8140 /* Add a default case to hold the CLASS IS cases. */
8141 for (tail = code; tail->block; tail = tail->block) ;
11e5274a 8142 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
7c1dab0d 8143 tail = tail->block;
29a63d67
TB
8144 tail->ext.block.case_list = gfc_get_case ();
8145 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
7c1dab0d
JW
8146 tail->next = NULL;
8147 default_case = tail;
8148 }
eece1eb9 8149
7c1dab0d
JW
8150 /* More than one CLASS IS block? */
8151 if (class_is->block)
cf2b3c22 8152 {
7c1dab0d
JW
8153 gfc_code **c1,*c2;
8154 bool swapped;
8155 /* Sort CLASS IS blocks by extension level. */
8156 do
8157 {
8158 swapped = false;
8159 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8160 {
8161 c2 = (*c1)->block;
8162 /* F03:C817 (check for doubles). */
29a63d67
TB
8163 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8164 == c2->ext.block.case_list->ts.u.derived->hash_value)
7c1dab0d
JW
8165 {
8166 gfc_error ("Double CLASS IS block in SELECT TYPE "
29a63d67
TB
8167 "statement at %L",
8168 &c2->ext.block.case_list->where);
7c1dab0d
JW
8169 return;
8170 }
29a63d67
TB
8171 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8172 < c2->ext.block.case_list->ts.u.derived->attr.extension)
7c1dab0d
JW
8173 {
8174 /* Swap. */
8175 (*c1)->block = c2->block;
8176 c2->block = *c1;
8177 *c1 = c2;
8178 swapped = true;
8179 }
8180 }
8181 }
8182 while (swapped);
cf2b3c22 8183 }
4d382327 8184
7c1dab0d 8185 /* Generate IF chain. */
11e5274a 8186 if_st = gfc_get_code (EXEC_IF);
7c1dab0d
JW
8187 new_st = if_st;
8188 for (body = class_is; body; body = body->block)
8189 {
11e5274a 8190 new_st->block = gfc_get_code (EXEC_IF);
7c1dab0d 8191 new_st = new_st->block;
7c1dab0d
JW
8192 /* Set up IF condition: Call _gfortran_is_extension_of. */
8193 new_st->expr1 = gfc_get_expr ();
8194 new_st->expr1->expr_type = EXPR_FUNCTION;
8195 new_st->expr1->ts.type = BT_LOGICAL;
8196 new_st->expr1->ts.kind = 4;
8197 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8198 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8199 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8200 /* Set up arguments. */
8201 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8202 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
2c3d0cd3 8203 new_st->expr1->value.function.actual->expr->where = code->loc;
b04533af 8204 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
29a63d67 8205 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
7c1dab0d
JW
8206 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8207 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8208 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8209 new_st->next = body->next;
8210 }
8211 if (default_case->next)
8212 {
11e5274a 8213 new_st->block = gfc_get_code (EXEC_IF);
7c1dab0d 8214 new_st = new_st->block;
7c1dab0d
JW
8215 new_st->next = default_case->next;
8216 }
4d382327 8217
7c1dab0d
JW
8218 /* Replace CLASS DEFAULT code by the IF chain. */
8219 default_case->next = if_st;
cf2b3c22
TB
8220 }
8221
8c91ab34
DK
8222 /* Resolve the internal code. This can not be done earlier because
8223 it requires that the sym->assoc of selectors is set already. */
8224 gfc_current_ns = ns;
8225 gfc_resolve_blocks (code->block, gfc_current_ns);
8226 gfc_current_ns = old_ns;
cf2b3c22 8227
ad3e2ad2 8228 resolve_select (code, true);
cf2b3c22
TB
8229}
8230
8231
0e6928d8
TS
8232/* Resolve a transfer statement. This is making sure that:
8233 -- a derived type being transferred has only non-pointer components
4d382327 8234 -- a derived type being transferred doesn't have private components, unless
8451584a 8235 it's being transferred from the module where the type was defined
0e6928d8
TS
8236 -- we're not trying to transfer a whole assumed size array. */
8237
8238static void
edf1eac2 8239resolve_transfer (gfc_code *code)
0e6928d8
TS
8240{
8241 gfc_typespec *ts;
8242 gfc_symbol *sym;
8243 gfc_ref *ref;
8244 gfc_expr *exp;
8245
a513927a 8246 exp = code->expr1;
0e6928d8 8247
771c5727
JD
8248 while (exp != NULL && exp->expr_type == EXPR_OP
8249 && exp->value.op.op == INTRINSIC_PARENTHESES)
8250 exp = exp->value.op.op1;
8251
49560f0c
PT
8252 if (exp && exp->expr_type == EXPR_NULL
8253 && code->ext.dt)
ea8ad3e5 8254 {
49560f0c
PT
8255 gfc_error ("Invalid context for NULL () intrinsic at %L",
8256 &exp->where);
ea8ad3e5
TB
8257 return;
8258 }
8259
771c5727
JD
8260 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
8261 && exp->expr_type != EXPR_FUNCTION))
0e6928d8
TS
8262 return;
8263
8e8dc060
DK
8264 /* If we are reading, the variable will be changed. Note that
8265 code->ext.dt may be NULL if the TRANSFER is related to
8266 an INQUIRE statement -- but in this case, we are not reading, either. */
8267 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
524af0d6
JB
8268 && !gfc_check_vardef_context (exp, false, false, false,
8269 _("item in READ")))
8e8dc060
DK
8270 return;
8271
0e6928d8
TS
8272 sym = exp->symtree->n.sym;
8273 ts = &sym->ts;
8274
8275 /* Go to actual component transferred. */
6cf860a2 8276 for (ref = exp->ref; ref; ref = ref->next)
0e6928d8
TS
8277 if (ref->type == REF_COMPONENT)
8278 ts = &ref->u.c.component->ts;
8279
d5656544
TB
8280 if (ts->type == BT_CLASS)
8281 {
8282 /* FIXME: Test for defined input/output. */
8283 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8284 "it is processed by a defined input/output procedure",
8285 &code->loc);
8286 return;
8287 }
8288
0e6928d8
TS
8289 if (ts->type == BT_DERIVED)
8290 {
8291 /* Check that transferred derived type doesn't contain POINTER
8292 components. */
bc21d315 8293 if (ts->u.derived->attr.pointer_comp)
0e6928d8 8294 {
d8155bf5
TB
8295 gfc_error ("Data transfer element at %L cannot have POINTER "
8296 "components unless it is processed by a defined "
8297 "input/output procedure", &code->loc);
0e6928d8
TS
8298 return;
8299 }
8300
357f98e5
JW
8301 /* F08:C935. */
8302 if (ts->u.derived->attr.proc_pointer_comp)
8303 {
8304 gfc_error ("Data transfer element at %L cannot have "
8305 "procedure pointer components", &code->loc);
8306 return;
8307 }
8308
bc21d315 8309 if (ts->u.derived->attr.alloc_comp)
5046aff5 8310 {
d8155bf5
TB
8311 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8312 "components unless it is processed by a defined "
8313 "input/output procedure", &code->loc);
5046aff5
PT
8314 return;
8315 }
8316
cadddfdd
TB
8317 /* C_PTR and C_FUNPTR have private components which means they can not
8318 be printed. However, if -std=gnu and not -pedantic, allow
8319 the component to be printed to help debugging. */
8320 if (ts->u.derived->ts.f90_type == BT_VOID)
8321 {
524af0d6
JB
8322 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8323 "cannot have PRIVATE components", &code->loc))
cadddfdd
TB
8324 return;
8325 }
8326 else if (derived_inaccessible (ts->u.derived))
0e6928d8
TS
8327 {
8328 gfc_error ("Data transfer element at %L cannot have "
8329 "PRIVATE components",&code->loc);
8330 return;
8331 }
8332 }
8333
f2ce74d1 8334 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
0e6928d8
TS
8335 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8336 {
8337 gfc_error ("Data transfer element at %L cannot be a full reference to "
8338 "an assumed-size array", &code->loc);
8339 return;
8340 }
8341}
8342
8343
6de9cd9a
DN
8344/*********** Toplevel code resolution subroutines ***********/
8345
0615f923 8346/* Find the set of labels that are reachable from this block. We also
d80c695f 8347 record the last statement in each block. */
4d382327 8348
0615f923 8349static void
d80c695f 8350find_reachable_labels (gfc_code *block)
0615f923
TS
8351{
8352 gfc_code *c;
8353
8354 if (!block)
8355 return;
8356
8357 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8358
d80c695f
TS
8359 /* Collect labels in this block. We don't keep those corresponding
8360 to END {IF|SELECT}, these are checked in resolve_branch by going
8361 up through the code_stack. */
0615f923
TS
8362 for (c = block; c; c = c->next)
8363 {
df1a69f6 8364 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
0615f923 8365 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
0615f923
TS
8366 }
8367
8368 /* Merge with labels from parent block. */
8369 if (cs_base->prev)
8370 {
8371 gcc_assert (cs_base->prev->reachable_labels);
8372 bitmap_ior_into (cs_base->reachable_labels,
8373 cs_base->prev->reachable_labels);
8374 }
8375}
8376
d0a4a61c 8377
5493aa17
TB
8378static void
8379resolve_lock_unlock (gfc_code *code)
8380{
fea54935
TB
8381 if (code->expr1->ts.type != BT_DERIVED
8382 || code->expr1->expr_type != EXPR_VARIABLE
8383 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8384 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8385 || code->expr1->rank != 0
3b6fa7a5
TB
8386 || (!gfc_is_coarray (code->expr1) && !gfc_is_coindexed (code->expr1)))
8387 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8388 &code->expr1->where);
5493aa17
TB
8389
8390 /* Check STAT. */
8391 if (code->expr2
8392 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8393 || code->expr2->expr_type != EXPR_VARIABLE))
8394 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8395 &code->expr2->where);
8396
fea54935 8397 if (code->expr2
524af0d6
JB
8398 && !gfc_check_vardef_context (code->expr2, false, false, false,
8399 _("STAT variable")))
fea54935
TB
8400 return;
8401
5493aa17
TB
8402 /* Check ERRMSG. */
8403 if (code->expr3
8404 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8405 || code->expr3->expr_type != EXPR_VARIABLE))
8406 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8407 &code->expr3->where);
8408
fea54935 8409 if (code->expr3
524af0d6
JB
8410 && !gfc_check_vardef_context (code->expr3, false, false, false,
8411 _("ERRMSG variable")))
fea54935
TB
8412 return;
8413
5493aa17
TB
8414 /* Check ACQUIRED_LOCK. */
8415 if (code->expr4
8416 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8417 || code->expr4->expr_type != EXPR_VARIABLE))
8418 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8419 "variable", &code->expr4->where);
fea54935
TB
8420
8421 if (code->expr4
524af0d6
JB
8422 && !gfc_check_vardef_context (code->expr4, false, false, false,
8423 _("ACQUIRED_LOCK variable")))
fea54935 8424 return;
5493aa17
TB
8425}
8426
8427
d0a4a61c
TB
8428static void
8429resolve_sync (gfc_code *code)
8430{
8431 /* Check imageset. The * case matches expr1 == NULL. */
8432 if (code->expr1)
8433 {
8434 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8435 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8436 "INTEGER expression", &code->expr1->where);
8437 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8438 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8439 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8440 &code->expr1->where);
8441 else if (code->expr1->expr_type == EXPR_ARRAY
524af0d6 8442 && gfc_simplify_expr (code->expr1, 0))
d0a4a61c
TB
8443 {
8444 gfc_constructor *cons;
b7e75771
JD
8445 cons = gfc_constructor_first (code->expr1->value.constructor);
8446 for (; cons; cons = gfc_constructor_next (cons))
d0a4a61c
TB
8447 if (cons->expr->expr_type == EXPR_CONSTANT
8448 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8449 gfc_error ("Imageset argument at %L must between 1 and "
8450 "num_images()", &cons->expr->where);
8451 }
8452 }
8453
8454 /* Check STAT. */
8455 if (code->expr2
8456 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8457 || code->expr2->expr_type != EXPR_VARIABLE))
8458 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8459 &code->expr2->where);
8460
8461 /* Check ERRMSG. */
8462 if (code->expr3
8463 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8464 || code->expr3->expr_type != EXPR_VARIABLE))
8465 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8466 &code->expr3->where);
8467}
8468
8469
d80c695f 8470/* Given a branch to a label, see if the branch is conforming.
0615f923 8471 The code node describes where the branch is located. */
6de9cd9a
DN
8472
8473static void
edf1eac2 8474resolve_branch (gfc_st_label *label, gfc_code *code)
6de9cd9a 8475{
6de9cd9a 8476 code_stack *stack;
6de9cd9a
DN
8477
8478 if (label == NULL)
8479 return;
6de9cd9a
DN
8480
8481 /* Step one: is this a valid branching target? */
8482
0615f923 8483 if (label->defined == ST_LABEL_UNKNOWN)
6de9cd9a 8484 {
0615f923
TS
8485 gfc_error ("Label %d referenced at %L is never defined", label->value,
8486 &label->where);
6de9cd9a
DN
8487 return;
8488 }
8489
f3e7b9d6 8490 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
6de9cd9a
DN
8491 {
8492 gfc_error ("Statement at %L is not a valid branch target statement "
0615f923 8493 "for the branch statement at %L", &label->where, &code->loc);
6de9cd9a
DN
8494 return;
8495 }
8496
8497 /* Step two: make sure this branch is not a branch to itself ;-) */
8498
8499 if (code->here == label)
8500 {
ab551054 8501 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6de9cd9a
DN
8502 return;
8503 }
8504
0615f923
TS
8505 /* Step three: See if the label is in the same block as the
8506 branching statement. The hard work has been done by setting up
8507 the bitmap reachable_labels. */
6de9cd9a 8508
d80c695f 8509 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
d0a4a61c
TB
8510 {
8511 /* Check now whether there is a CRITICAL construct; if so, check
8512 whether the label is still visible outside of the CRITICAL block,
8513 which is invalid. */
8514 for (stack = cs_base; stack; stack = stack->prev)
8c6a85e3
TB
8515 {
8516 if (stack->current->op == EXEC_CRITICAL
8517 && bitmap_bit_p (stack->reachable_labels, label->value))
8518 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8519 "label at %L", &code->loc, &label->where);
8520 else if (stack->current->op == EXEC_DO_CONCURRENT
8521 && bitmap_bit_p (stack->reachable_labels, label->value))
8522 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8523 "for label at %L", &code->loc, &label->where);
8524 }
d0a4a61c
TB
8525
8526 return;
8527 }
6de9cd9a 8528
d80c695f
TS
8529 /* Step four: If we haven't found the label in the bitmap, it may
8530 still be the label of the END of the enclosing block, in which
8531 case we find it by going up the code_stack. */
6de9cd9a 8532
0615f923 8533 for (stack = cs_base; stack; stack = stack->prev)
d0a4a61c
TB
8534 {
8535 if (stack->current->next && stack->current->next->here == label)
8536 break;
8537 if (stack->current->op == EXEC_CRITICAL)
8538 {
8539 /* Note: A label at END CRITICAL does not leave the CRITICAL
8540 construct as END CRITICAL is still part of it. */
8541 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
8542 " at %L", &code->loc, &label->where);
8543 return;
8544 }
8c6a85e3
TB
8545 else if (stack->current->op == EXEC_DO_CONCURRENT)
8546 {
8547 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8548 "label at %L", &code->loc, &label->where);
8549 return;
8550 }
d0a4a61c 8551 }
6de9cd9a 8552
d80c695f 8553 if (stack)
0615f923 8554 {
df1a69f6 8555 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
d80c695f 8556 return;
6de9cd9a 8557 }
0615f923 8558
d80c695f
TS
8559 /* The label is not in an enclosing block, so illegal. This was
8560 allowed in Fortran 66, so we allow it as extension. No
8561 further checks are necessary in this case. */
8562 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
8563 "as the GOTO statement at %L", &label->where,
8564 &code->loc);
8565 return;
6de9cd9a
DN
8566}
8567
8568
8569/* Check whether EXPR1 has the same shape as EXPR2. */
8570
524af0d6 8571static bool
6de9cd9a
DN
8572resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
8573{
8574 mpz_t shape[GFC_MAX_DIMENSIONS];
8575 mpz_t shape2[GFC_MAX_DIMENSIONS];
524af0d6 8576 bool result = false;
6de9cd9a
DN
8577 int i;
8578
8579 /* Compare the rank. */
8580 if (expr1->rank != expr2->rank)
8581 return result;
8582
8583 /* Compare the size of each dimension. */
8584 for (i=0; i<expr1->rank; i++)
8585 {
524af0d6 8586 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
edf1eac2 8587 goto ignore;
6de9cd9a 8588
524af0d6 8589 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
edf1eac2 8590 goto ignore;
6de9cd9a
DN
8591
8592 if (mpz_cmp (shape[i], shape2[i]))
edf1eac2 8593 goto over;
6de9cd9a
DN
8594 }
8595
8596 /* When either of the two expression is an assumed size array, we
8597 ignore the comparison of dimension sizes. */
8598ignore:
524af0d6 8599 result = true;
6de9cd9a
DN
8600
8601over:
7d7212ec
MM
8602 gfc_clear_shape (shape, i);
8603 gfc_clear_shape (shape2, i);
6de9cd9a
DN
8604 return result;
8605}
8606
8607
8608/* Check whether a WHERE assignment target or a WHERE mask expression
8609 has the same shape as the outmost WHERE mask expression. */
8610
8611static void
8612resolve_where (gfc_code *code, gfc_expr *mask)
8613{
8614 gfc_code *cblock;
8615 gfc_code *cnext;
8616 gfc_expr *e = NULL;
8617
8618 cblock = code->block;
8619
8620 /* Store the first WHERE mask-expr of the WHERE statement or construct.
8621 In case of nested WHERE, only the outmost one is stored. */
8622 if (mask == NULL) /* outmost WHERE */
a513927a 8623 e = cblock->expr1;
6de9cd9a
DN
8624 else /* inner WHERE */
8625 e = mask;
8626
8627 while (cblock)
8628 {
a513927a 8629 if (cblock->expr1)
edf1eac2
SK
8630 {
8631 /* Check if the mask-expr has a consistent shape with the
8632 outmost WHERE mask-expr. */
524af0d6 8633 if (!resolve_where_shape (cblock->expr1, e))
edf1eac2 8634 gfc_error ("WHERE mask at %L has inconsistent shape",
a513927a 8635 &cblock->expr1->where);
edf1eac2 8636 }
6de9cd9a
DN
8637
8638 /* the assignment statement of a WHERE statement, or the first
edf1eac2 8639 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
8640 cnext = cblock->next;
8641 while (cnext)
edf1eac2
SK
8642 {
8643 switch (cnext->op)
8644 {
8645 /* WHERE assignment statement */
8646 case EXEC_ASSIGN:
8647
8648 /* Check shape consistent for WHERE assignment target. */
524af0d6 8649 if (e && !resolve_where_shape (cnext->expr1, e))
edf1eac2 8650 gfc_error ("WHERE assignment target at %L has "
a513927a 8651 "inconsistent shape", &cnext->expr1->where);
edf1eac2
SK
8652 break;
8653
4d382327 8654
a00b8d1a
PT
8655 case EXEC_ASSIGN_CALL:
8656 resolve_call (cnext);
42cd23cb 8657 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 8658 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 8659 &cnext->ext.actual->expr->where);
a00b8d1a
PT
8660 break;
8661
edf1eac2
SK
8662 /* WHERE or WHERE construct is part of a where-body-construct */
8663 case EXEC_WHERE:
8664 resolve_where (cnext, e);
8665 break;
8666
8667 default:
8668 gfc_error ("Unsupported statement inside WHERE at %L",
8669 &cnext->loc);
8670 }
8671 /* the next statement within the same where-body-construct */
8672 cnext = cnext->next;
6de9cd9a
DN
8673 }
8674 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8675 cblock = cblock->block;
8676 }
8677}
8678
8679
6de9cd9a
DN
8680/* Resolve assignment in FORALL construct.
8681 NVAR is the number of FORALL index variables, and VAR_EXPR records the
8682 FORALL index variables. */
8683
8684static void
8685gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
8686{
8687 int n;
8688
8689 for (n = 0; n < nvar; n++)
8690 {
8691 gfc_symbol *forall_index;
8692
8693 forall_index = var_expr[n]->symtree->n.sym;
8694
8695 /* Check whether the assignment target is one of the FORALL index
edf1eac2 8696 variable. */
a513927a
SK
8697 if ((code->expr1->expr_type == EXPR_VARIABLE)
8698 && (code->expr1->symtree->n.sym == forall_index))
edf1eac2 8699 gfc_error ("Assignment to a FORALL index variable at %L",
a513927a 8700 &code->expr1->where);
6de9cd9a 8701 else
edf1eac2
SK
8702 {
8703 /* If one of the FORALL index variables doesn't appear in the
67cec813
PT
8704 assignment variable, then there could be a many-to-one
8705 assignment. Emit a warning rather than an error because the
8706 mask could be resolving this problem. */
524af0d6 8707 if (!find_forall_index (code->expr1, forall_index, 0))
67cec813
PT
8708 gfc_warning ("The FORALL with index '%s' is not used on the "
8709 "left side of the assignment at %L and so might "
8710 "cause multiple assignment to this object",
a513927a 8711 var_expr[n]->symtree->name, &code->expr1->where);
edf1eac2 8712 }
6de9cd9a
DN
8713 }
8714}
8715
8716
8717/* Resolve WHERE statement in FORALL construct. */
8718
8719static void
edf1eac2
SK
8720gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
8721 gfc_expr **var_expr)
8722{
6de9cd9a
DN
8723 gfc_code *cblock;
8724 gfc_code *cnext;
8725
8726 cblock = code->block;
8727 while (cblock)
8728 {
8729 /* the assignment statement of a WHERE statement, or the first
edf1eac2 8730 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
8731 cnext = cblock->next;
8732 while (cnext)
edf1eac2
SK
8733 {
8734 switch (cnext->op)
8735 {
8736 /* WHERE assignment statement */
8737 case EXEC_ASSIGN:
8738 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
8739 break;
4d382327 8740
a00b8d1a
PT
8741 /* WHERE operator assignment statement */
8742 case EXEC_ASSIGN_CALL:
8743 resolve_call (cnext);
42cd23cb 8744 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 8745 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 8746 &cnext->ext.actual->expr->where);
a00b8d1a 8747 break;
edf1eac2
SK
8748
8749 /* WHERE or WHERE construct is part of a where-body-construct */
8750 case EXEC_WHERE:
8751 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
8752 break;
8753
8754 default:
8755 gfc_error ("Unsupported statement inside WHERE at %L",
8756 &cnext->loc);
8757 }
8758 /* the next statement within the same where-body-construct */
8759 cnext = cnext->next;
8760 }
6de9cd9a
DN
8761 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
8762 cblock = cblock->block;
8763 }
8764}
8765
8766
8767/* Traverse the FORALL body to check whether the following errors exist:
8768 1. For assignment, check if a many-to-one assignment happens.
8769 2. For WHERE statement, check the WHERE body to see if there is any
8770 many-to-one assignment. */
8771
8772static void
8773gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
8774{
8775 gfc_code *c;
8776
8777 c = code->block->next;
8778 while (c)
8779 {
8780 switch (c->op)
edf1eac2
SK
8781 {
8782 case EXEC_ASSIGN:
8783 case EXEC_POINTER_ASSIGN:
8784 gfc_resolve_assign_in_forall (c, nvar, var_expr);
8785 break;
8786
a00b8d1a
PT
8787 case EXEC_ASSIGN_CALL:
8788 resolve_call (c);
8789 break;
8790
edf1eac2
SK
8791 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
8792 there is no need to handle it here. */
8793 case EXEC_FORALL:
8794 break;
8795 case EXEC_WHERE:
8796 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
8797 break;
8798 default:
8799 break;
8800 }
6de9cd9a
DN
8801 /* The next statement in the FORALL body. */
8802 c = c->next;
8803 }
8804}
8805
8806
0e6834af 8807/* Counts the number of iterators needed inside a forall construct, including
4d382327 8808 nested forall constructs. This is used to allocate the needed memory
0e6834af
MM
8809 in gfc_resolve_forall. */
8810
4d382327 8811static int
0e6834af
MM
8812gfc_count_forall_iterators (gfc_code *code)
8813{
8814 int max_iters, sub_iters, current_iters;
8815 gfc_forall_iterator *fa;
8816
8817 gcc_assert(code->op == EXEC_FORALL);
8818 max_iters = 0;
8819 current_iters = 0;
8820
8821 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8822 current_iters ++;
4d382327 8823
0e6834af
MM
8824 code = code->block->next;
8825
8826 while (code)
4d382327 8827 {
0e6834af
MM
8828 if (code->op == EXEC_FORALL)
8829 {
8830 sub_iters = gfc_count_forall_iterators (code);
8831 if (sub_iters > max_iters)
8832 max_iters = sub_iters;
8833 }
8834 code = code->next;
8835 }
8836
8837 return current_iters + max_iters;
8838}
8839
8840
6de9cd9a
DN
8841/* Given a FORALL construct, first resolve the FORALL iterator, then call
8842 gfc_resolve_forall_body to resolve the FORALL body. */
8843
6de9cd9a
DN
8844static void
8845gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
8846{
8847 static gfc_expr **var_expr;
8848 static int total_var = 0;
8849 static int nvar = 0;
0e6834af 8850 int old_nvar, tmp;
6de9cd9a 8851 gfc_forall_iterator *fa;
6de9cd9a
DN
8852 int i;
8853
0e6834af
MM
8854 old_nvar = nvar;
8855
6de9cd9a
DN
8856 /* Start to resolve a FORALL construct */
8857 if (forall_save == 0)
8858 {
8859 /* Count the total number of FORALL index in the nested FORALL
0e6834af
MM
8860 construct in order to allocate the VAR_EXPR with proper size. */
8861 total_var = gfc_count_forall_iterators (code);
6de9cd9a 8862
f7b529fa 8863 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
93acb62c 8864 var_expr = XCNEWVEC (gfc_expr *, total_var);
6de9cd9a
DN
8865 }
8866
8867 /* The information about FORALL iterator, including FORALL index start, end
8868 and stride. The FORALL index can not appear in start, end or stride. */
8869 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
8870 {
8871 /* Check if any outer FORALL index name is the same as the current
edf1eac2 8872 one. */
6de9cd9a 8873 for (i = 0; i < nvar; i++)
edf1eac2
SK
8874 {
8875 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
8876 {
8877 gfc_error ("An outer FORALL construct already has an index "
8878 "with this name %L", &fa->var->where);
8879 }
8880 }
6de9cd9a
DN
8881
8882 /* Record the current FORALL index. */
8883 var_expr[nvar] = gfc_copy_expr (fa->var);
8884
6de9cd9a 8885 nvar++;
0e6834af
MM
8886
8887 /* No memory leak. */
8888 gcc_assert (nvar <= total_var);
6de9cd9a
DN
8889 }
8890
8891 /* Resolve the FORALL body. */
8892 gfc_resolve_forall_body (code, nvar, var_expr);
8893
8894 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6c7a4dfd 8895 gfc_resolve_blocks (code->block, ns);
6de9cd9a 8896
0e6834af
MM
8897 tmp = nvar;
8898 nvar = old_nvar;
8899 /* Free only the VAR_EXPRs allocated in this frame. */
8900 for (i = nvar; i < tmp; i++)
8901 gfc_free_expr (var_expr[i]);
6de9cd9a 8902
0e6834af
MM
8903 if (nvar == 0)
8904 {
8905 /* We are in the outermost FORALL construct. */
8906 gcc_assert (forall_save == 0);
8907
8908 /* VAR_EXPR is not needed any more. */
cede9502 8909 free (var_expr);
0e6834af
MM
8910 total_var = 0;
8911 }
6de9cd9a
DN
8912}
8913
8914
9abe5e56
DK
8915/* Resolve a BLOCK construct statement. */
8916
8917static void
8918resolve_block_construct (gfc_code* code)
8919{
03af1e4c
DK
8920 /* Resolve the BLOCK's namespace. */
8921 gfc_resolve (code->ext.block.ns);
52bf62f9
DK
8922
8923 /* For an ASSOCIATE block, the associations (and their targets) are already
3e78238a 8924 resolved during resolve_symbol. */
9abe5e56
DK
8925}
8926
8927
8928/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
6de9cd9a
DN
8929 DO code nodes. */
8930
8931static void resolve_code (gfc_code *, gfc_namespace *);
8932
6c7a4dfd 8933void
edf1eac2 8934gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6de9cd9a 8935{
524af0d6 8936 bool t;
6de9cd9a
DN
8937
8938 for (; b; b = b->block)
8939 {
a513927a 8940 t = gfc_resolve_expr (b->expr1);
524af0d6
JB
8941 if (!gfc_resolve_expr (b->expr2))
8942 t = false;
6de9cd9a
DN
8943
8944 switch (b->op)
8945 {
8946 case EXEC_IF:
524af0d6 8947 if (t && b->expr1 != NULL
a513927a 8948 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
edf1eac2 8949 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 8950 &b->expr1->where);
6de9cd9a
DN
8951 break;
8952
8953 case EXEC_WHERE:
524af0d6 8954 if (t
a513927a
SK
8955 && b->expr1 != NULL
8956 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
edf1eac2 8957 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
a513927a 8958 &b->expr1->where);
6de9cd9a
DN
8959 break;
8960
edf1eac2 8961 case EXEC_GOTO:
79bd1948 8962 resolve_branch (b->label1, b);
edf1eac2 8963 break;
6de9cd9a 8964
9abe5e56
DK
8965 case EXEC_BLOCK:
8966 resolve_block_construct (b);
8967 break;
8968
6de9cd9a 8969 case EXEC_SELECT:
cf2b3c22 8970 case EXEC_SELECT_TYPE:
6de9cd9a
DN
8971 case EXEC_FORALL:
8972 case EXEC_DO:
8973 case EXEC_DO_WHILE:
8c6a85e3 8974 case EXEC_DO_CONCURRENT:
d0a4a61c 8975 case EXEC_CRITICAL:
5e805e44
JJ
8976 case EXEC_READ:
8977 case EXEC_WRITE:
8978 case EXEC_IOLENGTH:
6f0f0b2e 8979 case EXEC_WAIT:
6de9cd9a
DN
8980 break;
8981
6c7a4dfd
JJ
8982 case EXEC_OMP_ATOMIC:
8983 case EXEC_OMP_CRITICAL:
8984 case EXEC_OMP_DO:
8985 case EXEC_OMP_MASTER:
8986 case EXEC_OMP_ORDERED:
8987 case EXEC_OMP_PARALLEL:
8988 case EXEC_OMP_PARALLEL_DO:
8989 case EXEC_OMP_PARALLEL_SECTIONS:
8990 case EXEC_OMP_PARALLEL_WORKSHARE:
8991 case EXEC_OMP_SECTIONS:
8992 case EXEC_OMP_SINGLE:
a68ab351
JJ
8993 case EXEC_OMP_TASK:
8994 case EXEC_OMP_TASKWAIT:
20906c66 8995 case EXEC_OMP_TASKYIELD:
6c7a4dfd
JJ
8996 case EXEC_OMP_WORKSHARE:
8997 break;
8998
6de9cd9a 8999 default:
9abe5e56 9000 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
6de9cd9a
DN
9001 }
9002
9003 resolve_code (b->next, ns);
9004 }
9005}
9006
9007
c5422462 9008/* Does everything to resolve an ordinary assignment. Returns true
df2fba9e 9009 if this is an interface assignment. */
c5422462
PT
9010static bool
9011resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9012{
9013 bool rval = false;
9014 gfc_expr *lhs;
9015 gfc_expr *rhs;
9016 int llen = 0;
9017 int rlen = 0;
9018 int n;
9019 gfc_ref *ref;
83ba23b7 9020 symbol_attribute attr;
c5422462 9021
524af0d6 9022 if (gfc_extend_assign (code, ns))
c5422462 9023 {
4a44a72d
DK
9024 gfc_expr** rhsptr;
9025
9026 if (code->op == EXEC_ASSIGN_CALL)
c5422462 9027 {
4a44a72d
DK
9028 lhs = code->ext.actual->expr;
9029 rhsptr = &code->ext.actual->next->expr;
4a44a72d
DK
9030 }
9031 else
9032 {
9033 gfc_actual_arglist* args;
9034 gfc_typebound_proc* tbp;
9035
9036 gcc_assert (code->op == EXEC_COMPCALL);
9037
9038 args = code->expr1->value.compcall.actual;
9039 lhs = args->expr;
9040 rhsptr = &args->next->expr;
9041
9042 tbp = code->expr1->value.compcall.tbp;
9043 gcc_assert (!tbp->is_generic);
c5422462
PT
9044 }
9045
9046 /* Make a temporary rhs when there is a default initializer
9047 and rhs is the same symbol as the lhs. */
4a44a72d
DK
9048 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9049 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
16e520b6 9050 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
4a44a72d
DK
9051 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9052 *rhsptr = gfc_get_parentheses (*rhsptr);
c5422462
PT
9053
9054 return true;
9055 }
9056
a513927a 9057 lhs = code->expr1;
c5422462
PT
9058 rhs = code->expr2;
9059
00a4618b 9060 if (rhs->is_boz
524af0d6
JB
9061 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
9062 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
9063 &code->loc))
00a4618b
TB
9064 return false;
9065
9066 /* Handle the case of a BOZ literal on the RHS. */
9067 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9068 {
4956b1f1 9069 int rc;
00a4618b
TB
9070 if (gfc_option.warn_surprising)
9071 gfc_warning ("BOZ literal at %L is bitwise transferred "
9072 "non-integer symbol '%s'", &code->loc,
9073 lhs->symtree->n.sym->name);
9074
c7abc45c
TB
9075 if (!gfc_convert_boz (rhs, &lhs->ts))
9076 return false;
4956b1f1
TB
9077 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9078 {
9079 if (rc == ARITH_UNDERFLOW)
9080 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9081 ". This check can be disabled with the option "
9082 "-fno-range-check", &rhs->where);
9083 else if (rc == ARITH_OVERFLOW)
9084 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9085 ". This check can be disabled with the option "
9086 "-fno-range-check", &rhs->where);
9087 else if (rc == ARITH_NAN)
9088 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9089 ". This check can be disabled with the option "
9090 "-fno-range-check", &rhs->where);
9091 return false;
9092 }
00a4618b
TB
9093 }
9094
c5422462
PT
9095 if (lhs->ts.type == BT_CHARACTER
9096 && gfc_option.warn_character_truncation)
9097 {
bc21d315
JW
9098 if (lhs->ts.u.cl != NULL
9099 && lhs->ts.u.cl->length != NULL
9100 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9101 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
c5422462
PT
9102
9103 if (rhs->expr_type == EXPR_CONSTANT)
9104 rlen = rhs->value.character.length;
9105
bc21d315 9106 else if (rhs->ts.u.cl != NULL
4a44a72d 9107 && rhs->ts.u.cl->length != NULL
bc21d315
JW
9108 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9109 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
c5422462
PT
9110
9111 if (rlen && llen && rlen > llen)
9112 gfc_warning_now ("CHARACTER expression will be truncated "
9113 "in assignment (%d/%d) at %L",
9114 llen, rlen, &code->loc);
9115 }
9116
9117 /* Ensure that a vector index expression for the lvalue is evaluated
908a2235 9118 to a temporary if the lvalue symbol is referenced in it. */
c5422462
PT
9119 if (lhs->rank)
9120 {
9121 for (ref = lhs->ref; ref; ref= ref->next)
9122 if (ref->type == REF_ARRAY)
9123 {
9124 for (n = 0; n < ref->u.ar.dimen; n++)
908a2235 9125 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
a68ab351
JJ
9126 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9127 ref->u.ar.start[n]))
c5422462
PT
9128 ref->u.ar.start[n]
9129 = gfc_get_parentheses (ref->u.ar.start[n]);
9130 }
9131 }
9132
9133 if (gfc_pure (NULL))
9134 {
c5422462
PT
9135 if (lhs->ts.type == BT_DERIVED
9136 && lhs->expr_type == EXPR_VARIABLE
bc21d315 9137 && lhs->ts.u.derived->attr.pointer_comp
4eceddd7 9138 && rhs->expr_type == EXPR_VARIABLE
d3a9eea2
TB
9139 && (gfc_impure_variable (rhs->symtree->n.sym)
9140 || gfc_is_coindexed (rhs)))
9141 {
9142 /* F2008, C1283. */
9143 if (gfc_is_coindexed (rhs))
9144 gfc_error ("Coindexed expression at %L is assigned to "
9145 "a derived type variable with a POINTER "
9146 "component in a PURE procedure",
9147 &rhs->where);
9148 else
9149 gfc_error ("The impure variable at %L is assigned to "
9150 "a derived type variable with a POINTER "
9151 "component in a PURE procedure (12.6)",
9152 &rhs->where);
9153 return rval;
9154 }
9155
9156 /* Fortran 2008, C1283. */
9157 if (gfc_is_coindexed (lhs))
c5422462 9158 {
d3a9eea2
TB
9159 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9160 "procedure", &rhs->where);
c5422462
PT
9161 return rval;
9162 }
9163 }
9164
f1f39033
PT
9165 if (gfc_implicit_pure (NULL))
9166 {
9167 if (lhs->expr_type == EXPR_VARIABLE
9168 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9169 && lhs->symtree->n.sym->ns != gfc_current_ns)
9170 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9171
9172 if (lhs->ts.type == BT_DERIVED
9173 && lhs->expr_type == EXPR_VARIABLE
9174 && lhs->ts.u.derived->attr.pointer_comp
9175 && rhs->expr_type == EXPR_VARIABLE
9176 && (gfc_impure_variable (rhs->symtree->n.sym)
9177 || gfc_is_coindexed (rhs)))
9178 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9179
9180 /* Fortran 2008, C1283. */
9181 if (gfc_is_coindexed (lhs))
9182 gfc_current_ns->proc_name->attr.implicit_pure = 0;
9183 }
9184
83ba23b7
TB
9185 /* F2008, 7.2.1.2. */
9186 attr = gfc_expr_attr (lhs);
9187 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9188 {
9189 if (attr.codimension)
9190 {
9191 gfc_error ("Assignment to polymorphic coarray at %L is not "
9192 "permitted", &lhs->where);
9193 return false;
9194 }
9195 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9196 "polymorphic variable at %L", &lhs->where))
9197 return false;
9198 if (!gfc_option.flag_realloc_lhs)
9199 {
9200 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9201 "requires -frealloc-lhs", &lhs->where);
9202 return false;
9203 }
9204 /* See PR 43366. */
9205 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9206 "is not yet supported", &lhs->where);
9207 return false;
9208 }
9209 else if (lhs->ts.type == BT_CLASS)
0ae278e7 9210 {
83ba23b7
TB
9211 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9212 "assignment at %L - check that there is a matching specific "
9213 "subroutine for '=' operator", &lhs->where);
0ae278e7
JW
9214 return false;
9215 }
9216
d3a9eea2
TB
9217 /* F2008, Section 7.2.1.2. */
9218 if (gfc_is_coindexed (lhs) && gfc_has_ultimate_allocatable (lhs))
9219 {
9220 gfc_error ("Coindexed variable must not be have an allocatable ultimate "
9221 "component in assignment at %L", &lhs->where);
9222 return false;
9223 }
9224
c5422462
PT
9225 gfc_check_assign (lhs, rhs, 1);
9226 return false;
9227}
9228
9abe5e56 9229
4d382327
AF
9230/* Add a component reference onto an expression. */
9231
9232static void
9233add_comp_ref (gfc_expr *e, gfc_component *c)
9234{
9235 gfc_ref **ref;
9236 ref = &(e->ref);
9237 while (*ref)
9238 ref = &((*ref)->next);
9239 *ref = gfc_get_ref ();
9240 (*ref)->type = REF_COMPONENT;
9241 (*ref)->u.c.sym = e->ts.u.derived;
9242 (*ref)->u.c.component = c;
9243 e->ts = c->ts;
9244
9245 /* Add a full array ref, as necessary. */
9246 if (c->as)
9247 {
9248 gfc_add_full_array_ref (e, c->as);
9249 e->rank = c->as->rank;
9250 }
9251}
9252
9253
9254/* Build an assignment. Keep the argument 'op' for future use, so that
9255 pointer assignments can be made. */
9256
9257static gfc_code *
9258build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9259 gfc_component *comp1, gfc_component *comp2, locus loc)
9260{
9261 gfc_code *this_code;
9262
11e5274a 9263 this_code = gfc_get_code (op);
4d382327
AF
9264 this_code->next = NULL;
9265 this_code->expr1 = gfc_copy_expr (expr1);
9266 this_code->expr2 = gfc_copy_expr (expr2);
9267 this_code->loc = loc;
9268 if (comp1 && comp2)
9269 {
9270 add_comp_ref (this_code->expr1, comp1);
9271 add_comp_ref (this_code->expr2, comp2);
9272 }
9273
9274 return this_code;
9275}
9276
9277
9278/* Makes a temporary variable expression based on the characteristics of
9279 a given variable expression. */
9280
9281static gfc_expr*
9282get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9283{
9284 static int serial = 0;
9285 char name[GFC_MAX_SYMBOL_LEN];
9286 gfc_symtree *tmp;
9287 gfc_array_spec *as;
9288 gfc_array_ref *aref;
9289 gfc_ref *ref;
9290
bbf38bcf 9291 sprintf (name, GFC_PREFIX("DA%d"), serial++);
4d382327
AF
9292 gfc_get_sym_tree (name, ns, &tmp, false);
9293 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9294
9295 as = NULL;
9296 ref = NULL;
9297 aref = NULL;
9298
9299 /* This function could be expanded to support other expression type
9300 but this is not needed here. */
9301 gcc_assert (e->expr_type == EXPR_VARIABLE);
9302
9303 /* Obtain the arrayspec for the temporary. */
9304 if (e->rank)
9305 {
9306 aref = gfc_find_array_ref (e);
9307 if (e->expr_type == EXPR_VARIABLE
9308 && e->symtree->n.sym->as == aref->as)
9309 as = aref->as;
9310 else
9311 {
9312 for (ref = e->ref; ref; ref = ref->next)
9313 if (ref->type == REF_COMPONENT
9314 && ref->u.c.component->as == aref->as)
9315 {
9316 as = aref->as;
9317 break;
9318 }
9319 }
9320 }
9321
9322 /* Add the attributes and the arrayspec to the temporary. */
9323 tmp->n.sym->attr = gfc_expr_attr (e);
9d827441
TB
9324 tmp->n.sym->attr.function = 0;
9325 tmp->n.sym->attr.result = 0;
9326 tmp->n.sym->attr.flavor = FL_VARIABLE;
9327
4d382327
AF
9328 if (as)
9329 {
9330 tmp->n.sym->as = gfc_copy_array_spec (as);
9331 if (!ref)
9332 ref = e->ref;
9333 if (as->type == AS_DEFERRED)
9334 tmp->n.sym->attr.allocatable = 1;
9335 }
9336 else
9337 tmp->n.sym->attr.dimension = 0;
9338
9339 gfc_set_sym_referenced (tmp->n.sym);
28a595fc 9340 gfc_commit_symbol (tmp->n.sym);
4d382327
AF
9341 e = gfc_lval_expr_from_sym (tmp->n.sym);
9342
9343 /* Should the lhs be a section, use its array ref for the
9344 temporary expression. */
9345 if (aref && aref->type != AR_FULL)
9346 {
9347 gfc_free_ref_list (e->ref);
9348 e->ref = gfc_copy_ref (ref);
9349 }
9350 return e;
9351}
9352
9353
9354/* Add one line of code to the code chain, making sure that 'head' and
9355 'tail' are appropriately updated. */
9356
9357static void
9358add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9359{
9360 gcc_assert (this_code);
9361 if (*head == NULL)
9362 *head = *tail = *this_code;
9363 else
9364 *tail = gfc_append_code (*tail, *this_code);
9365 *this_code = NULL;
9366}
9367
9368
9369/* Counts the potential number of part array references that would
9370 result from resolution of typebound defined assignments. */
9371
9372static int
9373nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9374{
9375 gfc_component *c;
9376 int c_depth = 0, t_depth;
9377
9378 for (c= derived->components; c; c = c->next)
9379 {
9380 if ((c->ts.type != BT_DERIVED
9381 || c->attr.pointer
9382 || c->attr.allocatable
9383 || c->attr.proc_pointer_comp
9384 || c->attr.class_pointer
9385 || c->attr.proc_pointer)
9386 && !c->attr.defined_assign_comp)
9387 continue;
9388
9389 if (c->as && c_depth == 0)
9390 c_depth = 1;
9391
9392 if (c->ts.u.derived->attr.defined_assign_comp)
9393 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9394 c->as ? 1 : 0);
9395 else
9396 t_depth = 0;
9397
9398 c_depth = t_depth > c_depth ? t_depth : c_depth;
9399 }
9400 return depth + c_depth;
9401}
9402
9403
9404/* Implement 7.2.1.3 of the F08 standard:
9405 "An intrinsic assignment where the variable is of derived type is
9406 performed as if each component of the variable were assigned from the
9407 corresponding component of expr using pointer assignment (7.2.2) for
9408 each pointer component, defined assignment for each nonpointer
9409 nonallocatable component of a type that has a type-bound defined
9410 assignment consistent with the component, intrinsic assignment for
9411 each other nonpointer nonallocatable component, ..."
9412
9413 The pointer assignments are taken care of by the intrinsic
9414 assignment of the structure itself. This function recursively adds
9415 defined assignments where required. The recursion is accomplished
9416 by calling resolve_code.
9417
9418 When the lhs in a defined assignment has intent INOUT, we need a
9419 temporary for the lhs. In pseudo-code:
9420
9421 ! Only call function lhs once.
9422 if (lhs is not a constant or an variable)
9423 temp_x = expr2
9424 expr2 => temp_x
9425 ! Do the intrinsic assignment
9426 expr1 = expr2
9427 ! Now do the defined assignments
9428 do over components with typebound defined assignment [%cmp]
9429 #if one component's assignment procedure is INOUT
9430 t1 = expr1
9431 #if expr2 non-variable
9432 temp_x = expr2
9433 expr2 => temp_x
9434 # endif
9435 expr1 = expr2
9436 # for each cmp
9437 t1%cmp {defined=} expr2%cmp
9438 expr1%cmp = t1%cmp
9439 #else
9440 expr1 = expr2
9441
9442 # for each cmp
9443 expr1%cmp {defined=} expr2%cmp
9444 #endif
9445 */
9446
9447/* The temporary assignments have to be put on top of the additional
9448 code to avoid the result being changed by the intrinsic assignment.
9449 */
9450static int component_assignment_level = 0;
9451static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9452
9453static void
9454generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9455{
9456 gfc_component *comp1, *comp2;
9457 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9458 gfc_expr *t1;
9459 int error_count, depth;
9460
9461 gfc_get_errors (NULL, &error_count);
9462
9463 /* Filter out continuing processing after an error. */
9464 if (error_count
9465 || (*code)->expr1->ts.type != BT_DERIVED
9466 || (*code)->expr2->ts.type != BT_DERIVED)
9467 return;
9468
9469 /* TODO: Handle more than one part array reference in assignments. */
9470 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9471 (*code)->expr1->rank ? 1 : 0);
9472 if (depth > 1)
9473 {
f4aef5ac 9474 gfc_warning ("TODO: type-bound defined assignment(s) at %L not "
4d382327
AF
9475 "done because multiple part array references would "
9476 "occur in intermediate expressions.", &(*code)->loc);
9477 return;
9478 }
9479
9480 component_assignment_level++;
9481
9482 /* Create a temporary so that functions get called only once. */
9483 if ((*code)->expr2->expr_type != EXPR_VARIABLE
9484 && (*code)->expr2->expr_type != EXPR_CONSTANT)
9485 {
9486 gfc_expr *tmp_expr;
9487
9488 /* Assign the rhs to the temporary. */
9489 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
9490 this_code = build_assignment (EXEC_ASSIGN,
9491 tmp_expr, (*code)->expr2,
9492 NULL, NULL, (*code)->loc);
9493 /* Add the code and substitute the rhs expression. */
9494 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
9495 gfc_free_expr ((*code)->expr2);
9496 (*code)->expr2 = tmp_expr;
9497 }
9498
9499 /* Do the intrinsic assignment. This is not needed if the lhs is one
9500 of the temporaries generated here, since the intrinsic assignment
9501 to the final result already does this. */
9502 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
9503 {
9504 this_code = build_assignment (EXEC_ASSIGN,
9505 (*code)->expr1, (*code)->expr2,
9506 NULL, NULL, (*code)->loc);
9507 add_code_to_chain (&this_code, &head, &tail);
9508 }
9509
9510 comp1 = (*code)->expr1->ts.u.derived->components;
9511 comp2 = (*code)->expr2->ts.u.derived->components;
9512
9513 t1 = NULL;
9514 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
9515 {
9516 bool inout = false;
9517
9518 /* The intrinsic assignment does the right thing for pointers
9519 of all kinds and allocatable components. */
9520 if (comp1->ts.type != BT_DERIVED
9521 || comp1->attr.pointer
9522 || comp1->attr.allocatable
9523 || comp1->attr.proc_pointer_comp
9524 || comp1->attr.class_pointer
9525 || comp1->attr.proc_pointer)
9526 continue;
9527
9528 /* Make an assigment for this component. */
4d382327
AF
9529 this_code = build_assignment (EXEC_ASSIGN,
9530 (*code)->expr1, (*code)->expr2,
9531 comp1, comp2, (*code)->loc);
9532
9533 /* Convert the assignment if there is a defined assignment for
9534 this type. Otherwise, using the call from resolve_code,
9535 recurse into its components. */
9536 resolve_code (this_code, ns);
9537
9538 if (this_code->op == EXEC_ASSIGN_CALL)
9539 {
4cbc9039 9540 gfc_formal_arglist *dummy_args;
4d382327
AF
9541 gfc_symbol *rsym;
9542 /* Check that there is a typebound defined assignment. If not,
9543 then this must be a module defined assignment. We cannot
9544 use the defined_assign_comp attribute here because it must
9545 be this derived type that has the defined assignment and not
9546 a parent type. */
9547 if (!(comp1->ts.u.derived->f2k_derived
9548 && comp1->ts.u.derived->f2k_derived
9549 ->tb_op[INTRINSIC_ASSIGN]))
9550 {
9551 gfc_free_statements (this_code);
9552 this_code = NULL;
9553 continue;
9554 }
9555
9556 /* If the first argument of the subroutine has intent INOUT
9557 a temporary must be generated and used instead. */
9558 rsym = this_code->resolved_sym;
4cbc9039
JW
9559 dummy_args = gfc_sym_get_dummy_args (rsym);
9560 if (dummy_args
9561 && dummy_args->sym->attr.intent == INTENT_INOUT)
4d382327
AF
9562 {
9563 gfc_code *temp_code;
9564 inout = true;
9565
9566 /* Build the temporary required for the assignment and put
9567 it at the head of the generated code. */
9568 if (!t1)
9569 {
9570 t1 = get_temp_from_expr ((*code)->expr1, ns);
9571 temp_code = build_assignment (EXEC_ASSIGN,
9572 t1, (*code)->expr1,
9573 NULL, NULL, (*code)->loc);
5ef7093d 9574
d14fc2c6
TB
9575 /* For allocatable LHS, check whether it is allocated. Note
9576 that allocatable components with defined assignment are
9577 not yet support. See PR 57696. */
9578 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
5ef7093d
TB
9579 {
9580 gfc_code *block;
d14fc2c6
TB
9581 gfc_expr *e =
9582 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
5ef7093d
TB
9583 block = gfc_get_code (EXEC_IF);
9584 block->block = gfc_get_code (EXEC_IF);
9585 block->block->expr1
9586 = gfc_build_intrinsic_call (ns,
d14fc2c6
TB
9587 GFC_ISYM_ALLOCATED, "allocated",
9588 (*code)->loc, 1, e);
5ef7093d
TB
9589 block->block->next = temp_code;
9590 temp_code = block;
9591 }
4d382327
AF
9592 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
9593 }
9594
9595 /* Replace the first actual arg with the component of the
9596 temporary. */
9597 gfc_free_expr (this_code->ext.actual->expr);
9598 this_code->ext.actual->expr = gfc_copy_expr (t1);
9599 add_comp_ref (this_code->ext.actual->expr, comp1);
5ef7093d 9600
d14fc2c6
TB
9601 /* If the LHS variable is allocatable and wasn't allocated and
9602 the temporary is allocatable, pointer assign the address of
9603 the freshly allocated LHS to the temporary. */
9604 if ((*code)->expr1->symtree->n.sym->attr.allocatable
9605 && gfc_expr_attr ((*code)->expr1).allocatable)
5ef7093d
TB
9606 {
9607 gfc_code *block;
71e482dc
TB
9608 gfc_expr *cond;
9609
9610 cond = gfc_get_expr ();
5ef7093d
TB
9611 cond->ts.type = BT_LOGICAL;
9612 cond->ts.kind = gfc_default_logical_kind;
9613 cond->expr_type = EXPR_OP;
9614 cond->where = (*code)->loc;
9615 cond->value.op.op = INTRINSIC_NOT;
9616 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
d14fc2c6
TB
9617 GFC_ISYM_ALLOCATED, "allocated",
9618 (*code)->loc, 1, gfc_copy_expr (t1));
5ef7093d
TB
9619 block = gfc_get_code (EXEC_IF);
9620 block->block = gfc_get_code (EXEC_IF);
9621 block->block->expr1 = cond;
9622 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9623 t1, (*code)->expr1,
9624 NULL, NULL, (*code)->loc);
9625 add_code_to_chain (&block, &head, &tail);
9626 }
4d382327 9627 }
71e482dc 9628 }
4d382327
AF
9629 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
9630 {
9631 /* Don't add intrinsic assignments since they are already
9632 effected by the intrinsic assignment of the structure. */
9633 gfc_free_statements (this_code);
9634 this_code = NULL;
9635 continue;
9636 }
9637
9638 add_code_to_chain (&this_code, &head, &tail);
9639
9640 if (t1 && inout)
9641 {
9642 /* Transfer the value to the final result. */
9643 this_code = build_assignment (EXEC_ASSIGN,
9644 (*code)->expr1, t1,
9645 comp1, comp2, (*code)->loc);
9646 add_code_to_chain (&this_code, &head, &tail);
9647 }
9648 }
9649
4d382327
AF
9650 /* Put the temporary assignments at the top of the generated code. */
9651 if (tmp_head && component_assignment_level == 1)
9652 {
9653 gfc_append_code (tmp_head, head);
9654 head = tmp_head;
9655 tmp_head = tmp_tail = NULL;
9656 }
9657
71e482dc
TB
9658 // If we did a pointer assignment - thus, we need to ensure that the LHS is
9659 // not accidentally deallocated. Hence, nullify t1.
9660 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
9661 && gfc_expr_attr ((*code)->expr1).allocatable)
9662 {
9663 gfc_code *block;
9664 gfc_expr *cond;
9665 gfc_expr *e;
9666
9667 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
9668 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
9669 (*code)->loc, 2, gfc_copy_expr (t1), e);
9670 block = gfc_get_code (EXEC_IF);
9671 block->block = gfc_get_code (EXEC_IF);
9672 block->block->expr1 = cond;
9673 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
9674 t1, gfc_get_null_expr (&(*code)->loc),
9675 NULL, NULL, (*code)->loc);
9676 gfc_append_code (tail, block);
9677 tail = block;
9678 }
9679
4d382327
AF
9680 /* Now attach the remaining code chain to the input code. Step on
9681 to the end of the new code since resolution is complete. */
9682 gcc_assert ((*code)->op == EXEC_ASSIGN);
9683 tail->next = (*code)->next;
9684 /* Overwrite 'code' because this would place the intrinsic assignment
9685 before the temporary for the lhs is created. */
9686 gfc_free_expr ((*code)->expr1);
9687 gfc_free_expr ((*code)->expr2);
9688 **code = *head;
71e482dc
TB
9689 if (head != tail)
9690 free (head);
4d382327
AF
9691 *code = tail;
9692
9693 component_assignment_level--;
9694}
9695
9696
6de9cd9a
DN
9697/* Given a block of code, recursively resolve everything pointed to by this
9698 code block. */
9699
9700static void
edf1eac2 9701resolve_code (gfc_code *code, gfc_namespace *ns)
6de9cd9a 9702{
6c7a4dfd 9703 int omp_workshare_save;
8c6a85e3 9704 int forall_save, do_concurrent_save;
6de9cd9a 9705 code_stack frame;
524af0d6 9706 bool t;
6de9cd9a
DN
9707
9708 frame.prev = cs_base;
9709 frame.head = code;
9710 cs_base = &frame;
9711
d80c695f 9712 find_reachable_labels (code);
0615f923 9713
6de9cd9a
DN
9714 for (; code; code = code->next)
9715 {
9716 frame.current = code;
d68bd5a8 9717 forall_save = forall_flag;
ce96d372 9718 do_concurrent_save = gfc_do_concurrent_flag;
6de9cd9a
DN
9719
9720 if (code->op == EXEC_FORALL)
9721 {
6de9cd9a 9722 forall_flag = 1;
6c7a4dfd 9723 gfc_resolve_forall (code, ns, forall_save);
d68bd5a8 9724 forall_flag = 2;
6c7a4dfd
JJ
9725 }
9726 else if (code->block)
9727 {
9728 omp_workshare_save = -1;
9729 switch (code->op)
9730 {
9731 case EXEC_OMP_PARALLEL_WORKSHARE:
9732 omp_workshare_save = omp_workshare_flag;
9733 omp_workshare_flag = 1;
9734 gfc_resolve_omp_parallel_blocks (code, ns);
9735 break;
9736 case EXEC_OMP_PARALLEL:
9737 case EXEC_OMP_PARALLEL_DO:
9738 case EXEC_OMP_PARALLEL_SECTIONS:
a68ab351 9739 case EXEC_OMP_TASK:
6c7a4dfd
JJ
9740 omp_workshare_save = omp_workshare_flag;
9741 omp_workshare_flag = 0;
9742 gfc_resolve_omp_parallel_blocks (code, ns);
9743 break;
9744 case EXEC_OMP_DO:
9745 gfc_resolve_omp_do_blocks (code, ns);
9746 break;
d1039125 9747 case EXEC_SELECT_TYPE:
8c91ab34
DK
9748 /* Blocks are handled in resolve_select_type because we have
9749 to transform the SELECT TYPE into ASSOCIATE first. */
d1039125 9750 break;
8c6a85e3 9751 case EXEC_DO_CONCURRENT:
ce96d372 9752 gfc_do_concurrent_flag = 1;
8c6a85e3 9753 gfc_resolve_blocks (code->block, ns);
ce96d372 9754 gfc_do_concurrent_flag = 2;
8c6a85e3 9755 break;
6c7a4dfd
JJ
9756 case EXEC_OMP_WORKSHARE:
9757 omp_workshare_save = omp_workshare_flag;
9758 omp_workshare_flag = 1;
eea58adb 9759 /* FALL THROUGH */
6c7a4dfd
JJ
9760 default:
9761 gfc_resolve_blocks (code->block, ns);
9762 break;
9763 }
6de9cd9a 9764
6c7a4dfd
JJ
9765 if (omp_workshare_save != -1)
9766 omp_workshare_flag = omp_workshare_save;
9767 }
6de9cd9a 9768
524af0d6 9769 t = true;
713485cc 9770 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
a513927a 9771 t = gfc_resolve_expr (code->expr1);
d68bd5a8 9772 forall_flag = forall_save;
ce96d372 9773 gfc_do_concurrent_flag = do_concurrent_save;
d68bd5a8 9774
524af0d6
JB
9775 if (!gfc_resolve_expr (code->expr2))
9776 t = false;
6de9cd9a 9777
8460475b 9778 if (code->op == EXEC_ALLOCATE
524af0d6
JB
9779 && !gfc_resolve_expr (code->expr3))
9780 t = false;
8460475b 9781
6de9cd9a
DN
9782 switch (code->op)
9783 {
9784 case EXEC_NOP:
d80c695f 9785 case EXEC_END_BLOCK:
df1a69f6 9786 case EXEC_END_NESTED_BLOCK:
6de9cd9a 9787 case EXEC_CYCLE:
6de9cd9a
DN
9788 case EXEC_PAUSE:
9789 case EXEC_STOP:
d0a4a61c 9790 case EXEC_ERROR_STOP:
6de9cd9a
DN
9791 case EXEC_EXIT:
9792 case EXEC_CONTINUE:
9793 case EXEC_DT_END:
4a44a72d 9794 case EXEC_ASSIGN_CALL:
d0a4a61c
TB
9795 case EXEC_CRITICAL:
9796 break;
9797
9798 case EXEC_SYNC_ALL:
9799 case EXEC_SYNC_IMAGES:
9800 case EXEC_SYNC_MEMORY:
9801 resolve_sync (code);
0e9a445b
PT
9802 break;
9803
5493aa17
TB
9804 case EXEC_LOCK:
9805 case EXEC_UNLOCK:
9806 resolve_lock_unlock (code);
9807 break;
9808
3d79abbd 9809 case EXEC_ENTRY:
0e9a445b
PT
9810 /* Keep track of which entry we are up to. */
9811 current_entry_id = code->ext.entry->id;
6de9cd9a
DN
9812 break;
9813
9814 case EXEC_WHERE:
9815 resolve_where (code, NULL);
9816 break;
9817
9818 case EXEC_GOTO:
a513927a 9819 if (code->expr1 != NULL)
ce2df7c6 9820 {
a513927a 9821 if (code->expr1->ts.type != BT_INTEGER)
edf1eac2 9822 gfc_error ("ASSIGNED GOTO statement at %L requires an "
a513927a
SK
9823 "INTEGER variable", &code->expr1->where);
9824 else if (code->expr1->symtree->n.sym->attr.assign != 1)
edf1eac2 9825 gfc_error ("Variable '%s' has not been assigned a target "
a513927a
SK
9826 "label at %L", code->expr1->symtree->n.sym->name,
9827 &code->expr1->where);
ce2df7c6
FW
9828 }
9829 else
79bd1948 9830 resolve_branch (code->label1, code);
6de9cd9a
DN
9831 break;
9832
9833 case EXEC_RETURN:
a513927a
SK
9834 if (code->expr1 != NULL
9835 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
b6398823 9836 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
a513927a 9837 "INTEGER return specifier", &code->expr1->where);
6de9cd9a
DN
9838 break;
9839
6b591ec0 9840 case EXEC_INIT_ASSIGN:
5c71a5e0 9841 case EXEC_END_PROCEDURE:
6b591ec0
PT
9842 break;
9843
6de9cd9a 9844 case EXEC_ASSIGN:
524af0d6 9845 if (!t)
6de9cd9a
DN
9846 break;
9847
524af0d6
JB
9848 if (!gfc_check_vardef_context (code->expr1, false, false, false,
9849 _("assignment")))
8c91ab34
DK
9850 break;
9851
c5422462 9852 if (resolve_ordinary_assign (code, ns))
664e411b
JW
9853 {
9854 if (code->op == EXEC_COMPCALL)
9855 goto compcall;
9856 else
9857 goto call;
9858 }
4d382327
AF
9859
9860 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
9861 if (code->expr1->ts.type == BT_DERIVED
9862 && code->expr1->ts.u.derived->attr.defined_assign_comp)
9863 generate_component_assignments (&code, ns);
9864
6de9cd9a
DN
9865 break;
9866
9867 case EXEC_LABEL_ASSIGN:
79bd1948 9868 if (code->label1->defined == ST_LABEL_UNKNOWN)
edf1eac2 9869 gfc_error ("Label %d referenced at %L is never defined",
79bd1948 9870 code->label1->value, &code->label1->where);
524af0d6 9871 if (t
a513927a
SK
9872 && (code->expr1->expr_type != EXPR_VARIABLE
9873 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
9874 || code->expr1->symtree->n.sym->ts.kind
edf1eac2 9875 != gfc_default_integer_kind
a513927a 9876 || code->expr1->symtree->n.sym->as != NULL))
40f2165e 9877 gfc_error ("ASSIGN statement at %L requires a scalar "
a513927a 9878 "default INTEGER variable", &code->expr1->where);
6de9cd9a
DN
9879 break;
9880
9881 case EXEC_POINTER_ASSIGN:
8c91ab34
DK
9882 {
9883 gfc_expr* e;
6de9cd9a 9884
524af0d6 9885 if (!t)
8c91ab34
DK
9886 break;
9887
9888 /* This is both a variable definition and pointer assignment
9889 context, so check both of them. For rank remapping, a final
9890 array ref may be present on the LHS and fool gfc_expr_attr
9891 used in gfc_check_vardef_context. Remove it. */
9892 e = remove_last_array_ref (code->expr1);
57bf28ea 9893 t = gfc_check_vardef_context (e, true, false, false,
fea54935 9894 _("pointer assignment"));
524af0d6 9895 if (t)
57bf28ea 9896 t = gfc_check_vardef_context (e, false, false, false,
fea54935 9897 _("pointer assignment"));
8c91ab34 9898 gfc_free_expr (e);
524af0d6 9899 if (!t)
8c91ab34
DK
9900 break;
9901
9902 gfc_check_pointer_assign (code->expr1, code->expr2);
9903 break;
9904 }
6de9cd9a
DN
9905
9906 case EXEC_ARITHMETIC_IF:
524af0d6 9907 if (t
a513927a
SK
9908 && code->expr1->ts.type != BT_INTEGER
9909 && code->expr1->ts.type != BT_REAL)
6de9cd9a 9910 gfc_error ("Arithmetic IF statement at %L requires a numeric "
a513927a 9911 "expression", &code->expr1->where);
6de9cd9a 9912
79bd1948 9913 resolve_branch (code->label1, code);
6de9cd9a
DN
9914 resolve_branch (code->label2, code);
9915 resolve_branch (code->label3, code);
9916 break;
9917
9918 case EXEC_IF:
524af0d6 9919 if (t && code->expr1 != NULL
a513927a
SK
9920 && (code->expr1->ts.type != BT_LOGICAL
9921 || code->expr1->rank != 0))
6de9cd9a 9922 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 9923 &code->expr1->where);
6de9cd9a
DN
9924 break;
9925
9926 case EXEC_CALL:
9927 call:
9928 resolve_call (code);
9929 break;
9930
8e1f752a 9931 case EXEC_COMPCALL:
664e411b 9932 compcall:
6a943ee7 9933 resolve_typebound_subroutine (code);
8e1f752a
DK
9934 break;
9935
713485cc 9936 case EXEC_CALL_PPC:
9abe5e56 9937 resolve_ppc_call (code);
713485cc
JW
9938 break;
9939
6de9cd9a
DN
9940 case EXEC_SELECT:
9941 /* Select is complicated. Also, a SELECT construct could be
9942 a transformed computed GOTO. */
ad3e2ad2 9943 resolve_select (code, false);
6de9cd9a
DN
9944 break;
9945
cf2b3c22 9946 case EXEC_SELECT_TYPE:
8c91ab34 9947 resolve_select_type (code, ns);
cf2b3c22
TB
9948 break;
9949
9abe5e56 9950 case EXEC_BLOCK:
52bf62f9 9951 resolve_block_construct (code);
9abe5e56
DK
9952 break;
9953
6de9cd9a
DN
9954 case EXEC_DO:
9955 if (code->ext.iterator != NULL)
6c7a4dfd
JJ
9956 {
9957 gfc_iterator *iter = code->ext.iterator;
524af0d6 9958 if (gfc_resolve_iterator (iter, true, false))
6c7a4dfd
JJ
9959 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
9960 }
6de9cd9a
DN
9961 break;
9962
9963 case EXEC_DO_WHILE:
a513927a 9964 if (code->expr1 == NULL)
6de9cd9a 9965 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
524af0d6 9966 if (t
a513927a
SK
9967 && (code->expr1->rank != 0
9968 || code->expr1->ts.type != BT_LOGICAL))
6de9cd9a 9969 gfc_error ("Exit condition of DO WHILE loop at %L must be "
a513927a 9970 "a scalar LOGICAL expression", &code->expr1->where);
6de9cd9a
DN
9971 break;
9972
9973 case EXEC_ALLOCATE:
524af0d6 9974 if (t)
b9332b09 9975 resolve_allocate_deallocate (code, "ALLOCATE");
6de9cd9a
DN
9976
9977 break;
9978
9979 case EXEC_DEALLOCATE:
524af0d6 9980 if (t)
b9332b09 9981 resolve_allocate_deallocate (code, "DEALLOCATE");
6de9cd9a
DN
9982
9983 break;
9984
9985 case EXEC_OPEN:
524af0d6 9986 if (!gfc_resolve_open (code->ext.open))
6de9cd9a
DN
9987 break;
9988
9989 resolve_branch (code->ext.open->err, code);
9990 break;
9991
9992 case EXEC_CLOSE:
524af0d6 9993 if (!gfc_resolve_close (code->ext.close))
6de9cd9a
DN
9994 break;
9995
9996 resolve_branch (code->ext.close->err, code);
9997 break;
9998
9999 case EXEC_BACKSPACE:
10000 case EXEC_ENDFILE:
10001 case EXEC_REWIND:
6403ec5f 10002 case EXEC_FLUSH:
524af0d6 10003 if (!gfc_resolve_filepos (code->ext.filepos))
6de9cd9a
DN
10004 break;
10005
10006 resolve_branch (code->ext.filepos->err, code);
10007 break;
10008
10009 case EXEC_INQUIRE:
524af0d6 10010 if (!gfc_resolve_inquire (code->ext.inquire))
8750f9cd
JB
10011 break;
10012
10013 resolve_branch (code->ext.inquire->err, code);
10014 break;
10015
10016 case EXEC_IOLENGTH:
6e45f57b 10017 gcc_assert (code->ext.inquire != NULL);
524af0d6 10018 if (!gfc_resolve_inquire (code->ext.inquire))
6de9cd9a
DN
10019 break;
10020
10021 resolve_branch (code->ext.inquire->err, code);
10022 break;
10023
6f0f0b2e 10024 case EXEC_WAIT:
524af0d6 10025 if (!gfc_resolve_wait (code->ext.wait))
6f0f0b2e
JD
10026 break;
10027
10028 resolve_branch (code->ext.wait->err, code);
10029 resolve_branch (code->ext.wait->end, code);
10030 resolve_branch (code->ext.wait->eor, code);
10031 break;
10032
6de9cd9a
DN
10033 case EXEC_READ:
10034 case EXEC_WRITE:
524af0d6 10035 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
6de9cd9a
DN
10036 break;
10037
10038 resolve_branch (code->ext.dt->err, code);
10039 resolve_branch (code->ext.dt->end, code);
10040 resolve_branch (code->ext.dt->eor, code);
10041 break;
10042
0e6928d8
TS
10043 case EXEC_TRANSFER:
10044 resolve_transfer (code);
10045 break;
10046
8c6a85e3 10047 case EXEC_DO_CONCURRENT:
6de9cd9a
DN
10048 case EXEC_FORALL:
10049 resolve_forall_iterators (code->ext.forall_iterator);
10050
d5656544
TB
10051 if (code->expr1 != NULL
10052 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10053 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
a513927a 10054 "expression", &code->expr1->where);
6de9cd9a
DN
10055 break;
10056
6c7a4dfd
JJ
10057 case EXEC_OMP_ATOMIC:
10058 case EXEC_OMP_BARRIER:
10059 case EXEC_OMP_CRITICAL:
10060 case EXEC_OMP_FLUSH:
10061 case EXEC_OMP_DO:
10062 case EXEC_OMP_MASTER:
10063 case EXEC_OMP_ORDERED:
10064 case EXEC_OMP_SECTIONS:
10065 case EXEC_OMP_SINGLE:
a68ab351 10066 case EXEC_OMP_TASKWAIT:
20906c66 10067 case EXEC_OMP_TASKYIELD:
6c7a4dfd
JJ
10068 case EXEC_OMP_WORKSHARE:
10069 gfc_resolve_omp_directive (code, ns);
10070 break;
10071
10072 case EXEC_OMP_PARALLEL:
10073 case EXEC_OMP_PARALLEL_DO:
10074 case EXEC_OMP_PARALLEL_SECTIONS:
10075 case EXEC_OMP_PARALLEL_WORKSHARE:
a68ab351 10076 case EXEC_OMP_TASK:
6c7a4dfd
JJ
10077 omp_workshare_save = omp_workshare_flag;
10078 omp_workshare_flag = 0;
10079 gfc_resolve_omp_directive (code, ns);
10080 omp_workshare_flag = omp_workshare_save;
10081 break;
10082
6de9cd9a
DN
10083 default:
10084 gfc_internal_error ("resolve_code(): Bad statement code");
10085 }
10086 }
10087
10088 cs_base = frame.prev;
10089}
10090
10091
10092/* Resolve initial values and make sure they are compatible with
10093 the variable. */
10094
10095static void
edf1eac2 10096resolve_values (gfc_symbol *sym)
6de9cd9a 10097{
524af0d6 10098 bool t;
80f95228 10099
22c30bc0 10100 if (sym->value == NULL)
6de9cd9a
DN
10101 return;
10102
80f95228
JW
10103 if (sym->value->expr_type == EXPR_STRUCTURE)
10104 t= resolve_structure_cons (sym->value, 1);
4d382327 10105 else
80f95228
JW
10106 t = gfc_resolve_expr (sym->value);
10107
524af0d6 10108 if (!t)
6de9cd9a
DN
10109 return;
10110
e35e87dc 10111 gfc_check_assign_symbol (sym, NULL, sym->value);
6de9cd9a
DN
10112}
10113
10114
a8b3b0b6
CR
10115/* Verify any BIND(C) derived types in the namespace so we can report errors
10116 for them once, rather than for each variable declared of that type. */
10117
10118static void
10119resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10120{
10121 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10122 && derived_sym->attr.is_bind_c == 1)
10123 verify_bind_c_derived_type (derived_sym);
4d382327 10124
a8b3b0b6
CR
10125 return;
10126}
10127
10128
4d382327 10129/* Verify that any binding labels used in a given namespace do not collide
77f8682b
TB
10130 with the names or binding labels of any global symbols. Multiple INTERFACE
10131 for the same procedure are permitted. */
a8b3b0b6
CR
10132
10133static void
10134gfc_verify_binding_labels (gfc_symbol *sym)
10135{
77f8682b
TB
10136 gfc_gsymbol *gsym;
10137 const char *module;
10138
10139 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10140 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10141 return;
10142
10143 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10144
10145 if (sym->module)
10146 module = sym->module;
10147 else if (sym->ns && sym->ns->proc_name
10148 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10149 module = sym->ns->proc_name->name;
10150 else if (sym->ns && sym->ns->parent
10151 && sym->ns && sym->ns->parent->proc_name
10152 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10153 module = sym->ns->parent->proc_name->name;
10154 else
10155 module = NULL;
4d382327 10156
77f8682b
TB
10157 if (!gsym
10158 || (!gsym->defined
10159 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
a8b3b0b6 10160 {
77f8682b
TB
10161 if (!gsym)
10162 gsym = gfc_get_gsymbol (sym->binding_label);
10163 gsym->where = sym->declared_at;
10164 gsym->sym_name = sym->name;
10165 gsym->binding_label = sym->binding_label;
10166 gsym->binding_label = sym->binding_label;
10167 gsym->ns = sym->ns;
10168 gsym->mod_name = module;
10169 if (sym->attr.function)
10170 gsym->type = GSYM_FUNCTION;
10171 else if (sym->attr.subroutine)
10172 gsym->type = GSYM_SUBROUTINE;
10173 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10174 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10175 return;
10176 }
10177
10178 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10179 {
10180 gfc_error ("Variable %s with binding label %s at %L uses the same global "
10181 "identifier as entity at %L", sym->name,
10182 sym->binding_label, &sym->declared_at, &gsym->where);
10183 /* Clear the binding label to prevent checking multiple times. */
10184 sym->binding_label = NULL;
a8b3b0b6 10185
a8b3b0b6 10186 }
77f8682b
TB
10187 else if (sym->attr.flavor == FL_VARIABLE
10188 && (strcmp (module, gsym->mod_name) != 0
10189 || strcmp (sym->name, gsym->sym_name) != 0))
10190 {
10191 /* This can only happen if the variable is defined in a module - if it
10192 isn't the same module, reject it. */
10193 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
10194 "the same global identifier as entity at %L from module %s",
10195 sym->name, module, sym->binding_label,
10196 &sym->declared_at, &gsym->where, gsym->mod_name);
10197 sym->binding_label = NULL;
10198 }
10199 else if ((sym->attr.function || sym->attr.subroutine)
10200 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10201 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10202 && sym != gsym->ns->proc_name
10203 && (strcmp (gsym->sym_name, sym->name) != 0
10204 || module != gsym->mod_name
10205 || (module && strcmp (module, gsym->mod_name) != 0)))
10206 {
10207 /* Print an error if the procdure is defined multiple times; we have to
10208 exclude references to the same procedure via module association or
10209 multiple checks for the same procedure. */
10210 gfc_error ("Procedure %s with binding label %s at %L uses the same "
10211 "global identifier as entity at %L", sym->name,
10212 sym->binding_label, &sym->declared_at, &gsym->where);
10213 sym->binding_label = NULL;
10214 }
a8b3b0b6
CR
10215}
10216
10217
2ed8d224
PT
10218/* Resolve an index expression. */
10219
524af0d6 10220static bool
edf1eac2 10221resolve_index_expr (gfc_expr *e)
2ed8d224 10222{
524af0d6
JB
10223 if (!gfc_resolve_expr (e))
10224 return false;
2ed8d224 10225
524af0d6
JB
10226 if (!gfc_simplify_expr (e, 0))
10227 return false;
2ed8d224 10228
524af0d6
JB
10229 if (!gfc_specification_expr (e))
10230 return false;
2ed8d224 10231
524af0d6 10232 return true;
2ed8d224
PT
10233}
10234
e69afb29 10235
110eec24
TS
10236/* Resolve a charlen structure. */
10237
524af0d6 10238static bool
110eec24
TS
10239resolve_charlen (gfc_charlen *cl)
10240{
b0c06816 10241 int i, k;
fd061185 10242 bool saved_specification_expr;
5cd09fac 10243
110eec24 10244 if (cl->resolved)
524af0d6 10245 return true;
110eec24
TS
10246
10247 cl->resolved = 1;
fd061185
TB
10248 saved_specification_expr = specification_expr;
10249 specification_expr = true;
0e9a445b 10250
239b48db 10251 if (cl->length_from_typespec)
0e9a445b 10252 {
524af0d6 10253 if (!gfc_resolve_expr (cl->length))
fd061185
TB
10254 {
10255 specification_expr = saved_specification_expr;
524af0d6 10256 return false;
fd061185 10257 }
239b48db 10258
524af0d6 10259 if (!gfc_simplify_expr (cl->length, 0))
fd061185
TB
10260 {
10261 specification_expr = saved_specification_expr;
524af0d6 10262 return false;
fd061185 10263 }
239b48db
TB
10264 }
10265 else
10266 {
239b48db 10267
524af0d6 10268 if (!resolve_index_expr (cl->length))
239b48db 10269 {
fd061185 10270 specification_expr = saved_specification_expr;
524af0d6 10271 return false;
239b48db 10272 }
0e9a445b 10273 }
110eec24 10274
5cd09fac
TS
10275 /* "If the character length parameter value evaluates to a negative
10276 value, the length of character entities declared is zero." */
815cd406 10277 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
5cd09fac 10278 {
5e1d6b4c
DK
10279 if (gfc_option.warn_surprising)
10280 gfc_warning_now ("CHARACTER variable at %L has negative length %d,"
10281 " the length has been set to zero",
10282 &cl->length->where, i);
b7e75771
JD
10283 gfc_replace_expr (cl->length,
10284 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
5cd09fac
TS
10285 }
10286
b0c06816
FXC
10287 /* Check that the character length is not too large. */
10288 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
10289 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
10290 && cl->length->ts.type == BT_INTEGER
10291 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
10292 {
10293 gfc_error ("String length at %L is too large", &cl->length->where);
fd061185 10294 specification_expr = saved_specification_expr;
524af0d6 10295 return false;
b0c06816
FXC
10296 }
10297
fd061185 10298 specification_expr = saved_specification_expr;
524af0d6 10299 return true;
2ed8d224
PT
10300}
10301
10302
66e4ab31 10303/* Test for non-constant shape arrays. */
3e1cf500
PT
10304
10305static bool
10306is_non_constant_shape_array (gfc_symbol *sym)
10307{
10308 gfc_expr *e;
10309 int i;
0e9a445b 10310 bool not_constant;
3e1cf500 10311
0e9a445b 10312 not_constant = false;
3e1cf500
PT
10313 if (sym->as != NULL)
10314 {
10315 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
10316 has not been simplified; parameter array references. Do the
10317 simplification now. */
be59db2d 10318 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
3e1cf500
PT
10319 {
10320 e = sym->as->lower[i];
524af0d6 10321 if (e && (!resolve_index_expr(e)
edf1eac2 10322 || !gfc_is_constant_expr (e)))
0e9a445b 10323 not_constant = true;
3e1cf500 10324 e = sym->as->upper[i];
524af0d6 10325 if (e && (!resolve_index_expr(e)
edf1eac2 10326 || !gfc_is_constant_expr (e)))
0e9a445b 10327 not_constant = true;
3e1cf500
PT
10328 }
10329 }
0e9a445b 10330 return not_constant;
3e1cf500
PT
10331}
10332
51b09ce3
AL
10333/* Given a symbol and an initialization expression, add code to initialize
10334 the symbol to the function entry. */
6b591ec0 10335static void
51b09ce3 10336build_init_assign (gfc_symbol *sym, gfc_expr *init)
6b591ec0
PT
10337{
10338 gfc_expr *lval;
6b591ec0
PT
10339 gfc_code *init_st;
10340 gfc_namespace *ns = sym->ns;
10341
6b591ec0
PT
10342 /* Search for the function namespace if this is a contained
10343 function without an explicit result. */
10344 if (sym->attr.function && sym == sym->result
edf1eac2 10345 && sym->name != sym->ns->proc_name->name)
6b591ec0
PT
10346 {
10347 ns = ns->contained;
10348 for (;ns; ns = ns->sibling)
10349 if (strcmp (ns->proc_name->name, sym->name) == 0)
10350 break;
10351 }
10352
10353 if (ns == NULL)
10354 {
10355 gfc_free_expr (init);
10356 return;
10357 }
10358
10359 /* Build an l-value expression for the result. */
08113c73 10360 lval = gfc_lval_expr_from_sym (sym);
6b591ec0
PT
10361
10362 /* Add the code at scope entry. */
11e5274a 10363 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6b591ec0
PT
10364 init_st->next = ns->code;
10365 ns->code = init_st;
10366
10367 /* Assign the default initializer to the l-value. */
10368 init_st->loc = sym->declared_at;
a513927a 10369 init_st->expr1 = lval;
6b591ec0
PT
10370 init_st->expr2 = init;
10371}
10372
51b09ce3
AL
10373/* Assign the default initializer to a derived type variable or result. */
10374
10375static void
10376apply_default_init (gfc_symbol *sym)
10377{
10378 gfc_expr *init = NULL;
10379
10380 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10381 return;
10382
bc21d315 10383 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
51b09ce3
AL
10384 init = gfc_default_initializer (&sym->ts);
10385
50f30801 10386 if (init == NULL && sym->ts.type != BT_CLASS)
51b09ce3
AL
10387 return;
10388
10389 build_init_assign (sym, init);
86e6a239 10390 sym->attr.referenced = 1;
51b09ce3
AL
10391}
10392
10393/* Build an initializer for a local integer, real, complex, logical, or
10394 character variable, based on the command line flags finit-local-zero,
4d382327 10395 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
51b09ce3
AL
10396 null if the symbol should not have a default initialization. */
10397static gfc_expr *
10398build_default_init_expr (gfc_symbol *sym)
10399{
10400 int char_len;
10401 gfc_expr *init_expr;
10402 int i;
51b09ce3
AL
10403
10404 /* These symbols should never have a default initialization. */
a3fd80ea 10405 if (sym->attr.allocatable
51b09ce3
AL
10406 || sym->attr.external
10407 || sym->attr.dummy
10408 || sym->attr.pointer
10409 || sym->attr.in_equivalence
10410 || sym->attr.in_common
10411 || sym->attr.data
10412 || sym->module
10413 || sym->attr.cray_pointee
a67cfde8
TB
10414 || sym->attr.cray_pointer
10415 || sym->assoc)
51b09ce3
AL
10416 return NULL;
10417
10418 /* Now we'll try to build an initializer expression. */
b7e75771
JD
10419 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
10420 &sym->declared_at);
10421
51b09ce3
AL
10422 /* We will only initialize integers, reals, complex, logicals, and
10423 characters, and only if the corresponding command-line flags
10424 were set. Otherwise, we free init_expr and return null. */
10425 switch (sym->ts.type)
4d382327 10426 {
51b09ce3
AL
10427 case BT_INTEGER:
10428 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
4d382327 10429 mpz_set_si (init_expr->value.integer,
51b09ce3
AL
10430 gfc_option.flag_init_integer_value);
10431 else
10432 {
10433 gfc_free_expr (init_expr);
10434 init_expr = NULL;
10435 }
10436 break;
10437
10438 case BT_REAL:
51b09ce3
AL
10439 switch (gfc_option.flag_init_real)
10440 {
346a77d1
TB
10441 case GFC_INIT_REAL_SNAN:
10442 init_expr->is_snan = 1;
10443 /* Fall through. */
51b09ce3
AL
10444 case GFC_INIT_REAL_NAN:
10445 mpfr_set_nan (init_expr->value.real);
10446 break;
10447
10448 case GFC_INIT_REAL_INF:
10449 mpfr_set_inf (init_expr->value.real, 1);
10450 break;
10451
10452 case GFC_INIT_REAL_NEG_INF:
10453 mpfr_set_inf (init_expr->value.real, -1);
10454 break;
10455
10456 case GFC_INIT_REAL_ZERO:
10457 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
10458 break;
10459
10460 default:
10461 gfc_free_expr (init_expr);
10462 init_expr = NULL;
10463 break;
10464 }
10465 break;
4d382327 10466
51b09ce3 10467 case BT_COMPLEX:
51b09ce3
AL
10468 switch (gfc_option.flag_init_real)
10469 {
346a77d1
TB
10470 case GFC_INIT_REAL_SNAN:
10471 init_expr->is_snan = 1;
10472 /* Fall through. */
51b09ce3 10473 case GFC_INIT_REAL_NAN:
eb6f9a86
KG
10474 mpfr_set_nan (mpc_realref (init_expr->value.complex));
10475 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
51b09ce3
AL
10476 break;
10477
10478 case GFC_INIT_REAL_INF:
eb6f9a86
KG
10479 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
10480 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
51b09ce3
AL
10481 break;
10482
10483 case GFC_INIT_REAL_NEG_INF:
eb6f9a86
KG
10484 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
10485 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
51b09ce3
AL
10486 break;
10487
10488 case GFC_INIT_REAL_ZERO:
eb6f9a86 10489 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
51b09ce3
AL
10490 break;
10491
10492 default:
10493 gfc_free_expr (init_expr);
10494 init_expr = NULL;
10495 break;
10496 }
10497 break;
4d382327 10498
51b09ce3
AL
10499 case BT_LOGICAL:
10500 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
10501 init_expr->value.logical = 0;
10502 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
10503 init_expr->value.logical = 1;
10504 else
10505 {
10506 gfc_free_expr (init_expr);
10507 init_expr = NULL;
10508 }
10509 break;
4d382327 10510
51b09ce3 10511 case BT_CHARACTER:
4d382327 10512 /* For characters, the length must be constant in order to
51b09ce3
AL
10513 create a default initializer. */
10514 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
bc21d315
JW
10515 && sym->ts.u.cl->length
10516 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
51b09ce3 10517 {
bc21d315 10518 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
51b09ce3 10519 init_expr->value.character.length = char_len;
00660189 10520 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
51b09ce3 10521 for (i = 0; i < char_len; i++)
00660189
FXC
10522 init_expr->value.character.string[i]
10523 = (unsigned char) gfc_option.flag_init_character_value;
51b09ce3
AL
10524 }
10525 else
10526 {
10527 gfc_free_expr (init_expr);
10528 init_expr = NULL;
10529 }
068ed5e0
TB
10530 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
10531 && sym->ts.u.cl->length)
10532 {
10533 gfc_actual_arglist *arg;
10534 init_expr = gfc_get_expr ();
10535 init_expr->where = sym->declared_at;
10536 init_expr->ts = sym->ts;
10537 init_expr->expr_type = EXPR_FUNCTION;
10538 init_expr->value.function.isym =
10539 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
10540 init_expr->value.function.name = "repeat";
10541 arg = gfc_get_actual_arglist ();
10542 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
10543 NULL, 1);
10544 arg->expr->value.character.string[0]
10545 = gfc_option.flag_init_character_value;
10546 arg->next = gfc_get_actual_arglist ();
10547 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
10548 init_expr->value.function.actual = arg;
10549 }
51b09ce3 10550 break;
4d382327 10551
51b09ce3
AL
10552 default:
10553 gfc_free_expr (init_expr);
10554 init_expr = NULL;
10555 }
10556 return init_expr;
10557}
10558
10559/* Add an initialization expression to a local variable. */
10560static void
10561apply_default_init_local (gfc_symbol *sym)
10562{
10563 gfc_expr *init = NULL;
10564
10565 /* The symbol should be a variable or a function return value. */
10566 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
10567 || (sym->attr.function && sym->result != sym))
10568 return;
10569
10570 /* Try to build the initializer expression. If we can't initialize
10571 this symbol, then init will be NULL. */
10572 init = build_default_init_expr (sym);
10573 if (init == NULL)
10574 return;
10575
068ed5e0
TB
10576 /* For saved variables, we don't want to add an initializer at function
10577 entry, so we just add a static initializer. Note that automatic variables
fab99ea2
TB
10578 are stack allocated even with -fno-automatic; we have also to exclude
10579 result variable, which are also nonstatic. */
4d382327 10580 if (sym->attr.save || sym->ns->save_all
fab99ea2 10581 || (gfc_option.flag_max_stack_var_size == 0 && !sym->attr.result
068ed5e0 10582 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
51b09ce3
AL
10583 {
10584 /* Don't clobber an existing initializer! */
10585 gcc_assert (sym->value == NULL);
10586 sym->value = init;
10587 return;
10588 }
10589
10590 build_init_assign (sym, init);
10591}
6b591ec0 10592
e69afb29 10593
66e4ab31 10594/* Resolution of common features of flavors variable and procedure. */
2ed8d224 10595
524af0d6 10596static bool
2ed8d224
PT
10597resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
10598{
fac665b2
TB
10599 gfc_array_spec *as;
10600
fac665b2
TB
10601 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
10602 as = CLASS_DATA (sym)->as;
10603 else
10604 as = sym->as;
10605
2ed8d224 10606 /* Constraints on deferred shape variable. */
fac665b2 10607 if (as == NULL || as->type != AS_DEFERRED)
2ed8d224 10608 {
fac665b2
TB
10609 bool pointer, allocatable, dimension;
10610
10611 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2ed8d224 10612 {
fac665b2
TB
10613 pointer = CLASS_DATA (sym)->attr.class_pointer;
10614 allocatable = CLASS_DATA (sym)->attr.allocatable;
10615 dimension = CLASS_DATA (sym)->attr.dimension;
10616 }
10617 else
10618 {
4cc70466 10619 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
fac665b2
TB
10620 allocatable = sym->attr.allocatable;
10621 dimension = sym->attr.dimension;
10622 }
10623
10624 if (allocatable)
10625 {
c62c6622 10626 if (dimension && as->type != AS_ASSUMED_RANK)
2fbd4117 10627 {
c62c6622
TB
10628 gfc_error ("Allocatable array '%s' at %L must have a deferred "
10629 "shape or assumed rank", sym->name, &sym->declared_at);
524af0d6 10630 return false;
2fbd4117 10631 }
524af0d6
JB
10632 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
10633 "'%s' at %L may not be ALLOCATABLE",
10634 sym->name, &sym->declared_at))
10635 return false;
2ed8d224
PT
10636 }
10637
c62c6622 10638 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
2ed8d224 10639 {
c62c6622
TB
10640 gfc_error ("Array pointer '%s' at %L must have a deferred shape or "
10641 "assumed rank", sym->name, &sym->declared_at);
524af0d6 10642 return false;
2ed8d224 10643 }
2ed8d224
PT
10644 }
10645 else
10646 {
cf2b3c22 10647 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12578be7 10648 && sym->ts.type != BT_CLASS && !sym->assoc)
2ed8d224
PT
10649 {
10650 gfc_error ("Array '%s' at %L cannot have a deferred shape",
10651 sym->name, &sym->declared_at);
524af0d6 10652 return false;
2ed8d224
PT
10653 }
10654 }
233961db
JW
10655
10656 /* Constraints on polymorphic variables. */
10657 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
10658 {
10659 /* F03:C502. */
d40477b4 10660 if (sym->attr.class_ok
8b704316 10661 && !sym->attr.select_type_temporary
524af0d6 10662 && !UNLIMITED_POLY (sym)
d40477b4 10663 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
233961db
JW
10664 {
10665 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
7a08eda1
JW
10666 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
10667 &sym->declared_at);
524af0d6 10668 return false;
233961db
JW
10669 }
10670
10671 /* F03:C509. */
3e78238a
DK
10672 /* Assume that use associated symbols were checked in the module ns.
10673 Class-variables that are associate-names are also something special
10674 and excepted from the test. */
10675 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
233961db
JW
10676 {
10677 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
10678 "or pointer", sym->name, &sym->declared_at);
524af0d6 10679 return false;
233961db
JW
10680 }
10681 }
4d382327 10682
524af0d6 10683 return true;
2ed8d224
PT
10684}
10685
edf1eac2 10686
448d2cd2
TS
10687/* Additional checks for symbols with flavor variable and derived
10688 type. To be called from resolve_fl_variable. */
10689
524af0d6 10690static bool
9de88093 10691resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
448d2cd2 10692{
cf2b3c22 10693 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
448d2cd2
TS
10694
10695 /* Check to see if a derived type is blocked from being host
10696 associated by the presence of another class I symbol in the same
10697 namespace. 14.6.1.3 of the standard and the discussion on
10698 comp.lang.fortran. */
bc21d315 10699 if (sym->ns != sym->ts.u.derived->ns
448d2cd2
TS
10700 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
10701 {
10702 gfc_symbol *s;
bc21d315 10703 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
c3f34952
TB
10704 if (s && s->attr.generic)
10705 s = gfc_find_dt_in_generic (s);
334e912a 10706 if (s && s->attr.flavor != FL_DERIVED)
448d2cd2
TS
10707 {
10708 gfc_error ("The type '%s' cannot be host associated at %L "
10709 "because it is blocked by an incompatible object "
10710 "of the same name declared at %L",
bc21d315 10711 sym->ts.u.derived->name, &sym->declared_at,
448d2cd2 10712 &s->declared_at);
524af0d6 10713 return false;
448d2cd2
TS
10714 }
10715 }
10716
10717 /* 4th constraint in section 11.3: "If an object of a type for which
10718 component-initialization is specified (R429) appears in the
10719 specification-part of a module and does not have the ALLOCATABLE
10720 or POINTER attribute, the object shall have the SAVE attribute."
10721
10722 The check for initializers is performed with
16e520b6 10723 gfc_has_default_initializer because gfc_default_initializer generates
448d2cd2 10724 a hidden default for allocatable components. */
9de88093 10725 if (!(sym->value || no_init_flag) && sym->ns->proc_name
448d2cd2
TS
10726 && sym->ns->proc_name->attr.flavor == FL_MODULE
10727 && !sym->ns->save_all && !sym->attr.save
10728 && !sym->attr.pointer && !sym->attr.allocatable
16e520b6 10729 && gfc_has_default_initializer (sym->ts.u.derived)
524af0d6
JB
10730 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
10731 "'%s' at %L, needed due to the default "
10732 "initialization", sym->name, &sym->declared_at))
10733 return false;
448d2cd2
TS
10734
10735 /* Assign default initializer. */
10736 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9de88093 10737 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
448d2cd2
TS
10738 {
10739 sym->value = gfc_default_initializer (&sym->ts);
10740 }
10741
524af0d6 10742 return true;
448d2cd2
TS
10743}
10744
10745
2ed8d224
PT
10746/* Resolve symbols with flavor variable. */
10747
524af0d6 10748static bool
2ed8d224
PT
10749resolve_fl_variable (gfc_symbol *sym, int mp_flag)
10750{
9de88093 10751 int no_init_flag, automatic_flag;
2ed8d224 10752 gfc_expr *e;
edf1eac2 10753 const char *auto_save_msg;
fd061185 10754 bool saved_specification_expr;
0e9a445b 10755
9de88093 10756 auto_save_msg = "Automatic object '%s' at %L cannot have the "
0e9a445b 10757 "SAVE attribute";
2ed8d224 10758
524af0d6
JB
10759 if (!resolve_fl_var_and_proc (sym, mp_flag))
10760 return false;
110eec24 10761
0e9a445b
PT
10762 /* Set this flag to check that variables are parameters of all entries.
10763 This check is effected by the call to gfc_resolve_expr through
10764 is_non_constant_shape_array. */
fd061185
TB
10765 saved_specification_expr = specification_expr;
10766 specification_expr = true;
0e9a445b 10767
c4d4556f
TS
10768 if (sym->ns->proc_name
10769 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10770 || sym->ns->proc_name->attr.is_main_program)
10771 && !sym->attr.use_assoc
edf1eac2
SK
10772 && !sym->attr.allocatable
10773 && !sym->attr.pointer
10774 && is_non_constant_shape_array (sym))
2ed8d224 10775 {
c4d4556f
TS
10776 /* The shape of a main program or module array needs to be
10777 constant. */
10778 gfc_error ("The module or main program array '%s' at %L must "
10779 "have constant shape", sym->name, &sym->declared_at);
fd061185 10780 specification_expr = saved_specification_expr;
524af0d6 10781 return false;
2ed8d224
PT
10782 }
10783
e69afb29
SK
10784 /* Constraints on deferred type parameter. */
10785 if (sym->ts.deferred && !(sym->attr.pointer || sym->attr.allocatable))
10786 {
10787 gfc_error ("Entity '%s' at %L has a deferred type parameter and "
10788 "requires either the pointer or allocatable attribute",
10789 sym->name, &sym->declared_at);
fd061185 10790 specification_expr = saved_specification_expr;
524af0d6 10791 return false;
e69afb29
SK
10792 }
10793
2ed8d224
PT
10794 if (sym->ts.type == BT_CHARACTER)
10795 {
10796 /* Make sure that character string variables with assumed length are
10797 dummy arguments. */
bc21d315 10798 e = sym->ts.u.cl->length;
e69afb29 10799 if (e == NULL && !sym->attr.dummy && !sym->attr.result
8b704316 10800 && !sym->ts.deferred && !sym->attr.select_type_temporary)
2ed8d224
PT
10801 {
10802 gfc_error ("Entity with assumed character length at %L must be a "
10803 "dummy argument or a PARAMETER", &sym->declared_at);
fd061185 10804 specification_expr = saved_specification_expr;
524af0d6 10805 return false;
2ed8d224
PT
10806 }
10807
80f95228 10808 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
0e9a445b
PT
10809 {
10810 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
fd061185 10811 specification_expr = saved_specification_expr;
524af0d6 10812 return false;
0e9a445b
PT
10813 }
10814
2ed8d224 10815 if (!gfc_is_constant_expr (e)
edf1eac2 10816 && !(e->expr_type == EXPR_VARIABLE
30228b61
JW
10817 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
10818 {
10819 if (!sym->attr.use_assoc && sym->ns->proc_name
10820 && (sym->ns->proc_name->attr.flavor == FL_MODULE
10821 || sym->ns->proc_name->attr.is_main_program))
10822 {
10823 gfc_error ("'%s' at %L must have constant character length "
10824 "in this context", sym->name, &sym->declared_at);
fd061185 10825 specification_expr = saved_specification_expr;
524af0d6 10826 return false;
30228b61
JW
10827 }
10828 if (sym->attr.in_common)
10829 {
10830 gfc_error ("COMMON variable '%s' at %L must have constant "
10831 "character length", sym->name, &sym->declared_at);
fd061185 10832 specification_expr = saved_specification_expr;
524af0d6 10833 return false;
30228b61 10834 }
2ed8d224
PT
10835 }
10836 }
10837
51b09ce3
AL
10838 if (sym->value == NULL && sym->attr.referenced)
10839 apply_default_init_local (sym); /* Try to apply a default initialization. */
10840
9de88093
TS
10841 /* Determine if the symbol may not have an initializer. */
10842 no_init_flag = automatic_flag = 0;
2ed8d224 10843 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9de88093
TS
10844 || sym->attr.intrinsic || sym->attr.result)
10845 no_init_flag = 1;
be59db2d 10846 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9de88093 10847 && is_non_constant_shape_array (sym))
2ed8d224 10848 {
9de88093 10849 no_init_flag = automatic_flag = 1;
0e9a445b 10850
5349080d
TB
10851 /* Also, they must not have the SAVE attribute.
10852 SAVE_IMPLICIT is checked below. */
9f3761c5
TB
10853 if (sym->as && sym->attr.codimension)
10854 {
10855 int corank = sym->as->corank;
10856 sym->as->corank = 0;
10857 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
10858 sym->as->corank = corank;
10859 }
10860 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
0e9a445b
PT
10861 {
10862 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
fd061185 10863 specification_expr = saved_specification_expr;
524af0d6 10864 return false;
0e9a445b 10865 }
448d2cd2 10866 }
2ed8d224 10867
7a99defe
SK
10868 /* Ensure that any initializer is simplified. */
10869 if (sym->value)
10870 gfc_simplify_expr (sym->value, 1);
10871
2ed8d224 10872 /* Reject illegal initializers. */
9de88093 10873 if (!sym->mark && sym->value)
2ed8d224 10874 {
da285ce8
JW
10875 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
10876 && CLASS_DATA (sym)->attr.allocatable))
2ed8d224
PT
10877 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
10878 sym->name, &sym->declared_at);
10879 else if (sym->attr.external)
10880 gfc_error ("External '%s' at %L cannot have an initializer",
10881 sym->name, &sym->declared_at);
145bdc2c
PT
10882 else if (sym->attr.dummy
10883 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
2ed8d224
PT
10884 gfc_error ("Dummy '%s' at %L cannot have an initializer",
10885 sym->name, &sym->declared_at);
10886 else if (sym->attr.intrinsic)
10887 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
10888 sym->name, &sym->declared_at);
10889 else if (sym->attr.result)
10890 gfc_error ("Function result '%s' at %L cannot have an initializer",
10891 sym->name, &sym->declared_at);
9de88093 10892 else if (automatic_flag)
2ed8d224
PT
10893 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
10894 sym->name, &sym->declared_at);
145bdc2c
PT
10895 else
10896 goto no_init_error;
fd061185 10897 specification_expr = saved_specification_expr;
524af0d6 10898 return false;
2ed8d224
PT
10899 }
10900
145bdc2c 10901no_init_error:
cf2b3c22 10902 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
fd061185 10903 {
524af0d6 10904 bool res = resolve_fl_variable_derived (sym, no_init_flag);
fd061185
TB
10905 specification_expr = saved_specification_expr;
10906 return res;
10907 }
2ed8d224 10908
fd061185 10909 specification_expr = saved_specification_expr;
524af0d6 10910 return true;
2ed8d224
PT
10911}
10912
10913
10914/* Resolve a procedure. */
10915
524af0d6 10916static bool
2ed8d224
PT
10917resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
10918{
10919 gfc_formal_arglist *arg;
10920
10921 if (sym->attr.function
524af0d6
JB
10922 && !resolve_fl_var_and_proc (sym, mp_flag))
10923 return false;
110eec24 10924
92c59193 10925 if (sym->ts.type == BT_CHARACTER)
2ed8d224 10926 {
bc21d315 10927 gfc_charlen *cl = sym->ts.u.cl;
8111a921
PT
10928
10929 if (cl && cl->length && gfc_is_constant_expr (cl->length)
524af0d6
JB
10930 && !resolve_charlen (cl))
10931 return false;
8111a921 10932
d94be5e0
TB
10933 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10934 && sym->attr.proc == PROC_ST_FUNCTION)
92c59193 10935 {
d94be5e0
TB
10936 gfc_error ("Character-valued statement function '%s' at %L must "
10937 "have constant length", sym->name, &sym->declared_at);
524af0d6 10938 return false;
edf1eac2 10939 }
2ed8d224
PT
10940 }
10941
37e47ee9 10942 /* Ensure that derived type for are not of a private type. Internal
df2fba9e 10943 module procedures are excluded by 2.2.3.3 - i.e., they are not
b82feea5 10944 externally accessible and can access all the objects accessible in
66e4ab31 10945 the host. */
37e47ee9 10946 if (!(sym->ns->parent
edf1eac2 10947 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
6e2062b0 10948 && gfc_check_symbol_access (sym))
2ed8d224 10949 {
83b2e4e8
DF
10950 gfc_interface *iface;
10951
4cbc9039 10952 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
2ed8d224
PT
10953 {
10954 if (arg->sym
edf1eac2 10955 && arg->sym->ts.type == BT_DERIVED
bc21d315 10956 && !arg->sym->ts.u.derived->attr.use_assoc
6e2062b0 10957 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
524af0d6
JB
10958 && !gfc_notify_std (GFC_STD_F2003, "'%s' is of a PRIVATE type "
10959 "and cannot be a dummy argument"
10960 " of '%s', which is PUBLIC at %L",
10961 arg->sym->name, sym->name,
10962 &sym->declared_at))
2ed8d224 10963 {
2ed8d224 10964 /* Stop this message from recurring. */
bc21d315 10965 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
524af0d6 10966 return false;
2ed8d224
PT
10967 }
10968 }
83b2e4e8 10969
3bed9dd0
DF
10970 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10971 PRIVATE to the containing module. */
10972 for (iface = sym->generic; iface; iface = iface->next)
10973 {
4cbc9039 10974 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
3bed9dd0
DF
10975 {
10976 if (arg->sym
10977 && arg->sym->ts.type == BT_DERIVED
bc21d315 10978 && !arg->sym->ts.u.derived->attr.use_assoc
6e2062b0 10979 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
524af0d6
JB
10980 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
10981 "PUBLIC interface '%s' at %L "
10982 "takes dummy arguments of '%s' which "
10983 "is PRIVATE", iface->sym->name,
10984 sym->name, &iface->sym->declared_at,
10985 gfc_typename(&arg->sym->ts)))
3bed9dd0 10986 {
3bed9dd0 10987 /* Stop this message from recurring. */
bc21d315 10988 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
524af0d6 10989 return false;
3bed9dd0
DF
10990 }
10991 }
10992 }
10993
83b2e4e8
DF
10994 /* PUBLIC interfaces may expose PRIVATE procedures that take types
10995 PRIVATE to the containing module. */
10996 for (iface = sym->generic; iface; iface = iface->next)
10997 {
4cbc9039 10998 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
83b2e4e8
DF
10999 {
11000 if (arg->sym
11001 && arg->sym->ts.type == BT_DERIVED
bc21d315 11002 && !arg->sym->ts.u.derived->attr.use_assoc
6e2062b0 11003 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
524af0d6
JB
11004 && !gfc_notify_std (GFC_STD_F2003, "Procedure '%s' in "
11005 "PUBLIC interface '%s' at %L takes "
11006 "dummy arguments of '%s' which is "
11007 "PRIVATE", iface->sym->name,
11008 sym->name, &iface->sym->declared_at,
11009 gfc_typename(&arg->sym->ts)))
83b2e4e8 11010 {
83b2e4e8 11011 /* Stop this message from recurring. */
bc21d315 11012 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
524af0d6 11013 return false;
83b2e4e8
DF
11014 }
11015 }
11016 }
2ed8d224
PT
11017 }
11018
8fb74da4
JW
11019 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11020 && !sym->attr.proc_pointer)
f8faa85e
DF
11021 {
11022 gfc_error ("Function '%s' at %L cannot have an initializer",
11023 sym->name, &sym->declared_at);
524af0d6 11024 return false;
f8faa85e
DF
11025 }
11026
e2ae1407 11027 /* An external symbol may not have an initializer because it is taken to be
8fb74da4
JW
11028 a procedure. Exception: Procedure Pointers. */
11029 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
2ed8d224
PT
11030 {
11031 gfc_error ("External object '%s' at %L may not have an initializer",
11032 sym->name, &sym->declared_at);
524af0d6 11033 return false;
2ed8d224
PT
11034 }
11035
d68bd5a8
PT
11036 /* An elemental function is required to return a scalar 12.7.1 */
11037 if (sym->attr.elemental && sym->attr.function && sym->as)
11038 {
11039 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
11040 "result", sym->name, &sym->declared_at);
11041 /* Reset so that the error only occurs once. */
11042 sym->attr.elemental = 0;
524af0d6 11043 return false;
d68bd5a8
PT
11044 }
11045
1ca99f75
TB
11046 if (sym->attr.proc == PROC_ST_FUNCTION
11047 && (sym->attr.allocatable || sym->attr.pointer))
11048 {
11049 gfc_error ("Statement function '%s' at %L may not have pointer or "
11050 "allocatable attribute", sym->name, &sym->declared_at);
524af0d6 11051 return false;
1ca99f75
TB
11052 }
11053
2ed8d224
PT
11054 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11055 char-len-param shall not be array-valued, pointer-valued, recursive
11056 or pure. ....snip... A character value of * may only be used in the
11057 following ways: (i) Dummy arg of procedure - dummy associates with
11058 actual length; (ii) To declare a named constant; or (iii) External
11059 function - but length must be declared in calling scoping unit. */
11060 if (sym->attr.function
dd912331 11061 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
bc21d315 11062 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
2ed8d224
PT
11063 {
11064 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
edf1eac2 11065 || (sym->attr.recursive) || (sym->attr.pure))
2ed8d224
PT
11066 {
11067 if (sym->as && sym->as->rank)
11068 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11069 "array-valued", sym->name, &sym->declared_at);
11070
11071 if (sym->attr.pointer)
11072 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11073 "pointer-valued", sym->name, &sym->declared_at);
11074
11075 if (sym->attr.pure)
11076 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11077 "pure", sym->name, &sym->declared_at);
11078
11079 if (sym->attr.recursive)
11080 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
11081 "recursive", sym->name, &sym->declared_at);
11082
524af0d6 11083 return false;
2ed8d224
PT
11084 }
11085
11086 /* Appendix B.2 of the standard. Contained functions give an
8d51f26f
PT
11087 error anyway. Fixed-form is likely to be F77/legacy. Deferred
11088 character length is an F2003 feature. */
11089 if (!sym->attr.contained
11090 && gfc_current_form != FORM_FIXED
11091 && !sym->ts.deferred)
9717f7a1 11092 gfc_notify_std (GFC_STD_F95_OBS,
e2ab8b09 11093 "CHARACTER(*) function '%s' at %L",
2ed8d224
PT
11094 sym->name, &sym->declared_at);
11095 }
a8b3b0b6 11096
019c0e5d
TB
11097 /* F2008, C1218. */
11098 if (sym->attr.elemental)
11099 {
11100 if (sym->attr.proc_pointer)
11101 {
11102 gfc_error ("Procedure pointer '%s' at %L shall not be elemental",
11103 sym->name, &sym->declared_at);
11104 return false;
11105 }
11106 if (sym->attr.dummy)
11107 {
11108 gfc_error ("Dummy procedure '%s' at %L shall not be elemental",
11109 sym->name, &sym->declared_at);
11110 return false;
11111 }
11112 }
11113
a8b3b0b6
CR
11114 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11115 {
11116 gfc_formal_arglist *curr_arg;
aa5e22f0 11117 int has_non_interop_arg = 0;
a8b3b0b6 11118
524af0d6
JB
11119 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11120 sym->common_block))
a8b3b0b6
CR
11121 {
11122 /* Clear these to prevent looking at them again if there was an
11123 error. */
11124 sym->attr.is_bind_c = 0;
11125 sym->attr.is_c_interop = 0;
11126 sym->ts.is_c_interop = 0;
11127 }
11128 else
11129 {
11130 /* So far, no errors have been found. */
11131 sym->attr.is_c_interop = 1;
11132 sym->ts.is_c_interop = 1;
11133 }
4d382327 11134
4cbc9039 11135 curr_arg = gfc_sym_get_dummy_args (sym);
a8b3b0b6
CR
11136 while (curr_arg != NULL)
11137 {
11138 /* Skip implicitly typed dummy args here. */
aa5e22f0 11139 if (curr_arg->sym->attr.implicit_type == 0)
524af0d6 11140 if (!gfc_verify_c_interop_param (curr_arg->sym))
aa5e22f0
CR
11141 /* If something is found to fail, record the fact so we
11142 can mark the symbol for the procedure as not being
11143 BIND(C) to try and prevent multiple errors being
11144 reported. */
11145 has_non_interop_arg = 1;
4d382327 11146
a8b3b0b6
CR
11147 curr_arg = curr_arg->next;
11148 }
aa5e22f0
CR
11149
11150 /* See if any of the arguments were not interoperable and if so, clear
11151 the procedure symbol to prevent duplicate error messages. */
11152 if (has_non_interop_arg != 0)
11153 {
11154 sym->attr.is_c_interop = 0;
11155 sym->ts.is_c_interop = 0;
11156 sym->attr.is_bind_c = 0;
11157 }
a8b3b0b6 11158 }
4d382327 11159
3070bab4 11160 if (!sym->attr.proc_pointer)
beb4bd6c 11161 {
3070bab4
JW
11162 if (sym->attr.save == SAVE_EXPLICIT)
11163 {
11164 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
11165 "in '%s' at %L", sym->name, &sym->declared_at);
524af0d6 11166 return false;
3070bab4
JW
11167 }
11168 if (sym->attr.intent)
11169 {
11170 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
11171 "in '%s' at %L", sym->name, &sym->declared_at);
524af0d6 11172 return false;
3070bab4
JW
11173 }
11174 if (sym->attr.subroutine && sym->attr.result)
11175 {
11176 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
11177 "in '%s' at %L", sym->name, &sym->declared_at);
524af0d6 11178 return false;
3070bab4
JW
11179 }
11180 if (sym->attr.external && sym->attr.function
11181 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11182 || sym->attr.contained))
11183 {
11184 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
11185 "in '%s' at %L", sym->name, &sym->declared_at);
524af0d6 11186 return false;
3070bab4
JW
11187 }
11188 if (strcmp ("ppr@", sym->name) == 0)
11189 {
11190 gfc_error ("Procedure pointer result '%s' at %L "
11191 "is missing the pointer attribute",
11192 sym->ns->proc_name->name, &sym->declared_at);
524af0d6 11193 return false;
3070bab4 11194 }
beb4bd6c
JW
11195 }
11196
524af0d6 11197 return true;
110eec24
TS
11198}
11199
11200
34523524
DK
11201/* Resolve a list of finalizer procedures. That is, after they have hopefully
11202 been defined and we now know their defined arguments, check that they fulfill
11203 the requirements of the standard for procedures used as finalizers. */
11204
524af0d6 11205static bool
34523524
DK
11206gfc_resolve_finalizers (gfc_symbol* derived)
11207{
11208 gfc_finalizer* list;
11209 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
524af0d6 11210 bool result = true;
34523524
DK
11211 bool seen_scalar = false;
11212
11213 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
524af0d6 11214 return true;
34523524
DK
11215
11216 /* Walk over the list of finalizer-procedures, check them, and if any one
11217 does not fit in with the standard's definition, print an error and remove
11218 it from the list. */
11219 prev_link = &derived->f2k_derived->finalizers;
11220 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
11221 {
4cbc9039 11222 gfc_formal_arglist *dummy_args;
34523524
DK
11223 gfc_symbol* arg;
11224 gfc_finalizer* i;
11225 int my_rank;
11226
f6fad28e
DK
11227 /* Skip this finalizer if we already resolved it. */
11228 if (list->proc_tree)
11229 {
11230 prev_link = &(list->next);
11231 continue;
11232 }
11233
34523524 11234 /* Check this exists and is a SUBROUTINE. */
f6fad28e 11235 if (!list->proc_sym->attr.subroutine)
34523524
DK
11236 {
11237 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
f6fad28e 11238 list->proc_sym->name, &list->where);
34523524
DK
11239 goto error;
11240 }
11241
11242 /* We should have exactly one argument. */
4cbc9039
JW
11243 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
11244 if (!dummy_args || dummy_args->next)
34523524
DK
11245 {
11246 gfc_error ("FINAL procedure at %L must have exactly one argument",
11247 &list->where);
11248 goto error;
11249 }
4cbc9039 11250 arg = dummy_args->sym;
34523524
DK
11251
11252 /* This argument must be of our type. */
bc21d315 11253 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
34523524
DK
11254 {
11255 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
11256 &arg->declared_at, derived->name);
11257 goto error;
11258 }
11259
11260 /* It must neither be a pointer nor allocatable nor optional. */
11261 if (arg->attr.pointer)
11262 {
11263 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
11264 &arg->declared_at);
11265 goto error;
11266 }
11267 if (arg->attr.allocatable)
11268 {
11269 gfc_error ("Argument of FINAL procedure at %L must not be"
11270 " ALLOCATABLE", &arg->declared_at);
11271 goto error;
11272 }
11273 if (arg->attr.optional)
11274 {
11275 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
11276 &arg->declared_at);
11277 goto error;
11278 }
11279
11280 /* It must not be INTENT(OUT). */
11281 if (arg->attr.intent == INTENT_OUT)
11282 {
11283 gfc_error ("Argument of FINAL procedure at %L must not be"
11284 " INTENT(OUT)", &arg->declared_at);
11285 goto error;
11286 }
11287
11288 /* Warn if the procedure is non-scalar and not assumed shape. */
c62c6622 11289 if (gfc_option.warn_surprising && arg->as && arg->as->rank != 0
34523524
DK
11290 && arg->as->type != AS_ASSUMED_SHAPE)
11291 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
11292 " shape argument", &arg->declared_at);
11293
11294 /* Check that it does not match in kind and rank with a FINAL procedure
11295 defined earlier. To really loop over the *earlier* declarations,
11296 we need to walk the tail of the list as new ones were pushed at the
11297 front. */
11298 /* TODO: Handle kind parameters once they are implemented. */
11299 my_rank = (arg->as ? arg->as->rank : 0);
11300 for (i = list->next; i; i = i->next)
11301 {
4cbc9039
JW
11302 gfc_formal_arglist *dummy_args;
11303
34523524
DK
11304 /* Argument list might be empty; that is an error signalled earlier,
11305 but we nevertheless continued resolving. */
4cbc9039
JW
11306 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
11307 if (dummy_args)
34523524 11308 {
4cbc9039 11309 gfc_symbol* i_arg = dummy_args->sym;
34523524
DK
11310 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
11311 if (i_rank == my_rank)
11312 {
11313 gfc_error ("FINAL procedure '%s' declared at %L has the same"
11314 " rank (%d) as '%s'",
4d382327 11315 list->proc_sym->name, &list->where, my_rank,
f6fad28e 11316 i->proc_sym->name);
34523524
DK
11317 goto error;
11318 }
11319 }
11320 }
11321
11322 /* Is this the/a scalar finalizer procedure? */
11323 if (!arg->as || arg->as->rank == 0)
11324 seen_scalar = true;
11325
f6fad28e
DK
11326 /* Find the symtree for this procedure. */
11327 gcc_assert (!list->proc_tree);
11328 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
11329
34523524
DK
11330 prev_link = &list->next;
11331 continue;
11332
df2fba9e 11333 /* Remove wrong nodes immediately from the list so we don't risk any
34523524
DK
11334 troubles in the future when they might fail later expectations. */
11335error:
524af0d6 11336 result = false;
34523524
DK
11337 i = list;
11338 *prev_link = list->next;
11339 gfc_free_finalizer (i);
11340 }
11341
11342 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
11343 were nodes in the list, must have been for arrays. It is surely a good
11344 idea to have a scalar version there if there's something to finalize. */
524af0d6 11345 if (gfc_option.warn_surprising && result && !seen_scalar)
34523524
DK
11346 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
11347 " defined at %L, suggest also scalar one",
11348 derived->name, &derived->declared_at);
11349
8e54f139 11350 gfc_find_derived_vtab (derived);
34523524
DK
11351 return result;
11352}
11353
11354
e157f736
DK
11355/* Check if two GENERIC targets are ambiguous and emit an error is they are. */
11356
524af0d6 11357static bool
e157f736
DK
11358check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
11359 const char* generic_name, locus where)
11360{
6f3ab30d
JW
11361 gfc_symbol *sym1, *sym2;
11362 const char *pass1, *pass2;
e157f736
DK
11363
11364 gcc_assert (t1->specific && t2->specific);
11365 gcc_assert (!t1->specific->is_generic);
11366 gcc_assert (!t2->specific->is_generic);
218e1228 11367 gcc_assert (t1->is_operator == t2->is_operator);
e157f736
DK
11368
11369 sym1 = t1->specific->u.specific->n.sym;
11370 sym2 = t2->specific->u.specific->n.sym;
11371
cf2b3c22 11372 if (sym1 == sym2)
524af0d6 11373 return true;
cf2b3c22 11374
e157f736
DK
11375 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
11376 if (sym1->attr.subroutine != sym2->attr.subroutine
11377 || sym1->attr.function != sym2->attr.function)
11378 {
11379 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
11380 " GENERIC '%s' at %L",
11381 sym1->name, sym2->name, generic_name, &where);
524af0d6 11382 return false;
e157f736
DK
11383 }
11384
11385 /* Compare the interfaces. */
6f3ab30d
JW
11386 if (t1->specific->nopass)
11387 pass1 = NULL;
11388 else if (t1->specific->pass_arg)
11389 pass1 = t1->specific->pass_arg;
11390 else
4cbc9039 11391 pass1 = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym)->sym->name;
6f3ab30d
JW
11392 if (t2->specific->nopass)
11393 pass2 = NULL;
11394 else if (t2->specific->pass_arg)
11395 pass2 = t2->specific->pass_arg;
11396 else
4cbc9039 11397 pass2 = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym)->sym->name;
218e1228 11398 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
6f3ab30d 11399 NULL, 0, pass1, pass2))
e157f736
DK
11400 {
11401 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
11402 sym1->name, sym2->name, generic_name, &where);
524af0d6 11403 return false;
e157f736
DK
11404 }
11405
524af0d6 11406 return true;
e157f736
DK
11407}
11408
11409
94747289
DK
11410/* Worker function for resolving a generic procedure binding; this is used to
11411 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
11412
11413 The difference between those cases is finding possible inherited bindings
11414 that are overridden, as one has to look for them in tb_sym_root,
11415 tb_uop_root or tb_op, respectively. Thus the caller must already find
11416 the super-type and set p->overridden correctly. */
e157f736 11417
524af0d6 11418static bool
94747289
DK
11419resolve_tb_generic_targets (gfc_symbol* super_type,
11420 gfc_typebound_proc* p, const char* name)
e157f736
DK
11421{
11422 gfc_tbp_generic* target;
11423 gfc_symtree* first_target;
e157f736 11424 gfc_symtree* inherited;
e157f736 11425
94747289 11426 gcc_assert (p && p->is_generic);
e157f736
DK
11427
11428 /* Try to find the specific bindings for the symtrees in our target-list. */
94747289
DK
11429 gcc_assert (p->u.generic);
11430 for (target = p->u.generic; target; target = target->next)
e157f736
DK
11431 if (!target->specific)
11432 {
11433 gfc_typebound_proc* overridden_tbp;
11434 gfc_tbp_generic* g;
11435 const char* target_name;
11436
11437 target_name = target->specific_st->name;
11438
11439 /* Defined for this type directly. */
aea18e92 11440 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
e157f736 11441 {
e34ccb4c 11442 target->specific = target->specific_st->n.tb;
e157f736
DK
11443 goto specific_found;
11444 }
11445
11446 /* Look for an inherited specific binding. */
11447 if (super_type)
11448 {
4a44a72d
DK
11449 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
11450 true, NULL);
e157f736
DK
11451
11452 if (inherited)
11453 {
e34ccb4c
DK
11454 gcc_assert (inherited->n.tb);
11455 target->specific = inherited->n.tb;
e157f736
DK
11456 goto specific_found;
11457 }
11458 }
11459
11460 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
94747289 11461 " at %L", target_name, name, &p->where);
524af0d6 11462 return false;
e157f736
DK
11463
11464 /* Once we've found the specific binding, check it is not ambiguous with
11465 other specifics already found or inherited for the same GENERIC. */
11466specific_found:
11467 gcc_assert (target->specific);
11468
11469 /* This must really be a specific binding! */
11470 if (target->specific->is_generic)
11471 {
11472 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
94747289 11473 " '%s' is GENERIC, too", name, &p->where, target_name);
524af0d6 11474 return false;
e157f736
DK
11475 }
11476
11477 /* Check those already resolved on this type directly. */
94747289 11478 for (g = p->u.generic; g; g = g->next)
e157f736 11479 if (g != target && g->specific
524af0d6
JB
11480 && !check_generic_tbp_ambiguity (target, g, name, p->where))
11481 return false;
e157f736
DK
11482
11483 /* Check for ambiguity with inherited specific targets. */
94747289 11484 for (overridden_tbp = p->overridden; overridden_tbp;
e157f736
DK
11485 overridden_tbp = overridden_tbp->overridden)
11486 if (overridden_tbp->is_generic)
11487 {
11488 for (g = overridden_tbp->u.generic; g; g = g->next)
11489 {
11490 gcc_assert (g->specific);
524af0d6
JB
11491 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
11492 return false;
e157f736
DK
11493 }
11494 }
11495 }
11496
11497 /* If we attempt to "overwrite" a specific binding, this is an error. */
94747289 11498 if (p->overridden && !p->overridden->is_generic)
e157f736
DK
11499 {
11500 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
94747289 11501 " the same name", name, &p->where);
524af0d6 11502 return false;
e157f736
DK
11503 }
11504
11505 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
11506 all must have the same attributes here. */
94747289 11507 first_target = p->u.generic->specific->u.specific;
e34ccb4c 11508 gcc_assert (first_target);
94747289
DK
11509 p->subroutine = first_target->n.sym->attr.subroutine;
11510 p->function = first_target->n.sym->attr.function;
e157f736 11511
524af0d6 11512 return true;
e157f736
DK
11513}
11514
11515
94747289
DK
11516/* Resolve a GENERIC procedure binding for a derived type. */
11517
524af0d6 11518static bool
94747289
DK
11519resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
11520{
11521 gfc_symbol* super_type;
11522
11523 /* Find the overridden binding if any. */
11524 st->n.tb->overridden = NULL;
11525 super_type = gfc_get_derived_super_type (derived);
11526 if (super_type)
11527 {
11528 gfc_symtree* overridden;
4a44a72d
DK
11529 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
11530 true, NULL);
94747289
DK
11531
11532 if (overridden && overridden->n.tb)
11533 st->n.tb->overridden = overridden->n.tb;
11534 }
11535
11536 /* Resolve using worker function. */
11537 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
11538}
11539
11540
b325faf9
DK
11541/* Retrieve the target-procedure of an operator binding and do some checks in
11542 common for intrinsic and user-defined type-bound operators. */
11543
11544static gfc_symbol*
11545get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
11546{
11547 gfc_symbol* target_proc;
11548
11549 gcc_assert (target->specific && !target->specific->is_generic);
11550 target_proc = target->specific->u.specific->n.sym;
11551 gcc_assert (target_proc);
11552
2e33ad21 11553 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
b325faf9
DK
11554 if (target->specific->nopass)
11555 {
11556 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
11557 return NULL;
11558 }
11559
11560 return target_proc;
11561}
11562
11563
94747289
DK
11564/* Resolve a type-bound intrinsic operator. */
11565
524af0d6 11566static bool
94747289
DK
11567resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
11568 gfc_typebound_proc* p)
11569{
11570 gfc_symbol* super_type;
11571 gfc_tbp_generic* target;
4d382327 11572
94747289
DK
11573 /* If there's already an error here, do nothing (but don't fail again). */
11574 if (p->error)
524af0d6 11575 return true;
94747289
DK
11576
11577 /* Operators should always be GENERIC bindings. */
11578 gcc_assert (p->is_generic);
11579
11580 /* Look for an overridden binding. */
11581 super_type = gfc_get_derived_super_type (derived);
11582 if (super_type && super_type->f2k_derived)
11583 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
4a44a72d 11584 op, true, NULL);
94747289
DK
11585 else
11586 p->overridden = NULL;
11587
11588 /* Resolve general GENERIC properties using worker function. */
524af0d6 11589 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
94747289
DK
11590 goto error;
11591
11592 /* Check the targets to be procedures of correct interface. */
11593 for (target = p->u.generic; target; target = target->next)
11594 {
11595 gfc_symbol* target_proc;
11596
b325faf9
DK
11597 target_proc = get_checked_tb_operator_target (target, p->where);
11598 if (!target_proc)
4a44a72d 11599 goto error;
94747289
DK
11600
11601 if (!gfc_check_operator_interface (target_proc, op, p->where))
4a44a72d 11602 goto error;
362aa474
JW
11603
11604 /* Add target to non-typebound operator list. */
11605 if (!target->specific->deferred && !derived->attr.use_assoc
474d486a 11606 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
362aa474
JW
11607 {
11608 gfc_interface *head, *intr;
524af0d6
JB
11609 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
11610 return false;
362aa474
JW
11611 head = derived->ns->op[op];
11612 intr = gfc_get_interface ();
11613 intr->sym = target_proc;
11614 intr->where = p->where;
11615 intr->next = head;
11616 derived->ns->op[op] = intr;
11617 }
94747289
DK
11618 }
11619
524af0d6 11620 return true;
94747289
DK
11621
11622error:
11623 p->error = 1;
524af0d6 11624 return false;
94747289
DK
11625}
11626
11627
11628/* Resolve a type-bound user operator (tree-walker callback). */
30b608eb
DK
11629
11630static gfc_symbol* resolve_bindings_derived;
524af0d6 11631static bool resolve_bindings_result;
30b608eb 11632
524af0d6 11633static bool check_uop_procedure (gfc_symbol* sym, locus where);
94747289
DK
11634
11635static void
11636resolve_typebound_user_op (gfc_symtree* stree)
11637{
11638 gfc_symbol* super_type;
11639 gfc_tbp_generic* target;
11640
11641 gcc_assert (stree && stree->n.tb);
11642
11643 if (stree->n.tb->error)
11644 return;
11645
11646 /* Operators should always be GENERIC bindings. */
11647 gcc_assert (stree->n.tb->is_generic);
11648
11649 /* Find overridden procedure, if any. */
11650 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11651 if (super_type && super_type->f2k_derived)
11652 {
11653 gfc_symtree* overridden;
11654 overridden = gfc_find_typebound_user_op (super_type, NULL,
4a44a72d 11655 stree->name, true, NULL);
94747289
DK
11656
11657 if (overridden && overridden->n.tb)
11658 stree->n.tb->overridden = overridden->n.tb;
11659 }
11660 else
11661 stree->n.tb->overridden = NULL;
11662
11663 /* Resolve basically using worker function. */
524af0d6 11664 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
94747289
DK
11665 goto error;
11666
11667 /* Check the targets to be functions of correct interface. */
11668 for (target = stree->n.tb->u.generic; target; target = target->next)
11669 {
11670 gfc_symbol* target_proc;
11671
b325faf9
DK
11672 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
11673 if (!target_proc)
11674 goto error;
94747289 11675
524af0d6 11676 if (!check_uop_procedure (target_proc, stree->n.tb->where))
94747289
DK
11677 goto error;
11678 }
11679
11680 return;
11681
11682error:
524af0d6 11683 resolve_bindings_result = false;
94747289
DK
11684 stree->n.tb->error = 1;
11685}
11686
11687
11688/* Resolve the type-bound procedures for a derived type. */
11689
30b608eb
DK
11690static void
11691resolve_typebound_procedure (gfc_symtree* stree)
11692{
11693 gfc_symbol* proc;
11694 locus where;
11695 gfc_symbol* me_arg;
11696 gfc_symbol* super_type;
9d1210f4 11697 gfc_component* comp;
30b608eb 11698
e34ccb4c
DK
11699 gcc_assert (stree);
11700
11701 /* Undefined specific symbol from GENERIC target definition. */
11702 if (!stree->n.tb)
11703 return;
11704
11705 if (stree->n.tb->error)
30b608eb
DK
11706 return;
11707
e157f736 11708 /* If this is a GENERIC binding, use that routine. */
e34ccb4c 11709 if (stree->n.tb->is_generic)
e157f736 11710 {
524af0d6 11711 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
e157f736
DK
11712 goto error;
11713 return;
11714 }
11715
30b608eb 11716 /* Get the target-procedure to check it. */
e34ccb4c
DK
11717 gcc_assert (!stree->n.tb->is_generic);
11718 gcc_assert (stree->n.tb->u.specific);
11719 proc = stree->n.tb->u.specific->n.sym;
11720 where = stree->n.tb->where;
30b608eb
DK
11721
11722 /* Default access should already be resolved from the parser. */
e34ccb4c 11723 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
30b608eb 11724
b6a45605 11725 if (stree->n.tb->deferred)
30b608eb 11726 {
524af0d6 11727 if (!check_proc_interface (proc, &where))
b6a45605
JW
11728 goto error;
11729 }
11730 else
11731 {
11732 /* Check for F08:C465. */
11733 if ((!proc->attr.subroutine && !proc->attr.function)
11734 || (proc->attr.proc != PROC_MODULE
11735 && proc->attr.if_source != IFSRC_IFBODY)
11736 || proc->attr.abstract)
11737 {
11738 gfc_error ("'%s' must be a module procedure or an external procedure with"
11739 " an explicit interface at %L", proc->name, &where);
11740 goto error;
11741 }
30b608eb 11742 }
b6a45605 11743
e34ccb4c
DK
11744 stree->n.tb->subroutine = proc->attr.subroutine;
11745 stree->n.tb->function = proc->attr.function;
30b608eb
DK
11746
11747 /* Find the super-type of the current derived type. We could do this once and
11748 store in a global if speed is needed, but as long as not I believe this is
11749 more readable and clearer. */
11750 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
11751
e157f736
DK
11752 /* If PASS, resolve and check arguments if not already resolved / loaded
11753 from a .mod file. */
e34ccb4c 11754 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
30b608eb 11755 {
4cbc9039
JW
11756 gfc_formal_arglist *dummy_args;
11757
11758 dummy_args = gfc_sym_get_dummy_args (proc);
e34ccb4c 11759 if (stree->n.tb->pass_arg)
30b608eb 11760 {
4cbc9039 11761 gfc_formal_arglist *i;
30b608eb
DK
11762
11763 /* If an explicit passing argument name is given, walk the arg-list
11764 and look for it. */
11765
11766 me_arg = NULL;
e34ccb4c 11767 stree->n.tb->pass_arg_num = 1;
4cbc9039 11768 for (i = dummy_args; i; i = i->next)
30b608eb 11769 {
e34ccb4c 11770 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
30b608eb
DK
11771 {
11772 me_arg = i->sym;
11773 break;
11774 }
e34ccb4c 11775 ++stree->n.tb->pass_arg_num;
30b608eb
DK
11776 }
11777
11778 if (!me_arg)
11779 {
11780 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
11781 " argument '%s'",
e34ccb4c
DK
11782 proc->name, stree->n.tb->pass_arg, &where,
11783 stree->n.tb->pass_arg);
30b608eb
DK
11784 goto error;
11785 }
11786 }
11787 else
11788 {
11789 /* Otherwise, take the first one; there should in fact be at least
11790 one. */
e34ccb4c 11791 stree->n.tb->pass_arg_num = 1;
4cbc9039 11792 if (!dummy_args)
30b608eb
DK
11793 {
11794 gfc_error ("Procedure '%s' with PASS at %L must have at"
11795 " least one argument", proc->name, &where);
11796 goto error;
11797 }
4cbc9039 11798 me_arg = dummy_args->sym;
30b608eb
DK
11799 }
11800
41a394bb
DK
11801 /* Now check that the argument-type matches and the passed-object
11802 dummy argument is generally fine. */
11803
30b608eb 11804 gcc_assert (me_arg);
41a394bb 11805
cf2b3c22 11806 if (me_arg->ts.type != BT_CLASS)
30b608eb 11807 {
cf2b3c22
TB
11808 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
11809 " at %L", proc->name, &where);
30b608eb
DK
11810 goto error;
11811 }
8e1f752a 11812
7a08eda1 11813 if (CLASS_DATA (me_arg)->ts.u.derived
cf2b3c22 11814 != resolve_bindings_derived)
727e8544 11815 {
cf2b3c22
TB
11816 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
11817 " the derived-type '%s'", me_arg->name, proc->name,
11818 me_arg->name, &where, resolve_bindings_derived->name);
727e8544
JW
11819 goto error;
11820 }
4d382327 11821
41a394bb 11822 gcc_assert (me_arg->ts.type == BT_CLASS);
c62c6622 11823 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
41a394bb
DK
11824 {
11825 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
11826 " scalar", proc->name, &where);
11827 goto error;
11828 }
7a08eda1 11829 if (CLASS_DATA (me_arg)->attr.allocatable)
41a394bb
DK
11830 {
11831 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11832 " be ALLOCATABLE", proc->name, &where);
11833 goto error;
11834 }
7a08eda1 11835 if (CLASS_DATA (me_arg)->attr.class_pointer)
41a394bb
DK
11836 {
11837 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
11838 " be POINTER", proc->name, &where);
11839 goto error;
11840 }
30b608eb
DK
11841 }
11842
11843 /* If we are extending some type, check that we don't override a procedure
11844 flagged NON_OVERRIDABLE. */
e34ccb4c 11845 stree->n.tb->overridden = NULL;
30b608eb
DK
11846 if (super_type)
11847 {
11848 gfc_symtree* overridden;
8e1f752a 11849 overridden = gfc_find_typebound_proc (super_type, NULL,
4a44a72d 11850 stree->name, true, NULL);
30b608eb 11851
99fc1b90
JW
11852 if (overridden)
11853 {
11854 if (overridden->n.tb)
11855 stree->n.tb->overridden = overridden->n.tb;
e157f736 11856
524af0d6 11857 if (!gfc_check_typebound_override (stree, overridden))
99fc1b90
JW
11858 goto error;
11859 }
30b608eb
DK
11860 }
11861
9d1210f4
DK
11862 /* See if there's a name collision with a component directly in this type. */
11863 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
11864 if (!strcmp (comp->name, stree->name))
11865 {
11866 gfc_error ("Procedure '%s' at %L has the same name as a component of"
11867 " '%s'",
11868 stree->name, &where, resolve_bindings_derived->name);
11869 goto error;
11870 }
11871
11872 /* Try to find a name collision with an inherited component. */
11873 if (super_type && gfc_find_component (super_type, stree->name, true, true))
11874 {
11875 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
11876 " component of '%s'",
11877 stree->name, &where, resolve_bindings_derived->name);
11878 goto error;
11879 }
11880
e34ccb4c 11881 stree->n.tb->error = 0;
30b608eb
DK
11882 return;
11883
11884error:
524af0d6 11885 resolve_bindings_result = false;
e34ccb4c 11886 stree->n.tb->error = 1;
30b608eb
DK
11887}
11888
bd48f123 11889
524af0d6 11890static bool
30b608eb
DK
11891resolve_typebound_procedures (gfc_symbol* derived)
11892{
94747289 11893 int op;
0291fa25 11894 gfc_symbol* super_type;
94747289 11895
e34ccb4c 11896 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
524af0d6 11897 return true;
4d382327 11898
0291fa25
JW
11899 super_type = gfc_get_derived_super_type (derived);
11900 if (super_type)
49c8d79b 11901 resolve_symbol (super_type);
30b608eb
DK
11902
11903 resolve_bindings_derived = derived;
524af0d6 11904 resolve_bindings_result = true;
94747289
DK
11905
11906 if (derived->f2k_derived->tb_sym_root)
11907 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
11908 &resolve_typebound_procedure);
11909
94747289
DK
11910 if (derived->f2k_derived->tb_uop_root)
11911 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
11912 &resolve_typebound_user_op);
11913
11914 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
11915 {
11916 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
524af0d6
JB
11917 if (p && !resolve_typebound_intrinsic_op (derived,
11918 (gfc_intrinsic_op)op, p))
11919 resolve_bindings_result = false;
94747289 11920 }
30b608eb
DK
11921
11922 return resolve_bindings_result;
11923}
11924
11925
9d5c21c1
PT
11926/* Add a derived type to the dt_list. The dt_list is used in trans-types.c
11927 to give all identical derived types the same backend_decl. */
11928static void
11929add_dt_to_dt_list (gfc_symbol *derived)
11930{
11931 gfc_dt_list *dt_list;
11932
11933 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
11934 if (derived == dt_list->derived)
f372a0c0 11935 return;
9d5c21c1 11936
f372a0c0
MM
11937 dt_list = gfc_get_dt_list ();
11938 dt_list->next = gfc_derived_types;
11939 dt_list->derived = derived;
11940 gfc_derived_types = dt_list;
9d5c21c1
PT
11941}
11942
11943
b0e5fa94
DK
11944/* Ensure that a derived-type is really not abstract, meaning that every
11945 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
11946
524af0d6 11947static bool
b0e5fa94
DK
11948ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
11949{
11950 if (!st)
524af0d6 11951 return true;
b0e5fa94 11952
524af0d6
JB
11953 if (!ensure_not_abstract_walker (sub, st->left))
11954 return false;
11955 if (!ensure_not_abstract_walker (sub, st->right))
11956 return false;
b0e5fa94 11957
e34ccb4c 11958 if (st->n.tb && st->n.tb->deferred)
b0e5fa94
DK
11959 {
11960 gfc_symtree* overriding;
4a44a72d 11961 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
9c4174d8 11962 if (!overriding)
524af0d6 11963 return false;
9c4174d8 11964 gcc_assert (overriding->n.tb);
e34ccb4c 11965 if (overriding->n.tb->deferred)
b0e5fa94
DK
11966 {
11967 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
11968 " '%s' is DEFERRED and not overridden",
11969 sub->name, &sub->declared_at, st->name);
524af0d6 11970 return false;
b0e5fa94
DK
11971 }
11972 }
11973
524af0d6 11974 return true;
b0e5fa94
DK
11975}
11976
524af0d6 11977static bool
b0e5fa94
DK
11978ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
11979{
11980 /* The algorithm used here is to recursively travel up the ancestry of sub
11981 and for each ancestor-type, check all bindings. If any of them is
11982 DEFERRED, look it up starting from sub and see if the found (overriding)
11983 binding is not DEFERRED.
11984 This is not the most efficient way to do this, but it should be ok and is
11985 clearer than something sophisticated. */
11986
7c9b8fb9 11987 gcc_assert (ancestor && !sub->attr.abstract);
4d382327 11988
7c9b8fb9 11989 if (!ancestor->attr.abstract)
524af0d6 11990 return true;
b0e5fa94
DK
11991
11992 /* Walk bindings of this ancestor. */
11993 if (ancestor->f2k_derived)
11994 {
524af0d6 11995 bool t;
e34ccb4c 11996 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
524af0d6
JB
11997 if (!t)
11998 return false;
b0e5fa94
DK
11999 }
12000
12001 /* Find next ancestor type and recurse on it. */
12002 ancestor = gfc_get_derived_super_type (ancestor);
12003 if (ancestor)
12004 return ensure_not_abstract (sub, ancestor);
12005
524af0d6 12006 return true;
b0e5fa94
DK
12007}
12008
12009
4d382327
AF
12010/* This check for typebound defined assignments is done recursively
12011 since the order in which derived types are resolved is not always in
12012 order of the declarations. */
12013
12014static void
12015check_defined_assignments (gfc_symbol *derived)
12016{
12017 gfc_component *c;
12018
12019 for (c = derived->components; c; c = c->next)
12020 {
12021 if (c->ts.type != BT_DERIVED
12022 || c->attr.pointer
12023 || c->attr.allocatable
12024 || c->attr.proc_pointer_comp
12025 || c->attr.class_pointer
12026 || c->attr.proc_pointer)
12027 continue;
12028
12029 if (c->ts.u.derived->attr.defined_assign_comp
12030 || (c->ts.u.derived->f2k_derived
12031 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12032 {
12033 derived->attr.defined_assign_comp = 1;
12034 return;
12035 }
12036
12037 check_defined_assignments (c->ts.u.derived);
12038 if (c->ts.u.derived->attr.defined_assign_comp)
12039 {
12040 derived->attr.defined_assign_comp = 1;
12041 return;
12042 }
12043 }
12044}
12045
12046
0291fa25
JW
12047/* Resolve the components of a derived type. This does not have to wait until
12048 resolution stage, but can be done as soon as the dt declaration has been
12049 parsed. */
110eec24 12050
524af0d6 12051static bool
0291fa25 12052resolve_fl_derived0 (gfc_symbol *sym)
110eec24 12053{
9d1210f4 12054 gfc_symbol* super_type;
110eec24
TS
12055 gfc_component *c;
12056
8b704316 12057 if (sym->attr.unlimited_polymorphic)
524af0d6 12058 return true;
8b704316 12059
9d1210f4
DK
12060 super_type = gfc_get_derived_super_type (sym);
12061
be59db2d
TB
12062 /* F2008, C432. */
12063 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12064 {
12065 gfc_error ("As extending type '%s' at %L has a coarray component, "
12066 "parent type '%s' shall also have one", sym->name,
12067 &sym->declared_at, super_type->name);
524af0d6 12068 return false;
be59db2d
TB
12069 }
12070
e157f736 12071 /* Ensure the extended type gets resolved before we do. */
524af0d6
JB
12072 if (super_type && !resolve_fl_derived0 (super_type))
12073 return false;
e157f736 12074
52f49934 12075 /* An ABSTRACT type must be extensible. */
cf2b3c22 12076 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
52f49934
DK
12077 {
12078 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
12079 sym->name, &sym->declared_at);
524af0d6 12080 return false;
52f49934
DK
12081 }
12082
fac665b2
TB
12083 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12084 : sym->components;
12085
12086 for ( ; c != NULL; c = c->next)
110eec24 12087 {
8e54f139
TB
12088 if (c->attr.artificial)
12089 continue;
12090
0c5c7b00 12091 /* See PRs 51550, 47545, 48654, 49050, 51075 - and 45170. */
8ae1ec92 12092 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function)
0c5c7b00
TB
12093 {
12094 gfc_error ("Deferred-length character component '%s' at %L is not "
12095 "yet supported", c->name, &c->loc);
524af0d6 12096 return false;
0c5c7b00
TB
12097 }
12098
be59db2d 12099 /* F2008, C442. */
c49ea23d
PT
12100 if ((!sym->attr.is_class || c != sym->components)
12101 && c->attr.codimension
d3a9eea2 12102 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
be59db2d
TB
12103 {
12104 gfc_error ("Coarray component '%s' at %L must be allocatable with "
12105 "deferred shape", c->name, &c->loc);
524af0d6 12106 return false;
be59db2d
TB
12107 }
12108
12109 /* F2008, C443. */
12110 if (c->attr.codimension && c->ts.type == BT_DERIVED
12111 && c->ts.u.derived->ts.is_iso_c)
12112 {
12113 gfc_error ("Component '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
12114 "shall not be a coarray", c->name, &c->loc);
524af0d6 12115 return false;
be59db2d
TB
12116 }
12117
12118 /* F2008, C444. */
12119 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
178f9aa1
TB
12120 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12121 || c->attr.allocatable))
be59db2d
TB
12122 {
12123 gfc_error ("Component '%s' at %L with coarray component "
12124 "shall be a nonpointer, nonallocatable scalar",
12125 c->name, &c->loc);
524af0d6 12126 return false;
be59db2d
TB
12127 }
12128
fe4e525c
TB
12129 /* F2008, C448. */
12130 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12131 {
12132 gfc_error ("Component '%s' at %L has the CONTIGUOUS attribute but "
12133 "is not an array pointer", c->name, &c->loc);
524af0d6 12134 return false;
fe4e525c
TB
12135 }
12136
713485cc
JW
12137 if (c->attr.proc_pointer && c->ts.interface)
12138 {
b6a45605 12139 gfc_symbol *ifc = c->ts.interface;
713485cc 12140
b6a45605 12141 if (!sym->attr.vtype
524af0d6
JB
12142 && !check_proc_interface (ifc, &c->loc))
12143 return false;
713485cc 12144
b6a45605
JW
12145 if (ifc->attr.if_source || ifc->attr.intrinsic)
12146 {
12147 /* Resolve interface and copy attributes. */
acbdc378
JW
12148 if (ifc->formal && !ifc->formal_ns)
12149 resolve_symbol (ifc);
713485cc 12150 if (ifc->attr.intrinsic)
2dda89a8 12151 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
713485cc
JW
12152
12153 if (ifc->result)
f64edc8b
JW
12154 {
12155 c->ts = ifc->result->ts;
12156 c->attr.allocatable = ifc->result->attr.allocatable;
12157 c->attr.pointer = ifc->result->attr.pointer;
12158 c->attr.dimension = ifc->result->attr.dimension;
12159 c->as = gfc_copy_array_spec (ifc->result->as);
5e25600e 12160 c->attr.class_ok = ifc->result->attr.class_ok;
f64edc8b
JW
12161 }
12162 else
4d382327 12163 {
f64edc8b
JW
12164 c->ts = ifc->ts;
12165 c->attr.allocatable = ifc->attr.allocatable;
12166 c->attr.pointer = ifc->attr.pointer;
12167 c->attr.dimension = ifc->attr.dimension;
12168 c->as = gfc_copy_array_spec (ifc->as);
5e25600e 12169 c->attr.class_ok = ifc->attr.class_ok;
f64edc8b 12170 }
713485cc
JW
12171 c->ts.interface = ifc;
12172 c->attr.function = ifc->attr.function;
12173 c->attr.subroutine = ifc->attr.subroutine;
713485cc 12174
713485cc
JW
12175 c->attr.pure = ifc->attr.pure;
12176 c->attr.elemental = ifc->attr.elemental;
713485cc
JW
12177 c->attr.recursive = ifc->attr.recursive;
12178 c->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 12179 c->attr.ext_attr |= ifc->attr.ext_attr;
713485cc 12180 /* Copy char length. */
bc21d315 12181 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
713485cc 12182 {
9c4174d8 12183 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
9c4174d8 12184 if (cl->length && !cl->resolved
524af0d6
JB
12185 && !gfc_resolve_expr (cl->length))
12186 return false;
9c4174d8 12187 c->ts.u.cl = cl;
713485cc
JW
12188 }
12189 }
713485cc
JW
12190 }
12191 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
12192 {
6c036626
JW
12193 /* Since PPCs are not implicitly typed, a PPC without an explicit
12194 interface must be a subroutine. */
12195 gfc_add_subroutine (&c->attr, c->name, &c->loc);
713485cc
JW
12196 }
12197
90661f26 12198 /* Procedure pointer components: Check PASS arg. */
eece1eb9
PT
12199 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
12200 && !sym->attr.vtype)
90661f26
JW
12201 {
12202 gfc_symbol* me_arg;
12203
12204 if (c->tb->pass_arg)
12205 {
12206 gfc_formal_arglist* i;
12207
12208 /* If an explicit passing argument name is given, walk the arg-list
12209 and look for it. */
12210
12211 me_arg = NULL;
12212 c->tb->pass_arg_num = 1;
4cbc9039 12213 for (i = c->ts.interface->formal; i; i = i->next)
90661f26
JW
12214 {
12215 if (!strcmp (i->sym->name, c->tb->pass_arg))
12216 {
12217 me_arg = i->sym;
12218 break;
12219 }
12220 c->tb->pass_arg_num++;
12221 }
12222
12223 if (!me_arg)
12224 {
12225 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
12226 "at %L has no argument '%s'", c->name,
12227 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
12228 c->tb->error = 1;
524af0d6 12229 return false;
90661f26
JW
12230 }
12231 }
12232 else
12233 {
12234 /* Otherwise, take the first one; there should in fact be at least
12235 one. */
12236 c->tb->pass_arg_num = 1;
4cbc9039 12237 if (!c->ts.interface->formal)
90661f26
JW
12238 {
12239 gfc_error ("Procedure pointer component '%s' with PASS at %L "
12240 "must have at least one argument",
12241 c->name, &c->loc);
12242 c->tb->error = 1;
524af0d6 12243 return false;
90661f26 12244 }
4cbc9039 12245 me_arg = c->ts.interface->formal->sym;
90661f26
JW
12246 }
12247
12248 /* Now check that the argument-type matches. */
12249 gcc_assert (me_arg);
cf2b3c22
TB
12250 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
12251 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
12252 || (me_arg->ts.type == BT_CLASS
7a08eda1 12253 && CLASS_DATA (me_arg)->ts.u.derived != sym))
90661f26
JW
12254 {
12255 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
12256 " the derived type '%s'", me_arg->name, c->name,
12257 me_arg->name, &c->loc, sym->name);
12258 c->tb->error = 1;
524af0d6 12259 return false;
90661f26
JW
12260 }
12261
12262 /* Check for C453. */
12263 if (me_arg->attr.dimension)
12264 {
12265 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12266 "must be scalar", me_arg->name, c->name, me_arg->name,
12267 &c->loc);
12268 c->tb->error = 1;
524af0d6 12269 return false;
90661f26
JW
12270 }
12271
12272 if (me_arg->attr.pointer)
12273 {
12274 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12275 "may not have the POINTER attribute", me_arg->name,
12276 c->name, me_arg->name, &c->loc);
12277 c->tb->error = 1;
524af0d6 12278 return false;
90661f26
JW
12279 }
12280
12281 if (me_arg->attr.allocatable)
12282 {
12283 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
12284 "may not be ALLOCATABLE", me_arg->name, c->name,
12285 me_arg->name, &c->loc);
12286 c->tb->error = 1;
524af0d6 12287 return false;
90661f26
JW
12288 }
12289
cf2b3c22 12290 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
727e8544 12291 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
cf2b3c22 12292 " at %L", c->name, &c->loc);
90661f26
JW
12293
12294 }
12295
52f49934 12296 /* Check type-spec if this is not the parent-type component. */
fac665b2
TB
12297 if (((sym->attr.is_class
12298 && (!sym->components->ts.u.derived->attr.extension
12299 || c != sym->components->ts.u.derived->components))
12300 || (!sym->attr.is_class
12301 && (!sym->attr.extension || c != sym->components)))
12302 && !sym->attr.vtype
524af0d6
JB
12303 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
12304 return false;
52f49934 12305
f89cc1a3
JW
12306 /* If this type is an extension, set the accessibility of the parent
12307 component. */
fac665b2
TB
12308 if (super_type
12309 && ((sym->attr.is_class
12310 && c == sym->components->ts.u.derived->components)
12311 || (!sym->attr.is_class && c == sym->components))
f89cc1a3
JW
12312 && strcmp (super_type->name, c->name) == 0)
12313 c->attr.access = super_type->attr.access;
4d382327 12314
9d1210f4
DK
12315 /* If this type is an extension, see if this component has the same name
12316 as an inherited type-bound procedure. */
371b334e 12317 if (super_type && !sym->attr.is_class
4a44a72d 12318 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9d1210f4
DK
12319 {
12320 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
12321 " inherited type-bound procedure",
12322 c->name, sym->name, &c->loc);
524af0d6 12323 return false;
9d1210f4
DK
12324 }
12325
8d51f26f
PT
12326 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
12327 && !c->ts.deferred)
110eec24 12328 {
bc21d315 12329 if (c->ts.u.cl->length == NULL
524af0d6 12330 || (!resolve_charlen(c->ts.u.cl))
bc21d315 12331 || !gfc_is_constant_expr (c->ts.u.cl->length))
110eec24
TS
12332 {
12333 gfc_error ("Character length of component '%s' needs to "
e25a0da3 12334 "be a constant specification expression at %L",
110eec24 12335 c->name,
bc21d315 12336 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
524af0d6 12337 return false;
110eec24
TS
12338 }
12339 }
12340
8d51f26f
PT
12341 if (c->ts.type == BT_CHARACTER && c->ts.deferred
12342 && !c->attr.pointer && !c->attr.allocatable)
12343 {
12344 gfc_error ("Character component '%s' of '%s' at %L with deferred "
12345 "length must be a POINTER or ALLOCATABLE",
12346 c->name, sym->name, &c->loc);
524af0d6 12347 return false;
8d51f26f
PT
12348 }
12349
2ed8d224 12350 if (c->ts.type == BT_DERIVED
edf1eac2 12351 && sym->component_access != ACCESS_PRIVATE
6e2062b0 12352 && gfc_check_symbol_access (sym)
bc21d315
JW
12353 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
12354 && !c->ts.u.derived->attr.use_assoc
6e2062b0 12355 && !gfc_check_symbol_access (c->ts.u.derived)
524af0d6
JB
12356 && !gfc_notify_std (GFC_STD_F2003, "the component '%s' is a "
12357 "PRIVATE type and cannot be a component of "
12358 "'%s', which is PUBLIC at %L", c->name,
12359 sym->name, &sym->declared_at))
12360 return false;
2ed8d224 12361
0149d8cc
TB
12362 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
12363 {
12364 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
12365 "type %s", c->name, &c->loc, sym->name);
524af0d6 12366 return false;
0149d8cc
TB
12367 }
12368
f970c857
PT
12369 if (sym->attr.sequence)
12370 {
bc21d315 12371 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
f970c857
PT
12372 {
12373 gfc_error ("Component %s of SEQUENCE type declared at %L does "
12374 "not have the SEQUENCE attribute",
bc21d315 12375 c->ts.u.derived->name, &sym->declared_at);
524af0d6 12376 return false;
f970c857
PT
12377 }
12378 }
12379
c3f34952
TB
12380 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
12381 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
12382 else if (c->ts.type == BT_CLASS && c->attr.class_ok
12383 && CLASS_DATA (c)->ts.u.derived->attr.generic)
12384 CLASS_DATA (c)->ts.u.derived
12385 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
12386
50f30801
JW
12387 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
12388 && c->attr.pointer && c->ts.u.derived->components == NULL
bc21d315 12389 && !c->ts.u.derived->attr.zero_comp)
982186b1
PT
12390 {
12391 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12392 "that has not been declared", c->name, sym->name,
12393 &c->loc);
524af0d6 12394 return false;
982186b1
PT
12395 }
12396
9c9eacb9
JW
12397 if (c->ts.type == BT_CLASS && c->attr.class_ok
12398 && CLASS_DATA (c)->attr.class_pointer
7a08eda1 12399 && CLASS_DATA (c)->ts.u.derived->components == NULL
8b704316
PT
12400 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
12401 && !UNLIMITED_POLY (c))
371b334e
JW
12402 {
12403 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
12404 "that has not been declared", c->name, sym->name,
12405 &c->loc);
524af0d6 12406 return false;
371b334e
JW
12407 }
12408
727e8544 12409 /* C437. */
9c9eacb9
JW
12410 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
12411 && (!c->attr.class_ok
12412 || !(CLASS_DATA (c)->attr.class_pointer
12413 || CLASS_DATA (c)->attr.allocatable)))
727e8544
JW
12414 {
12415 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
12416 "or pointer", c->name, &c->loc);
8ec4321f
PT
12417 /* Prevent a recurrence of the error. */
12418 c->ts.type = BT_UNKNOWN;
524af0d6 12419 return false;
727e8544
JW
12420 }
12421
9d5c21c1
PT
12422 /* Ensure that all the derived type components are put on the
12423 derived type list; even in formal namespaces, where derived type
12424 pointer components might not have been declared. */
12425 if (c->ts.type == BT_DERIVED
bc21d315
JW
12426 && c->ts.u.derived
12427 && c->ts.u.derived->components
d4b7d0f0 12428 && c->attr.pointer
bc21d315
JW
12429 && sym != c->ts.u.derived)
12430 add_dt_to_dt_list (c->ts.u.derived);
9d5c21c1 12431
524af0d6
JB
12432 if (!gfc_resolve_array_spec (c->as,
12433 !(c->attr.pointer || c->attr.proc_pointer
12434 || c->attr.allocatable)))
12435 return false;
e35e87dc
TB
12436
12437 if (c->initializer && !sym->attr.vtype
524af0d6
JB
12438 && !gfc_check_assign_symbol (sym, c, c->initializer))
12439 return false;
110eec24 12440 }
05c1e3a7 12441
4d382327
AF
12442 check_defined_assignments (sym);
12443
12444 if (!sym->attr.defined_assign_comp && super_type)
12445 sym->attr.defined_assign_comp
12446 = super_type->attr.defined_assign_comp;
12447
b0e5fa94
DK
12448 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
12449 all DEFERRED bindings are overridden. */
12450 if (super_type && super_type->attr.abstract && !sym->attr.abstract
5cd2f815 12451 && !sym->attr.is_class
524af0d6
JB
12452 && !ensure_not_abstract (sym, super_type))
12453 return false;
b0e5fa94 12454
6b887797 12455 /* Add derived type to the derived type list. */
9d5c21c1 12456 add_dt_to_dt_list (sym);
6b887797 12457
86035eec
TB
12458 /* Check if the type is finalizable. This is done in order to ensure that the
12459 finalization wrapper is generated early enough. */
12460 gfc_is_finalizable (sym, NULL);
12461
524af0d6 12462 return true;
110eec24
TS
12463}
12464
2ed8d224 12465
0291fa25
JW
12466/* The following procedure does the full resolution of a derived type,
12467 including resolution of all type-bound procedures (if present). In contrast
12468 to 'resolve_fl_derived0' this can only be done after the module has been
12469 parsed completely. */
12470
524af0d6 12471static bool
0291fa25
JW
12472resolve_fl_derived (gfc_symbol *sym)
12473{
c3f34952
TB
12474 gfc_symbol *gen_dt = NULL;
12475
8b704316 12476 if (sym->attr.unlimited_polymorphic)
524af0d6 12477 return true;
8b704316 12478
c3f34952
TB
12479 if (!sym->attr.is_class)
12480 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
12481 if (gen_dt && gen_dt->generic && gen_dt->generic->next
6ba84c31
TB
12482 && (!gen_dt->generic->sym->attr.use_assoc
12483 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
524af0d6
JB
12484 && !gfc_notify_std (GFC_STD_F2003, "Generic name '%s' of function "
12485 "'%s' at %L being the same name as derived "
12486 "type at %L", sym->name,
12487 gen_dt->generic->sym == sym
12488 ? gen_dt->generic->next->sym->name
12489 : gen_dt->generic->sym->name,
12490 gen_dt->generic->sym == sym
12491 ? &gen_dt->generic->next->sym->declared_at
12492 : &gen_dt->generic->sym->declared_at,
12493 &sym->declared_at))
12494 return false;
c3f34952 12495
8e54f139 12496 /* Resolve the finalizer procedures. */
524af0d6
JB
12497 if (!gfc_resolve_finalizers (sym))
12498 return false;
4d382327 12499
0291fa25
JW
12500 if (sym->attr.is_class && sym->ts.u.derived == NULL)
12501 {
12502 /* Fix up incomplete CLASS symbols. */
12503 gfc_component *data = gfc_find_component (sym, "_data", true, true);
12504 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
8b704316
PT
12505
12506 /* Nothing more to do for unlimited polymorphic entities. */
12507 if (data->ts.u.derived->attr.unlimited_polymorphic)
524af0d6 12508 return true;
8b704316 12509 else if (vptr->ts.u.derived == NULL)
0291fa25
JW
12510 {
12511 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
12512 gcc_assert (vtab);
12513 vptr->ts.u.derived = vtab->ts.u.derived;
12514 }
12515 }
4d382327 12516
524af0d6
JB
12517 if (!resolve_fl_derived0 (sym))
12518 return false;
4d382327 12519
0291fa25 12520 /* Resolve the type-bound procedures. */
524af0d6
JB
12521 if (!resolve_typebound_procedures (sym))
12522 return false;
0291fa25 12523
524af0d6 12524 return true;
0291fa25
JW
12525}
12526
12527
524af0d6 12528static bool
3e1cf500
PT
12529resolve_fl_namelist (gfc_symbol *sym)
12530{
12531 gfc_namelist *nl;
12532 gfc_symbol *nlsym;
12533
e0608471
TB
12534 for (nl = sym->namelist; nl; nl = nl->next)
12535 {
19d36107
TB
12536 /* Check again, the check in match only works if NAMELIST comes
12537 after the decl. */
12538 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
12539 {
12540 gfc_error ("Assumed size array '%s' in namelist '%s' at %L is not "
12541 "allowed", nl->sym->name, sym->name, &sym->declared_at);
524af0d6 12542 return false;
19d36107
TB
12543 }
12544
e0608471 12545 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
524af0d6
JB
12546 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12547 "with assumed shape in namelist '%s' at %L",
12548 nl->sym->name, sym->name, &sym->declared_at))
12549 return false;
e0608471 12550
19d36107 12551 if (is_non_constant_shape_array (nl->sym)
524af0d6
JB
12552 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
12553 "with nonconstant shape in namelist '%s' at %L",
12554 nl->sym->name, sym->name, &sym->declared_at))
12555 return false;
e0608471 12556
19d36107
TB
12557 if (nl->sym->ts.type == BT_CHARACTER
12558 && (nl->sym->ts.u.cl->length == NULL
12559 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
524af0d6
JB
12560 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' with "
12561 "nonconstant character length in "
12562 "namelist '%s' at %L", nl->sym->name,
12563 sym->name, &sym->declared_at))
12564 return false;
e0608471 12565
19d36107
TB
12566 /* FIXME: Once UDDTIO is implemented, the following can be
12567 removed. */
12568 if (nl->sym->ts.type == BT_CLASS)
e0608471 12569 {
19d36107
TB
12570 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L is "
12571 "polymorphic and requires a defined input/output "
12572 "procedure", nl->sym->name, sym->name, &sym->declared_at);
524af0d6 12573 return false;
e0608471
TB
12574 }
12575
19d36107
TB
12576 if (nl->sym->ts.type == BT_DERIVED
12577 && (nl->sym->ts.u.derived->attr.alloc_comp
12578 || nl->sym->ts.u.derived->attr.pointer_comp))
e0608471 12579 {
524af0d6
JB
12580 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object '%s' in "
12581 "namelist '%s' at %L with ALLOCATABLE "
12582 "or POINTER components", nl->sym->name,
12583 sym->name, &sym->declared_at))
12584 return false;
19d36107
TB
12585
12586 /* FIXME: Once UDDTIO is implemented, the following can be
12587 removed. */
12588 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L has "
12589 "ALLOCATABLE or POINTER components and thus requires "
12590 "a defined input/output procedure", nl->sym->name,
12591 sym->name, &sym->declared_at);
524af0d6 12592 return false;
e0608471
TB
12593 }
12594 }
12595
3e1cf500 12596 /* Reject PRIVATE objects in a PUBLIC namelist. */
6e2062b0 12597 if (gfc_check_symbol_access (sym))
3e1cf500
PT
12598 {
12599 for (nl = sym->namelist; nl; nl = nl->next)
12600 {
3dbf6538 12601 if (!nl->sym->attr.use_assoc
c867b7b6 12602 && !is_sym_host_assoc (nl->sym, sym->ns)
6e2062b0 12603 && !gfc_check_symbol_access (nl->sym))
3e1cf500 12604 {
5cca320d
DF
12605 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
12606 "cannot be member of PUBLIC namelist '%s' at %L",
12607 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 12608 return false;
5cca320d
DF
12609 }
12610
3dbf6538
DF
12611 /* Types with private components that came here by USE-association. */
12612 if (nl->sym->ts.type == BT_DERIVED
bc21d315 12613 && derived_inaccessible (nl->sym->ts.u.derived))
3dbf6538
DF
12614 {
12615 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
12616 "components and cannot be member of namelist '%s' at %L",
12617 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 12618 return false;
3dbf6538
DF
12619 }
12620
12621 /* Types with private components that are defined in the same module. */
5cca320d 12622 if (nl->sym->ts.type == BT_DERIVED
bc21d315 12623 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
6e2062b0 12624 && nl->sym->ts.u.derived->attr.private_comp)
5cca320d
DF
12625 {
12626 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
12627 "cannot be a member of PUBLIC namelist '%s' at %L",
12628 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 12629 return false;
3e1cf500
PT
12630 }
12631 }
12632 }
12633
5cca320d 12634
3e1cf500 12635 /* 14.1.2 A module or internal procedure represent local entities
847b053d 12636 of the same type as a namelist member and so are not allowed. */
3e1cf500
PT
12637 for (nl = sym->namelist; nl; nl = nl->next)
12638 {
982186b1
PT
12639 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
12640 continue;
847b053d
PT
12641
12642 if (nl->sym->attr.function && nl->sym == nl->sym->result)
12643 if ((nl->sym == sym->ns->proc_name)
12644 ||
12645 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
12646 continue;
12647
3e1cf500 12648 nlsym = NULL;
99c25a87 12649 if (nl->sym->name)
847b053d 12650 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
982186b1
PT
12651 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
12652 {
12653 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
12654 "attribute in '%s' at %L", nlsym->name,
12655 &sym->declared_at);
524af0d6 12656 return false;
982186b1 12657 }
3e1cf500
PT
12658 }
12659
524af0d6 12660 return true;
3e1cf500
PT
12661}
12662
12663
524af0d6 12664static bool
2ed8d224
PT
12665resolve_fl_parameter (gfc_symbol *sym)
12666{
12667 /* A parameter array's shape needs to be constant. */
4d382327 12668 if (sym->as != NULL
c317bc40
DF
12669 && (sym->as->type == AS_DEFERRED
12670 || is_non_constant_shape_array (sym)))
2ed8d224
PT
12671 {
12672 gfc_error ("Parameter array '%s' at %L cannot be automatic "
c317bc40 12673 "or of deferred shape", sym->name, &sym->declared_at);
524af0d6 12674 return false;
2ed8d224
PT
12675 }
12676
12677 /* Make sure a parameter that has been implicitly typed still
12678 matches the implicit type, since PARAMETER statements can precede
12679 IMPLICIT statements. */
12680 if (sym->attr.implicit_type
713485cc
JW
12681 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
12682 sym->ns)))
2ed8d224
PT
12683 {
12684 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
12685 "later IMPLICIT type", sym->name, &sym->declared_at);
524af0d6 12686 return false;
2ed8d224
PT
12687 }
12688
12689 /* Make sure the types of derived parameters are consistent. This
12690 type checking is deferred until resolution because the type may
12691 refer to a derived type from the host. */
22c30bc0 12692 if (sym->ts.type == BT_DERIVED
edf1eac2 12693 && !gfc_compare_types (&sym->ts, &sym->value->ts))
2ed8d224
PT
12694 {
12695 gfc_error ("Incompatible derived type in PARAMETER at %L",
12696 &sym->value->where);
524af0d6 12697 return false;
2ed8d224 12698 }
524af0d6 12699 return true;
2ed8d224
PT
12700}
12701
12702
6de9cd9a
DN
12703/* Do anything necessary to resolve a symbol. Right now, we just
12704 assume that an otherwise unknown symbol is a variable. This sort
12705 of thing commonly happens for symbols in module. */
12706
12707static void
edf1eac2 12708resolve_symbol (gfc_symbol *sym)
6de9cd9a 12709{
a34437a1 12710 int check_constant, mp_flag;
219fa8c3
SK
12711 gfc_symtree *symtree;
12712 gfc_symtree *this_symtree;
12713 gfc_namespace *ns;
12714 gfc_component *c;
fac665b2
TB
12715 symbol_attribute class_attr;
12716 gfc_array_spec *as;
fd061185 12717 bool saved_specification_expr;
6de9cd9a 12718
4af8d042
MM
12719 if (sym->resolved)
12720 return;
12721 sym->resolved = 1;
12722
8e54f139
TB
12723 if (sym->attr.artificial)
12724 return;
12725
8b704316
PT
12726 if (sym->attr.unlimited_polymorphic)
12727 return;
12728
60fa3931
TB
12729 if (sym->attr.flavor == FL_UNKNOWN
12730 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
12731 && !sym->attr.generic && !sym->attr.external
6bd59684
JW
12732 && sym->attr.if_source == IFSRC_UNKNOWN
12733 && sym->ts.type == BT_UNKNOWN))
6de9cd9a 12734 {
24d36d28
PT
12735
12736 /* If we find that a flavorless symbol is an interface in one of the
12737 parent namespaces, find its symtree in this namespace, free the
12738 symbol and set the symtree to point to the interface symbol. */
12739 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
12740 {
12741 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7ca17033
JW
12742 if (symtree && (symtree->n.sym->generic ||
12743 (symtree->n.sym->attr.flavor == FL_PROCEDURE
12744 && sym->ns->construct_entities)))
24d36d28
PT
12745 {
12746 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12747 sym->name);
3cb595ac 12748 gfc_release_symbol (sym);
24d36d28
PT
12749 symtree->n.sym->refs++;
12750 this_symtree->n.sym = symtree->n.sym;
12751 return;
12752 }
12753 }
12754
12755 /* Otherwise give it a flavor according to such attributes as
12756 it has. */
60fa3931
TB
12757 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
12758 && sym->attr.intrinsic == 0)
6de9cd9a 12759 sym->attr.flavor = FL_VARIABLE;
60fa3931 12760 else if (sym->attr.flavor == FL_UNKNOWN)
6de9cd9a
DN
12761 {
12762 sym->attr.flavor = FL_PROCEDURE;
12763 if (sym->attr.dimension)
12764 sym->attr.function = 1;
12765 }
12766 }
12767
c73b6478
JW
12768 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
12769 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
12770
0e8d854e 12771 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
524af0d6 12772 && !resolve_procedure_interface (sym))
2fcac97d 12773 return;
69773742 12774
c064bf1c
TB
12775 if (sym->attr.is_protected && !sym->attr.proc_pointer
12776 && (sym->attr.procedure || sym->attr.external))
12777 {
12778 if (sym->attr.external)
12779 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
12780 "at %L", &sym->declared_at);
12781 else
12782 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
12783 "at %L", &sym->declared_at);
12784
12785 return;
12786 }
12787
524af0d6 12788 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
110eec24
TS
12789 return;
12790
6de9cd9a
DN
12791 /* Symbols that are module procedures with results (functions) have
12792 the types and array specification copied for type checking in
12793 procedures that call them, as well as for saving to a module
12794 file. These symbols can't stand the scrutiny that their results
12795 can. */
12796 mp_flag = (sym->result != NULL && sym->result != sym);
12797
4d382327
AF
12798 /* Make sure that the intrinsic is consistent with its internal
12799 representation. This needs to be done before assigning a default
eb2c598d 12800 type to avoid spurious warnings. */
f6038131 12801 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
524af0d6 12802 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
f6038131 12803 return;
eb2c598d 12804
3e78238a 12805 /* Resolve associate names. */
03af1e4c 12806 if (sym->assoc)
3e78238a 12807 resolve_assoc_var (sym, true);
03af1e4c 12808
6de9cd9a
DN
12809 /* Assign default type to symbols that need one and don't have one. */
12810 if (sym->ts.type == BT_UNKNOWN)
12811 {
12812 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
fac665b2
TB
12813 {
12814 gfc_set_default_type (sym, 1, NULL);
12815 }
6de9cd9a 12816
fc9c6e5d
JW
12817 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
12818 && !sym->attr.function && !sym->attr.subroutine
12819 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
12820 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
12821
6de9cd9a
DN
12822 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
12823 {
53096259
PT
12824 /* The specific case of an external procedure should emit an error
12825 in the case that there is no implicit type. */
6de9cd9a 12826 if (!mp_flag)
53096259 12827 gfc_set_default_type (sym, sym->attr.external, NULL);
6de9cd9a
DN
12828 else
12829 {
edf1eac2 12830 /* Result may be in another namespace. */
6de9cd9a
DN
12831 resolve_symbol (sym->result);
12832
3070bab4
JW
12833 if (!sym->result->attr.proc_pointer)
12834 {
12835 sym->ts = sym->result->ts;
12836 sym->as = gfc_copy_array_spec (sym->result->as);
12837 sym->attr.dimension = sym->result->attr.dimension;
12838 sym->attr.pointer = sym->result->attr.pointer;
12839 sym->attr.allocatable = sym->result->attr.allocatable;
fe4e525c 12840 sym->attr.contiguous = sym->result->attr.contiguous;
3070bab4 12841 }
6de9cd9a
DN
12842 }
12843 }
12844 }
e3d748dd 12845 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
fd061185
TB
12846 {
12847 bool saved_specification_expr = specification_expr;
12848 specification_expr = true;
12849 gfc_resolve_array_spec (sym->result->as, false);
12850 specification_expr = saved_specification_expr;
12851 }
6de9cd9a 12852
fac665b2
TB
12853 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
12854 {
12855 as = CLASS_DATA (sym)->as;
12856 class_attr = CLASS_DATA (sym)->attr;
12857 class_attr.pointer = class_attr.class_pointer;
12858 }
12859 else
12860 {
12861 class_attr = sym->attr;
12862 as = sym->as;
12863 }
12864
12865 /* F2008, C530. */
12866 if (sym->attr.contiguous
12867 && (!class_attr.dimension
8e54f139
TB
12868 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
12869 && !class_attr.pointer)))
fac665b2
TB
12870 {
12871 gfc_error ("'%s' at %L has the CONTIGUOUS attribute but is not an "
8e54f139
TB
12872 "array pointer or an assumed-shape or assumed-rank array",
12873 sym->name, &sym->declared_at);
fac665b2
TB
12874 return;
12875 }
12876
f5e440e1 12877 /* Assumed size arrays and assumed shape arrays must be dummy
f5ca06e6
DK
12878 arguments. Array-spec's of implied-shape should have been resolved to
12879 AS_EXPLICIT already. */
f5e440e1 12880
fac665b2 12881 if (as)
6de9cd9a 12882 {
fac665b2
TB
12883 gcc_assert (as->type != AS_IMPLIED_SHAPE);
12884 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
12885 || as->type == AS_ASSUMED_SHAPE)
4cc70466 12886 && !sym->attr.dummy && !sym->attr.select_type_temporary)
f5ca06e6 12887 {
fac665b2 12888 if (as->type == AS_ASSUMED_SIZE)
f5ca06e6
DK
12889 gfc_error ("Assumed size array at %L must be a dummy argument",
12890 &sym->declared_at);
12891 else
12892 gfc_error ("Assumed shape array at %L must be a dummy argument",
12893 &sym->declared_at);
12894 return;
12895 }
c62c6622 12896 /* TS 29113, C535a. */
4cc70466
PT
12897 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
12898 && !sym->attr.select_type_temporary)
c62c6622
TB
12899 {
12900 gfc_error ("Assumed-rank array at %L must be a dummy argument",
12901 &sym->declared_at);
12902 return;
12903 }
12904 if (as->type == AS_ASSUMED_RANK
12905 && (sym->attr.codimension || sym->attr.value))
12906 {
12907 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
12908 "CODIMENSION attribute", &sym->declared_at);
12909 return;
12910 }
a4ac5dd3
TS
12911 }
12912
6de9cd9a
DN
12913 /* Make sure symbols with known intent or optional are really dummy
12914 variable. Because of ENTRY statement, this has to be deferred
12915 until resolution time. */
12916
2ed8d224 12917 if (!sym->attr.dummy
edf1eac2 12918 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
6de9cd9a
DN
12919 {
12920 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
12921 return;
12922 }
12923
06469efd
PT
12924 if (sym->attr.value && !sym->attr.dummy)
12925 {
12926 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
1084b6b0 12927 "it is not a dummy argument", sym->name, &sym->declared_at);
06469efd
PT
12928 return;
12929 }
12930
1084b6b0
TB
12931 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
12932 {
bc21d315 12933 gfc_charlen *cl = sym->ts.u.cl;
1084b6b0
TB
12934 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
12935 {
12936 gfc_error ("Character dummy variable '%s' at %L with VALUE "
12937 "attribute must have constant length",
12938 sym->name, &sym->declared_at);
12939 return;
12940 }
a8b3b0b6
CR
12941
12942 if (sym->ts.is_c_interop
12943 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
12944 {
12945 gfc_error ("C interoperable character dummy variable '%s' at %L "
12946 "with VALUE attribute must have length one",
12947 sym->name, &sym->declared_at);
12948 return;
12949 }
12950 }
12951
c3f34952
TB
12952 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
12953 && sym->ts.u.derived->attr.generic)
12954 {
12955 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
12956 if (!sym->ts.u.derived)
12957 {
12958 gfc_error ("The derived type '%s' at %L is of type '%s', "
12959 "which has not been defined", sym->name,
12960 &sym->declared_at, sym->ts.u.derived->name);
12961 sym->ts.type = BT_UNKNOWN;
12962 return;
12963 }
12964 }
12965
e7ac6a7c
TB
12966 /* Use the same constraints as TYPE(*), except for the type check
12967 and that only scalars and assumed-size arrays are permitted. */
12968 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
12969 {
12970 if (!sym->attr.dummy)
12971 {
12972 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12973 "a dummy argument", sym->name, &sym->declared_at);
12974 return;
12975 }
12976
12977 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
12978 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
12979 && sym->ts.type != BT_COMPLEX)
12980 {
12981 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
12982 "of type TYPE(*) or of an numeric intrinsic type",
12983 sym->name, &sym->declared_at);
12984 return;
12985 }
12986
12987 if (sym->attr.allocatable || sym->attr.codimension
12988 || sym->attr.pointer || sym->attr.value)
12989 {
12990 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12991 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
12992 "attribute", sym->name, &sym->declared_at);
12993 return;
12994 }
12995
12996 if (sym->attr.intent == INTENT_OUT)
12997 {
12998 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
12999 "have the INTENT(OUT) attribute",
13000 sym->name, &sym->declared_at);
13001 return;
13002 }
13003 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13004 {
13005 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13006 "either be a scalar or an assumed-size array",
13007 sym->name, &sym->declared_at);
13008 return;
13009 }
13010
13011 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13012 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13013 packing. */
13014 sym->ts.type = BT_ASSUMED;
13015 sym->as = gfc_get_array_spec ();
13016 sym->as->type = AS_ASSUMED_SIZE;
13017 sym->as->rank = 1;
13018 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13019 }
13020 else if (sym->ts.type == BT_ASSUMED)
4d382327 13021 {
45a69325
TB
13022 /* TS 29113, C407a. */
13023 if (!sym->attr.dummy)
13024 {
13025 gfc_error ("Assumed type of variable %s at %L is only permitted "
13026 "for dummy variables", sym->name, &sym->declared_at);
13027 return;
13028 }
13029 if (sym->attr.allocatable || sym->attr.codimension
13030 || sym->attr.pointer || sym->attr.value)
13031 {
13032 gfc_error ("Assumed-type variable %s at %L may not have the "
13033 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13034 sym->name, &sym->declared_at);
13035 return;
13036 }
c62c6622
TB
13037 if (sym->attr.intent == INTENT_OUT)
13038 {
13039 gfc_error ("Assumed-type variable %s at %L may not have the "
13040 "INTENT(OUT) attribute",
13041 sym->name, &sym->declared_at);
13042 return;
13043 }
45a69325
TB
13044 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13045 {
13046 gfc_error ("Assumed-type variable %s at %L shall not be an "
13047 "explicit-shape array", sym->name, &sym->declared_at);
13048 return;
13049 }
13050 }
13051
a8b3b0b6
CR
13052 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13053 do this for something that was implicitly typed because that is handled
13054 in gfc_set_default_type. Handle dummy arguments and procedure
13055 definitions separately. Also, anything that is use associated is not
13056 handled here but instead is handled in the module it is declared in.
13057 Finally, derived type definitions are allowed to be BIND(C) since that
13058 only implies that they're interoperable, and they are checked fully for
13059 interoperability when a variable is declared of that type. */
13060 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13061 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13062 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13063 {
524af0d6 13064 bool t = true;
4d382327 13065
a8b3b0b6
CR
13066 /* First, make sure the variable is declared at the
13067 module-level scope (J3/04-007, Section 15.3). */
13068 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13069 sym->attr.in_common == 0)
13070 {
13071 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
13072 "is neither a COMMON block nor declared at the "
13073 "module level scope", sym->name, &(sym->declared_at));
524af0d6 13074 t = false;
a8b3b0b6
CR
13075 }
13076 else if (sym->common_head != NULL)
13077 {
13078 t = verify_com_block_vars_c_interop (sym->common_head);
13079 }
13080 else
13081 {
13082 /* If type() declaration, we need to verify that the components
13083 of the given type are all C interoperable, etc. */
13084 if (sym->ts.type == BT_DERIVED &&
bc21d315 13085 sym->ts.u.derived->attr.is_c_interop != 1)
a8b3b0b6
CR
13086 {
13087 /* Make sure the user marked the derived type as BIND(C). If
13088 not, call the verify routine. This could print an error
13089 for the derived type more than once if multiple variables
13090 of that type are declared. */
bc21d315
JW
13091 if (sym->ts.u.derived->attr.is_bind_c != 1)
13092 verify_bind_c_derived_type (sym->ts.u.derived);
524af0d6 13093 t = false;
a8b3b0b6 13094 }
4d382327 13095
a8b3b0b6
CR
13096 /* Verify the variable itself as C interoperable if it
13097 is BIND(C). It is not possible for this to succeed if
13098 the verify_bind_c_derived_type failed, so don't have to handle
13099 any error returned by verify_bind_c_derived_type. */
13100 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13101 sym->common_block);
13102 }
13103
524af0d6 13104 if (!t)
a8b3b0b6
CR
13105 {
13106 /* clear the is_bind_c flag to prevent reporting errors more than
13107 once if something failed. */
13108 sym->attr.is_bind_c = 0;
13109 return;
13110 }
1084b6b0
TB
13111 }
13112
976e21f6
PT
13113 /* If a derived type symbol has reached this point, without its
13114 type being declared, we have an error. Notice that most
13115 conditions that produce undefined derived types have already
13116 been dealt with. However, the likes of:
13117 implicit type(t) (t) ..... call foo (t) will get us here if
13118 the type is not declared in the scope of the implicit
13119 statement. Change the type to BT_UNKNOWN, both because it is so
13120 and to prevent an ICE. */
c3f34952
TB
13121 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13122 && sym->ts.u.derived->components == NULL
bc21d315 13123 && !sym->ts.u.derived->attr.zero_comp)
976e21f6
PT
13124 {
13125 gfc_error ("The derived type '%s' at %L is of type '%s', "
e25a0da3 13126 "which has not been defined", sym->name,
bc21d315 13127 &sym->declared_at, sym->ts.u.derived->name);
976e21f6
PT
13128 sym->ts.type = BT_UNKNOWN;
13129 return;
13130 }
13131
c1203a70
PT
13132 /* Make sure that the derived type has been resolved and that the
13133 derived type is visible in the symbol's namespace, if it is a
13134 module function and is not PRIVATE. */
13135 if (sym->ts.type == BT_DERIVED
bc21d315 13136 && sym->ts.u.derived->attr.use_assoc
96ffc6cd 13137 && sym->ns->proc_name
c3f34952 13138 && sym->ns->proc_name->attr.flavor == FL_MODULE
524af0d6 13139 && !resolve_fl_derived (sym->ts.u.derived))
c3f34952 13140 return;
c1203a70 13141
a08a5751
TB
13142 /* Unless the derived-type declaration is use associated, Fortran 95
13143 does not allow public entries of private derived types.
13144 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
13145 161 in 95-006r3. */
13146 if (sym->ts.type == BT_DERIVED
72052237 13147 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
bc21d315 13148 && !sym->ts.u.derived->attr.use_assoc
6e2062b0
JW
13149 && gfc_check_symbol_access (sym)
13150 && !gfc_check_symbol_access (sym->ts.u.derived)
524af0d6
JB
13151 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s '%s' at %L of PRIVATE "
13152 "derived type '%s'",
13153 (sym->attr.flavor == FL_PARAMETER)
13154 ? "parameter" : "variable",
13155 sym->name, &sym->declared_at,
13156 sym->ts.u.derived->name))
a08a5751
TB
13157 return;
13158
fea54935
TB
13159 /* F2008, C1302. */
13160 if (sym->ts.type == BT_DERIVED
3b6fa7a5
TB
13161 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
13162 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
13163 || sym->ts.u.derived->attr.lock_comp)
13164 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
fea54935 13165 {
3b6fa7a5
TB
13166 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
13167 "type LOCK_TYPE must be a coarray", sym->name,
13168 &sym->declared_at);
fea54935
TB
13169 return;
13170 }
13171
4213f93b
PT
13172 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
13173 default initialization is defined (5.1.2.4.4). */
13174 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
13175 && sym->attr.dummy
13176 && sym->attr.intent == INTENT_OUT
13177 && sym->as
13178 && sym->as->type == AS_ASSUMED_SIZE)
4213f93b 13179 {
bc21d315 13180 for (c = sym->ts.u.derived->components; c; c = c->next)
4213f93b
PT
13181 {
13182 if (c->initializer)
13183 {
13184 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
13185 "ASSUMED SIZE and so cannot have a default initializer",
13186 sym->name, &sym->declared_at);
13187 return;
13188 }
13189 }
13190 }
13191
fea54935
TB
13192 /* F2008, C542. */
13193 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
13194 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
e535f1b2
TB
13195 {
13196 gfc_error ("Dummy argument '%s' at %L of LOCK_TYPE shall not be "
13197 "INTENT(OUT)", sym->name, &sym->declared_at);
13198 return;
13199 }
fea54935 13200
e535f1b2 13201 /* F2008, C525. */
fac665b2
TB
13202 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13203 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13204 && CLASS_DATA (sym)->attr.coarray_comp))
13205 || class_attr.codimension)
e535f1b2
TB
13206 && (sym->attr.result || sym->result == sym))
13207 {
565fc114 13208 gfc_error ("Function result '%s' at %L shall not be a coarray or have "
e535f1b2
TB
13209 "a coarray component", sym->name, &sym->declared_at);
13210 return;
13211 }
be59db2d
TB
13212
13213 /* F2008, C524. */
13214 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
13215 && sym->ts.u.derived->ts.is_iso_c)
e535f1b2
TB
13216 {
13217 gfc_error ("Variable '%s' at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
13218 "shall not be a coarray", sym->name, &sym->declared_at);
13219 return;
13220 }
be59db2d
TB
13221
13222 /* F2008, C525. */
fac665b2
TB
13223 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13224 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13225 && CLASS_DATA (sym)->attr.coarray_comp))
13226 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
13227 || class_attr.allocatable))
e535f1b2 13228 {
abc2d807
TB
13229 gfc_error ("Variable '%s' at %L with coarray component shall be a "
13230 "nonpointer, nonallocatable scalar, which is not a coarray",
e535f1b2
TB
13231 sym->name, &sym->declared_at);
13232 return;
13233 }
be59db2d
TB
13234
13235 /* F2008, C526. The function-result case was handled above. */
fac665b2
TB
13236 if (class_attr.codimension
13237 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
13238 || sym->attr.select_type_temporary
9f3761c5 13239 || sym->ns->save_all
be59db2d
TB
13240 || sym->ns->proc_name->attr.flavor == FL_MODULE
13241 || sym->ns->proc_name->attr.is_main_program
13242 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
e535f1b2
TB
13243 {
13244 gfc_error ("Variable '%s' at %L is a coarray and is not ALLOCATABLE, SAVE "
13245 "nor a dummy argument", sym->name, &sym->declared_at);
13246 return;
13247 }
fac665b2
TB
13248 /* F2008, C528. */
13249 else if (class_attr.codimension && !sym->attr.select_type_temporary
13250 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
e535f1b2
TB
13251 {
13252 gfc_error ("Coarray variable '%s' at %L shall not have codimensions with "
13253 "deferred shape", sym->name, &sym->declared_at);
13254 return;
13255 }
fac665b2
TB
13256 else if (class_attr.codimension && class_attr.allocatable && as
13257 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
e535f1b2
TB
13258 {
13259 gfc_error ("Allocatable coarray variable '%s' at %L must have "
13260 "deferred shape", sym->name, &sym->declared_at);
13261 return;
13262 }
be59db2d
TB
13263
13264 /* F2008, C541. */
fac665b2
TB
13265 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
13266 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
13267 && CLASS_DATA (sym)->attr.coarray_comp))
13268 || (class_attr.codimension && class_attr.allocatable))
be59db2d 13269 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
e535f1b2
TB
13270 {
13271 gfc_error ("Variable '%s' at %L is INTENT(OUT) and can thus not be an "
13272 "allocatable coarray or have coarray components",
13273 sym->name, &sym->declared_at);
13274 return;
13275 }
be59db2d 13276
fac665b2 13277 if (class_attr.codimension && sym->attr.dummy
be59db2d 13278 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
e535f1b2
TB
13279 {
13280 gfc_error ("Coarray dummy variable '%s' at %L not allowed in BIND(C) "
13281 "procedure '%s'", sym->name, &sym->declared_at,
13282 sym->ns->proc_name->name);
13283 return;
13284 }
be59db2d 13285
d0841b5b
TB
13286 if (sym->ts.type == BT_LOGICAL
13287 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
13288 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
13289 && sym->ns->proc_name->attr.is_bind_c)))
13290 {
13291 int i;
13292 for (i = 0; gfc_logical_kinds[i].kind; i++)
13293 if (gfc_logical_kinds[i].kind == sym->ts.kind)
13294 break;
13295 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
524af0d6
JB
13296 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument '%s' at "
13297 "%L with non-C_Bool kind in BIND(C) procedure "
13298 "'%s'", sym->name, &sym->declared_at,
13299 sym->ns->proc_name->name))
d0841b5b
TB
13300 return;
13301 else if (!gfc_logical_kinds[i].c_bool
524af0d6
JB
13302 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
13303 "'%s' at %L with non-C_Bool kind in "
13304 "BIND(C) procedure '%s'", sym->name,
13305 &sym->declared_at,
13306 sym->attr.function ? sym->name
13307 : sym->ns->proc_name->name))
d0841b5b
TB
13308 return;
13309 }
13310
af30f793 13311 switch (sym->attr.flavor)
54b4ba60 13312 {
af30f793 13313 case FL_VARIABLE:
524af0d6 13314 if (!resolve_fl_variable (sym, mp_flag))
2ed8d224
PT
13315 return;
13316 break;
219fa8c3 13317
2ed8d224 13318 case FL_PROCEDURE:
524af0d6 13319 if (!resolve_fl_procedure (sym, mp_flag))
2ed8d224 13320 return;
af30f793
PB
13321 break;
13322
13323 case FL_NAMELIST:
524af0d6 13324 if (!resolve_fl_namelist (sym))
3e1cf500 13325 return;
68ea355b
PT
13326 break;
13327
2ed8d224 13328 case FL_PARAMETER:
524af0d6 13329 if (!resolve_fl_parameter (sym))
2ed8d224 13330 return;
e0e85e06
PT
13331 break;
13332
af30f793
PB
13333 default:
13334 break;
54b4ba60
PB
13335 }
13336
6de9cd9a 13337 /* Resolve array specifier. Check as well some constraints
f7b529fa 13338 on COMMON blocks. */
6de9cd9a
DN
13339
13340 check_constant = sym->attr.in_common && !sym->attr.pointer;
98bbe5ee
PT
13341
13342 /* Set the formal_arg_flag so that check_conflict will not throw
13343 an error for host associated variables in the specification
13344 expression for an array_valued function. */
13345 if (sym->attr.function && sym->as)
13346 formal_arg_flag = 1;
13347
fd061185
TB
13348 saved_specification_expr = specification_expr;
13349 specification_expr = true;
6de9cd9a 13350 gfc_resolve_array_spec (sym->as, check_constant);
fd061185 13351 specification_expr = saved_specification_expr;
6de9cd9a 13352
98bbe5ee
PT
13353 formal_arg_flag = 0;
13354
a34437a1 13355 /* Resolve formal namespaces. */
f6ddbf11 13356 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
e4c1aa19 13357 && !sym->attr.contained && !sym->attr.intrinsic)
a34437a1 13358 gfc_resolve (sym->formal_ns);
6c7a4dfd 13359
acbdc378
JW
13360 /* Make sure the formal namespace is present. */
13361 if (sym->formal && !sym->formal_ns)
13362 {
13363 gfc_formal_arglist *formal = sym->formal;
13364 while (formal && !formal->sym)
13365 formal = formal->next;
13366
13367 if (formal)
13368 {
13369 sym->formal_ns = formal->sym->ns;
6f79f4d1
TB
13370 if (sym->ns != formal->sym->ns)
13371 sym->formal_ns->refs++;
acbdc378
JW
13372 }
13373 }
13374
6c7a4dfd 13375 /* Check threadprivate restrictions. */
5349080d 13376 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
6c7a4dfd 13377 && (!sym->attr.in_common
edf1eac2
SK
13378 && sym->module == NULL
13379 && (sym->ns->proc_name == NULL
13380 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
6c7a4dfd 13381 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
6b591ec0
PT
13382
13383 /* If we have come this far we can apply default-initializers, as
13384 described in 14.7.5, to those variables that have not already
13385 been assigned one. */
7114edca 13386 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
13387 && !sym->value
13388 && !sym->attr.allocatable
13389 && !sym->attr.alloc_comp)
6b591ec0
PT
13390 {
13391 symbol_attribute *a = &sym->attr;
13392
13393 if ((!a->save && !a->dummy && !a->pointer
edf1eac2 13394 && !a->in_common && !a->use_assoc
86e6a239 13395 && (a->referenced || a->result)
edf1eac2 13396 && !(a->function && sym != sym->result))
758e12af 13397 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
6b591ec0
PT
13398 apply_default_init (sym);
13399 }
52f49934 13400
50f30801
JW
13401 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
13402 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
c330d181
JW
13403 && !CLASS_DATA (sym)->attr.class_pointer
13404 && !CLASS_DATA (sym)->attr.allocatable)
86e6a239 13405 apply_default_init (sym);
50f30801 13406
52f49934
DK
13407 /* If this symbol has a type-spec, check it. */
13408 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
13409 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
524af0d6 13410 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
52f49934 13411 return;
6de9cd9a
DN
13412}
13413
13414
6de9cd9a
DN
13415/************* Resolve DATA statements *************/
13416
13417static struct
13418{
13419 gfc_data_value *vnode;
f2112868 13420 mpz_t left;
6de9cd9a
DN
13421}
13422values;
13423
13424
13425/* Advance the values structure to point to the next value in the data list. */
13426
524af0d6 13427static bool
6de9cd9a
DN
13428next_data_value (void)
13429{
f2112868 13430 while (mpz_cmp_ui (values.left, 0) == 0)
6de9cd9a 13431 {
abeab938 13432
6de9cd9a 13433 if (values.vnode->next == NULL)
524af0d6 13434 return false;
6de9cd9a
DN
13435
13436 values.vnode = values.vnode->next;
f2112868 13437 mpz_set (values.left, values.vnode->repeat);
6de9cd9a
DN
13438 }
13439
524af0d6 13440 return true;
6de9cd9a
DN
13441}
13442
13443
524af0d6 13444static bool
edf1eac2 13445check_data_variable (gfc_data_variable *var, locus *where)
6de9cd9a
DN
13446{
13447 gfc_expr *e;
13448 mpz_t size;
13449 mpz_t offset;
524af0d6 13450 bool t;
f5e440e1 13451 ar_type mark = AR_UNKNOWN;
6de9cd9a
DN
13452 int i;
13453 mpz_t section_index[GFC_MAX_DIMENSIONS];
13454 gfc_ref *ref;
13455 gfc_array_ref *ar;
e49be8f7
PT
13456 gfc_symbol *sym;
13457 int has_pointer;
6de9cd9a 13458
524af0d6
JB
13459 if (!gfc_resolve_expr (var->expr))
13460 return false;
6de9cd9a
DN
13461
13462 ar = NULL;
13463 mpz_init_set_si (offset, 0);
13464 e = var->expr;
13465
13466 if (e->expr_type != EXPR_VARIABLE)
13467 gfc_internal_error ("check_data_variable(): Bad expression");
13468
e49be8f7
PT
13469 sym = e->symtree->n.sym;
13470
13471 if (sym->ns->is_block_data && !sym->attr.in_common)
2ed8d224
PT
13472 {
13473 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
e49be8f7 13474 sym->name, &sym->declared_at);
2ed8d224
PT
13475 }
13476
e49be8f7 13477 if (e->ref == NULL && sym->as)
f1607c01
JD
13478 {
13479 gfc_error ("DATA array '%s' at %L must be specified in a previous"
e49be8f7 13480 " declaration", sym->name, where);
524af0d6 13481 return false;
f1607c01
JD
13482 }
13483
e49be8f7
PT
13484 has_pointer = sym->attr.pointer;
13485
a3935ffc
TB
13486 if (gfc_is_coindexed (e))
13487 {
13488 gfc_error ("DATA element '%s' at %L cannot have a coindex", sym->name,
13489 where);
524af0d6 13490 return false;
a3935ffc
TB
13491 }
13492
e49be8f7
PT
13493 for (ref = e->ref; ref; ref = ref->next)
13494 {
13495 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
13496 has_pointer = 1;
13497
13498 if (has_pointer
13499 && ref->type == REF_ARRAY
13500 && ref->u.ar.type != AR_FULL)
13501 {
13502 gfc_error ("DATA element '%s' at %L is a pointer and so must "
13503 "be a full array", sym->name, where);
524af0d6 13504 return false;
e49be8f7
PT
13505 }
13506 }
13507
13508 if (e->rank == 0 || has_pointer)
b8502435
RH
13509 {
13510 mpz_init_set_ui (size, 1);
13511 ref = NULL;
13512 }
6de9cd9a
DN
13513 else
13514 {
13515 ref = e->ref;
13516
13517 /* Find the array section reference. */
13518 for (ref = e->ref; ref; ref = ref->next)
13519 {
13520 if (ref->type != REF_ARRAY)
13521 continue;
13522 if (ref->u.ar.type == AR_ELEMENT)
13523 continue;
13524 break;
13525 }
6e45f57b 13526 gcc_assert (ref);
6de9cd9a 13527
1f2959f0 13528 /* Set marks according to the reference pattern. */
6de9cd9a
DN
13529 switch (ref->u.ar.type)
13530 {
13531 case AR_FULL:
f5e440e1 13532 mark = AR_FULL;
6de9cd9a
DN
13533 break;
13534
13535 case AR_SECTION:
edf1eac2
SK
13536 ar = &ref->u.ar;
13537 /* Get the start position of array section. */
13538 gfc_get_section_index (ar, section_index, &offset);
13539 mark = AR_SECTION;
6de9cd9a
DN
13540 break;
13541
13542 default:
6e45f57b 13543 gcc_unreachable ();
6de9cd9a
DN
13544 }
13545
524af0d6 13546 if (!gfc_array_size (e, &size))
6de9cd9a
DN
13547 {
13548 gfc_error ("Nonconstant array section at %L in DATA statement",
13549 &e->where);
13550 mpz_clear (offset);
524af0d6 13551 return false;
6de9cd9a
DN
13552 }
13553 }
13554
524af0d6 13555 t = true;
6de9cd9a
DN
13556
13557 while (mpz_cmp_ui (size, 0) > 0)
13558 {
524af0d6 13559 if (!next_data_value ())
6de9cd9a
DN
13560 {
13561 gfc_error ("DATA statement at %L has more variables than values",
13562 where);
524af0d6 13563 t = false;
6de9cd9a
DN
13564 break;
13565 }
13566
13567 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
524af0d6 13568 if (!t)
6de9cd9a
DN
13569 break;
13570
b8502435
RH
13571 /* If we have more than one element left in the repeat count,
13572 and we have more than one element left in the target variable,
13573 then create a range assignment. */
f2112868 13574 /* FIXME: Only done for full arrays for now, since array sections
b8502435
RH
13575 seem tricky. */
13576 if (mark == AR_FULL && ref && ref->next == NULL
f2112868 13577 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
b8502435
RH
13578 {
13579 mpz_t range;
13580
f2112868 13581 if (mpz_cmp (size, values.left) >= 0)
b8502435 13582 {
f2112868
SK
13583 mpz_init_set (range, values.left);
13584 mpz_sub (size, size, values.left);
13585 mpz_set_ui (values.left, 0);
b8502435
RH
13586 }
13587 else
13588 {
13589 mpz_init_set (range, size);
f2112868 13590 mpz_sub (values.left, values.left, size);
b8502435
RH
13591 mpz_set_ui (size, 0);
13592 }
13593
21ea4922
JJ
13594 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13595 offset, &range);
b8502435
RH
13596
13597 mpz_add (offset, offset, range);
13598 mpz_clear (range);
e5880243 13599
524af0d6 13600 if (!t)
e5880243 13601 break;
b8502435
RH
13602 }
13603
6de9cd9a 13604 /* Assign initial value to symbol. */
b8502435
RH
13605 else
13606 {
f2112868 13607 mpz_sub_ui (values.left, values.left, 1);
b8502435 13608 mpz_sub_ui (size, size, 1);
6de9cd9a 13609
21ea4922
JJ
13610 t = gfc_assign_data_value (var->expr, values.vnode->expr,
13611 offset, NULL);
524af0d6 13612 if (!t)
a24668a3 13613 break;
6de9cd9a 13614
b8502435
RH
13615 if (mark == AR_FULL)
13616 mpz_add_ui (offset, offset, 1);
6de9cd9a 13617
b8502435
RH
13618 /* Modify the array section indexes and recalculate the offset
13619 for next element. */
13620 else if (mark == AR_SECTION)
13621 gfc_advance_section (section_index, ar, &offset);
13622 }
6de9cd9a 13623 }
b8502435 13624
f5e440e1 13625 if (mark == AR_SECTION)
6de9cd9a
DN
13626 {
13627 for (i = 0; i < ar->dimen; i++)
edf1eac2 13628 mpz_clear (section_index[i]);
6de9cd9a
DN
13629 }
13630
13631 mpz_clear (size);
13632 mpz_clear (offset);
13633
13634 return t;
13635}
13636
13637
524af0d6 13638static bool traverse_data_var (gfc_data_variable *, locus *);
6de9cd9a
DN
13639
13640/* Iterate over a list of elements in a DATA statement. */
13641
524af0d6 13642static bool
edf1eac2 13643traverse_data_list (gfc_data_variable *var, locus *where)
6de9cd9a
DN
13644{
13645 mpz_t trip;
13646 iterator_stack frame;
2220652d 13647 gfc_expr *e, *start, *end, *step;
524af0d6 13648 bool retval = true;
6de9cd9a
DN
13649
13650 mpz_init (frame.value);
147a19a9 13651 mpz_init (trip);
6de9cd9a 13652
2220652d
PT
13653 start = gfc_copy_expr (var->iter.start);
13654 end = gfc_copy_expr (var->iter.end);
13655 step = gfc_copy_expr (var->iter.step);
13656
524af0d6 13657 if (!gfc_simplify_expr (start, 1)
edf1eac2 13658 || start->expr_type != EXPR_CONSTANT)
2220652d 13659 {
147a19a9
DF
13660 gfc_error ("start of implied-do loop at %L could not be "
13661 "simplified to a constant value", &start->where);
524af0d6 13662 retval = false;
2220652d
PT
13663 goto cleanup;
13664 }
524af0d6 13665 if (!gfc_simplify_expr (end, 1)
edf1eac2 13666 || end->expr_type != EXPR_CONSTANT)
2220652d 13667 {
147a19a9
DF
13668 gfc_error ("end of implied-do loop at %L could not be "
13669 "simplified to a constant value", &start->where);
524af0d6 13670 retval = false;
2220652d
PT
13671 goto cleanup;
13672 }
524af0d6 13673 if (!gfc_simplify_expr (step, 1)
edf1eac2 13674 || step->expr_type != EXPR_CONSTANT)
2220652d 13675 {
147a19a9
DF
13676 gfc_error ("step of implied-do loop at %L could not be "
13677 "simplified to a constant value", &start->where);
524af0d6 13678 retval = false;
2220652d
PT
13679 goto cleanup;
13680 }
13681
147a19a9 13682 mpz_set (trip, end->value.integer);
2220652d
PT
13683 mpz_sub (trip, trip, start->value.integer);
13684 mpz_add (trip, trip, step->value.integer);
6de9cd9a 13685
2220652d 13686 mpz_div (trip, trip, step->value.integer);
6de9cd9a 13687
2220652d 13688 mpz_set (frame.value, start->value.integer);
6de9cd9a
DN
13689
13690 frame.prev = iter_stack;
13691 frame.variable = var->iter.var->symtree;
13692 iter_stack = &frame;
13693
13694 while (mpz_cmp_ui (trip, 0) > 0)
13695 {
524af0d6 13696 if (!traverse_data_var (var->list, where))
6de9cd9a 13697 {
524af0d6 13698 retval = false;
2220652d 13699 goto cleanup;
6de9cd9a
DN
13700 }
13701
13702 e = gfc_copy_expr (var->expr);
524af0d6 13703 if (!gfc_simplify_expr (e, 1))
2220652d
PT
13704 {
13705 gfc_free_expr (e);
524af0d6 13706 retval = false;
2220652d
PT
13707 goto cleanup;
13708 }
6de9cd9a 13709
2220652d 13710 mpz_add (frame.value, frame.value, step->value.integer);
6de9cd9a
DN
13711
13712 mpz_sub_ui (trip, trip, 1);
13713 }
13714
2220652d 13715cleanup:
6de9cd9a 13716 mpz_clear (frame.value);
147a19a9 13717 mpz_clear (trip);
6de9cd9a 13718
2220652d
PT
13719 gfc_free_expr (start);
13720 gfc_free_expr (end);
13721 gfc_free_expr (step);
13722
6de9cd9a 13723 iter_stack = frame.prev;
2220652d 13724 return retval;
6de9cd9a
DN
13725}
13726
13727
13728/* Type resolve variables in the variable list of a DATA statement. */
13729
524af0d6 13730static bool
edf1eac2 13731traverse_data_var (gfc_data_variable *var, locus *where)
6de9cd9a 13732{
524af0d6 13733 bool t;
6de9cd9a
DN
13734
13735 for (; var; var = var->next)
13736 {
13737 if (var->expr == NULL)
13738 t = traverse_data_list (var, where);
13739 else
13740 t = check_data_variable (var, where);
13741
524af0d6
JB
13742 if (!t)
13743 return false;
6de9cd9a
DN
13744 }
13745
524af0d6 13746 return true;
6de9cd9a
DN
13747}
13748
13749
13750/* Resolve the expressions and iterators associated with a data statement.
13751 This is separate from the assignment checking because data lists should
13752 only be resolved once. */
13753
524af0d6 13754static bool
edf1eac2 13755resolve_data_variables (gfc_data_variable *d)
6de9cd9a 13756{
6de9cd9a
DN
13757 for (; d; d = d->next)
13758 {
13759 if (d->list == NULL)
13760 {
524af0d6
JB
13761 if (!gfc_resolve_expr (d->expr))
13762 return false;
6de9cd9a
DN
13763 }
13764 else
13765 {
524af0d6
JB
13766 if (!gfc_resolve_iterator (&d->iter, false, true))
13767 return false;
6de9cd9a 13768
524af0d6
JB
13769 if (!resolve_data_variables (d->list))
13770 return false;
6de9cd9a
DN
13771 }
13772 }
13773
524af0d6 13774 return true;
6de9cd9a
DN
13775}
13776
13777
13778/* Resolve a single DATA statement. We implement this by storing a pointer to
13779 the value list into static variables, and then recursively traversing the
13780 variables list, expanding iterators and such. */
13781
13782static void
f2112868 13783resolve_data (gfc_data *d)
6de9cd9a 13784{
f2112868 13785
524af0d6 13786 if (!resolve_data_variables (d->var))
6de9cd9a
DN
13787 return;
13788
13789 values.vnode = d->value;
f2112868
SK
13790 if (d->value == NULL)
13791 mpz_set_ui (values.left, 0);
13792 else
13793 mpz_set (values.left, d->value->repeat);
6de9cd9a 13794
524af0d6 13795 if (!traverse_data_var (d->var, &d->where))
6de9cd9a
DN
13796 return;
13797
13798 /* At this point, we better not have any values left. */
13799
524af0d6 13800 if (next_data_value ())
6de9cd9a
DN
13801 gfc_error ("DATA statement at %L has more values than variables",
13802 &d->where);
13803}
13804
13805
d2088bb6
PT
13806/* 12.6 Constraint: In a pure subprogram any variable which is in common or
13807 accessed by host or use association, is a dummy argument to a pure function,
13808 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
13809 is storage associated with any such variable, shall not be used in the
13810 following contexts: (clients of this function). */
13811
df2fba9e 13812/* Determines if a variable is not 'pure', i.e., not assignable within a pure
edf1eac2
SK
13813 procedure. Returns zero if assignment is OK, nonzero if there is a
13814 problem. */
6de9cd9a 13815int
edf1eac2 13816gfc_impure_variable (gfc_symbol *sym)
6de9cd9a 13817{
d2088bb6 13818 gfc_symbol *proc;
d1039125 13819 gfc_namespace *ns;
d2088bb6 13820
6de9cd9a
DN
13821 if (sym->attr.use_assoc || sym->attr.in_common)
13822 return 1;
13823
d1039125
JW
13824 /* Check if the symbol's ns is inside the pure procedure. */
13825 for (ns = gfc_current_ns; ns; ns = ns->parent)
13826 {
13827 if (ns == sym->ns)
13828 break;
13829 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
13830 return 1;
13831 }
6de9cd9a 13832
d2088bb6 13833 proc = sym->ns->proc_name;
c915f8bc
TB
13834 if (sym->attr.dummy
13835 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
13836 || proc->attr.function))
d2088bb6 13837 return 1;
6de9cd9a 13838
d2088bb6
PT
13839 /* TODO: Sort out what can be storage associated, if anything, and include
13840 it here. In principle equivalences should be scanned but it does not
13841 seem to be possible to storage associate an impure variable this way. */
6de9cd9a
DN
13842 return 0;
13843}
13844
13845
d1039125
JW
13846/* Test whether a symbol is pure or not. For a NULL pointer, checks if the
13847 current namespace is inside a pure procedure. */
6de9cd9a
DN
13848
13849int
edf1eac2 13850gfc_pure (gfc_symbol *sym)
6de9cd9a
DN
13851{
13852 symbol_attribute attr;
d1039125 13853 gfc_namespace *ns;
6de9cd9a
DN
13854
13855 if (sym == NULL)
d1039125
JW
13856 {
13857 /* Check if the current namespace or one of its parents
13858 belongs to a pure procedure. */
13859 for (ns = gfc_current_ns; ns; ns = ns->parent)
13860 {
13861 sym = ns->proc_name;
13862 if (sym == NULL)
13863 return 0;
13864 attr = sym->attr;
e6c14898 13865 if (attr.flavor == FL_PROCEDURE && attr.pure)
d1039125
JW
13866 return 1;
13867 }
13868 return 0;
13869 }
6de9cd9a
DN
13870
13871 attr = sym->attr;
13872
e6c14898 13873 return attr.flavor == FL_PROCEDURE && attr.pure;
6de9cd9a
DN
13874}
13875
13876
f1f39033
PT
13877/* Test whether a symbol is implicitly pure or not. For a NULL pointer,
13878 checks if the current namespace is implicitly pure. Note that this
13879 function returns false for a PURE procedure. */
13880
13881int
13882gfc_implicit_pure (gfc_symbol *sym)
13883{
f29041dd 13884 gfc_namespace *ns;
f1f39033
PT
13885
13886 if (sym == NULL)
13887 {
f29041dd
TK
13888 /* Check if the current procedure is implicit_pure. Walk up
13889 the procedure list until we find a procedure. */
13890 for (ns = gfc_current_ns; ns; ns = ns->parent)
13891 {
13892 sym = ns->proc_name;
13893 if (sym == NULL)
13894 return 0;
4d382327 13895
f29041dd
TK
13896 if (sym->attr.flavor == FL_PROCEDURE)
13897 break;
13898 }
f1f39033 13899 }
4d382327 13900
f29041dd
TK
13901 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
13902 && !sym->attr.pure;
f1f39033
PT
13903}
13904
13905
6de9cd9a
DN
13906/* Test whether the current procedure is elemental or not. */
13907
13908int
edf1eac2 13909gfc_elemental (gfc_symbol *sym)
6de9cd9a
DN
13910{
13911 symbol_attribute attr;
13912
13913 if (sym == NULL)
13914 sym = gfc_current_ns->proc_name;
13915 if (sym == NULL)
13916 return 0;
13917 attr = sym->attr;
13918
13919 return attr.flavor == FL_PROCEDURE && attr.elemental;
13920}
13921
13922
13923/* Warn about unused labels. */
13924
13925static void
edf1eac2 13926warn_unused_fortran_label (gfc_st_label *label)
6de9cd9a 13927{
5cf54585 13928 if (label == NULL)
6de9cd9a
DN
13929 return;
13930
994c1cc0 13931 warn_unused_fortran_label (label->left);
6de9cd9a 13932
5cf54585
TS
13933 if (label->defined == ST_LABEL_UNKNOWN)
13934 return;
6de9cd9a 13935
5cf54585
TS
13936 switch (label->referenced)
13937 {
13938 case ST_LABEL_UNKNOWN:
13939 gfc_warning ("Label %d at %L defined but not used", label->value,
13940 &label->where);
13941 break;
6de9cd9a 13942
5cf54585
TS
13943 case ST_LABEL_BAD_TARGET:
13944 gfc_warning ("Label %d at %L defined but cannot be used",
13945 label->value, &label->where);
13946 break;
6de9cd9a 13947
5cf54585
TS
13948 default:
13949 break;
6de9cd9a 13950 }
5cf54585 13951
994c1cc0 13952 warn_unused_fortran_label (label->right);
6de9cd9a
DN
13953}
13954
13955
e8ec07e1
PT
13956/* Returns the sequence type of a symbol or sequence. */
13957
13958static seq_type
13959sequence_type (gfc_typespec ts)
13960{
13961 seq_type result;
13962 gfc_component *c;
13963
13964 switch (ts.type)
13965 {
13966 case BT_DERIVED:
13967
bc21d315 13968 if (ts.u.derived->components == NULL)
e8ec07e1
PT
13969 return SEQ_NONDEFAULT;
13970
bc21d315
JW
13971 result = sequence_type (ts.u.derived->components->ts);
13972 for (c = ts.u.derived->components->next; c; c = c->next)
e8ec07e1
PT
13973 if (sequence_type (c->ts) != result)
13974 return SEQ_MIXED;
13975
13976 return result;
13977
13978 case BT_CHARACTER:
13979 if (ts.kind != gfc_default_character_kind)
13980 return SEQ_NONDEFAULT;
13981
13982 return SEQ_CHARACTER;
13983
13984 case BT_INTEGER:
13985 if (ts.kind != gfc_default_integer_kind)
13986 return SEQ_NONDEFAULT;
13987
13988 return SEQ_NUMERIC;
13989
13990 case BT_REAL:
13991 if (!(ts.kind == gfc_default_real_kind
edf1eac2 13992 || ts.kind == gfc_default_double_kind))
e8ec07e1
PT
13993 return SEQ_NONDEFAULT;
13994
13995 return SEQ_NUMERIC;
13996
13997 case BT_COMPLEX:
13998 if (ts.kind != gfc_default_complex_kind)
13999 return SEQ_NONDEFAULT;
14000
14001 return SEQ_NUMERIC;
14002
14003 case BT_LOGICAL:
14004 if (ts.kind != gfc_default_logical_kind)
14005 return SEQ_NONDEFAULT;
14006
14007 return SEQ_NUMERIC;
14008
14009 default:
14010 return SEQ_NONDEFAULT;
14011 }
14012}
14013
14014
6de9cd9a
DN
14015/* Resolve derived type EQUIVALENCE object. */
14016
524af0d6 14017static bool
6de9cd9a
DN
14018resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14019{
6de9cd9a
DN
14020 gfc_component *c = derived->components;
14021
14022 if (!derived)
524af0d6 14023 return true;
6de9cd9a
DN
14024
14025 /* Shall not be an object of nonsequence derived type. */
14026 if (!derived->attr.sequence)
14027 {
14028 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
edf1eac2
SK
14029 "attribute to be an EQUIVALENCE object", sym->name,
14030 &e->where);
524af0d6 14031 return false;
6de9cd9a
DN
14032 }
14033
66e4ab31 14034 /* Shall not have allocatable components. */
5046aff5
PT
14035 if (derived->attr.alloc_comp)
14036 {
14037 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
edf1eac2
SK
14038 "components to be an EQUIVALENCE object",sym->name,
14039 &e->where);
524af0d6 14040 return false;
5046aff5
PT
14041 }
14042
16e520b6 14043 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
cddcf0d4
TB
14044 {
14045 gfc_error ("Derived type variable '%s' at %L with default "
14046 "initialization cannot be in EQUIVALENCE with a variable "
14047 "in COMMON", sym->name, &e->where);
524af0d6 14048 return false;
cddcf0d4
TB
14049 }
14050
6de9cd9a
DN
14051 for (; c ; c = c->next)
14052 {
bc21d315 14053 if (c->ts.type == BT_DERIVED
524af0d6
JB
14054 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
14055 return false;
05c1e3a7 14056
6de9cd9a 14057 /* Shall not be an object of sequence derived type containing a pointer
edf1eac2 14058 in the structure. */
d4b7d0f0 14059 if (c->attr.pointer)
edf1eac2
SK
14060 {
14061 gfc_error ("Derived type variable '%s' at %L with pointer "
14062 "component(s) cannot be an EQUIVALENCE object",
14063 sym->name, &e->where);
524af0d6 14064 return false;
edf1eac2 14065 }
6de9cd9a 14066 }
524af0d6 14067 return true;
6de9cd9a
DN
14068}
14069
14070
4d382327 14071/* Resolve equivalence object.
e8ec07e1
PT
14072 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
14073 an allocatable array, an object of nonsequence derived type, an object of
6de9cd9a
DN
14074 sequence derived type containing a pointer at any level of component
14075 selection, an automatic object, a function name, an entry name, a result
14076 name, a named constant, a structure component, or a subobject of any of
e8ec07e1
PT
14077 the preceding objects. A substring shall not have length zero. A
14078 derived type shall not have components with default initialization nor
14079 shall two objects of an equivalence group be initialized.
ee7e677f 14080 Either all or none of the objects shall have an protected attribute.
e8ec07e1
PT
14081 The simple constraints are done in symbol.c(check_conflict) and the rest
14082 are implemented here. */
6de9cd9a
DN
14083
14084static void
14085resolve_equivalence (gfc_equiv *eq)
14086{
14087 gfc_symbol *sym;
e8ec07e1 14088 gfc_symbol *first_sym;
6de9cd9a
DN
14089 gfc_expr *e;
14090 gfc_ref *r;
e8ec07e1
PT
14091 locus *last_where = NULL;
14092 seq_type eq_type, last_eq_type;
14093 gfc_typespec *last_ts;
ee7e677f 14094 int object, cnt_protected;
e8ec07e1
PT
14095 const char *msg;
14096
e8ec07e1 14097 last_ts = &eq->expr->symtree->n.sym->ts;
6de9cd9a 14098
e8ec07e1
PT
14099 first_sym = eq->expr->symtree->n.sym;
14100
ee7e677f
TB
14101 cnt_protected = 0;
14102
e8ec07e1 14103 for (object = 1; eq; eq = eq->eq, object++)
6de9cd9a
DN
14104 {
14105 e = eq->expr;
a8006d09
JJ
14106
14107 e->ts = e->symtree->n.sym->ts;
14108 /* match_varspec might not know yet if it is seeing
14109 array reference or substring reference, as it doesn't
14110 know the types. */
14111 if (e->ref && e->ref->type == REF_ARRAY)
14112 {
14113 gfc_ref *ref = e->ref;
14114 sym = e->symtree->n.sym;
14115
14116 if (sym->attr.dimension)
14117 {
14118 ref->u.ar.as = sym->as;
14119 ref = ref->next;
14120 }
14121
14122 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
14123 if (e->ts.type == BT_CHARACTER
14124 && ref
14125 && ref->type == REF_ARRAY
14126 && ref->u.ar.dimen == 1
14127 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
14128 && ref->u.ar.stride[0] == NULL)
14129 {
14130 gfc_expr *start = ref->u.ar.start[0];
14131 gfc_expr *end = ref->u.ar.end[0];
14132 void *mem = NULL;
14133
14134 /* Optimize away the (:) reference. */
14135 if (start == NULL && end == NULL)
14136 {
14137 if (e->ref == ref)
14138 e->ref = ref->next;
14139 else
14140 e->ref->next = ref->next;
14141 mem = ref;
14142 }
14143 else
14144 {
14145 ref->type = REF_SUBSTRING;
14146 if (start == NULL)
b7e75771
JD
14147 start = gfc_get_int_expr (gfc_default_integer_kind,
14148 NULL, 1);
a8006d09 14149 ref->u.ss.start = start;
bc21d315
JW
14150 if (end == NULL && e->ts.u.cl)
14151 end = gfc_copy_expr (e->ts.u.cl->length);
a8006d09 14152 ref->u.ss.end = end;
bc21d315
JW
14153 ref->u.ss.length = e->ts.u.cl;
14154 e->ts.u.cl = NULL;
a8006d09
JJ
14155 }
14156 ref = ref->next;
cede9502 14157 free (mem);
a8006d09
JJ
14158 }
14159
14160 /* Any further ref is an error. */
14161 if (ref)
14162 {
14163 gcc_assert (ref->type == REF_ARRAY);
14164 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
14165 &ref->u.ar.where);
14166 continue;
14167 }
14168 }
14169
524af0d6 14170 if (!gfc_resolve_expr (e))
edf1eac2 14171 continue;
6de9cd9a
DN
14172
14173 sym = e->symtree->n.sym;
6de9cd9a 14174
9aa433c2 14175 if (sym->attr.is_protected)
ee7e677f
TB
14176 cnt_protected++;
14177 if (cnt_protected > 0 && cnt_protected != object)
14178 {
14179 gfc_error ("Either all or none of the objects in the "
14180 "EQUIVALENCE set at %L shall have the "
14181 "PROTECTED attribute",
14182 &e->where);
14183 break;
edf1eac2 14184 }
ee7e677f 14185
e8ec07e1 14186 /* Shall not equivalence common block variables in a PURE procedure. */
05c1e3a7 14187 if (sym->ns->proc_name
edf1eac2
SK
14188 && sym->ns->proc_name->attr.pure
14189 && sym->attr.in_common)
14190 {
14191 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
e8ec07e1
PT
14192 "object in the pure procedure '%s'",
14193 sym->name, &e->where, sym->ns->proc_name->name);
edf1eac2
SK
14194 break;
14195 }
05c1e3a7
BF
14196
14197 /* Shall not be a named constant. */
6de9cd9a 14198 if (e->expr_type == EXPR_CONSTANT)
edf1eac2
SK
14199 {
14200 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
14201 "object", sym->name, &e->where);
14202 continue;
14203 }
6de9cd9a 14204
bc21d315 14205 if (e->ts.type == BT_DERIVED
524af0d6 14206 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
edf1eac2 14207 continue;
6de9cd9a 14208
e8ec07e1
PT
14209 /* Check that the types correspond correctly:
14210 Note 5.28:
14211 A numeric sequence structure may be equivalenced to another sequence
14212 structure, an object of default integer type, default real type, double
14213 precision real type, default logical type such that components of the
14214 structure ultimately only become associated to objects of the same
14215 kind. A character sequence structure may be equivalenced to an object
14216 of default character kind or another character sequence structure.
14217 Other objects may be equivalenced only to objects of the same type and
14218 kind parameters. */
14219
14220 /* Identical types are unconditionally OK. */
14221 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
14222 goto identical_types;
14223
14224 last_eq_type = sequence_type (*last_ts);
14225 eq_type = sequence_type (sym->ts);
14226
14227 /* Since the pair of objects is not of the same type, mixed or
14228 non-default sequences can be rejected. */
14229
14230 msg = "Sequence %s with mixed components in EQUIVALENCE "
14231 "statement at %L with different type objects";
14232 if ((object ==2
edf1eac2 14233 && last_eq_type == SEQ_MIXED
524af0d6 14234 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
edf1eac2 14235 || (eq_type == SEQ_MIXED
524af0d6 14236 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
e8ec07e1
PT
14237 continue;
14238
14239 msg = "Non-default type object or sequence %s in EQUIVALENCE "
14240 "statement at %L with objects of different type";
14241 if ((object ==2
edf1eac2 14242 && last_eq_type == SEQ_NONDEFAULT
524af0d6 14243 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
edf1eac2 14244 || (eq_type == SEQ_NONDEFAULT
524af0d6 14245 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
e8ec07e1
PT
14246 continue;
14247
14248 msg ="Non-CHARACTER object '%s' in default CHARACTER "
14249 "EQUIVALENCE statement at %L";
14250 if (last_eq_type == SEQ_CHARACTER
edf1eac2 14251 && eq_type != SEQ_CHARACTER
524af0d6 14252 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
e8ec07e1
PT
14253 continue;
14254
14255 msg ="Non-NUMERIC object '%s' in default NUMERIC "
14256 "EQUIVALENCE statement at %L";
14257 if (last_eq_type == SEQ_NUMERIC
edf1eac2 14258 && eq_type != SEQ_NUMERIC
524af0d6 14259 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
e8ec07e1
PT
14260 continue;
14261
14262 identical_types:
14263 last_ts =&sym->ts;
14264 last_where = &e->where;
14265
6de9cd9a 14266 if (!e->ref)
edf1eac2 14267 continue;
6de9cd9a
DN
14268
14269 /* Shall not be an automatic array. */
14270 if (e->ref->type == REF_ARRAY
524af0d6 14271 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
edf1eac2
SK
14272 {
14273 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
14274 "an EQUIVALENCE object", sym->name, &e->where);
14275 continue;
14276 }
6de9cd9a 14277
6de9cd9a
DN
14278 r = e->ref;
14279 while (r)
edf1eac2 14280 {
a8006d09
JJ
14281 /* Shall not be a structure component. */
14282 if (r->type == REF_COMPONENT)
14283 {
14284 gfc_error ("Structure component '%s' at %L cannot be an "
14285 "EQUIVALENCE object",
14286 r->u.c.component->name, &e->where);
14287 break;
14288 }
14289
14290 /* A substring shall not have length zero. */
14291 if (r->type == REF_SUBSTRING)
14292 {
14293 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
14294 {
14295 gfc_error ("Substring at %L has length zero",
14296 &r->u.ss.start->where);
14297 break;
14298 }
14299 }
14300 r = r->next;
14301 }
05c1e3a7
BF
14302 }
14303}
cf4d246b
JJ
14304
14305
66e4ab31 14306/* Resolve function and ENTRY types, issue diagnostics if needed. */
cf4d246b
JJ
14307
14308static void
edf1eac2 14309resolve_fntype (gfc_namespace *ns)
cf4d246b
JJ
14310{
14311 gfc_entry_list *el;
14312 gfc_symbol *sym;
14313
14314 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
14315 return;
14316
14317 /* If there are any entries, ns->proc_name is the entry master
14318 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
14319 if (ns->entries)
14320 sym = ns->entries->sym;
14321 else
14322 sym = ns->proc_name;
14323 if (sym->result == sym
14324 && sym->ts.type == BT_UNKNOWN
524af0d6 14325 && !gfc_set_default_type (sym, 0, NULL)
cf4d246b
JJ
14326 && !sym->attr.untyped)
14327 {
14328 gfc_error ("Function '%s' at %L has no IMPLICIT type",
14329 sym->name, &sym->declared_at);
14330 sym->attr.untyped = 1;
14331 }
14332
bc21d315 14333 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
0d6872cb 14334 && !sym->attr.contained
6e2062b0
JW
14335 && !gfc_check_symbol_access (sym->ts.u.derived)
14336 && gfc_check_symbol_access (sym))
3bcc018c 14337 {
9717f7a1 14338 gfc_notify_std (GFC_STD_F2003, "PUBLIC function '%s' at "
0d6872cb 14339 "%L of PRIVATE type '%s'", sym->name,
bc21d315 14340 &sym->declared_at, sym->ts.u.derived->name);
3bcc018c
EE
14341 }
14342
7453378e 14343 if (ns->entries)
cf4d246b
JJ
14344 for (el = ns->entries->next; el; el = el->next)
14345 {
14346 if (el->sym->result == el->sym
14347 && el->sym->ts.type == BT_UNKNOWN
524af0d6 14348 && !gfc_set_default_type (el->sym, 0, NULL)
cf4d246b
JJ
14349 && !el->sym->attr.untyped)
14350 {
14351 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
14352 el->sym->name, &el->sym->declared_at);
14353 el->sym->attr.untyped = 1;
14354 }
14355 }
14356}
14357
94747289 14358
0e3e65bc
PT
14359/* 12.3.2.1.1 Defined operators. */
14360
524af0d6 14361static bool
94747289 14362check_uop_procedure (gfc_symbol *sym, locus where)
0e3e65bc 14363{
0e3e65bc
PT
14364 gfc_formal_arglist *formal;
14365
94747289
DK
14366 if (!sym->attr.function)
14367 {
14368 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
14369 sym->name, &where);
524af0d6 14370 return false;
94747289 14371 }
05c1e3a7 14372
94747289 14373 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
14374 && !(sym->ts.u.cl && sym->ts.u.cl->length)
14375 && !(sym->result && sym->result->ts.u.cl
14376 && sym->result->ts.u.cl->length))
94747289
DK
14377 {
14378 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
14379 "character length", sym->name, &where);
524af0d6 14380 return false;
94747289 14381 }
0e3e65bc 14382
4cbc9039 14383 formal = gfc_sym_get_dummy_args (sym);
94747289 14384 if (!formal || !formal->sym)
0e3e65bc 14385 {
94747289
DK
14386 gfc_error ("User operator procedure '%s' at %L must have at least "
14387 "one argument", sym->name, &where);
524af0d6 14388 return false;
94747289 14389 }
0e3e65bc 14390
94747289
DK
14391 if (formal->sym->attr.intent != INTENT_IN)
14392 {
14393 gfc_error ("First argument of operator interface at %L must be "
14394 "INTENT(IN)", &where);
524af0d6 14395 return false;
94747289 14396 }
0e3e65bc 14397
94747289
DK
14398 if (formal->sym->attr.optional)
14399 {
14400 gfc_error ("First argument of operator interface at %L cannot be "
14401 "optional", &where);
524af0d6 14402 return false;
94747289 14403 }
0e3e65bc 14404
94747289
DK
14405 formal = formal->next;
14406 if (!formal || !formal->sym)
524af0d6 14407 return true;
0e3e65bc 14408
94747289
DK
14409 if (formal->sym->attr.intent != INTENT_IN)
14410 {
14411 gfc_error ("Second argument of operator interface at %L must be "
14412 "INTENT(IN)", &where);
524af0d6 14413 return false;
94747289 14414 }
0e3e65bc 14415
94747289
DK
14416 if (formal->sym->attr.optional)
14417 {
14418 gfc_error ("Second argument of operator interface at %L cannot be "
14419 "optional", &where);
524af0d6 14420 return false;
94747289 14421 }
0e3e65bc 14422
94747289
DK
14423 if (formal->next)
14424 {
14425 gfc_error ("Operator interface at %L must have, at most, two "
14426 "arguments", &where);
524af0d6 14427 return false;
94747289 14428 }
0e3e65bc 14429
524af0d6 14430 return true;
94747289 14431}
0e3e65bc 14432
94747289
DK
14433static void
14434gfc_resolve_uops (gfc_symtree *symtree)
14435{
14436 gfc_interface *itr;
14437
14438 if (symtree == NULL)
14439 return;
14440
14441 gfc_resolve_uops (symtree->left);
14442 gfc_resolve_uops (symtree->right);
14443
14444 for (itr = symtree->n.uop->op; itr; itr = itr->next)
14445 check_uop_procedure (itr->sym, itr->sym->declared_at);
0e3e65bc
PT
14446}
14447
cf4d246b 14448
efb0828d
L
14449/* Examine all of the expressions associated with a program unit,
14450 assign types to all intermediate expressions, make sure that all
14451 assignments are to compatible types and figure out which names
14452 refer to which functions or subroutines. It doesn't check code
14453 block, which is handled by resolve_code. */
6de9cd9a 14454
efb0828d 14455static void
edf1eac2 14456resolve_types (gfc_namespace *ns)
6de9cd9a 14457{
efb0828d 14458 gfc_namespace *n;
6de9cd9a
DN
14459 gfc_charlen *cl;
14460 gfc_data *d;
14461 gfc_equiv *eq;
a82f1f2e 14462 gfc_namespace* old_ns = gfc_current_ns;
6de9cd9a 14463
52f49934
DK
14464 /* Check that all IMPLICIT types are ok. */
14465 if (!ns->seen_implicit_none)
14466 {
14467 unsigned letter;
14468 for (letter = 0; letter != GFC_LETTERS; ++letter)
14469 if (ns->set_flag[letter]
524af0d6
JB
14470 && !resolve_typespec_used (&ns->default_type[letter],
14471 &ns->implicit_loc[letter], NULL))
52f49934
DK
14472 return;
14473 }
14474
a82f1f2e
DK
14475 gfc_current_ns = ns;
14476
0f3162e3
PT
14477 resolve_entries (ns);
14478
346ecba8 14479 resolve_common_vars (ns->blank_common.head, false);
ad22b1ff
TB
14480 resolve_common_blocks (ns->common_root);
14481
0f3162e3
PT
14482 resolve_contained_functions (ns);
14483
12578be7
TB
14484 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
14485 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
14486 resolve_formal_arglist (ns->proc_name);
14487
a8b3b0b6
CR
14488 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
14489
5cd09fac
TS
14490 for (cl = ns->cl_list; cl; cl = cl->next)
14491 resolve_charlen (cl);
14492
6de9cd9a
DN
14493 gfc_traverse_ns (ns, resolve_symbol);
14494
cf4d246b
JJ
14495 resolve_fntype (ns);
14496
6de9cd9a
DN
14497 for (n = ns->contained; n; n = n->sibling)
14498 {
14499 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
14500 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
14501 "also be PURE", n->proc_name->name,
14502 &n->proc_name->declared_at);
14503
efb0828d 14504 resolve_types (n);
6de9cd9a
DN
14505 }
14506
14507 forall_flag = 0;
ce96d372 14508 gfc_do_concurrent_flag = 0;
6de9cd9a
DN
14509 gfc_check_interfaces (ns);
14510
6de9cd9a
DN
14511 gfc_traverse_ns (ns, resolve_values);
14512
d05d9ac7 14513 if (ns->save_all)
6de9cd9a
DN
14514 gfc_save_all (ns);
14515
14516 iter_stack = NULL;
14517 for (d = ns->data; d; d = d->next)
14518 resolve_data (d);
14519
14520 iter_stack = NULL;
14521 gfc_traverse_ns (ns, gfc_formalize_init_value);
14522
a8b3b0b6
CR
14523 gfc_traverse_ns (ns, gfc_verify_binding_labels);
14524
6de9cd9a
DN
14525 for (eq = ns->equiv; eq; eq = eq->next)
14526 resolve_equivalence (eq);
14527
6de9cd9a 14528 /* Warn about unused labels. */
2e5758e8 14529 if (warn_unused_label)
994c1cc0 14530 warn_unused_fortran_label (ns->st_labels);
0e3e65bc
PT
14531
14532 gfc_resolve_uops (ns->uop_root);
a82f1f2e
DK
14533
14534 gfc_current_ns = old_ns;
efb0828d
L
14535}
14536
14537
14538/* Call resolve_code recursively. */
14539
14540static void
edf1eac2 14541resolve_codes (gfc_namespace *ns)
efb0828d
L
14542{
14543 gfc_namespace *n;
71a7778c 14544 bitmap_obstack old_obstack;
efb0828d 14545
611c64f0
JW
14546 if (ns->resolved == 1)
14547 return;
14548
efb0828d
L
14549 for (n = ns->contained; n; n = n->sibling)
14550 resolve_codes (n);
14551
14552 gfc_current_ns = ns;
76d02e9f
JW
14553
14554 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
14555 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
14556 cs_base = NULL;
14557
0e9a445b
PT
14558 /* Set to an out of range value. */
14559 current_entry_id = -1;
0615f923 14560
71a7778c 14561 old_obstack = labels_obstack;
0615f923 14562 bitmap_obstack_initialize (&labels_obstack);
71a7778c 14563
efb0828d 14564 resolve_code (ns->code, ns);
71a7778c 14565
0615f923 14566 bitmap_obstack_release (&labels_obstack);
71a7778c 14567 labels_obstack = old_obstack;
efb0828d
L
14568}
14569
14570
14571/* This function is called after a complete program unit has been compiled.
14572 Its purpose is to examine all of the expressions associated with a program
14573 unit, assign types to all intermediate expressions, make sure that all
14574 assignments are to compatible types and figure out which names refer to
14575 which functions or subroutines. */
14576
14577void
edf1eac2 14578gfc_resolve (gfc_namespace *ns)
efb0828d
L
14579{
14580 gfc_namespace *old_ns;
3af8d8cb 14581 code_stack *old_cs_base;
efb0828d 14582
71a7778c
PT
14583 if (ns->resolved)
14584 return;
14585
3af8d8cb 14586 ns->resolved = -1;
efb0828d 14587 old_ns = gfc_current_ns;
3af8d8cb 14588 old_cs_base = cs_base;
efb0828d
L
14589
14590 resolve_types (ns);
4d382327 14591 component_assignment_level = 0;
efb0828d 14592 resolve_codes (ns);
6de9cd9a
DN
14593
14594 gfc_current_ns = old_ns;
3af8d8cb 14595 cs_base = old_cs_base;
71a7778c 14596 ns->resolved = 1;
601d98be
TK
14597
14598 gfc_run_passes (ns);
6de9cd9a 14599}