Branch data Line data Source code
1 : : /* Perform type resolution on the various structures.
2 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 : : Contributed by Andy Vaught
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "options.h"
25 : : #include "bitmap.h"
26 : : #include "gfortran.h"
27 : : #include "arith.h" /* For gfc_compare_expr(). */
28 : : #include "dependency.h"
29 : : #include "data.h"
30 : : #include "target-memory.h" /* for gfc_simplify_transfer */
31 : : #include "constructor.h"
32 : :
33 : : /* Types used in equivalence statements. */
34 : :
35 : : enum seq_type
36 : : {
37 : : SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 : : };
39 : :
40 : : /* Stack to keep track of the nesting of blocks as we move through the
41 : : code. See resolve_branch() and gfc_resolve_code(). */
42 : :
43 : : typedef struct code_stack
44 : : {
45 : : struct gfc_code *head, *current;
46 : : struct code_stack *prev;
47 : :
48 : : /* This bitmap keeps track of the targets valid for a branch from
49 : : inside this block except for END {IF|SELECT}s of enclosing
50 : : blocks. */
51 : : bitmap reachable_labels;
52 : : }
53 : : code_stack;
54 : :
55 : : static code_stack *cs_base = NULL;
56 : :
57 : : struct check_default_none_data
58 : : {
59 : : gfc_code *code;
60 : : hash_set<gfc_symbol *> *sym_hash;
61 : : gfc_namespace *ns;
62 : : bool default_none;
63 : : };
64 : :
65 : : /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
66 : :
67 : : static int forall_flag;
68 : : int gfc_do_concurrent_flag;
69 : :
70 : : /* True when we are resolving an expression that is an actual argument to
71 : : a procedure. */
72 : : static bool actual_arg = false;
73 : : /* True when we are resolving an expression that is the first actual argument
74 : : to a procedure. */
75 : : static bool first_actual_arg = false;
76 : :
77 : :
78 : : /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
79 : :
80 : : static int omp_workshare_flag;
81 : :
82 : :
83 : : /* True if we are resolving a specification expression. */
84 : : static bool specification_expr = false;
85 : :
86 : : /* The id of the last entry seen. */
87 : : static int current_entry_id;
88 : :
89 : : /* We use bitmaps to determine if a branch target is valid. */
90 : : static bitmap_obstack labels_obstack;
91 : :
92 : : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
93 : : static bool inquiry_argument = false;
94 : :
95 : :
96 : : /* Is the symbol host associated? */
97 : : static bool
98 : 49398 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
99 : : {
100 : 53803 : for (ns = ns->parent; ns; ns = ns->parent)
101 : : {
102 : 4656 : if (sym->ns == ns)
103 : : return true;
104 : : }
105 : :
106 : : return false;
107 : : }
108 : :
109 : : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
110 : : an ABSTRACT derived-type. If where is not NULL, an error message with that
111 : : locus is printed, optionally using name. */
112 : :
113 : : static bool
114 : 1478998 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
115 : : {
116 : 1478998 : if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
117 : : {
118 : 5 : if (where)
119 : : {
120 : 5 : if (name)
121 : 4 : gfc_error ("%qs at %L is of the ABSTRACT type %qs",
122 : : name, where, ts->u.derived->name);
123 : : else
124 : 1 : gfc_error ("ABSTRACT type %qs used at %L",
125 : : ts->u.derived->name, where);
126 : : }
127 : :
128 : 5 : return false;
129 : : }
130 : :
131 : : return true;
132 : : }
133 : :
134 : :
135 : : static bool
136 : 5031 : check_proc_interface (gfc_symbol *ifc, locus *where)
137 : : {
138 : : /* Several checks for F08:C1216. */
139 : 5031 : if (ifc->attr.procedure)
140 : : {
141 : 2 : gfc_error ("Interface %qs at %L is declared "
142 : : "in a later PROCEDURE statement", ifc->name, where);
143 : 2 : return false;
144 : : }
145 : 5029 : if (ifc->generic)
146 : : {
147 : : /* For generic interfaces, check if there is
148 : : a specific procedure with the same name. */
149 : : gfc_interface *gen = ifc->generic;
150 : 12 : while (gen && strcmp (gen->sym->name, ifc->name) != 0)
151 : 5 : gen = gen->next;
152 : 7 : if (!gen)
153 : : {
154 : 4 : gfc_error ("Interface %qs at %L may not be generic",
155 : : ifc->name, where);
156 : 4 : return false;
157 : : }
158 : : }
159 : 5025 : if (ifc->attr.proc == PROC_ST_FUNCTION)
160 : : {
161 : 4 : gfc_error ("Interface %qs at %L may not be a statement function",
162 : : ifc->name, where);
163 : 4 : return false;
164 : : }
165 : 5021 : if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
166 : 5021 : || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
167 : 17 : ifc->attr.intrinsic = 1;
168 : 5021 : if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
169 : : {
170 : 3 : gfc_error ("Intrinsic procedure %qs not allowed in "
171 : : "PROCEDURE statement at %L", ifc->name, where);
172 : 3 : return false;
173 : : }
174 : 5018 : if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
175 : : {
176 : 7 : gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
177 : 7 : return false;
178 : : }
179 : : return true;
180 : : }
181 : :
182 : :
183 : : static void resolve_symbol (gfc_symbol *sym);
184 : :
185 : :
186 : : /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
187 : :
188 : : static bool
189 : 1999 : resolve_procedure_interface (gfc_symbol *sym)
190 : : {
191 : 1999 : gfc_symbol *ifc = sym->ts.interface;
192 : :
193 : 1999 : if (!ifc)
194 : : return true;
195 : :
196 : 1843 : if (ifc == sym)
197 : : {
198 : 2 : gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
199 : : sym->name, &sym->declared_at);
200 : 2 : return false;
201 : : }
202 : 1841 : if (!check_proc_interface (ifc, &sym->declared_at))
203 : : return false;
204 : :
205 : 1832 : if (ifc->attr.if_source || ifc->attr.intrinsic)
206 : : {
207 : : /* Resolve interface and copy attributes. */
208 : 1553 : resolve_symbol (ifc);
209 : 1553 : if (ifc->attr.intrinsic)
210 : 14 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
211 : :
212 : 1553 : if (ifc->result)
213 : : {
214 : 678 : sym->ts = ifc->result->ts;
215 : 678 : sym->attr.allocatable = ifc->result->attr.allocatable;
216 : 678 : sym->attr.pointer = ifc->result->attr.pointer;
217 : 678 : sym->attr.dimension = ifc->result->attr.dimension;
218 : 678 : sym->attr.class_ok = ifc->result->attr.class_ok;
219 : 678 : sym->as = gfc_copy_array_spec (ifc->result->as);
220 : 678 : sym->result = sym;
221 : : }
222 : : else
223 : : {
224 : 875 : sym->ts = ifc->ts;
225 : 875 : sym->attr.allocatable = ifc->attr.allocatable;
226 : 875 : sym->attr.pointer = ifc->attr.pointer;
227 : 875 : sym->attr.dimension = ifc->attr.dimension;
228 : 875 : sym->attr.class_ok = ifc->attr.class_ok;
229 : 875 : sym->as = gfc_copy_array_spec (ifc->as);
230 : : }
231 : 1553 : sym->ts.interface = ifc;
232 : 1553 : sym->attr.function = ifc->attr.function;
233 : 1553 : sym->attr.subroutine = ifc->attr.subroutine;
234 : :
235 : 1553 : sym->attr.pure = ifc->attr.pure;
236 : 1553 : sym->attr.elemental = ifc->attr.elemental;
237 : 1553 : sym->attr.contiguous = ifc->attr.contiguous;
238 : 1553 : sym->attr.recursive = ifc->attr.recursive;
239 : 1553 : sym->attr.always_explicit = ifc->attr.always_explicit;
240 : 1553 : sym->attr.ext_attr |= ifc->attr.ext_attr;
241 : 1553 : sym->attr.is_bind_c = ifc->attr.is_bind_c;
242 : : /* Copy char length. */
243 : 1553 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
244 : : {
245 : 45 : sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
246 : 45 : if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
247 : 53 : && !gfc_resolve_expr (sym->ts.u.cl->length))
248 : : return false;
249 : : }
250 : : }
251 : :
252 : : return true;
253 : : }
254 : :
255 : :
256 : : /* Resolve types of formal argument lists. These have to be done early so that
257 : : the formal argument lists of module procedures can be copied to the
258 : : containing module before the individual procedures are resolved
259 : : individually. We also resolve argument lists of procedures in interface
260 : : blocks because they are self-contained scoping units.
261 : :
262 : : Since a dummy argument cannot be a non-dummy procedure, the only
263 : : resort left for untyped names are the IMPLICIT types. */
264 : :
265 : : void
266 : 508422 : gfc_resolve_formal_arglist (gfc_symbol *proc)
267 : : {
268 : 508422 : gfc_formal_arglist *f;
269 : 508422 : gfc_symbol *sym;
270 : 508422 : bool saved_specification_expr;
271 : 508422 : int i;
272 : :
273 : 508422 : if (proc->result != NULL)
274 : 317325 : sym = proc->result;
275 : : else
276 : : sym = proc;
277 : :
278 : 508422 : if (gfc_elemental (proc)
279 : 346908 : || sym->attr.pointer || sym->attr.allocatable
280 : 843714 : || (sym->as && sym->as->rank != 0))
281 : : {
282 : 175405 : proc->attr.always_explicit = 1;
283 : 175405 : sym->attr.always_explicit = 1;
284 : : }
285 : :
286 : 508422 : gfc_namespace *orig_current_ns = gfc_current_ns;
287 : 508422 : gfc_current_ns = gfc_get_procedure_ns (proc);
288 : :
289 : 1317751 : for (f = proc->formal; f; f = f->next)
290 : : {
291 : 809331 : gfc_array_spec *as;
292 : :
293 : 809331 : sym = f->sym;
294 : :
295 : 809331 : if (sym == NULL)
296 : : {
297 : : /* Alternate return placeholder. */
298 : 171 : if (gfc_elemental (proc))
299 : 1 : gfc_error ("Alternate return specifier in elemental subroutine "
300 : : "%qs at %L is not allowed", proc->name,
301 : : &proc->declared_at);
302 : 171 : if (proc->attr.function)
303 : 1 : gfc_error ("Alternate return specifier in function "
304 : : "%qs at %L is not allowed", proc->name,
305 : : &proc->declared_at);
306 : 171 : continue;
307 : : }
308 : :
309 : 554 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 : 809714 : && !resolve_procedure_interface (sym))
311 : : break;
312 : :
313 : 809160 : if (strcmp (proc->name, sym->name) == 0)
314 : : {
315 : 2 : gfc_error ("Self-referential argument "
316 : : "%qs at %L is not allowed", sym->name,
317 : : &proc->declared_at);
318 : 2 : break;
319 : : }
320 : :
321 : 809158 : if (sym->attr.if_source != IFSRC_UNKNOWN)
322 : 817 : gfc_resolve_formal_arglist (sym);
323 : :
324 : 809158 : if (sym->attr.subroutine || sym->attr.external)
325 : : {
326 : 821 : if (sym->attr.flavor == FL_UNKNOWN)
327 : 9 : gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 : : }
329 : : else
330 : : {
331 : 808337 : if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 : 3661 : && (!sym->attr.function || sym->result == sym))
333 : 3623 : gfc_set_default_type (sym, 1, sym->ns);
334 : : }
335 : :
336 : 809158 : as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 : 822470 : ? CLASS_DATA (sym)->as : sym->as;
338 : :
339 : 809158 : saved_specification_expr = specification_expr;
340 : 809158 : specification_expr = true;
341 : 809158 : gfc_resolve_array_spec (as, 0);
342 : 809158 : specification_expr = saved_specification_expr;
343 : :
344 : : /* We can't tell if an array with dimension (:) is assumed or deferred
345 : : shape until we know if it has the pointer or allocatable attributes.
346 : : */
347 : 809158 : if (as && as->rank > 0 && as->type == AS_DEFERRED
348 : 11998 : && ((sym->ts.type != BT_CLASS
349 : 10929 : && !(sym->attr.pointer || sym->attr.allocatable))
350 : 5244 : || (sym->ts.type == BT_CLASS
351 : 1069 : && !(CLASS_DATA (sym)->attr.class_pointer
352 : 875 : || CLASS_DATA (sym)->attr.allocatable)))
353 : 7215 : && sym->attr.flavor != FL_PROCEDURE)
354 : : {
355 : 7214 : as->type = AS_ASSUMED_SHAPE;
356 : 16771 : for (i = 0; i < as->rank; i++)
357 : 9557 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 : : }
359 : :
360 : 125996 : if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 : 112760 : || (as && as->type == AS_ASSUMED_RANK)
362 : 758876 : || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 : 748857 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 : 11245 : && (CLASS_DATA (sym)->attr.class_pointer
365 : 10768 : || CLASS_DATA (sym)->attr.allocatable
366 : 9891 : || CLASS_DATA (sym)->attr.target))
367 : 747503 : || sym->attr.optional)
368 : : {
369 : 76457 : proc->attr.always_explicit = 1;
370 : 76457 : if (proc->result)
371 : 35307 : proc->result->attr.always_explicit = 1;
372 : : }
373 : :
374 : : /* If the flavor is unknown at this point, it has to be a variable.
375 : : A procedure specification would have already set the type. */
376 : :
377 : 809158 : if (sym->attr.flavor == FL_UNKNOWN)
378 : 50084 : gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379 : :
380 : 809158 : if (gfc_pure (proc))
381 : : {
382 : 325811 : if (sym->attr.flavor == FL_PROCEDURE)
383 : : {
384 : : /* F08:C1279. */
385 : 29 : if (!gfc_pure (sym))
386 : : {
387 : 1 : gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 : : "also be PURE", sym->name, &sym->declared_at);
389 : 1 : continue;
390 : : }
391 : : }
392 : 325782 : else if (!sym->attr.pointer)
393 : : {
394 : 325774 : if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 : : {
396 : 109 : if (sym->attr.value)
397 : 108 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 : : " of pure function %qs at %L with VALUE "
399 : : "attribute but without INTENT(IN)",
400 : : sym->name, proc->name, &sym->declared_at);
401 : : else
402 : 1 : gfc_error ("Argument %qs of pure function %qs at %L must "
403 : : "be INTENT(IN) or VALUE", sym->name, proc->name,
404 : : &sym->declared_at);
405 : : }
406 : :
407 : 325774 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 : : {
409 : 159 : if (sym->attr.value)
410 : 159 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 : : " of pure subroutine %qs at %L with VALUE "
412 : : "attribute but without INTENT", sym->name,
413 : : proc->name, &sym->declared_at);
414 : : else
415 : 0 : gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 : : "must have its INTENT specified or have the "
417 : : "VALUE attribute", sym->name, proc->name,
418 : : &sym->declared_at);
419 : : }
420 : : }
421 : :
422 : : /* F08:C1278a. */
423 : 325810 : if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 : : {
425 : 1 : gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 : : " may not be polymorphic", sym->name, proc->name,
427 : : &sym->declared_at);
428 : 1 : continue;
429 : : }
430 : : }
431 : :
432 : 809156 : if (proc->attr.implicit_pure)
433 : : {
434 : 24247 : if (sym->attr.flavor == FL_PROCEDURE)
435 : : {
436 : 295 : if (!gfc_pure (sym))
437 : 275 : proc->attr.implicit_pure = 0;
438 : : }
439 : 23952 : else if (!sym->attr.pointer)
440 : : {
441 : 23170 : if (proc->attr.function && sym->attr.intent != INTENT_IN
442 : 2682 : && !sym->value)
443 : 2682 : proc->attr.implicit_pure = 0;
444 : :
445 : 23170 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 : 4117 : && !sym->value)
447 : 4117 : proc->attr.implicit_pure = 0;
448 : : }
449 : : }
450 : :
451 : 809156 : if (gfc_elemental (proc))
452 : : {
453 : : /* F08:C1289. */
454 : 300422 : if (sym->attr.codimension
455 : 300421 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 : 898 : && CLASS_DATA (sym)->attr.codimension))
457 : : {
458 : 3 : gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 : : "procedure", sym->name, &sym->declared_at);
460 : 3 : continue;
461 : : }
462 : :
463 : 300419 : if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 : 896 : && CLASS_DATA (sym)->as))
465 : : {
466 : 2 : gfc_error ("Argument %qs of elemental procedure at %L must "
467 : : "be scalar", sym->name, &sym->declared_at);
468 : 2 : continue;
469 : : }
470 : :
471 : 300417 : if (sym->attr.allocatable
472 : 300416 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 : 895 : && CLASS_DATA (sym)->attr.allocatable))
474 : : {
475 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 : : "have the ALLOCATABLE attribute", sym->name,
477 : : &sym->declared_at);
478 : 2 : continue;
479 : : }
480 : :
481 : 300415 : if (sym->attr.pointer
482 : 300414 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 : 894 : && CLASS_DATA (sym)->attr.class_pointer))
484 : : {
485 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 : : "have the POINTER attribute", sym->name,
487 : : &sym->declared_at);
488 : 2 : continue;
489 : : }
490 : :
491 : 300413 : if (sym->attr.flavor == FL_PROCEDURE)
492 : : {
493 : 2 : gfc_error ("Dummy procedure %qs not allowed in elemental "
494 : : "procedure %qs at %L", sym->name, proc->name,
495 : : &sym->declared_at);
496 : 2 : continue;
497 : : }
498 : :
499 : : /* Fortran 2008 Corrigendum 1, C1290a. */
500 : 300411 : if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 : : {
502 : 2 : gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 : : "have its INTENT specified or have the VALUE "
504 : : "attribute", sym->name, proc->name,
505 : : &sym->declared_at);
506 : 2 : continue;
507 : : }
508 : : }
509 : :
510 : : /* Each dummy shall be specified to be scalar. */
511 : 809143 : if (proc->attr.proc == PROC_ST_FUNCTION)
512 : : {
513 : 305 : if (sym->as != NULL)
514 : : {
515 : : /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 : : shall be specified, explicitly or implicitly, to be scalar. */
517 : 1 : gfc_error ("Argument %qs of statement function %qs at %L "
518 : : "must be scalar", sym->name, proc->name,
519 : : &proc->declared_at);
520 : 1 : continue;
521 : : }
522 : :
523 : 304 : if (sym->ts.type == BT_CHARACTER)
524 : : {
525 : 48 : gfc_charlen *cl = sym->ts.u.cl;
526 : 48 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 : : {
528 : 0 : gfc_error ("Character-valued argument %qs of statement "
529 : : "function at %L must have constant length",
530 : : sym->name, &sym->declared_at);
531 : 0 : continue;
532 : : }
533 : : }
534 : : }
535 : : }
536 : 508422 : if (sym)
537 : 508330 : sym->formal_resolved = 1;
538 : 508422 : gfc_current_ns = orig_current_ns;
539 : 508422 : }
540 : :
541 : :
542 : : /* Work function called when searching for symbols that have argument lists
543 : : associated with them. */
544 : :
545 : : static void
546 : 1769633 : find_arglists (gfc_symbol *sym)
547 : : {
548 : 1769633 : if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
549 : 321490 : || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
550 : : return;
551 : :
552 : 319714 : gfc_resolve_formal_arglist (sym);
553 : : }
554 : :
555 : :
556 : : /* Given a namespace, resolve all formal argument lists within the namespace.
557 : : */
558 : :
559 : : static void
560 : 335623 : resolve_formal_arglists (gfc_namespace *ns)
561 : : {
562 : 0 : if (ns == NULL)
563 : : return;
564 : :
565 : 335623 : gfc_traverse_ns (ns, find_arglists);
566 : : }
567 : :
568 : :
569 : : static void
570 : 36069 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
571 : : {
572 : 36069 : bool t;
573 : :
574 : 36069 : if (sym && sym->attr.flavor == FL_PROCEDURE
575 : 36069 : && sym->ns->parent
576 : 1062 : && sym->ns->parent->proc_name
577 : 1062 : && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
578 : 1 : && !strcmp (sym->name, sym->ns->parent->proc_name->name))
579 : 0 : gfc_error ("Contained procedure %qs at %L has the same name as its "
580 : : "encompassing procedure", sym->name, &sym->declared_at);
581 : :
582 : : /* If this namespace is not a function or an entry master function,
583 : : ignore it. */
584 : 36069 : if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
585 : 10588 : || sym->attr.entry_master)
586 : 25669 : return;
587 : :
588 : 10400 : if (!sym->result)
589 : : return;
590 : :
591 : : /* Try to find out of what the return type is. */
592 : 10400 : if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
593 : : {
594 : 55 : t = gfc_set_default_type (sym->result, 0, ns);
595 : :
596 : 55 : if (!t && !sym->result->attr.untyped)
597 : : {
598 : 19 : if (sym->result == sym)
599 : 1 : gfc_error ("Contained function %qs at %L has no IMPLICIT type",
600 : : sym->name, &sym->declared_at);
601 : 18 : else if (!sym->result->attr.proc_pointer)
602 : 0 : gfc_error ("Result %qs of contained function %qs at %L has "
603 : : "no IMPLICIT type", sym->result->name, sym->name,
604 : : &sym->result->declared_at);
605 : 19 : sym->result->attr.untyped = 1;
606 : : }
607 : : }
608 : :
609 : : /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
610 : : type, lists the only ways a character length value of * can be used:
611 : : dummy arguments of procedures, named constants, function results and
612 : : in allocate statements if the allocate_object is an assumed length dummy
613 : : in external functions. Internal function results and results of module
614 : : procedures are not on this list, ergo, not permitted. */
615 : :
616 : 10400 : if (sym->result->ts.type == BT_CHARACTER)
617 : : {
618 : 1158 : gfc_charlen *cl = sym->result->ts.u.cl;
619 : 1158 : if ((!cl || !cl->length) && !sym->result->ts.deferred)
620 : : {
621 : : /* See if this is a module-procedure and adapt error message
622 : : accordingly. */
623 : 4 : bool module_proc;
624 : 4 : gcc_assert (ns->parent && ns->parent->proc_name);
625 : 4 : module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
626 : :
627 : 7 : gfc_error (module_proc
628 : : ? G_("Character-valued module procedure %qs at %L"
629 : : " must not be assumed length")
630 : : : G_("Character-valued internal function %qs at %L"
631 : : " must not be assumed length"),
632 : : sym->name, &sym->declared_at);
633 : : }
634 : : }
635 : : }
636 : :
637 : :
638 : : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
639 : : introduce duplicates. */
640 : :
641 : : static void
642 : 1420 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
643 : : {
644 : 1420 : gfc_formal_arglist *f, *new_arglist;
645 : 1420 : gfc_symbol *new_sym;
646 : :
647 : 2561 : for (; new_args != NULL; new_args = new_args->next)
648 : : {
649 : 1141 : new_sym = new_args->sym;
650 : : /* See if this arg is already in the formal argument list. */
651 : 2165 : for (f = proc->formal; f; f = f->next)
652 : : {
653 : 1470 : if (new_sym == f->sym)
654 : : break;
655 : : }
656 : :
657 : 1141 : if (f)
658 : 446 : continue;
659 : :
660 : : /* Add a new argument. Argument order is not important. */
661 : 695 : new_arglist = gfc_get_formal_arglist ();
662 : 695 : new_arglist->sym = new_sym;
663 : 695 : new_arglist->next = proc->formal;
664 : 695 : proc->formal = new_arglist;
665 : : }
666 : 1420 : }
667 : :
668 : :
669 : : /* Flag the arguments that are not present in all entries. */
670 : :
671 : : static void
672 : 1420 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
673 : : {
674 : 1420 : gfc_formal_arglist *f, *head;
675 : 1420 : head = new_args;
676 : :
677 : 2994 : for (f = proc->formal; f; f = f->next)
678 : : {
679 : 1574 : if (f->sym == NULL)
680 : 36 : continue;
681 : :
682 : 2704 : for (new_args = head; new_args; new_args = new_args->next)
683 : : {
684 : 2262 : if (new_args->sym == f->sym)
685 : : break;
686 : : }
687 : :
688 : 1538 : if (new_args)
689 : 1096 : continue;
690 : :
691 : 442 : f->sym->attr.not_always_present = 1;
692 : : }
693 : 1420 : }
694 : :
695 : :
696 : : /* Resolve alternate entry points. If a symbol has multiple entry points we
697 : : create a new master symbol for the main routine, and turn the existing
698 : : symbol into an entry point. */
699 : :
700 : : static void
701 : 371187 : resolve_entries (gfc_namespace *ns)
702 : : {
703 : 371187 : gfc_namespace *old_ns;
704 : 371187 : gfc_code *c;
705 : 371187 : gfc_symbol *proc;
706 : 371187 : gfc_entry_list *el;
707 : : /* Provide sufficient space to hold "master.%d.%s". */
708 : 371187 : char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
709 : 371187 : static int master_count = 0;
710 : :
711 : 371187 : if (ns->proc_name == NULL)
712 : 370519 : return;
713 : :
714 : : /* No need to do anything if this procedure doesn't have alternate entry
715 : : points. */
716 : 371139 : if (!ns->entries)
717 : : return;
718 : :
719 : : /* We may already have resolved alternate entry points. */
720 : 918 : if (ns->proc_name->attr.entry_master)
721 : : return;
722 : :
723 : : /* If this isn't a procedure something has gone horribly wrong. */
724 : 668 : gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
725 : :
726 : : /* Remember the current namespace. */
727 : 668 : old_ns = gfc_current_ns;
728 : :
729 : 668 : gfc_current_ns = ns;
730 : :
731 : : /* Add the main entry point to the list of entry points. */
732 : 668 : el = gfc_get_entry_list ();
733 : 668 : el->sym = ns->proc_name;
734 : 668 : el->id = 0;
735 : 668 : el->next = ns->entries;
736 : 668 : ns->entries = el;
737 : 668 : ns->proc_name->attr.entry = 1;
738 : :
739 : : /* If it is a module function, it needs to be in the right namespace
740 : : so that gfc_get_fake_result_decl can gather up the results. The
741 : : need for this arose in get_proc_name, where these beasts were
742 : : left in their own namespace, to keep prior references linked to
743 : : the entry declaration.*/
744 : 668 : if (ns->proc_name->attr.function
745 : 564 : && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
746 : 188 : el->sym->ns = ns;
747 : :
748 : : /* Do the same for entries where the master is not a module
749 : : procedure. These are retained in the module namespace because
750 : : of the module procedure declaration. */
751 : 1420 : for (el = el->next; el; el = el->next)
752 : 752 : if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
753 : 0 : && el->sym->attr.mod_proc)
754 : 0 : el->sym->ns = ns;
755 : 668 : el = ns->entries;
756 : :
757 : : /* Add an entry statement for it. */
758 : 668 : c = gfc_get_code (EXEC_ENTRY);
759 : 668 : c->ext.entry = el;
760 : 668 : c->next = ns->code;
761 : 668 : ns->code = c;
762 : :
763 : : /* Create a new symbol for the master function. */
764 : : /* Give the internal function a unique name (within this file).
765 : : Also include the function name so the user has some hope of figuring
766 : : out what is going on. */
767 : 668 : snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
768 : 668 : master_count++, ns->proc_name->name);
769 : 668 : gfc_get_ha_symbol (name, &proc);
770 : 668 : gcc_assert (proc != NULL);
771 : :
772 : 668 : gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
773 : 668 : if (ns->proc_name->attr.subroutine)
774 : 104 : gfc_add_subroutine (&proc->attr, proc->name, NULL);
775 : : else
776 : : {
777 : 564 : gfc_symbol *sym;
778 : 564 : gfc_typespec *ts, *fts;
779 : 564 : gfc_array_spec *as, *fas;
780 : 564 : gfc_add_function (&proc->attr, proc->name, NULL);
781 : 564 : proc->result = proc;
782 : 564 : fas = ns->entries->sym->as;
783 : 564 : fas = fas ? fas : ns->entries->sym->result->as;
784 : 564 : fts = &ns->entries->sym->result->ts;
785 : 564 : if (fts->type == BT_UNKNOWN)
786 : 51 : fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
787 : 1058 : for (el = ns->entries->next; el; el = el->next)
788 : : {
789 : 603 : ts = &el->sym->result->ts;
790 : 603 : as = el->sym->as;
791 : 603 : as = as ? as : el->sym->result->as;
792 : 603 : if (ts->type == BT_UNKNOWN)
793 : 61 : ts = gfc_get_default_type (el->sym->result->name, NULL);
794 : :
795 : 603 : if (! gfc_compare_types (ts, fts)
796 : 497 : || (el->sym->result->attr.dimension
797 : 497 : != ns->entries->sym->result->attr.dimension)
798 : 603 : || (el->sym->result->attr.pointer
799 : 497 : != ns->entries->sym->result->attr.pointer))
800 : : break;
801 : 65 : else if (as && fas && ns->entries->sym->result != el->sym->result
802 : 559 : && gfc_compare_array_spec (as, fas) == 0)
803 : 5 : gfc_error ("Function %s at %L has entries with mismatched "
804 : : "array specifications", ns->entries->sym->name,
805 : 5 : &ns->entries->sym->declared_at);
806 : : /* The characteristics need to match and thus both need to have
807 : : the same string length, i.e. both len=*, or both len=4.
808 : : Having both len=<variable> is also possible, but difficult to
809 : : check at compile time. */
810 : 492 : else if (ts->type == BT_CHARACTER
811 : 89 : && (el->sym->result->attr.allocatable
812 : 89 : != ns->entries->sym->result->attr.allocatable))
813 : : {
814 : 3 : gfc_error ("Function %s at %L has entry %s with mismatched "
815 : : "characteristics", ns->entries->sym->name,
816 : : &ns->entries->sym->declared_at, el->sym->name);
817 : 3 : goto cleanup;
818 : : }
819 : 489 : else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
820 : 86 : && (((ts->u.cl->length && !fts->u.cl->length)
821 : 85 : ||(!ts->u.cl->length && fts->u.cl->length))
822 : 66 : || (ts->u.cl->length
823 : 29 : && ts->u.cl->length->expr_type
824 : 29 : != fts->u.cl->length->expr_type)
825 : 66 : || (ts->u.cl->length
826 : 29 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
827 : 28 : && mpz_cmp (ts->u.cl->length->value.integer,
828 : 28 : fts->u.cl->length->value.integer) != 0)))
829 : 21 : gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
830 : : "entries returning variables of different "
831 : : "string lengths", ns->entries->sym->name,
832 : 21 : &ns->entries->sym->declared_at);
833 : 468 : else if (el->sym->result->attr.allocatable
834 : 468 : != ns->entries->sym->result->attr.allocatable)
835 : : break;
836 : : }
837 : :
838 : 561 : if (el == NULL)
839 : : {
840 : 455 : sym = ns->entries->sym->result;
841 : : /* All result types the same. */
842 : 455 : proc->ts = *fts;
843 : 455 : if (sym->attr.dimension)
844 : 63 : gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
845 : 455 : if (sym->attr.pointer)
846 : 78 : gfc_add_pointer (&proc->attr, NULL);
847 : 455 : if (sym->attr.allocatable)
848 : 24 : gfc_add_allocatable (&proc->attr, NULL);
849 : : }
850 : : else
851 : : {
852 : : /* Otherwise the result will be passed through a union by
853 : : reference. */
854 : 106 : proc->attr.mixed_entry_master = 1;
855 : 340 : for (el = ns->entries; el; el = el->next)
856 : : {
857 : 234 : sym = el->sym->result;
858 : 234 : if (sym->attr.dimension)
859 : : {
860 : 1 : if (el == ns->entries)
861 : 0 : gfc_error ("FUNCTION result %s cannot be an array in "
862 : : "FUNCTION %s at %L", sym->name,
863 : 0 : ns->entries->sym->name, &sym->declared_at);
864 : : else
865 : 1 : gfc_error ("ENTRY result %s cannot be an array in "
866 : : "FUNCTION %s at %L", sym->name,
867 : 1 : ns->entries->sym->name, &sym->declared_at);
868 : : }
869 : 233 : else if (sym->attr.pointer)
870 : : {
871 : 1 : if (el == ns->entries)
872 : 1 : gfc_error ("FUNCTION result %s cannot be a POINTER in "
873 : : "FUNCTION %s at %L", sym->name,
874 : 1 : ns->entries->sym->name, &sym->declared_at);
875 : : else
876 : 0 : gfc_error ("ENTRY result %s cannot be a POINTER in "
877 : : "FUNCTION %s at %L", sym->name,
878 : 0 : ns->entries->sym->name, &sym->declared_at);
879 : : }
880 : 232 : else if (sym->attr.allocatable)
881 : : {
882 : 0 : if (el == ns->entries)
883 : 0 : gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
884 : : "FUNCTION %s at %L", sym->name,
885 : 0 : ns->entries->sym->name, &sym->declared_at);
886 : : else
887 : 0 : gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
888 : : "FUNCTION %s at %L", sym->name,
889 : 0 : ns->entries->sym->name, &sym->declared_at);
890 : : }
891 : : else
892 : : {
893 : 232 : ts = &sym->ts;
894 : 232 : if (ts->type == BT_UNKNOWN)
895 : 9 : ts = gfc_get_default_type (sym->name, NULL);
896 : 232 : switch (ts->type)
897 : : {
898 : 84 : case BT_INTEGER:
899 : 84 : if (ts->kind == gfc_default_integer_kind)
900 : : sym = NULL;
901 : : break;
902 : 99 : case BT_REAL:
903 : 99 : if (ts->kind == gfc_default_real_kind
904 : 18 : || ts->kind == gfc_default_double_kind)
905 : : sym = NULL;
906 : : break;
907 : 19 : case BT_COMPLEX:
908 : 19 : if (ts->kind == gfc_default_complex_kind)
909 : : sym = NULL;
910 : : break;
911 : 27 : case BT_LOGICAL:
912 : 27 : if (ts->kind == gfc_default_logical_kind)
913 : : sym = NULL;
914 : : break;
915 : : case BT_UNKNOWN:
916 : : /* We will issue error elsewhere. */
917 : : sym = NULL;
918 : : break;
919 : : default:
920 : : break;
921 : : }
922 : 3 : if (sym)
923 : : {
924 : 3 : if (el == ns->entries)
925 : 1 : gfc_error ("FUNCTION result %s cannot be of type %s "
926 : : "in FUNCTION %s at %L", sym->name,
927 : 1 : gfc_typename (ts), ns->entries->sym->name,
928 : : &sym->declared_at);
929 : : else
930 : 2 : gfc_error ("ENTRY result %s cannot be of type %s "
931 : : "in FUNCTION %s at %L", sym->name,
932 : 2 : gfc_typename (ts), ns->entries->sym->name,
933 : : &sym->declared_at);
934 : : }
935 : : }
936 : : }
937 : : }
938 : : }
939 : :
940 : 106 : cleanup:
941 : 668 : proc->attr.access = ACCESS_PRIVATE;
942 : 668 : proc->attr.entry_master = 1;
943 : :
944 : : /* Merge all the entry point arguments. */
945 : 2088 : for (el = ns->entries; el; el = el->next)
946 : 1420 : merge_argument_lists (proc, el->sym->formal);
947 : :
948 : : /* Check the master formal arguments for any that are not
949 : : present in all entry points. */
950 : 2088 : for (el = ns->entries; el; el = el->next)
951 : 1420 : check_argument_lists (proc, el->sym->formal);
952 : :
953 : : /* Use the master function for the function body. */
954 : 668 : ns->proc_name = proc;
955 : :
956 : : /* Finalize the new symbols. */
957 : 668 : gfc_commit_symbols ();
958 : :
959 : : /* Restore the original namespace. */
960 : 668 : gfc_current_ns = old_ns;
961 : : }
962 : :
963 : :
964 : : /* Forward declaration. */
965 : : static bool is_non_constant_shape_array (gfc_symbol *sym);
966 : :
967 : :
968 : : /* Resolve common variables. */
969 : : static void
970 : 337520 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
971 : : {
972 : 337520 : gfc_symbol *csym = common_block->head;
973 : 337520 : gfc_gsymbol *gsym;
974 : :
975 : 343491 : for (; csym; csym = csym->common_next)
976 : : {
977 : 5971 : gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
978 : 5971 : if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
979 : : {
980 : 3 : if (csym->common_block)
981 : 2 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
982 : : "COMMON block at %L", gsym->name,
983 : : &gsym->where, &csym->common_block->where);
984 : : else
985 : 1 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
986 : : "COMMON block", gsym->name, &gsym->where);
987 : : }
988 : :
989 : : /* gfc_add_in_common may have been called before, but the reported errors
990 : : have been ignored to continue parsing.
991 : : We do the checks again here, unless the symbol is USE associated. */
992 : 5971 : if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
993 : : {
994 : 5698 : gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
995 : 5698 : gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
996 : : &common_block->where);
997 : : }
998 : :
999 : 5971 : if (csym->value || csym->attr.data)
1000 : : {
1001 : 132 : if (!csym->ns->is_block_data)
1002 : 32 : gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1003 : : "but only in BLOCK DATA initialization is "
1004 : : "allowed", csym->name, &csym->declared_at);
1005 : 100 : else if (!named_common)
1006 : 8 : gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1007 : : "in a blank COMMON but initialization is only "
1008 : : "allowed in named common blocks", csym->name,
1009 : : &csym->declared_at);
1010 : : }
1011 : :
1012 : 5971 : if (UNLIMITED_POLY (csym))
1013 : 1 : gfc_error_now ("%qs at %L cannot appear in COMMON "
1014 : : "[F2008:C5100]", csym->name, &csym->declared_at);
1015 : :
1016 : 5971 : if (csym->attr.dimension && is_non_constant_shape_array (csym))
1017 : : {
1018 : 1 : gfc_error_now ("Automatic object %qs at %L cannot appear in "
1019 : : "COMMON at %L", csym->name, &csym->declared_at,
1020 : : &common_block->where);
1021 : : /* Avoid confusing follow-on error. */
1022 : 1 : csym->error = 1;
1023 : : }
1024 : :
1025 : 5971 : if (csym->ts.type != BT_DERIVED)
1026 : 5924 : continue;
1027 : :
1028 : 47 : if (!(csym->ts.u.derived->attr.sequence
1029 : 3 : || csym->ts.u.derived->attr.is_bind_c))
1030 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1031 : : "has neither the SEQUENCE nor the BIND(C) "
1032 : : "attribute", csym->name, &csym->declared_at);
1033 : 47 : if (csym->ts.u.derived->attr.alloc_comp)
1034 : 3 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1035 : : "has an ultimate component that is "
1036 : : "allocatable", csym->name, &csym->declared_at);
1037 : 47 : if (gfc_has_default_initializer (csym->ts.u.derived))
1038 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1039 : : "may not have default initializer", csym->name,
1040 : : &csym->declared_at);
1041 : :
1042 : 47 : if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1043 : 16 : gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1044 : : }
1045 : 337520 : }
1046 : :
1047 : : /* Resolve common blocks. */
1048 : : static void
1049 : 336088 : resolve_common_blocks (gfc_symtree *common_root)
1050 : : {
1051 : 336088 : gfc_symbol *sym = NULL;
1052 : 336088 : gfc_gsymbol * gsym;
1053 : :
1054 : 336088 : if (common_root == NULL)
1055 : 335966 : return;
1056 : :
1057 : 1897 : if (common_root->left)
1058 : 213 : resolve_common_blocks (common_root->left);
1059 : 1897 : if (common_root->right)
1060 : 252 : resolve_common_blocks (common_root->right);
1061 : :
1062 : 1897 : resolve_common_vars (common_root->n.common, true);
1063 : :
1064 : : /* The common name is a global name - in Fortran 2003 also if it has a
1065 : : C binding name, since Fortran 2008 only the C binding name is a global
1066 : : identifier. */
1067 : 1897 : if (!common_root->n.common->binding_label
1068 : 1897 : || gfc_notification_std (GFC_STD_F2008))
1069 : : {
1070 : 3650 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1071 : 1825 : common_root->n.common->name);
1072 : :
1073 : 820 : if (gsym && gfc_notification_std (GFC_STD_F2008)
1074 : 14 : && gsym->type == GSYM_COMMON
1075 : 1838 : && ((common_root->n.common->binding_label
1076 : 6 : && (!gsym->binding_label
1077 : 0 : || strcmp (common_root->n.common->binding_label,
1078 : : gsym->binding_label) != 0))
1079 : 7 : || (!common_root->n.common->binding_label
1080 : 7 : && gsym->binding_label)))
1081 : : {
1082 : 6 : gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1083 : : "identifier and must thus have the same binding name "
1084 : : "as the same-named COMMON block at %L: %s vs %s",
1085 : 6 : common_root->n.common->name, &common_root->n.common->where,
1086 : : &gsym->where,
1087 : : common_root->n.common->binding_label
1088 : : ? common_root->n.common->binding_label : "(blank)",
1089 : 6 : gsym->binding_label ? gsym->binding_label : "(blank)");
1090 : 6 : return;
1091 : : }
1092 : :
1093 : 1819 : if (gsym && gsym->type != GSYM_COMMON
1094 : 1 : && !common_root->n.common->binding_label)
1095 : : {
1096 : 0 : gfc_error ("COMMON block %qs at %L uses the same global identifier "
1097 : : "as entity at %L",
1098 : 0 : common_root->n.common->name, &common_root->n.common->where,
1099 : : &gsym->where);
1100 : 0 : return;
1101 : : }
1102 : 814 : if (gsym && gsym->type != GSYM_COMMON)
1103 : : {
1104 : 1 : gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1105 : : "%L sharing the identifier with global non-COMMON-block "
1106 : 1 : "entity at %L", common_root->n.common->name,
1107 : 1 : &common_root->n.common->where, &gsym->where);
1108 : 1 : return;
1109 : : }
1110 : 1005 : if (!gsym)
1111 : : {
1112 : 1005 : gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1113 : 1005 : gsym->type = GSYM_COMMON;
1114 : 1005 : gsym->where = common_root->n.common->where;
1115 : 1005 : gsym->defined = 1;
1116 : : }
1117 : 1818 : gsym->used = 1;
1118 : : }
1119 : :
1120 : 1890 : if (common_root->n.common->binding_label)
1121 : : {
1122 : 76 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1123 : : common_root->n.common->binding_label);
1124 : 76 : if (gsym && gsym->type != GSYM_COMMON)
1125 : : {
1126 : 1 : gfc_error ("COMMON block at %L with binding label %qs uses the same "
1127 : : "global identifier as entity at %L",
1128 : : &common_root->n.common->where,
1129 : 1 : common_root->n.common->binding_label, &gsym->where);
1130 : 1 : return;
1131 : : }
1132 : 57 : if (!gsym)
1133 : : {
1134 : 57 : gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1135 : 57 : gsym->type = GSYM_COMMON;
1136 : 57 : gsym->where = common_root->n.common->where;
1137 : 57 : gsym->defined = 1;
1138 : : }
1139 : 75 : gsym->used = 1;
1140 : : }
1141 : :
1142 : 1889 : gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1143 : 1889 : if (sym == NULL)
1144 : : return;
1145 : :
1146 : 122 : if (sym->attr.flavor == FL_PARAMETER)
1147 : 2 : gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1148 : 2 : sym->name, &common_root->n.common->where, &sym->declared_at);
1149 : :
1150 : 122 : if (sym->attr.external)
1151 : 1 : gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1152 : 1 : sym->name, &common_root->n.common->where);
1153 : :
1154 : 122 : if (sym->attr.intrinsic)
1155 : 2 : gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1156 : 2 : sym->name, &common_root->n.common->where);
1157 : 120 : else if (sym->attr.result
1158 : 120 : || gfc_is_function_return_value (sym, gfc_current_ns))
1159 : 1 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1160 : : "that is also a function result", sym->name,
1161 : 1 : &common_root->n.common->where);
1162 : 119 : else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1163 : 5 : && sym->attr.proc != PROC_ST_FUNCTION)
1164 : 3 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1165 : : "that is also a global procedure", sym->name,
1166 : 3 : &common_root->n.common->where);
1167 : : }
1168 : :
1169 : :
1170 : : /* Resolve contained function types. Because contained functions can call one
1171 : : another, they have to be worked out before any of the contained procedures
1172 : : can be resolved.
1173 : :
1174 : : The good news is that if a function doesn't already have a type, the only
1175 : : way it can get one is through an IMPLICIT type or a RESULT variable, because
1176 : : by definition contained functions are contained namespace they're contained
1177 : : in, not in a sibling or parent namespace. */
1178 : :
1179 : : static void
1180 : 335623 : resolve_contained_functions (gfc_namespace *ns)
1181 : : {
1182 : 335623 : gfc_namespace *child;
1183 : 335623 : gfc_entry_list *el;
1184 : :
1185 : 335623 : resolve_formal_arglists (ns);
1186 : :
1187 : 371187 : for (child = ns->contained; child; child = child->sibling)
1188 : : {
1189 : : /* Resolve alternate entry points first. */
1190 : 35564 : resolve_entries (child);
1191 : :
1192 : : /* Then check function return types. */
1193 : 35564 : resolve_contained_fntype (child->proc_name, child);
1194 : 36069 : for (el = child->entries; el; el = el->next)
1195 : 505 : resolve_contained_fntype (el->sym, child);
1196 : : }
1197 : 335623 : }
1198 : :
1199 : :
1200 : :
1201 : : /* A Parameterized Derived Type constructor must contain values for
1202 : : the PDT KIND parameters or they must have a default initializer.
1203 : : Go through the constructor picking out the KIND expressions,
1204 : : storing them in 'param_list' and then call gfc_get_pdt_instance
1205 : : to obtain the PDT instance. */
1206 : :
1207 : : static gfc_actual_arglist *param_list, *param_tail, *param;
1208 : :
1209 : : static bool
1210 : 74 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1211 : : {
1212 : 74 : param = gfc_get_actual_arglist ();
1213 : 74 : if (!param_list)
1214 : 68 : param_list = param_tail = param;
1215 : : else
1216 : : {
1217 : 6 : param_tail->next = param;
1218 : 6 : param_tail = param_tail->next;
1219 : : }
1220 : :
1221 : 74 : param_tail->name = c->name;
1222 : 74 : if (expr)
1223 : 74 : param_tail->expr = gfc_copy_expr (expr);
1224 : 0 : else if (c->initializer)
1225 : 0 : param_tail->expr = gfc_copy_expr (c->initializer);
1226 : : else
1227 : : {
1228 : 0 : param_tail->spec_type = SPEC_ASSUMED;
1229 : 0 : if (c->attr.pdt_kind)
1230 : : {
1231 : 0 : gfc_error ("The KIND parameter %qs in the PDT constructor "
1232 : : "at %C has no value", param->name);
1233 : 0 : return false;
1234 : : }
1235 : : }
1236 : :
1237 : : return true;
1238 : : }
1239 : :
1240 : : static bool
1241 : 68 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1242 : : gfc_symbol *derived)
1243 : : {
1244 : 68 : gfc_constructor *cons = NULL;
1245 : 68 : gfc_component *comp;
1246 : 68 : bool t = true;
1247 : :
1248 : 68 : if (expr && expr->expr_type == EXPR_STRUCTURE)
1249 : 68 : cons = gfc_constructor_first (expr->value.constructor);
1250 : 0 : else if (constr)
1251 : 0 : cons = *constr;
1252 : 68 : gcc_assert (cons);
1253 : :
1254 : 68 : comp = derived->components;
1255 : :
1256 : 228 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1257 : : {
1258 : 160 : if (cons->expr
1259 : 160 : && cons->expr->expr_type == EXPR_STRUCTURE
1260 : 0 : && comp->ts.type == BT_DERIVED)
1261 : : {
1262 : 0 : t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1263 : 0 : if (!t)
1264 : : return t;
1265 : : }
1266 : 160 : else if (comp->ts.type == BT_DERIVED)
1267 : : {
1268 : 0 : t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1269 : 0 : if (!t)
1270 : : return t;
1271 : : }
1272 : 160 : else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1273 : 74 : && derived->attr.pdt_template)
1274 : : {
1275 : 74 : t = get_pdt_spec_expr (comp, cons->expr);
1276 : 74 : if (!t)
1277 : : return t;
1278 : : }
1279 : : }
1280 : : return t;
1281 : : }
1282 : :
1283 : :
1284 : : static bool resolve_fl_derived0 (gfc_symbol *sym);
1285 : : static bool resolve_fl_struct (gfc_symbol *sym);
1286 : :
1287 : :
1288 : : /* Resolve all of the elements of a structure constructor and make sure that
1289 : : the types are correct. The 'init' flag indicates that the given
1290 : : constructor is an initializer. */
1291 : :
1292 : : static bool
1293 : 60271 : resolve_structure_cons (gfc_expr *expr, int init)
1294 : : {
1295 : 60271 : gfc_constructor *cons;
1296 : 60271 : gfc_component *comp;
1297 : 60271 : bool t;
1298 : 60271 : symbol_attribute a;
1299 : :
1300 : 60271 : t = true;
1301 : :
1302 : 60271 : if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1303 : : {
1304 : 57478 : if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1305 : 57328 : resolve_fl_derived0 (expr->ts.u.derived);
1306 : : else
1307 : 150 : resolve_fl_struct (expr->ts.u.derived);
1308 : :
1309 : : /* If this is a Parameterized Derived Type template, find the
1310 : : instance corresponding to the PDT kind parameters. */
1311 : 57478 : if (expr->ts.u.derived->attr.pdt_template)
1312 : : {
1313 : 68 : param_list = NULL;
1314 : 68 : t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1315 : 68 : if (!t)
1316 : : return t;
1317 : 68 : gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1318 : :
1319 : 68 : expr->param_list = gfc_copy_actual_arglist (param_list);
1320 : :
1321 : 68 : if (param_list)
1322 : 68 : gfc_free_actual_arglist (param_list);
1323 : :
1324 : 68 : if (!expr->ts.u.derived->attr.pdt_type)
1325 : : return false;
1326 : : }
1327 : : }
1328 : :
1329 : : /* A constructor may have references if it is the result of substituting a
1330 : : parameter variable. In this case we just pull out the component we
1331 : : want. */
1332 : 60271 : if (expr->ref)
1333 : 160 : comp = expr->ref->u.c.sym->components;
1334 : 60111 : else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1335 : : || expr->ts.type == BT_UNION)
1336 : 60109 : && expr->ts.u.derived)
1337 : 60109 : comp = expr->ts.u.derived->components;
1338 : : else
1339 : : return false;
1340 : :
1341 : 60269 : cons = gfc_constructor_first (expr->value.constructor);
1342 : :
1343 : 199119 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1344 : : {
1345 : 138852 : int rank;
1346 : :
1347 : 138852 : if (!cons->expr)
1348 : 9362 : continue;
1349 : :
1350 : : /* Unions use an EXPR_NULL contrived expression to tell the translation
1351 : : phase to generate an initializer of the appropriate length.
1352 : : Ignore it here. */
1353 : 129490 : if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1354 : 15 : continue;
1355 : :
1356 : 129475 : if (!gfc_resolve_expr (cons->expr))
1357 : : {
1358 : 0 : t = false;
1359 : 0 : continue;
1360 : : }
1361 : :
1362 : 129475 : rank = comp->as ? comp->as->rank : 0;
1363 : 129475 : if (comp->ts.type == BT_CLASS
1364 : 1729 : && !comp->ts.u.derived->attr.unlimited_polymorphic
1365 : 1728 : && CLASS_DATA (comp)->as)
1366 : 511 : rank = CLASS_DATA (comp)->as->rank;
1367 : :
1368 : 129475 : if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1369 : 215 : gfc_find_vtab (&cons->expr->ts);
1370 : :
1371 : 129475 : if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1372 : 453 : && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
1373 : : {
1374 : 4 : gfc_error ("The rank of the element in the structure "
1375 : : "constructor at %L does not match that of the "
1376 : : "component (%d/%d)", &cons->expr->where,
1377 : : cons->expr->rank, rank);
1378 : 4 : t = false;
1379 : : }
1380 : :
1381 : : /* If we don't have the right type, try to convert it. */
1382 : :
1383 : 226670 : if (!comp->attr.proc_pointer &&
1384 : 97195 : !gfc_compare_types (&cons->expr->ts, &comp->ts))
1385 : : {
1386 : 11804 : if (strcmp (comp->name, "_extends") == 0)
1387 : : {
1388 : : /* Can afford to be brutal with the _extends initializer.
1389 : : The derived type can get lost because it is PRIVATE
1390 : : but it is not usage constrained by the standard. */
1391 : 8578 : cons->expr->ts = comp->ts;
1392 : : }
1393 : 3226 : else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1394 : : {
1395 : 2 : gfc_error ("The element in the structure constructor at %L, "
1396 : : "for pointer component %qs, is %s but should be %s",
1397 : 2 : &cons->expr->where, comp->name,
1398 : 2 : gfc_basic_typename (cons->expr->ts.type),
1399 : : gfc_basic_typename (comp->ts.type));
1400 : 2 : t = false;
1401 : : }
1402 : 3224 : else if (!UNLIMITED_POLY (comp))
1403 : : {
1404 : 3162 : bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1405 : 3162 : if (t)
1406 : 129475 : t = t2;
1407 : : }
1408 : : }
1409 : :
1410 : : /* For strings, the length of the constructor should be the same as
1411 : : the one of the structure, ensure this if the lengths are known at
1412 : : compile time and when we are dealing with PARAMETER or structure
1413 : : constructors. */
1414 : 129475 : if (cons->expr->ts.type == BT_CHARACTER
1415 : 3824 : && comp->ts.type == BT_CHARACTER
1416 : 3799 : && comp->ts.u.cl && comp->ts.u.cl->length
1417 : 2471 : && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1418 : 2436 : && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1419 : 920 : && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1420 : 920 : && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1421 : 920 : && comp->ts.u.cl->length->ts.type == BT_INTEGER
1422 : 920 : && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1423 : 920 : comp->ts.u.cl->length->value.integer) != 0)
1424 : : {
1425 : 11 : if (comp->attr.pointer)
1426 : : {
1427 : 3 : HOST_WIDE_INT la, lb;
1428 : 3 : la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1429 : 3 : lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1430 : 3 : gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1431 : : "component %qs in constructor at %L",
1432 : 3 : la, lb, comp->name, &cons->expr->where);
1433 : 3 : t = false;
1434 : : }
1435 : :
1436 : 11 : if (cons->expr->expr_type == EXPR_VARIABLE
1437 : 4 : && cons->expr->rank != 0
1438 : 2 : && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1439 : : {
1440 : : /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1441 : : to make use of the gfc_resolve_character_array_constructor
1442 : : machinery. The expression is later simplified away to
1443 : : an array of string literals. */
1444 : 1 : gfc_expr *para = cons->expr;
1445 : 1 : cons->expr = gfc_get_expr ();
1446 : 1 : cons->expr->ts = para->ts;
1447 : 1 : cons->expr->where = para->where;
1448 : 1 : cons->expr->expr_type = EXPR_ARRAY;
1449 : 1 : cons->expr->rank = para->rank;
1450 : 1 : cons->expr->corank = para->corank;
1451 : 1 : cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1452 : 1 : gfc_constructor_append_expr (&cons->expr->value.constructor,
1453 : 1 : para, &cons->expr->where);
1454 : : }
1455 : :
1456 : 11 : if (cons->expr->expr_type == EXPR_ARRAY)
1457 : : {
1458 : : /* Rely on the cleanup of the namespace to deal correctly with
1459 : : the old charlen. (There was a block here that attempted to
1460 : : remove the charlen but broke the chain in so doing.) */
1461 : 5 : cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1462 : 5 : cons->expr->ts.u.cl->length_from_typespec = true;
1463 : 5 : cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1464 : 5 : gfc_resolve_character_array_constructor (cons->expr);
1465 : : }
1466 : : }
1467 : :
1468 : 129475 : if (cons->expr->expr_type == EXPR_NULL
1469 : 38533 : && !(comp->attr.pointer || comp->attr.allocatable
1470 : 19554 : || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1471 : 1091 : || (comp->ts.type == BT_CLASS
1472 : 1089 : && (CLASS_DATA (comp)->attr.class_pointer
1473 : 878 : || CLASS_DATA (comp)->attr.allocatable))))
1474 : : {
1475 : 2 : t = false;
1476 : 2 : gfc_error ("The NULL in the structure constructor at %L is "
1477 : : "being applied to component %qs, which is neither "
1478 : : "a POINTER nor ALLOCATABLE", &cons->expr->where,
1479 : : comp->name);
1480 : : }
1481 : :
1482 : 129475 : if (comp->attr.proc_pointer && comp->ts.interface)
1483 : : {
1484 : : /* Check procedure pointer interface. */
1485 : 14094 : gfc_symbol *s2 = NULL;
1486 : 14094 : gfc_component *c2;
1487 : 14094 : const char *name;
1488 : 14094 : char err[200];
1489 : :
1490 : 14094 : c2 = gfc_get_proc_ptr_comp (cons->expr);
1491 : 14094 : if (c2)
1492 : : {
1493 : 12 : s2 = c2->ts.interface;
1494 : 12 : name = c2->name;
1495 : : }
1496 : 14082 : else if (cons->expr->expr_type == EXPR_FUNCTION)
1497 : : {
1498 : 0 : s2 = cons->expr->symtree->n.sym->result;
1499 : 0 : name = cons->expr->symtree->n.sym->result->name;
1500 : : }
1501 : 14082 : else if (cons->expr->expr_type != EXPR_NULL)
1502 : : {
1503 : 13712 : s2 = cons->expr->symtree->n.sym;
1504 : 13712 : name = cons->expr->symtree->n.sym->name;
1505 : : }
1506 : :
1507 : 13724 : if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1508 : : err, sizeof (err), NULL, NULL))
1509 : : {
1510 : 2 : gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1511 : : "component %qs in structure constructor at %L:"
1512 : 2 : " %s", comp->name, &cons->expr->where, err);
1513 : 2 : return false;
1514 : : }
1515 : : }
1516 : :
1517 : : /* Validate shape, except for dynamic or PDT arrays. */
1518 : 129473 : if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1519 : 1912 : && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1520 : 1223 : && !comp->attr.pdt_array)
1521 : : {
1522 : 1221 : mpz_t len;
1523 : 1221 : mpz_init (len);
1524 : 2539 : for (int n = 0; n < rank; n++)
1525 : : {
1526 : 1319 : if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1527 : 1318 : || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1528 : : {
1529 : 1 : gfc_error ("Bad array spec of component %qs referenced in "
1530 : : "structure constructor at %L",
1531 : 1 : comp->name, &cons->expr->where);
1532 : 1 : t = false;
1533 : 1 : break;
1534 : 1318 : };
1535 : 1318 : if (cons->expr->shape == NULL)
1536 : 12 : continue;
1537 : 1306 : mpz_set_ui (len, 1);
1538 : 1306 : mpz_add (len, len, comp->as->upper[n]->value.integer);
1539 : 1306 : mpz_sub (len, len, comp->as->lower[n]->value.integer);
1540 : 1306 : if (mpz_cmp (cons->expr->shape[n], len) != 0)
1541 : : {
1542 : 9 : gfc_error ("The shape of component %qs in the structure "
1543 : : "constructor at %L differs from the shape of the "
1544 : : "declared component for dimension %d (%ld/%ld)",
1545 : : comp->name, &cons->expr->where, n+1,
1546 : : mpz_get_si (cons->expr->shape[n]),
1547 : : mpz_get_si (len));
1548 : 9 : t = false;
1549 : : }
1550 : : }
1551 : 1221 : mpz_clear (len);
1552 : : }
1553 : :
1554 : 129473 : if (!comp->attr.pointer || comp->attr.proc_pointer
1555 : 20739 : || cons->expr->expr_type == EXPR_NULL)
1556 : 119968 : continue;
1557 : :
1558 : 9505 : a = gfc_expr_attr (cons->expr);
1559 : :
1560 : 9505 : if (!a.pointer && !a.target)
1561 : : {
1562 : 1 : t = false;
1563 : 1 : gfc_error ("The element in the structure constructor at %L, "
1564 : : "for pointer component %qs should be a POINTER or "
1565 : 1 : "a TARGET", &cons->expr->where, comp->name);
1566 : : }
1567 : :
1568 : 9505 : if (init)
1569 : : {
1570 : : /* F08:C461. Additional checks for pointer initialization. */
1571 : 9437 : if (a.allocatable)
1572 : : {
1573 : 0 : t = false;
1574 : 0 : gfc_error ("Pointer initialization target at %L "
1575 : 0 : "must not be ALLOCATABLE", &cons->expr->where);
1576 : : }
1577 : 9437 : if (!a.save)
1578 : : {
1579 : 0 : t = false;
1580 : 0 : gfc_error ("Pointer initialization target at %L "
1581 : 0 : "must have the SAVE attribute", &cons->expr->where);
1582 : : }
1583 : : }
1584 : :
1585 : : /* F2023:C770: A designator that is an initial-data-target shall ...
1586 : : not have a vector subscript. */
1587 : 9505 : if (comp->attr.pointer && (a.pointer || a.target)
1588 : 19009 : && gfc_has_vector_index (cons->expr))
1589 : : {
1590 : 1 : gfc_error ("Pointer assignment target at %L has a vector subscript",
1591 : 1 : &cons->expr->where);
1592 : 1 : t = false;
1593 : : }
1594 : :
1595 : : /* F2003, C1272 (3). */
1596 : 9505 : bool impure = cons->expr->expr_type == EXPR_VARIABLE
1597 : 9505 : && (gfc_impure_variable (cons->expr->symtree->n.sym)
1598 : 9469 : || gfc_is_coindexed (cons->expr));
1599 : 33 : if (impure && gfc_pure (NULL))
1600 : : {
1601 : 1 : t = false;
1602 : 1 : gfc_error ("Invalid expression in the structure constructor for "
1603 : : "pointer component %qs at %L in PURE procedure",
1604 : 1 : comp->name, &cons->expr->where);
1605 : : }
1606 : :
1607 : 9505 : if (impure)
1608 : 33 : gfc_unset_implicit_pure (NULL);
1609 : : }
1610 : :
1611 : : return t;
1612 : : }
1613 : :
1614 : :
1615 : : /****************** Expression name resolution ******************/
1616 : :
1617 : : /* Returns 0 if a symbol was not declared with a type or
1618 : : attribute declaration statement, nonzero otherwise. */
1619 : :
1620 : : static bool
1621 : 727782 : was_declared (gfc_symbol *sym)
1622 : : {
1623 : 727782 : symbol_attribute a;
1624 : :
1625 : 727782 : a = sym->attr;
1626 : :
1627 : 727782 : if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1628 : : return 1;
1629 : :
1630 : 616377 : if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1631 : 608079 : || a.optional || a.pointer || a.save || a.target || a.volatile_
1632 : 608077 : || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1633 : 608023 : || a.asynchronous || a.codimension || a.subroutine)
1634 : 93208 : return 1;
1635 : :
1636 : : return 0;
1637 : : }
1638 : :
1639 : :
1640 : : /* Determine if a symbol is generic or not. */
1641 : :
1642 : : static int
1643 : 403605 : generic_sym (gfc_symbol *sym)
1644 : : {
1645 : 403605 : gfc_symbol *s;
1646 : :
1647 : 403605 : if (sym->attr.generic ||
1648 : 375461 : (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1649 : 29207 : return 1;
1650 : :
1651 : 374398 : if (was_declared (sym) || sym->ns->parent == NULL)
1652 : : return 0;
1653 : :
1654 : 75617 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1655 : :
1656 : 75617 : if (s != NULL)
1657 : : {
1658 : 133 : if (s == sym)
1659 : : return 0;
1660 : : else
1661 : 132 : return generic_sym (s);
1662 : : }
1663 : :
1664 : : return 0;
1665 : : }
1666 : :
1667 : :
1668 : : /* Determine if a symbol is specific or not. */
1669 : :
1670 : : static int
1671 : 374310 : specific_sym (gfc_symbol *sym)
1672 : : {
1673 : 374310 : gfc_symbol *s;
1674 : :
1675 : 374310 : if (sym->attr.if_source == IFSRC_IFBODY
1676 : 363289 : || sym->attr.proc == PROC_MODULE
1677 : : || sym->attr.proc == PROC_INTERNAL
1678 : : || sym->attr.proc == PROC_ST_FUNCTION
1679 : 288363 : || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1680 : 661942 : || sym->attr.external)
1681 : 89061 : return 1;
1682 : :
1683 : 285249 : if (was_declared (sym) || sym->ns->parent == NULL)
1684 : : return 0;
1685 : :
1686 : 75515 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1687 : :
1688 : 75515 : return (s == NULL) ? 0 : specific_sym (s);
1689 : : }
1690 : :
1691 : :
1692 : : /* Figure out if the procedure is specific, generic or unknown. */
1693 : :
1694 : : enum proc_type
1695 : : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1696 : :
1697 : : static proc_type
1698 : 403327 : procedure_kind (gfc_symbol *sym)
1699 : : {
1700 : 403327 : if (generic_sym (sym))
1701 : : return PTYPE_GENERIC;
1702 : :
1703 : 374263 : if (specific_sym (sym))
1704 : 89061 : return PTYPE_SPECIFIC;
1705 : :
1706 : : return PTYPE_UNKNOWN;
1707 : : }
1708 : :
1709 : : /* Check references to assumed size arrays. The flag need_full_assumed_size
1710 : : is nonzero when matching actual arguments. */
1711 : :
1712 : : static int need_full_assumed_size = 0;
1713 : :
1714 : : static bool
1715 : 1392453 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1716 : : {
1717 : 1392453 : if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1718 : : return false;
1719 : :
1720 : : /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1721 : : What should it be? */
1722 : 3788 : if (e->ref
1723 : 3786 : && e->ref->u.ar.as
1724 : 3785 : && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1725 : 3290 : && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1726 : 3290 : && (e->ref->u.ar.type == AR_FULL))
1727 : : {
1728 : 25 : gfc_error ("The upper bound in the last dimension must "
1729 : : "appear in the reference to the assumed size "
1730 : : "array %qs at %L", sym->name, &e->where);
1731 : 25 : return true;
1732 : : }
1733 : : return false;
1734 : : }
1735 : :
1736 : :
1737 : : /* Look for bad assumed size array references in argument expressions
1738 : : of elemental and array valued intrinsic procedures. Since this is
1739 : : called from procedure resolution functions, it only recurses at
1740 : : operators. */
1741 : :
1742 : : static bool
1743 : 224811 : resolve_assumed_size_actual (gfc_expr *e)
1744 : : {
1745 : 224811 : if (e == NULL)
1746 : : return false;
1747 : :
1748 : 224320 : switch (e->expr_type)
1749 : : {
1750 : 108118 : case EXPR_VARIABLE:
1751 : 108118 : if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1752 : : return true;
1753 : : break;
1754 : :
1755 : 47189 : case EXPR_OP:
1756 : 47189 : if (resolve_assumed_size_actual (e->value.op.op1)
1757 : 47189 : || resolve_assumed_size_actual (e->value.op.op2))
1758 : 0 : return true;
1759 : : break;
1760 : :
1761 : : default:
1762 : : break;
1763 : : }
1764 : : return false;
1765 : : }
1766 : :
1767 : :
1768 : : /* Check a generic procedure, passed as an actual argument, to see if
1769 : : there is a matching specific name. If none, it is an error, and if
1770 : : more than one, the reference is ambiguous. */
1771 : : static int
1772 : 8 : count_specific_procs (gfc_expr *e)
1773 : : {
1774 : 8 : int n;
1775 : 8 : gfc_interface *p;
1776 : 8 : gfc_symbol *sym;
1777 : :
1778 : 8 : n = 0;
1779 : 8 : sym = e->symtree->n.sym;
1780 : :
1781 : 22 : for (p = sym->generic; p; p = p->next)
1782 : 14 : if (strcmp (sym->name, p->sym->name) == 0)
1783 : : {
1784 : 8 : e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1785 : : sym->name);
1786 : 8 : n++;
1787 : : }
1788 : :
1789 : 8 : if (n > 1)
1790 : 1 : gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1791 : : &e->where);
1792 : :
1793 : 8 : if (n == 0)
1794 : 1 : gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1795 : : "argument at %L", sym->name, &e->where);
1796 : :
1797 : 8 : return n;
1798 : : }
1799 : :
1800 : :
1801 : : /* See if a call to sym could possibly be a not allowed RECURSION because of
1802 : : a missing RECURSIVE declaration. This means that either sym is the current
1803 : : context itself, or sym is the parent of a contained procedure calling its
1804 : : non-RECURSIVE containing procedure.
1805 : : This also works if sym is an ENTRY. */
1806 : :
1807 : : static bool
1808 : 145837 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1809 : : {
1810 : 145837 : gfc_symbol* proc_sym;
1811 : 145837 : gfc_symbol* context_proc;
1812 : 145837 : gfc_namespace* real_context;
1813 : :
1814 : 145837 : if (sym->attr.flavor == FL_PROGRAM
1815 : : || gfc_fl_struct (sym->attr.flavor))
1816 : : return false;
1817 : :
1818 : : /* If we've got an ENTRY, find real procedure. */
1819 : 145836 : if (sym->attr.entry && sym->ns->entries)
1820 : 45 : proc_sym = sym->ns->entries->sym;
1821 : : else
1822 : : proc_sym = sym;
1823 : :
1824 : : /* If sym is RECURSIVE, all is well of course. */
1825 : 145836 : if (proc_sym->attr.recursive || flag_recursive)
1826 : : return false;
1827 : :
1828 : : /* Find the context procedure's "real" symbol if it has entries.
1829 : : We look for a procedure symbol, so recurse on the parents if we don't
1830 : : find one (like in case of a BLOCK construct). */
1831 : 1698 : for (real_context = context; ; real_context = real_context->parent)
1832 : : {
1833 : : /* We should find something, eventually! */
1834 : 125305 : gcc_assert (real_context);
1835 : :
1836 : 125305 : context_proc = (real_context->entries ? real_context->entries->sym
1837 : : : real_context->proc_name);
1838 : :
1839 : : /* In some special cases, there may not be a proc_name, like for this
1840 : : invalid code:
1841 : : real(bad_kind()) function foo () ...
1842 : : when checking the call to bad_kind ().
1843 : : In these cases, we simply return here and assume that the
1844 : : call is ok. */
1845 : 125305 : if (!context_proc)
1846 : : return false;
1847 : :
1848 : 125041 : if (context_proc->attr.flavor != FL_LABEL)
1849 : : break;
1850 : : }
1851 : :
1852 : : /* A call from sym's body to itself is recursion, of course. */
1853 : 123343 : if (context_proc == proc_sym)
1854 : : return true;
1855 : :
1856 : : /* The same is true if context is a contained procedure and sym the
1857 : : containing one. */
1858 : 123329 : if (context_proc->attr.contained)
1859 : : {
1860 : 20478 : gfc_symbol* parent_proc;
1861 : :
1862 : 20478 : gcc_assert (context->parent);
1863 : 20478 : parent_proc = (context->parent->entries ? context->parent->entries->sym
1864 : : : context->parent->proc_name);
1865 : :
1866 : 20478 : if (parent_proc == proc_sym)
1867 : 9 : return true;
1868 : : }
1869 : :
1870 : : return false;
1871 : : }
1872 : :
1873 : :
1874 : : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1875 : : its typespec and formal argument list. */
1876 : :
1877 : : bool
1878 : 41072 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1879 : : {
1880 : 41072 : gfc_intrinsic_sym* isym = NULL;
1881 : 41072 : const char* symstd;
1882 : :
1883 : 41072 : if (sym->resolve_symbol_called >= 2)
1884 : : return true;
1885 : :
1886 : 31458 : sym->resolve_symbol_called = 2;
1887 : :
1888 : : /* Already resolved. */
1889 : 31458 : if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1890 : : return true;
1891 : :
1892 : : /* We already know this one is an intrinsic, so we don't call
1893 : : gfc_is_intrinsic for full checking but rather use gfc_find_function and
1894 : : gfc_find_subroutine directly to check whether it is a function or
1895 : : subroutine. */
1896 : :
1897 : 23704 : if (sym->intmod_sym_id && sym->attr.subroutine)
1898 : : {
1899 : 8541 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1900 : 8541 : isym = gfc_intrinsic_subroutine_by_id (id);
1901 : 8541 : }
1902 : 15163 : else if (sym->intmod_sym_id)
1903 : : {
1904 : 11787 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1905 : 11787 : isym = gfc_intrinsic_function_by_id (id);
1906 : : }
1907 : 3376 : else if (!sym->attr.subroutine)
1908 : 3304 : isym = gfc_find_function (sym->name);
1909 : :
1910 : 23632 : if (isym && !sym->attr.subroutine)
1911 : : {
1912 : 15052 : if (sym->ts.type != BT_UNKNOWN && warn_surprising
1913 : 24 : && !sym->attr.implicit_type)
1914 : 10 : gfc_warning (OPT_Wsurprising,
1915 : : "Type specified for intrinsic function %qs at %L is"
1916 : : " ignored", sym->name, &sym->declared_at);
1917 : :
1918 : 18633 : if (!sym->attr.function &&
1919 : 3581 : !gfc_add_function(&sym->attr, sym->name, loc))
1920 : : return false;
1921 : :
1922 : 15052 : sym->ts = isym->ts;
1923 : : }
1924 : 8652 : else if (isym || (isym = gfc_find_subroutine (sym->name)))
1925 : : {
1926 : 8649 : if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1927 : : {
1928 : 1 : gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1929 : : " specifier", sym->name, &sym->declared_at);
1930 : 1 : return false;
1931 : : }
1932 : :
1933 : 8683 : if (!sym->attr.subroutine &&
1934 : 35 : !gfc_add_subroutine(&sym->attr, sym->name, loc))
1935 : : return false;
1936 : : }
1937 : : else
1938 : : {
1939 : 3 : gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1940 : : &sym->declared_at);
1941 : 3 : return false;
1942 : : }
1943 : :
1944 : 23699 : gfc_copy_formal_args_intr (sym, isym, NULL);
1945 : :
1946 : 23699 : sym->attr.pure = isym->pure;
1947 : 23699 : sym->attr.elemental = isym->elemental;
1948 : :
1949 : : /* Check it is actually available in the standard settings. */
1950 : 23699 : if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1951 : : {
1952 : 31 : gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1953 : : "available in the current standard settings but %s. Use "
1954 : : "an appropriate %<-std=*%> option or enable "
1955 : : "%<-fall-intrinsics%> in order to use it.",
1956 : : sym->name, &sym->declared_at, symstd);
1957 : 31 : return false;
1958 : : }
1959 : :
1960 : : return true;
1961 : : }
1962 : :
1963 : :
1964 : : /* Resolve a procedure expression, like passing it to a called procedure or as
1965 : : RHS for a procedure pointer assignment. */
1966 : :
1967 : : static bool
1968 : 1296747 : resolve_procedure_expression (gfc_expr* expr)
1969 : : {
1970 : 1296747 : gfc_symbol* sym;
1971 : :
1972 : 1296747 : if (expr->expr_type != EXPR_VARIABLE)
1973 : : return true;
1974 : 1296730 : gcc_assert (expr->symtree);
1975 : :
1976 : 1296730 : sym = expr->symtree->n.sym;
1977 : :
1978 : 1296730 : if (sym->attr.intrinsic)
1979 : 1346 : gfc_resolve_intrinsic (sym, &expr->where);
1980 : :
1981 : 1296730 : if (sym->attr.flavor != FL_PROCEDURE
1982 : 29864 : || (sym->attr.function && sym->result == sym))
1983 : : return true;
1984 : :
1985 : : /* A non-RECURSIVE procedure that is used as procedure expression within its
1986 : : own body is in danger of being called recursively. */
1987 : 14092 : if (is_illegal_recursion (sym, gfc_current_ns))
1988 : : {
1989 : 10 : if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
1990 : 0 : gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
1991 : : " possibly calling itself recursively in procedure %qs. "
1992 : : " Declare it RECURSIVE or use %<-frecursive%>",
1993 : 0 : sym->name, sym->module, gfc_current_ns->proc_name->name);
1994 : : else
1995 : 10 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1996 : : " itself recursively. Declare it RECURSIVE or use"
1997 : : " %<-frecursive%>", sym->name, &expr->where);
1998 : : }
1999 : :
2000 : : return true;
2001 : : }
2002 : :
2003 : :
2004 : : /* Check that name is not a derived type. */
2005 : :
2006 : : static bool
2007 : 3107 : is_dt_name (const char *name)
2008 : : {
2009 : 3107 : gfc_symbol *dt_list, *dt_first;
2010 : :
2011 : 3107 : dt_list = dt_first = gfc_derived_types;
2012 : 5519 : for (; dt_list; dt_list = dt_list->dt_next)
2013 : : {
2014 : 3427 : if (strcmp(dt_list->name, name) == 0)
2015 : : return true;
2016 : 3424 : if (dt_first == dt_list->dt_next)
2017 : : break;
2018 : : }
2019 : : return false;
2020 : : }
2021 : :
2022 : :
2023 : : /* Resolve an actual argument list. Most of the time, this is just
2024 : : resolving the expressions in the list.
2025 : : The exception is that we sometimes have to decide whether arguments
2026 : : that look like procedure arguments are really simple variable
2027 : : references. */
2028 : :
2029 : : static bool
2030 : 416977 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2031 : : bool no_formal_args)
2032 : : {
2033 : 416977 : gfc_symbol *sym = NULL;
2034 : 416977 : gfc_symtree *parent_st;
2035 : 416977 : gfc_expr *e;
2036 : 416977 : gfc_component *comp;
2037 : 416977 : int save_need_full_assumed_size;
2038 : 416977 : bool return_value = false;
2039 : 416977 : bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2040 : :
2041 : 416977 : actual_arg = true;
2042 : 416977 : first_actual_arg = true;
2043 : :
2044 : 1073784 : for (; arg; arg = arg->next)
2045 : : {
2046 : 656908 : e = arg->expr;
2047 : 656908 : if (e == NULL)
2048 : : {
2049 : : /* Check the label is a valid branching target. */
2050 : 2358 : if (arg->label)
2051 : : {
2052 : 236 : if (arg->label->defined == ST_LABEL_UNKNOWN)
2053 : : {
2054 : 0 : gfc_error ("Label %d referenced at %L is never defined",
2055 : : arg->label->value, &arg->label->where);
2056 : 0 : goto cleanup;
2057 : : }
2058 : : }
2059 : 2358 : first_actual_arg = false;
2060 : 2358 : continue;
2061 : : }
2062 : :
2063 : 654550 : if (e->expr_type == EXPR_VARIABLE
2064 : 289243 : && e->symtree->n.sym->attr.generic
2065 : 8 : && no_formal_args
2066 : 654555 : && count_specific_procs (e) != 1)
2067 : 2 : goto cleanup;
2068 : :
2069 : 654548 : if (e->ts.type != BT_PROCEDURE)
2070 : : {
2071 : 583306 : save_need_full_assumed_size = need_full_assumed_size;
2072 : 583306 : if (e->expr_type != EXPR_VARIABLE)
2073 : 365307 : need_full_assumed_size = 0;
2074 : 583306 : if (!gfc_resolve_expr (e))
2075 : 60 : goto cleanup;
2076 : 583246 : need_full_assumed_size = save_need_full_assumed_size;
2077 : 583246 : goto argument_list;
2078 : : }
2079 : :
2080 : : /* See if the expression node should really be a variable reference. */
2081 : :
2082 : 71242 : sym = e->symtree->n.sym;
2083 : :
2084 : 71242 : if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2085 : : {
2086 : 3 : gfc_error ("Derived type %qs is used as an actual "
2087 : : "argument at %L", sym->name, &e->where);
2088 : 3 : goto cleanup;
2089 : : }
2090 : :
2091 : 71239 : if (sym->attr.flavor == FL_PROCEDURE
2092 : 68135 : || sym->attr.intrinsic
2093 : 68135 : || sym->attr.external)
2094 : : {
2095 : 3104 : int actual_ok;
2096 : :
2097 : : /* If a procedure is not already determined to be something else
2098 : : check if it is intrinsic. */
2099 : 3104 : if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2100 : 1254 : sym->attr.intrinsic = 1;
2101 : :
2102 : 3104 : if (sym->attr.proc == PROC_ST_FUNCTION)
2103 : : {
2104 : 2 : gfc_error ("Statement function %qs at %L is not allowed as an "
2105 : : "actual argument", sym->name, &e->where);
2106 : : }
2107 : :
2108 : 6208 : actual_ok = gfc_intrinsic_actual_ok (sym->name,
2109 : 3104 : sym->attr.subroutine);
2110 : 3104 : if (sym->attr.intrinsic && actual_ok == 0)
2111 : : {
2112 : 0 : gfc_error ("Intrinsic %qs at %L is not allowed as an "
2113 : : "actual argument", sym->name, &e->where);
2114 : : }
2115 : :
2116 : 3104 : if (sym->attr.contained && !sym->attr.use_assoc
2117 : 404 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
2118 : : {
2119 : 216 : if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2120 : : " used as actual argument at %L",
2121 : : sym->name, &e->where))
2122 : 3 : goto cleanup;
2123 : : }
2124 : :
2125 : 3101 : if (sym->attr.elemental && !sym->attr.intrinsic)
2126 : : {
2127 : 2 : gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2128 : : "allowed as an actual argument at %L", sym->name,
2129 : : &e->where);
2130 : : }
2131 : :
2132 : : /* Check if a generic interface has a specific procedure
2133 : : with the same name before emitting an error. */
2134 : 3101 : if (sym->attr.generic && count_specific_procs (e) != 1)
2135 : 0 : goto cleanup;
2136 : :
2137 : : /* Just in case a specific was found for the expression. */
2138 : 3101 : sym = e->symtree->n.sym;
2139 : :
2140 : : /* If the symbol is the function that names the current (or
2141 : : parent) scope, then we really have a variable reference. */
2142 : :
2143 : 3101 : if (gfc_is_function_return_value (sym, sym->ns))
2144 : 0 : goto got_variable;
2145 : :
2146 : : /* If all else fails, see if we have a specific intrinsic. */
2147 : 3101 : if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2148 : : {
2149 : 0 : gfc_intrinsic_sym *isym;
2150 : :
2151 : 0 : isym = gfc_find_function (sym->name);
2152 : 0 : if (isym == NULL || !isym->specific)
2153 : : {
2154 : 0 : gfc_error ("Unable to find a specific INTRINSIC procedure "
2155 : : "for the reference %qs at %L", sym->name,
2156 : : &e->where);
2157 : 0 : goto cleanup;
2158 : : }
2159 : 0 : sym->ts = isym->ts;
2160 : 0 : sym->attr.intrinsic = 1;
2161 : 0 : sym->attr.function = 1;
2162 : : }
2163 : :
2164 : 3101 : if (!gfc_resolve_expr (e))
2165 : 0 : goto cleanup;
2166 : 3101 : goto argument_list;
2167 : : }
2168 : :
2169 : : /* See if the name is a module procedure in a parent unit. */
2170 : :
2171 : 68135 : if (was_declared (sym) || sym->ns->parent == NULL)
2172 : 68042 : goto got_variable;
2173 : :
2174 : 93 : if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2175 : : {
2176 : 0 : gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2177 : 0 : goto cleanup;
2178 : : }
2179 : :
2180 : 93 : if (parent_st == NULL)
2181 : 93 : goto got_variable;
2182 : :
2183 : 0 : sym = parent_st->n.sym;
2184 : 0 : e->symtree = parent_st; /* Point to the right thing. */
2185 : :
2186 : 0 : if (sym->attr.flavor == FL_PROCEDURE
2187 : 0 : || sym->attr.intrinsic
2188 : 0 : || sym->attr.external)
2189 : : {
2190 : 0 : if (!gfc_resolve_expr (e))
2191 : 0 : goto cleanup;
2192 : 0 : goto argument_list;
2193 : : }
2194 : :
2195 : 0 : got_variable:
2196 : 68135 : e->expr_type = EXPR_VARIABLE;
2197 : 68135 : e->ts = sym->ts;
2198 : 68135 : if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2199 : 35425 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2200 : 3775 : && CLASS_DATA (sym)->as))
2201 : : {
2202 : 38144 : gfc_array_spec *as
2203 : 35427 : = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2204 : 35427 : e->rank = as->rank;
2205 : 35427 : e->corank = as->corank;
2206 : 35427 : e->ref = gfc_get_ref ();
2207 : 35427 : e->ref->type = REF_ARRAY;
2208 : 35427 : e->ref->u.ar.type = AR_FULL;
2209 : 35427 : e->ref->u.ar.as = as;
2210 : : }
2211 : :
2212 : : /* These symbols are set untyped by calls to gfc_set_default_type
2213 : : with 'error_flag' = false. Reset the untyped attribute so that
2214 : : the error will be generated in gfc_resolve_expr. */
2215 : 68135 : if (e->expr_type == EXPR_VARIABLE
2216 : 68135 : && sym->ts.type == BT_UNKNOWN
2217 : 36 : && sym->attr.untyped)
2218 : 5 : sym->attr.untyped = 0;
2219 : :
2220 : : /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2221 : : primary.cc (match_actual_arg). If above code determines that it
2222 : : is a variable instead, it needs to be resolved as it was not
2223 : : done at the beginning of this function. */
2224 : 68135 : save_need_full_assumed_size = need_full_assumed_size;
2225 : 68135 : if (e->expr_type != EXPR_VARIABLE)
2226 : 0 : need_full_assumed_size = 0;
2227 : 68135 : if (!gfc_resolve_expr (e))
2228 : 22 : goto cleanup;
2229 : 68113 : need_full_assumed_size = save_need_full_assumed_size;
2230 : :
2231 : 654460 : argument_list:
2232 : : /* Check argument list functions %VAL, %LOC and %REF. There is
2233 : : nothing to do for %REF. */
2234 : 654460 : if (arg->name && arg->name[0] == '%')
2235 : : {
2236 : 42 : if (strcmp ("%VAL", arg->name) == 0)
2237 : : {
2238 : 28 : if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2239 : : {
2240 : 2 : gfc_error ("By-value argument at %L is not of numeric "
2241 : : "type", &e->where);
2242 : 2 : goto cleanup;
2243 : : }
2244 : :
2245 : 26 : if (e->rank)
2246 : : {
2247 : 1 : gfc_error ("By-value argument at %L cannot be an array or "
2248 : : "an array section", &e->where);
2249 : 1 : goto cleanup;
2250 : : }
2251 : :
2252 : : /* Intrinsics are still PROC_UNKNOWN here. However,
2253 : : since same file external procedures are not resolvable
2254 : : in gfortran, it is a good deal easier to leave them to
2255 : : intrinsic.cc. */
2256 : 25 : if (ptype != PROC_UNKNOWN
2257 : 25 : && ptype != PROC_DUMMY
2258 : 9 : && ptype != PROC_EXTERNAL
2259 : 9 : && ptype != PROC_MODULE)
2260 : : {
2261 : 3 : gfc_error ("By-value argument at %L is not allowed "
2262 : : "in this context", &e->where);
2263 : 3 : goto cleanup;
2264 : : }
2265 : : }
2266 : :
2267 : : /* Statement functions have already been excluded above. */
2268 : 14 : else if (strcmp ("%LOC", arg->name) == 0
2269 : 8 : && e->ts.type == BT_PROCEDURE)
2270 : : {
2271 : 0 : if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2272 : : {
2273 : 0 : gfc_error ("Passing internal procedure at %L by location "
2274 : : "not allowed", &e->where);
2275 : 0 : goto cleanup;
2276 : : }
2277 : : }
2278 : : }
2279 : :
2280 : 654454 : comp = gfc_get_proc_ptr_comp(e);
2281 : 654454 : if (e->expr_type == EXPR_VARIABLE
2282 : 287865 : && comp && comp->attr.elemental)
2283 : : {
2284 : 1 : gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2285 : : "allowed as an actual argument at %L", comp->name,
2286 : : &e->where);
2287 : : }
2288 : :
2289 : : /* Fortran 2008, C1237. */
2290 : 287865 : if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2291 : 654760 : && gfc_has_ultimate_pointer (e))
2292 : : {
2293 : 3 : gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2294 : : "component", &e->where);
2295 : 3 : goto cleanup;
2296 : : }
2297 : :
2298 : 654451 : if (e->expr_type == EXPR_VARIABLE
2299 : 287862 : && e->ts.type == BT_PROCEDURE
2300 : 3101 : && no_formal_args
2301 : 1451 : && sym->attr.flavor == FL_PROCEDURE
2302 : 1451 : && sym->attr.if_source == IFSRC_UNKNOWN
2303 : 142 : && !sym->attr.external
2304 : 2 : && !sym->attr.intrinsic
2305 : 2 : && !sym->attr.artificial
2306 : 2 : && !sym->ts.interface)
2307 : : {
2308 : : /* Emit a warning for -std=legacy and an error otherwise. */
2309 : 2 : if (gfc_option.warn_std == 0)
2310 : 0 : gfc_warning (0, "Procedure %qs at %L used as actual argument but "
2311 : : "does neither have an explicit interface nor the "
2312 : : "EXTERNAL attribute", sym->name, &e->where);
2313 : : else
2314 : : {
2315 : 2 : gfc_error ("Procedure %qs at %L used as actual argument but "
2316 : : "does neither have an explicit interface nor the "
2317 : : "EXTERNAL attribute", sym->name, &e->where);
2318 : 2 : goto cleanup;
2319 : : }
2320 : : }
2321 : :
2322 : 654449 : first_actual_arg = false;
2323 : : }
2324 : :
2325 : : return_value = true;
2326 : :
2327 : 416977 : cleanup:
2328 : 416977 : actual_arg = actual_arg_sav;
2329 : 416977 : first_actual_arg = first_actual_arg_sav;
2330 : :
2331 : 416977 : return return_value;
2332 : : }
2333 : :
2334 : :
2335 : : /* Do the checks of the actual argument list that are specific to elemental
2336 : : procedures. If called with c == NULL, we have a function, otherwise if
2337 : : expr == NULL, we have a subroutine. */
2338 : :
2339 : : static bool
2340 : 318015 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2341 : : {
2342 : 318015 : gfc_actual_arglist *arg0;
2343 : 318015 : gfc_actual_arglist *arg;
2344 : 318015 : gfc_symbol *esym = NULL;
2345 : 318015 : gfc_intrinsic_sym *isym = NULL;
2346 : 318015 : gfc_expr *e = NULL;
2347 : 318015 : gfc_intrinsic_arg *iformal = NULL;
2348 : 318015 : gfc_formal_arglist *eformal = NULL;
2349 : 318015 : bool formal_optional = false;
2350 : 318015 : bool set_by_optional = false;
2351 : 318015 : int i;
2352 : 318015 : int rank = 0;
2353 : :
2354 : : /* Is this an elemental procedure? */
2355 : 318015 : if (expr && expr->value.function.actual != NULL)
2356 : : {
2357 : 230835 : if (expr->value.function.esym != NULL
2358 : 43378 : && expr->value.function.esym->attr.elemental)
2359 : : {
2360 : : arg0 = expr->value.function.actual;
2361 : : esym = expr->value.function.esym;
2362 : : }
2363 : 214561 : else if (expr->value.function.isym != NULL
2364 : 186434 : && expr->value.function.isym->elemental)
2365 : : {
2366 : : arg0 = expr->value.function.actual;
2367 : : isym = expr->value.function.isym;
2368 : : }
2369 : : else
2370 : : return true;
2371 : : }
2372 : 87180 : else if (c && c->ext.actual != NULL)
2373 : : {
2374 : 69089 : arg0 = c->ext.actual;
2375 : :
2376 : 69089 : if (c->resolved_sym)
2377 : : esym = c->resolved_sym;
2378 : : else
2379 : 313 : esym = c->symtree->n.sym;
2380 : 69089 : gcc_assert (esym);
2381 : :
2382 : 69089 : if (!esym->attr.elemental)
2383 : : return true;
2384 : : }
2385 : : else
2386 : : return true;
2387 : :
2388 : : /* The rank of an elemental is the rank of its array argument(s). */
2389 : 171698 : for (arg = arg0; arg; arg = arg->next)
2390 : : {
2391 : 111260 : if (arg->expr != NULL && arg->expr->rank != 0)
2392 : : {
2393 : 10364 : rank = arg->expr->rank;
2394 : 10364 : if (arg->expr->expr_type == EXPR_VARIABLE
2395 : 5216 : && arg->expr->symtree->n.sym->attr.optional)
2396 : 10364 : set_by_optional = true;
2397 : :
2398 : : /* Function specific; set the result rank and shape. */
2399 : 10364 : if (expr)
2400 : : {
2401 : 8196 : expr->rank = rank;
2402 : 8196 : expr->corank = arg->expr->corank;
2403 : 8196 : if (!expr->shape && arg->expr->shape)
2404 : : {
2405 : 3923 : expr->shape = gfc_get_shape (rank);
2406 : 8638 : for (i = 0; i < rank; i++)
2407 : 4715 : mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2408 : : }
2409 : : }
2410 : : break;
2411 : : }
2412 : : }
2413 : :
2414 : : /* If it is an array, it shall not be supplied as an actual argument
2415 : : to an elemental procedure unless an array of the same rank is supplied
2416 : : as an actual argument corresponding to a nonoptional dummy argument of
2417 : : that elemental procedure(12.4.1.5). */
2418 : 70802 : formal_optional = false;
2419 : 70802 : if (isym)
2420 : 48561 : iformal = isym->formal;
2421 : : else
2422 : 22241 : eformal = esym->formal;
2423 : :
2424 : 187347 : for (arg = arg0; arg; arg = arg->next)
2425 : : {
2426 : 116545 : if (eformal)
2427 : : {
2428 : 39899 : if (eformal->sym && eformal->sym->attr.optional)
2429 : 39899 : formal_optional = true;
2430 : 39899 : eformal = eformal->next;
2431 : : }
2432 : 76646 : else if (isym && iformal)
2433 : : {
2434 : 66616 : if (iformal->optional)
2435 : 13268 : formal_optional = true;
2436 : 66616 : iformal = iformal->next;
2437 : : }
2438 : 10030 : else if (isym)
2439 : 10022 : formal_optional = true;
2440 : :
2441 : 116545 : if (pedantic && arg->expr != NULL
2442 : 71141 : && arg->expr->expr_type == EXPR_VARIABLE
2443 : 34459 : && arg->expr->symtree->n.sym->attr.optional
2444 : 572 : && formal_optional
2445 : 479 : && arg->expr->rank
2446 : 153 : && (set_by_optional || arg->expr->rank != rank)
2447 : 42 : && !(isym && isym->id == GFC_ISYM_CONVERSION))
2448 : : {
2449 : 114 : bool t = false;
2450 : : gfc_actual_arglist *a;
2451 : :
2452 : : /* Scan the argument list for a non-optional argument with the
2453 : : same rank as arg. */
2454 : 114 : for (a = arg0; a; a = a->next)
2455 : 87 : if (a != arg
2456 : 45 : && a->expr->rank == arg->expr->rank
2457 : 39 : && (a->expr->expr_type != EXPR_VARIABLE
2458 : 37 : || (a->expr->expr_type == EXPR_VARIABLE
2459 : 37 : && !a->expr->symtree->n.sym->attr.optional)))
2460 : : {
2461 : : t = true;
2462 : : break;
2463 : : }
2464 : :
2465 : 42 : if (!t)
2466 : 27 : gfc_warning (OPT_Wpedantic,
2467 : : "%qs at %L is an array and OPTIONAL; If it is not "
2468 : : "present, then it cannot be the actual argument of "
2469 : : "an ELEMENTAL procedure unless there is a non-optional"
2470 : : " argument with the same rank "
2471 : : "(Fortran 2018, 15.5.2.12)",
2472 : : arg->expr->symtree->n.sym->name, &arg->expr->where);
2473 : : }
2474 : : }
2475 : :
2476 : 187336 : for (arg = arg0; arg; arg = arg->next)
2477 : : {
2478 : 116543 : if (arg->expr == NULL || arg->expr->rank == 0)
2479 : 103519 : continue;
2480 : :
2481 : : /* Being elemental, the last upper bound of an assumed size array
2482 : : argument must be present. */
2483 : 13024 : if (resolve_assumed_size_actual (arg->expr))
2484 : : return false;
2485 : :
2486 : : /* Elemental procedure's array actual arguments must conform. */
2487 : 13021 : if (e != NULL)
2488 : : {
2489 : 2660 : if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2490 : : return false;
2491 : : }
2492 : : else
2493 : 10361 : e = arg->expr;
2494 : : }
2495 : :
2496 : : /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2497 : : is an array, the intent inout/out variable needs to be also an array. */
2498 : 70793 : if (rank > 0 && esym && expr == NULL)
2499 : 6637 : for (eformal = esym->formal, arg = arg0; arg && eformal;
2500 : 4475 : arg = arg->next, eformal = eformal->next)
2501 : 4477 : if (eformal->sym
2502 : 4476 : && (eformal->sym->attr.intent == INTENT_OUT
2503 : 3394 : || eformal->sym->attr.intent == INTENT_INOUT)
2504 : 1476 : && arg->expr && arg->expr->rank == 0)
2505 : : {
2506 : 2 : gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2507 : : "ELEMENTAL subroutine %qs is a scalar, but another "
2508 : : "actual argument is an array", &arg->expr->where,
2509 : : (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2510 : : : "INOUT", eformal->sym->name, esym->name);
2511 : 2 : return false;
2512 : : }
2513 : : return true;
2514 : : }
2515 : :
2516 : :
2517 : : /* This function does the checking of references to global procedures
2518 : : as defined in sections 18.1 and 14.1, respectively, of the Fortran
2519 : : 77 and 95 standards. It checks for a gsymbol for the name, making
2520 : : one if it does not already exist. If it already exists, then the
2521 : : reference being resolved must correspond to the type of gsymbol.
2522 : : Otherwise, the new symbol is equipped with the attributes of the
2523 : : reference. The corresponding code that is called in creating
2524 : : global entities is parse.cc.
2525 : :
2526 : : In addition, for all but -std=legacy, the gsymbols are used to
2527 : : check the interfaces of external procedures from the same file.
2528 : : The namespace of the gsymbol is resolved and then, once this is
2529 : : done the interface is checked. */
2530 : :
2531 : :
2532 : : static bool
2533 : 14829 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2534 : : {
2535 : 14829 : if (!gsym_ns->proc_name->attr.recursive)
2536 : : return true;
2537 : :
2538 : 151 : if (sym->ns == gsym_ns)
2539 : : return false;
2540 : :
2541 : 151 : if (sym->ns->parent && sym->ns->parent == gsym_ns)
2542 : 0 : return false;
2543 : :
2544 : : return true;
2545 : : }
2546 : :
2547 : : static bool
2548 : 14829 : not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2549 : : {
2550 : 14829 : if (gsym_ns->entries)
2551 : : {
2552 : : gfc_entry_list *entry = gsym_ns->entries;
2553 : :
2554 : 3234 : for (; entry; entry = entry->next)
2555 : : {
2556 : 2281 : if (strcmp (sym->name, entry->sym->name) == 0)
2557 : : {
2558 : 946 : if (strcmp (gsym_ns->proc_name->name,
2559 : 946 : sym->ns->proc_name->name) == 0)
2560 : : return false;
2561 : :
2562 : 946 : if (sym->ns->parent
2563 : 0 : && strcmp (gsym_ns->proc_name->name,
2564 : 0 : sym->ns->parent->proc_name->name) == 0)
2565 : : return false;
2566 : : }
2567 : : }
2568 : : }
2569 : : return true;
2570 : : }
2571 : :
2572 : :
2573 : : /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2574 : :
2575 : : bool
2576 : 15648 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2577 : : {
2578 : 15648 : gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2579 : :
2580 : 58670 : for ( ; arg; arg = arg->next)
2581 : : {
2582 : 27744 : if (!arg->sym)
2583 : 157 : continue;
2584 : :
2585 : 27587 : if (arg->sym->attr.allocatable) /* (2a) */
2586 : : {
2587 : 0 : strncpy (errmsg, _("allocatable argument"), err_len);
2588 : 0 : return true;
2589 : : }
2590 : 27587 : else if (arg->sym->attr.asynchronous)
2591 : : {
2592 : 0 : strncpy (errmsg, _("asynchronous argument"), err_len);
2593 : 0 : return true;
2594 : : }
2595 : 27587 : else if (arg->sym->attr.optional)
2596 : : {
2597 : 75 : strncpy (errmsg, _("optional argument"), err_len);
2598 : 75 : return true;
2599 : : }
2600 : 27512 : else if (arg->sym->attr.pointer)
2601 : : {
2602 : 12 : strncpy (errmsg, _("pointer argument"), err_len);
2603 : 12 : return true;
2604 : : }
2605 : 27500 : else if (arg->sym->attr.target)
2606 : : {
2607 : 72 : strncpy (errmsg, _("target argument"), err_len);
2608 : 72 : return true;
2609 : : }
2610 : 27428 : else if (arg->sym->attr.value)
2611 : : {
2612 : 0 : strncpy (errmsg, _("value argument"), err_len);
2613 : 0 : return true;
2614 : : }
2615 : 27428 : else if (arg->sym->attr.volatile_)
2616 : : {
2617 : 1 : strncpy (errmsg, _("volatile argument"), err_len);
2618 : 1 : return true;
2619 : : }
2620 : 27427 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2621 : : {
2622 : 45 : strncpy (errmsg, _("assumed-shape argument"), err_len);
2623 : 45 : return true;
2624 : : }
2625 : 27382 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2626 : : {
2627 : 1 : strncpy (errmsg, _("assumed-rank argument"), err_len);
2628 : 1 : return true;
2629 : : }
2630 : 27381 : else if (arg->sym->attr.codimension) /* (2c) */
2631 : : {
2632 : 1 : strncpy (errmsg, _("coarray argument"), err_len);
2633 : 1 : return true;
2634 : : }
2635 : 27380 : else if (false) /* (2d) TODO: parametrized derived type */
2636 : : {
2637 : : strncpy (errmsg, _("parametrized derived type argument"), err_len);
2638 : : return true;
2639 : : }
2640 : 27380 : else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2641 : : {
2642 : 162 : strncpy (errmsg, _("polymorphic argument"), err_len);
2643 : 162 : return true;
2644 : : }
2645 : 27218 : else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2646 : : {
2647 : 0 : strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2648 : 0 : return true;
2649 : : }
2650 : 27218 : else if (arg->sym->ts.type == BT_ASSUMED)
2651 : : {
2652 : : /* As assumed-type is unlimited polymorphic (cf. above).
2653 : : See also TS 29113, Note 6.1. */
2654 : 1 : strncpy (errmsg, _("assumed-type argument"), err_len);
2655 : 1 : return true;
2656 : : }
2657 : : }
2658 : :
2659 : 15278 : if (sym->attr.function)
2660 : : {
2661 : 3455 : gfc_symbol *res = sym->result ? sym->result : sym;
2662 : :
2663 : 3455 : if (res->attr.dimension) /* (3a) */
2664 : : {
2665 : 93 : strncpy (errmsg, _("array result"), err_len);
2666 : 93 : return true;
2667 : : }
2668 : 3362 : else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2669 : : {
2670 : 38 : strncpy (errmsg, _("pointer or allocatable result"), err_len);
2671 : 38 : return true;
2672 : : }
2673 : 3324 : else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2674 : 347 : && res->ts.u.cl->length
2675 : 166 : && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2676 : : {
2677 : 12 : strncpy (errmsg, _("result with non-constant character length"), err_len);
2678 : 12 : return true;
2679 : : }
2680 : : }
2681 : :
2682 : 15135 : if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2683 : : {
2684 : 7 : strncpy (errmsg, _("elemental procedure"), err_len);
2685 : 7 : return true;
2686 : : }
2687 : 15128 : else if (sym->attr.is_bind_c) /* (5) */
2688 : : {
2689 : 0 : strncpy (errmsg, _("bind(c) procedure"), err_len);
2690 : 0 : return true;
2691 : : }
2692 : :
2693 : : return false;
2694 : : }
2695 : :
2696 : :
2697 : : static void
2698 : 28957 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2699 : : {
2700 : 28957 : gfc_gsymbol * gsym;
2701 : 28957 : gfc_namespace *ns;
2702 : 28957 : enum gfc_symbol_type type;
2703 : 28957 : char reason[200];
2704 : :
2705 : 28957 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2706 : :
2707 : 28957 : gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2708 : 28957 : sym->binding_label != NULL);
2709 : :
2710 : 28957 : if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2711 : 10 : gfc_global_used (gsym, where);
2712 : :
2713 : 28957 : if ((sym->attr.if_source == IFSRC_UNKNOWN
2714 : 8895 : || sym->attr.if_source == IFSRC_IFBODY)
2715 : 24856 : && gsym->type != GSYM_UNKNOWN
2716 : 22700 : && !gsym->binding_label
2717 : 20438 : && gsym->ns
2718 : 14829 : && gsym->ns->proc_name
2719 : 14829 : && not_in_recursive (sym, gsym->ns)
2720 : 43786 : && not_entry_self_reference (sym, gsym->ns))
2721 : : {
2722 : 14829 : gfc_symbol *def_sym;
2723 : 14829 : def_sym = gsym->ns->proc_name;
2724 : :
2725 : 14829 : if (gsym->ns->resolved != -1)
2726 : : {
2727 : :
2728 : : /* Resolve the gsymbol namespace if needed. */
2729 : 14808 : if (!gsym->ns->resolved)
2730 : : {
2731 : 2765 : gfc_symbol *old_dt_list;
2732 : :
2733 : : /* Stash away derived types so that the backend_decls
2734 : : do not get mixed up. */
2735 : 2765 : old_dt_list = gfc_derived_types;
2736 : 2765 : gfc_derived_types = NULL;
2737 : :
2738 : 2765 : gfc_resolve (gsym->ns);
2739 : :
2740 : : /* Store the new derived types with the global namespace. */
2741 : 2765 : if (gfc_derived_types)
2742 : 305 : gsym->ns->derived_types = gfc_derived_types;
2743 : :
2744 : : /* Restore the derived types of this namespace. */
2745 : 2765 : gfc_derived_types = old_dt_list;
2746 : : }
2747 : :
2748 : : /* Make sure that translation for the gsymbol occurs before
2749 : : the procedure currently being resolved. */
2750 : 14808 : ns = gfc_global_ns_list;
2751 : 25112 : for (; ns && ns != gsym->ns; ns = ns->sibling)
2752 : : {
2753 : 16752 : if (ns->sibling == gsym->ns)
2754 : : {
2755 : 6448 : ns->sibling = gsym->ns->sibling;
2756 : 6448 : gsym->ns->sibling = gfc_global_ns_list;
2757 : 6448 : gfc_global_ns_list = gsym->ns;
2758 : 6448 : break;
2759 : : }
2760 : : }
2761 : :
2762 : : /* This can happen if a binding name has been specified. */
2763 : 14808 : if (gsym->binding_label && gsym->sym_name != def_sym->name)
2764 : 0 : gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2765 : :
2766 : 14808 : if (def_sym->attr.entry_master || def_sym->attr.entry)
2767 : : {
2768 : 953 : gfc_entry_list *entry;
2769 : 1659 : for (entry = gsym->ns->entries; entry; entry = entry->next)
2770 : 1659 : if (strcmp (entry->sym->name, sym->name) == 0)
2771 : : {
2772 : 953 : def_sym = entry->sym;
2773 : 953 : break;
2774 : : }
2775 : : }
2776 : : }
2777 : :
2778 : 14829 : if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2779 : : {
2780 : 6 : gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2781 : : sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2782 : 6 : gfc_typename (&def_sym->ts));
2783 : 29 : goto done;
2784 : : }
2785 : :
2786 : 14823 : if (sym->attr.if_source == IFSRC_UNKNOWN
2787 : 14823 : && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2788 : : {
2789 : 8 : gfc_error ("Explicit interface required for %qs at %L: %s",
2790 : : sym->name, &sym->declared_at, reason);
2791 : 8 : goto done;
2792 : : }
2793 : :
2794 : 14815 : bool bad_result_characteristics;
2795 : 14815 : if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2796 : : reason, sizeof(reason), NULL, NULL,
2797 : : &bad_result_characteristics))
2798 : : {
2799 : : /* Turn erros into warnings with -std=gnu and -std=legacy,
2800 : : unless a function returns a wrong type, which can lead
2801 : : to all kinds of ICEs and wrong code. */
2802 : :
2803 : 15 : if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2804 : 2 : && !bad_result_characteristics)
2805 : 2 : gfc_errors_to_warnings (true);
2806 : :
2807 : 15 : gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2808 : : sym->name, &sym->declared_at, reason);
2809 : 15 : sym->error = 1;
2810 : 15 : gfc_errors_to_warnings (false);
2811 : 15 : goto done;
2812 : : }
2813 : : }
2814 : :
2815 : 28957 : done:
2816 : :
2817 : 28957 : if (gsym->type == GSYM_UNKNOWN)
2818 : : {
2819 : 3860 : gsym->type = type;
2820 : 3860 : gsym->where = *where;
2821 : : }
2822 : :
2823 : 28957 : gsym->used = 1;
2824 : 28957 : }
2825 : :
2826 : :
2827 : : /************* Function resolution *************/
2828 : :
2829 : : /* Resolve a function call known to be generic.
2830 : : Section 14.1.2.4.1. */
2831 : :
2832 : : static match
2833 : 26992 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2834 : : {
2835 : 26992 : gfc_symbol *s;
2836 : :
2837 : 26992 : if (sym->attr.generic)
2838 : : {
2839 : 25887 : s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2840 : 25887 : if (s != NULL)
2841 : : {
2842 : 19726 : expr->value.function.name = s->name;
2843 : 19726 : expr->value.function.esym = s;
2844 : :
2845 : 19726 : if (s->ts.type != BT_UNKNOWN)
2846 : 19709 : expr->ts = s->ts;
2847 : 17 : else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2848 : 15 : expr->ts = s->result->ts;
2849 : :
2850 : 19726 : if (s->as != NULL)
2851 : : {
2852 : 54 : expr->rank = s->as->rank;
2853 : 54 : expr->corank = s->as->corank;
2854 : : }
2855 : 19672 : else if (s->result != NULL && s->result->as != NULL)
2856 : : {
2857 : 0 : expr->rank = s->result->as->rank;
2858 : 0 : expr->corank = s->result->as->corank;
2859 : : }
2860 : :
2861 : 19726 : gfc_set_sym_referenced (expr->value.function.esym);
2862 : :
2863 : 19726 : return MATCH_YES;
2864 : : }
2865 : :
2866 : : /* TODO: Need to search for elemental references in generic
2867 : : interface. */
2868 : : }
2869 : :
2870 : 7266 : if (sym->attr.intrinsic)
2871 : 1062 : return gfc_intrinsic_func_interface (expr, 0);
2872 : :
2873 : : return MATCH_NO;
2874 : : }
2875 : :
2876 : :
2877 : : static bool
2878 : 26851 : resolve_generic_f (gfc_expr *expr)
2879 : : {
2880 : 26851 : gfc_symbol *sym;
2881 : 26851 : match m;
2882 : 26851 : gfc_interface *intr = NULL;
2883 : :
2884 : 26851 : sym = expr->symtree->n.sym;
2885 : :
2886 : 26992 : for (;;)
2887 : : {
2888 : 26992 : m = resolve_generic_f0 (expr, sym);
2889 : 26992 : if (m == MATCH_YES)
2890 : : return true;
2891 : 6206 : else if (m == MATCH_ERROR)
2892 : : return false;
2893 : :
2894 : 6206 : generic:
2895 : 6209 : if (!intr)
2896 : 6165 : for (intr = sym->generic; intr; intr = intr->next)
2897 : 6081 : if (gfc_fl_struct (intr->sym->attr.flavor))
2898 : : break;
2899 : :
2900 : 6209 : if (sym->ns->parent == NULL)
2901 : : break;
2902 : 264 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2903 : :
2904 : 264 : if (sym == NULL)
2905 : : break;
2906 : 144 : if (!generic_sym (sym))
2907 : 3 : goto generic;
2908 : : }
2909 : :
2910 : : /* Last ditch attempt. See if the reference is to an intrinsic
2911 : : that possesses a matching interface. 14.1.2.4 */
2912 : 6065 : if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2913 : : {
2914 : 5 : if (gfc_init_expr_flag)
2915 : 1 : gfc_error ("Function %qs in initialization expression at %L "
2916 : : "must be an intrinsic function",
2917 : 1 : expr->symtree->n.sym->name, &expr->where);
2918 : : else
2919 : 4 : gfc_error ("There is no specific function for the generic %qs "
2920 : 4 : "at %L", expr->symtree->n.sym->name, &expr->where);
2921 : 5 : return false;
2922 : : }
2923 : :
2924 : 6060 : if (intr)
2925 : : {
2926 : 6025 : if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2927 : : NULL, false))
2928 : : return false;
2929 : 6005 : if (!gfc_use_derived (expr->ts.u.derived))
2930 : : return false;
2931 : 6005 : return resolve_structure_cons (expr, 0);
2932 : : }
2933 : :
2934 : 35 : m = gfc_intrinsic_func_interface (expr, 0);
2935 : 35 : if (m == MATCH_YES)
2936 : : return true;
2937 : :
2938 : 3 : if (m == MATCH_NO)
2939 : 3 : gfc_error ("Generic function %qs at %L is not consistent with a "
2940 : 3 : "specific intrinsic interface", expr->symtree->n.sym->name,
2941 : : &expr->where);
2942 : :
2943 : : return false;
2944 : : }
2945 : :
2946 : :
2947 : : /* Resolve a function call known to be specific. */
2948 : :
2949 : : static match
2950 : 27481 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2951 : : {
2952 : 27481 : match m;
2953 : :
2954 : 27481 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2955 : : {
2956 : 7866 : if (sym->attr.dummy)
2957 : : {
2958 : 276 : sym->attr.proc = PROC_DUMMY;
2959 : 276 : goto found;
2960 : : }
2961 : :
2962 : 7590 : sym->attr.proc = PROC_EXTERNAL;
2963 : 7590 : goto found;
2964 : : }
2965 : :
2966 : 19615 : if (sym->attr.proc == PROC_MODULE
2967 : : || sym->attr.proc == PROC_ST_FUNCTION
2968 : : || sym->attr.proc == PROC_INTERNAL)
2969 : 18877 : goto found;
2970 : :
2971 : 738 : if (sym->attr.intrinsic)
2972 : : {
2973 : 731 : m = gfc_intrinsic_func_interface (expr, 1);
2974 : 731 : if (m == MATCH_YES)
2975 : : return MATCH_YES;
2976 : 0 : if (m == MATCH_NO)
2977 : 0 : gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2978 : : "with an intrinsic", sym->name, &expr->where);
2979 : :
2980 : 0 : return MATCH_ERROR;
2981 : : }
2982 : :
2983 : : return MATCH_NO;
2984 : :
2985 : 26743 : found:
2986 : 26743 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2987 : :
2988 : 26743 : if (sym->result)
2989 : 26743 : expr->ts = sym->result->ts;
2990 : : else
2991 : 0 : expr->ts = sym->ts;
2992 : 26743 : expr->value.function.name = sym->name;
2993 : 26743 : expr->value.function.esym = sym;
2994 : : /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2995 : : error(s). */
2996 : 26743 : if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2997 : : return MATCH_ERROR;
2998 : 26742 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2999 : : {
3000 : 322 : expr->rank = CLASS_DATA (sym)->as->rank;
3001 : 322 : expr->corank = CLASS_DATA (sym)->as->corank;
3002 : : }
3003 : 26420 : else if (sym->as != NULL)
3004 : : {
3005 : 2261 : expr->rank = sym->as->rank;
3006 : 2261 : expr->corank = sym->as->corank;
3007 : : }
3008 : :
3009 : : return MATCH_YES;
3010 : : }
3011 : :
3012 : :
3013 : : static bool
3014 : 27474 : resolve_specific_f (gfc_expr *expr)
3015 : : {
3016 : 27474 : gfc_symbol *sym;
3017 : 27474 : match m;
3018 : :
3019 : 27474 : sym = expr->symtree->n.sym;
3020 : :
3021 : 27481 : for (;;)
3022 : : {
3023 : 27481 : m = resolve_specific_f0 (sym, expr);
3024 : 27481 : if (m == MATCH_YES)
3025 : : return true;
3026 : 8 : if (m == MATCH_ERROR)
3027 : : return false;
3028 : :
3029 : 7 : if (sym->ns->parent == NULL)
3030 : : break;
3031 : :
3032 : 7 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3033 : :
3034 : 7 : if (sym == NULL)
3035 : : break;
3036 : : }
3037 : :
3038 : 0 : gfc_error ("Unable to resolve the specific function %qs at %L",
3039 : 0 : expr->symtree->n.sym->name, &expr->where);
3040 : :
3041 : 0 : return true;
3042 : : }
3043 : :
3044 : : /* Recursively append candidate SYM to CANDIDATES. Store the number of
3045 : : candidates in CANDIDATES_LEN. */
3046 : :
3047 : : static void
3048 : 212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3049 : : char **&candidates,
3050 : : size_t &candidates_len)
3051 : : {
3052 : 388 : gfc_symtree *p;
3053 : :
3054 : 388 : if (sym == NULL)
3055 : : return;
3056 : 388 : if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3057 : 126 : && sym->n.sym->attr.flavor == FL_PROCEDURE)
3058 : 51 : vec_push (candidates, candidates_len, sym->name);
3059 : :
3060 : 388 : p = sym->left;
3061 : 388 : if (p)
3062 : 155 : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3063 : :
3064 : 388 : p = sym->right;
3065 : 388 : if (p)
3066 : : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3067 : : }
3068 : :
3069 : :
3070 : : /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3071 : :
3072 : : const char*
3073 : 57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3074 : : {
3075 : 57 : char **candidates = NULL;
3076 : 57 : size_t candidates_len = 0;
3077 : 57 : lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3078 : 57 : return gfc_closest_fuzzy_match (fn, candidates);
3079 : : }
3080 : :
3081 : :
3082 : : /* Resolve a procedure call not known to be generic nor specific. */
3083 : :
3084 : : static bool
3085 : 269672 : resolve_unknown_f (gfc_expr *expr)
3086 : : {
3087 : 269672 : gfc_symbol *sym;
3088 : 269672 : gfc_typespec *ts;
3089 : :
3090 : 269672 : sym = expr->symtree->n.sym;
3091 : :
3092 : 269672 : if (sym->attr.dummy)
3093 : : {
3094 : 288 : sym->attr.proc = PROC_DUMMY;
3095 : 288 : expr->value.function.name = sym->name;
3096 : 288 : goto set_type;
3097 : : }
3098 : :
3099 : : /* See if we have an intrinsic function reference. */
3100 : :
3101 : 269384 : if (gfc_is_intrinsic (sym, 0, expr->where))
3102 : : {
3103 : 267126 : if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3104 : : return true;
3105 : : return false;
3106 : : }
3107 : :
3108 : : /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3109 : : /* Intrinsics were handled above, only non-intrinsics left here. */
3110 : 2258 : if (sym->attr.flavor == FL_PROCEDURE
3111 : 2255 : && sym->attr.implicit_type
3112 : 374 : && sym->ns
3113 : 374 : && sym->ns->has_implicit_none_export)
3114 : : {
3115 : 3 : gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3116 : : "for symbol %qs at %L", sym->name, &sym->declared_at);
3117 : 3 : sym->error = 1;
3118 : 3 : return false;
3119 : : }
3120 : :
3121 : : /* The reference is to an external name. */
3122 : :
3123 : 2255 : sym->attr.proc = PROC_EXTERNAL;
3124 : 2255 : expr->value.function.name = sym->name;
3125 : 2255 : expr->value.function.esym = expr->symtree->n.sym;
3126 : :
3127 : 2255 : if (sym->as != NULL)
3128 : : {
3129 : 1 : expr->rank = sym->as->rank;
3130 : 1 : expr->corank = sym->as->corank;
3131 : : }
3132 : :
3133 : : /* Type of the expression is either the type of the symbol or the
3134 : : default type of the symbol. */
3135 : :
3136 : 2254 : set_type:
3137 : 2543 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3138 : :
3139 : 2543 : if (sym->ts.type != BT_UNKNOWN)
3140 : 2492 : expr->ts = sym->ts;
3141 : : else
3142 : : {
3143 : 51 : ts = gfc_get_default_type (sym->name, sym->ns);
3144 : :
3145 : 51 : if (ts->type == BT_UNKNOWN)
3146 : : {
3147 : 41 : const char *guessed
3148 : 41 : = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3149 : 41 : if (guessed)
3150 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type"
3151 : : "; did you mean %qs?",
3152 : : sym->name, &expr->where, guessed);
3153 : : else
3154 : 38 : gfc_error ("Function %qs at %L has no IMPLICIT type",
3155 : : sym->name, &expr->where);
3156 : 41 : return false;
3157 : : }
3158 : : else
3159 : 10 : expr->ts = *ts;
3160 : : }
3161 : :
3162 : : return true;
3163 : : }
3164 : :
3165 : :
3166 : : /* Return true, if the symbol is an external procedure. */
3167 : : static bool
3168 : 831755 : is_external_proc (gfc_symbol *sym)
3169 : : {
3170 : 830069 : if (!sym->attr.dummy && !sym->attr.contained
3171 : 724071 : && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3172 : 157875 : && sym->attr.proc != PROC_ST_FUNCTION
3173 : 157280 : && !sym->attr.proc_pointer
3174 : 156172 : && !sym->attr.use_assoc
3175 : 889696 : && sym->name)
3176 : : return true;
3177 : :
3178 : : return false;
3179 : : }
3180 : :
3181 : :
3182 : : /* Figure out if a function reference is pure or not. Also set the name
3183 : : of the function for a potential error message. Return nonzero if the
3184 : : function is PURE, zero if not. */
3185 : : static bool
3186 : : pure_stmt_function (gfc_expr *, gfc_symbol *);
3187 : :
3188 : : bool
3189 : 250646 : gfc_pure_function (gfc_expr *e, const char **name)
3190 : : {
3191 : 250646 : bool pure;
3192 : 250646 : gfc_component *comp;
3193 : :
3194 : 250646 : *name = NULL;
3195 : :
3196 : 250646 : if (e->symtree != NULL
3197 : 250317 : && e->symtree->n.sym != NULL
3198 : 250317 : && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3199 : 305 : return pure_stmt_function (e, e->symtree->n.sym);
3200 : :
3201 : 250341 : comp = gfc_get_proc_ptr_comp (e);
3202 : 250341 : if (comp)
3203 : : {
3204 : 302 : pure = gfc_pure (comp->ts.interface);
3205 : 302 : *name = comp->name;
3206 : : }
3207 : 250039 : else if (e->value.function.esym)
3208 : : {
3209 : 51968 : pure = gfc_pure (e->value.function.esym);
3210 : 51968 : *name = e->value.function.esym->name;
3211 : : }
3212 : 198071 : else if (e->value.function.isym)
3213 : : {
3214 : 394068 : pure = e->value.function.isym->pure
3215 : 197034 : || e->value.function.isym->elemental;
3216 : 197034 : *name = e->value.function.isym->name;
3217 : : }
3218 : 1037 : else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
3219 : : {
3220 : : /* The function has been resolved, but esym is not yet set.
3221 : : This can happen with functions as dummy argument. */
3222 : 286 : pure = e->symtree->n.sym->attr.pure;
3223 : 286 : *name = e->symtree->n.sym->name;
3224 : : }
3225 : : else
3226 : : {
3227 : : /* Implicit functions are not pure. */
3228 : 751 : pure = 0;
3229 : 751 : *name = e->value.function.name;
3230 : : }
3231 : :
3232 : : return pure;
3233 : : }
3234 : :
3235 : :
3236 : : /* Check if the expression is a reference to an implicitly pure function. */
3237 : :
3238 : : bool
3239 : 37308 : gfc_implicit_pure_function (gfc_expr *e)
3240 : : {
3241 : 37308 : gfc_component *comp = gfc_get_proc_ptr_comp (e);
3242 : 37308 : if (comp)
3243 : 286 : return gfc_implicit_pure (comp->ts.interface);
3244 : 37022 : else if (e->value.function.esym)
3245 : 31662 : return gfc_implicit_pure (e->value.function.esym);
3246 : : else
3247 : : return 0;
3248 : : }
3249 : :
3250 : :
3251 : : static bool
3252 : 981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3253 : : int *f ATTRIBUTE_UNUSED)
3254 : : {
3255 : 981 : const char *name;
3256 : :
3257 : : /* Don't bother recursing into other statement functions
3258 : : since they will be checked individually for purity. */
3259 : 981 : if (e->expr_type != EXPR_FUNCTION
3260 : 343 : || !e->symtree
3261 : 343 : || e->symtree->n.sym == sym
3262 : 20 : || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3263 : : return false;
3264 : :
3265 : 19 : return gfc_pure_function (e, &name) ? false : true;
3266 : : }
3267 : :
3268 : :
3269 : : static bool
3270 : 305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3271 : : {
3272 : 305 : return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3273 : : }
3274 : :
3275 : :
3276 : : /* Check if an impure function is allowed in the current context. */
3277 : :
3278 : 238915 : static bool check_pure_function (gfc_expr *e)
3279 : : {
3280 : 238915 : const char *name = NULL;
3281 : 238915 : code_stack *stack;
3282 : 238915 : bool saw_block = false;
3283 : :
3284 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3285 : : gfc_do_concurrent_flag = 0 when the check for an impure function
3286 : : occurs. Check the stack to see if the source code has a nested
3287 : : BLOCK construct. */
3288 : :
3289 : 552272 : for (stack = cs_base; stack; stack = stack->prev)
3290 : : {
3291 : 313359 : if (!saw_block && stack->current->op == EXEC_BLOCK)
3292 : : {
3293 : 6738 : saw_block = true;
3294 : 6738 : continue;
3295 : : }
3296 : :
3297 : 5087 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3298 : : {
3299 : 9 : bool is_pure;
3300 : 313357 : is_pure = (e->value.function.isym
3301 : 8 : && (e->value.function.isym->pure
3302 : 1 : || e->value.function.isym->elemental))
3303 : 10 : || (e->value.function.esym
3304 : 1 : && (e->value.function.esym->attr.pure
3305 : 1 : || e->value.function.esym->attr.elemental));
3306 : 2 : if (!is_pure)
3307 : : {
3308 : 2 : gfc_error ("Reference to impure function at %L inside a "
3309 : : "DO CONCURRENT", &e->where);
3310 : 2 : return false;
3311 : : }
3312 : : }
3313 : : }
3314 : :
3315 : 238913 : if (!gfc_pure_function (e, &name) && name)
3316 : : {
3317 : 36042 : if (forall_flag)
3318 : : {
3319 : 4 : gfc_error ("Reference to impure function %qs at %L inside a "
3320 : : "FORALL %s", name, &e->where,
3321 : : forall_flag == 2 ? "mask" : "block");
3322 : 4 : return false;
3323 : : }
3324 : 36038 : else if (gfc_do_concurrent_flag)
3325 : : {
3326 : 2 : gfc_error ("Reference to impure function %qs at %L inside a "
3327 : : "DO CONCURRENT %s", name, &e->where,
3328 : : gfc_do_concurrent_flag == 2 ? "mask" : "block");
3329 : 2 : return false;
3330 : : }
3331 : 36036 : else if (gfc_pure (NULL))
3332 : : {
3333 : 5 : gfc_error ("Reference to impure function %qs at %L "
3334 : : "within a PURE procedure", name, &e->where);
3335 : 5 : return false;
3336 : : }
3337 : 36031 : if (!gfc_implicit_pure_function (e))
3338 : 29731 : gfc_unset_implicit_pure (NULL);
3339 : : }
3340 : : return true;
3341 : : }
3342 : :
3343 : :
3344 : : /* Update current procedure's array_outer_dependency flag, considering
3345 : : a call to procedure SYM. */
3346 : :
3347 : : static void
3348 : 129786 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
3349 : : {
3350 : : /* Check to see if this is a sibling function that has not yet
3351 : : been resolved. */
3352 : 129786 : gfc_namespace *sibling = gfc_current_ns->sibling;
3353 : 246073 : for (; sibling; sibling = sibling->sibling)
3354 : : {
3355 : 123070 : if (sibling->proc_name == sym)
3356 : : {
3357 : 6783 : gfc_resolve (sibling);
3358 : 6783 : break;
3359 : : }
3360 : : }
3361 : :
3362 : : /* If SYM has references to outer arrays, so has the procedure calling
3363 : : SYM. If SYM is a procedure pointer, we can assume the worst. */
3364 : 129786 : if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3365 : 66851 : && gfc_current_ns->proc_name)
3366 : 66807 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3367 : 129786 : }
3368 : :
3369 : :
3370 : : /* Resolve a function call, which means resolving the arguments, then figuring
3371 : : out which entity the name refers to. */
3372 : :
3373 : : static bool
3374 : 336936 : resolve_function (gfc_expr *expr)
3375 : : {
3376 : 336936 : gfc_actual_arglist *arg;
3377 : 336936 : gfc_symbol *sym;
3378 : 336936 : bool t;
3379 : 336936 : int temp;
3380 : 336936 : procedure_type p = PROC_INTRINSIC;
3381 : 336936 : bool no_formal_args;
3382 : :
3383 : 336936 : sym = NULL;
3384 : 336936 : if (expr->symtree)
3385 : 336607 : sym = expr->symtree->n.sym;
3386 : :
3387 : : /* If this is a procedure pointer component, it has already been resolved. */
3388 : 336936 : if (gfc_is_proc_ptr_comp (expr))
3389 : : return true;
3390 : :
3391 : : /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3392 : : another caf_get. */
3393 : 336539 : if (sym && sym->attr.intrinsic
3394 : 8341 : && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3395 : 8341 : || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3396 : : return true;
3397 : :
3398 : 336539 : if (expr->ref)
3399 : : {
3400 : 1 : gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3401 : : &expr->where);
3402 : 1 : return false;
3403 : : }
3404 : :
3405 : 336209 : if (sym && sym->attr.intrinsic
3406 : 344879 : && !gfc_resolve_intrinsic (sym, &expr->where))
3407 : : return false;
3408 : :
3409 : 336538 : if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3410 : : {
3411 : 4 : gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3412 : 4 : return false;
3413 : : }
3414 : :
3415 : : /* If this is a deferred TBP with an abstract interface (which may
3416 : : of course be referenced), expr->value.function.esym will be set. */
3417 : 336205 : if (sym && sym->attr.abstract && !expr->value.function.esym)
3418 : : {
3419 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3420 : : sym->name, &expr->where);
3421 : 1 : return false;
3422 : : }
3423 : :
3424 : : /* If this is a deferred TBP with an abstract interface, its result
3425 : : cannot be an assumed length character (F2003: C418). */
3426 : 336204 : if (sym && sym->attr.abstract && sym->attr.function
3427 : 191 : && sym->result->ts.u.cl
3428 : 157 : && sym->result->ts.u.cl->length == NULL
3429 : 2 : && !sym->result->ts.deferred)
3430 : : {
3431 : 1 : gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3432 : : "character length result (F2008: C418)", sym->name,
3433 : : &sym->declared_at);
3434 : 1 : return false;
3435 : : }
3436 : :
3437 : : /* Switch off assumed size checking and do this again for certain kinds
3438 : : of procedure, once the procedure itself is resolved. */
3439 : 336532 : need_full_assumed_size++;
3440 : :
3441 : 336532 : if (expr->symtree && expr->symtree->n.sym)
3442 : 336203 : p = expr->symtree->n.sym->attr.proc;
3443 : :
3444 : 336532 : if (expr->value.function.isym && expr->value.function.isym->inquiry)
3445 : 1078 : inquiry_argument = true;
3446 : 336203 : no_formal_args = sym && is_external_proc (sym)
3447 : 349978 : && gfc_sym_get_dummy_args (sym) == NULL;
3448 : :
3449 : 336532 : if (!resolve_actual_arglist (expr->value.function.actual,
3450 : : p, no_formal_args))
3451 : : {
3452 : 67 : inquiry_argument = false;
3453 : 67 : return false;
3454 : : }
3455 : :
3456 : 336465 : inquiry_argument = false;
3457 : :
3458 : : /* Resume assumed_size checking. */
3459 : 336465 : need_full_assumed_size--;
3460 : :
3461 : : /* If the procedure is external, check for usage. */
3462 : 336465 : if (sym && is_external_proc (sym))
3463 : 13426 : resolve_global_procedure (sym, &expr->where, 0);
3464 : :
3465 : 336465 : if (sym && sym->ts.type == BT_CHARACTER
3466 : 3229 : && sym->ts.u.cl
3467 : 3169 : && sym->ts.u.cl->length == NULL
3468 : 657 : && !sym->attr.dummy
3469 : 650 : && !sym->ts.deferred
3470 : 2 : && expr->value.function.esym == NULL
3471 : 2 : && !sym->attr.contained)
3472 : : {
3473 : : /* Internal procedures are taken care of in resolve_contained_fntype. */
3474 : 1 : gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3475 : : "be used at %L since it is not a dummy argument",
3476 : : sym->name, &expr->where);
3477 : 1 : return false;
3478 : : }
3479 : :
3480 : : /* Add and check formal interface when -fc-prototypes-external is in
3481 : : force, see comment in resolve_call(). */
3482 : :
3483 : 336464 : if (warn_external_argument_mismatch && sym && sym->attr.dummy
3484 : 18 : && sym->attr.external)
3485 : : {
3486 : 18 : if (sym->formal)
3487 : : {
3488 : 6 : bool conflict;
3489 : 6 : conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
3490 : : sym->formal, 0, 0, 0, NULL);
3491 : 6 : if (conflict)
3492 : : {
3493 : 6 : sym->ext_dummy_arglist_mismatch = 1;
3494 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
3495 : : "Different argument lists in external dummy "
3496 : : "function %s at %L and %L", sym->name,
3497 : : &expr->where, &sym->formal_at);
3498 : : }
3499 : : }
3500 : 12 : else if (!sym->formal_resolved)
3501 : : {
3502 : 6 : gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
3503 : 6 : sym->formal_at = expr->where;
3504 : : }
3505 : : }
3506 : : /* See if function is already resolved. */
3507 : :
3508 : 336464 : if (expr->value.function.name != NULL
3509 : 324783 : || expr->value.function.isym != NULL)
3510 : : {
3511 : 12467 : if (expr->ts.type == BT_UNKNOWN)
3512 : 3 : expr->ts = sym->ts;
3513 : : t = true;
3514 : : }
3515 : : else
3516 : : {
3517 : : /* Apply the rules of section 14.1.2. */
3518 : :
3519 : 323997 : switch (procedure_kind (sym))
3520 : : {
3521 : 26851 : case PTYPE_GENERIC:
3522 : 26851 : t = resolve_generic_f (expr);
3523 : 26851 : break;
3524 : :
3525 : 27474 : case PTYPE_SPECIFIC:
3526 : 27474 : t = resolve_specific_f (expr);
3527 : 27474 : break;
3528 : :
3529 : 269672 : case PTYPE_UNKNOWN:
3530 : 269672 : t = resolve_unknown_f (expr);
3531 : 269672 : break;
3532 : :
3533 : : default:
3534 : : gfc_internal_error ("resolve_function(): bad function type");
3535 : : }
3536 : : }
3537 : :
3538 : : /* If the expression is still a function (it might have simplified),
3539 : : then we check to see if we are calling an elemental function. */
3540 : :
3541 : 336464 : if (expr->expr_type != EXPR_FUNCTION)
3542 : : return t;
3543 : :
3544 : : /* Walk the argument list looking for invalid BOZ. */
3545 : 725990 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3546 : 487355 : if (arg->expr && arg->expr->ts.type == BT_BOZ)
3547 : : {
3548 : 5 : gfc_error ("A BOZ literal constant at %L cannot appear as an "
3549 : : "actual argument in a function reference",
3550 : : &arg->expr->where);
3551 : 5 : return false;
3552 : : }
3553 : :
3554 : 238635 : temp = need_full_assumed_size;
3555 : 238635 : need_full_assumed_size = 0;
3556 : :
3557 : 238635 : if (!resolve_elemental_actual (expr, NULL))
3558 : : return false;
3559 : :
3560 : 238632 : if (omp_workshare_flag
3561 : 32 : && expr->value.function.esym
3562 : 238637 : && ! gfc_elemental (expr->value.function.esym))
3563 : : {
3564 : 4 : gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3565 : 4 : "in WORKSHARE construct", expr->value.function.esym->name,
3566 : : &expr->where);
3567 : 4 : t = false;
3568 : : }
3569 : :
3570 : : #define GENERIC_ID expr->value.function.isym->id
3571 : 238628 : else if (expr->value.function.actual != NULL
3572 : 230832 : && expr->value.function.isym != NULL
3573 : 186433 : && GENERIC_ID != GFC_ISYM_LBOUND
3574 : : && GENERIC_ID != GFC_ISYM_LCOBOUND
3575 : : && GENERIC_ID != GFC_ISYM_UCOBOUND
3576 : : && GENERIC_ID != GFC_ISYM_LEN
3577 : : && GENERIC_ID != GFC_ISYM_LOC
3578 : : && GENERIC_ID != GFC_ISYM_C_LOC
3579 : : && GENERIC_ID != GFC_ISYM_PRESENT)
3580 : : {
3581 : : /* Array intrinsics must also have the last upper bound of an
3582 : : assumed size array argument. UBOUND and SIZE have to be
3583 : : excluded from the check if the second argument is anything
3584 : : than a constant. */
3585 : :
3586 : 526590 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3587 : : {
3588 : 365499 : if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3589 : 44984 : && arg == expr->value.function.actual
3590 : 16598 : && arg->next != NULL && arg->next->expr)
3591 : : {
3592 : 8214 : if (arg->next->expr->expr_type != EXPR_CONSTANT)
3593 : : break;
3594 : :
3595 : 7990 : if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3596 : : break;
3597 : :
3598 : 7990 : if ((int)mpz_get_si (arg->next->expr->value.integer)
3599 : 7990 : < arg->expr->rank)
3600 : : break;
3601 : : }
3602 : :
3603 : 363096 : if (arg->expr != NULL
3604 : 243051 : && arg->expr->rank > 0
3605 : 480505 : && resolve_assumed_size_actual (arg->expr))
3606 : : return false;
3607 : : }
3608 : : }
3609 : : #undef GENERIC_ID
3610 : :
3611 : 238629 : need_full_assumed_size = temp;
3612 : :
3613 : 238629 : if (!check_pure_function(expr))
3614 : 12 : t = false;
3615 : :
3616 : : /* Functions without the RECURSIVE attribution are not allowed to
3617 : : * call themselves. */
3618 : 238629 : if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3619 : : {
3620 : 50762 : gfc_symbol *esym;
3621 : 50762 : esym = expr->value.function.esym;
3622 : :
3623 : 50762 : if (is_illegal_recursion (esym, gfc_current_ns))
3624 : : {
3625 : 5 : if (esym->attr.entry && esym->ns->entries)
3626 : 3 : gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3627 : : " function %qs is not RECURSIVE",
3628 : 3 : esym->name, &expr->where, esym->ns->entries->sym->name);
3629 : : else
3630 : 2 : gfc_error ("Function %qs at %L cannot be called recursively, as it"
3631 : : " is not RECURSIVE", esym->name, &expr->where);
3632 : :
3633 : : t = false;
3634 : : }
3635 : : }
3636 : :
3637 : : /* Character lengths of use associated functions may contains references to
3638 : : symbols not referenced from the current program unit otherwise. Make sure
3639 : : those symbols are marked as referenced. */
3640 : :
3641 : 238629 : if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3642 : 3367 : && expr->value.function.esym->attr.use_assoc)
3643 : : {
3644 : 1236 : gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3645 : : }
3646 : :
3647 : : /* Make sure that the expression has a typespec that works. */
3648 : 238629 : if (expr->ts.type == BT_UNKNOWN)
3649 : : {
3650 : 890 : if (expr->symtree->n.sym->result
3651 : 881 : && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3652 : 559 : && !expr->symtree->n.sym->result->attr.proc_pointer)
3653 : 559 : expr->ts = expr->symtree->n.sym->result->ts;
3654 : : }
3655 : :
3656 : : /* These derived types with an incomplete namespace, arising from use
3657 : : association, cause gfc_get_derived_vtab to segfault. If the function
3658 : : namespace does not suffice, something is badly wrong. */
3659 : 238629 : if (expr->ts.type == BT_DERIVED
3660 : 8967 : && !expr->ts.u.derived->ns->proc_name)
3661 : : {
3662 : 3 : gfc_symbol *der;
3663 : 3 : gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3664 : 3 : if (der)
3665 : : {
3666 : 3 : expr->ts.u.derived->refs--;
3667 : 3 : expr->ts.u.derived = der;
3668 : 3 : der->refs++;
3669 : : }
3670 : : else
3671 : 0 : expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3672 : : }
3673 : :
3674 : 238629 : if (!expr->ref && !expr->value.function.isym)
3675 : : {
3676 : 52075 : if (expr->value.function.esym)
3677 : 51038 : update_current_proc_array_outer_dependency (expr->value.function.esym);
3678 : : else
3679 : 1037 : update_current_proc_array_outer_dependency (sym);
3680 : : }
3681 : 186554 : else if (expr->ref)
3682 : : /* typebound procedure: Assume the worst. */
3683 : 0 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3684 : :
3685 : 238629 : if (expr->value.function.esym
3686 : 51038 : && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3687 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
3688 : : "Using function %qs at %L is deprecated",
3689 : : sym->name, &expr->where);
3690 : : return t;
3691 : : }
3692 : :
3693 : :
3694 : : /************* Subroutine resolution *************/
3695 : :
3696 : : static bool
3697 : 75418 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3698 : : {
3699 : 75418 : code_stack *stack;
3700 : 75418 : bool saw_block = false;
3701 : :
3702 : 75418 : if (gfc_pure (sym))
3703 : : return true;
3704 : :
3705 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3706 : : gfc_do_concurrent_flag = 0 when the check for an impure subroutine
3707 : : occurs. Walk up the stack to see if the source code has a nested
3708 : : construct. */
3709 : :
3710 : 156082 : for (stack = cs_base; stack; stack = stack->prev)
3711 : : {
3712 : 86069 : if (stack->current->op == EXEC_BLOCK)
3713 : : {
3714 : 1875 : saw_block = true;
3715 : 1875 : continue;
3716 : : }
3717 : :
3718 : 84194 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3719 : : {
3720 : :
3721 : 2 : bool is_pure = true;
3722 : 86069 : is_pure = sym->attr.pure || sym->attr.elemental;
3723 : :
3724 : 2 : if (!is_pure)
3725 : : {
3726 : 2 : gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
3727 : : "is not PURE", loc);
3728 : 2 : return false;
3729 : : }
3730 : : }
3731 : : }
3732 : :
3733 : 70013 : if (forall_flag)
3734 : : {
3735 : 0 : gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3736 : : name, loc);
3737 : 0 : return false;
3738 : : }
3739 : 70013 : else if (gfc_do_concurrent_flag)
3740 : : {
3741 : 6 : gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3742 : : "PURE", name, loc);
3743 : 6 : return false;
3744 : : }
3745 : 70007 : else if (gfc_pure (NULL))
3746 : : {
3747 : 4 : gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3748 : 4 : return false;
3749 : : }
3750 : :
3751 : 70003 : gfc_unset_implicit_pure (NULL);
3752 : 70003 : return true;
3753 : : }
3754 : :
3755 : :
3756 : : static match
3757 : 2215 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3758 : : {
3759 : 2215 : gfc_symbol *s;
3760 : :
3761 : 2215 : if (sym->attr.generic)
3762 : : {
3763 : 2214 : s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3764 : 2214 : if (s != NULL)
3765 : : {
3766 : 2205 : c->resolved_sym = s;
3767 : 2205 : if (!pure_subroutine (s, s->name, &c->loc))
3768 : : return MATCH_ERROR;
3769 : 2205 : return MATCH_YES;
3770 : : }
3771 : :
3772 : : /* TODO: Need to search for elemental references in generic interface. */
3773 : : }
3774 : :
3775 : 10 : if (sym->attr.intrinsic)
3776 : 1 : return gfc_intrinsic_sub_interface (c, 0);
3777 : :
3778 : : return MATCH_NO;
3779 : : }
3780 : :
3781 : :
3782 : : static bool
3783 : 2213 : resolve_generic_s (gfc_code *c)
3784 : : {
3785 : 2213 : gfc_symbol *sym;
3786 : 2213 : match m;
3787 : :
3788 : 2213 : sym = c->symtree->n.sym;
3789 : :
3790 : 2215 : for (;;)
3791 : : {
3792 : 2215 : m = resolve_generic_s0 (c, sym);
3793 : 2215 : if (m == MATCH_YES)
3794 : : return true;
3795 : 9 : else if (m == MATCH_ERROR)
3796 : : return false;
3797 : :
3798 : 9 : generic:
3799 : 9 : if (sym->ns->parent == NULL)
3800 : : break;
3801 : 3 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3802 : :
3803 : 3 : if (sym == NULL)
3804 : : break;
3805 : 2 : if (!generic_sym (sym))
3806 : 0 : goto generic;
3807 : : }
3808 : :
3809 : : /* Last ditch attempt. See if the reference is to an intrinsic
3810 : : that possesses a matching interface. 14.1.2.4 */
3811 : 7 : sym = c->symtree->n.sym;
3812 : :
3813 : 7 : if (!gfc_is_intrinsic (sym, 1, c->loc))
3814 : : {
3815 : 4 : gfc_error ("There is no specific subroutine for the generic %qs at %L",
3816 : : sym->name, &c->loc);
3817 : 4 : return false;
3818 : : }
3819 : :
3820 : 3 : m = gfc_intrinsic_sub_interface (c, 0);
3821 : 3 : if (m == MATCH_YES)
3822 : : return true;
3823 : 1 : if (m == MATCH_NO)
3824 : 1 : gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3825 : : "intrinsic subroutine interface", sym->name, &c->loc);
3826 : :
3827 : : return false;
3828 : : }
3829 : :
3830 : :
3831 : : /* Resolve a subroutine call known to be specific. */
3832 : :
3833 : : static match
3834 : 61587 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3835 : : {
3836 : 61587 : match m;
3837 : :
3838 : 61587 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3839 : : {
3840 : 5624 : if (sym->attr.dummy)
3841 : : {
3842 : 256 : sym->attr.proc = PROC_DUMMY;
3843 : 256 : goto found;
3844 : : }
3845 : :
3846 : 5368 : sym->attr.proc = PROC_EXTERNAL;
3847 : 5368 : goto found;
3848 : : }
3849 : :
3850 : 55963 : if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3851 : 55963 : goto found;
3852 : :
3853 : 0 : if (sym->attr.intrinsic)
3854 : : {
3855 : 0 : m = gfc_intrinsic_sub_interface (c, 1);
3856 : 0 : if (m == MATCH_YES)
3857 : : return MATCH_YES;
3858 : 0 : if (m == MATCH_NO)
3859 : 0 : gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3860 : : "with an intrinsic", sym->name, &c->loc);
3861 : :
3862 : 0 : return MATCH_ERROR;
3863 : : }
3864 : :
3865 : : return MATCH_NO;
3866 : :
3867 : 61587 : found:
3868 : 61587 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3869 : :
3870 : 61587 : c->resolved_sym = sym;
3871 : 61587 : if (!pure_subroutine (sym, sym->name, &c->loc))
3872 : : return MATCH_ERROR;
3873 : :
3874 : : return MATCH_YES;
3875 : : }
3876 : :
3877 : :
3878 : : static bool
3879 : 61587 : resolve_specific_s (gfc_code *c)
3880 : : {
3881 : 61587 : gfc_symbol *sym;
3882 : 61587 : match m;
3883 : :
3884 : 61587 : sym = c->symtree->n.sym;
3885 : :
3886 : 61587 : for (;;)
3887 : : {
3888 : 61587 : m = resolve_specific_s0 (c, sym);
3889 : 61587 : if (m == MATCH_YES)
3890 : : return true;
3891 : 7 : if (m == MATCH_ERROR)
3892 : : return false;
3893 : :
3894 : 0 : if (sym->ns->parent == NULL)
3895 : : break;
3896 : :
3897 : 0 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3898 : :
3899 : 0 : if (sym == NULL)
3900 : : break;
3901 : : }
3902 : :
3903 : 0 : sym = c->symtree->n.sym;
3904 : 0 : gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3905 : : sym->name, &c->loc);
3906 : :
3907 : 0 : return false;
3908 : : }
3909 : :
3910 : :
3911 : : /* Resolve a subroutine call not known to be generic nor specific. */
3912 : :
3913 : : static bool
3914 : 15530 : resolve_unknown_s (gfc_code *c)
3915 : : {
3916 : 15530 : gfc_symbol *sym;
3917 : :
3918 : 15530 : sym = c->symtree->n.sym;
3919 : :
3920 : 15530 : if (sym->attr.dummy)
3921 : : {
3922 : 20 : sym->attr.proc = PROC_DUMMY;
3923 : 20 : goto found;
3924 : : }
3925 : :
3926 : : /* See if we have an intrinsic function reference. */
3927 : :
3928 : 15510 : if (gfc_is_intrinsic (sym, 1, c->loc))
3929 : : {
3930 : 4026 : if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3931 : : return true;
3932 : 309 : return false;
3933 : : }
3934 : :
3935 : : /* The reference is to an external name. */
3936 : :
3937 : 11484 : found:
3938 : 11504 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3939 : :
3940 : 11504 : c->resolved_sym = sym;
3941 : :
3942 : 11504 : return pure_subroutine (sym, sym->name, &c->loc);
3943 : : }
3944 : :
3945 : :
3946 : :
3947 : : static bool
3948 : 805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
3949 : : gfc_code *c, gfc_namespace *ns)
3950 : : {
3951 : 805 : locus *here;
3952 : :
3953 : : /* If the type has been imported then its vtype functions are OK. */
3954 : 805 : if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
3955 : : return true;
3956 : :
3957 : : if (e)
3958 : 791 : here = &e->where;
3959 : : else
3960 : 7 : here = &c->loc;
3961 : :
3962 : 798 : if (s && !s->import_only)
3963 : 705 : s = gfc_find_symtree (ns->sym_root, sym->name);
3964 : :
3965 : 798 : if (ns->import_state == IMPORT_ONLY
3966 : 75 : && sym->ns != ns
3967 : 58 : && (!s || !s->import_only))
3968 : : {
3969 : 21 : gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
3970 : : "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
3971 : 21 : return false;
3972 : : }
3973 : 777 : else if (ns->import_state == IMPORT_NONE
3974 : 27 : && sym->ns != ns)
3975 : : {
3976 : 12 : gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
3977 : : "has IMPORT, NONE", sym->name, here);
3978 : 12 : return false;
3979 : : }
3980 : : return true;
3981 : : }
3982 : :
3983 : :
3984 : : static bool
3985 : 6919 : check_import_status (gfc_expr *e)
3986 : : {
3987 : 6919 : gfc_symtree *st;
3988 : 6919 : gfc_ref *ref;
3989 : 6919 : gfc_symbol *sym, *der;
3990 : 6919 : gfc_namespace *ns = gfc_current_ns;
3991 : :
3992 : 6919 : switch (e->expr_type)
3993 : : {
3994 : 727 : case EXPR_VARIABLE:
3995 : 727 : case EXPR_FUNCTION:
3996 : 727 : case EXPR_SUBSTRING:
3997 : 727 : sym = e->symtree ? e->symtree->n.sym : NULL;
3998 : :
3999 : : /* Check the symbol itself. */
4000 : 727 : if (sym
4001 : 727 : && !(ns->proc_name
4002 : : && (sym == ns->proc_name))
4003 : 1450 : && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
4004 : : return false;
4005 : :
4006 : : /* Check the declared derived type. */
4007 : 717 : if (sym->ts.type == BT_DERIVED)
4008 : : {
4009 : 16 : der = sym->ts.u.derived;
4010 : 16 : st = gfc_find_symtree (ns->sym_root, der->name);
4011 : :
4012 : 16 : if (!check_sym_import_status (der, st, e, NULL, ns))
4013 : : return false;
4014 : : }
4015 : 701 : else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
4016 : : {
4017 : 44 : der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
4018 : : : sym->ts.u.derived;
4019 : 44 : st = gfc_find_symtree (ns->sym_root, der->name);
4020 : :
4021 : 44 : if (!check_sym_import_status (der, st, e, NULL, ns))
4022 : : return false;
4023 : : }
4024 : :
4025 : : /* Check the declared derived types of component references. */
4026 : 724 : for (ref = e->ref; ref; ref = ref->next)
4027 : 20 : if (ref->type == REF_COMPONENT)
4028 : : {
4029 : 19 : gfc_component *c = ref->u.c.component;
4030 : 19 : if (c->ts.type == BT_DERIVED)
4031 : : {
4032 : 7 : der = c->ts.u.derived;
4033 : 7 : st = gfc_find_symtree (ns->sym_root, der->name);
4034 : 7 : if (!check_sym_import_status (der, st, e, NULL, ns))
4035 : : return false;
4036 : : }
4037 : 12 : else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
4038 : : {
4039 : 0 : der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
4040 : : : c->ts.u.derived;
4041 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4042 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4043 : : return false;
4044 : : }
4045 : : }
4046 : :
4047 : : break;
4048 : :
4049 : 8 : case EXPR_ARRAY:
4050 : 8 : case EXPR_STRUCTURE:
4051 : : /* Check the declared derived type. */
4052 : 8 : if (e->ts.type == BT_DERIVED)
4053 : : {
4054 : 8 : der = e->ts.u.derived;
4055 : 8 : st = gfc_find_symtree (ns->sym_root, der->name);
4056 : :
4057 : 8 : if (!check_sym_import_status (der, st, e, NULL, ns))
4058 : : return false;
4059 : : }
4060 : 0 : else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
4061 : : {
4062 : 0 : der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
4063 : : : e->ts.u.derived;
4064 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4065 : :
4066 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4067 : : return false;
4068 : : }
4069 : :
4070 : : break;
4071 : :
4072 : : /* Either not applicable or resolved away
4073 : : case EXPR_OP:
4074 : : case EXPR_UNKNOWN:
4075 : : case EXPR_CONSTANT:
4076 : : case EXPR_NULL:
4077 : : case EXPR_COMPCALL:
4078 : : case EXPR_PPC: */
4079 : :
4080 : : default:
4081 : : break;
4082 : : }
4083 : :
4084 : : return true;
4085 : : }
4086 : :
4087 : :
4088 : : /* Resolve a subroutine call. Although it was tempting to use the same code
4089 : : for functions, subroutines and functions are stored differently and this
4090 : : makes things awkward. */
4091 : :
4092 : :
4093 : : static bool
4094 : 79420 : resolve_call (gfc_code *c)
4095 : : {
4096 : 79420 : bool t;
4097 : 79420 : procedure_type ptype = PROC_INTRINSIC;
4098 : 79420 : gfc_symbol *csym, *sym;
4099 : 79420 : bool no_formal_args;
4100 : :
4101 : 79420 : csym = c->symtree ? c->symtree->n.sym : NULL;
4102 : :
4103 : 79420 : if (csym && csym->ts.type != BT_UNKNOWN)
4104 : : {
4105 : 4 : gfc_error ("%qs at %L has a type, which is not consistent with "
4106 : : "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
4107 : 4 : return false;
4108 : : }
4109 : :
4110 : 79416 : if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
4111 : : {
4112 : 16709 : gfc_symtree *st;
4113 : 16709 : gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
4114 : 16709 : sym = st ? st->n.sym : NULL;
4115 : 16709 : if (sym && csym != sym
4116 : 3 : && sym->ns == gfc_current_ns
4117 : 3 : && sym->attr.flavor == FL_PROCEDURE
4118 : 3 : && sym->attr.contained)
4119 : : {
4120 : 3 : sym->refs++;
4121 : 3 : if (csym->attr.generic)
4122 : 2 : c->symtree->n.sym = sym;
4123 : : else
4124 : 1 : c->symtree = st;
4125 : 3 : csym = c->symtree->n.sym;
4126 : : }
4127 : : }
4128 : :
4129 : : /* If this ia a deferred TBP, c->expr1 will be set. */
4130 : 79416 : if (!c->expr1 && csym)
4131 : : {
4132 : 77755 : if (csym->attr.abstract)
4133 : : {
4134 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
4135 : : csym->name, &c->loc);
4136 : 1 : return false;
4137 : : }
4138 : :
4139 : : /* Subroutines without the RECURSIVE attribution are not allowed to
4140 : : call themselves. */
4141 : 77754 : if (is_illegal_recursion (csym, gfc_current_ns))
4142 : : {
4143 : 4 : if (csym->attr.entry && csym->ns->entries)
4144 : 2 : gfc_error ("ENTRY %qs at %L cannot be called recursively, "
4145 : : "as subroutine %qs is not RECURSIVE",
4146 : 2 : csym->name, &c->loc, csym->ns->entries->sym->name);
4147 : : else
4148 : 2 : gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
4149 : : "as it is not RECURSIVE", csym->name, &c->loc);
4150 : :
4151 : 79415 : t = false;
4152 : : }
4153 : : }
4154 : :
4155 : : /* Switch off assumed size checking and do this again for certain kinds
4156 : : of procedure, once the procedure itself is resolved. */
4157 : 79415 : need_full_assumed_size++;
4158 : :
4159 : 79415 : if (csym)
4160 : 79415 : ptype = csym->attr.proc;
4161 : :
4162 : 79415 : no_formal_args = csym && is_external_proc (csym)
4163 : 15537 : && gfc_sym_get_dummy_args (csym) == NULL;
4164 : 79415 : if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
4165 : : return false;
4166 : :
4167 : : /* Resume assumed_size checking. */
4168 : 79381 : need_full_assumed_size--;
4169 : :
4170 : : /* If external, check for usage. */
4171 : 79381 : if (csym && is_external_proc (csym))
4172 : 15531 : resolve_global_procedure (csym, &c->loc, 1);
4173 : :
4174 : : /* If we have an external dummy argument, we want to write out its arguments
4175 : : with -fc-prototypes-external. Code like
4176 : :
4177 : : subroutine foo(a,n)
4178 : : external a
4179 : : if (n == 1) call a(1)
4180 : : if (n == 2) call a(2,3)
4181 : : end subroutine foo
4182 : :
4183 : : is actually legal Fortran, but it is not possible to generate a C23-
4184 : : compliant prototype for this, so we just record the fact here and
4185 : : handle that during -fc-prototypes-external processing. */
4186 : :
4187 : 79381 : if (warn_external_argument_mismatch && csym && csym->attr.dummy
4188 : 14 : && csym->attr.external)
4189 : : {
4190 : 14 : if (csym->formal)
4191 : : {
4192 : 6 : bool conflict;
4193 : 6 : conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4194 : : 0, 0, 0, NULL);
4195 : 6 : if (conflict)
4196 : : {
4197 : 6 : csym->ext_dummy_arglist_mismatch = 1;
4198 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
4199 : : "Different argument lists in external dummy "
4200 : : "subroutine %s at %L and %L", csym->name,
4201 : : &c->loc, &csym->formal_at);
4202 : : }
4203 : : }
4204 : 8 : else if (!csym->formal_resolved)
4205 : : {
4206 : 7 : gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4207 : 7 : csym->formal_at = c->loc;
4208 : : }
4209 : : }
4210 : :
4211 : 79381 : t = true;
4212 : 79381 : if (c->resolved_sym == NULL)
4213 : : {
4214 : 79330 : c->resolved_isym = NULL;
4215 : 79330 : switch (procedure_kind (csym))
4216 : : {
4217 : 2213 : case PTYPE_GENERIC:
4218 : 2213 : t = resolve_generic_s (c);
4219 : 2213 : break;
4220 : :
4221 : 61587 : case PTYPE_SPECIFIC:
4222 : 61587 : t = resolve_specific_s (c);
4223 : 61587 : break;
4224 : :
4225 : 15530 : case PTYPE_UNKNOWN:
4226 : 15530 : t = resolve_unknown_s (c);
4227 : 15530 : break;
4228 : :
4229 : : default:
4230 : : gfc_internal_error ("resolve_subroutine(): bad function type");
4231 : : }
4232 : : }
4233 : :
4234 : : /* Some checks of elemental subroutine actual arguments. */
4235 : 79380 : if (!resolve_elemental_actual (NULL, c))
4236 : : return false;
4237 : :
4238 : 79372 : if (!c->expr1)
4239 : 77711 : update_current_proc_array_outer_dependency (csym);
4240 : : else
4241 : : /* Typebound procedure: Assume the worst. */
4242 : 1661 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4243 : :
4244 : 79372 : if (c->resolved_sym
4245 : 79059 : && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4246 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
4247 : : "Using subroutine %qs at %L is deprecated",
4248 : : c->resolved_sym->name, &c->loc);
4249 : :
4250 : 79372 : csym = c->resolved_sym ? c->resolved_sym : csym;
4251 : 79372 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
4252 : 2 : && csym != gfc_current_ns->proc_name)
4253 : 1 : return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
4254 : :
4255 : : return t;
4256 : : }
4257 : :
4258 : :
4259 : : /* Compare the shapes of two arrays that have non-NULL shapes. If both
4260 : : op1->shape and op2->shape are non-NULL return true if their shapes
4261 : : match. If both op1->shape and op2->shape are non-NULL return false
4262 : : if their shapes do not match. If either op1->shape or op2->shape is
4263 : : NULL, return true. */
4264 : :
4265 : : static bool
4266 : 31561 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
4267 : : {
4268 : 31561 : bool t;
4269 : 31561 : int i;
4270 : :
4271 : 31561 : t = true;
4272 : :
4273 : 31561 : if (op1->shape != NULL && op2->shape != NULL)
4274 : : {
4275 : 42512 : for (i = 0; i < op1->rank; i++)
4276 : : {
4277 : 22680 : if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4278 : : {
4279 : 3 : gfc_error ("Shapes for operands at %L and %L are not conformable",
4280 : : &op1->where, &op2->where);
4281 : 3 : t = false;
4282 : 3 : break;
4283 : : }
4284 : : }
4285 : : }
4286 : :
4287 : 31561 : return t;
4288 : : }
4289 : :
4290 : : /* Convert a logical operator to the corresponding bitwise intrinsic call.
4291 : : For example A .AND. B becomes IAND(A, B). */
4292 : : static gfc_expr *
4293 : 668 : logical_to_bitwise (gfc_expr *e)
4294 : : {
4295 : 668 : gfc_expr *tmp, *op1, *op2;
4296 : 668 : gfc_isym_id isym;
4297 : 668 : gfc_actual_arglist *args = NULL;
4298 : :
4299 : 668 : gcc_assert (e->expr_type == EXPR_OP);
4300 : :
4301 : 668 : isym = GFC_ISYM_NONE;
4302 : 668 : op1 = e->value.op.op1;
4303 : 668 : op2 = e->value.op.op2;
4304 : :
4305 : 668 : switch (e->value.op.op)
4306 : : {
4307 : : case INTRINSIC_NOT:
4308 : : isym = GFC_ISYM_NOT;
4309 : : break;
4310 : 126 : case INTRINSIC_AND:
4311 : 126 : isym = GFC_ISYM_IAND;
4312 : 126 : break;
4313 : 127 : case INTRINSIC_OR:
4314 : 127 : isym = GFC_ISYM_IOR;
4315 : 127 : break;
4316 : 270 : case INTRINSIC_NEQV:
4317 : 270 : isym = GFC_ISYM_IEOR;
4318 : 270 : break;
4319 : 126 : case INTRINSIC_EQV:
4320 : : /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4321 : : Change the old expression to NEQV, which will get replaced by IEOR,
4322 : : and wrap it in NOT. */
4323 : 126 : tmp = gfc_copy_expr (e);
4324 : 126 : tmp->value.op.op = INTRINSIC_NEQV;
4325 : 126 : tmp = logical_to_bitwise (tmp);
4326 : 126 : isym = GFC_ISYM_NOT;
4327 : 126 : op1 = tmp;
4328 : 126 : op2 = NULL;
4329 : 126 : break;
4330 : 0 : default:
4331 : 0 : gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4332 : : }
4333 : :
4334 : : /* Inherit the original operation's operands as arguments. */
4335 : 668 : args = gfc_get_actual_arglist ();
4336 : 668 : args->expr = op1;
4337 : 668 : if (op2)
4338 : : {
4339 : 523 : args->next = gfc_get_actual_arglist ();
4340 : 523 : args->next->expr = op2;
4341 : : }
4342 : :
4343 : : /* Convert the expression to a function call. */
4344 : 668 : e->expr_type = EXPR_FUNCTION;
4345 : 668 : e->value.function.actual = args;
4346 : 668 : e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4347 : 668 : e->value.function.name = e->value.function.isym->name;
4348 : 668 : e->value.function.esym = NULL;
4349 : :
4350 : : /* Make up a pre-resolved function call symtree if we need to. */
4351 : 668 : if (!e->symtree || !e->symtree->n.sym)
4352 : : {
4353 : 668 : gfc_symbol *sym;
4354 : 668 : gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4355 : 668 : sym = e->symtree->n.sym;
4356 : 668 : sym->result = sym;
4357 : 668 : sym->attr.flavor = FL_PROCEDURE;
4358 : 668 : sym->attr.function = 1;
4359 : 668 : sym->attr.elemental = 1;
4360 : 668 : sym->attr.pure = 1;
4361 : 668 : sym->attr.referenced = 1;
4362 : 668 : gfc_intrinsic_symbol (sym);
4363 : 668 : gfc_commit_symbol (sym);
4364 : : }
4365 : :
4366 : 668 : args->name = e->value.function.isym->formal->name;
4367 : 668 : if (e->value.function.isym->formal->next)
4368 : 523 : args->next->name = e->value.function.isym->formal->next->name;
4369 : :
4370 : 668 : return e;
4371 : : }
4372 : :
4373 : : /* Recursively append candidate UOP to CANDIDATES. Store the number of
4374 : : candidates in CANDIDATES_LEN. */
4375 : : static void
4376 : 69 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4377 : : char **&candidates,
4378 : : size_t &candidates_len)
4379 : : {
4380 : 71 : gfc_symtree *p;
4381 : :
4382 : 71 : if (uop == NULL)
4383 : : return;
4384 : :
4385 : : /* Not sure how to properly filter here. Use all for a start.
4386 : : n.uop.op is NULL for empty interface operators (is that legal?) disregard
4387 : : these as i suppose they don't make terribly sense. */
4388 : :
4389 : 71 : if (uop->n.uop->op != NULL)
4390 : 2 : vec_push (candidates, candidates_len, uop->name);
4391 : :
4392 : 71 : p = uop->left;
4393 : 71 : if (p)
4394 : 0 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4395 : :
4396 : 71 : p = uop->right;
4397 : 71 : if (p)
4398 : : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4399 : : }
4400 : :
4401 : : /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4402 : :
4403 : : static const char*
4404 : 69 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4405 : : {
4406 : 69 : char **candidates = NULL;
4407 : 69 : size_t candidates_len = 0;
4408 : 69 : lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4409 : 69 : return gfc_closest_fuzzy_match (op, candidates);
4410 : : }
4411 : :
4412 : :
4413 : : /* Callback finding an impure function as an operand to an .and. or
4414 : : .or. expression. Remember the last function warned about to
4415 : : avoid double warnings when recursing. */
4416 : :
4417 : : static int
4418 : 190930 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4419 : : void *data)
4420 : : {
4421 : 190930 : gfc_expr *f = *e;
4422 : 190930 : const char *name;
4423 : 190930 : static gfc_expr *last = NULL;
4424 : 190930 : bool *found = (bool *) data;
4425 : :
4426 : 190930 : if (f->expr_type == EXPR_FUNCTION)
4427 : : {
4428 : 11708 : *found = 1;
4429 : 11708 : if (f != last && !gfc_pure_function (f, &name)
4430 : 12983 : && !gfc_implicit_pure_function (f))
4431 : : {
4432 : 1136 : if (name)
4433 : 1136 : gfc_warning (OPT_Wfunction_elimination,
4434 : : "Impure function %qs at %L might not be evaluated",
4435 : : name, &f->where);
4436 : : else
4437 : 0 : gfc_warning (OPT_Wfunction_elimination,
4438 : : "Impure function at %L might not be evaluated",
4439 : : &f->where);
4440 : : }
4441 : 11708 : last = f;
4442 : : }
4443 : :
4444 : 190930 : return 0;
4445 : : }
4446 : :
4447 : : /* Return true if TYPE is character based, false otherwise. */
4448 : :
4449 : : static int
4450 : 1373 : is_character_based (bt type)
4451 : : {
4452 : 1373 : return type == BT_CHARACTER || type == BT_HOLLERITH;
4453 : : }
4454 : :
4455 : :
4456 : : /* If expression is a hollerith, convert it to character and issue a warning
4457 : : for the conversion. */
4458 : :
4459 : : static void
4460 : 408 : convert_hollerith_to_character (gfc_expr *e)
4461 : : {
4462 : 408 : if (e->ts.type == BT_HOLLERITH)
4463 : : {
4464 : 108 : gfc_typespec t;
4465 : 108 : gfc_clear_ts (&t);
4466 : 108 : t.type = BT_CHARACTER;
4467 : 108 : t.kind = e->ts.kind;
4468 : 108 : gfc_convert_type_warn (e, &t, 2, 1);
4469 : : }
4470 : 408 : }
4471 : :
4472 : : /* Convert to numeric and issue a warning for the conversion. */
4473 : :
4474 : : static void
4475 : 240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
4476 : : {
4477 : 240 : gfc_typespec t;
4478 : 240 : gfc_clear_ts (&t);
4479 : 240 : t.type = b->ts.type;
4480 : 240 : t.kind = b->ts.kind;
4481 : 240 : gfc_convert_type_warn (a, &t, 2, 1);
4482 : 240 : }
4483 : :
4484 : : /* Resolve an operator expression node. This can involve replacing the
4485 : : operation with a user defined function call. CHECK_INTERFACES is a
4486 : : helper macro. */
4487 : :
4488 : : #define CHECK_INTERFACES \
4489 : : { \
4490 : : match m = gfc_extend_expr (e); \
4491 : : if (m == MATCH_YES) \
4492 : : return true; \
4493 : : if (m == MATCH_ERROR) \
4494 : : return false; \
4495 : : }
4496 : :
4497 : : static bool
4498 : 519862 : resolve_operator (gfc_expr *e)
4499 : : {
4500 : 519862 : gfc_expr *op1, *op2;
4501 : : /* One error uses 3 names; additional space for wording (also via gettext). */
4502 : 519862 : bool t = true;
4503 : :
4504 : : /* Reduce stacked parentheses to single pair */
4505 : 519862 : while (e->expr_type == EXPR_OP
4506 : 519972 : && e->value.op.op == INTRINSIC_PARENTHESES
4507 : 20546 : && e->value.op.op1->expr_type == EXPR_OP
4508 : 536238 : && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4509 : : {
4510 : 110 : gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4511 : 110 : gfc_replace_expr (e, tmp);
4512 : : }
4513 : :
4514 : : /* Resolve all subnodes-- give them types. */
4515 : :
4516 : 519862 : switch (e->value.op.op)
4517 : : {
4518 : 471223 : default:
4519 : 471223 : if (!gfc_resolve_expr (e->value.op.op2))
4520 : 519862 : t = false;
4521 : :
4522 : : /* Fall through. */
4523 : :
4524 : 519862 : case INTRINSIC_NOT:
4525 : 519862 : case INTRINSIC_UPLUS:
4526 : 519862 : case INTRINSIC_UMINUS:
4527 : 519862 : case INTRINSIC_PARENTHESES:
4528 : 519862 : if (!gfc_resolve_expr (e->value.op.op1))
4529 : : return false;
4530 : 519694 : if (e->value.op.op1
4531 : 519685 : && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4532 : : {
4533 : 0 : gfc_error ("BOZ literal constant at %L cannot be an operand of "
4534 : 0 : "unary operator %qs", &e->value.op.op1->where,
4535 : : gfc_op2string (e->value.op.op));
4536 : 0 : return false;
4537 : : }
4538 : 519694 : if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4539 : 6 : && e->value.op.op == INTRINSIC_UMINUS)
4540 : : {
4541 : 2 : gfc_error ("Negation of unsigned expression at %L not permitted ",
4542 : : &e->value.op.op1->where);
4543 : 2 : return false;
4544 : : }
4545 : 519692 : break;
4546 : : }
4547 : :
4548 : : /* Typecheck the new node. */
4549 : :
4550 : 519692 : op1 = e->value.op.op1;
4551 : 519692 : op2 = e->value.op.op2;
4552 : 519692 : if (op1 == NULL && op2 == NULL)
4553 : : return false;
4554 : : /* Error out if op2 did not resolve. We already diagnosed op1. */
4555 : 519683 : if (t == false)
4556 : : return false;
4557 : :
4558 : : /* op1 and op2 cannot both be BOZ. */
4559 : 519617 : if (op1 && op1->ts.type == BT_BOZ
4560 : 0 : && op2 && op2->ts.type == BT_BOZ)
4561 : : {
4562 : 0 : gfc_error ("Operands at %L and %L cannot appear as operands of "
4563 : 0 : "binary operator %qs", &op1->where, &op2->where,
4564 : : gfc_op2string (e->value.op.op));
4565 : 0 : return false;
4566 : : }
4567 : :
4568 : 519617 : if ((op1 && op1->expr_type == EXPR_NULL)
4569 : 519615 : || (op2 && op2->expr_type == EXPR_NULL))
4570 : : {
4571 : 3 : CHECK_INTERFACES
4572 : 3 : gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4573 : 3 : return false;
4574 : : }
4575 : :
4576 : 519614 : switch (e->value.op.op)
4577 : : {
4578 : 8015 : case INTRINSIC_UPLUS:
4579 : 8015 : case INTRINSIC_UMINUS:
4580 : 8015 : if (op1->ts.type == BT_INTEGER
4581 : : || op1->ts.type == BT_REAL
4582 : : || op1->ts.type == BT_COMPLEX
4583 : : || op1->ts.type == BT_UNSIGNED)
4584 : : {
4585 : 7946 : e->ts = op1->ts;
4586 : 7946 : break;
4587 : : }
4588 : :
4589 : 69 : CHECK_INTERFACES
4590 : 43 : gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4591 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4592 : 43 : return false;
4593 : :
4594 : 151694 : case INTRINSIC_POWER:
4595 : 151694 : case INTRINSIC_PLUS:
4596 : 151694 : case INTRINSIC_MINUS:
4597 : 151694 : case INTRINSIC_TIMES:
4598 : 151694 : case INTRINSIC_DIVIDE:
4599 : :
4600 : : /* UNSIGNED cannot appear in a mixed expression without explicit
4601 : : conversion. */
4602 : 151694 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4603 : : {
4604 : 3 : CHECK_INTERFACES
4605 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are "
4606 : : "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4607 : : gfc_typename (op1), gfc_typename (op2));
4608 : 3 : return false;
4609 : : }
4610 : :
4611 : 151691 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4612 : : {
4613 : : /* Do not perform conversions if operands are not conformable as
4614 : : required for the binary intrinsic operators (F2018:10.1.5).
4615 : : Defer to a possibly overloading user-defined operator. */
4616 : 151235 : if (!gfc_op_rank_conformable (op1, op2))
4617 : : {
4618 : 36 : CHECK_INTERFACES
4619 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4620 : 0 : &op1->where, &op2->where);
4621 : 0 : return false;
4622 : : }
4623 : :
4624 : 151199 : gfc_type_convert_binary (e, 1);
4625 : 151199 : break;
4626 : : }
4627 : :
4628 : 456 : if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4629 : : {
4630 : 227 : CHECK_INTERFACES
4631 : 2 : gfc_error ("Unexpected derived-type entities in binary intrinsic "
4632 : : "numeric operator %qs at %L",
4633 : : gfc_op2string (e->value.op.op), &e->where);
4634 : 2 : return false;
4635 : : }
4636 : : else
4637 : : {
4638 : 229 : CHECK_INTERFACES
4639 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4640 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4641 : : gfc_typename (op2));
4642 : 3 : return false;
4643 : : }
4644 : :
4645 : 2142 : case INTRINSIC_CONCAT:
4646 : 2142 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4647 : 2117 : && op1->ts.kind == op2->ts.kind)
4648 : : {
4649 : 2108 : e->ts.type = BT_CHARACTER;
4650 : 2108 : e->ts.kind = op1->ts.kind;
4651 : 2108 : break;
4652 : : }
4653 : :
4654 : 34 : CHECK_INTERFACES
4655 : 10 : gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4656 : : &e->where, gfc_typename (op1), gfc_typename (op2));
4657 : 10 : return false;
4658 : :
4659 : 69026 : case INTRINSIC_AND:
4660 : 69026 : case INTRINSIC_OR:
4661 : 69026 : case INTRINSIC_EQV:
4662 : 69026 : case INTRINSIC_NEQV:
4663 : 69026 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4664 : : {
4665 : 68475 : e->ts.type = BT_LOGICAL;
4666 : 68475 : e->ts.kind = gfc_kind_max (op1, op2);
4667 : 68475 : if (op1->ts.kind < e->ts.kind)
4668 : 138 : gfc_convert_type (op1, &e->ts, 2);
4669 : 68337 : else if (op2->ts.kind < e->ts.kind)
4670 : 117 : gfc_convert_type (op2, &e->ts, 2);
4671 : :
4672 : 68475 : if (flag_frontend_optimize &&
4673 : 57460 : (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4674 : : {
4675 : : /* Warn about short-circuiting
4676 : : with impure function as second operand. */
4677 : 51489 : bool op2_f = false;
4678 : 51489 : gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4679 : : }
4680 : : break;
4681 : : }
4682 : :
4683 : : /* Logical ops on integers become bitwise ops with -fdec. */
4684 : 551 : else if (flag_dec
4685 : 523 : && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4686 : : {
4687 : 523 : e->ts.type = BT_INTEGER;
4688 : 523 : e->ts.kind = gfc_kind_max (op1, op2);
4689 : 523 : if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4690 : 289 : gfc_convert_type (op1, &e->ts, 1);
4691 : 523 : if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4692 : 144 : gfc_convert_type (op2, &e->ts, 1);
4693 : 523 : e = logical_to_bitwise (e);
4694 : 523 : goto simplify_op;
4695 : : }
4696 : :
4697 : 28 : CHECK_INTERFACES
4698 : 16 : gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4699 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4700 : : gfc_typename (op2));
4701 : 16 : return false;
4702 : :
4703 : 20172 : case INTRINSIC_NOT:
4704 : : /* Logical ops on integers become bitwise ops with -fdec. */
4705 : 20172 : if (flag_dec && op1->ts.type == BT_INTEGER)
4706 : : {
4707 : 19 : e->ts.type = BT_INTEGER;
4708 : 19 : e->ts.kind = op1->ts.kind;
4709 : 19 : e = logical_to_bitwise (e);
4710 : 19 : goto simplify_op;
4711 : : }
4712 : :
4713 : 20153 : if (op1->ts.type == BT_LOGICAL)
4714 : : {
4715 : 20147 : e->ts.type = BT_LOGICAL;
4716 : 20147 : e->ts.kind = op1->ts.kind;
4717 : 20147 : break;
4718 : : }
4719 : :
4720 : 6 : CHECK_INTERFACES
4721 : 3 : gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4722 : : gfc_typename (op1));
4723 : 3 : return false;
4724 : :
4725 : 21077 : case INTRINSIC_GT:
4726 : 21077 : case INTRINSIC_GT_OS:
4727 : 21077 : case INTRINSIC_GE:
4728 : 21077 : case INTRINSIC_GE_OS:
4729 : 21077 : case INTRINSIC_LT:
4730 : 21077 : case INTRINSIC_LT_OS:
4731 : 21077 : case INTRINSIC_LE:
4732 : 21077 : case INTRINSIC_LE_OS:
4733 : 21077 : if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4734 : : {
4735 : 18 : CHECK_INTERFACES
4736 : 0 : gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4737 : 0 : return false;
4738 : : }
4739 : :
4740 : : /* Fall through. */
4741 : :
4742 : 247863 : case INTRINSIC_EQ:
4743 : 247863 : case INTRINSIC_EQ_OS:
4744 : 247863 : case INTRINSIC_NE:
4745 : 247863 : case INTRINSIC_NE_OS:
4746 : :
4747 : 247863 : if (flag_dec
4748 : 1038 : && is_character_based (op1->ts.type)
4749 : 248198 : && is_character_based (op2->ts.type))
4750 : : {
4751 : 204 : convert_hollerith_to_character (op1);
4752 : 204 : convert_hollerith_to_character (op2);
4753 : : }
4754 : :
4755 : 247863 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4756 : 36818 : && op1->ts.kind == op2->ts.kind)
4757 : : {
4758 : 36781 : e->ts.type = BT_LOGICAL;
4759 : 36781 : e->ts.kind = gfc_default_logical_kind;
4760 : 36781 : break;
4761 : : }
4762 : :
4763 : : /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4764 : 211082 : if (op1->ts.type == BT_BOZ)
4765 : : {
4766 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4767 : : "as an operand of a relational operator"),
4768 : : &op1->where))
4769 : : return false;
4770 : :
4771 : 0 : if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4772 : : return false;
4773 : :
4774 : 0 : if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4775 : : return false;
4776 : : }
4777 : :
4778 : : /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4779 : 211082 : if (op2->ts.type == BT_BOZ)
4780 : : {
4781 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4782 : : " as an operand of a relational operator"),
4783 : : &op2->where))
4784 : : return false;
4785 : :
4786 : 0 : if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4787 : : return false;
4788 : :
4789 : 0 : if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4790 : : return false;
4791 : : }
4792 : 211082 : if (flag_dec
4793 : 211082 : && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4794 : 120 : convert_to_numeric (op1, op2);
4795 : :
4796 : 211082 : if (flag_dec
4797 : 211082 : && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4798 : 120 : convert_to_numeric (op2, op1);
4799 : :
4800 : 211082 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4801 : : {
4802 : : /* Do not perform conversions if operands are not conformable as
4803 : : required for the binary intrinsic operators (F2018:10.1.5).
4804 : : Defer to a possibly overloading user-defined operator. */
4805 : 209957 : if (!gfc_op_rank_conformable (op1, op2))
4806 : : {
4807 : 70 : CHECK_INTERFACES
4808 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4809 : 0 : &op1->where, &op2->where);
4810 : 0 : return false;
4811 : : }
4812 : :
4813 : 209887 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4814 : : {
4815 : 1 : CHECK_INTERFACES
4816 : 1 : gfc_error ("Inconsistent types for operator at %L and %L: "
4817 : 1 : "%s and %s", &op1->where, &op2->where,
4818 : : gfc_typename (op1), gfc_typename (op2));
4819 : 1 : return false;
4820 : : }
4821 : :
4822 : 209886 : gfc_type_convert_binary (e, 1);
4823 : :
4824 : 209886 : e->ts.type = BT_LOGICAL;
4825 : 209886 : e->ts.kind = gfc_default_logical_kind;
4826 : :
4827 : 209886 : if (warn_compare_reals)
4828 : : {
4829 : 69 : gfc_intrinsic_op op = e->value.op.op;
4830 : :
4831 : : /* Type conversion has made sure that the types of op1 and op2
4832 : : agree, so it is only necessary to check the first one. */
4833 : 69 : if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4834 : 13 : && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4835 : 6 : || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4836 : : {
4837 : 13 : const char *msg;
4838 : :
4839 : 13 : if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4840 : : msg = G_("Equality comparison for %s at %L");
4841 : : else
4842 : 6 : msg = G_("Inequality comparison for %s at %L");
4843 : :
4844 : 13 : gfc_warning (OPT_Wcompare_reals, msg,
4845 : : gfc_typename (op1), &op1->where);
4846 : : }
4847 : : }
4848 : :
4849 : : break;
4850 : : }
4851 : :
4852 : 1125 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4853 : : {
4854 : 2 : CHECK_INTERFACES
4855 : 4 : gfc_error ("Logicals at %L must be compared with %s instead of %s",
4856 : : &e->where,
4857 : 2 : (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4858 : : ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4859 : 2 : }
4860 : : else
4861 : : {
4862 : 1123 : CHECK_INTERFACES
4863 : 113 : gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4864 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4865 : : gfc_typename (op2));
4866 : : }
4867 : :
4868 : : return false;
4869 : :
4870 : 294 : case INTRINSIC_USER:
4871 : 294 : if (e->value.op.uop->op == NULL)
4872 : : {
4873 : 69 : const char *name = e->value.op.uop->name;
4874 : 69 : const char *guessed;
4875 : 69 : guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4876 : 69 : CHECK_INTERFACES
4877 : 11 : if (guessed)
4878 : 1 : gfc_error ("Unknown operator %qs at %L; did you mean "
4879 : : "%qs?", name, &e->where, guessed);
4880 : : else
4881 : 10 : gfc_error ("Unknown operator %qs at %L", name, &e->where);
4882 : : }
4883 : 225 : else if (op2 == NULL)
4884 : : {
4885 : 48 : CHECK_INTERFACES
4886 : 0 : gfc_error ("Operand of user operator %qs at %L is %s",
4887 : 0 : e->value.op.uop->name, &e->where, gfc_typename (op1));
4888 : : }
4889 : : else
4890 : : {
4891 : 177 : e->value.op.uop->op->sym->attr.referenced = 1;
4892 : 177 : CHECK_INTERFACES
4893 : 5 : gfc_error ("Operands of user operator %qs at %L are %s/%s",
4894 : 5 : e->value.op.uop->name, &e->where, gfc_typename (op1),
4895 : : gfc_typename (op2));
4896 : : }
4897 : :
4898 : : return false;
4899 : :
4900 : 20390 : case INTRINSIC_PARENTHESES:
4901 : 20390 : e->ts = op1->ts;
4902 : 20390 : if (e->ts.type == BT_CHARACTER)
4903 : 301 : e->ts.u.cl = op1->ts.u.cl;
4904 : : break;
4905 : :
4906 : 0 : default:
4907 : 0 : gfc_internal_error ("resolve_operator(): Bad intrinsic");
4908 : : }
4909 : :
4910 : : /* Deal with arrayness of an operand through an operator. */
4911 : :
4912 : 516932 : switch (e->value.op.op)
4913 : : {
4914 : 468449 : case INTRINSIC_PLUS:
4915 : 468449 : case INTRINSIC_MINUS:
4916 : 468449 : case INTRINSIC_TIMES:
4917 : 468449 : case INTRINSIC_DIVIDE:
4918 : 468449 : case INTRINSIC_POWER:
4919 : 468449 : case INTRINSIC_CONCAT:
4920 : 468449 : case INTRINSIC_AND:
4921 : 468449 : case INTRINSIC_OR:
4922 : 468449 : case INTRINSIC_EQV:
4923 : 468449 : case INTRINSIC_NEQV:
4924 : 468449 : case INTRINSIC_EQ:
4925 : 468449 : case INTRINSIC_EQ_OS:
4926 : 468449 : case INTRINSIC_NE:
4927 : 468449 : case INTRINSIC_NE_OS:
4928 : 468449 : case INTRINSIC_GT:
4929 : 468449 : case INTRINSIC_GT_OS:
4930 : 468449 : case INTRINSIC_GE:
4931 : 468449 : case INTRINSIC_GE_OS:
4932 : 468449 : case INTRINSIC_LT:
4933 : 468449 : case INTRINSIC_LT_OS:
4934 : 468449 : case INTRINSIC_LE:
4935 : 468449 : case INTRINSIC_LE_OS:
4936 : :
4937 : 468449 : if (op1->rank == 0 && op2->rank == 0)
4938 : 417712 : e->rank = 0;
4939 : :
4940 : 468449 : if (op1->rank == 0 && op2->rank != 0)
4941 : : {
4942 : 2611 : e->rank = op2->rank;
4943 : :
4944 : 2611 : if (e->shape == NULL)
4945 : 2581 : e->shape = gfc_copy_shape (op2->shape, op2->rank);
4946 : : }
4947 : :
4948 : 468449 : if (op1->rank != 0 && op2->rank == 0)
4949 : : {
4950 : 16504 : e->rank = op1->rank;
4951 : :
4952 : 16504 : if (e->shape == NULL)
4953 : 16486 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4954 : : }
4955 : :
4956 : 468449 : if (op1->rank != 0 && op2->rank != 0)
4957 : : {
4958 : 31622 : if (op1->rank == op2->rank)
4959 : : {
4960 : 31622 : e->rank = op1->rank;
4961 : 31622 : if (e->shape == NULL)
4962 : : {
4963 : 31561 : t = compare_shapes (op1, op2);
4964 : 31561 : if (!t)
4965 : 3 : e->shape = NULL;
4966 : : else
4967 : 31558 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4968 : : }
4969 : : }
4970 : : else
4971 : : {
4972 : : /* Allow higher level expressions to work. */
4973 : 0 : e->rank = 0;
4974 : :
4975 : : /* Try user-defined operators, and otherwise throw an error. */
4976 : 0 : CHECK_INTERFACES
4977 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4978 : 0 : &op1->where, &op2->where);
4979 : 0 : return false;
4980 : : }
4981 : : }
4982 : : break;
4983 : :
4984 : 48483 : case INTRINSIC_PARENTHESES:
4985 : 48483 : case INTRINSIC_NOT:
4986 : 48483 : case INTRINSIC_UPLUS:
4987 : 48483 : case INTRINSIC_UMINUS:
4988 : : /* Simply copy arrayness attribute */
4989 : 48483 : e->rank = op1->rank;
4990 : 48483 : e->corank = op1->corank;
4991 : :
4992 : 48483 : if (e->shape == NULL)
4993 : 48477 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4994 : :
4995 : : break;
4996 : :
4997 : : default:
4998 : : break;
4999 : : }
5000 : :
5001 : 517474 : simplify_op:
5002 : :
5003 : : /* Attempt to simplify the expression. */
5004 : 3 : if (t)
5005 : : {
5006 : 517471 : t = gfc_simplify_expr (e, 0);
5007 : : /* Some calls do not succeed in simplification and return false
5008 : : even though there is no error; e.g. variable references to
5009 : : PARAMETER arrays. */
5010 : 517471 : if (!gfc_is_constant_expr (e))
5011 : 474570 : t = true;
5012 : : }
5013 : : return t;
5014 : : }
5015 : :
5016 : : static bool
5017 : 150 : resolve_conditional (gfc_expr *expr)
5018 : : {
5019 : 150 : gfc_expr *condition, *true_expr, *false_expr;
5020 : :
5021 : 150 : condition = expr->value.conditional.condition;
5022 : 150 : true_expr = expr->value.conditional.true_expr;
5023 : 150 : false_expr = expr->value.conditional.false_expr;
5024 : :
5025 : 300 : if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
5026 : 300 : || !gfc_resolve_expr (false_expr))
5027 : 0 : return false;
5028 : :
5029 : 150 : if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
5030 : : {
5031 : 2 : gfc_error (
5032 : : "Condition in conditional expression must be a scalar logical at %L",
5033 : : &condition->where);
5034 : 2 : return false;
5035 : : }
5036 : :
5037 : 148 : if (true_expr->ts.type != false_expr->ts.type)
5038 : : {
5039 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5040 : : "must have the same declared type",
5041 : : &true_expr->where, &false_expr->where);
5042 : 1 : return false;
5043 : : }
5044 : :
5045 : 147 : if (true_expr->ts.kind != false_expr->ts.kind)
5046 : : {
5047 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5048 : : "must have the same kind parameter",
5049 : : &true_expr->where, &false_expr->where);
5050 : 1 : return false;
5051 : : }
5052 : :
5053 : 146 : if (true_expr->rank != false_expr->rank)
5054 : : {
5055 : 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5056 : : "must have the same rank",
5057 : : &true_expr->where, &false_expr->where);
5058 : 1 : return false;
5059 : : }
5060 : :
5061 : : /* TODO: support more data types for conditional expressions */
5062 : 145 : if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
5063 : 145 : && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
5064 : 55 : && true_expr->ts.type != BT_CHARACTER)
5065 : : {
5066 : 1 : gfc_error (
5067 : : "Sorry, only integer, logical, real, complex and character types are "
5068 : : "currently supported for conditional expressions at %L",
5069 : : &expr->where);
5070 : 1 : return false;
5071 : : }
5072 : :
5073 : : /* TODO: support arrays in conditional expressions */
5074 : 144 : if (true_expr->rank > 0)
5075 : : {
5076 : 1 : gfc_error ("Sorry, array is currently unsupported for conditional "
5077 : : "expressions at %L",
5078 : : &expr->where);
5079 : 1 : return false;
5080 : : }
5081 : :
5082 : 143 : expr->ts = true_expr->ts;
5083 : 143 : expr->rank = true_expr->rank;
5084 : 143 : return true;
5085 : : }
5086 : :
5087 : : /************** Array resolution subroutines **************/
5088 : :
5089 : : enum compare_result
5090 : : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
5091 : :
5092 : : /* Compare two integer expressions. */
5093 : :
5094 : : static compare_result
5095 : 450900 : compare_bound (gfc_expr *a, gfc_expr *b)
5096 : : {
5097 : 450900 : int i;
5098 : :
5099 : 450900 : if (a == NULL || a->expr_type != EXPR_CONSTANT
5100 : 294475 : || b == NULL || b->expr_type != EXPR_CONSTANT)
5101 : : return CMP_UNKNOWN;
5102 : :
5103 : : /* If either of the types isn't INTEGER, we must have
5104 : : raised an error earlier. */
5105 : :
5106 : 205302 : if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
5107 : : return CMP_UNKNOWN;
5108 : :
5109 : 205298 : i = mpz_cmp (a->value.integer, b->value.integer);
5110 : :
5111 : 205298 : if (i < 0)
5112 : : return CMP_LT;
5113 : 96744 : if (i > 0)
5114 : 38379 : return CMP_GT;
5115 : : return CMP_EQ;
5116 : : }
5117 : :
5118 : :
5119 : : /* Compare an integer expression with an integer. */
5120 : :
5121 : : static compare_result
5122 : 72786 : compare_bound_int (gfc_expr *a, int b)
5123 : : {
5124 : 72786 : int i;
5125 : :
5126 : 72786 : if (a == NULL
5127 : 31248 : || a->expr_type != EXPR_CONSTANT
5128 : 28449 : || a->ts.type != BT_INTEGER)
5129 : : return CMP_UNKNOWN;
5130 : :
5131 : 28449 : i = mpz_cmp_si (a->value.integer, b);
5132 : :
5133 : 28449 : if (i < 0)
5134 : : return CMP_LT;
5135 : 24410 : if (i > 0)
5136 : 21335 : return CMP_GT;
5137 : : return CMP_EQ;
5138 : : }
5139 : :
5140 : :
5141 : : /* Compare an integer expression with a mpz_t. */
5142 : :
5143 : : static compare_result
5144 : 67401 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
5145 : : {
5146 : 67401 : int i;
5147 : :
5148 : 67401 : if (a == NULL
5149 : 54989 : || a->expr_type != EXPR_CONSTANT
5150 : 52899 : || a->ts.type != BT_INTEGER)
5151 : : return CMP_UNKNOWN;
5152 : :
5153 : 52896 : i = mpz_cmp (a->value.integer, b);
5154 : :
5155 : 52896 : if (i < 0)
5156 : : return CMP_LT;
5157 : 24068 : if (i > 0)
5158 : 10336 : return CMP_GT;
5159 : : return CMP_EQ;
5160 : : }
5161 : :
5162 : :
5163 : : /* Compute the last value of a sequence given by a triplet.
5164 : : Return 0 if it wasn't able to compute the last value, or if the
5165 : : sequence if empty, and 1 otherwise. */
5166 : :
5167 : : static int
5168 : 50675 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
5169 : : gfc_expr *stride, mpz_t last)
5170 : : {
5171 : 50675 : mpz_t rem;
5172 : :
5173 : 50675 : if (start == NULL || start->expr_type != EXPR_CONSTANT
5174 : 35910 : || end == NULL || end->expr_type != EXPR_CONSTANT
5175 : 31356 : || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
5176 : : return 0;
5177 : :
5178 : 31037 : if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
5179 : 31036 : || (stride != NULL && stride->ts.type != BT_INTEGER))
5180 : : return 0;
5181 : :
5182 : 6476 : if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
5183 : : {
5184 : 24686 : if (compare_bound (start, end) == CMP_GT)
5185 : : return 0;
5186 : 23297 : mpz_set (last, end->value.integer);
5187 : 23297 : return 1;
5188 : : }
5189 : :
5190 : 6350 : if (compare_bound_int (stride, 0) == CMP_GT)
5191 : : {
5192 : : /* Stride is positive */
5193 : 5130 : if (mpz_cmp (start->value.integer, end->value.integer) > 0)
5194 : : return 0;
5195 : : }
5196 : : else
5197 : : {
5198 : : /* Stride is negative */
5199 : 1220 : if (mpz_cmp (start->value.integer, end->value.integer) < 0)
5200 : : return 0;
5201 : : }
5202 : :
5203 : 6330 : mpz_init (rem);
5204 : 6330 : mpz_sub (rem, end->value.integer, start->value.integer);
5205 : 6330 : mpz_tdiv_r (rem, rem, stride->value.integer);
5206 : 6330 : mpz_sub (last, end->value.integer, rem);
5207 : 6330 : mpz_clear (rem);
5208 : :
5209 : 6330 : return 1;
5210 : : }
5211 : :
5212 : :
5213 : : /* Compare a single dimension of an array reference to the array
5214 : : specification. */
5215 : :
5216 : : static bool
5217 : 207452 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5218 : : {
5219 : 207452 : mpz_t last_value;
5220 : :
5221 : 207452 : if (ar->dimen_type[i] == DIMEN_STAR)
5222 : : {
5223 : 413 : gcc_assert (ar->stride[i] == NULL);
5224 : : /* This implies [*] as [*:] and [*:3] are not possible. */
5225 : 413 : if (ar->start[i] == NULL)
5226 : : {
5227 : 339 : gcc_assert (ar->end[i] == NULL);
5228 : : return true;
5229 : : }
5230 : : }
5231 : :
5232 : : /* Given start, end and stride values, calculate the minimum and
5233 : : maximum referenced indexes. */
5234 : :
5235 : 207113 : switch (ar->dimen_type[i])
5236 : : {
5237 : : case DIMEN_VECTOR:
5238 : : case DIMEN_THIS_IMAGE:
5239 : : break;
5240 : :
5241 : 150074 : case DIMEN_STAR:
5242 : 150074 : case DIMEN_ELEMENT:
5243 : 150074 : if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5244 : : {
5245 : 2 : if (i < as->rank)
5246 : 2 : gfc_warning (0, "Array reference at %L is out of bounds "
5247 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5248 : 2 : mpz_get_si (ar->start[i]->value.integer),
5249 : 2 : mpz_get_si (as->lower[i]->value.integer), i+1);
5250 : : else
5251 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5252 : : "(%ld < %ld) in codimension %d", &ar->c_where[i],
5253 : 0 : mpz_get_si (ar->start[i]->value.integer),
5254 : 0 : mpz_get_si (as->lower[i]->value.integer),
5255 : 0 : i + 1 - as->rank);
5256 : 2 : return true;
5257 : : }
5258 : 150072 : if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5259 : : {
5260 : 39 : if (i < as->rank)
5261 : 39 : gfc_warning (0, "Array reference at %L is out of bounds "
5262 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5263 : 39 : mpz_get_si (ar->start[i]->value.integer),
5264 : 39 : mpz_get_si (as->upper[i]->value.integer), i+1);
5265 : : else
5266 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5267 : : "(%ld > %ld) in codimension %d", &ar->c_where[i],
5268 : 0 : mpz_get_si (ar->start[i]->value.integer),
5269 : 0 : mpz_get_si (as->upper[i]->value.integer),
5270 : 0 : i + 1 - as->rank);
5271 : 39 : return true;
5272 : : }
5273 : :
5274 : : break;
5275 : :
5276 : 50720 : case DIMEN_RANGE:
5277 : 50720 : {
5278 : : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5279 : : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5280 : :
5281 : 50720 : compare_result comp_start_end = compare_bound (AR_START, AR_END);
5282 : 50720 : compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5283 : :
5284 : : /* Check for zero stride, which is not allowed. */
5285 : 50720 : if (comp_stride_zero == CMP_EQ)
5286 : : {
5287 : 1 : gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5288 : 1 : return false;
5289 : : }
5290 : :
5291 : : /* if start == end || (stride > 0 && start < end)
5292 : : || (stride < 0 && start > end),
5293 : : then the array section contains at least one element. In this
5294 : : case, there is an out-of-bounds access if
5295 : : (start < lower || start > upper). */
5296 : 50719 : if (comp_start_end == CMP_EQ
5297 : 49992 : || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5298 : 47465 : && comp_start_end == CMP_LT)
5299 : 22246 : || (comp_stride_zero == CMP_LT
5300 : 22246 : && comp_start_end == CMP_GT))
5301 : : {
5302 : 29673 : if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5303 : : {
5304 : 27 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5305 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5306 : 27 : mpz_get_si (AR_START->value.integer),
5307 : 27 : mpz_get_si (as->lower[i]->value.integer), i+1);
5308 : 27 : return true;
5309 : : }
5310 : 29646 : if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5311 : : {
5312 : 17 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5313 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5314 : 17 : mpz_get_si (AR_START->value.integer),
5315 : 17 : mpz_get_si (as->upper[i]->value.integer), i+1);
5316 : 17 : return true;
5317 : : }
5318 : : }
5319 : :
5320 : : /* If we can compute the highest index of the array section,
5321 : : then it also has to be between lower and upper. */
5322 : 50675 : mpz_init (last_value);
5323 : 50675 : if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5324 : : last_value))
5325 : : {
5326 : 29627 : if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5327 : : {
5328 : 3 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5329 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5330 : : mpz_get_si (last_value),
5331 : 3 : mpz_get_si (as->lower[i]->value.integer), i+1);
5332 : 3 : mpz_clear (last_value);
5333 : 3 : return true;
5334 : : }
5335 : 29624 : if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5336 : : {
5337 : 7 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5338 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5339 : : mpz_get_si (last_value),
5340 : 7 : mpz_get_si (as->upper[i]->value.integer), i+1);
5341 : 7 : mpz_clear (last_value);
5342 : 7 : return true;
5343 : : }
5344 : : }
5345 : 50665 : mpz_clear (last_value);
5346 : :
5347 : : #undef AR_START
5348 : : #undef AR_END
5349 : : }
5350 : 50665 : break;
5351 : :
5352 : 0 : default:
5353 : 0 : gfc_internal_error ("check_dimension(): Bad array reference");
5354 : : }
5355 : :
5356 : : return true;
5357 : : }
5358 : :
5359 : :
5360 : : /* Compare an array reference with an array specification. */
5361 : :
5362 : : static bool
5363 : 411966 : compare_spec_to_ref (gfc_array_ref *ar)
5364 : : {
5365 : 411966 : gfc_array_spec *as;
5366 : 411966 : int i;
5367 : :
5368 : 411966 : as = ar->as;
5369 : 411966 : i = as->rank - 1;
5370 : : /* TODO: Full array sections are only allowed as actual parameters. */
5371 : 411966 : if (as->type == AS_ASSUMED_SIZE
5372 : 5765 : && (/*ar->type == AR_FULL
5373 : 5765 : ||*/ (ar->type == AR_SECTION
5374 : 514 : && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5375 : : {
5376 : 5 : gfc_error ("Rightmost upper bound of assumed size array section "
5377 : : "not specified at %L", &ar->where);
5378 : 5 : return false;
5379 : : }
5380 : :
5381 : 411961 : if (ar->type == AR_FULL)
5382 : : return true;
5383 : :
5384 : 156792 : if (as->rank != ar->dimen)
5385 : : {
5386 : 28 : gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5387 : : &ar->where, ar->dimen, as->rank);
5388 : 28 : return false;
5389 : : }
5390 : :
5391 : : /* ar->codimen == 0 is a local array. */
5392 : 156764 : if (as->corank != ar->codimen && ar->codimen != 0)
5393 : : {
5394 : 0 : gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5395 : : &ar->where, ar->codimen, as->corank);
5396 : 0 : return false;
5397 : : }
5398 : :
5399 : 356511 : for (i = 0; i < as->rank; i++)
5400 : 199748 : if (!check_dimension (i, ar, as))
5401 : : return false;
5402 : :
5403 : : /* Local access has no coarray spec. */
5404 : 156763 : if (ar->codimen != 0)
5405 : 14745 : for (i = as->rank; i < as->rank + as->corank; i++)
5406 : : {
5407 : 7706 : if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5408 : 5268 : && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5409 : : {
5410 : 2 : gfc_error ("Coindex of codimension %d must be a scalar at %L",
5411 : 2 : i + 1 - as->rank, &ar->where);
5412 : 2 : return false;
5413 : : }
5414 : 7704 : if (!check_dimension (i, ar, as))
5415 : : return false;
5416 : : }
5417 : :
5418 : : return true;
5419 : : }
5420 : :
5421 : :
5422 : : /* Resolve one part of an array index. */
5423 : :
5424 : : static bool
5425 : 704896 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5426 : : int force_index_integer_kind)
5427 : : {
5428 : 704896 : gfc_typespec ts;
5429 : :
5430 : 704896 : if (index == NULL)
5431 : : return true;
5432 : :
5433 : 209854 : if (!gfc_resolve_expr (index))
5434 : : return false;
5435 : :
5436 : 209831 : if (check_scalar && index->rank != 0)
5437 : : {
5438 : 2 : gfc_error ("Array index at %L must be scalar", &index->where);
5439 : 2 : return false;
5440 : : }
5441 : :
5442 : 209829 : if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5443 : : {
5444 : 3 : gfc_error ("Array index at %L must be of INTEGER type, found %s",
5445 : : &index->where, gfc_basic_typename (index->ts.type));
5446 : 3 : return false;
5447 : : }
5448 : :
5449 : 209826 : if (index->ts.type == BT_REAL)
5450 : 336 : if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5451 : : &index->where))
5452 : : return false;
5453 : :
5454 : 209826 : if ((index->ts.kind != gfc_index_integer_kind
5455 : 205142 : && force_index_integer_kind)
5456 : 179510 : || (index->ts.type != BT_INTEGER
5457 : : && index->ts.type != BT_UNKNOWN))
5458 : : {
5459 : 30651 : gfc_clear_ts (&ts);
5460 : 30651 : ts.type = BT_INTEGER;
5461 : 30651 : ts.kind = gfc_index_integer_kind;
5462 : :
5463 : 30651 : gfc_convert_type_warn (index, &ts, 2, 0);
5464 : : }
5465 : :
5466 : : return true;
5467 : : }
5468 : :
5469 : : /* Resolve one part of an array index. */
5470 : :
5471 : : bool
5472 : 470149 : gfc_resolve_index (gfc_expr *index, int check_scalar)
5473 : : {
5474 : 470149 : return gfc_resolve_index_1 (index, check_scalar, 1);
5475 : : }
5476 : :
5477 : : /* Resolve a dim argument to an intrinsic function. */
5478 : :
5479 : : bool
5480 : 23817 : gfc_resolve_dim_arg (gfc_expr *dim)
5481 : : {
5482 : 23817 : if (dim == NULL)
5483 : : return true;
5484 : :
5485 : 23817 : if (!gfc_resolve_expr (dim))
5486 : : return false;
5487 : :
5488 : 23817 : if (dim->rank != 0)
5489 : : {
5490 : 0 : gfc_error ("Argument dim at %L must be scalar", &dim->where);
5491 : 0 : return false;
5492 : :
5493 : : }
5494 : :
5495 : 23817 : if (dim->ts.type != BT_INTEGER)
5496 : : {
5497 : 0 : gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5498 : 0 : return false;
5499 : : }
5500 : :
5501 : 23817 : if (dim->ts.kind != gfc_index_integer_kind)
5502 : : {
5503 : 15209 : gfc_typespec ts;
5504 : :
5505 : 15209 : gfc_clear_ts (&ts);
5506 : 15209 : ts.type = BT_INTEGER;
5507 : 15209 : ts.kind = gfc_index_integer_kind;
5508 : :
5509 : 15209 : gfc_convert_type_warn (dim, &ts, 2, 0);
5510 : : }
5511 : :
5512 : : return true;
5513 : : }
5514 : :
5515 : : /* Given an expression that contains array references, update those array
5516 : : references to point to the right array specifications. While this is
5517 : : filled in during matching, this information is difficult to save and load
5518 : : in a module, so we take care of it here.
5519 : :
5520 : : The idea here is that the original array reference comes from the
5521 : : base symbol. We traverse the list of reference structures, setting
5522 : : the stored reference to references. Component references can
5523 : : provide an additional array specification. */
5524 : : static void
5525 : : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5526 : :
5527 : : static bool
5528 : 897 : find_array_spec (gfc_expr *e)
5529 : : {
5530 : 897 : gfc_array_spec *as;
5531 : 897 : gfc_component *c;
5532 : 897 : gfc_ref *ref;
5533 : 897 : bool class_as = false;
5534 : :
5535 : 897 : if (e->symtree->n.sym->assoc)
5536 : : {
5537 : 217 : if (e->symtree->n.sym->assoc->target)
5538 : 217 : gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5539 : 217 : resolve_assoc_var (e->symtree->n.sym, false);
5540 : : }
5541 : :
5542 : 897 : if (e->symtree->n.sym->ts.type == BT_CLASS)
5543 : : {
5544 : 112 : as = CLASS_DATA (e->symtree->n.sym)->as;
5545 : 112 : class_as = true;
5546 : : }
5547 : : else
5548 : 785 : as = e->symtree->n.sym->as;
5549 : :
5550 : 2034 : for (ref = e->ref; ref; ref = ref->next)
5551 : 1144 : switch (ref->type)
5552 : : {
5553 : 899 : case REF_ARRAY:
5554 : 899 : if (as == NULL)
5555 : : {
5556 : 7 : locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5557 : 14 : ? ref->u.ar.where : e->where);
5558 : 7 : gfc_error ("Invalid array reference of a non-array entity at %L",
5559 : : &loc);
5560 : 7 : return false;
5561 : : }
5562 : :
5563 : 892 : ref->u.ar.as = as;
5564 : 892 : if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5565 : : as = NULL;
5566 : : break;
5567 : :
5568 : 221 : case REF_COMPONENT:
5569 : 221 : c = ref->u.c.component;
5570 : 221 : if (c->attr.dimension)
5571 : : {
5572 : 90 : if (as != NULL && !(class_as && as == c->as))
5573 : 0 : gfc_internal_error ("find_array_spec(): unused as(1)");
5574 : 90 : as = c->as;
5575 : : }
5576 : :
5577 : : break;
5578 : :
5579 : : case REF_SUBSTRING:
5580 : : case REF_INQUIRY:
5581 : : break;
5582 : : }
5583 : :
5584 : 890 : if (as != NULL)
5585 : 0 : gfc_internal_error ("find_array_spec(): unused as(2)");
5586 : :
5587 : : return true;
5588 : : }
5589 : :
5590 : :
5591 : : /* Resolve an array reference. */
5592 : :
5593 : : static bool
5594 : 412691 : resolve_array_ref (gfc_array_ref *ar)
5595 : : {
5596 : 412691 : int i, check_scalar;
5597 : 412691 : gfc_expr *e;
5598 : :
5599 : 647410 : for (i = 0; i < ar->dimen + ar->codimen; i++)
5600 : : {
5601 : 234747 : check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5602 : :
5603 : : /* Do not force gfc_index_integer_kind for the start. We can
5604 : : do fine with any integer kind. This avoids temporary arrays
5605 : : created for indexing with a vector. */
5606 : 234747 : if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5607 : : return false;
5608 : 234721 : if (!gfc_resolve_index (ar->end[i], check_scalar))
5609 : : return false;
5610 : 234719 : if (!gfc_resolve_index (ar->stride[i], check_scalar))
5611 : : return false;
5612 : :
5613 : 234719 : e = ar->start[i];
5614 : :
5615 : 234719 : if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5616 : 140642 : switch (e->rank)
5617 : : {
5618 : 139778 : case 0:
5619 : 139778 : ar->dimen_type[i] = DIMEN_ELEMENT;
5620 : 139778 : break;
5621 : :
5622 : 864 : case 1:
5623 : 864 : ar->dimen_type[i] = DIMEN_VECTOR;
5624 : 864 : if (e->expr_type == EXPR_VARIABLE
5625 : 446 : && e->symtree->n.sym->ts.type == BT_DERIVED)
5626 : 13 : ar->start[i] = gfc_get_parentheses (e);
5627 : : break;
5628 : :
5629 : 0 : default:
5630 : 0 : gfc_error ("Array index at %L is an array of rank %d",
5631 : : &ar->c_where[i], e->rank);
5632 : 0 : return false;
5633 : : }
5634 : :
5635 : : /* Fill in the upper bound, which may be lower than the
5636 : : specified one for something like a(2:10:5), which is
5637 : : identical to a(2:7:5). Only relevant for strides not equal
5638 : : to one. Don't try a division by zero. */
5639 : 234719 : if (ar->dimen_type[i] == DIMEN_RANGE
5640 : 70643 : && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5641 : 8205 : && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5642 : 8058 : && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5643 : : {
5644 : 8057 : mpz_t size, end;
5645 : :
5646 : 8057 : if (gfc_ref_dimen_size (ar, i, &size, &end))
5647 : : {
5648 : 6360 : if (ar->end[i] == NULL)
5649 : : {
5650 : 7888 : ar->end[i] =
5651 : 3944 : gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5652 : : &ar->where);
5653 : 3944 : mpz_set (ar->end[i]->value.integer, end);
5654 : : }
5655 : 2416 : else if (ar->end[i]->ts.type == BT_INTEGER
5656 : 2416 : && ar->end[i]->expr_type == EXPR_CONSTANT)
5657 : : {
5658 : 2416 : mpz_set (ar->end[i]->value.integer, end);
5659 : : }
5660 : : else
5661 : 0 : gcc_unreachable ();
5662 : :
5663 : 6360 : mpz_clear (size);
5664 : 6360 : mpz_clear (end);
5665 : : }
5666 : : }
5667 : : }
5668 : :
5669 : 412663 : if (ar->type == AR_FULL)
5670 : : {
5671 : 258126 : if (ar->as->rank == 0)
5672 : 2923 : ar->type = AR_ELEMENT;
5673 : :
5674 : : /* Make sure array is the same as array(:,:), this way
5675 : : we don't need to special case all the time. */
5676 : 258126 : ar->dimen = ar->as->rank;
5677 : 617469 : for (i = 0; i < ar->dimen; i++)
5678 : : {
5679 : 359343 : ar->dimen_type[i] = DIMEN_RANGE;
5680 : :
5681 : 359343 : gcc_assert (ar->start[i] == NULL);
5682 : 359343 : gcc_assert (ar->end[i] == NULL);
5683 : 359343 : gcc_assert (ar->stride[i] == NULL);
5684 : : }
5685 : : }
5686 : :
5687 : : /* If the reference type is unknown, figure out what kind it is. */
5688 : :
5689 : 412663 : if (ar->type == AR_UNKNOWN)
5690 : : {
5691 : 142402 : ar->type = AR_ELEMENT;
5692 : 277000 : for (i = 0; i < ar->dimen; i++)
5693 : 171425 : if (ar->dimen_type[i] == DIMEN_RANGE
5694 : 171425 : || ar->dimen_type[i] == DIMEN_VECTOR)
5695 : : {
5696 : 36827 : ar->type = AR_SECTION;
5697 : 36827 : break;
5698 : : }
5699 : : }
5700 : :
5701 : 412663 : if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5702 : : return false;
5703 : :
5704 : 412627 : if (ar->as->corank && ar->codimen == 0)
5705 : : {
5706 : 1655 : int n;
5707 : 1655 : ar->codimen = ar->as->corank;
5708 : 4546 : for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5709 : 2891 : ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5710 : : }
5711 : :
5712 : 412627 : if (ar->codimen)
5713 : : {
5714 : 10647 : if (ar->team_type == TEAM_NUMBER)
5715 : : {
5716 : 40 : if (!gfc_resolve_expr (ar->team))
5717 : : return false;
5718 : :
5719 : 40 : if (ar->team->rank != 0)
5720 : : {
5721 : 0 : gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5722 : : &ar->team->where);
5723 : 0 : return false;
5724 : : }
5725 : :
5726 : 40 : if (ar->team->ts.type != BT_INTEGER)
5727 : : {
5728 : 4 : gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5729 : : "type, found %s",
5730 : 4 : &ar->team->where,
5731 : : gfc_basic_typename (ar->team->ts.type));
5732 : 4 : return false;
5733 : : }
5734 : : }
5735 : 10607 : else if (ar->team_type == TEAM_TEAM)
5736 : : {
5737 : 28 : if (!gfc_resolve_expr (ar->team))
5738 : : return false;
5739 : :
5740 : 28 : if (ar->team->rank != 0)
5741 : : {
5742 : 2 : gfc_error ("TEAM argument at %L must be scalar",
5743 : : &ar->team->where);
5744 : 2 : return false;
5745 : : }
5746 : :
5747 : 26 : if (ar->team->ts.type != BT_DERIVED
5748 : 24 : || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5749 : 24 : || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5750 : : {
5751 : 2 : gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5752 : : "the intrinsic module ISO_FORTRAN_ENV, found %s",
5753 : 2 : &ar->team->where,
5754 : : gfc_basic_typename (ar->team->ts.type));
5755 : 2 : return false;
5756 : : }
5757 : : }
5758 : 10639 : if (ar->stat)
5759 : : {
5760 : 45 : if (!gfc_resolve_expr (ar->stat))
5761 : : return false;
5762 : :
5763 : 45 : if (ar->stat->rank != 0)
5764 : : {
5765 : 2 : gfc_error ("STAT argument at %L must be scalar",
5766 : : &ar->stat->where);
5767 : 2 : return false;
5768 : : }
5769 : :
5770 : 43 : if (ar->stat->ts.type != BT_INTEGER)
5771 : : {
5772 : 2 : gfc_error ("STAT argument at %L must be of INTEGER "
5773 : : "type, found %s",
5774 : 2 : &ar->stat->where,
5775 : : gfc_basic_typename (ar->stat->ts.type));
5776 : 2 : return false;
5777 : : }
5778 : :
5779 : 41 : if (ar->stat->expr_type != EXPR_VARIABLE)
5780 : : {
5781 : 0 : gfc_error ("STAT's expression at %L must be a variable",
5782 : : &ar->stat->where);
5783 : 0 : return false;
5784 : : }
5785 : : }
5786 : : }
5787 : : return true;
5788 : : }
5789 : :
5790 : :
5791 : : bool
5792 : 8327 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5793 : : {
5794 : 8327 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5795 : :
5796 : 8327 : if (ref->u.ss.start != NULL)
5797 : : {
5798 : 8327 : if (!gfc_resolve_expr (ref->u.ss.start))
5799 : : return false;
5800 : :
5801 : 8327 : if (ref->u.ss.start->ts.type != BT_INTEGER)
5802 : : {
5803 : 1 : gfc_error ("Substring start index at %L must be of type INTEGER",
5804 : : &ref->u.ss.start->where);
5805 : 1 : return false;
5806 : : }
5807 : :
5808 : 8326 : if (ref->u.ss.start->rank != 0)
5809 : : {
5810 : 0 : gfc_error ("Substring start index at %L must be scalar",
5811 : : &ref->u.ss.start->where);
5812 : 0 : return false;
5813 : : }
5814 : :
5815 : 8326 : if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5816 : 8326 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5817 : 37 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5818 : : {
5819 : 1 : gfc_error ("Substring start index at %L is less than one",
5820 : : &ref->u.ss.start->where);
5821 : 1 : return false;
5822 : : }
5823 : : }
5824 : :
5825 : 8325 : if (ref->u.ss.end != NULL)
5826 : : {
5827 : 8155 : if (!gfc_resolve_expr (ref->u.ss.end))
5828 : : return false;
5829 : :
5830 : 8155 : if (ref->u.ss.end->ts.type != BT_INTEGER)
5831 : : {
5832 : 1 : gfc_error ("Substring end index at %L must be of type INTEGER",
5833 : : &ref->u.ss.end->where);
5834 : 1 : return false;
5835 : : }
5836 : :
5837 : 8154 : if (ref->u.ss.end->rank != 0)
5838 : : {
5839 : 0 : gfc_error ("Substring end index at %L must be scalar",
5840 : : &ref->u.ss.end->where);
5841 : 0 : return false;
5842 : : }
5843 : :
5844 : 8154 : if (ref->u.ss.length != NULL
5845 : 7820 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5846 : 8166 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5847 : 12 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5848 : : {
5849 : 4 : gfc_error ("Substring end index at %L exceeds the string length",
5850 : : &ref->u.ss.start->where);
5851 : 4 : return false;
5852 : : }
5853 : :
5854 : 8150 : if (compare_bound_mpz_t (ref->u.ss.end,
5855 : 8150 : gfc_integer_kinds[k].huge) == CMP_GT
5856 : 8150 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5857 : 7 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5858 : : {
5859 : 4 : gfc_error ("Substring end index at %L is too large",
5860 : : &ref->u.ss.end->where);
5861 : 4 : return false;
5862 : : }
5863 : : /* If the substring has the same length as the original
5864 : : variable, the reference itself can be deleted. */
5865 : :
5866 : 8146 : if (ref->u.ss.length != NULL
5867 : 7812 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5868 : 9060 : && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5869 : 228 : *equal_length = true;
5870 : : }
5871 : :
5872 : : return true;
5873 : : }
5874 : :
5875 : :
5876 : : /* This function supplies missing substring charlens. */
5877 : :
5878 : : void
5879 : 4543 : gfc_resolve_substring_charlen (gfc_expr *e)
5880 : : {
5881 : 4543 : gfc_ref *char_ref;
5882 : 4543 : gfc_expr *start, *end;
5883 : 4543 : gfc_typespec *ts = NULL;
5884 : 4543 : mpz_t diff;
5885 : :
5886 : 8842 : for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5887 : : {
5888 : 7008 : if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5889 : : break;
5890 : 4299 : if (char_ref->type == REF_COMPONENT)
5891 : 322 : ts = &char_ref->u.c.component->ts;
5892 : : }
5893 : :
5894 : 4543 : if (!char_ref || char_ref->type == REF_INQUIRY)
5895 : 1896 : return;
5896 : :
5897 : 2709 : gcc_assert (char_ref->next == NULL);
5898 : :
5899 : 2709 : if (e->ts.u.cl)
5900 : : {
5901 : 120 : if (e->ts.u.cl->length)
5902 : 108 : gfc_free_expr (e->ts.u.cl->length);
5903 : 12 : else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5904 : : return;
5905 : : }
5906 : :
5907 : 2697 : if (!e->ts.u.cl)
5908 : 2589 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5909 : :
5910 : 2697 : if (char_ref->u.ss.start)
5911 : 2697 : start = gfc_copy_expr (char_ref->u.ss.start);
5912 : : else
5913 : 0 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5914 : :
5915 : 2697 : if (char_ref->u.ss.end)
5916 : 2647 : end = gfc_copy_expr (char_ref->u.ss.end);
5917 : 50 : else if (e->expr_type == EXPR_VARIABLE)
5918 : : {
5919 : 50 : if (!ts)
5920 : 32 : ts = &e->symtree->n.sym->ts;
5921 : 50 : end = gfc_copy_expr (ts->u.cl->length);
5922 : : }
5923 : : else
5924 : : end = NULL;
5925 : :
5926 : 2697 : if (!start || !end)
5927 : : {
5928 : 50 : gfc_free_expr (start);
5929 : 50 : gfc_free_expr (end);
5930 : 50 : return;
5931 : : }
5932 : :
5933 : : /* Length = (end - start + 1).
5934 : : Check first whether it has a constant length. */
5935 : 2647 : if (gfc_dep_difference (end, start, &diff))
5936 : : {
5937 : 2532 : gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5938 : : &e->where);
5939 : :
5940 : 2532 : mpz_add_ui (len->value.integer, diff, 1);
5941 : 2532 : mpz_clear (diff);
5942 : 2532 : e->ts.u.cl->length = len;
5943 : : /* The check for length < 0 is handled below */
5944 : : }
5945 : : else
5946 : : {
5947 : 115 : e->ts.u.cl->length = gfc_subtract (end, start);
5948 : 115 : e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5949 : : gfc_get_int_expr (gfc_charlen_int_kind,
5950 : : NULL, 1));
5951 : : }
5952 : :
5953 : : /* F2008, 6.4.1: Both the starting point and the ending point shall
5954 : : be within the range 1, 2, ..., n unless the starting point exceeds
5955 : : the ending point, in which case the substring has length zero. */
5956 : :
5957 : 2647 : if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5958 : 15 : mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5959 : :
5960 : 2647 : e->ts.u.cl->length->ts.type = BT_INTEGER;
5961 : 2647 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5962 : :
5963 : : /* Make sure that the length is simplified. */
5964 : 2647 : gfc_simplify_expr (e->ts.u.cl->length, 1);
5965 : 2647 : gfc_resolve_expr (e->ts.u.cl->length);
5966 : : }
5967 : :
5968 : :
5969 : : /* Convert an array reference to an array element so that PDT KIND and LEN
5970 : : or inquiry references are always scalar. */
5971 : :
5972 : : static void
5973 : 21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
5974 : : {
5975 : 21 : gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5976 : 21 : int dim;
5977 : :
5978 : 21 : array_ref->u.ar.type = AR_ELEMENT;
5979 : 21 : expr->rank = 0;
5980 : : /* Suppress the runtime bounds check. */
5981 : 21 : expr->no_bounds_check = 1;
5982 : 42 : for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5983 : : {
5984 : 21 : array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5985 : 21 : if (array_ref->u.ar.start[dim])
5986 : 0 : gfc_free_expr (array_ref->u.ar.start[dim]);
5987 : :
5988 : 21 : if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
5989 : 9 : array_ref->u.ar.start[dim]
5990 : 9 : = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
5991 : : else
5992 : 12 : array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
5993 : :
5994 : 21 : if (array_ref->u.ar.end[dim])
5995 : 0 : gfc_free_expr (array_ref->u.ar.end[dim]);
5996 : 21 : if (array_ref->u.ar.stride[dim])
5997 : 0 : gfc_free_expr (array_ref->u.ar.stride[dim]);
5998 : : }
5999 : 21 : gfc_free_expr (unity);
6000 : 21 : }
6001 : :
6002 : :
6003 : : /* Resolve subtype references. */
6004 : :
6005 : : bool
6006 : 524887 : gfc_resolve_ref (gfc_expr *expr)
6007 : : {
6008 : 524887 : int current_part_dimension, n_components, seen_part_dimension;
6009 : 524887 : gfc_ref *ref, **prev, *array_ref;
6010 : 524887 : bool equal_length;
6011 : 524887 : gfc_symbol *last_pdt = NULL;
6012 : :
6013 : 1028004 : for (ref = expr->ref; ref; ref = ref->next)
6014 : 504014 : if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
6015 : : {
6016 : 897 : if (!find_array_spec (expr))
6017 : : return false;
6018 : : break;
6019 : : }
6020 : :
6021 : 1532827 : for (prev = &expr->ref; *prev != NULL;
6022 : 504075 : prev = *prev == NULL ? prev : &(*prev)->next)
6023 : 504159 : switch ((*prev)->type)
6024 : : {
6025 : 412691 : case REF_ARRAY:
6026 : 412691 : if (!resolve_array_ref (&(*prev)->u.ar))
6027 : : return false;
6028 : : break;
6029 : :
6030 : : case REF_COMPONENT:
6031 : : case REF_INQUIRY:
6032 : : break;
6033 : :
6034 : 8046 : case REF_SUBSTRING:
6035 : 8046 : equal_length = false;
6036 : 8046 : if (!gfc_resolve_substring (*prev, &equal_length))
6037 : : return false;
6038 : :
6039 : 8038 : if (expr->expr_type != EXPR_SUBSTRING && equal_length)
6040 : : {
6041 : : /* Remove the reference and move the charlen, if any. */
6042 : 203 : ref = *prev;
6043 : 203 : *prev = ref->next;
6044 : 203 : ref->next = NULL;
6045 : 203 : expr->ts.u.cl = ref->u.ss.length;
6046 : 203 : ref->u.ss.length = NULL;
6047 : 203 : gfc_free_ref_list (ref);
6048 : : }
6049 : : break;
6050 : : }
6051 : :
6052 : : /* Check constraints on part references. */
6053 : :
6054 : 524796 : current_part_dimension = 0;
6055 : 524796 : seen_part_dimension = 0;
6056 : 524796 : n_components = 0;
6057 : 524796 : array_ref = NULL;
6058 : :
6059 : 524796 : if (expr->expr_type == EXPR_VARIABLE
6060 : 437605 : && expr->symtree->n.sym->ts.type == BT_DERIVED
6061 : 74834 : && expr->symtree->n.sym->ts.u.derived->attr.pdt_type)
6062 : 524796 : last_pdt = expr->symtree->n.sym->ts.u.derived;
6063 : :
6064 : 1028643 : for (ref = expr->ref; ref; ref = ref->next)
6065 : : {
6066 : 503858 : switch (ref->type)
6067 : : {
6068 : 412608 : case REF_ARRAY:
6069 : 412608 : array_ref = ref;
6070 : 412608 : switch (ref->u.ar.type)
6071 : : {
6072 : 255201 : case AR_FULL:
6073 : : /* Coarray scalar. */
6074 : 255201 : if (ref->u.ar.as->rank == 0)
6075 : : {
6076 : : current_part_dimension = 0;
6077 : : break;
6078 : : }
6079 : : /* Fall through. */
6080 : 294728 : case AR_SECTION:
6081 : 294728 : current_part_dimension = 1;
6082 : 294728 : break;
6083 : :
6084 : 117880 : case AR_ELEMENT:
6085 : 117880 : array_ref = NULL;
6086 : 117880 : current_part_dimension = 0;
6087 : 117880 : break;
6088 : :
6089 : 0 : case AR_UNKNOWN:
6090 : 0 : gfc_internal_error ("resolve_ref(): Bad array reference");
6091 : : }
6092 : :
6093 : : break;
6094 : :
6095 : 82690 : case REF_COMPONENT:
6096 : 82690 : if (current_part_dimension || seen_part_dimension)
6097 : : {
6098 : : /* F03:C614. */
6099 : 5993 : if (ref->u.c.component->attr.pointer
6100 : 5990 : || ref->u.c.component->attr.proc_pointer
6101 : 5989 : || (ref->u.c.component->ts.type == BT_CLASS
6102 : 1 : && CLASS_DATA (ref->u.c.component)->attr.pointer))
6103 : : {
6104 : 4 : gfc_error ("Component to the right of a part reference "
6105 : : "with nonzero rank must not have the POINTER "
6106 : : "attribute at %L", &expr->where);
6107 : 4 : return false;
6108 : : }
6109 : 5989 : else if (ref->u.c.component->attr.allocatable
6110 : 5983 : || (ref->u.c.component->ts.type == BT_CLASS
6111 : 1 : && CLASS_DATA (ref->u.c.component)->attr.allocatable))
6112 : :
6113 : : {
6114 : 7 : gfc_error ("Component to the right of a part reference "
6115 : : "with nonzero rank must not have the ALLOCATABLE "
6116 : : "attribute at %L", &expr->where);
6117 : 7 : return false;
6118 : : }
6119 : : }
6120 : :
6121 : : /* Sometimes the component in a component reference is that of the
6122 : : pdt_template. Point to the component of pdt_type instead. This
6123 : : ensures that the component gets a backend_decl in translation. */
6124 : 82679 : if (last_pdt)
6125 : : {
6126 : 1423 : gfc_component *cmp = last_pdt->components;
6127 : 3755 : for (; cmp; cmp = cmp->next)
6128 : 3649 : if (!strcmp (cmp->name, ref->u.c.component->name))
6129 : : {
6130 : 1317 : ref->u.c.component = cmp;
6131 : 1317 : break;
6132 : : }
6133 : 1423 : ref->u.c.sym = last_pdt;
6134 : : }
6135 : :
6136 : : /* Convert pdt_templates, if necessary, and update 'last_pdt'. */
6137 : 82679 : if (ref->u.c.component->ts.type == BT_DERIVED)
6138 : : {
6139 : 18189 : if (ref->u.c.component->ts.u.derived->attr.pdt_template)
6140 : : {
6141 : 0 : if (gfc_get_pdt_instance (ref->u.c.component->param_list,
6142 : : &ref->u.c.component->ts.u.derived,
6143 : : NULL) != MATCH_YES)
6144 : : return false;
6145 : 0 : last_pdt = ref->u.c.component->ts.u.derived;
6146 : : }
6147 : 18189 : else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
6148 : 231 : last_pdt = ref->u.c.component->ts.u.derived;
6149 : : else
6150 : : last_pdt = NULL;
6151 : : }
6152 : :
6153 : : /* The F08 standard requires(See R425, R431, R435, and in particular
6154 : : Note 6.7) that a PDT parameter reference be a scalar even if
6155 : : the designator is an array." */
6156 : 82679 : if (array_ref && last_pdt && last_pdt->attr.pdt_type
6157 : 23 : && (ref->u.c.component->attr.pdt_kind
6158 : 23 : || ref->u.c.component->attr.pdt_len))
6159 : 7 : reset_array_ref_to_scalar (expr, array_ref);
6160 : :
6161 : 82679 : n_components++;
6162 : 82679 : break;
6163 : :
6164 : : case REF_SUBSTRING:
6165 : : break;
6166 : :
6167 : 725 : case REF_INQUIRY:
6168 : : /* Implement requirement in note 9.7 of F2018 that the result of the
6169 : : LEN inquiry be a scalar. */
6170 : 725 : if (ref->u.i == INQUIRY_LEN && array_ref
6171 : 40 : && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
6172 : 40 : || expr->ts.type == BT_INTEGER))
6173 : 14 : reset_array_ref_to_scalar (expr, array_ref);
6174 : : break;
6175 : : }
6176 : :
6177 : 503847 : if (((ref->type == REF_COMPONENT && n_components > 1)
6178 : 492152 : || ref->next == NULL)
6179 : : && current_part_dimension
6180 : 445325 : && seen_part_dimension)
6181 : : {
6182 : 0 : gfc_error ("Two or more part references with nonzero rank must "
6183 : : "not be specified at %L", &expr->where);
6184 : 0 : return false;
6185 : : }
6186 : :
6187 : 503847 : if (ref->type == REF_COMPONENT)
6188 : : {
6189 : 82679 : if (current_part_dimension)
6190 : 5801 : seen_part_dimension = 1;
6191 : :
6192 : : /* reset to make sure */
6193 : : current_part_dimension = 0;
6194 : : }
6195 : : }
6196 : :
6197 : : return true;
6198 : : }
6199 : :
6200 : :
6201 : : /* Given an expression, determine its shape. This is easier than it sounds.
6202 : : Leaves the shape array NULL if it is not possible to determine the shape. */
6203 : :
6204 : : static void
6205 : 2549819 : expression_shape (gfc_expr *e)
6206 : : {
6207 : 2549819 : mpz_t array[GFC_MAX_DIMENSIONS];
6208 : 2549819 : int i;
6209 : :
6210 : 2549819 : if (e->rank <= 0 || e->shape != NULL)
6211 : 2380692 : return;
6212 : :
6213 : 683402 : for (i = 0; i < e->rank; i++)
6214 : 461603 : if (!gfc_array_dimen_size (e, i, &array[i]))
6215 : 169127 : goto fail;
6216 : :
6217 : 221799 : e->shape = gfc_get_shape (e->rank);
6218 : :
6219 : 221799 : memcpy (e->shape, array, e->rank * sizeof (mpz_t));
6220 : :
6221 : 221799 : return;
6222 : :
6223 : 169127 : fail:
6224 : 170798 : for (i--; i >= 0; i--)
6225 : 1671 : mpz_clear (array[i]);
6226 : : }
6227 : :
6228 : :
6229 : : /* Given a variable expression node, compute the rank of the expression by
6230 : : examining the base symbol and any reference structures it may have. */
6231 : :
6232 : : void
6233 : 2549819 : gfc_expression_rank (gfc_expr *e)
6234 : : {
6235 : 2549819 : gfc_ref *ref, *last_arr_ref = nullptr;
6236 : 2549819 : int i, rank, corank;
6237 : :
6238 : : /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
6239 : : could lead to serious confusion... */
6240 : 2549819 : gcc_assert (e->expr_type != EXPR_COMPCALL);
6241 : :
6242 : 2549819 : if (e->ref == NULL)
6243 : : {
6244 : 1885722 : if (e->expr_type == EXPR_ARRAY)
6245 : 67637 : goto done;
6246 : : /* Constructors can have a rank different from one via RESHAPE(). */
6247 : :
6248 : 1818085 : if (e->symtree != NULL)
6249 : : {
6250 : : /* After errors the ts.u.derived of a CLASS might not be set. */
6251 : 1818073 : gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
6252 : 13672 : && e->symtree->n.sym->ts.u.derived
6253 : 13667 : && CLASS_DATA (e->symtree->n.sym))
6254 : 1818073 : ? CLASS_DATA (e->symtree->n.sym)->as
6255 : : : e->symtree->n.sym->as;
6256 : 1818073 : if (as)
6257 : : {
6258 : 580 : e->rank = as->rank;
6259 : 580 : e->corank = as->corank;
6260 : 580 : goto done;
6261 : : }
6262 : : }
6263 : 1817505 : e->rank = 0;
6264 : 1817505 : e->corank = 0;
6265 : 1817505 : goto done;
6266 : : }
6267 : :
6268 : : rank = 0;
6269 : : corank = 0;
6270 : :
6271 : 1042725 : for (ref = e->ref; ref; ref = ref->next)
6272 : : {
6273 : 760737 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6274 : 547 : && ref->u.c.component->attr.function && !ref->next)
6275 : : {
6276 : 356 : rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6277 : 356 : corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6278 : : }
6279 : :
6280 : 760737 : if (ref->type != REF_ARRAY)
6281 : 148150 : continue;
6282 : :
6283 : 612587 : last_arr_ref = ref;
6284 : 612587 : if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6285 : : {
6286 : 338130 : rank = ref->u.ar.as->rank;
6287 : 338130 : break;
6288 : : }
6289 : :
6290 : 274457 : if (ref->u.ar.type == AR_SECTION)
6291 : : {
6292 : : /* Figure out the rank of the section. */
6293 : 43979 : if (rank != 0)
6294 : 0 : gfc_internal_error ("gfc_expression_rank(): Two array specs");
6295 : :
6296 : 110154 : for (i = 0; i < ref->u.ar.dimen; i++)
6297 : 66175 : if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6298 : 66175 : || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6299 : 57563 : rank++;
6300 : :
6301 : : break;
6302 : : }
6303 : : }
6304 : 664097 : if (last_arr_ref && last_arr_ref->u.ar.as
6305 : 595688 : && last_arr_ref->u.ar.as->rank != -1)
6306 : : {
6307 : 14912 : for (i = last_arr_ref->u.ar.as->rank;
6308 : 602504 : i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6309 : : {
6310 : : /* For unknown dimen in non-resolved as assume full corank. */
6311 : 15651 : if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6312 : 15178 : || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6313 : 266 : && !last_arr_ref->u.ar.as->resolved))
6314 : : {
6315 : : corank = last_arr_ref->u.ar.as->corank;
6316 : : break;
6317 : : }
6318 : 14912 : else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6319 : 14912 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6320 : 14829 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6321 : 12912 : corank++;
6322 : 2000 : else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6323 : 0 : gfc_internal_error ("Illegal coarray index");
6324 : : }
6325 : : }
6326 : :
6327 : 664097 : e->rank = rank;
6328 : 664097 : e->corank = corank;
6329 : :
6330 : 2549819 : done:
6331 : 2549819 : expression_shape (e);
6332 : 2549819 : }
6333 : :
6334 : :
6335 : : /* Given two expressions, check that their rank is conformable, i.e. either
6336 : : both have the same rank or at least one is a scalar. */
6337 : :
6338 : : bool
6339 : 12181175 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6340 : : {
6341 : 12181175 : if (op1->expr_type == EXPR_VARIABLE)
6342 : 722843 : gfc_expression_rank (op1);
6343 : 12181175 : if (op2->expr_type == EXPR_VARIABLE)
6344 : 443720 : gfc_expression_rank (op2);
6345 : :
6346 : 74398 : return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6347 : 12255247 : && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
6348 : 20 : || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
6349 : : }
6350 : :
6351 : : /* Resolve a variable expression. */
6352 : :
6353 : : static bool
6354 : 1297595 : resolve_variable (gfc_expr *e)
6355 : : {
6356 : 1297595 : gfc_symbol *sym;
6357 : 1297595 : bool t;
6358 : :
6359 : 1297595 : t = true;
6360 : :
6361 : 1297595 : if (e->symtree == NULL)
6362 : : return false;
6363 : 1297176 : sym = e->symtree->n.sym;
6364 : :
6365 : : /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6366 : : as ts.type is set to BT_ASSUMED in resolve_symbol. */
6367 : 1297176 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6368 : : {
6369 : 183 : if (!actual_arg || inquiry_argument)
6370 : : {
6371 : 2 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6372 : : "be used as actual argument", sym->name, &e->where);
6373 : 2 : return false;
6374 : : }
6375 : : }
6376 : : /* TS 29113, 407b. */
6377 : 1296993 : else if (e->ts.type == BT_ASSUMED)
6378 : : {
6379 : 571 : if (!actual_arg)
6380 : : {
6381 : 20 : gfc_error ("Assumed-type variable %s at %L may only be used "
6382 : : "as actual argument", sym->name, &e->where);
6383 : 20 : return false;
6384 : : }
6385 : 551 : else if (inquiry_argument && !first_actual_arg)
6386 : : {
6387 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6388 : : for all inquiry functions in resolve_function; the reason is
6389 : : that the function-name resolution happens too late in that
6390 : : function. */
6391 : 0 : gfc_error ("Assumed-type variable %s at %L as actual argument to "
6392 : : "an inquiry function shall be the first argument",
6393 : : sym->name, &e->where);
6394 : 0 : return false;
6395 : : }
6396 : : }
6397 : : /* TS 29113, C535b. */
6398 : 1296422 : else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6399 : 35996 : && sym->ts.u.derived && CLASS_DATA (sym)
6400 : 35991 : && CLASS_DATA (sym)->as
6401 : 13761 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6402 : 1295512 : || (sym->ts.type != BT_CLASS && sym->as
6403 : 354296 : && sym->as->type == AS_ASSUMED_RANK))
6404 : 7888 : && !sym->attr.select_rank_temporary
6405 : 7888 : && !(sym->assoc && sym->assoc->ar))
6406 : : {
6407 : 7888 : if (!actual_arg
6408 : 1247 : && !(cs_base && cs_base->current
6409 : 1246 : && (cs_base->current->op == EXEC_SELECT_RANK
6410 : 188 : || sym->attr.target)))
6411 : : {
6412 : 144 : gfc_error ("Assumed-rank variable %s at %L may only be used as "
6413 : : "actual argument", sym->name, &e->where);
6414 : 144 : return false;
6415 : : }
6416 : 7744 : else if (inquiry_argument && !first_actual_arg)
6417 : : {
6418 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6419 : : for all inquiry functions in resolve_function; the reason is
6420 : : that the function-name resolution happens too late in that
6421 : : function. */
6422 : 0 : gfc_error ("Assumed-rank variable %s at %L as actual argument "
6423 : : "to an inquiry function shall be the first argument",
6424 : : sym->name, &e->where);
6425 : 0 : return false;
6426 : : }
6427 : : }
6428 : :
6429 : 1297010 : if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6430 : 181 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6431 : 180 : && e->ref->next == NULL))
6432 : : {
6433 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6434 : : "a subobject reference", sym->name, &e->ref->u.ar.where);
6435 : 1 : return false;
6436 : : }
6437 : : /* TS 29113, 407b. */
6438 : 1297009 : else if (e->ts.type == BT_ASSUMED && e->ref
6439 : 687 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6440 : 680 : && e->ref->next == NULL))
6441 : : {
6442 : 7 : gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6443 : : "reference", sym->name, &e->ref->u.ar.where);
6444 : 7 : return false;
6445 : : }
6446 : :
6447 : : /* TS 29113, C535b. */
6448 : 1297002 : if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6449 : 35996 : && sym->ts.u.derived && CLASS_DATA (sym)
6450 : 35991 : && CLASS_DATA (sym)->as
6451 : 13761 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6452 : 1296092 : || (sym->ts.type != BT_CLASS && sym->as
6453 : 354832 : && sym->as->type == AS_ASSUMED_RANK))
6454 : 8028 : && !(sym->assoc && sym->assoc->ar)
6455 : 8028 : && e->ref
6456 : 8028 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6457 : 8024 : && e->ref->next == NULL))
6458 : : {
6459 : 4 : gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6460 : : "reference", sym->name, &e->ref->u.ar.where);
6461 : 4 : return false;
6462 : : }
6463 : :
6464 : : /* Guessed type variables are associate_names whose selector had not been
6465 : : parsed at the time that the construct was parsed. Now the namespace is
6466 : : being resolved, the TKR of the selector will be available for fixup of
6467 : : the associate_name. */
6468 : 1296998 : if (IS_INFERRED_TYPE (e) && e->ref)
6469 : : {
6470 : 378 : gfc_fixup_inferred_type_refs (e);
6471 : : /* KIND inquiry ref returns the kind of the target. */
6472 : 378 : if (e->expr_type == EXPR_CONSTANT)
6473 : : return true;
6474 : : }
6475 : 1296620 : else if (sym->attr.select_type_temporary
6476 : 8826 : && sym->ns->assoc_name_inferred)
6477 : 92 : gfc_fixup_inferred_type_refs (e);
6478 : :
6479 : : /* For variables that are used in an associate (target => object) where
6480 : : the object's basetype is array valued while the target is scalar,
6481 : : the ts' type of the component refs is still array valued, which
6482 : : can't be translated that way. */
6483 : 1296986 : if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6484 : 563 : && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6485 : 563 : && sym->assoc->target->ts.u.derived
6486 : 563 : && CLASS_DATA (sym->assoc->target)
6487 : 563 : && CLASS_DATA (sym->assoc->target)->as)
6488 : : {
6489 : : gfc_ref *ref = e->ref;
6490 : 657 : while (ref)
6491 : : {
6492 : 499 : switch (ref->type)
6493 : : {
6494 : 216 : case REF_COMPONENT:
6495 : 216 : ref->u.c.sym = sym->ts.u.derived;
6496 : : /* Stop the loop. */
6497 : 216 : ref = NULL;
6498 : 216 : break;
6499 : 283 : default:
6500 : 283 : ref = ref->next;
6501 : 283 : break;
6502 : : }
6503 : : }
6504 : : }
6505 : :
6506 : : /* If this is an associate-name, it may be parsed with an array reference
6507 : : in error even though the target is scalar. Fail directly in this case.
6508 : : TODO Understand why class scalar expressions must be excluded. */
6509 : 1296986 : if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6510 : : {
6511 : 10894 : if (sym->ts.type == BT_CLASS)
6512 : 242 : gfc_fix_class_refs (e);
6513 : 10894 : if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6514 : 2025 : && e->ref->type == REF_ARRAY)
6515 : : {
6516 : : /* Unambiguously scalar! */
6517 : 3 : if (sym->assoc->target
6518 : 3 : && (sym->assoc->target->expr_type == EXPR_CONSTANT
6519 : 1 : || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6520 : 2 : gfc_error ("Scalar variable %qs has an array reference at %L",
6521 : : sym->name, &e->where);
6522 : 3 : return false;
6523 : : }
6524 : 10891 : else if ((sym->attr.dimension || sym->attr.codimension)
6525 : 6797 : && (!e->ref || e->ref->type != REF_ARRAY))
6526 : : {
6527 : : /* This can happen because the parser did not detect that the
6528 : : associate name is an array and the expression had no array
6529 : : part_ref. */
6530 : 140 : gfc_ref *ref = gfc_get_ref ();
6531 : 140 : ref->type = REF_ARRAY;
6532 : 140 : ref->u.ar.type = AR_FULL;
6533 : 140 : if (sym->as)
6534 : : {
6535 : 139 : ref->u.ar.as = sym->as;
6536 : 139 : ref->u.ar.dimen = sym->as->rank;
6537 : : }
6538 : 140 : ref->next = e->ref;
6539 : 140 : e->ref = ref;
6540 : : }
6541 : : }
6542 : :
6543 : 1296983 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6544 : 0 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6545 : :
6546 : : /* On the other hand, the parser may not have known this is an array;
6547 : : in this case, we have to add a FULL reference. */
6548 : 1296983 : if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6549 : : {
6550 : 0 : e->ref = gfc_get_ref ();
6551 : 0 : e->ref->type = REF_ARRAY;
6552 : 0 : e->ref->u.ar.type = AR_FULL;
6553 : 0 : e->ref->u.ar.dimen = 0;
6554 : : }
6555 : :
6556 : : /* Like above, but for class types, where the checking whether an array
6557 : : ref is present is more complicated. Furthermore make sure not to add
6558 : : the full array ref to _vptr or _len refs. */
6559 : 1296983 : if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6560 : 968 : && CLASS_DATA (sym)
6561 : 968 : && (CLASS_DATA (sym)->attr.dimension
6562 : 417 : || CLASS_DATA (sym)->attr.codimension)
6563 : 555 : && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6564 : : {
6565 : 531 : gfc_ref *ref, *newref;
6566 : :
6567 : 531 : newref = gfc_get_ref ();
6568 : 531 : newref->type = REF_ARRAY;
6569 : 531 : newref->u.ar.type = AR_FULL;
6570 : 531 : newref->u.ar.dimen = 0;
6571 : :
6572 : : /* Because this is an associate var and the first ref either is a ref to
6573 : : the _data component or not, no traversal of the ref chain is
6574 : : needed. The array ref needs to be inserted after the _data ref,
6575 : : or when that is not present, which may happened for polymorphic
6576 : : types, then at the first position. */
6577 : 531 : ref = e->ref;
6578 : 531 : if (!ref)
6579 : 18 : e->ref = newref;
6580 : 513 : else if (ref->type == REF_COMPONENT
6581 : 230 : && strcmp ("_data", ref->u.c.component->name) == 0)
6582 : : {
6583 : 230 : if (!ref->next || ref->next->type != REF_ARRAY)
6584 : : {
6585 : 12 : newref->next = ref->next;
6586 : 12 : ref->next = newref;
6587 : : }
6588 : : else
6589 : : /* Array ref present already. */
6590 : 218 : gfc_free_ref_list (newref);
6591 : : }
6592 : 283 : else if (ref->type == REF_ARRAY)
6593 : : /* Array ref present already. */
6594 : 283 : gfc_free_ref_list (newref);
6595 : : else
6596 : : {
6597 : 0 : newref->next = ref;
6598 : 0 : e->ref = newref;
6599 : : }
6600 : : }
6601 : 1296452 : else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6602 : : {
6603 : 485 : gfc_ref *ref;
6604 : 908 : for (ref = e->ref; ref; ref = ref->next)
6605 : 453 : if (ref->type == REF_SUBSTRING)
6606 : : break;
6607 : 485 : if (ref == NULL)
6608 : 455 : e->ts = sym->ts;
6609 : : }
6610 : :
6611 : 1296983 : if (e->ref && !gfc_resolve_ref (e))
6612 : : return false;
6613 : :
6614 : 1296897 : if (sym->attr.flavor == FL_PROCEDURE
6615 : 29882 : && (!sym->attr.function
6616 : 17446 : || (sym->attr.function && sym->result
6617 : 16998 : && sym->result->attr.proc_pointer
6618 : 562 : && !sym->result->attr.function)))
6619 : : {
6620 : 12436 : e->ts.type = BT_PROCEDURE;
6621 : 12436 : goto resolve_procedure;
6622 : : }
6623 : :
6624 : 1284461 : if (sym->ts.type != BT_UNKNOWN)
6625 : 1283822 : gfc_variable_attr (e, &e->ts);
6626 : 639 : else if (sym->attr.flavor == FL_PROCEDURE
6627 : 12 : && sym->attr.function && sym->result
6628 : 12 : && sym->result->ts.type != BT_UNKNOWN
6629 : 10 : && sym->result->attr.proc_pointer)
6630 : 10 : e->ts = sym->result->ts;
6631 : : else
6632 : : {
6633 : : /* Must be a simple variable reference. */
6634 : 629 : if (!gfc_set_default_type (sym, 1, sym->ns))
6635 : : return false;
6636 : 503 : e->ts = sym->ts;
6637 : : }
6638 : :
6639 : 1284335 : if (check_assumed_size_reference (sym, e))
6640 : : return false;
6641 : :
6642 : : /* Deal with forward references to entries during gfc_resolve_code, to
6643 : : satisfy, at least partially, 12.5.2.5. */
6644 : 1284316 : if (gfc_current_ns->entries
6645 : 3060 : && current_entry_id == sym->entry_id
6646 : 1000 : && cs_base
6647 : 914 : && cs_base->current
6648 : 914 : && cs_base->current->op != EXEC_ENTRY)
6649 : : {
6650 : 914 : gfc_entry_list *entry;
6651 : 914 : gfc_formal_arglist *formal;
6652 : 914 : int n;
6653 : 914 : bool seen, saved_specification_expr;
6654 : :
6655 : : /* If the symbol is a dummy... */
6656 : 914 : if (sym->attr.dummy && sym->ns == gfc_current_ns)
6657 : : {
6658 : : entry = gfc_current_ns->entries;
6659 : : seen = false;
6660 : :
6661 : : /* ...test if the symbol is a parameter of previous entries. */
6662 : 1033 : for (; entry && entry->id <= current_entry_id; entry = entry->next)
6663 : 1006 : for (formal = entry->sym->formal; formal; formal = formal->next)
6664 : : {
6665 : 997 : if (formal->sym && sym->name == formal->sym->name)
6666 : : {
6667 : : seen = true;
6668 : : break;
6669 : : }
6670 : : }
6671 : :
6672 : : /* If it has not been seen as a dummy, this is an error. */
6673 : 453 : if (!seen)
6674 : : {
6675 : 3 : if (specification_expr)
6676 : 2 : gfc_error ("Variable %qs, used in a specification expression"
6677 : : ", is referenced at %L before the ENTRY statement "
6678 : : "in which it is a parameter",
6679 : : sym->name, &cs_base->current->loc);
6680 : : else
6681 : 1 : gfc_error ("Variable %qs is used at %L before the ENTRY "
6682 : : "statement in which it is a parameter",
6683 : : sym->name, &cs_base->current->loc);
6684 : : t = false;
6685 : : }
6686 : : }
6687 : :
6688 : : /* Now do the same check on the specification expressions. */
6689 : 914 : saved_specification_expr = specification_expr;
6690 : 914 : specification_expr = true;
6691 : 914 : if (sym->ts.type == BT_CHARACTER
6692 : 914 : && !gfc_resolve_expr (sym->ts.u.cl->length))
6693 : : t = false;
6694 : :
6695 : 914 : if (sym->as)
6696 : : {
6697 : 271 : for (n = 0; n < sym->as->rank; n++)
6698 : : {
6699 : 159 : if (!gfc_resolve_expr (sym->as->lower[n]))
6700 : 0 : t = false;
6701 : 159 : if (!gfc_resolve_expr (sym->as->upper[n]))
6702 : 1 : t = false;
6703 : : }
6704 : : }
6705 : 914 : specification_expr = saved_specification_expr;
6706 : :
6707 : 914 : if (t)
6708 : : /* Update the symbol's entry level. */
6709 : 909 : sym->entry_id = current_entry_id + 1;
6710 : : }
6711 : :
6712 : : /* If a symbol has been host_associated mark it. This is used latter,
6713 : : to identify if aliasing is possible via host association. */
6714 : 1284316 : if (sym->attr.flavor == FL_VARIABLE
6715 : 1247810 : && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6716 : 5362 : || !sym->ns->code->ext.block.assoc)
6717 : 1246300 : && gfc_current_ns->parent
6718 : 594404 : && (gfc_current_ns->parent == sym->ns
6719 : 557258 : || (gfc_current_ns->parent->parent
6720 : 10681 : && gfc_current_ns->parent->parent == sym->ns)))
6721 : 43511 : sym->attr.host_assoc = 1;
6722 : :
6723 : 1284316 : if (gfc_current_ns->proc_name
6724 : 1280922 : && sym->attr.dimension
6725 : 349446 : && (sym->ns != gfc_current_ns
6726 : 325844 : || sym->attr.use_assoc
6727 : 321893 : || sym->attr.in_common))
6728 : 32341 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6729 : :
6730 : 1296752 : resolve_procedure:
6731 : 1296752 : if (t && !resolve_procedure_expression (e))
6732 : : t = false;
6733 : :
6734 : : /* F2008, C617 and C1229. */
6735 : 1295725 : if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6736 : 1390324 : && gfc_is_coindexed (e))
6737 : : {
6738 : 304 : gfc_ref *ref, *ref2 = NULL;
6739 : :
6740 : 383 : for (ref = e->ref; ref; ref = ref->next)
6741 : : {
6742 : 383 : if (ref->type == REF_COMPONENT)
6743 : 79 : ref2 = ref;
6744 : 383 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6745 : : break;
6746 : : }
6747 : :
6748 : 608 : for ( ; ref; ref = ref->next)
6749 : 316 : if (ref->type == REF_COMPONENT)
6750 : : break;
6751 : :
6752 : : /* Expression itself is not coindexed object. */
6753 : 304 : if (ref && e->ts.type == BT_CLASS)
6754 : : {
6755 : 3 : gfc_error ("Polymorphic subobject of coindexed object at %L",
6756 : : &e->where);
6757 : 3 : t = false;
6758 : : }
6759 : :
6760 : : /* Expression itself is coindexed object. */
6761 : 292 : if (ref == NULL)
6762 : : {
6763 : 292 : gfc_component *c;
6764 : 292 : c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6765 : 408 : for ( ; c; c = c->next)
6766 : 116 : if (c->attr.allocatable && c->ts.type == BT_CLASS)
6767 : : {
6768 : 0 : gfc_error ("Coindexed object with polymorphic allocatable "
6769 : : "subcomponent at %L", &e->where);
6770 : 0 : t = false;
6771 : 0 : break;
6772 : : }
6773 : : }
6774 : : }
6775 : :
6776 : 1296752 : if (t)
6777 : 1296744 : gfc_expression_rank (e);
6778 : :
6779 : 1296752 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6780 : 3 : gfc_warning (OPT_Wdeprecated_declarations,
6781 : : "Using variable %qs at %L is deprecated",
6782 : : sym->name, &e->where);
6783 : : /* Simplify cases where access to a parameter array results in a
6784 : : single constant. Suppress errors since those will have been
6785 : : issued before, as warnings. */
6786 : 1296752 : if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6787 : : {
6788 : 2665 : gfc_push_suppress_errors ();
6789 : 2665 : gfc_simplify_expr (e, 1);
6790 : 2665 : gfc_pop_suppress_errors ();
6791 : : }
6792 : :
6793 : : return t;
6794 : : }
6795 : :
6796 : :
6797 : : /* 'sym' was initially guessed to be derived type but has been corrected
6798 : : in resolve_assoc_var to be a class entity or the derived type correcting.
6799 : : If a class entity it will certainly need the _data reference or the
6800 : : reference derived type symbol correcting in the first component ref if
6801 : : a derived type. */
6802 : :
6803 : : void
6804 : 856 : gfc_fixup_inferred_type_refs (gfc_expr *e)
6805 : : {
6806 : 856 : gfc_ref *ref, *new_ref;
6807 : 856 : gfc_symbol *sym, *derived;
6808 : 856 : gfc_expr *target;
6809 : 856 : sym = e->symtree->n.sym;
6810 : :
6811 : : /* An associate_name whose selector is (i) a component ref of a selector
6812 : : that is a inferred type associate_name; or (ii) an intrinsic type that
6813 : : has been inferred from an inquiry ref. */
6814 : 856 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6815 : : {
6816 : 282 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6817 : 282 : sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6818 : 282 : if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6819 : : {
6820 : 60 : ref = e->ref;
6821 : : /* A substring misidentified as an array section. */
6822 : 60 : if (sym->ts.type == BT_CHARACTER
6823 : 30 : && ref->u.ar.start[0] && ref->u.ar.end[0]
6824 : 6 : && !ref->u.ar.stride[0])
6825 : : {
6826 : 6 : new_ref = gfc_get_ref ();
6827 : 6 : new_ref->type = REF_SUBSTRING;
6828 : 6 : new_ref->u.ss.start = ref->u.ar.start[0];
6829 : 6 : new_ref->u.ss.end = ref->u.ar.end[0];
6830 : 6 : new_ref->u.ss.length = sym->ts.u.cl;
6831 : 6 : *ref = *new_ref;
6832 : 6 : free (new_ref);
6833 : : }
6834 : : else
6835 : : {
6836 : 54 : if (e->ref->u.ar.type == AR_UNKNOWN)
6837 : 24 : gfc_error ("Invalid array reference at %L", &e->where);
6838 : 54 : e->ref = ref->next;
6839 : 54 : free (ref);
6840 : : }
6841 : : }
6842 : :
6843 : : /* It is possible for an inquiry reference to be mistaken for a
6844 : : component reference. Correct this now. */
6845 : 282 : ref = e->ref;
6846 : 282 : if (ref && ref->type == REF_ARRAY)
6847 : 138 : ref = ref->next;
6848 : 150 : if (ref && ref->type == REF_COMPONENT
6849 : 150 : && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6850 : : {
6851 : 12 : e->symtree->n.sym = sym;
6852 : 12 : *ref = *new_ref;
6853 : 12 : gfc_free_ref_list (new_ref);
6854 : : }
6855 : :
6856 : : /* The kind of the associate name is best evaluated directly from the
6857 : : selector because of the guesses made in primary.cc, when the type
6858 : : is still unknown. */
6859 : 282 : if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6860 : : {
6861 : 24 : gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6862 : 12 : sym->assoc->target->ts.kind);
6863 : 12 : gfc_replace_expr (e, ne);
6864 : : }
6865 : :
6866 : : /* Now that the references are all sorted out, set the expression rank
6867 : : and return. */
6868 : 282 : gfc_expression_rank (e);
6869 : 282 : return;
6870 : : }
6871 : :
6872 : 574 : derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6873 : : : sym->ts.u.derived;
6874 : :
6875 : : /* Ensure that class symbols have an array spec and ensure that there
6876 : : is a _data field reference following class type references. */
6877 : 574 : if (sym->ts.type == BT_CLASS
6878 : 196 : && sym->assoc->target->ts.type == BT_CLASS)
6879 : : {
6880 : 196 : e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6881 : 196 : e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6882 : 196 : sym->attr.dimension = 0;
6883 : 196 : sym->attr.codimension = 0;
6884 : 196 : CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6885 : 196 : CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6886 : 196 : if (e->ref && (e->ref->type != REF_COMPONENT
6887 : 160 : || e->ref->u.c.component->name[0] != '_'))
6888 : : {
6889 : 82 : ref = gfc_get_ref ();
6890 : 82 : ref->type = REF_COMPONENT;
6891 : 82 : ref->next = e->ref;
6892 : 82 : e->ref = ref;
6893 : 82 : ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6894 : : true, true, NULL);
6895 : 82 : ref->u.c.sym = sym->ts.u.derived;
6896 : : }
6897 : : }
6898 : :
6899 : : /* Proceed as far as the first component reference and ensure that the
6900 : : correct derived type is being used. */
6901 : 837 : for (ref = e->ref; ref; ref = ref->next)
6902 : 801 : if (ref->type == REF_COMPONENT)
6903 : : {
6904 : 538 : if (ref->u.c.component->name[0] != '_')
6905 : 342 : ref->u.c.sym = derived;
6906 : : else
6907 : 196 : ref->u.c.sym = sym->ts.u.derived;
6908 : : break;
6909 : : }
6910 : :
6911 : : /* Verify that the type inferrence mechanism has not introduced a spurious
6912 : : array reference. This can happen with an associate name, whose selector
6913 : : is an element of another inferred type. */
6914 : 574 : target = e->symtree->n.sym->assoc->target;
6915 : 574 : if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6916 : 162 : && e != target && !target->rank)
6917 : : {
6918 : : /* First case: array ref after the scalar class or derived
6919 : : associate_name. */
6920 : 162 : if (e->ref && e->ref->type == REF_ARRAY
6921 : 7 : && e->ref->u.ar.type != AR_ELEMENT)
6922 : : {
6923 : 7 : ref = e->ref;
6924 : 7 : if (ref->u.ar.type == AR_UNKNOWN)
6925 : 1 : gfc_error ("Invalid array reference at %L", &e->where);
6926 : 7 : e->ref = ref->next;
6927 : 7 : free (ref);
6928 : :
6929 : : /* If it hasn't a ref to the '_data' field supply one. */
6930 : 7 : if (sym->ts.type == BT_CLASS
6931 : 0 : && !(e->ref->type == REF_COMPONENT
6932 : 0 : && strcmp (e->ref->u.c.component->name, "_data")))
6933 : : {
6934 : 0 : gfc_ref *new_ref;
6935 : 0 : gfc_find_component (e->symtree->n.sym->ts.u.derived,
6936 : : "_data", true, true, &new_ref);
6937 : 0 : new_ref->next = e->ref;
6938 : 0 : e->ref = new_ref;
6939 : : }
6940 : : }
6941 : : /* 2nd case: a ref to the '_data' field followed by an array ref. */
6942 : 155 : else if (e->ref && e->ref->type == REF_COMPONENT
6943 : 155 : && strcmp (e->ref->u.c.component->name, "_data") == 0
6944 : 64 : && e->ref->next && e->ref->next->type == REF_ARRAY
6945 : 0 : && e->ref->next->u.ar.type != AR_ELEMENT)
6946 : : {
6947 : 0 : ref = e->ref->next;
6948 : 0 : if (ref->u.ar.type == AR_UNKNOWN)
6949 : 0 : gfc_error ("Invalid array reference at %L", &e->where);
6950 : 0 : e->ref->next = e->ref->next->next;
6951 : 0 : free (ref);
6952 : : }
6953 : : }
6954 : :
6955 : : /* Now that all the references are OK, get the expression rank. */
6956 : 574 : gfc_expression_rank (e);
6957 : : }
6958 : :
6959 : :
6960 : : /* Checks to see that the correct symbol has been host associated.
6961 : : The only situations where this arises are:
6962 : : (i) That in which a twice contained function is parsed after
6963 : : the host association is made. On detecting this, change
6964 : : the symbol in the expression and convert the array reference
6965 : : into an actual arglist if the old symbol is a variable; or
6966 : : (ii) That in which an external function is typed but not declared
6967 : : explicitly to be external. Here, the old symbol is changed
6968 : : from a variable to an external function. */
6969 : : static bool
6970 : 1634531 : check_host_association (gfc_expr *e)
6971 : : {
6972 : 1634531 : gfc_symbol *sym, *old_sym;
6973 : 1634531 : gfc_symtree *st;
6974 : 1634531 : int n;
6975 : 1634531 : gfc_ref *ref;
6976 : 1634531 : gfc_actual_arglist *arg, *tail = NULL;
6977 : 1634531 : bool retval = e->expr_type == EXPR_FUNCTION;
6978 : :
6979 : : /* If the expression is the result of substitution in
6980 : : interface.cc(gfc_extend_expr) because there is no way in
6981 : : which the host association can be wrong. */
6982 : 1634531 : if (e->symtree == NULL
6983 : 1633783 : || e->symtree->n.sym == NULL
6984 : 1633783 : || e->user_operator)
6985 : : return retval;
6986 : :
6987 : 1632012 : old_sym = e->symtree->n.sym;
6988 : :
6989 : 1632012 : if (gfc_current_ns->parent
6990 : 715888 : && old_sym->ns != gfc_current_ns)
6991 : : {
6992 : : /* Use the 'USE' name so that renamed module symbols are
6993 : : correctly handled. */
6994 : 87799 : gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6995 : :
6996 : 87799 : if (sym && old_sym != sym
6997 : 500 : && sym->attr.flavor == FL_PROCEDURE
6998 : 105 : && sym->attr.contained)
6999 : : {
7000 : : /* Clear the shape, since it might not be valid. */
7001 : 83 : gfc_free_shape (&e->shape, e->rank);
7002 : :
7003 : : /* Give the expression the right symtree! */
7004 : 83 : gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
7005 : 83 : gcc_assert (st != NULL);
7006 : :
7007 : 83 : if (old_sym->attr.flavor == FL_PROCEDURE
7008 : 59 : || e->expr_type == EXPR_FUNCTION)
7009 : : {
7010 : : /* Original was function so point to the new symbol, since
7011 : : the actual argument list is already attached to the
7012 : : expression. */
7013 : 30 : e->value.function.esym = NULL;
7014 : 30 : e->symtree = st;
7015 : : }
7016 : : else
7017 : : {
7018 : : /* Original was variable so convert array references into
7019 : : an actual arglist. This does not need any checking now
7020 : : since resolve_function will take care of it. */
7021 : 53 : e->value.function.actual = NULL;
7022 : 53 : e->expr_type = EXPR_FUNCTION;
7023 : 53 : e->symtree = st;
7024 : :
7025 : : /* Ambiguity will not arise if the array reference is not
7026 : : the last reference. */
7027 : 55 : for (ref = e->ref; ref; ref = ref->next)
7028 : 38 : if (ref->type == REF_ARRAY && ref->next == NULL)
7029 : : break;
7030 : :
7031 : 53 : if ((ref == NULL || ref->type != REF_ARRAY)
7032 : 17 : && sym->attr.proc == PROC_INTERNAL)
7033 : : {
7034 : 4 : gfc_error ("%qs at %L is host associated at %L into "
7035 : : "a contained procedure with an internal "
7036 : : "procedure of the same name", sym->name,
7037 : : &old_sym->declared_at, &e->where);
7038 : 4 : return false;
7039 : : }
7040 : :
7041 : 13 : if (ref == NULL)
7042 : : return false;
7043 : :
7044 : 36 : gcc_assert (ref->type == REF_ARRAY);
7045 : :
7046 : : /* Grab the start expressions from the array ref and
7047 : : copy them into actual arguments. */
7048 : 84 : for (n = 0; n < ref->u.ar.dimen; n++)
7049 : : {
7050 : 48 : arg = gfc_get_actual_arglist ();
7051 : 48 : arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
7052 : 48 : if (e->value.function.actual == NULL)
7053 : 36 : tail = e->value.function.actual = arg;
7054 : : else
7055 : : {
7056 : 12 : tail->next = arg;
7057 : 12 : tail = arg;
7058 : : }
7059 : : }
7060 : :
7061 : : /* Dump the reference list and set the rank. */
7062 : 36 : gfc_free_ref_list (e->ref);
7063 : 36 : e->ref = NULL;
7064 : 36 : e->rank = sym->as ? sym->as->rank : 0;
7065 : 36 : e->corank = sym->as ? sym->as->corank : 0;
7066 : : }
7067 : :
7068 : 66 : gfc_resolve_expr (e);
7069 : 66 : sym->refs++;
7070 : : }
7071 : : /* This case corresponds to a call, from a block or a contained
7072 : : procedure, to an external function, which has not been declared
7073 : : as being external in the main program but has been typed. */
7074 : 87716 : else if (sym && old_sym != sym
7075 : 417 : && !e->ref
7076 : 255 : && sym->ts.type == BT_UNKNOWN
7077 : 21 : && old_sym->ts.type != BT_UNKNOWN
7078 : 19 : && sym->attr.flavor == FL_PROCEDURE
7079 : 19 : && old_sym->attr.flavor == FL_VARIABLE
7080 : 7 : && sym->ns->parent == old_sym->ns
7081 : 7 : && sym->ns->proc_name
7082 : 7 : && sym->ns->proc_name->attr.proc != PROC_MODULE
7083 : 6 : && (sym->ns->proc_name->attr.flavor == FL_LABEL
7084 : 6 : || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
7085 : : {
7086 : 6 : old_sym->attr.flavor = FL_PROCEDURE;
7087 : 6 : old_sym->attr.external = 1;
7088 : 6 : old_sym->attr.function = 1;
7089 : 6 : old_sym->result = old_sym;
7090 : 6 : gfc_resolve_expr (e);
7091 : : }
7092 : : }
7093 : : /* This might have changed! */
7094 : 1631995 : return e->expr_type == EXPR_FUNCTION;
7095 : : }
7096 : :
7097 : :
7098 : : static void
7099 : 1328 : gfc_resolve_character_operator (gfc_expr *e)
7100 : : {
7101 : 1328 : gfc_expr *op1 = e->value.op.op1;
7102 : 1328 : gfc_expr *op2 = e->value.op.op2;
7103 : 1328 : gfc_expr *e1 = NULL;
7104 : 1328 : gfc_expr *e2 = NULL;
7105 : :
7106 : 1328 : gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
7107 : :
7108 : 1328 : if (op1->ts.u.cl && op1->ts.u.cl->length)
7109 : 653 : e1 = gfc_copy_expr (op1->ts.u.cl->length);
7110 : 675 : else if (op1->expr_type == EXPR_CONSTANT)
7111 : 268 : e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7112 : 268 : op1->value.character.length);
7113 : :
7114 : 1328 : if (op2->ts.u.cl && op2->ts.u.cl->length)
7115 : 641 : e2 = gfc_copy_expr (op2->ts.u.cl->length);
7116 : 687 : else if (op2->expr_type == EXPR_CONSTANT)
7117 : 457 : e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7118 : 457 : op2->value.character.length);
7119 : :
7120 : 1328 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7121 : :
7122 : 1328 : if (!e1 || !e2)
7123 : : {
7124 : 535 : gfc_free_expr (e1);
7125 : 535 : gfc_free_expr (e2);
7126 : :
7127 : 535 : return;
7128 : : }
7129 : :
7130 : 793 : e->ts.u.cl->length = gfc_add (e1, e2);
7131 : 793 : e->ts.u.cl->length->ts.type = BT_INTEGER;
7132 : 793 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
7133 : 793 : gfc_simplify_expr (e->ts.u.cl->length, 0);
7134 : 793 : gfc_resolve_expr (e->ts.u.cl->length);
7135 : :
7136 : 793 : return;
7137 : : }
7138 : :
7139 : :
7140 : : /* Ensure that an character expression has a charlen and, if possible, a
7141 : : length expression. */
7142 : :
7143 : : static void
7144 : 176839 : fixup_charlen (gfc_expr *e)
7145 : : {
7146 : : /* The cases fall through so that changes in expression type and the need
7147 : : for multiple fixes are picked up. In all circumstances, a charlen should
7148 : : be available for the middle end to hang a backend_decl on. */
7149 : 176839 : switch (e->expr_type)
7150 : : {
7151 : 1328 : case EXPR_OP:
7152 : 1328 : gfc_resolve_character_operator (e);
7153 : : /* FALLTHRU */
7154 : :
7155 : 1383 : case EXPR_ARRAY:
7156 : 1383 : if (e->expr_type == EXPR_ARRAY)
7157 : 55 : gfc_resolve_character_array_constructor (e);
7158 : : /* FALLTHRU */
7159 : :
7160 : 1839 : case EXPR_SUBSTRING:
7161 : 1839 : if (!e->ts.u.cl && e->ref)
7162 : 452 : gfc_resolve_substring_charlen (e);
7163 : : /* FALLTHRU */
7164 : :
7165 : 176839 : default:
7166 : 176839 : if (!e->ts.u.cl)
7167 : 175004 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7168 : :
7169 : 176839 : break;
7170 : : }
7171 : 176839 : }
7172 : :
7173 : :
7174 : : /* Update an actual argument to include the passed-object for type-bound
7175 : : procedures at the right position. */
7176 : :
7177 : : static gfc_actual_arglist*
7178 : 2895 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
7179 : : const char *name)
7180 : : {
7181 : 2919 : gcc_assert (argpos > 0);
7182 : :
7183 : 2919 : if (argpos == 1)
7184 : : {
7185 : 2770 : gfc_actual_arglist* result;
7186 : :
7187 : 2770 : result = gfc_get_actual_arglist ();
7188 : 2770 : result->expr = po;
7189 : 2770 : result->next = lst;
7190 : 2770 : if (name)
7191 : 514 : result->name = name;
7192 : :
7193 : 2770 : return result;
7194 : : }
7195 : :
7196 : 149 : if (lst)
7197 : 125 : lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
7198 : : else
7199 : 24 : lst = update_arglist_pass (NULL, po, argpos - 1, name);
7200 : : return lst;
7201 : : }
7202 : :
7203 : :
7204 : : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
7205 : :
7206 : : static gfc_expr*
7207 : 7006 : extract_compcall_passed_object (gfc_expr* e)
7208 : : {
7209 : 7006 : gfc_expr* po;
7210 : :
7211 : 7006 : if (e->expr_type == EXPR_UNKNOWN)
7212 : : {
7213 : 0 : gfc_error ("Error in typebound call at %L",
7214 : : &e->where);
7215 : 0 : return NULL;
7216 : : }
7217 : :
7218 : 7006 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7219 : :
7220 : 7006 : if (e->value.compcall.base_object)
7221 : 1506 : po = gfc_copy_expr (e->value.compcall.base_object);
7222 : : else
7223 : : {
7224 : 5500 : po = gfc_get_expr ();
7225 : 5500 : po->expr_type = EXPR_VARIABLE;
7226 : 5500 : po->symtree = e->symtree;
7227 : 5500 : po->ref = gfc_copy_ref (e->ref);
7228 : 5500 : po->where = e->where;
7229 : : }
7230 : :
7231 : 7006 : if (!gfc_resolve_expr (po))
7232 : : return NULL;
7233 : :
7234 : : return po;
7235 : : }
7236 : :
7237 : :
7238 : : /* Update the arglist of an EXPR_COMPCALL expression to include the
7239 : : passed-object. */
7240 : :
7241 : : static bool
7242 : 3231 : update_compcall_arglist (gfc_expr* e)
7243 : : {
7244 : 3231 : gfc_expr* po;
7245 : 3231 : gfc_typebound_proc* tbp;
7246 : :
7247 : 3231 : tbp = e->value.compcall.tbp;
7248 : :
7249 : 3231 : if (tbp->error)
7250 : : return false;
7251 : :
7252 : 3230 : po = extract_compcall_passed_object (e);
7253 : 3230 : if (!po)
7254 : : return false;
7255 : :
7256 : 3230 : if (tbp->nopass || e->value.compcall.ignore_pass)
7257 : : {
7258 : 1077 : gfc_free_expr (po);
7259 : 1077 : return true;
7260 : : }
7261 : :
7262 : 2153 : if (tbp->pass_arg_num <= 0)
7263 : : return false;
7264 : :
7265 : 2152 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7266 : : tbp->pass_arg_num,
7267 : : tbp->pass_arg);
7268 : :
7269 : 2152 : return true;
7270 : : }
7271 : :
7272 : :
7273 : : /* Extract the passed object from a PPC call (a copy of it). */
7274 : :
7275 : : static gfc_expr*
7276 : 85 : extract_ppc_passed_object (gfc_expr *e)
7277 : : {
7278 : 85 : gfc_expr *po;
7279 : 85 : gfc_ref **ref;
7280 : :
7281 : 85 : po = gfc_get_expr ();
7282 : 85 : po->expr_type = EXPR_VARIABLE;
7283 : 85 : po->symtree = e->symtree;
7284 : 85 : po->ref = gfc_copy_ref (e->ref);
7285 : 85 : po->where = e->where;
7286 : :
7287 : : /* Remove PPC reference. */
7288 : 85 : ref = &po->ref;
7289 : 91 : while ((*ref)->next)
7290 : 6 : ref = &(*ref)->next;
7291 : 85 : gfc_free_ref_list (*ref);
7292 : 85 : *ref = NULL;
7293 : :
7294 : 85 : if (!gfc_resolve_expr (po))
7295 : 0 : return NULL;
7296 : :
7297 : : return po;
7298 : : }
7299 : :
7300 : :
7301 : : /* Update the actual arglist of a procedure pointer component to include the
7302 : : passed-object. */
7303 : :
7304 : : static bool
7305 : 411 : update_ppc_arglist (gfc_expr* e)
7306 : : {
7307 : 411 : gfc_expr* po;
7308 : 411 : gfc_component *ppc;
7309 : 411 : gfc_typebound_proc* tb;
7310 : :
7311 : 411 : ppc = gfc_get_proc_ptr_comp (e);
7312 : 411 : if (!ppc)
7313 : : return false;
7314 : :
7315 : 411 : tb = ppc->tb;
7316 : :
7317 : 411 : if (tb->error)
7318 : : return false;
7319 : 409 : else if (tb->nopass)
7320 : : return true;
7321 : :
7322 : 85 : po = extract_ppc_passed_object (e);
7323 : 85 : if (!po)
7324 : : return false;
7325 : :
7326 : : /* F08:R739. */
7327 : 85 : if (po->rank != 0)
7328 : : {
7329 : 0 : gfc_error ("Passed-object at %L must be scalar", &e->where);
7330 : 0 : return false;
7331 : : }
7332 : :
7333 : : /* F08:C611. */
7334 : 85 : if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7335 : : {
7336 : 1 : gfc_error ("Base object for procedure-pointer component call at %L is of"
7337 : : " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7338 : 1 : return false;
7339 : : }
7340 : :
7341 : 84 : gcc_assert (tb->pass_arg_num > 0);
7342 : 84 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7343 : : tb->pass_arg_num,
7344 : : tb->pass_arg);
7345 : :
7346 : 84 : return true;
7347 : : }
7348 : :
7349 : :
7350 : : /* Check that the object a TBP is called on is valid, i.e. it must not be
7351 : : of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7352 : :
7353 : : static bool
7354 : 3242 : check_typebound_baseobject (gfc_expr* e)
7355 : : {
7356 : 3242 : gfc_expr* base;
7357 : 3242 : bool return_value = false;
7358 : :
7359 : 3242 : base = extract_compcall_passed_object (e);
7360 : 3242 : if (!base)
7361 : : return false;
7362 : :
7363 : 3239 : if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7364 : : {
7365 : 1 : gfc_error ("Error in typebound call at %L", &e->where);
7366 : 1 : goto cleanup;
7367 : : }
7368 : :
7369 : 3238 : if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7370 : 1 : return false;
7371 : :
7372 : : /* F08:C611. */
7373 : 3237 : if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7374 : : {
7375 : 3 : gfc_error ("Base object for type-bound procedure call at %L is of"
7376 : : " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7377 : 3 : goto cleanup;
7378 : : }
7379 : :
7380 : : /* F08:C1230. If the procedure called is NOPASS,
7381 : : the base object must be scalar. */
7382 : 3234 : if (e->value.compcall.tbp->nopass && base->rank != 0)
7383 : : {
7384 : 1 : gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7385 : : " be scalar", &e->where);
7386 : 1 : goto cleanup;
7387 : : }
7388 : :
7389 : : return_value = true;
7390 : :
7391 : 3238 : cleanup:
7392 : 3238 : gfc_free_expr (base);
7393 : 3238 : return return_value;
7394 : : }
7395 : :
7396 : :
7397 : : /* Resolve a call to a type-bound procedure, either function or subroutine,
7398 : : statically from the data in an EXPR_COMPCALL expression. The adapted
7399 : : arglist and the target-procedure symtree are returned. */
7400 : :
7401 : : static bool
7402 : 3231 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7403 : : gfc_actual_arglist** actual)
7404 : : {
7405 : 3231 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7406 : 3231 : gcc_assert (!e->value.compcall.tbp->is_generic);
7407 : :
7408 : : /* Update the actual arglist for PASS. */
7409 : 3231 : if (!update_compcall_arglist (e))
7410 : : return false;
7411 : :
7412 : 3229 : *actual = e->value.compcall.actual;
7413 : 3229 : *target = e->value.compcall.tbp->u.specific;
7414 : :
7415 : 3229 : gfc_free_ref_list (e->ref);
7416 : 3229 : e->ref = NULL;
7417 : 3229 : e->value.compcall.actual = NULL;
7418 : :
7419 : : /* If we find a deferred typebound procedure, check for derived types
7420 : : that an overriding typebound procedure has not been missed. */
7421 : 3229 : if (e->value.compcall.name
7422 : 3229 : && !e->value.compcall.tbp->non_overridable
7423 : 3213 : && e->value.compcall.base_object
7424 : 753 : && e->value.compcall.base_object->ts.type == BT_DERIVED)
7425 : : {
7426 : 466 : gfc_symtree *st;
7427 : 466 : gfc_symbol *derived;
7428 : :
7429 : : /* Use the derived type of the base_object. */
7430 : 466 : derived = e->value.compcall.base_object->ts.u.derived;
7431 : 466 : st = NULL;
7432 : :
7433 : : /* If necessary, go through the inheritance chain. */
7434 : 1399 : while (!st && derived)
7435 : : {
7436 : : /* Look for the typebound procedure 'name'. */
7437 : 467 : if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7438 : 466 : st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7439 : : e->value.compcall.name);
7440 : 467 : if (!st)
7441 : 1 : derived = gfc_get_derived_super_type (derived);
7442 : : }
7443 : :
7444 : : /* Now find the specific name in the derived type namespace. */
7445 : 466 : if (st && st->n.tb && st->n.tb->u.specific)
7446 : 466 : gfc_find_sym_tree (st->n.tb->u.specific->name,
7447 : 466 : derived->ns, 1, &st);
7448 : 466 : if (st)
7449 : 466 : *target = st;
7450 : : }
7451 : :
7452 : 3229 : if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7453 : 3229 : && !e->value.compcall.tbp->deferred)
7454 : 1 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7455 : : " itself recursively. Declare it RECURSIVE or use"
7456 : : " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7457 : :
7458 : : return true;
7459 : : }
7460 : :
7461 : :
7462 : : /* Get the ultimate declared type from an expression. In addition,
7463 : : return the last class/derived type reference and the copy of the
7464 : : reference list. If check_types is set true, derived types are
7465 : : identified as well as class references. */
7466 : : static gfc_symbol*
7467 : 3196 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7468 : : gfc_expr *e, bool check_types)
7469 : : {
7470 : 3196 : gfc_symbol *declared;
7471 : 3196 : gfc_ref *ref;
7472 : :
7473 : 3196 : declared = NULL;
7474 : 3196 : if (class_ref)
7475 : 2800 : *class_ref = NULL;
7476 : 3196 : if (new_ref)
7477 : 2513 : *new_ref = gfc_copy_ref (e->ref);
7478 : :
7479 : 3967 : for (ref = e->ref; ref; ref = ref->next)
7480 : : {
7481 : 771 : if (ref->type != REF_COMPONENT)
7482 : 283 : continue;
7483 : :
7484 : 488 : if ((ref->u.c.component->ts.type == BT_CLASS
7485 : 242 : || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7486 : 413 : && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7487 : : {
7488 : 339 : declared = ref->u.c.component->ts.u.derived;
7489 : 339 : if (class_ref)
7490 : 320 : *class_ref = ref;
7491 : : }
7492 : : }
7493 : :
7494 : 3196 : if (declared == NULL)
7495 : 2883 : declared = e->symtree->n.sym->ts.u.derived;
7496 : :
7497 : 3196 : return declared;
7498 : : }
7499 : :
7500 : :
7501 : : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7502 : : which of the specific bindings (if any) matches the arglist and transform
7503 : : the expression into a call of that binding. */
7504 : :
7505 : : static bool
7506 : 3233 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
7507 : : {
7508 : 3233 : gfc_typebound_proc* genproc;
7509 : 3233 : const char* genname;
7510 : 3233 : gfc_symtree *st;
7511 : 3233 : gfc_symbol *derived;
7512 : :
7513 : 3233 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7514 : 3233 : genname = e->value.compcall.name;
7515 : 3233 : genproc = e->value.compcall.tbp;
7516 : :
7517 : 3233 : if (!genproc->is_generic)
7518 : : return true;
7519 : :
7520 : : /* Try the bindings on this type and in the inheritance hierarchy. */
7521 : 408 : for (; genproc; genproc = genproc->overridden)
7522 : : {
7523 : 406 : gfc_tbp_generic* g;
7524 : :
7525 : 406 : gcc_assert (genproc->is_generic);
7526 : 630 : for (g = genproc->u.generic; g; g = g->next)
7527 : : {
7528 : 620 : gfc_symbol* target;
7529 : 620 : gfc_actual_arglist* args;
7530 : 620 : bool matches;
7531 : :
7532 : 620 : gcc_assert (g->specific);
7533 : :
7534 : 620 : if (g->specific->error)
7535 : 0 : continue;
7536 : :
7537 : 620 : target = g->specific->u.specific->n.sym;
7538 : :
7539 : : /* Get the right arglist by handling PASS/NOPASS. */
7540 : 620 : args = gfc_copy_actual_arglist (e->value.compcall.actual);
7541 : 620 : if (!g->specific->nopass)
7542 : : {
7543 : 534 : gfc_expr* po;
7544 : 534 : po = extract_compcall_passed_object (e);
7545 : 534 : if (!po)
7546 : : {
7547 : 0 : gfc_free_actual_arglist (args);
7548 : 0 : return false;
7549 : : }
7550 : :
7551 : 534 : gcc_assert (g->specific->pass_arg_num > 0);
7552 : 534 : gcc_assert (!g->specific->error);
7553 : 534 : args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7554 : : g->specific->pass_arg);
7555 : : }
7556 : 620 : resolve_actual_arglist (args, target->attr.proc,
7557 : 620 : is_external_proc (target)
7558 : 620 : && gfc_sym_get_dummy_args (target) == NULL);
7559 : :
7560 : : /* Check if this arglist matches the formal. */
7561 : 620 : matches = gfc_arglist_matches_symbol (&args, target);
7562 : :
7563 : : /* Clean up and break out of the loop if we've found it. */
7564 : 620 : gfc_free_actual_arglist (args);
7565 : 620 : if (matches)
7566 : : {
7567 : 396 : e->value.compcall.tbp = g->specific;
7568 : 396 : genname = g->specific_st->name;
7569 : : /* Pass along the name for CLASS methods, where the vtab
7570 : : procedure pointer component has to be referenced. */
7571 : 396 : if (name)
7572 : 159 : *name = genname;
7573 : 396 : goto success;
7574 : : }
7575 : : }
7576 : : }
7577 : :
7578 : : /* Nothing matching found! */
7579 : 2 : gfc_error ("Found no matching specific binding for the call to the GENERIC"
7580 : : " %qs at %L", genname, &e->where);
7581 : 2 : return false;
7582 : :
7583 : 396 : success:
7584 : : /* Make sure that we have the right specific instance for the name. */
7585 : 396 : derived = get_declared_from_expr (NULL, NULL, e, true);
7586 : :
7587 : 396 : st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7588 : 396 : if (st)
7589 : 396 : e->value.compcall.tbp = st->n.tb;
7590 : :
7591 : : return true;
7592 : : }
7593 : :
7594 : :
7595 : : /* Resolve a call to a type-bound subroutine. */
7596 : :
7597 : : static bool
7598 : 1676 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7599 : : {
7600 : 1676 : gfc_actual_arglist* newactual;
7601 : 1676 : gfc_symtree* target;
7602 : :
7603 : : /* Check that's really a SUBROUTINE. */
7604 : 1676 : if (!c->expr1->value.compcall.tbp->subroutine)
7605 : : {
7606 : 17 : if (!c->expr1->value.compcall.tbp->is_generic
7607 : 15 : && c->expr1->value.compcall.tbp->u.specific
7608 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym
7609 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7610 : 12 : c->expr1->value.compcall.tbp->subroutine = 1;
7611 : : else
7612 : : {
7613 : 5 : gfc_error ("%qs at %L should be a SUBROUTINE",
7614 : : c->expr1->value.compcall.name, &c->loc);
7615 : 5 : return false;
7616 : : }
7617 : : }
7618 : :
7619 : 1671 : if (!check_typebound_baseobject (c->expr1))
7620 : : return false;
7621 : :
7622 : : /* Pass along the name for CLASS methods, where the vtab
7623 : : procedure pointer component has to be referenced. */
7624 : 1664 : if (name)
7625 : 473 : *name = c->expr1->value.compcall.name;
7626 : :
7627 : 1664 : if (!resolve_typebound_generic_call (c->expr1, name))
7628 : : return false;
7629 : :
7630 : : /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7631 : 1663 : if (overridable)
7632 : 370 : *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7633 : :
7634 : : /* Transform into an ordinary EXEC_CALL for now. */
7635 : :
7636 : 1663 : if (!resolve_typebound_static (c->expr1, &target, &newactual))
7637 : : return false;
7638 : :
7639 : 1661 : c->ext.actual = newactual;
7640 : 1661 : c->symtree = target;
7641 : 1661 : c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7642 : :
7643 : 1661 : gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7644 : :
7645 : 1661 : gfc_free_expr (c->expr1);
7646 : 1661 : c->expr1 = gfc_get_expr ();
7647 : 1661 : c->expr1->expr_type = EXPR_FUNCTION;
7648 : 1661 : c->expr1->symtree = target;
7649 : 1661 : c->expr1->where = c->loc;
7650 : :
7651 : 1661 : return resolve_call (c);
7652 : : }
7653 : :
7654 : :
7655 : : /* Resolve a component-call expression. */
7656 : : static bool
7657 : 1590 : resolve_compcall (gfc_expr* e, const char **name)
7658 : : {
7659 : 1590 : gfc_actual_arglist* newactual;
7660 : 1590 : gfc_symtree* target;
7661 : :
7662 : : /* Check that's really a FUNCTION. */
7663 : 1590 : if (!e->value.compcall.tbp->function)
7664 : : {
7665 : 19 : if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7666 : 5 : gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7667 : : &e->where);
7668 : 19 : return false;
7669 : : }
7670 : :
7671 : :
7672 : : /* These must not be assign-calls! */
7673 : 1571 : gcc_assert (!e->value.compcall.assign);
7674 : :
7675 : 1571 : if (!check_typebound_baseobject (e))
7676 : : return false;
7677 : :
7678 : : /* Pass along the name for CLASS methods, where the vtab
7679 : : procedure pointer component has to be referenced. */
7680 : 1569 : if (name)
7681 : 858 : *name = e->value.compcall.name;
7682 : :
7683 : 1569 : if (!resolve_typebound_generic_call (e, name))
7684 : : return false;
7685 : 1568 : gcc_assert (!e->value.compcall.tbp->is_generic);
7686 : :
7687 : : /* Take the rank from the function's symbol. */
7688 : 1568 : if (e->value.compcall.tbp->u.specific->n.sym->as)
7689 : : {
7690 : 149 : e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7691 : 149 : e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7692 : : }
7693 : :
7694 : : /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7695 : : arglist to the TBP's binding target. */
7696 : :
7697 : 1568 : if (!resolve_typebound_static (e, &target, &newactual))
7698 : : return false;
7699 : :
7700 : 1568 : e->value.function.actual = newactual;
7701 : 1568 : e->value.function.name = NULL;
7702 : 1568 : e->value.function.esym = target->n.sym;
7703 : 1568 : e->value.function.isym = NULL;
7704 : 1568 : e->symtree = target;
7705 : 1568 : e->ts = target->n.sym->ts;
7706 : 1568 : e->expr_type = EXPR_FUNCTION;
7707 : :
7708 : : /* Resolution is not necessary if this is a class subroutine; this
7709 : : function only has to identify the specific proc. Resolution of
7710 : : the call will be done next in resolve_typebound_call. */
7711 : 1568 : return gfc_resolve_expr (e);
7712 : : }
7713 : :
7714 : :
7715 : : static bool resolve_fl_derived (gfc_symbol *sym);
7716 : :
7717 : :
7718 : : /* Resolve a typebound function, or 'method'. First separate all
7719 : : the non-CLASS references by calling resolve_compcall directly. */
7720 : :
7721 : : static bool
7722 : 1590 : resolve_typebound_function (gfc_expr* e)
7723 : : {
7724 : 1590 : gfc_symbol *declared;
7725 : 1590 : gfc_component *c;
7726 : 1590 : gfc_ref *new_ref;
7727 : 1590 : gfc_ref *class_ref;
7728 : 1590 : gfc_symtree *st;
7729 : 1590 : const char *name;
7730 : 1590 : gfc_typespec ts;
7731 : 1590 : gfc_expr *expr;
7732 : 1590 : bool overridable;
7733 : :
7734 : 1590 : st = e->symtree;
7735 : :
7736 : : /* Deal with typebound operators for CLASS objects. */
7737 : 1590 : expr = e->value.compcall.base_object;
7738 : 1590 : overridable = !e->value.compcall.tbp->non_overridable;
7739 : 1590 : if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7740 : : {
7741 : : /* Since the typebound operators are generic, we have to ensure
7742 : : that any delays in resolution are corrected and that the vtab
7743 : : is present. */
7744 : 184 : ts = expr->ts;
7745 : 184 : declared = ts.u.derived;
7746 : 184 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7747 : 184 : if (c->ts.u.derived == NULL)
7748 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7749 : :
7750 : 184 : if (!resolve_compcall (e, &name))
7751 : : return false;
7752 : :
7753 : : /* Use the generic name if it is there. */
7754 : 184 : name = name ? name : e->value.function.esym->name;
7755 : 184 : e->symtree = expr->symtree;
7756 : 184 : e->ref = gfc_copy_ref (expr->ref);
7757 : 184 : get_declared_from_expr (&class_ref, NULL, e, false);
7758 : :
7759 : : /* Trim away the extraneous references that emerge from nested
7760 : : use of interface.cc (extend_expr). */
7761 : 184 : if (class_ref && class_ref->next)
7762 : : {
7763 : 0 : gfc_free_ref_list (class_ref->next);
7764 : 0 : class_ref->next = NULL;
7765 : : }
7766 : 184 : else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7767 : : {
7768 : 0 : gfc_free_ref_list (e->ref);
7769 : 0 : e->ref = NULL;
7770 : : }
7771 : :
7772 : 184 : gfc_add_vptr_component (e);
7773 : 184 : gfc_add_component_ref (e, name);
7774 : 184 : e->value.function.esym = NULL;
7775 : 184 : if (expr->expr_type != EXPR_VARIABLE)
7776 : 80 : e->base_expr = expr;
7777 : 184 : return true;
7778 : : }
7779 : :
7780 : 1406 : if (st == NULL)
7781 : 153 : return resolve_compcall (e, NULL);
7782 : :
7783 : 1253 : if (!gfc_resolve_ref (e))
7784 : : return false;
7785 : :
7786 : : /* Get the CLASS declared type. */
7787 : 1253 : declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7788 : :
7789 : 1253 : if (!resolve_fl_derived (declared))
7790 : : return false;
7791 : :
7792 : : /* Weed out cases of the ultimate component being a derived type. */
7793 : 1253 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7794 : 1168 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7795 : : {
7796 : 565 : gfc_free_ref_list (new_ref);
7797 : 565 : return resolve_compcall (e, NULL);
7798 : : }
7799 : :
7800 : 688 : c = gfc_find_component (declared, "_data", true, true, NULL);
7801 : :
7802 : : /* Treat the call as if it is a typebound procedure, in order to roll
7803 : : out the correct name for the specific function. */
7804 : 688 : if (!resolve_compcall (e, &name))
7805 : : {
7806 : 15 : gfc_free_ref_list (new_ref);
7807 : 15 : return false;
7808 : : }
7809 : 673 : ts = e->ts;
7810 : :
7811 : 673 : if (overridable)
7812 : : {
7813 : : /* Convert the expression to a procedure pointer component call. */
7814 : 671 : e->value.function.esym = NULL;
7815 : 671 : e->symtree = st;
7816 : :
7817 : 671 : if (new_ref)
7818 : 124 : e->ref = new_ref;
7819 : :
7820 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7821 : 671 : gfc_add_vptr_component (e);
7822 : 671 : gfc_add_component_ref (e, name);
7823 : :
7824 : : /* Recover the typespec for the expression. This is really only
7825 : : necessary for generic procedures, where the additional call
7826 : : to gfc_add_component_ref seems to throw the collection of the
7827 : : correct typespec. */
7828 : 671 : e->ts = ts;
7829 : : }
7830 : 2 : else if (new_ref)
7831 : 0 : gfc_free_ref_list (new_ref);
7832 : :
7833 : : return true;
7834 : : }
7835 : :
7836 : : /* Resolve a typebound subroutine, or 'method'. First separate all
7837 : : the non-CLASS references by calling resolve_typebound_call
7838 : : directly. */
7839 : :
7840 : : static bool
7841 : 1676 : resolve_typebound_subroutine (gfc_code *code)
7842 : : {
7843 : 1676 : gfc_symbol *declared;
7844 : 1676 : gfc_component *c;
7845 : 1676 : gfc_ref *new_ref;
7846 : 1676 : gfc_ref *class_ref;
7847 : 1676 : gfc_symtree *st;
7848 : 1676 : const char *name;
7849 : 1676 : gfc_typespec ts;
7850 : 1676 : gfc_expr *expr;
7851 : 1676 : bool overridable;
7852 : :
7853 : 1676 : st = code->expr1->symtree;
7854 : :
7855 : : /* Deal with typebound operators for CLASS objects. */
7856 : 1676 : expr = code->expr1->value.compcall.base_object;
7857 : 1676 : overridable = !code->expr1->value.compcall.tbp->non_overridable;
7858 : 1676 : if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7859 : : {
7860 : : /* If the base_object is not a variable, the corresponding actual
7861 : : argument expression must be stored in e->base_expression so
7862 : : that the corresponding tree temporary can be used as the base
7863 : : object in gfc_conv_procedure_call. */
7864 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7865 : : {
7866 : : gfc_actual_arglist *args;
7867 : :
7868 : : args= code->expr1->value.function.actual;
7869 : : for (; args; args = args->next)
7870 : : if (expr == args->expr)
7871 : : expr = args->expr;
7872 : : }
7873 : :
7874 : : /* Since the typebound operators are generic, we have to ensure
7875 : : that any delays in resolution are corrected and that the vtab
7876 : : is present. */
7877 : 103 : declared = expr->ts.u.derived;
7878 : 103 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7879 : 103 : if (c->ts.u.derived == NULL)
7880 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7881 : :
7882 : 103 : if (!resolve_typebound_call (code, &name, NULL))
7883 : : return false;
7884 : :
7885 : : /* Use the generic name if it is there. */
7886 : 103 : name = name ? name : code->expr1->value.function.esym->name;
7887 : 103 : code->expr1->symtree = expr->symtree;
7888 : 103 : code->expr1->ref = gfc_copy_ref (expr->ref);
7889 : :
7890 : : /* Trim away the extraneous references that emerge from nested
7891 : : use of interface.cc (extend_expr). */
7892 : 103 : get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7893 : 103 : if (class_ref && class_ref->next)
7894 : : {
7895 : 0 : gfc_free_ref_list (class_ref->next);
7896 : 0 : class_ref->next = NULL;
7897 : : }
7898 : 103 : else if (code->expr1->ref && !class_ref)
7899 : : {
7900 : 12 : gfc_free_ref_list (code->expr1->ref);
7901 : 12 : code->expr1->ref = NULL;
7902 : : }
7903 : :
7904 : : /* Now use the procedure in the vtable. */
7905 : 103 : gfc_add_vptr_component (code->expr1);
7906 : 103 : gfc_add_component_ref (code->expr1, name);
7907 : 103 : code->expr1->value.function.esym = NULL;
7908 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7909 : 0 : code->expr1->base_expr = expr;
7910 : 103 : return true;
7911 : : }
7912 : :
7913 : 1573 : if (st == NULL)
7914 : 313 : return resolve_typebound_call (code, NULL, NULL);
7915 : :
7916 : 1260 : if (!gfc_resolve_ref (code->expr1))
7917 : : return false;
7918 : :
7919 : : /* Get the CLASS declared type. */
7920 : 1260 : get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7921 : :
7922 : : /* Weed out cases of the ultimate component being a derived type. */
7923 : 1260 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7924 : 1197 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7925 : : {
7926 : 885 : gfc_free_ref_list (new_ref);
7927 : 885 : return resolve_typebound_call (code, NULL, NULL);
7928 : : }
7929 : :
7930 : 375 : if (!resolve_typebound_call (code, &name, &overridable))
7931 : : {
7932 : 5 : gfc_free_ref_list (new_ref);
7933 : 5 : return false;
7934 : : }
7935 : 370 : ts = code->expr1->ts;
7936 : :
7937 : 370 : if (overridable)
7938 : : {
7939 : : /* Convert the expression to a procedure pointer component call. */
7940 : 368 : code->expr1->value.function.esym = NULL;
7941 : 368 : code->expr1->symtree = st;
7942 : :
7943 : 368 : if (new_ref)
7944 : 92 : code->expr1->ref = new_ref;
7945 : :
7946 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7947 : 368 : gfc_add_vptr_component (code->expr1);
7948 : 368 : gfc_add_component_ref (code->expr1, name);
7949 : :
7950 : : /* Recover the typespec for the expression. This is really only
7951 : : necessary for generic procedures, where the additional call
7952 : : to gfc_add_component_ref seems to throw the collection of the
7953 : : correct typespec. */
7954 : 368 : code->expr1->ts = ts;
7955 : : }
7956 : 2 : else if (new_ref)
7957 : 0 : gfc_free_ref_list (new_ref);
7958 : :
7959 : : return true;
7960 : : }
7961 : :
7962 : :
7963 : : /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7964 : :
7965 : : static bool
7966 : 123 : resolve_ppc_call (gfc_code* c)
7967 : : {
7968 : 123 : gfc_component *comp;
7969 : :
7970 : 123 : comp = gfc_get_proc_ptr_comp (c->expr1);
7971 : 123 : gcc_assert (comp != NULL);
7972 : :
7973 : 123 : c->resolved_sym = c->expr1->symtree->n.sym;
7974 : 123 : c->expr1->expr_type = EXPR_VARIABLE;
7975 : :
7976 : 123 : if (!comp->attr.subroutine)
7977 : 1 : gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7978 : :
7979 : 123 : if (!gfc_resolve_ref (c->expr1))
7980 : : return false;
7981 : :
7982 : 123 : if (!update_ppc_arglist (c->expr1))
7983 : : return false;
7984 : :
7985 : 122 : c->ext.actual = c->expr1->value.compcall.actual;
7986 : :
7987 : 122 : if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7988 : 122 : !(comp->ts.interface
7989 : 93 : && comp->ts.interface->formal)))
7990 : : return false;
7991 : :
7992 : 122 : if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7993 : : return false;
7994 : :
7995 : 121 : gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7996 : :
7997 : 121 : return true;
7998 : : }
7999 : :
8000 : :
8001 : : /* Resolve a Function Call to a Procedure Pointer Component (Function). */
8002 : :
8003 : : static bool
8004 : 288 : resolve_expr_ppc (gfc_expr* e)
8005 : : {
8006 : 288 : gfc_component *comp;
8007 : :
8008 : 288 : comp = gfc_get_proc_ptr_comp (e);
8009 : 288 : gcc_assert (comp != NULL);
8010 : :
8011 : : /* Convert to EXPR_FUNCTION. */
8012 : 288 : e->expr_type = EXPR_FUNCTION;
8013 : 288 : e->value.function.isym = NULL;
8014 : 288 : e->value.function.actual = e->value.compcall.actual;
8015 : 288 : e->ts = comp->ts;
8016 : 288 : if (comp->as != NULL)
8017 : : {
8018 : 28 : e->rank = comp->as->rank;
8019 : 28 : e->corank = comp->as->corank;
8020 : : }
8021 : :
8022 : 288 : if (!comp->attr.function)
8023 : 3 : gfc_add_function (&comp->attr, comp->name, &e->where);
8024 : :
8025 : 288 : if (!gfc_resolve_ref (e))
8026 : : return false;
8027 : :
8028 : 288 : if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
8029 : 288 : !(comp->ts.interface
8030 : 287 : && comp->ts.interface->formal)))
8031 : : return false;
8032 : :
8033 : 288 : if (!update_ppc_arglist (e))
8034 : : return false;
8035 : :
8036 : 286 : if (!check_pure_function(e))
8037 : : return false;
8038 : :
8039 : 285 : gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
8040 : :
8041 : 285 : return true;
8042 : : }
8043 : :
8044 : :
8045 : : static bool
8046 : 11777 : gfc_is_expandable_expr (gfc_expr *e)
8047 : : {
8048 : 11777 : gfc_constructor *con;
8049 : :
8050 : 11777 : if (e->expr_type == EXPR_ARRAY)
8051 : : {
8052 : : /* Traverse the constructor looking for variables that are flavor
8053 : : parameter. Parameters must be expanded since they are fully used at
8054 : : compile time. */
8055 : 11777 : con = gfc_constructor_first (e->value.constructor);
8056 : 31342 : for (; con; con = gfc_constructor_next (con))
8057 : : {
8058 : 13457 : if (con->expr->expr_type == EXPR_VARIABLE
8059 : 4988 : && con->expr->symtree
8060 : 4988 : && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
8061 : 4909 : || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
8062 : : return true;
8063 : 8469 : if (con->expr->expr_type == EXPR_ARRAY
8064 : 8469 : && gfc_is_expandable_expr (con->expr))
8065 : : return true;
8066 : : }
8067 : : }
8068 : :
8069 : : return false;
8070 : : }
8071 : :
8072 : :
8073 : : /* Sometimes variables in specification expressions of the result
8074 : : of module procedures in submodules wind up not being the 'real'
8075 : : dummy. Find this, if possible, in the namespace of the first
8076 : : formal argument. */
8077 : :
8078 : : static void
8079 : 3449 : fixup_unique_dummy (gfc_expr *e)
8080 : : {
8081 : 3449 : gfc_symtree *st = NULL;
8082 : 3449 : gfc_symbol *s = NULL;
8083 : :
8084 : 3449 : if (e->symtree->n.sym->ns->proc_name
8085 : 3419 : && e->symtree->n.sym->ns->proc_name->formal)
8086 : 3419 : s = e->symtree->n.sym->ns->proc_name->formal->sym;
8087 : :
8088 : 3419 : if (s != NULL)
8089 : 3419 : st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
8090 : :
8091 : 3449 : if (st != NULL
8092 : 14 : && st->n.sym != NULL
8093 : 14 : && st->n.sym->attr.dummy)
8094 : 14 : e->symtree = st;
8095 : 3449 : }
8096 : :
8097 : :
8098 : : /* Resolve an expression. That is, make sure that types of operands agree
8099 : : with their operators, intrinsic operators are converted to function calls
8100 : : for overloaded types and unresolved function references are resolved. */
8101 : :
8102 : : bool
8103 : 6976613 : gfc_resolve_expr (gfc_expr *e)
8104 : : {
8105 : 6976613 : bool t;
8106 : 6976613 : bool inquiry_save, actual_arg_save, first_actual_arg_save;
8107 : :
8108 : 6976613 : if (e == NULL || e->do_not_resolve_again)
8109 : : return true;
8110 : :
8111 : : /* inquiry_argument only applies to variables. */
8112 : 5100835 : inquiry_save = inquiry_argument;
8113 : 5100835 : actual_arg_save = actual_arg;
8114 : 5100835 : first_actual_arg_save = first_actual_arg;
8115 : :
8116 : 5100835 : if (e->expr_type != EXPR_VARIABLE)
8117 : : {
8118 : 3803204 : inquiry_argument = false;
8119 : 3803204 : actual_arg = false;
8120 : 3803204 : first_actual_arg = false;
8121 : : }
8122 : 1297631 : else if (e->symtree != NULL
8123 : 1297212 : && *e->symtree->name == '@'
8124 : 4157 : && e->symtree->n.sym->attr.dummy)
8125 : : {
8126 : : /* Deal with submodule specification expressions that are not
8127 : : found to be referenced in module.cc(read_cleanup). */
8128 : 3449 : fixup_unique_dummy (e);
8129 : : }
8130 : :
8131 : 5100835 : switch (e->expr_type)
8132 : : {
8133 : 519862 : case EXPR_OP:
8134 : 519862 : t = resolve_operator (e);
8135 : 519862 : break;
8136 : :
8137 : 150 : case EXPR_CONDITIONAL:
8138 : 150 : t = resolve_conditional (e);
8139 : 150 : break;
8140 : :
8141 : 1634531 : case EXPR_FUNCTION:
8142 : 1634531 : case EXPR_VARIABLE:
8143 : :
8144 : 1634531 : if (check_host_association (e))
8145 : 336936 : t = resolve_function (e);
8146 : : else
8147 : 1297595 : t = resolve_variable (e);
8148 : :
8149 : 1634531 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
8150 : 6867 : && e->ref->type != REF_SUBSTRING)
8151 : 2143 : gfc_resolve_substring_charlen (e);
8152 : :
8153 : : break;
8154 : :
8155 : 1590 : case EXPR_COMPCALL:
8156 : 1590 : t = resolve_typebound_function (e);
8157 : 1590 : break;
8158 : :
8159 : 507 : case EXPR_SUBSTRING:
8160 : 507 : t = gfc_resolve_ref (e);
8161 : 507 : break;
8162 : :
8163 : : case EXPR_CONSTANT:
8164 : : case EXPR_NULL:
8165 : : t = true;
8166 : : break;
8167 : :
8168 : 288 : case EXPR_PPC:
8169 : 288 : t = resolve_expr_ppc (e);
8170 : 288 : break;
8171 : :
8172 : 67860 : case EXPR_ARRAY:
8173 : 67860 : t = false;
8174 : 67860 : if (!gfc_resolve_ref (e))
8175 : : break;
8176 : :
8177 : 67860 : t = gfc_resolve_array_constructor (e);
8178 : : /* Also try to expand a constructor. */
8179 : 67860 : if (t)
8180 : : {
8181 : 67764 : gfc_expression_rank (e);
8182 : 67764 : if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
8183 : 62753 : gfc_expand_constructor (e, false);
8184 : : }
8185 : :
8186 : : /* This provides the opportunity for the length of constructors with
8187 : : character valued function elements to propagate the string length
8188 : : to the expression. */
8189 : 67764 : if (t && e->ts.type == BT_CHARACTER)
8190 : : {
8191 : : /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
8192 : : here rather then add a duplicate test for it above. */
8193 : 10031 : gfc_expand_constructor (e, false);
8194 : 10031 : t = gfc_resolve_character_array_constructor (e);
8195 : : }
8196 : :
8197 : : break;
8198 : :
8199 : 16021 : case EXPR_STRUCTURE:
8200 : 16021 : t = gfc_resolve_ref (e);
8201 : 16021 : if (!t)
8202 : : break;
8203 : :
8204 : 16021 : t = resolve_structure_cons (e, 0);
8205 : 16021 : if (!t)
8206 : : break;
8207 : :
8208 : 16009 : t = gfc_simplify_expr (e, 0);
8209 : 16009 : break;
8210 : :
8211 : 0 : default:
8212 : 0 : gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
8213 : : }
8214 : :
8215 : 5100835 : if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
8216 : 176839 : fixup_charlen (e);
8217 : :
8218 : 5100835 : inquiry_argument = inquiry_save;
8219 : 5100835 : actual_arg = actual_arg_save;
8220 : 5100835 : first_actual_arg = first_actual_arg_save;
8221 : :
8222 : : /* For some reason, resolving these expressions a second time mangles
8223 : : the typespec of the expression itself. */
8224 : 5100835 : if (t && e->expr_type == EXPR_VARIABLE
8225 : 1294770 : && e->symtree->n.sym->attr.select_rank_temporary
8226 : 3422 : && UNLIMITED_POLY (e->symtree->n.sym))
8227 : 83 : e->do_not_resolve_again = 1;
8228 : :
8229 : 5098359 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
8230 : 6919 : t = check_import_status (e);
8231 : :
8232 : : return t;
8233 : : }
8234 : :
8235 : :
8236 : : /* Resolve an expression from an iterator. They must be scalar and have
8237 : : INTEGER or (optionally) REAL type. */
8238 : :
8239 : : static bool
8240 : 149081 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
8241 : : const char *name_msgid)
8242 : : {
8243 : 149081 : if (!gfc_resolve_expr (expr))
8244 : : return false;
8245 : :
8246 : 149076 : if (expr->rank != 0)
8247 : : {
8248 : 0 : gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
8249 : 0 : return false;
8250 : : }
8251 : :
8252 : 149076 : if (expr->ts.type != BT_INTEGER)
8253 : : {
8254 : 274 : if (expr->ts.type == BT_REAL)
8255 : : {
8256 : 274 : if (real_ok)
8257 : 271 : return gfc_notify_std (GFC_STD_F95_DEL,
8258 : : "%s at %L must be integer",
8259 : 271 : _(name_msgid), &expr->where);
8260 : : else
8261 : : {
8262 : 3 : gfc_error ("%s at %L must be INTEGER", _(name_msgid),
8263 : : &expr->where);
8264 : 3 : return false;
8265 : : }
8266 : : }
8267 : : else
8268 : : {
8269 : 0 : gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
8270 : 0 : return false;
8271 : : }
8272 : : }
8273 : : return true;
8274 : : }
8275 : :
8276 : :
8277 : : /* Resolve the expressions in an iterator structure. If REAL_OK is
8278 : : false allow only INTEGER type iterators, otherwise allow REAL types.
8279 : : Set own_scope to true for ac-implied-do and data-implied-do as those
8280 : : have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8281 : :
8282 : : bool
8283 : 37279 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8284 : : {
8285 : 37279 : if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8286 : : return false;
8287 : :
8288 : 37275 : if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8289 : 37275 : _("iterator variable")))
8290 : : return false;
8291 : :
8292 : 37269 : if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8293 : : "Start expression in DO loop"))
8294 : : return false;
8295 : :
8296 : 37268 : if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8297 : : "End expression in DO loop"))
8298 : : return false;
8299 : :
8300 : 37265 : if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8301 : : "Step expression in DO loop"))
8302 : : return false;
8303 : :
8304 : : /* Convert start, end, and step to the same type as var. */
8305 : 37264 : if (iter->start->ts.kind != iter->var->ts.kind
8306 : 36984 : || iter->start->ts.type != iter->var->ts.type)
8307 : 315 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8308 : :
8309 : 37264 : if (iter->end->ts.kind != iter->var->ts.kind
8310 : 37011 : || iter->end->ts.type != iter->var->ts.type)
8311 : 278 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8312 : :
8313 : 37264 : if (iter->step->ts.kind != iter->var->ts.kind
8314 : 37020 : || iter->step->ts.type != iter->var->ts.type)
8315 : 280 : gfc_convert_type (iter->step, &iter->var->ts, 1);
8316 : :
8317 : 37264 : if (iter->step->expr_type == EXPR_CONSTANT)
8318 : : {
8319 : 36142 : if ((iter->step->ts.type == BT_INTEGER
8320 : 36059 : && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8321 : 72199 : || (iter->step->ts.type == BT_REAL
8322 : 83 : && mpfr_sgn (iter->step->value.real) == 0))
8323 : : {
8324 : 3 : gfc_error ("Step expression in DO loop at %L cannot be zero",
8325 : 3 : &iter->step->where);
8326 : 3 : return false;
8327 : : }
8328 : : }
8329 : :
8330 : 37261 : if (iter->start->expr_type == EXPR_CONSTANT
8331 : 34207 : && iter->end->expr_type == EXPR_CONSTANT
8332 : 26807 : && iter->step->expr_type == EXPR_CONSTANT)
8333 : : {
8334 : 26540 : int sgn, cmp;
8335 : 26540 : if (iter->start->ts.type == BT_INTEGER)
8336 : : {
8337 : 26486 : sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8338 : 26486 : cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8339 : : }
8340 : : else
8341 : : {
8342 : 54 : sgn = mpfr_sgn (iter->step->value.real);
8343 : 54 : cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8344 : : }
8345 : 26540 : if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8346 : 146 : gfc_warning (OPT_Wzerotrip,
8347 : : "DO loop at %L will be executed zero times",
8348 : 146 : &iter->step->where);
8349 : : }
8350 : :
8351 : 37261 : if (iter->end->expr_type == EXPR_CONSTANT
8352 : 27172 : && iter->end->ts.type == BT_INTEGER
8353 : 27118 : && iter->step->expr_type == EXPR_CONSTANT
8354 : 26808 : && iter->step->ts.type == BT_INTEGER
8355 : 26808 : && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8356 : 26440 : || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8357 : : {
8358 : 25694 : bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8359 : 25694 : int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8360 : :
8361 : 25694 : if (is_step_positive
8362 : 25326 : && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8363 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8364 : : "DO loop at %L is undefined as it overflows",
8365 : 7 : &iter->step->where);
8366 : : else if (!is_step_positive
8367 : 368 : && mpz_cmp (iter->end->value.integer,
8368 : 368 : gfc_integer_kinds[k].min_int) == 0)
8369 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8370 : : "DO loop at %L is undefined as it underflows",
8371 : 7 : &iter->step->where);
8372 : : }
8373 : :
8374 : : return true;
8375 : : }
8376 : :
8377 : :
8378 : : /* Traversal function for find_forall_index. f == 2 signals that
8379 : : that variable itself is not to be checked - only the references. */
8380 : :
8381 : : static bool
8382 : 41684 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8383 : : {
8384 : 41684 : if (expr->expr_type != EXPR_VARIABLE)
8385 : : return false;
8386 : :
8387 : : /* A scalar assignment */
8388 : 17617 : if (!expr->ref || *f == 1)
8389 : : {
8390 : 11917 : if (expr->symtree->n.sym == sym)
8391 : : return true;
8392 : : else
8393 : : return false;
8394 : : }
8395 : :
8396 : 5700 : if (*f == 2)
8397 : 1730 : *f = 1;
8398 : : return false;
8399 : : }
8400 : :
8401 : :
8402 : : /* Check whether the FORALL index appears in the expression or not.
8403 : : Returns true if SYM is found in EXPR. */
8404 : :
8405 : : bool
8406 : 26375 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8407 : : {
8408 : 26375 : if (gfc_traverse_expr (expr, sym, forall_index, f))
8409 : : return true;
8410 : : else
8411 : : return false;
8412 : : }
8413 : :
8414 : : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8415 : : This constraint specifies rules for variables in locality-specs. */
8416 : :
8417 : : static int
8418 : 589 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8419 : : {
8420 : 589 : struct check_default_none_data *dt = (struct check_default_none_data *) data;
8421 : :
8422 : 589 : if ((*expr)->expr_type == EXPR_VARIABLE)
8423 : : {
8424 : 21 : gfc_symbol *sym = (*expr)->symtree->n.sym;
8425 : 21 : for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8426 : 23 : list; list = list->next)
8427 : : {
8428 : 5 : if (list->expr->symtree->n.sym == sym)
8429 : : {
8430 : 3 : gfc_error ("Variable %qs referenced in concurrent-header at %L "
8431 : : "must not appear in LOCAL locality-spec at %L",
8432 : : sym->name, &(*expr)->where, &list->expr->where);
8433 : 3 : *walk_subtrees = 0;
8434 : 3 : return 1;
8435 : : }
8436 : : }
8437 : : }
8438 : :
8439 : 586 : *walk_subtrees = 1;
8440 : 586 : return 0;
8441 : : }
8442 : :
8443 : : static int
8444 : 3761 : check_default_none_expr (gfc_expr **e, int *, void *data)
8445 : : {
8446 : 3761 : struct check_default_none_data *d = (struct check_default_none_data*) data;
8447 : :
8448 : 3761 : if ((*e)->expr_type == EXPR_VARIABLE)
8449 : : {
8450 : 1685 : gfc_symbol *sym = (*e)->symtree->n.sym;
8451 : :
8452 : 1685 : if (d->sym_hash->contains (sym))
8453 : 1262 : sym->mark = 1;
8454 : :
8455 : 423 : else if (d->default_none)
8456 : : {
8457 : 3 : gfc_namespace *ns2 = d->ns;
8458 : 5 : while (ns2)
8459 : : {
8460 : 3 : if (ns2 == sym->ns)
8461 : : break;
8462 : 2 : ns2 = ns2->parent;
8463 : : }
8464 : 3 : if (ns2 != NULL)
8465 : : {
8466 : 1 : gfc_error ("Variable %qs at %L not specified in a locality spec "
8467 : : "of DO CONCURRENT at %L but required due to "
8468 : : "DEFAULT (NONE)",
8469 : 1 : sym->name, &(*e)->where, &d->code->loc);
8470 : 1 : d->sym_hash->add (sym);
8471 : : }
8472 : : }
8473 : : }
8474 : 3761 : return 0;
8475 : : }
8476 : :
8477 : : static void
8478 : 174 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8479 : : {
8480 : 174 : struct check_default_none_data data;
8481 : 174 : data.code = code;
8482 : 174 : data.sym_hash = new hash_set<gfc_symbol *>;
8483 : 174 : data.ns = ns;
8484 : 174 : data.default_none = code->ext.concur.default_none;
8485 : :
8486 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8487 : : {
8488 : 696 : const char *name;
8489 : 696 : switch (locality)
8490 : : {
8491 : : case LOCALITY_LOCAL: name = "LOCAL"; break;
8492 : 174 : case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8493 : 174 : case LOCALITY_SHARED: name = "SHARED"; break;
8494 : 174 : case LOCALITY_REDUCE: name = "REDUCE"; break;
8495 : : default: gcc_unreachable ();
8496 : : }
8497 : :
8498 : 1082 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8499 : 386 : list = list->next)
8500 : : {
8501 : 386 : gfc_expr *expr = list->expr;
8502 : :
8503 : 386 : if (locality == LOCALITY_REDUCE
8504 : 72 : && (expr->expr_type == EXPR_FUNCTION
8505 : 48 : || expr->expr_type == EXPR_OP))
8506 : 35 : continue;
8507 : :
8508 : 362 : if (!gfc_resolve_expr (expr))
8509 : 3 : continue;
8510 : :
8511 : 359 : if (expr->expr_type != EXPR_VARIABLE
8512 : 359 : || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8513 : 359 : || (expr->ref
8514 : 147 : && (expr->ref->type != REF_ARRAY
8515 : 147 : || expr->ref->u.ar.type != AR_FULL
8516 : 143 : || expr->ref->next)))
8517 : : {
8518 : 4 : gfc_error ("Expected variable name in %s locality spec at %L",
8519 : : name, &expr->where);
8520 : 4 : continue;
8521 : : }
8522 : :
8523 : 355 : gfc_symbol *sym = expr->symtree->n.sym;
8524 : :
8525 : 355 : if (data.sym_hash->contains (sym))
8526 : : {
8527 : 4 : gfc_error ("Variable %qs at %L has already been specified in a "
8528 : : "locality-spec", sym->name, &expr->where);
8529 : 4 : continue;
8530 : : }
8531 : :
8532 : 351 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8533 : 702 : iter; iter = iter->next)
8534 : : {
8535 : 351 : if (iter->var->symtree->n.sym == sym)
8536 : : {
8537 : 0 : gfc_error ("Index variable %qs at %L cannot be specified in a "
8538 : : "locality-spec", sym->name, &expr->where);
8539 : 0 : continue;
8540 : : }
8541 : :
8542 : 351 : data.sym_hash->add (iter->var->symtree->n.sym);
8543 : : }
8544 : :
8545 : 351 : if (locality == LOCALITY_LOCAL
8546 : 351 : || locality == LOCALITY_LOCAL_INIT
8547 : 351 : || locality == LOCALITY_REDUCE)
8548 : : {
8549 : 197 : if (sym->attr.optional)
8550 : 3 : gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8551 : : "locality-spec at %L",
8552 : : sym->name, name, &expr->where);
8553 : :
8554 : 197 : if (sym->attr.dimension
8555 : 66 : && sym->as
8556 : 66 : && sym->as->type == AS_ASSUMED_SIZE)
8557 : 0 : gfc_error ("Assumed-size array not permitted for %qs in %s "
8558 : : "locality-spec at %L",
8559 : : sym->name, name, &expr->where);
8560 : :
8561 : 197 : gfc_check_vardef_context (expr, false, false, false, name);
8562 : : }
8563 : :
8564 : 197 : if (locality == LOCALITY_LOCAL
8565 : : || locality == LOCALITY_LOCAL_INIT)
8566 : : {
8567 : 180 : symbol_attribute attr = gfc_expr_attr (expr);
8568 : :
8569 : 180 : if (attr.allocatable)
8570 : 2 : gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8571 : : "locality-spec at %L",
8572 : : sym->name, name, &expr->where);
8573 : :
8574 : 178 : else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8575 : 2 : gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8576 : : " for %qs in %s locality-spec at %L",
8577 : : sym->name, name, &expr->where);
8578 : :
8579 : 176 : else if (attr.codimension)
8580 : 0 : gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8581 : : "at %L",
8582 : : sym->name, name, &expr->where);
8583 : :
8584 : 176 : else if (expr->ts.type == BT_DERIVED
8585 : 176 : && gfc_is_finalizable (expr->ts.u.derived, NULL))
8586 : 0 : gfc_error ("Finalizable type not permitted for %qs in %s "
8587 : : "locality-spec at %L",
8588 : : sym->name, name, &expr->where);
8589 : :
8590 : 176 : else if (gfc_has_ultimate_allocatable (expr))
8591 : 4 : gfc_error ("Type with ultimate allocatable component not "
8592 : : "permitted for %qs in %s locality-spec at %L",
8593 : : sym->name, name, &expr->where);
8594 : : }
8595 : :
8596 : 171 : else if (locality == LOCALITY_REDUCE)
8597 : : {
8598 : 17 : if (sym->attr.asynchronous)
8599 : 1 : gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8600 : : "REDUCE locality-spec at %L",
8601 : : sym->name, &expr->where);
8602 : 17 : if (sym->attr.volatile_)
8603 : 1 : gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8604 : : "locality-spec at %L", sym->name, &expr->where);
8605 : : }
8606 : :
8607 : 351 : data.sym_hash->add (sym);
8608 : : }
8609 : :
8610 : 696 : if (locality == LOCALITY_LOCAL)
8611 : : {
8612 : 174 : gcc_assert (locality == 0);
8613 : :
8614 : 174 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8615 : 359 : iter; iter = iter->next)
8616 : : {
8617 : 185 : gfc_expr_walker (&iter->start,
8618 : : do_concur_locality_specs_f2023,
8619 : : &data);
8620 : :
8621 : 185 : gfc_expr_walker (&iter->end,
8622 : : do_concur_locality_specs_f2023,
8623 : : &data);
8624 : :
8625 : 185 : gfc_expr_walker (&iter->stride,
8626 : : do_concur_locality_specs_f2023,
8627 : : &data);
8628 : : }
8629 : :
8630 : 174 : if (code->expr1)
8631 : 7 : gfc_expr_walker (&code->expr1,
8632 : : do_concur_locality_specs_f2023,
8633 : : &data);
8634 : : }
8635 : : }
8636 : :
8637 : 174 : gfc_expr *reduce_op = NULL;
8638 : :
8639 : 174 : for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8640 : 222 : list; list = list->next)
8641 : : {
8642 : 48 : gfc_expr *expr = list->expr;
8643 : :
8644 : 48 : if (expr->expr_type != EXPR_VARIABLE)
8645 : : {
8646 : 24 : reduce_op = expr;
8647 : 24 : continue;
8648 : : }
8649 : :
8650 : 24 : if (reduce_op->expr_type == EXPR_OP)
8651 : : {
8652 : 17 : switch (reduce_op->value.op.op)
8653 : : {
8654 : 17 : case INTRINSIC_PLUS:
8655 : 17 : case INTRINSIC_TIMES:
8656 : 17 : if (!gfc_numeric_ts (&expr->ts))
8657 : 3 : gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8658 : 3 : "got %s", expr->symtree->n.sym->name,
8659 : : &expr->where, gfc_basic_typename (expr->ts.type));
8660 : : break;
8661 : 0 : case INTRINSIC_AND:
8662 : 0 : case INTRINSIC_OR:
8663 : 0 : case INTRINSIC_EQV:
8664 : 0 : case INTRINSIC_NEQV:
8665 : 0 : if (expr->ts.type != BT_LOGICAL)
8666 : 0 : gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8667 : 0 : "got %qs", expr->symtree->n.sym->name,
8668 : : &expr->where, gfc_basic_typename (expr->ts.type));
8669 : : break;
8670 : 0 : default:
8671 : 0 : gcc_unreachable ();
8672 : : }
8673 : : }
8674 : :
8675 : 7 : else if (reduce_op->expr_type == EXPR_FUNCTION)
8676 : : {
8677 : 7 : switch (reduce_op->value.function.isym->id)
8678 : : {
8679 : 6 : case GFC_ISYM_MIN:
8680 : 6 : case GFC_ISYM_MAX:
8681 : 6 : if (expr->ts.type != BT_INTEGER
8682 : : && expr->ts.type != BT_REAL
8683 : : && expr->ts.type != BT_CHARACTER)
8684 : 2 : gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8685 : : "in REDUCE with MIN/MAX at %L, got %s",
8686 : 2 : expr->symtree->n.sym->name, &expr->where,
8687 : : gfc_basic_typename (expr->ts.type));
8688 : : break;
8689 : 1 : case GFC_ISYM_IAND:
8690 : 1 : case GFC_ISYM_IOR:
8691 : 1 : case GFC_ISYM_IEOR:
8692 : 1 : if (expr->ts.type != BT_INTEGER)
8693 : 1 : gfc_error ("Expected integer type for %qs in REDUCE with "
8694 : : "IAND/IOR/IEOR at %L, got %s",
8695 : 1 : expr->symtree->n.sym->name, &expr->where,
8696 : : gfc_basic_typename (expr->ts.type));
8697 : : break;
8698 : 0 : default:
8699 : 0 : gcc_unreachable ();
8700 : : }
8701 : : }
8702 : :
8703 : : else
8704 : 0 : gcc_unreachable ();
8705 : : }
8706 : :
8707 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8708 : : {
8709 : 1082 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8710 : 386 : list = list->next)
8711 : : {
8712 : 386 : if (list->expr->expr_type == EXPR_VARIABLE)
8713 : 362 : list->expr->symtree->n.sym->mark = 0;
8714 : : }
8715 : : }
8716 : :
8717 : 174 : gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8718 : : check_default_none_expr, &data);
8719 : :
8720 : 870 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8721 : : {
8722 : 696 : gfc_expr_list **plist = &code->ext.concur.locality[locality];
8723 : 1082 : while (*plist)
8724 : : {
8725 : 386 : gfc_expr *expr = (*plist)->expr;
8726 : 386 : if (expr->expr_type == EXPR_VARIABLE)
8727 : : {
8728 : 362 : gfc_symbol *sym = expr->symtree->n.sym;
8729 : 362 : if (sym->mark == 0)
8730 : : {
8731 : 70 : gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8732 : : "locality-spec at %L is not used",
8733 : : sym->name, &expr->where);
8734 : 70 : gfc_expr_list *tmp = *plist;
8735 : 70 : *plist = (*plist)->next;
8736 : 70 : gfc_free_expr (tmp->expr);
8737 : 70 : free (tmp);
8738 : 70 : continue;
8739 : 70 : }
8740 : : }
8741 : 316 : plist = &((*plist)->next);
8742 : : }
8743 : : }
8744 : 174 : }
8745 : :
8746 : : /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8747 : : to be a scalar INTEGER variable. The subscripts and stride are scalar
8748 : : INTEGERs, and if stride is a constant it must be nonzero.
8749 : : Furthermore "A subscript or stride in a forall-triplet-spec shall
8750 : : not contain a reference to any index-name in the
8751 : : forall-triplet-spec-list in which it appears." (7.5.4.1) */
8752 : :
8753 : : static void
8754 : 2164 : resolve_forall_iterators (gfc_forall_iterator *it)
8755 : : {
8756 : 2164 : gfc_forall_iterator *iter, *iter2;
8757 : :
8758 : 6237 : for (iter = it; iter; iter = iter->next)
8759 : : {
8760 : 4073 : if (gfc_resolve_expr (iter->var)
8761 : 4073 : && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8762 : 0 : gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8763 : : &iter->var->where);
8764 : :
8765 : 4073 : if (gfc_resolve_expr (iter->start)
8766 : 4073 : && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8767 : 0 : gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8768 : : &iter->start->where);
8769 : 4073 : if (iter->var->ts.kind != iter->start->ts.kind)
8770 : 1 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8771 : :
8772 : 4073 : if (gfc_resolve_expr (iter->end)
8773 : 4073 : && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8774 : 0 : gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8775 : : &iter->end->where);
8776 : 4073 : if (iter->var->ts.kind != iter->end->ts.kind)
8777 : 2 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8778 : :
8779 : 4073 : if (gfc_resolve_expr (iter->stride))
8780 : : {
8781 : 4073 : if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8782 : 0 : gfc_error ("FORALL stride expression at %L must be a scalar %s",
8783 : : &iter->stride->where, "INTEGER");
8784 : :
8785 : 4073 : if (iter->stride->expr_type == EXPR_CONSTANT
8786 : 4070 : && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8787 : 1 : gfc_error ("FORALL stride expression at %L cannot be zero",
8788 : : &iter->stride->where);
8789 : : }
8790 : 4073 : if (iter->var->ts.kind != iter->stride->ts.kind)
8791 : 1 : gfc_convert_type (iter->stride, &iter->var->ts, 1);
8792 : : }
8793 : :
8794 : 6237 : for (iter = it; iter; iter = iter->next)
8795 : 10981 : for (iter2 = iter; iter2; iter2 = iter2->next)
8796 : : {
8797 : 6908 : if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8798 : 6906 : || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8799 : 13812 : || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8800 : 6 : gfc_error ("FORALL index %qs may not appear in triplet "
8801 : 6 : "specification at %L", iter->var->symtree->name,
8802 : 6 : &iter2->start->where);
8803 : : }
8804 : 2164 : }
8805 : :
8806 : :
8807 : : /* Given a pointer to a symbol that is a derived type, see if it's
8808 : : inaccessible, i.e. if it's defined in another module and the components are
8809 : : PRIVATE. The search is recursive if necessary. Returns zero if no
8810 : : inaccessible components are found, nonzero otherwise. */
8811 : :
8812 : : static bool
8813 : 1329 : derived_inaccessible (gfc_symbol *sym)
8814 : : {
8815 : 1329 : gfc_component *c;
8816 : :
8817 : 1329 : if (sym->attr.use_assoc && sym->attr.private_comp)
8818 : : return 1;
8819 : :
8820 : 3949 : for (c = sym->components; c; c = c->next)
8821 : : {
8822 : : /* Prevent an infinite loop through this function. */
8823 : 2633 : if (c->ts.type == BT_DERIVED
8824 : 288 : && (c->attr.pointer || c->attr.allocatable)
8825 : 72 : && sym == c->ts.u.derived)
8826 : 72 : continue;
8827 : :
8828 : 2561 : if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8829 : : return 1;
8830 : : }
8831 : :
8832 : : return 0;
8833 : : }
8834 : :
8835 : :
8836 : : /* Resolve the argument of a deallocate expression. The expression must be
8837 : : a pointer or a full array. */
8838 : :
8839 : : static bool
8840 : 8027 : resolve_deallocate_expr (gfc_expr *e)
8841 : : {
8842 : 8027 : symbol_attribute attr;
8843 : 8027 : int allocatable, pointer;
8844 : 8027 : gfc_ref *ref;
8845 : 8027 : gfc_symbol *sym;
8846 : 8027 : gfc_component *c;
8847 : 8027 : bool unlimited;
8848 : :
8849 : 8027 : if (!gfc_resolve_expr (e))
8850 : : return false;
8851 : :
8852 : 8027 : if (e->expr_type != EXPR_VARIABLE)
8853 : 0 : goto bad;
8854 : :
8855 : 8027 : sym = e->symtree->n.sym;
8856 : 8027 : unlimited = UNLIMITED_POLY(sym);
8857 : :
8858 : 8027 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8859 : : {
8860 : 1504 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8861 : 1504 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8862 : : }
8863 : : else
8864 : : {
8865 : 6523 : allocatable = sym->attr.allocatable;
8866 : 6523 : pointer = sym->attr.pointer;
8867 : : }
8868 : 16039 : for (ref = e->ref; ref; ref = ref->next)
8869 : : {
8870 : 8012 : switch (ref->type)
8871 : : {
8872 : 5966 : case REF_ARRAY:
8873 : 5966 : if (ref->u.ar.type != AR_FULL
8874 : 6154 : && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8875 : 188 : && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8876 : : allocatable = 0;
8877 : : break;
8878 : :
8879 : 2046 : case REF_COMPONENT:
8880 : 2046 : c = ref->u.c.component;
8881 : 2046 : if (c->ts.type == BT_CLASS)
8882 : : {
8883 : 290 : allocatable = CLASS_DATA (c)->attr.allocatable;
8884 : 290 : pointer = CLASS_DATA (c)->attr.class_pointer;
8885 : : }
8886 : : else
8887 : : {
8888 : 1756 : allocatable = c->attr.allocatable;
8889 : 1756 : pointer = c->attr.pointer;
8890 : : }
8891 : : break;
8892 : :
8893 : : case REF_SUBSTRING:
8894 : : case REF_INQUIRY:
8895 : 489 : allocatable = 0;
8896 : : break;
8897 : : }
8898 : : }
8899 : :
8900 : 8027 : attr = gfc_expr_attr (e);
8901 : :
8902 : 8027 : if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8903 : : {
8904 : 3 : bad:
8905 : 3 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8906 : : &e->where);
8907 : 3 : return false;
8908 : : }
8909 : :
8910 : : /* F2008, C644. */
8911 : 8024 : if (gfc_is_coindexed (e))
8912 : : {
8913 : 1 : gfc_error ("Coindexed allocatable object at %L", &e->where);
8914 : 1 : return false;
8915 : : }
8916 : :
8917 : 8023 : if (pointer
8918 : 10361 : && !gfc_check_vardef_context (e, true, true, false,
8919 : 2338 : _("DEALLOCATE object")))
8920 : : return false;
8921 : 8021 : if (!gfc_check_vardef_context (e, false, true, false,
8922 : 8021 : _("DEALLOCATE object")))
8923 : : return false;
8924 : :
8925 : : return true;
8926 : : }
8927 : :
8928 : :
8929 : : /* Returns true if the expression e contains a reference to the symbol sym. */
8930 : : static bool
8931 : 46617 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8932 : : {
8933 : 46617 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8934 : 2081 : return true;
8935 : :
8936 : : return false;
8937 : : }
8938 : :
8939 : : bool
8940 : 20027 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8941 : : {
8942 : 20027 : return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8943 : : }
8944 : :
8945 : : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
8946 : : of character expressions. */
8947 : : static bool
8948 : 20205 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
8949 : : {
8950 : 0 : return gfc_traverse_expr (e, sym, sym_in_expr, -1);
8951 : : }
8952 : :
8953 : :
8954 : : /* Given the expression node e for an allocatable/pointer of derived type to be
8955 : : allocated, get the expression node to be initialized afterwards (needed for
8956 : : derived types with default initializers, and derived types with allocatable
8957 : : components that need nullification.) */
8958 : :
8959 : : gfc_expr *
8960 : 5528 : gfc_expr_to_initialize (gfc_expr *e)
8961 : : {
8962 : 5528 : gfc_expr *result;
8963 : 5528 : gfc_ref *ref;
8964 : 5528 : int i;
8965 : :
8966 : 5528 : result = gfc_copy_expr (e);
8967 : :
8968 : : /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8969 : 10855 : for (ref = result->ref; ref; ref = ref->next)
8970 : 8491 : if (ref->type == REF_ARRAY && ref->next == NULL)
8971 : : {
8972 : 3164 : if (ref->u.ar.dimen == 0
8973 : 64 : && ref->u.ar.as && ref->u.ar.as->corank)
8974 : : return result;
8975 : :
8976 : 3100 : ref->u.ar.type = AR_FULL;
8977 : :
8978 : 7041 : for (i = 0; i < ref->u.ar.dimen; i++)
8979 : 3941 : ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8980 : :
8981 : : break;
8982 : : }
8983 : :
8984 : 5464 : gfc_free_shape (&result->shape, result->rank);
8985 : :
8986 : : /* Recalculate rank, shape, etc. */
8987 : 5464 : gfc_resolve_expr (result);
8988 : 5464 : return result;
8989 : : }
8990 : :
8991 : :
8992 : : /* If the last ref of an expression is an array ref, return a copy of the
8993 : : expression with that one removed. Otherwise, a copy of the original
8994 : : expression. This is used for allocate-expressions and pointer assignment
8995 : : LHS, where there may be an array specification that needs to be stripped
8996 : : off when using gfc_check_vardef_context. */
8997 : :
8998 : : static gfc_expr*
8999 : 26971 : remove_last_array_ref (gfc_expr* e)
9000 : : {
9001 : 26971 : gfc_expr* e2;
9002 : 26971 : gfc_ref** r;
9003 : :
9004 : 26971 : e2 = gfc_copy_expr (e);
9005 : 34432 : for (r = &e2->ref; *r; r = &(*r)->next)
9006 : 23281 : if ((*r)->type == REF_ARRAY && !(*r)->next)
9007 : : {
9008 : 15820 : gfc_free_ref_list (*r);
9009 : 15820 : *r = NULL;
9010 : 15820 : break;
9011 : : }
9012 : :
9013 : 26971 : return e2;
9014 : : }
9015 : :
9016 : :
9017 : : /* Used in resolve_allocate_expr to check that a allocation-object and
9018 : : a source-expr are conformable. This does not catch all possible
9019 : : cases; in particular a runtime checking is needed. */
9020 : :
9021 : : static bool
9022 : 1883 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
9023 : : {
9024 : 1883 : gfc_ref *tail;
9025 : 1883 : bool scalar;
9026 : :
9027 : 2602 : for (tail = e2->ref; tail && tail->next; tail = tail->next);
9028 : :
9029 : : /* If MOLD= is present and is not scalar, and the allocate-object has an
9030 : : explicit-shape-spec, the ranks need not agree. This may be unintended,
9031 : : so let's emit a warning if -Wsurprising is given. */
9032 : 1883 : scalar = !tail || tail->type == REF_COMPONENT;
9033 : 1883 : if (e1->mold && e1->rank > 0
9034 : 163 : && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
9035 : : {
9036 : 26 : if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
9037 : 15 : gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
9038 : : "but MOLD= expression at %L has rank %d",
9039 : 6 : &e2->where, scalar ? 0 : tail->u.ar.as->rank,
9040 : : &e1->where, e1->rank);
9041 : 29 : return true;
9042 : : }
9043 : :
9044 : : /* First compare rank. */
9045 : 1854 : if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
9046 : 2 : || (!tail && e1->rank != e2->rank))
9047 : : {
9048 : 7 : gfc_error ("Source-expr at %L must be scalar or have the "
9049 : : "same rank as the allocate-object at %L",
9050 : : &e1->where, &e2->where);
9051 : 7 : return false;
9052 : : }
9053 : :
9054 : 1847 : if (e1->shape)
9055 : : {
9056 : 1354 : int i;
9057 : 1354 : mpz_t s;
9058 : :
9059 : 1354 : mpz_init (s);
9060 : :
9061 : 3124 : for (i = 0; i < e1->rank; i++)
9062 : : {
9063 : 1360 : if (tail->u.ar.start[i] == NULL)
9064 : : break;
9065 : :
9066 : 416 : if (tail->u.ar.end[i])
9067 : : {
9068 : 54 : mpz_set (s, tail->u.ar.end[i]->value.integer);
9069 : 54 : mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
9070 : 54 : mpz_add_ui (s, s, 1);
9071 : : }
9072 : : else
9073 : : {
9074 : 362 : mpz_set (s, tail->u.ar.start[i]->value.integer);
9075 : : }
9076 : :
9077 : 416 : if (mpz_cmp (e1->shape[i], s) != 0)
9078 : : {
9079 : 0 : gfc_error ("Source-expr at %L and allocate-object at %L must "
9080 : : "have the same shape", &e1->where, &e2->where);
9081 : 0 : mpz_clear (s);
9082 : 0 : return false;
9083 : : }
9084 : : }
9085 : :
9086 : 1354 : mpz_clear (s);
9087 : : }
9088 : :
9089 : : return true;
9090 : : }
9091 : :
9092 : :
9093 : : /* Resolve the expression in an ALLOCATE statement, doing the additional
9094 : : checks to see whether the expression is OK or not. The expression must
9095 : : have a trailing array reference that gives the size of the array. */
9096 : :
9097 : : static bool
9098 : 16781 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
9099 : : {
9100 : 16781 : int i, pointer, allocatable, dimension, is_abstract;
9101 : 16781 : int codimension;
9102 : 16781 : bool coindexed;
9103 : 16781 : bool unlimited;
9104 : 16781 : symbol_attribute attr;
9105 : 16781 : gfc_ref *ref, *ref2;
9106 : 16781 : gfc_expr *e2;
9107 : 16781 : gfc_array_ref *ar;
9108 : 16781 : gfc_symbol *sym = NULL;
9109 : 16781 : gfc_alloc *a;
9110 : 16781 : gfc_component *c;
9111 : 16781 : bool t;
9112 : :
9113 : : /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
9114 : : checking of coarrays. */
9115 : 21022 : for (ref = e->ref; ref; ref = ref->next)
9116 : 16918 : if (ref->next == NULL)
9117 : : break;
9118 : :
9119 : 16781 : if (ref && ref->type == REF_ARRAY)
9120 : 11541 : ref->u.ar.in_allocate = true;
9121 : :
9122 : 16781 : if (!gfc_resolve_expr (e))
9123 : 1 : goto failure;
9124 : :
9125 : : /* Make sure the expression is allocatable or a pointer. If it is
9126 : : pointer, the next-to-last reference must be a pointer. */
9127 : :
9128 : 16780 : ref2 = NULL;
9129 : 16780 : if (e->symtree)
9130 : 16780 : sym = e->symtree->n.sym;
9131 : :
9132 : : /* Check whether ultimate component is abstract and CLASS. */
9133 : 33560 : is_abstract = 0;
9134 : :
9135 : : /* Is the allocate-object unlimited polymorphic? */
9136 : 16780 : unlimited = UNLIMITED_POLY(e);
9137 : :
9138 : 16780 : if (e->expr_type != EXPR_VARIABLE)
9139 : : {
9140 : 0 : allocatable = 0;
9141 : 0 : attr = gfc_expr_attr (e);
9142 : 0 : pointer = attr.pointer;
9143 : 0 : dimension = attr.dimension;
9144 : 0 : codimension = attr.codimension;
9145 : : }
9146 : : else
9147 : : {
9148 : 16780 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
9149 : : {
9150 : 3314 : allocatable = CLASS_DATA (sym)->attr.allocatable;
9151 : 3314 : pointer = CLASS_DATA (sym)->attr.class_pointer;
9152 : 3314 : dimension = CLASS_DATA (sym)->attr.dimension;
9153 : 3314 : codimension = CLASS_DATA (sym)->attr.codimension;
9154 : 3314 : is_abstract = CLASS_DATA (sym)->attr.abstract;
9155 : : }
9156 : : else
9157 : : {
9158 : 13466 : allocatable = sym->attr.allocatable;
9159 : 13466 : pointer = sym->attr.pointer;
9160 : 13466 : dimension = sym->attr.dimension;
9161 : 13466 : codimension = sym->attr.codimension;
9162 : : }
9163 : :
9164 : 16780 : coindexed = false;
9165 : :
9166 : 33692 : for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
9167 : : {
9168 : 16914 : switch (ref->type)
9169 : : {
9170 : 12833 : case REF_ARRAY:
9171 : 12833 : if (ref->u.ar.codimen > 0)
9172 : : {
9173 : 659 : int n;
9174 : 940 : for (n = ref->u.ar.dimen;
9175 : 940 : n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
9176 : 696 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
9177 : : {
9178 : : coindexed = true;
9179 : : break;
9180 : : }
9181 : : }
9182 : :
9183 : 12833 : if (ref->next != NULL)
9184 : 1294 : pointer = 0;
9185 : : break;
9186 : :
9187 : 4081 : case REF_COMPONENT:
9188 : : /* F2008, C644. */
9189 : 4081 : if (coindexed)
9190 : : {
9191 : 2 : gfc_error ("Coindexed allocatable object at %L",
9192 : : &e->where);
9193 : 2 : goto failure;
9194 : : }
9195 : :
9196 : 4079 : c = ref->u.c.component;
9197 : 4079 : if (c->ts.type == BT_CLASS)
9198 : : {
9199 : 977 : allocatable = CLASS_DATA (c)->attr.allocatable;
9200 : 977 : pointer = CLASS_DATA (c)->attr.class_pointer;
9201 : 977 : dimension = CLASS_DATA (c)->attr.dimension;
9202 : 977 : codimension = CLASS_DATA (c)->attr.codimension;
9203 : 977 : is_abstract = CLASS_DATA (c)->attr.abstract;
9204 : : }
9205 : : else
9206 : : {
9207 : 3102 : allocatable = c->attr.allocatable;
9208 : 3102 : pointer = c->attr.pointer;
9209 : 3102 : dimension = c->attr.dimension;
9210 : 3102 : codimension = c->attr.codimension;
9211 : 3102 : is_abstract = c->attr.abstract;
9212 : : }
9213 : : break;
9214 : :
9215 : 0 : case REF_SUBSTRING:
9216 : 0 : case REF_INQUIRY:
9217 : 0 : allocatable = 0;
9218 : 0 : pointer = 0;
9219 : 0 : break;
9220 : : }
9221 : : }
9222 : : }
9223 : :
9224 : : /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
9225 : : pointer or an allocatable variable. */
9226 : 16778 : if (allocatable == 0 && pointer == 0)
9227 : : {
9228 : 4 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
9229 : : &e->where);
9230 : 4 : goto failure;
9231 : : }
9232 : :
9233 : : /* Some checks for the SOURCE tag. */
9234 : 16774 : if (code->expr3)
9235 : : {
9236 : : /* Check F03:C632: "The source-expr shall be a scalar or have the same
9237 : : rank as allocate-object". This would require the MOLD argument to
9238 : : NULL() as source-expr for subsequent checking. However, even the
9239 : : resulting disassociated pointer or unallocated array has no shape that
9240 : : could be used for SOURCE= or MOLD=. */
9241 : 3771 : if (code->expr3->expr_type == EXPR_NULL)
9242 : : {
9243 : 4 : gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
9244 : : &code->expr3->where);
9245 : 4 : goto failure;
9246 : : }
9247 : :
9248 : : /* Check F03:C631. */
9249 : 3767 : if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
9250 : : {
9251 : 10 : gfc_error ("Type of entity at %L is type incompatible with "
9252 : 10 : "source-expr at %L", &e->where, &code->expr3->where);
9253 : 10 : goto failure;
9254 : : }
9255 : :
9256 : : /* Check F03:C632 and restriction following Note 6.18. */
9257 : 3757 : if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
9258 : 7 : goto failure;
9259 : :
9260 : : /* Check F03:C633. */
9261 : 3750 : if (code->expr3->ts.kind != e->ts.kind && !unlimited)
9262 : : {
9263 : 1 : gfc_error ("The allocate-object at %L and the source-expr at %L "
9264 : : "shall have the same kind type parameter",
9265 : : &e->where, &code->expr3->where);
9266 : 1 : goto failure;
9267 : : }
9268 : :
9269 : : /* Check F2008, C642. */
9270 : 3749 : if (code->expr3->ts.type == BT_DERIVED
9271 : 3749 : && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
9272 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9273 : : == INTMOD_ISO_FORTRAN_ENV
9274 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9275 : : == ISOFORTRAN_LOCK_TYPE)))
9276 : : {
9277 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9278 : : "LOCK_TYPE nor have a LOCK_TYPE component if "
9279 : : "allocate-object at %L is a coarray",
9280 : 0 : &code->expr3->where, &e->where);
9281 : 0 : goto failure;
9282 : : }
9283 : :
9284 : : /* Check F2008:C639: "Corresponding kind type parameters of
9285 : : allocate-object and source-expr shall have the same values." */
9286 : 3749 : if (e->ts.type == BT_CHARACTER
9287 : 806 : && !e->ts.deferred
9288 : 162 : && e->ts.u.cl->length
9289 : 162 : && code->expr3->ts.type == BT_CHARACTER
9290 : 3911 : && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9291 : : "SOURCE= or MOLD= specifier"))
9292 : 17 : goto failure;
9293 : :
9294 : : /* Check TS18508, C702/C703. */
9295 : 3732 : if (code->expr3->ts.type == BT_DERIVED
9296 : 4895 : && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9297 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9298 : : == INTMOD_ISO_FORTRAN_ENV
9299 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9300 : : == ISOFORTRAN_EVENT_TYPE)))
9301 : : {
9302 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9303 : : "EVENT_TYPE nor have a EVENT_TYPE component if "
9304 : : "allocate-object at %L is a coarray",
9305 : 0 : &code->expr3->where, &e->where);
9306 : 0 : goto failure;
9307 : : }
9308 : : }
9309 : :
9310 : : /* Check F08:C629. */
9311 : 16735 : if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9312 : 153 : && !code->expr3)
9313 : : {
9314 : 2 : gcc_assert (e->ts.type == BT_CLASS);
9315 : 2 : gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9316 : : "type-spec or source-expr", sym->name, &e->where);
9317 : 2 : goto failure;
9318 : : }
9319 : :
9320 : : /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9321 : : if and only if each allocate-object is a dummy argument for which the
9322 : : corresponding type parameter is assumed. */
9323 : 16733 : if (code->ext.alloc.ts.type == BT_CHARACTER
9324 : 501 : && code->ext.alloc.ts.u.cl->length != NULL
9325 : 486 : && e->ts.type == BT_CHARACTER && !e->ts.deferred
9326 : 23 : && e->ts.u.cl->length == NULL
9327 : 2 : && e->symtree->n.sym->attr.dummy)
9328 : : {
9329 : 2 : gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9330 : : "shall be an asterisk as allocate object %qs at %L is a "
9331 : : "dummy argument with assumed type parameter",
9332 : : sym->name, &e->where);
9333 : 2 : goto failure;
9334 : : }
9335 : :
9336 : : /* Check F08:C632. */
9337 : 16731 : if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9338 : 60 : && !UNLIMITED_POLY (e))
9339 : : {
9340 : 36 : int cmp;
9341 : :
9342 : 36 : if (!e->ts.u.cl->length)
9343 : 15 : goto failure;
9344 : :
9345 : 42 : cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9346 : 21 : code->ext.alloc.ts.u.cl->length);
9347 : 21 : if (cmp == 1 || cmp == -1 || cmp == -3)
9348 : : {
9349 : 2 : gfc_error ("Allocating %s at %L with type-spec requires the same "
9350 : : "character-length parameter as in the declaration",
9351 : : sym->name, &e->where);
9352 : 2 : goto failure;
9353 : : }
9354 : : }
9355 : :
9356 : : /* In the variable definition context checks, gfc_expr_attr is used
9357 : : on the expression. This is fooled by the array specification
9358 : : present in e, thus we have to eliminate that one temporarily. */
9359 : 16714 : e2 = remove_last_array_ref (e);
9360 : 16714 : t = true;
9361 : 16714 : if (t && pointer)
9362 : 3795 : t = gfc_check_vardef_context (e2, true, true, false,
9363 : 3795 : _("ALLOCATE object"));
9364 : 3795 : if (t)
9365 : 16706 : t = gfc_check_vardef_context (e2, false, true, false,
9366 : 16706 : _("ALLOCATE object"));
9367 : 16714 : gfc_free_expr (e2);
9368 : 16714 : if (!t)
9369 : 11 : goto failure;
9370 : :
9371 : 16703 : code->ext.alloc.expr3_not_explicit = 0;
9372 : 16703 : if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9373 : 1574 : && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9374 : : {
9375 : : /* For class arrays, the initialization with SOURCE is done
9376 : : using _copy and trans_call. It is convenient to exploit that
9377 : : when the allocated type is different from the declared type but
9378 : : no SOURCE exists by setting expr3. */
9379 : 284 : code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9380 : 284 : code->ext.alloc.expr3_not_explicit = 1;
9381 : : }
9382 : 16419 : else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9383 : 2403 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9384 : 5 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9385 : : {
9386 : : /* We have to zero initialize the integer variable. */
9387 : 1 : code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9388 : 1 : code->ext.alloc.expr3_not_explicit = 1;
9389 : : }
9390 : :
9391 : 16703 : if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9392 : : {
9393 : : /* Make sure the vtab symbol is present when
9394 : : the module variables are generated. */
9395 : 2917 : gfc_typespec ts = e->ts;
9396 : 2917 : if (code->expr3)
9397 : 1305 : ts = code->expr3->ts;
9398 : 1612 : else if (code->ext.alloc.ts.type == BT_DERIVED)
9399 : 707 : ts = code->ext.alloc.ts;
9400 : :
9401 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9402 : : statement is necessary. */
9403 : 2917 : gfc_find_derived_vtab (ts.u.derived);
9404 : 2917 : }
9405 : 13786 : else if (unlimited && !UNLIMITED_POLY (code->expr3))
9406 : : {
9407 : : /* Again, make sure the vtab symbol is present when
9408 : : the module variables are generated. */
9409 : 433 : gfc_typespec *ts = NULL;
9410 : 433 : if (code->expr3)
9411 : 347 : ts = &code->expr3->ts;
9412 : : else
9413 : 86 : ts = &code->ext.alloc.ts;
9414 : :
9415 : 433 : gcc_assert (ts);
9416 : :
9417 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9418 : : statement is necessary. */
9419 : 433 : gfc_find_vtab (ts);
9420 : : }
9421 : :
9422 : 16703 : if (dimension == 0 && codimension == 0)
9423 : 5193 : goto success;
9424 : :
9425 : : /* Make sure the last reference node is an array specification. */
9426 : :
9427 : 11510 : if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9428 : 10300 : || (dimension && ref2->u.ar.dimen == 0))
9429 : : {
9430 : : /* F08:C633. */
9431 : 1210 : if (code->expr3)
9432 : : {
9433 : 1209 : if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9434 : : "in ALLOCATE statement at %L", &e->where))
9435 : 0 : goto failure;
9436 : 1209 : if (code->expr3->rank != 0)
9437 : 1208 : *array_alloc_wo_spec = true;
9438 : : else
9439 : : {
9440 : 1 : gfc_error ("Array specification or array-valued SOURCE= "
9441 : : "expression required in ALLOCATE statement at %L",
9442 : : &e->where);
9443 : 1 : goto failure;
9444 : : }
9445 : : }
9446 : : else
9447 : : {
9448 : 1 : gfc_error ("Array specification required in ALLOCATE statement "
9449 : : "at %L", &e->where);
9450 : 1 : goto failure;
9451 : : }
9452 : : }
9453 : :
9454 : : /* Make sure that the array section reference makes sense in the
9455 : : context of an ALLOCATE specification. */
9456 : :
9457 : 11508 : ar = &ref2->u.ar;
9458 : :
9459 : 11508 : if (codimension)
9460 : 982 : for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9461 : : {
9462 : 580 : switch (ar->dimen_type[i])
9463 : : {
9464 : 2 : case DIMEN_THIS_IMAGE:
9465 : 2 : gfc_error ("Coarray specification required in ALLOCATE statement "
9466 : : "at %L", &e->where);
9467 : 2 : goto failure;
9468 : :
9469 : 83 : case DIMEN_RANGE:
9470 : : /* F2018:R937:
9471 : : * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9472 : : */
9473 : 83 : if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9474 : : {
9475 : 8 : gfc_error ("Bad coarray specification in ALLOCATE statement "
9476 : : "at %L", &e->where);
9477 : 8 : goto failure;
9478 : : }
9479 : 75 : else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9480 : : {
9481 : 2 : gfc_error ("Upper cobound is less than lower cobound at %L",
9482 : 2 : &ar->start[i]->where);
9483 : 2 : goto failure;
9484 : : }
9485 : : break;
9486 : :
9487 : 93 : case DIMEN_ELEMENT:
9488 : 93 : if (ar->start[i]->expr_type == EXPR_CONSTANT)
9489 : : {
9490 : 85 : gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9491 : 85 : if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9492 : : {
9493 : 1 : gfc_error ("Upper cobound is less than lower cobound "
9494 : : "of 1 at %L", &ar->start[i]->where);
9495 : 1 : goto failure;
9496 : : }
9497 : : }
9498 : : break;
9499 : :
9500 : : case DIMEN_STAR:
9501 : : break;
9502 : :
9503 : 0 : default:
9504 : 0 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9505 : : &e->where);
9506 : 0 : goto failure;
9507 : :
9508 : : }
9509 : : }
9510 : 28381 : for (i = 0; i < ar->dimen; i++)
9511 : : {
9512 : 16890 : if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9513 : 14218 : goto check_symbols;
9514 : :
9515 : 2672 : switch (ar->dimen_type[i])
9516 : : {
9517 : : case DIMEN_ELEMENT:
9518 : : break;
9519 : :
9520 : 2407 : case DIMEN_RANGE:
9521 : 2407 : if (ar->start[i] != NULL
9522 : 2407 : && ar->end[i] != NULL
9523 : 2406 : && ar->stride[i] == NULL)
9524 : : break;
9525 : :
9526 : : /* Fall through. */
9527 : :
9528 : 1 : case DIMEN_UNKNOWN:
9529 : 1 : case DIMEN_VECTOR:
9530 : 1 : case DIMEN_STAR:
9531 : 1 : case DIMEN_THIS_IMAGE:
9532 : 1 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9533 : : &e->where);
9534 : 1 : goto failure;
9535 : : }
9536 : :
9537 : 2406 : check_symbols:
9538 : 44120 : for (a = code->ext.alloc.list; a; a = a->next)
9539 : : {
9540 : 27234 : sym = a->expr->symtree->n.sym;
9541 : :
9542 : : /* TODO - check derived type components. */
9543 : 27234 : if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9544 : 8983 : continue;
9545 : :
9546 : 18251 : if ((ar->start[i] != NULL
9547 : 17573 : && gfc_find_var_in_expr (sym, ar->start[i]))
9548 : 35821 : || (ar->end[i] != NULL
9549 : 2632 : && gfc_find_var_in_expr (sym, ar->end[i])))
9550 : : {
9551 : 3 : gfc_error ("%qs must not appear in the array specification at "
9552 : : "%L in the same ALLOCATE statement where it is "
9553 : : "itself allocated", sym->name, &ar->where);
9554 : 3 : goto failure;
9555 : : }
9556 : : }
9557 : : }
9558 : :
9559 : 11655 : for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9560 : : {
9561 : 729 : if (ar->dimen_type[i] == DIMEN_ELEMENT
9562 : 565 : || ar->dimen_type[i] == DIMEN_RANGE)
9563 : : {
9564 : 164 : if (i == (ar->dimen + ar->codimen - 1))
9565 : : {
9566 : 0 : gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9567 : : "statement at %L", &e->where);
9568 : 0 : goto failure;
9569 : : }
9570 : 164 : continue;
9571 : : }
9572 : :
9573 : 401 : if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9574 : 401 : && ar->stride[i] == NULL)
9575 : : break;
9576 : :
9577 : 0 : gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9578 : : &e->where);
9579 : 0 : goto failure;
9580 : : }
9581 : :
9582 : 11491 : success:
9583 : : return true;
9584 : :
9585 : : failure:
9586 : : return false;
9587 : : }
9588 : :
9589 : :
9590 : : static void
9591 : 19583 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9592 : : {
9593 : 19583 : gfc_expr *stat, *errmsg, *pe, *qe;
9594 : 19583 : gfc_alloc *a, *p, *q;
9595 : :
9596 : 19583 : stat = code->expr1;
9597 : 19583 : errmsg = code->expr2;
9598 : :
9599 : : /* Check the stat variable. */
9600 : 19583 : if (stat)
9601 : : {
9602 : 643 : if (!gfc_check_vardef_context (stat, false, false, false,
9603 : 643 : _("STAT variable")))
9604 : 8 : goto done_stat;
9605 : :
9606 : 635 : if (stat->ts.type != BT_INTEGER
9607 : 626 : || stat->rank > 0)
9608 : 11 : gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9609 : : "variable", &stat->where);
9610 : :
9611 : 635 : if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9612 : 0 : goto done_stat;
9613 : :
9614 : : /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9615 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9616 : : */
9617 : 1315 : for (p = code->ext.alloc.list; p; p = p->next)
9618 : 687 : if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9619 : : {
9620 : 9 : gfc_ref *ref1, *ref2;
9621 : 9 : bool found = true;
9622 : :
9623 : 16 : for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9624 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9625 : : {
9626 : 9 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9627 : 5 : continue;
9628 : 4 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9629 : : {
9630 : : found = false;
9631 : : break;
9632 : : }
9633 : : }
9634 : :
9635 : 9 : if (found)
9636 : : {
9637 : 7 : gfc_error ("Stat-variable at %L shall not be %sd within "
9638 : : "the same %s statement", &stat->where, fcn, fcn);
9639 : 7 : break;
9640 : : }
9641 : : }
9642 : : }
9643 : :
9644 : 18940 : done_stat:
9645 : :
9646 : : /* Check the errmsg variable. */
9647 : 19583 : if (errmsg)
9648 : : {
9649 : 146 : if (!stat)
9650 : 2 : gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9651 : : &errmsg->where);
9652 : :
9653 : 146 : if (!gfc_check_vardef_context (errmsg, false, false, false,
9654 : 146 : _("ERRMSG variable")))
9655 : 6 : goto done_errmsg;
9656 : :
9657 : : /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9658 : : F18:R930 errmsg-variable is scalar-default-char-variable
9659 : : F18:R906 default-char-variable is variable
9660 : : F18:C906 default-char-variable shall be default character. */
9661 : 140 : if (errmsg->ts.type != BT_CHARACTER
9662 : 138 : || errmsg->rank > 0
9663 : 137 : || errmsg->ts.kind != gfc_default_character_kind)
9664 : 4 : gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9665 : : "variable", &errmsg->where);
9666 : :
9667 : 140 : if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9668 : 0 : goto done_errmsg;
9669 : :
9670 : : /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9671 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9672 : : */
9673 : 278 : for (p = code->ext.alloc.list; p; p = p->next)
9674 : 143 : if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9675 : : {
9676 : 9 : gfc_ref *ref1, *ref2;
9677 : 9 : bool found = true;
9678 : :
9679 : 16 : for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9680 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9681 : : {
9682 : 11 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9683 : 4 : continue;
9684 : 7 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9685 : : {
9686 : : found = false;
9687 : : break;
9688 : : }
9689 : : }
9690 : :
9691 : 9 : if (found)
9692 : : {
9693 : 5 : gfc_error ("Errmsg-variable at %L shall not be %sd within "
9694 : : "the same %s statement", &errmsg->where, fcn, fcn);
9695 : 5 : break;
9696 : : }
9697 : : }
9698 : : }
9699 : :
9700 : 19437 : done_errmsg:
9701 : :
9702 : : /* Check that an allocate-object appears only once in the statement. */
9703 : :
9704 : 44391 : for (p = code->ext.alloc.list; p; p = p->next)
9705 : : {
9706 : 24808 : pe = p->expr;
9707 : 33993 : for (q = p->next; q; q = q->next)
9708 : : {
9709 : 9185 : qe = q->expr;
9710 : 9185 : if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9711 : : {
9712 : : /* This is a potential collision. */
9713 : 2093 : gfc_ref *pr = pe->ref;
9714 : 2093 : gfc_ref *qr = qe->ref;
9715 : :
9716 : : /* Follow the references until
9717 : : a) They start to differ, in which case there is no error;
9718 : : you can deallocate a%b and a%c in a single statement
9719 : : b) Both of them stop, which is an error
9720 : : c) One of them stops, which is also an error. */
9721 : 4517 : while (1)
9722 : : {
9723 : 3305 : if (pr == NULL && qr == NULL)
9724 : : {
9725 : 7 : gfc_error ("Allocate-object at %L also appears at %L",
9726 : : &pe->where, &qe->where);
9727 : 7 : break;
9728 : : }
9729 : 3298 : else if (pr != NULL && qr == NULL)
9730 : : {
9731 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9732 : : " object at %L", &pe->where, &qe->where);
9733 : 2 : break;
9734 : : }
9735 : 3296 : else if (pr == NULL && qr != NULL)
9736 : : {
9737 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9738 : : " object at %L", &qe->where, &pe->where);
9739 : 2 : break;
9740 : : }
9741 : : /* Here, pr != NULL && qr != NULL */
9742 : 3294 : gcc_assert(pr->type == qr->type);
9743 : 3294 : if (pr->type == REF_ARRAY)
9744 : : {
9745 : : /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9746 : : which are legal. */
9747 : 1065 : gcc_assert (qr->type == REF_ARRAY);
9748 : :
9749 : 1065 : if (pr->next && qr->next)
9750 : : {
9751 : : int i;
9752 : : gfc_array_ref *par = &(pr->u.ar);
9753 : : gfc_array_ref *qar = &(qr->u.ar);
9754 : :
9755 : 1840 : for (i=0; i<par->dimen; i++)
9756 : : {
9757 : 954 : if ((par->start[i] != NULL
9758 : 0 : || qar->start[i] != NULL)
9759 : 1908 : && gfc_dep_compare_expr (par->start[i],
9760 : 954 : qar->start[i]) != 0)
9761 : 168 : goto break_label;
9762 : : }
9763 : : }
9764 : : }
9765 : : else
9766 : : {
9767 : 2229 : if (pr->u.c.component->name != qr->u.c.component->name)
9768 : : break;
9769 : : }
9770 : :
9771 : 1212 : pr = pr->next;
9772 : 1212 : qr = qr->next;
9773 : 1212 : }
9774 : 9185 : break_label:
9775 : : ;
9776 : : }
9777 : : }
9778 : : }
9779 : :
9780 : 19583 : if (strcmp (fcn, "ALLOCATE") == 0)
9781 : : {
9782 : 13782 : bool arr_alloc_wo_spec = false;
9783 : :
9784 : : /* Resolving the expr3 in the loop over all objects to allocate would
9785 : : execute loop invariant code for each loop item. Therefore do it just
9786 : : once here. */
9787 : 13782 : if (code->expr3 && code->expr3->mold
9788 : 343 : && code->expr3->ts.type == BT_DERIVED)
9789 : : {
9790 : : /* Default initialization via MOLD (non-polymorphic). */
9791 : 20 : gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9792 : 20 : if (rhs != NULL)
9793 : : {
9794 : 7 : gfc_resolve_expr (rhs);
9795 : 7 : gfc_free_expr (code->expr3);
9796 : 7 : code->expr3 = rhs;
9797 : : }
9798 : : }
9799 : 30563 : for (a = code->ext.alloc.list; a; a = a->next)
9800 : 16781 : resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9801 : :
9802 : 13782 : if (arr_alloc_wo_spec && code->expr3)
9803 : : {
9804 : : /* Mark the allocate to have to take the array specification
9805 : : from the expr3. */
9806 : 1202 : code->ext.alloc.arr_spec_from_expr3 = 1;
9807 : : }
9808 : : }
9809 : : else
9810 : : {
9811 : 13828 : for (a = code->ext.alloc.list; a; a = a->next)
9812 : 8027 : resolve_deallocate_expr (a->expr);
9813 : : }
9814 : 19583 : }
9815 : :
9816 : :
9817 : : /************ SELECT CASE resolution subroutines ************/
9818 : :
9819 : : /* Callback function for our mergesort variant. Determines interval
9820 : : overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9821 : : op1 > op2. Assumes we're not dealing with the default case.
9822 : : We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9823 : : There are nine situations to check. */
9824 : :
9825 : : static int
9826 : 1542 : compare_cases (const gfc_case *op1, const gfc_case *op2)
9827 : : {
9828 : 1542 : int retval;
9829 : :
9830 : 1542 : if (op1->low == NULL) /* op1 = (:L) */
9831 : : {
9832 : : /* op2 = (:N), so overlap. */
9833 : 52 : retval = 0;
9834 : : /* op2 = (M:) or (M:N), L < M */
9835 : 52 : if (op2->low != NULL
9836 : 52 : && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9837 : : retval = -1;
9838 : : }
9839 : 1490 : else if (op1->high == NULL) /* op1 = (K:) */
9840 : : {
9841 : : /* op2 = (M:), so overlap. */
9842 : 10 : retval = 0;
9843 : : /* op2 = (:N) or (M:N), K > N */
9844 : 10 : if (op2->high != NULL
9845 : 10 : && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9846 : : retval = 1;
9847 : : }
9848 : : else /* op1 = (K:L) */
9849 : : {
9850 : 1480 : if (op2->low == NULL) /* op2 = (:N), K > N */
9851 : 18 : retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9852 : 18 : ? 1 : 0;
9853 : 1462 : else if (op2->high == NULL) /* op2 = (M:), L < M */
9854 : 14 : retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9855 : 10 : ? -1 : 0;
9856 : : else /* op2 = (M:N) */
9857 : : {
9858 : 1452 : retval = 0;
9859 : : /* L < M */
9860 : 1452 : if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9861 : : retval = -1;
9862 : : /* K > N */
9863 : 412 : else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9864 : 438 : retval = 1;
9865 : : }
9866 : : }
9867 : :
9868 : 1542 : return retval;
9869 : : }
9870 : :
9871 : :
9872 : : /* Merge-sort a double linked case list, detecting overlap in the
9873 : : process. LIST is the head of the double linked case list before it
9874 : : is sorted. Returns the head of the sorted list if we don't see any
9875 : : overlap, or NULL otherwise. */
9876 : :
9877 : : static gfc_case *
9878 : 642 : check_case_overlap (gfc_case *list)
9879 : : {
9880 : 642 : gfc_case *p, *q, *e, *tail;
9881 : 642 : int insize, nmerges, psize, qsize, cmp, overlap_seen;
9882 : :
9883 : : /* If the passed list was empty, return immediately. */
9884 : 642 : if (!list)
9885 : : return NULL;
9886 : :
9887 : : overlap_seen = 0;
9888 : : insize = 1;
9889 : :
9890 : : /* Loop unconditionally. The only exit from this loop is a return
9891 : : statement, when we've finished sorting the case list. */
9892 : 1334 : for (;;)
9893 : : {
9894 : 988 : p = list;
9895 : 988 : list = NULL;
9896 : 988 : tail = NULL;
9897 : :
9898 : : /* Count the number of merges we do in this pass. */
9899 : 988 : nmerges = 0;
9900 : :
9901 : : /* Loop while there exists a merge to be done. */
9902 : 2491 : while (p)
9903 : : {
9904 : 1503 : int i;
9905 : :
9906 : : /* Count this merge. */
9907 : 1503 : nmerges++;
9908 : :
9909 : : /* Cut the list in two pieces by stepping INSIZE places
9910 : : forward in the list, starting from P. */
9911 : 1503 : psize = 0;
9912 : 1503 : q = p;
9913 : 3150 : for (i = 0; i < insize; i++)
9914 : : {
9915 : 2206 : psize++;
9916 : 2206 : q = q->right;
9917 : 2206 : if (!q)
9918 : : break;
9919 : : }
9920 : : qsize = insize;
9921 : :
9922 : : /* Now we have two lists. Merge them! */
9923 : 4918 : while (psize > 0 || (qsize > 0 && q != NULL))
9924 : : {
9925 : : /* See from which the next case to merge comes from. */
9926 : 771 : if (psize == 0)
9927 : : {
9928 : : /* P is empty so the next case must come from Q. */
9929 : 771 : e = q;
9930 : 771 : q = q->right;
9931 : 771 : qsize--;
9932 : : }
9933 : 2644 : else if (qsize == 0 || q == NULL)
9934 : : {
9935 : : /* Q is empty. */
9936 : 1102 : e = p;
9937 : 1102 : p = p->right;
9938 : 1102 : psize--;
9939 : : }
9940 : : else
9941 : : {
9942 : 1542 : cmp = compare_cases (p, q);
9943 : 1542 : if (cmp < 0)
9944 : : {
9945 : : /* The whole case range for P is less than the
9946 : : one for Q. */
9947 : 1100 : e = p;
9948 : 1100 : p = p->right;
9949 : 1100 : psize--;
9950 : : }
9951 : 442 : else if (cmp > 0)
9952 : : {
9953 : : /* The whole case range for Q is greater than
9954 : : the case range for P. */
9955 : 438 : e = q;
9956 : 438 : q = q->right;
9957 : 438 : qsize--;
9958 : : }
9959 : : else
9960 : : {
9961 : : /* The cases overlap, or they are the same
9962 : : element in the list. Either way, we must
9963 : : issue an error and get the next case from P. */
9964 : : /* FIXME: Sort P and Q by line number. */
9965 : 4 : gfc_error ("CASE label at %L overlaps with CASE "
9966 : : "label at %L", &p->where, &q->where);
9967 : 4 : overlap_seen = 1;
9968 : 4 : e = p;
9969 : 4 : p = p->right;
9970 : 4 : psize--;
9971 : : }
9972 : : }
9973 : :
9974 : : /* Add the next element to the merged list. */
9975 : 3415 : if (tail)
9976 : 2427 : tail->right = e;
9977 : : else
9978 : : list = e;
9979 : 3415 : e->left = tail;
9980 : 3415 : tail = e;
9981 : : }
9982 : :
9983 : : /* P has now stepped INSIZE places along, and so has Q. So
9984 : : they're the same. */
9985 : : p = q;
9986 : : }
9987 : 988 : tail->right = NULL;
9988 : :
9989 : : /* If we have done only one merge or none at all, we've
9990 : : finished sorting the cases. */
9991 : 988 : if (nmerges <= 1)
9992 : : {
9993 : 642 : if (!overlap_seen)
9994 : : return list;
9995 : : else
9996 : : return NULL;
9997 : : }
9998 : :
9999 : : /* Otherwise repeat, merging lists twice the size. */
10000 : 346 : insize *= 2;
10001 : 346 : }
10002 : : }
10003 : :
10004 : :
10005 : : /* Check to see if an expression is suitable for use in a CASE statement.
10006 : : Makes sure that all case expressions are scalar constants of the same
10007 : : type. Return false if anything is wrong. */
10008 : :
10009 : : static bool
10010 : 3257 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
10011 : : {
10012 : 3257 : if (e == NULL) return true;
10013 : :
10014 : 3164 : if (e->ts.type != case_expr->ts.type)
10015 : : {
10016 : 4 : gfc_error ("Expression in CASE statement at %L must be of type %s",
10017 : : &e->where, gfc_basic_typename (case_expr->ts.type));
10018 : 4 : return false;
10019 : : }
10020 : :
10021 : : /* C805 (R808) For a given case-construct, each case-value shall be of
10022 : : the same type as case-expr. For character type, length differences
10023 : : are allowed, but the kind type parameters shall be the same. */
10024 : :
10025 : 3160 : if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
10026 : : {
10027 : 4 : gfc_error ("Expression in CASE statement at %L must be of kind %d",
10028 : : &e->where, case_expr->ts.kind);
10029 : 4 : return false;
10030 : : }
10031 : :
10032 : : /* Convert the case value kind to that of case expression kind,
10033 : : if needed */
10034 : :
10035 : 3156 : if (e->ts.kind != case_expr->ts.kind)
10036 : 14 : gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
10037 : :
10038 : 3156 : if (e->rank != 0)
10039 : : {
10040 : 0 : gfc_error ("Expression in CASE statement at %L must be scalar",
10041 : : &e->where);
10042 : 0 : return false;
10043 : : }
10044 : :
10045 : : return true;
10046 : : }
10047 : :
10048 : :
10049 : : /* Given a completely parsed select statement, we:
10050 : :
10051 : : - Validate all expressions and code within the SELECT.
10052 : : - Make sure that the selection expression is not of the wrong type.
10053 : : - Make sure that no case ranges overlap.
10054 : : - Eliminate unreachable cases and unreachable code resulting from
10055 : : removing case labels.
10056 : :
10057 : : The standard does allow unreachable cases, e.g. CASE (5:3). But
10058 : : they are a hassle for code generation, and to prevent that, we just
10059 : : cut them out here. This is not necessary for overlapping cases
10060 : : because they are illegal and we never even try to generate code.
10061 : :
10062 : : We have the additional caveat that a SELECT construct could have
10063 : : been a computed GOTO in the source code. Fortunately we can fairly
10064 : : easily work around that here: The case_expr for a "real" SELECT CASE
10065 : : is in code->expr1, but for a computed GOTO it is in code->expr2. All
10066 : : we have to do is make sure that the case_expr is a scalar integer
10067 : : expression. */
10068 : :
10069 : : static void
10070 : 683 : resolve_select (gfc_code *code, bool select_type)
10071 : : {
10072 : 683 : gfc_code *body;
10073 : 683 : gfc_expr *case_expr;
10074 : 683 : gfc_case *cp, *default_case, *tail, *head;
10075 : 683 : int seen_unreachable;
10076 : 683 : int seen_logical;
10077 : 683 : int ncases;
10078 : 683 : bt type;
10079 : 683 : bool t;
10080 : :
10081 : 683 : if (code->expr1 == NULL)
10082 : : {
10083 : : /* This was actually a computed GOTO statement. */
10084 : 5 : case_expr = code->expr2;
10085 : 5 : if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
10086 : 3 : gfc_error ("Selection expression in computed GOTO statement "
10087 : : "at %L must be a scalar integer expression",
10088 : : &case_expr->where);
10089 : :
10090 : : /* Further checking is not necessary because this SELECT was built
10091 : : by the compiler, so it should always be OK. Just move the
10092 : : case_expr from expr2 to expr so that we can handle computed
10093 : : GOTOs as normal SELECTs from here on. */
10094 : 5 : code->expr1 = code->expr2;
10095 : 5 : code->expr2 = NULL;
10096 : 5 : return;
10097 : : }
10098 : :
10099 : 678 : case_expr = code->expr1;
10100 : 678 : type = case_expr->ts.type;
10101 : :
10102 : : /* F08:C830. */
10103 : 678 : if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
10104 : 6 : && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
10105 : :
10106 : : {
10107 : 0 : gfc_error ("Argument of SELECT statement at %L cannot be %s",
10108 : : &case_expr->where, gfc_typename (case_expr));
10109 : :
10110 : : /* Punt. Going on here just produce more garbage error messages. */
10111 : 0 : return;
10112 : : }
10113 : :
10114 : : /* F08:R842. */
10115 : 678 : if (!select_type && case_expr->rank != 0)
10116 : : {
10117 : 1 : gfc_error ("Argument of SELECT statement at %L must be a scalar "
10118 : : "expression", &case_expr->where);
10119 : :
10120 : : /* Punt. */
10121 : 1 : return;
10122 : : }
10123 : :
10124 : : /* Raise a warning if an INTEGER case value exceeds the range of
10125 : : the case-expr. Later, all expressions will be promoted to the
10126 : : largest kind of all case-labels. */
10127 : :
10128 : 677 : if (type == BT_INTEGER)
10129 : 1897 : for (body = code->block; body; body = body->block)
10130 : 2800 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10131 : : {
10132 : 1436 : if (cp->low
10133 : 1436 : && gfc_check_integer_range (cp->low->value.integer,
10134 : : case_expr->ts.kind) != ARITH_OK)
10135 : 6 : gfc_warning (0, "Expression in CASE statement at %L is "
10136 : 6 : "not in the range of %s", &cp->low->where,
10137 : : gfc_typename (case_expr));
10138 : :
10139 : 1436 : if (cp->high
10140 : 1153 : && cp->low != cp->high
10141 : 1544 : && gfc_check_integer_range (cp->high->value.integer,
10142 : : case_expr->ts.kind) != ARITH_OK)
10143 : 0 : gfc_warning (0, "Expression in CASE statement at %L is "
10144 : 0 : "not in the range of %s", &cp->high->where,
10145 : : gfc_typename (case_expr));
10146 : : }
10147 : :
10148 : : /* PR 19168 has a long discussion concerning a mismatch of the kinds
10149 : : of the SELECT CASE expression and its CASE values. Walk the lists
10150 : : of case values, and if we find a mismatch, promote case_expr to
10151 : : the appropriate kind. */
10152 : :
10153 : 677 : if (type == BT_LOGICAL || type == BT_INTEGER)
10154 : : {
10155 : 2083 : for (body = code->block; body; body = body->block)
10156 : : {
10157 : : /* Walk the case label list. */
10158 : 3061 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10159 : : {
10160 : : /* Intercept the DEFAULT case. It does not have a kind. */
10161 : 1571 : if (cp->low == NULL && cp->high == NULL)
10162 : 291 : continue;
10163 : :
10164 : : /* Unreachable case ranges are discarded, so ignore. */
10165 : 1235 : if (cp->low != NULL && cp->high != NULL
10166 : 1187 : && cp->low != cp->high
10167 : 1345 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10168 : 33 : continue;
10169 : :
10170 : 1247 : if (cp->low != NULL
10171 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
10172 : 17 : gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
10173 : :
10174 : 1247 : if (cp->high != NULL
10175 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
10176 : 4 : gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
10177 : : }
10178 : : }
10179 : : }
10180 : :
10181 : : /* Assume there is no DEFAULT case. */
10182 : 677 : default_case = NULL;
10183 : 677 : head = tail = NULL;
10184 : 677 : ncases = 0;
10185 : 677 : seen_logical = 0;
10186 : :
10187 : 2472 : for (body = code->block; body; body = body->block)
10188 : : {
10189 : : /* Assume the CASE list is OK, and all CASE labels can be matched. */
10190 : 1795 : t = true;
10191 : 1795 : seen_unreachable = 0;
10192 : :
10193 : : /* Walk the case label list, making sure that all case labels
10194 : : are legal. */
10195 : 3777 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10196 : : {
10197 : : /* Count the number of cases in the whole construct. */
10198 : 1993 : ncases++;
10199 : :
10200 : : /* Intercept the DEFAULT case. */
10201 : 1993 : if (cp->low == NULL && cp->high == NULL)
10202 : : {
10203 : 361 : if (default_case != NULL)
10204 : : {
10205 : 0 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10206 : : "by a second DEFAULT CASE at %L",
10207 : : &default_case->where, &cp->where);
10208 : 0 : t = false;
10209 : 0 : break;
10210 : : }
10211 : : else
10212 : : {
10213 : 361 : default_case = cp;
10214 : 361 : continue;
10215 : : }
10216 : : }
10217 : :
10218 : : /* Deal with single value cases and case ranges. Errors are
10219 : : issued from the validation function. */
10220 : 1632 : if (!validate_case_label_expr (cp->low, case_expr)
10221 : 1632 : || !validate_case_label_expr (cp->high, case_expr))
10222 : : {
10223 : : t = false;
10224 : : break;
10225 : : }
10226 : :
10227 : 1624 : if (type == BT_LOGICAL
10228 : 78 : && ((cp->low == NULL || cp->high == NULL)
10229 : 76 : || cp->low != cp->high))
10230 : : {
10231 : 2 : gfc_error ("Logical range in CASE statement at %L is not "
10232 : : "allowed",
10233 : 1 : cp->low ? &cp->low->where : &cp->high->where);
10234 : 2 : t = false;
10235 : 2 : break;
10236 : : }
10237 : :
10238 : 76 : if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
10239 : : {
10240 : 76 : int value;
10241 : 76 : value = cp->low->value.logical == 0 ? 2 : 1;
10242 : 76 : if (value & seen_logical)
10243 : : {
10244 : 1 : gfc_error ("Constant logical value in CASE statement "
10245 : : "is repeated at %L",
10246 : : &cp->low->where);
10247 : 1 : t = false;
10248 : 1 : break;
10249 : : }
10250 : 75 : seen_logical |= value;
10251 : : }
10252 : :
10253 : 1577 : if (cp->low != NULL && cp->high != NULL
10254 : 1530 : && cp->low != cp->high
10255 : 1733 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10256 : : {
10257 : 35 : if (warn_surprising)
10258 : 1 : gfc_warning (OPT_Wsurprising,
10259 : : "Range specification at %L can never be matched",
10260 : : &cp->where);
10261 : :
10262 : 35 : cp->unreachable = 1;
10263 : 35 : seen_unreachable = 1;
10264 : : }
10265 : : else
10266 : : {
10267 : : /* If the case range can be matched, it can also overlap with
10268 : : other cases. To make sure it does not, we put it in a
10269 : : double linked list here. We sort that with a merge sort
10270 : : later on to detect any overlapping cases. */
10271 : 1586 : if (!head)
10272 : : {
10273 : 642 : head = tail = cp;
10274 : 642 : head->right = head->left = NULL;
10275 : : }
10276 : : else
10277 : : {
10278 : 944 : tail->right = cp;
10279 : 944 : tail->right->left = tail;
10280 : 944 : tail = tail->right;
10281 : 944 : tail->right = NULL;
10282 : : }
10283 : : }
10284 : : }
10285 : :
10286 : : /* It there was a failure in the previous case label, give up
10287 : : for this case label list. Continue with the next block. */
10288 : 1795 : if (!t)
10289 : 11 : continue;
10290 : :
10291 : : /* See if any case labels that are unreachable have been seen.
10292 : : If so, we eliminate them. This is a bit of a kludge because
10293 : : the case lists for a single case statement (label) is a
10294 : : single forward linked lists. */
10295 : 1784 : if (seen_unreachable)
10296 : : {
10297 : : /* Advance until the first case in the list is reachable. */
10298 : 69 : while (body->ext.block.case_list != NULL
10299 : 69 : && body->ext.block.case_list->unreachable)
10300 : : {
10301 : 34 : gfc_case *n = body->ext.block.case_list;
10302 : 34 : body->ext.block.case_list = body->ext.block.case_list->next;
10303 : 34 : n->next = NULL;
10304 : 34 : gfc_free_case_list (n);
10305 : : }
10306 : :
10307 : : /* Strip all other unreachable cases. */
10308 : 35 : if (body->ext.block.case_list)
10309 : : {
10310 : 2 : for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10311 : : {
10312 : 1 : if (cp->next->unreachable)
10313 : : {
10314 : 1 : gfc_case *n = cp->next;
10315 : 1 : cp->next = cp->next->next;
10316 : 1 : n->next = NULL;
10317 : 1 : gfc_free_case_list (n);
10318 : : }
10319 : : }
10320 : : }
10321 : : }
10322 : : }
10323 : :
10324 : : /* See if there were overlapping cases. If the check returns NULL,
10325 : : there was overlap. In that case we don't do anything. If head
10326 : : is non-NULL, we prepend the DEFAULT case. The sorted list can
10327 : : then used during code generation for SELECT CASE constructs with
10328 : : a case expression of a CHARACTER type. */
10329 : 677 : if (head)
10330 : : {
10331 : 642 : head = check_case_overlap (head);
10332 : :
10333 : : /* Prepend the default_case if it is there. */
10334 : 642 : if (head != NULL && default_case)
10335 : : {
10336 : 344 : default_case->left = NULL;
10337 : 344 : default_case->right = head;
10338 : 344 : head->left = default_case;
10339 : : }
10340 : : }
10341 : :
10342 : : /* Eliminate dead blocks that may be the result if we've seen
10343 : : unreachable case labels for a block. */
10344 : 2438 : for (body = code; body && body->block; body = body->block)
10345 : : {
10346 : 1761 : if (body->block->ext.block.case_list == NULL)
10347 : : {
10348 : : /* Cut the unreachable block from the code chain. */
10349 : 34 : gfc_code *c = body->block;
10350 : 34 : body->block = c->block;
10351 : :
10352 : : /* Kill the dead block, but not the blocks below it. */
10353 : 34 : c->block = NULL;
10354 : 34 : gfc_free_statements (c);
10355 : : }
10356 : : }
10357 : :
10358 : : /* More than two cases is legal but insane for logical selects.
10359 : : Issue a warning for it. */
10360 : 677 : if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10361 : 0 : gfc_warning (OPT_Wsurprising,
10362 : : "Logical SELECT CASE block at %L has more that two cases",
10363 : : &code->loc);
10364 : : }
10365 : :
10366 : :
10367 : : /* Check if a derived type is extensible. */
10368 : :
10369 : : bool
10370 : 23041 : gfc_type_is_extensible (gfc_symbol *sym)
10371 : : {
10372 : 23041 : return !(sym->attr.is_bind_c || sym->attr.sequence
10373 : 23025 : || (sym->attr.is_class
10374 : 2070 : && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10375 : : }
10376 : :
10377 : :
10378 : : static void
10379 : : resolve_types (gfc_namespace *ns);
10380 : :
10381 : : /* Resolve an associate-name: Resolve target and ensure the type-spec is
10382 : : correct as well as possibly the array-spec. */
10383 : :
10384 : : static void
10385 : 12367 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10386 : : {
10387 : 12367 : gfc_expr* target;
10388 : 12367 : bool parentheses = false;
10389 : :
10390 : 12367 : gcc_assert (sym->assoc);
10391 : 12367 : gcc_assert (sym->attr.flavor == FL_VARIABLE);
10392 : :
10393 : : /* If this is for SELECT TYPE, the target may not yet be set. In that
10394 : : case, return. Resolution will be called later manually again when
10395 : : this is done. */
10396 : 12367 : target = sym->assoc->target;
10397 : 12367 : if (!target)
10398 : : return;
10399 : 7317 : gcc_assert (!sym->assoc->dangling);
10400 : :
10401 : 7317 : if (target->expr_type == EXPR_OP
10402 : 204 : && target->value.op.op == INTRINSIC_PARENTHESES
10403 : 42 : && target->value.op.op1->expr_type == EXPR_VARIABLE)
10404 : : {
10405 : 23 : sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10406 : 23 : gfc_free_expr (target);
10407 : 23 : target = sym->assoc->target;
10408 : 23 : parentheses = true;
10409 : : }
10410 : :
10411 : 7317 : if (resolve_target && !gfc_resolve_expr (target))
10412 : : return;
10413 : :
10414 : 7312 : if (sym->assoc->ar)
10415 : : {
10416 : : int dim;
10417 : : gfc_array_ref *ar = sym->assoc->ar;
10418 : 67 : for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10419 : : {
10420 : 39 : if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10421 : 39 : && ar->start[dim]->ts.type == BT_INTEGER)
10422 : 78 : || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10423 : 39 : && ar->end[dim]->ts.type == BT_INTEGER))
10424 : 0 : gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10425 : : "remapping of associate name %s at %L",
10426 : : sym->name, &sym->declared_at);
10427 : : }
10428 : : }
10429 : :
10430 : : /* For variable targets, we get some attributes from the target. */
10431 : 7312 : if (target->expr_type == EXPR_VARIABLE)
10432 : : {
10433 : 6449 : gfc_symbol *tsym, *dsym;
10434 : :
10435 : 6449 : gcc_assert (target->symtree);
10436 : 6449 : tsym = target->symtree->n.sym;
10437 : :
10438 : 6449 : if (gfc_expr_attr (target).proc_pointer)
10439 : : {
10440 : 0 : gfc_error ("Associating entity %qs at %L is a procedure pointer",
10441 : : tsym->name, &target->where);
10442 : 0 : return;
10443 : : }
10444 : :
10445 : 74 : if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10446 : 2 : && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10447 : 6450 : && dsym->attr.flavor == FL_DERIVED)
10448 : : {
10449 : 1 : gfc_error ("Derived type %qs cannot be used as a variable at %L",
10450 : : tsym->name, &target->where);
10451 : 1 : return;
10452 : : }
10453 : :
10454 : 6448 : if (tsym->attr.flavor == FL_PROCEDURE)
10455 : : {
10456 : 73 : bool is_error = true;
10457 : 73 : if (tsym->attr.function && tsym->result == tsym)
10458 : 141 : for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10459 : 137 : if (tsym == ns->proc_name)
10460 : : {
10461 : : is_error = false;
10462 : : break;
10463 : : }
10464 : 64 : if (is_error)
10465 : : {
10466 : 13 : gfc_error ("Associating entity %qs at %L is a procedure name",
10467 : : tsym->name, &target->where);
10468 : 13 : return;
10469 : : }
10470 : : }
10471 : :
10472 : 6435 : sym->attr.asynchronous = tsym->attr.asynchronous;
10473 : 6435 : sym->attr.volatile_ = tsym->attr.volatile_;
10474 : :
10475 : 12870 : sym->attr.target = tsym->attr.target
10476 : 6435 : || gfc_expr_attr (target).pointer;
10477 : 6435 : if (is_subref_array (target))
10478 : 378 : sym->attr.subref_array_pointer = 1;
10479 : : }
10480 : 863 : else if (target->ts.type == BT_PROCEDURE)
10481 : : {
10482 : 0 : gfc_error ("Associating selector-expression at %L yields a procedure",
10483 : : &target->where);
10484 : 0 : return;
10485 : : }
10486 : :
10487 : 7298 : if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10488 : : {
10489 : : /* By now, the type of the target has been fixed up. */
10490 : 293 : symbol_attribute attr;
10491 : :
10492 : 293 : if (sym->ts.type == BT_DERIVED
10493 : 166 : && target->ts.type == BT_CLASS
10494 : 31 : && !UNLIMITED_POLY (target))
10495 : : {
10496 : : /* Inferred to be derived type but the target has type class. */
10497 : 31 : sym->ts = CLASS_DATA (target)->ts;
10498 : 31 : if (!sym->as)
10499 : 31 : sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10500 : 31 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10501 : 31 : sym->attr.dimension = target->rank ? 1 : 0;
10502 : 31 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10503 : : target->corank);
10504 : 31 : sym->as = NULL;
10505 : : }
10506 : 262 : else if (target->ts.type == BT_DERIVED
10507 : 135 : && target->symtree && target->symtree->n.sym
10508 : 111 : && target->symtree->n.sym->ts.type == BT_CLASS
10509 : 0 : && IS_INFERRED_TYPE (target)
10510 : 0 : && target->ref && target->ref->next
10511 : 0 : && target->ref->next->type == REF_ARRAY
10512 : 0 : && !target->ref->next->next)
10513 : : {
10514 : : /* A inferred type selector whose symbol has been determined to be
10515 : : a class array but which only has an array reference. Change the
10516 : : associate name and the selector to class type. */
10517 : 0 : sym->ts = target->ts;
10518 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10519 : 0 : sym->attr.dimension = target->rank ? 1 : 0;
10520 : 0 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10521 : : target->corank);
10522 : 0 : sym->as = NULL;
10523 : 0 : target->ts = sym->ts;
10524 : : }
10525 : 262 : else if ((target->ts.type == BT_DERIVED)
10526 : 127 : || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10527 : 61 : && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10528 : : /* Confirmed to be either a derived type or misidentified to be a
10529 : : scalar class object, when the selector is a class array. */
10530 : 141 : sym->ts = target->ts;
10531 : : }
10532 : :
10533 : :
10534 : 7298 : if (target->expr_type == EXPR_NULL)
10535 : : {
10536 : 1 : gfc_error ("Selector at %L cannot be NULL()", &target->where);
10537 : 1 : return;
10538 : : }
10539 : 7297 : else if (target->ts.type == BT_UNKNOWN)
10540 : : {
10541 : 2 : gfc_error ("Selector at %L has no type", &target->where);
10542 : 2 : return;
10543 : : }
10544 : :
10545 : : /* Get type if this was not already set. Note that it can be
10546 : : some other type than the target in case this is a SELECT TYPE
10547 : : selector! So we must not update when the type is already there. */
10548 : 7295 : if (sym->ts.type == BT_UNKNOWN)
10549 : 220 : sym->ts = target->ts;
10550 : :
10551 : 7295 : gcc_assert (sym->ts.type != BT_UNKNOWN);
10552 : :
10553 : : /* See if this is a valid association-to-variable. */
10554 : 14590 : sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10555 : 6435 : && !parentheses
10556 : 6414 : && !gfc_has_vector_subscript (target))
10557 : 7343 : || gfc_is_ptr_fcn (target));
10558 : :
10559 : : /* Finally resolve if this is an array or not. */
10560 : 7295 : if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10561 : 137 : && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10562 : : {
10563 : 92 : gfc_expression_rank (target);
10564 : 92 : if (target->ts.type == BT_DERIVED
10565 : 45 : && !sym->as
10566 : 45 : && target->symtree->n.sym->as)
10567 : : {
10568 : 0 : sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10569 : 0 : sym->attr.dimension = 1;
10570 : : }
10571 : 92 : else if (target->ts.type == BT_CLASS
10572 : 47 : && CLASS_DATA (target)->as)
10573 : : {
10574 : 0 : target->rank = CLASS_DATA (target)->as->rank;
10575 : 0 : target->corank = CLASS_DATA (target)->as->corank;
10576 : 0 : if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10577 : : {
10578 : 0 : sym->ts = target->ts;
10579 : 0 : sym->attr.dimension = 0;
10580 : : }
10581 : : }
10582 : : }
10583 : :
10584 : :
10585 : 7295 : if (sym->attr.dimension && target->rank == 0)
10586 : : {
10587 : : /* primary.cc makes the assumption that a reference to an associate
10588 : : name followed by a left parenthesis is an array reference. */
10589 : 17 : if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10590 : : {
10591 : 12 : gfc_expression_rank (sym->assoc->target);
10592 : 12 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10593 : 12 : if (!sym->attr.dimension && sym->as)
10594 : 0 : sym->as = NULL;
10595 : : }
10596 : :
10597 : 17 : if (sym->attr.dimension && target->rank == 0)
10598 : : {
10599 : 5 : if (sym->ts.type != BT_CHARACTER)
10600 : 5 : gfc_error ("Associate-name %qs at %L is used as array",
10601 : : sym->name, &sym->declared_at);
10602 : 5 : sym->attr.dimension = 0;
10603 : 5 : return;
10604 : : }
10605 : : }
10606 : :
10607 : : /* We cannot deal with class selectors that need temporaries. */
10608 : 7290 : if (target->ts.type == BT_CLASS
10609 : 7290 : && gfc_ref_needs_temporary_p (target->ref))
10610 : : {
10611 : 1 : gfc_error ("CLASS selector at %L needs a temporary which is not "
10612 : : "yet implemented", &target->where);
10613 : 1 : return;
10614 : : }
10615 : :
10616 : 7289 : if (target->ts.type == BT_CLASS)
10617 : 2698 : gfc_fix_class_refs (target);
10618 : :
10619 : 7289 : if ((target->rank > 0 || target->corank > 0)
10620 : 2623 : && !sym->attr.select_rank_temporary)
10621 : : {
10622 : 2623 : gfc_array_spec *as;
10623 : : /* The rank may be incorrectly guessed at parsing, therefore make sure
10624 : : it is corrected now. */
10625 : 2623 : if (sym->ts.type != BT_CLASS
10626 : 2068 : && (!sym->as || sym->as->corank != target->corank))
10627 : : {
10628 : 135 : if (!sym->as)
10629 : 128 : sym->as = gfc_get_array_spec ();
10630 : 135 : as = sym->as;
10631 : 135 : as->rank = target->rank;
10632 : 135 : as->type = AS_DEFERRED;
10633 : 135 : as->corank = target->corank;
10634 : 135 : sym->attr.dimension = 1;
10635 : 135 : if (as->corank != 0)
10636 : 7 : sym->attr.codimension = 1;
10637 : : }
10638 : 2488 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10639 : 554 : && (!CLASS_DATA (sym)->as
10640 : 554 : || CLASS_DATA (sym)->as->corank != target->corank))
10641 : : {
10642 : 0 : if (!CLASS_DATA (sym)->as)
10643 : 0 : CLASS_DATA (sym)->as = gfc_get_array_spec ();
10644 : 0 : as = CLASS_DATA (sym)->as;
10645 : 0 : as->rank = target->rank;
10646 : 0 : as->type = AS_DEFERRED;
10647 : 0 : as->corank = target->corank;
10648 : 0 : CLASS_DATA (sym)->attr.dimension = 1;
10649 : 0 : if (as->corank != 0)
10650 : 0 : CLASS_DATA (sym)->attr.codimension = 1;
10651 : : }
10652 : : }
10653 : 4666 : else if (!sym->attr.select_rank_temporary)
10654 : : {
10655 : : /* target's rank is 0, but the type of the sym is still array valued,
10656 : : which has to be corrected. */
10657 : 3283 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10658 : 675 : && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10659 : : {
10660 : 24 : gfc_array_spec *as;
10661 : 24 : symbol_attribute attr;
10662 : : /* The associated variable's type is still the array type
10663 : : correct this now. */
10664 : 24 : gfc_typespec *ts = &target->ts;
10665 : 24 : gfc_ref *ref;
10666 : : /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10667 : : */
10668 : 24 : bool internal_ref = true;
10669 : :
10670 : 72 : for (ref = target->ref; ref != NULL; ref = ref->next)
10671 : : {
10672 : 48 : switch (ref->type)
10673 : : {
10674 : 24 : case REF_COMPONENT:
10675 : 24 : ts = &ref->u.c.component->ts;
10676 : 24 : internal_ref
10677 : 24 : = target->ref == ref && ref->next
10678 : 48 : && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10679 : : break;
10680 : 24 : case REF_ARRAY:
10681 : 24 : if (ts->type == BT_CLASS)
10682 : 0 : ts = &ts->u.derived->components->ts;
10683 : 24 : if (internal_ref && ref->u.ar.codimen > 0)
10684 : 0 : for (int i = ref->u.ar.dimen;
10685 : : internal_ref
10686 : 0 : && i < ref->u.ar.dimen + ref->u.ar.codimen;
10687 : : ++i)
10688 : 0 : internal_ref
10689 : 0 : = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10690 : : break;
10691 : : default:
10692 : : break;
10693 : : }
10694 : : }
10695 : : /* Only rewrite the type of this symbol, when the refs are not the
10696 : : internal ones for class and co-array this-image. */
10697 : 24 : if (!internal_ref)
10698 : : {
10699 : : /* Create a scalar instance of the current class type. Because
10700 : : the rank of a class array goes into its name, the type has to
10701 : : be rebuilt. The alternative of (re-)setting just the
10702 : : attributes and as in the current type, destroys the type also
10703 : : in other places. */
10704 : 0 : as = NULL;
10705 : 0 : sym->ts = *ts;
10706 : 0 : sym->ts.type = BT_CLASS;
10707 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10708 : 0 : gfc_change_class (&sym->ts, &attr, as, 0, 0);
10709 : 0 : sym->as = NULL;
10710 : : }
10711 : : }
10712 : : }
10713 : :
10714 : : /* Mark this as an associate variable. */
10715 : 7289 : sym->attr.associate_var = 1;
10716 : :
10717 : : /* Fix up the type-spec for CHARACTER types. */
10718 : 7289 : if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10719 : : {
10720 : 442 : gfc_ref *ref;
10721 : 709 : for (ref = target->ref; ref; ref = ref->next)
10722 : 293 : if (ref->type == REF_SUBSTRING
10723 : 68 : && (ref->u.ss.start == NULL
10724 : 68 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
10725 : 68 : || ref->u.ss.end == NULL
10726 : 48 : || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10727 : : break;
10728 : :
10729 : 442 : if (!sym->ts.u.cl)
10730 : 122 : sym->ts.u.cl = target->ts.u.cl;
10731 : :
10732 : 442 : if (sym->ts.deferred
10733 : 189 : && sym->ts.u.cl == target->ts.u.cl)
10734 : : {
10735 : 110 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10736 : 110 : sym->ts.deferred = 1;
10737 : : }
10738 : :
10739 : 442 : if (!sym->ts.u.cl->length
10740 : 326 : && !sym->ts.deferred
10741 : 137 : && target->expr_type == EXPR_CONSTANT)
10742 : : {
10743 : 30 : sym->ts.u.cl->length =
10744 : 30 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10745 : 30 : target->value.character.length);
10746 : : }
10747 : 412 : else if (((!sym->ts.u.cl->length
10748 : 116 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10749 : 302 : && target->expr_type != EXPR_VARIABLE)
10750 : 290 : || ref)
10751 : : {
10752 : 148 : if (!sym->ts.deferred)
10753 : : {
10754 : 44 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10755 : 44 : sym->ts.deferred = 1;
10756 : : }
10757 : :
10758 : : /* This is reset in trans-stmt.cc after the assignment
10759 : : of the target expression to the associate name. */
10760 : 148 : if (ref && sym->as)
10761 : 26 : sym->attr.pointer = 1;
10762 : : else
10763 : 122 : sym->attr.allocatable = 1;
10764 : : }
10765 : : }
10766 : :
10767 : 7289 : if (sym->ts.type == BT_CLASS
10768 : 1375 : && IS_INFERRED_TYPE (target)
10769 : 13 : && target->ts.type == BT_DERIVED
10770 : 0 : && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10771 : 0 : && target->ref && target->ref->next && !target->ref->next->next
10772 : 0 : && target->ref->next->type == REF_ARRAY)
10773 : 0 : target->ts = target->symtree->n.sym->ts;
10774 : :
10775 : : /* If the target is a good class object, so is the associate variable. */
10776 : 7289 : if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10777 : 688 : sym->attr.class_ok = 1;
10778 : : }
10779 : :
10780 : :
10781 : : /* Ensure that SELECT TYPE expressions have the correct rank and a full
10782 : : array reference, where necessary. The symbols are artificial and so
10783 : : the dimension attribute and arrayspec can also be set. In addition,
10784 : : sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10785 : : This is corrected here as well.*/
10786 : :
10787 : : static void
10788 : 1644 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10789 : : gfc_ref *ref)
10790 : : {
10791 : 1644 : gfc_ref *nref = (*expr1)->ref;
10792 : 1644 : gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10793 : 1644 : gfc_symbol *sym2;
10794 : 1644 : gfc_expr *selector = gfc_copy_expr (expr2);
10795 : :
10796 : 1644 : (*expr1)->rank = rank;
10797 : 1644 : (*expr1)->corank = corank;
10798 : 1644 : if (selector)
10799 : : {
10800 : 309 : gfc_resolve_expr (selector);
10801 : 309 : if (selector->expr_type == EXPR_OP
10802 : 2 : && selector->value.op.op == INTRINSIC_PARENTHESES)
10803 : 2 : sym2 = selector->value.op.op1->symtree->n.sym;
10804 : 307 : else if (selector->expr_type == EXPR_VARIABLE
10805 : 7 : || selector->expr_type == EXPR_FUNCTION)
10806 : 307 : sym2 = selector->symtree->n.sym;
10807 : : else
10808 : 0 : gcc_unreachable ();
10809 : : }
10810 : : else
10811 : : sym2 = NULL;
10812 : :
10813 : 1644 : if (sym1->ts.type == BT_CLASS)
10814 : : {
10815 : 1644 : if ((*expr1)->ts.type != BT_CLASS)
10816 : 13 : (*expr1)->ts = sym1->ts;
10817 : :
10818 : 1644 : CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
10819 : 1644 : CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
10820 : 1644 : if (CLASS_DATA (sym1)->as == NULL && sym2)
10821 : 1 : CLASS_DATA (sym1)->as
10822 : 1 : = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
10823 : : }
10824 : : else
10825 : : {
10826 : 0 : sym1->attr.dimension = rank > 0 ? 1 : 0;
10827 : 0 : sym1->attr.codimension = corank > 0 ? 1 : 0;
10828 : 0 : if (sym1->as == NULL && sym2)
10829 : 0 : sym1->as = gfc_copy_array_spec (sym2->as);
10830 : : }
10831 : :
10832 : 2973 : for (; nref; nref = nref->next)
10833 : 2664 : if (nref->next == NULL)
10834 : : break;
10835 : :
10836 : 1644 : if (ref && nref && nref->type != REF_ARRAY)
10837 : 6 : nref->next = gfc_copy_ref (ref);
10838 : 1638 : else if (ref && !nref)
10839 : 300 : (*expr1)->ref = gfc_copy_ref (ref);
10840 : 1338 : else if (ref && nref->u.ar.codimen != corank)
10841 : : {
10842 : 912 : for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
10843 : 855 : nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
10844 : 57 : nref->u.ar.codimen = corank;
10845 : : }
10846 : 1644 : }
10847 : :
10848 : :
10849 : : static gfc_expr *
10850 : 6545 : build_loc_call (gfc_expr *sym_expr)
10851 : : {
10852 : 6545 : gfc_expr *loc_call;
10853 : 6545 : loc_call = gfc_get_expr ();
10854 : 6545 : loc_call->expr_type = EXPR_FUNCTION;
10855 : 6545 : gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
10856 : 6545 : loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
10857 : 6545 : loc_call->symtree->n.sym->attr.intrinsic = 1;
10858 : 6545 : loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
10859 : 6545 : gfc_commit_symbol (loc_call->symtree->n.sym);
10860 : 6545 : loc_call->ts.type = BT_INTEGER;
10861 : 6545 : loc_call->ts.kind = gfc_index_integer_kind;
10862 : 6545 : loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
10863 : 6545 : loc_call->value.function.actual = gfc_get_actual_arglist ();
10864 : 6545 : loc_call->value.function.actual->expr = sym_expr;
10865 : 6545 : loc_call->where = sym_expr->where;
10866 : 6545 : return loc_call;
10867 : : }
10868 : :
10869 : : /* Resolve a SELECT TYPE statement. */
10870 : :
10871 : : static void
10872 : 2924 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
10873 : : {
10874 : 2924 : gfc_symbol *selector_type;
10875 : 2924 : gfc_code *body, *new_st, *if_st, *tail;
10876 : 2924 : gfc_code *class_is = NULL, *default_case = NULL;
10877 : 2924 : gfc_case *c;
10878 : 2924 : gfc_symtree *st;
10879 : 2924 : char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
10880 : 2924 : gfc_namespace *ns;
10881 : 2924 : int error = 0;
10882 : 2924 : int rank = 0, corank = 0;
10883 : 2924 : gfc_ref* ref = NULL;
10884 : 2924 : gfc_expr *selector_expr = NULL;
10885 : 2924 : gfc_code *old_code = code;
10886 : :
10887 : 2924 : ns = code->ext.block.ns;
10888 : 2924 : if (code->expr2)
10889 : : {
10890 : : /* Set this, or coarray checks in resolve will fail. */
10891 : 623 : code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10892 : : }
10893 : 2924 : gfc_resolve (ns);
10894 : :
10895 : : /* Check for F03:C813. */
10896 : 2924 : if (code->expr1->ts.type != BT_CLASS
10897 : 36 : && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10898 : : {
10899 : 13 : gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10900 : : "at %L", &code->loc);
10901 : 41 : return;
10902 : : }
10903 : :
10904 : : /* Prevent segfault, when class type is not initialized due to previous
10905 : : error. */
10906 : 2911 : if (!code->expr1->symtree->n.sym->attr.class_ok
10907 : 2909 : || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10908 : : return;
10909 : :
10910 : 2904 : if (code->expr2)
10911 : : {
10912 : 614 : gfc_ref *ref2 = NULL;
10913 : 1440 : for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10914 : 826 : if (ref->type == REF_COMPONENT
10915 : 425 : && ref->u.c.component->ts.type == BT_CLASS)
10916 : 826 : ref2 = ref;
10917 : :
10918 : 614 : if (ref2)
10919 : : {
10920 : 333 : if (code->expr1->symtree->n.sym->attr.untyped)
10921 : 1 : code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10922 : 333 : selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10923 : : }
10924 : : else
10925 : : {
10926 : 281 : if (code->expr1->symtree->n.sym->attr.untyped)
10927 : 28 : code->expr1->symtree->n.sym->ts = code->expr2->ts;
10928 : : /* Sometimes the selector expression is given the typespec of the
10929 : : '_data' field, which is logical enough but inappropriate here. */
10930 : 281 : if (code->expr2->ts.type == BT_DERIVED
10931 : 80 : && code->expr2->symtree
10932 : 80 : && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10933 : 80 : code->expr2->ts = code->expr2->symtree->n.sym->ts;
10934 : 281 : selector_type = CLASS_DATA (code->expr2)
10935 : : ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10936 : : }
10937 : :
10938 : 614 : if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10939 : : {
10940 : 295 : CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10941 : 295 : CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10942 : 295 : CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10943 : : }
10944 : :
10945 : : /* F2008: C803 The selector expression must not be coindexed. */
10946 : 614 : if (gfc_is_coindexed (code->expr2))
10947 : : {
10948 : 3 : gfc_error ("Selector at %L must not be coindexed",
10949 : 3 : &code->expr2->where);
10950 : 3 : return;
10951 : : }
10952 : :
10953 : : }
10954 : : else
10955 : : {
10956 : 2290 : selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10957 : :
10958 : 2290 : if (gfc_is_coindexed (code->expr1))
10959 : : {
10960 : 0 : gfc_error ("Selector at %L must not be coindexed",
10961 : 0 : &code->expr1->where);
10962 : 0 : return;
10963 : : }
10964 : : }
10965 : :
10966 : : /* Loop over TYPE IS / CLASS IS cases. */
10967 : 8142 : for (body = code->block; body; body = body->block)
10968 : : {
10969 : 5242 : c = body->ext.block.case_list;
10970 : :
10971 : 5242 : if (!error)
10972 : : {
10973 : : /* Check for repeated cases. */
10974 : 8177 : for (tail = code->block; tail; tail = tail->block)
10975 : : {
10976 : 8177 : gfc_case *d = tail->ext.block.case_list;
10977 : 8177 : if (tail == body)
10978 : : break;
10979 : :
10980 : 2944 : if (c->ts.type == d->ts.type
10981 : 516 : && ((c->ts.type == BT_DERIVED
10982 : 418 : && c->ts.u.derived && d->ts.u.derived
10983 : 418 : && !strcmp (c->ts.u.derived->name,
10984 : : d->ts.u.derived->name))
10985 : 515 : || c->ts.type == BT_UNKNOWN
10986 : 515 : || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10987 : 55 : && c->ts.kind == d->ts.kind)))
10988 : : {
10989 : 1 : gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10990 : : &c->where, &d->where);
10991 : 1 : return;
10992 : : }
10993 : : }
10994 : : }
10995 : :
10996 : : /* Check F03:C815. */
10997 : 3317 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10998 : 2247 : && selector_type
10999 : 2247 : && !selector_type->attr.unlimited_polymorphic
11000 : 7167 : && !gfc_type_is_extensible (c->ts.u.derived))
11001 : : {
11002 : 1 : gfc_error ("Derived type %qs at %L must be extensible",
11003 : 1 : c->ts.u.derived->name, &c->where);
11004 : 1 : error++;
11005 : 1 : continue;
11006 : : }
11007 : :
11008 : : /* Check F03:C816. */
11009 : 5246 : if (c->ts.type != BT_UNKNOWN
11010 : 3660 : && selector_type && !selector_type->attr.unlimited_polymorphic
11011 : 7169 : && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
11012 : 1925 : || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
11013 : : {
11014 : 6 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11015 : 2 : gfc_error ("Derived type %qs at %L must be an extension of %qs",
11016 : 2 : c->ts.u.derived->name, &c->where, selector_type->name);
11017 : : else
11018 : 4 : gfc_error ("Unexpected intrinsic type %qs at %L",
11019 : : gfc_basic_typename (c->ts.type), &c->where);
11020 : 6 : error++;
11021 : 6 : continue;
11022 : : }
11023 : :
11024 : : /* Check F03:C814. */
11025 : 5234 : if (c->ts.type == BT_CHARACTER
11026 : 736 : && (c->ts.u.cl->length != NULL || c->ts.deferred))
11027 : : {
11028 : 0 : gfc_error ("The type-spec at %L shall specify that each length "
11029 : : "type parameter is assumed", &c->where);
11030 : 0 : error++;
11031 : 0 : continue;
11032 : : }
11033 : :
11034 : : /* Intercept the DEFAULT case. */
11035 : 5234 : if (c->ts.type == BT_UNKNOWN)
11036 : : {
11037 : : /* Check F03:C818. */
11038 : 1580 : if (default_case)
11039 : : {
11040 : 1 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
11041 : : "by a second DEFAULT CASE at %L",
11042 : 1 : &default_case->ext.block.case_list->where, &c->where);
11043 : 1 : error++;
11044 : 1 : continue;
11045 : : }
11046 : :
11047 : : default_case = body;
11048 : : }
11049 : : }
11050 : :
11051 : 2900 : if (error > 0)
11052 : : return;
11053 : :
11054 : : /* Transform SELECT TYPE statement to BLOCK and associate selector to
11055 : : target if present. If there are any EXIT statements referring to the
11056 : : SELECT TYPE construct, this is no problem because the gfc_code
11057 : : reference stays the same and EXIT is equally possible from the BLOCK
11058 : : it is changed to. */
11059 : 2897 : code->op = EXEC_BLOCK;
11060 : 2897 : if (code->expr2)
11061 : : {
11062 : 611 : gfc_association_list* assoc;
11063 : :
11064 : 611 : assoc = gfc_get_association_list ();
11065 : 611 : assoc->st = code->expr1->symtree;
11066 : 611 : assoc->target = gfc_copy_expr (code->expr2);
11067 : 611 : assoc->target->where = code->expr2->where;
11068 : : /* assoc->variable will be set by resolve_assoc_var. */
11069 : :
11070 : 611 : code->ext.block.assoc = assoc;
11071 : 611 : code->expr1->symtree->n.sym->assoc = assoc;
11072 : :
11073 : 611 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11074 : : }
11075 : : else
11076 : 2286 : code->ext.block.assoc = NULL;
11077 : :
11078 : : /* Ensure that the selector rank and arrayspec are available to
11079 : : correct expressions in which they might be missing. */
11080 : 2897 : if (code->expr2 && (code->expr2->rank || code->expr2->corank))
11081 : : {
11082 : 309 : rank = code->expr2->rank;
11083 : 309 : corank = code->expr2->corank;
11084 : 582 : for (ref = code->expr2->ref; ref; ref = ref->next)
11085 : 573 : if (ref->next == NULL)
11086 : : break;
11087 : 309 : if (ref && ref->type == REF_ARRAY)
11088 : 300 : ref = gfc_copy_ref (ref);
11089 : :
11090 : : /* Fixup expr1 if necessary. */
11091 : 309 : if (rank || corank)
11092 : 309 : fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
11093 : : }
11094 : 2588 : else if (code->expr1->rank || code->expr1->corank)
11095 : : {
11096 : 827 : rank = code->expr1->rank;
11097 : 827 : corank = code->expr1->corank;
11098 : 827 : for (ref = code->expr1->ref; ref; ref = ref->next)
11099 : 827 : if (ref->next == NULL)
11100 : : break;
11101 : 827 : if (ref && ref->type == REF_ARRAY)
11102 : 827 : ref = gfc_copy_ref (ref);
11103 : : }
11104 : :
11105 : 2897 : gfc_expr *orig_expr1 = code->expr1;
11106 : :
11107 : : /* Add EXEC_SELECT to switch on type. */
11108 : 2897 : new_st = gfc_get_code (code->op);
11109 : 2897 : new_st->expr1 = code->expr1;
11110 : 2897 : new_st->expr2 = code->expr2;
11111 : 2897 : new_st->block = code->block;
11112 : 2897 : code->expr1 = code->expr2 = NULL;
11113 : 2897 : code->block = NULL;
11114 : 2897 : if (!ns->code)
11115 : 2897 : ns->code = new_st;
11116 : : else
11117 : 0 : ns->code->next = new_st;
11118 : 2897 : code = new_st;
11119 : 2897 : code->op = EXEC_SELECT_TYPE;
11120 : :
11121 : : /* Use the intrinsic LOC function to generate an integer expression
11122 : : for the vtable of the selector. Note that the rank of the selector
11123 : : expression has to be set to zero. */
11124 : 2897 : gfc_add_vptr_component (code->expr1);
11125 : 2897 : code->expr1->rank = 0;
11126 : 2897 : code->expr1->corank = 0;
11127 : 2897 : code->expr1 = build_loc_call (code->expr1);
11128 : 2897 : selector_expr = code->expr1->value.function.actual->expr;
11129 : :
11130 : : /* Loop over TYPE IS / CLASS IS cases. */
11131 : 8123 : for (body = code->block; body; body = body->block)
11132 : : {
11133 : 5226 : gfc_symbol *vtab;
11134 : 5226 : c = body->ext.block.case_list;
11135 : :
11136 : : /* Generate an index integer expression for address of the
11137 : : TYPE/CLASS vtable and store it in c->low. The hash expression
11138 : : is stored in c->high and is used to resolve intrinsic cases. */
11139 : 5226 : if (c->ts.type != BT_UNKNOWN)
11140 : : {
11141 : 3648 : gfc_expr *e;
11142 : 3648 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11143 : : {
11144 : 2238 : vtab = gfc_find_derived_vtab (c->ts.u.derived);
11145 : 2238 : gcc_assert (vtab);
11146 : 2238 : c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
11147 : 2238 : c->ts.u.derived->hash_value);
11148 : : }
11149 : : else
11150 : : {
11151 : 1410 : vtab = gfc_find_vtab (&c->ts);
11152 : 1410 : gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
11153 : 1410 : e = CLASS_DATA (vtab)->initializer;
11154 : 1410 : c->high = gfc_copy_expr (e);
11155 : 1410 : if (c->high->ts.kind != gfc_integer_4_kind)
11156 : : {
11157 : 1 : gfc_typespec ts;
11158 : 1 : ts.kind = gfc_integer_4_kind;
11159 : 1 : ts.type = BT_INTEGER;
11160 : 1 : gfc_convert_type_warn (c->high, &ts, 2, 0);
11161 : : }
11162 : : }
11163 : :
11164 : 3648 : e = gfc_lval_expr_from_sym (vtab);
11165 : 3648 : c->low = build_loc_call (e);
11166 : : }
11167 : : else
11168 : 1578 : continue;
11169 : :
11170 : : /* Associate temporary to selector. This should only be done
11171 : : when this case is actually true, so build a new ASSOCIATE
11172 : : that does precisely this here (instead of using the
11173 : : 'global' one). */
11174 : :
11175 : : /* First check the derived type import status. */
11176 : 3648 : if (gfc_current_ns->import_state != IMPORT_NOT_SET
11177 : 6 : && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
11178 : : {
11179 : 12 : st = gfc_find_symtree (gfc_current_ns->sym_root,
11180 : 6 : c->ts.u.derived->name);
11181 : 6 : if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
11182 : : gfc_current_ns))
11183 : 6 : error++;
11184 : : }
11185 : :
11186 : 3648 : const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
11187 : 3648 : if (c->ts.type == BT_CLASS)
11188 : 321 : snprintf (name, sizeof (name), "__tmp_class_%s_%s",
11189 : 321 : c->ts.u.derived->name, var_name);
11190 : 3327 : else if (c->ts.type == BT_DERIVED)
11191 : 1917 : snprintf (name, sizeof (name), "__tmp_type_%s_%s",
11192 : 1917 : c->ts.u.derived->name, var_name);
11193 : 1410 : else if (c->ts.type == BT_CHARACTER)
11194 : : {
11195 : 736 : HOST_WIDE_INT charlen = 0;
11196 : 736 : if (c->ts.u.cl && c->ts.u.cl->length
11197 : 0 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11198 : 0 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11199 : 736 : snprintf (name, sizeof (name),
11200 : : "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
11201 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
11202 : : var_name);
11203 : : }
11204 : : else
11205 : 674 : snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
11206 : : gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
11207 : :
11208 : 3648 : st = gfc_find_symtree (ns->sym_root, name);
11209 : 3648 : gcc_assert (st->n.sym->assoc);
11210 : 3648 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11211 : 3648 : st->n.sym->assoc->target->where = selector_expr->where;
11212 : 3648 : if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
11213 : : {
11214 : 3327 : gfc_add_data_component (st->n.sym->assoc->target);
11215 : : /* Fixup the target expression if necessary. */
11216 : 3327 : if (rank || corank)
11217 : 1335 : fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
11218 : : ref);
11219 : : }
11220 : :
11221 : 3648 : new_st = gfc_get_code (EXEC_BLOCK);
11222 : 3648 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11223 : 3648 : new_st->ext.block.ns->code = body->next;
11224 : 3648 : body->next = new_st;
11225 : :
11226 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11227 : : there is a CASE label overlap and this is already used. Just ignore,
11228 : : the error is diagnosed elsewhere. */
11229 : 3648 : if (st->n.sym->assoc->dangling)
11230 : : {
11231 : 3647 : new_st->ext.block.assoc = st->n.sym->assoc;
11232 : 3647 : st->n.sym->assoc->dangling = 0;
11233 : : }
11234 : :
11235 : 3648 : resolve_assoc_var (st->n.sym, false);
11236 : : }
11237 : :
11238 : : /* Take out CLASS IS cases for separate treatment. */
11239 : : body = code;
11240 : 8123 : while (body && body->block)
11241 : : {
11242 : 5226 : if (body->block->ext.block.case_list->ts.type == BT_CLASS)
11243 : : {
11244 : : /* Add to class_is list. */
11245 : 321 : if (class_is == NULL)
11246 : : {
11247 : 290 : class_is = body->block;
11248 : 290 : tail = class_is;
11249 : : }
11250 : : else
11251 : : {
11252 : 43 : for (tail = class_is; tail->block; tail = tail->block) ;
11253 : 31 : tail->block = body->block;
11254 : 31 : tail = tail->block;
11255 : : }
11256 : : /* Remove from EXEC_SELECT list. */
11257 : 321 : body->block = body->block->block;
11258 : 321 : tail->block = NULL;
11259 : : }
11260 : : else
11261 : : body = body->block;
11262 : : }
11263 : :
11264 : 2897 : if (class_is)
11265 : : {
11266 : 290 : gfc_symbol *vtab;
11267 : :
11268 : 290 : if (!default_case)
11269 : : {
11270 : : /* Add a default case to hold the CLASS IS cases. */
11271 : 288 : for (tail = code; tail->block; tail = tail->block) ;
11272 : 180 : tail->block = gfc_get_code (EXEC_SELECT_TYPE);
11273 : 180 : tail = tail->block;
11274 : 180 : tail->ext.block.case_list = gfc_get_case ();
11275 : 180 : tail->ext.block.case_list->ts.type = BT_UNKNOWN;
11276 : 180 : tail->next = NULL;
11277 : 180 : default_case = tail;
11278 : : }
11279 : :
11280 : : /* More than one CLASS IS block? */
11281 : 290 : if (class_is->block)
11282 : : {
11283 : 37 : gfc_code **c1,*c2;
11284 : 37 : bool swapped;
11285 : : /* Sort CLASS IS blocks by extension level. */
11286 : 36 : do
11287 : : {
11288 : 37 : swapped = false;
11289 : 97 : for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
11290 : : {
11291 : 61 : c2 = (*c1)->block;
11292 : : /* F03:C817 (check for doubles). */
11293 : 61 : if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
11294 : 61 : == c2->ext.block.case_list->ts.u.derived->hash_value)
11295 : : {
11296 : 1 : gfc_error ("Double CLASS IS block in SELECT TYPE "
11297 : : "statement at %L",
11298 : : &c2->ext.block.case_list->where);
11299 : 1 : return;
11300 : : }
11301 : 60 : if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11302 : 60 : < c2->ext.block.case_list->ts.u.derived->attr.extension)
11303 : : {
11304 : : /* Swap. */
11305 : 24 : (*c1)->block = c2->block;
11306 : 24 : c2->block = *c1;
11307 : 24 : *c1 = c2;
11308 : 24 : swapped = true;
11309 : : }
11310 : : }
11311 : : }
11312 : : while (swapped);
11313 : : }
11314 : :
11315 : : /* Generate IF chain. */
11316 : 289 : if_st = gfc_get_code (EXEC_IF);
11317 : 289 : new_st = if_st;
11318 : 608 : for (body = class_is; body; body = body->block)
11319 : : {
11320 : 319 : new_st->block = gfc_get_code (EXEC_IF);
11321 : 319 : new_st = new_st->block;
11322 : : /* Set up IF condition: Call _gfortran_is_extension_of. */
11323 : 319 : new_st->expr1 = gfc_get_expr ();
11324 : 319 : new_st->expr1->expr_type = EXPR_FUNCTION;
11325 : 319 : new_st->expr1->ts.type = BT_LOGICAL;
11326 : 319 : new_st->expr1->ts.kind = 4;
11327 : 319 : new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11328 : 319 : new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11329 : 319 : new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11330 : : /* Set up arguments. */
11331 : 319 : new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11332 : 319 : new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11333 : 319 : new_st->expr1->value.function.actual->expr->where = code->loc;
11334 : 319 : new_st->expr1->where = code->loc;
11335 : 319 : gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11336 : 319 : vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11337 : 319 : st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11338 : 319 : new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11339 : 319 : new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11340 : 319 : new_st->expr1->value.function.actual->next->expr->where = code->loc;
11341 : : /* Set up types in formal arg list. */
11342 : 319 : new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11343 : 319 : new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11344 : 319 : new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11345 : 319 : new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11346 : :
11347 : 319 : new_st->next = body->next;
11348 : : }
11349 : 289 : if (default_case->next)
11350 : : {
11351 : 110 : new_st->block = gfc_get_code (EXEC_IF);
11352 : 110 : new_st = new_st->block;
11353 : 110 : new_st->next = default_case->next;
11354 : : }
11355 : :
11356 : : /* Replace CLASS DEFAULT code by the IF chain. */
11357 : 289 : default_case->next = if_st;
11358 : : }
11359 : :
11360 : : /* Resolve the internal code. This cannot be done earlier because
11361 : : it requires that the sym->assoc of selectors is set already. */
11362 : 2896 : gfc_current_ns = ns;
11363 : 2896 : gfc_resolve_blocks (code->block, gfc_current_ns);
11364 : 2896 : gfc_current_ns = old_ns;
11365 : :
11366 : 2896 : free (ref);
11367 : : }
11368 : :
11369 : :
11370 : : /* Resolve a SELECT RANK statement. */
11371 : :
11372 : : static void
11373 : 1018 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11374 : : {
11375 : 1018 : gfc_namespace *ns;
11376 : 1018 : gfc_code *body, *new_st, *tail;
11377 : 1018 : gfc_case *c;
11378 : 1018 : char tname[GFC_MAX_SYMBOL_LEN + 7];
11379 : 1018 : char name[2 * GFC_MAX_SYMBOL_LEN];
11380 : 1018 : gfc_symtree *st;
11381 : 1018 : gfc_expr *selector_expr = NULL;
11382 : 1018 : int case_value;
11383 : 1018 : HOST_WIDE_INT charlen = 0;
11384 : :
11385 : 1018 : ns = code->ext.block.ns;
11386 : 1018 : gfc_resolve (ns);
11387 : :
11388 : 1018 : code->op = EXEC_BLOCK;
11389 : 1018 : if (code->expr2)
11390 : : {
11391 : 42 : gfc_association_list* assoc;
11392 : :
11393 : 42 : assoc = gfc_get_association_list ();
11394 : 42 : assoc->st = code->expr1->symtree;
11395 : 42 : assoc->target = gfc_copy_expr (code->expr2);
11396 : 42 : assoc->target->where = code->expr2->where;
11397 : : /* assoc->variable will be set by resolve_assoc_var. */
11398 : :
11399 : 42 : code->ext.block.assoc = assoc;
11400 : 42 : code->expr1->symtree->n.sym->assoc = assoc;
11401 : :
11402 : 42 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11403 : : }
11404 : : else
11405 : 976 : code->ext.block.assoc = NULL;
11406 : :
11407 : : /* Loop over RANK cases. Note that returning on the errors causes a
11408 : : cascade of further errors because the case blocks do not compile
11409 : : correctly. */
11410 : 3320 : for (body = code->block; body; body = body->block)
11411 : : {
11412 : 2302 : c = body->ext.block.case_list;
11413 : 2302 : if (c->low)
11414 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11415 : : else
11416 : : case_value = -2;
11417 : :
11418 : : /* Check for repeated cases. */
11419 : 5836 : for (tail = code->block; tail; tail = tail->block)
11420 : : {
11421 : 5836 : gfc_case *d = tail->ext.block.case_list;
11422 : 5836 : int case_value2;
11423 : :
11424 : 5836 : if (tail == body)
11425 : : break;
11426 : :
11427 : : /* Check F2018: C1153. */
11428 : 3534 : if (!c->low && !d->low)
11429 : 1 : gfc_error ("RANK DEFAULT at %L is repeated at %L",
11430 : : &c->where, &d->where);
11431 : :
11432 : 3534 : if (!c->low || !d->low)
11433 : 1253 : continue;
11434 : :
11435 : : /* Check F2018: C1153. */
11436 : 2281 : case_value2 = (int) mpz_get_si (d->low->value.integer);
11437 : 2281 : if ((case_value == case_value2) && case_value == -1)
11438 : 1 : gfc_error ("RANK (*) at %L is repeated at %L",
11439 : : &c->where, &d->where);
11440 : 2280 : else if (case_value == case_value2)
11441 : 1 : gfc_error ("RANK (%i) at %L is repeated at %L",
11442 : : case_value, &c->where, &d->where);
11443 : : }
11444 : :
11445 : 2302 : if (!c->low)
11446 : 919 : continue;
11447 : :
11448 : : /* Check F2018: C1155. */
11449 : 1383 : if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11450 : 1381 : || gfc_expr_attr (code->expr1).pointer))
11451 : 3 : gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11452 : 3 : "allocatable selector at %L", &c->where, &code->expr1->where);
11453 : : }
11454 : :
11455 : : /* Add EXEC_SELECT to switch on rank. */
11456 : 1018 : new_st = gfc_get_code (code->op);
11457 : 1018 : new_st->expr1 = code->expr1;
11458 : 1018 : new_st->expr2 = code->expr2;
11459 : 1018 : new_st->block = code->block;
11460 : 1018 : code->expr1 = code->expr2 = NULL;
11461 : 1018 : code->block = NULL;
11462 : 1018 : if (!ns->code)
11463 : 1018 : ns->code = new_st;
11464 : : else
11465 : 0 : ns->code->next = new_st;
11466 : 1018 : code = new_st;
11467 : 1018 : code->op = EXEC_SELECT_RANK;
11468 : :
11469 : 1018 : selector_expr = code->expr1;
11470 : :
11471 : : /* Loop over SELECT RANK cases. */
11472 : 3320 : for (body = code->block; body; body = body->block)
11473 : : {
11474 : 2302 : c = body->ext.block.case_list;
11475 : 2302 : int case_value;
11476 : :
11477 : : /* Pass on the default case. */
11478 : 2302 : if (c->low == NULL)
11479 : 919 : continue;
11480 : :
11481 : : /* Associate temporary to selector. This should only be done
11482 : : when this case is actually true, so build a new ASSOCIATE
11483 : : that does precisely this here (instead of using the
11484 : : 'global' one). */
11485 : 1383 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11486 : 265 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11487 : 186 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11488 : :
11489 : 1383 : if (c->ts.type == BT_CLASS)
11490 : 145 : sprintf (tname, "class_%s", c->ts.u.derived->name);
11491 : 1238 : else if (c->ts.type == BT_DERIVED)
11492 : 110 : sprintf (tname, "type_%s", c->ts.u.derived->name);
11493 : 1128 : else if (c->ts.type != BT_CHARACTER)
11494 : 569 : sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11495 : : else
11496 : 559 : sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11497 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11498 : :
11499 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11500 : 1383 : if (case_value >= 0)
11501 : 1350 : sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11502 : : else
11503 : 33 : sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11504 : :
11505 : 1383 : st = gfc_find_symtree (ns->sym_root, name);
11506 : 1383 : gcc_assert (st->n.sym->assoc);
11507 : :
11508 : 1383 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11509 : 1383 : st->n.sym->assoc->target->where = selector_expr->where;
11510 : :
11511 : 1383 : new_st = gfc_get_code (EXEC_BLOCK);
11512 : 1383 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11513 : 1383 : new_st->ext.block.ns->code = body->next;
11514 : 1383 : body->next = new_st;
11515 : :
11516 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11517 : : there is a CASE label overlap and this is already used. Just ignore,
11518 : : the error is diagnosed elsewhere. */
11519 : 1383 : if (st->n.sym->assoc->dangling)
11520 : : {
11521 : 1381 : new_st->ext.block.assoc = st->n.sym->assoc;
11522 : 1381 : st->n.sym->assoc->dangling = 0;
11523 : : }
11524 : :
11525 : 1383 : resolve_assoc_var (st->n.sym, false);
11526 : : }
11527 : :
11528 : 1018 : gfc_current_ns = ns;
11529 : 1018 : gfc_resolve_blocks (code->block, gfc_current_ns);
11530 : 1018 : gfc_current_ns = old_ns;
11531 : 1018 : }
11532 : :
11533 : :
11534 : : /* Resolve a transfer statement. This is making sure that:
11535 : : -- a derived type being transferred has only non-pointer components
11536 : : -- a derived type being transferred doesn't have private components, unless
11537 : : it's being transferred from the module where the type was defined
11538 : : -- we're not trying to transfer a whole assumed size array. */
11539 : :
11540 : : static void
11541 : 45768 : resolve_transfer (gfc_code *code)
11542 : : {
11543 : 45768 : gfc_symbol *sym, *derived;
11544 : 45768 : gfc_ref *ref;
11545 : 45768 : gfc_expr *exp;
11546 : 45768 : bool write = false;
11547 : 45768 : bool formatted = false;
11548 : 45768 : gfc_dt *dt = code->ext.dt;
11549 : 45768 : gfc_symbol *dtio_sub = NULL;
11550 : :
11551 : 45768 : exp = code->expr1;
11552 : :
11553 : 91542 : while (exp != NULL && exp->expr_type == EXPR_OP
11554 : 46654 : && exp->value.op.op == INTRINSIC_PARENTHESES)
11555 : 6 : exp = exp->value.op.op1;
11556 : :
11557 : 45768 : if (exp && exp->expr_type == EXPR_NULL
11558 : 2 : && code->ext.dt)
11559 : : {
11560 : 2 : gfc_error ("Invalid context for NULL () intrinsic at %L",
11561 : : &exp->where);
11562 : 2 : return;
11563 : : }
11564 : :
11565 : : if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11566 : : && exp->expr_type != EXPR_FUNCTION
11567 : : && exp->expr_type != EXPR_ARRAY
11568 : : && exp->expr_type != EXPR_STRUCTURE))
11569 : : return;
11570 : :
11571 : : /* If we are reading, the variable will be changed. Note that
11572 : : code->ext.dt may be NULL if the TRANSFER is related to
11573 : : an INQUIRE statement -- but in this case, we are not reading, either. */
11574 : 25043 : if (dt && dt->dt_io_kind->value.iokind == M_READ
11575 : 32451 : && !gfc_check_vardef_context (exp, false, false, false,
11576 : 7260 : _("item in READ")))
11577 : : return;
11578 : :
11579 : 25187 : const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11580 : 25187 : || exp->expr_type == EXPR_FUNCTION
11581 : 20842 : || exp->expr_type == EXPR_ARRAY
11582 : 46029 : ? &exp->ts : &exp->symtree->n.sym->ts;
11583 : :
11584 : : /* Go to actual component transferred. */
11585 : 32816 : for (ref = exp->ref; ref; ref = ref->next)
11586 : 7629 : if (ref->type == REF_COMPONENT)
11587 : 2149 : ts = &ref->u.c.component->ts;
11588 : :
11589 : 25187 : if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11590 : 25039 : && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11591 : : {
11592 : 698 : derived = ts->u.derived;
11593 : :
11594 : : /* Determine when to use the formatted DTIO procedure. */
11595 : 698 : if (dt && (dt->format_expr || dt->format_label))
11596 : 623 : formatted = true;
11597 : :
11598 : 698 : write = dt->dt_io_kind->value.iokind == M_WRITE
11599 : 698 : || dt->dt_io_kind->value.iokind == M_PRINT;
11600 : 698 : dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11601 : :
11602 : 698 : if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11603 : : {
11604 : 431 : dt->udtio = exp;
11605 : 431 : sym = exp->symtree->n.sym->ns->proc_name;
11606 : : /* Check to see if this is a nested DTIO call, with the
11607 : : dummy as the io-list object. */
11608 : 431 : if (sym && sym == dtio_sub && sym->formal
11609 : 30 : && sym->formal->sym == exp->symtree->n.sym
11610 : 30 : && exp->ref == NULL)
11611 : : {
11612 : 0 : if (!sym->attr.recursive)
11613 : : {
11614 : 0 : gfc_error ("DTIO %s procedure at %L must be recursive",
11615 : : sym->name, &sym->declared_at);
11616 : 0 : return;
11617 : : }
11618 : : }
11619 : : }
11620 : : }
11621 : :
11622 : 25187 : if (ts->type == BT_CLASS && dtio_sub == NULL)
11623 : : {
11624 : 3 : gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11625 : : "it is processed by a defined input/output procedure",
11626 : : &code->loc);
11627 : 3 : return;
11628 : : }
11629 : :
11630 : 25184 : if (ts->type == BT_DERIVED)
11631 : : {
11632 : : /* Check that transferred derived type doesn't contain POINTER
11633 : : components unless it is processed by a defined input/output
11634 : : procedure". */
11635 : 666 : if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11636 : : {
11637 : 2 : gfc_error ("Data transfer element at %L cannot have POINTER "
11638 : : "components unless it is processed by a defined "
11639 : : "input/output procedure", &code->loc);
11640 : 2 : return;
11641 : : }
11642 : :
11643 : : /* F08:C935. */
11644 : 664 : if (ts->u.derived->attr.proc_pointer_comp)
11645 : : {
11646 : 2 : gfc_error ("Data transfer element at %L cannot have "
11647 : : "procedure pointer components", &code->loc);
11648 : 2 : return;
11649 : : }
11650 : :
11651 : 662 : if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11652 : : {
11653 : 6 : gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11654 : : "components unless it is processed by a defined "
11655 : : "input/output procedure", &code->loc);
11656 : 6 : return;
11657 : : }
11658 : :
11659 : : /* C_PTR and C_FUNPTR have private components which means they cannot
11660 : : be printed. However, if -std=gnu and not -pedantic, allow
11661 : : the component to be printed to help debugging. */
11662 : 656 : if (ts->u.derived->ts.f90_type == BT_VOID)
11663 : : {
11664 : 4 : gfc_error ("Data transfer element at %L "
11665 : : "cannot have PRIVATE components", &code->loc);
11666 : 4 : return;
11667 : : }
11668 : 652 : else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11669 : : {
11670 : 4 : gfc_error ("Data transfer element at %L cannot have "
11671 : : "PRIVATE components unless it is processed by "
11672 : : "a defined input/output procedure", &code->loc);
11673 : 4 : return;
11674 : : }
11675 : : }
11676 : :
11677 : 25166 : if (exp->expr_type == EXPR_STRUCTURE)
11678 : : return;
11679 : :
11680 : 25121 : if (exp->expr_type == EXPR_ARRAY)
11681 : : return;
11682 : :
11683 : 24745 : sym = exp->symtree->n.sym;
11684 : :
11685 : 24745 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11686 : 81 : && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11687 : : {
11688 : 1 : gfc_error ("Data transfer element at %L cannot be a full reference to "
11689 : : "an assumed-size array", &code->loc);
11690 : 1 : return;
11691 : : }
11692 : : }
11693 : :
11694 : :
11695 : : /*********** Toplevel code resolution subroutines ***********/
11696 : :
11697 : : /* Find the set of labels that are reachable from this block. We also
11698 : : record the last statement in each block. */
11699 : :
11700 : : static void
11701 : 662611 : find_reachable_labels (gfc_code *block)
11702 : : {
11703 : 662611 : gfc_code *c;
11704 : :
11705 : 662611 : if (!block)
11706 : : return;
11707 : :
11708 : 415719 : cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11709 : :
11710 : : /* Collect labels in this block. We don't keep those corresponding
11711 : : to END {IF|SELECT}, these are checked in resolve_branch by going
11712 : : up through the code_stack. */
11713 : 1525275 : for (c = block; c; c = c->next)
11714 : : {
11715 : 1109556 : if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11716 : 3589 : bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11717 : : }
11718 : :
11719 : : /* Merge with labels from parent block. */
11720 : 415719 : if (cs_base->prev)
11721 : : {
11722 : 341574 : gcc_assert (cs_base->prev->reachable_labels);
11723 : 341574 : bitmap_ior_into (cs_base->reachable_labels,
11724 : : cs_base->prev->reachable_labels);
11725 : : }
11726 : : }
11727 : :
11728 : : static void
11729 : 136 : resolve_lock_unlock_event (gfc_code *code)
11730 : : {
11731 : 136 : if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11732 : 136 : && (code->expr1->ts.type != BT_DERIVED
11733 : 95 : || code->expr1->expr_type != EXPR_VARIABLE
11734 : 95 : || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11735 : 94 : || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11736 : 94 : || code->expr1->rank != 0
11737 : 124 : || (!gfc_is_coarray (code->expr1) &&
11738 : 31 : !gfc_is_coindexed (code->expr1))))
11739 : 4 : gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11740 : 4 : &code->expr1->where);
11741 : 132 : else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11742 : 39 : && (code->expr1->ts.type != BT_DERIVED
11743 : 39 : || code->expr1->expr_type != EXPR_VARIABLE
11744 : 39 : || code->expr1->ts.u.derived->from_intmod
11745 : : != INTMOD_ISO_FORTRAN_ENV
11746 : 39 : || code->expr1->ts.u.derived->intmod_sym_id
11747 : : != ISOFORTRAN_EVENT_TYPE
11748 : 39 : || code->expr1->rank != 0))
11749 : 0 : gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11750 : : &code->expr1->where);
11751 : 23 : else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11752 : 143 : && !gfc_is_coindexed (code->expr1))
11753 : 0 : gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11754 : 0 : &code->expr1->where);
11755 : 132 : else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11756 : 0 : gfc_error ("Event variable argument at %L must be a coarray but not "
11757 : 0 : "coindexed", &code->expr1->where);
11758 : :
11759 : : /* Check STAT. */
11760 : 136 : if (code->expr2
11761 : 38 : && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11762 : 38 : || code->expr2->expr_type != EXPR_VARIABLE))
11763 : 0 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11764 : : &code->expr2->where);
11765 : :
11766 : 136 : if (code->expr2
11767 : 174 : && !gfc_check_vardef_context (code->expr2, false, false, false,
11768 : 38 : _("STAT variable")))
11769 : : return;
11770 : :
11771 : : /* Check ERRMSG. */
11772 : 136 : if (code->expr3
11773 : 2 : && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11774 : 2 : || code->expr3->expr_type != EXPR_VARIABLE))
11775 : 0 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11776 : : &code->expr3->where);
11777 : :
11778 : 136 : if (code->expr3
11779 : 138 : && !gfc_check_vardef_context (code->expr3, false, false, false,
11780 : 2 : _("ERRMSG variable")))
11781 : : return;
11782 : :
11783 : : /* Check for LOCK the ACQUIRED_LOCK. */
11784 : 136 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11785 : 16 : && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11786 : 16 : || code->expr4->expr_type != EXPR_VARIABLE))
11787 : 0 : gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11788 : : "variable", &code->expr4->where);
11789 : :
11790 : 120 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11791 : 152 : && !gfc_check_vardef_context (code->expr4, false, false, false,
11792 : 16 : _("ACQUIRED_LOCK variable")))
11793 : : return;
11794 : :
11795 : : /* Check for EVENT WAIT the UNTIL_COUNT. */
11796 : 136 : if (code->op == EXEC_EVENT_WAIT && code->expr4)
11797 : : {
11798 : 24 : if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11799 : 24 : || code->expr4->rank != 0)
11800 : 0 : gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11801 : 0 : "expression", &code->expr4->where);
11802 : : }
11803 : : }
11804 : :
11805 : : static void
11806 : 195 : resolve_team_argument (gfc_expr *team)
11807 : : {
11808 : 195 : gfc_resolve_expr (team);
11809 : 195 : if (team->rank != 0 || team->ts.type != BT_DERIVED
11810 : 188 : || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11811 : 188 : || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
11812 : : {
11813 : 7 : gfc_error ("TEAM argument at %L must be a scalar expression "
11814 : : "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
11815 : : &team->where);
11816 : : }
11817 : 195 : }
11818 : :
11819 : : static void
11820 : 1228 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
11821 : : gfc_expr *e)
11822 : : {
11823 : 1228 : gfc_resolve_expr (e);
11824 : 1228 : if (e
11825 : 139 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
11826 : 124 : || e->expr_type != EXPR_VARIABLE))
11827 : 15 : gfc_error ("%s argument at %L must be a scalar %s variable of at least "
11828 : : "kind %d", name, &e->where, gfc_basic_typename (exp_type),
11829 : : exp_kind);
11830 : 1228 : }
11831 : :
11832 : : void
11833 : 614 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
11834 : : {
11835 : 614 : resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
11836 : 614 : resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
11837 : : gfc_default_character_kind,
11838 : : sync_stat->errmsg);
11839 : 614 : }
11840 : :
11841 : : static void
11842 : 214 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
11843 : : gfc_expr *e)
11844 : : {
11845 : 214 : gfc_resolve_expr (e);
11846 : 214 : if (e
11847 : 140 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
11848 : 3 : gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
11849 : : name, &e->where, gfc_basic_typename (exp_type), exp_kind);
11850 : 214 : }
11851 : :
11852 : : static void
11853 : 107 : resolve_form_team (gfc_code *code)
11854 : : {
11855 : 107 : resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
11856 : : code->expr1);
11857 : 107 : resolve_team_argument (code->expr2);
11858 : 107 : resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
11859 : : code->expr3);
11860 : 107 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11861 : 107 : }
11862 : :
11863 : : static void resolve_block_construct (gfc_code *);
11864 : :
11865 : : static void
11866 : 66 : resolve_change_team (gfc_code *code)
11867 : : {
11868 : 66 : resolve_team_argument (code->expr1);
11869 : 66 : gfc_resolve_sync_stat (&code->ext.block.sync_stat);
11870 : 132 : resolve_block_construct (code);
11871 : : /* Map the coarray bounds as selected. */
11872 : 68 : for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
11873 : 2 : if (a->ar)
11874 : : {
11875 : 2 : gfc_array_spec *src = a->ar->as, *dst;
11876 : 2 : if (a->st->n.sym->ts.type == BT_CLASS)
11877 : 0 : dst = CLASS_DATA (a->st->n.sym)->as;
11878 : : else
11879 : 2 : dst = a->st->n.sym->as;
11880 : 2 : dst->corank = src->corank;
11881 : 2 : dst->cotype = src->cotype;
11882 : 4 : for (int i = 0; i < src->corank; ++i)
11883 : : {
11884 : 2 : dst->lower[dst->rank + i] = src->lower[i];
11885 : 2 : dst->upper[dst->rank + i] = src->upper[i];
11886 : 2 : src->lower[i] = src->upper[i] = nullptr;
11887 : : }
11888 : 2 : gfc_free_array_spec (src);
11889 : 2 : free (a->ar);
11890 : 2 : a->ar = nullptr;
11891 : 2 : dst->resolved = false;
11892 : 2 : gfc_resolve_array_spec (dst, 0);
11893 : : }
11894 : 66 : }
11895 : :
11896 : : static void
11897 : 22 : resolve_sync_team (gfc_code *code)
11898 : : {
11899 : 22 : resolve_team_argument (code->expr1);
11900 : 22 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11901 : 22 : }
11902 : :
11903 : : static void
11904 : 64 : resolve_end_team (gfc_code *code)
11905 : : {
11906 : 64 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11907 : 64 : }
11908 : :
11909 : : static void
11910 : 50 : resolve_critical (gfc_code *code)
11911 : : {
11912 : 50 : gfc_symtree *symtree;
11913 : 50 : gfc_symbol *lock_type;
11914 : 50 : char name[GFC_MAX_SYMBOL_LEN];
11915 : 50 : static int serial = 0;
11916 : :
11917 : 50 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11918 : :
11919 : 50 : if (flag_coarray != GFC_FCOARRAY_LIB)
11920 : 30 : return;
11921 : :
11922 : 20 : symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11923 : : GFC_PREFIX ("lock_type"));
11924 : 20 : if (symtree)
11925 : 10 : lock_type = symtree->n.sym;
11926 : : else
11927 : : {
11928 : 10 : if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
11929 : : false) != 0)
11930 : 0 : gcc_unreachable ();
11931 : 10 : lock_type = symtree->n.sym;
11932 : 10 : lock_type->attr.flavor = FL_DERIVED;
11933 : 10 : lock_type->attr.zero_comp = 1;
11934 : 10 : lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
11935 : 10 : lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
11936 : : }
11937 : :
11938 : 20 : sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
11939 : 20 : if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
11940 : 0 : gcc_unreachable ();
11941 : :
11942 : 20 : code->resolved_sym = symtree->n.sym;
11943 : 20 : symtree->n.sym->attr.flavor = FL_VARIABLE;
11944 : 20 : symtree->n.sym->attr.referenced = 1;
11945 : 20 : symtree->n.sym->attr.artificial = 1;
11946 : 20 : symtree->n.sym->attr.codimension = 1;
11947 : 20 : symtree->n.sym->ts.type = BT_DERIVED;
11948 : 20 : symtree->n.sym->ts.u.derived = lock_type;
11949 : 20 : symtree->n.sym->as = gfc_get_array_spec ();
11950 : 20 : symtree->n.sym->as->corank = 1;
11951 : 20 : symtree->n.sym->as->type = AS_EXPLICIT;
11952 : 20 : symtree->n.sym->as->cotype = AS_EXPLICIT;
11953 : 20 : symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
11954 : : NULL, 1);
11955 : 20 : gfc_commit_symbols();
11956 : : }
11957 : :
11958 : :
11959 : : static void
11960 : 747 : resolve_sync (gfc_code *code)
11961 : : {
11962 : : /* Check imageset. The * case matches expr1 == NULL. */
11963 : 747 : if (code->expr1)
11964 : : {
11965 : 48 : if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
11966 : 1 : gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
11967 : : "INTEGER expression", &code->expr1->where);
11968 : 48 : if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
11969 : 23 : && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
11970 : 1 : gfc_error ("Imageset argument at %L must between 1 and num_images()",
11971 : : &code->expr1->where);
11972 : 47 : else if (code->expr1->expr_type == EXPR_ARRAY
11973 : 47 : && gfc_simplify_expr (code->expr1, 0))
11974 : : {
11975 : 18 : gfc_constructor *cons;
11976 : 18 : cons = gfc_constructor_first (code->expr1->value.constructor);
11977 : 54 : for (; cons; cons = gfc_constructor_next (cons))
11978 : 18 : if (cons->expr->expr_type == EXPR_CONSTANT
11979 : 18 : && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
11980 : 0 : gfc_error ("Imageset argument at %L must between 1 and "
11981 : : "num_images()", &cons->expr->where);
11982 : : }
11983 : : }
11984 : :
11985 : : /* Check STAT. */
11986 : 747 : gfc_resolve_expr (code->expr2);
11987 : 747 : if (code->expr2)
11988 : : {
11989 : 84 : if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
11990 : 1 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11991 : : &code->expr2->where);
11992 : : else
11993 : 83 : gfc_check_vardef_context (code->expr2, false, false, false,
11994 : 83 : _("STAT variable"));
11995 : : }
11996 : :
11997 : : /* Check ERRMSG. */
11998 : 747 : gfc_resolve_expr (code->expr3);
11999 : 747 : if (code->expr3)
12000 : : {
12001 : 75 : if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
12002 : 4 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
12003 : : &code->expr3->where);
12004 : : else
12005 : 71 : gfc_check_vardef_context (code->expr3, false, false, false,
12006 : 71 : _("ERRMSG variable"));
12007 : : }
12008 : 747 : }
12009 : :
12010 : :
12011 : : /* Given a branch to a label, see if the branch is conforming.
12012 : : The code node describes where the branch is located. */
12013 : :
12014 : : static void
12015 : 106991 : resolve_branch (gfc_st_label *label, gfc_code *code)
12016 : : {
12017 : 106991 : code_stack *stack;
12018 : :
12019 : 106991 : if (label == NULL)
12020 : : return;
12021 : :
12022 : : /* Step one: is this a valid branching target? */
12023 : :
12024 : 2459 : if (label->defined == ST_LABEL_UNKNOWN)
12025 : : {
12026 : 4 : gfc_error ("Label %d referenced at %L is never defined", label->value,
12027 : : &code->loc);
12028 : 4 : return;
12029 : : }
12030 : :
12031 : 2455 : if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
12032 : : {
12033 : 4 : gfc_error ("Statement at %L is not a valid branch target statement "
12034 : : "for the branch statement at %L", &label->where, &code->loc);
12035 : 4 : return;
12036 : : }
12037 : :
12038 : : /* Step two: make sure this branch is not a branch to itself ;-) */
12039 : :
12040 : 2451 : if (code->here == label)
12041 : : {
12042 : 0 : gfc_warning (0, "Branch at %L may result in an infinite loop",
12043 : : &code->loc);
12044 : 0 : return;
12045 : : }
12046 : :
12047 : : /* Step three: See if the label is in the same block as the
12048 : : branching statement. The hard work has been done by setting up
12049 : : the bitmap reachable_labels. */
12050 : :
12051 : 2451 : if (bitmap_bit_p (cs_base->reachable_labels, label->value))
12052 : : {
12053 : : /* Check now whether there is a CRITICAL construct; if so, check
12054 : : whether the label is still visible outside of the CRITICAL block,
12055 : : which is invalid. */
12056 : 6264 : for (stack = cs_base; stack; stack = stack->prev)
12057 : : {
12058 : 3881 : if (stack->current->op == EXEC_CRITICAL
12059 : 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
12060 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
12061 : : "label at %L", &code->loc, &label->where);
12062 : 3879 : else if (stack->current->op == EXEC_DO_CONCURRENT
12063 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
12064 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
12065 : : "for label at %L", &code->loc, &label->where);
12066 : 3879 : else if (stack->current->op == EXEC_CHANGE_TEAM
12067 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
12068 : 1 : gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
12069 : : "for label at %L", &code->loc, &label->where);
12070 : : }
12071 : :
12072 : : return;
12073 : : }
12074 : :
12075 : : /* Step four: If we haven't found the label in the bitmap, it may
12076 : : still be the label of the END of the enclosing block, in which
12077 : : case we find it by going up the code_stack. */
12078 : :
12079 : 167 : for (stack = cs_base; stack; stack = stack->prev)
12080 : : {
12081 : 131 : if (stack->current->next && stack->current->next->here == label)
12082 : : break;
12083 : 101 : if (stack->current->op == EXEC_CRITICAL)
12084 : : {
12085 : : /* Note: A label at END CRITICAL does not leave the CRITICAL
12086 : : construct as END CRITICAL is still part of it. */
12087 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
12088 : : " at %L", &code->loc, &label->where);
12089 : 2 : return;
12090 : : }
12091 : 99 : else if (stack->current->op == EXEC_DO_CONCURRENT)
12092 : : {
12093 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
12094 : : "label at %L", &code->loc, &label->where);
12095 : 0 : return;
12096 : : }
12097 : : }
12098 : :
12099 : 66 : if (stack)
12100 : : {
12101 : 30 : gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
12102 : : return;
12103 : : }
12104 : :
12105 : : /* The label is not in an enclosing block, so illegal. This was
12106 : : allowed in Fortran 66, so we allow it as extension. No
12107 : : further checks are necessary in this case. */
12108 : 36 : gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
12109 : : "as the GOTO statement at %L", &label->where,
12110 : : &code->loc);
12111 : 36 : return;
12112 : : }
12113 : :
12114 : :
12115 : : /* Check whether EXPR1 has the same shape as EXPR2. */
12116 : :
12117 : : static bool
12118 : 1461 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
12119 : : {
12120 : 1461 : mpz_t shape[GFC_MAX_DIMENSIONS];
12121 : 1461 : mpz_t shape2[GFC_MAX_DIMENSIONS];
12122 : 1461 : bool result = false;
12123 : 1461 : int i;
12124 : :
12125 : : /* Compare the rank. */
12126 : 1461 : if (expr1->rank != expr2->rank)
12127 : : return result;
12128 : :
12129 : : /* Compare the size of each dimension. */
12130 : 2795 : for (i=0; i<expr1->rank; i++)
12131 : : {
12132 : 1484 : if (!gfc_array_dimen_size (expr1, i, &shape[i]))
12133 : 150 : goto ignore;
12134 : :
12135 : 1334 : if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
12136 : 0 : goto ignore;
12137 : :
12138 : 1334 : if (mpz_cmp (shape[i], shape2[i]))
12139 : 0 : goto over;
12140 : : }
12141 : :
12142 : : /* When either of the two expression is an assumed size array, we
12143 : : ignore the comparison of dimension sizes. */
12144 : 1311 : ignore:
12145 : : result = true;
12146 : :
12147 : 1461 : over:
12148 : 1461 : gfc_clear_shape (shape, i);
12149 : 1461 : gfc_clear_shape (shape2, i);
12150 : 1461 : return result;
12151 : : }
12152 : :
12153 : :
12154 : : /* Check whether a WHERE assignment target or a WHERE mask expression
12155 : : has the same shape as the outmost WHERE mask expression. */
12156 : :
12157 : : static void
12158 : 506 : resolve_where (gfc_code *code, gfc_expr *mask)
12159 : : {
12160 : 506 : gfc_code *cblock;
12161 : 506 : gfc_code *cnext;
12162 : 506 : gfc_expr *e = NULL;
12163 : :
12164 : 506 : cblock = code->block;
12165 : :
12166 : : /* Store the first WHERE mask-expr of the WHERE statement or construct.
12167 : : In case of nested WHERE, only the outmost one is stored. */
12168 : 506 : if (mask == NULL) /* outmost WHERE */
12169 : 450 : e = cblock->expr1;
12170 : : else /* inner WHERE */
12171 : 506 : e = mask;
12172 : :
12173 : 1381 : while (cblock)
12174 : : {
12175 : 875 : if (cblock->expr1)
12176 : : {
12177 : : /* Check if the mask-expr has a consistent shape with the
12178 : : outmost WHERE mask-expr. */
12179 : 711 : if (!resolve_where_shape (cblock->expr1, e))
12180 : 0 : gfc_error ("WHERE mask at %L has inconsistent shape",
12181 : 0 : &cblock->expr1->where);
12182 : : }
12183 : :
12184 : : /* the assignment statement of a WHERE statement, or the first
12185 : : statement in where-body-construct of a WHERE construct */
12186 : 875 : cnext = cblock->next;
12187 : 1727 : while (cnext)
12188 : : {
12189 : 852 : switch (cnext->op)
12190 : : {
12191 : : /* WHERE assignment statement */
12192 : 750 : case EXEC_ASSIGN:
12193 : :
12194 : : /* Check shape consistent for WHERE assignment target. */
12195 : 750 : if (e && !resolve_where_shape (cnext->expr1, e))
12196 : 0 : gfc_error ("WHERE assignment target at %L has "
12197 : 0 : "inconsistent shape", &cnext->expr1->where);
12198 : :
12199 : 750 : if (cnext->op == EXEC_ASSIGN
12200 : 750 : && gfc_may_be_finalized (cnext->expr1->ts))
12201 : 0 : cnext->expr1->must_finalize = 1;
12202 : :
12203 : : break;
12204 : :
12205 : :
12206 : 46 : case EXEC_ASSIGN_CALL:
12207 : 46 : resolve_call (cnext);
12208 : 46 : if (!cnext->resolved_sym->attr.elemental)
12209 : 2 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12210 : 2 : &cnext->ext.actual->expr->where);
12211 : : break;
12212 : :
12213 : : /* WHERE or WHERE construct is part of a where-body-construct */
12214 : 56 : case EXEC_WHERE:
12215 : 56 : resolve_where (cnext, e);
12216 : 56 : break;
12217 : :
12218 : 0 : default:
12219 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12220 : : &cnext->loc);
12221 : : }
12222 : : /* the next statement within the same where-body-construct */
12223 : 852 : cnext = cnext->next;
12224 : : }
12225 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12226 : 875 : cblock = cblock->block;
12227 : : }
12228 : 506 : }
12229 : :
12230 : :
12231 : : /* Resolve assignment in FORALL construct.
12232 : : NVAR is the number of FORALL index variables, and VAR_EXPR records the
12233 : : FORALL index variables. */
12234 : :
12235 : : static void
12236 : 1944 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
12237 : : {
12238 : 1944 : int n;
12239 : :
12240 : 5871 : for (n = 0; n < nvar; n++)
12241 : : {
12242 : 3927 : gfc_symbol *forall_index;
12243 : :
12244 : 3927 : forall_index = var_expr[n]->symtree->n.sym;
12245 : :
12246 : : /* Check whether the assignment target is one of the FORALL index
12247 : : variable. */
12248 : 3927 : if ((code->expr1->expr_type == EXPR_VARIABLE)
12249 : 3927 : && (code->expr1->symtree->n.sym == forall_index))
12250 : 0 : gfc_error ("Assignment to a FORALL index variable at %L",
12251 : : &code->expr1->where);
12252 : : else
12253 : : {
12254 : : /* If one of the FORALL index variables doesn't appear in the
12255 : : assignment variable, then there could be a many-to-one
12256 : : assignment. Emit a warning rather than an error because the
12257 : : mask could be resolving this problem. */
12258 : 3927 : if (!find_forall_index (code->expr1, forall_index, 0))
12259 : 0 : gfc_warning (0, "The FORALL with index %qs is not used on the "
12260 : : "left side of the assignment at %L and so might "
12261 : : "cause multiple assignment to this object",
12262 : 0 : var_expr[n]->symtree->name, &code->expr1->where);
12263 : : }
12264 : : }
12265 : 1944 : }
12266 : :
12267 : :
12268 : : /* Resolve WHERE statement in FORALL construct. */
12269 : :
12270 : : static void
12271 : 46 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
12272 : : gfc_expr **var_expr)
12273 : : {
12274 : 46 : gfc_code *cblock;
12275 : 46 : gfc_code *cnext;
12276 : :
12277 : 46 : cblock = code->block;
12278 : 111 : while (cblock)
12279 : : {
12280 : : /* the assignment statement of a WHERE statement, or the first
12281 : : statement in where-body-construct of a WHERE construct */
12282 : 65 : cnext = cblock->next;
12283 : 130 : while (cnext)
12284 : : {
12285 : 65 : switch (cnext->op)
12286 : : {
12287 : : /* WHERE assignment statement */
12288 : 65 : case EXEC_ASSIGN:
12289 : 65 : gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
12290 : :
12291 : 65 : if (cnext->op == EXEC_ASSIGN
12292 : 65 : && gfc_may_be_finalized (cnext->expr1->ts))
12293 : 0 : cnext->expr1->must_finalize = 1;
12294 : :
12295 : : break;
12296 : :
12297 : : /* WHERE operator assignment statement */
12298 : 0 : case EXEC_ASSIGN_CALL:
12299 : 0 : resolve_call (cnext);
12300 : 0 : if (!cnext->resolved_sym->attr.elemental)
12301 : 0 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12302 : 0 : &cnext->ext.actual->expr->where);
12303 : : break;
12304 : :
12305 : : /* WHERE or WHERE construct is part of a where-body-construct */
12306 : 0 : case EXEC_WHERE:
12307 : 0 : gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
12308 : 0 : break;
12309 : :
12310 : 0 : default:
12311 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12312 : : &cnext->loc);
12313 : : }
12314 : : /* the next statement within the same where-body-construct */
12315 : 65 : cnext = cnext->next;
12316 : : }
12317 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12318 : 65 : cblock = cblock->block;
12319 : : }
12320 : 46 : }
12321 : :
12322 : :
12323 : : /* Traverse the FORALL body to check whether the following errors exist:
12324 : : 1. For assignment, check if a many-to-one assignment happens.
12325 : : 2. For WHERE statement, check the WHERE body to see if there is any
12326 : : many-to-one assignment. */
12327 : :
12328 : : static void
12329 : 1990 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
12330 : : {
12331 : 1990 : gfc_code *c;
12332 : :
12333 : 1990 : c = code->block->next;
12334 : 3993 : while (c)
12335 : : {
12336 : 2003 : switch (c->op)
12337 : : {
12338 : 1879 : case EXEC_ASSIGN:
12339 : 1879 : case EXEC_POINTER_ASSIGN:
12340 : 1879 : gfc_resolve_assign_in_forall (c, nvar, var_expr);
12341 : :
12342 : 1879 : if (c->op == EXEC_ASSIGN
12343 : 1879 : && gfc_may_be_finalized (c->expr1->ts))
12344 : 0 : c->expr1->must_finalize = 1;
12345 : :
12346 : : break;
12347 : :
12348 : 0 : case EXEC_ASSIGN_CALL:
12349 : 0 : resolve_call (c);
12350 : 0 : break;
12351 : :
12352 : : /* Because the gfc_resolve_blocks() will handle the nested FORALL,
12353 : : there is no need to handle it here. */
12354 : : case EXEC_FORALL:
12355 : : break;
12356 : 46 : case EXEC_WHERE:
12357 : 46 : gfc_resolve_where_code_in_forall(c, nvar, var_expr);
12358 : 46 : break;
12359 : : default:
12360 : : break;
12361 : : }
12362 : : /* The next statement in the FORALL body. */
12363 : 2003 : c = c->next;
12364 : : }
12365 : 1990 : }
12366 : :
12367 : :
12368 : : /* Counts the number of iterators needed inside a forall construct, including
12369 : : nested forall constructs. This is used to allocate the needed memory
12370 : : in gfc_resolve_forall. */
12371 : :
12372 : : static int
12373 : 1990 : gfc_count_forall_iterators (gfc_code *code)
12374 : : {
12375 : 1990 : int max_iters, sub_iters, current_iters;
12376 : 1990 : gfc_forall_iterator *fa;
12377 : :
12378 : 1990 : gcc_assert(code->op == EXEC_FORALL);
12379 : 1990 : max_iters = 0;
12380 : 1990 : current_iters = 0;
12381 : :
12382 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12383 : 3888 : current_iters ++;
12384 : :
12385 : 1990 : code = code->block->next;
12386 : :
12387 : 3993 : while (code)
12388 : : {
12389 : 2003 : if (code->op == EXEC_FORALL)
12390 : : {
12391 : 78 : sub_iters = gfc_count_forall_iterators (code);
12392 : 78 : if (sub_iters > max_iters)
12393 : 2003 : max_iters = sub_iters;
12394 : : }
12395 : 2003 : code = code->next;
12396 : : }
12397 : :
12398 : 1990 : return current_iters + max_iters;
12399 : : }
12400 : :
12401 : :
12402 : : /* Given a FORALL construct, first resolve the FORALL iterator, then call
12403 : : gfc_resolve_forall_body to resolve the FORALL body. */
12404 : :
12405 : : static void
12406 : 1990 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
12407 : : {
12408 : 1990 : static gfc_expr **var_expr;
12409 : 1990 : static int total_var = 0;
12410 : 1990 : static int nvar = 0;
12411 : 1990 : int i, old_nvar, tmp;
12412 : 1990 : gfc_forall_iterator *fa;
12413 : :
12414 : 1990 : old_nvar = nvar;
12415 : :
12416 : 1990 : if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12417 : : return;
12418 : :
12419 : : /* Start to resolve a FORALL construct */
12420 : 1990 : if (forall_save == 0)
12421 : : {
12422 : : /* Count the total number of FORALL indices in the nested FORALL
12423 : : construct in order to allocate the VAR_EXPR with proper size. */
12424 : 1912 : total_var = gfc_count_forall_iterators (code);
12425 : :
12426 : : /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12427 : 1912 : var_expr = XCNEWVEC (gfc_expr *, total_var);
12428 : : }
12429 : :
12430 : : /* The information about FORALL iterator, including FORALL indices start, end
12431 : : and stride. An outer FORALL indice cannot appear in start, end or stride. */
12432 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12433 : : {
12434 : : /* Fortran 20008: C738 (R753). */
12435 : 3888 : if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12436 : : {
12437 : 2 : gfc_error ("FORALL index-name at %L must be a scalar variable "
12438 : : "of type integer", &fa->var->where);
12439 : 2 : continue;
12440 : : }
12441 : :
12442 : : /* Check if any outer FORALL index name is the same as the current
12443 : : one. */
12444 : 6859 : for (i = 0; i < nvar; i++)
12445 : : {
12446 : 2973 : if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12447 : 0 : gfc_error ("An outer FORALL construct already has an index "
12448 : : "with this name %L", &fa->var->where);
12449 : : }
12450 : :
12451 : : /* Record the current FORALL index. */
12452 : 3886 : var_expr[nvar] = gfc_copy_expr (fa->var);
12453 : :
12454 : 3886 : nvar++;
12455 : :
12456 : : /* No memory leak. */
12457 : 3886 : gcc_assert (nvar <= total_var);
12458 : : }
12459 : :
12460 : : /* Resolve the FORALL body. */
12461 : 1990 : gfc_resolve_forall_body (code, nvar, var_expr);
12462 : :
12463 : : /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12464 : 1990 : gfc_resolve_blocks (code->block, ns);
12465 : :
12466 : 1990 : tmp = nvar;
12467 : 1990 : nvar = old_nvar;
12468 : : /* Free only the VAR_EXPRs allocated in this frame. */
12469 : 5876 : for (i = nvar; i < tmp; i++)
12470 : 3886 : gfc_free_expr (var_expr[i]);
12471 : :
12472 : 1990 : if (nvar == 0)
12473 : : {
12474 : : /* We are in the outermost FORALL construct. */
12475 : 1912 : gcc_assert (forall_save == 0);
12476 : :
12477 : : /* VAR_EXPR is not needed any more. */
12478 : 1912 : free (var_expr);
12479 : 1912 : total_var = 0;
12480 : : }
12481 : : }
12482 : :
12483 : :
12484 : : /* Resolve a BLOCK construct statement. */
12485 : :
12486 : : static void
12487 : 7672 : resolve_block_construct (gfc_code* code)
12488 : : {
12489 : 7672 : gfc_namespace *ns = code->ext.block.ns;
12490 : :
12491 : : /* For an ASSOCIATE block, the associations (and their targets) will be
12492 : : resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12493 : : namespace. */
12494 : 7672 : gfc_resolve (ns);
12495 : 0 : }
12496 : :
12497 : :
12498 : : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12499 : : DO code nodes. */
12500 : :
12501 : : void
12502 : 324806 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12503 : : {
12504 : 324806 : bool t;
12505 : :
12506 : 660840 : for (; b; b = b->block)
12507 : : {
12508 : 336034 : t = gfc_resolve_expr (b->expr1);
12509 : 336034 : if (!gfc_resolve_expr (b->expr2))
12510 : 0 : t = false;
12511 : :
12512 : 336034 : switch (b->op)
12513 : : {
12514 : 231858 : case EXEC_IF:
12515 : 231858 : if (t && b->expr1 != NULL
12516 : 227730 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12517 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12518 : : &b->expr1->where);
12519 : : break;
12520 : :
12521 : 761 : case EXEC_WHERE:
12522 : 761 : if (t
12523 : 761 : && b->expr1 != NULL
12524 : 628 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12525 : 0 : gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12526 : : &b->expr1->where);
12527 : : break;
12528 : :
12529 : 76 : case EXEC_GOTO:
12530 : 76 : resolve_branch (b->label1, b);
12531 : 76 : break;
12532 : :
12533 : 0 : case EXEC_BLOCK:
12534 : 0 : resolve_block_construct (b);
12535 : 0 : break;
12536 : :
12537 : : case EXEC_SELECT:
12538 : : case EXEC_SELECT_TYPE:
12539 : : case EXEC_SELECT_RANK:
12540 : : case EXEC_FORALL:
12541 : : case EXEC_DO:
12542 : : case EXEC_DO_WHILE:
12543 : : case EXEC_DO_CONCURRENT:
12544 : : case EXEC_CRITICAL:
12545 : : case EXEC_READ:
12546 : : case EXEC_WRITE:
12547 : : case EXEC_IOLENGTH:
12548 : : case EXEC_WAIT:
12549 : : break;
12550 : :
12551 : 2697 : case EXEC_OMP_ATOMIC:
12552 : 2697 : case EXEC_OACC_ATOMIC:
12553 : 2697 : {
12554 : : /* Verify this before calling gfc_resolve_code, which might
12555 : : change it. */
12556 : 2697 : gcc_assert (b->op == EXEC_OMP_ATOMIC
12557 : : || (b->next && b->next->op == EXEC_ASSIGN));
12558 : : }
12559 : : break;
12560 : :
12561 : : case EXEC_OACC_PARALLEL_LOOP:
12562 : : case EXEC_OACC_PARALLEL:
12563 : : case EXEC_OACC_KERNELS_LOOP:
12564 : : case EXEC_OACC_KERNELS:
12565 : : case EXEC_OACC_SERIAL_LOOP:
12566 : : case EXEC_OACC_SERIAL:
12567 : : case EXEC_OACC_DATA:
12568 : : case EXEC_OACC_HOST_DATA:
12569 : : case EXEC_OACC_LOOP:
12570 : : case EXEC_OACC_UPDATE:
12571 : : case EXEC_OACC_WAIT:
12572 : : case EXEC_OACC_CACHE:
12573 : : case EXEC_OACC_ENTER_DATA:
12574 : : case EXEC_OACC_EXIT_DATA:
12575 : : case EXEC_OACC_ROUTINE:
12576 : : case EXEC_OMP_ALLOCATE:
12577 : : case EXEC_OMP_ALLOCATORS:
12578 : : case EXEC_OMP_ASSUME:
12579 : : case EXEC_OMP_CRITICAL:
12580 : : case EXEC_OMP_DISPATCH:
12581 : : case EXEC_OMP_DISTRIBUTE:
12582 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12583 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12584 : : case EXEC_OMP_DISTRIBUTE_SIMD:
12585 : : case EXEC_OMP_DO:
12586 : : case EXEC_OMP_DO_SIMD:
12587 : : case EXEC_OMP_ERROR:
12588 : : case EXEC_OMP_LOOP:
12589 : : case EXEC_OMP_MASKED:
12590 : : case EXEC_OMP_MASKED_TASKLOOP:
12591 : : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12592 : : case EXEC_OMP_MASTER:
12593 : : case EXEC_OMP_MASTER_TASKLOOP:
12594 : : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12595 : : case EXEC_OMP_ORDERED:
12596 : : case EXEC_OMP_PARALLEL:
12597 : : case EXEC_OMP_PARALLEL_DO:
12598 : : case EXEC_OMP_PARALLEL_DO_SIMD:
12599 : : case EXEC_OMP_PARALLEL_LOOP:
12600 : : case EXEC_OMP_PARALLEL_MASKED:
12601 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12602 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12603 : : case EXEC_OMP_PARALLEL_MASTER:
12604 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12605 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12606 : : case EXEC_OMP_PARALLEL_SECTIONS:
12607 : : case EXEC_OMP_PARALLEL_WORKSHARE:
12608 : : case EXEC_OMP_SECTIONS:
12609 : : case EXEC_OMP_SIMD:
12610 : : case EXEC_OMP_SCOPE:
12611 : : case EXEC_OMP_SINGLE:
12612 : : case EXEC_OMP_TARGET:
12613 : : case EXEC_OMP_TARGET_DATA:
12614 : : case EXEC_OMP_TARGET_ENTER_DATA:
12615 : : case EXEC_OMP_TARGET_EXIT_DATA:
12616 : : case EXEC_OMP_TARGET_PARALLEL:
12617 : : case EXEC_OMP_TARGET_PARALLEL_DO:
12618 : : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12619 : : case EXEC_OMP_TARGET_PARALLEL_LOOP:
12620 : : case EXEC_OMP_TARGET_SIMD:
12621 : : case EXEC_OMP_TARGET_TEAMS:
12622 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12623 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12624 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12625 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12626 : : case EXEC_OMP_TARGET_TEAMS_LOOP:
12627 : : case EXEC_OMP_TARGET_UPDATE:
12628 : : case EXEC_OMP_TASK:
12629 : : case EXEC_OMP_TASKGROUP:
12630 : : case EXEC_OMP_TASKLOOP:
12631 : : case EXEC_OMP_TASKLOOP_SIMD:
12632 : : case EXEC_OMP_TASKWAIT:
12633 : : case EXEC_OMP_TASKYIELD:
12634 : : case EXEC_OMP_TEAMS:
12635 : : case EXEC_OMP_TEAMS_DISTRIBUTE:
12636 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12637 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12638 : : case EXEC_OMP_TEAMS_LOOP:
12639 : : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12640 : : case EXEC_OMP_TILE:
12641 : : case EXEC_OMP_UNROLL:
12642 : : case EXEC_OMP_WORKSHARE:
12643 : : break;
12644 : :
12645 : 0 : default:
12646 : 0 : gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
12647 : : }
12648 : :
12649 : 336034 : gfc_resolve_code (b->next, ns);
12650 : : }
12651 : 324806 : }
12652 : :
12653 : : bool
12654 : 0 : caf_possible_reallocate (gfc_expr *e)
12655 : : {
12656 : 0 : symbol_attribute caf_attr;
12657 : 0 : gfc_ref *last_arr_ref = nullptr;
12658 : :
12659 : 0 : caf_attr = gfc_caf_attr (e);
12660 : 0 : if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
12661 : : return false;
12662 : :
12663 : : /* Only full array refs can indicate a needed reallocation. */
12664 : 0 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
12665 : 0 : if (ref->type == REF_ARRAY && ref->u.ar.dimen)
12666 : 0 : last_arr_ref = ref;
12667 : :
12668 : 0 : return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
12669 : : }
12670 : :
12671 : : /* Does everything to resolve an ordinary assignment. Returns true
12672 : : if this is an interface assignment. */
12673 : : static bool
12674 : 280241 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
12675 : : {
12676 : 280241 : bool rval = false;
12677 : 280241 : gfc_expr *lhs;
12678 : 280241 : gfc_expr *rhs;
12679 : 280241 : int n;
12680 : 280241 : gfc_ref *ref;
12681 : 280241 : symbol_attribute attr;
12682 : :
12683 : 280241 : if (gfc_extend_assign (code, ns))
12684 : : {
12685 : 776 : gfc_expr** rhsptr;
12686 : :
12687 : 776 : if (code->op == EXEC_ASSIGN_CALL)
12688 : : {
12689 : 360 : lhs = code->ext.actual->expr;
12690 : 360 : rhsptr = &code->ext.actual->next->expr;
12691 : : }
12692 : : else
12693 : : {
12694 : 416 : gfc_actual_arglist* args;
12695 : 416 : gfc_typebound_proc* tbp;
12696 : :
12697 : 416 : gcc_assert (code->op == EXEC_COMPCALL);
12698 : :
12699 : 416 : args = code->expr1->value.compcall.actual;
12700 : 416 : lhs = args->expr;
12701 : 416 : rhsptr = &args->next->expr;
12702 : :
12703 : 416 : tbp = code->expr1->value.compcall.tbp;
12704 : 416 : gcc_assert (!tbp->is_generic);
12705 : : }
12706 : :
12707 : : /* Make a temporary rhs when there is a default initializer
12708 : : and rhs is the same symbol as the lhs. */
12709 : 776 : if ((*rhsptr)->expr_type == EXPR_VARIABLE
12710 : 392 : && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
12711 : 339 : && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
12712 : 967 : && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
12713 : 24 : *rhsptr = gfc_get_parentheses (*rhsptr);
12714 : :
12715 : 776 : return true;
12716 : : }
12717 : :
12718 : 279465 : lhs = code->expr1;
12719 : 279465 : rhs = code->expr2;
12720 : :
12721 : 279465 : if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
12722 : 260850 : || lhs->symtree->n.sym->ts.type == BT_CLASS)
12723 : 21046 : && !lhs->symtree->n.sym->attr.proc_pointer
12724 : 300511 : && gfc_expr_attr (lhs).proc_pointer)
12725 : : {
12726 : 1 : gfc_error ("Variable in the ordinary assignment at %L is a procedure "
12727 : : "pointer component",
12728 : : &lhs->where);
12729 : 1 : return false;
12730 : : }
12731 : :
12732 : 328142 : if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
12733 : 246074 : && rhs->ts.type == BT_CHARACTER
12734 : 279857 : && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
12735 : : {
12736 : : /* Use of -fdec-char-conversions allows assignment of character data
12737 : : to non-character variables. This not permitted for nonconstant
12738 : : strings. */
12739 : 29 : gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
12740 : : gfc_typename (lhs), &rhs->where);
12741 : 29 : return false;
12742 : : }
12743 : :
12744 : 279435 : if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
12745 : : {
12746 : 0 : gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
12747 : : gfc_typename (lhs), &rhs->where);
12748 : 0 : return false;
12749 : : }
12750 : :
12751 : : /* Handle the case of a BOZ literal on the RHS. */
12752 : 279435 : if (rhs->ts.type == BT_BOZ)
12753 : : {
12754 : 3 : if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
12755 : : "statement value nor an actual argument of "
12756 : : "INT/REAL/DBLE/CMPLX intrinsic subprogram",
12757 : : &rhs->where))
12758 : : return false;
12759 : :
12760 : 1 : switch (lhs->ts.type)
12761 : : {
12762 : 0 : case BT_INTEGER:
12763 : 0 : if (!gfc_boz2int (rhs, lhs->ts.kind))
12764 : : return false;
12765 : : break;
12766 : 1 : case BT_REAL:
12767 : 1 : if (!gfc_boz2real (rhs, lhs->ts.kind))
12768 : : return false;
12769 : : break;
12770 : 0 : default:
12771 : 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
12772 : 0 : return false;
12773 : : }
12774 : : }
12775 : :
12776 : 279433 : if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
12777 : : {
12778 : 64 : HOST_WIDE_INT llen = 0, rlen = 0;
12779 : 64 : if (lhs->ts.u.cl != NULL
12780 : 64 : && lhs->ts.u.cl->length != NULL
12781 : 53 : && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12782 : 53 : llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
12783 : :
12784 : 64 : if (rhs->expr_type == EXPR_CONSTANT)
12785 : 26 : rlen = rhs->value.character.length;
12786 : :
12787 : 38 : else if (rhs->ts.u.cl != NULL
12788 : 38 : && rhs->ts.u.cl->length != NULL
12789 : 35 : && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12790 : 35 : rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
12791 : :
12792 : 64 : if (rlen && llen && rlen > llen)
12793 : 28 : gfc_warning_now (OPT_Wcharacter_truncation,
12794 : : "CHARACTER expression will be truncated "
12795 : : "in assignment (%wd/%wd) at %L",
12796 : : llen, rlen, &code->loc);
12797 : : }
12798 : :
12799 : : /* Ensure that a vector index expression for the lvalue is evaluated
12800 : : to a temporary if the lvalue symbol is referenced in it. */
12801 : 279433 : if (lhs->rank)
12802 : : {
12803 : 107019 : for (ref = lhs->ref; ref; ref= ref->next)
12804 : 56725 : if (ref->type == REF_ARRAY)
12805 : : {
12806 : 127265 : for (n = 0; n < ref->u.ar.dimen; n++)
12807 : 75522 : if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
12808 : 75747 : && gfc_find_sym_in_expr (lhs->symtree->n.sym,
12809 : 225 : ref->u.ar.start[n]))
12810 : 14 : ref->u.ar.start[n]
12811 : 14 : = gfc_get_parentheses (ref->u.ar.start[n]);
12812 : : }
12813 : : }
12814 : :
12815 : 279433 : if (gfc_pure (NULL))
12816 : : {
12817 : 3257 : if (lhs->ts.type == BT_DERIVED
12818 : 84 : && lhs->expr_type == EXPR_VARIABLE
12819 : 84 : && lhs->ts.u.derived->attr.pointer_comp
12820 : 4 : && rhs->expr_type == EXPR_VARIABLE
12821 : 3260 : && (gfc_impure_variable (rhs->symtree->n.sym)
12822 : 2 : || gfc_is_coindexed (rhs)))
12823 : : {
12824 : : /* F2008, C1283. */
12825 : 2 : if (gfc_is_coindexed (rhs))
12826 : 1 : gfc_error ("Coindexed expression at %L is assigned to "
12827 : : "a derived type variable with a POINTER "
12828 : : "component in a PURE procedure",
12829 : : &rhs->where);
12830 : : else
12831 : : /* F2008, C1283 (4). */
12832 : 1 : gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
12833 : : "shall not be used as the expr at %L of an intrinsic "
12834 : : "assignment statement in which the variable is of a "
12835 : : "derived type if the derived type has a pointer "
12836 : : "component at any level of component selection.",
12837 : : &rhs->where);
12838 : 2 : return rval;
12839 : : }
12840 : :
12841 : : /* Fortran 2008, C1283. */
12842 : 3255 : if (gfc_is_coindexed (lhs))
12843 : : {
12844 : 1 : gfc_error ("Assignment to coindexed variable at %L in a PURE "
12845 : : "procedure", &rhs->where);
12846 : 1 : return rval;
12847 : : }
12848 : : }
12849 : :
12850 : 279430 : if (gfc_implicit_pure (NULL))
12851 : : {
12852 : 7081 : if (lhs->expr_type == EXPR_VARIABLE
12853 : 7081 : && lhs->symtree->n.sym != gfc_current_ns->proc_name
12854 : 5046 : && lhs->symtree->n.sym->ns != gfc_current_ns)
12855 : 243 : gfc_unset_implicit_pure (NULL);
12856 : :
12857 : 7081 : if (lhs->ts.type == BT_DERIVED
12858 : 299 : && lhs->expr_type == EXPR_VARIABLE
12859 : 299 : && lhs->ts.u.derived->attr.pointer_comp
12860 : 7 : && rhs->expr_type == EXPR_VARIABLE
12861 : 7088 : && (gfc_impure_variable (rhs->symtree->n.sym)
12862 : 7 : || gfc_is_coindexed (rhs)))
12863 : 0 : gfc_unset_implicit_pure (NULL);
12864 : :
12865 : : /* Fortran 2008, C1283. */
12866 : 7081 : if (gfc_is_coindexed (lhs))
12867 : 0 : gfc_unset_implicit_pure (NULL);
12868 : : }
12869 : :
12870 : : /* F2008, 7.2.1.2. */
12871 : 279430 : attr = gfc_expr_attr (lhs);
12872 : 279430 : if (lhs->ts.type == BT_CLASS && attr.allocatable)
12873 : : {
12874 : 900 : if (attr.codimension)
12875 : : {
12876 : 1 : gfc_error ("Assignment to polymorphic coarray at %L is not "
12877 : : "permitted", &lhs->where);
12878 : 1 : return false;
12879 : : }
12880 : 899 : if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
12881 : : "polymorphic variable at %L", &lhs->where))
12882 : : return false;
12883 : 898 : if (!flag_realloc_lhs)
12884 : : {
12885 : 1 : gfc_error ("Assignment to an allocatable polymorphic variable at %L "
12886 : : "requires %<-frealloc-lhs%>", &lhs->where);
12887 : 1 : return false;
12888 : : }
12889 : : }
12890 : 278530 : else if (lhs->ts.type == BT_CLASS)
12891 : : {
12892 : 9 : gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
12893 : : "assignment at %L - check that there is a matching specific "
12894 : : "subroutine for %<=%> operator", &lhs->where);
12895 : 9 : return false;
12896 : : }
12897 : :
12898 : 279418 : bool lhs_coindexed = gfc_is_coindexed (lhs);
12899 : :
12900 : : /* F2008, Section 7.2.1.2. */
12901 : 279418 : if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
12902 : : {
12903 : 1 : gfc_error ("Coindexed variable must not have an allocatable ultimate "
12904 : : "component in assignment at %L", &lhs->where);
12905 : 1 : return false;
12906 : : }
12907 : :
12908 : : /* Assign the 'data' of a class object to a derived type. */
12909 : 279417 : if (lhs->ts.type == BT_DERIVED
12910 : 6728 : && rhs->ts.type == BT_CLASS
12911 : 137 : && rhs->expr_type != EXPR_ARRAY)
12912 : 131 : gfc_add_data_component (rhs);
12913 : :
12914 : : /* Make sure there is a vtable and, in particular, a _copy for the
12915 : : rhs type. */
12916 : 279417 : if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
12917 : 540 : gfc_find_vtab (&rhs->ts);
12918 : :
12919 : 279417 : gfc_check_assign (lhs, rhs, 1);
12920 : :
12921 : 279417 : return false;
12922 : : }
12923 : :
12924 : :
12925 : : /* Add a component reference onto an expression. */
12926 : :
12927 : : static void
12928 : 665 : add_comp_ref (gfc_expr *e, gfc_component *c)
12929 : : {
12930 : 665 : gfc_ref **ref;
12931 : 665 : ref = &(e->ref);
12932 : 889 : while (*ref)
12933 : 224 : ref = &((*ref)->next);
12934 : 665 : *ref = gfc_get_ref ();
12935 : 665 : (*ref)->type = REF_COMPONENT;
12936 : 665 : (*ref)->u.c.sym = e->ts.u.derived;
12937 : 665 : (*ref)->u.c.component = c;
12938 : 665 : e->ts = c->ts;
12939 : :
12940 : : /* Add a full array ref, as necessary. */
12941 : 665 : if (c->as)
12942 : : {
12943 : 84 : gfc_add_full_array_ref (e, c->as);
12944 : 84 : e->rank = c->as->rank;
12945 : 84 : e->corank = c->as->corank;
12946 : : }
12947 : 665 : }
12948 : :
12949 : :
12950 : : /* Build an assignment. Keep the argument 'op' for future use, so that
12951 : : pointer assignments can be made. */
12952 : :
12953 : : static gfc_code *
12954 : 898 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
12955 : : gfc_component *comp1, gfc_component *comp2, locus loc)
12956 : : {
12957 : 898 : gfc_code *this_code;
12958 : :
12959 : 898 : this_code = gfc_get_code (op);
12960 : 898 : this_code->next = NULL;
12961 : 898 : this_code->expr1 = gfc_copy_expr (expr1);
12962 : 898 : this_code->expr2 = gfc_copy_expr (expr2);
12963 : 898 : this_code->loc = loc;
12964 : 898 : if (comp1 && comp2)
12965 : : {
12966 : 288 : add_comp_ref (this_code->expr1, comp1);
12967 : 288 : add_comp_ref (this_code->expr2, comp2);
12968 : : }
12969 : :
12970 : 898 : return this_code;
12971 : : }
12972 : :
12973 : :
12974 : : /* Makes a temporary variable expression based on the characteristics of
12975 : : a given variable expression. */
12976 : :
12977 : : static gfc_expr*
12978 : 392 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12979 : : {
12980 : 392 : static int serial = 0;
12981 : 392 : char name[GFC_MAX_SYMBOL_LEN];
12982 : 392 : gfc_symtree *tmp;
12983 : 392 : gfc_array_spec *as;
12984 : 392 : gfc_array_ref *aref;
12985 : 392 : gfc_ref *ref;
12986 : :
12987 : 392 : sprintf (name, GFC_PREFIX("DA%d"), serial++);
12988 : 392 : gfc_get_sym_tree (name, ns, &tmp, false);
12989 : 392 : gfc_add_type (tmp->n.sym, &e->ts, NULL);
12990 : :
12991 : 392 : if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12992 : 0 : tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12993 : : NULL,
12994 : 0 : e->value.character.length);
12995 : :
12996 : 392 : as = NULL;
12997 : 392 : ref = NULL;
12998 : 392 : aref = NULL;
12999 : :
13000 : : /* Obtain the arrayspec for the temporary. */
13001 : 392 : if (e->rank && e->expr_type != EXPR_ARRAY
13002 : : && e->expr_type != EXPR_FUNCTION
13003 : : && e->expr_type != EXPR_OP)
13004 : : {
13005 : 52 : aref = gfc_find_array_ref (e);
13006 : 52 : if (e->expr_type == EXPR_VARIABLE
13007 : 52 : && e->symtree->n.sym->as == aref->as)
13008 : : as = aref->as;
13009 : : else
13010 : : {
13011 : 0 : for (ref = e->ref; ref; ref = ref->next)
13012 : 0 : if (ref->type == REF_COMPONENT
13013 : 0 : && ref->u.c.component->as == aref->as)
13014 : : {
13015 : : as = aref->as;
13016 : : break;
13017 : : }
13018 : : }
13019 : : }
13020 : :
13021 : : /* Add the attributes and the arrayspec to the temporary. */
13022 : 392 : tmp->n.sym->attr = gfc_expr_attr (e);
13023 : 392 : tmp->n.sym->attr.function = 0;
13024 : 392 : tmp->n.sym->attr.proc_pointer = 0;
13025 : 392 : tmp->n.sym->attr.result = 0;
13026 : 392 : tmp->n.sym->attr.flavor = FL_VARIABLE;
13027 : 392 : tmp->n.sym->attr.dummy = 0;
13028 : 392 : tmp->n.sym->attr.use_assoc = 0;
13029 : 392 : tmp->n.sym->attr.intent = INTENT_UNKNOWN;
13030 : :
13031 : :
13032 : 392 : if (as)
13033 : : {
13034 : 52 : tmp->n.sym->as = gfc_copy_array_spec (as);
13035 : 52 : if (!ref)
13036 : 52 : ref = e->ref;
13037 : 52 : if (as->type == AS_DEFERRED)
13038 : 46 : tmp->n.sym->attr.allocatable = 1;
13039 : : }
13040 : 340 : else if ((e->rank || e->corank)
13041 : 48 : && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
13042 : 0 : || e->expr_type == EXPR_OP))
13043 : : {
13044 : 48 : tmp->n.sym->as = gfc_get_array_spec ();
13045 : 48 : tmp->n.sym->as->type = AS_DEFERRED;
13046 : 48 : tmp->n.sym->as->rank = e->rank;
13047 : 48 : tmp->n.sym->as->corank = e->corank;
13048 : 48 : tmp->n.sym->attr.allocatable = 1;
13049 : 48 : tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
13050 : 96 : tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
13051 : : }
13052 : : else
13053 : 292 : tmp->n.sym->attr.dimension = 0;
13054 : :
13055 : 392 : gfc_set_sym_referenced (tmp->n.sym);
13056 : 392 : gfc_commit_symbol (tmp->n.sym);
13057 : 392 : e = gfc_lval_expr_from_sym (tmp->n.sym);
13058 : :
13059 : : /* Should the lhs be a section, use its array ref for the
13060 : : temporary expression. */
13061 : 392 : if (aref && aref->type != AR_FULL)
13062 : : {
13063 : 6 : gfc_free_ref_list (e->ref);
13064 : 6 : e->ref = gfc_copy_ref (ref);
13065 : : }
13066 : 392 : return e;
13067 : : }
13068 : :
13069 : :
13070 : : /* Add one line of code to the code chain, making sure that 'head' and
13071 : : 'tail' are appropriately updated. */
13072 : :
13073 : : static void
13074 : 656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
13075 : : {
13076 : 656 : gcc_assert (this_code);
13077 : 656 : if (*head == NULL)
13078 : 308 : *head = *tail = *this_code;
13079 : : else
13080 : 348 : *tail = gfc_append_code (*tail, *this_code);
13081 : 656 : *this_code = NULL;
13082 : 656 : }
13083 : :
13084 : :
13085 : : /* Generate a final call from a variable expression */
13086 : :
13087 : : static void
13088 : 81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
13089 : : {
13090 : 81 : gfc_code *this_code;
13091 : 81 : gfc_expr *final_expr = NULL;
13092 : 81 : gfc_expr *size_expr;
13093 : 81 : gfc_expr *fini_coarray;
13094 : :
13095 : 81 : gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
13096 : 81 : if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
13097 : 75 : return;
13098 : :
13099 : : /* Now generate the finalizer call. */
13100 : 6 : this_code = gfc_get_code (EXEC_CALL);
13101 : 6 : this_code->symtree = final_expr->symtree;
13102 : 6 : this_code->resolved_sym = final_expr->symtree->n.sym;
13103 : :
13104 : : //* Expression to be finalized */
13105 : 6 : this_code->ext.actual = gfc_get_actual_arglist ();
13106 : 6 : this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
13107 : :
13108 : : /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
13109 : 6 : this_code->ext.actual->next = gfc_get_actual_arglist ();
13110 : 6 : size_expr = gfc_get_expr ();
13111 : 6 : size_expr->where = gfc_current_locus;
13112 : 6 : size_expr->expr_type = EXPR_OP;
13113 : 6 : size_expr->value.op.op = INTRINSIC_DIVIDE;
13114 : 6 : size_expr->value.op.op1
13115 : 12 : = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
13116 : : "storage_size", gfc_current_locus, 2,
13117 : 6 : gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
13118 : : gfc_get_int_expr (gfc_index_integer_kind,
13119 : : NULL, 0));
13120 : 6 : size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
13121 : : gfc_character_storage_size);
13122 : 6 : size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
13123 : 6 : size_expr->ts = size_expr->value.op.op1->ts;
13124 : 6 : this_code->ext.actual->next->expr = size_expr;
13125 : :
13126 : : /* fini_coarray */
13127 : 6 : this_code->ext.actual->next->next = gfc_get_actual_arglist ();
13128 : 6 : fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
13129 : : &tmp_expr->where);
13130 : 6 : fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
13131 : 6 : this_code->ext.actual->next->next->expr = fini_coarray;
13132 : :
13133 : 6 : add_code_to_chain (&this_code, head, tail);
13134 : :
13135 : : }
13136 : :
13137 : : /* Counts the potential number of part array references that would
13138 : : result from resolution of typebound defined assignments. */
13139 : :
13140 : :
13141 : : static int
13142 : 243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
13143 : : {
13144 : 243 : gfc_component *c;
13145 : 243 : int c_depth = 0, t_depth;
13146 : :
13147 : 584 : for (c= derived->components; c; c = c->next)
13148 : : {
13149 : 341 : if ((!gfc_bt_struct (c->ts.type)
13150 : 261 : || c->attr.pointer
13151 : 261 : || c->attr.allocatable
13152 : 260 : || c->attr.proc_pointer_comp
13153 : 260 : || c->attr.class_pointer
13154 : 260 : || c->attr.proc_pointer)
13155 : 81 : && !c->attr.defined_assign_comp)
13156 : 81 : continue;
13157 : :
13158 : 260 : if (c->as && c_depth == 0)
13159 : 260 : c_depth = 1;
13160 : :
13161 : 260 : if (c->ts.u.derived->attr.defined_assign_comp)
13162 : 110 : t_depth = nonscalar_typebound_assign (c->ts.u.derived,
13163 : : c->as ? 1 : 0);
13164 : : else
13165 : : t_depth = 0;
13166 : :
13167 : 260 : c_depth = t_depth > c_depth ? t_depth : c_depth;
13168 : : }
13169 : 243 : return depth + c_depth;
13170 : : }
13171 : :
13172 : :
13173 : : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
13174 : : "An intrinsic assignment where the variable is of derived type is performed
13175 : : as if each component of the variable were assigned from the corresponding
13176 : : component of expr using pointer assignment (10.2.2) for each pointer
13177 : : component, defined assignment for each nonpointer nonallocatable component
13178 : : of a type that has a type-bound defined assignment consistent with the
13179 : : component, intrinsic assignment for each other nonpointer nonallocatable
13180 : : component, and intrinsic assignment for each allocated coarray component.
13181 : : For unallocated coarray components, the corresponding component of the
13182 : : variable shall be unallocated. For a noncoarray allocatable component the
13183 : : following sequence of operations is applied.
13184 : : (1) If the component of the variable is allocated, it is deallocated.
13185 : : (2) If the component of the value of expr is allocated, the
13186 : : corresponding component of the variable is allocated with the same
13187 : : dynamic type and type parameters as the component of the value of
13188 : : expr. If it is an array, it is allocated with the same bounds. The
13189 : : value of the component of the value of expr is then assigned to the
13190 : : corresponding component of the variable using defined assignment if
13191 : : the declared type of the component has a type-bound defined
13192 : : assignment consistent with the component, and intrinsic assignment
13193 : : for the dynamic type of that component otherwise."
13194 : :
13195 : : The pointer assignments are taken care of by the intrinsic assignment of the
13196 : : structure itself. This function recursively adds defined assignments where
13197 : : required. The recursion is accomplished by calling gfc_resolve_code.
13198 : :
13199 : : When the lhs in a defined assignment has intent INOUT or is intent OUT
13200 : : and the component of 'var' is finalizable, we need a temporary for the
13201 : : lhs. In pseudo-code for an assignment var = expr:
13202 : :
13203 : : ! Confine finalization of temporaries, as far as possible.
13204 : : Enclose the code for the assignment in a block
13205 : : ! Only call function 'expr' once.
13206 : : #if ('expr is not a constant or an variable)
13207 : : temp_expr = expr
13208 : : expr = temp_x
13209 : : ! Do the intrinsic assignment
13210 : : #if typeof ('var') has a typebound final subroutine
13211 : : finalize (var)
13212 : : var = expr
13213 : : ! Now do the component assignments
13214 : : #do over derived type components [%cmp]
13215 : : #if (cmp is a pointer of any kind)
13216 : : continue
13217 : : build the assignment
13218 : : resolve the code
13219 : : #if the code is a typebound assignment
13220 : : #if (arg1 is INOUT or finalizable OUT && !t1)
13221 : : t1 = var
13222 : : arg1 = t1
13223 : : deal with allocatation or not of var and this component
13224 : : #elseif the code is an assignment by itself
13225 : : #if this component does not need finalization
13226 : : delete code and continue
13227 : : #else
13228 : : remove the leading assignment
13229 : : #endif
13230 : : commit the code
13231 : : #if (t1 and (arg1 is INOUT or finalizable OUT))
13232 : : var%cmp = t1%cmp
13233 : : #enddo
13234 : : put all code chunks involving t1 to the top of the generated code
13235 : : insert the generated block in place of the original code
13236 : : */
13237 : :
13238 : : static bool
13239 : 381 : is_finalizable_type (gfc_typespec ts)
13240 : : {
13241 : 381 : gfc_component *c;
13242 : :
13243 : 381 : if (ts.type != BT_DERIVED)
13244 : : return false;
13245 : :
13246 : : /* (1) Check for FINAL subroutines. */
13247 : 381 : if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
13248 : : return true;
13249 : :
13250 : : /* (2) Check for components of finalizable type. */
13251 : 809 : for (c = ts.u.derived->components; c; c = c->next)
13252 : 470 : if (c->ts.type == BT_DERIVED
13253 : 243 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
13254 : 242 : && c->ts.u.derived->f2k_derived
13255 : 242 : && c->ts.u.derived->f2k_derived->finalizers)
13256 : : return true;
13257 : :
13258 : : return false;
13259 : : }
13260 : :
13261 : : /* The temporary assignments have to be put on top of the additional
13262 : : code to avoid the result being changed by the intrinsic assignment.
13263 : : */
13264 : : static int component_assignment_level = 0;
13265 : : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
13266 : : static bool finalizable_comp;
13267 : :
13268 : : static void
13269 : 188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
13270 : : {
13271 : 188 : gfc_component *comp1, *comp2;
13272 : 188 : gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
13273 : 188 : gfc_code *tmp_code = NULL;
13274 : 188 : gfc_expr *t1 = NULL;
13275 : 188 : gfc_expr *tmp_expr = NULL;
13276 : 188 : int error_count, depth;
13277 : 188 : bool finalizable_lhs;
13278 : :
13279 : 188 : gfc_get_errors (NULL, &error_count);
13280 : :
13281 : : /* Filter out continuing processing after an error. */
13282 : 188 : if (error_count
13283 : 188 : || (*code)->expr1->ts.type != BT_DERIVED
13284 : 188 : || (*code)->expr2->ts.type != BT_DERIVED)
13285 : 140 : return;
13286 : :
13287 : : /* TODO: Handle more than one part array reference in assignments. */
13288 : 188 : depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
13289 : 188 : (*code)->expr1->rank ? 1 : 0);
13290 : 188 : if (depth > 1)
13291 : : {
13292 : 6 : gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
13293 : : "done because multiple part array references would "
13294 : : "occur in intermediate expressions.", &(*code)->loc);
13295 : 6 : return;
13296 : : }
13297 : :
13298 : 182 : if (!component_assignment_level)
13299 : 134 : finalizable_comp = true;
13300 : :
13301 : : /* Build a block so that function result temporaries are finalized
13302 : : locally on exiting the rather than enclosing scope. */
13303 : 182 : if (!component_assignment_level)
13304 : : {
13305 : 134 : ns = gfc_build_block_ns (ns);
13306 : 134 : tmp_code = gfc_get_code (EXEC_NOP);
13307 : 134 : *tmp_code = **code;
13308 : 134 : tmp_code->next = NULL;
13309 : 134 : (*code)->op = EXEC_BLOCK;
13310 : 134 : (*code)->ext.block.ns = ns;
13311 : 134 : (*code)->ext.block.assoc = NULL;
13312 : 134 : (*code)->expr1 = (*code)->expr2 = NULL;
13313 : 134 : ns->code = tmp_code;
13314 : 134 : code = &ns->code;
13315 : : }
13316 : :
13317 : 182 : component_assignment_level++;
13318 : :
13319 : 182 : finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
13320 : :
13321 : : /* Create a temporary so that functions get called only once. */
13322 : 182 : if ((*code)->expr2->expr_type != EXPR_VARIABLE
13323 : 182 : && (*code)->expr2->expr_type != EXPR_CONSTANT)
13324 : : {
13325 : : /* Assign the rhs to the temporary. */
13326 : 81 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13327 : 81 : if (tmp_expr->symtree->n.sym->attr.pointer)
13328 : : {
13329 : : /* Use allocate on assignment for the sake of simplicity. The
13330 : : temporary must not take on the optional attribute. Assume
13331 : : that the assignment is guarded by a PRESENT condition if the
13332 : : lhs is optional. */
13333 : 25 : tmp_expr->symtree->n.sym->attr.pointer = 0;
13334 : 25 : tmp_expr->symtree->n.sym->attr.optional = 0;
13335 : 25 : tmp_expr->symtree->n.sym->attr.allocatable = 1;
13336 : : }
13337 : 162 : this_code = build_assignment (EXEC_ASSIGN,
13338 : : tmp_expr, (*code)->expr2,
13339 : 81 : NULL, NULL, (*code)->loc);
13340 : 81 : this_code->expr2->must_finalize = 1;
13341 : : /* Add the code and substitute the rhs expression. */
13342 : 81 : add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
13343 : 81 : gfc_free_expr ((*code)->expr2);
13344 : 81 : (*code)->expr2 = tmp_expr;
13345 : : }
13346 : :
13347 : : /* Do the intrinsic assignment. This is not needed if the lhs is one
13348 : : of the temporaries generated here, since the intrinsic assignment
13349 : : to the final result already does this. */
13350 : 182 : if ((*code)->expr1->symtree->n.sym->name[2] != '.')
13351 : : {
13352 : 182 : if (finalizable_lhs)
13353 : 18 : (*code)->expr1->must_finalize = 1;
13354 : 182 : this_code = build_assignment (EXEC_ASSIGN,
13355 : : (*code)->expr1, (*code)->expr2,
13356 : : NULL, NULL, (*code)->loc);
13357 : 182 : add_code_to_chain (&this_code, &head, &tail);
13358 : : }
13359 : :
13360 : 182 : comp1 = (*code)->expr1->ts.u.derived->components;
13361 : 182 : comp2 = (*code)->expr2->ts.u.derived->components;
13362 : :
13363 : 449 : for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
13364 : : {
13365 : 267 : bool inout = false;
13366 : 267 : bool finalizable_out = false;
13367 : :
13368 : : /* The intrinsic assignment does the right thing for pointers
13369 : : of all kinds and allocatable components. */
13370 : 267 : if (!gfc_bt_struct (comp1->ts.type)
13371 : 200 : || comp1->attr.pointer
13372 : 200 : || comp1->attr.allocatable
13373 : 199 : || comp1->attr.proc_pointer_comp
13374 : 199 : || comp1->attr.class_pointer
13375 : 199 : || comp1->attr.proc_pointer)
13376 : 68 : continue;
13377 : :
13378 : 398 : finalizable_comp = is_finalizable_type (comp1->ts)
13379 : 199 : && !finalizable_lhs;
13380 : :
13381 : : /* Make an assignment for this component. */
13382 : 398 : this_code = build_assignment (EXEC_ASSIGN,
13383 : : (*code)->expr1, (*code)->expr2,
13384 : 199 : comp1, comp2, (*code)->loc);
13385 : :
13386 : : /* Convert the assignment if there is a defined assignment for
13387 : : this type. Otherwise, using the call from gfc_resolve_code,
13388 : : recurse into its components. */
13389 : 199 : gfc_resolve_code (this_code, ns);
13390 : :
13391 : 199 : if (this_code->op == EXEC_ASSIGN_CALL)
13392 : : {
13393 : 144 : gfc_formal_arglist *dummy_args;
13394 : 144 : gfc_symbol *rsym;
13395 : : /* Check that there is a typebound defined assignment. If not,
13396 : : then this must be a module defined assignment. We cannot
13397 : : use the defined_assign_comp attribute here because it must
13398 : : be this derived type that has the defined assignment and not
13399 : : a parent type. */
13400 : 144 : if (!(comp1->ts.u.derived->f2k_derived
13401 : : && comp1->ts.u.derived->f2k_derived
13402 : 144 : ->tb_op[INTRINSIC_ASSIGN]))
13403 : : {
13404 : 1 : gfc_free_statements (this_code);
13405 : 1 : this_code = NULL;
13406 : 1 : continue;
13407 : : }
13408 : :
13409 : : /* If the first argument of the subroutine has intent INOUT
13410 : : a temporary must be generated and used instead. */
13411 : 143 : rsym = this_code->resolved_sym;
13412 : 143 : dummy_args = gfc_sym_get_dummy_args (rsym);
13413 : 268 : finalizable_out = gfc_may_be_finalized (comp1->ts)
13414 : 18 : && dummy_args
13415 : 161 : && dummy_args->sym->attr.intent == INTENT_OUT;
13416 : 286 : inout = dummy_args
13417 : 268 : && dummy_args->sym->attr.intent == INTENT_INOUT;
13418 : 72 : if ((inout || finalizable_out)
13419 : 89 : && !comp1->attr.allocatable)
13420 : : {
13421 : 89 : gfc_code *temp_code;
13422 : 89 : inout = true;
13423 : :
13424 : : /* Build the temporary required for the assignment and put
13425 : : it at the head of the generated code. */
13426 : 89 : if (!t1)
13427 : : {
13428 : 89 : gfc_namespace *tmp_ns = ns;
13429 : 89 : if (ns->parent && gfc_may_be_finalized (comp1->ts))
13430 : 18 : tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13431 : 89 : t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13432 : 89 : t1->symtree->n.sym->attr.artificial = 1;
13433 : 178 : temp_code = build_assignment (EXEC_ASSIGN,
13434 : : t1, (*code)->expr1,
13435 : 89 : NULL, NULL, (*code)->loc);
13436 : :
13437 : : /* For allocatable LHS, check whether it is allocated. Note
13438 : : that allocatable components with defined assignment are
13439 : : not yet support. See PR 57696. */
13440 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13441 : : {
13442 : 24 : gfc_code *block;
13443 : 24 : gfc_expr *e =
13444 : 24 : gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13445 : 24 : block = gfc_get_code (EXEC_IF);
13446 : 24 : block->block = gfc_get_code (EXEC_IF);
13447 : 24 : block->block->expr1
13448 : 48 : = gfc_build_intrinsic_call (ns,
13449 : : GFC_ISYM_ALLOCATED, "allocated",
13450 : 24 : (*code)->loc, 1, e);
13451 : 24 : block->block->next = temp_code;
13452 : 24 : temp_code = block;
13453 : : }
13454 : 89 : add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13455 : : }
13456 : :
13457 : : /* Replace the first actual arg with the component of the
13458 : : temporary. */
13459 : 89 : gfc_free_expr (this_code->ext.actual->expr);
13460 : 89 : this_code->ext.actual->expr = gfc_copy_expr (t1);
13461 : 89 : add_comp_ref (this_code->ext.actual->expr, comp1);
13462 : :
13463 : : /* If the LHS variable is allocatable and wasn't allocated and
13464 : : the temporary is allocatable, pointer assign the address of
13465 : : the freshly allocated LHS to the temporary. */
13466 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable
13467 : 89 : && gfc_expr_attr ((*code)->expr1).allocatable)
13468 : : {
13469 : 18 : gfc_code *block;
13470 : 18 : gfc_expr *cond;
13471 : :
13472 : 18 : cond = gfc_get_expr ();
13473 : 18 : cond->ts.type = BT_LOGICAL;
13474 : 18 : cond->ts.kind = gfc_default_logical_kind;
13475 : 18 : cond->expr_type = EXPR_OP;
13476 : 18 : cond->where = (*code)->loc;
13477 : 18 : cond->value.op.op = INTRINSIC_NOT;
13478 : 18 : cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13479 : : GFC_ISYM_ALLOCATED, "allocated",
13480 : 18 : (*code)->loc, 1, gfc_copy_expr (t1));
13481 : 18 : block = gfc_get_code (EXEC_IF);
13482 : 18 : block->block = gfc_get_code (EXEC_IF);
13483 : 18 : block->block->expr1 = cond;
13484 : 36 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13485 : : t1, (*code)->expr1,
13486 : 18 : NULL, NULL, (*code)->loc);
13487 : 18 : add_code_to_chain (&block, &head, &tail);
13488 : : }
13489 : : }
13490 : : }
13491 : 55 : else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13492 : : {
13493 : : /* Don't add intrinsic assignments since they are already
13494 : : effected by the intrinsic assignment of the structure, unless
13495 : : finalization is required. */
13496 : 7 : if (finalizable_comp)
13497 : 0 : this_code->expr1->must_finalize = 1;
13498 : : else
13499 : : {
13500 : 7 : gfc_free_statements (this_code);
13501 : 7 : this_code = NULL;
13502 : 7 : continue;
13503 : : }
13504 : : }
13505 : : else
13506 : : {
13507 : : /* Resolution has expanded an assignment of a derived type with
13508 : : defined assigned components. Remove the redundant, leading
13509 : : assignment. */
13510 : 48 : gcc_assert (this_code->op == EXEC_ASSIGN);
13511 : 48 : gfc_code *tmp = this_code;
13512 : 48 : this_code = this_code->next;
13513 : 48 : tmp->next = NULL;
13514 : 48 : gfc_free_statements (tmp);
13515 : : }
13516 : :
13517 : 191 : add_code_to_chain (&this_code, &head, &tail);
13518 : :
13519 : 191 : if (t1 && (inout || finalizable_out))
13520 : : {
13521 : : /* Transfer the value to the final result. */
13522 : 178 : this_code = build_assignment (EXEC_ASSIGN,
13523 : : (*code)->expr1, t1,
13524 : 89 : comp1, comp2, (*code)->loc);
13525 : 89 : this_code->expr1->must_finalize = 0;
13526 : 89 : add_code_to_chain (&this_code, &head, &tail);
13527 : : }
13528 : : }
13529 : :
13530 : : /* Put the temporary assignments at the top of the generated code. */
13531 : 182 : if (tmp_head && component_assignment_level == 1)
13532 : : {
13533 : 126 : gfc_append_code (tmp_head, head);
13534 : 126 : head = tmp_head;
13535 : 126 : tmp_head = tmp_tail = NULL;
13536 : : }
13537 : :
13538 : : /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13539 : : not accidentally deallocated. Hence, nullify t1. */
13540 : 89 : if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13541 : 271 : && gfc_expr_attr ((*code)->expr1).allocatable)
13542 : : {
13543 : 18 : gfc_code *block;
13544 : 18 : gfc_expr *cond;
13545 : 18 : gfc_expr *e;
13546 : :
13547 : 18 : e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13548 : 18 : cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
13549 : 18 : (*code)->loc, 2, gfc_copy_expr (t1), e);
13550 : 18 : block = gfc_get_code (EXEC_IF);
13551 : 18 : block->block = gfc_get_code (EXEC_IF);
13552 : 18 : block->block->expr1 = cond;
13553 : 18 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13554 : : t1, gfc_get_null_expr (&(*code)->loc),
13555 : 18 : NULL, NULL, (*code)->loc);
13556 : 18 : gfc_append_code (tail, block);
13557 : 18 : tail = block;
13558 : : }
13559 : :
13560 : 182 : component_assignment_level--;
13561 : :
13562 : : /* Make an explicit final call for the function result. */
13563 : 182 : if (tmp_expr)
13564 : 81 : generate_final_call (tmp_expr, &head, &tail);
13565 : :
13566 : 182 : if (tmp_code)
13567 : : {
13568 : 134 : ns->code = head;
13569 : 134 : return;
13570 : : }
13571 : :
13572 : : /* Now attach the remaining code chain to the input code. Step on
13573 : : to the end of the new code since resolution is complete. */
13574 : 48 : gcc_assert ((*code)->op == EXEC_ASSIGN);
13575 : 48 : tail->next = (*code)->next;
13576 : : /* Overwrite 'code' because this would place the intrinsic assignment
13577 : : before the temporary for the lhs is created. */
13578 : 48 : gfc_free_expr ((*code)->expr1);
13579 : 48 : gfc_free_expr ((*code)->expr2);
13580 : 48 : **code = *head;
13581 : 48 : if (head != tail)
13582 : 48 : free (head);
13583 : 48 : *code = tail;
13584 : : }
13585 : :
13586 : :
13587 : : /* F2008: Pointer function assignments are of the form:
13588 : : ptr_fcn (args) = expr
13589 : : This function breaks these assignments into two statements:
13590 : : temporary_pointer => ptr_fcn(args)
13591 : : temporary_pointer = expr */
13592 : :
13593 : : static bool
13594 : 280484 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
13595 : : {
13596 : 280484 : gfc_expr *tmp_ptr_expr;
13597 : 280484 : gfc_code *this_code;
13598 : 280484 : gfc_component *comp;
13599 : 280484 : gfc_symbol *s;
13600 : :
13601 : 280484 : if ((*code)->expr1->expr_type != EXPR_FUNCTION)
13602 : : return false;
13603 : :
13604 : : /* Even if standard does not support this feature, continue to build
13605 : : the two statements to avoid upsetting frontend_passes.c. */
13606 : 205 : gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
13607 : : "%L", &(*code)->loc);
13608 : :
13609 : 205 : comp = gfc_get_proc_ptr_comp ((*code)->expr1);
13610 : :
13611 : 205 : if (comp)
13612 : 6 : s = comp->ts.interface;
13613 : : else
13614 : 199 : s = (*code)->expr1->symtree->n.sym;
13615 : :
13616 : 205 : if (s == NULL || !s->result->attr.pointer)
13617 : : {
13618 : 5 : gfc_error ("The function result on the lhs of the assignment at "
13619 : : "%L must have the pointer attribute.",
13620 : 5 : &(*code)->expr1->where);
13621 : 5 : (*code)->op = EXEC_NOP;
13622 : 5 : return false;
13623 : : }
13624 : :
13625 : 200 : tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
13626 : :
13627 : : /* get_temp_from_expression is set up for ordinary assignments. To that
13628 : : end, where array bounds are not known, arrays are made allocatable.
13629 : : Change the temporary to a pointer here. */
13630 : 200 : tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
13631 : 200 : tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
13632 : 200 : tmp_ptr_expr->where = (*code)->loc;
13633 : :
13634 : : /* A new charlen is required to ensure that the variable string length
13635 : : is different to that of the original lhs for deferred results. */
13636 : 200 : if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
13637 : : {
13638 : 60 : tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
13639 : 60 : tmp_ptr_expr->ts.deferred = 1;
13640 : 60 : tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
13641 : 60 : gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
13642 : 60 : tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
13643 : : }
13644 : :
13645 : 400 : this_code = build_assignment (EXEC_ASSIGN,
13646 : : tmp_ptr_expr, (*code)->expr2,
13647 : 200 : NULL, NULL, (*code)->loc);
13648 : 200 : this_code->next = (*code)->next;
13649 : 200 : (*code)->next = this_code;
13650 : 200 : (*code)->op = EXEC_POINTER_ASSIGN;
13651 : 200 : (*code)->expr2 = (*code)->expr1;
13652 : 200 : (*code)->expr1 = tmp_ptr_expr;
13653 : :
13654 : 200 : return true;
13655 : : }
13656 : :
13657 : :
13658 : : /* Deferred character length assignments from an operator expression
13659 : : require a temporary because the character length of the lhs can
13660 : : change in the course of the assignment. */
13661 : :
13662 : : static bool
13663 : 279465 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
13664 : : {
13665 : 279465 : gfc_expr *tmp_expr;
13666 : 279465 : gfc_code *this_code;
13667 : :
13668 : 279465 : if (!((*code)->expr1->ts.type == BT_CHARACTER
13669 : 25751 : && (*code)->expr1->ts.deferred && (*code)->expr1->rank
13670 : 752 : && (*code)->expr2->ts.type == BT_CHARACTER
13671 : 751 : && (*code)->expr2->expr_type == EXPR_OP))
13672 : : return false;
13673 : :
13674 : 34 : if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
13675 : : return false;
13676 : :
13677 : 28 : if (gfc_expr_attr ((*code)->expr1).pointer)
13678 : : return false;
13679 : :
13680 : 22 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13681 : 22 : tmp_expr->where = (*code)->loc;
13682 : :
13683 : : /* A new charlen is required to ensure that the variable string
13684 : : length is different to that of the original lhs. */
13685 : 22 : tmp_expr->ts.u.cl = gfc_get_charlen();
13686 : 22 : tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
13687 : 22 : tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
13688 : 22 : (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
13689 : :
13690 : 22 : tmp_expr->symtree->n.sym->ts.deferred = 1;
13691 : :
13692 : 22 : this_code = build_assignment (EXEC_ASSIGN,
13693 : 22 : (*code)->expr1,
13694 : : gfc_copy_expr (tmp_expr),
13695 : : NULL, NULL, (*code)->loc);
13696 : :
13697 : 22 : (*code)->expr1 = tmp_expr;
13698 : :
13699 : 22 : this_code->next = (*code)->next;
13700 : 22 : (*code)->next = this_code;
13701 : :
13702 : 22 : return true;
13703 : : }
13704 : :
13705 : :
13706 : : /* Given a block of code, recursively resolve everything pointed to by this
13707 : : code block. */
13708 : :
13709 : : void
13710 : 662611 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
13711 : : {
13712 : 662611 : int omp_workshare_save;
13713 : 662611 : int forall_save, do_concurrent_save;
13714 : 662611 : code_stack frame;
13715 : 662611 : bool t;
13716 : :
13717 : 662611 : frame.prev = cs_base;
13718 : 662611 : frame.head = code;
13719 : 662611 : cs_base = &frame;
13720 : :
13721 : 662611 : find_reachable_labels (code);
13722 : :
13723 : 1772387 : for (; code; code = code->next)
13724 : : {
13725 : 1109777 : frame.current = code;
13726 : 1109777 : forall_save = forall_flag;
13727 : 1109777 : do_concurrent_save = gfc_do_concurrent_flag;
13728 : :
13729 : 1109777 : if (code->op == EXEC_FORALL)
13730 : : {
13731 : 1990 : forall_flag = 1;
13732 : 1990 : gfc_resolve_forall (code, ns, forall_save);
13733 : 1990 : forall_flag = 2;
13734 : : }
13735 : 1107787 : else if (code->op == EXEC_OMP_METADIRECTIVE)
13736 : 109 : for (gfc_omp_variant *variant
13737 : : = code->ext.omp_variants;
13738 : 359 : variant; variant = variant->next)
13739 : 250 : gfc_resolve_code (variant->code, ns);
13740 : 1107678 : else if (code->block)
13741 : : {
13742 : 322819 : omp_workshare_save = -1;
13743 : 322819 : switch (code->op)
13744 : : {
13745 : 10115 : case EXEC_OACC_PARALLEL_LOOP:
13746 : 10115 : case EXEC_OACC_PARALLEL:
13747 : 10115 : case EXEC_OACC_KERNELS_LOOP:
13748 : 10115 : case EXEC_OACC_KERNELS:
13749 : 10115 : case EXEC_OACC_SERIAL_LOOP:
13750 : 10115 : case EXEC_OACC_SERIAL:
13751 : 10115 : case EXEC_OACC_DATA:
13752 : 10115 : case EXEC_OACC_HOST_DATA:
13753 : 10115 : case EXEC_OACC_LOOP:
13754 : 10115 : gfc_resolve_oacc_blocks (code, ns);
13755 : 10115 : break;
13756 : 54 : case EXEC_OMP_PARALLEL_WORKSHARE:
13757 : 54 : omp_workshare_save = omp_workshare_flag;
13758 : 54 : omp_workshare_flag = 1;
13759 : 54 : gfc_resolve_omp_parallel_blocks (code, ns);
13760 : 54 : break;
13761 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13762 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13763 : 5905 : case EXEC_OMP_MASKED_TASKLOOP:
13764 : 5905 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13765 : 5905 : case EXEC_OMP_MASTER_TASKLOOP:
13766 : 5905 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13767 : 5905 : case EXEC_OMP_PARALLEL:
13768 : 5905 : case EXEC_OMP_PARALLEL_DO:
13769 : 5905 : case EXEC_OMP_PARALLEL_DO_SIMD:
13770 : 5905 : case EXEC_OMP_PARALLEL_LOOP:
13771 : 5905 : case EXEC_OMP_PARALLEL_MASKED:
13772 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13773 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13774 : 5905 : case EXEC_OMP_PARALLEL_MASTER:
13775 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13776 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13777 : 5905 : case EXEC_OMP_PARALLEL_SECTIONS:
13778 : 5905 : case EXEC_OMP_TARGET_PARALLEL:
13779 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO:
13780 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13781 : 5905 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
13782 : 5905 : case EXEC_OMP_TARGET_TEAMS:
13783 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13784 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13785 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13786 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13787 : 5905 : case EXEC_OMP_TARGET_TEAMS_LOOP:
13788 : 5905 : case EXEC_OMP_TASK:
13789 : 5905 : case EXEC_OMP_TASKLOOP:
13790 : 5905 : case EXEC_OMP_TASKLOOP_SIMD:
13791 : 5905 : case EXEC_OMP_TEAMS:
13792 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE:
13793 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13794 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13795 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13796 : 5905 : case EXEC_OMP_TEAMS_LOOP:
13797 : 5905 : omp_workshare_save = omp_workshare_flag;
13798 : 5905 : omp_workshare_flag = 0;
13799 : 5905 : gfc_resolve_omp_parallel_blocks (code, ns);
13800 : 5905 : break;
13801 : 3039 : case EXEC_OMP_DISTRIBUTE:
13802 : 3039 : case EXEC_OMP_DISTRIBUTE_SIMD:
13803 : 3039 : case EXEC_OMP_DO:
13804 : 3039 : case EXEC_OMP_DO_SIMD:
13805 : 3039 : case EXEC_OMP_LOOP:
13806 : 3039 : case EXEC_OMP_SIMD:
13807 : 3039 : case EXEC_OMP_TARGET_SIMD:
13808 : 3039 : case EXEC_OMP_TILE:
13809 : 3039 : case EXEC_OMP_UNROLL:
13810 : 3039 : gfc_resolve_omp_do_blocks (code, ns);
13811 : 3039 : break;
13812 : : case EXEC_SELECT_TYPE:
13813 : : case EXEC_SELECT_RANK:
13814 : : /* Blocks are handled in resolve_select_type/rank because we
13815 : : have to transform the SELECT TYPE into ASSOCIATE first. */
13816 : : break;
13817 : 174 : case EXEC_DO_CONCURRENT:
13818 : 174 : gfc_do_concurrent_flag = 1;
13819 : 174 : gfc_resolve_blocks (code->block, ns);
13820 : 174 : gfc_do_concurrent_flag = 2;
13821 : 174 : break;
13822 : 39 : case EXEC_OMP_WORKSHARE:
13823 : 39 : omp_workshare_save = omp_workshare_flag;
13824 : 39 : omp_workshare_flag = 1;
13825 : : /* FALL THROUGH */
13826 : 299615 : default:
13827 : 299615 : gfc_resolve_blocks (code->block, ns);
13828 : 299615 : break;
13829 : : }
13830 : :
13831 : 318902 : if (omp_workshare_save != -1)
13832 : 5998 : omp_workshare_flag = omp_workshare_save;
13833 : : }
13834 : 784859 : start:
13835 : 1109982 : t = true;
13836 : 1109982 : if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
13837 : 1108599 : t = gfc_resolve_expr (code->expr1);
13838 : :
13839 : 1109982 : forall_flag = forall_save;
13840 : 1109982 : gfc_do_concurrent_flag = do_concurrent_save;
13841 : :
13842 : 1109982 : if (!gfc_resolve_expr (code->expr2))
13843 : 609 : t = false;
13844 : :
13845 : 1109982 : if (code->op == EXEC_ALLOCATE
13846 : 1109982 : && !gfc_resolve_expr (code->expr3))
13847 : : t = false;
13848 : :
13849 : 1109982 : switch (code->op)
13850 : : {
13851 : : case EXEC_NOP:
13852 : : case EXEC_END_BLOCK:
13853 : : case EXEC_END_NESTED_BLOCK:
13854 : : case EXEC_CYCLE:
13855 : : case EXEC_PAUSE:
13856 : : break;
13857 : :
13858 : 212944 : case EXEC_STOP:
13859 : 212944 : case EXEC_ERROR_STOP:
13860 : 212944 : if (code->expr2 != NULL
13861 : 37 : && (code->expr2->ts.type != BT_LOGICAL
13862 : 37 : || code->expr2->rank != 0))
13863 : 0 : gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
13864 : : &code->expr2->where);
13865 : : break;
13866 : :
13867 : : case EXEC_EXIT:
13868 : : case EXEC_CONTINUE:
13869 : : case EXEC_DT_END:
13870 : : case EXEC_ASSIGN_CALL:
13871 : : break;
13872 : :
13873 : 50 : case EXEC_CRITICAL:
13874 : 50 : resolve_critical (code);
13875 : 50 : break;
13876 : :
13877 : 747 : case EXEC_SYNC_ALL:
13878 : 747 : case EXEC_SYNC_IMAGES:
13879 : 747 : case EXEC_SYNC_MEMORY:
13880 : 747 : resolve_sync (code);
13881 : 747 : break;
13882 : :
13883 : 136 : case EXEC_LOCK:
13884 : 136 : case EXEC_UNLOCK:
13885 : 136 : case EXEC_EVENT_POST:
13886 : 136 : case EXEC_EVENT_WAIT:
13887 : 136 : resolve_lock_unlock_event (code);
13888 : 136 : break;
13889 : :
13890 : : case EXEC_FAIL_IMAGE:
13891 : : break;
13892 : :
13893 : 107 : case EXEC_FORM_TEAM:
13894 : 107 : resolve_form_team (code);
13895 : 107 : break;
13896 : :
13897 : 66 : case EXEC_CHANGE_TEAM:
13898 : 66 : resolve_change_team (code);
13899 : 66 : break;
13900 : :
13901 : 64 : case EXEC_END_TEAM:
13902 : 64 : resolve_end_team (code);
13903 : 64 : break;
13904 : :
13905 : 22 : case EXEC_SYNC_TEAM:
13906 : 22 : resolve_sync_team (code);
13907 : 22 : break;
13908 : :
13909 : 1420 : case EXEC_ENTRY:
13910 : : /* Keep track of which entry we are up to. */
13911 : 1420 : current_entry_id = code->ext.entry->id;
13912 : 1420 : break;
13913 : :
13914 : 450 : case EXEC_WHERE:
13915 : 450 : resolve_where (code, NULL);
13916 : 450 : break;
13917 : :
13918 : 1249 : case EXEC_GOTO:
13919 : 1249 : if (code->expr1 != NULL)
13920 : : {
13921 : 78 : if (code->expr1->expr_type != EXPR_VARIABLE
13922 : 76 : || code->expr1->ts.type != BT_INTEGER
13923 : 76 : || (code->expr1->ref
13924 : 1 : && code->expr1->ref->type == REF_ARRAY)
13925 : 75 : || code->expr1->symtree == NULL
13926 : 75 : || (code->expr1->symtree->n.sym
13927 : 75 : && (code->expr1->symtree->n.sym->attr.flavor
13928 : 75 : == FL_PARAMETER)))
13929 : 4 : gfc_error ("ASSIGNED GOTO statement at %L requires a "
13930 : : "scalar INTEGER variable", &code->expr1->where);
13931 : 74 : else if (code->expr1->symtree->n.sym
13932 : 74 : && code->expr1->symtree->n.sym->attr.assign != 1)
13933 : 1 : gfc_error ("Variable %qs has not been assigned a target "
13934 : : "label at %L", code->expr1->symtree->n.sym->name,
13935 : : &code->expr1->where);
13936 : : }
13937 : : else
13938 : 1171 : resolve_branch (code->label1, code);
13939 : : break;
13940 : :
13941 : 3123 : case EXEC_RETURN:
13942 : 3123 : if (code->expr1 != NULL
13943 : 53 : && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13944 : 1 : gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
13945 : : "INTEGER return specifier", &code->expr1->where);
13946 : : break;
13947 : :
13948 : : case EXEC_INIT_ASSIGN:
13949 : : case EXEC_END_PROCEDURE:
13950 : : break;
13951 : :
13952 : 281521 : case EXEC_ASSIGN:
13953 : 281521 : if (!t)
13954 : : break;
13955 : :
13956 : 280874 : if (flag_coarray == GFC_FCOARRAY_LIB
13957 : 280874 : && gfc_is_coindexed (code->expr1))
13958 : : {
13959 : : /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
13960 : : coindexed variable. */
13961 : 390 : code->op = EXEC_CALL;
13962 : 390 : gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
13963 : : true);
13964 : 390 : code->resolved_sym = code->symtree->n.sym;
13965 : 390 : code->resolved_sym->attr.flavor = FL_PROCEDURE;
13966 : 390 : code->resolved_sym->attr.intrinsic = 1;
13967 : 390 : code->resolved_sym->attr.subroutine = 1;
13968 : 390 : code->resolved_isym
13969 : 390 : = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
13970 : 390 : gfc_commit_symbol (code->resolved_sym);
13971 : 390 : code->ext.actual = gfc_get_actual_arglist ();
13972 : 390 : code->ext.actual->expr = code->expr1;
13973 : 390 : code->ext.actual->next = gfc_get_actual_arglist ();
13974 : 390 : code->ext.actual->next->expr = code->expr2;
13975 : :
13976 : 390 : code->expr1 = NULL;
13977 : 390 : code->expr2 = NULL;
13978 : 390 : break;
13979 : : }
13980 : :
13981 : 280484 : if (code->expr1->ts.type == BT_CLASS)
13982 : 1015 : gfc_find_vtab (&code->expr2->ts);
13983 : :
13984 : : /* If this is a pointer function in an lvalue variable context,
13985 : : the new code will have to be resolved afresh. This is also the
13986 : : case with an error, where the code is transformed into NOP to
13987 : : prevent ICEs downstream. */
13988 : 280484 : if (resolve_ptr_fcn_assign (&code, ns)
13989 : 280484 : || code->op == EXEC_NOP)
13990 : 205 : goto start;
13991 : :
13992 : 280279 : if (!gfc_check_vardef_context (code->expr1, false, false, false,
13993 : 280279 : _("assignment")))
13994 : : break;
13995 : :
13996 : 280241 : if (resolve_ordinary_assign (code, ns))
13997 : : {
13998 : 776 : if (omp_workshare_flag)
13999 : : {
14000 : 1 : gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
14001 : 1 : "at %L", &code->loc);
14002 : 1 : break;
14003 : : }
14004 : 775 : if (code->op == EXEC_COMPCALL)
14005 : 416 : goto compcall;
14006 : : else
14007 : 359 : goto call;
14008 : : }
14009 : :
14010 : : /* Check for dependencies in deferred character length array
14011 : : assignments and generate a temporary, if necessary. */
14012 : 279465 : if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
14013 : : break;
14014 : :
14015 : : /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
14016 : 279443 : if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
14017 : 6731 : && code->expr1->ts.u.derived
14018 : 6731 : && code->expr1->ts.u.derived->attr.defined_assign_comp)
14019 : 188 : generate_component_assignments (&code, ns);
14020 : 279255 : else if (code->op == EXEC_ASSIGN)
14021 : : {
14022 : 279255 : if (gfc_may_be_finalized (code->expr1->ts))
14023 : 1129 : code->expr1->must_finalize = 1;
14024 : 279255 : if (code->expr2->expr_type == EXPR_ARRAY
14025 : 279255 : && gfc_may_be_finalized (code->expr2->ts))
14026 : 43 : code->expr2->must_finalize = 1;
14027 : : }
14028 : :
14029 : : break;
14030 : :
14031 : 126 : case EXEC_LABEL_ASSIGN:
14032 : 126 : if (code->label1->defined == ST_LABEL_UNKNOWN)
14033 : 0 : gfc_error ("Label %d referenced at %L is never defined",
14034 : : code->label1->value, &code->label1->where);
14035 : 126 : if (t
14036 : 126 : && (code->expr1->expr_type != EXPR_VARIABLE
14037 : 126 : || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
14038 : 126 : || code->expr1->symtree->n.sym->ts.kind
14039 : 126 : != gfc_default_integer_kind
14040 : 126 : || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
14041 : 125 : || code->expr1->symtree->n.sym->as != NULL))
14042 : 2 : gfc_error ("ASSIGN statement at %L requires a scalar "
14043 : : "default INTEGER variable", &code->expr1->where);
14044 : : break;
14045 : :
14046 : 10262 : case EXEC_POINTER_ASSIGN:
14047 : 10262 : {
14048 : 10262 : gfc_expr* e;
14049 : :
14050 : 10262 : if (!t)
14051 : : break;
14052 : :
14053 : : /* This is both a variable definition and pointer assignment
14054 : : context, so check both of them. For rank remapping, a final
14055 : : array ref may be present on the LHS and fool gfc_expr_attr
14056 : : used in gfc_check_vardef_context. Remove it. */
14057 : 10257 : e = remove_last_array_ref (code->expr1);
14058 : 20514 : t = gfc_check_vardef_context (e, true, false, false,
14059 : 10257 : _("pointer assignment"));
14060 : 10257 : if (t)
14061 : 10236 : t = gfc_check_vardef_context (e, false, false, false,
14062 : 10236 : _("pointer assignment"));
14063 : 10257 : gfc_free_expr (e);
14064 : :
14065 : 1119899 : t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
14066 : :
14067 : 10123 : if (!t)
14068 : : break;
14069 : :
14070 : : /* Assigning a class object always is a regular assign. */
14071 : 10123 : if (code->expr2->ts.type == BT_CLASS
14072 : 565 : && code->expr1->ts.type == BT_CLASS
14073 : 474 : && CLASS_DATA (code->expr2)
14074 : 473 : && !CLASS_DATA (code->expr2)->attr.dimension
14075 : 10742 : && !(gfc_expr_attr (code->expr1).proc_pointer
14076 : 54 : && code->expr2->expr_type == EXPR_VARIABLE
14077 : 42 : && code->expr2->symtree->n.sym->attr.flavor
14078 : 42 : == FL_PROCEDURE))
14079 : 324 : code->op = EXEC_ASSIGN;
14080 : : break;
14081 : : }
14082 : :
14083 : 72 : case EXEC_ARITHMETIC_IF:
14084 : 72 : {
14085 : 72 : gfc_expr *e = code->expr1;
14086 : :
14087 : 72 : gfc_resolve_expr (e);
14088 : 72 : if (e->expr_type == EXPR_NULL)
14089 : 1 : gfc_error ("Invalid NULL at %L", &e->where);
14090 : :
14091 : 72 : if (t && (e->rank > 0
14092 : 68 : || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
14093 : 5 : gfc_error ("Arithmetic IF statement at %L requires a scalar "
14094 : : "REAL or INTEGER expression", &e->where);
14095 : :
14096 : 72 : resolve_branch (code->label1, code);
14097 : 72 : resolve_branch (code->label2, code);
14098 : 72 : resolve_branch (code->label3, code);
14099 : : }
14100 : 72 : break;
14101 : :
14102 : 225856 : case EXEC_IF:
14103 : 225856 : if (t && code->expr1 != NULL
14104 : 0 : && (code->expr1->ts.type != BT_LOGICAL
14105 : 0 : || code->expr1->rank != 0))
14106 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
14107 : : &code->expr1->where);
14108 : : break;
14109 : :
14110 : 77713 : case EXEC_CALL:
14111 : 77713 : call:
14112 : 77713 : resolve_call (code);
14113 : 77713 : break;
14114 : :
14115 : 1676 : case EXEC_COMPCALL:
14116 : 1676 : compcall:
14117 : 1676 : resolve_typebound_subroutine (code);
14118 : 1676 : break;
14119 : :
14120 : 123 : case EXEC_CALL_PPC:
14121 : 123 : resolve_ppc_call (code);
14122 : 123 : break;
14123 : :
14124 : 683 : case EXEC_SELECT:
14125 : : /* Select is complicated. Also, a SELECT construct could be
14126 : : a transformed computed GOTO. */
14127 : 683 : resolve_select (code, false);
14128 : 683 : break;
14129 : :
14130 : 2924 : case EXEC_SELECT_TYPE:
14131 : 2924 : resolve_select_type (code, ns);
14132 : 2924 : break;
14133 : :
14134 : 1018 : case EXEC_SELECT_RANK:
14135 : 1018 : resolve_select_rank (code, ns);
14136 : 1018 : break;
14137 : :
14138 : 7606 : case EXEC_BLOCK:
14139 : 7606 : resolve_block_construct (code);
14140 : 7606 : break;
14141 : :
14142 : 32343 : case EXEC_DO:
14143 : 32343 : if (code->ext.iterator != NULL)
14144 : : {
14145 : 32343 : gfc_iterator *iter = code->ext.iterator;
14146 : 32343 : if (gfc_resolve_iterator (iter, true, false))
14147 : 32329 : gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
14148 : : true);
14149 : : }
14150 : : break;
14151 : :
14152 : 527 : case EXEC_DO_WHILE:
14153 : 527 : if (code->expr1 == NULL)
14154 : 0 : gfc_internal_error ("gfc_resolve_code(): No expression on "
14155 : : "DO WHILE");
14156 : 527 : if (t
14157 : 527 : && (code->expr1->rank != 0
14158 : 527 : || code->expr1->ts.type != BT_LOGICAL))
14159 : 0 : gfc_error ("Exit condition of DO WHILE loop at %L must be "
14160 : : "a scalar LOGICAL expression", &code->expr1->where);
14161 : : break;
14162 : :
14163 : 13784 : case EXEC_ALLOCATE:
14164 : 13784 : if (t)
14165 : 13782 : resolve_allocate_deallocate (code, "ALLOCATE");
14166 : :
14167 : : break;
14168 : :
14169 : 5801 : case EXEC_DEALLOCATE:
14170 : 5801 : if (t)
14171 : 5801 : resolve_allocate_deallocate (code, "DEALLOCATE");
14172 : :
14173 : : break;
14174 : :
14175 : 3879 : case EXEC_OPEN:
14176 : 3879 : if (!gfc_resolve_open (code->ext.open, &code->loc))
14177 : : break;
14178 : :
14179 : 3652 : resolve_branch (code->ext.open->err, code);
14180 : 3652 : break;
14181 : :
14182 : 3067 : case EXEC_CLOSE:
14183 : 3067 : if (!gfc_resolve_close (code->ext.close, &code->loc))
14184 : : break;
14185 : :
14186 : 3033 : resolve_branch (code->ext.close->err, code);
14187 : 3033 : break;
14188 : :
14189 : 2779 : case EXEC_BACKSPACE:
14190 : 2779 : case EXEC_ENDFILE:
14191 : 2779 : case EXEC_REWIND:
14192 : 2779 : case EXEC_FLUSH:
14193 : 2779 : if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
14194 : : break;
14195 : :
14196 : 2713 : resolve_branch (code->ext.filepos->err, code);
14197 : 2713 : break;
14198 : :
14199 : 817 : case EXEC_INQUIRE:
14200 : 817 : if (!gfc_resolve_inquire (code->ext.inquire))
14201 : : break;
14202 : :
14203 : 769 : resolve_branch (code->ext.inquire->err, code);
14204 : 769 : break;
14205 : :
14206 : 92 : case EXEC_IOLENGTH:
14207 : 92 : gcc_assert (code->ext.inquire != NULL);
14208 : 92 : if (!gfc_resolve_inquire (code->ext.inquire))
14209 : : break;
14210 : :
14211 : 90 : resolve_branch (code->ext.inquire->err, code);
14212 : 90 : break;
14213 : :
14214 : 89 : case EXEC_WAIT:
14215 : 89 : if (!gfc_resolve_wait (code->ext.wait))
14216 : : break;
14217 : :
14218 : 74 : resolve_branch (code->ext.wait->err, code);
14219 : 74 : resolve_branch (code->ext.wait->end, code);
14220 : 74 : resolve_branch (code->ext.wait->eor, code);
14221 : 74 : break;
14222 : :
14223 : 31991 : case EXEC_READ:
14224 : 31991 : case EXEC_WRITE:
14225 : 31991 : if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
14226 : : break;
14227 : :
14228 : 31683 : resolve_branch (code->ext.dt->err, code);
14229 : 31683 : resolve_branch (code->ext.dt->end, code);
14230 : 31683 : resolve_branch (code->ext.dt->eor, code);
14231 : 31683 : break;
14232 : :
14233 : 45768 : case EXEC_TRANSFER:
14234 : 45768 : resolve_transfer (code);
14235 : 45768 : break;
14236 : :
14237 : 2164 : case EXEC_DO_CONCURRENT:
14238 : 2164 : case EXEC_FORALL:
14239 : 2164 : resolve_forall_iterators (code->ext.concur.forall_iterator);
14240 : :
14241 : 2164 : if (code->expr1 != NULL
14242 : 730 : && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
14243 : 2 : gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
14244 : : "expression", &code->expr1->where);
14245 : :
14246 : 2164 : if (code->op == EXEC_DO_CONCURRENT)
14247 : 174 : resolve_locality_spec (code, ns);
14248 : : break;
14249 : :
14250 : 13160 : case EXEC_OACC_PARALLEL_LOOP:
14251 : 13160 : case EXEC_OACC_PARALLEL:
14252 : 13160 : case EXEC_OACC_KERNELS_LOOP:
14253 : 13160 : case EXEC_OACC_KERNELS:
14254 : 13160 : case EXEC_OACC_SERIAL_LOOP:
14255 : 13160 : case EXEC_OACC_SERIAL:
14256 : 13160 : case EXEC_OACC_DATA:
14257 : 13160 : case EXEC_OACC_HOST_DATA:
14258 : 13160 : case EXEC_OACC_LOOP:
14259 : 13160 : case EXEC_OACC_UPDATE:
14260 : 13160 : case EXEC_OACC_WAIT:
14261 : 13160 : case EXEC_OACC_CACHE:
14262 : 13160 : case EXEC_OACC_ENTER_DATA:
14263 : 13160 : case EXEC_OACC_EXIT_DATA:
14264 : 13160 : case EXEC_OACC_ATOMIC:
14265 : 13160 : case EXEC_OACC_DECLARE:
14266 : 13160 : gfc_resolve_oacc_directive (code, ns);
14267 : 13160 : break;
14268 : :
14269 : 16584 : case EXEC_OMP_ALLOCATE:
14270 : 16584 : case EXEC_OMP_ALLOCATORS:
14271 : 16584 : case EXEC_OMP_ASSUME:
14272 : 16584 : case EXEC_OMP_ATOMIC:
14273 : 16584 : case EXEC_OMP_BARRIER:
14274 : 16584 : case EXEC_OMP_CANCEL:
14275 : 16584 : case EXEC_OMP_CANCELLATION_POINT:
14276 : 16584 : case EXEC_OMP_CRITICAL:
14277 : 16584 : case EXEC_OMP_FLUSH:
14278 : 16584 : case EXEC_OMP_DEPOBJ:
14279 : 16584 : case EXEC_OMP_DISPATCH:
14280 : 16584 : case EXEC_OMP_DISTRIBUTE:
14281 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
14282 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
14283 : 16584 : case EXEC_OMP_DISTRIBUTE_SIMD:
14284 : 16584 : case EXEC_OMP_DO:
14285 : 16584 : case EXEC_OMP_DO_SIMD:
14286 : 16584 : case EXEC_OMP_ERROR:
14287 : 16584 : case EXEC_OMP_INTEROP:
14288 : 16584 : case EXEC_OMP_LOOP:
14289 : 16584 : case EXEC_OMP_MASTER:
14290 : 16584 : case EXEC_OMP_MASTER_TASKLOOP:
14291 : 16584 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
14292 : 16584 : case EXEC_OMP_MASKED:
14293 : 16584 : case EXEC_OMP_MASKED_TASKLOOP:
14294 : 16584 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
14295 : 16584 : case EXEC_OMP_METADIRECTIVE:
14296 : 16584 : case EXEC_OMP_ORDERED:
14297 : 16584 : case EXEC_OMP_SCAN:
14298 : 16584 : case EXEC_OMP_SCOPE:
14299 : 16584 : case EXEC_OMP_SECTIONS:
14300 : 16584 : case EXEC_OMP_SIMD:
14301 : 16584 : case EXEC_OMP_SINGLE:
14302 : 16584 : case EXEC_OMP_TARGET:
14303 : 16584 : case EXEC_OMP_TARGET_DATA:
14304 : 16584 : case EXEC_OMP_TARGET_ENTER_DATA:
14305 : 16584 : case EXEC_OMP_TARGET_EXIT_DATA:
14306 : 16584 : case EXEC_OMP_TARGET_PARALLEL:
14307 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO:
14308 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14309 : 16584 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14310 : 16584 : case EXEC_OMP_TARGET_SIMD:
14311 : 16584 : case EXEC_OMP_TARGET_TEAMS:
14312 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14313 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14314 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14315 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14316 : 16584 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14317 : 16584 : case EXEC_OMP_TARGET_UPDATE:
14318 : 16584 : case EXEC_OMP_TASK:
14319 : 16584 : case EXEC_OMP_TASKGROUP:
14320 : 16584 : case EXEC_OMP_TASKLOOP:
14321 : 16584 : case EXEC_OMP_TASKLOOP_SIMD:
14322 : 16584 : case EXEC_OMP_TASKWAIT:
14323 : 16584 : case EXEC_OMP_TASKYIELD:
14324 : 16584 : case EXEC_OMP_TEAMS:
14325 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14326 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14327 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14328 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14329 : 16584 : case EXEC_OMP_TEAMS_LOOP:
14330 : 16584 : case EXEC_OMP_TILE:
14331 : 16584 : case EXEC_OMP_UNROLL:
14332 : 16584 : case EXEC_OMP_WORKSHARE:
14333 : 16584 : gfc_resolve_omp_directive (code, ns);
14334 : 16584 : break;
14335 : :
14336 : 3833 : case EXEC_OMP_PARALLEL:
14337 : 3833 : case EXEC_OMP_PARALLEL_DO:
14338 : 3833 : case EXEC_OMP_PARALLEL_DO_SIMD:
14339 : 3833 : case EXEC_OMP_PARALLEL_LOOP:
14340 : 3833 : case EXEC_OMP_PARALLEL_MASKED:
14341 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14342 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14343 : 3833 : case EXEC_OMP_PARALLEL_MASTER:
14344 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14345 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14346 : 3833 : case EXEC_OMP_PARALLEL_SECTIONS:
14347 : 3833 : case EXEC_OMP_PARALLEL_WORKSHARE:
14348 : 3833 : omp_workshare_save = omp_workshare_flag;
14349 : 3833 : omp_workshare_flag = 0;
14350 : 3833 : gfc_resolve_omp_directive (code, ns);
14351 : 3833 : omp_workshare_flag = omp_workshare_save;
14352 : 3833 : break;
14353 : :
14354 : 0 : default:
14355 : 0 : gfc_internal_error ("gfc_resolve_code(): Bad statement code");
14356 : : }
14357 : : }
14358 : :
14359 : 662610 : cs_base = frame.prev;
14360 : 662610 : }
14361 : :
14362 : :
14363 : : /* Resolve initial values and make sure they are compatible with
14364 : : the variable. */
14365 : :
14366 : : static void
14367 : 1797230 : resolve_values (gfc_symbol *sym)
14368 : : {
14369 : 1797230 : bool t;
14370 : :
14371 : 1797230 : if (sym->value == NULL)
14372 : : return;
14373 : :
14374 : 403661 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
14375 : 4 : gfc_warning (OPT_Wdeprecated_declarations,
14376 : : "Using parameter %qs declared at %L is deprecated",
14377 : : sym->name, &sym->declared_at);
14378 : :
14379 : 403661 : if (sym->value->expr_type == EXPR_STRUCTURE)
14380 : 38245 : t= resolve_structure_cons (sym->value, 1);
14381 : : else
14382 : 365416 : t = gfc_resolve_expr (sym->value);
14383 : :
14384 : 403661 : if (!t)
14385 : : return;
14386 : :
14387 : 403659 : gfc_check_assign_symbol (sym, NULL, sym->value);
14388 : : }
14389 : :
14390 : :
14391 : : /* Verify any BIND(C) derived types in the namespace so we can report errors
14392 : : for them once, rather than for each variable declared of that type. */
14393 : :
14394 : : static void
14395 : 1769770 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14396 : : {
14397 : 1769770 : if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14398 : 80011 : && derived_sym->attr.is_bind_c == 1)
14399 : 26392 : verify_bind_c_derived_type (derived_sym);
14400 : :
14401 : 1769770 : return;
14402 : : }
14403 : :
14404 : :
14405 : : /* Check the interfaces of DTIO procedures associated with derived
14406 : : type 'sym'. These procedures can either have typebound bindings or
14407 : : can appear in DTIO generic interfaces. */
14408 : :
14409 : : static void
14410 : 1798198 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
14411 : : {
14412 : 1798198 : if (!sym || sym->attr.flavor != FL_DERIVED)
14413 : : return;
14414 : :
14415 : 88786 : gfc_check_dtio_interfaces (sym);
14416 : :
14417 : 88786 : return;
14418 : : }
14419 : :
14420 : : /* Verify that any binding labels used in a given namespace do not collide
14421 : : with the names or binding labels of any global symbols. Multiple INTERFACE
14422 : : for the same procedure are permitted. Abstract interfaces and dummy
14423 : : arguments are not checked. */
14424 : :
14425 : : static void
14426 : 1798198 : gfc_verify_binding_labels (gfc_symbol *sym)
14427 : : {
14428 : 1798198 : gfc_gsymbol *gsym;
14429 : 1798198 : const char *module;
14430 : :
14431 : 1798198 : if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
14432 : 60427 : || sym->attr.flavor == FL_DERIVED || !sym->binding_label
14433 : 33119 : || sym->attr.abstract || sym->attr.dummy)
14434 : : return;
14435 : :
14436 : 33019 : gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
14437 : :
14438 : 33019 : if (sym->module)
14439 : : module = sym->module;
14440 : 11948 : else if (sym->ns && sym->ns->proc_name
14441 : 11948 : && sym->ns->proc_name->attr.flavor == FL_MODULE)
14442 : 4502 : module = sym->ns->proc_name->name;
14443 : 7446 : else if (sym->ns && sym->ns->parent
14444 : 358 : && sym->ns && sym->ns->parent->proc_name
14445 : 358 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14446 : 272 : module = sym->ns->parent->proc_name->name;
14447 : : else
14448 : : module = NULL;
14449 : :
14450 : 33019 : if (!gsym
14451 : 11316 : || (!gsym->defined
14452 : 8486 : && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
14453 : : {
14454 : 21703 : if (!gsym)
14455 : 21703 : gsym = gfc_get_gsymbol (sym->binding_label, true);
14456 : 30189 : gsym->where = sym->declared_at;
14457 : 30189 : gsym->sym_name = sym->name;
14458 : 30189 : gsym->binding_label = sym->binding_label;
14459 : 30189 : gsym->ns = sym->ns;
14460 : 30189 : gsym->mod_name = module;
14461 : 30189 : if (sym->attr.function)
14462 : 19362 : gsym->type = GSYM_FUNCTION;
14463 : 10827 : else if (sym->attr.subroutine)
14464 : 10688 : gsym->type = GSYM_SUBROUTINE;
14465 : : /* Mark as variable/procedure as defined, unless its an INTERFACE. */
14466 : 30189 : gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
14467 : 30189 : return;
14468 : : }
14469 : :
14470 : 2830 : if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
14471 : : {
14472 : 1 : gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
14473 : : "identifier as entity at %L", sym->name,
14474 : : sym->binding_label, &sym->declared_at, &gsym->where);
14475 : : /* Clear the binding label to prevent checking multiple times. */
14476 : 1 : sym->binding_label = NULL;
14477 : 1 : return;
14478 : : }
14479 : :
14480 : 2829 : if (sym->attr.flavor == FL_VARIABLE && module
14481 : 37 : && (strcmp (module, gsym->mod_name) != 0
14482 : 35 : || strcmp (sym->name, gsym->sym_name) != 0))
14483 : : {
14484 : : /* This can only happen if the variable is defined in a module - if it
14485 : : isn't the same module, reject it. */
14486 : 3 : gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
14487 : : "uses the same global identifier as entity at %L from module %qs",
14488 : : sym->name, module, sym->binding_label,
14489 : : &sym->declared_at, &gsym->where, gsym->mod_name);
14490 : 3 : sym->binding_label = NULL;
14491 : 3 : return;
14492 : : }
14493 : :
14494 : 2826 : if ((sym->attr.function || sym->attr.subroutine)
14495 : 2790 : && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
14496 : 2788 : || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
14497 : 2475 : && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
14498 : 2087 : && (module != gsym->mod_name
14499 : 2083 : || strcmp (gsym->sym_name, sym->name) != 0
14500 : 2083 : || (module && strcmp (module, gsym->mod_name) != 0)))
14501 : : {
14502 : : /* Print an error if the procedure is defined multiple times; we have to
14503 : : exclude references to the same procedure via module association or
14504 : : multiple checks for the same procedure. */
14505 : 4 : gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
14506 : : "global identifier as entity at %L", sym->name,
14507 : : sym->binding_label, &sym->declared_at, &gsym->where);
14508 : 4 : sym->binding_label = NULL;
14509 : : }
14510 : : }
14511 : :
14512 : :
14513 : : /* Resolve an index expression. */
14514 : :
14515 : : static bool
14516 : 261384 : resolve_index_expr (gfc_expr *e)
14517 : : {
14518 : 261384 : if (!gfc_resolve_expr (e))
14519 : : return false;
14520 : :
14521 : 261374 : if (!gfc_simplify_expr (e, 0))
14522 : : return false;
14523 : :
14524 : 261372 : if (!gfc_specification_expr (e))
14525 : : return false;
14526 : :
14527 : : return true;
14528 : : }
14529 : :
14530 : :
14531 : : /* Resolve a charlen structure. */
14532 : :
14533 : : static bool
14534 : 100552 : resolve_charlen (gfc_charlen *cl)
14535 : : {
14536 : 100552 : int k;
14537 : 100552 : bool saved_specification_expr;
14538 : :
14539 : 100552 : if (cl->resolved)
14540 : : return true;
14541 : :
14542 : 92382 : cl->resolved = 1;
14543 : 92382 : saved_specification_expr = specification_expr;
14544 : 92382 : specification_expr = true;
14545 : :
14546 : 92382 : if (cl->length_from_typespec)
14547 : : {
14548 : 1102 : if (!gfc_resolve_expr (cl->length))
14549 : : {
14550 : 1 : specification_expr = saved_specification_expr;
14551 : 1 : return false;
14552 : : }
14553 : :
14554 : 1101 : if (!gfc_simplify_expr (cl->length, 0))
14555 : : {
14556 : 0 : specification_expr = saved_specification_expr;
14557 : 0 : return false;
14558 : : }
14559 : :
14560 : : /* cl->length has been resolved. It should have an integer type. */
14561 : 1101 : if (cl->length
14562 : 1100 : && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
14563 : : {
14564 : 4 : gfc_error ("Scalar INTEGER expression expected at %L",
14565 : : &cl->length->where);
14566 : 4 : return false;
14567 : : }
14568 : : }
14569 : : else
14570 : : {
14571 : 91280 : if (!resolve_index_expr (cl->length))
14572 : : {
14573 : 19 : specification_expr = saved_specification_expr;
14574 : 19 : return false;
14575 : : }
14576 : : }
14577 : :
14578 : : /* F2008, 4.4.3.2: If the character length parameter value evaluates to
14579 : : a negative value, the length of character entities declared is zero. */
14580 : 92358 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14581 : 55141 : && mpz_sgn (cl->length->value.integer) < 0)
14582 : 0 : gfc_replace_expr (cl->length,
14583 : : gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
14584 : :
14585 : : /* Check that the character length is not too large. */
14586 : 92358 : k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
14587 : 92358 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14588 : 55141 : && cl->length->ts.type == BT_INTEGER
14589 : 55141 : && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
14590 : : {
14591 : 4 : gfc_error ("String length at %L is too large", &cl->length->where);
14592 : 4 : specification_expr = saved_specification_expr;
14593 : 4 : return false;
14594 : : }
14595 : :
14596 : 92354 : specification_expr = saved_specification_expr;
14597 : 92354 : return true;
14598 : : }
14599 : :
14600 : :
14601 : : /* Test for non-constant shape arrays. */
14602 : :
14603 : : static bool
14604 : 115446 : is_non_constant_shape_array (gfc_symbol *sym)
14605 : : {
14606 : 115446 : gfc_expr *e;
14607 : 115446 : int i;
14608 : 115446 : bool not_constant;
14609 : :
14610 : 115446 : not_constant = false;
14611 : 115446 : if (sym->as != NULL)
14612 : : {
14613 : : /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
14614 : : has not been simplified; parameter array references. Do the
14615 : : simplification now. */
14616 : 152891 : for (i = 0; i < sym->as->rank + sym->as->corank; i++)
14617 : : {
14618 : 88315 : if (i == GFC_MAX_DIMENSIONS)
14619 : : break;
14620 : :
14621 : 88313 : e = sym->as->lower[i];
14622 : 88313 : if (e && (!resolve_index_expr(e)
14623 : 85558 : || !gfc_is_constant_expr (e)))
14624 : : not_constant = true;
14625 : 88313 : e = sym->as->upper[i];
14626 : 88313 : if (e && (!resolve_index_expr(e)
14627 : 84518 : || !gfc_is_constant_expr (e)))
14628 : : not_constant = true;
14629 : : }
14630 : : }
14631 : 115446 : return not_constant;
14632 : : }
14633 : :
14634 : : /* Given a symbol and an initialization expression, add code to initialize
14635 : : the symbol to the function entry. */
14636 : : static void
14637 : 1938 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
14638 : : {
14639 : 1938 : gfc_expr *lval;
14640 : 1938 : gfc_code *init_st;
14641 : 1938 : gfc_namespace *ns = sym->ns;
14642 : :
14643 : 1938 : if (sym->attr.function && sym->result == sym
14644 : 317 : && sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_type)
14645 : : {
14646 : 14 : gfc_free_expr (init);
14647 : 14 : return;
14648 : : }
14649 : :
14650 : : /* Search for the function namespace if this is a contained
14651 : : function without an explicit result. */
14652 : 1924 : if (sym->attr.function && sym == sym->result
14653 : 303 : && sym->name != sym->ns->proc_name->name)
14654 : : {
14655 : 302 : ns = ns->contained;
14656 : 1365 : for (;ns; ns = ns->sibling)
14657 : 1299 : if (strcmp (ns->proc_name->name, sym->name) == 0)
14658 : : break;
14659 : : }
14660 : :
14661 : 1924 : if (ns == NULL)
14662 : : {
14663 : 66 : gfc_free_expr (init);
14664 : 66 : return;
14665 : : }
14666 : :
14667 : : /* Build an l-value expression for the result. */
14668 : 1858 : lval = gfc_lval_expr_from_sym (sym);
14669 : :
14670 : : /* Add the code at scope entry. */
14671 : 1858 : init_st = gfc_get_code (EXEC_INIT_ASSIGN);
14672 : 1858 : init_st->next = ns->code;
14673 : 1858 : ns->code = init_st;
14674 : :
14675 : : /* Assign the default initializer to the l-value. */
14676 : 1858 : init_st->loc = sym->declared_at;
14677 : 1858 : init_st->expr1 = lval;
14678 : 1858 : init_st->expr2 = init;
14679 : : }
14680 : :
14681 : :
14682 : : /* Whether or not we can generate a default initializer for a symbol. */
14683 : :
14684 : : static bool
14685 : 29024 : can_generate_init (gfc_symbol *sym)
14686 : : {
14687 : 29024 : symbol_attribute *a;
14688 : 29024 : if (!sym)
14689 : : return false;
14690 : 29024 : a = &sym->attr;
14691 : :
14692 : : /* These symbols should never have a default initialization. */
14693 : 47646 : return !(
14694 : 29024 : a->allocatable
14695 : 29024 : || a->external
14696 : 27877 : || a->pointer
14697 : 27877 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
14698 : 5513 : && (CLASS_DATA (sym)->attr.class_pointer
14699 : 3610 : || CLASS_DATA (sym)->attr.proc_pointer))
14700 : 25974 : || a->in_equivalence
14701 : 25853 : || a->in_common
14702 : 25806 : || a->data
14703 : 25628 : || sym->module
14704 : 21888 : || a->cray_pointee
14705 : 21826 : || a->cray_pointer
14706 : 21826 : || sym->assoc
14707 : 19248 : || (!a->referenced && !a->result)
14708 : 18622 : || (a->dummy && (a->intent != INTENT_OUT
14709 : 1061 : || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
14710 : 18622 : || (a->function && sym != sym->result)
14711 : : );
14712 : : }
14713 : :
14714 : :
14715 : : /* Assign the default initializer to a derived type variable or result. */
14716 : :
14717 : : static void
14718 : 11125 : apply_default_init (gfc_symbol *sym)
14719 : : {
14720 : 11125 : gfc_expr *init = NULL;
14721 : :
14722 : 11125 : if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14723 : : return;
14724 : :
14725 : 10926 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
14726 : 10119 : init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14727 : :
14728 : 10926 : if (init == NULL && sym->ts.type != BT_CLASS)
14729 : : return;
14730 : :
14731 : 1556 : build_init_assign (sym, init);
14732 : 1556 : sym->attr.referenced = 1;
14733 : : }
14734 : :
14735 : :
14736 : : /* Build an initializer for a local. Returns null if the symbol should not have
14737 : : a default initialization. */
14738 : :
14739 : : static gfc_expr *
14740 : 200341 : build_default_init_expr (gfc_symbol *sym)
14741 : : {
14742 : : /* These symbols should never have a default initialization. */
14743 : 200341 : if (sym->attr.allocatable
14744 : 186970 : || sym->attr.external
14745 : 186970 : || sym->attr.dummy
14746 : 122849 : || sym->attr.pointer
14747 : 114826 : || sym->attr.in_equivalence
14748 : 112450 : || sym->attr.in_common
14749 : 109397 : || sym->attr.data
14750 : 107101 : || sym->module
14751 : 104764 : || sym->attr.cray_pointee
14752 : 104463 : || sym->attr.cray_pointer
14753 : 104161 : || sym->assoc)
14754 : : return NULL;
14755 : :
14756 : : /* Get the appropriate init expression. */
14757 : 99673 : return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
14758 : : }
14759 : :
14760 : : /* Add an initialization expression to a local variable. */
14761 : : static void
14762 : 200341 : apply_default_init_local (gfc_symbol *sym)
14763 : : {
14764 : 200341 : gfc_expr *init = NULL;
14765 : :
14766 : : /* The symbol should be a variable or a function return value. */
14767 : 200341 : if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14768 : 200341 : || (sym->attr.function && sym->result != sym))
14769 : : return;
14770 : :
14771 : : /* Try to build the initializer expression. If we can't initialize
14772 : : this symbol, then init will be NULL. */
14773 : 200341 : init = build_default_init_expr (sym);
14774 : 200341 : if (init == NULL)
14775 : : return;
14776 : :
14777 : : /* For saved variables, we don't want to add an initializer at function
14778 : : entry, so we just add a static initializer. Note that automatic variables
14779 : : are stack allocated even with -fno-automatic; we have also to exclude
14780 : : result variable, which are also nonstatic. */
14781 : 419 : if (!sym->attr.automatic
14782 : 419 : && (sym->attr.save || sym->ns->save_all
14783 : 377 : || (flag_max_stack_var_size == 0 && !sym->attr.result
14784 : 27 : && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
14785 : 14 : && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
14786 : : {
14787 : : /* Don't clobber an existing initializer! */
14788 : 37 : gcc_assert (sym->value == NULL);
14789 : 37 : sym->value = init;
14790 : 37 : return;
14791 : : }
14792 : :
14793 : 382 : build_init_assign (sym, init);
14794 : : }
14795 : :
14796 : :
14797 : : /* Resolution of common features of flavors variable and procedure. */
14798 : :
14799 : : static bool
14800 : 948044 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
14801 : : {
14802 : 948044 : gfc_array_spec *as;
14803 : :
14804 : 948044 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14805 : 18738 : && sym->ts.u.derived && CLASS_DATA (sym))
14806 : 18732 : as = CLASS_DATA (sym)->as;
14807 : : else
14808 : 929312 : as = sym->as;
14809 : :
14810 : : /* Constraints on deferred shape variable. */
14811 : 948044 : if (as == NULL || as->type != AS_DEFERRED)
14812 : : {
14813 : 924314 : bool pointer, allocatable, dimension;
14814 : :
14815 : 924314 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14816 : 15641 : && sym->ts.u.derived && CLASS_DATA (sym))
14817 : : {
14818 : 15635 : pointer = CLASS_DATA (sym)->attr.class_pointer;
14819 : 15635 : allocatable = CLASS_DATA (sym)->attr.allocatable;
14820 : 15635 : dimension = CLASS_DATA (sym)->attr.dimension;
14821 : : }
14822 : : else
14823 : : {
14824 : 908679 : pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
14825 : 908679 : allocatable = sym->attr.allocatable;
14826 : 908679 : dimension = sym->attr.dimension;
14827 : : }
14828 : :
14829 : 924314 : if (allocatable)
14830 : : {
14831 : 7890 : if (dimension
14832 : 7890 : && as
14833 : 523 : && as->type != AS_ASSUMED_RANK
14834 : 5 : && !sym->attr.select_rank_temporary)
14835 : : {
14836 : 3 : gfc_error ("Allocatable array %qs at %L must have a deferred "
14837 : : "shape or assumed rank", sym->name, &sym->declared_at);
14838 : 3 : return false;
14839 : : }
14840 : 7887 : else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
14841 : : "%qs at %L may not be ALLOCATABLE",
14842 : : sym->name, &sym->declared_at))
14843 : : return false;
14844 : : }
14845 : :
14846 : 924310 : if (pointer && dimension && as->type != AS_ASSUMED_RANK)
14847 : : {
14848 : 4 : gfc_error ("Array pointer %qs at %L must have a deferred shape or "
14849 : : "assumed rank", sym->name, &sym->declared_at);
14850 : 4 : sym->error = 1;
14851 : 4 : return false;
14852 : : }
14853 : : }
14854 : : else
14855 : : {
14856 : 23730 : if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
14857 : 4505 : && sym->ts.type != BT_CLASS && !sym->assoc)
14858 : : {
14859 : 3 : gfc_error ("Array %qs at %L cannot have a deferred shape",
14860 : : sym->name, &sym->declared_at);
14861 : 3 : return false;
14862 : : }
14863 : : }
14864 : :
14865 : : /* Constraints on polymorphic variables. */
14866 : 948033 : if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
14867 : : {
14868 : : /* F03:C502. */
14869 : 18078 : if (sym->attr.class_ok
14870 : 18022 : && sym->ts.u.derived
14871 : 18017 : && !sym->attr.select_type_temporary
14872 : 16957 : && !UNLIMITED_POLY (sym)
14873 : 14513 : && CLASS_DATA (sym)
14874 : 14512 : && CLASS_DATA (sym)->ts.u.derived
14875 : 32589 : && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
14876 : : {
14877 : 5 : gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
14878 : 5 : CLASS_DATA (sym)->ts.u.derived->name, sym->name,
14879 : : &sym->declared_at);
14880 : 5 : return false;
14881 : : }
14882 : :
14883 : : /* F03:C509. */
14884 : : /* Assume that use associated symbols were checked in the module ns.
14885 : : Class-variables that are associate-names are also something special
14886 : : and excepted from the test. */
14887 : 18073 : if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
14888 : 54 : && !sym->attr.select_type_temporary
14889 : 54 : && !sym->attr.select_rank_temporary)
14890 : : {
14891 : 54 : gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
14892 : : "or pointer", sym->name, &sym->declared_at);
14893 : 54 : return false;
14894 : : }
14895 : : }
14896 : :
14897 : : return true;
14898 : : }
14899 : :
14900 : :
14901 : : /* Additional checks for symbols with flavor variable and derived
14902 : : type. To be called from resolve_fl_variable. */
14903 : :
14904 : : static bool
14905 : 79492 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
14906 : : {
14907 : 79492 : gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
14908 : :
14909 : : /* Check to see if a derived type is blocked from being host
14910 : : associated by the presence of another class I symbol in the same
14911 : : namespace. 14.6.1.3 of the standard and the discussion on
14912 : : comp.lang.fortran. */
14913 : 79492 : if (sym->ts.u.derived
14914 : 79487 : && sym->ns != sym->ts.u.derived->ns
14915 : 45713 : && !sym->ts.u.derived->attr.use_assoc
14916 : 16758 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
14917 : : {
14918 : 15822 : gfc_symbol *s;
14919 : 15822 : gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
14920 : 15822 : if (s && s->attr.generic)
14921 : 2 : s = gfc_find_dt_in_generic (s);
14922 : 15822 : if (s && !gfc_fl_struct (s->attr.flavor))
14923 : : {
14924 : 2 : gfc_error ("The type %qs cannot be host associated at %L "
14925 : : "because it is blocked by an incompatible object "
14926 : : "of the same name declared at %L",
14927 : 2 : sym->ts.u.derived->name, &sym->declared_at,
14928 : : &s->declared_at);
14929 : 2 : return false;
14930 : : }
14931 : : }
14932 : :
14933 : : /* 4th constraint in section 11.3: "If an object of a type for which
14934 : : component-initialization is specified (R429) appears in the
14935 : : specification-part of a module and does not have the ALLOCATABLE
14936 : : or POINTER attribute, the object shall have the SAVE attribute."
14937 : :
14938 : : The check for initializers is performed with
14939 : : gfc_has_default_initializer because gfc_default_initializer generates
14940 : : a hidden default for allocatable components. */
14941 : 78878 : if (!(sym->value || no_init_flag) && sym->ns->proc_name
14942 : 17678 : && sym->ns->proc_name->attr.flavor == FL_MODULE
14943 : 356 : && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
14944 : 20 : && !sym->attr.pointer && !sym->attr.allocatable
14945 : 20 : && gfc_has_default_initializer (sym->ts.u.derived)
14946 : 79499 : && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
14947 : : "%qs at %L, needed due to the default "
14948 : : "initialization", sym->name, &sym->declared_at))
14949 : : return false;
14950 : :
14951 : : /* Assign default initializer. */
14952 : 79488 : if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
14953 : 73486 : && (!no_init_flag
14954 : 57586 : || (sym->attr.intent == INTENT_OUT
14955 : 3179 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
14956 : 18905 : sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14957 : :
14958 : : return true;
14959 : : }
14960 : :
14961 : :
14962 : : /* F2008, C402 (R401): A colon shall not be used as a type-param-value
14963 : : except in the declaration of an entity or component that has the POINTER
14964 : : or ALLOCATABLE attribute. */
14965 : :
14966 : : static bool
14967 : 1472692 : deferred_requirements (gfc_symbol *sym)
14968 : : {
14969 : 1472692 : if (sym->ts.deferred
14970 : 7667 : && !(sym->attr.pointer
14971 : 2314 : || sym->attr.allocatable
14972 : 92 : || sym->attr.associate_var
14973 : 7 : || sym->attr.omp_udr_artificial_var))
14974 : : {
14975 : : /* If a function has a result variable, only check the variable. */
14976 : 7 : if (sym->result && sym->name != sym->result->name)
14977 : : return true;
14978 : :
14979 : 6 : gfc_error ("Entity %qs at %L has a deferred type parameter and "
14980 : : "requires either the POINTER or ALLOCATABLE attribute",
14981 : : sym->name, &sym->declared_at);
14982 : 6 : return false;
14983 : : }
14984 : : return true;
14985 : : }
14986 : :
14987 : :
14988 : : /* Resolve symbols with flavor variable. */
14989 : :
14990 : : static bool
14991 : 636875 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
14992 : : {
14993 : 636875 : const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
14994 : : "SAVE attribute");
14995 : :
14996 : 636875 : if (!resolve_fl_var_and_proc (sym, mp_flag))
14997 : : return false;
14998 : :
14999 : : /* Set this flag to check that variables are parameters of all entries.
15000 : : This check is effected by the call to gfc_resolve_expr through
15001 : : is_non_constant_shape_array. */
15002 : 636815 : bool saved_specification_expr = specification_expr;
15003 : 636815 : specification_expr = true;
15004 : :
15005 : 636815 : if (sym->ns->proc_name
15006 : 636728 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
15007 : 631931 : || sym->ns->proc_name->attr.is_main_program)
15008 : 80727 : && !sym->attr.use_assoc
15009 : 77711 : && !sym->attr.allocatable
15010 : 72194 : && !sym->attr.pointer
15011 : 705403 : && is_non_constant_shape_array (sym))
15012 : : {
15013 : : /* F08:C541. The shape of an array defined in a main program or module
15014 : : * needs to be constant. */
15015 : 3 : gfc_error ("The module or main program array %qs at %L must "
15016 : : "have constant shape", sym->name, &sym->declared_at);
15017 : 3 : specification_expr = saved_specification_expr;
15018 : 3 : return false;
15019 : : }
15020 : :
15021 : : /* Constraints on deferred type parameter. */
15022 : 636812 : if (!deferred_requirements (sym))
15023 : : return false;
15024 : :
15025 : 636808 : if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
15026 : : {
15027 : : /* Make sure that character string variables with assumed length are
15028 : : dummy arguments. */
15029 : 35389 : gfc_expr *e = NULL;
15030 : :
15031 : 35389 : if (sym->ts.u.cl)
15032 : 35389 : e = sym->ts.u.cl->length;
15033 : : else
15034 : : return false;
15035 : :
15036 : 35389 : if (e == NULL && !sym->attr.dummy && !sym->attr.result
15037 : 2537 : && !sym->ts.deferred && !sym->attr.select_type_temporary
15038 : 2 : && !sym->attr.omp_udr_artificial_var)
15039 : : {
15040 : 2 : gfc_error ("Entity with assumed character length at %L must be a "
15041 : : "dummy argument or a PARAMETER", &sym->declared_at);
15042 : 2 : specification_expr = saved_specification_expr;
15043 : 2 : return false;
15044 : : }
15045 : :
15046 : 20538 : if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
15047 : : {
15048 : 1 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15049 : 1 : specification_expr = saved_specification_expr;
15050 : 1 : return false;
15051 : : }
15052 : :
15053 : 35386 : if (!gfc_is_constant_expr (e)
15054 : 35386 : && !(e->expr_type == EXPR_VARIABLE
15055 : 1388 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
15056 : : {
15057 : 2184 : if (!sym->attr.use_assoc && sym->ns->proc_name
15058 : 1680 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
15059 : 1679 : || sym->ns->proc_name->attr.is_main_program))
15060 : : {
15061 : 3 : gfc_error ("%qs at %L must have constant character length "
15062 : : "in this context", sym->name, &sym->declared_at);
15063 : 3 : specification_expr = saved_specification_expr;
15064 : 3 : return false;
15065 : : }
15066 : 2181 : if (sym->attr.in_common)
15067 : : {
15068 : 1 : gfc_error ("COMMON variable %qs at %L must have constant "
15069 : : "character length", sym->name, &sym->declared_at);
15070 : 1 : specification_expr = saved_specification_expr;
15071 : 1 : return false;
15072 : : }
15073 : : }
15074 : : }
15075 : :
15076 : 636801 : if (sym->value == NULL && sym->attr.referenced
15077 : 202220 : && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
15078 : 200341 : apply_default_init_local (sym); /* Try to apply a default initialization. */
15079 : :
15080 : : /* Determine if the symbol may not have an initializer. */
15081 : 636801 : int no_init_flag = 0, automatic_flag = 0;
15082 : 636801 : if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
15083 : 167489 : || sym->attr.intrinsic || sym->attr.result)
15084 : : no_init_flag = 1;
15085 : 135452 : else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
15086 : 169513 : && is_non_constant_shape_array (sym))
15087 : : {
15088 : 1329 : no_init_flag = automatic_flag = 1;
15089 : :
15090 : : /* Also, they must not have the SAVE attribute.
15091 : : SAVE_IMPLICIT is checked below. */
15092 : 1329 : if (sym->as && sym->attr.codimension)
15093 : : {
15094 : 7 : int corank = sym->as->corank;
15095 : 7 : sym->as->corank = 0;
15096 : 7 : no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
15097 : 7 : sym->as->corank = corank;
15098 : : }
15099 : 1329 : if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
15100 : : {
15101 : 2 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15102 : 2 : specification_expr = saved_specification_expr;
15103 : 2 : return false;
15104 : : }
15105 : : }
15106 : :
15107 : : /* Ensure that any initializer is simplified. */
15108 : 636799 : if (sym->value)
15109 : 7748 : gfc_simplify_expr (sym->value, 1);
15110 : :
15111 : : /* Reject illegal initializers. */
15112 : 636799 : if (!sym->mark && sym->value)
15113 : : {
15114 : 7748 : if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
15115 : 67 : && CLASS_DATA (sym)->attr.allocatable))
15116 : 1 : gfc_error ("Allocatable %qs at %L cannot have an initializer",
15117 : : sym->name, &sym->declared_at);
15118 : 7747 : else if (sym->attr.external)
15119 : 0 : gfc_error ("External %qs at %L cannot have an initializer",
15120 : : sym->name, &sym->declared_at);
15121 : 7747 : else if (sym->attr.dummy)
15122 : 3 : gfc_error ("Dummy %qs at %L cannot have an initializer",
15123 : : sym->name, &sym->declared_at);
15124 : 7744 : else if (sym->attr.intrinsic)
15125 : 0 : gfc_error ("Intrinsic %qs at %L cannot have an initializer",
15126 : : sym->name, &sym->declared_at);
15127 : 7744 : else if (sym->attr.result)
15128 : 1 : gfc_error ("Function result %qs at %L cannot have an initializer",
15129 : : sym->name, &sym->declared_at);
15130 : 7743 : else if (automatic_flag)
15131 : 5 : gfc_error ("Automatic array %qs at %L cannot have an initializer",
15132 : : sym->name, &sym->declared_at);
15133 : : else
15134 : 7738 : goto no_init_error;
15135 : 10 : specification_expr = saved_specification_expr;
15136 : 10 : return false;
15137 : : }
15138 : :
15139 : 629051 : no_init_error:
15140 : 636789 : if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
15141 : : {
15142 : 79492 : bool res = resolve_fl_variable_derived (sym, no_init_flag);
15143 : 79492 : specification_expr = saved_specification_expr;
15144 : 79492 : return res;
15145 : : }
15146 : :
15147 : 557297 : specification_expr = saved_specification_expr;
15148 : 557297 : return true;
15149 : : }
15150 : :
15151 : :
15152 : : /* Compare the dummy characteristics of a module procedure interface
15153 : : declaration with the corresponding declaration in a submodule. */
15154 : : static gfc_formal_arglist *new_formal;
15155 : : static char errmsg[200];
15156 : :
15157 : : static void
15158 : 1271 : compare_fsyms (gfc_symbol *sym)
15159 : : {
15160 : 1271 : gfc_symbol *fsym;
15161 : :
15162 : 1271 : if (sym == NULL || new_formal == NULL)
15163 : : return;
15164 : :
15165 : 1271 : fsym = new_formal->sym;
15166 : :
15167 : 1271 : if (sym == fsym)
15168 : : return;
15169 : :
15170 : 1247 : if (strcmp (sym->name, fsym->name) == 0)
15171 : : {
15172 : 472 : if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
15173 : 2 : gfc_error ("%s at %L", errmsg, &fsym->declared_at);
15174 : : }
15175 : : }
15176 : :
15177 : :
15178 : : /* Resolve a procedure. */
15179 : :
15180 : : static bool
15181 : 464642 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
15182 : : {
15183 : 464642 : gfc_formal_arglist *arg;
15184 : 464642 : bool allocatable_or_pointer = false;
15185 : :
15186 : 464642 : if (sym->attr.function
15187 : 464642 : && !resolve_fl_var_and_proc (sym, mp_flag))
15188 : : return false;
15189 : :
15190 : : /* Constraints on deferred type parameter. */
15191 : 464632 : if (!deferred_requirements (sym))
15192 : : return false;
15193 : :
15194 : 464631 : if (sym->ts.type == BT_CHARACTER)
15195 : : {
15196 : 11254 : gfc_charlen *cl = sym->ts.u.cl;
15197 : :
15198 : 7207 : if (cl && cl->length && gfc_is_constant_expr (cl->length)
15199 : 12340 : && !resolve_charlen (cl))
15200 : : return false;
15201 : :
15202 : 11253 : if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15203 : 10168 : && sym->attr.proc == PROC_ST_FUNCTION)
15204 : : {
15205 : 0 : gfc_error ("Character-valued statement function %qs at %L must "
15206 : : "have constant length", sym->name, &sym->declared_at);
15207 : 0 : return false;
15208 : : }
15209 : : }
15210 : :
15211 : : /* Ensure that derived type for are not of a private type. Internal
15212 : : module procedures are excluded by 2.2.3.3 - i.e., they are not
15213 : : externally accessible and can access all the objects accessible in
15214 : : the host. */
15215 : 107253 : if (!(sym->ns->parent && sym->ns->parent->proc_name
15216 : 107253 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
15217 : 548762 : && gfc_check_symbol_access (sym))
15218 : : {
15219 : 434680 : gfc_interface *iface;
15220 : :
15221 : 918187 : for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
15222 : : {
15223 : 483508 : if (arg->sym
15224 : 483367 : && arg->sym->ts.type == BT_DERIVED
15225 : 41922 : && arg->sym->ts.u.derived
15226 : 41922 : && !arg->sym->ts.u.derived->attr.use_assoc
15227 : 4258 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15228 : 483517 : && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
15229 : : "and cannot be a dummy argument"
15230 : : " of %qs, which is PUBLIC at %L",
15231 : 9 : arg->sym->name, sym->name,
15232 : : &sym->declared_at))
15233 : : {
15234 : : /* Stop this message from recurring. */
15235 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15236 : 1 : return false;
15237 : : }
15238 : : }
15239 : :
15240 : : /* PUBLIC interfaces may expose PRIVATE procedures that take types
15241 : : PRIVATE to the containing module. */
15242 : 619507 : for (iface = sym->generic; iface; iface = iface->next)
15243 : : {
15244 : 432640 : for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
15245 : : {
15246 : 247812 : if (arg->sym
15247 : 247780 : && arg->sym->ts.type == BT_DERIVED
15248 : 7914 : && !arg->sym->ts.u.derived->attr.use_assoc
15249 : 234 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15250 : 247816 : && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
15251 : : "PUBLIC interface %qs at %L "
15252 : : "takes dummy arguments of %qs which "
15253 : : "is PRIVATE", iface->sym->name,
15254 : 4 : sym->name, &iface->sym->declared_at,
15255 : 4 : gfc_typename(&arg->sym->ts)))
15256 : : {
15257 : : /* Stop this message from recurring. */
15258 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15259 : 1 : return false;
15260 : : }
15261 : : }
15262 : : }
15263 : : }
15264 : :
15265 : 464628 : if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
15266 : 67 : && !sym->attr.proc_pointer)
15267 : : {
15268 : 2 : gfc_error ("Function %qs at %L cannot have an initializer",
15269 : : sym->name, &sym->declared_at);
15270 : :
15271 : : /* Make sure no second error is issued for this. */
15272 : 2 : sym->value->error = 1;
15273 : 2 : return false;
15274 : : }
15275 : :
15276 : : /* An external symbol may not have an initializer because it is taken to be
15277 : : a procedure. Exception: Procedure Pointers. */
15278 : 464626 : if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
15279 : : {
15280 : 0 : gfc_error ("External object %qs at %L may not have an initializer",
15281 : : sym->name, &sym->declared_at);
15282 : 0 : return false;
15283 : : }
15284 : :
15285 : : /* An elemental function is required to return a scalar 12.7.1 */
15286 : 464626 : if (sym->attr.elemental && sym->attr.function
15287 : 86117 : && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15288 : 2 : && CLASS_DATA (sym)->as)))
15289 : : {
15290 : 3 : gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
15291 : : "result", sym->name, &sym->declared_at);
15292 : : /* Reset so that the error only occurs once. */
15293 : 3 : sym->attr.elemental = 0;
15294 : 3 : return false;
15295 : : }
15296 : :
15297 : 464623 : if (sym->attr.proc == PROC_ST_FUNCTION
15298 : 220 : && (sym->attr.allocatable || sym->attr.pointer))
15299 : : {
15300 : 2 : gfc_error ("Statement function %qs at %L may not have pointer or "
15301 : : "allocatable attribute", sym->name, &sym->declared_at);
15302 : 2 : return false;
15303 : : }
15304 : :
15305 : : /* 5.1.1.5 of the Standard: A function name declared with an asterisk
15306 : : char-len-param shall not be array-valued, pointer-valued, recursive
15307 : : or pure. ....snip... A character value of * may only be used in the
15308 : : following ways: (i) Dummy arg of procedure - dummy associates with
15309 : : actual length; (ii) To declare a named constant; or (iii) External
15310 : : function - but length must be declared in calling scoping unit. */
15311 : 464621 : if (sym->attr.function
15312 : 311150 : && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
15313 : 6425 : && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
15314 : : {
15315 : 180 : if ((sym->as && sym->as->rank) || (sym->attr.pointer)
15316 : 178 : || (sym->attr.recursive) || (sym->attr.pure))
15317 : : {
15318 : 4 : if (sym->as && sym->as->rank)
15319 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15320 : : "array-valued", sym->name, &sym->declared_at);
15321 : :
15322 : 4 : if (sym->attr.pointer)
15323 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15324 : : "pointer-valued", sym->name, &sym->declared_at);
15325 : :
15326 : 4 : if (sym->attr.pure)
15327 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15328 : : "pure", sym->name, &sym->declared_at);
15329 : :
15330 : 4 : if (sym->attr.recursive)
15331 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15332 : : "recursive", sym->name, &sym->declared_at);
15333 : :
15334 : 4 : return false;
15335 : : }
15336 : :
15337 : : /* Appendix B.2 of the standard. Contained functions give an
15338 : : error anyway. Deferred character length is an F2003 feature.
15339 : : Don't warn on intrinsic conversion functions, which start
15340 : : with two underscores. */
15341 : 176 : if (!sym->attr.contained && !sym->ts.deferred
15342 : 172 : && (sym->name[0] != '_' || sym->name[1] != '_'))
15343 : 172 : gfc_notify_std (GFC_STD_F95_OBS,
15344 : : "CHARACTER(*) function %qs at %L",
15345 : : sym->name, &sym->declared_at);
15346 : : }
15347 : :
15348 : : /* F2008, C1218. */
15349 : 464617 : if (sym->attr.elemental)
15350 : : {
15351 : 89287 : if (sym->attr.proc_pointer)
15352 : : {
15353 : 7 : const char* name = (sym->attr.result ? sym->ns->proc_name->name
15354 : : : sym->name);
15355 : 7 : gfc_error ("Procedure pointer %qs at %L shall not be elemental",
15356 : : name, &sym->declared_at);
15357 : 7 : return false;
15358 : : }
15359 : 89280 : if (sym->attr.dummy)
15360 : : {
15361 : 3 : gfc_error ("Dummy procedure %qs at %L shall not be elemental",
15362 : : sym->name, &sym->declared_at);
15363 : 3 : return false;
15364 : : }
15365 : : }
15366 : :
15367 : : /* F2018, C15100: "The result of an elemental function shall be scalar,
15368 : : and shall not have the POINTER or ALLOCATABLE attribute." The scalar
15369 : : pointer is tested and caught elsewhere. */
15370 : 464607 : if (sym->result)
15371 : 261422 : allocatable_or_pointer = sym->result->ts.type == BT_CLASS
15372 : 261422 : && CLASS_DATA (sym->result) ?
15373 : 1653 : (CLASS_DATA (sym->result)->attr.allocatable
15374 : 1653 : || CLASS_DATA (sym->result)->attr.pointer) :
15375 : 259769 : (sym->result->attr.allocatable
15376 : 259769 : || sym->result->attr.pointer);
15377 : :
15378 : 464607 : if (sym->attr.elemental && sym->result
15379 : 85742 : && allocatable_or_pointer)
15380 : : {
15381 : 4 : gfc_error ("Function result variable %qs at %L of elemental "
15382 : : "function %qs shall not have an ALLOCATABLE or POINTER "
15383 : : "attribute", sym->result->name,
15384 : : &sym->result->declared_at, sym->name);
15385 : 4 : return false;
15386 : : }
15387 : :
15388 : 464603 : if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
15389 : : {
15390 : 6576 : gfc_formal_arglist *curr_arg;
15391 : 6576 : int has_non_interop_arg = 0;
15392 : :
15393 : 6576 : if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15394 : 6576 : sym->common_block))
15395 : : {
15396 : : /* Clear these to prevent looking at them again if there was an
15397 : : error. */
15398 : 2 : sym->attr.is_bind_c = 0;
15399 : 2 : sym->attr.is_c_interop = 0;
15400 : 2 : sym->ts.is_c_interop = 0;
15401 : : }
15402 : : else
15403 : : {
15404 : : /* So far, no errors have been found. */
15405 : 6574 : sym->attr.is_c_interop = 1;
15406 : 6574 : sym->ts.is_c_interop = 1;
15407 : : }
15408 : :
15409 : 6576 : curr_arg = gfc_sym_get_dummy_args (sym);
15410 : 29555 : while (curr_arg != NULL)
15411 : : {
15412 : : /* Skip implicitly typed dummy args here. */
15413 : 16403 : if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
15414 : 16347 : if (!gfc_verify_c_interop_param (curr_arg->sym))
15415 : : /* If something is found to fail, record the fact so we
15416 : : can mark the symbol for the procedure as not being
15417 : : BIND(C) to try and prevent multiple errors being
15418 : : reported. */
15419 : 16403 : has_non_interop_arg = 1;
15420 : :
15421 : 16403 : curr_arg = curr_arg->next;
15422 : : }
15423 : :
15424 : : /* See if any of the arguments were not interoperable and if so, clear
15425 : : the procedure symbol to prevent duplicate error messages. */
15426 : 6576 : if (has_non_interop_arg != 0)
15427 : : {
15428 : 128 : sym->attr.is_c_interop = 0;
15429 : 128 : sym->ts.is_c_interop = 0;
15430 : 128 : sym->attr.is_bind_c = 0;
15431 : : }
15432 : : }
15433 : :
15434 : 464603 : if (!sym->attr.proc_pointer)
15435 : : {
15436 : 463559 : if (sym->attr.save == SAVE_EXPLICIT)
15437 : : {
15438 : 5 : gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
15439 : : "in %qs at %L", sym->name, &sym->declared_at);
15440 : 5 : return false;
15441 : : }
15442 : 463554 : if (sym->attr.intent)
15443 : : {
15444 : 1 : gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
15445 : : "in %qs at %L", sym->name, &sym->declared_at);
15446 : 1 : return false;
15447 : : }
15448 : 463553 : if (sym->attr.subroutine && sym->attr.result)
15449 : : {
15450 : 2 : gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
15451 : 2 : "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
15452 : 2 : return false;
15453 : : }
15454 : 463551 : if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
15455 : 132487 : && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
15456 : 132484 : || sym->attr.contained))
15457 : : {
15458 : 3 : gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
15459 : : "in %qs at %L", sym->name, &sym->declared_at);
15460 : 3 : return false;
15461 : : }
15462 : 463548 : if (strcmp ("ppr@", sym->name) == 0)
15463 : : {
15464 : 0 : gfc_error ("Procedure pointer result %qs at %L "
15465 : : "is missing the pointer attribute",
15466 : 0 : sym->ns->proc_name->name, &sym->declared_at);
15467 : 0 : return false;
15468 : : }
15469 : : }
15470 : :
15471 : : /* Assume that a procedure whose body is not known has references
15472 : : to external arrays. */
15473 : 464592 : if (sym->attr.if_source != IFSRC_DECL)
15474 : 320225 : sym->attr.array_outer_dependency = 1;
15475 : :
15476 : : /* Compare the characteristics of a module procedure with the
15477 : : interface declaration. Ideally this would be done with
15478 : : gfc_compare_interfaces but, at present, the formal interface
15479 : : cannot be copied to the ts.interface. */
15480 : 464592 : if (sym->attr.module_procedure
15481 : 1444 : && sym->attr.if_source == IFSRC_DECL)
15482 : : {
15483 : 607 : gfc_symbol *iface;
15484 : 607 : char name[2*GFC_MAX_SYMBOL_LEN + 1];
15485 : 607 : char *module_name;
15486 : 607 : char *submodule_name;
15487 : 607 : strcpy (name, sym->ns->proc_name->name);
15488 : 607 : module_name = strtok (name, ".");
15489 : 607 : submodule_name = strtok (NULL, ".");
15490 : :
15491 : 607 : iface = sym->tlink;
15492 : 607 : sym->tlink = NULL;
15493 : :
15494 : : /* Make sure that the result uses the correct charlen for deferred
15495 : : length results. */
15496 : 607 : if (iface && sym->result
15497 : 171 : && iface->ts.type == BT_CHARACTER
15498 : 19 : && iface->ts.deferred)
15499 : 6 : sym->result->ts.u.cl = iface->ts.u.cl;
15500 : :
15501 : 6 : if (iface == NULL)
15502 : 193 : goto check_formal;
15503 : :
15504 : : /* Check the procedure characteristics. */
15505 : 414 : if (sym->attr.elemental != iface->attr.elemental)
15506 : : {
15507 : 1 : gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
15508 : : "PROCEDURE at %L and its interface in %s",
15509 : : &sym->declared_at, module_name);
15510 : 10 : return false;
15511 : : }
15512 : :
15513 : 413 : if (sym->attr.pure != iface->attr.pure)
15514 : : {
15515 : 2 : gfc_error ("Mismatch in PURE attribute between MODULE "
15516 : : "PROCEDURE at %L and its interface in %s",
15517 : : &sym->declared_at, module_name);
15518 : 2 : return false;
15519 : : }
15520 : :
15521 : 411 : if (sym->attr.recursive != iface->attr.recursive)
15522 : : {
15523 : 2 : gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
15524 : : "PROCEDURE at %L and its interface in %s",
15525 : : &sym->declared_at, module_name);
15526 : 2 : return false;
15527 : : }
15528 : :
15529 : : /* Check the result characteristics. */
15530 : 409 : if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
15531 : : {
15532 : 5 : gfc_error ("%s between the MODULE PROCEDURE declaration "
15533 : : "in MODULE %qs and the declaration at %L in "
15534 : : "(SUB)MODULE %qs",
15535 : : errmsg, module_name, &sym->declared_at,
15536 : : submodule_name ? submodule_name : module_name);
15537 : 5 : return false;
15538 : : }
15539 : :
15540 : 404 : check_formal:
15541 : : /* Check the characteristics of the formal arguments. */
15542 : 597 : if (sym->formal && sym->formal_ns)
15543 : : {
15544 : 1167 : for (arg = sym->formal; arg && arg->sym; arg = arg->next)
15545 : : {
15546 : 670 : new_formal = arg;
15547 : 670 : gfc_traverse_ns (sym->formal_ns, compare_fsyms);
15548 : : }
15549 : : }
15550 : : }
15551 : :
15552 : : /* F2018:15.4.2.2 requires an explicit interface for procedures with the
15553 : : BIND(C) attribute. */
15554 : 464582 : if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
15555 : : {
15556 : 1 : gfc_error ("Interface of %qs at %L must be explicit",
15557 : : sym->name, &sym->declared_at);
15558 : 1 : return false;
15559 : : }
15560 : :
15561 : : return true;
15562 : : }
15563 : :
15564 : :
15565 : : /* Resolve a list of finalizer procedures. That is, after they have hopefully
15566 : : been defined and we now know their defined arguments, check that they fulfill
15567 : : the requirements of the standard for procedures used as finalizers. */
15568 : :
15569 : : static bool
15570 : 107354 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
15571 : : {
15572 : 107354 : gfc_finalizer* list;
15573 : 107354 : gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
15574 : 107354 : bool result = true;
15575 : 107354 : bool seen_scalar = false;
15576 : 107354 : gfc_symbol *vtab;
15577 : 107354 : gfc_component *c;
15578 : 107354 : gfc_symbol *parent = gfc_get_derived_super_type (derived);
15579 : :
15580 : 107354 : if (parent)
15581 : 14882 : gfc_resolve_finalizers (parent, finalizable);
15582 : :
15583 : : /* Ensure that derived-type components have a their finalizers resolved. */
15584 : 107354 : bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
15585 : 338032 : for (c = derived->components; c; c = c->next)
15586 : 230678 : if (c->ts.type == BT_DERIVED
15587 : 64447 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
15588 : : {
15589 : 7864 : bool has_final2 = false;
15590 : 7864 : if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
15591 : 0 : return false; /* Error. */
15592 : 7864 : has_final = has_final || has_final2;
15593 : : }
15594 : : /* Return early if not finalizable. */
15595 : 107354 : if (!has_final)
15596 : : {
15597 : 105090 : if (finalizable)
15598 : 7822 : *finalizable = false;
15599 : 105090 : return true;
15600 : : }
15601 : :
15602 : : /* Walk over the list of finalizer-procedures, check them, and if any one
15603 : : does not fit in with the standard's definition, print an error and remove
15604 : : it from the list. */
15605 : 2264 : prev_link = &derived->f2k_derived->finalizers;
15606 : 4672 : for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
15607 : : {
15608 : 2408 : gfc_formal_arglist *dummy_args;
15609 : 2408 : gfc_symbol* arg;
15610 : 2408 : gfc_finalizer* i;
15611 : 2408 : int my_rank;
15612 : :
15613 : : /* Skip this finalizer if we already resolved it. */
15614 : 2408 : if (list->proc_tree)
15615 : : {
15616 : 1939 : if (list->proc_tree->n.sym->formal->sym->as == NULL
15617 : 566 : || list->proc_tree->n.sym->formal->sym->as->rank == 0)
15618 : 1373 : seen_scalar = true;
15619 : 1939 : prev_link = &(list->next);
15620 : 1939 : continue;
15621 : : }
15622 : :
15623 : : /* Check this exists and is a SUBROUTINE. */
15624 : 469 : if (!list->proc_sym->attr.subroutine)
15625 : : {
15626 : 3 : gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
15627 : : list->proc_sym->name, &list->where);
15628 : 3 : goto error;
15629 : : }
15630 : :
15631 : : /* We should have exactly one argument. */
15632 : 466 : dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
15633 : 466 : if (!dummy_args || dummy_args->next)
15634 : : {
15635 : 2 : gfc_error ("FINAL procedure at %L must have exactly one argument",
15636 : : &list->where);
15637 : 2 : goto error;
15638 : : }
15639 : 464 : arg = dummy_args->sym;
15640 : :
15641 : 464 : if (!arg)
15642 : : {
15643 : 1 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15644 : 1 : &list->proc_sym->declared_at, derived->name);
15645 : 1 : goto error;
15646 : : }
15647 : :
15648 : 463 : if (arg->as && arg->as->type == AS_ASSUMED_RANK
15649 : 6 : && ((list != derived->f2k_derived->finalizers) || list->next))
15650 : : {
15651 : 0 : gfc_error ("FINAL procedure at %L with assumed rank argument must "
15652 : : "be the only finalizer with the same kind/type "
15653 : : "(F2018: C790)", &list->where);
15654 : 0 : goto error;
15655 : : }
15656 : :
15657 : : /* This argument must be of our type. */
15658 : 463 : if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
15659 : : {
15660 : 2 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15661 : : &arg->declared_at, derived->name);
15662 : 2 : goto error;
15663 : : }
15664 : :
15665 : : /* It must neither be a pointer nor allocatable nor optional. */
15666 : 461 : if (arg->attr.pointer)
15667 : : {
15668 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
15669 : : &arg->declared_at);
15670 : 1 : goto error;
15671 : : }
15672 : 460 : if (arg->attr.allocatable)
15673 : : {
15674 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15675 : : " ALLOCATABLE", &arg->declared_at);
15676 : 1 : goto error;
15677 : : }
15678 : 459 : if (arg->attr.optional)
15679 : : {
15680 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
15681 : : &arg->declared_at);
15682 : 1 : goto error;
15683 : : }
15684 : :
15685 : : /* It must not be INTENT(OUT). */
15686 : 458 : if (arg->attr.intent == INTENT_OUT)
15687 : : {
15688 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15689 : : " INTENT(OUT)", &arg->declared_at);
15690 : 1 : goto error;
15691 : : }
15692 : :
15693 : : /* Warn if the procedure is non-scalar and not assumed shape. */
15694 : 457 : if (warn_surprising && arg->as && arg->as->rank != 0
15695 : 3 : && arg->as->type != AS_ASSUMED_SHAPE)
15696 : 2 : gfc_warning (OPT_Wsurprising,
15697 : : "Non-scalar FINAL procedure at %L should have assumed"
15698 : : " shape argument", &arg->declared_at);
15699 : :
15700 : : /* Check that it does not match in kind and rank with a FINAL procedure
15701 : : defined earlier. To really loop over the *earlier* declarations,
15702 : : we need to walk the tail of the list as new ones were pushed at the
15703 : : front. */
15704 : : /* TODO: Handle kind parameters once they are implemented. */
15705 : 457 : my_rank = (arg->as ? arg->as->rank : 0);
15706 : 546 : for (i = list->next; i; i = i->next)
15707 : : {
15708 : 91 : gfc_formal_arglist *dummy_args;
15709 : :
15710 : : /* Argument list might be empty; that is an error signalled earlier,
15711 : : but we nevertheless continued resolving. */
15712 : 91 : dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
15713 : 91 : if (dummy_args)
15714 : : {
15715 : 89 : gfc_symbol* i_arg = dummy_args->sym;
15716 : 89 : const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
15717 : 89 : if (i_rank == my_rank)
15718 : : {
15719 : 2 : gfc_error ("FINAL procedure %qs declared at %L has the same"
15720 : : " rank (%d) as %qs",
15721 : 2 : list->proc_sym->name, &list->where, my_rank,
15722 : 2 : i->proc_sym->name);
15723 : 2 : goto error;
15724 : : }
15725 : : }
15726 : : }
15727 : :
15728 : : /* Is this the/a scalar finalizer procedure? */
15729 : 455 : if (my_rank == 0)
15730 : 335 : seen_scalar = true;
15731 : :
15732 : : /* Find the symtree for this procedure. */
15733 : 455 : gcc_assert (!list->proc_tree);
15734 : 455 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
15735 : :
15736 : 455 : prev_link = &list->next;
15737 : 455 : continue;
15738 : :
15739 : : /* Remove wrong nodes immediately from the list so we don't risk any
15740 : : troubles in the future when they might fail later expectations. */
15741 : 14 : error:
15742 : 14 : i = list;
15743 : 14 : *prev_link = list->next;
15744 : 14 : gfc_free_finalizer (i);
15745 : 14 : result = false;
15746 : 455 : }
15747 : :
15748 : 2264 : if (result == false)
15749 : : return false;
15750 : :
15751 : : /* Warn if we haven't seen a scalar finalizer procedure (but we know there
15752 : : were nodes in the list, must have been for arrays. It is surely a good
15753 : : idea to have a scalar version there if there's something to finalize. */
15754 : 2260 : if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
15755 : 1 : gfc_warning (OPT_Wsurprising,
15756 : : "Only array FINAL procedures declared for derived type %qs"
15757 : : " defined at %L, suggest also scalar one unless an assumed"
15758 : : " rank finalizer has been declared",
15759 : : derived->name, &derived->declared_at);
15760 : :
15761 : 2260 : vtab = gfc_find_derived_vtab (derived);
15762 : 2260 : c = vtab->ts.u.derived->components->next->next->next->next->next;
15763 : 2260 : gfc_set_sym_referenced (c->initializer->symtree->n.sym);
15764 : :
15765 : 2260 : if (finalizable)
15766 : 590 : *finalizable = true;
15767 : :
15768 : : return true;
15769 : : }
15770 : :
15771 : :
15772 : : static gfc_symbol * containing_dt;
15773 : :
15774 : : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
15775 : : arguments whose declared types are PDT instances only transmit the PASS arg
15776 : : if they match the enclosing derived type. */
15777 : :
15778 : : static bool
15779 : 1404 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
15780 : : {
15781 : 1404 : gfc_formal_arglist *dummy_args;
15782 : 1404 : if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
15783 : : {
15784 : 504 : dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
15785 : 1134 : while (dummy_args && strcmp (pass, dummy_args->sym->name))
15786 : 126 : dummy_args = dummy_args->next;
15787 : 504 : gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
15788 : 504 : if (dummy_args->sym->ts.type == BT_CLASS
15789 : 504 : && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
15790 : : containing_dt->name))
15791 : : return true;
15792 : : }
15793 : : return false;
15794 : : }
15795 : :
15796 : :
15797 : : /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
15798 : :
15799 : : static bool
15800 : 704 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
15801 : : const char* generic_name, locus where)
15802 : : {
15803 : 704 : gfc_symbol *sym1, *sym2;
15804 : 704 : const char *pass1, *pass2;
15805 : 704 : gfc_formal_arglist *dummy_args;
15806 : :
15807 : 704 : gcc_assert (t1->specific && t2->specific);
15808 : 704 : gcc_assert (!t1->specific->is_generic);
15809 : 704 : gcc_assert (!t2->specific->is_generic);
15810 : 704 : gcc_assert (t1->is_operator == t2->is_operator);
15811 : :
15812 : 704 : sym1 = t1->specific->u.specific->n.sym;
15813 : 704 : sym2 = t2->specific->u.specific->n.sym;
15814 : :
15815 : 704 : if (sym1 == sym2)
15816 : : return true;
15817 : :
15818 : : /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
15819 : 704 : if (sym1->attr.subroutine != sym2->attr.subroutine
15820 : 702 : || sym1->attr.function != sym2->attr.function)
15821 : : {
15822 : 2 : gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
15823 : : " GENERIC %qs at %L",
15824 : : sym1->name, sym2->name, generic_name, &where);
15825 : 2 : return false;
15826 : : }
15827 : :
15828 : : /* Determine PASS arguments. */
15829 : 702 : if (t1->specific->nopass)
15830 : : pass1 = NULL;
15831 : 651 : else if (t1->specific->pass_arg)
15832 : : pass1 = t1->specific->pass_arg;
15833 : : else
15834 : : {
15835 : 392 : dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
15836 : 392 : if (dummy_args)
15837 : 391 : pass1 = dummy_args->sym->name;
15838 : : else
15839 : : pass1 = NULL;
15840 : : }
15841 : 702 : if (t2->specific->nopass)
15842 : : pass2 = NULL;
15843 : 650 : else if (t2->specific->pass_arg)
15844 : : pass2 = t2->specific->pass_arg;
15845 : : else
15846 : : {
15847 : 513 : dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
15848 : 513 : if (dummy_args)
15849 : 512 : pass2 = dummy_args->sym->name;
15850 : : else
15851 : : pass2 = NULL;
15852 : : }
15853 : :
15854 : : /* Care must be taken with pdt types and templates because the declared type
15855 : : of the argument that is not 'no_pass' need not be the same as the
15856 : : containing derived type. If this is the case, subject the argument to
15857 : : the full interface check, even though it cannot be used in the type
15858 : : bound context. */
15859 : 702 : pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
15860 : 702 : pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
15861 : :
15862 : 702 : if (containing_dt != NULL && containing_dt->attr.pdt_template)
15863 : 702 : pass1 = pass2 = NULL;
15864 : :
15865 : : /* Compare the interfaces. */
15866 : 702 : if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
15867 : : NULL, 0, pass1, pass2))
15868 : : {
15869 : 8 : gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
15870 : : sym1->name, sym2->name, generic_name, &where);
15871 : 8 : return false;
15872 : : }
15873 : :
15874 : : return true;
15875 : : }
15876 : :
15877 : :
15878 : : /* Worker function for resolving a generic procedure binding; this is used to
15879 : : resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
15880 : :
15881 : : The difference between those cases is finding possible inherited bindings
15882 : : that are overridden, as one has to look for them in tb_sym_root,
15883 : : tb_uop_root or tb_op, respectively. Thus the caller must already find
15884 : : the super-type and set p->overridden correctly. */
15885 : :
15886 : : static bool
15887 : 2171 : resolve_tb_generic_targets (gfc_symbol* super_type,
15888 : : gfc_typebound_proc* p, const char* name)
15889 : : {
15890 : 2171 : gfc_tbp_generic* target;
15891 : 2171 : gfc_symtree* first_target;
15892 : 2171 : gfc_symtree* inherited;
15893 : :
15894 : 2171 : gcc_assert (p && p->is_generic);
15895 : :
15896 : : /* Try to find the specific bindings for the symtrees in our target-list. */
15897 : 2171 : gcc_assert (p->u.generic);
15898 : 4874 : for (target = p->u.generic; target; target = target->next)
15899 : 2720 : if (!target->specific)
15900 : : {
15901 : 2407 : gfc_typebound_proc* overridden_tbp;
15902 : 2407 : gfc_tbp_generic* g;
15903 : 2407 : const char* target_name;
15904 : :
15905 : 2407 : target_name = target->specific_st->name;
15906 : :
15907 : : /* Defined for this type directly. */
15908 : 2407 : if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
15909 : : {
15910 : 2398 : target->specific = target->specific_st->n.tb;
15911 : 2398 : goto specific_found;
15912 : : }
15913 : :
15914 : : /* Look for an inherited specific binding. */
15915 : 9 : if (super_type)
15916 : : {
15917 : 5 : inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
15918 : : true, NULL);
15919 : :
15920 : 5 : if (inherited)
15921 : : {
15922 : 5 : gcc_assert (inherited->n.tb);
15923 : 5 : target->specific = inherited->n.tb;
15924 : 5 : goto specific_found;
15925 : : }
15926 : : }
15927 : :
15928 : 4 : gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
15929 : : " at %L", target_name, name, &p->where);
15930 : 4 : return false;
15931 : :
15932 : : /* Once we've found the specific binding, check it is not ambiguous with
15933 : : other specifics already found or inherited for the same GENERIC. */
15934 : 2403 : specific_found:
15935 : 2403 : gcc_assert (target->specific);
15936 : :
15937 : : /* This must really be a specific binding! */
15938 : 2403 : if (target->specific->is_generic)
15939 : : {
15940 : 3 : gfc_error ("GENERIC %qs at %L must target a specific binding,"
15941 : : " %qs is GENERIC, too", name, &p->where, target_name);
15942 : 3 : return false;
15943 : : }
15944 : :
15945 : : /* Check those already resolved on this type directly. */
15946 : 6158 : for (g = p->u.generic; g; g = g->next)
15947 : 1372 : if (g != target && g->specific
15948 : 4451 : && !check_generic_tbp_ambiguity (target, g, name, p->where))
15949 : : return false;
15950 : :
15951 : : /* Check for ambiguity with inherited specific targets. */
15952 : 2409 : for (overridden_tbp = p->overridden; overridden_tbp;
15953 : 16 : overridden_tbp = overridden_tbp->overridden)
15954 : 19 : if (overridden_tbp->is_generic)
15955 : : {
15956 : 33 : for (g = overridden_tbp->u.generic; g; g = g->next)
15957 : : {
15958 : 18 : gcc_assert (g->specific);
15959 : 18 : if (!check_generic_tbp_ambiguity (target, g, name, p->where))
15960 : : return false;
15961 : : }
15962 : : }
15963 : : }
15964 : :
15965 : : /* If we attempt to "overwrite" a specific binding, this is an error. */
15966 : 2154 : if (p->overridden && !p->overridden->is_generic)
15967 : : {
15968 : 1 : gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
15969 : : " the same name", name, &p->where);
15970 : 1 : return false;
15971 : : }
15972 : :
15973 : : /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
15974 : : all must have the same attributes here. */
15975 : 2153 : first_target = p->u.generic->specific->u.specific;
15976 : 2153 : gcc_assert (first_target);
15977 : 2153 : p->subroutine = first_target->n.sym->attr.subroutine;
15978 : 2153 : p->function = first_target->n.sym->attr.function;
15979 : :
15980 : 2153 : return true;
15981 : : }
15982 : :
15983 : :
15984 : : /* Resolve a GENERIC procedure binding for a derived type. */
15985 : :
15986 : : static bool
15987 : 1159 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
15988 : : {
15989 : 1159 : gfc_symbol* super_type;
15990 : :
15991 : : /* Find the overridden binding if any. */
15992 : 1159 : st->n.tb->overridden = NULL;
15993 : 1159 : super_type = gfc_get_derived_super_type (derived);
15994 : 1159 : if (super_type)
15995 : : {
15996 : 40 : gfc_symtree* overridden;
15997 : 40 : overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
15998 : : true, NULL);
15999 : :
16000 : 40 : if (overridden && overridden->n.tb)
16001 : 21 : st->n.tb->overridden = overridden->n.tb;
16002 : : }
16003 : :
16004 : : /* Resolve using worker function. */
16005 : 1159 : return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
16006 : : }
16007 : :
16008 : :
16009 : : /* Retrieve the target-procedure of an operator binding and do some checks in
16010 : : common for intrinsic and user-defined type-bound operators. */
16011 : :
16012 : : static gfc_symbol*
16013 : 1076 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
16014 : : {
16015 : 1076 : gfc_symbol* target_proc;
16016 : :
16017 : 1076 : gcc_assert (target->specific && !target->specific->is_generic);
16018 : 1076 : target_proc = target->specific->u.specific->n.sym;
16019 : 1076 : gcc_assert (target_proc);
16020 : :
16021 : : /* F08:C468. All operator bindings must have a passed-object dummy argument. */
16022 : 1076 : if (target->specific->nopass)
16023 : : {
16024 : 2 : gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
16025 : 2 : return NULL;
16026 : : }
16027 : :
16028 : : return target_proc;
16029 : : }
16030 : :
16031 : :
16032 : : /* Resolve a type-bound intrinsic operator. */
16033 : :
16034 : : static bool
16035 : 958 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
16036 : : gfc_typebound_proc* p)
16037 : : {
16038 : 958 : gfc_symbol* super_type;
16039 : 958 : gfc_tbp_generic* target;
16040 : :
16041 : : /* If there's already an error here, do nothing (but don't fail again). */
16042 : 958 : if (p->error)
16043 : : return true;
16044 : :
16045 : : /* Operators should always be GENERIC bindings. */
16046 : 958 : gcc_assert (p->is_generic);
16047 : :
16048 : : /* Look for an overridden binding. */
16049 : 958 : super_type = gfc_get_derived_super_type (derived);
16050 : 958 : if (super_type && super_type->f2k_derived)
16051 : 1 : p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
16052 : : op, true, NULL);
16053 : : else
16054 : 957 : p->overridden = NULL;
16055 : :
16056 : : /* Resolve general GENERIC properties using worker function. */
16057 : 958 : if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
16058 : 1 : goto error;
16059 : :
16060 : : /* Check the targets to be procedures of correct interface. */
16061 : 1958 : for (target = p->u.generic; target; target = target->next)
16062 : : {
16063 : 1021 : gfc_symbol* target_proc;
16064 : :
16065 : 1021 : target_proc = get_checked_tb_operator_target (target, p->where);
16066 : 1021 : if (!target_proc)
16067 : 1 : goto error;
16068 : :
16069 : 1020 : if (!gfc_check_operator_interface (target_proc, op, p->where))
16070 : 3 : goto error;
16071 : :
16072 : : /* Add target to non-typebound operator list. */
16073 : 1017 : if (!target->specific->deferred && !derived->attr.use_assoc
16074 : 358 : && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
16075 : : {
16076 : 356 : gfc_interface *head, *intr;
16077 : :
16078 : : /* Preempt 'gfc_check_new_interface' for submodules, where the
16079 : : mechanism for handling module procedures winds up resolving
16080 : : operator interfaces twice and would otherwise cause an error. */
16081 : 424 : for (intr = derived->ns->op[op]; intr; intr = intr->next)
16082 : 82 : if (intr->sym == target_proc
16083 : 16 : && target_proc->attr.used_in_submodule)
16084 : : return true;
16085 : :
16086 : 342 : if (!gfc_check_new_interface (derived->ns->op[op],
16087 : : target_proc, p->where))
16088 : : return false;
16089 : 340 : head = derived->ns->op[op];
16090 : 340 : intr = gfc_get_interface ();
16091 : 340 : intr->sym = target_proc;
16092 : 340 : intr->where = p->where;
16093 : 340 : intr->next = head;
16094 : 340 : derived->ns->op[op] = intr;
16095 : : }
16096 : : }
16097 : :
16098 : : return true;
16099 : :
16100 : 5 : error:
16101 : 5 : p->error = 1;
16102 : 5 : return false;
16103 : : }
16104 : :
16105 : :
16106 : : /* Resolve a type-bound user operator (tree-walker callback). */
16107 : :
16108 : : static gfc_symbol* resolve_bindings_derived;
16109 : : static bool resolve_bindings_result;
16110 : :
16111 : : static bool check_uop_procedure (gfc_symbol* sym, locus where);
16112 : :
16113 : : static void
16114 : 54 : resolve_typebound_user_op (gfc_symtree* stree)
16115 : : {
16116 : 54 : gfc_symbol* super_type;
16117 : 54 : gfc_tbp_generic* target;
16118 : :
16119 : 54 : gcc_assert (stree && stree->n.tb);
16120 : :
16121 : 54 : if (stree->n.tb->error)
16122 : : return;
16123 : :
16124 : : /* Operators should always be GENERIC bindings. */
16125 : 54 : gcc_assert (stree->n.tb->is_generic);
16126 : :
16127 : : /* Find overridden procedure, if any. */
16128 : 54 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16129 : 54 : if (super_type && super_type->f2k_derived)
16130 : : {
16131 : 0 : gfc_symtree* overridden;
16132 : 0 : overridden = gfc_find_typebound_user_op (super_type, NULL,
16133 : : stree->name, true, NULL);
16134 : :
16135 : 0 : if (overridden && overridden->n.tb)
16136 : 0 : stree->n.tb->overridden = overridden->n.tb;
16137 : : }
16138 : : else
16139 : 54 : stree->n.tb->overridden = NULL;
16140 : :
16141 : : /* Resolve basically using worker function. */
16142 : 54 : if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
16143 : 0 : goto error;
16144 : :
16145 : : /* Check the targets to be functions of correct interface. */
16146 : 106 : for (target = stree->n.tb->u.generic; target; target = target->next)
16147 : : {
16148 : 55 : gfc_symbol* target_proc;
16149 : :
16150 : 55 : target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
16151 : 55 : if (!target_proc)
16152 : 1 : goto error;
16153 : :
16154 : 54 : if (!check_uop_procedure (target_proc, stree->n.tb->where))
16155 : 2 : goto error;
16156 : : }
16157 : :
16158 : : return;
16159 : :
16160 : 3 : error:
16161 : 3 : resolve_bindings_result = false;
16162 : 3 : stree->n.tb->error = 1;
16163 : : }
16164 : :
16165 : :
16166 : : /* Resolve the type-bound procedures for a derived type. */
16167 : :
16168 : : static void
16169 : 9621 : resolve_typebound_procedure (gfc_symtree* stree)
16170 : : {
16171 : 9621 : gfc_symbol* proc;
16172 : 9621 : locus where;
16173 : 9621 : gfc_symbol* me_arg;
16174 : 9621 : gfc_symbol* super_type;
16175 : 9621 : gfc_component* comp;
16176 : :
16177 : 9621 : gcc_assert (stree);
16178 : :
16179 : : /* Undefined specific symbol from GENERIC target definition. */
16180 : 9621 : if (!stree->n.tb)
16181 : 9539 : return;
16182 : :
16183 : 9615 : if (stree->n.tb->error)
16184 : : return;
16185 : :
16186 : : /* If this is a GENERIC binding, use that routine. */
16187 : 9599 : if (stree->n.tb->is_generic)
16188 : : {
16189 : 1159 : if (!resolve_typebound_generic (resolve_bindings_derived, stree))
16190 : 17 : goto error;
16191 : : return;
16192 : : }
16193 : :
16194 : : /* Get the target-procedure to check it. */
16195 : 8440 : gcc_assert (!stree->n.tb->is_generic);
16196 : 8440 : gcc_assert (stree->n.tb->u.specific);
16197 : 8440 : proc = stree->n.tb->u.specific->n.sym;
16198 : 8440 : where = stree->n.tb->where;
16199 : :
16200 : : /* Default access should already be resolved from the parser. */
16201 : 8440 : gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
16202 : :
16203 : 8440 : if (stree->n.tb->deferred)
16204 : : {
16205 : 671 : if (!check_proc_interface (proc, &where))
16206 : 5 : goto error;
16207 : : }
16208 : : else
16209 : : {
16210 : : /* If proc has not been resolved at this point, proc->name may
16211 : : actually be a USE associated entity. See PR fortran/89647. */
16212 : 7769 : if (!proc->resolve_symbol_called
16213 : 5201 : && proc->attr.function == 0 && proc->attr.subroutine == 0)
16214 : : {
16215 : 11 : gfc_symbol *tmp;
16216 : 11 : gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
16217 : 11 : if (tmp && tmp->attr.use_assoc)
16218 : : {
16219 : 1 : proc->module = tmp->module;
16220 : 1 : proc->attr.proc = tmp->attr.proc;
16221 : 1 : proc->attr.function = tmp->attr.function;
16222 : 1 : proc->attr.subroutine = tmp->attr.subroutine;
16223 : 1 : proc->attr.use_assoc = tmp->attr.use_assoc;
16224 : 1 : proc->ts = tmp->ts;
16225 : 1 : proc->result = tmp->result;
16226 : : }
16227 : : }
16228 : :
16229 : : /* Check for F08:C465. */
16230 : 7769 : if ((!proc->attr.subroutine && !proc->attr.function)
16231 : 7759 : || (proc->attr.proc != PROC_MODULE
16232 : 57 : && proc->attr.if_source != IFSRC_IFBODY
16233 : 7 : && !proc->attr.module_procedure)
16234 : 7758 : || proc->attr.abstract)
16235 : : {
16236 : 12 : gfc_error ("%qs must be a module procedure or an external "
16237 : : "procedure with an explicit interface at %L",
16238 : : proc->name, &where);
16239 : 12 : goto error;
16240 : : }
16241 : : }
16242 : :
16243 : 8423 : stree->n.tb->subroutine = proc->attr.subroutine;
16244 : 8423 : stree->n.tb->function = proc->attr.function;
16245 : :
16246 : : /* Find the super-type of the current derived type. We could do this once and
16247 : : store in a global if speed is needed, but as long as not I believe this is
16248 : : more readable and clearer. */
16249 : 8423 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16250 : :
16251 : : /* If PASS, resolve and check arguments if not already resolved / loaded
16252 : : from a .mod file. */
16253 : 8423 : if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
16254 : : {
16255 : 2689 : gfc_formal_arglist *dummy_args;
16256 : :
16257 : 2689 : dummy_args = gfc_sym_get_dummy_args (proc);
16258 : 2689 : if (stree->n.tb->pass_arg)
16259 : : {
16260 : 459 : gfc_formal_arglist *i;
16261 : :
16262 : : /* If an explicit passing argument name is given, walk the arg-list
16263 : : and look for it. */
16264 : :
16265 : 459 : me_arg = NULL;
16266 : 459 : stree->n.tb->pass_arg_num = 1;
16267 : 585 : for (i = dummy_args; i; i = i->next)
16268 : : {
16269 : 583 : if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
16270 : : {
16271 : : me_arg = i->sym;
16272 : : break;
16273 : : }
16274 : 126 : ++stree->n.tb->pass_arg_num;
16275 : : }
16276 : :
16277 : 459 : if (!me_arg)
16278 : : {
16279 : 2 : gfc_error ("Procedure %qs with PASS(%s) at %L has no"
16280 : : " argument %qs",
16281 : : proc->name, stree->n.tb->pass_arg, &where,
16282 : : stree->n.tb->pass_arg);
16283 : 2 : goto error;
16284 : : }
16285 : : }
16286 : : else
16287 : : {
16288 : : /* Otherwise, take the first one; there should in fact be at least
16289 : : one. */
16290 : 2230 : stree->n.tb->pass_arg_num = 1;
16291 : 2230 : if (!dummy_args)
16292 : : {
16293 : 2 : gfc_error ("Procedure %qs with PASS at %L must have at"
16294 : : " least one argument", proc->name, &where);
16295 : 2 : goto error;
16296 : : }
16297 : 2228 : me_arg = dummy_args->sym;
16298 : : }
16299 : :
16300 : : /* Now check that the argument-type matches and the passed-object
16301 : : dummy argument is generally fine. */
16302 : :
16303 : 2228 : gcc_assert (me_arg);
16304 : :
16305 : 2685 : if (me_arg->ts.type != BT_CLASS)
16306 : : {
16307 : 5 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16308 : : " at %L", proc->name, &where);
16309 : 5 : goto error;
16310 : : }
16311 : :
16312 : : /* The derived type is not a PDT template or type. Resolve as usual. */
16313 : 2680 : if (!resolve_bindings_derived->attr.pdt_template
16314 : 2665 : && !(containing_dt && containing_dt->attr.pdt_type
16315 : 43 : && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
16316 : 2647 : && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
16317 : : {
16318 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16319 : : "the derived-type %qs", me_arg->name, proc->name,
16320 : : me_arg->name, &where, resolve_bindings_derived->name);
16321 : 0 : goto error;
16322 : : }
16323 : :
16324 : 2680 : if (resolve_bindings_derived->attr.pdt_template
16325 : 2695 : && !gfc_pdt_is_instance_of (resolve_bindings_derived,
16326 : 15 : CLASS_DATA (me_arg)->ts.u.derived))
16327 : : {
16328 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16329 : : "the parametric derived-type %qs", me_arg->name,
16330 : : proc->name, me_arg->name, &where,
16331 : : resolve_bindings_derived->name);
16332 : 0 : goto error;
16333 : : }
16334 : :
16335 : 2680 : if (resolve_bindings_derived->attr.pdt_template
16336 : 15 : && gfc_pdt_is_instance_of (resolve_bindings_derived,
16337 : 15 : CLASS_DATA (me_arg)->ts.u.derived)
16338 : 15 : && (me_arg->param_list != NULL)
16339 : 2695 : && (gfc_spec_list_type (me_arg->param_list,
16340 : 15 : CLASS_DATA(me_arg)->ts.u.derived)
16341 : : != SPEC_ASSUMED))
16342 : : {
16343 : :
16344 : : /* Add a check to verify if there are any LEN parameters in the
16345 : : first place. If there are LEN parameters, throw this error.
16346 : : If there are only KIND parameters, then don't trigger
16347 : : this error. */
16348 : 6 : gfc_component *c;
16349 : 6 : bool seen_len_param = false;
16350 : 6 : gfc_actual_arglist *me_arg_param = me_arg->param_list;
16351 : :
16352 : 6 : for (; me_arg_param; me_arg_param = me_arg_param->next)
16353 : : {
16354 : 6 : c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
16355 : : me_arg_param->name, true, true, NULL);
16356 : :
16357 : 6 : gcc_assert (c != NULL);
16358 : :
16359 : 6 : if (c->attr.pdt_kind)
16360 : 0 : continue;
16361 : :
16362 : : /* Getting here implies that there is a pdt_len parameter
16363 : : in the list. */
16364 : : seen_len_param = true;
16365 : : break;
16366 : : }
16367 : :
16368 : 6 : if (seen_len_param)
16369 : : {
16370 : 6 : gfc_error ("All LEN type parameters of the passed dummy "
16371 : : "argument %qs of %qs at %L must be ASSUMED.",
16372 : : me_arg->name, proc->name, &where);
16373 : 6 : goto error;
16374 : : }
16375 : : }
16376 : :
16377 : 2674 : gcc_assert (me_arg->ts.type == BT_CLASS);
16378 : 2674 : if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
16379 : : {
16380 : 1 : gfc_error ("Passed-object dummy argument of %qs at %L must be"
16381 : : " scalar", proc->name, &where);
16382 : 1 : goto error;
16383 : : }
16384 : 2673 : if (CLASS_DATA (me_arg)->attr.allocatable)
16385 : : {
16386 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16387 : : " be ALLOCATABLE", proc->name, &where);
16388 : 2 : goto error;
16389 : : }
16390 : 2671 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16391 : : {
16392 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16393 : : " be POINTER", proc->name, &where);
16394 : 2 : goto error;
16395 : : }
16396 : : }
16397 : :
16398 : : /* If we are extending some type, check that we don't override a procedure
16399 : : flagged NON_OVERRIDABLE. */
16400 : 8403 : stree->n.tb->overridden = NULL;
16401 : 8403 : if (super_type)
16402 : : {
16403 : 1480 : gfc_symtree* overridden;
16404 : 1480 : overridden = gfc_find_typebound_proc (super_type, NULL,
16405 : : stree->name, true, NULL);
16406 : :
16407 : 1480 : if (overridden)
16408 : : {
16409 : 1210 : if (overridden->n.tb)
16410 : 1210 : stree->n.tb->overridden = overridden->n.tb;
16411 : :
16412 : 1210 : if (!gfc_check_typebound_override (stree, overridden))
16413 : 26 : goto error;
16414 : : }
16415 : : }
16416 : :
16417 : : /* See if there's a name collision with a component directly in this type. */
16418 : 20224 : for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
16419 : 11848 : if (!strcmp (comp->name, stree->name))
16420 : : {
16421 : 1 : gfc_error ("Procedure %qs at %L has the same name as a component of"
16422 : : " %qs",
16423 : : stree->name, &where, resolve_bindings_derived->name);
16424 : 1 : goto error;
16425 : : }
16426 : :
16427 : : /* Try to find a name collision with an inherited component. */
16428 : 8376 : if (super_type && gfc_find_component (super_type, stree->name, true, true,
16429 : : NULL))
16430 : : {
16431 : 1 : gfc_error ("Procedure %qs at %L has the same name as an inherited"
16432 : : " component of %qs",
16433 : : stree->name, &where, resolve_bindings_derived->name);
16434 : 1 : goto error;
16435 : : }
16436 : :
16437 : 8375 : stree->n.tb->error = 0;
16438 : 8375 : return;
16439 : :
16440 : 82 : error:
16441 : 82 : resolve_bindings_result = false;
16442 : 82 : stree->n.tb->error = 1;
16443 : : }
16444 : :
16445 : :
16446 : : static bool
16447 : 82543 : resolve_typebound_procedures (gfc_symbol* derived)
16448 : : {
16449 : 82543 : int op;
16450 : 82543 : gfc_symbol* super_type;
16451 : :
16452 : 82543 : if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
16453 : : return true;
16454 : :
16455 : 4587 : super_type = gfc_get_derived_super_type (derived);
16456 : 4587 : if (super_type)
16457 : 847 : resolve_symbol (super_type);
16458 : :
16459 : 4587 : resolve_bindings_derived = derived;
16460 : 4587 : resolve_bindings_result = true;
16461 : :
16462 : 4587 : containing_dt = derived; /* Needed for checks of PDTs. */
16463 : 4587 : if (derived->f2k_derived->tb_sym_root)
16464 : 4587 : gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
16465 : : &resolve_typebound_procedure);
16466 : :
16467 : 4587 : if (derived->f2k_derived->tb_uop_root)
16468 : 50 : gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
16469 : : &resolve_typebound_user_op);
16470 : 4587 : containing_dt = NULL;
16471 : :
16472 : 133023 : for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
16473 : : {
16474 : 128436 : gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
16475 : 128436 : if (p && !resolve_typebound_intrinsic_op (derived,
16476 : : (gfc_intrinsic_op)op, p))
16477 : 7 : resolve_bindings_result = false;
16478 : : }
16479 : :
16480 : 4587 : return resolve_bindings_result;
16481 : : }
16482 : :
16483 : :
16484 : : /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
16485 : : to give all identical derived types the same backend_decl. */
16486 : : static void
16487 : 169553 : add_dt_to_dt_list (gfc_symbol *derived)
16488 : : {
16489 : 169553 : if (!derived->dt_next)
16490 : : {
16491 : 78525 : if (gfc_derived_types)
16492 : : {
16493 : 64451 : derived->dt_next = gfc_derived_types->dt_next;
16494 : 64451 : gfc_derived_types->dt_next = derived;
16495 : : }
16496 : : else
16497 : : {
16498 : 14074 : derived->dt_next = derived;
16499 : : }
16500 : 78525 : gfc_derived_types = derived;
16501 : : }
16502 : 169553 : }
16503 : :
16504 : :
16505 : : /* Ensure that a derived-type is really not abstract, meaning that every
16506 : : inherited DEFERRED binding is overridden by a non-DEFERRED one. */
16507 : :
16508 : : static bool
16509 : 6924 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
16510 : : {
16511 : 6924 : if (!st)
16512 : : return true;
16513 : :
16514 : 2712 : if (!ensure_not_abstract_walker (sub, st->left))
16515 : : return false;
16516 : 2712 : if (!ensure_not_abstract_walker (sub, st->right))
16517 : : return false;
16518 : :
16519 : 2711 : if (st->n.tb && st->n.tb->deferred)
16520 : : {
16521 : 1959 : gfc_symtree* overriding;
16522 : 1959 : overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
16523 : 1959 : if (!overriding)
16524 : : return false;
16525 : 1958 : gcc_assert (overriding->n.tb);
16526 : 1958 : if (overriding->n.tb->deferred)
16527 : : {
16528 : 5 : gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
16529 : : " %qs is DEFERRED and not overridden",
16530 : : sub->name, &sub->declared_at, st->name);
16531 : 5 : return false;
16532 : : }
16533 : : }
16534 : :
16535 : : return true;
16536 : : }
16537 : :
16538 : : static bool
16539 : 1352 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
16540 : : {
16541 : : /* The algorithm used here is to recursively travel up the ancestry of sub
16542 : : and for each ancestor-type, check all bindings. If any of them is
16543 : : DEFERRED, look it up starting from sub and see if the found (overriding)
16544 : : binding is not DEFERRED.
16545 : : This is not the most efficient way to do this, but it should be ok and is
16546 : : clearer than something sophisticated. */
16547 : :
16548 : 1501 : gcc_assert (ancestor && !sub->attr.abstract);
16549 : :
16550 : 1501 : if (!ancestor->attr.abstract)
16551 : : return true;
16552 : :
16553 : : /* Walk bindings of this ancestor. */
16554 : 1500 : if (ancestor->f2k_derived)
16555 : : {
16556 : 1500 : bool t;
16557 : 1500 : t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
16558 : 1500 : if (!t)
16559 : : return false;
16560 : : }
16561 : :
16562 : : /* Find next ancestor type and recurse on it. */
16563 : 1494 : ancestor = gfc_get_derived_super_type (ancestor);
16564 : 1494 : if (ancestor)
16565 : : return ensure_not_abstract (sub, ancestor);
16566 : :
16567 : : return true;
16568 : : }
16569 : :
16570 : :
16571 : : /* This check for typebound defined assignments is done recursively
16572 : : since the order in which derived types are resolved is not always in
16573 : : order of the declarations. */
16574 : :
16575 : : static void
16576 : 173580 : check_defined_assignments (gfc_symbol *derived)
16577 : : {
16578 : 173580 : gfc_component *c;
16579 : :
16580 : 580883 : for (c = derived->components; c; c = c->next)
16581 : : {
16582 : 408855 : if (!gfc_bt_struct (c->ts.type)
16583 : 98025 : || c->attr.pointer
16584 : 18588 : || c->attr.proc_pointer_comp
16585 : 18588 : || c->attr.class_pointer
16586 : 18582 : || c->attr.proc_pointer)
16587 : 390684 : continue;
16588 : :
16589 : 18171 : if (c->ts.u.derived->attr.defined_assign_comp
16590 : 17954 : || (c->ts.u.derived->f2k_derived
16591 : 17385 : && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
16592 : : {
16593 : 1528 : derived->attr.defined_assign_comp = 1;
16594 : 1528 : return;
16595 : : }
16596 : :
16597 : 16643 : if (c->attr.allocatable)
16598 : 5620 : continue;
16599 : :
16600 : 11023 : check_defined_assignments (c->ts.u.derived);
16601 : 11023 : if (c->ts.u.derived->attr.defined_assign_comp)
16602 : : {
16603 : 24 : derived->attr.defined_assign_comp = 1;
16604 : 24 : return;
16605 : : }
16606 : : }
16607 : : }
16608 : :
16609 : :
16610 : : /* Resolve a single component of a derived type or structure. */
16611 : :
16612 : : static bool
16613 : 390336 : resolve_component (gfc_component *c, gfc_symbol *sym)
16614 : : {
16615 : 390336 : gfc_symbol *super_type;
16616 : 390336 : symbol_attribute *attr;
16617 : :
16618 : 390336 : if (c->attr.artificial)
16619 : : return true;
16620 : :
16621 : : /* Do not allow vtype components to be resolved in nameless namespaces
16622 : : such as block data because the procedure pointers will cause ICEs
16623 : : and vtables are not needed in these contexts. */
16624 : 266408 : if (sym->attr.vtype && sym->attr.use_assoc
16625 : 46822 : && sym->ns->proc_name == NULL)
16626 : : return true;
16627 : :
16628 : : /* F2008, C442. */
16629 : 266399 : if ((!sym->attr.is_class || c != sym->components)
16630 : 266399 : && c->attr.codimension
16631 : 177 : && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
16632 : : {
16633 : 4 : gfc_error ("Coarray component %qs at %L must be allocatable with "
16634 : : "deferred shape", c->name, &c->loc);
16635 : 4 : return false;
16636 : : }
16637 : :
16638 : : /* F2008, C443. */
16639 : 266395 : if (c->attr.codimension && c->ts.type == BT_DERIVED
16640 : 77 : && c->ts.u.derived->ts.is_iso_c)
16641 : : {
16642 : 1 : gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16643 : : "shall not be a coarray", c->name, &c->loc);
16644 : 1 : return false;
16645 : : }
16646 : :
16647 : : /* F2008, C444. */
16648 : 266394 : if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
16649 : 22 : && (c->attr.codimension || c->attr.pointer || c->attr.dimension
16650 : 20 : || c->attr.allocatable))
16651 : : {
16652 : 3 : gfc_error ("Component %qs at %L with coarray component "
16653 : : "shall be a nonpointer, nonallocatable scalar",
16654 : : c->name, &c->loc);
16655 : 3 : return false;
16656 : : }
16657 : :
16658 : : /* F2008, C448. */
16659 : 266391 : if (c->ts.type == BT_CLASS)
16660 : : {
16661 : 6627 : if (c->attr.class_ok && CLASS_DATA (c))
16662 : : {
16663 : 6619 : attr = &(CLASS_DATA (c)->attr);
16664 : :
16665 : : /* Fix up contiguous attribute. */
16666 : 6619 : if (c->attr.contiguous)
16667 : 3 : attr->contiguous = 1;
16668 : : }
16669 : : else
16670 : : attr = NULL;
16671 : : }
16672 : : else
16673 : 259764 : attr = &c->attr;
16674 : :
16675 : 266386 : if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
16676 : : {
16677 : 5 : gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
16678 : : "is not an array pointer", c->name, &c->loc);
16679 : 5 : return false;
16680 : : }
16681 : :
16682 : : /* F2003, 15.2.1 - length has to be one. */
16683 : 39835 : if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
16684 : 266405 : && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
16685 : 19 : || !gfc_is_constant_expr (c->ts.u.cl->length)
16686 : 19 : || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
16687 : : {
16688 : 1 : gfc_error ("Component %qs of BIND(C) type at %L must have length one",
16689 : : c->name, &c->loc);
16690 : 1 : return false;
16691 : : }
16692 : :
16693 : 48771 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
16694 : 145 : && !sym->attr.pdt_type && !sym->attr.pdt_template
16695 : 266393 : && !(gfc_get_derived_super_type (sym)
16696 : 0 : && (gfc_get_derived_super_type (sym)->attr.pdt_type
16697 : 0 : || gfc_get_derived_super_type (sym)->attr.pdt_template)))
16698 : : {
16699 : 8 : gfc_actual_arglist *type_spec_list;
16700 : 8 : if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
16701 : : &type_spec_list)
16702 : : != MATCH_YES)
16703 : 0 : return false;
16704 : 8 : gfc_free_actual_arglist (c->param_list);
16705 : 8 : c->param_list = type_spec_list;
16706 : 8 : if (!sym->attr.pdt_type)
16707 : 8 : sym->attr.pdt_comp = 1;
16708 : : }
16709 : 266377 : else if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_type
16710 : 311 : && !sym->attr.pdt_type)
16711 : 43 : sym->attr.pdt_comp = 1;
16712 : :
16713 : 266385 : if (c->attr.proc_pointer && c->ts.interface)
16714 : : {
16715 : 13816 : gfc_symbol *ifc = c->ts.interface;
16716 : :
16717 : 13816 : if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
16718 : : {
16719 : 6 : c->tb->error = 1;
16720 : 6 : return false;
16721 : : }
16722 : :
16723 : 13810 : if (ifc->attr.if_source || ifc->attr.intrinsic)
16724 : : {
16725 : : /* Resolve interface and copy attributes. */
16726 : 13761 : if (ifc->formal && !ifc->formal_ns)
16727 : 2492 : resolve_symbol (ifc);
16728 : 13761 : if (ifc->attr.intrinsic)
16729 : 0 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
16730 : :
16731 : 13761 : if (ifc->result)
16732 : : {
16733 : 6983 : c->ts = ifc->result->ts;
16734 : 6983 : c->attr.allocatable = ifc->result->attr.allocatable;
16735 : 6983 : c->attr.pointer = ifc->result->attr.pointer;
16736 : 6983 : c->attr.dimension = ifc->result->attr.dimension;
16737 : 6983 : c->as = gfc_copy_array_spec (ifc->result->as);
16738 : 6983 : c->attr.class_ok = ifc->result->attr.class_ok;
16739 : : }
16740 : : else
16741 : : {
16742 : 6778 : c->ts = ifc->ts;
16743 : 6778 : c->attr.allocatable = ifc->attr.allocatable;
16744 : 6778 : c->attr.pointer = ifc->attr.pointer;
16745 : 6778 : c->attr.dimension = ifc->attr.dimension;
16746 : 6778 : c->as = gfc_copy_array_spec (ifc->as);
16747 : 6778 : c->attr.class_ok = ifc->attr.class_ok;
16748 : : }
16749 : 13761 : c->ts.interface = ifc;
16750 : 13761 : c->attr.function = ifc->attr.function;
16751 : 13761 : c->attr.subroutine = ifc->attr.subroutine;
16752 : :
16753 : 13761 : c->attr.pure = ifc->attr.pure;
16754 : 13761 : c->attr.elemental = ifc->attr.elemental;
16755 : 13761 : c->attr.recursive = ifc->attr.recursive;
16756 : 13761 : c->attr.always_explicit = ifc->attr.always_explicit;
16757 : 13761 : c->attr.ext_attr |= ifc->attr.ext_attr;
16758 : : /* Copy char length. */
16759 : 13761 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
16760 : : {
16761 : 221 : gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
16762 : 184 : if (cl->length && !cl->resolved
16763 : 241 : && !gfc_resolve_expr (cl->length))
16764 : : {
16765 : 0 : c->tb->error = 1;
16766 : 0 : return false;
16767 : : }
16768 : 221 : c->ts.u.cl = cl;
16769 : : }
16770 : : }
16771 : : }
16772 : 252569 : else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
16773 : : {
16774 : : /* Since PPCs are not implicitly typed, a PPC without an explicit
16775 : : interface must be a subroutine. */
16776 : 115 : gfc_add_subroutine (&c->attr, c->name, &c->loc);
16777 : : }
16778 : :
16779 : : /* Procedure pointer components: Check PASS arg. */
16780 : 266379 : if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
16781 : 717 : && !sym->attr.vtype)
16782 : : {
16783 : 94 : gfc_symbol* me_arg;
16784 : :
16785 : 94 : if (c->tb->pass_arg)
16786 : : {
16787 : 19 : gfc_formal_arglist* i;
16788 : :
16789 : : /* If an explicit passing argument name is given, walk the arg-list
16790 : : and look for it. */
16791 : :
16792 : 19 : me_arg = NULL;
16793 : 19 : c->tb->pass_arg_num = 1;
16794 : 33 : for (i = c->ts.interface->formal; i; i = i->next)
16795 : : {
16796 : 32 : if (!strcmp (i->sym->name, c->tb->pass_arg))
16797 : : {
16798 : : me_arg = i->sym;
16799 : : break;
16800 : : }
16801 : 14 : c->tb->pass_arg_num++;
16802 : : }
16803 : :
16804 : 19 : if (!me_arg)
16805 : : {
16806 : 1 : gfc_error ("Procedure pointer component %qs with PASS(%s) "
16807 : : "at %L has no argument %qs", c->name,
16808 : : c->tb->pass_arg, &c->loc, c->tb->pass_arg);
16809 : 1 : c->tb->error = 1;
16810 : 1 : return false;
16811 : : }
16812 : : }
16813 : : else
16814 : : {
16815 : : /* Otherwise, take the first one; there should in fact be at least
16816 : : one. */
16817 : 75 : c->tb->pass_arg_num = 1;
16818 : 75 : if (!c->ts.interface->formal)
16819 : : {
16820 : 3 : gfc_error ("Procedure pointer component %qs with PASS at %L "
16821 : : "must have at least one argument",
16822 : : c->name, &c->loc);
16823 : 3 : c->tb->error = 1;
16824 : 3 : return false;
16825 : : }
16826 : 72 : me_arg = c->ts.interface->formal->sym;
16827 : : }
16828 : :
16829 : : /* Now check that the argument-type matches. */
16830 : 72 : gcc_assert (me_arg);
16831 : 90 : if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
16832 : 89 : || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
16833 : 89 : || (me_arg->ts.type == BT_CLASS
16834 : 81 : && CLASS_DATA (me_arg)->ts.u.derived != sym))
16835 : : {
16836 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
16837 : : " the derived type %qs", me_arg->name, c->name,
16838 : : me_arg->name, &c->loc, sym->name);
16839 : 1 : c->tb->error = 1;
16840 : 1 : return false;
16841 : : }
16842 : :
16843 : : /* Check for F03:C453. */
16844 : 89 : if (CLASS_DATA (me_arg)->attr.dimension)
16845 : : {
16846 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16847 : : "must be scalar", me_arg->name, c->name, me_arg->name,
16848 : : &c->loc);
16849 : 1 : c->tb->error = 1;
16850 : 1 : return false;
16851 : : }
16852 : :
16853 : 88 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16854 : : {
16855 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16856 : : "may not have the POINTER attribute", me_arg->name,
16857 : : c->name, me_arg->name, &c->loc);
16858 : 1 : c->tb->error = 1;
16859 : 1 : return false;
16860 : : }
16861 : :
16862 : 87 : if (CLASS_DATA (me_arg)->attr.allocatable)
16863 : : {
16864 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16865 : : "may not be ALLOCATABLE", me_arg->name, c->name,
16866 : : me_arg->name, &c->loc);
16867 : 1 : c->tb->error = 1;
16868 : 1 : return false;
16869 : : }
16870 : :
16871 : 86 : if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
16872 : : {
16873 : 2 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16874 : : " at %L", c->name, &c->loc);
16875 : 2 : return false;
16876 : : }
16877 : :
16878 : : }
16879 : :
16880 : : /* Check type-spec if this is not the parent-type component. */
16881 : 266369 : if (((sym->attr.is_class
16882 : 11837 : && (!sym->components->ts.u.derived->attr.extension
16883 : 2318 : || c != CLASS_DATA (sym->components)))
16884 : 255828 : || (!sym->attr.is_class
16885 : 254532 : && (!sym->attr.extension || c != sym->components)))
16886 : 258540 : && !sym->attr.vtype
16887 : 421249 : && !resolve_typespec_used (&c->ts, &c->loc, c->name))
16888 : : return false;
16889 : :
16890 : 266368 : super_type = gfc_get_derived_super_type (sym);
16891 : :
16892 : : /* If this type is an extension, set the accessibility of the parent
16893 : : component. */
16894 : 266368 : if (super_type
16895 : 24517 : && ((sym->attr.is_class
16896 : 11837 : && c == CLASS_DATA (sym->components))
16897 : 16328 : || (!sym->attr.is_class && c == sym->components))
16898 : 14722 : && strcmp (super_type->name, c->name) == 0)
16899 : 6407 : c->attr.access = super_type->attr.access;
16900 : :
16901 : : /* If this type is an extension, see if this component has the same name
16902 : : as an inherited type-bound procedure. */
16903 : 24517 : if (super_type && !sym->attr.is_class
16904 : 12680 : && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
16905 : : {
16906 : 1 : gfc_error ("Component %qs of %qs at %L has the same name as an"
16907 : : " inherited type-bound procedure",
16908 : : c->name, sym->name, &c->loc);
16909 : 1 : return false;
16910 : : }
16911 : :
16912 : 266367 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
16913 : 9228 : && !c->ts.deferred)
16914 : : {
16915 : 7077 : if (sym->attr.pdt_template || c->attr.pdt_string)
16916 : 226 : gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
16917 : :
16918 : 7077 : if (c->ts.u.cl->length == NULL
16919 : 7071 : || !resolve_charlen(c->ts.u.cl)
16920 : 14147 : || !gfc_is_constant_expr (c->ts.u.cl->length))
16921 : : {
16922 : 9 : gfc_error ("Character length of component %qs needs to "
16923 : : "be a constant specification expression at %L",
16924 : : c->name,
16925 : 9 : c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
16926 : 9 : return false;
16927 : : }
16928 : :
16929 : 7068 : if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
16930 : : {
16931 : 2 : if (!c->ts.u.cl->length->error)
16932 : : {
16933 : 1 : gfc_error ("Character length expression of component %qs at %L "
16934 : : "must be of INTEGER type, found %s",
16935 : 1 : c->name, &c->ts.u.cl->length->where,
16936 : : gfc_basic_typename (c->ts.u.cl->length->ts.type));
16937 : 1 : c->ts.u.cl->length->error = 1;
16938 : : }
16939 : 2 : return false;
16940 : : }
16941 : : }
16942 : :
16943 : 266356 : if (c->ts.type == BT_CHARACTER && c->ts.deferred
16944 : 2187 : && !c->attr.pointer && !c->attr.allocatable)
16945 : : {
16946 : 1 : gfc_error ("Character component %qs of %qs at %L with deferred "
16947 : : "length must be a POINTER or ALLOCATABLE",
16948 : : c->name, sym->name, &c->loc);
16949 : 1 : return false;
16950 : : }
16951 : :
16952 : : /* Add the hidden deferred length field. */
16953 : 266355 : if (c->ts.type == BT_CHARACTER
16954 : 9458 : && (c->ts.deferred || c->attr.pdt_string)
16955 : 2341 : && !c->attr.function
16956 : 2305 : && !sym->attr.is_class)
16957 : : {
16958 : 2158 : char name[GFC_MAX_SYMBOL_LEN+9];
16959 : 2158 : gfc_component *strlen;
16960 : 2158 : sprintf (name, "_%s_length", c->name);
16961 : 2158 : strlen = gfc_find_component (sym, name, true, true, NULL);
16962 : 2158 : if (strlen == NULL)
16963 : : {
16964 : 454 : if (!gfc_add_component (sym, name, &strlen))
16965 : 0 : return false;
16966 : 454 : strlen->ts.type = BT_INTEGER;
16967 : 454 : strlen->ts.kind = gfc_charlen_int_kind;
16968 : 454 : strlen->attr.access = ACCESS_PRIVATE;
16969 : 454 : strlen->attr.artificial = 1;
16970 : : }
16971 : : }
16972 : :
16973 : 266355 : if (c->ts.type == BT_DERIVED
16974 : 48941 : && sym->component_access != ACCESS_PRIVATE
16975 : 47969 : && gfc_check_symbol_access (sym)
16976 : 93934 : && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
16977 : 46915 : && !c->ts.u.derived->attr.use_assoc
16978 : 25066 : && !gfc_check_symbol_access (c->ts.u.derived)
16979 : 266549 : && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
16980 : : "PRIVATE type and cannot be a component of "
16981 : : "%qs, which is PUBLIC at %L", c->name,
16982 : : sym->name, &sym->declared_at))
16983 : : return false;
16984 : :
16985 : 266354 : if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
16986 : : {
16987 : 2 : gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
16988 : : "type %s", c->name, &c->loc, sym->name);
16989 : 2 : return false;
16990 : : }
16991 : :
16992 : 266352 : if (sym->attr.sequence)
16993 : : {
16994 : 2506 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
16995 : : {
16996 : 0 : gfc_error ("Component %s of SEQUENCE type declared at %L does "
16997 : : "not have the SEQUENCE attribute",
16998 : : c->ts.u.derived->name, &sym->declared_at);
16999 : 0 : return false;
17000 : : }
17001 : : }
17002 : :
17003 : 266352 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
17004 : 0 : c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
17005 : 266352 : else if (c->ts.type == BT_CLASS && c->attr.class_ok
17006 : 6959 : && CLASS_DATA (c)->ts.u.derived->attr.generic)
17007 : 0 : CLASS_DATA (c)->ts.u.derived
17008 : 0 : = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
17009 : :
17010 : : /* If an allocatable component derived type is of the same type as
17011 : : the enclosing derived type, we need a vtable generating so that
17012 : : the __deallocate procedure is created. */
17013 : 266352 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
17014 : 55910 : && c->ts.u.derived == sym && c->attr.allocatable == 1)
17015 : 190 : gfc_find_vtab (&c->ts);
17016 : :
17017 : : /* Ensure that all the derived type components are put on the
17018 : : derived type list; even in formal namespaces, where derived type
17019 : : pointer components might not have been declared. */
17020 : 266352 : if (c->ts.type == BT_DERIVED
17021 : 48940 : && c->ts.u.derived
17022 : 48940 : && c->ts.u.derived->components
17023 : 45802 : && c->attr.pointer
17024 : 32023 : && sym != c->ts.u.derived)
17025 : 4157 : add_dt_to_dt_list (c->ts.u.derived);
17026 : :
17027 : 266352 : if (c->as && c->as->type != AS_DEFERRED
17028 : 5810 : && (c->attr.pointer || c->attr.allocatable))
17029 : : return false;
17030 : :
17031 : 266338 : if (!gfc_resolve_array_spec (c->as,
17032 : 266338 : !(c->attr.pointer || c->attr.proc_pointer
17033 : 216926 : || c->attr.allocatable)))
17034 : : return false;
17035 : :
17036 : 99108 : if (c->initializer && !sym->attr.vtype
17037 : 29272 : && !c->attr.pdt_kind && !c->attr.pdt_len
17038 : 293403 : && !gfc_check_assign_symbol (sym, c, c->initializer))
17039 : : return false;
17040 : :
17041 : : return true;
17042 : : }
17043 : :
17044 : :
17045 : : /* Be nice about the locus for a structure expression - show the locus of the
17046 : : first non-null sub-expression if we can. */
17047 : :
17048 : : static locus *
17049 : 4 : cons_where (gfc_expr *struct_expr)
17050 : : {
17051 : 4 : gfc_constructor *cons;
17052 : :
17053 : 4 : gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
17054 : :
17055 : 4 : cons = gfc_constructor_first (struct_expr->value.constructor);
17056 : 12 : for (; cons; cons = gfc_constructor_next (cons))
17057 : : {
17058 : 8 : if (cons->expr && cons->expr->expr_type != EXPR_NULL)
17059 : 4 : return &cons->expr->where;
17060 : : }
17061 : :
17062 : 0 : return &struct_expr->where;
17063 : : }
17064 : :
17065 : : /* Resolve the components of a structure type. Much less work than derived
17066 : : types. */
17067 : :
17068 : : static bool
17069 : 913 : resolve_fl_struct (gfc_symbol *sym)
17070 : : {
17071 : 913 : gfc_component *c;
17072 : 913 : gfc_expr *init = NULL;
17073 : 913 : bool success;
17074 : :
17075 : : /* Make sure UNIONs do not have overlapping initializers. */
17076 : 913 : if (sym->attr.flavor == FL_UNION)
17077 : : {
17078 : 498 : for (c = sym->components; c; c = c->next)
17079 : : {
17080 : 331 : if (init && c->initializer)
17081 : : {
17082 : 2 : gfc_error ("Conflicting initializers in union at %L and %L",
17083 : : cons_where (init), cons_where (c->initializer));
17084 : 2 : gfc_free_expr (c->initializer);
17085 : 2 : c->initializer = NULL;
17086 : : }
17087 : 291 : if (init == NULL)
17088 : 291 : init = c->initializer;
17089 : : }
17090 : : }
17091 : :
17092 : 913 : success = true;
17093 : 2830 : for (c = sym->components; c; c = c->next)
17094 : 1917 : if (!resolve_component (c, sym))
17095 : 0 : success = false;
17096 : :
17097 : 913 : if (!success)
17098 : : return false;
17099 : :
17100 : 913 : if (sym->components)
17101 : 862 : add_dt_to_dt_list (sym);
17102 : :
17103 : : return true;
17104 : : }
17105 : :
17106 : : /* Figure if the derived type is using itself directly in one of its components
17107 : : or through referencing other derived types. The information is required to
17108 : : generate the __deallocate and __final type bound procedures to ensure
17109 : : freeing larger hierarchies of derived types with allocatable objects. */
17110 : :
17111 : : static void
17112 : 132002 : resolve_cyclic_derived_type (gfc_symbol *derived)
17113 : : {
17114 : 132002 : hash_set<gfc_symbol *> seen, to_examin;
17115 : 132002 : gfc_component *c;
17116 : 132002 : seen.add (derived);
17117 : 132002 : to_examin.add (derived);
17118 : 441858 : while (!to_examin.is_empty ())
17119 : : {
17120 : 179786 : gfc_symbol *cand = *to_examin.begin ();
17121 : 179786 : to_examin.remove (cand);
17122 : 482733 : for (c = cand->components; c; c = c->next)
17123 : 304879 : if (c->ts.type == BT_DERIVED)
17124 : : {
17125 : 66603 : if (c->ts.u.derived == derived)
17126 : : {
17127 : 962 : derived->attr.recursive = 1;
17128 : 1932 : return;
17129 : : }
17130 : 65641 : else if (!seen.contains (c->ts.u.derived))
17131 : : {
17132 : 43341 : seen.add (c->ts.u.derived);
17133 : 43341 : to_examin.add (c->ts.u.derived);
17134 : : }
17135 : : }
17136 : 238276 : else if (c->ts.type == BT_CLASS)
17137 : : {
17138 : 8837 : if (!c->attr.class_ok)
17139 : 7 : continue;
17140 : 8830 : if (CLASS_DATA (c)->ts.u.derived == derived)
17141 : : {
17142 : 970 : derived->attr.recursive = 1;
17143 : 970 : return;
17144 : : }
17145 : 7860 : else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
17146 : : {
17147 : 4644 : seen.add (CLASS_DATA (c)->ts.u.derived);
17148 : 4644 : to_examin.add (CLASS_DATA (c)->ts.u.derived);
17149 : : }
17150 : : }
17151 : : }
17152 : 132002 : }
17153 : :
17154 : : /* Resolve the components of a derived type. This does not have to wait until
17155 : : resolution stage, but can be done as soon as the dt declaration has been
17156 : : parsed. */
17157 : :
17158 : : static bool
17159 : 162642 : resolve_fl_derived0 (gfc_symbol *sym)
17160 : : {
17161 : 162642 : gfc_symbol* super_type;
17162 : 162642 : gfc_component *c;
17163 : 162642 : gfc_formal_arglist *f;
17164 : 162642 : bool success;
17165 : :
17166 : 162642 : if (sym->attr.unlimited_polymorphic)
17167 : : return true;
17168 : :
17169 : 162642 : super_type = gfc_get_derived_super_type (sym);
17170 : :
17171 : : /* F2008, C432. */
17172 : 162642 : if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
17173 : : {
17174 : 2 : gfc_error ("As extending type %qs at %L has a coarray component, "
17175 : : "parent type %qs shall also have one", sym->name,
17176 : : &sym->declared_at, super_type->name);
17177 : 2 : return false;
17178 : : }
17179 : :
17180 : : /* Ensure the extended type gets resolved before we do. */
17181 : 16631 : if (super_type && !resolve_fl_derived0 (super_type))
17182 : : return false;
17183 : :
17184 : : /* An ABSTRACT type must be extensible. */
17185 : 162634 : if (sym->attr.abstract && !gfc_type_is_extensible (sym))
17186 : : {
17187 : 2 : gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
17188 : : sym->name, &sym->declared_at);
17189 : 2 : return false;
17190 : : }
17191 : :
17192 : : /* Resolving components below, may create vtabs for which the cyclic type
17193 : : information needs to be present. */
17194 : 162632 : if (!sym->attr.vtype)
17195 : 132002 : resolve_cyclic_derived_type (sym);
17196 : :
17197 : 162632 : c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
17198 : : : sym->components;
17199 : :
17200 : : success = true;
17201 : 551051 : for ( ; c != NULL; c = c->next)
17202 : 388419 : if (!resolve_component (c, sym))
17203 : 85 : success = false;
17204 : :
17205 : 162632 : if (!success)
17206 : : return false;
17207 : :
17208 : : /* Now add the caf token field, where needed. */
17209 : 162557 : if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
17210 : 770 : && !sym->attr.vtype)
17211 : : {
17212 : 1883 : for (c = sym->components; c; c = c->next)
17213 : 1266 : if (!c->attr.dimension && !c->attr.codimension
17214 : 703 : && (c->attr.allocatable || c->attr.pointer))
17215 : : {
17216 : 134 : char name[GFC_MAX_SYMBOL_LEN+9];
17217 : 134 : gfc_component *token;
17218 : 134 : sprintf (name, "_caf_%s", c->name);
17219 : 134 : token = gfc_find_component (sym, name, true, true, NULL);
17220 : 134 : if (token == NULL)
17221 : : {
17222 : 75 : if (!gfc_add_component (sym, name, &token))
17223 : 0 : return false;
17224 : 75 : token->ts.type = BT_VOID;
17225 : 75 : token->ts.kind = gfc_default_integer_kind;
17226 : 75 : token->attr.access = ACCESS_PRIVATE;
17227 : 75 : token->attr.artificial = 1;
17228 : 75 : token->attr.caf_token = 1;
17229 : : }
17230 : 134 : c->caf_token = token;
17231 : : }
17232 : : }
17233 : :
17234 : 162557 : check_defined_assignments (sym);
17235 : :
17236 : 162557 : if (!sym->attr.defined_assign_comp && super_type)
17237 : 15801 : sym->attr.defined_assign_comp
17238 : 15801 : = super_type->attr.defined_assign_comp;
17239 : :
17240 : : /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
17241 : : all DEFERRED bindings are overridden. */
17242 : 16624 : if (super_type && super_type->attr.abstract && !sym->attr.abstract
17243 : 1355 : && !sym->attr.is_class
17244 : 2880 : && !ensure_not_abstract (sym, super_type))
17245 : : return false;
17246 : :
17247 : : /* Check that there is a component for every PDT parameter. */
17248 : 162551 : if (sym->attr.pdt_template)
17249 : : {
17250 : 1432 : for (f = sym->formal; f; f = f->next)
17251 : : {
17252 : 897 : if (!f->sym)
17253 : 1 : continue;
17254 : 896 : c = gfc_find_component (sym, f->sym->name, true, true, NULL);
17255 : 896 : if (c == NULL)
17256 : : {
17257 : 9 : gfc_error ("Parameterized type %qs does not have a component "
17258 : : "corresponding to parameter %qs at %L", sym->name,
17259 : 9 : f->sym->name, &sym->declared_at);
17260 : 9 : break;
17261 : : }
17262 : : }
17263 : : }
17264 : :
17265 : : /* Add derived type to the derived type list. */
17266 : 162551 : add_dt_to_dt_list (sym);
17267 : :
17268 : 162551 : return true;
17269 : : }
17270 : :
17271 : : /* The following procedure does the full resolution of a derived type,
17272 : : including resolution of all type-bound procedures (if present). In contrast
17273 : : to 'resolve_fl_derived0' this can only be done after the module has been
17274 : : parsed completely. */
17275 : :
17276 : : static bool
17277 : 84625 : resolve_fl_derived (gfc_symbol *sym)
17278 : : {
17279 : 84625 : gfc_symbol *gen_dt = NULL;
17280 : :
17281 : 84625 : if (sym->attr.unlimited_polymorphic)
17282 : : return true;
17283 : :
17284 : 84625 : if (!sym->attr.is_class)
17285 : 72545 : gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
17286 : 54204 : if (gen_dt && gen_dt->generic && gen_dt->generic->next
17287 : 2171 : && (!gen_dt->generic->sym->attr.use_assoc
17288 : 2071 : || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
17289 : 84758 : && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
17290 : : "%qs at %L being the same name as derived "
17291 : : "type at %L", sym->name,
17292 : : gen_dt->generic->sym == sym
17293 : 11 : ? gen_dt->generic->next->sym->name
17294 : : : gen_dt->generic->sym->name,
17295 : : gen_dt->generic->sym == sym
17296 : 11 : ? &gen_dt->generic->next->sym->declared_at
17297 : : : &gen_dt->generic->sym->declared_at,
17298 : : &sym->declared_at))
17299 : : return false;
17300 : :
17301 : 84621 : if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
17302 : : {
17303 : 13 : gfc_error ("Derived type %qs at %L has not been declared",
17304 : : sym->name, &sym->declared_at);
17305 : 13 : return false;
17306 : : }
17307 : :
17308 : : /* Resolve the finalizer procedures. */
17309 : 84608 : if (!gfc_resolve_finalizers (sym, NULL))
17310 : : return false;
17311 : :
17312 : 84605 : if (sym->attr.is_class && sym->ts.u.derived == NULL)
17313 : : {
17314 : : /* Fix up incomplete CLASS symbols. */
17315 : 12080 : gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
17316 : 12080 : gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
17317 : :
17318 : : /* Nothing more to do for unlimited polymorphic entities. */
17319 : 12080 : if (data->ts.u.derived->attr.unlimited_polymorphic)
17320 : : {
17321 : 1983 : add_dt_to_dt_list (sym);
17322 : 1983 : return true;
17323 : : }
17324 : 10097 : else if (vptr->ts.u.derived == NULL)
17325 : : {
17326 : 6061 : gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
17327 : 6061 : gcc_assert (vtab);
17328 : 6061 : vptr->ts.u.derived = vtab->ts.u.derived;
17329 : 6061 : if (!resolve_fl_derived0 (vptr->ts.u.derived))
17330 : : return false;
17331 : : }
17332 : : }
17333 : :
17334 : 82622 : if (!resolve_fl_derived0 (sym))
17335 : : return false;
17336 : :
17337 : : /* Resolve the type-bound procedures. */
17338 : 82543 : if (!resolve_typebound_procedures (sym))
17339 : : return false;
17340 : :
17341 : : /* Generate module vtables subject to their accessibility and their not
17342 : : being vtables or pdt templates. If this is not done class declarations
17343 : : in external procedures wind up with their own version and so SELECT TYPE
17344 : : fails because the vptrs do not have the same address. */
17345 : 82502 : if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
17346 : 82441 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
17347 : 61941 : || (sym->attr.recursive && sym->attr.alloc_comp))
17348 : 20604 : && sym->attr.access != ACCESS_PRIVATE
17349 : 20571 : && !(sym->attr.vtype || sym->attr.pdt_template))
17350 : : {
17351 : 18583 : gfc_symbol *vtab = gfc_find_derived_vtab (sym);
17352 : 18583 : gfc_set_sym_referenced (vtab);
17353 : : }
17354 : :
17355 : : return true;
17356 : : }
17357 : :
17358 : :
17359 : : static bool
17360 : 805 : resolve_fl_namelist (gfc_symbol *sym)
17361 : : {
17362 : 805 : gfc_namelist *nl;
17363 : 805 : gfc_symbol *nlsym;
17364 : :
17365 : 2894 : for (nl = sym->namelist; nl; nl = nl->next)
17366 : : {
17367 : : /* Check again, the check in match only works if NAMELIST comes
17368 : : after the decl. */
17369 : 2094 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
17370 : : {
17371 : 1 : gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
17372 : : "allowed", nl->sym->name, sym->name, &sym->declared_at);
17373 : 1 : return false;
17374 : : }
17375 : :
17376 : 652 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
17377 : 2101 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17378 : : "with assumed shape in namelist %qs at %L",
17379 : : nl->sym->name, sym->name, &sym->declared_at))
17380 : : return false;
17381 : :
17382 : 2092 : if (is_non_constant_shape_array (nl->sym)
17383 : 2142 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17384 : : "with nonconstant shape in namelist %qs at %L",
17385 : 50 : nl->sym->name, sym->name, &sym->declared_at))
17386 : : return false;
17387 : :
17388 : 2091 : if (nl->sym->ts.type == BT_CHARACTER
17389 : 565 : && (nl->sym->ts.u.cl->length == NULL
17390 : 526 : || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
17391 : 2173 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
17392 : : "nonconstant character length in "
17393 : 82 : "namelist %qs at %L", nl->sym->name,
17394 : : sym->name, &sym->declared_at))
17395 : : return false;
17396 : :
17397 : : }
17398 : :
17399 : : /* Reject PRIVATE objects in a PUBLIC namelist. */
17400 : 800 : if (gfc_check_symbol_access (sym))
17401 : : {
17402 : 2875 : for (nl = sym->namelist; nl; nl = nl->next)
17403 : : {
17404 : 2088 : if (!nl->sym->attr.use_assoc
17405 : 3952 : && !is_sym_host_assoc (nl->sym, sym->ns)
17406 : 4042 : && !gfc_check_symbol_access (nl->sym))
17407 : : {
17408 : 2 : gfc_error ("NAMELIST object %qs was declared PRIVATE and "
17409 : : "cannot be member of PUBLIC namelist %qs at %L",
17410 : 2 : nl->sym->name, sym->name, &sym->declared_at);
17411 : 2 : return false;
17412 : : }
17413 : :
17414 : 2086 : if (nl->sym->ts.type == BT_DERIVED
17415 : 466 : && (nl->sym->ts.u.derived->attr.alloc_comp
17416 : 464 : || nl->sym->ts.u.derived->attr.pointer_comp))
17417 : : {
17418 : 5 : if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
17419 : : "namelist %qs at %L with ALLOCATABLE "
17420 : : "or POINTER components", nl->sym->name,
17421 : : sym->name, &sym->declared_at))
17422 : : return false;
17423 : : return true;
17424 : : }
17425 : :
17426 : : /* Types with private components that came here by USE-association. */
17427 : 2081 : if (nl->sym->ts.type == BT_DERIVED
17428 : 2081 : && derived_inaccessible (nl->sym->ts.u.derived))
17429 : : {
17430 : 6 : gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
17431 : : "components and cannot be member of namelist %qs at %L",
17432 : : nl->sym->name, sym->name, &sym->declared_at);
17433 : 6 : return false;
17434 : : }
17435 : :
17436 : : /* Types with private components that are defined in the same module. */
17437 : 2075 : if (nl->sym->ts.type == BT_DERIVED
17438 : 910 : && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
17439 : 2353 : && nl->sym->ts.u.derived->attr.private_comp)
17440 : : {
17441 : 0 : gfc_error ("NAMELIST object %qs has PRIVATE components and "
17442 : : "cannot be a member of PUBLIC namelist %qs at %L",
17443 : : nl->sym->name, sym->name, &sym->declared_at);
17444 : 0 : return false;
17445 : : }
17446 : : }
17447 : : }
17448 : :
17449 : :
17450 : : /* 14.1.2 A module or internal procedure represent local entities
17451 : : of the same type as a namelist member and so are not allowed. */
17452 : 2859 : for (nl = sym->namelist; nl; nl = nl->next)
17453 : : {
17454 : 2075 : if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
17455 : 1516 : continue;
17456 : :
17457 : 559 : if (nl->sym->attr.function && nl->sym == nl->sym->result)
17458 : 7 : if ((nl->sym == sym->ns->proc_name)
17459 : 1 : ||
17460 : 1 : (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
17461 : 6 : continue;
17462 : :
17463 : 553 : nlsym = NULL;
17464 : 553 : if (nl->sym->name)
17465 : 553 : gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
17466 : 553 : if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
17467 : : {
17468 : 3 : gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
17469 : : "attribute in %qs at %L", nlsym->name,
17470 : : &sym->declared_at);
17471 : 3 : return false;
17472 : : }
17473 : : }
17474 : :
17475 : : return true;
17476 : : }
17477 : :
17478 : :
17479 : : static bool
17480 : 371265 : resolve_fl_parameter (gfc_symbol *sym)
17481 : : {
17482 : : /* A parameter array's shape needs to be constant. */
17483 : 371265 : if (sym->as != NULL
17484 : 371265 : && (sym->as->type == AS_DEFERRED
17485 : 6126 : || is_non_constant_shape_array (sym)))
17486 : : {
17487 : 17 : gfc_error ("Parameter array %qs at %L cannot be automatic "
17488 : : "or of deferred shape", sym->name, &sym->declared_at);
17489 : 17 : return false;
17490 : : }
17491 : :
17492 : : /* Constraints on deferred type parameter. */
17493 : 371248 : if (!deferred_requirements (sym))
17494 : : return false;
17495 : :
17496 : : /* Make sure a parameter that has been implicitly typed still
17497 : : matches the implicit type, since PARAMETER statements can precede
17498 : : IMPLICIT statements. */
17499 : 371247 : if (sym->attr.implicit_type
17500 : 371956 : && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
17501 : 709 : sym->ns)))
17502 : : {
17503 : 0 : gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
17504 : : "later IMPLICIT type", sym->name, &sym->declared_at);
17505 : 0 : return false;
17506 : : }
17507 : :
17508 : : /* Make sure the types of derived parameters are consistent. This
17509 : : type checking is deferred until resolution because the type may
17510 : : refer to a derived type from the host. */
17511 : 371247 : if (sym->ts.type == BT_DERIVED
17512 : 371247 : && !gfc_compare_types (&sym->ts, &sym->value->ts))
17513 : : {
17514 : 0 : gfc_error ("Incompatible derived type in PARAMETER at %L",
17515 : 0 : &sym->value->where);
17516 : 0 : return false;
17517 : : }
17518 : :
17519 : : /* F03:C509,C514. */
17520 : 371247 : if (sym->ts.type == BT_CLASS)
17521 : : {
17522 : 0 : gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
17523 : : sym->name, &sym->declared_at);
17524 : 0 : return false;
17525 : : }
17526 : :
17527 : : return true;
17528 : : }
17529 : :
17530 : :
17531 : : /* Called by resolve_symbol to check PDTs. */
17532 : :
17533 : : static void
17534 : 876 : resolve_pdt (gfc_symbol* sym)
17535 : : {
17536 : 876 : gfc_symbol *derived = NULL;
17537 : 876 : gfc_actual_arglist *param;
17538 : 876 : gfc_component *c;
17539 : 876 : bool const_len_exprs = true;
17540 : 876 : bool assumed_len_exprs = false;
17541 : 876 : symbol_attribute *attr;
17542 : :
17543 : 876 : if (sym->ts.type == BT_DERIVED)
17544 : : {
17545 : 704 : derived = sym->ts.u.derived;
17546 : 704 : attr = &(sym->attr);
17547 : : }
17548 : 172 : else if (sym->ts.type == BT_CLASS)
17549 : : {
17550 : 172 : derived = CLASS_DATA (sym)->ts.u.derived;
17551 : 172 : attr = &(CLASS_DATA (sym)->attr);
17552 : : }
17553 : : else
17554 : 0 : gcc_unreachable ();
17555 : :
17556 : 876 : gcc_assert (derived->attr.pdt_type);
17557 : :
17558 : 2223 : for (param = sym->param_list; param; param = param->next)
17559 : : {
17560 : 1347 : c = gfc_find_component (derived, param->name, false, true, NULL);
17561 : 1347 : gcc_assert (c);
17562 : 1347 : if (c->attr.pdt_kind)
17563 : 610 : continue;
17564 : :
17565 : 524 : if (param->expr && !gfc_is_constant_expr (param->expr)
17566 : 800 : && c->attr.pdt_len)
17567 : : const_len_exprs = false;
17568 : 674 : else if (param->spec_type == SPEC_ASSUMED)
17569 : 274 : assumed_len_exprs = true;
17570 : :
17571 : 737 : if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
17572 : 10 : && ((sym->ts.type == BT_DERIVED && !attr->pointer)
17573 : 8 : || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
17574 : 3 : gfc_error ("Entity %qs at %L has a deferred LEN "
17575 : : "parameter %qs and requires either the POINTER "
17576 : : "or ALLOCATABLE attribute",
17577 : : sym->name, &sym->declared_at,
17578 : : param->name);
17579 : :
17580 : : }
17581 : :
17582 : 876 : if (!const_len_exprs
17583 : 63 : && (sym->ns->proc_name->attr.is_main_program
17584 : 62 : || sym->ns->proc_name->attr.flavor == FL_MODULE
17585 : 61 : || sym->attr.save != SAVE_NONE))
17586 : 2 : gfc_error ("The AUTOMATIC object %qs at %L must not have the "
17587 : : "SAVE attribute or be a variable declared in the "
17588 : : "main program, a module or a submodule(F08/C513)",
17589 : : sym->name, &sym->declared_at);
17590 : :
17591 : 876 : if (assumed_len_exprs && !(sym->attr.dummy
17592 : 1 : || sym->attr.select_type_temporary || sym->attr.associate_var))
17593 : 1 : gfc_error ("The object %qs at %L with ASSUMED type parameters "
17594 : : "must be a dummy or a SELECT TYPE selector(F08/4.2)",
17595 : : sym->name, &sym->declared_at);
17596 : 876 : }
17597 : :
17598 : :
17599 : : /* Resolve the symbol's array spec. */
17600 : :
17601 : : static bool
17602 : 1654942 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
17603 : : {
17604 : 1654942 : gfc_namespace *orig_current_ns = gfc_current_ns;
17605 : 1654942 : gfc_current_ns = gfc_get_spec_ns (sym);
17606 : :
17607 : 1654942 : bool saved_specification_expr = specification_expr;
17608 : 1654942 : specification_expr = true;
17609 : :
17610 : 1654942 : bool result = gfc_resolve_array_spec (sym->as, check_constant);
17611 : :
17612 : 1654942 : specification_expr = saved_specification_expr;
17613 : 1654942 : gfc_current_ns = orig_current_ns;
17614 : :
17615 : 1654942 : return result;
17616 : : }
17617 : :
17618 : :
17619 : : /* Do anything necessary to resolve a symbol. Right now, we just
17620 : : assume that an otherwise unknown symbol is a variable. This sort
17621 : : of thing commonly happens for symbols in module. */
17622 : :
17623 : : static void
17624 : 1780211 : resolve_symbol (gfc_symbol *sym)
17625 : : {
17626 : 1780211 : int check_constant, mp_flag;
17627 : 1780211 : gfc_symtree *symtree;
17628 : 1780211 : gfc_symtree *this_symtree;
17629 : 1780211 : gfc_namespace *ns;
17630 : 1780211 : gfc_component *c;
17631 : 1780211 : symbol_attribute class_attr;
17632 : 1780211 : gfc_array_spec *as;
17633 : :
17634 : 1780211 : if (sym->resolve_symbol_called >= 1)
17635 : 156452 : return;
17636 : 1711544 : sym->resolve_symbol_called = 1;
17637 : :
17638 : : /* No symbol will ever have union type; only components can be unions.
17639 : : Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
17640 : : (just like derived type declaration symbols have flavor FL_DERIVED). */
17641 : 1711544 : gcc_assert (sym->ts.type != BT_UNION);
17642 : :
17643 : : /* Coarrayed polymorphic objects with allocatable or pointer components are
17644 : : yet unsupported for -fcoarray=lib. */
17645 : 1711544 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
17646 : 86 : && sym->ts.u.derived && CLASS_DATA (sym)
17647 : 86 : && CLASS_DATA (sym)->attr.codimension
17648 : 72 : && CLASS_DATA (sym)->ts.u.derived
17649 : 71 : && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
17650 : 68 : || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
17651 : : {
17652 : 6 : gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
17653 : : "type coarrays at %L are unsupported", &sym->declared_at);
17654 : 6 : return;
17655 : : }
17656 : :
17657 : 1711538 : if (sym->attr.artificial)
17658 : : return;
17659 : :
17660 : 1626372 : if (sym->attr.unlimited_polymorphic)
17661 : : return;
17662 : :
17663 : 1624932 : if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
17664 : : {
17665 : 4 : gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
17666 : : "the OpenMP DEPEND clause", &sym->declared_at);
17667 : 4 : return;
17668 : : }
17669 : :
17670 : 1624928 : if (sym->attr.flavor == FL_UNKNOWN
17671 : 1604243 : || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
17672 : 433845 : && !sym->attr.generic && !sym->attr.external
17673 : 176449 : && sym->attr.if_source == IFSRC_UNKNOWN
17674 : 79145 : && sym->ts.type == BT_UNKNOWN))
17675 : : {
17676 : : /* A symbol in a common block might not have been resolved yet properly.
17677 : : Do not try to find an interface with the same name. */
17678 : 91575 : if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
17679 : 20681 : && !sym->attr.generic && !sym->attr.external
17680 : 20630 : && sym->attr.in_common)
17681 : 2530 : goto skip_interfaces;
17682 : :
17683 : : /* If we find that a flavorless symbol is an interface in one of the
17684 : : parent namespaces, find its symtree in this namespace, free the
17685 : : symbol and set the symtree to point to the interface symbol. */
17686 : 127101 : for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
17687 : : {
17688 : 38710 : symtree = gfc_find_symtree (ns->sym_root, sym->name);
17689 : 38710 : if (symtree && (symtree->n.sym->generic ||
17690 : 687 : (symtree->n.sym->attr.flavor == FL_PROCEDURE
17691 : 598 : && sym->ns->construct_entities)))
17692 : : {
17693 : 662 : this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
17694 : : sym->name);
17695 : 662 : if (this_symtree->n.sym == sym)
17696 : : {
17697 : 654 : symtree->n.sym->refs++;
17698 : 654 : gfc_release_symbol (sym);
17699 : 654 : this_symtree->n.sym = symtree->n.sym;
17700 : 654 : return;
17701 : : }
17702 : : }
17703 : : }
17704 : :
17705 : 88391 : skip_interfaces:
17706 : : /* Otherwise give it a flavor according to such attributes as
17707 : : it has. */
17708 : 90921 : if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
17709 : 20506 : && sym->attr.intrinsic == 0)
17710 : 20502 : sym->attr.flavor = FL_VARIABLE;
17711 : 70419 : else if (sym->attr.flavor == FL_UNKNOWN)
17712 : : {
17713 : 55 : sym->attr.flavor = FL_PROCEDURE;
17714 : 55 : if (sym->attr.dimension)
17715 : 0 : sym->attr.function = 1;
17716 : : }
17717 : : }
17718 : :
17719 : 1624274 : if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
17720 : 2304 : gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
17721 : :
17722 : 1445 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
17723 : 1625719 : && !resolve_procedure_interface (sym))
17724 : : return;
17725 : :
17726 : 1624263 : if (sym->attr.is_protected && !sym->attr.proc_pointer
17727 : 130 : && (sym->attr.procedure || sym->attr.external))
17728 : : {
17729 : 0 : if (sym->attr.external)
17730 : 0 : gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
17731 : : "at %L", &sym->declared_at);
17732 : : else
17733 : 0 : gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
17734 : : "at %L", &sym->declared_at);
17735 : :
17736 : 0 : return;
17737 : : }
17738 : :
17739 : : /* Ensure that variables of derived or class type having a finalizer are
17740 : : marked used even when the variable is not used anything else in the scope.
17741 : : This fixes PR118730. */
17742 : 636992 : if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
17743 : 435994 : && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
17744 : 1672420 : && gfc_may_be_finalized (sym->ts))
17745 : 8136 : gfc_set_sym_referenced (sym);
17746 : :
17747 : 1624263 : if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
17748 : : return;
17749 : :
17750 : 1623492 : else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
17751 : 1624255 : && !resolve_fl_struct (sym))
17752 : : return;
17753 : :
17754 : : /* Symbols that are module procedures with results (functions) have
17755 : : the types and array specification copied for type checking in
17756 : : procedures that call them, as well as for saving to a module
17757 : : file. These symbols can't stand the scrutiny that their results
17758 : : can. */
17759 : 1624123 : mp_flag = (sym->result != NULL && sym->result != sym);
17760 : :
17761 : : /* Make sure that the intrinsic is consistent with its internal
17762 : : representation. This needs to be done before assigning a default
17763 : : type to avoid spurious warnings. */
17764 : 1590951 : if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
17765 : 1655457 : && !gfc_resolve_intrinsic (sym, &sym->declared_at))
17766 : : return;
17767 : :
17768 : : /* Resolve associate names. */
17769 : 1624087 : if (sym->assoc)
17770 : 6466 : resolve_assoc_var (sym, true);
17771 : :
17772 : : /* Assign default type to symbols that need one and don't have one. */
17773 : 1624087 : if (sym->ts.type == BT_UNKNOWN)
17774 : : {
17775 : 390378 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
17776 : : {
17777 : 11720 : gfc_set_default_type (sym, 1, NULL);
17778 : : }
17779 : :
17780 : 252789 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
17781 : 59776 : && !sym->attr.function && !sym->attr.subroutine
17782 : 391993 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
17783 : 564 : gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
17784 : :
17785 : 390378 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17786 : : {
17787 : : /* The specific case of an external procedure should emit an error
17788 : : in the case that there is no implicit type. */
17789 : 99318 : if (!mp_flag)
17790 : : {
17791 : 93490 : if (!sym->attr.mixed_entry_master)
17792 : 93384 : gfc_set_default_type (sym, sym->attr.external, NULL);
17793 : : }
17794 : : else
17795 : : {
17796 : : /* Result may be in another namespace. */
17797 : 5828 : resolve_symbol (sym->result);
17798 : :
17799 : 5828 : if (!sym->result->attr.proc_pointer)
17800 : : {
17801 : 5649 : sym->ts = sym->result->ts;
17802 : 5649 : sym->as = gfc_copy_array_spec (sym->result->as);
17803 : 5649 : sym->attr.dimension = sym->result->attr.dimension;
17804 : 5649 : sym->attr.codimension = sym->result->attr.codimension;
17805 : 5649 : sym->attr.pointer = sym->result->attr.pointer;
17806 : 5649 : sym->attr.allocatable = sym->result->attr.allocatable;
17807 : 5649 : sym->attr.contiguous = sym->result->attr.contiguous;
17808 : : }
17809 : : }
17810 : : }
17811 : : }
17812 : 1233709 : else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17813 : 31180 : resolve_symbol_array_spec (sym->result, false);
17814 : :
17815 : : /* For a CLASS-valued function with a result variable, affirm that it has
17816 : : been resolved also when looking at the symbol 'sym'. */
17817 : 421558 : if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
17818 : 713 : sym->attr.class_ok = sym->result->attr.class_ok;
17819 : :
17820 : 1624087 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
17821 : 18741 : && CLASS_DATA (sym))
17822 : : {
17823 : 18740 : as = CLASS_DATA (sym)->as;
17824 : 18740 : class_attr = CLASS_DATA (sym)->attr;
17825 : 18740 : class_attr.pointer = class_attr.class_pointer;
17826 : : }
17827 : : else
17828 : : {
17829 : 1605347 : class_attr = sym->attr;
17830 : 1605347 : as = sym->as;
17831 : : }
17832 : :
17833 : : /* F2008, C530. */
17834 : 1624087 : if (sym->attr.contiguous
17835 : 7682 : && (!class_attr.dimension
17836 : 7679 : || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
17837 : 125 : && !class_attr.pointer)))
17838 : : {
17839 : 7 : gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
17840 : : "array pointer or an assumed-shape or assumed-rank array",
17841 : : sym->name, &sym->declared_at);
17842 : 7 : return;
17843 : : }
17844 : :
17845 : : /* Assumed size arrays and assumed shape arrays must be dummy
17846 : : arguments. Array-spec's of implied-shape should have been resolved to
17847 : : AS_EXPLICIT already. */
17848 : :
17849 : 1616526 : if (as)
17850 : : {
17851 : : /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
17852 : : specification expression. */
17853 : 143264 : if (as->type == AS_IMPLIED_SHAPE)
17854 : : {
17855 : : int i;
17856 : 1 : for (i=0; i<as->rank; i++)
17857 : : {
17858 : 1 : if (as->lower[i] != NULL && as->upper[i] == NULL)
17859 : : {
17860 : 1 : gfc_error ("Bad specification for assumed size array at %L",
17861 : : &as->lower[i]->where);
17862 : 1 : return;
17863 : : }
17864 : : }
17865 : 0 : gcc_unreachable();
17866 : : }
17867 : :
17868 : 143263 : if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
17869 : 110947 : || as->type == AS_ASSUMED_SHAPE)
17870 : 43640 : && !sym->attr.dummy && !sym->attr.select_type_temporary
17871 : 8 : && !sym->attr.associate_var)
17872 : : {
17873 : 7 : if (as->type == AS_ASSUMED_SIZE)
17874 : 7 : gfc_error ("Assumed size array at %L must be a dummy argument",
17875 : : &sym->declared_at);
17876 : : else
17877 : 0 : gfc_error ("Assumed shape array at %L must be a dummy argument",
17878 : : &sym->declared_at);
17879 : 7 : return;
17880 : : }
17881 : : /* TS 29113, C535a. */
17882 : 143256 : if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
17883 : 60 : && !sym->attr.select_type_temporary
17884 : 60 : && !(cs_base && cs_base->current
17885 : 45 : && (cs_base->current->op == EXEC_SELECT_RANK
17886 : 3 : || ((gfc_option.allow_std & GFC_STD_F202Y)
17887 : 0 : && cs_base->current->op == EXEC_BLOCK))))
17888 : : {
17889 : 18 : gfc_error ("Assumed-rank array at %L must be a dummy argument",
17890 : : &sym->declared_at);
17891 : 18 : return;
17892 : : }
17893 : 143238 : if (as->type == AS_ASSUMED_RANK
17894 : 26191 : && (sym->attr.codimension || sym->attr.value))
17895 : : {
17896 : 2 : gfc_error ("Assumed-rank array at %L may not have the VALUE or "
17897 : : "CODIMENSION attribute", &sym->declared_at);
17898 : 2 : return;
17899 : : }
17900 : : }
17901 : :
17902 : : /* Make sure symbols with known intent or optional are really dummy
17903 : : variable. Because of ENTRY statement, this has to be deferred
17904 : : until resolution time. */
17905 : :
17906 : 1624052 : if (!sym->attr.dummy
17907 : 1165403 : && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
17908 : : {
17909 : 2 : gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
17910 : 2 : return;
17911 : : }
17912 : :
17913 : 1624050 : if (sym->attr.value && !sym->attr.dummy)
17914 : : {
17915 : 2 : gfc_error ("%qs at %L cannot have the VALUE attribute because "
17916 : : "it is not a dummy argument", sym->name, &sym->declared_at);
17917 : 2 : return;
17918 : : }
17919 : :
17920 : 1624048 : if (sym->attr.value && sym->ts.type == BT_CHARACTER)
17921 : : {
17922 : 616 : gfc_charlen *cl = sym->ts.u.cl;
17923 : 616 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
17924 : : {
17925 : 2 : gfc_error ("Character dummy variable %qs at %L with VALUE "
17926 : : "attribute must have constant length",
17927 : : sym->name, &sym->declared_at);
17928 : 2 : return;
17929 : : }
17930 : :
17931 : 614 : if (sym->ts.is_c_interop
17932 : 381 : && mpz_cmp_si (cl->length->value.integer, 1) != 0)
17933 : : {
17934 : 1 : gfc_error ("C interoperable character dummy variable %qs at %L "
17935 : : "with VALUE attribute must have length one",
17936 : : sym->name, &sym->declared_at);
17937 : 1 : return;
17938 : : }
17939 : : }
17940 : :
17941 : 1624045 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17942 : 119564 : && sym->ts.u.derived->attr.generic)
17943 : : {
17944 : 20 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
17945 : 20 : if (!sym->ts.u.derived)
17946 : : {
17947 : 0 : gfc_error ("The derived type %qs at %L is of type %qs, "
17948 : : "which has not been defined", sym->name,
17949 : : &sym->declared_at, sym->ts.u.derived->name);
17950 : 0 : sym->ts.type = BT_UNKNOWN;
17951 : 0 : return;
17952 : : }
17953 : : }
17954 : :
17955 : : /* Use the same constraints as TYPE(*), except for the type check
17956 : : and that only scalars and assumed-size arrays are permitted. */
17957 : 1624045 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
17958 : : {
17959 : 12960 : if (!sym->attr.dummy)
17960 : : {
17961 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17962 : : "a dummy argument", sym->name, &sym->declared_at);
17963 : 1 : return;
17964 : : }
17965 : :
17966 : 12959 : if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
17967 : 8 : && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
17968 : 0 : && sym->ts.type != BT_COMPLEX)
17969 : : {
17970 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17971 : : "of type TYPE(*) or of an numeric intrinsic type",
17972 : : sym->name, &sym->declared_at);
17973 : 0 : return;
17974 : : }
17975 : :
17976 : 12959 : if (sym->attr.allocatable || sym->attr.codimension
17977 : 12957 : || sym->attr.pointer || sym->attr.value)
17978 : : {
17979 : 4 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17980 : : "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
17981 : : "attribute", sym->name, &sym->declared_at);
17982 : 4 : return;
17983 : : }
17984 : :
17985 : 12955 : if (sym->attr.intent == INTENT_OUT)
17986 : : {
17987 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17988 : : "have the INTENT(OUT) attribute",
17989 : : sym->name, &sym->declared_at);
17990 : 0 : return;
17991 : : }
17992 : 12955 : if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
17993 : : {
17994 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
17995 : : "either be a scalar or an assumed-size array",
17996 : : sym->name, &sym->declared_at);
17997 : 1 : return;
17998 : : }
17999 : :
18000 : : /* Set the type to TYPE(*) and add a dimension(*) to ensure
18001 : : NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
18002 : : packing. */
18003 : 12954 : sym->ts.type = BT_ASSUMED;
18004 : 12954 : sym->as = gfc_get_array_spec ();
18005 : 12954 : sym->as->type = AS_ASSUMED_SIZE;
18006 : 12954 : sym->as->rank = 1;
18007 : 12954 : sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
18008 : : }
18009 : 1611085 : else if (sym->ts.type == BT_ASSUMED)
18010 : : {
18011 : : /* TS 29113, C407a. */
18012 : 11002 : if (!sym->attr.dummy)
18013 : : {
18014 : 7 : gfc_error ("Assumed type of variable %s at %L is only permitted "
18015 : : "for dummy variables", sym->name, &sym->declared_at);
18016 : 7 : return;
18017 : : }
18018 : 10995 : if (sym->attr.allocatable || sym->attr.codimension
18019 : 10991 : || sym->attr.pointer || sym->attr.value)
18020 : : {
18021 : 8 : gfc_error ("Assumed-type variable %s at %L may not have the "
18022 : : "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
18023 : : sym->name, &sym->declared_at);
18024 : 8 : return;
18025 : : }
18026 : 10987 : if (sym->attr.intent == INTENT_OUT)
18027 : : {
18028 : 2 : gfc_error ("Assumed-type variable %s at %L may not have the "
18029 : : "INTENT(OUT) attribute",
18030 : : sym->name, &sym->declared_at);
18031 : 2 : return;
18032 : : }
18033 : 10985 : if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
18034 : : {
18035 : 3 : gfc_error ("Assumed-type variable %s at %L shall not be an "
18036 : : "explicit-shape array", sym->name, &sym->declared_at);
18037 : 3 : return;
18038 : : }
18039 : : }
18040 : :
18041 : : /* If the symbol is marked as bind(c), that it is declared at module level
18042 : : scope and verify its type and kind. Do not do the latter for symbols
18043 : : that are implicitly typed because that is handled in
18044 : : gfc_set_default_type. Handle dummy arguments and procedure definitions
18045 : : separately. Also, anything that is use associated is not handled here
18046 : : but instead is handled in the module it is declared in. Finally, derived
18047 : : type definitions are allowed to be BIND(C) since that only implies that
18048 : : they're interoperable, and they are checked fully for interoperability
18049 : : when a variable is declared of that type. */
18050 : 1624019 : if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
18051 : 7151 : && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
18052 : 567 : && sym->attr.flavor != FL_DERIVED)
18053 : : {
18054 : 167 : bool t = true;
18055 : :
18056 : : /* First, make sure the variable is declared at the
18057 : : module-level scope (J3/04-007, Section 15.3). */
18058 : 167 : if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
18059 : 7 : && !sym->attr.in_common)
18060 : : {
18061 : 6 : gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
18062 : : "is neither a COMMON block nor declared at the "
18063 : : "module level scope", sym->name, &(sym->declared_at));
18064 : 6 : t = false;
18065 : : }
18066 : 161 : else if (sym->ts.type == BT_CHARACTER
18067 : 161 : && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
18068 : 1 : || !gfc_is_constant_expr (sym->ts.u.cl->length)
18069 : 1 : || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
18070 : : {
18071 : 1 : gfc_error ("BIND(C) Variable %qs at %L must have length one",
18072 : 1 : sym->name, &sym->declared_at);
18073 : 1 : t = false;
18074 : : }
18075 : 160 : else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
18076 : : {
18077 : 1 : t = verify_com_block_vars_c_interop (sym->common_head);
18078 : : }
18079 : 159 : else if (sym->attr.implicit_type == 0)
18080 : : {
18081 : : /* If type() declaration, we need to verify that the components
18082 : : of the given type are all C interoperable, etc. */
18083 : 157 : if (sym->ts.type == BT_DERIVED &&
18084 : 24 : sym->ts.u.derived->attr.is_c_interop != 1)
18085 : : {
18086 : : /* Make sure the user marked the derived type as BIND(C). If
18087 : : not, call the verify routine. This could print an error
18088 : : for the derived type more than once if multiple variables
18089 : : of that type are declared. */
18090 : 14 : if (sym->ts.u.derived->attr.is_bind_c != 1)
18091 : 1 : verify_bind_c_derived_type (sym->ts.u.derived);
18092 : 157 : t = false;
18093 : : }
18094 : :
18095 : : /* Verify the variable itself as C interoperable if it
18096 : : is BIND(C). It is not possible for this to succeed if
18097 : : the verify_bind_c_derived_type failed, so don't have to handle
18098 : : any error returned by verify_bind_c_derived_type. */
18099 : 157 : t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
18100 : 157 : sym->common_block);
18101 : : }
18102 : :
18103 : 165 : if (!t)
18104 : : {
18105 : : /* clear the is_bind_c flag to prevent reporting errors more than
18106 : : once if something failed. */
18107 : 10 : sym->attr.is_bind_c = 0;
18108 : 10 : return;
18109 : : }
18110 : : }
18111 : :
18112 : : /* If a derived type symbol has reached this point, without its
18113 : : type being declared, we have an error. Notice that most
18114 : : conditions that produce undefined derived types have already
18115 : : been dealt with. However, the likes of:
18116 : : implicit type(t) (t) ..... call foo (t) will get us here if
18117 : : the type is not declared in the scope of the implicit
18118 : : statement. Change the type to BT_UNKNOWN, both because it is so
18119 : : and to prevent an ICE. */
18120 : 1624009 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
18121 : 119562 : && sym->ts.u.derived->components == NULL
18122 : 1084 : && !sym->ts.u.derived->attr.zero_comp)
18123 : : {
18124 : 3 : gfc_error ("The derived type %qs at %L is of type %qs, "
18125 : : "which has not been defined", sym->name,
18126 : : &sym->declared_at, sym->ts.u.derived->name);
18127 : 3 : sym->ts.type = BT_UNKNOWN;
18128 : 3 : return;
18129 : : }
18130 : :
18131 : : /* Make sure that the derived type has been resolved and that the
18132 : : derived type is visible in the symbol's namespace, if it is a
18133 : : module function and is not PRIVATE. */
18134 : 1624006 : if (sym->ts.type == BT_DERIVED
18135 : 126431 : && sym->ts.u.derived->attr.use_assoc
18136 : 109902 : && sym->ns->proc_name
18137 : 109894 : && sym->ns->proc_name->attr.flavor == FL_MODULE
18138 : 1629812 : && !resolve_fl_derived (sym->ts.u.derived))
18139 : : return;
18140 : :
18141 : : /* Unless the derived-type declaration is use associated, Fortran 95
18142 : : does not allow public entries of private derived types.
18143 : : See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
18144 : : 161 in 95-006r3. */
18145 : 1624006 : if (sym->ts.type == BT_DERIVED
18146 : 126431 : && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
18147 : 7652 : && !sym->ts.u.derived->attr.use_assoc
18148 : 1846 : && gfc_check_symbol_access (sym)
18149 : 1649 : && !gfc_check_symbol_access (sym->ts.u.derived)
18150 : 1624019 : && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
18151 : : "derived type %qs",
18152 : 13 : (sym->attr.flavor == FL_PARAMETER)
18153 : : ? "parameter" : "variable",
18154 : : sym->name, &sym->declared_at,
18155 : 13 : sym->ts.u.derived->name))
18156 : : return;
18157 : :
18158 : : /* F2008, C1302. */
18159 : 1623999 : if (sym->ts.type == BT_DERIVED
18160 : 126424 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18161 : 118 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
18162 : 126400 : || sym->ts.u.derived->attr.lock_comp)
18163 : 37 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18164 : : {
18165 : 4 : gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
18166 : : "type LOCK_TYPE must be a coarray", sym->name,
18167 : : &sym->declared_at);
18168 : 4 : return;
18169 : : }
18170 : :
18171 : : /* TS18508, C702/C703. */
18172 : 1623995 : if (sym->ts.type == BT_DERIVED
18173 : 126420 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18174 : 117 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
18175 : 126410 : || sym->ts.u.derived->attr.event_comp)
18176 : 10 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18177 : : {
18178 : 1 : gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
18179 : : "type EVENT_TYPE must be a coarray", sym->name,
18180 : : &sym->declared_at);
18181 : 1 : return;
18182 : : }
18183 : :
18184 : : /* An assumed-size array with INTENT(OUT) shall not be of a type for which
18185 : : default initialization is defined (5.1.2.4.4). */
18186 : 1623994 : if (sym->ts.type == BT_DERIVED
18187 : 126419 : && sym->attr.dummy
18188 : 43739 : && sym->attr.intent == INTENT_OUT
18189 : 2354 : && sym->as
18190 : 381 : && sym->as->type == AS_ASSUMED_SIZE)
18191 : : {
18192 : 1 : for (c = sym->ts.u.derived->components; c; c = c->next)
18193 : : {
18194 : 1 : if (c->initializer)
18195 : : {
18196 : 1 : gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
18197 : : "ASSUMED SIZE and so cannot have a default initializer",
18198 : : sym->name, &sym->declared_at);
18199 : 1 : return;
18200 : : }
18201 : : }
18202 : : }
18203 : :
18204 : : /* F2008, C542. */
18205 : 1623993 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
18206 : 43738 : && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
18207 : : {
18208 : 0 : gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
18209 : : "INTENT(OUT)", sym->name, &sym->declared_at);
18210 : 0 : return;
18211 : : }
18212 : :
18213 : : /* TS18508. */
18214 : 1623993 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
18215 : 43738 : && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
18216 : : {
18217 : 0 : gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
18218 : : "INTENT(OUT)", sym->name, &sym->declared_at);
18219 : 0 : return;
18220 : : }
18221 : :
18222 : : /* F2008, C525. */
18223 : 1623993 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18224 : 1623904 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18225 : 18744 : && sym->ts.u.derived && CLASS_DATA (sym)
18226 : 18738 : && CLASS_DATA (sym)->attr.coarray_comp))
18227 : 1623904 : || class_attr.codimension)
18228 : 1544 : && (sym->attr.result || sym->result == sym))
18229 : : {
18230 : 8 : gfc_error ("Function result %qs at %L shall not be a coarray or have "
18231 : : "a coarray component", sym->name, &sym->declared_at);
18232 : 8 : return;
18233 : : }
18234 : :
18235 : : /* F2008, C524. */
18236 : 1623985 : if (sym->attr.codimension && sym->ts.type == BT_DERIVED
18237 : 370 : && sym->ts.u.derived->ts.is_iso_c)
18238 : : {
18239 : 3 : gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
18240 : : "shall not be a coarray", sym->name, &sym->declared_at);
18241 : 3 : return;
18242 : : }
18243 : :
18244 : : /* F2008, C525. */
18245 : 1623982 : if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18246 : 1623896 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18247 : 18743 : && sym->ts.u.derived && CLASS_DATA (sym)
18248 : 18737 : && CLASS_DATA (sym)->attr.coarray_comp))
18249 : 86 : && (class_attr.codimension || class_attr.pointer || class_attr.dimension
18250 : 82 : || class_attr.allocatable))
18251 : : {
18252 : 4 : gfc_error ("Variable %qs at %L with coarray component shall be a "
18253 : : "nonpointer, nonallocatable scalar, which is not a coarray",
18254 : : sym->name, &sym->declared_at);
18255 : 4 : return;
18256 : : }
18257 : :
18258 : : /* F2008, C526. The function-result case was handled above. */
18259 : 1623978 : if (class_attr.codimension
18260 : 1447 : && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
18261 : 296 : || sym->attr.select_type_temporary
18262 : 225 : || sym->attr.associate_var
18263 : 212 : || (sym->ns->save_all && !sym->attr.automatic)
18264 : 212 : || sym->ns->proc_name->attr.flavor == FL_MODULE
18265 : 212 : || sym->ns->proc_name->attr.is_main_program
18266 : 5 : || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
18267 : : {
18268 : 4 : gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
18269 : : "nor a dummy argument", sym->name, &sym->declared_at);
18270 : 4 : return;
18271 : : }
18272 : : /* F2008, C528. */
18273 : 1623974 : else if (class_attr.codimension && !sym->attr.select_type_temporary
18274 : 1372 : && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
18275 : : {
18276 : 6 : gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
18277 : : "deferred shape without allocatable", sym->name,
18278 : : &sym->declared_at);
18279 : 6 : return;
18280 : : }
18281 : 1623968 : else if (class_attr.codimension && class_attr.allocatable && as
18282 : 523 : && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
18283 : : {
18284 : 9 : gfc_error ("Allocatable coarray variable %qs at %L must have "
18285 : : "deferred shape", sym->name, &sym->declared_at);
18286 : 9 : return;
18287 : : }
18288 : :
18289 : : /* F2008, C541. */
18290 : 1623959 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18291 : 1623877 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18292 : 18738 : && sym->ts.u.derived && CLASS_DATA (sym)
18293 : 18732 : && CLASS_DATA (sym)->attr.coarray_comp))
18294 : 1623877 : || (class_attr.codimension && class_attr.allocatable))
18295 : 596 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
18296 : : {
18297 : 3 : gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
18298 : : "allocatable coarray or have coarray components",
18299 : : sym->name, &sym->declared_at);
18300 : 3 : return;
18301 : : }
18302 : :
18303 : 1623956 : if (class_attr.codimension && sym->attr.dummy
18304 : 425 : && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
18305 : : {
18306 : 2 : gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
18307 : : "procedure %qs", sym->name, &sym->declared_at,
18308 : : sym->ns->proc_name->name);
18309 : 2 : return;
18310 : : }
18311 : :
18312 : 1623954 : if (sym->ts.type == BT_LOGICAL
18313 : 111236 : && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
18314 : 111233 : || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
18315 : 30765 : && sym->ns->proc_name->attr.is_bind_c)))
18316 : : {
18317 : : int i;
18318 : 200 : for (i = 0; gfc_logical_kinds[i].kind; i++)
18319 : 200 : if (gfc_logical_kinds[i].kind == sym->ts.kind)
18320 : : break;
18321 : 16 : if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
18322 : 181 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
18323 : : "%L with non-C_Bool kind in BIND(C) procedure "
18324 : : "%qs", sym->name, &sym->declared_at,
18325 : 13 : sym->ns->proc_name->name))
18326 : : return;
18327 : 167 : else if (!gfc_logical_kinds[i].c_bool
18328 : 182 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
18329 : : "%qs at %L with non-C_Bool kind in "
18330 : : "BIND(C) procedure %qs", sym->name,
18331 : : &sym->declared_at,
18332 : 15 : sym->attr.function ? sym->name
18333 : 13 : : sym->ns->proc_name->name))
18334 : : return;
18335 : : }
18336 : :
18337 : 1623951 : switch (sym->attr.flavor)
18338 : : {
18339 : 636875 : case FL_VARIABLE:
18340 : 636875 : if (!resolve_fl_variable (sym, mp_flag))
18341 : : return;
18342 : : break;
18343 : :
18344 : 464643 : case FL_PROCEDURE:
18345 : 464643 : if (sym->formal && !sym->formal_ns)
18346 : : {
18347 : : /* Check that none of the arguments are a namelist. */
18348 : : gfc_formal_arglist *formal = sym->formal;
18349 : :
18350 : 103474 : for (; formal; formal = formal->next)
18351 : 70347 : if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
18352 : : {
18353 : 1 : gfc_error ("Namelist %qs cannot be an argument to "
18354 : : "subroutine or function at %L",
18355 : : formal->sym->name, &sym->declared_at);
18356 : 1 : return;
18357 : : }
18358 : : }
18359 : :
18360 : 464642 : if (!resolve_fl_procedure (sym, mp_flag))
18361 : : return;
18362 : : break;
18363 : :
18364 : 805 : case FL_NAMELIST:
18365 : 805 : if (!resolve_fl_namelist (sym))
18366 : : return;
18367 : : break;
18368 : :
18369 : 371265 : case FL_PARAMETER:
18370 : 371265 : if (!resolve_fl_parameter (sym))
18371 : : return;
18372 : : break;
18373 : :
18374 : : default:
18375 : : break;
18376 : : }
18377 : :
18378 : : /* Resolve array specifier. Check as well some constraints
18379 : : on COMMON blocks. */
18380 : :
18381 : 1623762 : check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
18382 : :
18383 : 1623762 : resolve_symbol_array_spec (sym, check_constant);
18384 : :
18385 : : /* Resolve formal namespaces. */
18386 : 1623762 : if (sym->formal_ns && sym->formal_ns != gfc_current_ns
18387 : 254148 : && !sym->attr.contained && !sym->attr.intrinsic)
18388 : 230043 : gfc_resolve (sym->formal_ns);
18389 : :
18390 : : /* Make sure the formal namespace is present. */
18391 : 1623762 : if (sym->formal && !sym->formal_ns)
18392 : : {
18393 : : gfc_formal_arglist *formal = sym->formal;
18394 : 33439 : while (formal && !formal->sym)
18395 : 11 : formal = formal->next;
18396 : :
18397 : 33428 : if (formal)
18398 : : {
18399 : 33417 : sym->formal_ns = formal->sym->ns;
18400 : 33417 : if (sym->formal_ns && sym->ns != formal->sym->ns)
18401 : 25169 : sym->formal_ns->refs++;
18402 : : }
18403 : : }
18404 : :
18405 : : /* Check threadprivate restrictions. */
18406 : 1623762 : if (sym->attr.threadprivate
18407 : 332 : && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
18408 : 32 : && !(sym->ns->save_all && !sym->attr.automatic)
18409 : 31 : && sym->module == NULL
18410 : 16 : && (sym->ns->proc_name == NULL
18411 : 16 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18412 : 3 : && !sym->ns->proc_name->attr.is_main_program)))
18413 : 1 : gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
18414 : :
18415 : : /* Check omp declare target restrictions. */
18416 : 1623762 : if (sym->attr.omp_declare_target
18417 : 1443 : && sym->attr.flavor == FL_VARIABLE
18418 : 604 : && !sym->attr.save
18419 : 195 : && !(sym->ns->save_all && !sym->attr.automatic)
18420 : 195 : && (!sym->attr.in_common
18421 : 182 : && sym->module == NULL
18422 : 92 : && (sym->ns->proc_name == NULL
18423 : 92 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18424 : 2 : && !sym->ns->proc_name->attr.is_main_program))))
18425 : 1 : gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
18426 : : sym->name, &sym->declared_at);
18427 : :
18428 : : /* If we have come this far we can apply default-initializers, as
18429 : : described in 14.7.5, to those variables that have not already
18430 : : been assigned one. */
18431 : 1623762 : if (sym->ts.type == BT_DERIVED
18432 : 126391 : && !sym->value
18433 : 102080 : && !sym->attr.allocatable
18434 : 99266 : && !sym->attr.alloc_comp)
18435 : : {
18436 : 99250 : symbol_attribute *a = &sym->attr;
18437 : :
18438 : 99250 : if ((!a->save && !a->dummy && !a->pointer
18439 : 54263 : && !a->in_common && !a->use_assoc
18440 : 9872 : && a->referenced
18441 : 7721 : && !((a->function || a->result)
18442 : 1436 : && (!a->dimension
18443 : 124 : || sym->ts.u.derived->attr.alloc_comp
18444 : 83 : || sym->ts.u.derived->attr.pointer_comp))
18445 : 6354 : && !(a->function && sym != sym->result))
18446 : 92916 : || (a->dummy && !a->pointer && a->intent == INTENT_OUT
18447 : 1528 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
18448 : 7763 : apply_default_init (sym);
18449 : 91487 : else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc
18450 : 2559 : && sym->result)
18451 : : /* Default initialization for function results. */
18452 : 2555 : apply_default_init (sym->result);
18453 : 88932 : else if (a->function && sym->result && a->access != ACCESS_PRIVATE
18454 : 11152 : && (sym->ts.u.derived->attr.alloc_comp
18455 : 10752 : || sym->ts.u.derived->attr.pointer_comp))
18456 : : /* Mark the result symbol to be referenced, when it has allocatable
18457 : : components. */
18458 : 446 : sym->result->attr.referenced = 1;
18459 : : }
18460 : :
18461 : 1623762 : if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
18462 : 18248 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT
18463 : 1180 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
18464 : 1105 : && !CLASS_DATA (sym)->attr.class_pointer
18465 : 1079 : && !CLASS_DATA (sym)->attr.allocatable)
18466 : 807 : apply_default_init (sym);
18467 : :
18468 : : /* If this symbol has a type-spec, check it. */
18469 : 1623762 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
18470 : 615730 : || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
18471 : 1319149 : if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
18472 : : return;
18473 : :
18474 : 1623759 : if (sym->param_list)
18475 : 876 : resolve_pdt (sym);
18476 : : }
18477 : :
18478 : :
18479 : : /************* Resolve DATA statements *************/
18480 : :
18481 : : static struct
18482 : : {
18483 : : gfc_data_value *vnode;
18484 : : mpz_t left;
18485 : : }
18486 : : values;
18487 : :
18488 : :
18489 : : /* Advance the values structure to point to the next value in the data list. */
18490 : :
18491 : : static bool
18492 : 10856 : next_data_value (void)
18493 : : {
18494 : 16622 : while (mpz_cmp_ui (values.left, 0) == 0)
18495 : : {
18496 : :
18497 : 8179 : if (values.vnode->next == NULL)
18498 : : return false;
18499 : :
18500 : 5766 : values.vnode = values.vnode->next;
18501 : 5766 : mpz_set (values.left, values.vnode->repeat);
18502 : : }
18503 : :
18504 : : return true;
18505 : : }
18506 : :
18507 : :
18508 : : static bool
18509 : 3537 : check_data_variable (gfc_data_variable *var, locus *where)
18510 : : {
18511 : 3537 : gfc_expr *e;
18512 : 3537 : mpz_t size;
18513 : 3537 : mpz_t offset;
18514 : 3537 : bool t;
18515 : 3537 : ar_type mark = AR_UNKNOWN;
18516 : 3537 : int i;
18517 : 3537 : mpz_t section_index[GFC_MAX_DIMENSIONS];
18518 : 3537 : int vector_offset[GFC_MAX_DIMENSIONS];
18519 : 3537 : gfc_ref *ref;
18520 : 3537 : gfc_array_ref *ar;
18521 : 3537 : gfc_symbol *sym;
18522 : 3537 : int has_pointer;
18523 : :
18524 : 3537 : if (!gfc_resolve_expr (var->expr))
18525 : : return false;
18526 : :
18527 : 3537 : ar = NULL;
18528 : 3537 : e = var->expr;
18529 : :
18530 : 3537 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
18531 : 0 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
18532 : 0 : e = e->value.function.actual->expr;
18533 : :
18534 : 3537 : if (e->expr_type != EXPR_VARIABLE)
18535 : : {
18536 : 0 : gfc_error ("Expecting definable entity near %L", where);
18537 : 0 : return false;
18538 : : }
18539 : :
18540 : 3537 : sym = e->symtree->n.sym;
18541 : :
18542 : 3537 : if (sym->ns->is_block_data && !sym->attr.in_common)
18543 : : {
18544 : 2 : gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
18545 : : sym->name, &sym->declared_at);
18546 : 2 : return false;
18547 : : }
18548 : :
18549 : 3535 : if (e->ref == NULL && sym->as)
18550 : : {
18551 : 1 : gfc_error ("DATA array %qs at %L must be specified in a previous"
18552 : : " declaration", sym->name, where);
18553 : 1 : return false;
18554 : : }
18555 : :
18556 : 3534 : if (gfc_is_coindexed (e))
18557 : : {
18558 : 5 : gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
18559 : : where);
18560 : 5 : return false;
18561 : : }
18562 : :
18563 : 3529 : has_pointer = sym->attr.pointer;
18564 : :
18565 : 5968 : for (ref = e->ref; ref; ref = ref->next)
18566 : : {
18567 : 2443 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
18568 : : has_pointer = 1;
18569 : :
18570 : 2417 : if (has_pointer)
18571 : : {
18572 : 29 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
18573 : : {
18574 : 1 : gfc_error ("DATA element %qs at %L is a pointer and so must "
18575 : : "be a full array", sym->name, where);
18576 : 1 : return false;
18577 : : }
18578 : :
18579 : 28 : if (values.vnode->expr->expr_type == EXPR_CONSTANT)
18580 : : {
18581 : 1 : gfc_error ("DATA object near %L has the pointer attribute "
18582 : : "and the corresponding DATA value is not a valid "
18583 : : "initial-data-target", where);
18584 : 1 : return false;
18585 : : }
18586 : : }
18587 : :
18588 : 2441 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
18589 : : {
18590 : 1 : gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
18591 : : "attribute", ref->u.c.component->name, &e->where);
18592 : 1 : return false;
18593 : : }
18594 : :
18595 : : /* Reject substrings of strings of non-constant length. */
18596 : 2440 : if (ref->type == REF_SUBSTRING
18597 : 73 : && ref->u.ss.length
18598 : 73 : && ref->u.ss.length->length
18599 : 2513 : && !gfc_is_constant_expr (ref->u.ss.length->length))
18600 : 1 : goto bad_charlen;
18601 : : }
18602 : :
18603 : : /* Reject strings with deferred length or non-constant length. */
18604 : 3525 : if (e->ts.type == BT_CHARACTER
18605 : 3525 : && (e->ts.deferred
18606 : 374 : || (e->ts.u.cl->length
18607 : 323 : && !gfc_is_constant_expr (e->ts.u.cl->length))))
18608 : 5 : goto bad_charlen;
18609 : :
18610 : 3520 : mpz_init_set_si (offset, 0);
18611 : :
18612 : 3520 : if (e->rank == 0 || has_pointer)
18613 : : {
18614 : 2675 : mpz_init_set_ui (size, 1);
18615 : 2675 : ref = NULL;
18616 : : }
18617 : : else
18618 : : {
18619 : 845 : ref = e->ref;
18620 : :
18621 : : /* Find the array section reference. */
18622 : 1028 : for (ref = e->ref; ref; ref = ref->next)
18623 : : {
18624 : 1028 : if (ref->type != REF_ARRAY)
18625 : 92 : continue;
18626 : 936 : if (ref->u.ar.type == AR_ELEMENT)
18627 : 91 : continue;
18628 : : break;
18629 : : }
18630 : 845 : gcc_assert (ref);
18631 : :
18632 : : /* Set marks according to the reference pattern. */
18633 : 845 : switch (ref->u.ar.type)
18634 : : {
18635 : : case AR_FULL:
18636 : : mark = AR_FULL;
18637 : : break;
18638 : :
18639 : 149 : case AR_SECTION:
18640 : 149 : ar = &ref->u.ar;
18641 : : /* Get the start position of array section. */
18642 : 149 : gfc_get_section_index (ar, section_index, &offset, vector_offset);
18643 : 149 : mark = AR_SECTION;
18644 : 149 : break;
18645 : :
18646 : 0 : default:
18647 : 0 : gcc_unreachable ();
18648 : : }
18649 : :
18650 : 845 : if (!gfc_array_size (e, &size))
18651 : : {
18652 : 1 : gfc_error ("Nonconstant array section at %L in DATA statement",
18653 : : where);
18654 : 1 : mpz_clear (offset);
18655 : 1 : return false;
18656 : : }
18657 : : }
18658 : :
18659 : 3519 : t = true;
18660 : :
18661 : 11900 : while (mpz_cmp_ui (size, 0) > 0)
18662 : : {
18663 : 8444 : if (!next_data_value ())
18664 : : {
18665 : 1 : gfc_error ("DATA statement at %L has more variables than values",
18666 : : where);
18667 : 1 : t = false;
18668 : 1 : break;
18669 : : }
18670 : :
18671 : 8443 : t = gfc_check_assign (var->expr, values.vnode->expr, 0);
18672 : 8443 : if (!t)
18673 : : break;
18674 : :
18675 : : /* If we have more than one element left in the repeat count,
18676 : : and we have more than one element left in the target variable,
18677 : : then create a range assignment. */
18678 : : /* FIXME: Only done for full arrays for now, since array sections
18679 : : seem tricky. */
18680 : 8424 : if (mark == AR_FULL && ref && ref->next == NULL
18681 : 5364 : && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
18682 : : {
18683 : 137 : mpz_t range;
18684 : :
18685 : 137 : if (mpz_cmp (size, values.left) >= 0)
18686 : : {
18687 : 126 : mpz_init_set (range, values.left);
18688 : 126 : mpz_sub (size, size, values.left);
18689 : 126 : mpz_set_ui (values.left, 0);
18690 : : }
18691 : : else
18692 : : {
18693 : 11 : mpz_init_set (range, size);
18694 : 11 : mpz_sub (values.left, values.left, size);
18695 : 11 : mpz_set_ui (size, 0);
18696 : : }
18697 : :
18698 : 137 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18699 : : offset, &range);
18700 : :
18701 : 137 : mpz_add (offset, offset, range);
18702 : 137 : mpz_clear (range);
18703 : :
18704 : 137 : if (!t)
18705 : : break;
18706 : 129 : }
18707 : :
18708 : : /* Assign initial value to symbol. */
18709 : : else
18710 : : {
18711 : 8287 : mpz_sub_ui (values.left, values.left, 1);
18712 : 8287 : mpz_sub_ui (size, size, 1);
18713 : :
18714 : 8287 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18715 : : offset, NULL);
18716 : 8287 : if (!t)
18717 : : break;
18718 : :
18719 : 8252 : if (mark == AR_FULL)
18720 : 5259 : mpz_add_ui (offset, offset, 1);
18721 : :
18722 : : /* Modify the array section indexes and recalculate the offset
18723 : : for next element. */
18724 : 2993 : else if (mark == AR_SECTION)
18725 : 363 : gfc_advance_section (section_index, ar, &offset, vector_offset);
18726 : : }
18727 : : }
18728 : :
18729 : 3519 : if (mark == AR_SECTION)
18730 : : {
18731 : 340 : for (i = 0; i < ar->dimen; i++)
18732 : 192 : mpz_clear (section_index[i]);
18733 : : }
18734 : :
18735 : 3519 : mpz_clear (size);
18736 : 3519 : mpz_clear (offset);
18737 : :
18738 : 3519 : return t;
18739 : :
18740 : 6 : bad_charlen:
18741 : 6 : gfc_error ("Non-constant character length at %L in DATA statement",
18742 : : &e->where);
18743 : 6 : return false;
18744 : : }
18745 : :
18746 : :
18747 : : static bool traverse_data_var (gfc_data_variable *, locus *);
18748 : :
18749 : : /* Iterate over a list of elements in a DATA statement. */
18750 : :
18751 : : static bool
18752 : 236 : traverse_data_list (gfc_data_variable *var, locus *where)
18753 : : {
18754 : 236 : mpz_t trip;
18755 : 236 : iterator_stack frame;
18756 : 236 : gfc_expr *e, *start, *end, *step;
18757 : 236 : bool retval = true;
18758 : :
18759 : 236 : mpz_init (frame.value);
18760 : 236 : mpz_init (trip);
18761 : :
18762 : 236 : start = gfc_copy_expr (var->iter.start);
18763 : 236 : end = gfc_copy_expr (var->iter.end);
18764 : 236 : step = gfc_copy_expr (var->iter.step);
18765 : :
18766 : 236 : if (!gfc_simplify_expr (start, 1)
18767 : 236 : || start->expr_type != EXPR_CONSTANT)
18768 : : {
18769 : 0 : gfc_error ("start of implied-do loop at %L could not be "
18770 : : "simplified to a constant value", &start->where);
18771 : 0 : retval = false;
18772 : 0 : goto cleanup;
18773 : : }
18774 : 236 : if (!gfc_simplify_expr (end, 1)
18775 : 236 : || end->expr_type != EXPR_CONSTANT)
18776 : : {
18777 : 0 : gfc_error ("end of implied-do loop at %L could not be "
18778 : : "simplified to a constant value", &end->where);
18779 : 0 : retval = false;
18780 : 0 : goto cleanup;
18781 : : }
18782 : 236 : if (!gfc_simplify_expr (step, 1)
18783 : 236 : || step->expr_type != EXPR_CONSTANT)
18784 : : {
18785 : 0 : gfc_error ("step of implied-do loop at %L could not be "
18786 : : "simplified to a constant value", &step->where);
18787 : 0 : retval = false;
18788 : 0 : goto cleanup;
18789 : : }
18790 : 236 : if (mpz_cmp_si (step->value.integer, 0) == 0)
18791 : : {
18792 : 1 : gfc_error ("step of implied-do loop at %L shall not be zero",
18793 : : &step->where);
18794 : 1 : retval = false;
18795 : 1 : goto cleanup;
18796 : : }
18797 : :
18798 : 235 : mpz_set (trip, end->value.integer);
18799 : 235 : mpz_sub (trip, trip, start->value.integer);
18800 : 235 : mpz_add (trip, trip, step->value.integer);
18801 : :
18802 : 235 : mpz_div (trip, trip, step->value.integer);
18803 : :
18804 : 235 : mpz_set (frame.value, start->value.integer);
18805 : :
18806 : 235 : frame.prev = iter_stack;
18807 : 235 : frame.variable = var->iter.var->symtree;
18808 : 235 : iter_stack = &frame;
18809 : :
18810 : 1124 : while (mpz_cmp_ui (trip, 0) > 0)
18811 : : {
18812 : 903 : if (!traverse_data_var (var->list, where))
18813 : : {
18814 : 14 : retval = false;
18815 : 14 : goto cleanup;
18816 : : }
18817 : :
18818 : 889 : e = gfc_copy_expr (var->expr);
18819 : 889 : if (!gfc_simplify_expr (e, 1))
18820 : : {
18821 : 0 : gfc_free_expr (e);
18822 : 0 : retval = false;
18823 : 0 : goto cleanup;
18824 : : }
18825 : :
18826 : 889 : mpz_add (frame.value, frame.value, step->value.integer);
18827 : :
18828 : 889 : mpz_sub_ui (trip, trip, 1);
18829 : : }
18830 : :
18831 : 221 : cleanup:
18832 : 236 : mpz_clear (frame.value);
18833 : 236 : mpz_clear (trip);
18834 : :
18835 : 236 : gfc_free_expr (start);
18836 : 236 : gfc_free_expr (end);
18837 : 236 : gfc_free_expr (step);
18838 : :
18839 : 236 : iter_stack = frame.prev;
18840 : 236 : return retval;
18841 : : }
18842 : :
18843 : :
18844 : : /* Type resolve variables in the variable list of a DATA statement. */
18845 : :
18846 : : static bool
18847 : 3397 : traverse_data_var (gfc_data_variable *var, locus *where)
18848 : : {
18849 : 3397 : bool t;
18850 : :
18851 : 7074 : for (; var; var = var->next)
18852 : : {
18853 : 3773 : if (var->expr == NULL)
18854 : 236 : t = traverse_data_list (var, where);
18855 : : else
18856 : 3537 : t = check_data_variable (var, where);
18857 : :
18858 : 3773 : if (!t)
18859 : : return false;
18860 : : }
18861 : :
18862 : : return true;
18863 : : }
18864 : :
18865 : :
18866 : : /* Resolve the expressions and iterators associated with a data statement.
18867 : : This is separate from the assignment checking because data lists should
18868 : : only be resolved once. */
18869 : :
18870 : : static bool
18871 : 2648 : resolve_data_variables (gfc_data_variable *d)
18872 : : {
18873 : 5667 : for (; d; d = d->next)
18874 : : {
18875 : 3024 : if (d->list == NULL)
18876 : : {
18877 : 2872 : if (!gfc_resolve_expr (d->expr))
18878 : : return false;
18879 : : }
18880 : : else
18881 : : {
18882 : 152 : if (!gfc_resolve_iterator (&d->iter, false, true))
18883 : : return false;
18884 : :
18885 : 149 : if (!resolve_data_variables (d->list))
18886 : : return false;
18887 : : }
18888 : : }
18889 : :
18890 : : return true;
18891 : : }
18892 : :
18893 : :
18894 : : /* Resolve a single DATA statement. We implement this by storing a pointer to
18895 : : the value list into static variables, and then recursively traversing the
18896 : : variables list, expanding iterators and such. */
18897 : :
18898 : : static void
18899 : 2499 : resolve_data (gfc_data *d)
18900 : : {
18901 : :
18902 : 2499 : if (!resolve_data_variables (d->var))
18903 : : return;
18904 : :
18905 : 2494 : values.vnode = d->value;
18906 : 2494 : if (d->value == NULL)
18907 : 0 : mpz_set_ui (values.left, 0);
18908 : : else
18909 : 2494 : mpz_set (values.left, d->value->repeat);
18910 : :
18911 : 2494 : if (!traverse_data_var (d->var, &d->where))
18912 : : return;
18913 : :
18914 : : /* At this point, we better not have any values left. */
18915 : :
18916 : 2412 : if (next_data_value ())
18917 : 0 : gfc_error ("DATA statement at %L has more values than variables",
18918 : : &d->where);
18919 : : }
18920 : :
18921 : :
18922 : : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
18923 : : accessed by host or use association, is a dummy argument to a pure function,
18924 : : is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
18925 : : is storage associated with any such variable, shall not be used in the
18926 : : following contexts: (clients of this function). */
18927 : :
18928 : : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
18929 : : procedure. Returns zero if assignment is OK, nonzero if there is a
18930 : : problem. */
18931 : : bool
18932 : 53531 : gfc_impure_variable (gfc_symbol *sym)
18933 : : {
18934 : 53531 : gfc_symbol *proc;
18935 : 53531 : gfc_namespace *ns;
18936 : :
18937 : 53531 : if (sym->attr.use_assoc || sym->attr.in_common)
18938 : : return 1;
18939 : :
18940 : : /* The namespace of a module procedure interface holds the arguments and
18941 : : symbols, and so the symbol namespace can be different to that of the
18942 : : procedure. */
18943 : 53010 : if (sym->ns != gfc_current_ns
18944 : 5672 : && gfc_current_ns->proc_name->abr_modproc_decl
18945 : 36 : && sym->ns->proc_name->attr.function
18946 : 12 : && sym->attr.result
18947 : 12 : && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
18948 : : return 0;
18949 : :
18950 : : /* Check if the symbol's ns is inside the pure procedure. */
18951 : 57491 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18952 : : {
18953 : 57212 : if (ns == sym->ns)
18954 : : break;
18955 : 5948 : if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
18956 : : return 1;
18957 : : }
18958 : :
18959 : 51543 : proc = sym->ns->proc_name;
18960 : 51543 : if (sym->attr.dummy
18961 : 5698 : && !sym->attr.value
18962 : 5576 : && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
18963 : 5381 : || proc->attr.function))
18964 : 675 : return 1;
18965 : :
18966 : : /* TODO: Sort out what can be storage associated, if anything, and include
18967 : : it here. In principle equivalences should be scanned but it does not
18968 : : seem to be possible to storage associate an impure variable this way. */
18969 : : return 0;
18970 : : }
18971 : :
18972 : :
18973 : : /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
18974 : : current namespace is inside a pure procedure. */
18975 : :
18976 : : bool
18977 : 2264281 : gfc_pure (gfc_symbol *sym)
18978 : : {
18979 : 2264281 : symbol_attribute attr;
18980 : 2264281 : gfc_namespace *ns;
18981 : :
18982 : 2264281 : if (sym == NULL)
18983 : : {
18984 : : /* Check if the current namespace or one of its parents
18985 : : belongs to a pure procedure. */
18986 : 3108365 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18987 : : {
18988 : 1835504 : sym = ns->proc_name;
18989 : 1835504 : if (sym == NULL)
18990 : : return 0;
18991 : 1834390 : attr = sym->attr;
18992 : 1834390 : if (attr.flavor == FL_PROCEDURE && attr.pure)
18993 : : return 1;
18994 : : }
18995 : : return 0;
18996 : : }
18997 : :
18998 : 983334 : attr = sym->attr;
18999 : :
19000 : 983334 : return attr.flavor == FL_PROCEDURE && attr.pure;
19001 : : }
19002 : :
19003 : :
19004 : : /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
19005 : : checks if the current namespace is implicitly pure. Note that this
19006 : : function returns false for a PURE procedure. */
19007 : :
19008 : : bool
19009 : 708742 : gfc_implicit_pure (gfc_symbol *sym)
19010 : : {
19011 : 708742 : gfc_namespace *ns;
19012 : :
19013 : 708742 : if (sym == NULL)
19014 : : {
19015 : : /* Check if the current procedure is implicit_pure. Walk up
19016 : : the procedure list until we find a procedure. */
19017 : 973975 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19018 : : {
19019 : 696375 : sym = ns->proc_name;
19020 : 696375 : if (sym == NULL)
19021 : : return 0;
19022 : :
19023 : 696302 : if (sym->attr.flavor == FL_PROCEDURE)
19024 : : break;
19025 : : }
19026 : : }
19027 : :
19028 : 431066 : return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
19029 : 735113 : && !sym->attr.pure;
19030 : : }
19031 : :
19032 : :
19033 : : void
19034 : 414613 : gfc_unset_implicit_pure (gfc_symbol *sym)
19035 : : {
19036 : 414613 : gfc_namespace *ns;
19037 : :
19038 : 414613 : if (sym == NULL)
19039 : : {
19040 : : /* Check if the current procedure is implicit_pure. Walk up
19041 : : the procedure list until we find a procedure. */
19042 : 676576 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19043 : : {
19044 : 418615 : sym = ns->proc_name;
19045 : 418615 : if (sym == NULL)
19046 : : return;
19047 : :
19048 : 417809 : if (sym->attr.flavor == FL_PROCEDURE)
19049 : : break;
19050 : : }
19051 : : }
19052 : :
19053 : 413807 : if (sym->attr.flavor == FL_PROCEDURE)
19054 : 147893 : sym->attr.implicit_pure = 0;
19055 : : else
19056 : 265914 : sym->attr.pure = 0;
19057 : : }
19058 : :
19059 : :
19060 : : /* Test whether the current procedure is elemental or not. */
19061 : :
19062 : : bool
19063 : 1317754 : gfc_elemental (gfc_symbol *sym)
19064 : : {
19065 : 1317754 : symbol_attribute attr;
19066 : :
19067 : 1317754 : if (sym == NULL)
19068 : 0 : sym = gfc_current_ns->proc_name;
19069 : 0 : if (sym == NULL)
19070 : : return 0;
19071 : 1317754 : attr = sym->attr;
19072 : :
19073 : 1317754 : return attr.flavor == FL_PROCEDURE && attr.elemental;
19074 : : }
19075 : :
19076 : :
19077 : : /* Warn about unused labels. */
19078 : :
19079 : : static void
19080 : 4581 : warn_unused_fortran_label (gfc_st_label *label)
19081 : : {
19082 : 4584 : if (label == NULL)
19083 : : return;
19084 : :
19085 : 4 : warn_unused_fortran_label (label->left);
19086 : :
19087 : 4 : if (label->defined == ST_LABEL_UNKNOWN)
19088 : : return;
19089 : :
19090 : 3 : switch (label->referenced)
19091 : : {
19092 : 1 : case ST_LABEL_UNKNOWN:
19093 : 1 : gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
19094 : : label->value, &label->where);
19095 : 1 : break;
19096 : :
19097 : 1 : case ST_LABEL_BAD_TARGET:
19098 : 1 : gfc_warning (OPT_Wunused_label,
19099 : : "Label %d at %L defined but cannot be used",
19100 : : label->value, &label->where);
19101 : 1 : break;
19102 : :
19103 : : default:
19104 : : break;
19105 : : }
19106 : :
19107 : 3 : warn_unused_fortran_label (label->right);
19108 : : }
19109 : :
19110 : :
19111 : : /* Returns the sequence type of a symbol or sequence. */
19112 : :
19113 : : static seq_type
19114 : 1076 : sequence_type (gfc_typespec ts)
19115 : : {
19116 : 1076 : seq_type result;
19117 : 1076 : gfc_component *c;
19118 : :
19119 : 1076 : switch (ts.type)
19120 : : {
19121 : 49 : case BT_DERIVED:
19122 : :
19123 : 49 : if (ts.u.derived->components == NULL)
19124 : : return SEQ_NONDEFAULT;
19125 : :
19126 : 49 : result = sequence_type (ts.u.derived->components->ts);
19127 : 103 : for (c = ts.u.derived->components->next; c; c = c->next)
19128 : 67 : if (sequence_type (c->ts) != result)
19129 : : return SEQ_MIXED;
19130 : :
19131 : : return result;
19132 : :
19133 : 129 : case BT_CHARACTER:
19134 : 129 : if (ts.kind != gfc_default_character_kind)
19135 : 0 : return SEQ_NONDEFAULT;
19136 : :
19137 : : return SEQ_CHARACTER;
19138 : :
19139 : 240 : case BT_INTEGER:
19140 : 240 : if (ts.kind != gfc_default_integer_kind)
19141 : 25 : return SEQ_NONDEFAULT;
19142 : :
19143 : : return SEQ_NUMERIC;
19144 : :
19145 : 559 : case BT_REAL:
19146 : 559 : if (!(ts.kind == gfc_default_real_kind
19147 : 269 : || ts.kind == gfc_default_double_kind))
19148 : 0 : return SEQ_NONDEFAULT;
19149 : :
19150 : : return SEQ_NUMERIC;
19151 : :
19152 : 81 : case BT_COMPLEX:
19153 : 81 : if (ts.kind != gfc_default_complex_kind)
19154 : 48 : return SEQ_NONDEFAULT;
19155 : :
19156 : : return SEQ_NUMERIC;
19157 : :
19158 : 17 : case BT_LOGICAL:
19159 : 17 : if (ts.kind != gfc_default_logical_kind)
19160 : 0 : return SEQ_NONDEFAULT;
19161 : :
19162 : : return SEQ_NUMERIC;
19163 : :
19164 : : default:
19165 : : return SEQ_NONDEFAULT;
19166 : : }
19167 : : }
19168 : :
19169 : :
19170 : : /* Resolve derived type EQUIVALENCE object. */
19171 : :
19172 : : static bool
19173 : 80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
19174 : : {
19175 : 80 : gfc_component *c = derived->components;
19176 : :
19177 : 80 : if (!derived)
19178 : : return true;
19179 : :
19180 : : /* Shall not be an object of nonsequence derived type. */
19181 : 80 : if (!derived->attr.sequence)
19182 : : {
19183 : 0 : gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
19184 : : "attribute to be an EQUIVALENCE object", sym->name,
19185 : : &e->where);
19186 : 0 : return false;
19187 : : }
19188 : :
19189 : : /* Shall not have allocatable components. */
19190 : 80 : if (derived->attr.alloc_comp)
19191 : : {
19192 : 1 : gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
19193 : : "components to be an EQUIVALENCE object",sym->name,
19194 : : &e->where);
19195 : 1 : return false;
19196 : : }
19197 : :
19198 : 79 : if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
19199 : : {
19200 : 1 : gfc_error ("Derived type variable %qs at %L with default "
19201 : : "initialization cannot be in EQUIVALENCE with a variable "
19202 : : "in COMMON", sym->name, &e->where);
19203 : 1 : return false;
19204 : : }
19205 : :
19206 : 245 : for (; c ; c = c->next)
19207 : : {
19208 : 167 : if (gfc_bt_struct (c->ts.type)
19209 : 167 : && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
19210 : : return false;
19211 : :
19212 : : /* Shall not be an object of sequence derived type containing a pointer
19213 : : in the structure. */
19214 : 167 : if (c->attr.pointer)
19215 : : {
19216 : 0 : gfc_error ("Derived type variable %qs at %L with pointer "
19217 : : "component(s) cannot be an EQUIVALENCE object",
19218 : : sym->name, &e->where);
19219 : 0 : return false;
19220 : : }
19221 : : }
19222 : : return true;
19223 : : }
19224 : :
19225 : :
19226 : : /* Resolve equivalence object.
19227 : : An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
19228 : : an allocatable array, an object of nonsequence derived type, an object of
19229 : : sequence derived type containing a pointer at any level of component
19230 : : selection, an automatic object, a function name, an entry name, a result
19231 : : name, a named constant, a structure component, or a subobject of any of
19232 : : the preceding objects. A substring shall not have length zero. A
19233 : : derived type shall not have components with default initialization nor
19234 : : shall two objects of an equivalence group be initialized.
19235 : : Either all or none of the objects shall have an protected attribute.
19236 : : The simple constraints are done in symbol.cc(check_conflict) and the rest
19237 : : are implemented here. */
19238 : :
19239 : : static void
19240 : 1565 : resolve_equivalence (gfc_equiv *eq)
19241 : : {
19242 : 1565 : gfc_symbol *sym;
19243 : 1565 : gfc_symbol *first_sym;
19244 : 1565 : gfc_expr *e;
19245 : 1565 : gfc_ref *r;
19246 : 1565 : locus *last_where = NULL;
19247 : 1565 : seq_type eq_type, last_eq_type;
19248 : 1565 : gfc_typespec *last_ts;
19249 : 1565 : int object, cnt_protected;
19250 : 1565 : const char *msg;
19251 : :
19252 : 1565 : last_ts = &eq->expr->symtree->n.sym->ts;
19253 : :
19254 : 1565 : first_sym = eq->expr->symtree->n.sym;
19255 : :
19256 : 1565 : cnt_protected = 0;
19257 : :
19258 : 4727 : for (object = 1; eq; eq = eq->eq, object++)
19259 : : {
19260 : 3171 : e = eq->expr;
19261 : :
19262 : 3171 : e->ts = e->symtree->n.sym->ts;
19263 : : /* match_varspec might not know yet if it is seeing
19264 : : array reference or substring reference, as it doesn't
19265 : : know the types. */
19266 : 3171 : if (e->ref && e->ref->type == REF_ARRAY)
19267 : : {
19268 : 2152 : gfc_ref *ref = e->ref;
19269 : 2152 : sym = e->symtree->n.sym;
19270 : :
19271 : 2152 : if (sym->attr.dimension)
19272 : : {
19273 : 1855 : ref->u.ar.as = sym->as;
19274 : 1855 : ref = ref->next;
19275 : : }
19276 : :
19277 : : /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
19278 : 2152 : if (e->ts.type == BT_CHARACTER
19279 : 592 : && ref
19280 : 371 : && ref->type == REF_ARRAY
19281 : 371 : && ref->u.ar.dimen == 1
19282 : 371 : && ref->u.ar.dimen_type[0] == DIMEN_RANGE
19283 : 371 : && ref->u.ar.stride[0] == NULL)
19284 : : {
19285 : 370 : gfc_expr *start = ref->u.ar.start[0];
19286 : 370 : gfc_expr *end = ref->u.ar.end[0];
19287 : 370 : void *mem = NULL;
19288 : :
19289 : : /* Optimize away the (:) reference. */
19290 : 370 : if (start == NULL && end == NULL)
19291 : : {
19292 : 9 : if (e->ref == ref)
19293 : 0 : e->ref = ref->next;
19294 : : else
19295 : 9 : e->ref->next = ref->next;
19296 : : mem = ref;
19297 : : }
19298 : : else
19299 : : {
19300 : 361 : ref->type = REF_SUBSTRING;
19301 : 361 : if (start == NULL)
19302 : 9 : start = gfc_get_int_expr (gfc_charlen_int_kind,
19303 : : NULL, 1);
19304 : 361 : ref->u.ss.start = start;
19305 : 361 : if (end == NULL && e->ts.u.cl)
19306 : 27 : end = gfc_copy_expr (e->ts.u.cl->length);
19307 : 361 : ref->u.ss.end = end;
19308 : 361 : ref->u.ss.length = e->ts.u.cl;
19309 : 361 : e->ts.u.cl = NULL;
19310 : : }
19311 : 370 : ref = ref->next;
19312 : 370 : free (mem);
19313 : : }
19314 : :
19315 : : /* Any further ref is an error. */
19316 : 1930 : if (ref)
19317 : : {
19318 : 1 : gcc_assert (ref->type == REF_ARRAY);
19319 : 1 : gfc_error ("Syntax error in EQUIVALENCE statement at %L",
19320 : : &ref->u.ar.where);
19321 : 1 : continue;
19322 : : }
19323 : : }
19324 : :
19325 : 3170 : if (!gfc_resolve_expr (e))
19326 : 2 : continue;
19327 : :
19328 : 3168 : sym = e->symtree->n.sym;
19329 : :
19330 : 3168 : if (sym->attr.is_protected)
19331 : 2 : cnt_protected++;
19332 : 3168 : if (cnt_protected > 0 && cnt_protected != object)
19333 : : {
19334 : 2 : gfc_error ("Either all or none of the objects in the "
19335 : : "EQUIVALENCE set at %L shall have the "
19336 : : "PROTECTED attribute",
19337 : : &e->where);
19338 : 2 : break;
19339 : : }
19340 : :
19341 : : /* Shall not equivalence common block variables in a PURE procedure. */
19342 : 3166 : if (sym->ns->proc_name
19343 : 3150 : && sym->ns->proc_name->attr.pure
19344 : 7 : && sym->attr.in_common)
19345 : : {
19346 : : /* Need to check for symbols that may have entered the pure
19347 : : procedure via a USE statement. */
19348 : 7 : bool saw_sym = false;
19349 : 7 : if (sym->ns->use_stmts)
19350 : : {
19351 : 6 : gfc_use_rename *r;
19352 : 10 : for (r = sym->ns->use_stmts->rename; r; r = r->next)
19353 : 4 : if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
19354 : : }
19355 : : else
19356 : : saw_sym = true;
19357 : :
19358 : 6 : if (saw_sym)
19359 : 3 : gfc_error ("COMMON block member %qs at %L cannot be an "
19360 : : "EQUIVALENCE object in the pure procedure %qs",
19361 : : sym->name, &e->where, sym->ns->proc_name->name);
19362 : : break;
19363 : : }
19364 : :
19365 : : /* Shall not be a named constant. */
19366 : 3159 : if (e->expr_type == EXPR_CONSTANT)
19367 : : {
19368 : 0 : gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
19369 : : "object", sym->name, &e->where);
19370 : 0 : continue;
19371 : : }
19372 : :
19373 : 3161 : if (e->ts.type == BT_DERIVED
19374 : 3159 : && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
19375 : 2 : continue;
19376 : :
19377 : : /* Check that the types correspond correctly:
19378 : : Note 5.28:
19379 : : A numeric sequence structure may be equivalenced to another sequence
19380 : : structure, an object of default integer type, default real type, double
19381 : : precision real type, default logical type such that components of the
19382 : : structure ultimately only become associated to objects of the same
19383 : : kind. A character sequence structure may be equivalenced to an object
19384 : : of default character kind or another character sequence structure.
19385 : : Other objects may be equivalenced only to objects of the same type and
19386 : : kind parameters. */
19387 : :
19388 : : /* Identical types are unconditionally OK. */
19389 : 3157 : if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
19390 : 2677 : goto identical_types;
19391 : :
19392 : 480 : last_eq_type = sequence_type (*last_ts);
19393 : 480 : eq_type = sequence_type (sym->ts);
19394 : :
19395 : : /* Since the pair of objects is not of the same type, mixed or
19396 : : non-default sequences can be rejected. */
19397 : :
19398 : 480 : msg = G_("Sequence %s with mixed components in EQUIVALENCE "
19399 : : "statement at %L with different type objects");
19400 : 481 : if ((object ==2
19401 : 480 : && last_eq_type == SEQ_MIXED
19402 : 7 : && last_where
19403 : 7 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19404 : 486 : || (eq_type == SEQ_MIXED
19405 : 6 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19406 : 1 : continue;
19407 : :
19408 : 479 : msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
19409 : : "statement at %L with objects of different type");
19410 : 483 : if ((object ==2
19411 : 479 : && last_eq_type == SEQ_NONDEFAULT
19412 : 50 : && last_where
19413 : 49 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19414 : 525 : || (eq_type == SEQ_NONDEFAULT
19415 : 24 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19416 : 4 : continue;
19417 : :
19418 : 475 : msg = G_("Non-CHARACTER object %qs in default CHARACTER "
19419 : : "EQUIVALENCE statement at %L");
19420 : 479 : if (last_eq_type == SEQ_CHARACTER
19421 : 475 : && eq_type != SEQ_CHARACTER
19422 : 475 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19423 : 4 : continue;
19424 : :
19425 : 471 : msg = G_("Non-NUMERIC object %qs in default NUMERIC "
19426 : : "EQUIVALENCE statement at %L");
19427 : 473 : if (last_eq_type == SEQ_NUMERIC
19428 : 471 : && eq_type != SEQ_NUMERIC
19429 : 471 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19430 : 2 : continue;
19431 : :
19432 : 3146 : identical_types:
19433 : :
19434 : 3146 : last_ts =&sym->ts;
19435 : 3146 : last_where = &e->where;
19436 : :
19437 : 3146 : if (!e->ref)
19438 : 1003 : continue;
19439 : :
19440 : : /* Shall not be an automatic array. */
19441 : 2143 : if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
19442 : : {
19443 : 3 : gfc_error ("Array %qs at %L with non-constant bounds cannot be "
19444 : : "an EQUIVALENCE object", sym->name, &e->where);
19445 : 3 : continue;
19446 : : }
19447 : :
19448 : 2140 : r = e->ref;
19449 : 4326 : while (r)
19450 : : {
19451 : : /* Shall not be a structure component. */
19452 : 2187 : if (r->type == REF_COMPONENT)
19453 : : {
19454 : 0 : gfc_error ("Structure component %qs at %L cannot be an "
19455 : : "EQUIVALENCE object",
19456 : 0 : r->u.c.component->name, &e->where);
19457 : 0 : break;
19458 : : }
19459 : :
19460 : : /* A substring shall not have length zero. */
19461 : 2187 : if (r->type == REF_SUBSTRING)
19462 : : {
19463 : 341 : if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
19464 : : {
19465 : 1 : gfc_error ("Substring at %L has length zero",
19466 : : &r->u.ss.start->where);
19467 : 1 : break;
19468 : : }
19469 : : }
19470 : 2186 : r = r->next;
19471 : : }
19472 : : }
19473 : 1565 : }
19474 : :
19475 : :
19476 : : /* Function called by resolve_fntype to flag other symbols used in the
19477 : : length type parameter specification of function results. */
19478 : :
19479 : : static bool
19480 : 4088 : flag_fn_result_spec (gfc_expr *expr,
19481 : : gfc_symbol *sym,
19482 : : int *f ATTRIBUTE_UNUSED)
19483 : : {
19484 : 4088 : gfc_namespace *ns;
19485 : 4088 : gfc_symbol *s;
19486 : :
19487 : 4088 : if (expr->expr_type == EXPR_VARIABLE)
19488 : : {
19489 : 1378 : s = expr->symtree->n.sym;
19490 : 2153 : for (ns = s->ns; ns; ns = ns->parent)
19491 : 2153 : if (!ns->parent)
19492 : : break;
19493 : :
19494 : 1378 : if (sym == s)
19495 : : {
19496 : 1 : gfc_error ("Self reference in character length expression "
19497 : : "for %qs at %L", sym->name, &expr->where);
19498 : 1 : return true;
19499 : : }
19500 : :
19501 : 1377 : if (!s->fn_result_spec
19502 : 1377 : && s->attr.flavor == FL_PARAMETER)
19503 : : {
19504 : : /* Function contained in a module.... */
19505 : 63 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
19506 : : {
19507 : 32 : gfc_symtree *st;
19508 : 32 : s->fn_result_spec = 1;
19509 : : /* Make sure that this symbol is translated as a module
19510 : : variable. */
19511 : 32 : st = gfc_get_unique_symtree (ns);
19512 : 32 : st->n.sym = s;
19513 : 32 : s->refs++;
19514 : 32 : }
19515 : : /* ... which is use associated and called. */
19516 : 31 : else if (s->attr.use_assoc || s->attr.used_in_submodule
19517 : 0 : ||
19518 : : /* External function matched with an interface. */
19519 : 0 : (s->ns->proc_name
19520 : 0 : && ((s->ns == ns
19521 : 0 : && s->ns->proc_name->attr.if_source == IFSRC_DECL)
19522 : 0 : || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
19523 : 0 : && s->ns->proc_name->attr.function))
19524 : 31 : s->fn_result_spec = 1;
19525 : : }
19526 : : }
19527 : : return false;
19528 : : }
19529 : :
19530 : :
19531 : : /* Resolve function and ENTRY types, issue diagnostics if needed. */
19532 : :
19533 : : static void
19534 : 335623 : resolve_fntype (gfc_namespace *ns)
19535 : : {
19536 : 335623 : gfc_entry_list *el;
19537 : 335623 : gfc_symbol *sym;
19538 : :
19539 : 335623 : if (ns->proc_name == NULL || !ns->proc_name->attr.function)
19540 : : return;
19541 : :
19542 : : /* If there are any entries, ns->proc_name is the entry master
19543 : : synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
19544 : 175480 : if (ns->entries)
19545 : 564 : sym = ns->entries->sym;
19546 : : else
19547 : : sym = ns->proc_name;
19548 : 175480 : if (sym->result == sym
19549 : 142882 : && sym->ts.type == BT_UNKNOWN
19550 : 6 : && !gfc_set_default_type (sym, 0, NULL)
19551 : 175484 : && !sym->attr.untyped)
19552 : : {
19553 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type",
19554 : : sym->name, &sym->declared_at);
19555 : 3 : sym->attr.untyped = 1;
19556 : : }
19557 : :
19558 : 13036 : if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
19559 : 1651 : && !sym->attr.contained
19560 : 281 : && !gfc_check_symbol_access (sym->ts.u.derived)
19561 : 175480 : && gfc_check_symbol_access (sym))
19562 : : {
19563 : 0 : gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
19564 : : "%L of PRIVATE type %qs", sym->name,
19565 : 0 : &sym->declared_at, sym->ts.u.derived->name);
19566 : : }
19567 : :
19568 : 175480 : if (ns->entries)
19569 : 1189 : for (el = ns->entries->next; el; el = el->next)
19570 : : {
19571 : 625 : if (el->sym->result == el->sym
19572 : 413 : && el->sym->ts.type == BT_UNKNOWN
19573 : 2 : && !gfc_set_default_type (el->sym, 0, NULL)
19574 : 627 : && !el->sym->attr.untyped)
19575 : : {
19576 : 2 : gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
19577 : : el->sym->name, &el->sym->declared_at);
19578 : 2 : el->sym->attr.untyped = 1;
19579 : : }
19580 : : }
19581 : :
19582 : 175480 : if (sym->ts.type == BT_CHARACTER
19583 : 6649 : && sym->ts.u.cl->length
19584 : 1740 : && sym->ts.u.cl->length->ts.type == BT_INTEGER)
19585 : 1735 : gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
19586 : : }
19587 : :
19588 : :
19589 : : /* 12.3.2.1.1 Defined operators. */
19590 : :
19591 : : static bool
19592 : 447 : check_uop_procedure (gfc_symbol *sym, locus where)
19593 : : {
19594 : 447 : gfc_formal_arglist *formal;
19595 : :
19596 : 447 : if (!sym->attr.function)
19597 : : {
19598 : 4 : gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
19599 : : sym->name, &where);
19600 : 4 : return false;
19601 : : }
19602 : :
19603 : 443 : if (sym->ts.type == BT_CHARACTER
19604 : 15 : && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
19605 : 2 : && !(sym->result && ((sym->result->ts.u.cl
19606 : 2 : && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
19607 : : {
19608 : 2 : gfc_error ("User operator procedure %qs at %L cannot be assumed "
19609 : : "character length", sym->name, &where);
19610 : 2 : return false;
19611 : : }
19612 : :
19613 : 441 : formal = gfc_sym_get_dummy_args (sym);
19614 : 441 : if (!formal || !formal->sym)
19615 : : {
19616 : 1 : gfc_error ("User operator procedure %qs at %L must have at least "
19617 : : "one argument", sym->name, &where);
19618 : 1 : return false;
19619 : : }
19620 : :
19621 : 440 : if (formal->sym->attr.intent != INTENT_IN)
19622 : : {
19623 : 0 : gfc_error ("First argument of operator interface at %L must be "
19624 : : "INTENT(IN)", &where);
19625 : 0 : return false;
19626 : : }
19627 : :
19628 : 440 : if (formal->sym->attr.optional)
19629 : : {
19630 : 0 : gfc_error ("First argument of operator interface at %L cannot be "
19631 : : "optional", &where);
19632 : 0 : return false;
19633 : : }
19634 : :
19635 : 440 : formal = formal->next;
19636 : 440 : if (!formal || !formal->sym)
19637 : : return true;
19638 : :
19639 : 295 : if (formal->sym->attr.intent != INTENT_IN)
19640 : : {
19641 : 0 : gfc_error ("Second argument of operator interface at %L must be "
19642 : : "INTENT(IN)", &where);
19643 : 0 : return false;
19644 : : }
19645 : :
19646 : 295 : if (formal->sym->attr.optional)
19647 : : {
19648 : 1 : gfc_error ("Second argument of operator interface at %L cannot be "
19649 : : "optional", &where);
19650 : 1 : return false;
19651 : : }
19652 : :
19653 : 294 : if (formal->next)
19654 : : {
19655 : 2 : gfc_error ("Operator interface at %L must have, at most, two "
19656 : : "arguments", &where);
19657 : 2 : return false;
19658 : : }
19659 : :
19660 : : return true;
19661 : : }
19662 : :
19663 : : static void
19664 : 336379 : gfc_resolve_uops (gfc_symtree *symtree)
19665 : : {
19666 : 336379 : gfc_interface *itr;
19667 : :
19668 : 336379 : if (symtree == NULL)
19669 : : return;
19670 : :
19671 : 378 : gfc_resolve_uops (symtree->left);
19672 : 378 : gfc_resolve_uops (symtree->right);
19673 : :
19674 : 771 : for (itr = symtree->n.uop->op; itr; itr = itr->next)
19675 : 393 : check_uop_procedure (itr->sym, itr->sym->declared_at);
19676 : : }
19677 : :
19678 : :
19679 : : /* Examine all of the expressions associated with a program unit,
19680 : : assign types to all intermediate expressions, make sure that all
19681 : : assignments are to compatible types and figure out which names
19682 : : refer to which functions or subroutines. It doesn't check code
19683 : : block, which is handled by gfc_resolve_code. */
19684 : :
19685 : : static void
19686 : 338089 : resolve_types (gfc_namespace *ns)
19687 : : {
19688 : 338089 : gfc_namespace *n;
19689 : 338089 : gfc_charlen *cl;
19690 : 338089 : gfc_data *d;
19691 : 338089 : gfc_equiv *eq;
19692 : 338089 : gfc_namespace* old_ns = gfc_current_ns;
19693 : 338089 : bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
19694 : :
19695 : 338089 : if (ns->types_resolved)
19696 : : return;
19697 : :
19698 : : /* Check that all IMPLICIT types are ok. */
19699 : 335624 : if (!ns->seen_implicit_none)
19700 : : {
19701 : : unsigned letter;
19702 : 8447167 : for (letter = 0; letter != GFC_LETTERS; ++letter)
19703 : 8134309 : if (ns->set_flag[letter]
19704 : 8134309 : && !resolve_typespec_used (&ns->default_type[letter],
19705 : : &ns->implicit_loc[letter], NULL))
19706 : : return;
19707 : : }
19708 : :
19709 : 335623 : gfc_current_ns = ns;
19710 : :
19711 : 335623 : resolve_entries (ns);
19712 : :
19713 : 335623 : resolve_common_vars (&ns->blank_common, false);
19714 : 335623 : resolve_common_blocks (ns->common_root);
19715 : :
19716 : 335623 : resolve_contained_functions (ns);
19717 : :
19718 : 335623 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
19719 : 287241 : && ns->proc_name->attr.if_source == IFSRC_IFBODY)
19720 : 187890 : gfc_resolve_formal_arglist (ns->proc_name);
19721 : :
19722 : 335623 : gfc_traverse_ns (ns, resolve_bind_c_derived_types);
19723 : :
19724 : 428018 : for (cl = ns->cl_list; cl; cl = cl->next)
19725 : 92395 : resolve_charlen (cl);
19726 : :
19727 : 335623 : gfc_traverse_ns (ns, resolve_symbol);
19728 : :
19729 : 335623 : resolve_fntype (ns);
19730 : :
19731 : 381703 : for (n = ns->contained; n; n = n->sibling)
19732 : : {
19733 : : /* Exclude final wrappers with the test for the artificial attribute. */
19734 : 46080 : if (gfc_pure (ns->proc_name)
19735 : 5 : && !gfc_pure (n->proc_name)
19736 : 46080 : && !n->proc_name->attr.artificial)
19737 : 0 : gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
19738 : : "also be PURE", n->proc_name->name,
19739 : : &n->proc_name->declared_at);
19740 : :
19741 : 46080 : resolve_types (n);
19742 : : }
19743 : :
19744 : 335623 : forall_flag = 0;
19745 : 335623 : gfc_do_concurrent_flag = 0;
19746 : 335623 : gfc_check_interfaces (ns);
19747 : :
19748 : 335623 : gfc_traverse_ns (ns, resolve_values);
19749 : :
19750 : 335623 : if (ns->save_all || (!flag_automatic && !recursive))
19751 : 297 : gfc_save_all (ns);
19752 : :
19753 : 335623 : iter_stack = NULL;
19754 : 338122 : for (d = ns->data; d; d = d->next)
19755 : 2499 : resolve_data (d);
19756 : :
19757 : 335623 : iter_stack = NULL;
19758 : 335623 : gfc_traverse_ns (ns, gfc_formalize_init_value);
19759 : :
19760 : 335623 : gfc_traverse_ns (ns, gfc_verify_binding_labels);
19761 : :
19762 : 337188 : for (eq = ns->equiv; eq; eq = eq->next)
19763 : 1565 : resolve_equivalence (eq);
19764 : :
19765 : : /* Warn about unused labels. */
19766 : 335623 : if (warn_unused_label)
19767 : 4577 : warn_unused_fortran_label (ns->st_labels);
19768 : :
19769 : 335623 : gfc_resolve_uops (ns->uop_root);
19770 : :
19771 : 335623 : gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
19772 : :
19773 : 335623 : gfc_resolve_omp_declare (ns);
19774 : :
19775 : 335623 : gfc_resolve_omp_udrs (ns->omp_udr_root);
19776 : :
19777 : 335623 : ns->types_resolved = 1;
19778 : :
19779 : 335623 : gfc_current_ns = old_ns;
19780 : : }
19781 : :
19782 : :
19783 : : /* Call gfc_resolve_code recursively. */
19784 : :
19785 : : static void
19786 : 338144 : resolve_codes (gfc_namespace *ns)
19787 : : {
19788 : 338144 : gfc_namespace *n;
19789 : 338144 : bitmap_obstack old_obstack;
19790 : :
19791 : 338144 : if (ns->resolved == 1)
19792 : 13063 : return;
19793 : :
19794 : 371216 : for (n = ns->contained; n; n = n->sibling)
19795 : 46135 : resolve_codes (n);
19796 : :
19797 : 325081 : gfc_current_ns = ns;
19798 : :
19799 : : /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
19800 : 325081 : if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
19801 : 313476 : cs_base = NULL;
19802 : :
19803 : : /* Set to an out of range value. */
19804 : 325081 : current_entry_id = -1;
19805 : :
19806 : 325081 : old_obstack = labels_obstack;
19807 : 325081 : bitmap_obstack_initialize (&labels_obstack);
19808 : :
19809 : 325081 : gfc_resolve_oacc_declare (ns);
19810 : 325081 : gfc_resolve_oacc_routines (ns);
19811 : 325081 : gfc_resolve_omp_local_vars (ns);
19812 : 325081 : if (ns->omp_allocate)
19813 : 55 : gfc_resolve_omp_allocate (ns, ns->omp_allocate);
19814 : 325081 : gfc_resolve_code (ns->code, ns);
19815 : :
19816 : 325080 : bitmap_obstack_release (&labels_obstack);
19817 : 325080 : labels_obstack = old_obstack;
19818 : : }
19819 : :
19820 : :
19821 : : /* This function is called after a complete program unit has been compiled.
19822 : : Its purpose is to examine all of the expressions associated with a program
19823 : : unit, assign types to all intermediate expressions, make sure that all
19824 : : assignments are to compatible types and figure out which names refer to
19825 : : which functions or subroutines. */
19826 : :
19827 : : void
19828 : 296542 : gfc_resolve (gfc_namespace *ns)
19829 : : {
19830 : 296542 : gfc_namespace *old_ns;
19831 : 296542 : code_stack *old_cs_base;
19832 : 296542 : struct gfc_omp_saved_state old_omp_state;
19833 : :
19834 : 296542 : if (ns->resolved)
19835 : 4533 : return;
19836 : :
19837 : 292009 : ns->resolved = -1;
19838 : 292009 : old_ns = gfc_current_ns;
19839 : 292009 : old_cs_base = cs_base;
19840 : :
19841 : : /* As gfc_resolve can be called during resolution of an OpenMP construct
19842 : : body, we should clear any state associated to it, so that say NS's
19843 : : DO loops are not interpreted as OpenMP loops. */
19844 : 292009 : if (!ns->construct_entities)
19845 : 280404 : gfc_omp_save_and_clear_state (&old_omp_state);
19846 : :
19847 : 292009 : resolve_types (ns);
19848 : 292009 : component_assignment_level = 0;
19849 : 292009 : resolve_codes (ns);
19850 : :
19851 : 292008 : if (ns->omp_assumes)
19852 : 13 : gfc_resolve_omp_assumptions (ns->omp_assumes);
19853 : :
19854 : 292008 : gfc_current_ns = old_ns;
19855 : 292008 : cs_base = old_cs_base;
19856 : 292008 : ns->resolved = 1;
19857 : :
19858 : 292008 : gfc_run_passes (ns);
19859 : :
19860 : 292008 : if (!ns->construct_entities)
19861 : 280403 : gfc_omp_restore_state (&old_omp_state);
19862 : : }
|