]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/resolve.c
Update copyright years.
[thirdparty/gcc.git] / gcc / fortran / resolve.c
CommitLineData
df2fba9e 1/* Perform type resolution on the various structures.
818ab71a 2 Copyright (C) 2001-2016 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"
1916bcb5 24#include "options.h"
0615f923 25#include "bitmap.h"
2adfab87 26#include "gfortran.h"
6de9cd9a 27#include "arith.h" /* For gfc_compare_expr(). */
1524f80b 28#include "dependency.h"
ca39e6f2 29#include "data.h"
00a4618b 30#include "target-memory.h" /* for gfc_simplify_transfer */
b7e75771 31#include "constructor.h"
d22e4895 32
e8ec07e1
PT
33/* Types used in equivalence statements. */
34
a79683d5 35enum seq_type
e8ec07e1
PT
36{
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
a79683d5 38};
6de9cd9a 39
0615f923 40/* Stack to keep track of the nesting of blocks as we move through the
b46ebd6c 41 code. See resolve_branch() and gfc_resolve_code(). */
6de9cd9a
DN
42
43typedef struct code_stack
44{
d80c695f 45 struct gfc_code *head, *current;
6de9cd9a 46 struct code_stack *prev;
0615f923
TS
47
48 /* This bitmap keeps track of the targets valid for a branch from
d80c695f
TS
49 inside this block except for END {IF|SELECT}s of enclosing
50 blocks. */
0615f923 51 bitmap reachable_labels;
6de9cd9a
DN
52}
53code_stack;
54
55static code_stack *cs_base = NULL;
56
57
8c6a85e3 58/* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
6de9cd9a
DN
59
60static int forall_flag;
ce96d372 61int gfc_do_concurrent_flag;
6de9cd9a 62
c62c6622
TB
63/* True when we are resolving an expression that is an actual argument to
64 a procedure. */
65static bool actual_arg = false;
66/* True when we are resolving an expression that is the first actual argument
67 to a procedure. */
68static bool first_actual_arg = false;
69
45a69325 70
6c7a4dfd
JJ
71/* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
72
73static int omp_workshare_flag;
74
4213f93b
PT
75/* Nonzero if we are processing a formal arglist. The corresponding function
76 resets the flag each time that it is read. */
77static int formal_arg_flag = 0;
78
0e9a445b 79/* True if we are resolving a specification expression. */
fd061185 80static bool specification_expr = false;
0e9a445b
PT
81
82/* The id of the last entry seen. */
83static int current_entry_id;
84
0615f923
TS
85/* We use bitmaps to determine if a branch target is valid. */
86static bitmap_obstack labels_obstack;
87
d3a9eea2
TB
88/* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
89static bool inquiry_argument = false;
90
c62c6622 91
4213f93b
PT
92int
93gfc_is_formal_arg (void)
94{
95 return formal_arg_flag;
96}
97
c867b7b6
PT
98/* Is the symbol host associated? */
99static bool
100is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
101{
102 for (ns = ns->parent; ns; ns = ns->parent)
4d382327 103 {
c867b7b6
PT
104 if (sym->ns == ns)
105 return true;
106 }
107
108 return false;
109}
52f49934
DK
110
111/* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
112 an ABSTRACT derived-type. If where is not NULL, an error message with that
113 locus is printed, optionally using name. */
114
524af0d6 115static bool
52f49934
DK
116resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
117{
bc21d315 118 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
52f49934
DK
119 {
120 if (where)
121 {
122 if (name)
a4d9b221 123 gfc_error ("%qs at %L is of the ABSTRACT type %qs",
bc21d315 124 name, where, ts->u.derived->name);
52f49934 125 else
a4d9b221 126 gfc_error ("ABSTRACT type %qs used at %L",
bc21d315 127 ts->u.derived->name, where);
52f49934
DK
128 }
129
524af0d6 130 return false;
52f49934
DK
131 }
132
524af0d6 133 return true;
52f49934
DK
134}
135
136
524af0d6 137static bool
b6a45605 138check_proc_interface (gfc_symbol *ifc, locus *where)
2fcac97d 139{
0e8d854e 140 /* Several checks for F08:C1216. */
0e8d854e 141 if (ifc->attr.procedure)
2fcac97d 142 {
a4d9b221 143 gfc_error ("Interface %qs at %L is declared "
b6a45605 144 "in a later PROCEDURE statement", ifc->name, where);
524af0d6 145 return false;
2fcac97d 146 }
0e8d854e
JW
147 if (ifc->generic)
148 {
149 /* For generic interfaces, check if there is
150 a specific procedure with the same name. */
151 gfc_interface *gen = ifc->generic;
152 while (gen && strcmp (gen->sym->name, ifc->name) != 0)
153 gen = gen->next;
154 if (!gen)
155 {
a4d9b221 156 gfc_error ("Interface %qs at %L may not be generic",
b6a45605 157 ifc->name, where);
524af0d6 158 return false;
0e8d854e
JW
159 }
160 }
161 if (ifc->attr.proc == PROC_ST_FUNCTION)
162 {
a4d9b221 163 gfc_error ("Interface %qs at %L may not be a statement function",
b6a45605 164 ifc->name, where);
524af0d6 165 return false;
0e8d854e
JW
166 }
167 if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
168 || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
169 ifc->attr.intrinsic = 1;
170 if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
171 {
a4d9b221 172 gfc_error ("Intrinsic procedure %qs not allowed in "
b6a45605 173 "PROCEDURE statement at %L", ifc->name, where);
524af0d6 174 return false;
b6a45605
JW
175 }
176 if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
177 {
a4d9b221 178 gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
524af0d6 179 return false;
0e8d854e 180 }
524af0d6 181 return true;
b6a45605
JW
182}
183
184
185static void resolve_symbol (gfc_symbol *sym);
186
187
188/* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
189
524af0d6 190static bool
b6a45605
JW
191resolve_procedure_interface (gfc_symbol *sym)
192{
193 gfc_symbol *ifc = sym->ts.interface;
194
195 if (!ifc)
524af0d6 196 return true;
b6a45605
JW
197
198 if (ifc == sym)
199 {
a4d9b221 200 gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
b6a45605 201 sym->name, &sym->declared_at);
524af0d6 202 return false;
b6a45605 203 }
524af0d6
JB
204 if (!check_proc_interface (ifc, &sym->declared_at))
205 return false;
2fcac97d 206
0e8d854e 207 if (ifc->attr.if_source || ifc->attr.intrinsic)
2fcac97d 208 {
b6a45605 209 /* Resolve interface and copy attributes. */
2fcac97d 210 resolve_symbol (ifc);
2fcac97d 211 if (ifc->attr.intrinsic)
2dda89a8 212 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
2fcac97d
JW
213
214 if (ifc->result)
c79bb355
JW
215 {
216 sym->ts = ifc->result->ts;
217 sym->result = sym;
218 }
4d382327 219 else
2fcac97d
JW
220 sym->ts = ifc->ts;
221 sym->ts.interface = ifc;
222 sym->attr.function = ifc->attr.function;
223 sym->attr.subroutine = ifc->attr.subroutine;
2fcac97d
JW
224
225 sym->attr.allocatable = ifc->attr.allocatable;
226 sym->attr.pointer = ifc->attr.pointer;
227 sym->attr.pure = ifc->attr.pure;
228 sym->attr.elemental = ifc->attr.elemental;
229 sym->attr.dimension = ifc->attr.dimension;
230 sym->attr.contiguous = ifc->attr.contiguous;
231 sym->attr.recursive = ifc->attr.recursive;
232 sym->attr.always_explicit = ifc->attr.always_explicit;
233 sym->attr.ext_attr |= ifc->attr.ext_attr;
8be3d7da 234 sym->attr.is_bind_c = ifc->attr.is_bind_c;
0b2d443b 235 sym->attr.class_ok = ifc->attr.class_ok;
2fcac97d
JW
236 /* Copy array spec. */
237 sym->as = gfc_copy_array_spec (ifc->as);
2fcac97d
JW
238 /* Copy char length. */
239 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
240 {
241 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
2fcac97d 242 if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
524af0d6
JB
243 && !gfc_resolve_expr (sym->ts.u.cl->length))
244 return false;
2fcac97d
JW
245 }
246 }
2fcac97d 247
524af0d6 248 return true;
2fcac97d
JW
249}
250
251
6de9cd9a
DN
252/* Resolve types of formal argument lists. These have to be done early so that
253 the formal argument lists of module procedures can be copied to the
254 containing module before the individual procedures are resolved
255 individually. We also resolve argument lists of procedures in interface
256 blocks because they are self-contained scoping units.
257
258 Since a dummy argument cannot be a non-dummy procedure, the only
259 resort left for untyped names are the IMPLICIT types. */
260
261static void
edf1eac2 262resolve_formal_arglist (gfc_symbol *proc)
6de9cd9a
DN
263{
264 gfc_formal_arglist *f;
265 gfc_symbol *sym;
fd061185 266 bool saved_specification_expr;
6de9cd9a
DN
267 int i;
268
6de9cd9a
DN
269 if (proc->result != NULL)
270 sym = proc->result;
271 else
272 sym = proc;
273
274 if (gfc_elemental (proc)
275 || sym->attr.pointer || sym->attr.allocatable
c62c6622 276 || (sym->as && sym->as->rank != 0))
43e7fd21
FXC
277 {
278 proc->attr.always_explicit = 1;
279 sym->attr.always_explicit = 1;
280 }
6de9cd9a 281
4213f93b
PT
282 formal_arg_flag = 1;
283
6de9cd9a
DN
284 for (f = proc->formal; f; f = f->next)
285 {
3d333a28 286 gfc_array_spec *as;
6de9cd9a 287
6220bf43
TB
288 sym = f->sym;
289
6de9cd9a
DN
290 if (sym == NULL)
291 {
edf1eac2 292 /* Alternate return placeholder. */
6de9cd9a
DN
293 if (gfc_elemental (proc))
294 gfc_error ("Alternate return specifier in elemental subroutine "
a4d9b221 295 "%qs at %L is not allowed", proc->name,
6de9cd9a 296 &proc->declared_at);
edf1eac2
SK
297 if (proc->attr.function)
298 gfc_error ("Alternate return specifier in function "
a4d9b221 299 "%qs at %L is not allowed", proc->name,
edf1eac2 300 &proc->declared_at);
6de9cd9a
DN
301 continue;
302 }
0e8d854e 303 else if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
524af0d6 304 && !resolve_procedure_interface (sym))
0e8d854e 305 return;
6de9cd9a 306
9281625b
BD
307 if (strcmp (proc->name, sym->name) == 0)
308 {
309 gfc_error ("Self-referential argument "
a4d9b221 310 "%qs at %L is not allowed", sym->name,
9281625b
BD
311 &proc->declared_at);
312 return;
313 }
314
6de9cd9a
DN
315 if (sym->attr.if_source != IFSRC_UNKNOWN)
316 resolve_formal_arglist (sym);
317
fe445bf7 318 if (sym->attr.subroutine || sym->attr.external)
4056cc1b 319 {
fe445bf7
JW
320 if (sym->attr.flavor == FL_UNKNOWN)
321 gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
4056cc1b 322 }
fe445bf7 323 else
6de9cd9a 324 {
fe445bf7
JW
325 if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
326 && (!sym->attr.function || sym->result == sym))
327 gfc_set_default_type (sym, 1, sym->ns);
6de9cd9a
DN
328 }
329
3d333a28
TB
330 as = sym->ts.type == BT_CLASS && sym->attr.class_ok
331 ? CLASS_DATA (sym)->as : sym->as;
332
fd061185
TB
333 saved_specification_expr = specification_expr;
334 specification_expr = true;
3d333a28 335 gfc_resolve_array_spec (as, 0);
fd061185 336 specification_expr = saved_specification_expr;
6de9cd9a
DN
337
338 /* We can't tell if an array with dimension (:) is assumed or deferred
edf1eac2 339 shape until we know if it has the pointer or allocatable attributes.
6de9cd9a 340 */
3d333a28
TB
341 if (as && as->rank > 0 && as->type == AS_DEFERRED
342 && ((sym->ts.type != BT_CLASS
343 && !(sym->attr.pointer || sym->attr.allocatable))
344 || (sym->ts.type == BT_CLASS
345 && !(CLASS_DATA (sym)->attr.class_pointer
346 || CLASS_DATA (sym)->attr.allocatable)))
12578be7 347 && sym->attr.flavor != FL_PROCEDURE)
edf1eac2 348 {
3d333a28
TB
349 as->type = AS_ASSUMED_SHAPE;
350 for (i = 0; i < as->rank; i++)
351 as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
edf1eac2 352 }
6de9cd9a 353
3d333a28 354 if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
c62c6622 355 || (as && as->type == AS_ASSUMED_RANK)
edf1eac2 356 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
3d333a28
TB
357 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
358 && (CLASS_DATA (sym)->attr.class_pointer
359 || CLASS_DATA (sym)->attr.allocatable
360 || CLASS_DATA (sym)->attr.target))
edf1eac2 361 || sym->attr.optional)
43e7fd21
FXC
362 {
363 proc->attr.always_explicit = 1;
364 if (proc->result)
365 proc->result->attr.always_explicit = 1;
366 }
6de9cd9a
DN
367
368 /* If the flavor is unknown at this point, it has to be a variable.
edf1eac2 369 A procedure specification would have already set the type. */
6de9cd9a
DN
370
371 if (sym->attr.flavor == FL_UNKNOWN)
231b2fcc 372 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
6de9cd9a 373
fe445bf7 374 if (gfc_pure (proc))
6de9cd9a 375 {
fe445bf7 376 if (sym->attr.flavor == FL_PROCEDURE)
a26e8df4 377 {
fe445bf7
JW
378 /* F08:C1279. */
379 if (!gfc_pure (sym))
380 {
a4d9b221 381 gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
fe445bf7
JW
382 "also be PURE", sym->name, &sym->declared_at);
383 continue;
384 }
a26e8df4 385 }
fe445bf7 386 else if (!sym->attr.pointer)
a26e8df4 387 {
fe445bf7
JW
388 if (proc->attr.function && sym->attr.intent != INTENT_IN)
389 {
390 if (sym->attr.value)
a4d9b221
TB
391 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
392 " of pure function %qs at %L with VALUE "
fe445bf7
JW
393 "attribute but without INTENT(IN)",
394 sym->name, proc->name, &sym->declared_at);
395 else
a4d9b221 396 gfc_error ("Argument %qs of pure function %qs at %L must "
fe445bf7
JW
397 "be INTENT(IN) or VALUE", sym->name, proc->name,
398 &sym->declared_at);
399 }
400
401 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
402 {
403 if (sym->attr.value)
a4d9b221
TB
404 gfc_notify_std (GFC_STD_F2008, "Argument %qs"
405 " of pure subroutine %qs at %L with VALUE "
fe445bf7
JW
406 "attribute but without INTENT", sym->name,
407 proc->name, &sym->declared_at);
408 else
a4d9b221 409 gfc_error ("Argument %qs of pure subroutine %qs at %L "
fe445bf7
JW
410 "must have its INTENT specified or have the "
411 "VALUE attribute", sym->name, proc->name,
412 &sym->declared_at);
413 }
a26e8df4 414 }
c19a0033
JW
415
416 /* F08:C1278a. */
417 if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
418 {
fea70c99 419 gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
c19a0033
JW
420 " may not be polymorphic", sym->name, proc->name,
421 &sym->declared_at);
422 continue;
423 }
6de9cd9a
DN
424 }
425
fe445bf7 426 if (proc->attr.implicit_pure)
f1f39033 427 {
fe445bf7
JW
428 if (sym->attr.flavor == FL_PROCEDURE)
429 {
524af0d6 430 if (!gfc_pure (sym))
fe445bf7
JW
431 proc->attr.implicit_pure = 0;
432 }
433 else if (!sym->attr.pointer)
434 {
c915f8bc
TB
435 if (proc->attr.function && sym->attr.intent != INTENT_IN
436 && !sym->value)
fe445bf7 437 proc->attr.implicit_pure = 0;
f1f39033 438
c915f8bc
TB
439 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
440 && !sym->value)
fe445bf7
JW
441 proc->attr.implicit_pure = 0;
442 }
f1f39033
PT
443 }
444
6de9cd9a
DN
445 if (gfc_elemental (proc))
446 {
4056cc1b 447 /* F08:C1289. */
9775a921
TB
448 if (sym->attr.codimension
449 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
450 && CLASS_DATA (sym)->attr.codimension))
be59db2d 451 {
a4d9b221 452 gfc_error ("Coarray dummy argument %qs at %L to elemental "
be59db2d
TB
453 "procedure", sym->name, &sym->declared_at);
454 continue;
455 }
456
9775a921
TB
457 if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
458 && CLASS_DATA (sym)->as))
6de9cd9a 459 {
a4d9b221 460 gfc_error ("Argument %qs of elemental procedure at %L must "
edf1eac2 461 "be scalar", sym->name, &sym->declared_at);
6de9cd9a
DN
462 continue;
463 }
464
9775a921
TB
465 if (sym->attr.allocatable
466 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
467 && CLASS_DATA (sym)->attr.allocatable))
e6c14898 468 {
a4d9b221 469 gfc_error ("Argument %qs of elemental procedure at %L cannot "
e6c14898
DK
470 "have the ALLOCATABLE attribute", sym->name,
471 &sym->declared_at);
472 continue;
473 }
474
c696c6f3
TB
475 if (sym->attr.pointer
476 || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
477 && CLASS_DATA (sym)->attr.class_pointer))
6de9cd9a 478 {
c4100eae 479 gfc_error ("Argument %qs of elemental procedure at %L cannot "
edf1eac2
SK
480 "have the POINTER attribute", sym->name,
481 &sym->declared_at);
6de9cd9a
DN
482 continue;
483 }
242633d6
TB
484
485 if (sym->attr.flavor == FL_PROCEDURE)
486 {
c4100eae
MLI
487 gfc_error ("Dummy procedure %qs not allowed in elemental "
488 "procedure %qs at %L", sym->name, proc->name,
242633d6
TB
489 &sym->declared_at);
490 continue;
491 }
e6c14898 492
25ffd46f
TB
493 /* Fortran 2008 Corrigendum 1, C1290a. */
494 if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
e6c14898 495 {
c4100eae 496 gfc_error ("Argument %qs of elemental procedure %qs at %L must "
25ffd46f
TB
497 "have its INTENT specified or have the VALUE "
498 "attribute", sym->name, proc->name,
e6c14898
DK
499 &sym->declared_at);
500 continue;
501 }
6de9cd9a
DN
502 }
503
504 /* Each dummy shall be specified to be scalar. */
505 if (proc->attr.proc == PROC_ST_FUNCTION)
edf1eac2
SK
506 {
507 if (sym->as != NULL)
508 {
c4100eae 509 gfc_error ("Argument %qs of statement function at %L must "
edf1eac2
SK
510 "be scalar", sym->name, &sym->declared_at);
511 continue;
512 }
513
514 if (sym->ts.type == BT_CHARACTER)
515 {
bc21d315 516 gfc_charlen *cl = sym->ts.u.cl;
edf1eac2
SK
517 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
518 {
c4100eae 519 gfc_error ("Character-valued argument %qs of statement "
edf1eac2
SK
520 "function at %L must have constant length",
521 sym->name, &sym->declared_at);
522 continue;
523 }
524 }
525 }
6de9cd9a 526 }
4213f93b 527 formal_arg_flag = 0;
6de9cd9a
DN
528}
529
530
531/* Work function called when searching for symbols that have argument lists
532 associated with them. */
533
534static void
edf1eac2 535find_arglists (gfc_symbol *sym)
6de9cd9a 536{
c3f34952 537 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
cadddfdd 538 || sym->attr.flavor == FL_DERIVED || sym->attr.intrinsic)
6de9cd9a
DN
539 return;
540
541 resolve_formal_arglist (sym);
542}
543
544
545/* Given a namespace, resolve all formal argument lists within the namespace.
546 */
547
548static void
edf1eac2 549resolve_formal_arglists (gfc_namespace *ns)
6de9cd9a 550{
6de9cd9a
DN
551 if (ns == NULL)
552 return;
553
554 gfc_traverse_ns (ns, find_arglists);
555}
556
557
3d79abbd 558static void
edf1eac2 559resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
3d79abbd 560{
524af0d6 561 bool t;
05c1e3a7 562
b5bf3e4d
TB
563 /* If this namespace is not a function or an entry master function,
564 ignore it. */
565 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
566 || sym->attr.entry_master)
3d79abbd
PB
567 return;
568
0dd973dd 569 /* Try to find out of what the return type is. */
f9909823 570 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
3d79abbd 571 {
c2de0c19 572 t = gfc_set_default_type (sym->result, 0, ns);
3d79abbd 573
524af0d6 574 if (!t && !sym->result->attr.untyped)
cf4d246b 575 {
c2de0c19 576 if (sym->result == sym)
c4100eae 577 gfc_error ("Contained function %qs at %L has no IMPLICIT type",
c2de0c19 578 sym->name, &sym->declared_at);
3070bab4 579 else if (!sym->result->attr.proc_pointer)
c4100eae 580 gfc_error ("Result %qs of contained function %qs at %L has "
c2de0c19
TB
581 "no IMPLICIT type", sym->result->name, sym->name,
582 &sym->result->declared_at);
583 sym->result->attr.untyped = 1;
cf4d246b 584 }
3d79abbd 585 }
b95605fb 586
4d382327 587 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
edf1eac2
SK
588 type, lists the only ways a character length value of * can be used:
589 dummy arguments of procedures, named constants, and function results
6c19d9b5
DK
590 in external functions. Internal function results and results of module
591 procedures are not on this list, ergo, not permitted. */
b95605fb 592
c2de0c19 593 if (sym->result->ts.type == BT_CHARACTER)
b95605fb 594 {
bc21d315 595 gfc_charlen *cl = sym->result->ts.u.cl;
8d51f26f 596 if ((!cl || !cl->length) && !sym->result->ts.deferred)
6c19d9b5
DK
597 {
598 /* See if this is a module-procedure and adapt error message
599 accordingly. */
600 bool module_proc;
601 gcc_assert (ns->parent && ns->parent->proc_name);
602 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
603
c4100eae 604 gfc_error ("Character-valued %s %qs at %L must not be"
6c19d9b5
DK
605 " assumed length",
606 module_proc ? _("module procedure")
607 : _("internal function"),
608 sym->name, &sym->declared_at);
609 }
b95605fb 610 }
3d79abbd
PB
611}
612
613
614/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
f7b529fa 615 introduce duplicates. */
3d79abbd
PB
616
617static void
618merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
619{
620 gfc_formal_arglist *f, *new_arglist;
621 gfc_symbol *new_sym;
622
623 for (; new_args != NULL; new_args = new_args->next)
624 {
625 new_sym = new_args->sym;
05c1e3a7 626 /* See if this arg is already in the formal argument list. */
3d79abbd
PB
627 for (f = proc->formal; f; f = f->next)
628 {
629 if (new_sym == f->sym)
630 break;
631 }
632
633 if (f)
634 continue;
635
636 /* Add a new argument. Argument order is not important. */
637 new_arglist = gfc_get_formal_arglist ();
638 new_arglist->sym = new_sym;
639 new_arglist->next = proc->formal;
640 proc->formal = new_arglist;
641 }
642}
643
644
54129a64
PT
645/* Flag the arguments that are not present in all entries. */
646
647static void
648check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
649{
650 gfc_formal_arglist *f, *head;
651 head = new_args;
652
653 for (f = proc->formal; f; f = f->next)
654 {
655 if (f->sym == NULL)
656 continue;
657
658 for (new_args = head; new_args; new_args = new_args->next)
659 {
660 if (new_args->sym == f->sym)
661 break;
662 }
663
664 if (new_args)
665 continue;
666
667 f->sym->attr.not_always_present = 1;
668 }
669}
670
671
3d79abbd
PB
672/* Resolve alternate entry points. If a symbol has multiple entry points we
673 create a new master symbol for the main routine, and turn the existing
674 symbol into an entry point. */
675
676static void
edf1eac2 677resolve_entries (gfc_namespace *ns)
3d79abbd
PB
678{
679 gfc_namespace *old_ns;
680 gfc_code *c;
681 gfc_symbol *proc;
682 gfc_entry_list *el;
683 char name[GFC_MAX_SYMBOL_LEN + 1];
684 static int master_count = 0;
685
686 if (ns->proc_name == NULL)
687 return;
688
689 /* No need to do anything if this procedure doesn't have alternate entry
690 points. */
691 if (!ns->entries)
692 return;
693
694 /* We may already have resolved alternate entry points. */
695 if (ns->proc_name->attr.entry_master)
696 return;
697
f7b529fa 698 /* If this isn't a procedure something has gone horribly wrong. */
6e45f57b 699 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
05c1e3a7 700
3d79abbd
PB
701 /* Remember the current namespace. */
702 old_ns = gfc_current_ns;
703
704 gfc_current_ns = ns;
705
706 /* Add the main entry point to the list of entry points. */
707 el = gfc_get_entry_list ();
708 el->sym = ns->proc_name;
709 el->id = 0;
710 el->next = ns->entries;
711 ns->entries = el;
712 ns->proc_name->attr.entry = 1;
713
1a492601
PT
714 /* If it is a module function, it needs to be in the right namespace
715 so that gfc_get_fake_result_decl can gather up the results. The
716 need for this arose in get_proc_name, where these beasts were
717 left in their own namespace, to keep prior references linked to
718 the entry declaration.*/
719 if (ns->proc_name->attr.function
edf1eac2 720 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
1a492601
PT
721 el->sym->ns = ns;
722
08ee9e85
PT
723 /* Do the same for entries where the master is not a module
724 procedure. These are retained in the module namespace because
725 of the module procedure declaration. */
726 for (el = el->next; el; el = el->next)
727 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
728 && el->sym->attr.mod_proc)
729 el->sym->ns = ns;
730 el = ns->entries;
731
3d79abbd 732 /* Add an entry statement for it. */
11e5274a 733 c = gfc_get_code (EXEC_ENTRY);
3d79abbd
PB
734 c->ext.entry = el;
735 c->next = ns->code;
736 ns->code = c;
737
738 /* Create a new symbol for the master function. */
739 /* Give the internal function a unique name (within this file).
7be7d41b
TS
740 Also include the function name so the user has some hope of figuring
741 out what is going on. */
3d79abbd
PB
742 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
743 master_count++, ns->proc_name->name);
3d79abbd 744 gfc_get_ha_symbol (name, &proc);
6e45f57b 745 gcc_assert (proc != NULL);
3d79abbd 746
231b2fcc 747 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
3d79abbd 748 if (ns->proc_name->attr.subroutine)
231b2fcc 749 gfc_add_subroutine (&proc->attr, proc->name, NULL);
3d79abbd
PB
750 else
751 {
d198b59a
JJ
752 gfc_symbol *sym;
753 gfc_typespec *ts, *fts;
5be38273 754 gfc_array_spec *as, *fas;
231b2fcc 755 gfc_add_function (&proc->attr, proc->name, NULL);
d198b59a 756 proc->result = proc;
5be38273
PT
757 fas = ns->entries->sym->as;
758 fas = fas ? fas : ns->entries->sym->result->as;
d198b59a
JJ
759 fts = &ns->entries->sym->result->ts;
760 if (fts->type == BT_UNKNOWN)
713485cc 761 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
d198b59a
JJ
762 for (el = ns->entries->next; el; el = el->next)
763 {
764 ts = &el->sym->result->ts;
5be38273
PT
765 as = el->sym->as;
766 as = as ? as : el->sym->result->as;
d198b59a 767 if (ts->type == BT_UNKNOWN)
713485cc 768 ts = gfc_get_default_type (el->sym->result->name, NULL);
5be38273 769
d198b59a
JJ
770 if (! gfc_compare_types (ts, fts)
771 || (el->sym->result->attr.dimension
772 != ns->entries->sym->result->attr.dimension)
773 || (el->sym->result->attr.pointer
774 != ns->entries->sym->result->attr.pointer))
775 break;
f5d67ede
PT
776 else if (as && fas && ns->entries->sym->result != el->sym->result
777 && gfc_compare_array_spec (as, fas) == 0)
107d5ff6 778 gfc_error ("Function %s at %L has entries with mismatched "
5be38273
PT
779 "array specifications", ns->entries->sym->name,
780 &ns->entries->sym->declared_at);
107d5ff6
TB
781 /* The characteristics need to match and thus both need to have
782 the same string length, i.e. both len=*, or both len=4.
783 Having both len=<variable> is also possible, but difficult to
784 check at compile time. */
bc21d315
JW
785 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
786 && (((ts->u.cl->length && !fts->u.cl->length)
787 ||(!ts->u.cl->length && fts->u.cl->length))
788 || (ts->u.cl->length
789 && ts->u.cl->length->expr_type
790 != fts->u.cl->length->expr_type)
791 || (ts->u.cl->length
792 && ts->u.cl->length->expr_type == EXPR_CONSTANT
793 && mpz_cmp (ts->u.cl->length->value.integer,
794 fts->u.cl->length->value.integer) != 0)))
9717f7a1 795 gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
107d5ff6
TB
796 "entries returning variables of different "
797 "string lengths", ns->entries->sym->name,
798 &ns->entries->sym->declared_at);
d198b59a
JJ
799 }
800
801 if (el == NULL)
802 {
803 sym = ns->entries->sym->result;
804 /* All result types the same. */
805 proc->ts = *fts;
806 if (sym->attr.dimension)
807 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
808 if (sym->attr.pointer)
809 gfc_add_pointer (&proc->attr, NULL);
810 }
811 else
812 {
49de9e73 813 /* Otherwise the result will be passed through a union by
d198b59a
JJ
814 reference. */
815 proc->attr.mixed_entry_master = 1;
816 for (el = ns->entries; el; el = el->next)
817 {
818 sym = el->sym->result;
819 if (sym->attr.dimension)
edf1eac2
SK
820 {
821 if (el == ns->entries)
822 gfc_error ("FUNCTION result %s can't be an array in "
823 "FUNCTION %s at %L", sym->name,
824 ns->entries->sym->name, &sym->declared_at);
825 else
826 gfc_error ("ENTRY result %s can't be an array in "
827 "FUNCTION %s at %L", sym->name,
828 ns->entries->sym->name, &sym->declared_at);
829 }
d198b59a 830 else if (sym->attr.pointer)
edf1eac2
SK
831 {
832 if (el == ns->entries)
833 gfc_error ("FUNCTION result %s can't be a POINTER in "
834 "FUNCTION %s at %L", sym->name,
835 ns->entries->sym->name, &sym->declared_at);
836 else
837 gfc_error ("ENTRY result %s can't be a POINTER in "
838 "FUNCTION %s at %L", sym->name,
839 ns->entries->sym->name, &sym->declared_at);
840 }
d198b59a
JJ
841 else
842 {
843 ts = &sym->ts;
844 if (ts->type == BT_UNKNOWN)
713485cc 845 ts = gfc_get_default_type (sym->name, NULL);
d198b59a
JJ
846 switch (ts->type)
847 {
848 case BT_INTEGER:
849 if (ts->kind == gfc_default_integer_kind)
850 sym = NULL;
851 break;
852 case BT_REAL:
853 if (ts->kind == gfc_default_real_kind
854 || ts->kind == gfc_default_double_kind)
855 sym = NULL;
856 break;
857 case BT_COMPLEX:
858 if (ts->kind == gfc_default_complex_kind)
859 sym = NULL;
860 break;
861 case BT_LOGICAL:
862 if (ts->kind == gfc_default_logical_kind)
863 sym = NULL;
864 break;
cf4d246b
JJ
865 case BT_UNKNOWN:
866 /* We will issue error elsewhere. */
867 sym = NULL;
868 break;
d198b59a
JJ
869 default:
870 break;
871 }
872 if (sym)
edf1eac2
SK
873 {
874 if (el == ns->entries)
875 gfc_error ("FUNCTION result %s can't be of type %s "
876 "in FUNCTION %s at %L", sym->name,
877 gfc_typename (ts), ns->entries->sym->name,
878 &sym->declared_at);
879 else
880 gfc_error ("ENTRY result %s can't be of type %s "
881 "in FUNCTION %s at %L", sym->name,
882 gfc_typename (ts), ns->entries->sym->name,
883 &sym->declared_at);
884 }
d198b59a
JJ
885 }
886 }
887 }
3d79abbd
PB
888 }
889 proc->attr.access = ACCESS_PRIVATE;
890 proc->attr.entry_master = 1;
891
892 /* Merge all the entry point arguments. */
893 for (el = ns->entries; el; el = el->next)
894 merge_argument_lists (proc, el->sym->formal);
895
54129a64
PT
896 /* Check the master formal arguments for any that are not
897 present in all entry points. */
898 for (el = ns->entries; el; el = el->next)
899 check_argument_lists (proc, el->sym->formal);
900
7be7d41b 901 /* Use the master function for the function body. */
3d79abbd
PB
902 ns->proc_name = proc;
903
7be7d41b 904 /* Finalize the new symbols. */
3d79abbd
PB
905 gfc_commit_symbols ();
906
907 /* Restore the original namespace. */
908 gfc_current_ns = old_ns;
909}
910
911
346ecba8 912/* Resolve common variables. */
ad22b1ff 913static void
6dcab507 914resolve_common_vars (gfc_common_head *common_block, bool named_common)
ad22b1ff 915{
6dcab507 916 gfc_symbol *csym = common_block->head;
ad22b1ff 917
346ecba8 918 for (; csym; csym = csym->common_next)
041cf987 919 {
2b3f52a2
MM
920 /* gfc_add_in_common may have been called before, but the reported errors
921 have been ignored to continue parsing.
922 We do the checks again here. */
923 if (!csym->attr.use_assoc)
924 gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
925
346ecba8
TB
926 if (csym->value || csym->attr.data)
927 {
928 if (!csym->ns->is_block_data)
a4d9b221 929 gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
346ecba8
TB
930 "but only in BLOCK DATA initialization is "
931 "allowed", csym->name, &csym->declared_at);
932 else if (!named_common)
a4d9b221 933 gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
346ecba8
TB
934 "in a blank COMMON but initialization is only "
935 "allowed in named common blocks", csym->name,
936 &csym->declared_at);
937 }
938
8b704316 939 if (UNLIMITED_POLY (csym))
4daa149b 940 gfc_error_now ("%qs in cannot appear in COMMON at %L "
8b704316
PT
941 "[F2008:C5100]", csym->name, &csym->declared_at);
942
448d2cd2
TS
943 if (csym->ts.type != BT_DERIVED)
944 continue;
945
bc21d315
JW
946 if (!(csym->ts.u.derived->attr.sequence
947 || csym->ts.u.derived->attr.is_bind_c))
4daa149b 948 gfc_error_now ("Derived type variable %qs in COMMON at %L "
448d2cd2
TS
949 "has neither the SEQUENCE nor the BIND(C) "
950 "attribute", csym->name, &csym->declared_at);
bc21d315 951 if (csym->ts.u.derived->attr.alloc_comp)
4daa149b 952 gfc_error_now ("Derived type variable %qs in COMMON at %L "
448d2cd2
TS
953 "has an ultimate component that is "
954 "allocatable", csym->name, &csym->declared_at);
16e520b6 955 if (gfc_has_default_initializer (csym->ts.u.derived))
4daa149b 956 gfc_error_now ("Derived type variable %qs in COMMON at %L "
448d2cd2
TS
957 "may not have default initializer", csym->name,
958 &csym->declared_at);
6f9c9d6d
TB
959
960 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
961 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
041cf987 962 }
346ecba8
TB
963}
964
965/* Resolve common blocks. */
966static void
967resolve_common_blocks (gfc_symtree *common_root)
968{
969 gfc_symbol *sym;
878cdb7b 970 gfc_gsymbol * gsym;
346ecba8
TB
971
972 if (common_root == NULL)
973 return;
974
975 if (common_root->left)
976 resolve_common_blocks (common_root->left);
977 if (common_root->right)
978 resolve_common_blocks (common_root->right);
979
6dcab507 980 resolve_common_vars (common_root->n.common, true);
ad22b1ff 981
878cdb7b
TB
982 /* The common name is a global name - in Fortran 2003 also if it has a
983 C binding name, since Fortran 2008 only the C binding name is a global
984 identifier. */
985 if (!common_root->n.common->binding_label
986 || gfc_notification_std (GFC_STD_F2008))
987 {
988 gsym = gfc_find_gsymbol (gfc_gsym_root,
989 common_root->n.common->name);
990
991 if (gsym && gfc_notification_std (GFC_STD_F2008)
992 && gsym->type == GSYM_COMMON
993 && ((common_root->n.common->binding_label
994 && (!gsym->binding_label
995 || strcmp (common_root->n.common->binding_label,
996 gsym->binding_label) != 0))
997 || (!common_root->n.common->binding_label
998 && gsym->binding_label)))
999 {
fea70c99 1000 gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
878cdb7b
TB
1001 "identifier and must thus have the same binding name "
1002 "as the same-named COMMON block at %L: %s vs %s",
1003 common_root->n.common->name, &common_root->n.common->where,
1004 &gsym->where,
1005 common_root->n.common->binding_label
1006 ? common_root->n.common->binding_label : "(blank)",
1007 gsym->binding_label ? gsym->binding_label : "(blank)");
1008 return;
1009 }
1010
1011 if (gsym && gsym->type != GSYM_COMMON
1012 && !common_root->n.common->binding_label)
1013 {
fea70c99 1014 gfc_error ("COMMON block %qs at %L uses the same global identifier "
878cdb7b
TB
1015 "as entity at %L",
1016 common_root->n.common->name, &common_root->n.common->where,
1017 &gsym->where);
1018 return;
1019 }
1020 if (gsym && gsym->type != GSYM_COMMON)
1021 {
fea70c99 1022 gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
878cdb7b
TB
1023 "%L sharing the identifier with global non-COMMON-block "
1024 "entity at %L", common_root->n.common->name,
1025 &common_root->n.common->where, &gsym->where);
1026 return;
1027 }
1028 if (!gsym)
1029 {
1030 gsym = gfc_get_gsymbol (common_root->n.common->name);
1031 gsym->type = GSYM_COMMON;
1032 gsym->where = common_root->n.common->where;
1033 gsym->defined = 1;
1034 }
1035 gsym->used = 1;
1036 }
1037
1038 if (common_root->n.common->binding_label)
1039 {
1040 gsym = gfc_find_gsymbol (gfc_gsym_root,
1041 common_root->n.common->binding_label);
1042 if (gsym && gsym->type != GSYM_COMMON)
1043 {
fea70c99 1044 gfc_error ("COMMON block at %L with binding label %s uses the same "
878cdb7b
TB
1045 "global identifier as entity at %L",
1046 &common_root->n.common->where,
1047 common_root->n.common->binding_label, &gsym->where);
1048 return;
1049 }
1050 if (!gsym)
1051 {
1052 gsym = gfc_get_gsymbol (common_root->n.common->binding_label);
1053 gsym->type = GSYM_COMMON;
1054 gsym->where = common_root->n.common->where;
1055 gsym->defined = 1;
1056 }
1057 gsym->used = 1;
1058 }
1059
041cf987
TB
1060 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1061 if (sym == NULL)
1062 return;
1063
1064 if (sym->attr.flavor == FL_PARAMETER)
fea70c99 1065 gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
041cf987
TB
1066 sym->name, &common_root->n.common->where, &sym->declared_at);
1067
ef71fdd9 1068 if (sym->attr.external)
c4100eae 1069 gfc_error ("COMMON block %qs at %L can not have the EXTERNAL attribute",
ef71fdd9
JW
1070 sym->name, &common_root->n.common->where);
1071
041cf987 1072 if (sym->attr.intrinsic)
c4100eae 1073 gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
041cf987
TB
1074 sym->name, &common_root->n.common->where);
1075 else if (sym->attr.result
2d71b918 1076 || gfc_is_function_return_value (sym, gfc_current_ns))
a4d9b221 1077 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
041cf987
TB
1078 "that is also a function result", sym->name,
1079 &common_root->n.common->where);
1080 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1081 && sym->attr.proc != PROC_ST_FUNCTION)
a4d9b221 1082 gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
041cf987
TB
1083 "that is also a global procedure", sym->name,
1084 &common_root->n.common->where);
ad22b1ff
TB
1085}
1086
1087
6de9cd9a
DN
1088/* Resolve contained function types. Because contained functions can call one
1089 another, they have to be worked out before any of the contained procedures
1090 can be resolved.
1091
1092 The good news is that if a function doesn't already have a type, the only
1093 way it can get one is through an IMPLICIT type or a RESULT variable, because
1094 by definition contained functions are contained namespace they're contained
1095 in, not in a sibling or parent namespace. */
1096
1097static void
edf1eac2 1098resolve_contained_functions (gfc_namespace *ns)
6de9cd9a 1099{
6de9cd9a 1100 gfc_namespace *child;
3d79abbd 1101 gfc_entry_list *el;
6de9cd9a
DN
1102
1103 resolve_formal_arglists (ns);
1104
1105 for (child = ns->contained; child; child = child->sibling)
1106 {
3d79abbd 1107 /* Resolve alternate entry points first. */
05c1e3a7 1108 resolve_entries (child);
6de9cd9a 1109
3d79abbd
PB
1110 /* Then check function return types. */
1111 resolve_contained_fntype (child->proc_name, child);
1112 for (el = child->entries; el; el = el->next)
1113 resolve_contained_fntype (el->sym, child);
6de9cd9a
DN
1114 }
1115}
1116
1117
524af0d6 1118static bool resolve_fl_derived0 (gfc_symbol *sym);
0291fa25
JW
1119
1120
6de9cd9a 1121/* Resolve all of the elements of a structure constructor and make sure that
80f95228
JW
1122 the types are correct. The 'init' flag indicates that the given
1123 constructor is an initializer. */
6de9cd9a 1124
524af0d6 1125static bool
80f95228 1126resolve_structure_cons (gfc_expr *expr, int init)
6de9cd9a
DN
1127{
1128 gfc_constructor *cons;
1129 gfc_component *comp;
524af0d6 1130 bool t;
5046aff5 1131 symbol_attribute a;
6de9cd9a 1132
524af0d6 1133 t = true;
bd48f123
JW
1134
1135 if (expr->ts.type == BT_DERIVED)
0291fa25 1136 resolve_fl_derived0 (expr->ts.u.derived);
bd48f123 1137
b7e75771 1138 cons = gfc_constructor_first (expr->value.constructor);
6de9cd9a 1139
c3f34952
TB
1140 /* A constructor may have references if it is the result of substituting a
1141 parameter variable. In this case we just pull out the component we
1142 want. */
1143 if (expr->ref)
1144 comp = expr->ref->u.c.sym->components;
1145 else
1146 comp = expr->ts.u.derived->components;
1147
b7e75771 1148 for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
6de9cd9a 1149 {
0df50e7a
FXC
1150 int rank;
1151
edf1eac2 1152 if (!cons->expr)
404d8401 1153 continue;
6de9cd9a 1154
524af0d6 1155 if (!gfc_resolve_expr (cons->expr))
6de9cd9a 1156 {
524af0d6 1157 t = false;
6de9cd9a
DN
1158 continue;
1159 }
1160
0df50e7a 1161 rank = comp->as ? comp->as->rank : 0;
3cd52c11
PT
1162 if (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->as)
1163 rank = CLASS_DATA (comp)->as->rank;
1164
0df50e7a 1165 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
d4b7d0f0 1166 && (comp->attr.allocatable || cons->expr->rank))
5046aff5 1167 {
6a38e151 1168 gfc_error ("The rank of the element in the structure "
5046aff5
PT
1169 "constructor at %L does not match that of the "
1170 "component (%d/%d)", &cons->expr->where,
0df50e7a 1171 cons->expr->rank, rank);
524af0d6 1172 t = false;
5046aff5
PT
1173 }
1174
6de9cd9a
DN
1175 /* If we don't have the right type, try to convert it. */
1176
80f95228
JW
1177 if (!comp->attr.proc_pointer &&
1178 !gfc_compare_types (&cons->expr->ts, &comp->ts))
e0e85e06 1179 {
b04533af 1180 if (strcmp (comp->name, "_extends") == 0)
eece1eb9 1181 {
b04533af 1182 /* Can afford to be brutal with the _extends initializer.
eece1eb9
PT
1183 The derived type can get lost because it is PRIVATE
1184 but it is not usage constrained by the standard. */
1185 cons->expr->ts = comp->ts;
eece1eb9
PT
1186 }
1187 else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
e35e87dc
TB
1188 {
1189 gfc_error ("The element in the structure constructor at %L, "
c4100eae 1190 "for pointer component %qs, is %s but should be %s",
e35e87dc
TB
1191 &cons->expr->where, comp->name,
1192 gfc_basic_typename (cons->expr->ts.type),
1193 gfc_basic_typename (comp->ts.type));
524af0d6 1194 t = false;
e35e87dc 1195 }
e0e85e06 1196 else
e35e87dc 1197 {
524af0d6
JB
1198 bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1199 if (t)
e35e87dc
TB
1200 t = t2;
1201 }
e0e85e06 1202 }
5046aff5 1203
a48a9173
TB
1204 /* For strings, the length of the constructor should be the same as
1205 the one of the structure, ensure this if the lengths are known at
1206 compile time and when we are dealing with PARAMETER or structure
1207 constructors. */
1208 if (cons->expr->ts.type == BT_CHARACTER && comp->ts.u.cl
1209 && comp->ts.u.cl->length
1210 && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1211 && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1212 && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
083dd940 1213 && cons->expr->rank != 0
a48a9173
TB
1214 && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1215 comp->ts.u.cl->length->value.integer) != 0)
1216 {
1217 if (cons->expr->expr_type == EXPR_VARIABLE
1218 && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1219 {
1220 /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1221 to make use of the gfc_resolve_character_array_constructor
1222 machinery. The expression is later simplified away to
1223 an array of string literals. */
1224 gfc_expr *para = cons->expr;
1225 cons->expr = gfc_get_expr ();
1226 cons->expr->ts = para->ts;
1227 cons->expr->where = para->where;
1228 cons->expr->expr_type = EXPR_ARRAY;
1229 cons->expr->rank = para->rank;
1230 cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1231 gfc_constructor_append_expr (&cons->expr->value.constructor,
1232 para, &cons->expr->where);
1233 }
1234 if (cons->expr->expr_type == EXPR_ARRAY)
1235 {
1236 gfc_constructor *p;
1237 p = gfc_constructor_first (cons->expr->value.constructor);
1238 if (cons->expr->ts.u.cl != p->expr->ts.u.cl)
1239 {
c130efd5
TB
1240 gfc_charlen *cl, *cl2;
1241
1242 cl2 = NULL;
1243 for (cl = gfc_current_ns->cl_list; cl; cl = cl->next)
1244 {
1245 if (cl == cons->expr->ts.u.cl)
1246 break;
1247 cl2 = cl;
1248 }
1249
1250 gcc_assert (cl);
1251
1252 if (cl2)
1253 cl2->next = cl->next;
1254
1255 gfc_free_expr (cl->length);
cede9502 1256 free (cl);
a48a9173
TB
1257 }
1258
c130efd5 1259 cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
a48a9173
TB
1260 cons->expr->ts.u.cl->length_from_typespec = true;
1261 cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1262 gfc_resolve_character_array_constructor (cons->expr);
1263 }
1264 }
1265
c1203a70 1266 if (cons->expr->expr_type == EXPR_NULL
713485cc 1267 && !(comp->attr.pointer || comp->attr.allocatable
cadddfdd 1268 || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
cf2b3c22 1269 || (comp->ts.type == BT_CLASS
d40477b4 1270 && (CLASS_DATA (comp)->attr.class_pointer
7a08eda1 1271 || CLASS_DATA (comp)->attr.allocatable))))
c1203a70 1272 {
524af0d6 1273 t = false;
6a38e151 1274 gfc_error ("The NULL in the structure constructor at %L is "
c4100eae 1275 "being applied to component %qs, which is neither "
c1203a70
PT
1276 "a POINTER nor ALLOCATABLE", &cons->expr->where,
1277 comp->name);
1278 }
1279
6a38e151
JW
1280 if (comp->attr.proc_pointer && comp->ts.interface)
1281 {
1282 /* Check procedure pointer interface. */
1283 gfc_symbol *s2 = NULL;
1284 gfc_component *c2;
1285 const char *name;
1286 char err[200];
1287
2a573572
MM
1288 c2 = gfc_get_proc_ptr_comp (cons->expr);
1289 if (c2)
6a38e151
JW
1290 {
1291 s2 = c2->ts.interface;
1292 name = c2->name;
1293 }
1294 else if (cons->expr->expr_type == EXPR_FUNCTION)
1295 {
1296 s2 = cons->expr->symtree->n.sym->result;
1297 name = cons->expr->symtree->n.sym->result->name;
1298 }
1299 else if (cons->expr->expr_type != EXPR_NULL)
1300 {
1301 s2 = cons->expr->symtree->n.sym;
1302 name = cons->expr->symtree->n.sym->name;
1303 }
1304
1305 if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
6f3ab30d 1306 err, sizeof (err), NULL, NULL))
6a38e151
JW
1307 {
1308 gfc_error ("Interface mismatch for procedure-pointer component "
c4100eae 1309 "%qs in structure constructor at %L: %s",
6a38e151 1310 comp->name, &cons->expr->where, err);
524af0d6 1311 return false;
6a38e151
JW
1312 }
1313 }
1314
e8cd3983
JW
1315 if (!comp->attr.pointer || comp->attr.proc_pointer
1316 || cons->expr->expr_type == EXPR_NULL)
5046aff5
PT
1317 continue;
1318
1319 a = gfc_expr_attr (cons->expr);
1320
1321 if (!a.pointer && !a.target)
1322 {
524af0d6 1323 t = false;
6a38e151 1324 gfc_error ("The element in the structure constructor at %L, "
c4100eae 1325 "for pointer component %qs should be a POINTER or "
5046aff5
PT
1326 "a TARGET", &cons->expr->where, comp->name);
1327 }
4eceddd7 1328
80f95228
JW
1329 if (init)
1330 {
1331 /* F08:C461. Additional checks for pointer initialization. */
1332 if (a.allocatable)
1333 {
524af0d6 1334 t = false;
80f95228
JW
1335 gfc_error ("Pointer initialization target at %L "
1336 "must not be ALLOCATABLE ", &cons->expr->where);
1337 }
1338 if (!a.save)
1339 {
524af0d6 1340 t = false;
80f95228
JW
1341 gfc_error ("Pointer initialization target at %L "
1342 "must have the SAVE attribute", &cons->expr->where);
1343 }
1344 }
1345
4eceddd7 1346 /* F2003, C1272 (3). */
ccd7751b
TB
1347 bool impure = cons->expr->expr_type == EXPR_VARIABLE
1348 && (gfc_impure_variable (cons->expr->symtree->n.sym)
1349 || gfc_is_coindexed (cons->expr));
1350 if (impure && gfc_pure (NULL))
4eceddd7 1351 {
524af0d6 1352 t = false;
6a38e151 1353 gfc_error ("Invalid expression in the structure constructor for "
c4100eae 1354 "pointer component %qs at %L in PURE procedure",
d3a9eea2 1355 comp->name, &cons->expr->where);
4eceddd7 1356 }
80f95228 1357
ccd7751b
TB
1358 if (impure)
1359 gfc_unset_implicit_pure (NULL);
6de9cd9a
DN
1360 }
1361
1362 return t;
1363}
1364
1365
6de9cd9a
DN
1366/****************** Expression name resolution ******************/
1367
1368/* Returns 0 if a symbol was not declared with a type or
4f613946 1369 attribute declaration statement, nonzero otherwise. */
6de9cd9a
DN
1370
1371static int
edf1eac2 1372was_declared (gfc_symbol *sym)
6de9cd9a
DN
1373{
1374 symbol_attribute a;
1375
1376 a = sym->attr;
1377
1378 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1379 return 1;
1380
9439ae41 1381 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
edf1eac2 1382 || a.optional || a.pointer || a.save || a.target || a.volatile_
1eee5628 1383 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
be59db2d 1384 || a.asynchronous || a.codimension)
6de9cd9a
DN
1385 return 1;
1386
1387 return 0;
1388}
1389
1390
1391/* Determine if a symbol is generic or not. */
1392
1393static int
edf1eac2 1394generic_sym (gfc_symbol *sym)
6de9cd9a
DN
1395{
1396 gfc_symbol *s;
1397
1398 if (sym->attr.generic ||
1399 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1400 return 1;
1401
1402 if (was_declared (sym) || sym->ns->parent == NULL)
1403 return 0;
1404
1405 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
4d382327 1406
6d023ec5
JD
1407 if (s != NULL)
1408 {
1409 if (s == sym)
1410 return 0;
1411 else
1412 return generic_sym (s);
1413 }
6de9cd9a 1414
6d023ec5 1415 return 0;
6de9cd9a
DN
1416}
1417
1418
1419/* Determine if a symbol is specific or not. */
1420
1421static int
edf1eac2 1422specific_sym (gfc_symbol *sym)
6de9cd9a
DN
1423{
1424 gfc_symbol *s;
1425
1426 if (sym->attr.if_source == IFSRC_IFBODY
1427 || sym->attr.proc == PROC_MODULE
1428 || sym->attr.proc == PROC_INTERNAL
1429 || sym->attr.proc == PROC_ST_FUNCTION
edf1eac2 1430 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
6de9cd9a
DN
1431 || sym->attr.external)
1432 return 1;
1433
1434 if (was_declared (sym) || sym->ns->parent == NULL)
1435 return 0;
1436
1437 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1438
1439 return (s == NULL) ? 0 : specific_sym (s);
1440}
1441
1442
1443/* Figure out if the procedure is specific, generic or unknown. */
1444
a79683d5
TS
1445enum proc_type
1446{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
6de9cd9a
DN
1447
1448static proc_type
edf1eac2 1449procedure_kind (gfc_symbol *sym)
6de9cd9a 1450{
6de9cd9a
DN
1451 if (generic_sym (sym))
1452 return PTYPE_GENERIC;
1453
1454 if (specific_sym (sym))
1455 return PTYPE_SPECIFIC;
1456
1457 return PTYPE_UNKNOWN;
1458}
1459
48474141 1460/* Check references to assumed size arrays. The flag need_full_assumed_size
b82feea5 1461 is nonzero when matching actual arguments. */
48474141
PT
1462
1463static int need_full_assumed_size = 0;
1464
1465static bool
edf1eac2 1466check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
48474141 1467{
edf1eac2 1468 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
48474141
PT
1469 return false;
1470
e0c68ce9
ILT
1471 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1472 What should it be? */
582f2176 1473 if (e->ref && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
c52938ec 1474 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
e0c68ce9 1475 && (e->ref->u.ar.type == AR_FULL))
48474141
PT
1476 {
1477 gfc_error ("The upper bound in the last dimension must "
1478 "appear in the reference to the assumed size "
c4100eae 1479 "array %qs at %L", sym->name, &e->where);
48474141
PT
1480 return true;
1481 }
1482 return false;
1483}
1484
1485
1486/* Look for bad assumed size array references in argument expressions
1487 of elemental and array valued intrinsic procedures. Since this is
1488 called from procedure resolution functions, it only recurses at
1489 operators. */
1490
1491static bool
1492resolve_assumed_size_actual (gfc_expr *e)
1493{
1494 if (e == NULL)
1495 return false;
1496
1497 switch (e->expr_type)
1498 {
1499 case EXPR_VARIABLE:
edf1eac2 1500 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
48474141
PT
1501 return true;
1502 break;
1503
1504 case EXPR_OP:
1505 if (resolve_assumed_size_actual (e->value.op.op1)
edf1eac2 1506 || resolve_assumed_size_actual (e->value.op.op2))
48474141
PT
1507 return true;
1508 break;
1509
1510 default:
1511 break;
1512 }
1513 return false;
1514}
1515
6de9cd9a 1516
0b4e2af7
PT
1517/* Check a generic procedure, passed as an actual argument, to see if
1518 there is a matching specific name. If none, it is an error, and if
1519 more than one, the reference is ambiguous. */
1520static int
1521count_specific_procs (gfc_expr *e)
1522{
1523 int n;
1524 gfc_interface *p;
1525 gfc_symbol *sym;
4d382327 1526
0b4e2af7
PT
1527 n = 0;
1528 sym = e->symtree->n.sym;
1529
1530 for (p = sym->generic; p; p = p->next)
1531 if (strcmp (sym->name, p->sym->name) == 0)
1532 {
1533 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1534 sym->name);
1535 n++;
1536 }
1537
1538 if (n > 1)
c4100eae 1539 gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
0b4e2af7
PT
1540 &e->where);
1541
1542 if (n == 0)
c4100eae 1543 gfc_error ("GENERIC procedure %qs is not allowed as an actual "
0b4e2af7
PT
1544 "argument at %L", sym->name, &e->where);
1545
1546 return n;
1547}
1548
a03826d1 1549
1933ba0f 1550/* See if a call to sym could possibly be a not allowed RECURSION because of
eea58adb 1551 a missing RECURSIVE declaration. This means that either sym is the current
1933ba0f
DK
1552 context itself, or sym is the parent of a contained procedure calling its
1553 non-RECURSIVE containing procedure.
1554 This also works if sym is an ENTRY. */
1555
1556static bool
1557is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1558{
1559 gfc_symbol* proc_sym;
1560 gfc_symbol* context_proc;
9abe5e56 1561 gfc_namespace* real_context;
1933ba0f 1562
c3f34952
TB
1563 if (sym->attr.flavor == FL_PROGRAM
1564 || sym->attr.flavor == FL_DERIVED)
6f7e06ce
JD
1565 return false;
1566
1933ba0f
DK
1567 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1568
1569 /* If we've got an ENTRY, find real procedure. */
1570 if (sym->attr.entry && sym->ns->entries)
1571 proc_sym = sym->ns->entries->sym;
1572 else
1573 proc_sym = sym;
1574
1575 /* If sym is RECURSIVE, all is well of course. */
c61819ff 1576 if (proc_sym->attr.recursive || flag_recursive)
1933ba0f
DK
1577 return false;
1578
9abe5e56
DK
1579 /* Find the context procedure's "real" symbol if it has entries.
1580 We look for a procedure symbol, so recurse on the parents if we don't
1581 find one (like in case of a BLOCK construct). */
1582 for (real_context = context; ; real_context = real_context->parent)
1583 {
1584 /* We should find something, eventually! */
1585 gcc_assert (real_context);
1586
1587 context_proc = (real_context->entries ? real_context->entries->sym
1588 : real_context->proc_name);
1589
1590 /* In some special cases, there may not be a proc_name, like for this
1591 invalid code:
1592 real(bad_kind()) function foo () ...
1593 when checking the call to bad_kind ().
1594 In these cases, we simply return here and assume that the
1595 call is ok. */
1596 if (!context_proc)
1597 return false;
1598
1599 if (context_proc->attr.flavor != FL_LABEL)
1600 break;
1601 }
1933ba0f
DK
1602
1603 /* A call from sym's body to itself is recursion, of course. */
1604 if (context_proc == proc_sym)
1605 return true;
1606
1607 /* The same is true if context is a contained procedure and sym the
1608 containing one. */
1609 if (context_proc->attr.contained)
1610 {
1611 gfc_symbol* parent_proc;
1612
1613 gcc_assert (context->parent);
1614 parent_proc = (context->parent->entries ? context->parent->entries->sym
1615 : context->parent->proc_name);
1616
1617 if (parent_proc == proc_sym)
1618 return true;
1619 }
1620
1621 return false;
1622}
1623
1624
c73b6478
JW
1625/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1626 its typespec and formal argument list. */
1627
524af0d6 1628bool
2dda89a8 1629gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
c73b6478 1630{
d000aa67 1631 gfc_intrinsic_sym* isym = NULL;
f6038131
JW
1632 const char* symstd;
1633
1634 if (sym->formal)
524af0d6 1635 return true;
f6038131 1636
13157033
TB
1637 /* Already resolved. */
1638 if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
524af0d6 1639 return true;
13157033 1640
f6038131
JW
1641 /* We already know this one is an intrinsic, so we don't call
1642 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1643 gfc_find_subroutine directly to check whether it is a function or
1644 subroutine. */
1645
cadddfdd
TB
1646 if (sym->intmod_sym_id && sym->attr.subroutine)
1647 {
1648 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1649 isym = gfc_intrinsic_subroutine_by_id (id);
1650 }
1651 else if (sym->intmod_sym_id)
1652 {
1653 gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1654 isym = gfc_intrinsic_function_by_id (id);
1655 }
2b91eb32 1656 else if (!sym->attr.subroutine)
d000aa67
TB
1657 isym = gfc_find_function (sym->name);
1658
cadddfdd 1659 if (isym && !sym->attr.subroutine)
c73b6478 1660 {
73e42eef 1661 if (sym->ts.type != BT_UNKNOWN && warn_surprising
f6038131 1662 && !sym->attr.implicit_type)
48749dbc
MLI
1663 gfc_warning (OPT_Wsurprising,
1664 "Type specified for intrinsic function %qs at %L is"
f6038131
JW
1665 " ignored", sym->name, &sym->declared_at);
1666
c73b6478 1667 if (!sym->attr.function &&
524af0d6
JB
1668 !gfc_add_function(&sym->attr, sym->name, loc))
1669 return false;
f6038131 1670
c73b6478
JW
1671 sym->ts = isym->ts;
1672 }
cadddfdd 1673 else if (isym || (isym = gfc_find_subroutine (sym->name)))
c73b6478 1674 {
f6038131
JW
1675 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1676 {
c4100eae 1677 gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
f6038131 1678 " specifier", sym->name, &sym->declared_at);
524af0d6 1679 return false;
f6038131
JW
1680 }
1681
c73b6478 1682 if (!sym->attr.subroutine &&
524af0d6
JB
1683 !gfc_add_subroutine(&sym->attr, sym->name, loc))
1684 return false;
c73b6478 1685 }
f6038131
JW
1686 else
1687 {
c4100eae 1688 gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
f6038131 1689 &sym->declared_at);
524af0d6 1690 return false;
f6038131
JW
1691 }
1692
8fdcb6a9 1693 gfc_copy_formal_args_intr (sym, isym, NULL);
f6038131 1694
019c0e5d
TB
1695 sym->attr.pure = isym->pure;
1696 sym->attr.elemental = isym->elemental;
1697
f6038131 1698 /* Check it is actually available in the standard settings. */
524af0d6 1699 if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
f6038131 1700 {
a4d9b221
TB
1701 gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1702 "available in the current standard settings but %s. Use "
1703 "an appropriate %<-std=*%> option or enable "
1704 "%<-fall-intrinsics%> in order to use it.",
f6038131 1705 sym->name, &sym->declared_at, symstd);
524af0d6 1706 return false;
f6038131
JW
1707 }
1708
524af0d6 1709 return true;
c73b6478
JW
1710}
1711
1712
a03826d1
DK
1713/* Resolve a procedure expression, like passing it to a called procedure or as
1714 RHS for a procedure pointer assignment. */
1715
524af0d6 1716static bool
a03826d1
DK
1717resolve_procedure_expression (gfc_expr* expr)
1718{
1719 gfc_symbol* sym;
1720
1933ba0f 1721 if (expr->expr_type != EXPR_VARIABLE)
524af0d6 1722 return true;
a03826d1 1723 gcc_assert (expr->symtree);
1933ba0f 1724
a03826d1 1725 sym = expr->symtree->n.sym;
c73b6478
JW
1726
1727 if (sym->attr.intrinsic)
2dda89a8 1728 gfc_resolve_intrinsic (sym, &expr->where);
c73b6478 1729
1933ba0f
DK
1730 if (sym->attr.flavor != FL_PROCEDURE
1731 || (sym->attr.function && sym->result == sym))
524af0d6 1732 return true;
a03826d1
DK
1733
1734 /* A non-RECURSIVE procedure that is used as procedure expression within its
1735 own body is in danger of being called recursively. */
1933ba0f 1736 if (is_illegal_recursion (sym, gfc_current_ns))
db30e21c 1737 gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
a03826d1 1738 " itself recursively. Declare it RECURSIVE or use"
48749dbc 1739 " %<-frecursive%>", sym->name, &expr->where);
4d382327 1740
524af0d6 1741 return true;
a03826d1
DK
1742}
1743
1744
6de9cd9a
DN
1745/* Resolve an actual argument list. Most of the time, this is just
1746 resolving the expressions in the list.
1747 The exception is that we sometimes have to decide whether arguments
1748 that look like procedure arguments are really simple variable
1749 references. */
1750
524af0d6 1751static bool
0b4e2af7
PT
1752resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1753 bool no_formal_args)
6de9cd9a
DN
1754{
1755 gfc_symbol *sym;
1756 gfc_symtree *parent_st;
1757 gfc_expr *e;
d06790a0 1758 gfc_component *comp;
5ad6345e 1759 int save_need_full_assumed_size;
524af0d6 1760 bool return_value = false;
c62c6622 1761 bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
d3a9eea2 1762
c62c6622
TB
1763 actual_arg = true;
1764 first_actual_arg = true;
45a69325 1765
6de9cd9a
DN
1766 for (; arg; arg = arg->next)
1767 {
6de9cd9a
DN
1768 e = arg->expr;
1769 if (e == NULL)
edf1eac2
SK
1770 {
1771 /* Check the label is a valid branching target. */
1772 if (arg->label)
1773 {
1774 if (arg->label->defined == ST_LABEL_UNKNOWN)
1775 {
1776 gfc_error ("Label %d referenced at %L is never defined",
1777 arg->label->value, &arg->label->where);
c62c6622 1778 goto cleanup;
edf1eac2
SK
1779 }
1780 }
c62c6622 1781 first_actual_arg = false;
edf1eac2
SK
1782 continue;
1783 }
6de9cd9a 1784
67cec813 1785 if (e->expr_type == EXPR_VARIABLE
0b4e2af7
PT
1786 && e->symtree->n.sym->attr.generic
1787 && no_formal_args
1788 && count_specific_procs (e) != 1)
c62c6622 1789 goto cleanup;
27372c38 1790
6de9cd9a
DN
1791 if (e->ts.type != BT_PROCEDURE)
1792 {
5ad6345e 1793 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1794 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1795 need_full_assumed_size = 0;
524af0d6 1796 if (!gfc_resolve_expr (e))
c62c6622 1797 goto cleanup;
5ad6345e 1798 need_full_assumed_size = save_need_full_assumed_size;
7fcafa71 1799 goto argument_list;
6de9cd9a
DN
1800 }
1801
edf1eac2 1802 /* See if the expression node should really be a variable reference. */
6de9cd9a
DN
1803
1804 sym = e->symtree->n.sym;
1805
1806 if (sym->attr.flavor == FL_PROCEDURE
1807 || sym->attr.intrinsic
1808 || sym->attr.external)
1809 {
0e7e7e6e 1810 int actual_ok;
6de9cd9a 1811
d68bd5a8
PT
1812 /* If a procedure is not already determined to be something else
1813 check if it is intrinsic. */
0e8d854e 1814 if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
d68bd5a8
PT
1815 sym->attr.intrinsic = 1;
1816
2ed8d224
PT
1817 if (sym->attr.proc == PROC_ST_FUNCTION)
1818 {
c4100eae 1819 gfc_error ("Statement function %qs at %L is not allowed as an "
2ed8d224
PT
1820 "actual argument", sym->name, &e->where);
1821 }
1822
edf1eac2
SK
1823 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1824 sym->attr.subroutine);
0e7e7e6e
FXC
1825 if (sym->attr.intrinsic && actual_ok == 0)
1826 {
c4100eae 1827 gfc_error ("Intrinsic %qs at %L is not allowed as an "
0e7e7e6e
FXC
1828 "actual argument", sym->name, &e->where);
1829 }
0e7e7e6e 1830
2ed8d224
PT
1831 if (sym->attr.contained && !sym->attr.use_assoc
1832 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1833 {
a4d9b221 1834 if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
22c23886 1835 " used as actual argument at %L",
524af0d6 1836 sym->name, &e->where))
c62c6622 1837 goto cleanup;
2ed8d224
PT
1838 }
1839
1840 if (sym->attr.elemental && !sym->attr.intrinsic)
1841 {
c4100eae 1842 gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
edf1eac2 1843 "allowed as an actual argument at %L", sym->name,
2ed8d224
PT
1844 &e->where);
1845 }
781e1004 1846
36d3fb4c
PT
1847 /* Check if a generic interface has a specific procedure
1848 with the same name before emitting an error. */
0b4e2af7 1849 if (sym->attr.generic && count_specific_procs (e) != 1)
c62c6622
TB
1850 goto cleanup;
1851
0b4e2af7
PT
1852 /* Just in case a specific was found for the expression. */
1853 sym = e->symtree->n.sym;
3e978d30 1854
6de9cd9a
DN
1855 /* If the symbol is the function that names the current (or
1856 parent) scope, then we really have a variable reference. */
1857
2d71b918 1858 if (gfc_is_function_return_value (sym, sym->ns))
6de9cd9a
DN
1859 goto got_variable;
1860
20a037d5 1861 /* If all else fails, see if we have a specific intrinsic. */
26033479 1862 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
20a037d5
PT
1863 {
1864 gfc_intrinsic_sym *isym;
6cc309c9 1865
20a037d5
PT
1866 isym = gfc_find_function (sym->name);
1867 if (isym == NULL || !isym->specific)
1868 {
1869 gfc_error ("Unable to find a specific INTRINSIC procedure "
c4100eae 1870 "for the reference %qs at %L", sym->name,
20a037d5 1871 &e->where);
c62c6622 1872 goto cleanup;
20a037d5
PT
1873 }
1874 sym->ts = isym->ts;
6cc309c9 1875 sym->attr.intrinsic = 1;
26033479 1876 sym->attr.function = 1;
20a037d5 1877 }
a03826d1 1878
524af0d6 1879 if (!gfc_resolve_expr (e))
c62c6622 1880 goto cleanup;
7fcafa71 1881 goto argument_list;
6de9cd9a
DN
1882 }
1883
1884 /* See if the name is a module procedure in a parent unit. */
1885
1886 if (was_declared (sym) || sym->ns->parent == NULL)
1887 goto got_variable;
1888
1889 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1890 {
c4100eae 1891 gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
c62c6622 1892 goto cleanup;
6de9cd9a
DN
1893 }
1894
1895 if (parent_st == NULL)
1896 goto got_variable;
1897
1898 sym = parent_st->n.sym;
1899 e->symtree = parent_st; /* Point to the right thing. */
1900
1901 if (sym->attr.flavor == FL_PROCEDURE
1902 || sym->attr.intrinsic
1903 || sym->attr.external)
1904 {
524af0d6 1905 if (!gfc_resolve_expr (e))
c62c6622 1906 goto cleanup;
7fcafa71 1907 goto argument_list;
6de9cd9a
DN
1908 }
1909
1910 got_variable:
1911 e->expr_type = EXPR_VARIABLE;
1912 e->ts = sym->ts;
102344e2
TB
1913 if ((sym->as != NULL && sym->ts.type != BT_CLASS)
1914 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
1915 && CLASS_DATA (sym)->as))
6de9cd9a 1916 {
102344e2
TB
1917 e->rank = sym->ts.type == BT_CLASS
1918 ? CLASS_DATA (sym)->as->rank : sym->as->rank;
6de9cd9a
DN
1919 e->ref = gfc_get_ref ();
1920 e->ref->type = REF_ARRAY;
1921 e->ref->u.ar.type = AR_FULL;
102344e2
TB
1922 e->ref->u.ar.as = sym->ts.type == BT_CLASS
1923 ? CLASS_DATA (sym)->as : sym->as;
6de9cd9a 1924 }
7fcafa71 1925
1b35264f
DF
1926 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1927 primary.c (match_actual_arg). If above code determines that it
1928 is a variable instead, it needs to be resolved as it was not
1929 done at the beginning of this function. */
5ad6345e 1930 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1931 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1932 need_full_assumed_size = 0;
524af0d6 1933 if (!gfc_resolve_expr (e))
c62c6622 1934 goto cleanup;
5ad6345e 1935 need_full_assumed_size = save_need_full_assumed_size;
1b35264f 1936
7fcafa71
PT
1937 argument_list:
1938 /* Check argument list functions %VAL, %LOC and %REF. There is
1939 nothing to do for %REF. */
1940 if (arg->name && arg->name[0] == '%')
1941 {
1942 if (strncmp ("%VAL", arg->name, 4) == 0)
1943 {
1944 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1945 {
1946 gfc_error ("By-value argument at %L is not of numeric "
1947 "type", &e->where);
c62c6622 1948 goto cleanup;
7fcafa71
PT
1949 }
1950
1951 if (e->rank)
1952 {
1953 gfc_error ("By-value argument at %L cannot be an array or "
1954 "an array section", &e->where);
c62c6622 1955 goto cleanup;
7fcafa71
PT
1956 }
1957
1958 /* Intrinsics are still PROC_UNKNOWN here. However,
1959 since same file external procedures are not resolvable
1960 in gfortran, it is a good deal easier to leave them to
1961 intrinsic.c. */
7193e30a
TB
1962 if (ptype != PROC_UNKNOWN
1963 && ptype != PROC_DUMMY
29ea08da
TB
1964 && ptype != PROC_EXTERNAL
1965 && ptype != PROC_MODULE)
7fcafa71
PT
1966 {
1967 gfc_error ("By-value argument at %L is not allowed "
1968 "in this context", &e->where);
c62c6622 1969 goto cleanup;
7fcafa71 1970 }
7fcafa71
PT
1971 }
1972
1973 /* Statement functions have already been excluded above. */
1974 else if (strncmp ("%LOC", arg->name, 4) == 0
edf1eac2 1975 && e->ts.type == BT_PROCEDURE)
7fcafa71
PT
1976 {
1977 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1978 {
1979 gfc_error ("Passing internal procedure at %L by location "
1980 "not allowed", &e->where);
c62c6622 1981 goto cleanup;
7fcafa71
PT
1982 }
1983 }
1984 }
d3a9eea2 1985
d06790a0 1986 comp = gfc_get_proc_ptr_comp(e);
bc0c7f39
MM
1987 if (e->expr_type == EXPR_VARIABLE
1988 && comp && comp->attr.elemental)
d06790a0
JW
1989 {
1990 gfc_error ("ELEMENTAL procedure pointer component %qs is not "
1991 "allowed as an actual argument at %L", comp->name,
1992 &e->where);
1993 }
1994
d3a9eea2
TB
1995 /* Fortran 2008, C1237. */
1996 if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
c62c6622
TB
1997 && gfc_has_ultimate_pointer (e))
1998 {
1999 gfc_error ("Coindexed actual argument at %L with ultimate pointer "
d3a9eea2 2000 "component", &e->where);
c62c6622
TB
2001 goto cleanup;
2002 }
2003
2004 first_actual_arg = false;
6de9cd9a
DN
2005 }
2006
524af0d6 2007 return_value = true;
c62c6622
TB
2008
2009cleanup:
2010 actual_arg = actual_arg_sav;
2011 first_actual_arg = first_actual_arg_sav;
2012
2013 return return_value;
6de9cd9a
DN
2014}
2015
2016
b8ea6dbc
PT
2017/* Do the checks of the actual argument list that are specific to elemental
2018 procedures. If called with c == NULL, we have a function, otherwise if
2019 expr == NULL, we have a subroutine. */
edf1eac2 2020
524af0d6 2021static bool
b8ea6dbc
PT
2022resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2023{
2024 gfc_actual_arglist *arg0;
2025 gfc_actual_arglist *arg;
2026 gfc_symbol *esym = NULL;
2027 gfc_intrinsic_sym *isym = NULL;
2028 gfc_expr *e = NULL;
2029 gfc_intrinsic_arg *iformal = NULL;
2030 gfc_formal_arglist *eformal = NULL;
2031 bool formal_optional = false;
2032 bool set_by_optional = false;
2033 int i;
2034 int rank = 0;
2035
2036 /* Is this an elemental procedure? */
2037 if (expr && expr->value.function.actual != NULL)
2038 {
2039 if (expr->value.function.esym != NULL
edf1eac2 2040 && expr->value.function.esym->attr.elemental)
b8ea6dbc
PT
2041 {
2042 arg0 = expr->value.function.actual;
2043 esym = expr->value.function.esym;
2044 }
2045 else if (expr->value.function.isym != NULL
edf1eac2 2046 && expr->value.function.isym->elemental)
b8ea6dbc
PT
2047 {
2048 arg0 = expr->value.function.actual;
2049 isym = expr->value.function.isym;
2050 }
2051 else
524af0d6 2052 return true;
b8ea6dbc 2053 }
dd9315de 2054 else if (c && c->ext.actual != NULL)
b8ea6dbc
PT
2055 {
2056 arg0 = c->ext.actual;
4d382327 2057
dd9315de
DK
2058 if (c->resolved_sym)
2059 esym = c->resolved_sym;
2060 else
2061 esym = c->symtree->n.sym;
2062 gcc_assert (esym);
2063
2064 if (!esym->attr.elemental)
524af0d6 2065 return true;
b8ea6dbc
PT
2066 }
2067 else
524af0d6 2068 return true;
b8ea6dbc
PT
2069
2070 /* The rank of an elemental is the rank of its array argument(s). */
2071 for (arg = arg0; arg; arg = arg->next)
2072 {
c62c6622 2073 if (arg->expr != NULL && arg->expr->rank != 0)
b8ea6dbc
PT
2074 {
2075 rank = arg->expr->rank;
2076 if (arg->expr->expr_type == EXPR_VARIABLE
edf1eac2 2077 && arg->expr->symtree->n.sym->attr.optional)
b8ea6dbc
PT
2078 set_by_optional = true;
2079
2080 /* Function specific; set the result rank and shape. */
2081 if (expr)
2082 {
2083 expr->rank = rank;
2084 if (!expr->shape && arg->expr->shape)
2085 {
2086 expr->shape = gfc_get_shape (rank);
2087 for (i = 0; i < rank; i++)
2088 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2089 }
2090 }
2091 break;
2092 }
2093 }
2094
2095 /* If it is an array, it shall not be supplied as an actual argument
2096 to an elemental procedure unless an array of the same rank is supplied
2097 as an actual argument corresponding to a nonoptional dummy argument of
2098 that elemental procedure(12.4.1.5). */
2099 formal_optional = false;
2100 if (isym)
2101 iformal = isym->formal;
2102 else
2103 eformal = esym->formal;
2104
2105 for (arg = arg0; arg; arg = arg->next)
2106 {
2107 if (eformal)
2108 {
2109 if (eformal->sym && eformal->sym->attr.optional)
2110 formal_optional = true;
2111 eformal = eformal->next;
2112 }
2113 else if (isym && iformal)
2114 {
2115 if (iformal->optional)
2116 formal_optional = true;
2117 iformal = iformal->next;
2118 }
2119 else if (isym)
2120 formal_optional = true;
2121
994c1cc0 2122 if (pedantic && arg->expr != NULL
edf1eac2
SK
2123 && arg->expr->expr_type == EXPR_VARIABLE
2124 && arg->expr->symtree->n.sym->attr.optional
2125 && formal_optional
2126 && arg->expr->rank
2127 && (set_by_optional || arg->expr->rank != rank)
cd5ecab6 2128 && !(isym && isym->id == GFC_ISYM_CONVERSION))
b8ea6dbc 2129 {
db30e21c 2130 gfc_warning (0, "%qs at %L is an array and OPTIONAL; IF IT IS "
994c1cc0 2131 "MISSING, it cannot be the actual argument of an "
edf1eac2 2132 "ELEMENTAL procedure unless there is a non-optional "
994c1cc0
SK
2133 "argument with the same rank (12.4.1.5)",
2134 arg->expr->symtree->n.sym->name, &arg->expr->where);
b8ea6dbc
PT
2135 }
2136 }
2137
2138 for (arg = arg0; arg; arg = arg->next)
2139 {
2140 if (arg->expr == NULL || arg->expr->rank == 0)
2141 continue;
2142
2143 /* Being elemental, the last upper bound of an assumed size array
2144 argument must be present. */
2145 if (resolve_assumed_size_actual (arg->expr))
524af0d6 2146 return false;
b8ea6dbc 2147
3c7b91d3 2148 /* Elemental procedure's array actual arguments must conform. */
b8ea6dbc
PT
2149 if (e != NULL)
2150 {
524af0d6
JB
2151 if (!gfc_check_conformance (arg->expr, e, "elemental procedure"))
2152 return false;
b8ea6dbc
PT
2153 }
2154 else
2155 e = arg->expr;
2156 }
2157
4a965827
TB
2158 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2159 is an array, the intent inout/out variable needs to be also an array. */
2160 if (rank > 0 && esym && expr == NULL)
2161 for (eformal = esym->formal, arg = arg0; arg && eformal;
2162 arg = arg->next, eformal = eformal->next)
2163 if ((eformal->sym->attr.intent == INTENT_OUT
2164 || eformal->sym->attr.intent == INTENT_INOUT)
2165 && arg->expr && arg->expr->rank == 0)
2166 {
c4100eae
MLI
2167 gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2168 "ELEMENTAL subroutine %qs is a scalar, but another "
4a965827
TB
2169 "actual argument is an array", &arg->expr->where,
2170 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2171 : "INOUT", eformal->sym->name, esym->name);
524af0d6 2172 return false;
4a965827 2173 }
524af0d6 2174 return true;
b8ea6dbc
PT
2175}
2176
2177
68ea355b
PT
2178/* This function does the checking of references to global procedures
2179 as defined in sections 18.1 and 14.1, respectively, of the Fortran
2180 77 and 95 standards. It checks for a gsymbol for the name, making
2181 one if it does not already exist. If it already exists, then the
2182 reference being resolved must correspond to the type of gsymbol.
05c1e3a7 2183 Otherwise, the new symbol is equipped with the attributes of the
68ea355b 2184 reference. The corresponding code that is called in creating
71a7778c
PT
2185 global entities is parse.c.
2186
2187 In addition, for all but -std=legacy, the gsymbols are used to
2188 check the interfaces of external procedures from the same file.
2189 The namespace of the gsymbol is resolved and then, once this is
2190 done the interface is checked. */
68ea355b 2191
3af8d8cb
PT
2192
2193static bool
2194not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2195{
2196 if (!gsym_ns->proc_name->attr.recursive)
2197 return true;
2198
2199 if (sym->ns == gsym_ns)
2200 return false;
2201
2202 if (sym->ns->parent && sym->ns->parent == gsym_ns)
2203 return false;
2204
2205 return true;
2206}
2207
2208static bool
2209not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2210{
2211 if (gsym_ns->entries)
2212 {
2213 gfc_entry_list *entry = gsym_ns->entries;
2214
2215 for (; entry; entry = entry->next)
2216 {
2217 if (strcmp (sym->name, entry->sym->name) == 0)
2218 {
2219 if (strcmp (gsym_ns->proc_name->name,
2220 sym->ns->proc_name->name) == 0)
2221 return false;
2222
2223 if (sym->ns->parent
2224 && strcmp (gsym_ns->proc_name->name,
2225 sym->ns->parent->proc_name->name) == 0)
2226 return false;
2227 }
2228 }
2229 }
2230 return true;
2231}
2232
96486998
JW
2233
2234/* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2235
2236bool
2237gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2238{
2239 gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2240
2241 for ( ; arg; arg = arg->next)
2242 {
2243 if (!arg->sym)
2244 continue;
2245
2246 if (arg->sym->attr.allocatable) /* (2a) */
2247 {
2248 strncpy (errmsg, _("allocatable argument"), err_len);
2249 return true;
2250 }
2251 else if (arg->sym->attr.asynchronous)
2252 {
2253 strncpy (errmsg, _("asynchronous argument"), err_len);
2254 return true;
2255 }
2256 else if (arg->sym->attr.optional)
2257 {
2258 strncpy (errmsg, _("optional argument"), err_len);
2259 return true;
2260 }
2261 else if (arg->sym->attr.pointer)
2262 {
2263 strncpy (errmsg, _("pointer argument"), err_len);
2264 return true;
2265 }
2266 else if (arg->sym->attr.target)
2267 {
2268 strncpy (errmsg, _("target argument"), err_len);
2269 return true;
2270 }
2271 else if (arg->sym->attr.value)
2272 {
2273 strncpy (errmsg, _("value argument"), err_len);
2274 return true;
2275 }
2276 else if (arg->sym->attr.volatile_)
2277 {
2278 strncpy (errmsg, _("volatile argument"), err_len);
2279 return true;
2280 }
2281 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2282 {
2283 strncpy (errmsg, _("assumed-shape argument"), err_len);
2284 return true;
2285 }
2286 else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2287 {
2288 strncpy (errmsg, _("assumed-rank argument"), err_len);
2289 return true;
2290 }
2291 else if (arg->sym->attr.codimension) /* (2c) */
2292 {
2293 strncpy (errmsg, _("coarray argument"), err_len);
2294 return true;
2295 }
2296 else if (false) /* (2d) TODO: parametrized derived type */
2297 {
2298 strncpy (errmsg, _("parametrized derived type argument"), err_len);
2299 return true;
2300 }
2301 else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2302 {
2303 strncpy (errmsg, _("polymorphic argument"), err_len);
2304 return true;
2305 }
e7ac6a7c
TB
2306 else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2307 {
2308 strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2309 return true;
2310 }
96486998
JW
2311 else if (arg->sym->ts.type == BT_ASSUMED)
2312 {
2313 /* As assumed-type is unlimited polymorphic (cf. above).
2314 See also TS 29113, Note 6.1. */
2315 strncpy (errmsg, _("assumed-type argument"), err_len);
2316 return true;
2317 }
2318 }
2319
2320 if (sym->attr.function)
2321 {
2322 gfc_symbol *res = sym->result ? sym->result : sym;
2323
2324 if (res->attr.dimension) /* (3a) */
2325 {
2326 strncpy (errmsg, _("array result"), err_len);
2327 return true;
2328 }
2329 else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2330 {
2331 strncpy (errmsg, _("pointer or allocatable result"), err_len);
2332 return true;
2333 }
2334 else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2335 && res->ts.u.cl->length
2336 && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2337 {
2338 strncpy (errmsg, _("result with non-constant character length"), err_len);
2339 return true;
2340 }
2341 }
2342
019c0e5d 2343 if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
96486998
JW
2344 {
2345 strncpy (errmsg, _("elemental procedure"), err_len);
2346 return true;
2347 }
2348 else if (sym->attr.is_bind_c) /* (5) */
2349 {
2350 strncpy (errmsg, _("bind(c) procedure"), err_len);
2351 return true;
2352 }
2353
2354 return false;
2355}
2356
2357
ff604888 2358static void
71a7778c
PT
2359resolve_global_procedure (gfc_symbol *sym, locus *where,
2360 gfc_actual_arglist **actual, int sub)
68ea355b
PT
2361{
2362 gfc_gsymbol * gsym;
71a7778c 2363 gfc_namespace *ns;
32e8bb8e 2364 enum gfc_symbol_type type;
96486998 2365 char reason[200];
68ea355b
PT
2366
2367 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2368
f11de7c5 2369 gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name);
68ea355b
PT
2370
2371 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
ca39e6f2 2372 gfc_global_used (gsym, where);
68ea355b 2373
9fa52231
TB
2374 if ((sym->attr.if_source == IFSRC_UNKNOWN
2375 || sym->attr.if_source == IFSRC_IFBODY)
2376 && gsym->type != GSYM_UNKNOWN
04ba12ef 2377 && !gsym->binding_label
9fa52231
TB
2378 && gsym->ns
2379 && gsym->ns->resolved != -1
2380 && gsym->ns->proc_name
2381 && not_in_recursive (sym, gsym->ns)
2382 && not_entry_self_reference (sym, gsym->ns))
71a7778c 2383 {
48a32c49
TB
2384 gfc_symbol *def_sym;
2385
cc9a4ca9 2386 /* Resolve the gsymbol namespace if needed. */
71a7778c 2387 if (!gsym->ns->resolved)
3af8d8cb
PT
2388 {
2389 gfc_dt_list *old_dt_list;
2390
2391 /* Stash away derived types so that the backend_decls do not
2392 get mixed up. */
2393 old_dt_list = gfc_derived_types;
2394 gfc_derived_types = NULL;
2395
2396 gfc_resolve (gsym->ns);
2397
2398 /* Store the new derived types with the global namespace. */
2399 if (gfc_derived_types)
2400 gsym->ns->derived_types = gfc_derived_types;
2401
2402 /* Restore the derived types of this namespace. */
2403 gfc_derived_types = old_dt_list;
2404 }
2405
cc9a4ca9
PT
2406 /* Make sure that translation for the gsymbol occurs before
2407 the procedure currently being resolved. */
2408 ns = gfc_global_ns_list;
2409 for (; ns && ns != gsym->ns; ns = ns->sibling)
2410 {
2411 if (ns->sibling == gsym->ns)
2412 {
2413 ns->sibling = gsym->ns->sibling;
2414 gsym->ns->sibling = gfc_global_ns_list;
2415 gfc_global_ns_list = gsym->ns;
2416 break;
2417 }
2418 }
2419
48a32c49 2420 def_sym = gsym->ns->proc_name;
77f8682b
TB
2421
2422 /* This can happen if a binding name has been specified. */
2423 if (gsym->binding_label && gsym->sym_name != def_sym->name)
2424 gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2425
48a32c49
TB
2426 if (def_sym->attr.entry_master)
2427 {
2428 gfc_entry_list *entry;
2429 for (entry = gsym->ns->entries; entry; entry = entry->next)
2430 if (strcmp (entry->sym->name, sym->name) == 0)
2431 {
2432 def_sym = entry->sym;
2433 break;
2434 }
2435 }
2436
96486998 2437 if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
30145da5 2438 {
c4100eae 2439 gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
96486998
JW
2440 sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2441 gfc_typename (&def_sym->ts));
2442 goto done;
30145da5
DF
2443 }
2444
96486998
JW
2445 if (sym->attr.if_source == IFSRC_UNKNOWN
2446 && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
30145da5 2447 {
c4100eae 2448 gfc_error ("Explicit interface required for %qs at %L: %s",
96486998
JW
2449 sym->name, &sym->declared_at, reason);
2450 goto done;
1b1a6626
DF
2451 }
2452
96486998
JW
2453 if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU))
2454 /* Turn erros into warnings with -std=gnu and -std=legacy. */
f4031599 2455 gfc_errors_to_warnings (true);
1b1a6626 2456
96486998
JW
2457 if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2458 reason, sizeof(reason), NULL, NULL))
22c23886 2459 {
c4100eae 2460 gfc_error ("Interface mismatch in global procedure %qs at %L: %s ",
96486998
JW
2461 sym->name, &sym->declared_at, reason);
2462 goto done;
30145da5
DF
2463 }
2464
9fa52231 2465 if (!pedantic
30145da5
DF
2466 || ((gfc_option.warn_std & GFC_STD_LEGACY)
2467 && !(gfc_option.warn_std & GFC_STD_GNU)))
f4031599 2468 gfc_errors_to_warnings (true);
71a7778c 2469
4d382327 2470 if (sym->attr.if_source != IFSRC_IFBODY)
fb55ca75 2471 gfc_procedure_use (def_sym, actual, where);
71a7778c 2472 }
22c23886 2473
96486998 2474done:
f4031599 2475 gfc_errors_to_warnings (false);
71a7778c 2476
68ea355b
PT
2477 if (gsym->type == GSYM_UNKNOWN)
2478 {
2479 gsym->type = type;
2480 gsym->where = *where;
2481 }
2482
2483 gsym->used = 1;
2484}
1524f80b 2485
edf1eac2 2486
6de9cd9a
DN
2487/************* Function resolution *************/
2488
2489/* Resolve a function call known to be generic.
2490 Section 14.1.2.4.1. */
2491
2492static match
edf1eac2 2493resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
6de9cd9a
DN
2494{
2495 gfc_symbol *s;
2496
2497 if (sym->attr.generic)
2498 {
edf1eac2 2499 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
6de9cd9a
DN
2500 if (s != NULL)
2501 {
2502 expr->value.function.name = s->name;
2503 expr->value.function.esym = s;
f5f701ad
PT
2504
2505 if (s->ts.type != BT_UNKNOWN)
2506 expr->ts = s->ts;
2507 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2508 expr->ts = s->result->ts;
2509
6de9cd9a
DN
2510 if (s->as != NULL)
2511 expr->rank = s->as->rank;
f5f701ad
PT
2512 else if (s->result != NULL && s->result->as != NULL)
2513 expr->rank = s->result->as->rank;
2514
0a164a3c
PT
2515 gfc_set_sym_referenced (expr->value.function.esym);
2516
6de9cd9a
DN
2517 return MATCH_YES;
2518 }
2519
edf1eac2
SK
2520 /* TODO: Need to search for elemental references in generic
2521 interface. */
6de9cd9a
DN
2522 }
2523
2524 if (sym->attr.intrinsic)
2525 return gfc_intrinsic_func_interface (expr, 0);
2526
2527 return MATCH_NO;
2528}
2529
2530
524af0d6 2531static bool
edf1eac2 2532resolve_generic_f (gfc_expr *expr)
6de9cd9a
DN
2533{
2534 gfc_symbol *sym;
2535 match m;
c3f34952 2536 gfc_interface *intr = NULL;
6de9cd9a
DN
2537
2538 sym = expr->symtree->n.sym;
2539
2540 for (;;)
2541 {
2542 m = resolve_generic_f0 (expr, sym);
2543 if (m == MATCH_YES)
524af0d6 2544 return true;
6de9cd9a 2545 else if (m == MATCH_ERROR)
524af0d6 2546 return false;
6de9cd9a
DN
2547
2548generic:
c3f34952
TB
2549 if (!intr)
2550 for (intr = sym->generic; intr; intr = intr->next)
2551 if (intr->sym->attr.flavor == FL_DERIVED)
2552 break;
2553
6de9cd9a
DN
2554 if (sym->ns->parent == NULL)
2555 break;
2556 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2557
2558 if (sym == NULL)
2559 break;
2560 if (!generic_sym (sym))
2561 goto generic;
2562 }
2563
71f77fd7
PT
2564 /* Last ditch attempt. See if the reference is to an intrinsic
2565 that possesses a matching interface. 14.1.2.4 */
c3f34952 2566 if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a 2567 {
c4100eae 2568 gfc_error ("There is no specific function for the generic %qs "
c3f34952 2569 "at %L", expr->symtree->n.sym->name, &expr->where);
524af0d6 2570 return false;
6de9cd9a
DN
2571 }
2572
c3f34952
TB
2573 if (intr)
2574 {
22c23886 2575 if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
524af0d6
JB
2576 NULL, false))
2577 return false;
c3f34952
TB
2578 return resolve_structure_cons (expr, 0);
2579 }
2580
6de9cd9a
DN
2581 m = gfc_intrinsic_func_interface (expr, 0);
2582 if (m == MATCH_YES)
524af0d6 2583 return true;
c3f34952 2584
6de9cd9a 2585 if (m == MATCH_NO)
c4100eae 2586 gfc_error ("Generic function %qs at %L is not consistent with a "
edf1eac2
SK
2587 "specific intrinsic interface", expr->symtree->n.sym->name,
2588 &expr->where);
6de9cd9a 2589
524af0d6 2590 return false;
6de9cd9a
DN
2591}
2592
2593
2594/* Resolve a function call known to be specific. */
2595
2596static match
edf1eac2 2597resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
6de9cd9a
DN
2598{
2599 match m;
2600
2601 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2602 {
2603 if (sym->attr.dummy)
2604 {
2605 sym->attr.proc = PROC_DUMMY;
2606 goto found;
2607 }
2608
2609 sym->attr.proc = PROC_EXTERNAL;
2610 goto found;
2611 }
2612
2613 if (sym->attr.proc == PROC_MODULE
2614 || sym->attr.proc == PROC_ST_FUNCTION
2615 || sym->attr.proc == PROC_INTERNAL)
2616 goto found;
2617
2618 if (sym->attr.intrinsic)
2619 {
2620 m = gfc_intrinsic_func_interface (expr, 1);
2621 if (m == MATCH_YES)
2622 return MATCH_YES;
2623 if (m == MATCH_NO)
c4100eae 2624 gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
edf1eac2 2625 "with an intrinsic", sym->name, &expr->where);
6de9cd9a
DN
2626
2627 return MATCH_ERROR;
2628 }
2629
2630 return MATCH_NO;
2631
2632found:
2633 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2634
a7c0b11d
JW
2635 if (sym->result)
2636 expr->ts = sym->result->ts;
2637 else
2638 expr->ts = sym->ts;
6de9cd9a
DN
2639 expr->value.function.name = sym->name;
2640 expr->value.function.esym = sym;
6c25f796
AV
2641 /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2642 error(s). */
2643 if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2644 return MATCH_ERROR;
36ad06d2
JW
2645 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2646 expr->rank = CLASS_DATA (sym)->as->rank;
2647 else if (sym->as != NULL)
6de9cd9a
DN
2648 expr->rank = sym->as->rank;
2649
2650 return MATCH_YES;
2651}
2652
2653
524af0d6 2654static bool
edf1eac2 2655resolve_specific_f (gfc_expr *expr)
6de9cd9a
DN
2656{
2657 gfc_symbol *sym;
2658 match m;
2659
2660 sym = expr->symtree->n.sym;
2661
2662 for (;;)
2663 {
2664 m = resolve_specific_f0 (sym, expr);
2665 if (m == MATCH_YES)
524af0d6 2666 return true;
6de9cd9a 2667 if (m == MATCH_ERROR)
524af0d6 2668 return false;
6de9cd9a
DN
2669
2670 if (sym->ns->parent == NULL)
2671 break;
2672
2673 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2674
2675 if (sym == NULL)
2676 break;
2677 }
2678
c4100eae 2679 gfc_error ("Unable to resolve the specific function %qs at %L",
6de9cd9a
DN
2680 expr->symtree->n.sym->name, &expr->where);
2681
524af0d6 2682 return true;
6de9cd9a
DN
2683}
2684
2685
2686/* Resolve a procedure call not known to be generic nor specific. */
2687
524af0d6 2688static bool
edf1eac2 2689resolve_unknown_f (gfc_expr *expr)
6de9cd9a
DN
2690{
2691 gfc_symbol *sym;
2692 gfc_typespec *ts;
2693
2694 sym = expr->symtree->n.sym;
2695
2696 if (sym->attr.dummy)
2697 {
2698 sym->attr.proc = PROC_DUMMY;
2699 expr->value.function.name = sym->name;
2700 goto set_type;
2701 }
2702
2703 /* See if we have an intrinsic function reference. */
2704
c3005b0f 2705 if (gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a
DN
2706 {
2707 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
524af0d6
JB
2708 return true;
2709 return false;
6de9cd9a
DN
2710 }
2711
2712 /* The reference is to an external name. */
2713
2714 sym->attr.proc = PROC_EXTERNAL;
2715 expr->value.function.name = sym->name;
2716 expr->value.function.esym = expr->symtree->n.sym;
2717
2718 if (sym->as != NULL)
2719 expr->rank = sym->as->rank;
2720
2721 /* Type of the expression is either the type of the symbol or the
2722 default type of the symbol. */
2723
2724set_type:
2725 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2726
2727 if (sym->ts.type != BT_UNKNOWN)
2728 expr->ts = sym->ts;
2729 else
2730 {
713485cc 2731 ts = gfc_get_default_type (sym->name, sym->ns);
6de9cd9a
DN
2732
2733 if (ts->type == BT_UNKNOWN)
2734 {
c4100eae 2735 gfc_error ("Function %qs at %L has no IMPLICIT type",
6de9cd9a 2736 sym->name, &expr->where);
524af0d6 2737 return false;
6de9cd9a
DN
2738 }
2739 else
2740 expr->ts = *ts;
2741 }
2742
524af0d6 2743 return true;
6de9cd9a
DN
2744}
2745
2746
e7c8ff56
PT
2747/* Return true, if the symbol is an external procedure. */
2748static bool
2749is_external_proc (gfc_symbol *sym)
2750{
2751 if (!sym->attr.dummy && !sym->attr.contained
0e8d854e 2752 && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
e7c8ff56 2753 && sym->attr.proc != PROC_ST_FUNCTION
68d8db77 2754 && !sym->attr.proc_pointer
e7c8ff56
PT
2755 && !sym->attr.use_assoc
2756 && sym->name)
2757 return true;
c3005b0f
DK
2758
2759 return false;
e7c8ff56
PT
2760}
2761
2762
2054fc29
VR
2763/* Figure out if a function reference is pure or not. Also set the name
2764 of the function for a potential error message. Return nonzero if the
6de9cd9a 2765 function is PURE, zero if not. */
908a2235
PT
2766static int
2767pure_stmt_function (gfc_expr *, gfc_symbol *);
6de9cd9a
DN
2768
2769static int
edf1eac2 2770pure_function (gfc_expr *e, const char **name)
6de9cd9a
DN
2771{
2772 int pure;
5930876d 2773 gfc_component *comp;
6de9cd9a 2774
36f7dcae
PT
2775 *name = NULL;
2776
9ebe2d22
PT
2777 if (e->symtree != NULL
2778 && e->symtree->n.sym != NULL
2779 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
908a2235 2780 return pure_stmt_function (e, e->symtree->n.sym);
9ebe2d22 2781
5930876d
JW
2782 comp = gfc_get_proc_ptr_comp (e);
2783 if (comp)
2784 {
2785 pure = gfc_pure (comp->ts.interface);
2786 *name = comp->name;
2787 }
2788 else if (e->value.function.esym)
6de9cd9a
DN
2789 {
2790 pure = gfc_pure (e->value.function.esym);
2791 *name = e->value.function.esym->name;
2792 }
2793 else if (e->value.function.isym)
2794 {
2795 pure = e->value.function.isym->pure
edf1eac2 2796 || e->value.function.isym->elemental;
6de9cd9a
DN
2797 *name = e->value.function.isym->name;
2798 }
2799 else
2800 {
2801 /* Implicit functions are not pure. */
2802 pure = 0;
2803 *name = e->value.function.name;
2804 }
2805
2806 return pure;
2807}
2808
2809
908a2235
PT
2810static bool
2811impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2812 int *f ATTRIBUTE_UNUSED)
2813{
2814 const char *name;
2815
2816 /* Don't bother recursing into other statement functions
2817 since they will be checked individually for purity. */
2818 if (e->expr_type != EXPR_FUNCTION
2819 || !e->symtree
2820 || e->symtree->n.sym == sym
2821 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2822 return false;
2823
2824 return pure_function (e, &name) ? false : true;
2825}
2826
2827
2828static int
2829pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2830{
2831 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2832}
2833
2834
41cc1dd0 2835/* Check if an impure function is allowed in the current context. */
5930876d
JW
2836
2837static bool check_pure_function (gfc_expr *e)
2838{
2839 const char *name = NULL;
2840 if (!pure_function (e, &name) && name)
2841 {
2842 if (forall_flag)
2843 {
41cc1dd0 2844 gfc_error ("Reference to impure function %qs at %L inside a "
5930876d
JW
2845 "FORALL %s", name, &e->where,
2846 forall_flag == 2 ? "mask" : "block");
2847 return false;
2848 }
2849 else if (gfc_do_concurrent_flag)
2850 {
41cc1dd0 2851 gfc_error ("Reference to impure function %qs at %L inside a "
5930876d
JW
2852 "DO CONCURRENT %s", name, &e->where,
2853 gfc_do_concurrent_flag == 2 ? "mask" : "block");
2854 return false;
2855 }
2856 else if (gfc_pure (NULL))
2857 {
41cc1dd0 2858 gfc_error ("Reference to impure function %qs at %L "
5930876d
JW
2859 "within a PURE procedure", name, &e->where);
2860 return false;
2861 }
2862 gfc_unset_implicit_pure (NULL);
2863 }
2864 return true;
2865}
2866
2867
30c931de
PT
2868/* Update current procedure's array_outer_dependency flag, considering
2869 a call to procedure SYM. */
2870
2871static void
2872update_current_proc_array_outer_dependency (gfc_symbol *sym)
2873{
2874 /* Check to see if this is a sibling function that has not yet
2875 been resolved. */
2876 gfc_namespace *sibling = gfc_current_ns->sibling;
2877 for (; sibling; sibling = sibling->sibling)
2878 {
2879 if (sibling->proc_name == sym)
2880 {
2881 gfc_resolve (sibling);
2882 break;
2883 }
2884 }
2885
2886 /* If SYM has references to outer arrays, so has the procedure calling
2887 SYM. If SYM is a procedure pointer, we can assume the worst. */
2888 if (sym->attr.array_outer_dependency
2889 || sym->attr.proc_pointer)
2890 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
2891}
2892
2893
6de9cd9a
DN
2894/* Resolve a function call, which means resolving the arguments, then figuring
2895 out which entity the name refers to. */
6de9cd9a 2896
524af0d6 2897static bool
edf1eac2 2898resolve_function (gfc_expr *expr)
6de9cd9a
DN
2899{
2900 gfc_actual_arglist *arg;
edf1eac2 2901 gfc_symbol *sym;
524af0d6 2902 bool t;
48474141 2903 int temp;
7fcafa71 2904 procedure_type p = PROC_INTRINSIC;
0b4e2af7 2905 bool no_formal_args;
48474141 2906
20236f90
PT
2907 sym = NULL;
2908 if (expr->symtree)
2909 sym = expr->symtree->n.sym;
2910
6c036626 2911 /* If this is a procedure pointer component, it has already been resolved. */
2a573572 2912 if (gfc_is_proc_ptr_comp (expr))
524af0d6 2913 return true;
2a573572 2914
2c68bc89 2915 if (sym && sym->attr.intrinsic
524af0d6
JB
2916 && !gfc_resolve_intrinsic (sym, &expr->where))
2917 return false;
2c68bc89 2918
726d8566 2919 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
20a037d5 2920 {
c4100eae 2921 gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
524af0d6 2922 return false;
20a037d5
PT
2923 }
2924
8bae6273 2925 /* If this ia a deferred TBP with an abstract interface (which may
b3d286ba
JW
2926 of course be referenced), expr->value.function.esym will be set. */
2927 if (sym && sym->attr.abstract && !expr->value.function.esym)
9e1d712c 2928 {
c4100eae 2929 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
9e1d712c 2930 sym->name, &expr->where);
524af0d6 2931 return false;
9e1d712c
TB
2932 }
2933
48474141
PT
2934 /* Switch off assumed size checking and do this again for certain kinds
2935 of procedure, once the procedure itself is resolved. */
2936 need_full_assumed_size++;
6de9cd9a 2937
7fcafa71
PT
2938 if (expr->symtree && expr->symtree->n.sym)
2939 p = expr->symtree->n.sym->attr.proc;
2940
d3a9eea2
TB
2941 if (expr->value.function.isym && expr->value.function.isym->inquiry)
2942 inquiry_argument = true;
4cbc9039
JW
2943 no_formal_args = sym && is_external_proc (sym)
2944 && gfc_sym_get_dummy_args (sym) == NULL;
d3a9eea2 2945
22c23886 2946 if (!resolve_actual_arglist (expr->value.function.actual,
524af0d6 2947 p, no_formal_args))
d3a9eea2
TB
2948 {
2949 inquiry_argument = false;
524af0d6 2950 return false;
d3a9eea2 2951 }
6de9cd9a 2952
d3a9eea2 2953 inquiry_argument = false;
4d382327 2954
a8b3b0b6 2955 /* Resume assumed_size checking. */
48474141
PT
2956 need_full_assumed_size--;
2957
71a7778c
PT
2958 /* If the procedure is external, check for usage. */
2959 if (sym && is_external_proc (sym))
2960 resolve_global_procedure (sym, &expr->where,
2961 &expr->value.function.actual, 0);
2962
20236f90 2963 if (sym && sym->ts.type == BT_CHARACTER
bc21d315
JW
2964 && sym->ts.u.cl
2965 && sym->ts.u.cl->length == NULL
edf1eac2 2966 && !sym->attr.dummy
8d51f26f 2967 && !sym->ts.deferred
edf1eac2
SK
2968 && expr->value.function.esym == NULL
2969 && !sym->attr.contained)
20236f90 2970 {
20236f90 2971 /* Internal procedures are taken care of in resolve_contained_fntype. */
c4100eae 2972 gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
0e3e65bc
PT
2973 "be used at %L since it is not a dummy argument",
2974 sym->name, &expr->where);
524af0d6 2975 return false;
20236f90
PT
2976 }
2977
edf1eac2 2978 /* See if function is already resolved. */
6de9cd9a 2979
b46ebd6c
JJ
2980 if (expr->value.function.name != NULL
2981 || expr->value.function.isym != NULL)
6de9cd9a
DN
2982 {
2983 if (expr->ts.type == BT_UNKNOWN)
20236f90 2984 expr->ts = sym->ts;
524af0d6 2985 t = true;
6de9cd9a
DN
2986 }
2987 else
2988 {
2989 /* Apply the rules of section 14.1.2. */
2990
20236f90 2991 switch (procedure_kind (sym))
6de9cd9a
DN
2992 {
2993 case PTYPE_GENERIC:
2994 t = resolve_generic_f (expr);
2995 break;
2996
2997 case PTYPE_SPECIFIC:
2998 t = resolve_specific_f (expr);
2999 break;
3000
3001 case PTYPE_UNKNOWN:
3002 t = resolve_unknown_f (expr);
3003 break;
3004
3005 default:
3006 gfc_internal_error ("resolve_function(): bad function type");
3007 }
3008 }
3009
3010 /* If the expression is still a function (it might have simplified),
3011 then we check to see if we are calling an elemental function. */
3012
3013 if (expr->expr_type != EXPR_FUNCTION)
3014 return t;
3015
48474141
PT
3016 temp = need_full_assumed_size;
3017 need_full_assumed_size = 0;
3018
524af0d6
JB
3019 if (!resolve_elemental_actual (expr, NULL))
3020 return false;
48474141 3021
6c7a4dfd
JJ
3022 if (omp_workshare_flag
3023 && expr->value.function.esym
3024 && ! gfc_elemental (expr->value.function.esym))
3025 {
c4100eae 3026 gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
edf1eac2 3027 "in WORKSHARE construct", expr->value.function.esym->name,
6c7a4dfd 3028 &expr->where);
524af0d6 3029 t = false;
6c7a4dfd 3030 }
6de9cd9a 3031
cd5ecab6 3032#define GENERIC_ID expr->value.function.isym->id
48474141 3033 else if (expr->value.function.actual != NULL
edf1eac2
SK
3034 && expr->value.function.isym != NULL
3035 && GENERIC_ID != GFC_ISYM_LBOUND
2c060879
TB
3036 && GENERIC_ID != GFC_ISYM_LCOBOUND
3037 && GENERIC_ID != GFC_ISYM_UCOBOUND
edf1eac2
SK
3038 && GENERIC_ID != GFC_ISYM_LEN
3039 && GENERIC_ID != GFC_ISYM_LOC
cadddfdd 3040 && GENERIC_ID != GFC_ISYM_C_LOC
edf1eac2 3041 && GENERIC_ID != GFC_ISYM_PRESENT)
48474141 3042 {
fa951694 3043 /* Array intrinsics must also have the last upper bound of an
b82feea5 3044 assumed size array argument. UBOUND and SIZE have to be
48474141
PT
3045 excluded from the check if the second argument is anything
3046 than a constant. */
05c1e3a7 3047
48474141
PT
3048 for (arg = expr->value.function.actual; arg; arg = arg->next)
3049 {
7a687b22 3050 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
1634e53f 3051 && arg == expr->value.function.actual
7a687b22 3052 && arg->next != NULL && arg->next->expr)
9ebe2d22
PT
3053 {
3054 if (arg->next->expr->expr_type != EXPR_CONSTANT)
3055 break;
3056
524af0d6 3057 if (arg->next->name && strncmp (arg->next->name, "kind", 4) == 0)
7a687b22
TB
3058 break;
3059
9ebe2d22
PT
3060 if ((int)mpz_get_si (arg->next->expr->value.integer)
3061 < arg->expr->rank)
3062 break;
3063 }
05c1e3a7 3064
48474141 3065 if (arg->expr != NULL
edf1eac2
SK
3066 && arg->expr->rank > 0
3067 && resolve_assumed_size_actual (arg->expr))
524af0d6 3068 return false;
48474141
PT
3069 }
3070 }
4d4074e4 3071#undef GENERIC_ID
48474141
PT
3072
3073 need_full_assumed_size = temp;
3074
5930876d
JW
3075 if (!check_pure_function(expr))
3076 t = false;
f1f39033 3077
77f131ca
FXC
3078 /* Functions without the RECURSIVE attribution are not allowed to
3079 * call themselves. */
3080 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3081 {
1933ba0f 3082 gfc_symbol *esym;
77f131ca 3083 esym = expr->value.function.esym;
77f131ca 3084
1933ba0f 3085 if (is_illegal_recursion (esym, gfc_current_ns))
77f131ca 3086 {
1933ba0f 3087 if (esym->attr.entry && esym->ns->entries)
c4100eae
MLI
3088 gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3089 " function %qs is not RECURSIVE",
1933ba0f
DK
3090 esym->name, &expr->where, esym->ns->entries->sym->name);
3091 else
c4100eae 3092 gfc_error ("Function %qs at %L cannot be called recursively, as it"
1933ba0f
DK
3093 " is not RECURSIVE", esym->name, &expr->where);
3094
524af0d6 3095 t = false;
77f131ca
FXC
3096 }
3097 }
3098
47992a4a
EE
3099 /* Character lengths of use associated functions may contains references to
3100 symbols not referenced from the current program unit otherwise. Make sure
3101 those symbols are marked as referenced. */
3102
05c1e3a7 3103 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
47992a4a
EE
3104 && expr->value.function.esym->attr.use_assoc)
3105 {
bc21d315 3106 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
47992a4a
EE
3107 }
3108
9ebe2d22
PT
3109 /* Make sure that the expression has a typespec that works. */
3110 if (expr->ts.type == BT_UNKNOWN)
3111 {
3112 if (expr->symtree->n.sym->result
3070bab4
JW
3113 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3114 && !expr->symtree->n.sym->result->attr.proc_pointer)
9ebe2d22 3115 expr->ts = expr->symtree->n.sym->result->ts;
9ebe2d22
PT
3116 }
3117
30c931de
PT
3118 if (!expr->ref && !expr->value.function.isym)
3119 {
3120 if (expr->value.function.esym)
3121 update_current_proc_array_outer_dependency (expr->value.function.esym);
3122 else
3123 update_current_proc_array_outer_dependency (sym);
3124 }
3125 else if (expr->ref)
3126 /* typebound procedure: Assume the worst. */
3127 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3128
6de9cd9a
DN
3129 return t;
3130}
3131
3132
3133/************* Subroutine resolution *************/
3134
5930876d
JW
3135static bool
3136pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
6de9cd9a 3137{
6de9cd9a 3138 if (gfc_pure (sym))
5930876d 3139 return true;
6de9cd9a
DN
3140
3141 if (forall_flag)
5930876d
JW
3142 {
3143 gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3144 name, loc);
3145 return false;
3146 }
ce96d372 3147 else if (gfc_do_concurrent_flag)
5930876d
JW
3148 {
3149 gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3150 "PURE", name, loc);
3151 return false;
3152 }
6de9cd9a 3153 else if (gfc_pure (NULL))
5930876d
JW
3154 {
3155 gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3156 return false;
3157 }
3d2cea8c 3158
ccd7751b 3159 gfc_unset_implicit_pure (NULL);
5930876d 3160 return true;
6de9cd9a
DN
3161}
3162
3163
3164static match
edf1eac2 3165resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
3166{
3167 gfc_symbol *s;
3168
3169 if (sym->attr.generic)
3170 {
3171 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3172 if (s != NULL)
3173 {
edf1eac2 3174 c->resolved_sym = s;
5930876d
JW
3175 if (!pure_subroutine (s, s->name, &c->loc))
3176 return MATCH_ERROR;
6de9cd9a
DN
3177 return MATCH_YES;
3178 }
3179
3180 /* TODO: Need to search for elemental references in generic interface. */
3181 }
3182
3183 if (sym->attr.intrinsic)
3184 return gfc_intrinsic_sub_interface (c, 0);
3185
3186 return MATCH_NO;
3187}
3188
3189
524af0d6 3190static bool
edf1eac2 3191resolve_generic_s (gfc_code *c)
6de9cd9a
DN
3192{
3193 gfc_symbol *sym;
3194 match m;
3195
3196 sym = c->symtree->n.sym;
3197
8c086c9c 3198 for (;;)
6de9cd9a 3199 {
8c086c9c
PT
3200 m = resolve_generic_s0 (c, sym);
3201 if (m == MATCH_YES)
524af0d6 3202 return true;
8c086c9c 3203 else if (m == MATCH_ERROR)
524af0d6 3204 return false;
8c086c9c
PT
3205
3206generic:
3207 if (sym->ns->parent == NULL)
3208 break;
6de9cd9a 3209 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
8c086c9c
PT
3210
3211 if (sym == NULL)
3212 break;
3213 if (!generic_sym (sym))
3214 goto generic;
6de9cd9a
DN
3215 }
3216
71f77fd7
PT
3217 /* Last ditch attempt. See if the reference is to an intrinsic
3218 that possesses a matching interface. 14.1.2.4 */
8c086c9c 3219 sym = c->symtree->n.sym;
71f77fd7 3220
c3005b0f 3221 if (!gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a 3222 {
c4100eae 3223 gfc_error ("There is no specific subroutine for the generic %qs at %L",
edf1eac2 3224 sym->name, &c->loc);
524af0d6 3225 return false;
6de9cd9a
DN
3226 }
3227
3228 m = gfc_intrinsic_sub_interface (c, 0);
3229 if (m == MATCH_YES)
524af0d6 3230 return true;
6de9cd9a 3231 if (m == MATCH_NO)
c4100eae 3232 gfc_error ("Generic subroutine %qs at %L is not consistent with an "
6de9cd9a
DN
3233 "intrinsic subroutine interface", sym->name, &c->loc);
3234
524af0d6 3235 return false;
6de9cd9a
DN
3236}
3237
3238
3239/* Resolve a subroutine call known to be specific. */
3240
3241static match
edf1eac2 3242resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
3243{
3244 match m;
3245
3246 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3247 {
3248 if (sym->attr.dummy)
3249 {
3250 sym->attr.proc = PROC_DUMMY;
3251 goto found;
3252 }
3253
3254 sym->attr.proc = PROC_EXTERNAL;
3255 goto found;
3256 }
3257
3258 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3259 goto found;
3260
3261 if (sym->attr.intrinsic)
3262 {
3263 m = gfc_intrinsic_sub_interface (c, 1);
3264 if (m == MATCH_YES)
3265 return MATCH_YES;
3266 if (m == MATCH_NO)
c4100eae 3267 gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
6de9cd9a
DN
3268 "with an intrinsic", sym->name, &c->loc);
3269
3270 return MATCH_ERROR;
3271 }
3272
3273 return MATCH_NO;
3274
3275found:
3276 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3277
3278 c->resolved_sym = sym;
5930876d
JW
3279 if (!pure_subroutine (sym, sym->name, &c->loc))
3280 return MATCH_ERROR;
6de9cd9a
DN
3281
3282 return MATCH_YES;
3283}
3284
3285
524af0d6 3286static bool
edf1eac2 3287resolve_specific_s (gfc_code *c)
6de9cd9a
DN
3288{
3289 gfc_symbol *sym;
3290 match m;
3291
3292 sym = c->symtree->n.sym;
3293
8c086c9c 3294 for (;;)
6de9cd9a
DN
3295 {
3296 m = resolve_specific_s0 (c, sym);
3297 if (m == MATCH_YES)
524af0d6 3298 return true;
6de9cd9a 3299 if (m == MATCH_ERROR)
524af0d6 3300 return false;
8c086c9c
PT
3301
3302 if (sym->ns->parent == NULL)
3303 break;
3304
3305 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3306
3307 if (sym == NULL)
3308 break;
6de9cd9a
DN
3309 }
3310
8c086c9c 3311 sym = c->symtree->n.sym;
c4100eae 3312 gfc_error ("Unable to resolve the specific subroutine %qs at %L",
6de9cd9a
DN
3313 sym->name, &c->loc);
3314
524af0d6 3315 return false;
6de9cd9a
DN
3316}
3317
3318
3319/* Resolve a subroutine call not known to be generic nor specific. */
3320
524af0d6 3321static bool
edf1eac2 3322resolve_unknown_s (gfc_code *c)
6de9cd9a
DN
3323{
3324 gfc_symbol *sym;
3325
3326 sym = c->symtree->n.sym;
3327
3328 if (sym->attr.dummy)
3329 {
3330 sym->attr.proc = PROC_DUMMY;
3331 goto found;
3332 }
3333
3334 /* See if we have an intrinsic function reference. */
3335
c3005b0f 3336 if (gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a
DN
3337 {
3338 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
524af0d6
JB
3339 return true;
3340 return false;
6de9cd9a
DN
3341 }
3342
3343 /* The reference is to an external name. */
3344
3345found:
3346 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3347
3348 c->resolved_sym = sym;
3349
5930876d 3350 return pure_subroutine (sym, sym->name, &c->loc);
6de9cd9a
DN
3351}
3352
3353
3354/* Resolve a subroutine call. Although it was tempting to use the same code
3355 for functions, subroutines and functions are stored differently and this
3356 makes things awkward. */
3357
524af0d6 3358static bool
edf1eac2 3359resolve_call (gfc_code *c)
6de9cd9a 3360{
524af0d6 3361 bool t;
7fcafa71 3362 procedure_type ptype = PROC_INTRINSIC;
67cec813 3363 gfc_symbol *csym, *sym;
0b4e2af7
PT
3364 bool no_formal_args;
3365
3366 csym = c->symtree ? c->symtree->n.sym : NULL;
6de9cd9a 3367
0b4e2af7 3368 if (csym && csym->ts.type != BT_UNKNOWN)
2ed8d224 3369 {
fea70c99 3370 gfc_error ("%qs at %L has a type, which is not consistent with "
0b4e2af7 3371 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
524af0d6 3372 return false;
2ed8d224
PT
3373 }
3374
67cec813
PT
3375 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3376 {
79b1d36c 3377 gfc_symtree *st;
d932cea8 3378 gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
79b1d36c 3379 sym = st ? st->n.sym : NULL;
67cec813
PT
3380 if (sym && csym != sym
3381 && sym->ns == gfc_current_ns
3382 && sym->attr.flavor == FL_PROCEDURE
3383 && sym->attr.contained)
3384 {
3385 sym->refs++;
79b1d36c
PT
3386 if (csym->attr.generic)
3387 c->symtree->n.sym = sym;
3388 else
3389 c->symtree = st;
3390 csym = c->symtree->n.sym;
67cec813
PT
3391 }
3392 }
3393
fdb1fa9e
JW
3394 /* If this ia a deferred TBP, c->expr1 will be set. */
3395 if (!c->expr1 && csym)
8bae6273 3396 {
fdb1fa9e
JW
3397 if (csym->attr.abstract)
3398 {
c4100eae 3399 gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
fdb1fa9e 3400 csym->name, &c->loc);
524af0d6 3401 return false;
fdb1fa9e 3402 }
8bae6273 3403
fdb1fa9e
JW
3404 /* Subroutines without the RECURSIVE attribution are not allowed to
3405 call themselves. */
3406 if (is_illegal_recursion (csym, gfc_current_ns))
3407 {
3408 if (csym->attr.entry && csym->ns->entries)
c4100eae
MLI
3409 gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3410 "as subroutine %qs is not RECURSIVE",
fdb1fa9e
JW
3411 csym->name, &c->loc, csym->ns->entries->sym->name);
3412 else
c4100eae 3413 gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
fdb1fa9e 3414 "as it is not RECURSIVE", csym->name, &c->loc);
1933ba0f 3415
524af0d6 3416 t = false;
fdb1fa9e 3417 }
77f131ca
FXC
3418 }
3419
48474141
PT
3420 /* Switch off assumed size checking and do this again for certain kinds
3421 of procedure, once the procedure itself is resolved. */
3422 need_full_assumed_size++;
3423
0b4e2af7
PT
3424 if (csym)
3425 ptype = csym->attr.proc;
7fcafa71 3426
4cbc9039
JW
3427 no_formal_args = csym && is_external_proc (csym)
3428 && gfc_sym_get_dummy_args (csym) == NULL;
524af0d6
JB
3429 if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3430 return false;
6de9cd9a 3431
66e4ab31 3432 /* Resume assumed_size checking. */
48474141
PT
3433 need_full_assumed_size--;
3434
71a7778c
PT
3435 /* If external, check for usage. */
3436 if (csym && is_external_proc (csym))
3437 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3438
524af0d6 3439 t = true;
1524f80b 3440 if (c->resolved_sym == NULL)
12f681a0
DK
3441 {
3442 c->resolved_isym = NULL;
3443 switch (procedure_kind (csym))
3444 {
3445 case PTYPE_GENERIC:
3446 t = resolve_generic_s (c);
3447 break;
6de9cd9a 3448
12f681a0
DK
3449 case PTYPE_SPECIFIC:
3450 t = resolve_specific_s (c);
3451 break;
6de9cd9a 3452
12f681a0
DK
3453 case PTYPE_UNKNOWN:
3454 t = resolve_unknown_s (c);
3455 break;
6de9cd9a 3456
12f681a0
DK
3457 default:
3458 gfc_internal_error ("resolve_subroutine(): bad function type");
3459 }
3460 }
6de9cd9a 3461
b8ea6dbc 3462 /* Some checks of elemental subroutine actual arguments. */
524af0d6
JB
3463 if (!resolve_elemental_actual (NULL, c))
3464 return false;
48474141 3465
30c931de
PT
3466 if (!c->expr1)
3467 update_current_proc_array_outer_dependency (csym);
3468 else
3469 /* Typebound procedure: Assume the worst. */
3470 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3471
6de9cd9a
DN
3472 return t;
3473}
3474
edf1eac2 3475
2c5ed587 3476/* Compare the shapes of two arrays that have non-NULL shapes. If both
524af0d6
JB
3477 op1->shape and op2->shape are non-NULL return true if their shapes
3478 match. If both op1->shape and op2->shape are non-NULL return false
2c5ed587 3479 if their shapes do not match. If either op1->shape or op2->shape is
524af0d6 3480 NULL, return true. */
2c5ed587 3481
524af0d6 3482static bool
edf1eac2 3483compare_shapes (gfc_expr *op1, gfc_expr *op2)
2c5ed587 3484{
524af0d6 3485 bool t;
2c5ed587
SK
3486 int i;
3487
524af0d6 3488 t = true;
05c1e3a7 3489
2c5ed587
SK
3490 if (op1->shape != NULL && op2->shape != NULL)
3491 {
3492 for (i = 0; i < op1->rank; i++)
3493 {
3494 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3495 {
fea70c99
MLI
3496 gfc_error ("Shapes for operands at %L and %L are not conformable",
3497 &op1->where, &op2->where);
524af0d6 3498 t = false;
2c5ed587
SK
3499 break;
3500 }
3501 }
3502 }
3503
3504 return t;
3505}
6de9cd9a 3506
edf1eac2 3507
6de9cd9a
DN
3508/* Resolve an operator expression node. This can involve replacing the
3509 operation with a user defined function call. */
3510
524af0d6 3511static bool
edf1eac2 3512resolve_operator (gfc_expr *e)
6de9cd9a
DN
3513{
3514 gfc_expr *op1, *op2;
3515 char msg[200];
27189292 3516 bool dual_locus_error;
524af0d6 3517 bool t;
6de9cd9a
DN
3518
3519 /* Resolve all subnodes-- give them types. */
3520
a1ee985f 3521 switch (e->value.op.op)
6de9cd9a
DN
3522 {
3523 default:
524af0d6
JB
3524 if (!gfc_resolve_expr (e->value.op.op2))
3525 return false;
6de9cd9a
DN
3526
3527 /* Fall through... */
3528
3529 case INTRINSIC_NOT:
3530 case INTRINSIC_UPLUS:
3531 case INTRINSIC_UMINUS:
2414e1d6 3532 case INTRINSIC_PARENTHESES:
524af0d6
JB
3533 if (!gfc_resolve_expr (e->value.op.op1))
3534 return false;
6de9cd9a
DN
3535 break;
3536 }
3537
3538 /* Typecheck the new node. */
3539
58b03ab2
TS
3540 op1 = e->value.op.op1;
3541 op2 = e->value.op.op2;
27189292 3542 dual_locus_error = false;
6de9cd9a 3543
bb9e683e
TB
3544 if ((op1 && op1->expr_type == EXPR_NULL)
3545 || (op2 && op2->expr_type == EXPR_NULL))
3546 {
3547 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3548 goto bad_op;
3549 }
3550
a1ee985f 3551 switch (e->value.op.op)
6de9cd9a
DN
3552 {
3553 case INTRINSIC_UPLUS:
3554 case INTRINSIC_UMINUS:
3555 if (op1->ts.type == BT_INTEGER
3556 || op1->ts.type == BT_REAL
3557 || op1->ts.type == BT_COMPLEX)
3558 {
3559 e->ts = op1->ts;
3560 break;
3561 }
3562
811582ec 3563 sprintf (msg, _("Operand of unary numeric operator %%<%s%%> at %%L is %s"),
a1ee985f 3564 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
6de9cd9a
DN
3565 goto bad_op;
3566
3567 case INTRINSIC_PLUS:
3568 case INTRINSIC_MINUS:
3569 case INTRINSIC_TIMES:
3570 case INTRINSIC_DIVIDE:
3571 case INTRINSIC_POWER:
3572 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3573 {
dcea1b2f 3574 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3575 break;
3576 }
3577
3578 sprintf (msg,
811582ec 3579 _("Operands of binary numeric operator %%<%s%%> at %%L are %s/%s"),
a1ee985f 3580 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3581 gfc_typename (&op2->ts));
3582 goto bad_op;
3583
3584 case INTRINSIC_CONCAT:
d393bbd7
FXC
3585 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3586 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3587 {
3588 e->ts.type = BT_CHARACTER;
3589 e->ts.kind = op1->ts.kind;
3590 break;
3591 }
3592
3593 sprintf (msg,
31043f6c 3594 _("Operands of string concatenation operator at %%L are %s/%s"),
6de9cd9a
DN
3595 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3596 goto bad_op;
3597
3598 case INTRINSIC_AND:
3599 case INTRINSIC_OR:
3600 case INTRINSIC_EQV:
3601 case INTRINSIC_NEQV:
3602 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3603 {
3604 e->ts.type = BT_LOGICAL;
3605 e->ts.kind = gfc_kind_max (op1, op2);
edf1eac2
SK
3606 if (op1->ts.kind < e->ts.kind)
3607 gfc_convert_type (op1, &e->ts, 2);
3608 else if (op2->ts.kind < e->ts.kind)
3609 gfc_convert_type (op2, &e->ts, 2);
6de9cd9a
DN
3610 break;
3611 }
3612
811582ec 3613 sprintf (msg, _("Operands of logical operator %%<%s%%> at %%L are %s/%s"),
a1ee985f 3614 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3615 gfc_typename (&op2->ts));
3616
3617 goto bad_op;
3618
3619 case INTRINSIC_NOT:
3620 if (op1->ts.type == BT_LOGICAL)
3621 {
3622 e->ts.type = BT_LOGICAL;
3623 e->ts.kind = op1->ts.kind;
3624 break;
3625 }
3626
3bed9dd0 3627 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
6de9cd9a
DN
3628 gfc_typename (&op1->ts));
3629 goto bad_op;
3630
3631 case INTRINSIC_GT:
3bed9dd0 3632 case INTRINSIC_GT_OS:
6de9cd9a 3633 case INTRINSIC_GE:
3bed9dd0 3634 case INTRINSIC_GE_OS:
6de9cd9a 3635 case INTRINSIC_LT:
3bed9dd0 3636 case INTRINSIC_LT_OS:
6de9cd9a 3637 case INTRINSIC_LE:
3bed9dd0 3638 case INTRINSIC_LE_OS:
6de9cd9a
DN
3639 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3640 {
31043f6c 3641 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
6de9cd9a
DN
3642 goto bad_op;
3643 }
3644
3645 /* Fall through... */
3646
3647 case INTRINSIC_EQ:
3bed9dd0 3648 case INTRINSIC_EQ_OS:
6de9cd9a 3649 case INTRINSIC_NE:
3bed9dd0 3650 case INTRINSIC_NE_OS:
d393bbd7
FXC
3651 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3652 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3653 {
3654 e->ts.type = BT_LOGICAL;
9d64df18 3655 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3656 break;
3657 }
3658
3659 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3660 {
dcea1b2f 3661 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3662
3663 e->ts.type = BT_LOGICAL;
9d64df18 3664 e->ts.kind = gfc_default_logical_kind;
cf21551e 3665
73e42eef 3666 if (warn_compare_reals)
cf21551e
TK
3667 {
3668 gfc_intrinsic_op op = e->value.op.op;
3669
3670 /* Type conversion has made sure that the types of op1 and op2
3671 agree, so it is only necessary to check the first one. */
3672 if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
3673 && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
3674 || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
3675 {
3676 const char *msg;
3677
3678 if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
3679 msg = "Equality comparison for %s at %L";
3680 else
3681 msg = "Inequality comparison for %s at %L";
4d382327 3682
db30e21c 3683 gfc_warning (0, msg, gfc_typename (&op1->ts), &op1->where);
cf21551e
TK
3684 }
3685 }
3686
6de9cd9a
DN
3687 break;
3688 }
3689
6a28f513 3690 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
31043f6c 3691 sprintf (msg,
edf1eac2 3692 _("Logicals at %%L must be compared with %s instead of %s"),
4d382327 3693 (e->value.op.op == INTRINSIC_EQ
a1ee985f
KG
3694 || e->value.op.op == INTRINSIC_EQ_OS)
3695 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
6a28f513 3696 else
31043f6c 3697 sprintf (msg,
811582ec 3698 _("Operands of comparison operator %%<%s%%> at %%L are %s/%s"),
a1ee985f 3699 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6a28f513 3700 gfc_typename (&op2->ts));
6de9cd9a
DN
3701
3702 goto bad_op;
3703
3704 case INTRINSIC_USER:
a1ee985f 3705 if (e->value.op.uop->op == NULL)
811582ec
TB
3706 sprintf (msg, _("Unknown operator %%<%s%%> at %%L"),
3707 e->value.op.uop->name);
622af87f 3708 else if (op2 == NULL)
811582ec 3709 sprintf (msg, _("Operand of user operator %%<%s%%> at %%L is %s"),
58b03ab2 3710 e->value.op.uop->name, gfc_typename (&op1->ts));
6de9cd9a 3711 else
7c1a49fa 3712 {
811582ec 3713 sprintf (msg, _("Operands of user operator %%<%s%%> at %%L are %s/%s"),
7c1a49fa
TK
3714 e->value.op.uop->name, gfc_typename (&op1->ts),
3715 gfc_typename (&op2->ts));
3716 e->value.op.uop->op->sym->attr.referenced = 1;
3717 }
6de9cd9a
DN
3718
3719 goto bad_op;
3720
2414e1d6 3721 case INTRINSIC_PARENTHESES:
dcdc83a1
TS
3722 e->ts = op1->ts;
3723 if (e->ts.type == BT_CHARACTER)
bc21d315 3724 e->ts.u.cl = op1->ts.u.cl;
2414e1d6
TS
3725 break;
3726
6de9cd9a
DN
3727 default:
3728 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3729 }
3730
3731 /* Deal with arrayness of an operand through an operator. */
3732
524af0d6 3733 t = true;
6de9cd9a 3734
a1ee985f 3735 switch (e->value.op.op)
6de9cd9a
DN
3736 {
3737 case INTRINSIC_PLUS:
3738 case INTRINSIC_MINUS:
3739 case INTRINSIC_TIMES:
3740 case INTRINSIC_DIVIDE:
3741 case INTRINSIC_POWER:
3742 case INTRINSIC_CONCAT:
3743 case INTRINSIC_AND:
3744 case INTRINSIC_OR:
3745 case INTRINSIC_EQV:
3746 case INTRINSIC_NEQV:
3747 case INTRINSIC_EQ:
3bed9dd0 3748 case INTRINSIC_EQ_OS:
6de9cd9a 3749 case INTRINSIC_NE:
3bed9dd0 3750 case INTRINSIC_NE_OS:
6de9cd9a 3751 case INTRINSIC_GT:
3bed9dd0 3752 case INTRINSIC_GT_OS:
6de9cd9a 3753 case INTRINSIC_GE:
3bed9dd0 3754 case INTRINSIC_GE_OS:
6de9cd9a 3755 case INTRINSIC_LT:
3bed9dd0 3756 case INTRINSIC_LT_OS:
6de9cd9a 3757 case INTRINSIC_LE:
3bed9dd0 3758 case INTRINSIC_LE_OS:
6de9cd9a
DN
3759
3760 if (op1->rank == 0 && op2->rank == 0)
3761 e->rank = 0;
3762
3763 if (op1->rank == 0 && op2->rank != 0)
3764 {
3765 e->rank = op2->rank;
3766
3767 if (e->shape == NULL)
3768 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3769 }
3770
3771 if (op1->rank != 0 && op2->rank == 0)
3772 {
3773 e->rank = op1->rank;
3774
3775 if (e->shape == NULL)
3776 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3777 }
3778
3779 if (op1->rank != 0 && op2->rank != 0)
3780 {
3781 if (op1->rank == op2->rank)
3782 {
3783 e->rank = op1->rank;
6de9cd9a 3784 if (e->shape == NULL)
2c5ed587 3785 {
d1d7b044 3786 t = compare_shapes (op1, op2);
524af0d6 3787 if (!t)
2c5ed587
SK
3788 e->shape = NULL;
3789 else
d1d7b044 3790 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2c5ed587 3791 }
6de9cd9a
DN
3792 }
3793 else
3794 {
edf1eac2 3795 /* Allow higher level expressions to work. */
6de9cd9a 3796 e->rank = 0;
27189292
FXC
3797
3798 /* Try user-defined operators, and otherwise throw an error. */
3799 dual_locus_error = true;
3800 sprintf (msg,
3801 _("Inconsistent ranks for operator at %%L and %%L"));
3802 goto bad_op;
6de9cd9a
DN
3803 }
3804 }
3805
3806 break;
3807
08113c73 3808 case INTRINSIC_PARENTHESES:
6de9cd9a
DN
3809 case INTRINSIC_NOT:
3810 case INTRINSIC_UPLUS:
3811 case INTRINSIC_UMINUS:
08113c73 3812 /* Simply copy arrayness attribute */
6de9cd9a
DN
3813 e->rank = op1->rank;
3814
3815 if (e->shape == NULL)
3816 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3817
6de9cd9a
DN
3818 break;
3819
3820 default:
3821 break;
3822 }
3823
3824 /* Attempt to simplify the expression. */
524af0d6 3825 if (t)
dd5ecf41
PT
3826 {
3827 t = gfc_simplify_expr (e, 0);
524af0d6 3828 /* Some calls do not succeed in simplification and return false
df2fba9e 3829 even though there is no error; e.g. variable references to
dd5ecf41
PT
3830 PARAMETER arrays. */
3831 if (!gfc_is_constant_expr (e))
524af0d6 3832 t = true;
dd5ecf41 3833 }
6de9cd9a
DN
3834 return t;
3835
3836bad_op:
2c5ed587 3837
4a44a72d 3838 {
eaee02a5
JW
3839 match m = gfc_extend_expr (e);
3840 if (m == MATCH_YES)
524af0d6 3841 return true;
eaee02a5 3842 if (m == MATCH_ERROR)
524af0d6 3843 return false;
4a44a72d 3844 }
6de9cd9a 3845
27189292
FXC
3846 if (dual_locus_error)
3847 gfc_error (msg, &op1->where, &op2->where);
3848 else
3849 gfc_error (msg, &e->where);
2c5ed587 3850
524af0d6 3851 return false;
6de9cd9a
DN
3852}
3853
3854
3855/************** Array resolution subroutines **************/
3856
a79683d5
TS
3857enum compare_result
3858{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
6de9cd9a
DN
3859
3860/* Compare two integer expressions. */
3861
ff5ed3f6 3862static compare_result
edf1eac2 3863compare_bound (gfc_expr *a, gfc_expr *b)
6de9cd9a
DN
3864{
3865 int i;
3866
3867 if (a == NULL || a->expr_type != EXPR_CONSTANT
3868 || b == NULL || b->expr_type != EXPR_CONSTANT)
3869 return CMP_UNKNOWN;
3870
df80a455
TK
3871 /* If either of the types isn't INTEGER, we must have
3872 raised an error earlier. */
3873
6de9cd9a 3874 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
df80a455 3875 return CMP_UNKNOWN;
6de9cd9a
DN
3876
3877 i = mpz_cmp (a->value.integer, b->value.integer);
3878
3879 if (i < 0)
3880 return CMP_LT;
3881 if (i > 0)
3882 return CMP_GT;
3883 return CMP_EQ;
3884}
3885
3886
3887/* Compare an integer expression with an integer. */
3888
ff5ed3f6 3889static compare_result
edf1eac2 3890compare_bound_int (gfc_expr *a, int b)
6de9cd9a
DN
3891{
3892 int i;
3893
3894 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3895 return CMP_UNKNOWN;
3896
3897 if (a->ts.type != BT_INTEGER)
3898 gfc_internal_error ("compare_bound_int(): Bad expression");
3899
3900 i = mpz_cmp_si (a->value.integer, b);
3901
3902 if (i < 0)
3903 return CMP_LT;
3904 if (i > 0)
3905 return CMP_GT;
3906 return CMP_EQ;
3907}
3908
3909
0094f362
FXC
3910/* Compare an integer expression with a mpz_t. */
3911
ff5ed3f6 3912static compare_result
edf1eac2 3913compare_bound_mpz_t (gfc_expr *a, mpz_t b)
0094f362
FXC
3914{
3915 int i;
3916
3917 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3918 return CMP_UNKNOWN;
3919
3920 if (a->ts.type != BT_INTEGER)
3921 gfc_internal_error ("compare_bound_int(): Bad expression");
3922
3923 i = mpz_cmp (a->value.integer, b);
3924
3925 if (i < 0)
3926 return CMP_LT;
3927 if (i > 0)
3928 return CMP_GT;
3929 return CMP_EQ;
3930}
3931
3932
4d382327 3933/* Compute the last value of a sequence given by a triplet.
0094f362
FXC
3934 Return 0 if it wasn't able to compute the last value, or if the
3935 sequence if empty, and 1 otherwise. */
3936
3937static int
edf1eac2
SK
3938compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3939 gfc_expr *stride, mpz_t last)
0094f362
FXC
3940{
3941 mpz_t rem;
3942
3943 if (start == NULL || start->expr_type != EXPR_CONSTANT
3944 || end == NULL || end->expr_type != EXPR_CONSTANT
3945 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3946 return 0;
3947
3948 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3949 || (stride != NULL && stride->ts.type != BT_INTEGER))
3950 return 0;
3951
524af0d6 3952 if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
0094f362
FXC
3953 {
3954 if (compare_bound (start, end) == CMP_GT)
3955 return 0;
3956 mpz_set (last, end->value.integer);
3957 return 1;
3958 }
05c1e3a7 3959
0094f362
FXC
3960 if (compare_bound_int (stride, 0) == CMP_GT)
3961 {
3962 /* Stride is positive */
3963 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3964 return 0;
3965 }
3966 else
3967 {
3968 /* Stride is negative */
3969 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3970 return 0;
3971 }
3972
3973 mpz_init (rem);
3974 mpz_sub (rem, end->value.integer, start->value.integer);
3975 mpz_tdiv_r (rem, rem, stride->value.integer);
3976 mpz_sub (last, end->value.integer, rem);
3977 mpz_clear (rem);
3978
3979 return 1;
3980}
3981
3982
6de9cd9a
DN
3983/* Compare a single dimension of an array reference to the array
3984 specification. */
3985
524af0d6 3986static bool
edf1eac2 3987check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
6de9cd9a 3988{
0094f362 3989 mpz_t last_value;
6de9cd9a 3990
d3a9eea2
TB
3991 if (ar->dimen_type[i] == DIMEN_STAR)
3992 {
3993 gcc_assert (ar->stride[i] == NULL);
3994 /* This implies [*] as [*:] and [*:3] are not possible. */
3995 if (ar->start[i] == NULL)
3996 {
3997 gcc_assert (ar->end[i] == NULL);
524af0d6 3998 return true;
d3a9eea2
TB
3999 }
4000 }
4001
6de9cd9a 4002/* Given start, end and stride values, calculate the minimum and
f7b529fa 4003 maximum referenced indexes. */
6de9cd9a 4004
1954a27b 4005 switch (ar->dimen_type[i])
6de9cd9a 4006 {
1954a27b 4007 case DIMEN_VECTOR:
a3935ffc 4008 case DIMEN_THIS_IMAGE:
6de9cd9a
DN
4009 break;
4010
d3a9eea2 4011 case DIMEN_STAR:
1954a27b 4012 case DIMEN_ELEMENT:
6de9cd9a 4013 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1954a27b 4014 {
d3a9eea2 4015 if (i < as->rank)
db30e21c 4016 gfc_warning (0, "Array reference at %L is out of bounds "
d3a9eea2
TB
4017 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4018 mpz_get_si (ar->start[i]->value.integer),
4019 mpz_get_si (as->lower[i]->value.integer), i+1);
4020 else
db30e21c 4021 gfc_warning (0, "Array reference at %L is out of bounds "
d3a9eea2
TB
4022 "(%ld < %ld) in codimension %d", &ar->c_where[i],
4023 mpz_get_si (ar->start[i]->value.integer),
4024 mpz_get_si (as->lower[i]->value.integer),
4025 i + 1 - as->rank);
524af0d6 4026 return true;
1954a27b 4027 }
6de9cd9a 4028 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1954a27b 4029 {
d3a9eea2 4030 if (i < as->rank)
db30e21c 4031 gfc_warning (0, "Array reference at %L is out of bounds "
d3a9eea2
TB
4032 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4033 mpz_get_si (ar->start[i]->value.integer),
4034 mpz_get_si (as->upper[i]->value.integer), i+1);
4035 else
db30e21c 4036 gfc_warning (0, "Array reference at %L is out of bounds "
d3a9eea2
TB
4037 "(%ld > %ld) in codimension %d", &ar->c_where[i],
4038 mpz_get_si (ar->start[i]->value.integer),
4039 mpz_get_si (as->upper[i]->value.integer),
4040 i + 1 - as->rank);
524af0d6 4041 return true;
1954a27b 4042 }
6de9cd9a
DN
4043
4044 break;
4045
1954a27b 4046 case DIMEN_RANGE:
d912240d 4047 {
0094f362
FXC
4048#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
4049#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
4050
ff5ed3f6 4051 compare_result comp_start_end = compare_bound (AR_START, AR_END);
0094f362 4052
d912240d
FXC
4053 /* Check for zero stride, which is not allowed. */
4054 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
4055 {
4056 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
524af0d6 4057 return false;
d912240d
FXC
4058 }
4059
4060 /* if start == len || (stride > 0 && start < len)
4061 || (stride < 0 && start > len),
4062 then the array section contains at least one element. In this
4063 case, there is an out-of-bounds access if
4064 (start < lower || start > upper). */
4065 if (compare_bound (AR_START, AR_END) == CMP_EQ
4066 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
4067 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
4068 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
4069 && comp_start_end == CMP_GT))
4070 {
1954a27b
TB
4071 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
4072 {
db30e21c 4073 gfc_warning (0, "Lower array reference at %L is out of bounds "
1954a27b
TB
4074 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4075 mpz_get_si (AR_START->value.integer),
4076 mpz_get_si (as->lower[i]->value.integer), i+1);
524af0d6 4077 return true;
1954a27b
TB
4078 }
4079 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
4080 {
db30e21c 4081 gfc_warning (0, "Lower array reference at %L is out of bounds "
1954a27b
TB
4082 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4083 mpz_get_si (AR_START->value.integer),
4084 mpz_get_si (as->upper[i]->value.integer), i+1);
524af0d6 4085 return true;
1954a27b 4086 }
d912240d
FXC
4087 }
4088
4089 /* If we can compute the highest index of the array section,
4090 then it also has to be between lower and upper. */
4091 mpz_init (last_value);
4092 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
4093 last_value))
4094 {
1954a27b
TB
4095 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
4096 {
db30e21c 4097 gfc_warning (0, "Upper array reference at %L is out of bounds "
1954a27b
TB
4098 "(%ld < %ld) in dimension %d", &ar->c_where[i],
4099 mpz_get_si (last_value),
4100 mpz_get_si (as->lower[i]->value.integer), i+1);
4101 mpz_clear (last_value);
524af0d6 4102 return true;
1954a27b
TB
4103 }
4104 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
d912240d 4105 {
db30e21c 4106 gfc_warning (0, "Upper array reference at %L is out of bounds "
1954a27b
TB
4107 "(%ld > %ld) in dimension %d", &ar->c_where[i],
4108 mpz_get_si (last_value),
4109 mpz_get_si (as->upper[i]->value.integer), i+1);
d912240d 4110 mpz_clear (last_value);
524af0d6 4111 return true;
d912240d
FXC
4112 }
4113 }
4114 mpz_clear (last_value);
0094f362
FXC
4115
4116#undef AR_START
4117#undef AR_END
d912240d 4118 }
6de9cd9a
DN
4119 break;
4120
4121 default:
4122 gfc_internal_error ("check_dimension(): Bad array reference");
4123 }
4124
524af0d6 4125 return true;
6de9cd9a
DN
4126}
4127
4128
4129/* Compare an array reference with an array specification. */
4130
524af0d6 4131static bool
edf1eac2 4132compare_spec_to_ref (gfc_array_ref *ar)
6de9cd9a
DN
4133{
4134 gfc_array_spec *as;
4135 int i;
4136
4137 as = ar->as;
4138 i = as->rank - 1;
4139 /* TODO: Full array sections are only allowed as actual parameters. */
4140 if (as->type == AS_ASSUMED_SIZE
4141 && (/*ar->type == AR_FULL
edf1eac2
SK
4142 ||*/ (ar->type == AR_SECTION
4143 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
6de9cd9a 4144 {
edf1eac2
SK
4145 gfc_error ("Rightmost upper bound of assumed size array section "
4146 "not specified at %L", &ar->where);
524af0d6 4147 return false;
6de9cd9a
DN
4148 }
4149
4150 if (ar->type == AR_FULL)
524af0d6 4151 return true;
6de9cd9a
DN
4152
4153 if (as->rank != ar->dimen)
4154 {
4155 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
4156 &ar->where, ar->dimen, as->rank);
524af0d6 4157 return false;
6de9cd9a
DN
4158 }
4159
d3a9eea2
TB
4160 /* ar->codimen == 0 is a local array. */
4161 if (as->corank != ar->codimen && ar->codimen != 0)
4162 {
4163 gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
4164 &ar->where, ar->codimen, as->corank);
524af0d6 4165 return false;
d3a9eea2
TB
4166 }
4167
6de9cd9a 4168 for (i = 0; i < as->rank; i++)
524af0d6
JB
4169 if (!check_dimension (i, ar, as))
4170 return false;
6de9cd9a 4171
d3a9eea2
TB
4172 /* Local access has no coarray spec. */
4173 if (ar->codimen != 0)
4174 for (i = as->rank; i < as->rank + as->corank; i++)
4175 {
a3935ffc
TB
4176 if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
4177 && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
d3a9eea2
TB
4178 {
4179 gfc_error ("Coindex of codimension %d must be a scalar at %L",
4180 i + 1 - as->rank, &ar->where);
524af0d6 4181 return false;
d3a9eea2 4182 }
524af0d6
JB
4183 if (!check_dimension (i, ar, as))
4184 return false;
d3a9eea2
TB
4185 }
4186
524af0d6 4187 return true;
6de9cd9a
DN
4188}
4189
4190
4191/* Resolve one part of an array index. */
4192
524af0d6 4193static bool
92375a20
RG
4194gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
4195 int force_index_integer_kind)
6de9cd9a
DN
4196{
4197 gfc_typespec ts;
4198
4199 if (index == NULL)
524af0d6 4200 return true;
6de9cd9a 4201
524af0d6
JB
4202 if (!gfc_resolve_expr (index))
4203 return false;
6de9cd9a 4204
ee943062 4205 if (check_scalar && index->rank != 0)
6de9cd9a 4206 {
ee943062 4207 gfc_error ("Array index at %L must be scalar", &index->where);
524af0d6 4208 return false;
6de9cd9a
DN
4209 }
4210
ee943062 4211 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
6de9cd9a 4212 {
acb388a0
JD
4213 gfc_error ("Array index at %L must be of INTEGER type, found %s",
4214 &index->where, gfc_basic_typename (index->ts.type));
524af0d6 4215 return false;
6de9cd9a
DN
4216 }
4217
ee943062 4218 if (index->ts.type == BT_REAL)
22c23886 4219 if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
524af0d6
JB
4220 &index->where))
4221 return false;
ee943062 4222
92375a20
RG
4223 if ((index->ts.kind != gfc_index_integer_kind
4224 && force_index_integer_kind)
ee943062 4225 || index->ts.type != BT_INTEGER)
6de9cd9a 4226 {
810306f2 4227 gfc_clear_ts (&ts);
6de9cd9a
DN
4228 ts.type = BT_INTEGER;
4229 ts.kind = gfc_index_integer_kind;
4230
4231 gfc_convert_type_warn (index, &ts, 2, 0);
4232 }
4233
524af0d6 4234 return true;
6de9cd9a
DN
4235}
4236
92375a20
RG
4237/* Resolve one part of an array index. */
4238
524af0d6 4239bool
92375a20
RG
4240gfc_resolve_index (gfc_expr *index, int check_scalar)
4241{
4242 return gfc_resolve_index_1 (index, check_scalar, 1);
4243}
4244
bf302220
TK
4245/* Resolve a dim argument to an intrinsic function. */
4246
524af0d6 4247bool
bf302220
TK
4248gfc_resolve_dim_arg (gfc_expr *dim)
4249{
4250 if (dim == NULL)
524af0d6 4251 return true;
bf302220 4252
524af0d6
JB
4253 if (!gfc_resolve_expr (dim))
4254 return false;
bf302220
TK
4255
4256 if (dim->rank != 0)
4257 {
4258 gfc_error ("Argument dim at %L must be scalar", &dim->where);
524af0d6 4259 return false;
05c1e3a7 4260
bf302220 4261 }
33717d59 4262
bf302220
TK
4263 if (dim->ts.type != BT_INTEGER)
4264 {
4265 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
524af0d6 4266 return false;
bf302220 4267 }
33717d59 4268
bf302220
TK
4269 if (dim->ts.kind != gfc_index_integer_kind)
4270 {
4271 gfc_typespec ts;
4272
a79ff094 4273 gfc_clear_ts (&ts);
bf302220
TK
4274 ts.type = BT_INTEGER;
4275 ts.kind = gfc_index_integer_kind;
4276
4277 gfc_convert_type_warn (dim, &ts, 2, 0);
4278 }
4279
524af0d6 4280 return true;
bf302220 4281}
6de9cd9a
DN
4282
4283/* Given an expression that contains array references, update those array
4284 references to point to the right array specifications. While this is
4285 filled in during matching, this information is difficult to save and load
4286 in a module, so we take care of it here.
4287
4288 The idea here is that the original array reference comes from the
4289 base symbol. We traverse the list of reference structures, setting
4290 the stored reference to references. Component references can
4291 provide an additional array specification. */
4292
4293static void
edf1eac2 4294find_array_spec (gfc_expr *e)
6de9cd9a
DN
4295{
4296 gfc_array_spec *as;
4297 gfc_component *c;
4298 gfc_ref *ref;
4299
cf2b3c22 4300 if (e->symtree->n.sym->ts.type == BT_CLASS)
7a08eda1 4301 as = CLASS_DATA (e->symtree->n.sym)->as;
cf2b3c22
TB
4302 else
4303 as = e->symtree->n.sym->as;
6de9cd9a
DN
4304
4305 for (ref = e->ref; ref; ref = ref->next)
4306 switch (ref->type)
4307 {
4308 case REF_ARRAY:
4309 if (as == NULL)
4310 gfc_internal_error ("find_array_spec(): Missing spec");
4311
4312 ref->u.ar.as = as;
4313 as = NULL;
4314 break;
4315
4316 case REF_COMPONENT:
02139671 4317 c = ref->u.c.component;
d4b7d0f0 4318 if (c->attr.dimension)
6de9cd9a
DN
4319 {
4320 if (as != NULL)
4321 gfc_internal_error ("find_array_spec(): unused as(1)");
4322 as = c->as;
4323 }
4324
6de9cd9a
DN
4325 break;
4326
4327 case REF_SUBSTRING:
4328 break;
4329 }
4330
4331 if (as != NULL)
4332 gfc_internal_error ("find_array_spec(): unused as(2)");
4333}
4334
4335
4336/* Resolve an array reference. */
4337
524af0d6 4338static bool
edf1eac2 4339resolve_array_ref (gfc_array_ref *ar)
6de9cd9a
DN
4340{
4341 int i, check_scalar;
b6398823 4342 gfc_expr *e;
6de9cd9a 4343
d3a9eea2 4344 for (i = 0; i < ar->dimen + ar->codimen; i++)
6de9cd9a
DN
4345 {
4346 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4347
92375a20
RG
4348 /* Do not force gfc_index_integer_kind for the start. We can
4349 do fine with any integer kind. This avoids temporary arrays
4350 created for indexing with a vector. */
524af0d6
JB
4351 if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
4352 return false;
4353 if (!gfc_resolve_index (ar->end[i], check_scalar))
4354 return false;
4355 if (!gfc_resolve_index (ar->stride[i], check_scalar))
4356 return false;
6de9cd9a 4357
b6398823
PT
4358 e = ar->start[i];
4359
6de9cd9a 4360 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
b6398823 4361 switch (e->rank)
6de9cd9a
DN
4362 {
4363 case 0:
4364 ar->dimen_type[i] = DIMEN_ELEMENT;
4365 break;
4366
4367 case 1:
4368 ar->dimen_type[i] = DIMEN_VECTOR;
b6398823 4369 if (e->expr_type == EXPR_VARIABLE
edf1eac2 4370 && e->symtree->n.sym->ts.type == BT_DERIVED)
b6398823 4371 ar->start[i] = gfc_get_parentheses (e);
6de9cd9a
DN
4372 break;
4373
4374 default:
4375 gfc_error ("Array index at %L is an array of rank %d",
b6398823 4376 &ar->c_where[i], e->rank);
524af0d6 4377 return false;
6de9cd9a 4378 }
ee247636
TK
4379
4380 /* Fill in the upper bound, which may be lower than the
4381 specified one for something like a(2:10:5), which is
4382 identical to a(2:7:5). Only relevant for strides not equal
2d27cb44 4383 to one. Don't try a division by zero. */
ee247636
TK
4384 if (ar->dimen_type[i] == DIMEN_RANGE
4385 && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
2d27cb44
TK
4386 && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
4387 && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
ee247636
TK
4388 {
4389 mpz_t size, end;
4390
524af0d6 4391 if (gfc_ref_dimen_size (ar, i, &size, &end))
ee247636
TK
4392 {
4393 if (ar->end[i] == NULL)
4394 {
4395 ar->end[i] =
4396 gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
4397 &ar->where);
4398 mpz_set (ar->end[i]->value.integer, end);
4399 }
4400 else if (ar->end[i]->ts.type == BT_INTEGER
4401 && ar->end[i]->expr_type == EXPR_CONSTANT)
4402 {
4403 mpz_set (ar->end[i]->value.integer, end);
4404 }
4405 else
4406 gcc_unreachable ();
4407
4408 mpz_clear (size);
4409 mpz_clear (end);
4410 }
4411 }
6de9cd9a
DN
4412 }
4413
5551a54e
MM
4414 if (ar->type == AR_FULL)
4415 {
4416 if (ar->as->rank == 0)
4417 ar->type = AR_ELEMENT;
4418
4419 /* Make sure array is the same as array(:,:), this way
4420 we don't need to special case all the time. */
4421 ar->dimen = ar->as->rank;
4422 for (i = 0; i < ar->dimen; i++)
4423 {
4424 ar->dimen_type[i] = DIMEN_RANGE;
4425
4426 gcc_assert (ar->start[i] == NULL);
4427 gcc_assert (ar->end[i] == NULL);
4428 gcc_assert (ar->stride[i] == NULL);
4429 }
4430 }
d3a9eea2 4431
6de9cd9a
DN
4432 /* If the reference type is unknown, figure out what kind it is. */
4433
4434 if (ar->type == AR_UNKNOWN)
4435 {
4436 ar->type = AR_ELEMENT;
4437 for (i = 0; i < ar->dimen; i++)
4438 if (ar->dimen_type[i] == DIMEN_RANGE
4439 || ar->dimen_type[i] == DIMEN_VECTOR)
4440 {
4441 ar->type = AR_SECTION;
4442 break;
4443 }
4444 }
4445
524af0d6
JB
4446 if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
4447 return false;
6de9cd9a 4448
b78a06b2
MM
4449 if (ar->as->corank && ar->codimen == 0)
4450 {
4451 int n;
4452 ar->codimen = ar->as->corank;
4453 for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
4454 ar->dimen_type[n] = DIMEN_THIS_IMAGE;
4455 }
4456
524af0d6 4457 return true;
6de9cd9a
DN
4458}
4459
4460
524af0d6 4461static bool
edf1eac2 4462resolve_substring (gfc_ref *ref)
6de9cd9a 4463{
b0c06816
FXC
4464 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4465
6de9cd9a
DN
4466 if (ref->u.ss.start != NULL)
4467 {
524af0d6
JB
4468 if (!gfc_resolve_expr (ref->u.ss.start))
4469 return false;
6de9cd9a
DN
4470
4471 if (ref->u.ss.start->ts.type != BT_INTEGER)
4472 {
4473 gfc_error ("Substring start index at %L must be of type INTEGER",
4474 &ref->u.ss.start->where);
524af0d6 4475 return false;
6de9cd9a
DN
4476 }
4477
4478 if (ref->u.ss.start->rank != 0)
4479 {
4480 gfc_error ("Substring start index at %L must be scalar",
4481 &ref->u.ss.start->where);
524af0d6 4482 return false;
6de9cd9a
DN
4483 }
4484
97bca513
FXC
4485 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4486 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4487 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a
DN
4488 {
4489 gfc_error ("Substring start index at %L is less than one",
4490 &ref->u.ss.start->where);
524af0d6 4491 return false;
6de9cd9a
DN
4492 }
4493 }
4494
4495 if (ref->u.ss.end != NULL)
4496 {
524af0d6
JB
4497 if (!gfc_resolve_expr (ref->u.ss.end))
4498 return false;
6de9cd9a
DN
4499
4500 if (ref->u.ss.end->ts.type != BT_INTEGER)
4501 {
4502 gfc_error ("Substring end index at %L must be of type INTEGER",
4503 &ref->u.ss.end->where);
524af0d6 4504 return false;
6de9cd9a
DN
4505 }
4506
4507 if (ref->u.ss.end->rank != 0)
4508 {
4509 gfc_error ("Substring end index at %L must be scalar",
4510 &ref->u.ss.end->where);
524af0d6 4511 return false;
6de9cd9a
DN
4512 }
4513
4514 if (ref->u.ss.length != NULL
97bca513
FXC
4515 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4516 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4517 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a 4518 {
97bca513 4519 gfc_error ("Substring end index at %L exceeds the string length",
6de9cd9a 4520 &ref->u.ss.start->where);
524af0d6 4521 return false;
6de9cd9a 4522 }
b0c06816
FXC
4523
4524 if (compare_bound_mpz_t (ref->u.ss.end,
4525 gfc_integer_kinds[k].huge) == CMP_GT
4526 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4527 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4528 {
4529 gfc_error ("Substring end index at %L is too large",
4530 &ref->u.ss.end->where);
524af0d6 4531 return false;
b0c06816 4532 }
6de9cd9a
DN
4533 }
4534
524af0d6 4535 return true;
6de9cd9a
DN
4536}
4537
4538
07368af0
PT
4539/* This function supplies missing substring charlens. */
4540
4541void
4542gfc_resolve_substring_charlen (gfc_expr *e)
4543{
4544 gfc_ref *char_ref;
4545 gfc_expr *start, *end;
58864d1c 4546 gfc_typespec *ts = NULL;
07368af0
PT
4547
4548 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
58864d1c
LK
4549 {
4550 if (char_ref->type == REF_SUBSTRING)
4551 break;
4552 if (char_ref->type == REF_COMPONENT)
4553 ts = &char_ref->u.c.component->ts;
4554 }
07368af0
PT
4555
4556 if (!char_ref)
4557 return;
4558
4559 gcc_assert (char_ref->next == NULL);
4560
bc21d315 4561 if (e->ts.u.cl)
07368af0 4562 {
bc21d315
JW
4563 if (e->ts.u.cl->length)
4564 gfc_free_expr (e->ts.u.cl->length);
98a819ea 4565 else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
07368af0
PT
4566 return;
4567 }
4568
4569 e->ts.type = BT_CHARACTER;
4570 e->ts.kind = gfc_default_character_kind;
4571
bc21d315 4572 if (!e->ts.u.cl)
b76e28c6 4573 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4574
4575 if (char_ref->u.ss.start)
4576 start = gfc_copy_expr (char_ref->u.ss.start);
4577 else
b7e75771 4578 start = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
07368af0
PT
4579
4580 if (char_ref->u.ss.end)
4581 end = gfc_copy_expr (char_ref->u.ss.end);
4582 else if (e->expr_type == EXPR_VARIABLE)
58864d1c
LK
4583 {
4584 if (!ts)
4585 ts = &e->symtree->n.sym->ts;
4586 end = gfc_copy_expr (ts->u.cl->length);
4587 }
07368af0
PT
4588 else
4589 end = NULL;
4590
4591 if (!start || !end)
efb63364
TB
4592 {
4593 gfc_free_expr (start);
4594 gfc_free_expr (end);
4595 return;
4596 }
07368af0 4597
98a819ea 4598 /* Length = (end - start + 1). */
bc21d315 4599 e->ts.u.cl->length = gfc_subtract (end, start);
b7e75771
JD
4600 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
4601 gfc_get_int_expr (gfc_default_integer_kind,
4602 NULL, 1));
07368af0 4603
98a819ea
SK
4604 /* F2008, 6.4.1: Both the starting point and the ending point shall
4605 be within the range 1, 2, ..., n unless the starting point exceeds
4606 the ending point, in which case the substring has length zero. */
4607
4608 if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
4609 mpz_set_si (e->ts.u.cl->length->value.integer, 0);
4610
bc21d315
JW
4611 e->ts.u.cl->length->ts.type = BT_INTEGER;
4612 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
07368af0
PT
4613
4614 /* Make sure that the length is simplified. */
bc21d315
JW
4615 gfc_simplify_expr (e->ts.u.cl->length, 1);
4616 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4617}
4618
4619
6de9cd9a
DN
4620/* Resolve subtype references. */
4621
524af0d6 4622static bool
edf1eac2 4623resolve_ref (gfc_expr *expr)
6de9cd9a
DN
4624{
4625 int current_part_dimension, n_components, seen_part_dimension;
4626 gfc_ref *ref;
4627
4628 for (ref = expr->ref; ref; ref = ref->next)
4629 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4630 {
4631 find_array_spec (expr);
4632 break;
4633 }
4634
4635 for (ref = expr->ref; ref; ref = ref->next)
4636 switch (ref->type)
4637 {
4638 case REF_ARRAY:
524af0d6
JB
4639 if (!resolve_array_ref (&ref->u.ar))
4640 return false;
6de9cd9a
DN
4641 break;
4642
4643 case REF_COMPONENT:
4644 break;
4645
4646 case REF_SUBSTRING:
524af0d6
JB
4647 if (!resolve_substring (ref))
4648 return false;
6de9cd9a
DN
4649 break;
4650 }
4651
4652 /* Check constraints on part references. */
4653
4654 current_part_dimension = 0;
4655 seen_part_dimension = 0;
4656 n_components = 0;
4657
4658 for (ref = expr->ref; ref; ref = ref->next)
4659 {
4660 switch (ref->type)
4661 {
4662 case REF_ARRAY:
4663 switch (ref->u.ar.type)
4664 {
4665 case AR_FULL:
d3a9eea2
TB
4666 /* Coarray scalar. */
4667 if (ref->u.ar.as->rank == 0)
4668 {
4669 current_part_dimension = 0;
4670 break;
4671 }
4672 /* Fall through. */
6de9cd9a
DN
4673 case AR_SECTION:
4674 current_part_dimension = 1;
4675 break;
4676
4677 case AR_ELEMENT:
4678 current_part_dimension = 0;
4679 break;
4680
4681 case AR_UNKNOWN:
4682 gfc_internal_error ("resolve_ref(): Bad array reference");
4683 }
4684
4685 break;
4686
4687 case REF_COMPONENT:
51f824b6 4688 if (current_part_dimension || seen_part_dimension)
6de9cd9a 4689 {
ef2bbc8c
JW
4690 /* F03:C614. */
4691 if (ref->u.c.component->attr.pointer
8f75db9f
PT
4692 || ref->u.c.component->attr.proc_pointer
4693 || (ref->u.c.component->ts.type == BT_CLASS
4694 && CLASS_DATA (ref->u.c.component)->attr.pointer))
edf1eac2
SK
4695 {
4696 gfc_error ("Component to the right of a part reference "
4697 "with nonzero rank must not have the POINTER "
4698 "attribute at %L", &expr->where);
524af0d6 4699 return false;
51f824b6 4700 }
8f75db9f
PT
4701 else if (ref->u.c.component->attr.allocatable
4702 || (ref->u.c.component->ts.type == BT_CLASS
4703 && CLASS_DATA (ref->u.c.component)->attr.allocatable))
4704
edf1eac2
SK
4705 {
4706 gfc_error ("Component to the right of a part reference "
4707 "with nonzero rank must not have the ALLOCATABLE "
4708 "attribute at %L", &expr->where);
524af0d6 4709 return false;
51f824b6 4710 }
6de9cd9a
DN
4711 }
4712
4713 n_components++;
4714 break;
4715
4716 case REF_SUBSTRING:
4717 break;
4718 }
4719
4720 if (((ref->type == REF_COMPONENT && n_components > 1)
4721 || ref->next == NULL)
edf1eac2 4722 && current_part_dimension
6de9cd9a
DN
4723 && seen_part_dimension)
4724 {
6de9cd9a
DN
4725 gfc_error ("Two or more part references with nonzero rank must "
4726 "not be specified at %L", &expr->where);
524af0d6 4727 return false;
6de9cd9a
DN
4728 }
4729
4730 if (ref->type == REF_COMPONENT)
4731 {
4732 if (current_part_dimension)
4733 seen_part_dimension = 1;
4734
edf1eac2 4735 /* reset to make sure */
6de9cd9a
DN
4736 current_part_dimension = 0;
4737 }
4738 }
4739
524af0d6 4740 return true;
6de9cd9a
DN
4741}
4742
4743
4744/* Given an expression, determine its shape. This is easier than it sounds.
f7b529fa 4745 Leaves the shape array NULL if it is not possible to determine the shape. */
6de9cd9a
DN
4746
4747static void
edf1eac2 4748expression_shape (gfc_expr *e)
6de9cd9a
DN
4749{
4750 mpz_t array[GFC_MAX_DIMENSIONS];
4751 int i;
4752
c62c6622 4753 if (e->rank <= 0 || e->shape != NULL)
6de9cd9a
DN
4754 return;
4755
4756 for (i = 0; i < e->rank; i++)
524af0d6 4757 if (!gfc_array_dimen_size (e, i, &array[i]))
6de9cd9a
DN
4758 goto fail;
4759
4760 e->shape = gfc_get_shape (e->rank);
4761
4762 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4763
4764 return;
4765
4766fail:
4767 for (i--; i >= 0; i--)
4768 mpz_clear (array[i]);
4769}
4770
4771
4772/* Given a variable expression node, compute the rank of the expression by
4773 examining the base symbol and any reference structures it may have. */
4774
4775static void
edf1eac2 4776expression_rank (gfc_expr *e)
6de9cd9a
DN
4777{
4778 gfc_ref *ref;
4779 int i, rank;
4780
00ca6640
DK
4781 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4782 could lead to serious confusion... */
4783 gcc_assert (e->expr_type != EXPR_COMPCALL);
4784
6de9cd9a
DN
4785 if (e->ref == NULL)
4786 {
4787 if (e->expr_type == EXPR_ARRAY)
4788 goto done;
f7b529fa 4789 /* Constructors can have a rank different from one via RESHAPE(). */
6de9cd9a
DN
4790
4791 if (e->symtree == NULL)
4792 {
4793 e->rank = 0;
4794 goto done;
4795 }
4796
4797 e->rank = (e->symtree->n.sym->as == NULL)
edf1eac2 4798 ? 0 : e->symtree->n.sym->as->rank;
6de9cd9a
DN
4799 goto done;
4800 }
4801
4802 rank = 0;
4803
4804 for (ref = e->ref; ref; ref = ref->next)
4805 {
2d300fac
JW
4806 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
4807 && ref->u.c.component->attr.function && !ref->next)
4808 rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
4809
6de9cd9a
DN
4810 if (ref->type != REF_ARRAY)
4811 continue;
4812
4813 if (ref->u.ar.type == AR_FULL)
4814 {
4815 rank = ref->u.ar.as->rank;
4816 break;
4817 }
4818
4819 if (ref->u.ar.type == AR_SECTION)
4820 {
edf1eac2 4821 /* Figure out the rank of the section. */
6de9cd9a
DN
4822 if (rank != 0)
4823 gfc_internal_error ("expression_rank(): Two array specs");
4824
4825 for (i = 0; i < ref->u.ar.dimen; i++)
4826 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4827 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4828 rank++;
4829
4830 break;
4831 }
4832 }
4833
4834 e->rank = rank;
4835
4836done:
4837 expression_shape (e);
4838}
4839
4840
8a8d1a16
TB
4841static void
4842add_caf_get_intrinsic (gfc_expr *e)
4843{
4844 gfc_expr *wrapper, *tmp_expr;
4845 gfc_ref *ref;
4846 int n;
4847
4848 for (ref = e->ref; ref; ref = ref->next)
4849 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
4850 break;
4851 if (ref == NULL)
4852 return;
4853
4854 for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
4855 if (ref->u.ar.dimen_type[n] != DIMEN_ELEMENT)
4856 return;
4857
4858 tmp_expr = XCNEW (gfc_expr);
4859 *tmp_expr = *e;
4860 wrapper = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_CAF_GET,
4861 "caf_get", tmp_expr->where, 1, tmp_expr);
4862 wrapper->ts = e->ts;
4863 wrapper->rank = e->rank;
4864 if (e->rank)
4865 wrapper->shape = gfc_copy_shape (e->shape, e->rank);
4866 *e = *wrapper;
4867 free (wrapper);
4868}
4869
4870
4871static void
4872remove_caf_get_intrinsic (gfc_expr *e)
4873{
4874 gcc_assert (e->expr_type == EXPR_FUNCTION && e->value.function.isym
4875 && e->value.function.isym->id == GFC_ISYM_CAF_GET);
4876 gfc_expr *e2 = e->value.function.actual->expr;
b5116268 4877 e->value.function.actual->expr = NULL;
8a8d1a16
TB
4878 gfc_free_actual_arglist (e->value.function.actual);
4879 gfc_free_shape (&e->shape, e->rank);
4880 *e = *e2;
4881 free (e2);
4882}
4883
4884
6de9cd9a
DN
4885/* Resolve a variable expression. */
4886
524af0d6 4887static bool
edf1eac2 4888resolve_variable (gfc_expr *e)
6de9cd9a
DN
4889{
4890 gfc_symbol *sym;
524af0d6 4891 bool t;
0e9a445b 4892
524af0d6 4893 t = true;
6de9cd9a 4894
3e978d30 4895 if (e->symtree == NULL)
524af0d6 4896 return false;
52bf62f9
DK
4897 sym = e->symtree->n.sym;
4898
e7ac6a7c
TB
4899 /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
4900 as ts.type is set to BT_ASSUMED in resolve_symbol. */
4901 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
4902 {
4903 if (!actual_arg || inquiry_argument)
4904 {
4905 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
4906 "be used as actual argument", sym->name, &e->where);
4907 return false;
4908 }
4909 }
45a69325 4910 /* TS 29113, 407b. */
e7ac6a7c 4911 else if (e->ts.type == BT_ASSUMED)
45a69325 4912 {
c62c6622
TB
4913 if (!actual_arg)
4914 {
4915 gfc_error ("Assumed-type variable %s at %L may only be used "
4916 "as actual argument", sym->name, &e->where);
524af0d6 4917 return false;
c62c6622
TB
4918 }
4919 else if (inquiry_argument && !first_actual_arg)
4920 {
4921 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4922 for all inquiry functions in resolve_function; the reason is
4923 that the function-name resolution happens too late in that
4924 function. */
4925 gfc_error ("Assumed-type variable %s at %L as actual argument to "
4926 "an inquiry function shall be the first argument",
4927 sym->name, &e->where);
524af0d6 4928 return false;
c62c6622
TB
4929 }
4930 }
c62c6622 4931 /* TS 29113, C535b. */
e7ac6a7c
TB
4932 else if ((sym->ts.type == BT_CLASS && sym->attr.class_ok
4933 && CLASS_DATA (sym)->as
4934 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4935 || (sym->ts.type != BT_CLASS && sym->as
4936 && sym->as->type == AS_ASSUMED_RANK))
c62c6622
TB
4937 {
4938 if (!actual_arg)
4939 {
4940 gfc_error ("Assumed-rank variable %s at %L may only be used as "
4941 "actual argument", sym->name, &e->where);
524af0d6 4942 return false;
c62c6622
TB
4943 }
4944 else if (inquiry_argument && !first_actual_arg)
4945 {
4946 /* FIXME: It doesn't work reliably as inquiry_argument is not set
4947 for all inquiry functions in resolve_function; the reason is
4948 that the function-name resolution happens too late in that
4949 function. */
4950 gfc_error ("Assumed-rank variable %s at %L as actual argument "
4951 "to an inquiry function shall be the first argument",
4952 sym->name, &e->where);
524af0d6 4953 return false;
c62c6622 4954 }
45a69325
TB
4955 }
4956
e7ac6a7c 4957 if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
45a69325 4958 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
c62c6622 4959 && e->ref->next == NULL))
e7ac6a7c
TB
4960 {
4961 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
4962 "a subobject reference", sym->name, &e->ref->u.ar.where);
4963 return false;
4964 }
4965 /* TS 29113, 407b. */
4966 else if (e->ts.type == BT_ASSUMED && e->ref
4967 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4968 && e->ref->next == NULL))
45a69325 4969 {
c62c6622
TB
4970 gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
4971 "reference", sym->name, &e->ref->u.ar.where);
524af0d6 4972 return false;
45a69325
TB
4973 }
4974
c62c6622
TB
4975 /* TS 29113, C535b. */
4976 if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
4977 && CLASS_DATA (sym)->as
4978 && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
4979 || (sym->ts.type != BT_CLASS && sym->as
4980 && sym->as->type == AS_ASSUMED_RANK))
4981 && e->ref
4982 && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
4983 && e->ref->next == NULL))
4984 {
4985 gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
4986 "reference", sym->name, &e->ref->u.ar.where);
524af0d6 4987 return false;
c62c6622
TB
4988 }
4989
76540ac3
AV
4990 /* For variables that are used in an associate (target => object) where
4991 the object's basetype is array valued while the target is scalar,
4992 the ts' type of the component refs is still array valued, which
4993 can't be translated that way. */
4994 if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
4995 && sym->assoc->target->ts.type == BT_CLASS
4996 && CLASS_DATA (sym->assoc->target)->as)
4997 {
4998 gfc_ref *ref = e->ref;
4999 while (ref)
5000 {
5001 switch (ref->type)
5002 {
5003 case REF_COMPONENT:
5004 ref->u.c.sym = sym->ts.u.derived;
5005 /* Stop the loop. */
5006 ref = NULL;
5007 break;
5008 default:
5009 ref = ref->next;
5010 break;
5011 }
5012 }
5013 }
c62c6622 5014
3e78238a 5015 /* If this is an associate-name, it may be parsed with an array reference
8f75db9f
PT
5016 in error even though the target is scalar. Fail directly in this case.
5017 TODO Understand why class scalar expressions must be excluded. */
5018 if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
5019 {
5020 if (sym->ts.type == BT_CLASS)
5021 gfc_fix_class_refs (e);
5022 if (!sym->attr.dimension && e->ref && e->ref->type == REF_ARRAY)
524af0d6 5023 return false;
8f75db9f 5024 }
52bf62f9 5025
c3f34952
TB
5026 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
5027 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
5028
52bf62f9
DK
5029 /* On the other hand, the parser may not have known this is an array;
5030 in this case, we have to add a FULL reference. */
5031 if (sym->assoc && sym->attr.dimension && !e->ref)
5032 {
5033 e->ref = gfc_get_ref ();
5034 e->ref->type = REF_ARRAY;
5035 e->ref->u.ar.type = AR_FULL;
5036 e->ref->u.ar.dimen = 0;
5037 }
6de9cd9a 5038
76540ac3
AV
5039 /* Like above, but for class types, where the checking whether an array
5040 ref is present is more complicated. Furthermore make sure not to add
5041 the full array ref to _vptr or _len refs. */
5042 if (sym->assoc && sym->ts.type == BT_CLASS
5043 && CLASS_DATA (sym)->attr.dimension
5044 && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
5045 {
5046 gfc_ref *ref, *newref;
5047
5048 newref = gfc_get_ref ();
5049 newref->type = REF_ARRAY;
5050 newref->u.ar.type = AR_FULL;
5051 newref->u.ar.dimen = 0;
5052 /* Because this is an associate var and the first ref either is a ref to
5053 the _data component or not, no traversal of the ref chain is
5054 needed. The array ref needs to be inserted after the _data ref,
5055 or when that is not present, which may happend for polymorphic
5056 types, then at the first position. */
5057 ref = e->ref;
5058 if (!ref)
5059 e->ref = newref;
5060 else if (ref->type == REF_COMPONENT
5061 && strcmp ("_data", ref->u.c.component->name) == 0)
5062 {
5063 if (!ref->next || ref->next->type != REF_ARRAY)
5064 {
5065 newref->next = ref->next;
5066 ref->next = newref;
5067 }
5068 else
5069 /* Array ref present already. */
5070 gfc_free_ref_list (newref);
5071 }
5072 else if (ref->type == REF_ARRAY)
5073 /* Array ref present already. */
5074 gfc_free_ref_list (newref);
5075 else
5076 {
5077 newref->next = ref;
5078 e->ref = newref;
5079 }
5080 }
5081
524af0d6
JB
5082 if (e->ref && !resolve_ref (e))
5083 return false;
009e94d4 5084
3070bab4
JW
5085 if (sym->attr.flavor == FL_PROCEDURE
5086 && (!sym->attr.function
5087 || (sym->attr.function && sym->result
5088 && sym->result->attr.proc_pointer
5089 && !sym->result->attr.function)))
6de9cd9a
DN
5090 {
5091 e->ts.type = BT_PROCEDURE;
a03826d1 5092 goto resolve_procedure;
6de9cd9a
DN
5093 }
5094
5095 if (sym->ts.type != BT_UNKNOWN)
5096 gfc_variable_attr (e, &e->ts);
5097 else
5098 {
5099 /* Must be a simple variable reference. */
524af0d6
JB
5100 if (!gfc_set_default_type (sym, 1, sym->ns))
5101 return false;
6de9cd9a
DN
5102 e->ts = sym->ts;
5103 }
5104
48474141 5105 if (check_assumed_size_reference (sym, e))
524af0d6 5106 return false;
48474141 5107
b46ebd6c 5108 /* Deal with forward references to entries during gfc_resolve_code, to
0e9a445b
PT
5109 satisfy, at least partially, 12.5.2.5. */
5110 if (gfc_current_ns->entries
edf1eac2
SK
5111 && current_entry_id == sym->entry_id
5112 && cs_base
5113 && cs_base->current
5114 && cs_base->current->op != EXEC_ENTRY)
0e9a445b
PT
5115 {
5116 gfc_entry_list *entry;
5117 gfc_formal_arglist *formal;
5118 int n;
fd061185 5119 bool seen, saved_specification_expr;
0e9a445b
PT
5120
5121 /* If the symbol is a dummy... */
70365b5c 5122 if (sym->attr.dummy && sym->ns == gfc_current_ns)
0e9a445b
PT
5123 {
5124 entry = gfc_current_ns->entries;
5125 seen = false;
5126
5127 /* ...test if the symbol is a parameter of previous entries. */
5128 for (; entry && entry->id <= current_entry_id; entry = entry->next)
5129 for (formal = entry->sym->formal; formal; formal = formal->next)
5130 {
5131 if (formal->sym && sym->name == formal->sym->name)
502af491
PCC
5132 {
5133 seen = true;
5134 break;
5135 }
0e9a445b
PT
5136 }
5137
5138 /* If it has not been seen as a dummy, this is an error. */
5139 if (!seen)
5140 {
5141 if (specification_expr)
a4d9b221 5142 gfc_error ("Variable %qs, used in a specification expression"
70365b5c 5143 ", is referenced at %L before the ENTRY statement "
0e9a445b
PT
5144 "in which it is a parameter",
5145 sym->name, &cs_base->current->loc);
5146 else
a4d9b221 5147 gfc_error ("Variable %qs is used at %L before the ENTRY "
0e9a445b
PT
5148 "statement in which it is a parameter",
5149 sym->name, &cs_base->current->loc);
524af0d6 5150 t = false;
0e9a445b
PT
5151 }
5152 }
5153
5154 /* Now do the same check on the specification expressions. */
fd061185
TB
5155 saved_specification_expr = specification_expr;
5156 specification_expr = true;
0e9a445b 5157 if (sym->ts.type == BT_CHARACTER
524af0d6
JB
5158 && !gfc_resolve_expr (sym->ts.u.cl->length))
5159 t = false;
0e9a445b
PT
5160
5161 if (sym->as)
5162 for (n = 0; n < sym->as->rank; n++)
5163 {
524af0d6
JB
5164 if (!gfc_resolve_expr (sym->as->lower[n]))
5165 t = false;
5166 if (!gfc_resolve_expr (sym->as->upper[n]))
5167 t = false;
0e9a445b 5168 }
fd061185 5169 specification_expr = saved_specification_expr;
0e9a445b 5170
524af0d6 5171 if (t)
0e9a445b
PT
5172 /* Update the symbol's entry level. */
5173 sym->entry_id = current_entry_id + 1;
5174 }
5175
022e30c0
PT
5176 /* If a symbol has been host_associated mark it. This is used latter,
5177 to identify if aliasing is possible via host association. */
5178 if (sym->attr.flavor == FL_VARIABLE
5179 && gfc_current_ns->parent
5180 && (gfc_current_ns->parent == sym->ns
5181 || (gfc_current_ns->parent->parent
5182 && gfc_current_ns->parent->parent == sym->ns)))
5183 sym->attr.host_assoc = 1;
5184
30c931de
PT
5185 if (gfc_current_ns->proc_name
5186 && sym->attr.dimension
5187 && (sym->ns != gfc_current_ns
5188 || sym->attr.use_assoc
5189 || sym->attr.in_common))
5190 gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
5191
a03826d1 5192resolve_procedure:
524af0d6
JB
5193 if (t && !resolve_procedure_expression (e))
5194 t = false;
a03826d1 5195
d3a9eea2
TB
5196 /* F2008, C617 and C1229. */
5197 if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
5198 && gfc_is_coindexed (e))
5199 {
5200 gfc_ref *ref, *ref2 = NULL;
5201
d3a9eea2
TB
5202 for (ref = e->ref; ref; ref = ref->next)
5203 {
5204 if (ref->type == REF_COMPONENT)
5205 ref2 = ref;
5206 if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
5207 break;
5208 }
5209
5210 for ( ; ref; ref = ref->next)
5211 if (ref->type == REF_COMPONENT)
5212 break;
5213
a70de21f
TB
5214 /* Expression itself is not coindexed object. */
5215 if (ref && e->ts.type == BT_CLASS)
5216 {
5217 gfc_error ("Polymorphic subobject of coindexed object at %L",
5218 &e->where);
524af0d6 5219 t = false;
a70de21f
TB
5220 }
5221
d3a9eea2
TB
5222 /* Expression itself is coindexed object. */
5223 if (ref == NULL)
5224 {
5225 gfc_component *c;
5226 c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
5227 for ( ; c; c = c->next)
5228 if (c->attr.allocatable && c->ts.type == BT_CLASS)
5229 {
5230 gfc_error ("Coindexed object with polymorphic allocatable "
5231 "subcomponent at %L", &e->where);
524af0d6 5232 t = false;
d3a9eea2
TB
5233 break;
5234 }
5235 }
5236 }
5237
8a8d1a16
TB
5238 if (t)
5239 expression_rank (e);
5240
f19626cf 5241 if (t && flag_coarray == GFC_FCOARRAY_LIB && gfc_is_coindexed (e))
8a8d1a16
TB
5242 add_caf_get_intrinsic (e);
5243
0e9a445b 5244 return t;
6de9cd9a
DN
5245}
5246
5247
eb77cddf
PT
5248/* Checks to see that the correct symbol has been host associated.
5249 The only situation where this arises is that in which a twice
5250 contained function is parsed after the host association is made.
5b3b1d09
PT
5251 Therefore, on detecting this, change the symbol in the expression
5252 and convert the array reference into an actual arglist if the old
5253 symbol is a variable. */
eb77cddf
PT
5254static bool
5255check_host_association (gfc_expr *e)
5256{
5257 gfc_symbol *sym, *old_sym;
5b3b1d09 5258 gfc_symtree *st;
eb77cddf 5259 int n;
5b3b1d09 5260 gfc_ref *ref;
e4bf01a4 5261 gfc_actual_arglist *arg, *tail = NULL;
8de10a62 5262 bool retval = e->expr_type == EXPR_FUNCTION;
eb77cddf 5263
a1ab6660
PT
5264 /* If the expression is the result of substitution in
5265 interface.c(gfc_extend_expr) because there is no way in
5266 which the host association can be wrong. */
5267 if (e->symtree == NULL
5268 || e->symtree->n.sym == NULL
5269 || e->user_operator)
8de10a62 5270 return retval;
eb77cddf
PT
5271
5272 old_sym = e->symtree->n.sym;
8de10a62 5273
eb77cddf 5274 if (gfc_current_ns->parent
eb77cddf
PT
5275 && old_sym->ns != gfc_current_ns)
5276 {
5b3b1d09
PT
5277 /* Use the 'USE' name so that renamed module symbols are
5278 correctly handled. */
9be3684b 5279 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5b3b1d09 5280
a944c79a 5281 if (sym && old_sym != sym
67cec813 5282 && sym->ts.type == old_sym->ts.type
a944c79a
PT
5283 && sym->attr.flavor == FL_PROCEDURE
5284 && sym->attr.contained)
eb77cddf 5285 {
5b3b1d09 5286 /* Clear the shape, since it might not be valid. */
d54e80ce 5287 gfc_free_shape (&e->shape, e->rank);
eb77cddf 5288
1aafbf99
PT
5289 /* Give the expression the right symtree! */
5290 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
5291 gcc_assert (st != NULL);
eb77cddf 5292
1aafbf99
PT
5293 if (old_sym->attr.flavor == FL_PROCEDURE
5294 || e->expr_type == EXPR_FUNCTION)
5295 {
5b3b1d09
PT
5296 /* Original was function so point to the new symbol, since
5297 the actual argument list is already attached to the
1cc0e193 5298 expression. */
5b3b1d09
PT
5299 e->value.function.esym = NULL;
5300 e->symtree = st;
5301 }
5302 else
5303 {
5304 /* Original was variable so convert array references into
5305 an actual arglist. This does not need any checking now
d8155bf5 5306 since resolve_function will take care of it. */
5b3b1d09
PT
5307 e->value.function.actual = NULL;
5308 e->expr_type = EXPR_FUNCTION;
5309 e->symtree = st;
eb77cddf 5310
5b3b1d09
PT
5311 /* Ambiguity will not arise if the array reference is not
5312 the last reference. */
5313 for (ref = e->ref; ref; ref = ref->next)
5314 if (ref->type == REF_ARRAY && ref->next == NULL)
5315 break;
5316
5317 gcc_assert (ref->type == REF_ARRAY);
5318
5319 /* Grab the start expressions from the array ref and
5320 copy them into actual arguments. */
5321 for (n = 0; n < ref->u.ar.dimen; n++)
5322 {
5323 arg = gfc_get_actual_arglist ();
5324 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
5325 if (e->value.function.actual == NULL)
5326 tail = e->value.function.actual = arg;
5327 else
5328 {
5329 tail->next = arg;
5330 tail = arg;
5331 }
5332 }
eb77cddf 5333
5b3b1d09
PT
5334 /* Dump the reference list and set the rank. */
5335 gfc_free_ref_list (e->ref);
5336 e->ref = NULL;
5337 e->rank = sym->as ? sym->as->rank : 0;
5338 }
5339
5340 gfc_resolve_expr (e);
5341 sym->refs++;
eb77cddf
PT
5342 }
5343 }
8de10a62 5344 /* This might have changed! */
eb77cddf
PT
5345 return e->expr_type == EXPR_FUNCTION;
5346}
5347
5348
07368af0
PT
5349static void
5350gfc_resolve_character_operator (gfc_expr *e)
5351{
5352 gfc_expr *op1 = e->value.op.op1;
5353 gfc_expr *op2 = e->value.op.op2;
5354 gfc_expr *e1 = NULL;
5355 gfc_expr *e2 = NULL;
5356
a1ee985f 5357 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
07368af0 5358
bc21d315
JW
5359 if (op1->ts.u.cl && op1->ts.u.cl->length)
5360 e1 = gfc_copy_expr (op1->ts.u.cl->length);
07368af0 5361 else if (op1->expr_type == EXPR_CONSTANT)
b7e75771
JD
5362 e1 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5363 op1->value.character.length);
07368af0 5364
bc21d315
JW
5365 if (op2->ts.u.cl && op2->ts.u.cl->length)
5366 e2 = gfc_copy_expr (op2->ts.u.cl->length);
07368af0 5367 else if (op2->expr_type == EXPR_CONSTANT)
b7e75771
JD
5368 e2 = gfc_get_int_expr (gfc_default_integer_kind, NULL,
5369 op2->value.character.length);
07368af0 5370
b76e28c6 5371 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
5372
5373 if (!e1 || !e2)
d7920cf0
TB
5374 {
5375 gfc_free_expr (e1);
5376 gfc_free_expr (e2);
4d382327 5377
d7920cf0
TB
5378 return;
5379 }
07368af0 5380
bc21d315
JW
5381 e->ts.u.cl->length = gfc_add (e1, e2);
5382 e->ts.u.cl->length->ts.type = BT_INTEGER;
5383 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5384 gfc_simplify_expr (e->ts.u.cl->length, 0);
5385 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
5386
5387 return;
5388}
5389
5390
5391/* Ensure that an character expression has a charlen and, if possible, a
5392 length expression. */
5393
5394static void
5395fixup_charlen (gfc_expr *e)
5396{
5397 /* The cases fall through so that changes in expression type and the need
5398 for multiple fixes are picked up. In all circumstances, a charlen should
5399 be available for the middle end to hang a backend_decl on. */
5400 switch (e->expr_type)
5401 {
5402 case EXPR_OP:
5403 gfc_resolve_character_operator (e);
5404
5405 case EXPR_ARRAY:
5406 if (e->expr_type == EXPR_ARRAY)
5407 gfc_resolve_character_array_constructor (e);
5408
5409 case EXPR_SUBSTRING:
bc21d315 5410 if (!e->ts.u.cl && e->ref)
07368af0
PT
5411 gfc_resolve_substring_charlen (e);
5412
5413 default:
bc21d315 5414 if (!e->ts.u.cl)
b76e28c6 5415 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
5416
5417 break;
5418 }
5419}
5420
5421
8e1f752a
DK
5422/* Update an actual argument to include the passed-object for type-bound
5423 procedures at the right position. */
5424
5425static gfc_actual_arglist*
90661f26
JW
5426update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
5427 const char *name)
8e1f752a 5428{
b82657f4
DK
5429 gcc_assert (argpos > 0);
5430
8e1f752a
DK
5431 if (argpos == 1)
5432 {
5433 gfc_actual_arglist* result;
5434
5435 result = gfc_get_actual_arglist ();
5436 result->expr = po;
5437 result->next = lst;
90661f26
JW
5438 if (name)
5439 result->name = name;
8e1f752a
DK
5440
5441 return result;
5442 }
5443
90661f26
JW
5444 if (lst)
5445 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
5446 else
5447 lst = update_arglist_pass (NULL, po, argpos - 1, name);
8e1f752a
DK
5448 return lst;
5449}
5450
5451
e157f736 5452/* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
8e1f752a 5453
e157f736
DK
5454static gfc_expr*
5455extract_compcall_passed_object (gfc_expr* e)
8e1f752a
DK
5456{
5457 gfc_expr* po;
8e1f752a 5458
e157f736 5459 gcc_assert (e->expr_type == EXPR_COMPCALL);
8e1f752a 5460
4a44a72d
DK
5461 if (e->value.compcall.base_object)
5462 po = gfc_copy_expr (e->value.compcall.base_object);
5463 else
5464 {
5465 po = gfc_get_expr ();
5466 po->expr_type = EXPR_VARIABLE;
5467 po->symtree = e->symtree;
5468 po->ref = gfc_copy_ref (e->ref);
63894de2 5469 po->where = e->where;
4a44a72d 5470 }
8e1f752a 5471
524af0d6 5472 if (!gfc_resolve_expr (po))
e157f736
DK
5473 return NULL;
5474
5475 return po;
5476}
5477
5478
5479/* Update the arglist of an EXPR_COMPCALL expression to include the
5480 passed-object. */
5481
524af0d6 5482static bool
e157f736
DK
5483update_compcall_arglist (gfc_expr* e)
5484{
5485 gfc_expr* po;
5486 gfc_typebound_proc* tbp;
5487
5488 tbp = e->value.compcall.tbp;
5489
b82657f4 5490 if (tbp->error)
524af0d6 5491 return false;
b82657f4 5492
e157f736
DK
5493 po = extract_compcall_passed_object (e);
5494 if (!po)
524af0d6 5495 return false;
e157f736 5496
4a44a72d 5497 if (tbp->nopass || e->value.compcall.ignore_pass)
8e1f752a
DK
5498 {
5499 gfc_free_expr (po);
524af0d6 5500 return true;
8e1f752a
DK
5501 }
5502
5503 gcc_assert (tbp->pass_arg_num > 0);
5504 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
90661f26
JW
5505 tbp->pass_arg_num,
5506 tbp->pass_arg);
5507
524af0d6 5508 return true;
90661f26
JW
5509}
5510
5511
5512/* Extract the passed object from a PPC call (a copy of it). */
5513
5514static gfc_expr*
5515extract_ppc_passed_object (gfc_expr *e)
5516{
5517 gfc_expr *po;
5518 gfc_ref **ref;
5519
5520 po = gfc_get_expr ();
5521 po->expr_type = EXPR_VARIABLE;
5522 po->symtree = e->symtree;
5523 po->ref = gfc_copy_ref (e->ref);
63894de2 5524 po->where = e->where;
90661f26
JW
5525
5526 /* Remove PPC reference. */
5527 ref = &po->ref;
5528 while ((*ref)->next)
63894de2 5529 ref = &(*ref)->next;
90661f26
JW
5530 gfc_free_ref_list (*ref);
5531 *ref = NULL;
5532
524af0d6 5533 if (!gfc_resolve_expr (po))
90661f26
JW
5534 return NULL;
5535
5536 return po;
5537}
5538
5539
5540/* Update the actual arglist of a procedure pointer component to include the
5541 passed-object. */
5542
524af0d6 5543static bool
90661f26
JW
5544update_ppc_arglist (gfc_expr* e)
5545{
5546 gfc_expr* po;
5547 gfc_component *ppc;
5548 gfc_typebound_proc* tb;
5549
2a573572
MM
5550 ppc = gfc_get_proc_ptr_comp (e);
5551 if (!ppc)
524af0d6 5552 return false;
90661f26
JW
5553
5554 tb = ppc->tb;
5555
5556 if (tb->error)
524af0d6 5557 return false;
90661f26 5558 else if (tb->nopass)
524af0d6 5559 return true;
90661f26
JW
5560
5561 po = extract_ppc_passed_object (e);
5562 if (!po)
524af0d6 5563 return false;
90661f26 5564
8b29bd22 5565 /* F08:R739. */
c62c6622 5566 if (po->rank != 0)
90661f26
JW
5567 {
5568 gfc_error ("Passed-object at %L must be scalar", &e->where);
524af0d6 5569 return false;
90661f26
JW
5570 }
5571
8b29bd22
JW
5572 /* F08:C611. */
5573 if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
5574 {
5575 gfc_error ("Base object for procedure-pointer component call at %L is of"
a4d9b221 5576 " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
524af0d6 5577 return false;
8b29bd22
JW
5578 }
5579
90661f26
JW
5580 gcc_assert (tb->pass_arg_num > 0);
5581 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
5582 tb->pass_arg_num,
5583 tb->pass_arg);
8e1f752a 5584
524af0d6 5585 return true;
8e1f752a
DK
5586}
5587
5588
b0e5fa94
DK
5589/* Check that the object a TBP is called on is valid, i.e. it must not be
5590 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
5591
524af0d6 5592static bool
b0e5fa94
DK
5593check_typebound_baseobject (gfc_expr* e)
5594{
5595 gfc_expr* base;
524af0d6 5596 bool return_value = false;
b0e5fa94
DK
5597
5598 base = extract_compcall_passed_object (e);
5599 if (!base)
524af0d6 5600 return false;
b0e5fa94 5601
cf2b3c22 5602 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
e56817db 5603
0b2d443b 5604 if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
524af0d6 5605 return false;
0b2d443b 5606
8b29bd22 5607 /* F08:C611. */
e56817db 5608 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
b0e5fa94
DK
5609 {
5610 gfc_error ("Base object for type-bound procedure call at %L is of"
a4d9b221 5611 " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
99b41d52 5612 goto cleanup;
b0e5fa94
DK
5613 }
5614
8b29bd22
JW
5615 /* F08:C1230. If the procedure called is NOPASS,
5616 the base object must be scalar. */
c62c6622 5617 if (e->value.compcall.tbp->nopass && base->rank != 0)
41a394bb
DK
5618 {
5619 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
5620 " be scalar", &e->where);
99b41d52 5621 goto cleanup;
41a394bb
DK
5622 }
5623
524af0d6 5624 return_value = true;
99b41d52
MM
5625
5626cleanup:
5627 gfc_free_expr (base);
5628 return return_value;
b0e5fa94
DK
5629}
5630
5631
8e1f752a
DK
5632/* Resolve a call to a type-bound procedure, either function or subroutine,
5633 statically from the data in an EXPR_COMPCALL expression. The adapted
5634 arglist and the target-procedure symtree are returned. */
5635
524af0d6 5636static bool
8e1f752a
DK
5637resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
5638 gfc_actual_arglist** actual)
5639{
5640 gcc_assert (e->expr_type == EXPR_COMPCALL);
e157f736 5641 gcc_assert (!e->value.compcall.tbp->is_generic);
8e1f752a
DK
5642
5643 /* Update the actual arglist for PASS. */
524af0d6
JB
5644 if (!update_compcall_arglist (e))
5645 return false;
8e1f752a
DK
5646
5647 *actual = e->value.compcall.actual;
e157f736 5648 *target = e->value.compcall.tbp->u.specific;
8e1f752a
DK
5649
5650 gfc_free_ref_list (e->ref);
5651 e->ref = NULL;
5652 e->value.compcall.actual = NULL;
5653
003e0ad6 5654 /* If we find a deferred typebound procedure, check for derived types
e3a2ec56
TB
5655 that an overriding typebound procedure has not been missed. */
5656 if (e->value.compcall.name
5657 && !e->value.compcall.tbp->non_overridable
5658 && e->value.compcall.base_object
5659 && e->value.compcall.base_object->ts.type == BT_DERIVED)
003e0ad6
PT
5660 {
5661 gfc_symtree *st;
5662 gfc_symbol *derived;
5663
5664 /* Use the derived type of the base_object. */
5665 derived = e->value.compcall.base_object->ts.u.derived;
5666 st = NULL;
5667
eea58adb 5668 /* If necessary, go through the inheritance chain. */
003e0ad6
PT
5669 while (!st && derived)
5670 {
5671 /* Look for the typebound procedure 'name'. */
5672 if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
5673 st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
5674 e->value.compcall.name);
5675 if (!st)
5676 derived = gfc_get_derived_super_type (derived);
5677 }
5678
5679 /* Now find the specific name in the derived type namespace. */
5680 if (st && st->n.tb && st->n.tb->u.specific)
5681 gfc_find_sym_tree (st->n.tb->u.specific->name,
5682 derived->ns, 1, &st);
5683 if (st)
5684 *target = st;
5685 }
524af0d6 5686 return true;
8e1f752a
DK
5687}
5688
5689
15d774f9
PT
5690/* Get the ultimate declared type from an expression. In addition,
5691 return the last class/derived type reference and the copy of the
94fae14b
PT
5692 reference list. If check_types is set true, derived types are
5693 identified as well as class references. */
15d774f9
PT
5694static gfc_symbol*
5695get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
94fae14b 5696 gfc_expr *e, bool check_types)
15d774f9
PT
5697{
5698 gfc_symbol *declared;
5699 gfc_ref *ref;
5700
5701 declared = NULL;
5702 if (class_ref)
5703 *class_ref = NULL;
5704 if (new_ref)
5705 *new_ref = gfc_copy_ref (e->ref);
5706
5707 for (ref = e->ref; ref; ref = ref->next)
5708 {
5709 if (ref->type != REF_COMPONENT)
5710 continue;
5711
94fae14b
PT
5712 if ((ref->u.c.component->ts.type == BT_CLASS
5713 || (check_types && ref->u.c.component->ts.type == BT_DERIVED))
5714 && ref->u.c.component->attr.flavor != FL_PROCEDURE)
15d774f9
PT
5715 {
5716 declared = ref->u.c.component->ts.u.derived;
5717 if (class_ref)
5718 *class_ref = ref;
5719 }
5720 }
5721
5722 if (declared == NULL)
5723 declared = e->symtree->n.sym->ts.u.derived;
5724
5725 return declared;
5726}
5727
5728
e157f736
DK
5729/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
5730 which of the specific bindings (if any) matches the arglist and transform
5731 the expression into a call of that binding. */
5732
524af0d6 5733static bool
eece1eb9 5734resolve_typebound_generic_call (gfc_expr* e, const char **name)
e157f736
DK
5735{
5736 gfc_typebound_proc* genproc;
5737 const char* genname;
15d774f9
PT
5738 gfc_symtree *st;
5739 gfc_symbol *derived;
e157f736
DK
5740
5741 gcc_assert (e->expr_type == EXPR_COMPCALL);
5742 genname = e->value.compcall.name;
5743 genproc = e->value.compcall.tbp;
5744
5745 if (!genproc->is_generic)
524af0d6 5746 return true;
e157f736
DK
5747
5748 /* Try the bindings on this type and in the inheritance hierarchy. */
5749 for (; genproc; genproc = genproc->overridden)
5750 {
5751 gfc_tbp_generic* g;
5752
5753 gcc_assert (genproc->is_generic);
5754 for (g = genproc->u.generic; g; g = g->next)
5755 {
5756 gfc_symbol* target;
5757 gfc_actual_arglist* args;
5758 bool matches;
5759
5760 gcc_assert (g->specific);
b82657f4
DK
5761
5762 if (g->specific->error)
5763 continue;
5764
e157f736
DK
5765 target = g->specific->u.specific->n.sym;
5766
5767 /* Get the right arglist by handling PASS/NOPASS. */
5768 args = gfc_copy_actual_arglist (e->value.compcall.actual);
5769 if (!g->specific->nopass)
5770 {
5771 gfc_expr* po;
5772 po = extract_compcall_passed_object (e);
5773 if (!po)
efb63364
TB
5774 {
5775 gfc_free_actual_arglist (args);
524af0d6 5776 return false;
efb63364 5777 }
e157f736 5778
b82657f4
DK
5779 gcc_assert (g->specific->pass_arg_num > 0);
5780 gcc_assert (!g->specific->error);
90661f26
JW
5781 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5782 g->specific->pass_arg);
e157f736 5783 }
f0ac18b7 5784 resolve_actual_arglist (args, target->attr.proc,
4cbc9039
JW
5785 is_external_proc (target)
5786 && gfc_sym_get_dummy_args (target) == NULL);
e157f736
DK
5787
5788 /* Check if this arglist matches the formal. */
f0ac18b7 5789 matches = gfc_arglist_matches_symbol (&args, target);
e157f736
DK
5790
5791 /* Clean up and break out of the loop if we've found it. */
5792 gfc_free_actual_arglist (args);
5793 if (matches)
5794 {
5795 e->value.compcall.tbp = g->specific;
ab7306ed 5796 genname = g->specific_st->name;
eece1eb9
PT
5797 /* Pass along the name for CLASS methods, where the vtab
5798 procedure pointer component has to be referenced. */
5799 if (name)
ab7306ed 5800 *name = genname;
e157f736
DK
5801 goto success;
5802 }
5803 }
5804 }
5805
5806 /* Nothing matching found! */
5807 gfc_error ("Found no matching specific binding for the call to the GENERIC"
a4d9b221 5808 " %qs at %L", genname, &e->where);
524af0d6 5809 return false;
e157f736
DK
5810
5811success:
15d774f9 5812 /* Make sure that we have the right specific instance for the name. */
94fae14b 5813 derived = get_declared_from_expr (NULL, NULL, e, true);
15d774f9 5814
12578be7 5815 st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
15d774f9
PT
5816 if (st)
5817 e->value.compcall.tbp = st->n.tb;
5818
524af0d6 5819 return true;
e157f736
DK
5820}
5821
5822
8e1f752a
DK
5823/* Resolve a call to a type-bound subroutine. */
5824
524af0d6 5825static bool
744868aa 5826resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
8e1f752a
DK
5827{
5828 gfc_actual_arglist* newactual;
5829 gfc_symtree* target;
5830
e157f736 5831 /* Check that's really a SUBROUTINE. */
a513927a 5832 if (!c->expr1->value.compcall.tbp->subroutine)
e157f736 5833 {
a4d9b221 5834 gfc_error ("%qs at %L should be a SUBROUTINE",
a513927a 5835 c->expr1->value.compcall.name, &c->loc);
524af0d6 5836 return false;
e157f736
DK
5837 }
5838
524af0d6
JB
5839 if (!check_typebound_baseobject (c->expr1))
5840 return false;
b0e5fa94 5841
eece1eb9
PT
5842 /* Pass along the name for CLASS methods, where the vtab
5843 procedure pointer component has to be referenced. */
5844 if (name)
5845 *name = c->expr1->value.compcall.name;
5846
524af0d6
JB
5847 if (!resolve_typebound_generic_call (c->expr1, name))
5848 return false;
e157f736 5849
744868aa
JW
5850 /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
5851 if (overridable)
5852 *overridable = !c->expr1->value.compcall.tbp->non_overridable;
5853
8e1f752a
DK
5854 /* Transform into an ordinary EXEC_CALL for now. */
5855
524af0d6
JB
5856 if (!resolve_typebound_static (c->expr1, &target, &newactual))
5857 return false;
8e1f752a
DK
5858
5859 c->ext.actual = newactual;
5860 c->symtree = target;
4a44a72d 5861 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
8e1f752a 5862
a513927a 5863 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7cf078dc 5864
a513927a 5865 gfc_free_expr (c->expr1);
7cf078dc
PT
5866 c->expr1 = gfc_get_expr ();
5867 c->expr1->expr_type = EXPR_FUNCTION;
5868 c->expr1->symtree = target;
5869 c->expr1->where = c->loc;
8e1f752a
DK
5870
5871 return resolve_call (c);
5872}
5873
5874
eece1eb9 5875/* Resolve a component-call expression. */
524af0d6 5876static bool
eece1eb9 5877resolve_compcall (gfc_expr* e, const char **name)
8e1f752a
DK
5878{
5879 gfc_actual_arglist* newactual;
5880 gfc_symtree* target;
5881
e157f736 5882 /* Check that's really a FUNCTION. */
eece1eb9 5883 if (!e->value.compcall.tbp->function)
e157f736 5884 {
a4d9b221 5885 gfc_error ("%qs at %L should be a FUNCTION",
e157f736 5886 e->value.compcall.name, &e->where);
524af0d6 5887 return false;
e157f736
DK
5888 }
5889
4a44a72d
DK
5890 /* These must not be assign-calls! */
5891 gcc_assert (!e->value.compcall.assign);
5892
524af0d6
JB
5893 if (!check_typebound_baseobject (e))
5894 return false;
b0e5fa94 5895
eece1eb9
PT
5896 /* Pass along the name for CLASS methods, where the vtab
5897 procedure pointer component has to be referenced. */
5898 if (name)
5899 *name = e->value.compcall.name;
5900
524af0d6
JB
5901 if (!resolve_typebound_generic_call (e, name))
5902 return false;
00ca6640
DK
5903 gcc_assert (!e->value.compcall.tbp->is_generic);
5904
5905 /* Take the rank from the function's symbol. */
5906 if (e->value.compcall.tbp->u.specific->n.sym->as)
5907 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
e157f736
DK
5908
5909 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
8e1f752a
DK
5910 arglist to the TBP's binding target. */
5911
524af0d6
JB
5912 if (!resolve_typebound_static (e, &target, &newactual))
5913 return false;
8e1f752a
DK
5914
5915 e->value.function.actual = newactual;
b3d286ba 5916 e->value.function.name = NULL;
37a40b53 5917 e->value.function.esym = target->n.sym;
e157f736 5918 e->value.function.isym = NULL;
8e1f752a 5919 e->symtree = target;
f0ac18b7 5920 e->ts = target->n.sym->ts;
8e1f752a
DK
5921 e->expr_type = EXPR_FUNCTION;
5922
eece1eb9
PT
5923 /* Resolution is not necessary if this is a class subroutine; this
5924 function only has to identify the specific proc. Resolution of
5925 the call will be done next in resolve_typebound_call. */
5926 return gfc_resolve_expr (e);
28188747
PT
5927}
5928
5929
f0051264
JW
5930static bool resolve_fl_derived (gfc_symbol *sym);
5931
28188747 5932
eece1eb9
PT
5933/* Resolve a typebound function, or 'method'. First separate all
5934 the non-CLASS references by calling resolve_compcall directly. */
6a943ee7 5935
524af0d6 5936static bool
6a943ee7 5937resolve_typebound_function (gfc_expr* e)
7cf078dc 5938{
eece1eb9
PT
5939 gfc_symbol *declared;
5940 gfc_component *c;
28188747
PT
5941 gfc_ref *new_ref;
5942 gfc_ref *class_ref;
5943 gfc_symtree *st;
eece1eb9 5944 const char *name;
eece1eb9 5945 gfc_typespec ts;
974df0f8 5946 gfc_expr *expr;
fd83db3d 5947 bool overridable;
28188747
PT
5948
5949 st = e->symtree;
974df0f8
PT
5950
5951 /* Deal with typebound operators for CLASS objects. */
5952 expr = e->value.compcall.base_object;
fd83db3d 5953 overridable = !e->value.compcall.tbp->non_overridable;
061e60bd 5954 if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
974df0f8 5955 {
94fae14b
PT
5956 /* If the base_object is not a variable, the corresponding actual
5957 argument expression must be stored in e->base_expression so
5958 that the corresponding tree temporary can be used as the base
5959 object in gfc_conv_procedure_call. */
5960 if (expr->expr_type != EXPR_VARIABLE)
5961 {
5962 gfc_actual_arglist *args;
5963
5964 for (args= e->value.function.actual; args; args = args->next)
5965 {
5966 if (expr == args->expr)
5967 expr = args->expr;
5968 }
5969 }
5970
974df0f8
PT
5971 /* Since the typebound operators are generic, we have to ensure
5972 that any delays in resolution are corrected and that the vtab
5973 is present. */
061e60bd 5974 ts = expr->ts;
974df0f8 5975 declared = ts.u.derived;
b04533af 5976 c = gfc_find_component (declared, "_vptr", true, true);
974df0f8
PT
5977 if (c->ts.u.derived == NULL)
5978 c->ts.u.derived = gfc_find_derived_vtab (declared);
5979
524af0d6
JB
5980 if (!resolve_compcall (e, &name))
5981 return false;
974df0f8
PT
5982
5983 /* Use the generic name if it is there. */
5984 name = name ? name : e->value.function.esym->name;
5985 e->symtree = expr->symtree;
d3735479 5986 e->ref = gfc_copy_ref (expr->ref);
94fae14b
PT
5987 get_declared_from_expr (&class_ref, NULL, e, false);
5988
5989 /* Trim away the extraneous references that emerge from nested
5990 use of interface.c (extend_expr). */
5991 if (class_ref && class_ref->next)
5992 {
5993 gfc_free_ref_list (class_ref->next);
5994 class_ref->next = NULL;
5995 }
5996 else if (e->ref && !class_ref)
5997 {
5998 gfc_free_ref_list (e->ref);
5999 e->ref = NULL;
6000 }
6001
b04533af 6002 gfc_add_vptr_component (e);
974df0f8
PT
6003 gfc_add_component_ref (e, name);
6004 e->value.function.esym = NULL;
94fae14b
PT
6005 if (expr->expr_type != EXPR_VARIABLE)
6006 e->base_expr = expr;
524af0d6 6007 return true;
974df0f8
PT
6008 }
6009
6a943ee7 6010 if (st == NULL)
eece1eb9 6011 return resolve_compcall (e, NULL);
7cf078dc 6012
524af0d6
JB
6013 if (!resolve_ref (e))
6014 return false;
f1a0b754 6015
28188747 6016 /* Get the CLASS declared type. */
94fae14b 6017 declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
22c23886 6018
f0051264
JW
6019 if (!resolve_fl_derived (declared))
6020 return false;
7cf078dc 6021
28188747 6022 /* Weed out cases of the ultimate component being a derived type. */
6a943ee7 6023 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
eece1eb9 6024 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
28188747
PT
6025 {
6026 gfc_free_ref_list (new_ref);
eece1eb9 6027 return resolve_compcall (e, NULL);
f116b2fc
PT
6028 }
6029
b04533af 6030 c = gfc_find_component (declared, "_data", true, true);
eece1eb9 6031 declared = c->ts.u.derived;
7cf078dc 6032
eece1eb9
PT
6033 /* Treat the call as if it is a typebound procedure, in order to roll
6034 out the correct name for the specific function. */
524af0d6 6035 if (!resolve_compcall (e, &name))
efb63364
TB
6036 {
6037 gfc_free_ref_list (new_ref);
524af0d6 6038 return false;
efb63364 6039 }
eece1eb9 6040 ts = e->ts;
7cf078dc 6041
fd83db3d
JW
6042 if (overridable)
6043 {
6044 /* Convert the expression to a procedure pointer component call. */
6045 e->value.function.esym = NULL;
6046 e->symtree = st;
7cf078dc 6047
4d382327 6048 if (new_ref)
fd83db3d 6049 e->ref = new_ref;
7cf078dc 6050
fd83db3d
JW
6051 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6052 gfc_add_vptr_component (e);
6053 gfc_add_component_ref (e, name);
6054
6055 /* Recover the typespec for the expression. This is really only
6056 necessary for generic procedures, where the additional call
6057 to gfc_add_component_ref seems to throw the collection of the
6058 correct typespec. */
6059 e->ts = ts;
6060 }
36abe895
TB
6061 else if (new_ref)
6062 gfc_free_ref_list (new_ref);
28188747 6063
524af0d6 6064 return true;
7cf078dc
PT
6065}
6066
eece1eb9
PT
6067/* Resolve a typebound subroutine, or 'method'. First separate all
6068 the non-CLASS references by calling resolve_typebound_call
6069 directly. */
6a943ee7 6070
524af0d6 6071static bool
6a943ee7 6072resolve_typebound_subroutine (gfc_code *code)
7cf078dc 6073{
974df0f8
PT
6074 gfc_symbol *declared;
6075 gfc_component *c;
28188747
PT
6076 gfc_ref *new_ref;
6077 gfc_ref *class_ref;
6078 gfc_symtree *st;
eece1eb9
PT
6079 const char *name;
6080 gfc_typespec ts;
974df0f8 6081 gfc_expr *expr;
fd83db3d 6082 bool overridable;
28188747
PT
6083
6084 st = code->expr1->symtree;
974df0f8
PT
6085
6086 /* Deal with typebound operators for CLASS objects. */
6087 expr = code->expr1->value.compcall.base_object;
fd83db3d 6088 overridable = !code->expr1->value.compcall.tbp->non_overridable;
b6c77bcb 6089 if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
974df0f8 6090 {
94fae14b
PT
6091 /* If the base_object is not a variable, the corresponding actual
6092 argument expression must be stored in e->base_expression so
6093 that the corresponding tree temporary can be used as the base
6094 object in gfc_conv_procedure_call. */
6095 if (expr->expr_type != EXPR_VARIABLE)
6096 {
6097 gfc_actual_arglist *args;
6098
6099 args= code->expr1->value.function.actual;
6100 for (; args; args = args->next)
6101 if (expr == args->expr)
6102 expr = args->expr;
6103 }
6104
974df0f8
PT
6105 /* Since the typebound operators are generic, we have to ensure
6106 that any delays in resolution are corrected and that the vtab
6107 is present. */
b6c77bcb 6108 declared = expr->ts.u.derived;
b04533af 6109 c = gfc_find_component (declared, "_vptr", true, true);
974df0f8
PT
6110 if (c->ts.u.derived == NULL)
6111 c->ts.u.derived = gfc_find_derived_vtab (declared);
6112
744868aa 6113 if (!resolve_typebound_call (code, &name, NULL))
524af0d6 6114 return false;
974df0f8
PT
6115
6116 /* Use the generic name if it is there. */
6117 name = name ? name : code->expr1->value.function.esym->name;
6118 code->expr1->symtree = expr->symtree;
b6c77bcb 6119 code->expr1->ref = gfc_copy_ref (expr->ref);
94fae14b
PT
6120
6121 /* Trim away the extraneous references that emerge from nested
6122 use of interface.c (extend_expr). */
6123 get_declared_from_expr (&class_ref, NULL, code->expr1, false);
6124 if (class_ref && class_ref->next)
6125 {
6126 gfc_free_ref_list (class_ref->next);
6127 class_ref->next = NULL;
6128 }
6129 else if (code->expr1->ref && !class_ref)
6130 {
6131 gfc_free_ref_list (code->expr1->ref);
6132 code->expr1->ref = NULL;
6133 }
6134
6135 /* Now use the procedure in the vtable. */
b04533af 6136 gfc_add_vptr_component (code->expr1);
974df0f8
PT
6137 gfc_add_component_ref (code->expr1, name);
6138 code->expr1->value.function.esym = NULL;
94fae14b
PT
6139 if (expr->expr_type != EXPR_VARIABLE)
6140 code->expr1->base_expr = expr;
524af0d6 6141 return true;
974df0f8
PT
6142 }
6143
6a943ee7 6144 if (st == NULL)
744868aa 6145 return resolve_typebound_call (code, NULL, NULL);
7cf078dc 6146
524af0d6
JB
6147 if (!resolve_ref (code->expr1))
6148 return false;
f1a0b754 6149
28188747 6150 /* Get the CLASS declared type. */
94fae14b 6151 get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7cf078dc 6152
28188747 6153 /* Weed out cases of the ultimate component being a derived type. */
6a943ee7 6154 if ((class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
eece1eb9 6155 || (!class_ref && st->n.sym->ts.type != BT_CLASS))
28188747
PT
6156 {
6157 gfc_free_ref_list (new_ref);
744868aa 6158 return resolve_typebound_call (code, NULL, NULL);
ab7306ed 6159 }
7cf078dc 6160
744868aa 6161 if (!resolve_typebound_call (code, &name, &overridable))
efb63364
TB
6162 {
6163 gfc_free_ref_list (new_ref);
524af0d6 6164 return false;
efb63364 6165 }
eece1eb9 6166 ts = code->expr1->ts;
7cf078dc 6167
fd83db3d
JW
6168 if (overridable)
6169 {
6170 /* Convert the expression to a procedure pointer component call. */
6171 code->expr1->value.function.esym = NULL;
6172 code->expr1->symtree = st;
7cf078dc 6173
fd83db3d
JW
6174 if (new_ref)
6175 code->expr1->ref = new_ref;
6176
6177 /* '_vptr' points to the vtab, which contains the procedure pointers. */
6178 gfc_add_vptr_component (code->expr1);
6179 gfc_add_component_ref (code->expr1, name);
7cf078dc 6180
fd83db3d
JW
6181 /* Recover the typespec for the expression. This is really only
6182 necessary for generic procedures, where the additional call
6183 to gfc_add_component_ref seems to throw the collection of the
6184 correct typespec. */
6185 code->expr1->ts = ts;
6186 }
adede54c
TB
6187 else if (new_ref)
6188 gfc_free_ref_list (new_ref);
28188747 6189
524af0d6 6190 return true;
8e1f752a
DK
6191}
6192
6193
713485cc
JW
6194/* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
6195
524af0d6 6196static bool
713485cc
JW
6197resolve_ppc_call (gfc_code* c)
6198{
6199 gfc_component *comp;
cf2b3c22 6200
2a573572
MM
6201 comp = gfc_get_proc_ptr_comp (c->expr1);
6202 gcc_assert (comp != NULL);
713485cc 6203
a513927a
SK
6204 c->resolved_sym = c->expr1->symtree->n.sym;
6205 c->expr1->expr_type = EXPR_VARIABLE;
713485cc
JW
6206
6207 if (!comp->attr.subroutine)
a513927a 6208 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
713485cc 6209
524af0d6
JB
6210 if (!resolve_ref (c->expr1))
6211 return false;
e35bbb23 6212
524af0d6
JB
6213 if (!update_ppc_arglist (c->expr1))
6214 return false;
90661f26
JW
6215
6216 c->ext.actual = c->expr1->value.compcall.actual;
6217
22c23886
PT
6218 if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
6219 !(comp->ts.interface
524af0d6
JB
6220 && comp->ts.interface->formal)))
6221 return false;
713485cc 6222
5930876d
JW
6223 if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
6224 return false;
6225
7e196f89 6226 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
713485cc 6227
524af0d6 6228 return true;
713485cc
JW
6229}
6230
6231
6232/* Resolve a Function Call to a Procedure Pointer Component (Function). */
6233
524af0d6 6234static bool
713485cc
JW
6235resolve_expr_ppc (gfc_expr* e)
6236{
6237 gfc_component *comp;
cf2b3c22 6238
2a573572
MM
6239 comp = gfc_get_proc_ptr_comp (e);
6240 gcc_assert (comp != NULL);
713485cc
JW
6241
6242 /* Convert to EXPR_FUNCTION. */
6243 e->expr_type = EXPR_FUNCTION;
6244 e->value.function.isym = NULL;
6245 e->value.function.actual = e->value.compcall.actual;
6246 e->ts = comp->ts;
c74b74a8
JW
6247 if (comp->as != NULL)
6248 e->rank = comp->as->rank;
713485cc
JW
6249
6250 if (!comp->attr.function)
6251 gfc_add_function (&comp->attr, comp->name, &e->where);
6252
524af0d6
JB
6253 if (!resolve_ref (e))
6254 return false;
e35bbb23 6255
22c23886
PT
6256 if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
6257 !(comp->ts.interface
524af0d6
JB
6258 && comp->ts.interface->formal)))
6259 return false;
713485cc 6260
524af0d6
JB
6261 if (!update_ppc_arglist (e))
6262 return false;
90661f26 6263
5930876d
JW
6264 if (!check_pure_function(e))
6265 return false;
6266
7e196f89 6267 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
713485cc 6268
524af0d6 6269 return true;
713485cc
JW
6270}
6271
6272
f2ff577a
JD
6273static bool
6274gfc_is_expandable_expr (gfc_expr *e)
6275{
6276 gfc_constructor *con;
6277
6278 if (e->expr_type == EXPR_ARRAY)
6279 {
6280 /* Traverse the constructor looking for variables that are flavor
6281 parameter. Parameters must be expanded since they are fully used at
6282 compile time. */
b7e75771
JD
6283 con = gfc_constructor_first (e->value.constructor);
6284 for (; con; con = gfc_constructor_next (con))
f2ff577a
JD
6285 {
6286 if (con->expr->expr_type == EXPR_VARIABLE
b7e75771
JD
6287 && con->expr->symtree
6288 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
f2ff577a
JD
6289 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
6290 return true;
6291 if (con->expr->expr_type == EXPR_ARRAY
b7e75771 6292 && gfc_is_expandable_expr (con->expr))
f2ff577a
JD
6293 return true;
6294 }
6295 }
6296
6297 return false;
6298}
6299
6de9cd9a
DN
6300/* Resolve an expression. That is, make sure that types of operands agree
6301 with their operators, intrinsic operators are converted to function calls
6302 for overloaded types and unresolved function references are resolved. */
6303
524af0d6 6304bool
edf1eac2 6305gfc_resolve_expr (gfc_expr *e)
6de9cd9a 6306{
524af0d6 6307 bool t;
c62c6622 6308 bool inquiry_save, actual_arg_save, first_actual_arg_save;
6de9cd9a
DN
6309
6310 if (e == NULL)
524af0d6 6311 return true;
6de9cd9a 6312
d3a9eea2
TB
6313 /* inquiry_argument only applies to variables. */
6314 inquiry_save = inquiry_argument;
c62c6622
TB
6315 actual_arg_save = actual_arg;
6316 first_actual_arg_save = first_actual_arg;
6317
d3a9eea2 6318 if (e->expr_type != EXPR_VARIABLE)
c62c6622
TB
6319 {
6320 inquiry_argument = false;
6321 actual_arg = false;
6322 first_actual_arg = false;
6323 }
d3a9eea2 6324
6de9cd9a
DN
6325 switch (e->expr_type)
6326 {
6327 case EXPR_OP:
6328 t = resolve_operator (e);
6329 break;
6330
6331 case EXPR_FUNCTION:
6de9cd9a 6332 case EXPR_VARIABLE:
eb77cddf
PT
6333
6334 if (check_host_association (e))
6335 t = resolve_function (e);
6336 else
8a8d1a16 6337 t = resolve_variable (e);
07368af0 6338
bc21d315 6339 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
9de88093 6340 && e->ref->type != REF_SUBSTRING)
07368af0
PT
6341 gfc_resolve_substring_charlen (e);
6342
6de9cd9a
DN
6343 break;
6344
8e1f752a 6345 case EXPR_COMPCALL:
6a943ee7 6346 t = resolve_typebound_function (e);
8e1f752a
DK
6347 break;
6348
6de9cd9a
DN
6349 case EXPR_SUBSTRING:
6350 t = resolve_ref (e);
6351 break;
6352
6353 case EXPR_CONSTANT:
6354 case EXPR_NULL:
524af0d6 6355 t = true;
6de9cd9a
DN
6356 break;
6357
713485cc
JW
6358 case EXPR_PPC:
6359 t = resolve_expr_ppc (e);
6360 break;
6361
6de9cd9a 6362 case EXPR_ARRAY:
524af0d6
JB
6363 t = false;
6364 if (!resolve_ref (e))
6de9cd9a
DN
6365 break;
6366
6367 t = gfc_resolve_array_constructor (e);
6368 /* Also try to expand a constructor. */
524af0d6 6369 if (t)
6de9cd9a
DN
6370 {
6371 expression_rank (e);
f2ff577a 6372 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
928f0490 6373 gfc_expand_constructor (e, false);
6de9cd9a 6374 }
1855915a 6375
edf1eac2 6376 /* This provides the opportunity for the length of constructors with
86bf520d 6377 character valued function elements to propagate the string length
edf1eac2 6378 to the expression. */
524af0d6 6379 if (t && e->ts.type == BT_CHARACTER)
f2ff577a
JD
6380 {
6381 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
4d382327 6382 here rather then add a duplicate test for it above. */
928f0490 6383 gfc_expand_constructor (e, false);
f2ff577a
JD
6384 t = gfc_resolve_character_array_constructor (e);
6385 }
6de9cd9a
DN
6386
6387 break;
6388
6389 case EXPR_STRUCTURE:
6390 t = resolve_ref (e);
524af0d6 6391 if (!t)
6de9cd9a
DN
6392 break;
6393
80f95228 6394 t = resolve_structure_cons (e, 0);
524af0d6 6395 if (!t)
6de9cd9a
DN
6396 break;
6397
6398 t = gfc_simplify_expr (e, 0);
6399 break;
6400
6401 default:
6402 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
6403 }
6404
524af0d6 6405 if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
07368af0
PT
6406 fixup_charlen (e);
6407
d3a9eea2 6408 inquiry_argument = inquiry_save;
c62c6622
TB
6409 actual_arg = actual_arg_save;
6410 first_actual_arg = first_actual_arg_save;
d3a9eea2 6411
6de9cd9a
DN
6412 return t;
6413}
6414
6415
8d5cfa27
SK
6416/* Resolve an expression from an iterator. They must be scalar and have
6417 INTEGER or (optionally) REAL type. */
6de9cd9a 6418
524af0d6 6419static bool
edf1eac2
SK
6420gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
6421 const char *name_msgid)
6de9cd9a 6422{
524af0d6
JB
6423 if (!gfc_resolve_expr (expr))
6424 return false;
6de9cd9a 6425
8d5cfa27 6426 if (expr->rank != 0)
6de9cd9a 6427 {
31043f6c 6428 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
524af0d6 6429 return false;
6de9cd9a
DN
6430 }
6431
79e7840d 6432 if (expr->ts.type != BT_INTEGER)
6de9cd9a 6433 {
79e7840d
JD
6434 if (expr->ts.type == BT_REAL)
6435 {
6436 if (real_ok)
6437 return gfc_notify_std (GFC_STD_F95_DEL,
9717f7a1 6438 "%s at %L must be integer",
79e7840d
JD
6439 _(name_msgid), &expr->where);
6440 else
6441 {
6442 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
6443 &expr->where);
524af0d6 6444 return false;
79e7840d
JD
6445 }
6446 }
31043f6c 6447 else
79e7840d
JD
6448 {
6449 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
524af0d6 6450 return false;
79e7840d 6451 }
6de9cd9a 6452 }
524af0d6 6453 return true;
8d5cfa27
SK
6454}
6455
6456
6457/* Resolve the expressions in an iterator structure. If REAL_OK is
57bf28ea
TB
6458 false allow only INTEGER type iterators, otherwise allow REAL types.
6459 Set own_scope to true for ac-implied-do and data-implied-do as those
6460 have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8d5cfa27 6461
524af0d6 6462bool
57bf28ea 6463gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8d5cfa27 6464{
524af0d6
JB
6465 if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
6466 return false;
6de9cd9a 6467
22c23886 6468 if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
524af0d6
JB
6469 _("iterator variable")))
6470 return false;
6de9cd9a 6471
22c23886 6472 if (!gfc_resolve_iterator_expr (iter->start, real_ok,
524af0d6
JB
6473 "Start expression in DO loop"))
6474 return false;
6de9cd9a 6475
22c23886 6476 if (!gfc_resolve_iterator_expr (iter->end, real_ok,
524af0d6
JB
6477 "End expression in DO loop"))
6478 return false;
6de9cd9a 6479
22c23886 6480 if (!gfc_resolve_iterator_expr (iter->step, real_ok,
524af0d6
JB
6481 "Step expression in DO loop"))
6482 return false;
6de9cd9a 6483
8d5cfa27 6484 if (iter->step->expr_type == EXPR_CONSTANT)
6de9cd9a 6485 {
8d5cfa27
SK
6486 if ((iter->step->ts.type == BT_INTEGER
6487 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
6488 || (iter->step->ts.type == BT_REAL
6489 && mpfr_sgn (iter->step->value.real) == 0))
6490 {
6491 gfc_error ("Step expression in DO loop at %L cannot be zero",
6492 &iter->step->where);
524af0d6 6493 return false;
8d5cfa27 6494 }
6de9cd9a
DN
6495 }
6496
8d5cfa27
SK
6497 /* Convert start, end, and step to the same type as var. */
6498 if (iter->start->ts.kind != iter->var->ts.kind
6499 || iter->start->ts.type != iter->var->ts.type)
6500 gfc_convert_type (iter->start, &iter->var->ts, 2);
6501
6502 if (iter->end->ts.kind != iter->var->ts.kind
6503 || iter->end->ts.type != iter->var->ts.type)
6504 gfc_convert_type (iter->end, &iter->var->ts, 2);
6505
6506 if (iter->step->ts.kind != iter->var->ts.kind
6507 || iter->step->ts.type != iter->var->ts.type)
6508 gfc_convert_type (iter->step, &iter->var->ts, 2);
6de9cd9a 6509
dc186969
TB
6510 if (iter->start->expr_type == EXPR_CONSTANT
6511 && iter->end->expr_type == EXPR_CONSTANT
6512 && iter->step->expr_type == EXPR_CONSTANT)
6513 {
6514 int sgn, cmp;
6515 if (iter->start->ts.type == BT_INTEGER)
6516 {
6517 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
6518 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
6519 }
6520 else
6521 {
6522 sgn = mpfr_sgn (iter->step->value.real);
6523 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
6524 }
73e42eef 6525 if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
48749dbc
MLI
6526 gfc_warning (OPT_Wzerotrip,
6527 "DO loop at %L will be executed zero times",
dc186969
TB
6528 &iter->step->where);
6529 }
6530
524af0d6 6531 return true;
6de9cd9a
DN
6532}
6533
6534
640670c7
PT
6535/* Traversal function for find_forall_index. f == 2 signals that
6536 that variable itself is not to be checked - only the references. */
ac5ba373 6537
640670c7
PT
6538static bool
6539forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
ac5ba373 6540{
908a2235
PT
6541 if (expr->expr_type != EXPR_VARIABLE)
6542 return false;
4d382327 6543
640670c7
PT
6544 /* A scalar assignment */
6545 if (!expr->ref || *f == 1)
ac5ba373 6546 {
640670c7
PT
6547 if (expr->symtree->n.sym == sym)
6548 return true;
6549 else
6550 return false;
6551 }
ac5ba373 6552
640670c7
PT
6553 if (*f == 2)
6554 *f = 1;
6555 return false;
6556}
ac5ba373 6557
ac5ba373 6558
640670c7 6559/* Check whether the FORALL index appears in the expression or not.
524af0d6 6560 Returns true if SYM is found in EXPR. */
ac5ba373 6561
524af0d6 6562bool
640670c7
PT
6563find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
6564{
6565 if (gfc_traverse_expr (expr, sym, forall_index, f))
524af0d6 6566 return true;
640670c7 6567 else
524af0d6 6568 return false;
ac5ba373
TS
6569}
6570
6571
1c54741a
SK
6572/* Resolve a list of FORALL iterators. The FORALL index-name is constrained
6573 to be a scalar INTEGER variable. The subscripts and stride are scalar
ac5ba373
TS
6574 INTEGERs, and if stride is a constant it must be nonzero.
6575 Furthermore "A subscript or stride in a forall-triplet-spec shall
6576 not contain a reference to any index-name in the
6577 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6de9cd9a
DN
6578
6579static void
ac5ba373 6580resolve_forall_iterators (gfc_forall_iterator *it)
6de9cd9a 6581{
ac5ba373
TS
6582 gfc_forall_iterator *iter, *iter2;
6583
6584 for (iter = it; iter; iter = iter->next)
6de9cd9a 6585 {
524af0d6 6586 if (gfc_resolve_expr (iter->var)
1c54741a
SK
6587 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
6588 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6de9cd9a
DN
6589 &iter->var->where);
6590
524af0d6 6591 if (gfc_resolve_expr (iter->start)
1c54741a
SK
6592 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
6593 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6de9cd9a
DN
6594 &iter->start->where);
6595 if (iter->var->ts.kind != iter->start->ts.kind)
7298eef3 6596 gfc_convert_type (iter->start, &iter->var->ts, 1);
6de9cd9a 6597
524af0d6 6598 if (gfc_resolve_expr (iter->end)
1c54741a
SK
6599 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
6600 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6de9cd9a
DN
6601 &iter->end->where);
6602 if (iter->var->ts.kind != iter->end->ts.kind)
7298eef3 6603 gfc_convert_type (iter->end, &iter->var->ts, 1);
6de9cd9a 6604
524af0d6 6605 if (gfc_resolve_expr (iter->stride))
1c54741a
SK
6606 {
6607 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
6608 gfc_error ("FORALL stride expression at %L must be a scalar %s",
edf1eac2 6609 &iter->stride->where, "INTEGER");
1c54741a
SK
6610
6611 if (iter->stride->expr_type == EXPR_CONSTANT
524af0d6 6612 && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
1c54741a
SK
6613 gfc_error ("FORALL stride expression at %L cannot be zero",
6614 &iter->stride->where);
6615 }
6de9cd9a 6616 if (iter->var->ts.kind != iter->stride->ts.kind)
7298eef3 6617 gfc_convert_type (iter->stride, &iter->var->ts, 1);
6de9cd9a 6618 }
ac5ba373
TS
6619
6620 for (iter = it; iter; iter = iter->next)
6621 for (iter2 = iter; iter2; iter2 = iter2->next)
6622 {
524af0d6
JB
6623 if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
6624 || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
6625 || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
a4d9b221 6626 gfc_error ("FORALL index %qs may not appear in triplet "
ac5ba373
TS
6627 "specification at %L", iter->var->symtree->name,
6628 &iter2->start->where);
6629 }
6de9cd9a
DN
6630}
6631
6632
8451584a
EE
6633/* Given a pointer to a symbol that is a derived type, see if it's
6634 inaccessible, i.e. if it's defined in another module and the components are
6635 PRIVATE. The search is recursive if necessary. Returns zero if no
6636 inaccessible components are found, nonzero otherwise. */
6637
6638static int
6639derived_inaccessible (gfc_symbol *sym)
6640{
6641 gfc_component *c;
6642
3dbf6538 6643 if (sym->attr.use_assoc && sym->attr.private_comp)
8451584a
EE
6644 return 1;
6645
6646 for (c = sym->components; c; c = c->next)
6647 {
bc21d315 6648 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
edf1eac2 6649 return 1;
8451584a
EE
6650 }
6651
6652 return 0;
6653}
6654
6655
6de9cd9a
DN
6656/* Resolve the argument of a deallocate expression. The expression must be
6657 a pointer or a full array. */
6658
524af0d6 6659static bool
edf1eac2 6660resolve_deallocate_expr (gfc_expr *e)
6de9cd9a
DN
6661{
6662 symbol_attribute attr;
8c91ab34 6663 int allocatable, pointer;
6de9cd9a 6664 gfc_ref *ref;
cf2b3c22
TB
6665 gfc_symbol *sym;
6666 gfc_component *c;
8b704316 6667 bool unlimited;
6de9cd9a 6668
524af0d6
JB
6669 if (!gfc_resolve_expr (e))
6670 return false;
6de9cd9a 6671
6de9cd9a
DN
6672 if (e->expr_type != EXPR_VARIABLE)
6673 goto bad;
6674
cf2b3c22 6675 sym = e->symtree->n.sym;
8b704316 6676 unlimited = UNLIMITED_POLY(sym);
cf2b3c22
TB
6677
6678 if (sym->ts.type == BT_CLASS)
6679 {
7a08eda1 6680 allocatable = CLASS_DATA (sym)->attr.allocatable;
d40477b4 6681 pointer = CLASS_DATA (sym)->attr.class_pointer;
cf2b3c22
TB
6682 }
6683 else
6684 {
6685 allocatable = sym->attr.allocatable;
6686 pointer = sym->attr.pointer;
6687 }
6de9cd9a 6688 for (ref = e->ref; ref; ref = ref->next)
f17facac 6689 {
f17facac 6690 switch (ref->type)
edf1eac2
SK
6691 {
6692 case REF_ARRAY:
badd9e69
TB
6693 if (ref->u.ar.type != AR_FULL
6694 && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
6695 && ref->u.ar.codimen && gfc_ref_this_image (ref)))
f17facac
TB
6696 allocatable = 0;
6697 break;
6de9cd9a 6698
edf1eac2 6699 case REF_COMPONENT:
cf2b3c22
TB
6700 c = ref->u.c.component;
6701 if (c->ts.type == BT_CLASS)
6702 {
7a08eda1 6703 allocatable = CLASS_DATA (c)->attr.allocatable;
d40477b4 6704 pointer = CLASS_DATA (c)->attr.class_pointer;
cf2b3c22
TB
6705 }
6706 else
6707 {
6708 allocatable = c->attr.allocatable;
6709 pointer = c->attr.pointer;
6710 }
f17facac 6711 break;
6de9cd9a 6712
edf1eac2 6713 case REF_SUBSTRING:
f17facac
TB
6714 allocatable = 0;
6715 break;
edf1eac2 6716 }
f17facac
TB
6717 }
6718
6719 attr = gfc_expr_attr (e);
6720
8b704316 6721 if (allocatable == 0 && attr.pointer == 0 && !unlimited)
6de9cd9a
DN
6722 {
6723 bad:
3759634f
SK
6724 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6725 &e->where);
524af0d6 6726 return false;
6de9cd9a
DN
6727 }
6728
5aacb11e
TB
6729 /* F2008, C644. */
6730 if (gfc_is_coindexed (e))
6731 {
6732 gfc_error ("Coindexed allocatable object at %L", &e->where);
524af0d6 6733 return false;
5aacb11e
TB
6734 }
6735
8c91ab34 6736 if (pointer
22c23886 6737 && !gfc_check_vardef_context (e, true, true, false,
524af0d6
JB
6738 _("DEALLOCATE object")))
6739 return false;
22c23886 6740 if (!gfc_check_vardef_context (e, false, true, false,
524af0d6
JB
6741 _("DEALLOCATE object")))
6742 return false;
aa08038d 6743
524af0d6 6744 return true;
6de9cd9a
DN
6745}
6746
edf1eac2 6747
908a2235 6748/* Returns true if the expression e contains a reference to the symbol sym. */
77726571 6749static bool
908a2235 6750sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
77726571 6751{
908a2235
PT
6752 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
6753 return true;
77726571 6754
908a2235
PT
6755 return false;
6756}
77726571 6757
a68ab351
JJ
6758bool
6759gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
908a2235
PT
6760{
6761 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
77726571
PT
6762}
6763
6de9cd9a 6764
68577e56
EE
6765/* Given the expression node e for an allocatable/pointer of derived type to be
6766 allocated, get the expression node to be initialized afterwards (needed for
5046aff5
PT
6767 derived types with default initializers, and derived types with allocatable
6768 components that need nullification.) */
68577e56 6769
cf2b3c22
TB
6770gfc_expr *
6771gfc_expr_to_initialize (gfc_expr *e)
68577e56
EE
6772{
6773 gfc_expr *result;
6774 gfc_ref *ref;
6775 int i;
6776
6777 result = gfc_copy_expr (e);
6778
6779 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6780 for (ref = result->ref; ref; ref = ref->next)
6781 if (ref->type == REF_ARRAY && ref->next == NULL)
6782 {
edf1eac2 6783 ref->u.ar.type = AR_FULL;
68577e56 6784
edf1eac2
SK
6785 for (i = 0; i < ref->u.ar.dimen; i++)
6786 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
68577e56 6787
edf1eac2 6788 break;
68577e56
EE
6789 }
6790
7d7212ec
MM
6791 gfc_free_shape (&result->shape, result->rank);
6792
6793 /* Recalculate rank, shape, etc. */
6794 gfc_resolve_expr (result);
68577e56
EE
6795 return result;
6796}
6797
6798
8c91ab34
DK
6799/* If the last ref of an expression is an array ref, return a copy of the
6800 expression with that one removed. Otherwise, a copy of the original
6801 expression. This is used for allocate-expressions and pointer assignment
6802 LHS, where there may be an array specification that needs to be stripped
6803 off when using gfc_check_vardef_context. */
6804
6805static gfc_expr*
6806remove_last_array_ref (gfc_expr* e)
6807{
6808 gfc_expr* e2;
6809 gfc_ref** r;
6810
6811 e2 = gfc_copy_expr (e);
6812 for (r = &e2->ref; *r; r = &(*r)->next)
6813 if ((*r)->type == REF_ARRAY && !(*r)->next)
6814 {
6815 gfc_free_ref_list (*r);
6816 *r = NULL;
6817 break;
6818 }
6819
6820 return e2;
6821}
6822
6823
8460475b 6824/* Used in resolve_allocate_expr to check that a allocation-object and
4d382327 6825 a source-expr are conformable. This does not catch all possible
8460475b
JW
6826 cases; in particular a runtime checking is needed. */
6827
524af0d6 6828static bool
8460475b
JW
6829conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6830{
66051b60
JW
6831 gfc_ref *tail;
6832 for (tail = e2->ref; tail && tail->next; tail = tail->next);
4d382327 6833
8460475b 6834 /* First compare rank. */
2ccd6f72
JW
6835 if ((tail && e1->rank != tail->u.ar.as->rank)
6836 || (!tail && e1->rank != e2->rank))
8460475b
JW
6837 {
6838 gfc_error ("Source-expr at %L must be scalar or have the "
6839 "same rank as the allocate-object at %L",
6840 &e1->where, &e2->where);
524af0d6 6841 return false;
8460475b
JW
6842 }
6843
6844 if (e1->shape)
6845 {
6846 int i;
6847 mpz_t s;
6848
6849 mpz_init (s);
6850
6851 for (i = 0; i < e1->rank; i++)
6852 {
f0470cc5
TB
6853 if (tail->u.ar.start[i] == NULL)
6854 break;
6855
66051b60 6856 if (tail->u.ar.end[i])
8460475b 6857 {
66051b60
JW
6858 mpz_set (s, tail->u.ar.end[i]->value.integer);
6859 mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8460475b
JW
6860 mpz_add_ui (s, s, 1);
6861 }
6862 else
6863 {
66051b60 6864 mpz_set (s, tail->u.ar.start[i]->value.integer);
8460475b
JW
6865 }
6866
6867 if (mpz_cmp (e1->shape[i], s) != 0)
6868 {
fea70c99 6869 gfc_error ("Source-expr at %L and allocate-object at %L must "
8460475b
JW
6870 "have the same shape", &e1->where, &e2->where);
6871 mpz_clear (s);
524af0d6 6872 return false;
8460475b
JW
6873 }
6874 }
6875
6876 mpz_clear (s);
6877 }
6878
524af0d6 6879 return true;
8460475b
JW
6880}
6881
6882
6de9cd9a
DN
6883/* Resolve the expression in an ALLOCATE statement, doing the additional
6884 checks to see whether the expression is OK or not. The expression must
6885 have a trailing array reference that gives the size of the array. */
6886
524af0d6 6887static bool
1792349b 6888resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
6de9cd9a 6889{
8c91ab34 6890 int i, pointer, allocatable, dimension, is_abstract;
d3a9eea2 6891 int codimension;
c49eaa23 6892 bool coindexed;
8b704316 6893 bool unlimited;
6de9cd9a
DN
6894 symbol_attribute attr;
6895 gfc_ref *ref, *ref2;
8c91ab34 6896 gfc_expr *e2;
6de9cd9a 6897 gfc_array_ref *ar;
0d7d4951 6898 gfc_symbol *sym = NULL;
77726571 6899 gfc_alloc *a;
cf2b3c22 6900 gfc_component *c;
524af0d6 6901 bool t;
f17facac 6902
eea58adb 6903 /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
d3a9eea2
TB
6904 checking of coarrays. */
6905 for (ref = e->ref; ref; ref = ref->next)
6906 if (ref->next == NULL)
6907 break;
6908
6909 if (ref && ref->type == REF_ARRAY)
6910 ref->u.ar.in_allocate = true;
6911
524af0d6 6912 if (!gfc_resolve_expr (e))
d3a9eea2 6913 goto failure;
6de9cd9a
DN
6914
6915 /* Make sure the expression is allocatable or a pointer. If it is
6916 pointer, the next-to-last reference must be a pointer. */
6917
6918 ref2 = NULL;
cf2b3c22
TB
6919 if (e->symtree)
6920 sym = e->symtree->n.sym;
6de9cd9a 6921
d0a9804e
TB
6922 /* Check whether ultimate component is abstract and CLASS. */
6923 is_abstract = 0;
6924
8b704316
PT
6925 /* Is the allocate-object unlimited polymorphic? */
6926 unlimited = UNLIMITED_POLY(e);
6927
6de9cd9a
DN
6928 if (e->expr_type != EXPR_VARIABLE)
6929 {
6930 allocatable = 0;
6de9cd9a
DN
6931 attr = gfc_expr_attr (e);
6932 pointer = attr.pointer;
6933 dimension = attr.dimension;
d3a9eea2 6934 codimension = attr.codimension;
6de9cd9a
DN
6935 }
6936 else
6937 {
c49ea23d 6938 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
cf2b3c22 6939 {
7a08eda1 6940 allocatable = CLASS_DATA (sym)->attr.allocatable;
d40477b4 6941 pointer = CLASS_DATA (sym)->attr.class_pointer;
7a08eda1
JW
6942 dimension = CLASS_DATA (sym)->attr.dimension;
6943 codimension = CLASS_DATA (sym)->attr.codimension;
6944 is_abstract = CLASS_DATA (sym)->attr.abstract;
cf2b3c22
TB
6945 }
6946 else
6947 {
6948 allocatable = sym->attr.allocatable;
6949 pointer = sym->attr.pointer;
6950 dimension = sym->attr.dimension;
d3a9eea2 6951 codimension = sym->attr.codimension;
cf2b3c22 6952 }
6de9cd9a 6953
c49eaa23
TB
6954 coindexed = false;
6955
6de9cd9a 6956 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
edf1eac2 6957 {
f17facac
TB
6958 switch (ref->type)
6959 {
6960 case REF_ARRAY:
c49eaa23
TB
6961 if (ref->u.ar.codimen > 0)
6962 {
6963 int n;
6964 for (n = ref->u.ar.dimen;
6965 n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
6966 if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
6967 {
6968 coindexed = true;
6969 break;
6970 }
6971 }
6972
edf1eac2
SK
6973 if (ref->next != NULL)
6974 pointer = 0;
6975 break;
f17facac
TB
6976
6977 case REF_COMPONENT:
d3a9eea2 6978 /* F2008, C644. */
c49eaa23 6979 if (coindexed)
d3a9eea2
TB
6980 {
6981 gfc_error ("Coindexed allocatable object at %L",
6982 &e->where);
6983 goto failure;
6984 }
6985
cf2b3c22
TB
6986 c = ref->u.c.component;
6987 if (c->ts.type == BT_CLASS)
6988 {
7a08eda1 6989 allocatable = CLASS_DATA (c)->attr.allocatable;
d40477b4 6990 pointer = CLASS_DATA (c)->attr.class_pointer;
7a08eda1
JW
6991 dimension = CLASS_DATA (c)->attr.dimension;
6992 codimension = CLASS_DATA (c)->attr.codimension;
6993 is_abstract = CLASS_DATA (c)->attr.abstract;
cf2b3c22
TB
6994 }
6995 else
6996 {
6997 allocatable = c->attr.allocatable;
6998 pointer = c->attr.pointer;
6999 dimension = c->attr.dimension;
d3a9eea2 7000 codimension = c->attr.codimension;
d0a9804e 7001 is_abstract = c->attr.abstract;
cf2b3c22 7002 }
edf1eac2 7003 break;
f17facac
TB
7004
7005 case REF_SUBSTRING:
edf1eac2
SK
7006 allocatable = 0;
7007 pointer = 0;
7008 break;
f17facac 7009 }
8e1f752a 7010 }
6de9cd9a
DN
7011 }
7012
98cf47d1 7013 /* Check for F08:C628. */
8b704316 7014 if (allocatable == 0 && pointer == 0 && !unlimited)
6de9cd9a 7015 {
3759634f
SK
7016 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
7017 &e->where);
d3a9eea2 7018 goto failure;
6de9cd9a
DN
7019 }
7020
8460475b
JW
7021 /* Some checks for the SOURCE tag. */
7022 if (code->expr3)
7023 {
7024 /* Check F03:C631. */
7025 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
7026 {
fea70c99
MLI
7027 gfc_error ("Type of entity at %L is type incompatible with "
7028 "source-expr at %L", &e->where, &code->expr3->where);
d3a9eea2 7029 goto failure;
8460475b
JW
7030 }
7031
7032 /* Check F03:C632 and restriction following Note 6.18. */
2ccd6f72 7033 if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
d3a9eea2 7034 goto failure;
8460475b
JW
7035
7036 /* Check F03:C633. */
8b704316 7037 if (code->expr3->ts.kind != e->ts.kind && !unlimited)
8460475b 7038 {
fea70c99
MLI
7039 gfc_error ("The allocate-object at %L and the source-expr at %L "
7040 "shall have the same kind type parameter",
7041 &e->where, &code->expr3->where);
d3a9eea2 7042 goto failure;
8460475b 7043 }
fea54935
TB
7044
7045 /* Check F2008, C642. */
7046 if (code->expr3->ts.type == BT_DERIVED
3b6fa7a5 7047 && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
fea54935
TB
7048 || (code->expr3->ts.u.derived->from_intmod
7049 == INTMOD_ISO_FORTRAN_ENV
7050 && code->expr3->ts.u.derived->intmod_sym_id
7051 == ISOFORTRAN_LOCK_TYPE)))
7052 {
fea70c99 7053 gfc_error ("The source-expr at %L shall neither be of type "
fea54935
TB
7054 "LOCK_TYPE nor have a LOCK_TYPE component if "
7055 "allocate-object at %L is a coarray",
7056 &code->expr3->where, &e->where);
7057 goto failure;
7058 }
5df445a2
TB
7059
7060 /* Check TS18508, C702/C703. */
7061 if (code->expr3->ts.type == BT_DERIVED
7062 && ((codimension && gfc_expr_attr (code->expr3).event_comp)
7063 || (code->expr3->ts.u.derived->from_intmod
7064 == INTMOD_ISO_FORTRAN_ENV
7065 && code->expr3->ts.u.derived->intmod_sym_id
7066 == ISOFORTRAN_EVENT_TYPE)))
7067 {
7068 gfc_error ("The source-expr at %L shall neither be of type "
7069 "EVENT_TYPE nor have a EVENT_TYPE component if "
7070 "allocate-object at %L is a coarray",
7071 &code->expr3->where, &e->where);
7072 goto failure;
7073 }
8460475b 7074 }
94bff632
JW
7075
7076 /* Check F08:C629. */
7077 if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
7078 && !code->expr3)
d0a9804e
TB
7079 {
7080 gcc_assert (e->ts.type == BT_CLASS);
7081 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
94bff632 7082 "type-spec or source-expr", sym->name, &e->where);
d3a9eea2 7083 goto failure;
d0a9804e
TB
7084 }
7085
e3a7c6cf
AV
7086 /* Check F08:C632. */
7087 if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
7088 && !UNLIMITED_POLY (e))
2e0bffaf
TB
7089 {
7090 int cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
7091 code->ext.alloc.ts.u.cl->length);
7092 if (cmp == 1 || cmp == -1 || cmp == -3)
7093 {
7094 gfc_error ("Allocating %s at %L with type-spec requires the same "
7095 "character-length parameter as in the declaration",
7096 sym->name, &e->where);
7097 goto failure;
7098 }
7099 }
7100
8c91ab34
DK
7101 /* In the variable definition context checks, gfc_expr_attr is used
7102 on the expression. This is fooled by the array specification
7103 present in e, thus we have to eliminate that one temporarily. */
7104 e2 = remove_last_array_ref (e);
524af0d6
JB
7105 t = true;
7106 if (t && pointer)
22c23886 7107 t = gfc_check_vardef_context (e2, true, true, false,
524af0d6
JB
7108 _("ALLOCATE object"));
7109 if (t)
22c23886 7110 t = gfc_check_vardef_context (e2, false, true, false,
524af0d6 7111 _("ALLOCATE object"));
8c91ab34 7112 gfc_free_expr (e2);
524af0d6 7113 if (!t)
8c91ab34 7114 goto failure;
aa08038d 7115
c49ea23d
PT
7116 if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
7117 && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
7118 {
7119 /* For class arrays, the initialization with SOURCE is done
7120 using _copy and trans_call. It is convenient to exploit that
7121 when the allocated type is different from the declared type but
7122 no SOURCE exists by setting expr3. */
4d382327 7123 code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
c49ea23d 7124 }
5df445a2
TB
7125 else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
7126 && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
7127 && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
7128 {
7129 /* We have to zero initialize the integer variable. */
7130 code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
7131 }
c49ea23d 7132 else if (!code->expr3)
b6ff8128
JW
7133 {
7134 /* Set up default initializer if needed. */
7135 gfc_typespec ts;
03d79dc3 7136 gfc_expr *init_e;
b6ff8128
JW
7137
7138 if (code->ext.alloc.ts.type == BT_DERIVED)
7139 ts = code->ext.alloc.ts;
7140 else
7141 ts = e->ts;
7142
7143 if (ts.type == BT_CLASS)
7144 ts = ts.u.derived->components->ts;
7145
03d79dc3 7146 if (ts.type == BT_DERIVED && (init_e = gfc_default_initializer (&ts)))
b6ff8128 7147 {
11e5274a 7148 gfc_code *init_st = gfc_get_code (EXEC_INIT_ASSIGN);
edd2b56a 7149 init_st->loc = code->loc;
edd2b56a
JW
7150 init_st->expr1 = gfc_expr_to_initialize (e);
7151 init_st->expr2 = init_e;
7152 init_st->next = code->next;
7153 code->next = init_st;
b6ff8128
JW
7154 }
7155 }
7156 else if (code->expr3->mold && code->expr3->ts.type == BT_DERIVED)
7157 {
7158 /* Default initialization via MOLD (non-polymorphic). */
7159 gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
40a778bd
PT
7160 if (rhs != NULL)
7161 {
7162 gfc_resolve_expr (rhs);
7163 gfc_free_expr (code->expr3);
7164 code->expr3 = rhs;
7165 }
b6ff8128
JW
7166 }
7167
8b704316 7168 if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
e10f52d0
JW
7169 {
7170 /* Make sure the vtab symbol is present when
7171 the module variables are generated. */
7172 gfc_typespec ts = e->ts;
7173 if (code->expr3)
7174 ts = code->expr3->ts;
7175 else if (code->ext.alloc.ts.type == BT_DERIVED)
7176 ts = code->ext.alloc.ts;
8b704316 7177
e10f52d0 7178 gfc_find_derived_vtab (ts.u.derived);
8b704316
PT
7179
7180 if (dimension)
7181 e = gfc_expr_to_initialize (e);
7182 }
7183 else if (unlimited && !UNLIMITED_POLY (code->expr3))
7184 {
7185 /* Again, make sure the vtab symbol is present when
7186 the module variables are generated. */
7187 gfc_typespec *ts = NULL;
7188 if (code->expr3)
7189 ts = &code->expr3->ts;
7190 else
7191 ts = &code->ext.alloc.ts;
7192
7193 gcc_assert (ts);
7194
7289d1c9 7195 gfc_find_vtab (ts);
8b704316 7196
c49ea23d
PT
7197 if (dimension)
7198 e = gfc_expr_to_initialize (e);
e10f52d0
JW
7199 }
7200
b21a544b 7201 if (dimension == 0 && codimension == 0)
d3a9eea2 7202 goto success;
6de9cd9a 7203
eea58adb 7204 /* Make sure the last reference node is an array specification. */
6de9cd9a 7205
8c91ab34 7206 if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
d3a9eea2 7207 || (dimension && ref2->u.ar.dimen == 0))
6de9cd9a 7208 {
1792349b
AV
7209 /* F08:C633. */
7210 if (code->expr3)
7211 {
7212 if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
7213 "in ALLOCATE statement at %L", &e->where))
7214 goto failure;
7215 *array_alloc_wo_spec = true;
7216 }
7217 else
7218 {
7219 gfc_error ("Array specification required in ALLOCATE statement "
7220 "at %L", &e->where);
7221 goto failure;
7222 }
6de9cd9a
DN
7223 }
7224
6de9cd9a 7225 /* Make sure that the array section reference makes sense in the
1792349b 7226 context of an ALLOCATE specification. */
6de9cd9a
DN
7227
7228 ar = &ref2->u.ar;
7229
a3935ffc
TB
7230 if (codimension)
7231 for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
7232 if (ar->dimen_type[i] == DIMEN_THIS_IMAGE)
7233 {
7234 gfc_error ("Coarray specification required in ALLOCATE statement "
7235 "at %L", &e->where);
7236 goto failure;
7237 }
d3a9eea2 7238
6de9cd9a 7239 for (i = 0; i < ar->dimen; i++)
77726571 7240 {
1792349b 7241 if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
77726571 7242 goto check_symbols;
6de9cd9a 7243
77726571
PT
7244 switch (ar->dimen_type[i])
7245 {
7246 case DIMEN_ELEMENT:
6de9cd9a
DN
7247 break;
7248
77726571
PT
7249 case DIMEN_RANGE:
7250 if (ar->start[i] != NULL
7251 && ar->end[i] != NULL
7252 && ar->stride[i] == NULL)
7253 break;
6de9cd9a 7254
77726571
PT
7255 /* Fall Through... */
7256
7257 case DIMEN_UNKNOWN:
7258 case DIMEN_VECTOR:
d3a9eea2 7259 case DIMEN_STAR:
a3935ffc 7260 case DIMEN_THIS_IMAGE:
77726571
PT
7261 gfc_error ("Bad array specification in ALLOCATE statement at %L",
7262 &e->where);
d3a9eea2 7263 goto failure;
77726571
PT
7264 }
7265
7266check_symbols:
cf2b3c22 7267 for (a = code->ext.alloc.list; a; a = a->next)
77726571
PT
7268 {
7269 sym = a->expr->symtree->n.sym;
25e8cb2e
PT
7270
7271 /* TODO - check derived type components. */
6168891d 7272 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
25e8cb2e
PT
7273 continue;
7274
a68ab351
JJ
7275 if ((ar->start[i] != NULL
7276 && gfc_find_sym_in_expr (sym, ar->start[i]))
7277 || (ar->end[i] != NULL
7278 && gfc_find_sym_in_expr (sym, ar->end[i])))
77726571 7279 {
a4d9b221 7280 gfc_error ("%qs must not appear in the array specification at "
77726571
PT
7281 "%L in the same ALLOCATE statement where it is "
7282 "itself allocated", sym->name, &ar->where);
d3a9eea2 7283 goto failure;
77726571
PT
7284 }
7285 }
7286 }
6de9cd9a 7287
d3a9eea2
TB
7288 for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
7289 {
7290 if (ar->dimen_type[i] == DIMEN_ELEMENT
7291 || ar->dimen_type[i] == DIMEN_RANGE)
7292 {
7293 if (i == (ar->dimen + ar->codimen - 1))
7294 {
7295 gfc_error ("Expected '*' in coindex specification in ALLOCATE "
7296 "statement at %L", &e->where);
7297 goto failure;
7298 }
c6423ef3 7299 continue;
d3a9eea2
TB
7300 }
7301
7302 if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
7303 && ar->stride[i] == NULL)
7304 break;
7305
7306 gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
7307 &e->where);
7308 goto failure;
7309 }
7310
d3a9eea2 7311success:
524af0d6 7312 return true;
d3a9eea2
TB
7313
7314failure:
524af0d6 7315 return false;
6de9cd9a
DN
7316}
7317
1792349b 7318
b9332b09
PT
7319static void
7320resolve_allocate_deallocate (gfc_code *code, const char *fcn)
7321{
3759634f
SK
7322 gfc_expr *stat, *errmsg, *pe, *qe;
7323 gfc_alloc *a, *p, *q;
7324
8c91ab34
DK
7325 stat = code->expr1;
7326 errmsg = code->expr2;
b9332b09 7327
3759634f
SK
7328 /* Check the stat variable. */
7329 if (stat)
b9332b09 7330 {
22c23886 7331 gfc_check_vardef_context (stat, false, false, false,
524af0d6 7332 _("STAT variable"));
b9332b09 7333
6c145259
TK
7334 if ((stat->ts.type != BT_INTEGER
7335 && !(stat->ref && (stat->ref->type == REF_ARRAY
7336 || stat->ref->type == REF_COMPONENT)))
7337 || stat->rank > 0)
3759634f
SK
7338 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
7339 "variable", &stat->where);
7340
cf2b3c22 7341 for (p = code->ext.alloc.list; p; p = p->next)
3759634f 7342 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
ddf58e42
TB
7343 {
7344 gfc_ref *ref1, *ref2;
7345 bool found = true;
7346
7347 for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
7348 ref1 = ref1->next, ref2 = ref2->next)
7349 {
7350 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7351 continue;
7352 if (ref1->u.c.component->name != ref2->u.c.component->name)
7353 {
7354 found = false;
7355 break;
7356 }
7357 }
7358
7359 if (found)
7360 {
7361 gfc_error ("Stat-variable at %L shall not be %sd within "
7362 "the same %s statement", &stat->where, fcn, fcn);
7363 break;
7364 }
7365 }
b9332b09
PT
7366 }
7367
3759634f
SK
7368 /* Check the errmsg variable. */
7369 if (errmsg)
7370 {
7371 if (!stat)
db30e21c 7372 gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
3759634f
SK
7373 &errmsg->where);
7374
57bf28ea
TB
7375 gfc_check_vardef_context (errmsg, false, false, false,
7376 _("ERRMSG variable"));
3759634f 7377
6c145259
TK
7378 if ((errmsg->ts.type != BT_CHARACTER
7379 && !(errmsg->ref
7380 && (errmsg->ref->type == REF_ARRAY
7381 || errmsg->ref->type == REF_COMPONENT)))
7382 || errmsg->rank > 0 )
3759634f
SK
7383 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
7384 "variable", &errmsg->where);
7385
cf2b3c22 7386 for (p = code->ext.alloc.list; p; p = p->next)
3759634f 7387 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
ddf58e42
TB
7388 {
7389 gfc_ref *ref1, *ref2;
7390 bool found = true;
7391
7392 for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
7393 ref1 = ref1->next, ref2 = ref2->next)
7394 {
7395 if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
7396 continue;
7397 if (ref1->u.c.component->name != ref2->u.c.component->name)
7398 {
7399 found = false;
7400 break;
7401 }
7402 }
7403
7404 if (found)
7405 {
7406 gfc_error ("Errmsg-variable at %L shall not be %sd within "
7407 "the same %s statement", &errmsg->where, fcn, fcn);
7408 break;
7409 }
7410 }
3759634f
SK
7411 }
7412
c2092deb
TK
7413 /* Check that an allocate-object appears only once in the statement. */
7414
cf2b3c22 7415 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
7416 {
7417 pe = p->expr;
75fee9f2 7418 for (q = p->next; q; q = q->next)
3759634f 7419 {
75fee9f2
TK
7420 qe = q->expr;
7421 if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
3759634f 7422 {
75fee9f2
TK
7423 /* This is a potential collision. */
7424 gfc_ref *pr = pe->ref;
7425 gfc_ref *qr = qe->ref;
4d382327 7426
75fee9f2
TK
7427 /* Follow the references until
7428 a) They start to differ, in which case there is no error;
7429 you can deallocate a%b and a%c in a single statement
7430 b) Both of them stop, which is an error
7431 c) One of them stops, which is also an error. */
7432 while (1)
7433 {
7434 if (pr == NULL && qr == NULL)
7435 {
fea70c99
MLI
7436 gfc_error ("Allocate-object at %L also appears at %L",
7437 &pe->where, &qe->where);
75fee9f2
TK
7438 break;
7439 }
7440 else if (pr != NULL && qr == NULL)
7441 {
fea70c99
MLI
7442 gfc_error ("Allocate-object at %L is subobject of"
7443 " object at %L", &pe->where, &qe->where);
75fee9f2
TK
7444 break;
7445 }
7446 else if (pr == NULL && qr != NULL)
7447 {
fea70c99
MLI
7448 gfc_error ("Allocate-object at %L is subobject of"
7449 " object at %L", &qe->where, &pe->where);
75fee9f2
TK
7450 break;
7451 }
7452 /* Here, pr != NULL && qr != NULL */
7453 gcc_assert(pr->type == qr->type);
7454 if (pr->type == REF_ARRAY)
7455 {
7456 /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
7457 which are legal. */
7458 gcc_assert (qr->type == REF_ARRAY);
7459
7460 if (pr->next && qr->next)
7461 {
474d486a 7462 int i;
75fee9f2
TK
7463 gfc_array_ref *par = &(pr->u.ar);
7464 gfc_array_ref *qar = &(qr->u.ar);
474d486a
PT
7465
7466 for (i=0; i<par->dimen; i++)
7467 {
7468 if ((par->start[i] != NULL
7469 || qar->start[i] != NULL)
7470 && gfc_dep_compare_expr (par->start[i],
7471 qar->start[i]) != 0)
7472 goto break_label;
7473 }
75fee9f2
TK
7474 }
7475 }
7476 else
7477 {
7478 if (pr->u.c.component->name != qr->u.c.component->name)
7479 break;
7480 }
4d382327 7481
75fee9f2
TK
7482 pr = pr->next;
7483 qr = qr->next;
7484 }
474d486a
PT
7485 break_label:
7486 ;
3759634f
SK
7487 }
7488 }
7489 }
b9332b09
PT
7490
7491 if (strcmp (fcn, "ALLOCATE") == 0)
7492 {
1792349b 7493 bool arr_alloc_wo_spec = false;
cf2b3c22 7494 for (a = code->ext.alloc.list; a; a = a->next)
1792349b
AV
7495 resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
7496
7497 if (arr_alloc_wo_spec && code->expr3)
7498 {
7499 /* Mark the allocate to have to take the array specification
7500 from the expr3. */
7501 code->ext.alloc.arr_spec_from_expr3 = 1;
7502 }
b9332b09
PT
7503 }
7504 else
7505 {
cf2b3c22 7506 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
7507 resolve_deallocate_expr (a->expr);
7508 }
7509}
6de9cd9a 7510
3759634f 7511
6de9cd9a
DN
7512/************ SELECT CASE resolution subroutines ************/
7513
7514/* Callback function for our mergesort variant. Determines interval
7515 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
4d382327 7516 op1 > op2. Assumes we're not dealing with the default case.
c224550f
SK
7517 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
7518 There are nine situations to check. */
6de9cd9a
DN
7519
7520static int
edf1eac2 7521compare_cases (const gfc_case *op1, const gfc_case *op2)
6de9cd9a 7522{
c224550f 7523 int retval;
6de9cd9a 7524
c224550f 7525 if (op1->low == NULL) /* op1 = (:L) */
6de9cd9a 7526 {
c224550f
SK
7527 /* op2 = (:N), so overlap. */
7528 retval = 0;
7529 /* op2 = (M:) or (M:N), L < M */
7530 if (op2->low != NULL
7b4c5f8b 7531 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 7532 retval = -1;
6de9cd9a 7533 }
c224550f 7534 else if (op1->high == NULL) /* op1 = (K:) */
6de9cd9a 7535 {
c224550f
SK
7536 /* op2 = (M:), so overlap. */
7537 retval = 0;
7538 /* op2 = (:N) or (M:N), K > N */
7539 if (op2->high != NULL
7b4c5f8b 7540 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 7541 retval = 1;
6de9cd9a 7542 }
c224550f 7543 else /* op1 = (K:L) */
6de9cd9a 7544 {
c224550f 7545 if (op2->low == NULL) /* op2 = (:N), K > N */
7b4c5f8b
TB
7546 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
7547 ? 1 : 0;
c224550f 7548 else if (op2->high == NULL) /* op2 = (M:), L < M */
7b4c5f8b
TB
7549 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
7550 ? -1 : 0;
edf1eac2
SK
7551 else /* op2 = (M:N) */
7552 {
c224550f 7553 retval = 0;
edf1eac2 7554 /* L < M */
7b4c5f8b 7555 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 7556 retval = -1;
edf1eac2 7557 /* K > N */
7b4c5f8b 7558 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 7559 retval = 1;
6de9cd9a
DN
7560 }
7561 }
c224550f
SK
7562
7563 return retval;
6de9cd9a
DN
7564}
7565
7566
7567/* Merge-sort a double linked case list, detecting overlap in the
7568 process. LIST is the head of the double linked case list before it
7569 is sorted. Returns the head of the sorted list if we don't see any
7570 overlap, or NULL otherwise. */
7571
7572static gfc_case *
edf1eac2 7573check_case_overlap (gfc_case *list)
6de9cd9a
DN
7574{
7575 gfc_case *p, *q, *e, *tail;
7576 int insize, nmerges, psize, qsize, cmp, overlap_seen;
7577
7578 /* If the passed list was empty, return immediately. */
7579 if (!list)
7580 return NULL;
7581
7582 overlap_seen = 0;
7583 insize = 1;
7584
7585 /* Loop unconditionally. The only exit from this loop is a return
7586 statement, when we've finished sorting the case list. */
7587 for (;;)
7588 {
7589 p = list;
7590 list = NULL;
7591 tail = NULL;
7592
7593 /* Count the number of merges we do in this pass. */
7594 nmerges = 0;
7595
7596 /* Loop while there exists a merge to be done. */
7597 while (p)
7598 {
7599 int i;
7600
7601 /* Count this merge. */
7602 nmerges++;
7603
5352b89f 7604 /* Cut the list in two pieces by stepping INSIZE places
edf1eac2 7605 forward in the list, starting from P. */
6de9cd9a
DN
7606 psize = 0;
7607 q = p;
7608 for (i = 0; i < insize; i++)
7609 {
7610 psize++;
7611 q = q->right;
7612 if (!q)
7613 break;
7614 }
7615 qsize = insize;
7616
7617 /* Now we have two lists. Merge them! */
7618 while (psize > 0 || (qsize > 0 && q != NULL))
7619 {
6de9cd9a
DN
7620 /* See from which the next case to merge comes from. */
7621 if (psize == 0)
7622 {
7623 /* P is empty so the next case must come from Q. */
7624 e = q;
7625 q = q->right;
7626 qsize--;
7627 }
7628 else if (qsize == 0 || q == NULL)
7629 {
7630 /* Q is empty. */
7631 e = p;
7632 p = p->right;
7633 psize--;
7634 }
7635 else
7636 {
7637 cmp = compare_cases (p, q);
7638 if (cmp < 0)
7639 {
7640 /* The whole case range for P is less than the
edf1eac2 7641 one for Q. */
6de9cd9a
DN
7642 e = p;
7643 p = p->right;
7644 psize--;
7645 }
7646 else if (cmp > 0)
7647 {
7648 /* The whole case range for Q is greater than
edf1eac2 7649 the case range for P. */
6de9cd9a
DN
7650 e = q;
7651 q = q->right;
7652 qsize--;
7653 }
7654 else
7655 {
7656 /* The cases overlap, or they are the same
7657 element in the list. Either way, we must
7658 issue an error and get the next case from P. */
7659 /* FIXME: Sort P and Q by line number. */
fea70c99 7660 gfc_error ("CASE label at %L overlaps with CASE "
6de9cd9a
DN
7661 "label at %L", &p->where, &q->where);
7662 overlap_seen = 1;
7663 e = p;
7664 p = p->right;
7665 psize--;
7666 }
7667 }
7668
7669 /* Add the next element to the merged list. */
7670 if (tail)
7671 tail->right = e;
7672 else
7673 list = e;
7674 e->left = tail;
7675 tail = e;
7676 }
7677
7678 /* P has now stepped INSIZE places along, and so has Q. So
edf1eac2 7679 they're the same. */
6de9cd9a
DN
7680 p = q;
7681 }
7682 tail->right = NULL;
7683
7684 /* If we have done only one merge or none at all, we've
edf1eac2 7685 finished sorting the cases. */
6de9cd9a 7686 if (nmerges <= 1)
edf1eac2 7687 {
6de9cd9a
DN
7688 if (!overlap_seen)
7689 return list;
7690 else
7691 return NULL;
7692 }
7693
7694 /* Otherwise repeat, merging lists twice the size. */
7695 insize *= 2;
7696 }
7697}
7698
7699
5352b89f
SK
7700/* Check to see if an expression is suitable for use in a CASE statement.
7701 Makes sure that all case expressions are scalar constants of the same
524af0d6 7702 type. Return false if anything is wrong. */
6de9cd9a 7703
524af0d6 7704static bool
edf1eac2 7705validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6de9cd9a 7706{
524af0d6 7707 if (e == NULL) return true;
6de9cd9a 7708
5352b89f 7709 if (e->ts.type != case_expr->ts.type)
6de9cd9a
DN
7710 {
7711 gfc_error ("Expression in CASE statement at %L must be of type %s",
5352b89f 7712 &e->where, gfc_basic_typename (case_expr->ts.type));
524af0d6 7713 return false;
6de9cd9a
DN
7714 }
7715
5352b89f
SK
7716 /* C805 (R808) For a given case-construct, each case-value shall be of
7717 the same type as case-expr. For character type, length differences
7718 are allowed, but the kind type parameters shall be the same. */
7719
7720 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6de9cd9a 7721 {
d393bbd7
FXC
7722 gfc_error ("Expression in CASE statement at %L must be of kind %d",
7723 &e->where, case_expr->ts.kind);
524af0d6 7724 return false;
6de9cd9a
DN
7725 }
7726
ad1614a7
DF
7727 /* Convert the case value kind to that of case expression kind,
7728 if needed */
7729
5352b89f
SK
7730 if (e->ts.kind != case_expr->ts.kind)
7731 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
7732
6de9cd9a
DN
7733 if (e->rank != 0)
7734 {
7735 gfc_error ("Expression in CASE statement at %L must be scalar",
7736 &e->where);
524af0d6 7737 return false;
6de9cd9a
DN
7738 }
7739
524af0d6 7740 return true;
6de9cd9a
DN
7741}
7742
7743
7744/* Given a completely parsed select statement, we:
7745
7746 - Validate all expressions and code within the SELECT.
7747 - Make sure that the selection expression is not of the wrong type.
7748 - Make sure that no case ranges overlap.
7749 - Eliminate unreachable cases and unreachable code resulting from
7750 removing case labels.
7751
7752 The standard does allow unreachable cases, e.g. CASE (5:3). But
7753 they are a hassle for code generation, and to prevent that, we just
7754 cut them out here. This is not necessary for overlapping cases
7755 because they are illegal and we never even try to generate code.
7756
7757 We have the additional caveat that a SELECT construct could have
1f2959f0 7758 been a computed GOTO in the source code. Fortunately we can fairly
6de9cd9a
DN
7759 easily work around that here: The case_expr for a "real" SELECT CASE
7760 is in code->expr1, but for a computed GOTO it is in code->expr2. All
7761 we have to do is make sure that the case_expr is a scalar integer
7762 expression. */
7763
7764static void
ad3e2ad2 7765resolve_select (gfc_code *code, bool select_type)
6de9cd9a
DN
7766{
7767 gfc_code *body;
7768 gfc_expr *case_expr;
7769 gfc_case *cp, *default_case, *tail, *head;
7770 int seen_unreachable;
d68bd5a8 7771 int seen_logical;
6de9cd9a
DN
7772 int ncases;
7773 bt type;
524af0d6 7774 bool t;
6de9cd9a 7775
a513927a 7776 if (code->expr1 == NULL)
6de9cd9a
DN
7777 {
7778 /* This was actually a computed GOTO statement. */
7779 case_expr = code->expr2;
edf1eac2 7780 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6de9cd9a
DN
7781 gfc_error ("Selection expression in computed GOTO statement "
7782 "at %L must be a scalar integer expression",
7783 &case_expr->where);
7784
7785 /* Further checking is not necessary because this SELECT was built
7786 by the compiler, so it should always be OK. Just move the
7787 case_expr from expr2 to expr so that we can handle computed
7788 GOTOs as normal SELECTs from here on. */
a513927a 7789 code->expr1 = code->expr2;
6de9cd9a
DN
7790 code->expr2 = NULL;
7791 return;
7792 }
7793
a513927a 7794 case_expr = code->expr1;
6de9cd9a 7795 type = case_expr->ts.type;
ad3e2ad2
JW
7796
7797 /* F08:C830. */
6de9cd9a
DN
7798 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
7799 {
7800 gfc_error ("Argument of SELECT statement at %L cannot be %s",
7801 &case_expr->where, gfc_typename (&case_expr->ts));
7802
7803 /* Punt. Going on here just produce more garbage error messages. */
7804 return;
7805 }
7806
ad3e2ad2
JW
7807 /* F08:R842. */
7808 if (!select_type && case_expr->rank != 0)
7809 {
7810 gfc_error ("Argument of SELECT statement at %L must be a scalar "
7811 "expression", &case_expr->where);
7812
7813 /* Punt. */
7814 return;
7815 }
7816
ad1614a7
DF
7817 /* Raise a warning if an INTEGER case value exceeds the range of
7818 the case-expr. Later, all expressions will be promoted to the
7819 largest kind of all case-labels. */
7820
7821 if (type == BT_INTEGER)
7822 for (body = code->block; body; body = body->block)
29a63d67 7823 for (cp = body->ext.block.case_list; cp; cp = cp->next)
ad1614a7
DF
7824 {
7825 if (cp->low
7826 && gfc_check_integer_range (cp->low->value.integer,
7827 case_expr->ts.kind) != ARITH_OK)
db30e21c 7828 gfc_warning (0, "Expression in CASE statement at %L is "
ad1614a7
DF
7829 "not in the range of %s", &cp->low->where,
7830 gfc_typename (&case_expr->ts));
7831
7832 if (cp->high
7833 && cp->low != cp->high
7834 && gfc_check_integer_range (cp->high->value.integer,
7835 case_expr->ts.kind) != ARITH_OK)
db30e21c 7836 gfc_warning (0, "Expression in CASE statement at %L is "
ad1614a7
DF
7837 "not in the range of %s", &cp->high->where,
7838 gfc_typename (&case_expr->ts));
7839 }
7840
5352b89f
SK
7841 /* PR 19168 has a long discussion concerning a mismatch of the kinds
7842 of the SELECT CASE expression and its CASE values. Walk the lists
7843 of case values, and if we find a mismatch, promote case_expr to
7844 the appropriate kind. */
7845
7846 if (type == BT_LOGICAL || type == BT_INTEGER)
7847 {
7848 for (body = code->block; body; body = body->block)
7849 {
7850 /* Walk the case label list. */
29a63d67 7851 for (cp = body->ext.block.case_list; cp; cp = cp->next)
5352b89f
SK
7852 {
7853 /* Intercept the DEFAULT case. It does not have a kind. */
7854 if (cp->low == NULL && cp->high == NULL)
7855 continue;
7856
05c1e3a7 7857 /* Unreachable case ranges are discarded, so ignore. */
5352b89f
SK
7858 if (cp->low != NULL && cp->high != NULL
7859 && cp->low != cp->high
7b4c5f8b 7860 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5352b89f
SK
7861 continue;
7862
5352b89f
SK
7863 if (cp->low != NULL
7864 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
7865 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
7866
7867 if (cp->high != NULL
7868 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
05c1e3a7 7869 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5352b89f
SK
7870 }
7871 }
7872 }
7873
6de9cd9a
DN
7874 /* Assume there is no DEFAULT case. */
7875 default_case = NULL;
7876 head = tail = NULL;
7877 ncases = 0;
d68bd5a8 7878 seen_logical = 0;
6de9cd9a
DN
7879
7880 for (body = code->block; body; body = body->block)
7881 {
7882 /* Assume the CASE list is OK, and all CASE labels can be matched. */
524af0d6 7883 t = true;
6de9cd9a
DN
7884 seen_unreachable = 0;
7885
7886 /* Walk the case label list, making sure that all case labels
edf1eac2 7887 are legal. */
29a63d67 7888 for (cp = body->ext.block.case_list; cp; cp = cp->next)
6de9cd9a
DN
7889 {
7890 /* Count the number of cases in the whole construct. */
7891 ncases++;
7892
7893 /* Intercept the DEFAULT case. */
7894 if (cp->low == NULL && cp->high == NULL)
7895 {
7896 if (default_case != NULL)
edf1eac2 7897 {
fea70c99 7898 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6de9cd9a
DN
7899 "by a second DEFAULT CASE at %L",
7900 &default_case->where, &cp->where);
524af0d6 7901 t = false;
6de9cd9a
DN
7902 break;
7903 }
7904 else
7905 {
7906 default_case = cp;
7907 continue;
7908 }
7909 }
7910
7911 /* Deal with single value cases and case ranges. Errors are
edf1eac2 7912 issued from the validation function. */
524af0d6
JB
7913 if (!validate_case_label_expr (cp->low, case_expr)
7914 || !validate_case_label_expr (cp->high, case_expr))
6de9cd9a 7915 {
524af0d6 7916 t = false;
6de9cd9a
DN
7917 break;
7918 }
7919
7920 if (type == BT_LOGICAL
7921 && ((cp->low == NULL || cp->high == NULL)
7922 || cp->low != cp->high))
7923 {
edf1eac2
SK
7924 gfc_error ("Logical range in CASE statement at %L is not "
7925 "allowed", &cp->low->where);
524af0d6 7926 t = false;
6de9cd9a
DN
7927 break;
7928 }
7929
d68bd5a8
PT
7930 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
7931 {
7932 int value;
7933 value = cp->low->value.logical == 0 ? 2 : 1;
7934 if (value & seen_logical)
7935 {
ad1614a7 7936 gfc_error ("Constant logical value in CASE statement "
d68bd5a8
PT
7937 "is repeated at %L",
7938 &cp->low->where);
524af0d6 7939 t = false;
d68bd5a8
PT
7940 break;
7941 }
7942 seen_logical |= value;
7943 }
7944
6de9cd9a
DN
7945 if (cp->low != NULL && cp->high != NULL
7946 && cp->low != cp->high
7b4c5f8b 7947 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6de9cd9a 7948 {
73e42eef 7949 if (warn_surprising)
48749dbc
MLI
7950 gfc_warning (OPT_Wsurprising,
7951 "Range specification at %L can never be matched",
7952 &cp->where);
6de9cd9a
DN
7953
7954 cp->unreachable = 1;
7955 seen_unreachable = 1;
7956 }
7957 else
7958 {
7959 /* If the case range can be matched, it can also overlap with
7960 other cases. To make sure it does not, we put it in a
7961 double linked list here. We sort that with a merge sort
7962 later on to detect any overlapping cases. */
7963 if (!head)
edf1eac2 7964 {
6de9cd9a
DN
7965 head = tail = cp;
7966 head->right = head->left = NULL;
7967 }
7968 else
edf1eac2 7969 {
6de9cd9a
DN
7970 tail->right = cp;
7971 tail->right->left = tail;
7972 tail = tail->right;
7973 tail->right = NULL;
7974 }
7975 }
7976 }
7977
7978 /* It there was a failure in the previous case label, give up
7979 for this case label list. Continue with the next block. */
524af0d6 7980 if (!t)
6de9cd9a
DN
7981 continue;
7982
7983 /* See if any case labels that are unreachable have been seen.
7984 If so, we eliminate them. This is a bit of a kludge because
7985 the case lists for a single case statement (label) is a
7986 single forward linked lists. */
7987 if (seen_unreachable)
7988 {
7989 /* Advance until the first case in the list is reachable. */
29a63d67
TB
7990 while (body->ext.block.case_list != NULL
7991 && body->ext.block.case_list->unreachable)
6de9cd9a 7992 {
29a63d67
TB
7993 gfc_case *n = body->ext.block.case_list;
7994 body->ext.block.case_list = body->ext.block.case_list->next;
6de9cd9a
DN
7995 n->next = NULL;
7996 gfc_free_case_list (n);
7997 }
7998
7999 /* Strip all other unreachable cases. */
29a63d67 8000 if (body->ext.block.case_list)
6de9cd9a 8001 {
f172301f 8002 for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
6de9cd9a
DN
8003 {
8004 if (cp->next->unreachable)
8005 {
8006 gfc_case *n = cp->next;
8007 cp->next = cp->next->next;
8008 n->next = NULL;
8009 gfc_free_case_list (n);
8010 }
8011 }
8012 }
8013 }
8014 }
8015
8016 /* See if there were overlapping cases. If the check returns NULL,
8017 there was overlap. In that case we don't do anything. If head
8018 is non-NULL, we prepend the DEFAULT case. The sorted list can
8019 then used during code generation for SELECT CASE constructs with
8020 a case expression of a CHARACTER type. */
8021 if (head)
8022 {
8023 head = check_case_overlap (head);
8024
8025 /* Prepend the default_case if it is there. */
8026 if (head != NULL && default_case)
8027 {
8028 default_case->left = NULL;
8029 default_case->right = head;
8030 head->left = default_case;
8031 }
8032 }
8033
8034 /* Eliminate dead blocks that may be the result if we've seen
8035 unreachable case labels for a block. */
8036 for (body = code; body && body->block; body = body->block)
8037 {
29a63d67 8038 if (body->block->ext.block.case_list == NULL)
edf1eac2 8039 {
6de9cd9a
DN
8040 /* Cut the unreachable block from the code chain. */
8041 gfc_code *c = body->block;
8042 body->block = c->block;
8043
8044 /* Kill the dead block, but not the blocks below it. */
8045 c->block = NULL;
8046 gfc_free_statements (c);
edf1eac2 8047 }
6de9cd9a
DN
8048 }
8049
8050 /* More than two cases is legal but insane for logical selects.
8051 Issue a warning for it. */
73e42eef 8052 if (warn_surprising && type == BT_LOGICAL && ncases > 2)
48749dbc
MLI
8053 gfc_warning (OPT_Wsurprising,
8054 "Logical SELECT CASE block at %L has more that two cases",
6de9cd9a
DN
8055 &code->loc);
8056}
8057
8058
cf2b3c22
TB
8059/* Check if a derived type is extensible. */
8060
8061bool
8062gfc_type_is_extensible (gfc_symbol *sym)
8063{
8b704316
PT
8064 return !(sym->attr.is_bind_c || sym->attr.sequence
8065 || (sym->attr.is_class
8066 && sym->components->ts.u.derived->attr.unlimited_polymorphic));
cf2b3c22
TB
8067}
8068
8069
76540ac3
AV
8070static void
8071resolve_types (gfc_namespace *ns);
8072
8f75db9f 8073/* Resolve an associate-name: Resolve target and ensure the type-spec is
3e78238a
DK
8074 correct as well as possibly the array-spec. */
8075
8076static void
8077resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
8078{
8079 gfc_expr* target;
3e78238a
DK
8080
8081 gcc_assert (sym->assoc);
8082 gcc_assert (sym->attr.flavor == FL_VARIABLE);
8083
8084 /* If this is for SELECT TYPE, the target may not yet be set. In that
8085 case, return. Resolution will be called later manually again when
8086 this is done. */
8087 target = sym->assoc->target;
8088 if (!target)
8089 return;
8090 gcc_assert (!sym->assoc->dangling);
8091
524af0d6 8092 if (resolve_target && !gfc_resolve_expr (target))
3e78238a
DK
8093 return;
8094
8095 /* For variable targets, we get some attributes from the target. */
8096 if (target->expr_type == EXPR_VARIABLE)
8097 {
8098 gfc_symbol* tsym;
8099
8100 gcc_assert (target->symtree);
8101 tsym = target->symtree->n.sym;
8102
8103 sym->attr.asynchronous = tsym->attr.asynchronous;
8104 sym->attr.volatile_ = tsym->attr.volatile_;
8105
102344e2
TB
8106 sym->attr.target = tsym->attr.target
8107 || gfc_expr_attr (target).pointer;
68b1c5e1
PT
8108 if (is_subref_array (target))
8109 sym->attr.subref_array_pointer = 1;
3e78238a
DK
8110 }
8111
414e8be2
DK
8112 /* Get type if this was not already set. Note that it can be
8113 some other type than the target in case this is a SELECT TYPE
8114 selector! So we must not update when the type is already there. */
8115 if (sym->ts.type == BT_UNKNOWN)
8116 sym->ts = target->ts;
3e78238a
DK
8117 gcc_assert (sym->ts.type != BT_UNKNOWN);
8118
8119 /* See if this is a valid association-to-variable. */
8c91ab34
DK
8120 sym->assoc->variable = (target->expr_type == EXPR_VARIABLE
8121 && !gfc_has_vector_subscript (target));
3e78238a
DK
8122
8123 /* Finally resolve if this is an array or not. */
102344e2 8124 if (sym->attr.dimension && target->rank == 0)
3e78238a 8125 {
e207c522
PT
8126 /* primary.c makes the assumption that a reference to an associate
8127 name followed by a left parenthesis is an array reference. */
8128 if (sym->ts.type != BT_CHARACTER)
8129 gfc_error ("Associate-name %qs at %L is used as array",
8130 sym->name, &sym->declared_at);
3e78238a
DK
8131 sym->attr.dimension = 0;
8132 return;
8133 }
8f75db9f 8134
76540ac3 8135
8f75db9f
PT
8136 /* We cannot deal with class selectors that need temporaries. */
8137 if (target->ts.type == BT_CLASS
8138 && gfc_ref_needs_temporary_p (target->ref))
8139 {
8140 gfc_error ("CLASS selector at %L needs a temporary which is not "
8141 "yet implemented", &target->where);
8142 return;
8143 }
8144
76540ac3 8145 if (target->ts.type == BT_CLASS)
8f75db9f
PT
8146 gfc_fix_class_refs (target);
8147
76540ac3
AV
8148 if (target->rank != 0)
8149 {
8150 gfc_array_spec *as;
8151 if (sym->ts.type != BT_CLASS && !sym->as)
8152 {
8153 as = gfc_get_array_spec ();
8154 as->rank = target->rank;
8155 as->type = AS_DEFERRED;
8156 as->corank = gfc_get_corank (target);
8157 sym->attr.dimension = 1;
8158 if (as->corank != 0)
8159 sym->attr.codimension = 1;
8160 sym->as = as;
8161 }
8162 }
8163 else
3e78238a 8164 {
76540ac3
AV
8165 /* target's rank is 0, but the type of the sym is still array valued,
8166 which has to be corrected. */
8167 if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
8168 {
8169 gfc_array_spec *as;
8170 symbol_attribute attr;
8171 /* The associated variable's type is still the array type
8172 correct this now. */
8173 gfc_typespec *ts = &target->ts;
8174 gfc_ref *ref;
8175 gfc_component *c;
8176 for (ref = target->ref; ref != NULL; ref = ref->next)
8177 {
8178 switch (ref->type)
8179 {
8180 case REF_COMPONENT:
8181 ts = &ref->u.c.component->ts;
8182 break;
8183 case REF_ARRAY:
8184 if (ts->type == BT_CLASS)
8185 ts = &ts->u.derived->components->ts;
8186 break;
8187 default:
8188 break;
8189 }
8190 }
8191 /* Create a scalar instance of the current class type. Because the
8192 rank of a class array goes into its name, the type has to be
8193 rebuild. The alternative of (re-)setting just the attributes
8194 and as in the current type, destroys the type also in other
8195 places. */
8196 as = NULL;
8197 sym->ts = *ts;
8198 sym->ts.type = BT_CLASS;
8199 attr = CLASS_DATA (sym)->attr;
8200 attr.class_ok = 0;
8201 attr.associate_var = 1;
8202 attr.dimension = attr.codimension = 0;
8203 attr.class_pointer = 1;
8204 if (!gfc_build_class_symbol (&sym->ts, &attr, &as))
8205 gcc_unreachable ();
8206 /* Make sure the _vptr is set. */
8207 c = gfc_find_component (sym->ts.u.derived, "_vptr", true, true);
8208 if (c->ts.u.derived == NULL)
8209 c->ts.u.derived = gfc_find_derived_vtab (sym->ts.u.derived);
8210 CLASS_DATA (sym)->attr.pointer = 1;
8211 CLASS_DATA (sym)->attr.class_pointer = 1;
8212 gfc_set_sym_referenced (sym->ts.u.derived);
8213 gfc_commit_symbol (sym->ts.u.derived);
8214 /* _vptr now has the _vtab in it, change it to the _vtype. */
8215 if (c->ts.u.derived->attr.vtab)
8216 c->ts.u.derived = c->ts.u.derived->ts.u.derived;
8217 c->ts.u.derived->ns->types_resolved = 0;
8218 resolve_types (c->ts.u.derived->ns);
8219 }
3e78238a 8220 }
aa271860
PT
8221
8222 /* Mark this as an associate variable. */
8223 sym->attr.associate_var = 1;
8224
8225 /* If the target is a good class object, so is the associate variable. */
8226 if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
8227 sym->attr.class_ok = 1;
3e78238a
DK
8228}
8229
8230
cf2b3c22
TB
8231/* Resolve a SELECT TYPE statement. */
8232
8233static void
8c91ab34 8234resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
cf2b3c22
TB
8235{
8236 gfc_symbol *selector_type;
7c1dab0d
JW
8237 gfc_code *body, *new_st, *if_st, *tail;
8238 gfc_code *class_is = NULL, *default_case = NULL;
8239 gfc_case *c;
cf2b3c22
TB
8240 gfc_symtree *st;
8241 char name[GFC_MAX_SYMBOL_LEN];
93d76687 8242 gfc_namespace *ns;
7c1dab0d 8243 int error = 0;
8b704316 8244 int charlen = 0;
93d76687 8245
03af1e4c 8246 ns = code->ext.block.ns;
93d76687 8247 gfc_resolve (ns);
cf2b3c22 8248
f5dbb57c
JW
8249 /* Check for F03:C813. */
8250 if (code->expr1->ts.type != BT_CLASS
8251 && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
8252 {
8253 gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
8254 "at %L", &code->loc);
8255 return;
8256 }
8257
cd99c23c
TB
8258 if (!code->expr1->symtree->n.sym->attr.class_ok)
8259 return;
8260
93d76687 8261 if (code->expr2)
f5dbb57c
JW
8262 {
8263 if (code->expr1->symtree->n.sym->attr.untyped)
8264 code->expr1->symtree->n.sym->ts = code->expr2->ts;
7a08eda1 8265 selector_type = CLASS_DATA (code->expr2)->ts.u.derived;
e4821cd8
PT
8266
8267 /* F2008: C803 The selector expression must not be coindexed. */
8268 if (gfc_is_coindexed (code->expr2))
8269 {
8270 gfc_error ("Selector at %L must not be coindexed",
8271 &code->expr2->where);
8272 return;
8273 }
8274
f5dbb57c 8275 }
93d76687 8276 else
e4821cd8
PT
8277 {
8278 selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
8279
8280 if (gfc_is_coindexed (code->expr1))
8281 {
8282 gfc_error ("Selector at %L must not be coindexed",
8283 &code->expr1->where);
8284 return;
8285 }
8286 }
cf2b3c22 8287
cf2b3c22
TB
8288 /* Loop over TYPE IS / CLASS IS cases. */
8289 for (body = code->block; body; body = body->block)
8290 {
29a63d67 8291 c = body->ext.block.case_list;
cf2b3c22
TB
8292
8293 /* Check F03:C815. */
8294 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
8b704316 8295 && !selector_type->attr.unlimited_polymorphic
cf2b3c22
TB
8296 && !gfc_type_is_extensible (c->ts.u.derived))
8297 {
a4d9b221 8298 gfc_error ("Derived type %qs at %L must be extensible",
cf2b3c22 8299 c->ts.u.derived->name, &c->where);
7c1dab0d 8300 error++;
cf2b3c22
TB
8301 continue;
8302 }
8303
8304 /* Check F03:C816. */
55d8631b
TB
8305 if (c->ts.type != BT_UNKNOWN && !selector_type->attr.unlimited_polymorphic
8306 && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
8307 || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
cf2b3c22 8308 {
55d8631b 8309 if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
a4d9b221 8310 gfc_error ("Derived type %qs at %L must be an extension of %qs",
55d8631b
TB
8311 c->ts.u.derived->name, &c->where, selector_type->name);
8312 else
a4d9b221 8313 gfc_error ("Unexpected intrinsic type %qs at %L",
55d8631b 8314 gfc_basic_typename (c->ts.type), &c->where);
7c1dab0d 8315 error++;
cf2b3c22
TB
8316 continue;
8317 }
8318
8b704316
PT
8319 /* Check F03:C814. */
8320 if (c->ts.type == BT_CHARACTER && c->ts.u.cl->length != NULL)
8321 {
8322 gfc_error ("The type-spec at %L shall specify that each length "
8323 "type parameter is assumed", &c->where);
8324 error++;
8325 continue;
8326 }
8327
cf2b3c22
TB
8328 /* Intercept the DEFAULT case. */
8329 if (c->ts.type == BT_UNKNOWN)
8330 {
8331 /* Check F03:C818. */
7c1dab0d
JW
8332 if (default_case)
8333 {
fea70c99 8334 gfc_error ("The DEFAULT CASE at %L cannot be followed "
7c1dab0d 8335 "by a second DEFAULT CASE at %L",
29a63d67 8336 &default_case->ext.block.case_list->where, &c->where);
7c1dab0d
JW
8337 error++;
8338 continue;
8339 }
414e8be2
DK
8340
8341 default_case = body;
cf2b3c22
TB
8342 }
8343 }
4d382327 8344
3e78238a 8345 if (error > 0)
7c1dab0d 8346 return;
cf2b3c22 8347
3e78238a 8348 /* Transform SELECT TYPE statement to BLOCK and associate selector to
e5ca9693
DK
8349 target if present. If there are any EXIT statements referring to the
8350 SELECT TYPE construct, this is no problem because the gfc_code
8351 reference stays the same and EXIT is equally possible from the BLOCK
8352 it is changed to. */
3e78238a 8353 code->op = EXEC_BLOCK;
93d76687
JW
8354 if (code->expr2)
8355 {
3e78238a
DK
8356 gfc_association_list* assoc;
8357
8358 assoc = gfc_get_association_list ();
8359 assoc->st = code->expr1->symtree;
8360 assoc->target = gfc_copy_expr (code->expr2);
c49ea23d 8361 assoc->target->where = code->expr2->where;
3e78238a 8362 /* assoc->variable will be set by resolve_assoc_var. */
4d382327 8363
3e78238a
DK
8364 code->ext.block.assoc = assoc;
8365 code->expr1->symtree->n.sym->assoc = assoc;
8366
8367 resolve_assoc_var (code->expr1->symtree->n.sym, false);
93d76687 8368 }
3e78238a
DK
8369 else
8370 code->ext.block.assoc = NULL;
93d76687 8371
3e78238a 8372 /* Add EXEC_SELECT to switch on type. */
11e5274a 8373 new_st = gfc_get_code (code->op);
93d76687
JW
8374 new_st->expr1 = code->expr1;
8375 new_st->expr2 = code->expr2;
8376 new_st->block = code->block;
3e78238a
DK
8377 code->expr1 = code->expr2 = NULL;
8378 code->block = NULL;
93d76687
JW
8379 if (!ns->code)
8380 ns->code = new_st;
8381 else
8382 ns->code->next = new_st;
93d76687 8383 code = new_st;
cf2b3c22 8384 code->op = EXEC_SELECT;
8b704316 8385
b04533af
JW
8386 gfc_add_vptr_component (code->expr1);
8387 gfc_add_hash_component (code->expr1);
cf2b3c22
TB
8388
8389 /* Loop over TYPE IS / CLASS IS cases. */
8390 for (body = code->block; body; body = body->block)
8391 {
29a63d67 8392 c = body->ext.block.case_list;
b7e75771 8393
cf2b3c22 8394 if (c->ts.type == BT_DERIVED)
b7e75771
JD
8395 c->low = c->high = gfc_get_int_expr (gfc_default_integer_kind, NULL,
8396 c->ts.u.derived->hash_value);
8b704316
PT
8397 else if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
8398 {
8399 gfc_symbol *ivtab;
8400 gfc_expr *e;
8401
7289d1c9 8402 ivtab = gfc_find_vtab (&c->ts);
4038d0fb 8403 gcc_assert (ivtab && CLASS_DATA (ivtab)->initializer);
8b704316
PT
8404 e = CLASS_DATA (ivtab)->initializer;
8405 c->low = c->high = gfc_copy_expr (e);
8406 }
b7e75771 8407
7c1dab0d 8408 else if (c->ts.type == BT_UNKNOWN)
cf2b3c22 8409 continue;
b7e75771 8410
3e78238a
DK
8411 /* Associate temporary to selector. This should only be done
8412 when this case is actually true, so build a new ASSOCIATE
8413 that does precisely this here (instead of using the
8414 'global' one). */
8415
7c1dab0d 8416 if (c->ts.type == BT_CLASS)
b04533af 8417 sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
8b704316 8418 else if (c->ts.type == BT_DERIVED)
b04533af 8419 sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
8b704316
PT
8420 else if (c->ts.type == BT_CHARACTER)
8421 {
8422 if (c->ts.u.cl && c->ts.u.cl->length
8423 && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
8424 charlen = mpz_get_si (c->ts.u.cl->length->value.integer);
8425 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (c->ts.type),
8426 charlen, c->ts.kind);
8427 }
8428 else
8429 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
8430 c->ts.kind);
8431
93d76687 8432 st = gfc_find_symtree (ns->sym_root, name);
3e78238a
DK
8433 gcc_assert (st->n.sym->assoc);
8434 st->n.sym->assoc->target = gfc_get_variable_expr (code->expr1->symtree);
c49ea23d 8435 st->n.sym->assoc->target->where = code->expr1->where;
8b704316 8436 if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
b04533af 8437 gfc_add_data_component (st->n.sym->assoc->target);
3e78238a 8438
11e5274a 8439 new_st = gfc_get_code (EXEC_BLOCK);
3e78238a
DK
8440 new_st->ext.block.ns = gfc_build_block_ns (ns);
8441 new_st->ext.block.ns->code = body->next;
8442 body->next = new_st;
8443
8444 /* Chain in the new list only if it is marked as dangling. Otherwise
8445 there is a CASE label overlap and this is already used. Just ignore,
eea58adb 8446 the error is diagnosed elsewhere. */
3e78238a 8447 if (st->n.sym->assoc->dangling)
7c1dab0d 8448 {
3e78238a
DK
8449 new_st->ext.block.assoc = st->n.sym->assoc;
8450 st->n.sym->assoc->dangling = 0;
7c1dab0d 8451 }
3e78238a
DK
8452
8453 resolve_assoc_var (st->n.sym, false);
cf2b3c22 8454 }
4d382327 8455
7c1dab0d
JW
8456 /* Take out CLASS IS cases for separate treatment. */
8457 body = code;
8458 while (body && body->block)
8459 {
29a63d67 8460 if (body->block->ext.block.case_list->ts.type == BT_CLASS)
7c1dab0d
JW
8461 {
8462 /* Add to class_is list. */
8463 if (class_is == NULL)
4d382327 8464 {
7c1dab0d
JW
8465 class_is = body->block;
8466 tail = class_is;
8467 }
8468 else
8469 {
8470 for (tail = class_is; tail->block; tail = tail->block) ;
8471 tail->block = body->block;
8472 tail = tail->block;
8473 }
8474 /* Remove from EXEC_SELECT list. */
8475 body->block = body->block->block;
8476 tail->block = NULL;
8477 }
8478 else
8479 body = body->block;
8480 }
cf2b3c22 8481
7c1dab0d 8482 if (class_is)
cf2b3c22 8483 {
7c1dab0d 8484 gfc_symbol *vtab;
4d382327 8485
7c1dab0d
JW
8486 if (!default_case)
8487 {
8488 /* Add a default case to hold the CLASS IS cases. */
8489 for (tail = code; tail->block; tail = tail->block) ;
11e5274a 8490 tail->block = gfc_get_code (EXEC_SELECT_TYPE);
7c1dab0d 8491 tail = tail->block;
29a63d67
TB
8492 tail->ext.block.case_list = gfc_get_case ();
8493 tail->ext.block.case_list->ts.type = BT_UNKNOWN;
7c1dab0d
JW
8494 tail->next = NULL;
8495 default_case = tail;
8496 }
eece1eb9 8497
7c1dab0d
JW
8498 /* More than one CLASS IS block? */
8499 if (class_is->block)
cf2b3c22 8500 {
7c1dab0d
JW
8501 gfc_code **c1,*c2;
8502 bool swapped;
8503 /* Sort CLASS IS blocks by extension level. */
8504 do
8505 {
8506 swapped = false;
8507 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
8508 {
8509 c2 = (*c1)->block;
8510 /* F03:C817 (check for doubles). */
29a63d67
TB
8511 if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
8512 == c2->ext.block.case_list->ts.u.derived->hash_value)
7c1dab0d
JW
8513 {
8514 gfc_error ("Double CLASS IS block in SELECT TYPE "
29a63d67
TB
8515 "statement at %L",
8516 &c2->ext.block.case_list->where);
7c1dab0d
JW
8517 return;
8518 }
29a63d67
TB
8519 if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
8520 < c2->ext.block.case_list->ts.u.derived->attr.extension)
7c1dab0d
JW
8521 {
8522 /* Swap. */
8523 (*c1)->block = c2->block;
8524 c2->block = *c1;
8525 *c1 = c2;
8526 swapped = true;
8527 }
8528 }
8529 }
8530 while (swapped);
cf2b3c22 8531 }
4d382327 8532
7c1dab0d 8533 /* Generate IF chain. */
11e5274a 8534 if_st = gfc_get_code (EXEC_IF);
7c1dab0d
JW
8535 new_st = if_st;
8536 for (body = class_is; body; body = body->block)
8537 {
11e5274a 8538 new_st->block = gfc_get_code (EXEC_IF);
7c1dab0d 8539 new_st = new_st->block;
7c1dab0d
JW
8540 /* Set up IF condition: Call _gfortran_is_extension_of. */
8541 new_st->expr1 = gfc_get_expr ();
8542 new_st->expr1->expr_type = EXPR_FUNCTION;
8543 new_st->expr1->ts.type = BT_LOGICAL;
8544 new_st->expr1->ts.kind = 4;
8545 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
8546 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
8547 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
8548 /* Set up arguments. */
8549 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
8550 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
2c3d0cd3 8551 new_st->expr1->value.function.actual->expr->where = code->loc;
b04533af 8552 gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
29a63d67 8553 vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
7c1dab0d
JW
8554 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
8555 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
8556 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
8557 new_st->next = body->next;
8558 }
8559 if (default_case->next)
8560 {
11e5274a 8561 new_st->block = gfc_get_code (EXEC_IF);
7c1dab0d 8562 new_st = new_st->block;
7c1dab0d
JW
8563 new_st->next = default_case->next;
8564 }
4d382327 8565
7c1dab0d
JW
8566 /* Replace CLASS DEFAULT code by the IF chain. */
8567 default_case->next = if_st;
cf2b3c22
TB
8568 }
8569
8c91ab34
DK
8570 /* Resolve the internal code. This can not be done earlier because
8571 it requires that the sym->assoc of selectors is set already. */
8572 gfc_current_ns = ns;
8573 gfc_resolve_blocks (code->block, gfc_current_ns);
8574 gfc_current_ns = old_ns;
cf2b3c22 8575
ad3e2ad2 8576 resolve_select (code, true);
cf2b3c22
TB
8577}
8578
8579
0e6928d8
TS
8580/* Resolve a transfer statement. This is making sure that:
8581 -- a derived type being transferred has only non-pointer components
4d382327 8582 -- a derived type being transferred doesn't have private components, unless
8451584a 8583 it's being transferred from the module where the type was defined
0e6928d8
TS
8584 -- we're not trying to transfer a whole assumed size array. */
8585
8586static void
edf1eac2 8587resolve_transfer (gfc_code *code)
0e6928d8
TS
8588{
8589 gfc_typespec *ts;
8590 gfc_symbol *sym;
8591 gfc_ref *ref;
8592 gfc_expr *exp;
8593
a513927a 8594 exp = code->expr1;
0e6928d8 8595
771c5727
JD
8596 while (exp != NULL && exp->expr_type == EXPR_OP
8597 && exp->value.op.op == INTRINSIC_PARENTHESES)
8598 exp = exp->value.op.op1;
8599
49560f0c
PT
8600 if (exp && exp->expr_type == EXPR_NULL
8601 && code->ext.dt)
ea8ad3e5 8602 {
49560f0c
PT
8603 gfc_error ("Invalid context for NULL () intrinsic at %L",
8604 &exp->where);
ea8ad3e5
TB
8605 return;
8606 }
8607
771c5727 8608 if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
2f72ca14
TB
8609 && exp->expr_type != EXPR_FUNCTION
8610 && exp->expr_type != EXPR_STRUCTURE))
0e6928d8
TS
8611 return;
8612
8e8dc060
DK
8613 /* If we are reading, the variable will be changed. Note that
8614 code->ext.dt may be NULL if the TRANSFER is related to
8615 an INQUIRE statement -- but in this case, we are not reading, either. */
8616 if (code->ext.dt && code->ext.dt->dt_io_kind->value.iokind == M_READ
22c23886 8617 && !gfc_check_vardef_context (exp, false, false, false,
524af0d6 8618 _("item in READ")))
8e8dc060
DK
8619 return;
8620
2f72ca14 8621 ts = exp->expr_type == EXPR_STRUCTURE ? &exp->ts : &exp->symtree->n.sym->ts;
0e6928d8
TS
8622
8623 /* Go to actual component transferred. */
6cf860a2 8624 for (ref = exp->ref; ref; ref = ref->next)
0e6928d8
TS
8625 if (ref->type == REF_COMPONENT)
8626 ts = &ref->u.c.component->ts;
8627
d5656544
TB
8628 if (ts->type == BT_CLASS)
8629 {
8630 /* FIXME: Test for defined input/output. */
8631 gfc_error ("Data transfer element at %L cannot be polymorphic unless "
8632 "it is processed by a defined input/output procedure",
8633 &code->loc);
8634 return;
8635 }
8636
0e6928d8
TS
8637 if (ts->type == BT_DERIVED)
8638 {
8639 /* Check that transferred derived type doesn't contain POINTER
8640 components. */
bc21d315 8641 if (ts->u.derived->attr.pointer_comp)
0e6928d8 8642 {
d8155bf5
TB
8643 gfc_error ("Data transfer element at %L cannot have POINTER "
8644 "components unless it is processed by a defined "
8645 "input/output procedure", &code->loc);
0e6928d8
TS
8646 return;
8647 }
8648
357f98e5
JW
8649 /* F08:C935. */
8650 if (ts->u.derived->attr.proc_pointer_comp)
8651 {
8652 gfc_error ("Data transfer element at %L cannot have "
8653 "procedure pointer components", &code->loc);
8654 return;
8655 }
8656
bc21d315 8657 if (ts->u.derived->attr.alloc_comp)
5046aff5 8658 {
d8155bf5
TB
8659 gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
8660 "components unless it is processed by a defined "
8661 "input/output procedure", &code->loc);
5046aff5
PT
8662 return;
8663 }
8664
cadddfdd
TB
8665 /* C_PTR and C_FUNPTR have private components which means they can not
8666 be printed. However, if -std=gnu and not -pedantic, allow
8667 the component to be printed to help debugging. */
8668 if (ts->u.derived->ts.f90_type == BT_VOID)
8669 {
524af0d6
JB
8670 if (!gfc_notify_std (GFC_STD_GNU, "Data transfer element at %L "
8671 "cannot have PRIVATE components", &code->loc))
cadddfdd
TB
8672 return;
8673 }
8674 else if (derived_inaccessible (ts->u.derived))
0e6928d8
TS
8675 {
8676 gfc_error ("Data transfer element at %L cannot have "
8677 "PRIVATE components",&code->loc);
8678 return;
8679 }
8680 }
4f283c42 8681
2f72ca14
TB
8682 if (exp->expr_type == EXPR_STRUCTURE)
8683 return;
8684
8685 sym = exp->symtree->n.sym;
0e6928d8 8686
f2ce74d1 8687 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
0e6928d8
TS
8688 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
8689 {
8690 gfc_error ("Data transfer element at %L cannot be a full reference to "
8691 "an assumed-size array", &code->loc);
8692 return;
8693 }
8694}
8695
8696
6de9cd9a
DN
8697/*********** Toplevel code resolution subroutines ***********/
8698
0615f923 8699/* Find the set of labels that are reachable from this block. We also
d80c695f 8700 record the last statement in each block. */
4d382327 8701
0615f923 8702static void
d80c695f 8703find_reachable_labels (gfc_code *block)
0615f923
TS
8704{
8705 gfc_code *c;
8706
8707 if (!block)
8708 return;
8709
8710 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
8711
d80c695f
TS
8712 /* Collect labels in this block. We don't keep those corresponding
8713 to END {IF|SELECT}, these are checked in resolve_branch by going
8714 up through the code_stack. */
0615f923
TS
8715 for (c = block; c; c = c->next)
8716 {
df1a69f6 8717 if (c->here && c->op != EXEC_END_NESTED_BLOCK)
0615f923 8718 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
0615f923
TS
8719 }
8720
8721 /* Merge with labels from parent block. */
8722 if (cs_base->prev)
8723 {
8724 gcc_assert (cs_base->prev->reachable_labels);
8725 bitmap_ior_into (cs_base->reachable_labels,
8726 cs_base->prev->reachable_labels);
8727 }
8728}
8729
d0a4a61c 8730
5493aa17 8731static void
5df445a2 8732resolve_lock_unlock_event (gfc_code *code)
5493aa17 8733{
b5116268
TB
8734 if (code->expr1->expr_type == EXPR_FUNCTION
8735 && code->expr1->value.function.isym
8736 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
8737 remove_caf_get_intrinsic (code->expr1);
8738
5df445a2
TB
8739 if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
8740 && (code->expr1->ts.type != BT_DERIVED
8741 || code->expr1->expr_type != EXPR_VARIABLE
8742 || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
8743 || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
8744 || code->expr1->rank != 0
8745 || (!gfc_is_coarray (code->expr1) &&
8746 !gfc_is_coindexed (code->expr1))))
3b6fa7a5
TB
8747 gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
8748 &code->expr1->where);
6b2e46bf 8749 else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
5df445a2
TB
8750 && (code->expr1->ts.type != BT_DERIVED
8751 || code->expr1->expr_type != EXPR_VARIABLE
8752 || code->expr1->ts.u.derived->from_intmod
8753 != INTMOD_ISO_FORTRAN_ENV
8754 || code->expr1->ts.u.derived->intmod_sym_id
8755 != ISOFORTRAN_EVENT_TYPE
8756 || code->expr1->rank != 0))
8757 gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
8758 &code->expr1->where);
8759 else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
8760 && !gfc_is_coindexed (code->expr1))
8761 gfc_error ("Event variable argument at %L must be a coarray or coindexed",
8762 &code->expr1->where);
8763 else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
8764 gfc_error ("Event variable argument at %L must be a coarray but not "
8765 "coindexed", &code->expr1->where);
5493aa17
TB
8766
8767 /* Check STAT. */
8768 if (code->expr2
8769 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8770 || code->expr2->expr_type != EXPR_VARIABLE))
8771 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8772 &code->expr2->where);
8773
fea54935 8774 if (code->expr2
22c23886 8775 && !gfc_check_vardef_context (code->expr2, false, false, false,
524af0d6 8776 _("STAT variable")))
fea54935
TB
8777 return;
8778
5493aa17
TB
8779 /* Check ERRMSG. */
8780 if (code->expr3
8781 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8782 || code->expr3->expr_type != EXPR_VARIABLE))
8783 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8784 &code->expr3->where);
8785
fea54935 8786 if (code->expr3
22c23886 8787 && !gfc_check_vardef_context (code->expr3, false, false, false,
524af0d6 8788 _("ERRMSG variable")))
fea54935
TB
8789 return;
8790
5df445a2
TB
8791 /* Check for LOCK the ACQUIRED_LOCK. */
8792 if (code->op != EXEC_EVENT_WAIT && code->expr4
5493aa17
TB
8793 && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
8794 || code->expr4->expr_type != EXPR_VARIABLE))
8795 gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
8796 "variable", &code->expr4->where);
fea54935 8797
5df445a2 8798 if (code->op != EXEC_EVENT_WAIT && code->expr4
22c23886 8799 && !gfc_check_vardef_context (code->expr4, false, false, false,
524af0d6 8800 _("ACQUIRED_LOCK variable")))
fea54935 8801 return;
5df445a2
TB
8802
8803 /* Check for EVENT WAIT the UNTIL_COUNT. */
8804 if (code->op == EXEC_EVENT_WAIT && code->expr4
8805 && (code->expr4->ts.type != BT_INTEGER || code->expr4->rank != 0))
8806 gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
8807 "expression", &code->expr4->where);
5493aa17
TB
8808}
8809
8810
bc0229f9
TB
8811static void
8812resolve_critical (gfc_code *code)
8813{
8814 gfc_symtree *symtree;
8815 gfc_symbol *lock_type;
8816 char name[GFC_MAX_SYMBOL_LEN];
8817 static int serial = 0;
8818
f19626cf 8819 if (flag_coarray != GFC_FCOARRAY_LIB)
bc0229f9
TB
8820 return;
8821
9de8e7af
TB
8822 symtree = gfc_find_symtree (gfc_current_ns->sym_root,
8823 GFC_PREFIX ("lock_type"));
bc0229f9
TB
8824 if (symtree)
8825 lock_type = symtree->n.sym;
8826 else
8827 {
9de8e7af
TB
8828 if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
8829 false) != 0)
bc0229f9
TB
8830 gcc_unreachable ();
8831 lock_type = symtree->n.sym;
8832 lock_type->attr.flavor = FL_DERIVED;
8833 lock_type->attr.zero_comp = 1;
8834 lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
8835 lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
8836 }
8837
9de8e7af 8838 sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
bc0229f9
TB
8839 if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
8840 gcc_unreachable ();
8841
8842 code->resolved_sym = symtree->n.sym;
8843 symtree->n.sym->attr.flavor = FL_VARIABLE;
8844 symtree->n.sym->attr.referenced = 1;
8845 symtree->n.sym->attr.artificial = 1;
8846 symtree->n.sym->attr.codimension = 1;
8847 symtree->n.sym->ts.type = BT_DERIVED;
8848 symtree->n.sym->ts.u.derived = lock_type;
8849 symtree->n.sym->as = gfc_get_array_spec ();
8850 symtree->n.sym->as->corank = 1;
8851 symtree->n.sym->as->type = AS_EXPLICIT;
8852 symtree->n.sym->as->cotype = AS_EXPLICIT;
8853 symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
8854 NULL, 1);
1fceb215 8855 gfc_commit_symbols();
bc0229f9
TB
8856}
8857
8858
d0a4a61c
TB
8859static void
8860resolve_sync (gfc_code *code)
8861{
8862 /* Check imageset. The * case matches expr1 == NULL. */
8863 if (code->expr1)
8864 {
8865 if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
8866 gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
8867 "INTEGER expression", &code->expr1->where);
8868 if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
8869 && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
8870 gfc_error ("Imageset argument at %L must between 1 and num_images()",
8871 &code->expr1->where);
8872 else if (code->expr1->expr_type == EXPR_ARRAY
524af0d6 8873 && gfc_simplify_expr (code->expr1, 0))
d0a4a61c
TB
8874 {
8875 gfc_constructor *cons;
b7e75771
JD
8876 cons = gfc_constructor_first (code->expr1->value.constructor);
8877 for (; cons; cons = gfc_constructor_next (cons))
d0a4a61c
TB
8878 if (cons->expr->expr_type == EXPR_CONSTANT
8879 && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
8880 gfc_error ("Imageset argument at %L must between 1 and "
8881 "num_images()", &cons->expr->where);
8882 }
8883 }
8884
8885 /* Check STAT. */
8886 if (code->expr2
8887 && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
8888 || code->expr2->expr_type != EXPR_VARIABLE))
8889 gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
8890 &code->expr2->where);
8891
8892 /* Check ERRMSG. */
8893 if (code->expr3
8894 && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
8895 || code->expr3->expr_type != EXPR_VARIABLE))
8896 gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
8897 &code->expr3->where);
8898}
8899
8900
d80c695f 8901/* Given a branch to a label, see if the branch is conforming.
0615f923 8902 The code node describes where the branch is located. */
6de9cd9a
DN
8903
8904static void
edf1eac2 8905resolve_branch (gfc_st_label *label, gfc_code *code)
6de9cd9a 8906{
6de9cd9a 8907 code_stack *stack;
6de9cd9a
DN
8908
8909 if (label == NULL)
8910 return;
6de9cd9a
DN
8911
8912 /* Step one: is this a valid branching target? */
8913
0615f923 8914 if (label->defined == ST_LABEL_UNKNOWN)
6de9cd9a 8915 {
0615f923
TS
8916 gfc_error ("Label %d referenced at %L is never defined", label->value,
8917 &label->where);
6de9cd9a
DN
8918 return;
8919 }
8920
f3e7b9d6 8921 if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
6de9cd9a 8922 {
fea70c99 8923 gfc_error ("Statement at %L is not a valid branch target statement "
0615f923 8924 "for the branch statement at %L", &label->where, &code->loc);
6de9cd9a
DN
8925 return;
8926 }
8927
8928 /* Step two: make sure this branch is not a branch to itself ;-) */
8929
8930 if (code->here == label)
8931 {
db30e21c
JM
8932 gfc_warning (0,
8933 "Branch at %L may result in an infinite loop", &code->loc);
6de9cd9a
DN
8934 return;
8935 }
8936
0615f923
TS
8937 /* Step three: See if the label is in the same block as the
8938 branching statement. The hard work has been done by setting up
8939 the bitmap reachable_labels. */
6de9cd9a 8940
d80c695f 8941 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
d0a4a61c
TB
8942 {
8943 /* Check now whether there is a CRITICAL construct; if so, check
8944 whether the label is still visible outside of the CRITICAL block,
8945 which is invalid. */
8946 for (stack = cs_base; stack; stack = stack->prev)
8c6a85e3
TB
8947 {
8948 if (stack->current->op == EXEC_CRITICAL
8949 && bitmap_bit_p (stack->reachable_labels, label->value))
fea70c99 8950 gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
8c6a85e3
TB
8951 "label at %L", &code->loc, &label->where);
8952 else if (stack->current->op == EXEC_DO_CONCURRENT
8953 && bitmap_bit_p (stack->reachable_labels, label->value))
fea70c99 8954 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
8c6a85e3
TB
8955 "for label at %L", &code->loc, &label->where);
8956 }
d0a4a61c
TB
8957
8958 return;
8959 }
6de9cd9a 8960
d80c695f
TS
8961 /* Step four: If we haven't found the label in the bitmap, it may
8962 still be the label of the END of the enclosing block, in which
8963 case we find it by going up the code_stack. */
6de9cd9a 8964
0615f923 8965 for (stack = cs_base; stack; stack = stack->prev)
d0a4a61c
TB
8966 {
8967 if (stack->current->next && stack->current->next->here == label)
8968 break;
8969 if (stack->current->op == EXEC_CRITICAL)
8970 {
8971 /* Note: A label at END CRITICAL does not leave the CRITICAL
8972 construct as END CRITICAL is still part of it. */
fea70c99 8973 gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
d0a4a61c
TB
8974 " at %L", &code->loc, &label->where);
8975 return;
8976 }
8c6a85e3
TB
8977 else if (stack->current->op == EXEC_DO_CONCURRENT)
8978 {
fea70c99 8979 gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
8c6a85e3
TB
8980 "label at %L", &code->loc, &label->where);
8981 return;
8982 }
d0a4a61c 8983 }
6de9cd9a 8984
d80c695f 8985 if (stack)
0615f923 8986 {
df1a69f6 8987 gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
d80c695f 8988 return;
6de9cd9a 8989 }
0615f923 8990
d80c695f
TS
8991 /* The label is not in an enclosing block, so illegal. This was
8992 allowed in Fortran 66, so we allow it as extension. No
8993 further checks are necessary in this case. */
2a2703a2 8994 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
d80c695f
TS
8995 "as the GOTO statement at %L", &label->where,
8996 &code->loc);
8997 return;
6de9cd9a
DN
8998}
8999
9000
9001/* Check whether EXPR1 has the same shape as EXPR2. */
9002
524af0d6 9003static bool
6de9cd9a
DN
9004resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
9005{
9006 mpz_t shape[GFC_MAX_DIMENSIONS];
9007 mpz_t shape2[GFC_MAX_DIMENSIONS];
524af0d6 9008 bool result = false;
6de9cd9a
DN
9009 int i;
9010
9011 /* Compare the rank. */
9012 if (expr1->rank != expr2->rank)
9013 return result;
9014
9015 /* Compare the size of each dimension. */
9016 for (i=0; i<expr1->rank; i++)
9017 {
524af0d6 9018 if (!gfc_array_dimen_size (expr1, i, &shape[i]))
edf1eac2 9019 goto ignore;
6de9cd9a 9020
524af0d6 9021 if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
edf1eac2 9022 goto ignore;
6de9cd9a
DN
9023
9024 if (mpz_cmp (shape[i], shape2[i]))
edf1eac2 9025 goto over;
6de9cd9a
DN
9026 }
9027
9028 /* When either of the two expression is an assumed size array, we
9029 ignore the comparison of dimension sizes. */
9030ignore:
524af0d6 9031 result = true;
6de9cd9a
DN
9032
9033over:
7d7212ec
MM
9034 gfc_clear_shape (shape, i);
9035 gfc_clear_shape (shape2, i);
6de9cd9a
DN
9036 return result;
9037}
9038
9039
9040/* Check whether a WHERE assignment target or a WHERE mask expression
9041 has the same shape as the outmost WHERE mask expression. */
9042
9043static void
9044resolve_where (gfc_code *code, gfc_expr *mask)
9045{
9046 gfc_code *cblock;
9047 gfc_code *cnext;
9048 gfc_expr *e = NULL;
9049
9050 cblock = code->block;
9051
9052 /* Store the first WHERE mask-expr of the WHERE statement or construct.
9053 In case of nested WHERE, only the outmost one is stored. */
9054 if (mask == NULL) /* outmost WHERE */
a513927a 9055 e = cblock->expr1;
6de9cd9a
DN
9056 else /* inner WHERE */
9057 e = mask;
9058
9059 while (cblock)
9060 {
a513927a 9061 if (cblock->expr1)
edf1eac2
SK
9062 {
9063 /* Check if the mask-expr has a consistent shape with the
9064 outmost WHERE mask-expr. */
524af0d6 9065 if (!resolve_where_shape (cblock->expr1, e))
edf1eac2 9066 gfc_error ("WHERE mask at %L has inconsistent shape",
a513927a 9067 &cblock->expr1->where);
edf1eac2 9068 }
6de9cd9a
DN
9069
9070 /* the assignment statement of a WHERE statement, or the first
edf1eac2 9071 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
9072 cnext = cblock->next;
9073 while (cnext)
edf1eac2
SK
9074 {
9075 switch (cnext->op)
9076 {
9077 /* WHERE assignment statement */
9078 case EXEC_ASSIGN:
9079
9080 /* Check shape consistent for WHERE assignment target. */
524af0d6 9081 if (e && !resolve_where_shape (cnext->expr1, e))
edf1eac2 9082 gfc_error ("WHERE assignment target at %L has "
a513927a 9083 "inconsistent shape", &cnext->expr1->where);
edf1eac2
SK
9084 break;
9085
4d382327 9086
a00b8d1a
PT
9087 case EXEC_ASSIGN_CALL:
9088 resolve_call (cnext);
42cd23cb 9089 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 9090 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 9091 &cnext->ext.actual->expr->where);
a00b8d1a
PT
9092 break;
9093
edf1eac2
SK
9094 /* WHERE or WHERE construct is part of a where-body-construct */
9095 case EXEC_WHERE:
9096 resolve_where (cnext, e);
9097 break;
9098
9099 default:
9100 gfc_error ("Unsupported statement inside WHERE at %L",
9101 &cnext->loc);
9102 }
9103 /* the next statement within the same where-body-construct */
9104 cnext = cnext->next;
6de9cd9a
DN
9105 }
9106 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9107 cblock = cblock->block;
9108 }
9109}
9110
9111
6de9cd9a
DN
9112/* Resolve assignment in FORALL construct.
9113 NVAR is the number of FORALL index variables, and VAR_EXPR records the
9114 FORALL index variables. */
9115
9116static void
9117gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
9118{
9119 int n;
9120
9121 for (n = 0; n < nvar; n++)
9122 {
9123 gfc_symbol *forall_index;
9124
9125 forall_index = var_expr[n]->symtree->n.sym;
9126
9127 /* Check whether the assignment target is one of the FORALL index
edf1eac2 9128 variable. */
a513927a
SK
9129 if ((code->expr1->expr_type == EXPR_VARIABLE)
9130 && (code->expr1->symtree->n.sym == forall_index))
edf1eac2 9131 gfc_error ("Assignment to a FORALL index variable at %L",
a513927a 9132 &code->expr1->where);
6de9cd9a 9133 else
edf1eac2
SK
9134 {
9135 /* If one of the FORALL index variables doesn't appear in the
67cec813
PT
9136 assignment variable, then there could be a many-to-one
9137 assignment. Emit a warning rather than an error because the
9138 mask could be resolving this problem. */
524af0d6 9139 if (!find_forall_index (code->expr1, forall_index, 0))
db30e21c 9140 gfc_warning (0, "The FORALL with index %qs is not used on the "
67cec813
PT
9141 "left side of the assignment at %L and so might "
9142 "cause multiple assignment to this object",
a513927a 9143 var_expr[n]->symtree->name, &code->expr1->where);
edf1eac2 9144 }
6de9cd9a
DN
9145 }
9146}
9147
9148
9149/* Resolve WHERE statement in FORALL construct. */
9150
9151static void
edf1eac2
SK
9152gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
9153 gfc_expr **var_expr)
9154{
6de9cd9a
DN
9155 gfc_code *cblock;
9156 gfc_code *cnext;
9157
9158 cblock = code->block;
9159 while (cblock)
9160 {
9161 /* the assignment statement of a WHERE statement, or the first
edf1eac2 9162 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
9163 cnext = cblock->next;
9164 while (cnext)
edf1eac2
SK
9165 {
9166 switch (cnext->op)
9167 {
9168 /* WHERE assignment statement */
9169 case EXEC_ASSIGN:
9170 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
9171 break;
4d382327 9172
a00b8d1a
PT
9173 /* WHERE operator assignment statement */
9174 case EXEC_ASSIGN_CALL:
9175 resolve_call (cnext);
42cd23cb 9176 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 9177 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 9178 &cnext->ext.actual->expr->where);
a00b8d1a 9179 break;
edf1eac2
SK
9180
9181 /* WHERE or WHERE construct is part of a where-body-construct */
9182 case EXEC_WHERE:
9183 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
9184 break;
9185
9186 default:
9187 gfc_error ("Unsupported statement inside WHERE at %L",
9188 &cnext->loc);
9189 }
9190 /* the next statement within the same where-body-construct */
9191 cnext = cnext->next;
9192 }
6de9cd9a
DN
9193 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
9194 cblock = cblock->block;
9195 }
9196}
9197
9198
9199/* Traverse the FORALL body to check whether the following errors exist:
9200 1. For assignment, check if a many-to-one assignment happens.
9201 2. For WHERE statement, check the WHERE body to see if there is any
9202 many-to-one assignment. */
9203
9204static void
9205gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
9206{
9207 gfc_code *c;
9208
9209 c = code->block->next;
9210 while (c)
9211 {
9212 switch (c->op)
edf1eac2
SK
9213 {
9214 case EXEC_ASSIGN:
9215 case EXEC_POINTER_ASSIGN:
9216 gfc_resolve_assign_in_forall (c, nvar, var_expr);
9217 break;
9218
a00b8d1a
PT
9219 case EXEC_ASSIGN_CALL:
9220 resolve_call (c);
9221 break;
9222
edf1eac2
SK
9223 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
9224 there is no need to handle it here. */
9225 case EXEC_FORALL:
9226 break;
9227 case EXEC_WHERE:
9228 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
9229 break;
9230 default:
9231 break;
9232 }
6de9cd9a
DN
9233 /* The next statement in the FORALL body. */
9234 c = c->next;
9235 }
9236}
9237
9238
0e6834af 9239/* Counts the number of iterators needed inside a forall construct, including
4d382327 9240 nested forall constructs. This is used to allocate the needed memory
0e6834af
MM
9241 in gfc_resolve_forall. */
9242
4d382327 9243static int
0e6834af
MM
9244gfc_count_forall_iterators (gfc_code *code)
9245{
9246 int max_iters, sub_iters, current_iters;
9247 gfc_forall_iterator *fa;
9248
9249 gcc_assert(code->op == EXEC_FORALL);
9250 max_iters = 0;
9251 current_iters = 0;
9252
9253 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9254 current_iters ++;
4d382327 9255
0e6834af
MM
9256 code = code->block->next;
9257
9258 while (code)
4d382327 9259 {
0e6834af
MM
9260 if (code->op == EXEC_FORALL)
9261 {
9262 sub_iters = gfc_count_forall_iterators (code);
9263 if (sub_iters > max_iters)
9264 max_iters = sub_iters;
9265 }
9266 code = code->next;
9267 }
9268
9269 return current_iters + max_iters;
9270}
9271
9272
6de9cd9a
DN
9273/* Given a FORALL construct, first resolve the FORALL iterator, then call
9274 gfc_resolve_forall_body to resolve the FORALL body. */
9275
6de9cd9a
DN
9276static void
9277gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
9278{
9279 static gfc_expr **var_expr;
9280 static int total_var = 0;
9281 static int nvar = 0;
0e6834af 9282 int old_nvar, tmp;
6de9cd9a 9283 gfc_forall_iterator *fa;
6de9cd9a
DN
9284 int i;
9285
0e6834af
MM
9286 old_nvar = nvar;
9287
6de9cd9a
DN
9288 /* Start to resolve a FORALL construct */
9289 if (forall_save == 0)
9290 {
9291 /* Count the total number of FORALL index in the nested FORALL
0e6834af
MM
9292 construct in order to allocate the VAR_EXPR with proper size. */
9293 total_var = gfc_count_forall_iterators (code);
6de9cd9a 9294
f7b529fa 9295 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
93acb62c 9296 var_expr = XCNEWVEC (gfc_expr *, total_var);
6de9cd9a
DN
9297 }
9298
9299 /* The information about FORALL iterator, including FORALL index start, end
9300 and stride. The FORALL index can not appear in start, end or stride. */
9301 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
9302 {
9303 /* Check if any outer FORALL index name is the same as the current
edf1eac2 9304 one. */
6de9cd9a 9305 for (i = 0; i < nvar; i++)
edf1eac2
SK
9306 {
9307 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
9308 {
9309 gfc_error ("An outer FORALL construct already has an index "
9310 "with this name %L", &fa->var->where);
9311 }
9312 }
6de9cd9a
DN
9313
9314 /* Record the current FORALL index. */
9315 var_expr[nvar] = gfc_copy_expr (fa->var);
9316
6de9cd9a 9317 nvar++;
0e6834af
MM
9318
9319 /* No memory leak. */
9320 gcc_assert (nvar <= total_var);
6de9cd9a
DN
9321 }
9322
9323 /* Resolve the FORALL body. */
9324 gfc_resolve_forall_body (code, nvar, var_expr);
9325
9326 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6c7a4dfd 9327 gfc_resolve_blocks (code->block, ns);
6de9cd9a 9328
0e6834af
MM
9329 tmp = nvar;
9330 nvar = old_nvar;
9331 /* Free only the VAR_EXPRs allocated in this frame. */
9332 for (i = nvar; i < tmp; i++)
9333 gfc_free_expr (var_expr[i]);
6de9cd9a 9334
0e6834af
MM
9335 if (nvar == 0)
9336 {
9337 /* We are in the outermost FORALL construct. */
9338 gcc_assert (forall_save == 0);
9339
9340 /* VAR_EXPR is not needed any more. */
cede9502 9341 free (var_expr);
0e6834af
MM
9342 total_var = 0;
9343 }
6de9cd9a
DN
9344}
9345
9346
9abe5e56
DK
9347/* Resolve a BLOCK construct statement. */
9348
9349static void
9350resolve_block_construct (gfc_code* code)
9351{
03af1e4c
DK
9352 /* Resolve the BLOCK's namespace. */
9353 gfc_resolve (code->ext.block.ns);
52bf62f9
DK
9354
9355 /* For an ASSOCIATE block, the associations (and their targets) are already
3e78238a 9356 resolved during resolve_symbol. */
9abe5e56
DK
9357}
9358
9359
9360/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
6de9cd9a
DN
9361 DO code nodes. */
9362
6c7a4dfd 9363void
edf1eac2 9364gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6de9cd9a 9365{
524af0d6 9366 bool t;
6de9cd9a
DN
9367
9368 for (; b; b = b->block)
9369 {
a513927a 9370 t = gfc_resolve_expr (b->expr1);
524af0d6
JB
9371 if (!gfc_resolve_expr (b->expr2))
9372 t = false;
6de9cd9a
DN
9373
9374 switch (b->op)
9375 {
9376 case EXEC_IF:
524af0d6 9377 if (t && b->expr1 != NULL
a513927a 9378 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
edf1eac2 9379 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 9380 &b->expr1->where);
6de9cd9a
DN
9381 break;
9382
9383 case EXEC_WHERE:
524af0d6 9384 if (t
a513927a
SK
9385 && b->expr1 != NULL
9386 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
edf1eac2 9387 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
a513927a 9388 &b->expr1->where);
6de9cd9a
DN
9389 break;
9390
edf1eac2 9391 case EXEC_GOTO:
79bd1948 9392 resolve_branch (b->label1, b);
edf1eac2 9393 break;
6de9cd9a 9394
9abe5e56
DK
9395 case EXEC_BLOCK:
9396 resolve_block_construct (b);
9397 break;
9398
6de9cd9a 9399 case EXEC_SELECT:
cf2b3c22 9400 case EXEC_SELECT_TYPE:
6de9cd9a
DN
9401 case EXEC_FORALL:
9402 case EXEC_DO:
9403 case EXEC_DO_WHILE:
8c6a85e3 9404 case EXEC_DO_CONCURRENT:
d0a4a61c 9405 case EXEC_CRITICAL:
5e805e44
JJ
9406 case EXEC_READ:
9407 case EXEC_WRITE:
9408 case EXEC_IOLENGTH:
6f0f0b2e 9409 case EXEC_WAIT:
6de9cd9a
DN
9410 break;
9411
41dbbb37
TS
9412 case EXEC_OACC_PARALLEL_LOOP:
9413 case EXEC_OACC_PARALLEL:
9414 case EXEC_OACC_KERNELS_LOOP:
9415 case EXEC_OACC_KERNELS:
9416 case EXEC_OACC_DATA:
9417 case EXEC_OACC_HOST_DATA:
9418 case EXEC_OACC_LOOP:
9419 case EXEC_OACC_UPDATE:
9420 case EXEC_OACC_WAIT:
9421 case EXEC_OACC_CACHE:
9422 case EXEC_OACC_ENTER_DATA:
9423 case EXEC_OACC_EXIT_DATA:
4bf9e5a8 9424 case EXEC_OACC_ATOMIC:
db941d7e 9425 case EXEC_OACC_ROUTINE:
6c7a4dfd
JJ
9426 case EXEC_OMP_ATOMIC:
9427 case EXEC_OMP_CRITICAL:
f014c653
JJ
9428 case EXEC_OMP_DISTRIBUTE:
9429 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
9430 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
9431 case EXEC_OMP_DISTRIBUTE_SIMD:
6c7a4dfd 9432 case EXEC_OMP_DO:
dd2fc525 9433 case EXEC_OMP_DO_SIMD:
6c7a4dfd
JJ
9434 case EXEC_OMP_MASTER:
9435 case EXEC_OMP_ORDERED:
9436 case EXEC_OMP_PARALLEL:
9437 case EXEC_OMP_PARALLEL_DO:
dd2fc525 9438 case EXEC_OMP_PARALLEL_DO_SIMD:
6c7a4dfd
JJ
9439 case EXEC_OMP_PARALLEL_SECTIONS:
9440 case EXEC_OMP_PARALLEL_WORKSHARE:
9441 case EXEC_OMP_SECTIONS:
dd2fc525 9442 case EXEC_OMP_SIMD:
6c7a4dfd 9443 case EXEC_OMP_SINGLE:
f014c653
JJ
9444 case EXEC_OMP_TARGET:
9445 case EXEC_OMP_TARGET_DATA:
9446 case EXEC_OMP_TARGET_TEAMS:
9447 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
9448 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
9449 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9450 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
9451 case EXEC_OMP_TARGET_UPDATE:
a68ab351 9452 case EXEC_OMP_TASK:
dd2fc525 9453 case EXEC_OMP_TASKGROUP:
a68ab351 9454 case EXEC_OMP_TASKWAIT:
20906c66 9455 case EXEC_OMP_TASKYIELD:
f014c653
JJ
9456 case EXEC_OMP_TEAMS:
9457 case EXEC_OMP_TEAMS_DISTRIBUTE:
9458 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
9459 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
9460 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
6c7a4dfd
JJ
9461 case EXEC_OMP_WORKSHARE:
9462 break;
9463
6de9cd9a 9464 default:
9abe5e56 9465 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
6de9cd9a
DN
9466 }
9467
b46ebd6c 9468 gfc_resolve_code (b->next, ns);
6de9cd9a
DN
9469 }
9470}
9471
9472
c5422462 9473/* Does everything to resolve an ordinary assignment. Returns true
df2fba9e 9474 if this is an interface assignment. */
c5422462
PT
9475static bool
9476resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
9477{
9478 bool rval = false;
9479 gfc_expr *lhs;
9480 gfc_expr *rhs;
9481 int llen = 0;
9482 int rlen = 0;
9483 int n;
9484 gfc_ref *ref;
83ba23b7 9485 symbol_attribute attr;
c5422462 9486
524af0d6 9487 if (gfc_extend_assign (code, ns))
c5422462 9488 {
4a44a72d
DK
9489 gfc_expr** rhsptr;
9490
9491 if (code->op == EXEC_ASSIGN_CALL)
c5422462 9492 {
4a44a72d
DK
9493 lhs = code->ext.actual->expr;
9494 rhsptr = &code->ext.actual->next->expr;
4a44a72d
DK
9495 }
9496 else
9497 {
9498 gfc_actual_arglist* args;
9499 gfc_typebound_proc* tbp;
9500
9501 gcc_assert (code->op == EXEC_COMPCALL);
9502
9503 args = code->expr1->value.compcall.actual;
9504 lhs = args->expr;
9505 rhsptr = &args->next->expr;
9506
9507 tbp = code->expr1->value.compcall.tbp;
9508 gcc_assert (!tbp->is_generic);
c5422462
PT
9509 }
9510
9511 /* Make a temporary rhs when there is a default initializer
9512 and rhs is the same symbol as the lhs. */
4a44a72d
DK
9513 if ((*rhsptr)->expr_type == EXPR_VARIABLE
9514 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
16e520b6 9515 && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
4a44a72d
DK
9516 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
9517 *rhsptr = gfc_get_parentheses (*rhsptr);
c5422462
PT
9518
9519 return true;
9520 }
9521
a513927a 9522 lhs = code->expr1;
c5422462
PT
9523 rhs = code->expr2;
9524
00a4618b 9525 if (rhs->is_boz
524af0d6 9526 && !gfc_notify_std (GFC_STD_GNU, "BOZ literal at %L outside "
22c23886 9527 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
524af0d6 9528 &code->loc))
00a4618b
TB
9529 return false;
9530
9531 /* Handle the case of a BOZ literal on the RHS. */
9532 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
9533 {
4956b1f1 9534 int rc;
73e42eef 9535 if (warn_surprising)
48749dbc
MLI
9536 gfc_warning (OPT_Wsurprising,
9537 "BOZ literal at %L is bitwise transferred "
9538 "non-integer symbol %qs", &code->loc,
00a4618b
TB
9539 lhs->symtree->n.sym->name);
9540
c7abc45c
TB
9541 if (!gfc_convert_boz (rhs, &lhs->ts))
9542 return false;
4956b1f1
TB
9543 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
9544 {
9545 if (rc == ARITH_UNDERFLOW)
9546 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
9547 ". This check can be disabled with the option "
a4d9b221 9548 "%<-fno-range-check%>", &rhs->where);
4956b1f1
TB
9549 else if (rc == ARITH_OVERFLOW)
9550 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
9551 ". This check can be disabled with the option "
a4d9b221 9552 "%<-fno-range-check%>", &rhs->where);
4956b1f1
TB
9553 else if (rc == ARITH_NAN)
9554 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
9555 ". This check can be disabled with the option "
a4d9b221 9556 "%<-fno-range-check%>", &rhs->where);
4956b1f1
TB
9557 return false;
9558 }
00a4618b
TB
9559 }
9560
c5422462 9561 if (lhs->ts.type == BT_CHARACTER
a96c39ea 9562 && warn_character_truncation)
c5422462 9563 {
bc21d315
JW
9564 if (lhs->ts.u.cl != NULL
9565 && lhs->ts.u.cl->length != NULL
9566 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9567 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
c5422462
PT
9568
9569 if (rhs->expr_type == EXPR_CONSTANT)
9570 rlen = rhs->value.character.length;
9571
bc21d315 9572 else if (rhs->ts.u.cl != NULL
4a44a72d 9573 && rhs->ts.u.cl->length != NULL
bc21d315
JW
9574 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
9575 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
c5422462
PT
9576
9577 if (rlen && llen && rlen > llen)
4daa149b
TB
9578 gfc_warning_now (OPT_Wcharacter_truncation,
9579 "CHARACTER expression will be truncated "
9580 "in assignment (%d/%d) at %L",
9581 llen, rlen, &code->loc);
c5422462
PT
9582 }
9583
9584 /* Ensure that a vector index expression for the lvalue is evaluated
908a2235 9585 to a temporary if the lvalue symbol is referenced in it. */
c5422462
PT
9586 if (lhs->rank)
9587 {
9588 for (ref = lhs->ref; ref; ref= ref->next)
9589 if (ref->type == REF_ARRAY)
9590 {
9591 for (n = 0; n < ref->u.ar.dimen; n++)
908a2235 9592 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
a68ab351
JJ
9593 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
9594 ref->u.ar.start[n]))
c5422462
PT
9595 ref->u.ar.start[n]
9596 = gfc_get_parentheses (ref->u.ar.start[n]);
9597 }
9598 }
9599
9600 if (gfc_pure (NULL))
9601 {
c5422462
PT
9602 if (lhs->ts.type == BT_DERIVED
9603 && lhs->expr_type == EXPR_VARIABLE
bc21d315 9604 && lhs->ts.u.derived->attr.pointer_comp
4eceddd7 9605 && rhs->expr_type == EXPR_VARIABLE
d3a9eea2
TB
9606 && (gfc_impure_variable (rhs->symtree->n.sym)
9607 || gfc_is_coindexed (rhs)))
9608 {
9609 /* F2008, C1283. */
9610 if (gfc_is_coindexed (rhs))
9611 gfc_error ("Coindexed expression at %L is assigned to "
9612 "a derived type variable with a POINTER "
9613 "component in a PURE procedure",
9614 &rhs->where);
9615 else
9616 gfc_error ("The impure variable at %L is assigned to "
9617 "a derived type variable with a POINTER "
9618 "component in a PURE procedure (12.6)",
9619 &rhs->where);
9620 return rval;
9621 }
9622
9623 /* Fortran 2008, C1283. */
9624 if (gfc_is_coindexed (lhs))
c5422462 9625 {
d3a9eea2
TB
9626 gfc_error ("Assignment to coindexed variable at %L in a PURE "
9627 "procedure", &rhs->where);
c5422462
PT
9628 return rval;
9629 }
9630 }
9631
f1f39033
PT
9632 if (gfc_implicit_pure (NULL))
9633 {
9634 if (lhs->expr_type == EXPR_VARIABLE
9635 && lhs->symtree->n.sym != gfc_current_ns->proc_name
9636 && lhs->symtree->n.sym->ns != gfc_current_ns)
9964e830 9637 gfc_unset_implicit_pure (NULL);
f1f39033
PT
9638
9639 if (lhs->ts.type == BT_DERIVED
9640 && lhs->expr_type == EXPR_VARIABLE
9641 && lhs->ts.u.derived->attr.pointer_comp
9642 && rhs->expr_type == EXPR_VARIABLE
9643 && (gfc_impure_variable (rhs->symtree->n.sym)
9644 || gfc_is_coindexed (rhs)))
9964e830 9645 gfc_unset_implicit_pure (NULL);
f1f39033
PT
9646
9647 /* Fortran 2008, C1283. */
9648 if (gfc_is_coindexed (lhs))
9964e830 9649 gfc_unset_implicit_pure (NULL);
f1f39033
PT
9650 }
9651
83ba23b7
TB
9652 /* F2008, 7.2.1.2. */
9653 attr = gfc_expr_attr (lhs);
9654 if (lhs->ts.type == BT_CLASS && attr.allocatable)
9655 {
9656 if (attr.codimension)
9657 {
9658 gfc_error ("Assignment to polymorphic coarray at %L is not "
9659 "permitted", &lhs->where);
9660 return false;
9661 }
9662 if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
9663 "polymorphic variable at %L", &lhs->where))
9664 return false;
203c7ebf 9665 if (!flag_realloc_lhs)
83ba23b7
TB
9666 {
9667 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
a4d9b221 9668 "requires %<-frealloc-lhs%>", &lhs->where);
83ba23b7
TB
9669 return false;
9670 }
9671 /* See PR 43366. */
9672 gfc_error ("Assignment to an allocatable polymorphic variable at %L "
9673 "is not yet supported", &lhs->where);
9674 return false;
9675 }
9676 else if (lhs->ts.type == BT_CLASS)
0ae278e7 9677 {
83ba23b7
TB
9678 gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
9679 "assignment at %L - check that there is a matching specific "
9680 "subroutine for '=' operator", &lhs->where);
0ae278e7
JW
9681 return false;
9682 }
9683
8a8d1a16
TB
9684 bool lhs_coindexed = gfc_is_coindexed (lhs);
9685
d3a9eea2 9686 /* F2008, Section 7.2.1.2. */
8a8d1a16 9687 if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
d3a9eea2 9688 {
6726b907 9689 gfc_error ("Coindexed variable must not have an allocatable ultimate "
d3a9eea2
TB
9690 "component in assignment at %L", &lhs->where);
9691 return false;
9692 }
9693
c5422462 9694 gfc_check_assign (lhs, rhs, 1);
8a8d1a16 9695
22c23886
PT
9696 /* Assign the 'data' of a class object to a derived type. */
9697 if (lhs->ts.type == BT_DERIVED
9698 && rhs->ts.type == BT_CLASS)
9699 gfc_add_data_component (rhs);
9700
b5116268
TB
9701 /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a coindexed variable.
9702 Additionally, insert this code when the RHS is a CAF as we then use the
9703 GFC_ISYM_CAF_SEND intrinsic just to avoid a temporary; but do not do so if
5c75088c
TB
9704 the LHS is (re)allocatable or has a vector subscript. If the LHS is a
9705 noncoindexed array and the RHS is a coindexed scalar, use the normal code
9706 path. */
f19626cf 9707 if (flag_coarray == GFC_FCOARRAY_LIB
b5116268
TB
9708 && (lhs_coindexed
9709 || (code->expr2->expr_type == EXPR_FUNCTION
9710 && code->expr2->value.function.isym
9711 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET
5c75088c 9712 && (code->expr1->rank == 0 || code->expr2->rank != 0)
b5116268
TB
9713 && !gfc_expr_attr (rhs).allocatable
9714 && !gfc_has_vector_subscript (rhs))))
9715 {
9716 if (code->expr2->expr_type == EXPR_FUNCTION
9717 && code->expr2->value.function.isym
9718 && code->expr2->value.function.isym->id == GFC_ISYM_CAF_GET)
9719 remove_caf_get_intrinsic (code->expr2);
8a8d1a16
TB
9720 code->op = EXEC_CALL;
9721 gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree, true);
9722 code->resolved_sym = code->symtree->n.sym;
9723 code->resolved_sym->attr.flavor = FL_PROCEDURE;
9724 code->resolved_sym->attr.intrinsic = 1;
9725 code->resolved_sym->attr.subroutine = 1;
9726 code->resolved_isym = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
9727 gfc_commit_symbol (code->resolved_sym);
9728 code->ext.actual = gfc_get_actual_arglist ();
9729 code->ext.actual->expr = lhs;
9730 code->ext.actual->next = gfc_get_actual_arglist ();
9731 code->ext.actual->next->expr = rhs;
9732 code->expr1 = NULL;
9733 code->expr2 = NULL;
9734 }
9735
c5422462
PT
9736 return false;
9737}
9738
9abe5e56 9739
4d382327
AF
9740/* Add a component reference onto an expression. */
9741
9742static void
9743add_comp_ref (gfc_expr *e, gfc_component *c)
9744{
9745 gfc_ref **ref;
9746 ref = &(e->ref);
9747 while (*ref)
9748 ref = &((*ref)->next);
9749 *ref = gfc_get_ref ();
9750 (*ref)->type = REF_COMPONENT;
9751 (*ref)->u.c.sym = e->ts.u.derived;
9752 (*ref)->u.c.component = c;
9753 e->ts = c->ts;
9754
9755 /* Add a full array ref, as necessary. */
9756 if (c->as)
9757 {
9758 gfc_add_full_array_ref (e, c->as);
9759 e->rank = c->as->rank;
9760 }
9761}
9762
9763
9764/* Build an assignment. Keep the argument 'op' for future use, so that
9765 pointer assignments can be made. */
9766
9767static gfc_code *
9768build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
9769 gfc_component *comp1, gfc_component *comp2, locus loc)
9770{
9771 gfc_code *this_code;
9772
11e5274a 9773 this_code = gfc_get_code (op);
4d382327
AF
9774 this_code->next = NULL;
9775 this_code->expr1 = gfc_copy_expr (expr1);
9776 this_code->expr2 = gfc_copy_expr (expr2);
9777 this_code->loc = loc;
9778 if (comp1 && comp2)
9779 {
9780 add_comp_ref (this_code->expr1, comp1);
9781 add_comp_ref (this_code->expr2, comp2);
9782 }
9783
9784 return this_code;
9785}
9786
9787
9788/* Makes a temporary variable expression based on the characteristics of
9789 a given variable expression. */
9790
9791static gfc_expr*
9792get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
9793{
9794 static int serial = 0;
9795 char name[GFC_MAX_SYMBOL_LEN];
9796 gfc_symtree *tmp;
9797 gfc_array_spec *as;
9798 gfc_array_ref *aref;
9799 gfc_ref *ref;
9800
bbf38bcf 9801 sprintf (name, GFC_PREFIX("DA%d"), serial++);
4d382327
AF
9802 gfc_get_sym_tree (name, ns, &tmp, false);
9803 gfc_add_type (tmp->n.sym, &e->ts, NULL);
9804
9805 as = NULL;
9806 ref = NULL;
9807 aref = NULL;
9808
4d382327 9809 /* Obtain the arrayspec for the temporary. */
79124116
PT
9810 if (e->rank && e->expr_type != EXPR_ARRAY
9811 && e->expr_type != EXPR_FUNCTION
9812 && e->expr_type != EXPR_OP)
4d382327
AF
9813 {
9814 aref = gfc_find_array_ref (e);
9815 if (e->expr_type == EXPR_VARIABLE
9816 && e->symtree->n.sym->as == aref->as)
9817 as = aref->as;
9818 else
9819 {
9820 for (ref = e->ref; ref; ref = ref->next)
9821 if (ref->type == REF_COMPONENT
9822 && ref->u.c.component->as == aref->as)
9823 {
9824 as = aref->as;
9825 break;
9826 }
9827 }
9828 }
9829
9830 /* Add the attributes and the arrayspec to the temporary. */
9831 tmp->n.sym->attr = gfc_expr_attr (e);
9d827441
TB
9832 tmp->n.sym->attr.function = 0;
9833 tmp->n.sym->attr.result = 0;
9834 tmp->n.sym->attr.flavor = FL_VARIABLE;
9835
4d382327
AF
9836 if (as)
9837 {
9838 tmp->n.sym->as = gfc_copy_array_spec (as);
9839 if (!ref)
9840 ref = e->ref;
9841 if (as->type == AS_DEFERRED)
9842 tmp->n.sym->attr.allocatable = 1;
9843 }
79124116
PT
9844 else if (e->rank && (e->expr_type == EXPR_ARRAY
9845 || e->expr_type == EXPR_FUNCTION
9846 || e->expr_type == EXPR_OP))
9847 {
9848 tmp->n.sym->as = gfc_get_array_spec ();
9849 tmp->n.sym->as->type = AS_DEFERRED;
9850 tmp->n.sym->as->rank = e->rank;
9851 tmp->n.sym->attr.allocatable = 1;
9852 tmp->n.sym->attr.dimension = 1;
9853 }
4d382327
AF
9854 else
9855 tmp->n.sym->attr.dimension = 0;
9856
9857 gfc_set_sym_referenced (tmp->n.sym);
28a595fc 9858 gfc_commit_symbol (tmp->n.sym);
4d382327
AF
9859 e = gfc_lval_expr_from_sym (tmp->n.sym);
9860
9861 /* Should the lhs be a section, use its array ref for the
9862 temporary expression. */
9863 if (aref && aref->type != AR_FULL)
9864 {
9865 gfc_free_ref_list (e->ref);
9866 e->ref = gfc_copy_ref (ref);
9867 }
9868 return e;
9869}
9870
9871
9872/* Add one line of code to the code chain, making sure that 'head' and
9873 'tail' are appropriately updated. */
9874
9875static void
9876add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
9877{
9878 gcc_assert (this_code);
9879 if (*head == NULL)
9880 *head = *tail = *this_code;
9881 else
9882 *tail = gfc_append_code (*tail, *this_code);
9883 *this_code = NULL;
9884}
9885
9886
9887/* Counts the potential number of part array references that would
9888 result from resolution of typebound defined assignments. */
9889
9890static int
9891nonscalar_typebound_assign (gfc_symbol *derived, int depth)
9892{
9893 gfc_component *c;
9894 int c_depth = 0, t_depth;
9895
9896 for (c= derived->components; c; c = c->next)
9897 {
9898 if ((c->ts.type != BT_DERIVED
9899 || c->attr.pointer
9900 || c->attr.allocatable
9901 || c->attr.proc_pointer_comp
9902 || c->attr.class_pointer
9903 || c->attr.proc_pointer)
9904 && !c->attr.defined_assign_comp)
9905 continue;
9906
9907 if (c->as && c_depth == 0)
9908 c_depth = 1;
9909
9910 if (c->ts.u.derived->attr.defined_assign_comp)
9911 t_depth = nonscalar_typebound_assign (c->ts.u.derived,
9912 c->as ? 1 : 0);
9913 else
9914 t_depth = 0;
9915
9916 c_depth = t_depth > c_depth ? t_depth : c_depth;
9917 }
9918 return depth + c_depth;
9919}
9920
9921
9922/* Implement 7.2.1.3 of the F08 standard:
9923 "An intrinsic assignment where the variable is of derived type is
9924 performed as if each component of the variable were assigned from the
9925 corresponding component of expr using pointer assignment (7.2.2) for
6ff560cc
SK
9926 each pointer component, defined assignment for each nonpointer
9927 nonallocatable component of a type that has a type-bound defined
4d382327
AF
9928 assignment consistent with the component, intrinsic assignment for
9929 each other nonpointer nonallocatable component, ..."
9930
9931 The pointer assignments are taken care of by the intrinsic
9932 assignment of the structure itself. This function recursively adds
9933 defined assignments where required. The recursion is accomplished
b46ebd6c 9934 by calling gfc_resolve_code.
4d382327
AF
9935
9936 When the lhs in a defined assignment has intent INOUT, we need a
9937 temporary for the lhs. In pseudo-code:
9938
9939 ! Only call function lhs once.
9940 if (lhs is not a constant or an variable)
9941 temp_x = expr2
9942 expr2 => temp_x
9943 ! Do the intrinsic assignment
9944 expr1 = expr2
9945 ! Now do the defined assignments
9946 do over components with typebound defined assignment [%cmp]
9947 #if one component's assignment procedure is INOUT
9948 t1 = expr1
9949 #if expr2 non-variable
9950 temp_x = expr2
9951 expr2 => temp_x
9952 # endif
9953 expr1 = expr2
9954 # for each cmp
9955 t1%cmp {defined=} expr2%cmp
9956 expr1%cmp = t1%cmp
9957 #else
9958 expr1 = expr2
9959
9960 # for each cmp
9961 expr1%cmp {defined=} expr2%cmp
9962 #endif
9963 */
9964
9965/* The temporary assignments have to be put on top of the additional
9966 code to avoid the result being changed by the intrinsic assignment.
9967 */
9968static int component_assignment_level = 0;
9969static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
9970
9971static void
9972generate_component_assignments (gfc_code **code, gfc_namespace *ns)
9973{
9974 gfc_component *comp1, *comp2;
9975 gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
9976 gfc_expr *t1;
9977 int error_count, depth;
9978
9979 gfc_get_errors (NULL, &error_count);
9980
9981 /* Filter out continuing processing after an error. */
9982 if (error_count
9983 || (*code)->expr1->ts.type != BT_DERIVED
9984 || (*code)->expr2->ts.type != BT_DERIVED)
9985 return;
9986
9987 /* TODO: Handle more than one part array reference in assignments. */
9988 depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
9989 (*code)->expr1->rank ? 1 : 0);
9990 if (depth > 1)
9991 {
db30e21c 9992 gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
4d382327
AF
9993 "done because multiple part array references would "
9994 "occur in intermediate expressions.", &(*code)->loc);
9995 return;
9996 }
9997
9998 component_assignment_level++;
9999
10000 /* Create a temporary so that functions get called only once. */
10001 if ((*code)->expr2->expr_type != EXPR_VARIABLE
10002 && (*code)->expr2->expr_type != EXPR_CONSTANT)
10003 {
10004 gfc_expr *tmp_expr;
10005
10006 /* Assign the rhs to the temporary. */
10007 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10008 this_code = build_assignment (EXEC_ASSIGN,
10009 tmp_expr, (*code)->expr2,
10010 NULL, NULL, (*code)->loc);
10011 /* Add the code and substitute the rhs expression. */
10012 add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
10013 gfc_free_expr ((*code)->expr2);
10014 (*code)->expr2 = tmp_expr;
10015 }
10016
10017 /* Do the intrinsic assignment. This is not needed if the lhs is one
10018 of the temporaries generated here, since the intrinsic assignment
10019 to the final result already does this. */
10020 if ((*code)->expr1->symtree->n.sym->name[2] != '@')
10021 {
10022 this_code = build_assignment (EXEC_ASSIGN,
10023 (*code)->expr1, (*code)->expr2,
10024 NULL, NULL, (*code)->loc);
10025 add_code_to_chain (&this_code, &head, &tail);
10026 }
10027
10028 comp1 = (*code)->expr1->ts.u.derived->components;
10029 comp2 = (*code)->expr2->ts.u.derived->components;
10030
10031 t1 = NULL;
10032 for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
10033 {
10034 bool inout = false;
10035
10036 /* The intrinsic assignment does the right thing for pointers
10037 of all kinds and allocatable components. */
10038 if (comp1->ts.type != BT_DERIVED
10039 || comp1->attr.pointer
10040 || comp1->attr.allocatable
10041 || comp1->attr.proc_pointer_comp
10042 || comp1->attr.class_pointer
10043 || comp1->attr.proc_pointer)
10044 continue;
10045
10046 /* Make an assigment for this component. */
4d382327
AF
10047 this_code = build_assignment (EXEC_ASSIGN,
10048 (*code)->expr1, (*code)->expr2,
10049 comp1, comp2, (*code)->loc);
10050
10051 /* Convert the assignment if there is a defined assignment for
b46ebd6c 10052 this type. Otherwise, using the call from gfc_resolve_code,
4d382327 10053 recurse into its components. */
b46ebd6c 10054 gfc_resolve_code (this_code, ns);
4d382327
AF
10055
10056 if (this_code->op == EXEC_ASSIGN_CALL)
10057 {
4cbc9039 10058 gfc_formal_arglist *dummy_args;
4d382327
AF
10059 gfc_symbol *rsym;
10060 /* Check that there is a typebound defined assignment. If not,
10061 then this must be a module defined assignment. We cannot
10062 use the defined_assign_comp attribute here because it must
10063 be this derived type that has the defined assignment and not
10064 a parent type. */
10065 if (!(comp1->ts.u.derived->f2k_derived
10066 && comp1->ts.u.derived->f2k_derived
10067 ->tb_op[INTRINSIC_ASSIGN]))
10068 {
10069 gfc_free_statements (this_code);
10070 this_code = NULL;
10071 continue;
10072 }
10073
10074 /* If the first argument of the subroutine has intent INOUT
10075 a temporary must be generated and used instead. */
10076 rsym = this_code->resolved_sym;
4cbc9039
JW
10077 dummy_args = gfc_sym_get_dummy_args (rsym);
10078 if (dummy_args
10079 && dummy_args->sym->attr.intent == INTENT_INOUT)
4d382327
AF
10080 {
10081 gfc_code *temp_code;
10082 inout = true;
10083
10084 /* Build the temporary required for the assignment and put
10085 it at the head of the generated code. */
10086 if (!t1)
10087 {
10088 t1 = get_temp_from_expr ((*code)->expr1, ns);
10089 temp_code = build_assignment (EXEC_ASSIGN,
10090 t1, (*code)->expr1,
10091 NULL, NULL, (*code)->loc);
5ef7093d 10092
d14fc2c6
TB
10093 /* For allocatable LHS, check whether it is allocated. Note
10094 that allocatable components with defined assignment are
10095 not yet support. See PR 57696. */
10096 if ((*code)->expr1->symtree->n.sym->attr.allocatable)
5ef7093d
TB
10097 {
10098 gfc_code *block;
d14fc2c6
TB
10099 gfc_expr *e =
10100 gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
5ef7093d
TB
10101 block = gfc_get_code (EXEC_IF);
10102 block->block = gfc_get_code (EXEC_IF);
10103 block->block->expr1
10104 = gfc_build_intrinsic_call (ns,
d14fc2c6
TB
10105 GFC_ISYM_ALLOCATED, "allocated",
10106 (*code)->loc, 1, e);
5ef7093d
TB
10107 block->block->next = temp_code;
10108 temp_code = block;
10109 }
4d382327
AF
10110 add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
10111 }
10112
10113 /* Replace the first actual arg with the component of the
10114 temporary. */
10115 gfc_free_expr (this_code->ext.actual->expr);
10116 this_code->ext.actual->expr = gfc_copy_expr (t1);
10117 add_comp_ref (this_code->ext.actual->expr, comp1);
5ef7093d 10118
d14fc2c6
TB
10119 /* If the LHS variable is allocatable and wasn't allocated and
10120 the temporary is allocatable, pointer assign the address of
10121 the freshly allocated LHS to the temporary. */
10122 if ((*code)->expr1->symtree->n.sym->attr.allocatable
10123 && gfc_expr_attr ((*code)->expr1).allocatable)
5ef7093d
TB
10124 {
10125 gfc_code *block;
71e482dc
TB
10126 gfc_expr *cond;
10127
10128 cond = gfc_get_expr ();
5ef7093d
TB
10129 cond->ts.type = BT_LOGICAL;
10130 cond->ts.kind = gfc_default_logical_kind;
10131 cond->expr_type = EXPR_OP;
10132 cond->where = (*code)->loc;
10133 cond->value.op.op = INTRINSIC_NOT;
10134 cond->value.op.op1 = gfc_build_intrinsic_call (ns,
d14fc2c6
TB
10135 GFC_ISYM_ALLOCATED, "allocated",
10136 (*code)->loc, 1, gfc_copy_expr (t1));
5ef7093d
TB
10137 block = gfc_get_code (EXEC_IF);
10138 block->block = gfc_get_code (EXEC_IF);
10139 block->block->expr1 = cond;
10140 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10141 t1, (*code)->expr1,
10142 NULL, NULL, (*code)->loc);
10143 add_code_to_chain (&block, &head, &tail);
10144 }
4d382327 10145 }
71e482dc 10146 }
4d382327
AF
10147 else if (this_code->op == EXEC_ASSIGN && !this_code->next)
10148 {
10149 /* Don't add intrinsic assignments since they are already
10150 effected by the intrinsic assignment of the structure. */
10151 gfc_free_statements (this_code);
10152 this_code = NULL;
10153 continue;
10154 }
10155
10156 add_code_to_chain (&this_code, &head, &tail);
10157
10158 if (t1 && inout)
10159 {
10160 /* Transfer the value to the final result. */
10161 this_code = build_assignment (EXEC_ASSIGN,
10162 (*code)->expr1, t1,
10163 comp1, comp2, (*code)->loc);
10164 add_code_to_chain (&this_code, &head, &tail);
10165 }
10166 }
10167
4d382327
AF
10168 /* Put the temporary assignments at the top of the generated code. */
10169 if (tmp_head && component_assignment_level == 1)
10170 {
10171 gfc_append_code (tmp_head, head);
10172 head = tmp_head;
10173 tmp_head = tmp_tail = NULL;
10174 }
10175
71e482dc
TB
10176 // If we did a pointer assignment - thus, we need to ensure that the LHS is
10177 // not accidentally deallocated. Hence, nullify t1.
10178 if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
10179 && gfc_expr_attr ((*code)->expr1).allocatable)
10180 {
10181 gfc_code *block;
10182 gfc_expr *cond;
10183 gfc_expr *e;
10184
10185 e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
10186 cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
10187 (*code)->loc, 2, gfc_copy_expr (t1), e);
10188 block = gfc_get_code (EXEC_IF);
10189 block->block = gfc_get_code (EXEC_IF);
10190 block->block->expr1 = cond;
10191 block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
10192 t1, gfc_get_null_expr (&(*code)->loc),
10193 NULL, NULL, (*code)->loc);
10194 gfc_append_code (tail, block);
10195 tail = block;
10196 }
10197
4d382327
AF
10198 /* Now attach the remaining code chain to the input code. Step on
10199 to the end of the new code since resolution is complete. */
10200 gcc_assert ((*code)->op == EXEC_ASSIGN);
10201 tail->next = (*code)->next;
10202 /* Overwrite 'code' because this would place the intrinsic assignment
10203 before the temporary for the lhs is created. */
10204 gfc_free_expr ((*code)->expr1);
10205 gfc_free_expr ((*code)->expr2);
10206 **code = *head;
71e482dc
TB
10207 if (head != tail)
10208 free (head);
4d382327
AF
10209 *code = tail;
10210
10211 component_assignment_level--;
10212}
10213
10214
79124116
PT
10215/* F2008: Pointer function assignments are of the form:
10216 ptr_fcn (args) = expr
10217 This function breaks these assignments into two statements:
10218 temporary_pointer => ptr_fcn(args)
10219 temporary_pointer = expr */
10220
10221static bool
10222resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
10223{
10224 gfc_expr *tmp_ptr_expr;
10225 gfc_code *this_code;
10226 gfc_component *comp;
10227 gfc_symbol *s;
10228
10229 if ((*code)->expr1->expr_type != EXPR_FUNCTION)
10230 return false;
10231
10232 /* Even if standard does not support this feature, continue to build
10233 the two statements to avoid upsetting frontend_passes.c. */
10234 gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
10235 "%L", &(*code)->loc);
10236
10237 comp = gfc_get_proc_ptr_comp ((*code)->expr1);
10238
10239 if (comp)
10240 s = comp->ts.interface;
10241 else
10242 s = (*code)->expr1->symtree->n.sym;
10243
10244 if (s == NULL || !s->result->attr.pointer)
10245 {
10246 gfc_error ("The function result on the lhs of the assignment at "
10247 "%L must have the pointer attribute.",
10248 &(*code)->expr1->where);
10249 (*code)->op = EXEC_NOP;
10250 return false;
10251 }
10252
10253 tmp_ptr_expr = get_temp_from_expr ((*code)->expr2, ns);
10254
10255 /* get_temp_from_expression is set up for ordinary assignments. To that
10256 end, where array bounds are not known, arrays are made allocatable.
10257 Change the temporary to a pointer here. */
10258 tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
10259 tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
10260 tmp_ptr_expr->where = (*code)->loc;
10261
10262 this_code = build_assignment (EXEC_ASSIGN,
10263 tmp_ptr_expr, (*code)->expr2,
10264 NULL, NULL, (*code)->loc);
10265 this_code->next = (*code)->next;
10266 (*code)->next = this_code;
10267 (*code)->op = EXEC_POINTER_ASSIGN;
10268 (*code)->expr2 = (*code)->expr1;
10269 (*code)->expr1 = tmp_ptr_expr;
10270
10271 return true;
10272}
10273
10274
78ab5260
PT
10275/* Deferred character length assignments from an operator expression
10276 require a temporary because the character length of the lhs can
10277 change in the course of the assignment. */
10278
10279static bool
10280deferred_op_assign (gfc_code **code, gfc_namespace *ns)
10281{
10282 gfc_expr *tmp_expr;
10283 gfc_code *this_code;
10284
10285 if (!((*code)->expr1->ts.type == BT_CHARACTER
10286 && (*code)->expr1->ts.deferred && (*code)->expr1->rank
10287 && (*code)->expr2->expr_type == EXPR_OP))
10288 return false;
10289
10290 if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
10291 return false;
10292
10293 tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
10294 tmp_expr->where = (*code)->loc;
10295
10296 /* A new charlen is required to ensure that the variable string
10297 length is different to that of the original lhs. */
10298 tmp_expr->ts.u.cl = gfc_get_charlen();
10299 tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
10300 tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
10301 (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
10302
10303 tmp_expr->symtree->n.sym->ts.deferred = 1;
10304
10305 this_code = build_assignment (EXEC_ASSIGN,
10306 (*code)->expr1,
10307 gfc_copy_expr (tmp_expr),
10308 NULL, NULL, (*code)->loc);
10309
10310 (*code)->expr1 = tmp_expr;
10311
10312 this_code->next = (*code)->next;
10313 (*code)->next = this_code;
10314
10315 return true;
10316}
10317
10318
6de9cd9a
DN
10319/* Given a block of code, recursively resolve everything pointed to by this
10320 code block. */
10321
b46ebd6c
JJ
10322void
10323gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
6de9cd9a 10324{
6c7a4dfd 10325 int omp_workshare_save;
8c6a85e3 10326 int forall_save, do_concurrent_save;
6de9cd9a 10327 code_stack frame;
524af0d6 10328 bool t;
6de9cd9a
DN
10329
10330 frame.prev = cs_base;
10331 frame.head = code;
10332 cs_base = &frame;
10333
d80c695f 10334 find_reachable_labels (code);
0615f923 10335
6de9cd9a
DN
10336 for (; code; code = code->next)
10337 {
10338 frame.current = code;
d68bd5a8 10339 forall_save = forall_flag;
ce96d372 10340 do_concurrent_save = gfc_do_concurrent_flag;
6de9cd9a
DN
10341
10342 if (code->op == EXEC_FORALL)
10343 {
6de9cd9a 10344 forall_flag = 1;
6c7a4dfd 10345 gfc_resolve_forall (code, ns, forall_save);
d68bd5a8 10346 forall_flag = 2;
6c7a4dfd
JJ
10347 }
10348 else if (code->block)
10349 {
10350 omp_workshare_save = -1;
10351 switch (code->op)
10352 {
41dbbb37
TS
10353 case EXEC_OACC_PARALLEL_LOOP:
10354 case EXEC_OACC_PARALLEL:
10355 case EXEC_OACC_KERNELS_LOOP:
10356 case EXEC_OACC_KERNELS:
10357 case EXEC_OACC_DATA:
10358 case EXEC_OACC_HOST_DATA:
10359 case EXEC_OACC_LOOP:
10360 gfc_resolve_oacc_blocks (code, ns);
10361 break;
6c7a4dfd
JJ
10362 case EXEC_OMP_PARALLEL_WORKSHARE:
10363 omp_workshare_save = omp_workshare_flag;
10364 omp_workshare_flag = 1;
10365 gfc_resolve_omp_parallel_blocks (code, ns);
10366 break;
10367 case EXEC_OMP_PARALLEL:
10368 case EXEC_OMP_PARALLEL_DO:
dd2fc525 10369 case EXEC_OMP_PARALLEL_DO_SIMD:
6c7a4dfd 10370 case EXEC_OMP_PARALLEL_SECTIONS:
f014c653
JJ
10371 case EXEC_OMP_TARGET_TEAMS:
10372 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10373 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10374 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10375 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
a68ab351 10376 case EXEC_OMP_TASK:
f014c653
JJ
10377 case EXEC_OMP_TEAMS:
10378 case EXEC_OMP_TEAMS_DISTRIBUTE:
10379 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10380 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10381 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
6c7a4dfd
JJ
10382 omp_workshare_save = omp_workshare_flag;
10383 omp_workshare_flag = 0;
10384 gfc_resolve_omp_parallel_blocks (code, ns);
10385 break;
f014c653
JJ
10386 case EXEC_OMP_DISTRIBUTE:
10387 case EXEC_OMP_DISTRIBUTE_SIMD:
6c7a4dfd 10388 case EXEC_OMP_DO:
dd2fc525
JJ
10389 case EXEC_OMP_DO_SIMD:
10390 case EXEC_OMP_SIMD:
6c7a4dfd
JJ
10391 gfc_resolve_omp_do_blocks (code, ns);
10392 break;
d1039125 10393 case EXEC_SELECT_TYPE:
8c91ab34
DK
10394 /* Blocks are handled in resolve_select_type because we have
10395 to transform the SELECT TYPE into ASSOCIATE first. */
d1039125 10396 break;
8c6a85e3 10397 case EXEC_DO_CONCURRENT:
ce96d372 10398 gfc_do_concurrent_flag = 1;
8c6a85e3 10399 gfc_resolve_blocks (code->block, ns);
ce96d372 10400 gfc_do_concurrent_flag = 2;
8c6a85e3 10401 break;
6c7a4dfd
JJ
10402 case EXEC_OMP_WORKSHARE:
10403 omp_workshare_save = omp_workshare_flag;
10404 omp_workshare_flag = 1;
eea58adb 10405 /* FALL THROUGH */
6c7a4dfd
JJ
10406 default:
10407 gfc_resolve_blocks (code->block, ns);
10408 break;
10409 }
6de9cd9a 10410
6c7a4dfd
JJ
10411 if (omp_workshare_save != -1)
10412 omp_workshare_flag = omp_workshare_save;
10413 }
79124116 10414start:
524af0d6 10415 t = true;
713485cc 10416 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
a513927a 10417 t = gfc_resolve_expr (code->expr1);
d68bd5a8 10418 forall_flag = forall_save;
ce96d372 10419 gfc_do_concurrent_flag = do_concurrent_save;
d68bd5a8 10420
524af0d6
JB
10421 if (!gfc_resolve_expr (code->expr2))
10422 t = false;
6de9cd9a 10423
8460475b 10424 if (code->op == EXEC_ALLOCATE
524af0d6
JB
10425 && !gfc_resolve_expr (code->expr3))
10426 t = false;
8460475b 10427
6de9cd9a
DN
10428 switch (code->op)
10429 {
10430 case EXEC_NOP:
d80c695f 10431 case EXEC_END_BLOCK:
df1a69f6 10432 case EXEC_END_NESTED_BLOCK:
6de9cd9a 10433 case EXEC_CYCLE:
6de9cd9a
DN
10434 case EXEC_PAUSE:
10435 case EXEC_STOP:
d0a4a61c 10436 case EXEC_ERROR_STOP:
6de9cd9a
DN
10437 case EXEC_EXIT:
10438 case EXEC_CONTINUE:
10439 case EXEC_DT_END:
4a44a72d 10440 case EXEC_ASSIGN_CALL:
bc0229f9
TB
10441 break;
10442
d0a4a61c 10443 case EXEC_CRITICAL:
bc0229f9 10444 resolve_critical (code);
d0a4a61c
TB
10445 break;
10446
10447 case EXEC_SYNC_ALL:
10448 case EXEC_SYNC_IMAGES:
10449 case EXEC_SYNC_MEMORY:
10450 resolve_sync (code);
0e9a445b
PT
10451 break;
10452
5493aa17
TB
10453 case EXEC_LOCK:
10454 case EXEC_UNLOCK:
5df445a2
TB
10455 case EXEC_EVENT_POST:
10456 case EXEC_EVENT_WAIT:
10457 resolve_lock_unlock_event (code);
5493aa17
TB
10458 break;
10459
3d79abbd 10460 case EXEC_ENTRY:
0e9a445b
PT
10461 /* Keep track of which entry we are up to. */
10462 current_entry_id = code->ext.entry->id;
6de9cd9a
DN
10463 break;
10464
10465 case EXEC_WHERE:
10466 resolve_where (code, NULL);
10467 break;
10468
10469 case EXEC_GOTO:
a513927a 10470 if (code->expr1 != NULL)
ce2df7c6 10471 {
a513927a 10472 if (code->expr1->ts.type != BT_INTEGER)
edf1eac2 10473 gfc_error ("ASSIGNED GOTO statement at %L requires an "
a513927a
SK
10474 "INTEGER variable", &code->expr1->where);
10475 else if (code->expr1->symtree->n.sym->attr.assign != 1)
c4100eae 10476 gfc_error ("Variable %qs has not been assigned a target "
a513927a
SK
10477 "label at %L", code->expr1->symtree->n.sym->name,
10478 &code->expr1->where);
ce2df7c6
FW
10479 }
10480 else
79bd1948 10481 resolve_branch (code->label1, code);
6de9cd9a
DN
10482 break;
10483
10484 case EXEC_RETURN:
a513927a
SK
10485 if (code->expr1 != NULL
10486 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
b6398823 10487 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
a513927a 10488 "INTEGER return specifier", &code->expr1->where);
6de9cd9a
DN
10489 break;
10490
6b591ec0 10491 case EXEC_INIT_ASSIGN:
5c71a5e0 10492 case EXEC_END_PROCEDURE:
6b591ec0
PT
10493 break;
10494
6de9cd9a 10495 case EXEC_ASSIGN:
524af0d6 10496 if (!t)
6de9cd9a
DN
10497 break;
10498
b5116268 10499 /* Remove a GFC_ISYM_CAF_GET inserted for a coindexed variable on
1cc0e193 10500 the LHS. */
8a8d1a16
TB
10501 if (code->expr1->expr_type == EXPR_FUNCTION
10502 && code->expr1->value.function.isym
10503 && code->expr1->value.function.isym->id == GFC_ISYM_CAF_GET)
10504 remove_caf_get_intrinsic (code->expr1);
10505
79124116
PT
10506 /* If this is a pointer function in an lvalue variable context,
10507 the new code will have to be resolved afresh. This is also the
10508 case with an error, where the code is transformed into NOP to
10509 prevent ICEs downstream. */
10510 if (resolve_ptr_fcn_assign (&code, ns)
10511 || code->op == EXEC_NOP)
10512 goto start;
10513
22c23886 10514 if (!gfc_check_vardef_context (code->expr1, false, false, false,
524af0d6 10515 _("assignment")))
8c91ab34
DK
10516 break;
10517
c5422462 10518 if (resolve_ordinary_assign (code, ns))
664e411b
JW
10519 {
10520 if (code->op == EXEC_COMPCALL)
10521 goto compcall;
10522 else
10523 goto call;
10524 }
4d382327 10525
78ab5260
PT
10526 /* Check for dependencies in deferred character length array
10527 assignments and generate a temporary, if necessary. */
10528 if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
10529 break;
10530
4d382327 10531 /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
8a8d1a16 10532 if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
79124116 10533 && code->expr1->ts.u.derived
4d382327
AF
10534 && code->expr1->ts.u.derived->attr.defined_assign_comp)
10535 generate_component_assignments (&code, ns);
10536
6de9cd9a
DN
10537 break;
10538
10539 case EXEC_LABEL_ASSIGN:
79bd1948 10540 if (code->label1->defined == ST_LABEL_UNKNOWN)
edf1eac2 10541 gfc_error ("Label %d referenced at %L is never defined",
79bd1948 10542 code->label1->value, &code->label1->where);
524af0d6 10543 if (t
a513927a
SK
10544 && (code->expr1->expr_type != EXPR_VARIABLE
10545 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
10546 || code->expr1->symtree->n.sym->ts.kind
edf1eac2 10547 != gfc_default_integer_kind
a513927a 10548 || code->expr1->symtree->n.sym->as != NULL))
40f2165e 10549 gfc_error ("ASSIGN statement at %L requires a scalar "
a513927a 10550 "default INTEGER variable", &code->expr1->where);
6de9cd9a
DN
10551 break;
10552
10553 case EXEC_POINTER_ASSIGN:
8c91ab34
DK
10554 {
10555 gfc_expr* e;
6de9cd9a 10556
524af0d6 10557 if (!t)
8c91ab34
DK
10558 break;
10559
10560 /* This is both a variable definition and pointer assignment
10561 context, so check both of them. For rank remapping, a final
10562 array ref may be present on the LHS and fool gfc_expr_attr
10563 used in gfc_check_vardef_context. Remove it. */
10564 e = remove_last_array_ref (code->expr1);
57bf28ea 10565 t = gfc_check_vardef_context (e, true, false, false,
fea54935 10566 _("pointer assignment"));
524af0d6 10567 if (t)
57bf28ea 10568 t = gfc_check_vardef_context (e, false, false, false,
fea54935 10569 _("pointer assignment"));
8c91ab34 10570 gfc_free_expr (e);
524af0d6 10571 if (!t)
8c91ab34
DK
10572 break;
10573
10574 gfc_check_pointer_assign (code->expr1, code->expr2);
10575 break;
10576 }
6de9cd9a
DN
10577
10578 case EXEC_ARITHMETIC_IF:
e2eb0806
SK
10579 {
10580 gfc_expr *e = code->expr1;
10581
2d2de608
SK
10582 gfc_resolve_expr (e);
10583 if (e->expr_type == EXPR_NULL)
10584 gfc_error ("Invalid NULL at %L", &e->where);
10585
e2eb0806
SK
10586 if (t && (e->rank > 0
10587 || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
10588 gfc_error ("Arithmetic IF statement at %L requires a scalar "
2d2de608 10589 "REAL or INTEGER expression", &e->where);
e2eb0806
SK
10590
10591 resolve_branch (code->label1, code);
10592 resolve_branch (code->label2, code);
10593 resolve_branch (code->label3, code);
10594 }
6de9cd9a
DN
10595 break;
10596
10597 case EXEC_IF:
524af0d6 10598 if (t && code->expr1 != NULL
a513927a
SK
10599 && (code->expr1->ts.type != BT_LOGICAL
10600 || code->expr1->rank != 0))
6de9cd9a 10601 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 10602 &code->expr1->where);
6de9cd9a
DN
10603 break;
10604
10605 case EXEC_CALL:
10606 call:
10607 resolve_call (code);
10608 break;
10609
8e1f752a 10610 case EXEC_COMPCALL:
664e411b 10611 compcall:
6a943ee7 10612 resolve_typebound_subroutine (code);
8e1f752a
DK
10613 break;
10614
713485cc 10615 case EXEC_CALL_PPC:
9abe5e56 10616 resolve_ppc_call (code);
713485cc
JW
10617 break;
10618
6de9cd9a
DN
10619 case EXEC_SELECT:
10620 /* Select is complicated. Also, a SELECT construct could be
10621 a transformed computed GOTO. */
ad3e2ad2 10622 resolve_select (code, false);
6de9cd9a
DN
10623 break;
10624
cf2b3c22 10625 case EXEC_SELECT_TYPE:
8c91ab34 10626 resolve_select_type (code, ns);
cf2b3c22
TB
10627 break;
10628
9abe5e56 10629 case EXEC_BLOCK:
52bf62f9 10630 resolve_block_construct (code);
9abe5e56
DK
10631 break;
10632
6de9cd9a
DN
10633 case EXEC_DO:
10634 if (code->ext.iterator != NULL)
6c7a4dfd
JJ
10635 {
10636 gfc_iterator *iter = code->ext.iterator;
524af0d6 10637 if (gfc_resolve_iterator (iter, true, false))
6c7a4dfd
JJ
10638 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
10639 }
6de9cd9a
DN
10640 break;
10641
10642 case EXEC_DO_WHILE:
a513927a 10643 if (code->expr1 == NULL)
b46ebd6c
JJ
10644 gfc_internal_error ("gfc_resolve_code(): No expression on "
10645 "DO WHILE");
524af0d6 10646 if (t
a513927a
SK
10647 && (code->expr1->rank != 0
10648 || code->expr1->ts.type != BT_LOGICAL))
6de9cd9a 10649 gfc_error ("Exit condition of DO WHILE loop at %L must be "
a513927a 10650 "a scalar LOGICAL expression", &code->expr1->where);
6de9cd9a
DN
10651 break;
10652
10653 case EXEC_ALLOCATE:
524af0d6 10654 if (t)
b9332b09 10655 resolve_allocate_deallocate (code, "ALLOCATE");
6de9cd9a
DN
10656
10657 break;
10658
10659 case EXEC_DEALLOCATE:
524af0d6 10660 if (t)
b9332b09 10661 resolve_allocate_deallocate (code, "DEALLOCATE");
6de9cd9a
DN
10662
10663 break;
10664
10665 case EXEC_OPEN:
524af0d6 10666 if (!gfc_resolve_open (code->ext.open))
6de9cd9a
DN
10667 break;
10668
10669 resolve_branch (code->ext.open->err, code);
10670 break;
10671
10672 case EXEC_CLOSE:
524af0d6 10673 if (!gfc_resolve_close (code->ext.close))
6de9cd9a
DN
10674 break;
10675
10676 resolve_branch (code->ext.close->err, code);
10677 break;
10678
10679 case EXEC_BACKSPACE:
10680 case EXEC_ENDFILE:
10681 case EXEC_REWIND:
6403ec5f 10682 case EXEC_FLUSH:
524af0d6 10683 if (!gfc_resolve_filepos (code->ext.filepos))
6de9cd9a
DN
10684 break;
10685
10686 resolve_branch (code->ext.filepos->err, code);
10687 break;
10688
10689 case EXEC_INQUIRE:
524af0d6 10690 if (!gfc_resolve_inquire (code->ext.inquire))
8750f9cd
JB
10691 break;
10692
10693 resolve_branch (code->ext.inquire->err, code);
10694 break;
10695
10696 case EXEC_IOLENGTH:
6e45f57b 10697 gcc_assert (code->ext.inquire != NULL);
524af0d6 10698 if (!gfc_resolve_inquire (code->ext.inquire))
6de9cd9a
DN
10699 break;
10700
10701 resolve_branch (code->ext.inquire->err, code);
10702 break;
10703
6f0f0b2e 10704 case EXEC_WAIT:
524af0d6 10705 if (!gfc_resolve_wait (code->ext.wait))
6f0f0b2e
JD
10706 break;
10707
10708 resolve_branch (code->ext.wait->err, code);
10709 resolve_branch (code->ext.wait->end, code);
10710 resolve_branch (code->ext.wait->eor, code);
10711 break;
10712
6de9cd9a
DN
10713 case EXEC_READ:
10714 case EXEC_WRITE:
524af0d6 10715 if (!gfc_resolve_dt (code->ext.dt, &code->loc))
6de9cd9a
DN
10716 break;
10717
10718 resolve_branch (code->ext.dt->err, code);
10719 resolve_branch (code->ext.dt->end, code);
10720 resolve_branch (code->ext.dt->eor, code);
10721 break;
10722
0e6928d8
TS
10723 case EXEC_TRANSFER:
10724 resolve_transfer (code);
10725 break;
10726
8c6a85e3 10727 case EXEC_DO_CONCURRENT:
6de9cd9a
DN
10728 case EXEC_FORALL:
10729 resolve_forall_iterators (code->ext.forall_iterator);
10730
d5656544
TB
10731 if (code->expr1 != NULL
10732 && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
10733 gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
a513927a 10734 "expression", &code->expr1->where);
6de9cd9a
DN
10735 break;
10736
41dbbb37
TS
10737 case EXEC_OACC_PARALLEL_LOOP:
10738 case EXEC_OACC_PARALLEL:
10739 case EXEC_OACC_KERNELS_LOOP:
10740 case EXEC_OACC_KERNELS:
10741 case EXEC_OACC_DATA:
10742 case EXEC_OACC_HOST_DATA:
10743 case EXEC_OACC_LOOP:
10744 case EXEC_OACC_UPDATE:
10745 case EXEC_OACC_WAIT:
10746 case EXEC_OACC_CACHE:
10747 case EXEC_OACC_ENTER_DATA:
10748 case EXEC_OACC_EXIT_DATA:
4bf9e5a8 10749 case EXEC_OACC_ATOMIC:
dc7a8b4b 10750 case EXEC_OACC_DECLARE:
41dbbb37
TS
10751 gfc_resolve_oacc_directive (code, ns);
10752 break;
10753
6c7a4dfd
JJ
10754 case EXEC_OMP_ATOMIC:
10755 case EXEC_OMP_BARRIER:
dd2fc525
JJ
10756 case EXEC_OMP_CANCEL:
10757 case EXEC_OMP_CANCELLATION_POINT:
6c7a4dfd
JJ
10758 case EXEC_OMP_CRITICAL:
10759 case EXEC_OMP_FLUSH:
f014c653
JJ
10760 case EXEC_OMP_DISTRIBUTE:
10761 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
10762 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
10763 case EXEC_OMP_DISTRIBUTE_SIMD:
6c7a4dfd 10764 case EXEC_OMP_DO:
dd2fc525 10765 case EXEC_OMP_DO_SIMD:
6c7a4dfd
JJ
10766 case EXEC_OMP_MASTER:
10767 case EXEC_OMP_ORDERED:
10768 case EXEC_OMP_SECTIONS:
dd2fc525 10769 case EXEC_OMP_SIMD:
6c7a4dfd 10770 case EXEC_OMP_SINGLE:
f014c653
JJ
10771 case EXEC_OMP_TARGET:
10772 case EXEC_OMP_TARGET_DATA:
10773 case EXEC_OMP_TARGET_TEAMS:
10774 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
10775 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
10776 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10777 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
10778 case EXEC_OMP_TARGET_UPDATE:
10779 case EXEC_OMP_TASK:
dd2fc525 10780 case EXEC_OMP_TASKGROUP:
a68ab351 10781 case EXEC_OMP_TASKWAIT:
20906c66 10782 case EXEC_OMP_TASKYIELD:
f014c653
JJ
10783 case EXEC_OMP_TEAMS:
10784 case EXEC_OMP_TEAMS_DISTRIBUTE:
10785 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
10786 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
10787 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
6c7a4dfd
JJ
10788 case EXEC_OMP_WORKSHARE:
10789 gfc_resolve_omp_directive (code, ns);
10790 break;
10791
10792 case EXEC_OMP_PARALLEL:
10793 case EXEC_OMP_PARALLEL_DO:
dd2fc525 10794 case EXEC_OMP_PARALLEL_DO_SIMD:
6c7a4dfd
JJ
10795 case EXEC_OMP_PARALLEL_SECTIONS:
10796 case EXEC_OMP_PARALLEL_WORKSHARE:
10797 omp_workshare_save = omp_workshare_flag;
10798 omp_workshare_flag = 0;
10799 gfc_resolve_omp_directive (code, ns);
10800 omp_workshare_flag = omp_workshare_save;
10801 break;
10802
6de9cd9a 10803 default:
b46ebd6c 10804 gfc_internal_error ("gfc_resolve_code(): Bad statement code");
6de9cd9a
DN
10805 }
10806 }
10807
10808 cs_base = frame.prev;
10809}
10810
10811
10812/* Resolve initial values and make sure they are compatible with
10813 the variable. */
10814
10815static void
edf1eac2 10816resolve_values (gfc_symbol *sym)
6de9cd9a 10817{
524af0d6 10818 bool t;
80f95228 10819
22c30bc0 10820 if (sym->value == NULL)
6de9cd9a
DN
10821 return;
10822
80f95228
JW
10823 if (sym->value->expr_type == EXPR_STRUCTURE)
10824 t= resolve_structure_cons (sym->value, 1);
4d382327 10825 else
80f95228
JW
10826 t = gfc_resolve_expr (sym->value);
10827
524af0d6 10828 if (!t)
6de9cd9a
DN
10829 return;
10830
e35e87dc 10831 gfc_check_assign_symbol (sym, NULL, sym->value);
6de9cd9a
DN
10832}
10833
10834
a8b3b0b6
CR
10835/* Verify any BIND(C) derived types in the namespace so we can report errors
10836 for them once, rather than for each variable declared of that type. */
10837
10838static void
10839resolve_bind_c_derived_types (gfc_symbol *derived_sym)
10840{
10841 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
10842 && derived_sym->attr.is_bind_c == 1)
10843 verify_bind_c_derived_type (derived_sym);
4d382327 10844
a8b3b0b6
CR
10845 return;
10846}
10847
10848
4d382327 10849/* Verify that any binding labels used in a given namespace do not collide
77f8682b
TB
10850 with the names or binding labels of any global symbols. Multiple INTERFACE
10851 for the same procedure are permitted. */
a8b3b0b6
CR
10852
10853static void
10854gfc_verify_binding_labels (gfc_symbol *sym)
10855{
77f8682b
TB
10856 gfc_gsymbol *gsym;
10857 const char *module;
10858
10859 if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
10860 || sym->attr.flavor == FL_DERIVED || !sym->binding_label)
10861 return;
10862
10863 gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
10864
10865 if (sym->module)
10866 module = sym->module;
10867 else if (sym->ns && sym->ns->proc_name
10868 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10869 module = sym->ns->proc_name->name;
10870 else if (sym->ns && sym->ns->parent
10871 && sym->ns && sym->ns->parent->proc_name
10872 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
10873 module = sym->ns->parent->proc_name->name;
10874 else
10875 module = NULL;
4d382327 10876
77f8682b
TB
10877 if (!gsym
10878 || (!gsym->defined
10879 && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
a8b3b0b6 10880 {
77f8682b
TB
10881 if (!gsym)
10882 gsym = gfc_get_gsymbol (sym->binding_label);
10883 gsym->where = sym->declared_at;
10884 gsym->sym_name = sym->name;
10885 gsym->binding_label = sym->binding_label;
77f8682b
TB
10886 gsym->ns = sym->ns;
10887 gsym->mod_name = module;
10888 if (sym->attr.function)
10889 gsym->type = GSYM_FUNCTION;
10890 else if (sym->attr.subroutine)
10891 gsym->type = GSYM_SUBROUTINE;
10892 /* Mark as variable/procedure as defined, unless its an INTERFACE. */
10893 gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
10894 return;
10895 }
10896
10897 if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
10898 {
fea70c99 10899 gfc_error ("Variable %s with binding label %s at %L uses the same global "
77f8682b
TB
10900 "identifier as entity at %L", sym->name,
10901 sym->binding_label, &sym->declared_at, &gsym->where);
10902 /* Clear the binding label to prevent checking multiple times. */
10903 sym->binding_label = NULL;
a8b3b0b6 10904
a8b3b0b6 10905 }
78ab5260 10906 else if (sym->attr.flavor == FL_VARIABLE && module
77f8682b
TB
10907 && (strcmp (module, gsym->mod_name) != 0
10908 || strcmp (sym->name, gsym->sym_name) != 0))
10909 {
10910 /* This can only happen if the variable is defined in a module - if it
10911 isn't the same module, reject it. */
fea70c99 10912 gfc_error ("Variable %s from module %s with binding label %s at %L uses "
c4100eae 10913 "the same global identifier as entity at %L from module %s",
77f8682b
TB
10914 sym->name, module, sym->binding_label,
10915 &sym->declared_at, &gsym->where, gsym->mod_name);
10916 sym->binding_label = NULL;
10917 }
10918 else if ((sym->attr.function || sym->attr.subroutine)
10919 && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
10920 || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
10921 && sym != gsym->ns->proc_name
76d3d479
JW
10922 && (module != gsym->mod_name
10923 || strcmp (gsym->sym_name, sym->name) != 0
77f8682b
TB
10924 || (module && strcmp (module, gsym->mod_name) != 0)))
10925 {
76d3d479 10926 /* Print an error if the procedure is defined multiple times; we have to
77f8682b
TB
10927 exclude references to the same procedure via module association or
10928 multiple checks for the same procedure. */
fea70c99 10929 gfc_error ("Procedure %s with binding label %s at %L uses the same "
77f8682b
TB
10930 "global identifier as entity at %L", sym->name,
10931 sym->binding_label, &sym->declared_at, &gsym->where);
10932 sym->binding_label = NULL;
10933 }
a8b3b0b6
CR
10934}
10935
10936
2ed8d224
PT
10937/* Resolve an index expression. */
10938
524af0d6 10939static bool
edf1eac2 10940resolve_index_expr (gfc_expr *e)
2ed8d224 10941{
524af0d6
JB
10942 if (!gfc_resolve_expr (e))
10943 return false;
2ed8d224 10944
524af0d6
JB
10945 if (!gfc_simplify_expr (e, 0))
10946 return false;
2ed8d224 10947
524af0d6
JB
10948 if (!gfc_specification_expr (e))
10949 return false;
2ed8d224 10950
524af0d6 10951 return true;
2ed8d224
PT
10952}
10953
e69afb29 10954
110eec24
TS
10955/* Resolve a charlen structure. */
10956
524af0d6 10957static bool
110eec24
TS
10958resolve_charlen (gfc_charlen *cl)
10959{
b0c06816 10960 int i, k;
fd061185 10961 bool saved_specification_expr;
5cd09fac 10962
110eec24 10963 if (cl->resolved)
524af0d6 10964 return true;
110eec24
TS
10965
10966 cl->resolved = 1;
fd061185
TB
10967 saved_specification_expr = specification_expr;
10968 specification_expr = true;
0e9a445b 10969
239b48db 10970 if (cl->length_from_typespec)
0e9a445b 10971 {
524af0d6 10972 if (!gfc_resolve_expr (cl->length))
fd061185
TB
10973 {
10974 specification_expr = saved_specification_expr;
524af0d6 10975 return false;
fd061185 10976 }
239b48db 10977
524af0d6 10978 if (!gfc_simplify_expr (cl->length, 0))
fd061185
TB
10979 {
10980 specification_expr = saved_specification_expr;
524af0d6 10981 return false;
fd061185 10982 }
239b48db
TB
10983 }
10984 else
10985 {
239b48db 10986
524af0d6 10987 if (!resolve_index_expr (cl->length))
239b48db 10988 {
fd061185 10989 specification_expr = saved_specification_expr;
524af0d6 10990 return false;
239b48db 10991 }
0e9a445b 10992 }
110eec24 10993
98a819ea
SK
10994 /* F2008, 4.4.3.2: If the character length parameter value evaluates to
10995 a negative value, the length of character entities declared is zero. */
815cd406 10996 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
98a819ea
SK
10997 gfc_replace_expr (cl->length,
10998 gfc_get_int_expr (gfc_default_integer_kind, NULL, 0));
5cd09fac 10999
b0c06816
FXC
11000 /* Check that the character length is not too large. */
11001 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
11002 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
11003 && cl->length->ts.type == BT_INTEGER
11004 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
11005 {
11006 gfc_error ("String length at %L is too large", &cl->length->where);
fd061185 11007 specification_expr = saved_specification_expr;
524af0d6 11008 return false;
b0c06816
FXC
11009 }
11010
fd061185 11011 specification_expr = saved_specification_expr;
524af0d6 11012 return true;
2ed8d224
PT
11013}
11014
11015
66e4ab31 11016/* Test for non-constant shape arrays. */
3e1cf500
PT
11017
11018static bool
11019is_non_constant_shape_array (gfc_symbol *sym)
11020{
11021 gfc_expr *e;
11022 int i;
0e9a445b 11023 bool not_constant;
3e1cf500 11024
0e9a445b 11025 not_constant = false;
3e1cf500
PT
11026 if (sym->as != NULL)
11027 {
11028 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
11029 has not been simplified; parameter array references. Do the
11030 simplification now. */
be59db2d 11031 for (i = 0; i < sym->as->rank + sym->as->corank; i++)
3e1cf500
PT
11032 {
11033 e = sym->as->lower[i];
524af0d6 11034 if (e && (!resolve_index_expr(e)
edf1eac2 11035 || !gfc_is_constant_expr (e)))
0e9a445b 11036 not_constant = true;
3e1cf500 11037 e = sym->as->upper[i];
524af0d6 11038 if (e && (!resolve_index_expr(e)
edf1eac2 11039 || !gfc_is_constant_expr (e)))
0e9a445b 11040 not_constant = true;
3e1cf500
PT
11041 }
11042 }
0e9a445b 11043 return not_constant;
3e1cf500
PT
11044}
11045
51b09ce3
AL
11046/* Given a symbol and an initialization expression, add code to initialize
11047 the symbol to the function entry. */
6b591ec0 11048static void
51b09ce3 11049build_init_assign (gfc_symbol *sym, gfc_expr *init)
6b591ec0
PT
11050{
11051 gfc_expr *lval;
6b591ec0
PT
11052 gfc_code *init_st;
11053 gfc_namespace *ns = sym->ns;
11054
6b591ec0
PT
11055 /* Search for the function namespace if this is a contained
11056 function without an explicit result. */
11057 if (sym->attr.function && sym == sym->result
edf1eac2 11058 && sym->name != sym->ns->proc_name->name)
6b591ec0
PT
11059 {
11060 ns = ns->contained;
11061 for (;ns; ns = ns->sibling)
11062 if (strcmp (ns->proc_name->name, sym->name) == 0)
11063 break;
11064 }
11065
11066 if (ns == NULL)
11067 {
11068 gfc_free_expr (init);
11069 return;
11070 }
11071
11072 /* Build an l-value expression for the result. */
08113c73 11073 lval = gfc_lval_expr_from_sym (sym);
6b591ec0
PT
11074
11075 /* Add the code at scope entry. */
11e5274a 11076 init_st = gfc_get_code (EXEC_INIT_ASSIGN);
6b591ec0
PT
11077 init_st->next = ns->code;
11078 ns->code = init_st;
11079
11080 /* Assign the default initializer to the l-value. */
11081 init_st->loc = sym->declared_at;
a513927a 11082 init_st->expr1 = lval;
6b591ec0
PT
11083 init_st->expr2 = init;
11084}
11085
51b09ce3
AL
11086/* Assign the default initializer to a derived type variable or result. */
11087
11088static void
11089apply_default_init (gfc_symbol *sym)
11090{
11091 gfc_expr *init = NULL;
11092
11093 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11094 return;
11095
bc21d315 11096 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
51b09ce3
AL
11097 init = gfc_default_initializer (&sym->ts);
11098
50f30801 11099 if (init == NULL && sym->ts.type != BT_CLASS)
51b09ce3
AL
11100 return;
11101
11102 build_init_assign (sym, init);
86e6a239 11103 sym->attr.referenced = 1;
51b09ce3
AL
11104}
11105
11106/* Build an initializer for a local integer, real, complex, logical, or
11107 character variable, based on the command line flags finit-local-zero,
4d382327 11108 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
51b09ce3
AL
11109 null if the symbol should not have a default initialization. */
11110static gfc_expr *
11111build_default_init_expr (gfc_symbol *sym)
11112{
11113 int char_len;
11114 gfc_expr *init_expr;
11115 int i;
51b09ce3
AL
11116
11117 /* These symbols should never have a default initialization. */
a3fd80ea 11118 if (sym->attr.allocatable
51b09ce3
AL
11119 || sym->attr.external
11120 || sym->attr.dummy
11121 || sym->attr.pointer
11122 || sym->attr.in_equivalence
11123 || sym->attr.in_common
11124 || sym->attr.data
11125 || sym->module
11126 || sym->attr.cray_pointee
a67cfde8
TB
11127 || sym->attr.cray_pointer
11128 || sym->assoc)
51b09ce3
AL
11129 return NULL;
11130
11131 /* Now we'll try to build an initializer expression. */
b7e75771
JD
11132 init_expr = gfc_get_constant_expr (sym->ts.type, sym->ts.kind,
11133 &sym->declared_at);
11134
51b09ce3
AL
11135 /* We will only initialize integers, reals, complex, logicals, and
11136 characters, and only if the corresponding command-line flags
11137 were set. Otherwise, we free init_expr and return null. */
11138 switch (sym->ts.type)
4d382327 11139 {
51b09ce3
AL
11140 case BT_INTEGER:
11141 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
4d382327 11142 mpz_set_si (init_expr->value.integer,
51b09ce3
AL
11143 gfc_option.flag_init_integer_value);
11144 else
11145 {
11146 gfc_free_expr (init_expr);
11147 init_expr = NULL;
11148 }
11149 break;
11150
11151 case BT_REAL:
f19626cf 11152 switch (flag_init_real)
51b09ce3 11153 {
346a77d1
TB
11154 case GFC_INIT_REAL_SNAN:
11155 init_expr->is_snan = 1;
11156 /* Fall through. */
51b09ce3
AL
11157 case GFC_INIT_REAL_NAN:
11158 mpfr_set_nan (init_expr->value.real);
11159 break;
11160
11161 case GFC_INIT_REAL_INF:
11162 mpfr_set_inf (init_expr->value.real, 1);
11163 break;
11164
11165 case GFC_INIT_REAL_NEG_INF:
11166 mpfr_set_inf (init_expr->value.real, -1);
11167 break;
11168
11169 case GFC_INIT_REAL_ZERO:
11170 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
11171 break;
11172
11173 default:
11174 gfc_free_expr (init_expr);
11175 init_expr = NULL;
11176 break;
11177 }
11178 break;
4d382327 11179
51b09ce3 11180 case BT_COMPLEX:
f19626cf 11181 switch (flag_init_real)
51b09ce3 11182 {
346a77d1
TB
11183 case GFC_INIT_REAL_SNAN:
11184 init_expr->is_snan = 1;
11185 /* Fall through. */
51b09ce3 11186 case GFC_INIT_REAL_NAN:
eb6f9a86
KG
11187 mpfr_set_nan (mpc_realref (init_expr->value.complex));
11188 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
51b09ce3
AL
11189 break;
11190
11191 case GFC_INIT_REAL_INF:
eb6f9a86
KG
11192 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
11193 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
51b09ce3
AL
11194 break;
11195
11196 case GFC_INIT_REAL_NEG_INF:
eb6f9a86
KG
11197 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
11198 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
51b09ce3
AL
11199 break;
11200
11201 case GFC_INIT_REAL_ZERO:
eb6f9a86 11202 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
51b09ce3
AL
11203 break;
11204
11205 default:
11206 gfc_free_expr (init_expr);
11207 init_expr = NULL;
11208 break;
11209 }
11210 break;
4d382327 11211
51b09ce3
AL
11212 case BT_LOGICAL:
11213 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
11214 init_expr->value.logical = 0;
11215 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
11216 init_expr->value.logical = 1;
11217 else
11218 {
11219 gfc_free_expr (init_expr);
11220 init_expr = NULL;
11221 }
11222 break;
4d382327 11223
51b09ce3 11224 case BT_CHARACTER:
4d382327 11225 /* For characters, the length must be constant in order to
51b09ce3
AL
11226 create a default initializer. */
11227 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
bc21d315
JW
11228 && sym->ts.u.cl->length
11229 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
51b09ce3 11230 {
bc21d315 11231 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
51b09ce3 11232 init_expr->value.character.length = char_len;
00660189 11233 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
51b09ce3 11234 for (i = 0; i < char_len; i++)
00660189
FXC
11235 init_expr->value.character.string[i]
11236 = (unsigned char) gfc_option.flag_init_character_value;
51b09ce3
AL
11237 }
11238 else
11239 {
11240 gfc_free_expr (init_expr);
11241 init_expr = NULL;
11242 }
068ed5e0 11243 if (!init_expr && gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
203c7ebf 11244 && sym->ts.u.cl->length && flag_max_stack_var_size != 0)
068ed5e0
TB
11245 {
11246 gfc_actual_arglist *arg;
11247 init_expr = gfc_get_expr ();
11248 init_expr->where = sym->declared_at;
11249 init_expr->ts = sym->ts;
11250 init_expr->expr_type = EXPR_FUNCTION;
11251 init_expr->value.function.isym =
11252 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
11253 init_expr->value.function.name = "repeat";
11254 arg = gfc_get_actual_arglist ();
11255 arg->expr = gfc_get_character_expr (sym->ts.kind, &sym->declared_at,
11256 NULL, 1);
11257 arg->expr->value.character.string[0]
11258 = gfc_option.flag_init_character_value;
11259 arg->next = gfc_get_actual_arglist ();
11260 arg->next->expr = gfc_copy_expr (sym->ts.u.cl->length);
11261 init_expr->value.function.actual = arg;
11262 }
51b09ce3 11263 break;
4d382327 11264
51b09ce3
AL
11265 default:
11266 gfc_free_expr (init_expr);
11267 init_expr = NULL;
11268 }
11269 return init_expr;
11270}
11271
11272/* Add an initialization expression to a local variable. */
11273static void
11274apply_default_init_local (gfc_symbol *sym)
11275{
11276 gfc_expr *init = NULL;
11277
11278 /* The symbol should be a variable or a function return value. */
11279 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
11280 || (sym->attr.function && sym->result != sym))
11281 return;
11282
11283 /* Try to build the initializer expression. If we can't initialize
11284 this symbol, then init will be NULL. */
11285 init = build_default_init_expr (sym);
11286 if (init == NULL)
11287 return;
11288
068ed5e0
TB
11289 /* For saved variables, we don't want to add an initializer at function
11290 entry, so we just add a static initializer. Note that automatic variables
fab99ea2
TB
11291 are stack allocated even with -fno-automatic; we have also to exclude
11292 result variable, which are also nonstatic. */
4d382327 11293 if (sym->attr.save || sym->ns->save_all
203c7ebf 11294 || (flag_max_stack_var_size == 0 && !sym->attr.result
d012125d 11295 && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
068ed5e0 11296 && (!sym->attr.dimension || !is_non_constant_shape_array (sym))))
51b09ce3
AL
11297 {
11298 /* Don't clobber an existing initializer! */
11299 gcc_assert (sym->value == NULL);
11300 sym->value = init;
11301 return;
11302 }
11303
11304 build_init_assign (sym, init);
11305}
6b591ec0 11306
e69afb29 11307
66e4ab31 11308/* Resolution of common features of flavors variable and procedure. */
2ed8d224 11309
524af0d6 11310static bool
2ed8d224
PT
11311resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
11312{
fac665b2
TB
11313 gfc_array_spec *as;
11314
fac665b2
TB
11315 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
11316 as = CLASS_DATA (sym)->as;
11317 else
11318 as = sym->as;
11319
2ed8d224 11320 /* Constraints on deferred shape variable. */
fac665b2 11321 if (as == NULL || as->type != AS_DEFERRED)
2ed8d224 11322 {
fac665b2
TB
11323 bool pointer, allocatable, dimension;
11324
11325 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
2ed8d224 11326 {
fac665b2
TB
11327 pointer = CLASS_DATA (sym)->attr.class_pointer;
11328 allocatable = CLASS_DATA (sym)->attr.allocatable;
11329 dimension = CLASS_DATA (sym)->attr.dimension;
11330 }
11331 else
11332 {
4cc70466 11333 pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
fac665b2
TB
11334 allocatable = sym->attr.allocatable;
11335 dimension = sym->attr.dimension;
11336 }
11337
11338 if (allocatable)
11339 {
c62c6622 11340 if (dimension && as->type != AS_ASSUMED_RANK)
2fbd4117 11341 {
a4d9b221 11342 gfc_error ("Allocatable array %qs at %L must have a deferred "
c62c6622 11343 "shape or assumed rank", sym->name, &sym->declared_at);
524af0d6 11344 return false;
2fbd4117 11345 }
524af0d6 11346 else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
a4d9b221 11347 "%qs at %L may not be ALLOCATABLE",
524af0d6
JB
11348 sym->name, &sym->declared_at))
11349 return false;
2ed8d224
PT
11350 }
11351
c62c6622 11352 if (pointer && dimension && as->type != AS_ASSUMED_RANK)
2ed8d224 11353 {
a4d9b221 11354 gfc_error ("Array pointer %qs at %L must have a deferred shape or "
c62c6622 11355 "assumed rank", sym->name, &sym->declared_at);
524af0d6 11356 return false;
2ed8d224 11357 }
2ed8d224
PT
11358 }
11359 else
11360 {
cf2b3c22 11361 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
12578be7 11362 && sym->ts.type != BT_CLASS && !sym->assoc)
2ed8d224 11363 {
a4d9b221 11364 gfc_error ("Array %qs at %L cannot have a deferred shape",
2ed8d224 11365 sym->name, &sym->declared_at);
524af0d6 11366 return false;
2ed8d224
PT
11367 }
11368 }
233961db
JW
11369
11370 /* Constraints on polymorphic variables. */
11371 if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
11372 {
11373 /* F03:C502. */
d40477b4 11374 if (sym->attr.class_ok
8b704316 11375 && !sym->attr.select_type_temporary
524af0d6 11376 && !UNLIMITED_POLY (sym)
d40477b4 11377 && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
233961db 11378 {
a4d9b221 11379 gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
7a08eda1
JW
11380 CLASS_DATA (sym)->ts.u.derived->name, sym->name,
11381 &sym->declared_at);
524af0d6 11382 return false;
233961db
JW
11383 }
11384
11385 /* F03:C509. */
3e78238a
DK
11386 /* Assume that use associated symbols were checked in the module ns.
11387 Class-variables that are associate-names are also something special
11388 and excepted from the test. */
11389 if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc)
233961db 11390 {
a4d9b221 11391 gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
233961db 11392 "or pointer", sym->name, &sym->declared_at);
524af0d6 11393 return false;
233961db
JW
11394 }
11395 }
4d382327 11396
524af0d6 11397 return true;
2ed8d224
PT
11398}
11399
edf1eac2 11400
448d2cd2
TS
11401/* Additional checks for symbols with flavor variable and derived
11402 type. To be called from resolve_fl_variable. */
11403
524af0d6 11404static bool
9de88093 11405resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
448d2cd2 11406{
cf2b3c22 11407 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
448d2cd2
TS
11408
11409 /* Check to see if a derived type is blocked from being host
11410 associated by the presence of another class I symbol in the same
11411 namespace. 14.6.1.3 of the standard and the discussion on
11412 comp.lang.fortran. */
bc21d315 11413 if (sym->ns != sym->ts.u.derived->ns
448d2cd2
TS
11414 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
11415 {
11416 gfc_symbol *s;
bc21d315 11417 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
c3f34952
TB
11418 if (s && s->attr.generic)
11419 s = gfc_find_dt_in_generic (s);
334e912a 11420 if (s && s->attr.flavor != FL_DERIVED)
448d2cd2 11421 {
fea70c99 11422 gfc_error ("The type %qs cannot be host associated at %L "
448d2cd2
TS
11423 "because it is blocked by an incompatible object "
11424 "of the same name declared at %L",
bc21d315 11425 sym->ts.u.derived->name, &sym->declared_at,
448d2cd2 11426 &s->declared_at);
524af0d6 11427 return false;
448d2cd2
TS
11428 }
11429 }
11430
11431 /* 4th constraint in section 11.3: "If an object of a type for which
11432 component-initialization is specified (R429) appears in the
11433 specification-part of a module and does not have the ALLOCATABLE
11434 or POINTER attribute, the object shall have the SAVE attribute."
11435
11436 The check for initializers is performed with
16e520b6 11437 gfc_has_default_initializer because gfc_default_initializer generates
448d2cd2 11438 a hidden default for allocatable components. */
9de88093 11439 if (!(sym->value || no_init_flag) && sym->ns->proc_name
448d2cd2
TS
11440 && sym->ns->proc_name->attr.flavor == FL_MODULE
11441 && !sym->ns->save_all && !sym->attr.save
11442 && !sym->attr.pointer && !sym->attr.allocatable
16e520b6 11443 && gfc_has_default_initializer (sym->ts.u.derived)
524af0d6 11444 && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
a4d9b221 11445 "%qs at %L, needed due to the default "
524af0d6
JB
11446 "initialization", sym->name, &sym->declared_at))
11447 return false;
448d2cd2
TS
11448
11449 /* Assign default initializer. */
11450 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9de88093 11451 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
448d2cd2
TS
11452 {
11453 sym->value = gfc_default_initializer (&sym->ts);
11454 }
11455
524af0d6 11456 return true;
448d2cd2
TS
11457}
11458
11459
2ed8d224
PT
11460/* Resolve symbols with flavor variable. */
11461
524af0d6 11462static bool
2ed8d224
PT
11463resolve_fl_variable (gfc_symbol *sym, int mp_flag)
11464{
9de88093 11465 int no_init_flag, automatic_flag;
2ed8d224 11466 gfc_expr *e;
edf1eac2 11467 const char *auto_save_msg;
fd061185 11468 bool saved_specification_expr;
0e9a445b 11469
a4d9b221 11470 auto_save_msg = "Automatic object %qs at %L cannot have the "
0e9a445b 11471 "SAVE attribute";
2ed8d224 11472
524af0d6
JB
11473 if (!resolve_fl_var_and_proc (sym, mp_flag))
11474 return false;
110eec24 11475
0e9a445b
PT
11476 /* Set this flag to check that variables are parameters of all entries.
11477 This check is effected by the call to gfc_resolve_expr through
11478 is_non_constant_shape_array. */
fd061185
TB
11479 saved_specification_expr = specification_expr;
11480 specification_expr = true;
0e9a445b 11481
c4d4556f
TS
11482 if (sym->ns->proc_name
11483 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11484 || sym->ns->proc_name->attr.is_main_program)
11485 && !sym->attr.use_assoc
edf1eac2
SK
11486 && !sym->attr.allocatable
11487 && !sym->attr.pointer
11488 && is_non_constant_shape_array (sym))
2ed8d224 11489 {
c4d4556f
TS
11490 /* The shape of a main program or module array needs to be
11491 constant. */
fea70c99 11492 gfc_error ("The module or main program array %qs at %L must "
c4d4556f 11493 "have constant shape", sym->name, &sym->declared_at);
fd061185 11494 specification_expr = saved_specification_expr;
524af0d6 11495 return false;
2ed8d224
PT
11496 }
11497
e69afb29 11498 /* Constraints on deferred type parameter. */
5f23671d
JJ
11499 if (sym->ts.deferred
11500 && !(sym->attr.pointer
11501 || sym->attr.allocatable
11502 || sym->attr.omp_udr_artificial_var))
e69afb29 11503 {
a4d9b221 11504 gfc_error ("Entity %qs at %L has a deferred type parameter and "
e69afb29
SK
11505 "requires either the pointer or allocatable attribute",
11506 sym->name, &sym->declared_at);
fd061185 11507 specification_expr = saved_specification_expr;
524af0d6 11508 return false;
e69afb29
SK
11509 }
11510
2ed8d224
PT
11511 if (sym->ts.type == BT_CHARACTER)
11512 {
11513 /* Make sure that character string variables with assumed length are
11514 dummy arguments. */
bc21d315 11515 e = sym->ts.u.cl->length;
e69afb29 11516 if (e == NULL && !sym->attr.dummy && !sym->attr.result
5f23671d
JJ
11517 && !sym->ts.deferred && !sym->attr.select_type_temporary
11518 && !sym->attr.omp_udr_artificial_var)
2ed8d224
PT
11519 {
11520 gfc_error ("Entity with assumed character length at %L must be a "
11521 "dummy argument or a PARAMETER", &sym->declared_at);
fd061185 11522 specification_expr = saved_specification_expr;
524af0d6 11523 return false;
2ed8d224
PT
11524 }
11525
80f95228 11526 if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
0e9a445b
PT
11527 {
11528 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
fd061185 11529 specification_expr = saved_specification_expr;
524af0d6 11530 return false;
0e9a445b
PT
11531 }
11532
2ed8d224 11533 if (!gfc_is_constant_expr (e)
edf1eac2 11534 && !(e->expr_type == EXPR_VARIABLE
30228b61
JW
11535 && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
11536 {
11537 if (!sym->attr.use_assoc && sym->ns->proc_name
11538 && (sym->ns->proc_name->attr.flavor == FL_MODULE
11539 || sym->ns->proc_name->attr.is_main_program))
11540 {
fea70c99 11541 gfc_error ("%qs at %L must have constant character length "
30228b61 11542 "in this context", sym->name, &sym->declared_at);
fd061185 11543 specification_expr = saved_specification_expr;
524af0d6 11544 return false;
30228b61
JW
11545 }
11546 if (sym->attr.in_common)
11547 {
a4d9b221 11548 gfc_error ("COMMON variable %qs at %L must have constant "
30228b61 11549 "character length", sym->name, &sym->declared_at);
fd061185 11550 specification_expr = saved_specification_expr;
524af0d6 11551 return false;
30228b61 11552 }
2ed8d224
PT
11553 }
11554 }
11555
51b09ce3
AL
11556 if (sym->value == NULL && sym->attr.referenced)
11557 apply_default_init_local (sym); /* Try to apply a default initialization. */
11558
9de88093
TS
11559 /* Determine if the symbol may not have an initializer. */
11560 no_init_flag = automatic_flag = 0;
2ed8d224 11561 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9de88093
TS
11562 || sym->attr.intrinsic || sym->attr.result)
11563 no_init_flag = 1;
be59db2d 11564 else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
9de88093 11565 && is_non_constant_shape_array (sym))
2ed8d224 11566 {
9de88093 11567 no_init_flag = automatic_flag = 1;
0e9a445b 11568
5349080d
TB
11569 /* Also, they must not have the SAVE attribute.
11570 SAVE_IMPLICIT is checked below. */
9f3761c5
TB
11571 if (sym->as && sym->attr.codimension)
11572 {
11573 int corank = sym->as->corank;
11574 sym->as->corank = 0;
11575 no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
11576 sym->as->corank = corank;
11577 }
11578 if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
0e9a445b
PT
11579 {
11580 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
fd061185 11581 specification_expr = saved_specification_expr;
524af0d6 11582 return false;
0e9a445b 11583 }
448d2cd2 11584 }
2ed8d224 11585
7a99defe
SK
11586 /* Ensure that any initializer is simplified. */
11587 if (sym->value)
11588 gfc_simplify_expr (sym->value, 1);
11589
2ed8d224 11590 /* Reject illegal initializers. */
9de88093 11591 if (!sym->mark && sym->value)
2ed8d224 11592 {
da285ce8
JW
11593 if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
11594 && CLASS_DATA (sym)->attr.allocatable))
a4d9b221 11595 gfc_error ("Allocatable %qs at %L cannot have an initializer",
2ed8d224
PT
11596 sym->name, &sym->declared_at);
11597 else if (sym->attr.external)
a4d9b221 11598 gfc_error ("External %qs at %L cannot have an initializer",
2ed8d224 11599 sym->name, &sym->declared_at);
145bdc2c
PT
11600 else if (sym->attr.dummy
11601 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
a4d9b221 11602 gfc_error ("Dummy %qs at %L cannot have an initializer",
2ed8d224
PT
11603 sym->name, &sym->declared_at);
11604 else if (sym->attr.intrinsic)
a4d9b221 11605 gfc_error ("Intrinsic %qs at %L cannot have an initializer",
2ed8d224
PT
11606 sym->name, &sym->declared_at);
11607 else if (sym->attr.result)
a4d9b221 11608 gfc_error ("Function result %qs at %L cannot have an initializer",
2ed8d224 11609 sym->name, &sym->declared_at);
9de88093 11610 else if (automatic_flag)
a4d9b221 11611 gfc_error ("Automatic array %qs at %L cannot have an initializer",
2ed8d224 11612 sym->name, &sym->declared_at);
145bdc2c
PT
11613 else
11614 goto no_init_error;
fd061185 11615 specification_expr = saved_specification_expr;
524af0d6 11616 return false;
2ed8d224
PT
11617 }
11618
145bdc2c 11619no_init_error:
cf2b3c22 11620 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
fd061185 11621 {
524af0d6 11622 bool res = resolve_fl_variable_derived (sym, no_init_flag);
fd061185
TB
11623 specification_expr = saved_specification_expr;
11624 return res;
11625 }
2ed8d224 11626
fd061185 11627 specification_expr = saved_specification_expr;
524af0d6 11628 return true;
2ed8d224
PT
11629}
11630
11631
4668d6f9
PT
11632/* Compare the dummy characteristics of a module procedure interface
11633 declaration with the corresponding declaration in a submodule. */
11634static gfc_formal_arglist *new_formal;
11635static char errmsg[200];
11636
11637static void
11638compare_fsyms (gfc_symbol *sym)
11639{
11640 gfc_symbol *fsym;
11641
11642 if (sym == NULL || new_formal == NULL)
11643 return;
11644
11645 fsym = new_formal->sym;
11646
11647 if (sym == fsym)
11648 return;
11649
11650 if (strcmp (sym->name, fsym->name) == 0)
11651 {
11652 if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
11653 gfc_error ("%s at %L", errmsg, &fsym->declared_at);
11654 }
11655}
11656
11657
2ed8d224
PT
11658/* Resolve a procedure. */
11659
524af0d6 11660static bool
2ed8d224
PT
11661resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
11662{
11663 gfc_formal_arglist *arg;
11664
11665 if (sym->attr.function
524af0d6
JB
11666 && !resolve_fl_var_and_proc (sym, mp_flag))
11667 return false;
110eec24 11668
92c59193 11669 if (sym->ts.type == BT_CHARACTER)
2ed8d224 11670 {
bc21d315 11671 gfc_charlen *cl = sym->ts.u.cl;
8111a921
PT
11672
11673 if (cl && cl->length && gfc_is_constant_expr (cl->length)
524af0d6
JB
11674 && !resolve_charlen (cl))
11675 return false;
8111a921 11676
d94be5e0
TB
11677 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
11678 && sym->attr.proc == PROC_ST_FUNCTION)
92c59193 11679 {
a4d9b221 11680 gfc_error ("Character-valued statement function %qs at %L must "
d94be5e0 11681 "have constant length", sym->name, &sym->declared_at);
524af0d6 11682 return false;
edf1eac2 11683 }
2ed8d224
PT
11684 }
11685
37e47ee9 11686 /* Ensure that derived type for are not of a private type. Internal
df2fba9e 11687 module procedures are excluded by 2.2.3.3 - i.e., they are not
b82feea5 11688 externally accessible and can access all the objects accessible in
66e4ab31 11689 the host. */
37e47ee9 11690 if (!(sym->ns->parent
edf1eac2 11691 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
6e2062b0 11692 && gfc_check_symbol_access (sym))
2ed8d224 11693 {
83b2e4e8
DF
11694 gfc_interface *iface;
11695
4cbc9039 11696 for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
2ed8d224
PT
11697 {
11698 if (arg->sym
edf1eac2 11699 && arg->sym->ts.type == BT_DERIVED
bc21d315 11700 && !arg->sym->ts.u.derived->attr.use_assoc
6e2062b0 11701 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
a4d9b221 11702 && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
524af0d6 11703 "and cannot be a dummy argument"
a4d9b221 11704 " of %qs, which is PUBLIC at %L",
22c23886 11705 arg->sym->name, sym->name,
524af0d6 11706 &sym->declared_at))
2ed8d224 11707 {
2ed8d224 11708 /* Stop this message from recurring. */
bc21d315 11709 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
524af0d6 11710 return false;
2ed8d224
PT
11711 }
11712 }
83b2e4e8 11713
3bed9dd0
DF
11714 /* PUBLIC interfaces may expose PRIVATE procedures that take types
11715 PRIVATE to the containing module. */
11716 for (iface = sym->generic; iface; iface = iface->next)
11717 {
4cbc9039 11718 for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
3bed9dd0
DF
11719 {
11720 if (arg->sym
11721 && arg->sym->ts.type == BT_DERIVED
bc21d315 11722 && !arg->sym->ts.u.derived->attr.use_assoc
6e2062b0 11723 && !gfc_check_symbol_access (arg->sym->ts.u.derived)
a4d9b221
TB
11724 && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
11725 "PUBLIC interface %qs at %L "
11726 "takes dummy arguments of %qs which "
22c23886
PT
11727 "is PRIVATE", iface->sym->name,
11728 sym->name, &iface->sym->declared_at,
524af0d6 11729 gfc_typename(&arg->sym->ts)))
3bed9dd0 11730 {
3bed9dd0 11731 /* Stop this message from recurring. */
bc21d315 11732 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
524af0d6 11733 return false;
3bed9dd0
DF
11734 }
11735 }
11736 }
2ed8d224
PT
11737 }
11738
8fb74da4
JW
11739 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
11740 && !sym->attr.proc_pointer)
f8faa85e 11741 {
a4d9b221 11742 gfc_error ("Function %qs at %L cannot have an initializer",
f8faa85e 11743 sym->name, &sym->declared_at);
524af0d6 11744 return false;
f8faa85e
DF
11745 }
11746
e2ae1407 11747 /* An external symbol may not have an initializer because it is taken to be
8fb74da4
JW
11748 a procedure. Exception: Procedure Pointers. */
11749 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
2ed8d224 11750 {
a4d9b221 11751 gfc_error ("External object %qs at %L may not have an initializer",
2ed8d224 11752 sym->name, &sym->declared_at);
524af0d6 11753 return false;
2ed8d224
PT
11754 }
11755
d68bd5a8
PT
11756 /* An elemental function is required to return a scalar 12.7.1 */
11757 if (sym->attr.elemental && sym->attr.function && sym->as)
11758 {
a4d9b221 11759 gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
d68bd5a8
PT
11760 "result", sym->name, &sym->declared_at);
11761 /* Reset so that the error only occurs once. */
11762 sym->attr.elemental = 0;
524af0d6 11763 return false;
d68bd5a8
PT
11764 }
11765
1ca99f75
TB
11766 if (sym->attr.proc == PROC_ST_FUNCTION
11767 && (sym->attr.allocatable || sym->attr.pointer))
11768 {
a4d9b221 11769 gfc_error ("Statement function %qs at %L may not have pointer or "
1ca99f75 11770 "allocatable attribute", sym->name, &sym->declared_at);
524af0d6 11771 return false;
1ca99f75
TB
11772 }
11773
2ed8d224
PT
11774 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
11775 char-len-param shall not be array-valued, pointer-valued, recursive
11776 or pure. ....snip... A character value of * may only be used in the
11777 following ways: (i) Dummy arg of procedure - dummy associates with
11778 actual length; (ii) To declare a named constant; or (iii) External
11779 function - but length must be declared in calling scoping unit. */
11780 if (sym->attr.function
dd912331 11781 && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
bc21d315 11782 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
2ed8d224
PT
11783 {
11784 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
edf1eac2 11785 || (sym->attr.recursive) || (sym->attr.pure))
2ed8d224
PT
11786 {
11787 if (sym->as && sym->as->rank)
a4d9b221 11788 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
2ed8d224
PT
11789 "array-valued", sym->name, &sym->declared_at);
11790
11791 if (sym->attr.pointer)
a4d9b221 11792 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
2ed8d224
PT
11793 "pointer-valued", sym->name, &sym->declared_at);
11794
11795 if (sym->attr.pure)
a4d9b221 11796 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
2ed8d224
PT
11797 "pure", sym->name, &sym->declared_at);
11798
11799 if (sym->attr.recursive)
a4d9b221 11800 gfc_error ("CHARACTER(*) function %qs at %L cannot be "
2ed8d224
PT
11801 "recursive", sym->name, &sym->declared_at);
11802
524af0d6 11803 return false;
2ed8d224
PT
11804 }
11805
11806 /* Appendix B.2 of the standard. Contained functions give an
63a496de
FXC
11807 error anyway. Deferred character length is an F2003 feature.
11808 Don't warn on intrinsic conversion functions, which start
11809 with two underscores. */
11810 if (!sym->attr.contained && !sym->ts.deferred
11811 && (sym->name[0] != '_' || sym->name[1] != '_'))
9717f7a1 11812 gfc_notify_std (GFC_STD_F95_OBS,
a4d9b221 11813 "CHARACTER(*) function %qs at %L",
2ed8d224
PT
11814 sym->name, &sym->declared_at);
11815 }
a8b3b0b6 11816
019c0e5d
TB
11817 /* F2008, C1218. */
11818 if (sym->attr.elemental)
11819 {
11820 if (sym->attr.proc_pointer)
11821 {
a4d9b221 11822 gfc_error ("Procedure pointer %qs at %L shall not be elemental",
019c0e5d
TB
11823 sym->name, &sym->declared_at);
11824 return false;
11825 }
11826 if (sym->attr.dummy)
11827 {
a4d9b221 11828 gfc_error ("Dummy procedure %qs at %L shall not be elemental",
019c0e5d
TB
11829 sym->name, &sym->declared_at);
11830 return false;
11831 }
11832 }
11833
a8b3b0b6
CR
11834 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
11835 {
11836 gfc_formal_arglist *curr_arg;
aa5e22f0 11837 int has_non_interop_arg = 0;
a8b3b0b6 11838
22c23886 11839 if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
524af0d6 11840 sym->common_block))
a8b3b0b6
CR
11841 {
11842 /* Clear these to prevent looking at them again if there was an
11843 error. */
11844 sym->attr.is_bind_c = 0;
11845 sym->attr.is_c_interop = 0;
11846 sym->ts.is_c_interop = 0;
11847 }
11848 else
11849 {
11850 /* So far, no errors have been found. */
11851 sym->attr.is_c_interop = 1;
11852 sym->ts.is_c_interop = 1;
11853 }
4d382327 11854
4cbc9039 11855 curr_arg = gfc_sym_get_dummy_args (sym);
a8b3b0b6
CR
11856 while (curr_arg != NULL)
11857 {
11858 /* Skip implicitly typed dummy args here. */
aa5e22f0 11859 if (curr_arg->sym->attr.implicit_type == 0)
524af0d6 11860 if (!gfc_verify_c_interop_param (curr_arg->sym))
aa5e22f0
CR
11861 /* If something is found to fail, record the fact so we
11862 can mark the symbol for the procedure as not being
11863 BIND(C) to try and prevent multiple errors being
11864 reported. */
11865 has_non_interop_arg = 1;
4d382327 11866
a8b3b0b6
CR
11867 curr_arg = curr_arg->next;
11868 }
aa5e22f0
CR
11869
11870 /* See if any of the arguments were not interoperable and if so, clear
11871 the procedure symbol to prevent duplicate error messages. */
11872 if (has_non_interop_arg != 0)
11873 {
11874 sym->attr.is_c_interop = 0;
11875 sym->ts.is_c_interop = 0;
11876 sym->attr.is_bind_c = 0;
11877 }
a8b3b0b6 11878 }
4d382327 11879
3070bab4 11880 if (!sym->attr.proc_pointer)
beb4bd6c 11881 {
3070bab4
JW
11882 if (sym->attr.save == SAVE_EXPLICIT)
11883 {
11884 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
a4d9b221 11885 "in %qs at %L", sym->name, &sym->declared_at);
524af0d6 11886 return false;
3070bab4
JW
11887 }
11888 if (sym->attr.intent)
11889 {
11890 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
a4d9b221 11891 "in %qs at %L", sym->name, &sym->declared_at);
524af0d6 11892 return false;
3070bab4
JW
11893 }
11894 if (sym->attr.subroutine && sym->attr.result)
11895 {
11896 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
a4d9b221 11897 "in %qs at %L", sym->name, &sym->declared_at);
524af0d6 11898 return false;
3070bab4
JW
11899 }
11900 if (sym->attr.external && sym->attr.function
11901 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
11902 || sym->attr.contained))
11903 {
11904 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
a4d9b221 11905 "in %qs at %L", sym->name, &sym->declared_at);
524af0d6 11906 return false;
3070bab4
JW
11907 }
11908 if (strcmp ("ppr@", sym->name) == 0)
11909 {
a4d9b221 11910 gfc_error ("Procedure pointer result %qs at %L "
3070bab4
JW
11911 "is missing the pointer attribute",
11912 sym->ns->proc_name->name, &sym->declared_at);
524af0d6 11913 return false;
3070bab4 11914 }
beb4bd6c
JW
11915 }
11916
30c931de
PT
11917 /* Assume that a procedure whose body is not known has references
11918 to external arrays. */
11919 if (sym->attr.if_source != IFSRC_DECL)
11920 sym->attr.array_outer_dependency = 1;
11921
4668d6f9
PT
11922 /* Compare the characteristics of a module procedure with the
11923 interface declaration. Ideally this would be done with
11924 gfc_compare_interfaces but, at present, the formal interface
11925 cannot be copied to the ts.interface. */
11926 if (sym->attr.module_procedure
11927 && sym->attr.if_source == IFSRC_DECL)
11928 {
11929 gfc_symbol *iface;
4f283c42
PT
11930 char name[2*GFC_MAX_SYMBOL_LEN + 1];
11931 char *module_name;
11932 char *submodule_name;
11933 strcpy (name, sym->ns->proc_name->name);
11934 module_name = strtok (name, ".");
11935 submodule_name = strtok (NULL, ".");
4668d6f9
PT
11936
11937 /* Stop the dummy characteristics test from using the interface
11938 symbol instead of 'sym'. */
11939 iface = sym->ts.interface;
11940 sym->ts.interface = NULL;
11941
11942 if (iface == NULL)
11943 goto check_formal;
11944
11945 /* Check the procedure characteristics. */
11946 if (sym->attr.pure != iface->attr.pure)
11947 {
11948 gfc_error ("Mismatch in PURE attribute between MODULE "
11949 "PROCEDURE at %L and its interface in %s",
4f283c42 11950 &sym->declared_at, module_name);
4668d6f9
PT
11951 return false;
11952 }
11953
11954 if (sym->attr.elemental != iface->attr.elemental)
11955 {
11956 gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
11957 "PROCEDURE at %L and its interface in %s",
4f283c42 11958 &sym->declared_at, module_name);
4668d6f9
PT
11959 return false;
11960 }
11961
11962 if (sym->attr.recursive != iface->attr.recursive)
11963 {
11964 gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
11965 "PROCEDURE at %L and its interface in %s",
4f283c42 11966 &sym->declared_at, module_name);
4668d6f9
PT
11967 return false;
11968 }
11969
11970 /* Check the result characteristics. */
11971 if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
11972 {
11973 gfc_error ("%s between the MODULE PROCEDURE declaration "
11974 "in module %s and the declaration at %L in "
4f283c42
PT
11975 "SUBMODULE %s", errmsg, module_name,
11976 &sym->declared_at, submodule_name);
4668d6f9
PT
11977 return false;
11978 }
11979
11980check_formal:
11981 /* Check the charcateristics of the formal arguments. */
11982 if (sym->formal && sym->formal_ns)
11983 {
11984 for (arg = sym->formal; arg && arg->sym; arg = arg->next)
11985 {
11986 new_formal = arg;
11987 gfc_traverse_ns (sym->formal_ns, compare_fsyms);
11988 }
11989 }
11990
11991 sym->ts.interface = iface;
11992 }
524af0d6 11993 return true;
110eec24
TS
11994}
11995
11996
34523524
DK
11997/* Resolve a list of finalizer procedures. That is, after they have hopefully
11998 been defined and we now know their defined arguments, check that they fulfill
11999 the requirements of the standard for procedures used as finalizers. */
12000
524af0d6 12001static bool
cb414900 12002gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
34523524
DK
12003{
12004 gfc_finalizer* list;
12005 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
524af0d6 12006 bool result = true;
34523524 12007 bool seen_scalar = false;
cb414900
TB
12008 gfc_symbol *vtab;
12009 gfc_component *c;
19fe9658
TB
12010 gfc_symbol *parent = gfc_get_derived_super_type (derived);
12011
12012 if (parent)
12013 gfc_resolve_finalizers (parent, finalizable);
34523524 12014
cb414900
TB
12015 /* Return early when not finalizable. Additionally, ensure that derived-type
12016 components have a their finalizables resolved. */
34523524 12017 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
cb414900
TB
12018 {
12019 bool has_final = false;
12020 for (c = derived->components; c; c = c->next)
12021 if (c->ts.type == BT_DERIVED
12022 && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
12023 {
12024 bool has_final2 = false;
12025 if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final))
12026 return false; /* Error. */
12027 has_final = has_final || has_final2;
12028 }
12029 if (!has_final)
12030 {
12031 if (finalizable)
12032 *finalizable = false;
12033 return true;
12034 }
12035 }
34523524
DK
12036
12037 /* Walk over the list of finalizer-procedures, check them, and if any one
12038 does not fit in with the standard's definition, print an error and remove
12039 it from the list. */
12040 prev_link = &derived->f2k_derived->finalizers;
12041 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
12042 {
4cbc9039 12043 gfc_formal_arglist *dummy_args;
34523524
DK
12044 gfc_symbol* arg;
12045 gfc_finalizer* i;
12046 int my_rank;
12047
f6fad28e
DK
12048 /* Skip this finalizer if we already resolved it. */
12049 if (list->proc_tree)
12050 {
12051 prev_link = &(list->next);
12052 continue;
12053 }
12054
34523524 12055 /* Check this exists and is a SUBROUTINE. */
f6fad28e 12056 if (!list->proc_sym->attr.subroutine)
34523524 12057 {
a4d9b221 12058 gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
f6fad28e 12059 list->proc_sym->name, &list->where);
34523524
DK
12060 goto error;
12061 }
12062
12063 /* We should have exactly one argument. */
4cbc9039
JW
12064 dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
12065 if (!dummy_args || dummy_args->next)
34523524
DK
12066 {
12067 gfc_error ("FINAL procedure at %L must have exactly one argument",
12068 &list->where);
12069 goto error;
12070 }
4cbc9039 12071 arg = dummy_args->sym;
34523524
DK
12072
12073 /* This argument must be of our type. */
bc21d315 12074 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
34523524 12075 {
a4d9b221 12076 gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
34523524
DK
12077 &arg->declared_at, derived->name);
12078 goto error;
12079 }
12080
12081 /* It must neither be a pointer nor allocatable nor optional. */
12082 if (arg->attr.pointer)
12083 {
12084 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
12085 &arg->declared_at);
12086 goto error;
12087 }
12088 if (arg->attr.allocatable)
12089 {
12090 gfc_error ("Argument of FINAL procedure at %L must not be"
12091 " ALLOCATABLE", &arg->declared_at);
12092 goto error;
12093 }
12094 if (arg->attr.optional)
12095 {
12096 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
12097 &arg->declared_at);
12098 goto error;
12099 }
12100
12101 /* It must not be INTENT(OUT). */
12102 if (arg->attr.intent == INTENT_OUT)
12103 {
12104 gfc_error ("Argument of FINAL procedure at %L must not be"
12105 " INTENT(OUT)", &arg->declared_at);
12106 goto error;
12107 }
12108
12109 /* Warn if the procedure is non-scalar and not assumed shape. */
73e42eef 12110 if (warn_surprising && arg->as && arg->as->rank != 0
34523524 12111 && arg->as->type != AS_ASSUMED_SHAPE)
48749dbc
MLI
12112 gfc_warning (OPT_Wsurprising,
12113 "Non-scalar FINAL procedure at %L should have assumed"
34523524
DK
12114 " shape argument", &arg->declared_at);
12115
12116 /* Check that it does not match in kind and rank with a FINAL procedure
12117 defined earlier. To really loop over the *earlier* declarations,
12118 we need to walk the tail of the list as new ones were pushed at the
12119 front. */
12120 /* TODO: Handle kind parameters once they are implemented. */
12121 my_rank = (arg->as ? arg->as->rank : 0);
12122 for (i = list->next; i; i = i->next)
12123 {
4cbc9039
JW
12124 gfc_formal_arglist *dummy_args;
12125
34523524
DK
12126 /* Argument list might be empty; that is an error signalled earlier,
12127 but we nevertheless continued resolving. */
4cbc9039
JW
12128 dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
12129 if (dummy_args)
34523524 12130 {
4cbc9039 12131 gfc_symbol* i_arg = dummy_args->sym;
34523524
DK
12132 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
12133 if (i_rank == my_rank)
12134 {
a4d9b221
TB
12135 gfc_error ("FINAL procedure %qs declared at %L has the same"
12136 " rank (%d) as %qs",
4d382327 12137 list->proc_sym->name, &list->where, my_rank,
f6fad28e 12138 i->proc_sym->name);
34523524
DK
12139 goto error;
12140 }
12141 }
12142 }
12143
12144 /* Is this the/a scalar finalizer procedure? */
12145 if (!arg->as || arg->as->rank == 0)
12146 seen_scalar = true;
12147
f6fad28e
DK
12148 /* Find the symtree for this procedure. */
12149 gcc_assert (!list->proc_tree);
12150 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
12151
34523524
DK
12152 prev_link = &list->next;
12153 continue;
12154
df2fba9e 12155 /* Remove wrong nodes immediately from the list so we don't risk any
34523524
DK
12156 troubles in the future when they might fail later expectations. */
12157error:
34523524
DK
12158 i = list;
12159 *prev_link = list->next;
12160 gfc_free_finalizer (i);
cb414900 12161 result = false;
34523524
DK
12162 }
12163
cb414900
TB
12164 if (result == false)
12165 return false;
12166
34523524
DK
12167 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
12168 were nodes in the list, must have been for arrays. It is surely a good
12169 idea to have a scalar version there if there's something to finalize. */
73e42eef 12170 if (warn_surprising && result && !seen_scalar)
48749dbc
MLI
12171 gfc_warning (OPT_Wsurprising,
12172 "Only array FINAL procedures declared for derived type %qs"
34523524
DK
12173 " defined at %L, suggest also scalar one",
12174 derived->name, &derived->declared_at);
12175
cb414900
TB
12176 vtab = gfc_find_derived_vtab (derived);
12177 c = vtab->ts.u.derived->components->next->next->next->next->next;
12178 gfc_set_sym_referenced (c->initializer->symtree->n.sym);
12179
12180 if (finalizable)
12181 *finalizable = true;
12182
12183 return true;
34523524
DK
12184}
12185
12186
e157f736
DK
12187/* Check if two GENERIC targets are ambiguous and emit an error is they are. */
12188
524af0d6 12189static bool
e157f736
DK
12190check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
12191 const char* generic_name, locus where)
12192{
6f3ab30d
JW
12193 gfc_symbol *sym1, *sym2;
12194 const char *pass1, *pass2;
2a144f64 12195 gfc_formal_arglist *dummy_args;
e157f736
DK
12196
12197 gcc_assert (t1->specific && t2->specific);
12198 gcc_assert (!t1->specific->is_generic);
12199 gcc_assert (!t2->specific->is_generic);
218e1228 12200 gcc_assert (t1->is_operator == t2->is_operator);
e157f736
DK
12201
12202 sym1 = t1->specific->u.specific->n.sym;
12203 sym2 = t2->specific->u.specific->n.sym;
12204
cf2b3c22 12205 if (sym1 == sym2)
524af0d6 12206 return true;
cf2b3c22 12207
e157f736
DK
12208 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
12209 if (sym1->attr.subroutine != sym2->attr.subroutine
12210 || sym1->attr.function != sym2->attr.function)
12211 {
a4d9b221
TB
12212 gfc_error ("%qs and %qs can't be mixed FUNCTION/SUBROUTINE for"
12213 " GENERIC %qs at %L",
e157f736 12214 sym1->name, sym2->name, generic_name, &where);
524af0d6 12215 return false;
e157f736
DK
12216 }
12217
2a144f64 12218 /* Determine PASS arguments. */
6f3ab30d
JW
12219 if (t1->specific->nopass)
12220 pass1 = NULL;
12221 else if (t1->specific->pass_arg)
12222 pass1 = t1->specific->pass_arg;
12223 else
2a144f64
JW
12224 {
12225 dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
12226 if (dummy_args)
12227 pass1 = dummy_args->sym->name;
12228 else
12229 pass1 = NULL;
12230 }
6f3ab30d
JW
12231 if (t2->specific->nopass)
12232 pass2 = NULL;
12233 else if (t2->specific->pass_arg)
12234 pass2 = t2->specific->pass_arg;
12235 else
2a144f64
JW
12236 {
12237 dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
12238 if (dummy_args)
12239 pass2 = dummy_args->sym->name;
12240 else
12241 pass2 = NULL;
12242 }
12243
12244 /* Compare the interfaces. */
218e1228 12245 if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
6f3ab30d 12246 NULL, 0, pass1, pass2))
e157f736 12247 {
a4d9b221 12248 gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
e157f736 12249 sym1->name, sym2->name, generic_name, &where);
524af0d6 12250 return false;
e157f736
DK
12251 }
12252
524af0d6 12253 return true;
e157f736
DK
12254}
12255
12256
94747289
DK
12257/* Worker function for resolving a generic procedure binding; this is used to
12258 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
12259
12260 The difference between those cases is finding possible inherited bindings
12261 that are overridden, as one has to look for them in tb_sym_root,
12262 tb_uop_root or tb_op, respectively. Thus the caller must already find
12263 the super-type and set p->overridden correctly. */
e157f736 12264
524af0d6 12265static bool
94747289
DK
12266resolve_tb_generic_targets (gfc_symbol* super_type,
12267 gfc_typebound_proc* p, const char* name)
e157f736
DK
12268{
12269 gfc_tbp_generic* target;
12270 gfc_symtree* first_target;
e157f736 12271 gfc_symtree* inherited;
e157f736 12272
94747289 12273 gcc_assert (p && p->is_generic);
e157f736
DK
12274
12275 /* Try to find the specific bindings for the symtrees in our target-list. */
94747289
DK
12276 gcc_assert (p->u.generic);
12277 for (target = p->u.generic; target; target = target->next)
e157f736
DK
12278 if (!target->specific)
12279 {
12280 gfc_typebound_proc* overridden_tbp;
12281 gfc_tbp_generic* g;
12282 const char* target_name;
12283
12284 target_name = target->specific_st->name;
12285
12286 /* Defined for this type directly. */
aea18e92 12287 if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
e157f736 12288 {
e34ccb4c 12289 target->specific = target->specific_st->n.tb;
e157f736
DK
12290 goto specific_found;
12291 }
12292
12293 /* Look for an inherited specific binding. */
12294 if (super_type)
12295 {
4a44a72d
DK
12296 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
12297 true, NULL);
e157f736
DK
12298
12299 if (inherited)
12300 {
e34ccb4c
DK
12301 gcc_assert (inherited->n.tb);
12302 target->specific = inherited->n.tb;
e157f736
DK
12303 goto specific_found;
12304 }
12305 }
12306
a4d9b221 12307 gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
94747289 12308 " at %L", target_name, name, &p->where);
524af0d6 12309 return false;
e157f736
DK
12310
12311 /* Once we've found the specific binding, check it is not ambiguous with
12312 other specifics already found or inherited for the same GENERIC. */
12313specific_found:
12314 gcc_assert (target->specific);
12315
12316 /* This must really be a specific binding! */
12317 if (target->specific->is_generic)
12318 {
a4d9b221
TB
12319 gfc_error ("GENERIC %qs at %L must target a specific binding,"
12320 " %qs is GENERIC, too", name, &p->where, target_name);
524af0d6 12321 return false;
e157f736
DK
12322 }
12323
12324 /* Check those already resolved on this type directly. */
94747289 12325 for (g = p->u.generic; g; g = g->next)
e157f736 12326 if (g != target && g->specific
524af0d6
JB
12327 && !check_generic_tbp_ambiguity (target, g, name, p->where))
12328 return false;
e157f736
DK
12329
12330 /* Check for ambiguity with inherited specific targets. */
94747289 12331 for (overridden_tbp = p->overridden; overridden_tbp;
e157f736
DK
12332 overridden_tbp = overridden_tbp->overridden)
12333 if (overridden_tbp->is_generic)
12334 {
12335 for (g = overridden_tbp->u.generic; g; g = g->next)
12336 {
12337 gcc_assert (g->specific);
524af0d6
JB
12338 if (!check_generic_tbp_ambiguity (target, g, name, p->where))
12339 return false;
e157f736
DK
12340 }
12341 }
12342 }
12343
12344 /* If we attempt to "overwrite" a specific binding, this is an error. */
94747289 12345 if (p->overridden && !p->overridden->is_generic)
e157f736 12346 {
a4d9b221 12347 gfc_error ("GENERIC %qs at %L can't overwrite specific binding with"
94747289 12348 " the same name", name, &p->where);
524af0d6 12349 return false;
e157f736
DK
12350 }
12351
12352 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
12353 all must have the same attributes here. */
94747289 12354 first_target = p->u.generic->specific->u.specific;
e34ccb4c 12355 gcc_assert (first_target);
94747289
DK
12356 p->subroutine = first_target->n.sym->attr.subroutine;
12357 p->function = first_target->n.sym->attr.function;
e157f736 12358
524af0d6 12359 return true;
e157f736
DK
12360}
12361
12362
94747289
DK
12363/* Resolve a GENERIC procedure binding for a derived type. */
12364
524af0d6 12365static bool
94747289
DK
12366resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
12367{
12368 gfc_symbol* super_type;
12369
12370 /* Find the overridden binding if any. */
12371 st->n.tb->overridden = NULL;
12372 super_type = gfc_get_derived_super_type (derived);
12373 if (super_type)
12374 {
12375 gfc_symtree* overridden;
4a44a72d
DK
12376 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
12377 true, NULL);
94747289
DK
12378
12379 if (overridden && overridden->n.tb)
12380 st->n.tb->overridden = overridden->n.tb;
12381 }
12382
12383 /* Resolve using worker function. */
12384 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
12385}
12386
12387
b325faf9
DK
12388/* Retrieve the target-procedure of an operator binding and do some checks in
12389 common for intrinsic and user-defined type-bound operators. */
12390
12391static gfc_symbol*
12392get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
12393{
12394 gfc_symbol* target_proc;
12395
12396 gcc_assert (target->specific && !target->specific->is_generic);
12397 target_proc = target->specific->u.specific->n.sym;
12398 gcc_assert (target_proc);
12399
2e33ad21 12400 /* F08:C468. All operator bindings must have a passed-object dummy argument. */
b325faf9
DK
12401 if (target->specific->nopass)
12402 {
12403 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
12404 return NULL;
12405 }
12406
12407 return target_proc;
12408}
12409
12410
94747289
DK
12411/* Resolve a type-bound intrinsic operator. */
12412
524af0d6 12413static bool
94747289
DK
12414resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
12415 gfc_typebound_proc* p)
12416{
12417 gfc_symbol* super_type;
12418 gfc_tbp_generic* target;
4d382327 12419
94747289
DK
12420 /* If there's already an error here, do nothing (but don't fail again). */
12421 if (p->error)
524af0d6 12422 return true;
94747289
DK
12423
12424 /* Operators should always be GENERIC bindings. */
12425 gcc_assert (p->is_generic);
12426
12427 /* Look for an overridden binding. */
12428 super_type = gfc_get_derived_super_type (derived);
12429 if (super_type && super_type->f2k_derived)
12430 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
4a44a72d 12431 op, true, NULL);
94747289
DK
12432 else
12433 p->overridden = NULL;
12434
12435 /* Resolve general GENERIC properties using worker function. */
524af0d6 12436 if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
94747289
DK
12437 goto error;
12438
12439 /* Check the targets to be procedures of correct interface. */
12440 for (target = p->u.generic; target; target = target->next)
12441 {
12442 gfc_symbol* target_proc;
12443
b325faf9
DK
12444 target_proc = get_checked_tb_operator_target (target, p->where);
12445 if (!target_proc)
4a44a72d 12446 goto error;
94747289
DK
12447
12448 if (!gfc_check_operator_interface (target_proc, op, p->where))
4a44a72d 12449 goto error;
362aa474
JW
12450
12451 /* Add target to non-typebound operator list. */
12452 if (!target->specific->deferred && !derived->attr.use_assoc
474d486a 12453 && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
362aa474
JW
12454 {
12455 gfc_interface *head, *intr;
524af0d6
JB
12456 if (!gfc_check_new_interface (derived->ns->op[op], target_proc, p->where))
12457 return false;
362aa474
JW
12458 head = derived->ns->op[op];
12459 intr = gfc_get_interface ();
12460 intr->sym = target_proc;
12461 intr->where = p->where;
12462 intr->next = head;
12463 derived->ns->op[op] = intr;
12464 }
94747289
DK
12465 }
12466
524af0d6 12467 return true;
94747289
DK
12468
12469error:
12470 p->error = 1;
524af0d6 12471 return false;
94747289
DK
12472}
12473
12474
12475/* Resolve a type-bound user operator (tree-walker callback). */
30b608eb
DK
12476
12477static gfc_symbol* resolve_bindings_derived;
524af0d6 12478static bool resolve_bindings_result;
30b608eb 12479
524af0d6 12480static bool check_uop_procedure (gfc_symbol* sym, locus where);
94747289
DK
12481
12482static void
12483resolve_typebound_user_op (gfc_symtree* stree)
12484{
12485 gfc_symbol* super_type;
12486 gfc_tbp_generic* target;
12487
12488 gcc_assert (stree && stree->n.tb);
12489
12490 if (stree->n.tb->error)
12491 return;
12492
12493 /* Operators should always be GENERIC bindings. */
12494 gcc_assert (stree->n.tb->is_generic);
12495
12496 /* Find overridden procedure, if any. */
12497 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12498 if (super_type && super_type->f2k_derived)
12499 {
12500 gfc_symtree* overridden;
12501 overridden = gfc_find_typebound_user_op (super_type, NULL,
4a44a72d 12502 stree->name, true, NULL);
94747289
DK
12503
12504 if (overridden && overridden->n.tb)
12505 stree->n.tb->overridden = overridden->n.tb;
12506 }
12507 else
12508 stree->n.tb->overridden = NULL;
12509
12510 /* Resolve basically using worker function. */
524af0d6 12511 if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
94747289
DK
12512 goto error;
12513
12514 /* Check the targets to be functions of correct interface. */
12515 for (target = stree->n.tb->u.generic; target; target = target->next)
12516 {
12517 gfc_symbol* target_proc;
12518
b325faf9
DK
12519 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
12520 if (!target_proc)
12521 goto error;
94747289 12522
524af0d6 12523 if (!check_uop_procedure (target_proc, stree->n.tb->where))
94747289
DK
12524 goto error;
12525 }
12526
12527 return;
12528
12529error:
524af0d6 12530 resolve_bindings_result = false;
94747289
DK
12531 stree->n.tb->error = 1;
12532}
12533
12534
12535/* Resolve the type-bound procedures for a derived type. */
12536
30b608eb
DK
12537static void
12538resolve_typebound_procedure (gfc_symtree* stree)
12539{
12540 gfc_symbol* proc;
12541 locus where;
12542 gfc_symbol* me_arg;
12543 gfc_symbol* super_type;
9d1210f4 12544 gfc_component* comp;
30b608eb 12545
e34ccb4c
DK
12546 gcc_assert (stree);
12547
12548 /* Undefined specific symbol from GENERIC target definition. */
12549 if (!stree->n.tb)
12550 return;
12551
12552 if (stree->n.tb->error)
30b608eb
DK
12553 return;
12554
e157f736 12555 /* If this is a GENERIC binding, use that routine. */
e34ccb4c 12556 if (stree->n.tb->is_generic)
e157f736 12557 {
524af0d6 12558 if (!resolve_typebound_generic (resolve_bindings_derived, stree))
e157f736
DK
12559 goto error;
12560 return;
12561 }
12562
30b608eb 12563 /* Get the target-procedure to check it. */
e34ccb4c
DK
12564 gcc_assert (!stree->n.tb->is_generic);
12565 gcc_assert (stree->n.tb->u.specific);
12566 proc = stree->n.tb->u.specific->n.sym;
12567 where = stree->n.tb->where;
30b608eb
DK
12568
12569 /* Default access should already be resolved from the parser. */
e34ccb4c 12570 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
30b608eb 12571
b6a45605 12572 if (stree->n.tb->deferred)
30b608eb 12573 {
524af0d6 12574 if (!check_proc_interface (proc, &where))
b6a45605
JW
12575 goto error;
12576 }
12577 else
12578 {
12579 /* Check for F08:C465. */
12580 if ((!proc->attr.subroutine && !proc->attr.function)
12581 || (proc->attr.proc != PROC_MODULE
12582 && proc->attr.if_source != IFSRC_IFBODY)
12583 || proc->attr.abstract)
12584 {
a4d9b221 12585 gfc_error ("%qs must be a module procedure or an external procedure with"
b6a45605
JW
12586 " an explicit interface at %L", proc->name, &where);
12587 goto error;
12588 }
30b608eb 12589 }
b6a45605 12590
e34ccb4c
DK
12591 stree->n.tb->subroutine = proc->attr.subroutine;
12592 stree->n.tb->function = proc->attr.function;
30b608eb
DK
12593
12594 /* Find the super-type of the current derived type. We could do this once and
12595 store in a global if speed is needed, but as long as not I believe this is
12596 more readable and clearer. */
12597 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
12598
e157f736
DK
12599 /* If PASS, resolve and check arguments if not already resolved / loaded
12600 from a .mod file. */
e34ccb4c 12601 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
30b608eb 12602 {
4cbc9039
JW
12603 gfc_formal_arglist *dummy_args;
12604
12605 dummy_args = gfc_sym_get_dummy_args (proc);
e34ccb4c 12606 if (stree->n.tb->pass_arg)
30b608eb 12607 {
4cbc9039 12608 gfc_formal_arglist *i;
30b608eb
DK
12609
12610 /* If an explicit passing argument name is given, walk the arg-list
12611 and look for it. */
12612
12613 me_arg = NULL;
e34ccb4c 12614 stree->n.tb->pass_arg_num = 1;
4cbc9039 12615 for (i = dummy_args; i; i = i->next)
30b608eb 12616 {
e34ccb4c 12617 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
30b608eb
DK
12618 {
12619 me_arg = i->sym;
12620 break;
12621 }
e34ccb4c 12622 ++stree->n.tb->pass_arg_num;
30b608eb
DK
12623 }
12624
12625 if (!me_arg)
12626 {
a4d9b221
TB
12627 gfc_error ("Procedure %qs with PASS(%s) at %L has no"
12628 " argument %qs",
e34ccb4c
DK
12629 proc->name, stree->n.tb->pass_arg, &where,
12630 stree->n.tb->pass_arg);
30b608eb
DK
12631 goto error;
12632 }
12633 }
12634 else
12635 {
12636 /* Otherwise, take the first one; there should in fact be at least
12637 one. */
e34ccb4c 12638 stree->n.tb->pass_arg_num = 1;
4cbc9039 12639 if (!dummy_args)
30b608eb 12640 {
a4d9b221 12641 gfc_error ("Procedure %qs with PASS at %L must have at"
30b608eb
DK
12642 " least one argument", proc->name, &where);
12643 goto error;
12644 }
4cbc9039 12645 me_arg = dummy_args->sym;
30b608eb
DK
12646 }
12647
41a394bb
DK
12648 /* Now check that the argument-type matches and the passed-object
12649 dummy argument is generally fine. */
12650
30b608eb 12651 gcc_assert (me_arg);
41a394bb 12652
cf2b3c22 12653 if (me_arg->ts.type != BT_CLASS)
30b608eb 12654 {
a4d9b221 12655 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
cf2b3c22 12656 " at %L", proc->name, &where);
30b608eb
DK
12657 goto error;
12658 }
8e1f752a 12659
7a08eda1 12660 if (CLASS_DATA (me_arg)->ts.u.derived
cf2b3c22 12661 != resolve_bindings_derived)
727e8544 12662 {
a4d9b221
TB
12663 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
12664 " the derived-type %qs", me_arg->name, proc->name,
cf2b3c22 12665 me_arg->name, &where, resolve_bindings_derived->name);
727e8544
JW
12666 goto error;
12667 }
4d382327 12668
41a394bb 12669 gcc_assert (me_arg->ts.type == BT_CLASS);
c62c6622 12670 if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
41a394bb 12671 {
a4d9b221 12672 gfc_error ("Passed-object dummy argument of %qs at %L must be"
41a394bb
DK
12673 " scalar", proc->name, &where);
12674 goto error;
12675 }
7a08eda1 12676 if (CLASS_DATA (me_arg)->attr.allocatable)
41a394bb 12677 {
a4d9b221 12678 gfc_error ("Passed-object dummy argument of %qs at %L must not"
41a394bb
DK
12679 " be ALLOCATABLE", proc->name, &where);
12680 goto error;
12681 }
7a08eda1 12682 if (CLASS_DATA (me_arg)->attr.class_pointer)
41a394bb 12683 {
a4d9b221 12684 gfc_error ("Passed-object dummy argument of %qs at %L must not"
41a394bb
DK
12685 " be POINTER", proc->name, &where);
12686 goto error;
12687 }
30b608eb
DK
12688 }
12689
12690 /* If we are extending some type, check that we don't override a procedure
12691 flagged NON_OVERRIDABLE. */
e34ccb4c 12692 stree->n.tb->overridden = NULL;
30b608eb
DK
12693 if (super_type)
12694 {
12695 gfc_symtree* overridden;
8e1f752a 12696 overridden = gfc_find_typebound_proc (super_type, NULL,
4a44a72d 12697 stree->name, true, NULL);
30b608eb 12698
99fc1b90
JW
12699 if (overridden)
12700 {
12701 if (overridden->n.tb)
12702 stree->n.tb->overridden = overridden->n.tb;
e157f736 12703
524af0d6 12704 if (!gfc_check_typebound_override (stree, overridden))
99fc1b90
JW
12705 goto error;
12706 }
30b608eb
DK
12707 }
12708
9d1210f4
DK
12709 /* See if there's a name collision with a component directly in this type. */
12710 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
12711 if (!strcmp (comp->name, stree->name))
12712 {
a4d9b221
TB
12713 gfc_error ("Procedure %qs at %L has the same name as a component of"
12714 " %qs",
9d1210f4
DK
12715 stree->name, &where, resolve_bindings_derived->name);
12716 goto error;
12717 }
12718
12719 /* Try to find a name collision with an inherited component. */
12720 if (super_type && gfc_find_component (super_type, stree->name, true, true))
12721 {
a4d9b221
TB
12722 gfc_error ("Procedure %qs at %L has the same name as an inherited"
12723 " component of %qs",
9d1210f4
DK
12724 stree->name, &where, resolve_bindings_derived->name);
12725 goto error;
12726 }
12727
e34ccb4c 12728 stree->n.tb->error = 0;
30b608eb
DK
12729 return;
12730
12731error:
524af0d6 12732 resolve_bindings_result = false;
e34ccb4c 12733 stree->n.tb->error = 1;
30b608eb
DK
12734}
12735
bd48f123 12736
524af0d6 12737static bool
30b608eb
DK
12738resolve_typebound_procedures (gfc_symbol* derived)
12739{
94747289 12740 int op;
0291fa25 12741 gfc_symbol* super_type;
94747289 12742
e34ccb4c 12743 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
524af0d6 12744 return true;
4d382327 12745
0291fa25
JW
12746 super_type = gfc_get_derived_super_type (derived);
12747 if (super_type)
49c8d79b 12748 resolve_symbol (super_type);
30b608eb
DK
12749
12750 resolve_bindings_derived = derived;
524af0d6 12751 resolve_bindings_result = true;
94747289
DK
12752
12753 if (derived->f2k_derived->tb_sym_root)
12754 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
12755 &resolve_typebound_procedure);
12756
94747289
DK
12757 if (derived->f2k_derived->tb_uop_root)
12758 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
12759 &resolve_typebound_user_op);
12760
12761 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
12762 {
12763 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
22c23886 12764 if (p && !resolve_typebound_intrinsic_op (derived,
524af0d6
JB
12765 (gfc_intrinsic_op)op, p))
12766 resolve_bindings_result = false;
94747289 12767 }
30b608eb
DK
12768
12769 return resolve_bindings_result;
12770}
12771
12772
9d5c21c1
PT
12773/* Add a derived type to the dt_list. The dt_list is used in trans-types.c
12774 to give all identical derived types the same backend_decl. */
12775static void
12776add_dt_to_dt_list (gfc_symbol *derived)
12777{
12778 gfc_dt_list *dt_list;
12779
12780 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
12781 if (derived == dt_list->derived)
f372a0c0 12782 return;
9d5c21c1 12783
f372a0c0
MM
12784 dt_list = gfc_get_dt_list ();
12785 dt_list->next = gfc_derived_types;
12786 dt_list->derived = derived;
12787 gfc_derived_types = dt_list;
9d5c21c1
PT
12788}
12789
12790
b0e5fa94
DK
12791/* Ensure that a derived-type is really not abstract, meaning that every
12792 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
12793
524af0d6 12794static bool
b0e5fa94
DK
12795ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
12796{
12797 if (!st)
524af0d6 12798 return true;
b0e5fa94 12799
524af0d6
JB
12800 if (!ensure_not_abstract_walker (sub, st->left))
12801 return false;
12802 if (!ensure_not_abstract_walker (sub, st->right))
12803 return false;
b0e5fa94 12804
e34ccb4c 12805 if (st->n.tb && st->n.tb->deferred)
b0e5fa94
DK
12806 {
12807 gfc_symtree* overriding;
4a44a72d 12808 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
9c4174d8 12809 if (!overriding)
524af0d6 12810 return false;
9c4174d8 12811 gcc_assert (overriding->n.tb);
e34ccb4c 12812 if (overriding->n.tb->deferred)
b0e5fa94 12813 {
a4d9b221
TB
12814 gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
12815 " %qs is DEFERRED and not overridden",
b0e5fa94 12816 sub->name, &sub->declared_at, st->name);
524af0d6 12817 return false;
b0e5fa94
DK
12818 }
12819 }
12820
524af0d6 12821 return true;
b0e5fa94
DK
12822}
12823
524af0d6 12824static bool
b0e5fa94
DK
12825ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
12826{
12827 /* The algorithm used here is to recursively travel up the ancestry of sub
12828 and for each ancestor-type, check all bindings. If any of them is
12829 DEFERRED, look it up starting from sub and see if the found (overriding)
12830 binding is not DEFERRED.
12831 This is not the most efficient way to do this, but it should be ok and is
12832 clearer than something sophisticated. */
12833
7c9b8fb9 12834 gcc_assert (ancestor && !sub->attr.abstract);
4d382327 12835
7c9b8fb9 12836 if (!ancestor->attr.abstract)
524af0d6 12837 return true;
b0e5fa94
DK
12838
12839 /* Walk bindings of this ancestor. */
12840 if (ancestor->f2k_derived)
12841 {
524af0d6 12842 bool t;
e34ccb4c 12843 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
524af0d6
JB
12844 if (!t)
12845 return false;
b0e5fa94
DK
12846 }
12847
12848 /* Find next ancestor type and recurse on it. */
12849 ancestor = gfc_get_derived_super_type (ancestor);
12850 if (ancestor)
12851 return ensure_not_abstract (sub, ancestor);
12852
524af0d6 12853 return true;
b0e5fa94
DK
12854}
12855
12856
4d382327
AF
12857/* This check for typebound defined assignments is done recursively
12858 since the order in which derived types are resolved is not always in
12859 order of the declarations. */
12860
12861static void
12862check_defined_assignments (gfc_symbol *derived)
12863{
12864 gfc_component *c;
12865
12866 for (c = derived->components; c; c = c->next)
12867 {
12868 if (c->ts.type != BT_DERIVED
12869 || c->attr.pointer
12870 || c->attr.allocatable
12871 || c->attr.proc_pointer_comp
12872 || c->attr.class_pointer
12873 || c->attr.proc_pointer)
12874 continue;
12875
12876 if (c->ts.u.derived->attr.defined_assign_comp
12877 || (c->ts.u.derived->f2k_derived
12878 && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
12879 {
12880 derived->attr.defined_assign_comp = 1;
12881 return;
12882 }
12883
12884 check_defined_assignments (c->ts.u.derived);
12885 if (c->ts.u.derived->attr.defined_assign_comp)
12886 {
12887 derived->attr.defined_assign_comp = 1;
12888 return;
12889 }
12890 }
12891}
12892
12893
0291fa25
JW
12894/* Resolve the components of a derived type. This does not have to wait until
12895 resolution stage, but can be done as soon as the dt declaration has been
12896 parsed. */
110eec24 12897
524af0d6 12898static bool
0291fa25 12899resolve_fl_derived0 (gfc_symbol *sym)
110eec24 12900{
9d1210f4 12901 gfc_symbol* super_type;
110eec24
TS
12902 gfc_component *c;
12903
8b704316 12904 if (sym->attr.unlimited_polymorphic)
524af0d6 12905 return true;
8b704316 12906
9d1210f4
DK
12907 super_type = gfc_get_derived_super_type (sym);
12908
1cc0e193 12909 /* F2008, C432. */
be59db2d
TB
12910 if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
12911 {
a4d9b221
TB
12912 gfc_error ("As extending type %qs at %L has a coarray component, "
12913 "parent type %qs shall also have one", sym->name,
be59db2d 12914 &sym->declared_at, super_type->name);
524af0d6 12915 return false;
be59db2d
TB
12916 }
12917
e157f736 12918 /* Ensure the extended type gets resolved before we do. */
524af0d6
JB
12919 if (super_type && !resolve_fl_derived0 (super_type))
12920 return false;
e157f736 12921
52f49934 12922 /* An ABSTRACT type must be extensible. */
cf2b3c22 12923 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
52f49934 12924 {
a4d9b221 12925 gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
52f49934 12926 sym->name, &sym->declared_at);
524af0d6 12927 return false;
52f49934
DK
12928 }
12929
fac665b2
TB
12930 c = (sym->attr.is_class) ? sym->components->ts.u.derived->components
12931 : sym->components;
12932
cab283f5
JW
12933 bool success = true;
12934
fac665b2 12935 for ( ; c != NULL; c = c->next)
110eec24 12936 {
8e54f139
TB
12937 if (c->attr.artificial)
12938 continue;
12939
be59db2d 12940 /* F2008, C442. */
c49ea23d
PT
12941 if ((!sym->attr.is_class || c != sym->components)
12942 && c->attr.codimension
d3a9eea2 12943 && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
be59db2d 12944 {
c4100eae 12945 gfc_error ("Coarray component %qs at %L must be allocatable with "
be59db2d 12946 "deferred shape", c->name, &c->loc);
cab283f5
JW
12947 success = false;
12948 continue;
be59db2d
TB
12949 }
12950
12951 /* F2008, C443. */
12952 if (c->attr.codimension && c->ts.type == BT_DERIVED
12953 && c->ts.u.derived->ts.is_iso_c)
12954 {
c4100eae 12955 gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
be59db2d 12956 "shall not be a coarray", c->name, &c->loc);
cab283f5
JW
12957 success = false;
12958 continue;
be59db2d
TB
12959 }
12960
12961 /* F2008, C444. */
12962 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.coarray_comp
178f9aa1
TB
12963 && (c->attr.codimension || c->attr.pointer || c->attr.dimension
12964 || c->attr.allocatable))
be59db2d 12965 {
c4100eae 12966 gfc_error ("Component %qs at %L with coarray component "
be59db2d
TB
12967 "shall be a nonpointer, nonallocatable scalar",
12968 c->name, &c->loc);
cab283f5
JW
12969 success = false;
12970 continue;
be59db2d
TB
12971 }
12972
fe4e525c
TB
12973 /* F2008, C448. */
12974 if (c->attr.contiguous && (!c->attr.dimension || !c->attr.pointer))
12975 {
c4100eae 12976 gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
fe4e525c 12977 "is not an array pointer", c->name, &c->loc);
cab283f5
JW
12978 success = false;
12979 continue;
fe4e525c
TB
12980 }
12981
713485cc
JW
12982 if (c->attr.proc_pointer && c->ts.interface)
12983 {
b6a45605 12984 gfc_symbol *ifc = c->ts.interface;
713485cc 12985
c34d453f
JW
12986 if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
12987 {
12988 c->tb->error = 1;
cab283f5
JW
12989 success = false;
12990 continue;
c34d453f 12991 }
713485cc 12992
b6a45605
JW
12993 if (ifc->attr.if_source || ifc->attr.intrinsic)
12994 {
12995 /* Resolve interface and copy attributes. */
acbdc378
JW
12996 if (ifc->formal && !ifc->formal_ns)
12997 resolve_symbol (ifc);
713485cc 12998 if (ifc->attr.intrinsic)
2dda89a8 12999 gfc_resolve_intrinsic (ifc, &ifc->declared_at);
713485cc
JW
13000
13001 if (ifc->result)
f64edc8b
JW
13002 {
13003 c->ts = ifc->result->ts;
13004 c->attr.allocatable = ifc->result->attr.allocatable;
13005 c->attr.pointer = ifc->result->attr.pointer;
13006 c->attr.dimension = ifc->result->attr.dimension;
13007 c->as = gfc_copy_array_spec (ifc->result->as);
5e25600e 13008 c->attr.class_ok = ifc->result->attr.class_ok;
f64edc8b
JW
13009 }
13010 else
4d382327 13011 {
f64edc8b
JW
13012 c->ts = ifc->ts;
13013 c->attr.allocatable = ifc->attr.allocatable;
13014 c->attr.pointer = ifc->attr.pointer;
13015 c->attr.dimension = ifc->attr.dimension;
13016 c->as = gfc_copy_array_spec (ifc->as);
5e25600e 13017 c->attr.class_ok = ifc->attr.class_ok;
f64edc8b 13018 }
713485cc
JW
13019 c->ts.interface = ifc;
13020 c->attr.function = ifc->attr.function;
13021 c->attr.subroutine = ifc->attr.subroutine;
713485cc 13022
713485cc
JW
13023 c->attr.pure = ifc->attr.pure;
13024 c->attr.elemental = ifc->attr.elemental;
713485cc
JW
13025 c->attr.recursive = ifc->attr.recursive;
13026 c->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 13027 c->attr.ext_attr |= ifc->attr.ext_attr;
713485cc 13028 /* Copy char length. */
bc21d315 13029 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
713485cc 13030 {
9c4174d8 13031 gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
9c4174d8 13032 if (cl->length && !cl->resolved
524af0d6 13033 && !gfc_resolve_expr (cl->length))
cab283f5
JW
13034 {
13035 c->tb->error = 1;
13036 success = false;
13037 continue;
13038 }
9c4174d8 13039 c->ts.u.cl = cl;
713485cc
JW
13040 }
13041 }
713485cc
JW
13042 }
13043 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
13044 {
6c036626
JW
13045 /* Since PPCs are not implicitly typed, a PPC without an explicit
13046 interface must be a subroutine. */
13047 gfc_add_subroutine (&c->attr, c->name, &c->loc);
713485cc
JW
13048 }
13049
90661f26 13050 /* Procedure pointer components: Check PASS arg. */
eece1eb9
PT
13051 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
13052 && !sym->attr.vtype)
90661f26
JW
13053 {
13054 gfc_symbol* me_arg;
13055
13056 if (c->tb->pass_arg)
13057 {
13058 gfc_formal_arglist* i;
13059
13060 /* If an explicit passing argument name is given, walk the arg-list
13061 and look for it. */
13062
13063 me_arg = NULL;
13064 c->tb->pass_arg_num = 1;
4cbc9039 13065 for (i = c->ts.interface->formal; i; i = i->next)
90661f26
JW
13066 {
13067 if (!strcmp (i->sym->name, c->tb->pass_arg))
13068 {
13069 me_arg = i->sym;
13070 break;
13071 }
13072 c->tb->pass_arg_num++;
13073 }
13074
13075 if (!me_arg)
13076 {
c4100eae
MLI
13077 gfc_error ("Procedure pointer component %qs with PASS(%s) "
13078 "at %L has no argument %qs", c->name,
90661f26
JW
13079 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
13080 c->tb->error = 1;
cab283f5
JW
13081 success = false;
13082 continue;
90661f26
JW
13083 }
13084 }
13085 else
13086 {
13087 /* Otherwise, take the first one; there should in fact be at least
13088 one. */
13089 c->tb->pass_arg_num = 1;
4cbc9039 13090 if (!c->ts.interface->formal)
90661f26 13091 {
c4100eae 13092 gfc_error ("Procedure pointer component %qs with PASS at %L "
90661f26
JW
13093 "must have at least one argument",
13094 c->name, &c->loc);
13095 c->tb->error = 1;
cab283f5
JW
13096 success = false;
13097 continue;
90661f26 13098 }
4cbc9039 13099 me_arg = c->ts.interface->formal->sym;
90661f26
JW
13100 }
13101
13102 /* Now check that the argument-type matches. */
13103 gcc_assert (me_arg);
cf2b3c22
TB
13104 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
13105 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
13106 || (me_arg->ts.type == BT_CLASS
7a08eda1 13107 && CLASS_DATA (me_arg)->ts.u.derived != sym))
90661f26 13108 {
c4100eae
MLI
13109 gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
13110 " the derived type %qs", me_arg->name, c->name,
90661f26
JW
13111 me_arg->name, &c->loc, sym->name);
13112 c->tb->error = 1;
cab283f5
JW
13113 success = false;
13114 continue;
90661f26
JW
13115 }
13116
13117 /* Check for C453. */
13118 if (me_arg->attr.dimension)
13119 {
c4100eae 13120 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
90661f26
JW
13121 "must be scalar", me_arg->name, c->name, me_arg->name,
13122 &c->loc);
13123 c->tb->error = 1;
cab283f5
JW
13124 success = false;
13125 continue;
90661f26
JW
13126 }
13127
13128 if (me_arg->attr.pointer)
13129 {
c4100eae 13130 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
90661f26
JW
13131 "may not have the POINTER attribute", me_arg->name,
13132 c->name, me_arg->name, &c->loc);
13133 c->tb->error = 1;
cab283f5
JW
13134 success = false;
13135 continue;
90661f26
JW
13136 }
13137
13138 if (me_arg->attr.allocatable)
13139 {
c4100eae 13140 gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
90661f26
JW
13141 "may not be ALLOCATABLE", me_arg->name, c->name,
13142 me_arg->name, &c->loc);
13143 c->tb->error = 1;
cab283f5
JW
13144 success = false;
13145 continue;
90661f26
JW
13146 }
13147
cf2b3c22 13148 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
cab283f5
JW
13149 {
13150 gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
13151 " at %L", c->name, &c->loc);
13152 success = false;
13153 continue;
13154 }
90661f26
JW
13155
13156 }
13157
52f49934 13158 /* Check type-spec if this is not the parent-type component. */
fac665b2
TB
13159 if (((sym->attr.is_class
13160 && (!sym->components->ts.u.derived->attr.extension
13161 || c != sym->components->ts.u.derived->components))
13162 || (!sym->attr.is_class
13163 && (!sym->attr.extension || c != sym->components)))
13164 && !sym->attr.vtype
524af0d6
JB
13165 && !resolve_typespec_used (&c->ts, &c->loc, c->name))
13166 return false;
52f49934 13167
f89cc1a3
JW
13168 /* If this type is an extension, set the accessibility of the parent
13169 component. */
fac665b2
TB
13170 if (super_type
13171 && ((sym->attr.is_class
13172 && c == sym->components->ts.u.derived->components)
13173 || (!sym->attr.is_class && c == sym->components))
f89cc1a3
JW
13174 && strcmp (super_type->name, c->name) == 0)
13175 c->attr.access = super_type->attr.access;
4d382327 13176
9d1210f4
DK
13177 /* If this type is an extension, see if this component has the same name
13178 as an inherited type-bound procedure. */
371b334e 13179 if (super_type && !sym->attr.is_class
4a44a72d 13180 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9d1210f4 13181 {
c4100eae 13182 gfc_error ("Component %qs of %qs at %L has the same name as an"
9d1210f4
DK
13183 " inherited type-bound procedure",
13184 c->name, sym->name, &c->loc);
524af0d6 13185 return false;
9d1210f4
DK
13186 }
13187
8d51f26f
PT
13188 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
13189 && !c->ts.deferred)
110eec24 13190 {
bc21d315 13191 if (c->ts.u.cl->length == NULL
524af0d6 13192 || (!resolve_charlen(c->ts.u.cl))
bc21d315 13193 || !gfc_is_constant_expr (c->ts.u.cl->length))
110eec24 13194 {
c4100eae 13195 gfc_error ("Character length of component %qs needs to "
e25a0da3 13196 "be a constant specification expression at %L",
110eec24 13197 c->name,
bc21d315 13198 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
524af0d6 13199 return false;
110eec24
TS
13200 }
13201 }
13202
8d51f26f
PT
13203 if (c->ts.type == BT_CHARACTER && c->ts.deferred
13204 && !c->attr.pointer && !c->attr.allocatable)
13205 {
c4100eae 13206 gfc_error ("Character component %qs of %qs at %L with deferred "
8d51f26f
PT
13207 "length must be a POINTER or ALLOCATABLE",
13208 c->name, sym->name, &c->loc);
524af0d6 13209 return false;
8d51f26f
PT
13210 }
13211
2b3dc0db
PT
13212 /* Add the hidden deferred length field. */
13213 if (c->ts.type == BT_CHARACTER && c->ts.deferred && !c->attr.function
13214 && !sym->attr.is_class)
13215 {
13216 char name[GFC_MAX_SYMBOL_LEN+9];
13217 gfc_component *strlen;
13218 sprintf (name, "_%s_length", c->name);
13219 strlen = gfc_find_component (sym, name, true, true);
13220 if (strlen == NULL)
13221 {
13222 if (!gfc_add_component (sym, name, &strlen))
13223 return false;
13224 strlen->ts.type = BT_INTEGER;
13225 strlen->ts.kind = gfc_charlen_int_kind;
13226 strlen->attr.access = ACCESS_PRIVATE;
9b548517 13227 strlen->attr.artificial = 1;
2b3dc0db
PT
13228 }
13229 }
13230
2ed8d224 13231 if (c->ts.type == BT_DERIVED
edf1eac2 13232 && sym->component_access != ACCESS_PRIVATE
6e2062b0 13233 && gfc_check_symbol_access (sym)
bc21d315
JW
13234 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
13235 && !c->ts.u.derived->attr.use_assoc
6e2062b0 13236 && !gfc_check_symbol_access (c->ts.u.derived)
a4d9b221 13237 && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
524af0d6 13238 "PRIVATE type and cannot be a component of "
a4d9b221 13239 "%qs, which is PUBLIC at %L", c->name,
524af0d6
JB
13240 sym->name, &sym->declared_at))
13241 return false;
2ed8d224 13242
0149d8cc
TB
13243 if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
13244 {
13245 gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
13246 "type %s", c->name, &c->loc, sym->name);
524af0d6 13247 return false;
0149d8cc
TB
13248 }
13249
f970c857
PT
13250 if (sym->attr.sequence)
13251 {
bc21d315 13252 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
f970c857
PT
13253 {
13254 gfc_error ("Component %s of SEQUENCE type declared at %L does "
13255 "not have the SEQUENCE attribute",
bc21d315 13256 c->ts.u.derived->name, &sym->declared_at);
524af0d6 13257 return false;
f970c857
PT
13258 }
13259 }
13260
c3f34952
TB
13261 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
13262 c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
13263 else if (c->ts.type == BT_CLASS && c->attr.class_ok
13264 && CLASS_DATA (c)->ts.u.derived->attr.generic)
13265 CLASS_DATA (c)->ts.u.derived
13266 = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
13267
50f30801
JW
13268 if (!sym->attr.is_class && c->ts.type == BT_DERIVED && !sym->attr.vtype
13269 && c->attr.pointer && c->ts.u.derived->components == NULL
bc21d315 13270 && !c->ts.u.derived->attr.zero_comp)
982186b1 13271 {
c4100eae 13272 gfc_error ("The pointer component %qs of %qs at %L is a type "
982186b1
PT
13273 "that has not been declared", c->name, sym->name,
13274 &c->loc);
524af0d6 13275 return false;
982186b1
PT
13276 }
13277
9c9eacb9
JW
13278 if (c->ts.type == BT_CLASS && c->attr.class_ok
13279 && CLASS_DATA (c)->attr.class_pointer
7a08eda1 13280 && CLASS_DATA (c)->ts.u.derived->components == NULL
8b704316
PT
13281 && !CLASS_DATA (c)->ts.u.derived->attr.zero_comp
13282 && !UNLIMITED_POLY (c))
371b334e 13283 {
c4100eae 13284 gfc_error ("The pointer component %qs of %qs at %L is a type "
371b334e
JW
13285 "that has not been declared", c->name, sym->name,
13286 &c->loc);
524af0d6 13287 return false;
371b334e
JW
13288 }
13289
727e8544 13290 /* C437. */
9c9eacb9
JW
13291 if (c->ts.type == BT_CLASS && c->attr.flavor != FL_PROCEDURE
13292 && (!c->attr.class_ok
13293 || !(CLASS_DATA (c)->attr.class_pointer
13294 || CLASS_DATA (c)->attr.allocatable)))
727e8544 13295 {
c4100eae 13296 gfc_error ("Component %qs with CLASS at %L must be allocatable "
727e8544 13297 "or pointer", c->name, &c->loc);
8ec4321f
PT
13298 /* Prevent a recurrence of the error. */
13299 c->ts.type = BT_UNKNOWN;
524af0d6 13300 return false;
727e8544
JW
13301 }
13302
9d5c21c1
PT
13303 /* Ensure that all the derived type components are put on the
13304 derived type list; even in formal namespaces, where derived type
13305 pointer components might not have been declared. */
13306 if (c->ts.type == BT_DERIVED
bc21d315
JW
13307 && c->ts.u.derived
13308 && c->ts.u.derived->components
d4b7d0f0 13309 && c->attr.pointer
bc21d315
JW
13310 && sym != c->ts.u.derived)
13311 add_dt_to_dt_list (c->ts.u.derived);
9d5c21c1 13312
22c23886
PT
13313 if (!gfc_resolve_array_spec (c->as,
13314 !(c->attr.pointer || c->attr.proc_pointer
524af0d6
JB
13315 || c->attr.allocatable)))
13316 return false;
e35e87dc
TB
13317
13318 if (c->initializer && !sym->attr.vtype
524af0d6
JB
13319 && !gfc_check_assign_symbol (sym, c, c->initializer))
13320 return false;
110eec24 13321 }
05c1e3a7 13322
cab283f5
JW
13323 if (!success)
13324 return false;
13325
4d382327
AF
13326 check_defined_assignments (sym);
13327
13328 if (!sym->attr.defined_assign_comp && super_type)
13329 sym->attr.defined_assign_comp
13330 = super_type->attr.defined_assign_comp;
13331
b0e5fa94
DK
13332 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
13333 all DEFERRED bindings are overridden. */
13334 if (super_type && super_type->attr.abstract && !sym->attr.abstract
5cd2f815 13335 && !sym->attr.is_class
524af0d6
JB
13336 && !ensure_not_abstract (sym, super_type))
13337 return false;
b0e5fa94 13338
6b887797 13339 /* Add derived type to the derived type list. */
9d5c21c1 13340 add_dt_to_dt_list (sym);
6b887797 13341
524af0d6 13342 return true;
110eec24
TS
13343}
13344
2ed8d224 13345
0291fa25
JW
13346/* The following procedure does the full resolution of a derived type,
13347 including resolution of all type-bound procedures (if present). In contrast
13348 to 'resolve_fl_derived0' this can only be done after the module has been
13349 parsed completely. */
13350
524af0d6 13351static bool
0291fa25
JW
13352resolve_fl_derived (gfc_symbol *sym)
13353{
c3f34952
TB
13354 gfc_symbol *gen_dt = NULL;
13355
8b704316 13356 if (sym->attr.unlimited_polymorphic)
524af0d6 13357 return true;
8b704316 13358
c3f34952
TB
13359 if (!sym->attr.is_class)
13360 gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
13361 if (gen_dt && gen_dt->generic && gen_dt->generic->next
6ba84c31
TB
13362 && (!gen_dt->generic->sym->attr.use_assoc
13363 || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
2a2703a2
MLI
13364 && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
13365 "%qs at %L being the same name as derived "
22c23886
PT
13366 "type at %L", sym->name,
13367 gen_dt->generic->sym == sym
13368 ? gen_dt->generic->next->sym->name
13369 : gen_dt->generic->sym->name,
13370 gen_dt->generic->sym == sym
13371 ? &gen_dt->generic->next->sym->declared_at
13372 : &gen_dt->generic->sym->declared_at,
524af0d6
JB
13373 &sym->declared_at))
13374 return false;
c3f34952 13375
8e54f139 13376 /* Resolve the finalizer procedures. */
cb414900 13377 if (!gfc_resolve_finalizers (sym, NULL))
524af0d6 13378 return false;
4d382327 13379
0291fa25
JW
13380 if (sym->attr.is_class && sym->ts.u.derived == NULL)
13381 {
13382 /* Fix up incomplete CLASS symbols. */
13383 gfc_component *data = gfc_find_component (sym, "_data", true, true);
13384 gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true);
8b704316
PT
13385
13386 /* Nothing more to do for unlimited polymorphic entities. */
13387 if (data->ts.u.derived->attr.unlimited_polymorphic)
524af0d6 13388 return true;
8b704316 13389 else if (vptr->ts.u.derived == NULL)
0291fa25
JW
13390 {
13391 gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
13392 gcc_assert (vtab);
13393 vptr->ts.u.derived = vtab->ts.u.derived;
13394 }
13395 }
4d382327 13396
524af0d6
JB
13397 if (!resolve_fl_derived0 (sym))
13398 return false;
4d382327 13399
0291fa25 13400 /* Resolve the type-bound procedures. */
524af0d6
JB
13401 if (!resolve_typebound_procedures (sym))
13402 return false;
0291fa25 13403
524af0d6 13404 return true;
0291fa25
JW
13405}
13406
13407
524af0d6 13408static bool
3e1cf500
PT
13409resolve_fl_namelist (gfc_symbol *sym)
13410{
13411 gfc_namelist *nl;
13412 gfc_symbol *nlsym;
13413
e0608471
TB
13414 for (nl = sym->namelist; nl; nl = nl->next)
13415 {
19d36107
TB
13416 /* Check again, the check in match only works if NAMELIST comes
13417 after the decl. */
13418 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
13419 {
a4d9b221 13420 gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
19d36107 13421 "allowed", nl->sym->name, sym->name, &sym->declared_at);
524af0d6 13422 return false;
19d36107
TB
13423 }
13424
e0608471 13425 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
a4d9b221
TB
13426 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13427 "with assumed shape in namelist %qs at %L",
524af0d6
JB
13428 nl->sym->name, sym->name, &sym->declared_at))
13429 return false;
e0608471 13430
19d36107 13431 if (is_non_constant_shape_array (nl->sym)
a4d9b221
TB
13432 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
13433 "with nonconstant shape in namelist %qs at %L",
524af0d6
JB
13434 nl->sym->name, sym->name, &sym->declared_at))
13435 return false;
e0608471 13436
19d36107
TB
13437 if (nl->sym->ts.type == BT_CHARACTER
13438 && (nl->sym->ts.u.cl->length == NULL
13439 || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
a4d9b221 13440 && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
524af0d6 13441 "nonconstant character length in "
a4d9b221 13442 "namelist %qs at %L", nl->sym->name,
524af0d6
JB
13443 sym->name, &sym->declared_at))
13444 return false;
e0608471 13445
19d36107
TB
13446 /* FIXME: Once UDDTIO is implemented, the following can be
13447 removed. */
13448 if (nl->sym->ts.type == BT_CLASS)
e0608471 13449 {
a4d9b221 13450 gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
19d36107
TB
13451 "polymorphic and requires a defined input/output "
13452 "procedure", nl->sym->name, sym->name, &sym->declared_at);
524af0d6 13453 return false;
e0608471
TB
13454 }
13455
19d36107
TB
13456 if (nl->sym->ts.type == BT_DERIVED
13457 && (nl->sym->ts.u.derived->attr.alloc_comp
13458 || nl->sym->ts.u.derived->attr.pointer_comp))
e0608471 13459 {
a4d9b221
TB
13460 if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
13461 "namelist %qs at %L with ALLOCATABLE "
22c23886 13462 "or POINTER components", nl->sym->name,
524af0d6
JB
13463 sym->name, &sym->declared_at))
13464 return false;
19d36107
TB
13465
13466 /* FIXME: Once UDDTIO is implemented, the following can be
13467 removed. */
a4d9b221 13468 gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
19d36107
TB
13469 "ALLOCATABLE or POINTER components and thus requires "
13470 "a defined input/output procedure", nl->sym->name,
13471 sym->name, &sym->declared_at);
524af0d6 13472 return false;
e0608471
TB
13473 }
13474 }
13475
3e1cf500 13476 /* Reject PRIVATE objects in a PUBLIC namelist. */
6e2062b0 13477 if (gfc_check_symbol_access (sym))
3e1cf500
PT
13478 {
13479 for (nl = sym->namelist; nl; nl = nl->next)
13480 {
3dbf6538 13481 if (!nl->sym->attr.use_assoc
c867b7b6 13482 && !is_sym_host_assoc (nl->sym, sym->ns)
6e2062b0 13483 && !gfc_check_symbol_access (nl->sym))
3e1cf500 13484 {
a4d9b221
TB
13485 gfc_error ("NAMELIST object %qs was declared PRIVATE and "
13486 "cannot be member of PUBLIC namelist %qs at %L",
5cca320d 13487 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 13488 return false;
5cca320d
DF
13489 }
13490
3dbf6538
DF
13491 /* Types with private components that came here by USE-association. */
13492 if (nl->sym->ts.type == BT_DERIVED
bc21d315 13493 && derived_inaccessible (nl->sym->ts.u.derived))
3dbf6538 13494 {
a4d9b221
TB
13495 gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
13496 "components and cannot be member of namelist %qs at %L",
3dbf6538 13497 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 13498 return false;
3dbf6538
DF
13499 }
13500
13501 /* Types with private components that are defined in the same module. */
5cca320d 13502 if (nl->sym->ts.type == BT_DERIVED
bc21d315 13503 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
6e2062b0 13504 && nl->sym->ts.u.derived->attr.private_comp)
5cca320d 13505 {
a4d9b221
TB
13506 gfc_error ("NAMELIST object %qs has PRIVATE components and "
13507 "cannot be a member of PUBLIC namelist %qs at %L",
5cca320d 13508 nl->sym->name, sym->name, &sym->declared_at);
524af0d6 13509 return false;
3e1cf500
PT
13510 }
13511 }
13512 }
13513
5cca320d 13514
3e1cf500 13515 /* 14.1.2 A module or internal procedure represent local entities
847b053d 13516 of the same type as a namelist member and so are not allowed. */
3e1cf500
PT
13517 for (nl = sym->namelist; nl; nl = nl->next)
13518 {
982186b1
PT
13519 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
13520 continue;
847b053d
PT
13521
13522 if (nl->sym->attr.function && nl->sym == nl->sym->result)
13523 if ((nl->sym == sym->ns->proc_name)
13524 ||
13525 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
13526 continue;
13527
3e1cf500 13528 nlsym = NULL;
99c25a87 13529 if (nl->sym->name)
847b053d 13530 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
982186b1
PT
13531 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
13532 {
13533 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
a4d9b221 13534 "attribute in %qs at %L", nlsym->name,
982186b1 13535 &sym->declared_at);
524af0d6 13536 return false;
982186b1 13537 }
3e1cf500
PT
13538 }
13539
524af0d6 13540 return true;
3e1cf500
PT
13541}
13542
13543
524af0d6 13544static bool
2ed8d224
PT
13545resolve_fl_parameter (gfc_symbol *sym)
13546{
13547 /* A parameter array's shape needs to be constant. */
4d382327 13548 if (sym->as != NULL
c317bc40
DF
13549 && (sym->as->type == AS_DEFERRED
13550 || is_non_constant_shape_array (sym)))
2ed8d224 13551 {
a4d9b221 13552 gfc_error ("Parameter array %qs at %L cannot be automatic "
c317bc40 13553 "or of deferred shape", sym->name, &sym->declared_at);
524af0d6 13554 return false;
2ed8d224
PT
13555 }
13556
13557 /* Make sure a parameter that has been implicitly typed still
13558 matches the implicit type, since PARAMETER statements can precede
13559 IMPLICIT statements. */
13560 if (sym->attr.implicit_type
713485cc
JW
13561 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
13562 sym->ns)))
2ed8d224 13563 {
a4d9b221 13564 gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
2ed8d224 13565 "later IMPLICIT type", sym->name, &sym->declared_at);
524af0d6 13566 return false;
2ed8d224
PT
13567 }
13568
13569 /* Make sure the types of derived parameters are consistent. This
13570 type checking is deferred until resolution because the type may
13571 refer to a derived type from the host. */
22c30bc0 13572 if (sym->ts.type == BT_DERIVED
edf1eac2 13573 && !gfc_compare_types (&sym->ts, &sym->value->ts))
2ed8d224
PT
13574 {
13575 gfc_error ("Incompatible derived type in PARAMETER at %L",
13576 &sym->value->where);
524af0d6 13577 return false;
2ed8d224 13578 }
524af0d6 13579 return true;
2ed8d224
PT
13580}
13581
13582
6de9cd9a
DN
13583/* Do anything necessary to resolve a symbol. Right now, we just
13584 assume that an otherwise unknown symbol is a variable. This sort
13585 of thing commonly happens for symbols in module. */
13586
13587static void
edf1eac2 13588resolve_symbol (gfc_symbol *sym)
6de9cd9a 13589{
a34437a1 13590 int check_constant, mp_flag;
219fa8c3
SK
13591 gfc_symtree *symtree;
13592 gfc_symtree *this_symtree;
13593 gfc_namespace *ns;
13594 gfc_component *c;
fac665b2
TB
13595 symbol_attribute class_attr;
13596 gfc_array_spec *as;
fd061185 13597 bool saved_specification_expr;
6de9cd9a 13598
4af8d042
MM
13599 if (sym->resolved)
13600 return;
13601 sym->resolved = 1;
13602
8e54f139
TB
13603 if (sym->attr.artificial)
13604 return;
13605
8b704316
PT
13606 if (sym->attr.unlimited_polymorphic)
13607 return;
13608
60fa3931
TB
13609 if (sym->attr.flavor == FL_UNKNOWN
13610 || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
13611 && !sym->attr.generic && !sym->attr.external
6bd59684
JW
13612 && sym->attr.if_source == IFSRC_UNKNOWN
13613 && sym->ts.type == BT_UNKNOWN))
6de9cd9a 13614 {
24d36d28
PT
13615
13616 /* If we find that a flavorless symbol is an interface in one of the
13617 parent namespaces, find its symtree in this namespace, free the
13618 symbol and set the symtree to point to the interface symbol. */
13619 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
13620 {
13621 symtree = gfc_find_symtree (ns->sym_root, sym->name);
7ca17033
JW
13622 if (symtree && (symtree->n.sym->generic ||
13623 (symtree->n.sym->attr.flavor == FL_PROCEDURE
13624 && sym->ns->construct_entities)))
24d36d28
PT
13625 {
13626 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
13627 sym->name);
511820a8
MM
13628 if (this_symtree->n.sym == sym)
13629 {
13630 symtree->n.sym->refs++;
13631 gfc_release_symbol (sym);
13632 this_symtree->n.sym = symtree->n.sym;
13633 return;
13634 }
24d36d28
PT
13635 }
13636 }
13637
13638 /* Otherwise give it a flavor according to such attributes as
13639 it has. */
60fa3931
TB
13640 if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
13641 && sym->attr.intrinsic == 0)
6de9cd9a 13642 sym->attr.flavor = FL_VARIABLE;
60fa3931 13643 else if (sym->attr.flavor == FL_UNKNOWN)
6de9cd9a
DN
13644 {
13645 sym->attr.flavor = FL_PROCEDURE;
13646 if (sym->attr.dimension)
13647 sym->attr.function = 1;
13648 }
13649 }
13650
c73b6478
JW
13651 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
13652 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
13653
0e8d854e 13654 if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
524af0d6 13655 && !resolve_procedure_interface (sym))
2fcac97d 13656 return;
69773742 13657
c064bf1c
TB
13658 if (sym->attr.is_protected && !sym->attr.proc_pointer
13659 && (sym->attr.procedure || sym->attr.external))
13660 {
13661 if (sym->attr.external)
13662 gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
13663 "at %L", &sym->declared_at);
13664 else
13665 gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
13666 "at %L", &sym->declared_at);
13667
13668 return;
13669 }
13670
524af0d6 13671 if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
110eec24
TS
13672 return;
13673
6de9cd9a
DN
13674 /* Symbols that are module procedures with results (functions) have
13675 the types and array specification copied for type checking in
13676 procedures that call them, as well as for saving to a module
13677 file. These symbols can't stand the scrutiny that their results
13678 can. */
13679 mp_flag = (sym->result != NULL && sym->result != sym);
13680
4d382327
AF
13681 /* Make sure that the intrinsic is consistent with its internal
13682 representation. This needs to be done before assigning a default
eb2c598d 13683 type to avoid spurious warnings. */
f6038131 13684 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
524af0d6 13685 && !gfc_resolve_intrinsic (sym, &sym->declared_at))
f6038131 13686 return;
eb2c598d 13687
3e78238a 13688 /* Resolve associate names. */
03af1e4c 13689 if (sym->assoc)
3e78238a 13690 resolve_assoc_var (sym, true);
03af1e4c 13691
6de9cd9a
DN
13692 /* Assign default type to symbols that need one and don't have one. */
13693 if (sym->ts.type == BT_UNKNOWN)
13694 {
13695 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
fac665b2
TB
13696 {
13697 gfc_set_default_type (sym, 1, NULL);
13698 }
6de9cd9a 13699
fc9c6e5d
JW
13700 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
13701 && !sym->attr.function && !sym->attr.subroutine
13702 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
13703 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
13704
6de9cd9a
DN
13705 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
13706 {
53096259
PT
13707 /* The specific case of an external procedure should emit an error
13708 in the case that there is no implicit type. */
6de9cd9a 13709 if (!mp_flag)
53096259 13710 gfc_set_default_type (sym, sym->attr.external, NULL);
6de9cd9a
DN
13711 else
13712 {
edf1eac2 13713 /* Result may be in another namespace. */
6de9cd9a
DN
13714 resolve_symbol (sym->result);
13715
3070bab4
JW
13716 if (!sym->result->attr.proc_pointer)
13717 {
13718 sym->ts = sym->result->ts;
13719 sym->as = gfc_copy_array_spec (sym->result->as);
13720 sym->attr.dimension = sym->result->attr.dimension;
13721 sym->attr.pointer = sym->result->attr.pointer;
13722 sym->attr.allocatable = sym->result->attr.allocatable;
fe4e525c 13723 sym->attr.contiguous = sym->result->attr.contiguous;
3070bab4 13724 }
6de9cd9a
DN
13725 }
13726 }
13727 }
e3d748dd 13728 else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
fd061185
TB
13729 {
13730 bool saved_specification_expr = specification_expr;
13731 specification_expr = true;
13732 gfc_resolve_array_spec (sym->result->as, false);
13733 specification_expr = saved_specification_expr;
13734 }
6de9cd9a 13735
fac665b2
TB
13736 if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
13737 {
13738 as = CLASS_DATA (sym)->as;
13739 class_attr = CLASS_DATA (sym)->attr;
13740 class_attr.pointer = class_attr.class_pointer;
13741 }
13742 else
13743 {
13744 class_attr = sym->attr;
13745 as = sym->as;
13746 }
13747
1cc0e193 13748 /* F2008, C530. */
fac665b2
TB
13749 if (sym->attr.contiguous
13750 && (!class_attr.dimension
8e54f139
TB
13751 || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
13752 && !class_attr.pointer)))
fac665b2 13753 {
a4d9b221 13754 gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
8e54f139
TB
13755 "array pointer or an assumed-shape or assumed-rank array",
13756 sym->name, &sym->declared_at);
fac665b2
TB
13757 return;
13758 }
13759
f5e440e1 13760 /* Assumed size arrays and assumed shape arrays must be dummy
f5ca06e6
DK
13761 arguments. Array-spec's of implied-shape should have been resolved to
13762 AS_EXPLICIT already. */
f5e440e1 13763
fac665b2 13764 if (as)
6de9cd9a 13765 {
fac665b2
TB
13766 gcc_assert (as->type != AS_IMPLIED_SHAPE);
13767 if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
13768 || as->type == AS_ASSUMED_SHAPE)
4cc70466 13769 && !sym->attr.dummy && !sym->attr.select_type_temporary)
f5ca06e6 13770 {
fac665b2 13771 if (as->type == AS_ASSUMED_SIZE)
f5ca06e6
DK
13772 gfc_error ("Assumed size array at %L must be a dummy argument",
13773 &sym->declared_at);
13774 else
13775 gfc_error ("Assumed shape array at %L must be a dummy argument",
13776 &sym->declared_at);
13777 return;
13778 }
c62c6622 13779 /* TS 29113, C535a. */
4cc70466
PT
13780 if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
13781 && !sym->attr.select_type_temporary)
c62c6622
TB
13782 {
13783 gfc_error ("Assumed-rank array at %L must be a dummy argument",
13784 &sym->declared_at);
13785 return;
13786 }
13787 if (as->type == AS_ASSUMED_RANK
13788 && (sym->attr.codimension || sym->attr.value))
13789 {
13790 gfc_error ("Assumed-rank array at %L may not have the VALUE or "
13791 "CODIMENSION attribute", &sym->declared_at);
13792 return;
13793 }
a4ac5dd3
TS
13794 }
13795
6de9cd9a
DN
13796 /* Make sure symbols with known intent or optional are really dummy
13797 variable. Because of ENTRY statement, this has to be deferred
13798 until resolution time. */
13799
2ed8d224 13800 if (!sym->attr.dummy
edf1eac2 13801 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
6de9cd9a
DN
13802 {
13803 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
13804 return;
13805 }
13806
06469efd
PT
13807 if (sym->attr.value && !sym->attr.dummy)
13808 {
a4d9b221 13809 gfc_error ("%qs at %L cannot have the VALUE attribute because "
1084b6b0 13810 "it is not a dummy argument", sym->name, &sym->declared_at);
06469efd
PT
13811 return;
13812 }
13813
1084b6b0
TB
13814 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
13815 {
bc21d315 13816 gfc_charlen *cl = sym->ts.u.cl;
1084b6b0
TB
13817 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
13818 {
a4d9b221 13819 gfc_error ("Character dummy variable %qs at %L with VALUE "
1084b6b0
TB
13820 "attribute must have constant length",
13821 sym->name, &sym->declared_at);
13822 return;
13823 }
a8b3b0b6
CR
13824
13825 if (sym->ts.is_c_interop
13826 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
13827 {
a4d9b221 13828 gfc_error ("C interoperable character dummy variable %qs at %L "
a8b3b0b6
CR
13829 "with VALUE attribute must have length one",
13830 sym->name, &sym->declared_at);
13831 return;
13832 }
13833 }
13834
c3f34952
TB
13835 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
13836 && sym->ts.u.derived->attr.generic)
13837 {
13838 sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
13839 if (!sym->ts.u.derived)
13840 {
a4d9b221 13841 gfc_error ("The derived type %qs at %L is of type %qs, "
c3f34952
TB
13842 "which has not been defined", sym->name,
13843 &sym->declared_at, sym->ts.u.derived->name);
13844 sym->ts.type = BT_UNKNOWN;
13845 return;
13846 }
13847 }
13848
e7ac6a7c
TB
13849 /* Use the same constraints as TYPE(*), except for the type check
13850 and that only scalars and assumed-size arrays are permitted. */
13851 if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
13852 {
13853 if (!sym->attr.dummy)
13854 {
13855 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13856 "a dummy argument", sym->name, &sym->declared_at);
13857 return;
13858 }
13859
13860 if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
13861 && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
13862 && sym->ts.type != BT_COMPLEX)
13863 {
13864 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
13865 "of type TYPE(*) or of an numeric intrinsic type",
13866 sym->name, &sym->declared_at);
13867 return;
13868 }
13869
13870 if (sym->attr.allocatable || sym->attr.codimension
13871 || sym->attr.pointer || sym->attr.value)
13872 {
13873 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13874 "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
13875 "attribute", sym->name, &sym->declared_at);
13876 return;
13877 }
13878
13879 if (sym->attr.intent == INTENT_OUT)
13880 {
13881 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
13882 "have the INTENT(OUT) attribute",
13883 sym->name, &sym->declared_at);
13884 return;
13885 }
13886 if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
13887 {
13888 gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
13889 "either be a scalar or an assumed-size array",
13890 sym->name, &sym->declared_at);
13891 return;
13892 }
13893
13894 /* Set the type to TYPE(*) and add a dimension(*) to ensure
13895 NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
13896 packing. */
13897 sym->ts.type = BT_ASSUMED;
13898 sym->as = gfc_get_array_spec ();
13899 sym->as->type = AS_ASSUMED_SIZE;
13900 sym->as->rank = 1;
13901 sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
13902 }
13903 else if (sym->ts.type == BT_ASSUMED)
4d382327 13904 {
45a69325
TB
13905 /* TS 29113, C407a. */
13906 if (!sym->attr.dummy)
13907 {
13908 gfc_error ("Assumed type of variable %s at %L is only permitted "
13909 "for dummy variables", sym->name, &sym->declared_at);
13910 return;
13911 }
13912 if (sym->attr.allocatable || sym->attr.codimension
13913 || sym->attr.pointer || sym->attr.value)
13914 {
13915 gfc_error ("Assumed-type variable %s at %L may not have the "
13916 "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
13917 sym->name, &sym->declared_at);
13918 return;
13919 }
c62c6622
TB
13920 if (sym->attr.intent == INTENT_OUT)
13921 {
13922 gfc_error ("Assumed-type variable %s at %L may not have the "
13923 "INTENT(OUT) attribute",
13924 sym->name, &sym->declared_at);
13925 return;
13926 }
45a69325
TB
13927 if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
13928 {
13929 gfc_error ("Assumed-type variable %s at %L shall not be an "
13930 "explicit-shape array", sym->name, &sym->declared_at);
13931 return;
13932 }
13933 }
13934
a8b3b0b6
CR
13935 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
13936 do this for something that was implicitly typed because that is handled
13937 in gfc_set_default_type. Handle dummy arguments and procedure
13938 definitions separately. Also, anything that is use associated is not
13939 handled here but instead is handled in the module it is declared in.
13940 Finally, derived type definitions are allowed to be BIND(C) since that
13941 only implies that they're interoperable, and they are checked fully for
13942 interoperability when a variable is declared of that type. */
13943 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
13944 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
13945 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
13946 {
524af0d6 13947 bool t = true;
4d382327 13948
a8b3b0b6
CR
13949 /* First, make sure the variable is declared at the
13950 module-level scope (J3/04-007, Section 15.3). */
13951 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
13952 sym->attr.in_common == 0)
13953 {
c4100eae 13954 gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
a8b3b0b6
CR
13955 "is neither a COMMON block nor declared at the "
13956 "module level scope", sym->name, &(sym->declared_at));
524af0d6 13957 t = false;
a8b3b0b6
CR
13958 }
13959 else if (sym->common_head != NULL)
13960 {
13961 t = verify_com_block_vars_c_interop (sym->common_head);
13962 }
13963 else
13964 {
13965 /* If type() declaration, we need to verify that the components
13966 of the given type are all C interoperable, etc. */
13967 if (sym->ts.type == BT_DERIVED &&
bc21d315 13968 sym->ts.u.derived->attr.is_c_interop != 1)
a8b3b0b6
CR
13969 {
13970 /* Make sure the user marked the derived type as BIND(C). If
13971 not, call the verify routine. This could print an error
13972 for the derived type more than once if multiple variables
13973 of that type are declared. */
bc21d315
JW
13974 if (sym->ts.u.derived->attr.is_bind_c != 1)
13975 verify_bind_c_derived_type (sym->ts.u.derived);
524af0d6 13976 t = false;
a8b3b0b6 13977 }
4d382327 13978
a8b3b0b6
CR
13979 /* Verify the variable itself as C interoperable if it
13980 is BIND(C). It is not possible for this to succeed if
13981 the verify_bind_c_derived_type failed, so don't have to handle
13982 any error returned by verify_bind_c_derived_type. */
13983 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
13984 sym->common_block);
13985 }
13986
524af0d6 13987 if (!t)
a8b3b0b6
CR
13988 {
13989 /* clear the is_bind_c flag to prevent reporting errors more than
13990 once if something failed. */
13991 sym->attr.is_bind_c = 0;
13992 return;
13993 }
1084b6b0
TB
13994 }
13995
976e21f6
PT
13996 /* If a derived type symbol has reached this point, without its
13997 type being declared, we have an error. Notice that most
13998 conditions that produce undefined derived types have already
13999 been dealt with. However, the likes of:
14000 implicit type(t) (t) ..... call foo (t) will get us here if
14001 the type is not declared in the scope of the implicit
14002 statement. Change the type to BT_UNKNOWN, both because it is so
14003 and to prevent an ICE. */
c3f34952
TB
14004 if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
14005 && sym->ts.u.derived->components == NULL
bc21d315 14006 && !sym->ts.u.derived->attr.zero_comp)
976e21f6 14007 {
a4d9b221 14008 gfc_error ("The derived type %qs at %L is of type %qs, "
e25a0da3 14009 "which has not been defined", sym->name,
bc21d315 14010 &sym->declared_at, sym->ts.u.derived->name);
976e21f6
PT
14011 sym->ts.type = BT_UNKNOWN;
14012 return;
14013 }
14014
c1203a70
PT
14015 /* Make sure that the derived type has been resolved and that the
14016 derived type is visible in the symbol's namespace, if it is a
14017 module function and is not PRIVATE. */
14018 if (sym->ts.type == BT_DERIVED
bc21d315 14019 && sym->ts.u.derived->attr.use_assoc
96ffc6cd 14020 && sym->ns->proc_name
c3f34952 14021 && sym->ns->proc_name->attr.flavor == FL_MODULE
524af0d6 14022 && !resolve_fl_derived (sym->ts.u.derived))
c3f34952 14023 return;
c1203a70 14024
a08a5751
TB
14025 /* Unless the derived-type declaration is use associated, Fortran 95
14026 does not allow public entries of private derived types.
14027 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
14028 161 in 95-006r3. */
14029 if (sym->ts.type == BT_DERIVED
72052237 14030 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
bc21d315 14031 && !sym->ts.u.derived->attr.use_assoc
6e2062b0
JW
14032 && gfc_check_symbol_access (sym)
14033 && !gfc_check_symbol_access (sym->ts.u.derived)
a4d9b221
TB
14034 && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
14035 "derived type %qs",
22c23886
PT
14036 (sym->attr.flavor == FL_PARAMETER)
14037 ? "parameter" : "variable",
14038 sym->name, &sym->declared_at,
524af0d6 14039 sym->ts.u.derived->name))
a08a5751
TB
14040 return;
14041
fea54935
TB
14042 /* F2008, C1302. */
14043 if (sym->ts.type == BT_DERIVED
3b6fa7a5
TB
14044 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14045 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
14046 || sym->ts.u.derived->attr.lock_comp)
14047 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
fea54935 14048 {
3b6fa7a5
TB
14049 gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
14050 "type LOCK_TYPE must be a coarray", sym->name,
14051 &sym->declared_at);
fea54935
TB
14052 return;
14053 }
14054
5df445a2
TB
14055 /* TS18508, C702/C703. */
14056 if (sym->ts.type == BT_DERIVED
14057 && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
14058 && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
14059 || sym->ts.u.derived->attr.event_comp)
14060 && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
14061 {
14062 gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
14063 "type LOCK_TYPE must be a coarray", sym->name,
14064 &sym->declared_at);
14065 return;
14066 }
14067
4213f93b
PT
14068 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
14069 default initialization is defined (5.1.2.4.4). */
14070 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
14071 && sym->attr.dummy
14072 && sym->attr.intent == INTENT_OUT
14073 && sym->as
14074 && sym->as->type == AS_ASSUMED_SIZE)
4213f93b 14075 {
bc21d315 14076 for (c = sym->ts.u.derived->components; c; c = c->next)
4213f93b
PT
14077 {
14078 if (c->initializer)
14079 {
a4d9b221 14080 gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
4213f93b
PT
14081 "ASSUMED SIZE and so cannot have a default initializer",
14082 sym->name, &sym->declared_at);
14083 return;
14084 }
14085 }
14086 }
14087
fea54935
TB
14088 /* F2008, C542. */
14089 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14090 && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
e535f1b2 14091 {
a4d9b221 14092 gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
e535f1b2
TB
14093 "INTENT(OUT)", sym->name, &sym->declared_at);
14094 return;
14095 }
fea54935 14096
5df445a2
TB
14097 /* TS18508. */
14098 if (sym->ts.type == BT_DERIVED && sym->attr.dummy
14099 && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
14100 {
14101 gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
14102 "INTENT(OUT)", sym->name, &sym->declared_at);
14103 return;
14104 }
14105
e535f1b2 14106 /* F2008, C525. */
fac665b2
TB
14107 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14108 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14109 && CLASS_DATA (sym)->attr.coarray_comp))
14110 || class_attr.codimension)
e535f1b2
TB
14111 && (sym->attr.result || sym->result == sym))
14112 {
a4d9b221 14113 gfc_error ("Function result %qs at %L shall not be a coarray or have "
e535f1b2
TB
14114 "a coarray component", sym->name, &sym->declared_at);
14115 return;
14116 }
be59db2d
TB
14117
14118 /* F2008, C524. */
14119 if (sym->attr.codimension && sym->ts.type == BT_DERIVED
14120 && sym->ts.u.derived->ts.is_iso_c)
e535f1b2 14121 {
a4d9b221 14122 gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
e535f1b2
TB
14123 "shall not be a coarray", sym->name, &sym->declared_at);
14124 return;
14125 }
be59db2d
TB
14126
14127 /* F2008, C525. */
fac665b2
TB
14128 if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14129 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14130 && CLASS_DATA (sym)->attr.coarray_comp))
14131 && (class_attr.codimension || class_attr.pointer || class_attr.dimension
14132 || class_attr.allocatable))
e535f1b2 14133 {
a4d9b221 14134 gfc_error ("Variable %qs at %L with coarray component shall be a "
abc2d807 14135 "nonpointer, nonallocatable scalar, which is not a coarray",
e535f1b2
TB
14136 sym->name, &sym->declared_at);
14137 return;
14138 }
be59db2d
TB
14139
14140 /* F2008, C526. The function-result case was handled above. */
fac665b2
TB
14141 if (class_attr.codimension
14142 && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
14143 || sym->attr.select_type_temporary
9f3761c5 14144 || sym->ns->save_all
be59db2d
TB
14145 || sym->ns->proc_name->attr.flavor == FL_MODULE
14146 || sym->ns->proc_name->attr.is_main_program
14147 || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
e535f1b2 14148 {
a4d9b221 14149 gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
e535f1b2
TB
14150 "nor a dummy argument", sym->name, &sym->declared_at);
14151 return;
14152 }
fac665b2
TB
14153 /* F2008, C528. */
14154 else if (class_attr.codimension && !sym->attr.select_type_temporary
14155 && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
e535f1b2 14156 {
a4d9b221 14157 gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
e535f1b2
TB
14158 "deferred shape", sym->name, &sym->declared_at);
14159 return;
14160 }
fac665b2
TB
14161 else if (class_attr.codimension && class_attr.allocatable && as
14162 && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
e535f1b2 14163 {
a4d9b221 14164 gfc_error ("Allocatable coarray variable %qs at %L must have "
e535f1b2
TB
14165 "deferred shape", sym->name, &sym->declared_at);
14166 return;
14167 }
be59db2d
TB
14168
14169 /* F2008, C541. */
fac665b2
TB
14170 if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
14171 || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14172 && CLASS_DATA (sym)->attr.coarray_comp))
14173 || (class_attr.codimension && class_attr.allocatable))
be59db2d 14174 && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
e535f1b2 14175 {
a4d9b221 14176 gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
e535f1b2
TB
14177 "allocatable coarray or have coarray components",
14178 sym->name, &sym->declared_at);
14179 return;
14180 }
be59db2d 14181
fac665b2 14182 if (class_attr.codimension && sym->attr.dummy
be59db2d 14183 && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
e535f1b2 14184 {
a4d9b221
TB
14185 gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
14186 "procedure %qs", sym->name, &sym->declared_at,
e535f1b2
TB
14187 sym->ns->proc_name->name);
14188 return;
14189 }
be59db2d 14190
d0841b5b
TB
14191 if (sym->ts.type == BT_LOGICAL
14192 && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
14193 || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
14194 && sym->ns->proc_name->attr.is_bind_c)))
14195 {
14196 int i;
14197 for (i = 0; gfc_logical_kinds[i].kind; i++)
14198 if (gfc_logical_kinds[i].kind == sym->ts.kind)
14199 break;
14200 if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
a4d9b221 14201 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
524af0d6 14202 "%L with non-C_Bool kind in BIND(C) procedure "
a4d9b221 14203 "%qs", sym->name, &sym->declared_at,
524af0d6 14204 sym->ns->proc_name->name))
d0841b5b
TB
14205 return;
14206 else if (!gfc_logical_kinds[i].c_bool
524af0d6 14207 && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
a4d9b221
TB
14208 "%qs at %L with non-C_Bool kind in "
14209 "BIND(C) procedure %qs", sym->name,
22c23886
PT
14210 &sym->declared_at,
14211 sym->attr.function ? sym->name
524af0d6 14212 : sym->ns->proc_name->name))
d0841b5b
TB
14213 return;
14214 }
14215
af30f793 14216 switch (sym->attr.flavor)
54b4ba60 14217 {
af30f793 14218 case FL_VARIABLE:
524af0d6 14219 if (!resolve_fl_variable (sym, mp_flag))
2ed8d224
PT
14220 return;
14221 break;
219fa8c3 14222
2ed8d224 14223 case FL_PROCEDURE:
524af0d6 14224 if (!resolve_fl_procedure (sym, mp_flag))
2ed8d224 14225 return;
af30f793
PB
14226 break;
14227
14228 case FL_NAMELIST:
524af0d6 14229 if (!resolve_fl_namelist (sym))
3e1cf500 14230 return;
68ea355b
PT
14231 break;
14232
2ed8d224 14233 case FL_PARAMETER:
524af0d6 14234 if (!resolve_fl_parameter (sym))
2ed8d224 14235 return;
e0e85e06
PT
14236 break;
14237
af30f793
PB
14238 default:
14239 break;
54b4ba60
PB
14240 }
14241
6de9cd9a 14242 /* Resolve array specifier. Check as well some constraints
f7b529fa 14243 on COMMON blocks. */
6de9cd9a
DN
14244
14245 check_constant = sym->attr.in_common && !sym->attr.pointer;
98bbe5ee
PT
14246
14247 /* Set the formal_arg_flag so that check_conflict will not throw
14248 an error for host associated variables in the specification
14249 expression for an array_valued function. */
14250 if (sym->attr.function && sym->as)
14251 formal_arg_flag = 1;
14252
fd061185
TB
14253 saved_specification_expr = specification_expr;
14254 specification_expr = true;
6de9cd9a 14255 gfc_resolve_array_spec (sym->as, check_constant);
fd061185 14256 specification_expr = saved_specification_expr;
6de9cd9a 14257
98bbe5ee
PT
14258 formal_arg_flag = 0;
14259
a34437a1 14260 /* Resolve formal namespaces. */
f6ddbf11 14261 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
e4c1aa19 14262 && !sym->attr.contained && !sym->attr.intrinsic)
a34437a1 14263 gfc_resolve (sym->formal_ns);
6c7a4dfd 14264
acbdc378
JW
14265 /* Make sure the formal namespace is present. */
14266 if (sym->formal && !sym->formal_ns)
14267 {
14268 gfc_formal_arglist *formal = sym->formal;
14269 while (formal && !formal->sym)
14270 formal = formal->next;
14271
14272 if (formal)
14273 {
14274 sym->formal_ns = formal->sym->ns;
6f79f4d1
TB
14275 if (sym->ns != formal->sym->ns)
14276 sym->formal_ns->refs++;
acbdc378
JW
14277 }
14278 }
14279
6c7a4dfd 14280 /* Check threadprivate restrictions. */
5349080d 14281 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
6c7a4dfd 14282 && (!sym->attr.in_common
edf1eac2
SK
14283 && sym->module == NULL
14284 && (sym->ns->proc_name == NULL
14285 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
6c7a4dfd 14286 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
6b591ec0 14287
f014c653
JJ
14288 /* Check omp declare target restrictions. */
14289 if (sym->attr.omp_declare_target
14290 && sym->attr.flavor == FL_VARIABLE
14291 && !sym->attr.save
14292 && !sym->ns->save_all
14293 && (!sym->attr.in_common
14294 && sym->module == NULL
14295 && (sym->ns->proc_name == NULL
14296 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
a4d9b221 14297 gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
f014c653
JJ
14298 sym->name, &sym->declared_at);
14299
6b591ec0
PT
14300 /* If we have come this far we can apply default-initializers, as
14301 described in 14.7.5, to those variables that have not already
14302 been assigned one. */
7114edca 14303 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
14304 && !sym->value
14305 && !sym->attr.allocatable
14306 && !sym->attr.alloc_comp)
6b591ec0
PT
14307 {
14308 symbol_attribute *a = &sym->attr;
14309
14310 if ((!a->save && !a->dummy && !a->pointer
edf1eac2 14311 && !a->in_common && !a->use_assoc
c16126ac 14312 && !a->result && !a->function)
758e12af 14313 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
6b591ec0 14314 apply_default_init (sym);
c16126ac
AV
14315 else if (a->function && sym->result && a->access != ACCESS_PRIVATE
14316 && (sym->ts.u.derived->attr.alloc_comp
14317 || sym->ts.u.derived->attr.pointer_comp))
14318 /* Mark the result symbol to be referenced, when it has allocatable
14319 components. */
14320 sym->result->attr.referenced = 1;
6b591ec0 14321 }
52f49934 14322
50f30801
JW
14323 if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
14324 && sym->attr.dummy && sym->attr.intent == INTENT_OUT
c330d181
JW
14325 && !CLASS_DATA (sym)->attr.class_pointer
14326 && !CLASS_DATA (sym)->attr.allocatable)
86e6a239 14327 apply_default_init (sym);
50f30801 14328
52f49934
DK
14329 /* If this symbol has a type-spec, check it. */
14330 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
14331 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
524af0d6 14332 if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
52f49934 14333 return;
6de9cd9a
DN
14334}
14335
14336
6de9cd9a
DN
14337/************* Resolve DATA statements *************/
14338
14339static struct
14340{
14341 gfc_data_value *vnode;
f2112868 14342 mpz_t left;
6de9cd9a
DN
14343}
14344values;
14345
14346
14347/* Advance the values structure to point to the next value in the data list. */
14348
524af0d6 14349static bool
6de9cd9a
DN
14350next_data_value (void)
14351{
f2112868 14352 while (mpz_cmp_ui (values.left, 0) == 0)
6de9cd9a 14353 {
abeab938 14354
6de9cd9a 14355 if (values.vnode->next == NULL)
524af0d6 14356 return false;
6de9cd9a
DN
14357
14358 values.vnode = values.vnode->next;
f2112868 14359 mpz_set (values.left, values.vnode->repeat);
6de9cd9a
DN
14360 }
14361
524af0d6 14362 return true;
6de9cd9a
DN
14363}
14364
14365
524af0d6 14366static bool
edf1eac2 14367check_data_variable (gfc_data_variable *var, locus *where)
6de9cd9a
DN
14368{
14369 gfc_expr *e;
14370 mpz_t size;
14371 mpz_t offset;
524af0d6 14372 bool t;
f5e440e1 14373 ar_type mark = AR_UNKNOWN;
6de9cd9a
DN
14374 int i;
14375 mpz_t section_index[GFC_MAX_DIMENSIONS];
14376 gfc_ref *ref;
14377 gfc_array_ref *ar;
e49be8f7
PT
14378 gfc_symbol *sym;
14379 int has_pointer;
6de9cd9a 14380
524af0d6
JB
14381 if (!gfc_resolve_expr (var->expr))
14382 return false;
6de9cd9a
DN
14383
14384 ar = NULL;
14385 mpz_init_set_si (offset, 0);
14386 e = var->expr;
14387
14388 if (e->expr_type != EXPR_VARIABLE)
14389 gfc_internal_error ("check_data_variable(): Bad expression");
14390
e49be8f7
PT
14391 sym = e->symtree->n.sym;
14392
14393 if (sym->ns->is_block_data && !sym->attr.in_common)
2ed8d224 14394 {
a4d9b221 14395 gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
e49be8f7 14396 sym->name, &sym->declared_at);
2ed8d224
PT
14397 }
14398
e49be8f7 14399 if (e->ref == NULL && sym->as)
f1607c01 14400 {
a4d9b221 14401 gfc_error ("DATA array %qs at %L must be specified in a previous"
e49be8f7 14402 " declaration", sym->name, where);
524af0d6 14403 return false;
f1607c01
JD
14404 }
14405
e49be8f7
PT
14406 has_pointer = sym->attr.pointer;
14407
a3935ffc
TB
14408 if (gfc_is_coindexed (e))
14409 {
a4d9b221 14410 gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
a3935ffc 14411 where);
524af0d6 14412 return false;
a3935ffc
TB
14413 }
14414
e49be8f7
PT
14415 for (ref = e->ref; ref; ref = ref->next)
14416 {
14417 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
14418 has_pointer = 1;
14419
14420 if (has_pointer
14421 && ref->type == REF_ARRAY
14422 && ref->u.ar.type != AR_FULL)
14423 {
a4d9b221 14424 gfc_error ("DATA element %qs at %L is a pointer and so must "
e49be8f7 14425 "be a full array", sym->name, where);
524af0d6 14426 return false;
e49be8f7
PT
14427 }
14428 }
14429
14430 if (e->rank == 0 || has_pointer)
b8502435
RH
14431 {
14432 mpz_init_set_ui (size, 1);
14433 ref = NULL;
14434 }
6de9cd9a
DN
14435 else
14436 {
14437 ref = e->ref;
14438
14439 /* Find the array section reference. */
14440 for (ref = e->ref; ref; ref = ref->next)
14441 {
14442 if (ref->type != REF_ARRAY)
14443 continue;
14444 if (ref->u.ar.type == AR_ELEMENT)
14445 continue;
14446 break;
14447 }
6e45f57b 14448 gcc_assert (ref);
6de9cd9a 14449
1f2959f0 14450 /* Set marks according to the reference pattern. */
6de9cd9a
DN
14451 switch (ref->u.ar.type)
14452 {
14453 case AR_FULL:
f5e440e1 14454 mark = AR_FULL;
6de9cd9a
DN
14455 break;
14456
14457 case AR_SECTION:
edf1eac2
SK
14458 ar = &ref->u.ar;
14459 /* Get the start position of array section. */
14460 gfc_get_section_index (ar, section_index, &offset);
14461 mark = AR_SECTION;
6de9cd9a
DN
14462 break;
14463
14464 default:
6e45f57b 14465 gcc_unreachable ();
6de9cd9a
DN
14466 }
14467
524af0d6 14468 if (!gfc_array_size (e, &size))
6de9cd9a
DN
14469 {
14470 gfc_error ("Nonconstant array section at %L in DATA statement",
14471 &e->where);
14472 mpz_clear (offset);
524af0d6 14473 return false;
6de9cd9a
DN
14474 }
14475 }
14476
524af0d6 14477 t = true;
6de9cd9a
DN
14478
14479 while (mpz_cmp_ui (size, 0) > 0)
14480 {
524af0d6 14481 if (!next_data_value ())
6de9cd9a
DN
14482 {
14483 gfc_error ("DATA statement at %L has more variables than values",
14484 where);
524af0d6 14485 t = false;
6de9cd9a
DN
14486 break;
14487 }
14488
14489 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
524af0d6 14490 if (!t)
6de9cd9a
DN
14491 break;
14492
b8502435
RH
14493 /* If we have more than one element left in the repeat count,
14494 and we have more than one element left in the target variable,
14495 then create a range assignment. */
f2112868 14496 /* FIXME: Only done for full arrays for now, since array sections
b8502435
RH
14497 seem tricky. */
14498 if (mark == AR_FULL && ref && ref->next == NULL
f2112868 14499 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
b8502435
RH
14500 {
14501 mpz_t range;
14502
f2112868 14503 if (mpz_cmp (size, values.left) >= 0)
b8502435 14504 {
f2112868
SK
14505 mpz_init_set (range, values.left);
14506 mpz_sub (size, size, values.left);
14507 mpz_set_ui (values.left, 0);
b8502435
RH
14508 }
14509 else
14510 {
14511 mpz_init_set (range, size);
f2112868 14512 mpz_sub (values.left, values.left, size);
b8502435
RH
14513 mpz_set_ui (size, 0);
14514 }
14515
21ea4922
JJ
14516 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14517 offset, &range);
b8502435
RH
14518
14519 mpz_add (offset, offset, range);
14520 mpz_clear (range);
e5880243 14521
524af0d6 14522 if (!t)
e5880243 14523 break;
b8502435
RH
14524 }
14525
6de9cd9a 14526 /* Assign initial value to symbol. */
b8502435
RH
14527 else
14528 {
f2112868 14529 mpz_sub_ui (values.left, values.left, 1);
b8502435 14530 mpz_sub_ui (size, size, 1);
6de9cd9a 14531
21ea4922
JJ
14532 t = gfc_assign_data_value (var->expr, values.vnode->expr,
14533 offset, NULL);
524af0d6 14534 if (!t)
a24668a3 14535 break;
6de9cd9a 14536
b8502435
RH
14537 if (mark == AR_FULL)
14538 mpz_add_ui (offset, offset, 1);
6de9cd9a 14539
b8502435
RH
14540 /* Modify the array section indexes and recalculate the offset
14541 for next element. */
14542 else if (mark == AR_SECTION)
14543 gfc_advance_section (section_index, ar, &offset);
14544 }
6de9cd9a 14545 }
b8502435 14546
f5e440e1 14547 if (mark == AR_SECTION)
6de9cd9a
DN
14548 {
14549 for (i = 0; i < ar->dimen; i++)
edf1eac2 14550 mpz_clear (section_index[i]);
6de9cd9a
DN
14551 }
14552
14553 mpz_clear (size);
14554 mpz_clear (offset);
14555
14556 return t;
14557}
14558
14559
524af0d6 14560static bool traverse_data_var (gfc_data_variable *, locus *);
6de9cd9a
DN
14561
14562/* Iterate over a list of elements in a DATA statement. */
14563
524af0d6 14564static bool
edf1eac2 14565traverse_data_list (gfc_data_variable *var, locus *where)
6de9cd9a
DN
14566{
14567 mpz_t trip;
14568 iterator_stack frame;
2220652d 14569 gfc_expr *e, *start, *end, *step;
524af0d6 14570 bool retval = true;
6de9cd9a
DN
14571
14572 mpz_init (frame.value);
147a19a9 14573 mpz_init (trip);
6de9cd9a 14574
2220652d
PT
14575 start = gfc_copy_expr (var->iter.start);
14576 end = gfc_copy_expr (var->iter.end);
14577 step = gfc_copy_expr (var->iter.step);
14578
524af0d6 14579 if (!gfc_simplify_expr (start, 1)
edf1eac2 14580 || start->expr_type != EXPR_CONSTANT)
2220652d 14581 {
147a19a9
DF
14582 gfc_error ("start of implied-do loop at %L could not be "
14583 "simplified to a constant value", &start->where);
524af0d6 14584 retval = false;
2220652d
PT
14585 goto cleanup;
14586 }
524af0d6 14587 if (!gfc_simplify_expr (end, 1)
edf1eac2 14588 || end->expr_type != EXPR_CONSTANT)
2220652d 14589 {
147a19a9
DF
14590 gfc_error ("end of implied-do loop at %L could not be "
14591 "simplified to a constant value", &start->where);
524af0d6 14592 retval = false;
2220652d
PT
14593 goto cleanup;
14594 }
524af0d6 14595 if (!gfc_simplify_expr (step, 1)
edf1eac2 14596 || step->expr_type != EXPR_CONSTANT)
2220652d 14597 {
147a19a9
DF
14598 gfc_error ("step of implied-do loop at %L could not be "
14599 "simplified to a constant value", &start->where);
524af0d6 14600 retval = false;
2220652d
PT
14601 goto cleanup;
14602 }
14603
147a19a9 14604 mpz_set (trip, end->value.integer);
2220652d
PT
14605 mpz_sub (trip, trip, start->value.integer);
14606 mpz_add (trip, trip, step->value.integer);
6de9cd9a 14607
2220652d 14608 mpz_div (trip, trip, step->value.integer);
6de9cd9a 14609
2220652d 14610 mpz_set (frame.value, start->value.integer);
6de9cd9a
DN
14611
14612 frame.prev = iter_stack;
14613 frame.variable = var->iter.var->symtree;
14614 iter_stack = &frame;
14615
14616 while (mpz_cmp_ui (trip, 0) > 0)
14617 {
524af0d6 14618 if (!traverse_data_var (var->list, where))
6de9cd9a 14619 {
524af0d6 14620 retval = false;
2220652d 14621 goto cleanup;
6de9cd9a
DN
14622 }
14623
14624 e = gfc_copy_expr (var->expr);
524af0d6 14625 if (!gfc_simplify_expr (e, 1))
2220652d
PT
14626 {
14627 gfc_free_expr (e);
524af0d6 14628 retval = false;
2220652d
PT
14629 goto cleanup;
14630 }
6de9cd9a 14631
2220652d 14632 mpz_add (frame.value, frame.value, step->value.integer);
6de9cd9a
DN
14633
14634 mpz_sub_ui (trip, trip, 1);
14635 }
14636
2220652d 14637cleanup:
6de9cd9a 14638 mpz_clear (frame.value);
147a19a9 14639 mpz_clear (trip);
6de9cd9a 14640
2220652d
PT
14641 gfc_free_expr (start);
14642 gfc_free_expr (end);
14643 gfc_free_expr (step);
14644
6de9cd9a 14645 iter_stack = frame.prev;
2220652d 14646 return retval;
6de9cd9a
DN
14647}
14648
14649
14650/* Type resolve variables in the variable list of a DATA statement. */
14651
524af0d6 14652static bool
edf1eac2 14653traverse_data_var (gfc_data_variable *var, locus *where)
6de9cd9a 14654{
524af0d6 14655 bool t;
6de9cd9a
DN
14656
14657 for (; var; var = var->next)
14658 {
14659 if (var->expr == NULL)
14660 t = traverse_data_list (var, where);
14661 else
14662 t = check_data_variable (var, where);
14663
524af0d6
JB
14664 if (!t)
14665 return false;
6de9cd9a
DN
14666 }
14667
524af0d6 14668 return true;
6de9cd9a
DN
14669}
14670
14671
14672/* Resolve the expressions and iterators associated with a data statement.
14673 This is separate from the assignment checking because data lists should
14674 only be resolved once. */
14675
524af0d6 14676static bool
edf1eac2 14677resolve_data_variables (gfc_data_variable *d)
6de9cd9a 14678{
6de9cd9a
DN
14679 for (; d; d = d->next)
14680 {
14681 if (d->list == NULL)
14682 {
524af0d6
JB
14683 if (!gfc_resolve_expr (d->expr))
14684 return false;
6de9cd9a
DN
14685 }
14686 else
14687 {
524af0d6
JB
14688 if (!gfc_resolve_iterator (&d->iter, false, true))
14689 return false;
6de9cd9a 14690
524af0d6
JB
14691 if (!resolve_data_variables (d->list))
14692 return false;
6de9cd9a
DN
14693 }
14694 }
14695
524af0d6 14696 return true;
6de9cd9a
DN
14697}
14698
14699
14700/* Resolve a single DATA statement. We implement this by storing a pointer to
14701 the value list into static variables, and then recursively traversing the
14702 variables list, expanding iterators and such. */
14703
14704static void
f2112868 14705resolve_data (gfc_data *d)
6de9cd9a 14706{
f2112868 14707
524af0d6 14708 if (!resolve_data_variables (d->var))
6de9cd9a
DN
14709 return;
14710
14711 values.vnode = d->value;
f2112868
SK
14712 if (d->value == NULL)
14713 mpz_set_ui (values.left, 0);
14714 else
14715 mpz_set (values.left, d->value->repeat);
6de9cd9a 14716
524af0d6 14717 if (!traverse_data_var (d->var, &d->where))
6de9cd9a
DN
14718 return;
14719
14720 /* At this point, we better not have any values left. */
14721
524af0d6 14722 if (next_data_value ())
6de9cd9a
DN
14723 gfc_error ("DATA statement at %L has more values than variables",
14724 &d->where);
14725}
14726
14727
d2088bb6
PT
14728/* 12.6 Constraint: In a pure subprogram any variable which is in common or
14729 accessed by host or use association, is a dummy argument to a pure function,
14730 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
14731 is storage associated with any such variable, shall not be used in the
14732 following contexts: (clients of this function). */
14733
df2fba9e 14734/* Determines if a variable is not 'pure', i.e., not assignable within a pure
edf1eac2
SK
14735 procedure. Returns zero if assignment is OK, nonzero if there is a
14736 problem. */
6de9cd9a 14737int
edf1eac2 14738gfc_impure_variable (gfc_symbol *sym)
6de9cd9a 14739{
d2088bb6 14740 gfc_symbol *proc;
d1039125 14741 gfc_namespace *ns;
d2088bb6 14742
6de9cd9a
DN
14743 if (sym->attr.use_assoc || sym->attr.in_common)
14744 return 1;
14745
d1039125
JW
14746 /* Check if the symbol's ns is inside the pure procedure. */
14747 for (ns = gfc_current_ns; ns; ns = ns->parent)
14748 {
14749 if (ns == sym->ns)
14750 break;
14751 if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
14752 return 1;
14753 }
6de9cd9a 14754
d2088bb6 14755 proc = sym->ns->proc_name;
c915f8bc
TB
14756 if (sym->attr.dummy
14757 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
14758 || proc->attr.function))
d2088bb6 14759 return 1;
6de9cd9a 14760
d2088bb6
PT
14761 /* TODO: Sort out what can be storage associated, if anything, and include
14762 it here. In principle equivalences should be scanned but it does not
14763 seem to be possible to storage associate an impure variable this way. */
6de9cd9a
DN
14764 return 0;
14765}
14766
14767
d1039125
JW
14768/* Test whether a symbol is pure or not. For a NULL pointer, checks if the
14769 current namespace is inside a pure procedure. */
6de9cd9a
DN
14770
14771int
edf1eac2 14772gfc_pure (gfc_symbol *sym)
6de9cd9a
DN
14773{
14774 symbol_attribute attr;
d1039125 14775 gfc_namespace *ns;
6de9cd9a
DN
14776
14777 if (sym == NULL)
d1039125
JW
14778 {
14779 /* Check if the current namespace or one of its parents
14780 belongs to a pure procedure. */
14781 for (ns = gfc_current_ns; ns; ns = ns->parent)
14782 {
14783 sym = ns->proc_name;
14784 if (sym == NULL)
14785 return 0;
14786 attr = sym->attr;
e6c14898 14787 if (attr.flavor == FL_PROCEDURE && attr.pure)
d1039125
JW
14788 return 1;
14789 }
14790 return 0;
14791 }
6de9cd9a
DN
14792
14793 attr = sym->attr;
14794
e6c14898 14795 return attr.flavor == FL_PROCEDURE && attr.pure;
6de9cd9a
DN
14796}
14797
14798
f1f39033
PT
14799/* Test whether a symbol is implicitly pure or not. For a NULL pointer,
14800 checks if the current namespace is implicitly pure. Note that this
14801 function returns false for a PURE procedure. */
14802
14803int
14804gfc_implicit_pure (gfc_symbol *sym)
14805{
f29041dd 14806 gfc_namespace *ns;
f1f39033
PT
14807
14808 if (sym == NULL)
14809 {
f29041dd
TK
14810 /* Check if the current procedure is implicit_pure. Walk up
14811 the procedure list until we find a procedure. */
14812 for (ns = gfc_current_ns; ns; ns = ns->parent)
14813 {
14814 sym = ns->proc_name;
14815 if (sym == NULL)
14816 return 0;
4d382327 14817
f29041dd
TK
14818 if (sym->attr.flavor == FL_PROCEDURE)
14819 break;
14820 }
f1f39033 14821 }
4d382327 14822
f29041dd
TK
14823 return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
14824 && !sym->attr.pure;
f1f39033
PT
14825}
14826
14827
ccd7751b
TB
14828void
14829gfc_unset_implicit_pure (gfc_symbol *sym)
14830{
14831 gfc_namespace *ns;
14832
14833 if (sym == NULL)
14834 {
14835 /* Check if the current procedure is implicit_pure. Walk up
14836 the procedure list until we find a procedure. */
14837 for (ns = gfc_current_ns; ns; ns = ns->parent)
14838 {
14839 sym = ns->proc_name;
14840 if (sym == NULL)
14841 return;
14842
14843 if (sym->attr.flavor == FL_PROCEDURE)
14844 break;
14845 }
14846 }
14847
14848 if (sym->attr.flavor == FL_PROCEDURE)
14849 sym->attr.implicit_pure = 0;
14850 else
14851 sym->attr.pure = 0;
14852}
14853
14854
6de9cd9a
DN
14855/* Test whether the current procedure is elemental or not. */
14856
14857int
edf1eac2 14858gfc_elemental (gfc_symbol *sym)
6de9cd9a
DN
14859{
14860 symbol_attribute attr;
14861
14862 if (sym == NULL)
14863 sym = gfc_current_ns->proc_name;
14864 if (sym == NULL)
14865 return 0;
14866 attr = sym->attr;
14867
14868 return attr.flavor == FL_PROCEDURE && attr.elemental;
14869}
14870
14871
14872/* Warn about unused labels. */
14873
14874static void
edf1eac2 14875warn_unused_fortran_label (gfc_st_label *label)
6de9cd9a 14876{
5cf54585 14877 if (label == NULL)
6de9cd9a
DN
14878 return;
14879
994c1cc0 14880 warn_unused_fortran_label (label->left);
6de9cd9a 14881
5cf54585
TS
14882 if (label->defined == ST_LABEL_UNKNOWN)
14883 return;
6de9cd9a 14884
5cf54585
TS
14885 switch (label->referenced)
14886 {
14887 case ST_LABEL_UNKNOWN:
db30e21c 14888 gfc_warning (0, "Label %d at %L defined but not used", label->value,
5cf54585
TS
14889 &label->where);
14890 break;
6de9cd9a 14891
5cf54585 14892 case ST_LABEL_BAD_TARGET:
db30e21c 14893 gfc_warning (0, "Label %d at %L defined but cannot be used",
5cf54585
TS
14894 label->value, &label->where);
14895 break;
6de9cd9a 14896
5cf54585
TS
14897 default:
14898 break;
6de9cd9a 14899 }
5cf54585 14900
994c1cc0 14901 warn_unused_fortran_label (label->right);
6de9cd9a
DN
14902}
14903
14904
e8ec07e1
PT
14905/* Returns the sequence type of a symbol or sequence. */
14906
14907static seq_type
14908sequence_type (gfc_typespec ts)
14909{
14910 seq_type result;
14911 gfc_component *c;
14912
14913 switch (ts.type)
14914 {
14915 case BT_DERIVED:
14916
bc21d315 14917 if (ts.u.derived->components == NULL)
e8ec07e1
PT
14918 return SEQ_NONDEFAULT;
14919
bc21d315
JW
14920 result = sequence_type (ts.u.derived->components->ts);
14921 for (c = ts.u.derived->components->next; c; c = c->next)
e8ec07e1
PT
14922 if (sequence_type (c->ts) != result)
14923 return SEQ_MIXED;
14924
14925 return result;
14926
14927 case BT_CHARACTER:
14928 if (ts.kind != gfc_default_character_kind)
14929 return SEQ_NONDEFAULT;
14930
14931 return SEQ_CHARACTER;
14932
14933 case BT_INTEGER:
14934 if (ts.kind != gfc_default_integer_kind)
14935 return SEQ_NONDEFAULT;
14936
14937 return SEQ_NUMERIC;
14938
14939 case BT_REAL:
14940 if (!(ts.kind == gfc_default_real_kind
edf1eac2 14941 || ts.kind == gfc_default_double_kind))
e8ec07e1
PT
14942 return SEQ_NONDEFAULT;
14943
14944 return SEQ_NUMERIC;
14945
14946 case BT_COMPLEX:
14947 if (ts.kind != gfc_default_complex_kind)
14948 return SEQ_NONDEFAULT;
14949
14950 return SEQ_NUMERIC;
14951
14952 case BT_LOGICAL:
14953 if (ts.kind != gfc_default_logical_kind)
14954 return SEQ_NONDEFAULT;
14955
14956 return SEQ_NUMERIC;
14957
14958 default:
14959 return SEQ_NONDEFAULT;
14960 }
14961}
14962
14963
6de9cd9a
DN
14964/* Resolve derived type EQUIVALENCE object. */
14965
524af0d6 14966static bool
6de9cd9a
DN
14967resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
14968{
6de9cd9a
DN
14969 gfc_component *c = derived->components;
14970
14971 if (!derived)
524af0d6 14972 return true;
6de9cd9a
DN
14973
14974 /* Shall not be an object of nonsequence derived type. */
14975 if (!derived->attr.sequence)
14976 {
a4d9b221 14977 gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
edf1eac2
SK
14978 "attribute to be an EQUIVALENCE object", sym->name,
14979 &e->where);
524af0d6 14980 return false;
6de9cd9a
DN
14981 }
14982
66e4ab31 14983 /* Shall not have allocatable components. */
5046aff5
PT
14984 if (derived->attr.alloc_comp)
14985 {
a4d9b221 14986 gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
edf1eac2
SK
14987 "components to be an EQUIVALENCE object",sym->name,
14988 &e->where);
524af0d6 14989 return false;
5046aff5
PT
14990 }
14991
16e520b6 14992 if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
cddcf0d4 14993 {
a4d9b221 14994 gfc_error ("Derived type variable %qs at %L with default "
cddcf0d4
TB
14995 "initialization cannot be in EQUIVALENCE with a variable "
14996 "in COMMON", sym->name, &e->where);
524af0d6 14997 return false;
cddcf0d4
TB
14998 }
14999
6de9cd9a
DN
15000 for (; c ; c = c->next)
15001 {
bc21d315 15002 if (c->ts.type == BT_DERIVED
524af0d6
JB
15003 && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
15004 return false;
05c1e3a7 15005
6de9cd9a 15006 /* Shall not be an object of sequence derived type containing a pointer
edf1eac2 15007 in the structure. */
d4b7d0f0 15008 if (c->attr.pointer)
edf1eac2 15009 {
a4d9b221 15010 gfc_error ("Derived type variable %qs at %L with pointer "
edf1eac2
SK
15011 "component(s) cannot be an EQUIVALENCE object",
15012 sym->name, &e->where);
524af0d6 15013 return false;
edf1eac2 15014 }
6de9cd9a 15015 }
524af0d6 15016 return true;
6de9cd9a
DN
15017}
15018
15019
4d382327 15020/* Resolve equivalence object.
e8ec07e1
PT
15021 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
15022 an allocatable array, an object of nonsequence derived type, an object of
6de9cd9a
DN
15023 sequence derived type containing a pointer at any level of component
15024 selection, an automatic object, a function name, an entry name, a result
15025 name, a named constant, a structure component, or a subobject of any of
e8ec07e1
PT
15026 the preceding objects. A substring shall not have length zero. A
15027 derived type shall not have components with default initialization nor
15028 shall two objects of an equivalence group be initialized.
ee7e677f 15029 Either all or none of the objects shall have an protected attribute.
e8ec07e1
PT
15030 The simple constraints are done in symbol.c(check_conflict) and the rest
15031 are implemented here. */
6de9cd9a
DN
15032
15033static void
15034resolve_equivalence (gfc_equiv *eq)
15035{
15036 gfc_symbol *sym;
e8ec07e1 15037 gfc_symbol *first_sym;
6de9cd9a
DN
15038 gfc_expr *e;
15039 gfc_ref *r;
e8ec07e1
PT
15040 locus *last_where = NULL;
15041 seq_type eq_type, last_eq_type;
15042 gfc_typespec *last_ts;
ee7e677f 15043 int object, cnt_protected;
e8ec07e1
PT
15044 const char *msg;
15045
e8ec07e1 15046 last_ts = &eq->expr->symtree->n.sym->ts;
6de9cd9a 15047
e8ec07e1
PT
15048 first_sym = eq->expr->symtree->n.sym;
15049
ee7e677f
TB
15050 cnt_protected = 0;
15051
e8ec07e1 15052 for (object = 1; eq; eq = eq->eq, object++)
6de9cd9a
DN
15053 {
15054 e = eq->expr;
a8006d09
JJ
15055
15056 e->ts = e->symtree->n.sym->ts;
15057 /* match_varspec might not know yet if it is seeing
15058 array reference or substring reference, as it doesn't
15059 know the types. */
15060 if (e->ref && e->ref->type == REF_ARRAY)
15061 {
15062 gfc_ref *ref = e->ref;
15063 sym = e->symtree->n.sym;
15064
15065 if (sym->attr.dimension)
15066 {
15067 ref->u.ar.as = sym->as;
15068 ref = ref->next;
15069 }
15070
15071 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
15072 if (e->ts.type == BT_CHARACTER
15073 && ref
15074 && ref->type == REF_ARRAY
15075 && ref->u.ar.dimen == 1
15076 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
15077 && ref->u.ar.stride[0] == NULL)
15078 {
15079 gfc_expr *start = ref->u.ar.start[0];
15080 gfc_expr *end = ref->u.ar.end[0];
15081 void *mem = NULL;
15082
15083 /* Optimize away the (:) reference. */
15084 if (start == NULL && end == NULL)
15085 {
15086 if (e->ref == ref)
15087 e->ref = ref->next;
15088 else
15089 e->ref->next = ref->next;
15090 mem = ref;
15091 }
15092 else
15093 {
15094 ref->type = REF_SUBSTRING;
15095 if (start == NULL)
b7e75771
JD
15096 start = gfc_get_int_expr (gfc_default_integer_kind,
15097 NULL, 1);
a8006d09 15098 ref->u.ss.start = start;
bc21d315
JW
15099 if (end == NULL && e->ts.u.cl)
15100 end = gfc_copy_expr (e->ts.u.cl->length);
a8006d09 15101 ref->u.ss.end = end;
bc21d315
JW
15102 ref->u.ss.length = e->ts.u.cl;
15103 e->ts.u.cl = NULL;
a8006d09
JJ
15104 }
15105 ref = ref->next;
cede9502 15106 free (mem);
a8006d09
JJ
15107 }
15108
15109 /* Any further ref is an error. */
15110 if (ref)
15111 {
15112 gcc_assert (ref->type == REF_ARRAY);
15113 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
15114 &ref->u.ar.where);
15115 continue;
15116 }
15117 }
15118
524af0d6 15119 if (!gfc_resolve_expr (e))
edf1eac2 15120 continue;
6de9cd9a
DN
15121
15122 sym = e->symtree->n.sym;
6de9cd9a 15123
9aa433c2 15124 if (sym->attr.is_protected)
ee7e677f
TB
15125 cnt_protected++;
15126 if (cnt_protected > 0 && cnt_protected != object)
15127 {
15128 gfc_error ("Either all or none of the objects in the "
15129 "EQUIVALENCE set at %L shall have the "
15130 "PROTECTED attribute",
15131 &e->where);
15132 break;
edf1eac2 15133 }
ee7e677f 15134
e8ec07e1 15135 /* Shall not equivalence common block variables in a PURE procedure. */
05c1e3a7 15136 if (sym->ns->proc_name
edf1eac2
SK
15137 && sym->ns->proc_name->attr.pure
15138 && sym->attr.in_common)
15139 {
a4d9b221
TB
15140 gfc_error ("Common block member %qs at %L cannot be an EQUIVALENCE "
15141 "object in the pure procedure %qs",
e8ec07e1 15142 sym->name, &e->where, sym->ns->proc_name->name);
edf1eac2
SK
15143 break;
15144 }
05c1e3a7
BF
15145
15146 /* Shall not be a named constant. */
6de9cd9a 15147 if (e->expr_type == EXPR_CONSTANT)
edf1eac2 15148 {
a4d9b221 15149 gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
edf1eac2
SK
15150 "object", sym->name, &e->where);
15151 continue;
15152 }
6de9cd9a 15153
bc21d315 15154 if (e->ts.type == BT_DERIVED
524af0d6 15155 && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
edf1eac2 15156 continue;
6de9cd9a 15157
e8ec07e1
PT
15158 /* Check that the types correspond correctly:
15159 Note 5.28:
15160 A numeric sequence structure may be equivalenced to another sequence
15161 structure, an object of default integer type, default real type, double
15162 precision real type, default logical type such that components of the
15163 structure ultimately only become associated to objects of the same
15164 kind. A character sequence structure may be equivalenced to an object
15165 of default character kind or another character sequence structure.
15166 Other objects may be equivalenced only to objects of the same type and
15167 kind parameters. */
15168
15169 /* Identical types are unconditionally OK. */
15170 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
15171 goto identical_types;
15172
15173 last_eq_type = sequence_type (*last_ts);
15174 eq_type = sequence_type (sym->ts);
15175
15176 /* Since the pair of objects is not of the same type, mixed or
15177 non-default sequences can be rejected. */
15178
15179 msg = "Sequence %s with mixed components in EQUIVALENCE "
15180 "statement at %L with different type objects";
15181 if ((object ==2
edf1eac2 15182 && last_eq_type == SEQ_MIXED
524af0d6 15183 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
edf1eac2 15184 || (eq_type == SEQ_MIXED
524af0d6 15185 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
e8ec07e1
PT
15186 continue;
15187
15188 msg = "Non-default type object or sequence %s in EQUIVALENCE "
15189 "statement at %L with objects of different type";
15190 if ((object ==2
edf1eac2 15191 && last_eq_type == SEQ_NONDEFAULT
524af0d6 15192 && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
edf1eac2 15193 || (eq_type == SEQ_NONDEFAULT
524af0d6 15194 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
e8ec07e1
PT
15195 continue;
15196
a4d9b221 15197 msg ="Non-CHARACTER object %qs in default CHARACTER "
e8ec07e1
PT
15198 "EQUIVALENCE statement at %L";
15199 if (last_eq_type == SEQ_CHARACTER
edf1eac2 15200 && eq_type != SEQ_CHARACTER
524af0d6 15201 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
e8ec07e1
PT
15202 continue;
15203
a4d9b221 15204 msg ="Non-NUMERIC object %qs in default NUMERIC "
e8ec07e1
PT
15205 "EQUIVALENCE statement at %L";
15206 if (last_eq_type == SEQ_NUMERIC
edf1eac2 15207 && eq_type != SEQ_NUMERIC
524af0d6 15208 && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
e8ec07e1
PT
15209 continue;
15210
15211 identical_types:
15212 last_ts =&sym->ts;
15213 last_where = &e->where;
15214
6de9cd9a 15215 if (!e->ref)
edf1eac2 15216 continue;
6de9cd9a
DN
15217
15218 /* Shall not be an automatic array. */
15219 if (e->ref->type == REF_ARRAY
524af0d6 15220 && !gfc_resolve_array_spec (e->ref->u.ar.as, 1))
edf1eac2 15221 {
a4d9b221 15222 gfc_error ("Array %qs at %L with non-constant bounds cannot be "
edf1eac2
SK
15223 "an EQUIVALENCE object", sym->name, &e->where);
15224 continue;
15225 }
6de9cd9a 15226
6de9cd9a
DN
15227 r = e->ref;
15228 while (r)
edf1eac2 15229 {
a8006d09
JJ
15230 /* Shall not be a structure component. */
15231 if (r->type == REF_COMPONENT)
15232 {
a4d9b221 15233 gfc_error ("Structure component %qs at %L cannot be an "
a8006d09
JJ
15234 "EQUIVALENCE object",
15235 r->u.c.component->name, &e->where);
15236 break;
15237 }
15238
15239 /* A substring shall not have length zero. */
15240 if (r->type == REF_SUBSTRING)
15241 {
15242 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
15243 {
15244 gfc_error ("Substring at %L has length zero",
15245 &r->u.ss.start->where);
15246 break;
15247 }
15248 }
15249 r = r->next;
15250 }
05c1e3a7
BF
15251 }
15252}
cf4d246b
JJ
15253
15254
66e4ab31 15255/* Resolve function and ENTRY types, issue diagnostics if needed. */
cf4d246b
JJ
15256
15257static void
edf1eac2 15258resolve_fntype (gfc_namespace *ns)
cf4d246b
JJ
15259{
15260 gfc_entry_list *el;
15261 gfc_symbol *sym;
15262
15263 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
15264 return;
15265
15266 /* If there are any entries, ns->proc_name is the entry master
15267 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
15268 if (ns->entries)
15269 sym = ns->entries->sym;
15270 else
15271 sym = ns->proc_name;
15272 if (sym->result == sym
15273 && sym->ts.type == BT_UNKNOWN
524af0d6 15274 && !gfc_set_default_type (sym, 0, NULL)
cf4d246b
JJ
15275 && !sym->attr.untyped)
15276 {
a4d9b221 15277 gfc_error ("Function %qs at %L has no IMPLICIT type",
cf4d246b
JJ
15278 sym->name, &sym->declared_at);
15279 sym->attr.untyped = 1;
15280 }
15281
bc21d315 15282 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
0d6872cb 15283 && !sym->attr.contained
6e2062b0
JW
15284 && !gfc_check_symbol_access (sym->ts.u.derived)
15285 && gfc_check_symbol_access (sym))
3bcc018c 15286 {
a4d9b221
TB
15287 gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
15288 "%L of PRIVATE type %qs", sym->name,
bc21d315 15289 &sym->declared_at, sym->ts.u.derived->name);
3bcc018c
EE
15290 }
15291
7453378e 15292 if (ns->entries)
cf4d246b
JJ
15293 for (el = ns->entries->next; el; el = el->next)
15294 {
15295 if (el->sym->result == el->sym
15296 && el->sym->ts.type == BT_UNKNOWN
524af0d6 15297 && !gfc_set_default_type (el->sym, 0, NULL)
cf4d246b
JJ
15298 && !el->sym->attr.untyped)
15299 {
a4d9b221 15300 gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
cf4d246b
JJ
15301 el->sym->name, &el->sym->declared_at);
15302 el->sym->attr.untyped = 1;
15303 }
15304 }
15305}
15306
94747289 15307
0e3e65bc
PT
15308/* 12.3.2.1.1 Defined operators. */
15309
524af0d6 15310static bool
94747289 15311check_uop_procedure (gfc_symbol *sym, locus where)
0e3e65bc 15312{
0e3e65bc
PT
15313 gfc_formal_arglist *formal;
15314
94747289
DK
15315 if (!sym->attr.function)
15316 {
a4d9b221 15317 gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
94747289 15318 sym->name, &where);
524af0d6 15319 return false;
94747289 15320 }
05c1e3a7 15321
94747289 15322 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
15323 && !(sym->ts.u.cl && sym->ts.u.cl->length)
15324 && !(sym->result && sym->result->ts.u.cl
15325 && sym->result->ts.u.cl->length))
94747289 15326 {
a4d9b221 15327 gfc_error ("User operator procedure %qs at %L cannot be assumed "
94747289 15328 "character length", sym->name, &where);
524af0d6 15329 return false;
94747289 15330 }
0e3e65bc 15331
4cbc9039 15332 formal = gfc_sym_get_dummy_args (sym);
94747289 15333 if (!formal || !formal->sym)
0e3e65bc 15334 {
a4d9b221 15335 gfc_error ("User operator procedure %qs at %L must have at least "
94747289 15336 "one argument", sym->name, &where);
524af0d6 15337 return false;
94747289 15338 }
0e3e65bc 15339
94747289
DK
15340 if (formal->sym->attr.intent != INTENT_IN)
15341 {
15342 gfc_error ("First argument of operator interface at %L must be "
15343 "INTENT(IN)", &where);
524af0d6 15344 return false;
94747289 15345 }
0e3e65bc 15346
94747289
DK
15347 if (formal->sym->attr.optional)
15348 {
15349 gfc_error ("First argument of operator interface at %L cannot be "
15350 "optional", &where);
524af0d6 15351 return false;
94747289 15352 }
0e3e65bc 15353
94747289
DK
15354 formal = formal->next;
15355 if (!formal || !formal->sym)
524af0d6 15356 return true;
0e3e65bc 15357
94747289
DK
15358 if (formal->sym->attr.intent != INTENT_IN)
15359 {
15360 gfc_error ("Second argument of operator interface at %L must be "
15361 "INTENT(IN)", &where);
524af0d6 15362 return false;
94747289 15363 }
0e3e65bc 15364
94747289
DK
15365 if (formal->sym->attr.optional)
15366 {
15367 gfc_error ("Second argument of operator interface at %L cannot be "
15368 "optional", &where);
524af0d6 15369 return false;
94747289 15370 }
0e3e65bc 15371
94747289
DK
15372 if (formal->next)
15373 {
15374 gfc_error ("Operator interface at %L must have, at most, two "
15375 "arguments", &where);
524af0d6 15376 return false;
94747289 15377 }
0e3e65bc 15378
524af0d6 15379 return true;
94747289 15380}
0e3e65bc 15381
94747289
DK
15382static void
15383gfc_resolve_uops (gfc_symtree *symtree)
15384{
15385 gfc_interface *itr;
15386
15387 if (symtree == NULL)
15388 return;
15389
15390 gfc_resolve_uops (symtree->left);
15391 gfc_resolve_uops (symtree->right);
15392
15393 for (itr = symtree->n.uop->op; itr; itr = itr->next)
15394 check_uop_procedure (itr->sym, itr->sym->declared_at);
0e3e65bc
PT
15395}
15396
cf4d246b 15397
efb0828d
L
15398/* Examine all of the expressions associated with a program unit,
15399 assign types to all intermediate expressions, make sure that all
15400 assignments are to compatible types and figure out which names
15401 refer to which functions or subroutines. It doesn't check code
b46ebd6c 15402 block, which is handled by gfc_resolve_code. */
6de9cd9a 15403
efb0828d 15404static void
edf1eac2 15405resolve_types (gfc_namespace *ns)
6de9cd9a 15406{
efb0828d 15407 gfc_namespace *n;
6de9cd9a
DN
15408 gfc_charlen *cl;
15409 gfc_data *d;
15410 gfc_equiv *eq;
a82f1f2e 15411 gfc_namespace* old_ns = gfc_current_ns;
6de9cd9a 15412
2b91aea8
MM
15413 if (ns->types_resolved)
15414 return;
15415
52f49934
DK
15416 /* Check that all IMPLICIT types are ok. */
15417 if (!ns->seen_implicit_none)
15418 {
15419 unsigned letter;
15420 for (letter = 0; letter != GFC_LETTERS; ++letter)
15421 if (ns->set_flag[letter]
22c23886 15422 && !resolve_typespec_used (&ns->default_type[letter],
524af0d6 15423 &ns->implicit_loc[letter], NULL))
52f49934
DK
15424 return;
15425 }
15426
a82f1f2e
DK
15427 gfc_current_ns = ns;
15428
0f3162e3
PT
15429 resolve_entries (ns);
15430
6dcab507 15431 resolve_common_vars (&ns->blank_common, false);
ad22b1ff
TB
15432 resolve_common_blocks (ns->common_root);
15433
0f3162e3
PT
15434 resolve_contained_functions (ns);
15435
12578be7
TB
15436 if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
15437 && ns->proc_name->attr.if_source == IFSRC_IFBODY)
15438 resolve_formal_arglist (ns->proc_name);
15439
a8b3b0b6
CR
15440 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
15441
5cd09fac
TS
15442 for (cl = ns->cl_list; cl; cl = cl->next)
15443 resolve_charlen (cl);
15444
6de9cd9a
DN
15445 gfc_traverse_ns (ns, resolve_symbol);
15446
cf4d246b
JJ
15447 resolve_fntype (ns);
15448
6de9cd9a
DN
15449 for (n = ns->contained; n; n = n->sibling)
15450 {
15451 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
a4d9b221 15452 gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
6de9cd9a
DN
15453 "also be PURE", n->proc_name->name,
15454 &n->proc_name->declared_at);
15455
efb0828d 15456 resolve_types (n);
6de9cd9a
DN
15457 }
15458
15459 forall_flag = 0;
ce96d372 15460 gfc_do_concurrent_flag = 0;
6de9cd9a
DN
15461 gfc_check_interfaces (ns);
15462
6de9cd9a
DN
15463 gfc_traverse_ns (ns, resolve_values);
15464
d05d9ac7 15465 if (ns->save_all)
6de9cd9a
DN
15466 gfc_save_all (ns);
15467
15468 iter_stack = NULL;
15469 for (d = ns->data; d; d = d->next)
15470 resolve_data (d);
15471
15472 iter_stack = NULL;
15473 gfc_traverse_ns (ns, gfc_formalize_init_value);
15474
a8b3b0b6
CR
15475 gfc_traverse_ns (ns, gfc_verify_binding_labels);
15476
6de9cd9a
DN
15477 for (eq = ns->equiv; eq; eq = eq->next)
15478 resolve_equivalence (eq);
15479
6de9cd9a 15480 /* Warn about unused labels. */
2e5758e8 15481 if (warn_unused_label)
994c1cc0 15482 warn_unused_fortran_label (ns->st_labels);
0e3e65bc
PT
15483
15484 gfc_resolve_uops (ns->uop_root);
a82f1f2e 15485
dd2fc525
JJ
15486 gfc_resolve_omp_declare_simd (ns);
15487
5f23671d
JJ
15488 gfc_resolve_omp_udrs (ns->omp_udr_root);
15489
2b91aea8
MM
15490 ns->types_resolved = 1;
15491
a82f1f2e 15492 gfc_current_ns = old_ns;
efb0828d
L
15493}
15494
15495
b46ebd6c 15496/* Call gfc_resolve_code recursively. */
efb0828d
L
15497
15498static void
edf1eac2 15499resolve_codes (gfc_namespace *ns)
efb0828d
L
15500{
15501 gfc_namespace *n;
71a7778c 15502 bitmap_obstack old_obstack;
efb0828d 15503
611c64f0
JW
15504 if (ns->resolved == 1)
15505 return;
15506
efb0828d
L
15507 for (n = ns->contained; n; n = n->sibling)
15508 resolve_codes (n);
15509
15510 gfc_current_ns = ns;
76d02e9f
JW
15511
15512 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
15513 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
15514 cs_base = NULL;
15515
0e9a445b
PT
15516 /* Set to an out of range value. */
15517 current_entry_id = -1;
0615f923 15518
71a7778c 15519 old_obstack = labels_obstack;
0615f923 15520 bitmap_obstack_initialize (&labels_obstack);
71a7778c 15521
41dbbb37 15522 gfc_resolve_oacc_declare (ns);
b46ebd6c 15523 gfc_resolve_code (ns->code, ns);
71a7778c 15524
0615f923 15525 bitmap_obstack_release (&labels_obstack);
71a7778c 15526 labels_obstack = old_obstack;
efb0828d
L
15527}
15528
15529
15530/* This function is called after a complete program unit has been compiled.
15531 Its purpose is to examine all of the expressions associated with a program
15532 unit, assign types to all intermediate expressions, make sure that all
15533 assignments are to compatible types and figure out which names refer to
15534 which functions or subroutines. */
15535
15536void
edf1eac2 15537gfc_resolve (gfc_namespace *ns)
efb0828d
L
15538{
15539 gfc_namespace *old_ns;
3af8d8cb 15540 code_stack *old_cs_base;
f0e99403 15541 struct gfc_omp_saved_state old_omp_state;
efb0828d 15542
71a7778c
PT
15543 if (ns->resolved)
15544 return;
15545
3af8d8cb 15546 ns->resolved = -1;
efb0828d 15547 old_ns = gfc_current_ns;
3af8d8cb 15548 old_cs_base = cs_base;
efb0828d 15549
f0e99403
MM
15550 /* As gfc_resolve can be called during resolution of an OpenMP construct
15551 body, we should clear any state associated to it, so that say NS's
15552 DO loops are not interpreted as OpenMP loops. */
15553 gfc_omp_save_and_clear_state (&old_omp_state);
15554
efb0828d 15555 resolve_types (ns);
4d382327 15556 component_assignment_level = 0;
efb0828d 15557 resolve_codes (ns);
6de9cd9a
DN
15558
15559 gfc_current_ns = old_ns;
3af8d8cb 15560 cs_base = old_cs_base;
71a7778c 15561 ns->resolved = 1;
601d98be
TK
15562
15563 gfc_run_passes (ns);
f0e99403
MM
15564
15565 gfc_omp_restore_state (&old_omp_state);
6de9cd9a 15566}