Line data Source code
1 : /* Perform type resolution on the various structures.
2 : Copyright (C) 2001-2026 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 : /* The dummy whose character length or array bounds are currently being
86 : resolved as a specification expression. */
87 : static gfc_symbol *specification_expr_symbol = NULL;
88 :
89 : /* The id of the last entry seen. */
90 : static int current_entry_id;
91 :
92 : /* We use bitmaps to determine if a branch target is valid. */
93 : static bitmap_obstack labels_obstack;
94 :
95 : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
96 : static bool inquiry_argument = false;
97 :
98 : static bool
99 464 : entry_dummy_seen_p (gfc_symbol *sym)
100 : {
101 464 : gfc_entry_list *entry;
102 464 : gfc_formal_arglist *formal;
103 :
104 464 : gcc_checking_assert (sym->attr.dummy && sym->ns == gfc_current_ns);
105 :
106 464 : for (entry = gfc_current_ns->entries;
107 471 : entry && entry->id <= current_entry_id;
108 7 : entry = entry->next)
109 765 : for (formal = entry->sym->formal; formal; formal = formal->next)
110 758 : if (formal->sym && sym->name == formal->sym->name)
111 : return true;
112 :
113 : return false;
114 : }
115 :
116 :
117 : /* Is the symbol host associated? */
118 : static bool
119 52587 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
120 : {
121 57107 : for (ns = ns->parent; ns; ns = ns->parent)
122 : {
123 4771 : if (sym->ns == ns)
124 : return true;
125 : }
126 :
127 : return false;
128 : }
129 :
130 : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
131 : an ABSTRACT derived-type. If where is not NULL, an error message with that
132 : locus is printed, optionally using name. */
133 :
134 : static bool
135 1545073 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
136 : {
137 1545073 : if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
138 : {
139 5 : if (where)
140 : {
141 5 : if (name)
142 4 : gfc_error ("%qs at %L is of the ABSTRACT type %qs",
143 : name, where, ts->u.derived->name);
144 : else
145 1 : gfc_error ("ABSTRACT type %qs used at %L",
146 : ts->u.derived->name, where);
147 : }
148 :
149 5 : return false;
150 : }
151 :
152 : return true;
153 : }
154 :
155 :
156 : static bool
157 5595 : check_proc_interface (gfc_symbol *ifc, locus *where)
158 : {
159 : /* Several checks for F08:C1216. */
160 5595 : if (ifc->attr.procedure)
161 : {
162 2 : gfc_error ("Interface %qs at %L is declared "
163 : "in a later PROCEDURE statement", ifc->name, where);
164 2 : return false;
165 : }
166 5593 : if (ifc->generic)
167 : {
168 : /* For generic interfaces, check if there is
169 : a specific procedure with the same name. */
170 : gfc_interface *gen = ifc->generic;
171 12 : while (gen && strcmp (gen->sym->name, ifc->name) != 0)
172 5 : gen = gen->next;
173 7 : if (!gen)
174 : {
175 4 : gfc_error ("Interface %qs at %L may not be generic",
176 : ifc->name, where);
177 4 : return false;
178 : }
179 : }
180 5589 : if (ifc->attr.proc == PROC_ST_FUNCTION)
181 : {
182 4 : gfc_error ("Interface %qs at %L may not be a statement function",
183 : ifc->name, where);
184 4 : return false;
185 : }
186 5585 : if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
187 5585 : || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
188 17 : ifc->attr.intrinsic = 1;
189 5585 : if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
190 : {
191 3 : gfc_error ("Intrinsic procedure %qs not allowed in "
192 : "PROCEDURE statement at %L", ifc->name, where);
193 3 : return false;
194 : }
195 5582 : if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
196 : {
197 7 : gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
198 7 : return false;
199 : }
200 : return true;
201 : }
202 :
203 :
204 : static void resolve_symbol (gfc_symbol *sym);
205 :
206 :
207 : /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
208 :
209 : static bool
210 2079 : resolve_procedure_interface (gfc_symbol *sym)
211 : {
212 2079 : gfc_symbol *ifc = sym->ts.interface;
213 :
214 2079 : if (!ifc)
215 : return true;
216 :
217 1919 : if (ifc == sym)
218 : {
219 2 : gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
220 : sym->name, &sym->declared_at);
221 2 : return false;
222 : }
223 1917 : if (!check_proc_interface (ifc, &sym->declared_at))
224 : return false;
225 :
226 1908 : if (ifc->attr.if_source || ifc->attr.intrinsic)
227 : {
228 : /* Resolve interface and copy attributes. */
229 1629 : resolve_symbol (ifc);
230 1629 : if (ifc->attr.intrinsic)
231 14 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
232 :
233 1629 : if (ifc->result)
234 : {
235 742 : sym->ts = ifc->result->ts;
236 742 : sym->attr.allocatable = ifc->result->attr.allocatable;
237 742 : sym->attr.pointer = ifc->result->attr.pointer;
238 742 : sym->attr.dimension = ifc->result->attr.dimension;
239 742 : sym->attr.class_ok = ifc->result->attr.class_ok;
240 742 : sym->as = gfc_copy_array_spec (ifc->result->as);
241 742 : sym->result = sym;
242 : }
243 : else
244 : {
245 887 : sym->ts = ifc->ts;
246 887 : sym->attr.allocatable = ifc->attr.allocatable;
247 887 : sym->attr.pointer = ifc->attr.pointer;
248 887 : sym->attr.dimension = ifc->attr.dimension;
249 887 : sym->attr.class_ok = ifc->attr.class_ok;
250 887 : sym->as = gfc_copy_array_spec (ifc->as);
251 : }
252 1629 : sym->ts.interface = ifc;
253 1629 : sym->attr.function = ifc->attr.function;
254 1629 : sym->attr.subroutine = ifc->attr.subroutine;
255 :
256 1629 : sym->attr.pure = ifc->attr.pure;
257 1629 : sym->attr.elemental = ifc->attr.elemental;
258 1629 : sym->attr.contiguous = ifc->attr.contiguous;
259 1629 : sym->attr.recursive = ifc->attr.recursive;
260 1629 : sym->attr.always_explicit = ifc->attr.always_explicit;
261 1629 : sym->attr.ext_attr |= ifc->attr.ext_attr;
262 1629 : sym->attr.is_bind_c = ifc->attr.is_bind_c;
263 : /* Copy char length. */
264 1629 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
265 : {
266 45 : sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
267 45 : if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
268 53 : && !gfc_resolve_expr (sym->ts.u.cl->length))
269 : return false;
270 : }
271 : }
272 :
273 : return true;
274 : }
275 :
276 :
277 : /* Resolve types of formal argument lists. These have to be done early so that
278 : the formal argument lists of module procedures can be copied to the
279 : containing module before the individual procedures are resolved
280 : individually. We also resolve argument lists of procedures in interface
281 : blocks because they are self-contained scoping units.
282 :
283 : Since a dummy argument cannot be a non-dummy procedure, the only
284 : resort left for untyped names are the IMPLICIT types. */
285 :
286 : void
287 524333 : gfc_resolve_formal_arglist (gfc_symbol *proc)
288 : {
289 524333 : gfc_formal_arglist *f;
290 524333 : gfc_symbol *sym;
291 524333 : bool saved_specification_expr;
292 524333 : int i;
293 :
294 524333 : if (proc->result != NULL)
295 327358 : sym = proc->result;
296 : else
297 : sym = proc;
298 :
299 524333 : if (gfc_elemental (proc)
300 362092 : || sym->attr.pointer || sym->attr.allocatable
301 874289 : || (sym->as && sym->as->rank != 0))
302 : {
303 176707 : proc->attr.always_explicit = 1;
304 176707 : sym->attr.always_explicit = 1;
305 : }
306 :
307 524333 : gfc_namespace *orig_current_ns = gfc_current_ns;
308 524333 : gfc_current_ns = gfc_get_procedure_ns (proc);
309 :
310 1356965 : for (f = proc->formal; f; f = f->next)
311 : {
312 832634 : gfc_array_spec *as;
313 832634 : gfc_symbol *saved_specification_expr_symbol;
314 :
315 832634 : sym = f->sym;
316 :
317 832634 : if (sym == NULL)
318 : {
319 : /* Alternate return placeholder. */
320 171 : if (gfc_elemental (proc))
321 1 : gfc_error ("Alternate return specifier in elemental subroutine "
322 : "%qs at %L is not allowed", proc->name,
323 : &proc->declared_at);
324 171 : if (proc->attr.function)
325 1 : gfc_error ("Alternate return specifier in function "
326 : "%qs at %L is not allowed", proc->name,
327 : &proc->declared_at);
328 171 : continue;
329 : }
330 :
331 587 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
332 833050 : && !resolve_procedure_interface (sym))
333 : break;
334 :
335 832463 : if (strcmp (proc->name, sym->name) == 0)
336 : {
337 2 : gfc_error ("Self-referential argument "
338 : "%qs at %L is not allowed", sym->name,
339 : &proc->declared_at);
340 2 : break;
341 : }
342 :
343 832461 : if (sym->attr.if_source != IFSRC_UNKNOWN)
344 855 : gfc_resolve_formal_arglist (sym);
345 :
346 832461 : if (sym->attr.subroutine || sym->attr.external)
347 : {
348 859 : if (sym->attr.flavor == FL_UNKNOWN)
349 9 : gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
350 : }
351 : else
352 : {
353 831602 : if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
354 3663 : && (!sym->attr.function || sym->result == sym))
355 3625 : gfc_set_default_type (sym, 1, sym->ns);
356 : }
357 :
358 832461 : as = sym->ts.type == BT_CLASS && sym->attr.class_ok
359 846305 : ? CLASS_DATA (sym)->as : sym->as;
360 :
361 832461 : saved_specification_expr = specification_expr;
362 832461 : saved_specification_expr_symbol = specification_expr_symbol;
363 832461 : specification_expr = true;
364 832461 : specification_expr_symbol = sym;
365 832461 : gfc_resolve_array_spec (as, 0);
366 832461 : specification_expr = saved_specification_expr;
367 832461 : specification_expr_symbol = saved_specification_expr_symbol;
368 :
369 : /* We can't tell if an array with dimension (:) is assumed or deferred
370 : shape until we know if it has the pointer or allocatable attributes.
371 : */
372 832461 : if (as && as->rank > 0 && as->type == AS_DEFERRED
373 12251 : && ((sym->ts.type != BT_CLASS
374 11131 : && !(sym->attr.pointer || sym->attr.allocatable))
375 5356 : || (sym->ts.type == BT_CLASS
376 1120 : && !(CLASS_DATA (sym)->attr.class_pointer
377 920 : || CLASS_DATA (sym)->attr.allocatable)))
378 7400 : && sym->attr.flavor != FL_PROCEDURE)
379 : {
380 7399 : as->type = AS_ASSUMED_SHAPE;
381 17159 : for (i = 0; i < as->rank; i++)
382 9760 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
383 : }
384 :
385 128438 : if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
386 114825 : || (as && as->type == AS_ASSUMED_RANK)
387 781364 : || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
388 771217 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
389 11665 : && (CLASS_DATA (sym)->attr.class_pointer
390 11182 : || CLASS_DATA (sym)->attr.allocatable
391 10284 : || CLASS_DATA (sym)->attr.target))
392 769836 : || sym->attr.optional)
393 : {
394 77889 : proc->attr.always_explicit = 1;
395 77889 : if (proc->result)
396 36246 : proc->result->attr.always_explicit = 1;
397 : }
398 :
399 : /* If the flavor is unknown at this point, it has to be a variable.
400 : A procedure specification would have already set the type. */
401 :
402 832461 : if (sym->attr.flavor == FL_UNKNOWN)
403 50765 : gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
404 :
405 832461 : if (gfc_pure (proc))
406 : {
407 327281 : if (sym->attr.flavor == FL_PROCEDURE)
408 : {
409 : /* F08:C1279. */
410 29 : if (!gfc_pure (sym))
411 : {
412 1 : gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
413 : "also be PURE", sym->name, &sym->declared_at);
414 1 : continue;
415 : }
416 : }
417 327252 : else if (!sym->attr.pointer)
418 : {
419 327238 : if (proc->attr.function && sym->attr.intent != INTENT_IN)
420 : {
421 111 : if (sym->attr.value)
422 110 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
423 : " of pure function %qs at %L with VALUE "
424 : "attribute but without INTENT(IN)",
425 : sym->name, proc->name, &sym->declared_at);
426 : else
427 1 : gfc_error ("Argument %qs of pure function %qs at %L must "
428 : "be INTENT(IN) or VALUE", sym->name, proc->name,
429 : &sym->declared_at);
430 : }
431 :
432 327238 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
433 : {
434 159 : if (sym->attr.value)
435 159 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
436 : " of pure subroutine %qs at %L with VALUE "
437 : "attribute but without INTENT", sym->name,
438 : proc->name, &sym->declared_at);
439 : else
440 0 : gfc_error ("Argument %qs of pure subroutine %qs at %L "
441 : "must have its INTENT specified or have the "
442 : "VALUE attribute", sym->name, proc->name,
443 : &sym->declared_at);
444 : }
445 : }
446 :
447 : /* F08:C1278a. */
448 327280 : if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
449 : {
450 1 : gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
451 : " may not be polymorphic", sym->name, proc->name,
452 : &sym->declared_at);
453 1 : continue;
454 : }
455 : }
456 :
457 832459 : if (proc->attr.implicit_pure)
458 : {
459 24896 : if (sym->attr.flavor == FL_PROCEDURE)
460 : {
461 313 : if (!gfc_pure (sym))
462 293 : proc->attr.implicit_pure = 0;
463 : }
464 24583 : else if (!sym->attr.pointer)
465 : {
466 23802 : if (proc->attr.function && sym->attr.intent != INTENT_IN
467 2739 : && !sym->value)
468 2739 : proc->attr.implicit_pure = 0;
469 :
470 23802 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
471 4197 : && !sym->value)
472 4197 : proc->attr.implicit_pure = 0;
473 : }
474 : }
475 :
476 832459 : if (gfc_elemental (proc))
477 : {
478 : /* F08:C1289. */
479 301798 : if (sym->attr.codimension
480 301797 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
481 965 : && CLASS_DATA (sym)->attr.codimension))
482 : {
483 3 : gfc_error ("Coarray dummy argument %qs at %L to elemental "
484 : "procedure", sym->name, &sym->declared_at);
485 3 : continue;
486 : }
487 :
488 301795 : if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
489 963 : && CLASS_DATA (sym)->as))
490 : {
491 2 : gfc_error ("Argument %qs of elemental procedure at %L must "
492 : "be scalar", sym->name, &sym->declared_at);
493 2 : continue;
494 : }
495 :
496 301793 : if (sym->attr.allocatable
497 301792 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
498 962 : && CLASS_DATA (sym)->attr.allocatable))
499 : {
500 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
501 : "have the ALLOCATABLE attribute", sym->name,
502 : &sym->declared_at);
503 2 : continue;
504 : }
505 :
506 301791 : if (sym->attr.pointer
507 301790 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
508 961 : && CLASS_DATA (sym)->attr.class_pointer))
509 : {
510 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
511 : "have the POINTER attribute", sym->name,
512 : &sym->declared_at);
513 2 : continue;
514 : }
515 :
516 301789 : if (sym->attr.flavor == FL_PROCEDURE)
517 : {
518 2 : gfc_error ("Dummy procedure %qs not allowed in elemental "
519 : "procedure %qs at %L", sym->name, proc->name,
520 : &sym->declared_at);
521 2 : continue;
522 : }
523 :
524 : /* Fortran 2008 Corrigendum 1, C1290a. */
525 301787 : if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
526 : {
527 2 : gfc_error ("Argument %qs of elemental procedure %qs at %L must "
528 : "have its INTENT specified or have the VALUE "
529 : "attribute", sym->name, proc->name,
530 : &sym->declared_at);
531 2 : continue;
532 : }
533 : }
534 :
535 : /* Each dummy shall be specified to be scalar. */
536 832446 : if (proc->attr.proc == PROC_ST_FUNCTION)
537 : {
538 307 : if (sym->as != NULL)
539 : {
540 : /* F03:C1263 (R1238) The function-name and each dummy-arg-name
541 : shall be specified, explicitly or implicitly, to be scalar. */
542 1 : gfc_error ("Argument %qs of statement function %qs at %L "
543 : "must be scalar", sym->name, proc->name,
544 : &proc->declared_at);
545 1 : continue;
546 : }
547 :
548 306 : if (sym->ts.type == BT_CHARACTER)
549 : {
550 48 : gfc_charlen *cl = sym->ts.u.cl;
551 48 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
552 : {
553 0 : gfc_error ("Character-valued argument %qs of statement "
554 : "function at %L must have constant length",
555 : sym->name, &sym->declared_at);
556 0 : continue;
557 : }
558 : }
559 : }
560 : }
561 524333 : if (sym)
562 524241 : sym->formal_resolved = 1;
563 524333 : gfc_current_ns = orig_current_ns;
564 524333 : }
565 :
566 :
567 : /* Work function called when searching for symbols that have argument lists
568 : associated with them. */
569 :
570 : static void
571 1853645 : find_arglists (gfc_symbol *sym)
572 : {
573 1853645 : if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
574 332019 : || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
575 : return;
576 :
577 329708 : gfc_resolve_formal_arglist (sym);
578 : }
579 :
580 :
581 : /* Given a namespace, resolve all formal argument lists within the namespace.
582 : */
583 :
584 : static void
585 345787 : resolve_formal_arglists (gfc_namespace *ns)
586 : {
587 0 : if (ns == NULL)
588 : return;
589 :
590 345787 : gfc_traverse_ns (ns, find_arglists);
591 : }
592 :
593 :
594 : static void
595 37130 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
596 : {
597 37130 : bool t;
598 :
599 37130 : if (sym && sym->attr.flavor == FL_PROCEDURE
600 37130 : && sym->ns->parent
601 1070 : && sym->ns->parent->proc_name
602 1070 : && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
603 1 : && !strcmp (sym->name, sym->ns->parent->proc_name->name))
604 0 : gfc_error ("Contained procedure %qs at %L has the same name as its "
605 : "encompassing procedure", sym->name, &sym->declared_at);
606 :
607 : /* If this namespace is not a function or an entry master function,
608 : ignore it. */
609 37130 : if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
610 10871 : || sym->attr.entry_master)
611 26448 : return;
612 :
613 10682 : if (!sym->result)
614 : return;
615 :
616 : /* Try to find out of what the return type is. */
617 10682 : if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
618 : {
619 57 : t = gfc_set_default_type (sym->result, 0, ns);
620 :
621 57 : if (!t && !sym->result->attr.untyped)
622 : {
623 19 : if (sym->result == sym)
624 1 : gfc_error ("Contained function %qs at %L has no IMPLICIT type",
625 : sym->name, &sym->declared_at);
626 18 : else if (!sym->result->attr.proc_pointer)
627 0 : gfc_error ("Result %qs of contained function %qs at %L has "
628 : "no IMPLICIT type", sym->result->name, sym->name,
629 : &sym->result->declared_at);
630 19 : sym->result->attr.untyped = 1;
631 : }
632 : }
633 :
634 : /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
635 : type, lists the only ways a character length value of * can be used:
636 : dummy arguments of procedures, named constants, function results and
637 : in allocate statements if the allocate_object is an assumed length dummy
638 : in external functions. Internal function results and results of module
639 : procedures are not on this list, ergo, not permitted. */
640 :
641 10682 : if (sym->result->ts.type == BT_CHARACTER)
642 : {
643 1191 : gfc_charlen *cl = sym->result->ts.u.cl;
644 1191 : if ((!cl || !cl->length) && !sym->result->ts.deferred)
645 : {
646 : /* See if this is a module-procedure and adapt error message
647 : accordingly. */
648 4 : bool module_proc;
649 4 : gcc_assert (ns->parent && ns->parent->proc_name);
650 4 : module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
651 :
652 7 : gfc_error (module_proc
653 : ? G_("Character-valued module procedure %qs at %L"
654 : " must not be assumed length")
655 : : G_("Character-valued internal function %qs at %L"
656 : " must not be assumed length"),
657 : sym->name, &sym->declared_at);
658 : }
659 : }
660 : }
661 :
662 :
663 : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
664 : introduce duplicates. */
665 :
666 : static void
667 1491 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
668 : {
669 1491 : gfc_formal_arglist *f, *new_arglist;
670 1491 : gfc_symbol *new_sym;
671 :
672 2644 : for (; new_args != NULL; new_args = new_args->next)
673 : {
674 1153 : new_sym = new_args->sym;
675 : /* See if this arg is already in the formal argument list. */
676 2186 : for (f = proc->formal; f; f = f->next)
677 : {
678 1481 : if (new_sym == f->sym)
679 : break;
680 : }
681 :
682 1153 : if (f)
683 448 : continue;
684 :
685 : /* Add a new argument. Argument order is not important. */
686 705 : new_arglist = gfc_get_formal_arglist ();
687 705 : new_arglist->sym = new_sym;
688 705 : new_arglist->next = proc->formal;
689 705 : proc->formal = new_arglist;
690 : }
691 1491 : }
692 :
693 :
694 : /* Flag the arguments that are not present in all entries. */
695 :
696 : static void
697 1491 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
698 : {
699 1491 : gfc_formal_arglist *f, *head;
700 1491 : head = new_args;
701 :
702 3086 : for (f = proc->formal; f; f = f->next)
703 : {
704 1595 : if (f->sym == NULL)
705 36 : continue;
706 :
707 2738 : for (new_args = head; new_args; new_args = new_args->next)
708 : {
709 2287 : if (new_args->sym == f->sym)
710 : break;
711 : }
712 :
713 1559 : if (new_args)
714 1108 : continue;
715 :
716 451 : f->sym->attr.not_always_present = 1;
717 : }
718 1491 : }
719 :
720 :
721 : /* Resolve alternate entry points. If a symbol has multiple entry points we
722 : create a new master symbol for the main routine, and turn the existing
723 : symbol into an entry point. */
724 :
725 : static void
726 382410 : resolve_entries (gfc_namespace *ns)
727 : {
728 382410 : gfc_namespace *old_ns;
729 382410 : gfc_code *c;
730 382410 : gfc_symbol *proc;
731 382410 : gfc_entry_list *el;
732 : /* Provide sufficient space to hold "master.%d.%s". */
733 382410 : char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
734 382410 : static int master_count = 0;
735 :
736 382410 : if (ns->proc_name == NULL)
737 381707 : return;
738 :
739 : /* No need to do anything if this procedure doesn't have alternate entry
740 : points. */
741 382361 : if (!ns->entries)
742 : return;
743 :
744 : /* We may already have resolved alternate entry points. */
745 954 : if (ns->proc_name->attr.entry_master)
746 : return;
747 :
748 : /* If this isn't a procedure something has gone horribly wrong. */
749 703 : gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
750 :
751 : /* Remember the current namespace. */
752 703 : old_ns = gfc_current_ns;
753 :
754 703 : gfc_current_ns = ns;
755 :
756 : /* Add the main entry point to the list of entry points. */
757 703 : el = gfc_get_entry_list ();
758 703 : el->sym = ns->proc_name;
759 703 : el->id = 0;
760 703 : el->next = ns->entries;
761 703 : ns->entries = el;
762 703 : ns->proc_name->attr.entry = 1;
763 :
764 : /* If it is a module function, it needs to be in the right namespace
765 : so that gfc_get_fake_result_decl can gather up the results. The
766 : need for this arose in get_proc_name, where these beasts were
767 : left in their own namespace, to keep prior references linked to
768 : the entry declaration.*/
769 703 : if (ns->proc_name->attr.function
770 596 : && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
771 189 : el->sym->ns = ns;
772 :
773 : /* Do the same for entries where the master is not a module
774 : procedure. These are retained in the module namespace because
775 : of the module procedure declaration. */
776 1491 : for (el = el->next; el; el = el->next)
777 788 : if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
778 0 : && el->sym->attr.mod_proc)
779 0 : el->sym->ns = ns;
780 703 : el = ns->entries;
781 :
782 : /* Add an entry statement for it. */
783 703 : c = gfc_get_code (EXEC_ENTRY);
784 703 : c->ext.entry = el;
785 703 : c->next = ns->code;
786 703 : ns->code = c;
787 :
788 : /* Create a new symbol for the master function. */
789 : /* Give the internal function a unique name (within this file).
790 : Also include the function name so the user has some hope of figuring
791 : out what is going on. */
792 703 : snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
793 703 : master_count++, ns->proc_name->name);
794 703 : gfc_get_ha_symbol (name, &proc);
795 703 : gcc_assert (proc != NULL);
796 :
797 703 : gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
798 703 : if (ns->proc_name->attr.subroutine)
799 107 : gfc_add_subroutine (&proc->attr, proc->name, NULL);
800 : else
801 : {
802 596 : gfc_symbol *sym;
803 596 : gfc_typespec *ts, *fts;
804 596 : gfc_array_spec *as, *fas;
805 596 : gfc_add_function (&proc->attr, proc->name, NULL);
806 596 : proc->result = proc;
807 596 : fas = ns->entries->sym->as;
808 596 : fas = fas ? fas : ns->entries->sym->result->as;
809 596 : fts = &ns->entries->sym->result->ts;
810 596 : if (fts->type == BT_UNKNOWN)
811 51 : fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
812 1120 : for (el = ns->entries->next; el; el = el->next)
813 : {
814 635 : ts = &el->sym->result->ts;
815 635 : as = el->sym->as;
816 635 : as = as ? as : el->sym->result->as;
817 635 : if (ts->type == BT_UNKNOWN)
818 61 : ts = gfc_get_default_type (el->sym->result->name, NULL);
819 :
820 635 : if (! gfc_compare_types (ts, fts)
821 527 : || (el->sym->result->attr.dimension
822 527 : != ns->entries->sym->result->attr.dimension)
823 635 : || (el->sym->result->attr.pointer
824 527 : != ns->entries->sym->result->attr.pointer))
825 : break;
826 65 : else if (as && fas && ns->entries->sym->result != el->sym->result
827 589 : && gfc_compare_array_spec (as, fas) == 0)
828 5 : gfc_error ("Function %s at %L has entries with mismatched "
829 : "array specifications", ns->entries->sym->name,
830 5 : &ns->entries->sym->declared_at);
831 : /* The characteristics need to match and thus both need to have
832 : the same string length, i.e. both len=*, or both len=4.
833 : Having both len=<variable> is also possible, but difficult to
834 : check at compile time. */
835 522 : else if (ts->type == BT_CHARACTER
836 113 : && (el->sym->result->attr.allocatable
837 113 : != ns->entries->sym->result->attr.allocatable))
838 : {
839 3 : gfc_error ("Function %s at %L has entry %s with mismatched "
840 : "characteristics", ns->entries->sym->name,
841 : &ns->entries->sym->declared_at, el->sym->name);
842 3 : goto cleanup;
843 : }
844 519 : else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
845 110 : && (((ts->u.cl->length && !fts->u.cl->length)
846 109 : ||(!ts->u.cl->length && fts->u.cl->length))
847 90 : || (ts->u.cl->length
848 53 : && ts->u.cl->length->expr_type
849 53 : != fts->u.cl->length->expr_type)
850 90 : || (ts->u.cl->length
851 53 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
852 52 : && mpz_cmp (ts->u.cl->length->value.integer,
853 52 : fts->u.cl->length->value.integer) != 0)))
854 21 : gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
855 : "entries returning variables of different "
856 : "string lengths", ns->entries->sym->name,
857 21 : &ns->entries->sym->declared_at);
858 498 : else if (el->sym->result->attr.allocatable
859 498 : != ns->entries->sym->result->attr.allocatable)
860 : break;
861 : }
862 :
863 593 : if (el == NULL)
864 : {
865 485 : sym = ns->entries->sym->result;
866 : /* All result types the same. */
867 485 : proc->ts = *fts;
868 485 : if (sym->attr.dimension)
869 63 : gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
870 485 : if (sym->attr.pointer)
871 78 : gfc_add_pointer (&proc->attr, NULL);
872 485 : if (sym->attr.allocatable)
873 24 : gfc_add_allocatable (&proc->attr, NULL);
874 : }
875 : else
876 : {
877 : /* Otherwise the result will be passed through a union by
878 : reference. */
879 108 : proc->attr.mixed_entry_master = 1;
880 346 : for (el = ns->entries; el; el = el->next)
881 : {
882 238 : sym = el->sym->result;
883 238 : if (sym->attr.dimension)
884 : {
885 1 : if (el == ns->entries)
886 0 : gfc_error ("FUNCTION result %s cannot be an array in "
887 : "FUNCTION %s at %L", sym->name,
888 0 : ns->entries->sym->name, &sym->declared_at);
889 : else
890 1 : gfc_error ("ENTRY result %s cannot be an array in "
891 : "FUNCTION %s at %L", sym->name,
892 1 : ns->entries->sym->name, &sym->declared_at);
893 : }
894 237 : else if (sym->attr.pointer)
895 : {
896 1 : if (el == ns->entries)
897 1 : gfc_error ("FUNCTION result %s cannot be a POINTER in "
898 : "FUNCTION %s at %L", sym->name,
899 1 : ns->entries->sym->name, &sym->declared_at);
900 : else
901 0 : gfc_error ("ENTRY result %s cannot be a POINTER in "
902 : "FUNCTION %s at %L", sym->name,
903 0 : ns->entries->sym->name, &sym->declared_at);
904 : }
905 236 : else if (sym->attr.allocatable)
906 : {
907 0 : if (el == ns->entries)
908 0 : gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
909 : "FUNCTION %s at %L", sym->name,
910 0 : ns->entries->sym->name, &sym->declared_at);
911 : else
912 0 : gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
913 : "FUNCTION %s at %L", sym->name,
914 0 : ns->entries->sym->name, &sym->declared_at);
915 : }
916 : else
917 : {
918 236 : ts = &sym->ts;
919 236 : if (ts->type == BT_UNKNOWN)
920 9 : ts = gfc_get_default_type (sym->name, NULL);
921 236 : switch (ts->type)
922 : {
923 85 : case BT_INTEGER:
924 85 : if (ts->kind == gfc_default_integer_kind)
925 : sym = NULL;
926 : break;
927 100 : case BT_REAL:
928 100 : if (ts->kind == gfc_default_real_kind
929 18 : || ts->kind == gfc_default_double_kind)
930 : sym = NULL;
931 : break;
932 20 : case BT_COMPLEX:
933 20 : if (ts->kind == gfc_default_complex_kind)
934 : sym = NULL;
935 : break;
936 28 : case BT_LOGICAL:
937 28 : if (ts->kind == gfc_default_logical_kind)
938 : sym = NULL;
939 : break;
940 : case BT_UNKNOWN:
941 : /* We will issue error elsewhere. */
942 : sym = NULL;
943 : break;
944 : default:
945 : break;
946 : }
947 3 : if (sym)
948 : {
949 3 : if (el == ns->entries)
950 1 : gfc_error ("FUNCTION result %s cannot be of type %s "
951 : "in FUNCTION %s at %L", sym->name,
952 1 : gfc_typename (ts), ns->entries->sym->name,
953 : &sym->declared_at);
954 : else
955 2 : gfc_error ("ENTRY result %s cannot be of type %s "
956 : "in FUNCTION %s at %L", sym->name,
957 2 : gfc_typename (ts), ns->entries->sym->name,
958 : &sym->declared_at);
959 : }
960 : }
961 : }
962 : }
963 : }
964 :
965 108 : cleanup:
966 703 : proc->attr.access = ACCESS_PRIVATE;
967 703 : proc->attr.entry_master = 1;
968 :
969 : /* Merge all the entry point arguments. */
970 2194 : for (el = ns->entries; el; el = el->next)
971 1491 : merge_argument_lists (proc, el->sym->formal);
972 :
973 : /* Check the master formal arguments for any that are not
974 : present in all entry points. */
975 2194 : for (el = ns->entries; el; el = el->next)
976 1491 : check_argument_lists (proc, el->sym->formal);
977 :
978 : /* Use the master function for the function body. */
979 703 : ns->proc_name = proc;
980 :
981 : /* Finalize the new symbols. */
982 703 : gfc_commit_symbols ();
983 :
984 : /* Restore the original namespace. */
985 703 : gfc_current_ns = old_ns;
986 : }
987 :
988 :
989 : /* Forward declaration. */
990 : static bool is_non_constant_shape_array (gfc_symbol *sym);
991 :
992 :
993 : /* Resolve common variables. */
994 : static void
995 347764 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
996 : {
997 347764 : gfc_symbol *csym = common_block->head;
998 347764 : gfc_gsymbol *gsym;
999 :
1000 353815 : for (; csym; csym = csym->common_next)
1001 : {
1002 6051 : gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
1003 6051 : if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
1004 : {
1005 3 : if (csym->common_block)
1006 2 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
1007 : "COMMON block at %L", gsym->name,
1008 : &gsym->where, &csym->common_block->where);
1009 : else
1010 1 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
1011 : "COMMON block", gsym->name, &gsym->where);
1012 : }
1013 :
1014 : /* gfc_add_in_common may have been called before, but the reported errors
1015 : have been ignored to continue parsing.
1016 : We do the checks again here, unless the symbol is USE associated. */
1017 6051 : if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
1018 : {
1019 5778 : gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
1020 5778 : gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
1021 : &common_block->where);
1022 : }
1023 :
1024 6051 : if (csym->value || csym->attr.data)
1025 : {
1026 149 : if (!csym->ns->is_block_data)
1027 33 : gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1028 : "but only in BLOCK DATA initialization is "
1029 : "allowed", csym->name, &csym->declared_at);
1030 116 : else if (!named_common)
1031 8 : gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1032 : "in a blank COMMON but initialization is only "
1033 : "allowed in named common blocks", csym->name,
1034 : &csym->declared_at);
1035 : }
1036 :
1037 6051 : if (UNLIMITED_POLY (csym))
1038 1 : gfc_error_now ("%qs at %L cannot appear in COMMON "
1039 : "[F2008:C5100]", csym->name, &csym->declared_at);
1040 :
1041 6051 : if (csym->attr.dimension && is_non_constant_shape_array (csym))
1042 : {
1043 1 : gfc_error_now ("Automatic object %qs at %L cannot appear in "
1044 : "COMMON at %L", csym->name, &csym->declared_at,
1045 : &common_block->where);
1046 : /* Avoid confusing follow-on error. */
1047 1 : csym->error = 1;
1048 : }
1049 :
1050 6051 : if (csym->ts.type != BT_DERIVED)
1051 6004 : continue;
1052 :
1053 47 : if (!(csym->ts.u.derived->attr.sequence
1054 3 : || csym->ts.u.derived->attr.is_bind_c))
1055 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1056 : "has neither the SEQUENCE nor the BIND(C) "
1057 : "attribute", csym->name, &csym->declared_at);
1058 47 : if (csym->ts.u.derived->attr.alloc_comp)
1059 3 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1060 : "has an ultimate component that is "
1061 : "allocatable", csym->name, &csym->declared_at);
1062 47 : if (gfc_has_default_initializer (csym->ts.u.derived))
1063 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1064 : "may not have default initializer", csym->name,
1065 : &csym->declared_at);
1066 :
1067 47 : if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1068 16 : gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1069 : }
1070 347764 : }
1071 :
1072 : /* Resolve common blocks. */
1073 : static void
1074 346317 : resolve_common_blocks (gfc_symtree *common_root)
1075 : {
1076 346317 : gfc_symbol *sym = NULL;
1077 346317 : gfc_gsymbol * gsym;
1078 :
1079 346317 : if (common_root == NULL)
1080 346195 : return;
1081 :
1082 1977 : if (common_root->left)
1083 257 : resolve_common_blocks (common_root->left);
1084 1977 : if (common_root->right)
1085 273 : resolve_common_blocks (common_root->right);
1086 :
1087 1977 : resolve_common_vars (common_root->n.common, true);
1088 :
1089 : /* The common name is a global name - in Fortran 2003 also if it has a
1090 : C binding name, since Fortran 2008 only the C binding name is a global
1091 : identifier. */
1092 1977 : if (!common_root->n.common->binding_label
1093 1977 : || gfc_notification_std (GFC_STD_F2008))
1094 : {
1095 3810 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1096 1905 : common_root->n.common->name);
1097 :
1098 820 : if (gsym && gfc_notification_std (GFC_STD_F2008)
1099 14 : && gsym->type == GSYM_COMMON
1100 1918 : && ((common_root->n.common->binding_label
1101 6 : && (!gsym->binding_label
1102 0 : || strcmp (common_root->n.common->binding_label,
1103 : gsym->binding_label) != 0))
1104 7 : || (!common_root->n.common->binding_label
1105 7 : && gsym->binding_label)))
1106 : {
1107 6 : gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1108 : "identifier and must thus have the same binding name "
1109 : "as the same-named COMMON block at %L: %s vs %s",
1110 6 : common_root->n.common->name, &common_root->n.common->where,
1111 : &gsym->where,
1112 : common_root->n.common->binding_label
1113 : ? common_root->n.common->binding_label : "(blank)",
1114 6 : gsym->binding_label ? gsym->binding_label : "(blank)");
1115 6 : return;
1116 : }
1117 :
1118 1899 : if (gsym && gsym->type != GSYM_COMMON
1119 1 : && !common_root->n.common->binding_label)
1120 : {
1121 0 : gfc_error ("COMMON block %qs at %L uses the same global identifier "
1122 : "as entity at %L",
1123 0 : common_root->n.common->name, &common_root->n.common->where,
1124 : &gsym->where);
1125 0 : return;
1126 : }
1127 814 : if (gsym && gsym->type != GSYM_COMMON)
1128 : {
1129 1 : gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1130 : "%L sharing the identifier with global non-COMMON-block "
1131 1 : "entity at %L", common_root->n.common->name,
1132 1 : &common_root->n.common->where, &gsym->where);
1133 1 : return;
1134 : }
1135 1085 : if (!gsym)
1136 : {
1137 1085 : gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1138 1085 : gsym->type = GSYM_COMMON;
1139 1085 : gsym->where = common_root->n.common->where;
1140 1085 : gsym->defined = 1;
1141 : }
1142 1898 : gsym->used = 1;
1143 : }
1144 :
1145 1970 : if (common_root->n.common->binding_label)
1146 : {
1147 76 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1148 : common_root->n.common->binding_label);
1149 76 : if (gsym && gsym->type != GSYM_COMMON)
1150 : {
1151 1 : gfc_error ("COMMON block at %L with binding label %qs uses the same "
1152 : "global identifier as entity at %L",
1153 : &common_root->n.common->where,
1154 1 : common_root->n.common->binding_label, &gsym->where);
1155 1 : return;
1156 : }
1157 57 : if (!gsym)
1158 : {
1159 57 : gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1160 57 : gsym->type = GSYM_COMMON;
1161 57 : gsym->where = common_root->n.common->where;
1162 57 : gsym->defined = 1;
1163 : }
1164 75 : gsym->used = 1;
1165 : }
1166 :
1167 1969 : gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1168 1969 : if (sym == NULL)
1169 : return;
1170 :
1171 122 : if (sym->attr.flavor == FL_PARAMETER)
1172 2 : gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1173 2 : sym->name, &common_root->n.common->where, &sym->declared_at);
1174 :
1175 122 : if (sym->attr.external)
1176 1 : gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1177 1 : sym->name, &common_root->n.common->where);
1178 :
1179 122 : if (sym->attr.intrinsic)
1180 2 : gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1181 2 : sym->name, &common_root->n.common->where);
1182 120 : else if (sym->attr.result
1183 120 : || gfc_is_function_return_value (sym, gfc_current_ns))
1184 1 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1185 : "that is also a function result", sym->name,
1186 1 : &common_root->n.common->where);
1187 119 : else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1188 5 : && sym->attr.proc != PROC_ST_FUNCTION)
1189 3 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1190 : "that is also a global procedure", sym->name,
1191 3 : &common_root->n.common->where);
1192 : }
1193 :
1194 :
1195 : /* Resolve contained function types. Because contained functions can call one
1196 : another, they have to be worked out before any of the contained procedures
1197 : can be resolved.
1198 :
1199 : The good news is that if a function doesn't already have a type, the only
1200 : way it can get one is through an IMPLICIT type or a RESULT variable, because
1201 : by definition contained functions are contained namespace they're contained
1202 : in, not in a sibling or parent namespace. */
1203 :
1204 : static void
1205 345787 : resolve_contained_functions (gfc_namespace *ns)
1206 : {
1207 345787 : gfc_namespace *child;
1208 345787 : gfc_entry_list *el;
1209 :
1210 345787 : resolve_formal_arglists (ns);
1211 :
1212 382410 : for (child = ns->contained; child; child = child->sibling)
1213 : {
1214 : /* Resolve alternate entry points first. */
1215 36623 : resolve_entries (child);
1216 :
1217 : /* Then check function return types. */
1218 36623 : resolve_contained_fntype (child->proc_name, child);
1219 37130 : for (el = child->entries; el; el = el->next)
1220 507 : resolve_contained_fntype (el->sym, child);
1221 : }
1222 345787 : }
1223 :
1224 :
1225 :
1226 : /* A Parameterized Derived Type constructor must contain values for
1227 : the PDT KIND parameters or they must have a default initializer.
1228 : Go through the constructor picking out the KIND expressions,
1229 : storing them in 'param_list' and then call gfc_get_pdt_instance
1230 : to obtain the PDT instance. */
1231 :
1232 : static gfc_actual_arglist *param_list, *param_tail, *param;
1233 :
1234 : static bool
1235 296 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1236 : {
1237 296 : param = gfc_get_actual_arglist ();
1238 296 : if (!param_list)
1239 240 : param_list = param_tail = param;
1240 : else
1241 : {
1242 56 : param_tail->next = param;
1243 56 : param_tail = param_tail->next;
1244 : }
1245 :
1246 296 : param_tail->name = c->name;
1247 296 : if (expr)
1248 296 : param_tail->expr = gfc_copy_expr (expr);
1249 0 : else if (c->initializer)
1250 0 : param_tail->expr = gfc_copy_expr (c->initializer);
1251 : else
1252 : {
1253 0 : param_tail->spec_type = SPEC_ASSUMED;
1254 0 : if (c->attr.pdt_kind)
1255 : {
1256 0 : gfc_error ("The KIND parameter %qs in the PDT constructor "
1257 : "at %C has no value", param->name);
1258 0 : return false;
1259 : }
1260 : }
1261 :
1262 : return true;
1263 : }
1264 :
1265 : static bool
1266 276 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1267 : gfc_symbol *derived)
1268 : {
1269 276 : gfc_constructor *cons = NULL;
1270 276 : gfc_component *comp;
1271 276 : bool t = true;
1272 :
1273 276 : if (expr && expr->expr_type == EXPR_STRUCTURE)
1274 240 : cons = gfc_constructor_first (expr->value.constructor);
1275 36 : else if (constr)
1276 36 : cons = *constr;
1277 276 : gcc_assert (cons);
1278 :
1279 276 : comp = derived->components;
1280 :
1281 844 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1282 : {
1283 568 : if (cons->expr
1284 568 : && cons->expr->expr_type == EXPR_STRUCTURE
1285 0 : && comp->ts.type == BT_DERIVED)
1286 : {
1287 0 : t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1288 0 : if (!t)
1289 : return t;
1290 : }
1291 568 : else if (comp->ts.type == BT_DERIVED)
1292 : {
1293 36 : t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1294 36 : if (!t)
1295 : return t;
1296 : }
1297 532 : else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1298 296 : && derived->attr.pdt_template)
1299 : {
1300 296 : t = get_pdt_spec_expr (comp, cons->expr);
1301 296 : if (!t)
1302 : return t;
1303 : }
1304 : }
1305 : return t;
1306 : }
1307 :
1308 :
1309 : static bool resolve_fl_derived0 (gfc_symbol *sym);
1310 : static bool resolve_fl_struct (gfc_symbol *sym);
1311 :
1312 :
1313 : /* Resolve all of the elements of a structure constructor and make sure that
1314 : the types are correct. The 'init' flag indicates that the given
1315 : constructor is an initializer. */
1316 :
1317 : static bool
1318 62970 : resolve_structure_cons (gfc_expr *expr, int init)
1319 : {
1320 62970 : gfc_constructor *cons;
1321 62970 : gfc_component *comp;
1322 62970 : bool t;
1323 62970 : symbol_attribute a;
1324 :
1325 62970 : t = true;
1326 :
1327 62970 : if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1328 : {
1329 60079 : if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1330 59929 : resolve_fl_derived0 (expr->ts.u.derived);
1331 : else
1332 150 : resolve_fl_struct (expr->ts.u.derived);
1333 :
1334 : /* If this is a Parameterized Derived Type template, find the
1335 : instance corresponding to the PDT kind parameters. */
1336 60079 : if (expr->ts.u.derived->attr.pdt_template)
1337 : {
1338 240 : param_list = NULL;
1339 240 : t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1340 240 : if (!t)
1341 : return t;
1342 240 : gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1343 :
1344 240 : expr->param_list = gfc_copy_actual_arglist (param_list);
1345 :
1346 240 : if (param_list)
1347 240 : gfc_free_actual_arglist (param_list);
1348 :
1349 240 : if (!expr->ts.u.derived->attr.pdt_type)
1350 : return false;
1351 : }
1352 : }
1353 :
1354 : /* A constructor may have references if it is the result of substituting a
1355 : parameter variable. In this case we just pull out the component we
1356 : want. */
1357 62970 : if (expr->ref)
1358 160 : comp = expr->ref->u.c.sym->components;
1359 62810 : else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1360 : || expr->ts.type == BT_UNION)
1361 62808 : && expr->ts.u.derived)
1362 62808 : comp = expr->ts.u.derived->components;
1363 : else
1364 : return false;
1365 :
1366 62968 : cons = gfc_constructor_first (expr->value.constructor);
1367 :
1368 209536 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1369 : {
1370 146570 : int rank;
1371 :
1372 146570 : if (!cons->expr)
1373 9758 : continue;
1374 :
1375 : /* Unions use an EXPR_NULL contrived expression to tell the translation
1376 : phase to generate an initializer of the appropriate length.
1377 : Ignore it here. */
1378 136812 : if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1379 15 : continue;
1380 :
1381 136797 : if (!gfc_resolve_expr (cons->expr))
1382 : {
1383 0 : t = false;
1384 0 : continue;
1385 : }
1386 :
1387 136797 : rank = comp->as ? comp->as->rank : 0;
1388 136797 : if (comp->ts.type == BT_CLASS
1389 1763 : && !comp->ts.u.derived->attr.unlimited_polymorphic
1390 1762 : && CLASS_DATA (comp)->as)
1391 519 : rank = CLASS_DATA (comp)->as->rank;
1392 :
1393 136797 : if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1394 228 : gfc_find_vtab (&cons->expr->ts);
1395 :
1396 136797 : if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1397 495 : && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
1398 : {
1399 4 : gfc_error ("The rank of the element in the structure "
1400 : "constructor at %L does not match that of the "
1401 : "component (%d/%d)", &cons->expr->where,
1402 : cons->expr->rank, rank);
1403 4 : t = false;
1404 : }
1405 :
1406 : /* If we don't have the right type, try to convert it. */
1407 :
1408 239193 : if (!comp->attr.proc_pointer &&
1409 102396 : !gfc_compare_types (&cons->expr->ts, &comp->ts))
1410 : {
1411 12481 : if (strcmp (comp->name, "_extends") == 0)
1412 : {
1413 : /* Can afford to be brutal with the _extends initializer.
1414 : The derived type can get lost because it is PRIVATE
1415 : but it is not usage constrained by the standard. */
1416 9129 : cons->expr->ts = comp->ts;
1417 : }
1418 3352 : else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1419 : {
1420 2 : gfc_error ("The element in the structure constructor at %L, "
1421 : "for pointer component %qs, is %s but should be %s",
1422 2 : &cons->expr->where, comp->name,
1423 2 : gfc_basic_typename (cons->expr->ts.type),
1424 : gfc_basic_typename (comp->ts.type));
1425 2 : t = false;
1426 : }
1427 3350 : else if (!UNLIMITED_POLY (comp))
1428 : {
1429 3287 : bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1430 3287 : if (t)
1431 136797 : t = t2;
1432 : }
1433 : }
1434 :
1435 : /* For strings, the length of the constructor should be the same as
1436 : the one of the structure, ensure this if the lengths are known at
1437 : compile time and when we are dealing with PARAMETER or structure
1438 : constructors. */
1439 136797 : if (cons->expr->ts.type == BT_CHARACTER
1440 3890 : && comp->ts.type == BT_CHARACTER
1441 3864 : && comp->ts.u.cl && comp->ts.u.cl->length
1442 2498 : && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1443 2463 : && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1444 926 : && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1445 926 : && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1446 926 : && comp->ts.u.cl->length->ts.type == BT_INTEGER
1447 926 : && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1448 926 : comp->ts.u.cl->length->value.integer) != 0)
1449 : {
1450 11 : if (comp->attr.pointer)
1451 : {
1452 3 : HOST_WIDE_INT la, lb;
1453 3 : la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1454 3 : lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1455 3 : gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1456 : "component %qs in constructor at %L",
1457 3 : la, lb, comp->name, &cons->expr->where);
1458 3 : t = false;
1459 : }
1460 :
1461 11 : if (cons->expr->expr_type == EXPR_VARIABLE
1462 4 : && cons->expr->rank != 0
1463 2 : && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1464 : {
1465 : /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1466 : to make use of the gfc_resolve_character_array_constructor
1467 : machinery. The expression is later simplified away to
1468 : an array of string literals. */
1469 1 : gfc_expr *para = cons->expr;
1470 1 : cons->expr = gfc_get_expr ();
1471 1 : cons->expr->ts = para->ts;
1472 1 : cons->expr->where = para->where;
1473 1 : cons->expr->expr_type = EXPR_ARRAY;
1474 1 : cons->expr->rank = para->rank;
1475 1 : cons->expr->corank = para->corank;
1476 1 : cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1477 1 : gfc_constructor_append_expr (&cons->expr->value.constructor,
1478 1 : para, &cons->expr->where);
1479 : }
1480 :
1481 11 : if (cons->expr->expr_type == EXPR_ARRAY)
1482 : {
1483 : /* Rely on the cleanup of the namespace to deal correctly with
1484 : the old charlen. (There was a block here that attempted to
1485 : remove the charlen but broke the chain in so doing.) */
1486 5 : cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1487 5 : cons->expr->ts.u.cl->length_from_typespec = true;
1488 5 : cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1489 5 : gfc_resolve_character_array_constructor (cons->expr);
1490 : }
1491 : }
1492 :
1493 136797 : if (cons->expr->expr_type == EXPR_NULL
1494 41039 : && !(comp->attr.pointer || comp->attr.allocatable
1495 20409 : || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1496 1112 : || (comp->ts.type == BT_CLASS
1497 1110 : && (CLASS_DATA (comp)->attr.class_pointer
1498 893 : || CLASS_DATA (comp)->attr.allocatable))))
1499 : {
1500 2 : t = false;
1501 2 : gfc_error ("The NULL in the structure constructor at %L is "
1502 : "being applied to component %qs, which is neither "
1503 : "a POINTER nor ALLOCATABLE", &cons->expr->where,
1504 : comp->name);
1505 : }
1506 :
1507 136797 : if (comp->attr.proc_pointer && comp->ts.interface)
1508 : {
1509 : /* Check procedure pointer interface. */
1510 15425 : gfc_symbol *s2 = NULL;
1511 15425 : gfc_component *c2;
1512 15425 : const char *name;
1513 15425 : char err[200];
1514 :
1515 15425 : c2 = gfc_get_proc_ptr_comp (cons->expr);
1516 15425 : if (c2)
1517 : {
1518 12 : s2 = c2->ts.interface;
1519 12 : name = c2->name;
1520 : }
1521 15413 : else if (cons->expr->expr_type == EXPR_FUNCTION)
1522 : {
1523 0 : s2 = cons->expr->symtree->n.sym->result;
1524 0 : name = cons->expr->symtree->n.sym->result->name;
1525 : }
1526 15413 : else if (cons->expr->expr_type != EXPR_NULL)
1527 : {
1528 14999 : s2 = cons->expr->symtree->n.sym;
1529 14999 : name = cons->expr->symtree->n.sym->name;
1530 : }
1531 :
1532 15011 : if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1533 : err, sizeof (err), NULL, NULL))
1534 : {
1535 2 : gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1536 : "component %qs in structure constructor at %L:"
1537 2 : " %s", comp->name, &cons->expr->where, err);
1538 2 : return false;
1539 : }
1540 : }
1541 :
1542 : /* Validate shape, except for dynamic or PDT arrays. */
1543 136795 : if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1544 2263 : && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1545 1526 : && !comp->attr.pdt_array)
1546 : {
1547 1279 : mpz_t len;
1548 1279 : mpz_init (len);
1549 2651 : for (int n = 0; n < rank; n++)
1550 : {
1551 1377 : if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1552 1372 : || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1553 : {
1554 5 : gfc_error ("Bad array spec of component %qs referenced in "
1555 : "structure constructor at %L",
1556 5 : comp->name, &cons->expr->where);
1557 5 : t = false;
1558 5 : break;
1559 1372 : };
1560 1372 : if (cons->expr->shape == NULL)
1561 12 : continue;
1562 1360 : mpz_set_ui (len, 1);
1563 1360 : mpz_add (len, len, comp->as->upper[n]->value.integer);
1564 1360 : mpz_sub (len, len, comp->as->lower[n]->value.integer);
1565 1360 : if (mpz_cmp (cons->expr->shape[n], len) != 0)
1566 : {
1567 9 : gfc_error ("The shape of component %qs in the structure "
1568 : "constructor at %L differs from the shape of the "
1569 : "declared component for dimension %d (%ld/%ld)",
1570 : comp->name, &cons->expr->where, n+1,
1571 : mpz_get_si (cons->expr->shape[n]),
1572 : mpz_get_si (len));
1573 9 : t = false;
1574 : }
1575 : }
1576 1279 : mpz_clear (len);
1577 : }
1578 :
1579 136795 : if (!comp->attr.pointer || comp->attr.proc_pointer
1580 21993 : || cons->expr->expr_type == EXPR_NULL)
1581 126690 : continue;
1582 :
1583 10105 : a = gfc_expr_attr (cons->expr);
1584 :
1585 10105 : if (!a.pointer && !a.target)
1586 : {
1587 1 : t = false;
1588 1 : gfc_error ("The element in the structure constructor at %L, "
1589 : "for pointer component %qs should be a POINTER or "
1590 1 : "a TARGET", &cons->expr->where, comp->name);
1591 : }
1592 :
1593 10105 : if (init)
1594 : {
1595 : /* F08:C461. Additional checks for pointer initialization. */
1596 10037 : if (a.allocatable)
1597 : {
1598 0 : t = false;
1599 0 : gfc_error ("Pointer initialization target at %L "
1600 0 : "must not be ALLOCATABLE", &cons->expr->where);
1601 : }
1602 10037 : if (!a.save)
1603 : {
1604 0 : t = false;
1605 0 : gfc_error ("Pointer initialization target at %L "
1606 0 : "must have the SAVE attribute", &cons->expr->where);
1607 : }
1608 : }
1609 :
1610 : /* F2023:C770: A designator that is an initial-data-target shall ...
1611 : not have a vector subscript. */
1612 10105 : if (comp->attr.pointer && (a.pointer || a.target)
1613 20209 : && gfc_has_vector_index (cons->expr))
1614 : {
1615 1 : gfc_error ("Pointer assignment target at %L has a vector subscript",
1616 1 : &cons->expr->where);
1617 1 : t = false;
1618 : }
1619 :
1620 : /* F2003, C1272 (3). */
1621 10105 : bool impure = cons->expr->expr_type == EXPR_VARIABLE
1622 10105 : && (gfc_impure_variable (cons->expr->symtree->n.sym)
1623 10069 : || gfc_is_coindexed (cons->expr));
1624 33 : if (impure && gfc_pure (NULL))
1625 : {
1626 1 : t = false;
1627 1 : gfc_error ("Invalid expression in the structure constructor for "
1628 : "pointer component %qs at %L in PURE procedure",
1629 1 : comp->name, &cons->expr->where);
1630 : }
1631 :
1632 10105 : if (impure)
1633 33 : gfc_unset_implicit_pure (NULL);
1634 : }
1635 :
1636 : return t;
1637 : }
1638 :
1639 :
1640 : /****************** Expression name resolution ******************/
1641 :
1642 : /* Returns 0 if a symbol was not declared with a type or
1643 : attribute declaration statement, nonzero otherwise. */
1644 :
1645 : static bool
1646 744650 : was_declared (gfc_symbol *sym)
1647 : {
1648 744650 : symbol_attribute a;
1649 :
1650 744650 : a = sym->attr;
1651 :
1652 744650 : if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1653 : return 1;
1654 :
1655 630963 : if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1656 622209 : || a.optional || a.pointer || a.save || a.target || a.volatile_
1657 622207 : || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1658 622153 : || a.asynchronous || a.codimension || a.subroutine)
1659 95069 : return 1;
1660 :
1661 : return 0;
1662 : }
1663 :
1664 :
1665 : /* Determine if a symbol is generic or not. */
1666 :
1667 : static int
1668 413502 : generic_sym (gfc_symbol *sym)
1669 : {
1670 413502 : gfc_symbol *s;
1671 :
1672 413502 : if (sym->attr.generic ||
1673 384251 : (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1674 30314 : return 1;
1675 :
1676 383188 : if (was_declared (sym) || sym->ns->parent == NULL)
1677 : return 0;
1678 :
1679 77153 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1680 :
1681 77153 : if (s != NULL)
1682 : {
1683 133 : if (s == sym)
1684 : return 0;
1685 : else
1686 132 : return generic_sym (s);
1687 : }
1688 :
1689 : return 0;
1690 : }
1691 :
1692 :
1693 : /* Determine if a symbol is specific or not. */
1694 :
1695 : static int
1696 383100 : specific_sym (gfc_symbol *sym)
1697 : {
1698 383100 : gfc_symbol *s;
1699 :
1700 383100 : if (sym->attr.if_source == IFSRC_IFBODY
1701 371781 : || sym->attr.proc == PROC_MODULE
1702 : || sym->attr.proc == PROC_INTERNAL
1703 : || sym->attr.proc == PROC_ST_FUNCTION
1704 295400 : || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1705 677769 : || sym->attr.external)
1706 90832 : return 1;
1707 :
1708 292268 : if (was_declared (sym) || sym->ns->parent == NULL)
1709 : return 0;
1710 :
1711 77051 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1712 :
1713 77051 : return (s == NULL) ? 0 : specific_sym (s);
1714 : }
1715 :
1716 :
1717 : /* Figure out if the procedure is specific, generic or unknown. */
1718 :
1719 : enum proc_type
1720 : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1721 :
1722 : static proc_type
1723 413224 : procedure_kind (gfc_symbol *sym)
1724 : {
1725 413224 : if (generic_sym (sym))
1726 : return PTYPE_GENERIC;
1727 :
1728 383053 : if (specific_sym (sym))
1729 90832 : return PTYPE_SPECIFIC;
1730 :
1731 : return PTYPE_UNKNOWN;
1732 : }
1733 :
1734 : /* Check references to assumed size arrays. The flag need_full_assumed_size
1735 : is nonzero when matching actual arguments. */
1736 :
1737 : static int need_full_assumed_size = 0;
1738 :
1739 : static bool
1740 1425333 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1741 : {
1742 1425333 : if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1743 : return false;
1744 :
1745 : /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1746 : What should it be? */
1747 3800 : if (e->ref
1748 3798 : && e->ref->u.ar.as
1749 3797 : && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1750 3302 : && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1751 3302 : && (e->ref->u.ar.type == AR_FULL))
1752 : {
1753 25 : gfc_error ("The upper bound in the last dimension must "
1754 : "appear in the reference to the assumed size "
1755 : "array %qs at %L", sym->name, &e->where);
1756 25 : return true;
1757 : }
1758 : return false;
1759 : }
1760 :
1761 :
1762 : /* Look for bad assumed size array references in argument expressions
1763 : of elemental and array valued intrinsic procedures. Since this is
1764 : called from procedure resolution functions, it only recurses at
1765 : operators. */
1766 :
1767 : static bool
1768 229639 : resolve_assumed_size_actual (gfc_expr *e)
1769 : {
1770 229639 : if (e == NULL)
1771 : return false;
1772 :
1773 229072 : switch (e->expr_type)
1774 : {
1775 110469 : case EXPR_VARIABLE:
1776 110469 : if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1777 : return true;
1778 : break;
1779 :
1780 48585 : case EXPR_OP:
1781 48585 : if (resolve_assumed_size_actual (e->value.op.op1)
1782 48585 : || resolve_assumed_size_actual (e->value.op.op2))
1783 0 : return true;
1784 : break;
1785 :
1786 : default:
1787 : break;
1788 : }
1789 : return false;
1790 : }
1791 :
1792 :
1793 : /* Check a generic procedure, passed as an actual argument, to see if
1794 : there is a matching specific name. If none, it is an error, and if
1795 : more than one, the reference is ambiguous. */
1796 : static int
1797 8 : count_specific_procs (gfc_expr *e)
1798 : {
1799 8 : int n;
1800 8 : gfc_interface *p;
1801 8 : gfc_symbol *sym;
1802 :
1803 8 : n = 0;
1804 8 : sym = e->symtree->n.sym;
1805 :
1806 22 : for (p = sym->generic; p; p = p->next)
1807 14 : if (strcmp (sym->name, p->sym->name) == 0)
1808 : {
1809 8 : e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1810 : sym->name);
1811 8 : n++;
1812 : }
1813 :
1814 8 : if (n > 1)
1815 1 : gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1816 : &e->where);
1817 :
1818 8 : if (n == 0)
1819 1 : gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1820 : "argument at %L", sym->name, &e->where);
1821 :
1822 8 : return n;
1823 : }
1824 :
1825 :
1826 : /* See if a call to sym could possibly be a not allowed RECURSION because of
1827 : a missing RECURSIVE declaration. This means that either sym is the current
1828 : context itself, or sym is the parent of a contained procedure calling its
1829 : non-RECURSIVE containing procedure.
1830 : This also works if sym is an ENTRY. */
1831 :
1832 : static bool
1833 151729 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1834 : {
1835 151729 : gfc_symbol* proc_sym;
1836 151729 : gfc_symbol* context_proc;
1837 151729 : gfc_namespace* real_context;
1838 :
1839 151729 : if (sym->attr.flavor == FL_PROGRAM
1840 : || gfc_fl_struct (sym->attr.flavor))
1841 : return false;
1842 :
1843 : /* If we've got an ENTRY, find real procedure. */
1844 151728 : if (sym->attr.entry && sym->ns->entries)
1845 45 : proc_sym = sym->ns->entries->sym;
1846 : else
1847 : proc_sym = sym;
1848 :
1849 : /* If sym is RECURSIVE, all is well of course. */
1850 151728 : if (proc_sym->attr.recursive || flag_recursive)
1851 : return false;
1852 :
1853 : /* Find the context procedure's "real" symbol if it has entries.
1854 : We look for a procedure symbol, so recurse on the parents if we don't
1855 : find one (like in case of a BLOCK construct). */
1856 1850 : for (real_context = context; ; real_context = real_context->parent)
1857 : {
1858 : /* We should find something, eventually! */
1859 128691 : gcc_assert (real_context);
1860 :
1861 128691 : context_proc = (real_context->entries ? real_context->entries->sym
1862 : : real_context->proc_name);
1863 :
1864 : /* In some special cases, there may not be a proc_name, like for this
1865 : invalid code:
1866 : real(bad_kind()) function foo () ...
1867 : when checking the call to bad_kind ().
1868 : In these cases, we simply return here and assume that the
1869 : call is ok. */
1870 128691 : if (!context_proc)
1871 : return false;
1872 :
1873 128427 : if (context_proc->attr.flavor != FL_LABEL)
1874 : break;
1875 : }
1876 :
1877 : /* A call from sym's body to itself is recursion, of course. */
1878 126577 : if (context_proc == proc_sym)
1879 : return true;
1880 :
1881 : /* The same is true if context is a contained procedure and sym the
1882 : containing one. */
1883 126562 : if (context_proc->attr.contained)
1884 : {
1885 21243 : gfc_symbol* parent_proc;
1886 :
1887 21243 : gcc_assert (context->parent);
1888 21243 : parent_proc = (context->parent->entries ? context->parent->entries->sym
1889 : : context->parent->proc_name);
1890 :
1891 21243 : if (parent_proc == proc_sym)
1892 9 : return true;
1893 : }
1894 :
1895 : return false;
1896 : }
1897 :
1898 :
1899 : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1900 : its typespec and formal argument list. */
1901 :
1902 : bool
1903 46361 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1904 : {
1905 46361 : gfc_intrinsic_sym* isym = NULL;
1906 46361 : const char* symstd;
1907 :
1908 46361 : if (sym->resolve_symbol_called >= 2)
1909 : return true;
1910 :
1911 36428 : sym->resolve_symbol_called = 2;
1912 :
1913 : /* Already resolved. */
1914 36428 : if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1915 : return true;
1916 :
1917 : /* We already know this one is an intrinsic, so we don't call
1918 : gfc_is_intrinsic for full checking but rather use gfc_find_function and
1919 : gfc_find_subroutine directly to check whether it is a function or
1920 : subroutine. */
1921 :
1922 28492 : if (sym->intmod_sym_id && sym->attr.subroutine)
1923 : {
1924 12431 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1925 12431 : isym = gfc_intrinsic_subroutine_by_id (id);
1926 12431 : }
1927 16061 : else if (sym->intmod_sym_id)
1928 : {
1929 12362 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1930 12362 : isym = gfc_intrinsic_function_by_id (id);
1931 : }
1932 3699 : else if (!sym->attr.subroutine)
1933 3612 : isym = gfc_find_function (sym->name);
1934 :
1935 28405 : if (isym && !sym->attr.subroutine)
1936 : {
1937 15929 : if (sym->ts.type != BT_UNKNOWN && warn_surprising
1938 24 : && !sym->attr.implicit_type)
1939 10 : gfc_warning (OPT_Wsurprising,
1940 : "Type specified for intrinsic function %qs at %L is"
1941 : " ignored", sym->name, &sym->declared_at);
1942 :
1943 20162 : if (!sym->attr.function &&
1944 4233 : !gfc_add_function(&sym->attr, sym->name, loc))
1945 : return false;
1946 :
1947 15929 : sym->ts = isym->ts;
1948 : }
1949 12563 : else if (isym || (isym = gfc_find_subroutine (sym->name)))
1950 : {
1951 12560 : if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1952 : {
1953 1 : gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1954 : " specifier", sym->name, &sym->declared_at);
1955 1 : return false;
1956 : }
1957 :
1958 12600 : if (!sym->attr.subroutine &&
1959 41 : !gfc_add_subroutine(&sym->attr, sym->name, loc))
1960 : return false;
1961 : }
1962 : else
1963 : {
1964 3 : gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1965 : &sym->declared_at);
1966 3 : return false;
1967 : }
1968 :
1969 28487 : gfc_copy_formal_args_intr (sym, isym, NULL);
1970 :
1971 28487 : sym->attr.pure = isym->pure;
1972 28487 : sym->attr.elemental = isym->elemental;
1973 :
1974 : /* Check it is actually available in the standard settings. */
1975 28487 : if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1976 : {
1977 31 : gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1978 : "available in the current standard settings but %s. Use "
1979 : "an appropriate %<-std=*%> option or enable "
1980 : "%<-fall-intrinsics%> in order to use it.",
1981 : sym->name, &sym->declared_at, symstd);
1982 31 : return false;
1983 : }
1984 :
1985 : return true;
1986 : }
1987 :
1988 :
1989 : /* Resolve a procedure expression, like passing it to a called procedure or as
1990 : RHS for a procedure pointer assignment. */
1991 :
1992 : static bool
1993 1327940 : resolve_procedure_expression (gfc_expr* expr)
1994 : {
1995 1327940 : gfc_symbol* sym;
1996 :
1997 1327940 : if (expr->expr_type != EXPR_VARIABLE)
1998 : return true;
1999 1327923 : gcc_assert (expr->symtree);
2000 :
2001 1327923 : sym = expr->symtree->n.sym;
2002 :
2003 1327923 : if (sym->attr.intrinsic)
2004 1346 : gfc_resolve_intrinsic (sym, &expr->where);
2005 :
2006 1327923 : if (sym->attr.flavor != FL_PROCEDURE
2007 31686 : || (sym->attr.function && sym->result == sym))
2008 : return true;
2009 :
2010 : /* A non-RECURSIVE procedure that is used as procedure expression within its
2011 : own body is in danger of being called recursively. */
2012 17135 : if (is_illegal_recursion (sym, gfc_current_ns))
2013 : {
2014 10 : if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
2015 0 : gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
2016 : " possibly calling itself recursively in procedure %qs. "
2017 : " Declare it RECURSIVE or use %<-frecursive%>",
2018 0 : sym->name, sym->module, gfc_current_ns->proc_name->name);
2019 : else
2020 10 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
2021 : " itself recursively. Declare it RECURSIVE or use"
2022 : " %<-frecursive%>", sym->name, &expr->where);
2023 : }
2024 :
2025 : return true;
2026 : }
2027 :
2028 :
2029 : /* Check that name is not a derived type. */
2030 :
2031 : static bool
2032 3403 : is_dt_name (const char *name)
2033 : {
2034 3403 : gfc_symbol *dt_list, *dt_first;
2035 :
2036 3403 : dt_list = dt_first = gfc_derived_types;
2037 5850 : for (; dt_list; dt_list = dt_list->dt_next)
2038 : {
2039 3563 : if (strcmp(dt_list->name, name) == 0)
2040 : return true;
2041 3560 : if (dt_first == dt_list->dt_next)
2042 : break;
2043 : }
2044 : return false;
2045 : }
2046 :
2047 :
2048 : /* Resolve an actual argument list. Most of the time, this is just
2049 : resolving the expressions in the list.
2050 : The exception is that we sometimes have to decide whether arguments
2051 : that look like procedure arguments are really simple variable
2052 : references. */
2053 :
2054 : static bool
2055 427356 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2056 : bool no_formal_args)
2057 : {
2058 427356 : gfc_symbol *sym = NULL;
2059 427356 : gfc_symtree *parent_st;
2060 427356 : gfc_expr *e;
2061 427356 : gfc_component *comp;
2062 427356 : int save_need_full_assumed_size;
2063 427356 : bool return_value = false;
2064 427356 : bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2065 :
2066 427356 : actual_arg = true;
2067 427356 : first_actual_arg = true;
2068 :
2069 1097248 : for (; arg; arg = arg->next)
2070 : {
2071 669993 : e = arg->expr;
2072 669993 : if (e == NULL)
2073 : {
2074 : /* Check the label is a valid branching target. */
2075 2436 : if (arg->label)
2076 : {
2077 236 : if (arg->label->defined == ST_LABEL_UNKNOWN)
2078 : {
2079 0 : gfc_error ("Label %d referenced at %L is never defined",
2080 : arg->label->value, &arg->label->where);
2081 0 : goto cleanup;
2082 : }
2083 : }
2084 2436 : first_actual_arg = false;
2085 2436 : continue;
2086 : }
2087 :
2088 667557 : if (e->expr_type == EXPR_VARIABLE
2089 294621 : && e->symtree->n.sym->attr.generic
2090 8 : && no_formal_args
2091 667562 : && count_specific_procs (e) != 1)
2092 2 : goto cleanup;
2093 :
2094 667555 : if (e->ts.type != BT_PROCEDURE)
2095 : {
2096 594958 : save_need_full_assumed_size = need_full_assumed_size;
2097 594958 : if (e->expr_type != EXPR_VARIABLE)
2098 372936 : need_full_assumed_size = 0;
2099 594958 : if (!gfc_resolve_expr (e))
2100 60 : goto cleanup;
2101 594898 : need_full_assumed_size = save_need_full_assumed_size;
2102 594898 : goto argument_list;
2103 : }
2104 :
2105 : /* See if the expression node should really be a variable reference. */
2106 :
2107 72597 : sym = e->symtree->n.sym;
2108 :
2109 72597 : if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2110 : {
2111 3 : gfc_error ("Derived type %qs is used as an actual "
2112 : "argument at %L", sym->name, &e->where);
2113 3 : goto cleanup;
2114 : }
2115 :
2116 72594 : if (sym->attr.flavor == FL_PROCEDURE
2117 69194 : || sym->attr.intrinsic
2118 69194 : || sym->attr.external)
2119 : {
2120 3400 : int actual_ok;
2121 :
2122 : /* If a procedure is not already determined to be something else
2123 : check if it is intrinsic. */
2124 3400 : if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2125 1254 : sym->attr.intrinsic = 1;
2126 :
2127 3400 : if (sym->attr.proc == PROC_ST_FUNCTION)
2128 : {
2129 2 : gfc_error ("Statement function %qs at %L is not allowed as an "
2130 : "actual argument", sym->name, &e->where);
2131 : }
2132 :
2133 6800 : actual_ok = gfc_intrinsic_actual_ok (sym->name,
2134 3400 : sym->attr.subroutine);
2135 3400 : if (sym->attr.intrinsic && actual_ok == 0)
2136 : {
2137 0 : gfc_error ("Intrinsic %qs at %L is not allowed as an "
2138 : "actual argument", sym->name, &e->where);
2139 : }
2140 :
2141 3400 : if (sym->attr.contained && !sym->attr.use_assoc
2142 432 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
2143 : {
2144 244 : if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2145 : " used as actual argument at %L",
2146 : sym->name, &e->where))
2147 3 : goto cleanup;
2148 : }
2149 :
2150 3397 : if (sym->attr.elemental && !sym->attr.intrinsic)
2151 : {
2152 2 : gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2153 : "allowed as an actual argument at %L", sym->name,
2154 : &e->where);
2155 : }
2156 :
2157 : /* Check if a generic interface has a specific procedure
2158 : with the same name before emitting an error. */
2159 3397 : if (sym->attr.generic && count_specific_procs (e) != 1)
2160 0 : goto cleanup;
2161 :
2162 : /* Just in case a specific was found for the expression. */
2163 3397 : sym = e->symtree->n.sym;
2164 :
2165 : /* If the symbol is the function that names the current (or
2166 : parent) scope, then we really have a variable reference. */
2167 :
2168 3397 : if (gfc_is_function_return_value (sym, sym->ns))
2169 0 : goto got_variable;
2170 :
2171 : /* If all else fails, see if we have a specific intrinsic. */
2172 3397 : if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2173 : {
2174 0 : gfc_intrinsic_sym *isym;
2175 :
2176 0 : isym = gfc_find_function (sym->name);
2177 0 : if (isym == NULL || !isym->specific)
2178 : {
2179 0 : gfc_error ("Unable to find a specific INTRINSIC procedure "
2180 : "for the reference %qs at %L", sym->name,
2181 : &e->where);
2182 0 : goto cleanup;
2183 : }
2184 0 : sym->ts = isym->ts;
2185 0 : sym->attr.intrinsic = 1;
2186 0 : sym->attr.function = 1;
2187 : }
2188 :
2189 3397 : if (!gfc_resolve_expr (e))
2190 0 : goto cleanup;
2191 3397 : goto argument_list;
2192 : }
2193 :
2194 : /* See if the name is a module procedure in a parent unit. */
2195 :
2196 69194 : if (was_declared (sym) || sym->ns->parent == NULL)
2197 69101 : goto got_variable;
2198 :
2199 93 : if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2200 : {
2201 0 : gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2202 0 : goto cleanup;
2203 : }
2204 :
2205 93 : if (parent_st == NULL)
2206 93 : goto got_variable;
2207 :
2208 0 : sym = parent_st->n.sym;
2209 0 : e->symtree = parent_st; /* Point to the right thing. */
2210 :
2211 0 : if (sym->attr.flavor == FL_PROCEDURE
2212 0 : || sym->attr.intrinsic
2213 0 : || sym->attr.external)
2214 : {
2215 0 : if (!gfc_resolve_expr (e))
2216 0 : goto cleanup;
2217 0 : goto argument_list;
2218 : }
2219 :
2220 0 : got_variable:
2221 69194 : e->expr_type = EXPR_VARIABLE;
2222 69194 : e->ts = sym->ts;
2223 69194 : if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2224 35919 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2225 3834 : && CLASS_DATA (sym)->as))
2226 : {
2227 38815 : gfc_array_spec *as
2228 36045 : = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2229 36045 : e->rank = as->rank;
2230 36045 : e->corank = as->corank;
2231 36045 : e->ref = gfc_get_ref ();
2232 36045 : e->ref->type = REF_ARRAY;
2233 36045 : e->ref->u.ar.type = AR_FULL;
2234 36045 : e->ref->u.ar.as = as;
2235 : }
2236 :
2237 : /* These symbols are set untyped by calls to gfc_set_default_type
2238 : with 'error_flag' = false. Reset the untyped attribute so that
2239 : the error will be generated in gfc_resolve_expr. */
2240 69194 : if (e->expr_type == EXPR_VARIABLE
2241 69194 : && sym->ts.type == BT_UNKNOWN
2242 36 : && sym->attr.untyped)
2243 5 : sym->attr.untyped = 0;
2244 :
2245 : /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2246 : primary.cc (match_actual_arg). If above code determines that it
2247 : is a variable instead, it needs to be resolved as it was not
2248 : done at the beginning of this function. */
2249 69194 : save_need_full_assumed_size = need_full_assumed_size;
2250 69194 : if (e->expr_type != EXPR_VARIABLE)
2251 0 : need_full_assumed_size = 0;
2252 69194 : if (!gfc_resolve_expr (e))
2253 22 : goto cleanup;
2254 69172 : need_full_assumed_size = save_need_full_assumed_size;
2255 :
2256 667467 : argument_list:
2257 : /* Check argument list functions %VAL, %LOC and %REF. There is
2258 : nothing to do for %REF. */
2259 667467 : if (arg->name && arg->name[0] == '%')
2260 : {
2261 42 : if (strcmp ("%VAL", arg->name) == 0)
2262 : {
2263 28 : if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2264 : {
2265 2 : gfc_error ("By-value argument at %L is not of numeric "
2266 : "type", &e->where);
2267 2 : goto cleanup;
2268 : }
2269 :
2270 26 : if (e->rank)
2271 : {
2272 1 : gfc_error ("By-value argument at %L cannot be an array or "
2273 : "an array section", &e->where);
2274 1 : goto cleanup;
2275 : }
2276 :
2277 : /* Intrinsics are still PROC_UNKNOWN here. However,
2278 : since same file external procedures are not resolvable
2279 : in gfortran, it is a good deal easier to leave them to
2280 : intrinsic.cc. */
2281 25 : if (ptype != PROC_UNKNOWN
2282 25 : && ptype != PROC_DUMMY
2283 9 : && ptype != PROC_EXTERNAL
2284 9 : && ptype != PROC_MODULE)
2285 : {
2286 3 : gfc_error ("By-value argument at %L is not allowed "
2287 : "in this context", &e->where);
2288 3 : goto cleanup;
2289 : }
2290 : }
2291 :
2292 : /* Statement functions have already been excluded above. */
2293 14 : else if (strcmp ("%LOC", arg->name) == 0
2294 8 : && e->ts.type == BT_PROCEDURE)
2295 : {
2296 0 : if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2297 : {
2298 0 : gfc_error ("Passing internal procedure at %L by location "
2299 : "not allowed", &e->where);
2300 0 : goto cleanup;
2301 : }
2302 : }
2303 : }
2304 :
2305 667461 : comp = gfc_get_proc_ptr_comp(e);
2306 667461 : if (e->expr_type == EXPR_VARIABLE
2307 293243 : && comp && comp->attr.elemental)
2308 : {
2309 1 : gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2310 : "allowed as an actual argument at %L", comp->name,
2311 : &e->where);
2312 : }
2313 :
2314 : /* Fortran 2008, C1237. */
2315 293243 : if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2316 667906 : && gfc_has_ultimate_pointer (e))
2317 : {
2318 3 : gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2319 : "component", &e->where);
2320 3 : goto cleanup;
2321 : }
2322 :
2323 667458 : if (e->expr_type == EXPR_VARIABLE
2324 293240 : && e->ts.type == BT_PROCEDURE
2325 3397 : && no_formal_args
2326 1505 : && sym->attr.flavor == FL_PROCEDURE
2327 1505 : && sym->attr.if_source == IFSRC_UNKNOWN
2328 142 : && !sym->attr.external
2329 2 : && !sym->attr.intrinsic
2330 2 : && !sym->attr.artificial
2331 2 : && !sym->ts.interface)
2332 : {
2333 : /* Emit a warning for -std=legacy and an error otherwise. */
2334 2 : if (gfc_option.warn_std == 0)
2335 0 : gfc_warning (0, "Procedure %qs at %L used as actual argument but "
2336 : "does neither have an explicit interface nor the "
2337 : "EXTERNAL attribute", sym->name, &e->where);
2338 : else
2339 : {
2340 2 : gfc_error ("Procedure %qs at %L used as actual argument but "
2341 : "does neither have an explicit interface nor the "
2342 : "EXTERNAL attribute", sym->name, &e->where);
2343 2 : goto cleanup;
2344 : }
2345 : }
2346 :
2347 667456 : first_actual_arg = false;
2348 : }
2349 :
2350 : return_value = true;
2351 :
2352 427356 : cleanup:
2353 427356 : actual_arg = actual_arg_sav;
2354 427356 : first_actual_arg = first_actual_arg_sav;
2355 :
2356 427356 : return return_value;
2357 : }
2358 :
2359 :
2360 : /* Do the checks of the actual argument list that are specific to elemental
2361 : procedures. If called with c == NULL, we have a function, otherwise if
2362 : expr == NULL, we have a subroutine. */
2363 :
2364 : static bool
2365 325391 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2366 : {
2367 325391 : gfc_actual_arglist *arg0;
2368 325391 : gfc_actual_arglist *arg;
2369 325391 : gfc_symbol *esym = NULL;
2370 325391 : gfc_intrinsic_sym *isym = NULL;
2371 325391 : gfc_expr *e = NULL;
2372 325391 : gfc_intrinsic_arg *iformal = NULL;
2373 325391 : gfc_formal_arglist *eformal = NULL;
2374 325391 : bool formal_optional = false;
2375 325391 : bool set_by_optional = false;
2376 325391 : int i;
2377 325391 : int rank = 0;
2378 :
2379 : /* Is this an elemental procedure? */
2380 325391 : if (expr && expr->value.function.actual != NULL)
2381 : {
2382 235815 : if (expr->value.function.esym != NULL
2383 43825 : && expr->value.function.esym->attr.elemental)
2384 : {
2385 : arg0 = expr->value.function.actual;
2386 : esym = expr->value.function.esym;
2387 : }
2388 219507 : else if (expr->value.function.isym != NULL
2389 190936 : && expr->value.function.isym->elemental)
2390 : {
2391 : arg0 = expr->value.function.actual;
2392 : isym = expr->value.function.isym;
2393 : }
2394 : else
2395 : return true;
2396 : }
2397 89576 : else if (c && c->ext.actual != NULL)
2398 : {
2399 70956 : arg0 = c->ext.actual;
2400 :
2401 70956 : if (c->resolved_sym)
2402 : esym = c->resolved_sym;
2403 : else
2404 323 : esym = c->symtree->n.sym;
2405 70956 : gcc_assert (esym);
2406 :
2407 70956 : if (!esym->attr.elemental)
2408 : return true;
2409 : }
2410 : else
2411 : return true;
2412 :
2413 : /* The rank of an elemental is the rank of its array argument(s). */
2414 173955 : for (arg = arg0; arg; arg = arg->next)
2415 : {
2416 112755 : if (arg->expr != NULL && arg->expr->rank != 0)
2417 : {
2418 10704 : rank = arg->expr->rank;
2419 10704 : if (arg->expr->expr_type == EXPR_VARIABLE
2420 5484 : && arg->expr->symtree->n.sym->attr.optional)
2421 10704 : set_by_optional = true;
2422 :
2423 : /* Function specific; set the result rank and shape. */
2424 10704 : if (expr)
2425 : {
2426 8302 : expr->rank = rank;
2427 8302 : expr->corank = arg->expr->corank;
2428 8302 : if (!expr->shape && arg->expr->shape)
2429 : {
2430 3944 : expr->shape = gfc_get_shape (rank);
2431 8683 : for (i = 0; i < rank; i++)
2432 4739 : mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2433 : }
2434 : }
2435 : break;
2436 : }
2437 : }
2438 :
2439 : /* If it is an array, it shall not be supplied as an actual argument
2440 : to an elemental procedure unless an array of the same rank is supplied
2441 : as an actual argument corresponding to a nonoptional dummy argument of
2442 : that elemental procedure(12.4.1.5). */
2443 71904 : formal_optional = false;
2444 71904 : if (isym)
2445 49389 : iformal = isym->formal;
2446 : else
2447 22515 : eformal = esym->formal;
2448 :
2449 190175 : for (arg = arg0; arg; arg = arg->next)
2450 : {
2451 118271 : if (eformal)
2452 : {
2453 40405 : if (eformal->sym && eformal->sym->attr.optional)
2454 40405 : formal_optional = true;
2455 40405 : eformal = eformal->next;
2456 : }
2457 77866 : else if (isym && iformal)
2458 : {
2459 67616 : if (iformal->optional)
2460 13418 : formal_optional = true;
2461 67616 : iformal = iformal->next;
2462 : }
2463 10250 : else if (isym)
2464 10242 : formal_optional = true;
2465 :
2466 118271 : if (pedantic && arg->expr != NULL
2467 67555 : && arg->expr->expr_type == EXPR_VARIABLE
2468 31902 : && arg->expr->symtree->n.sym->attr.optional
2469 572 : && formal_optional
2470 479 : && arg->expr->rank
2471 153 : && (set_by_optional || arg->expr->rank != rank)
2472 42 : && !(isym && isym->id == GFC_ISYM_CONVERSION))
2473 : {
2474 114 : bool t = false;
2475 : gfc_actual_arglist *a;
2476 :
2477 : /* Scan the argument list for a non-optional argument with the
2478 : same rank as arg. */
2479 114 : for (a = arg0; a; a = a->next)
2480 87 : if (a != arg
2481 45 : && a->expr->rank == arg->expr->rank
2482 39 : && (a->expr->expr_type != EXPR_VARIABLE
2483 37 : || (a->expr->expr_type == EXPR_VARIABLE
2484 37 : && !a->expr->symtree->n.sym->attr.optional)))
2485 : {
2486 : t = true;
2487 : break;
2488 : }
2489 :
2490 42 : if (!t)
2491 27 : gfc_warning (OPT_Wpedantic,
2492 : "%qs at %L is an array and OPTIONAL; If it is not "
2493 : "present, then it cannot be the actual argument of "
2494 : "an ELEMENTAL procedure unless there is a non-optional"
2495 : " argument with the same rank "
2496 : "(Fortran 2018, 15.5.2.12)",
2497 : arg->expr->symtree->n.sym->name, &arg->expr->where);
2498 : }
2499 : }
2500 :
2501 190164 : for (arg = arg0; arg; arg = arg->next)
2502 : {
2503 118269 : if (arg->expr == NULL || arg->expr->rank == 0)
2504 104677 : continue;
2505 :
2506 : /* Being elemental, the last upper bound of an assumed size array
2507 : argument must be present. */
2508 13592 : if (resolve_assumed_size_actual (arg->expr))
2509 : return false;
2510 :
2511 : /* Elemental procedure's array actual arguments must conform. */
2512 13589 : if (e != NULL)
2513 : {
2514 2888 : if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2515 : return false;
2516 : }
2517 : else
2518 10701 : e = arg->expr;
2519 : }
2520 :
2521 : /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2522 : is an array, the intent inout/out variable needs to be also an array. */
2523 71895 : if (rank > 0 && esym && expr == NULL)
2524 7321 : for (eformal = esym->formal, arg = arg0; arg && eformal;
2525 4925 : arg = arg->next, eformal = eformal->next)
2526 4927 : if (eformal->sym
2527 4926 : && (eformal->sym->attr.intent == INTENT_OUT
2528 3844 : || eformal->sym->attr.intent == INTENT_INOUT)
2529 1710 : && arg->expr && arg->expr->rank == 0)
2530 : {
2531 2 : gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2532 : "ELEMENTAL subroutine %qs is a scalar, but another "
2533 : "actual argument is an array", &arg->expr->where,
2534 : (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2535 : : "INOUT", eformal->sym->name, esym->name);
2536 2 : return false;
2537 : }
2538 : return true;
2539 : }
2540 :
2541 :
2542 : /* This function does the checking of references to global procedures
2543 : as defined in sections 18.1 and 14.1, respectively, of the Fortran
2544 : 77 and 95 standards. It checks for a gsymbol for the name, making
2545 : one if it does not already exist. If it already exists, then the
2546 : reference being resolved must correspond to the type of gsymbol.
2547 : Otherwise, the new symbol is equipped with the attributes of the
2548 : reference. The corresponding code that is called in creating
2549 : global entities is parse.cc.
2550 :
2551 : In addition, for all but -std=legacy, the gsymbols are used to
2552 : check the interfaces of external procedures from the same file.
2553 : The namespace of the gsymbol is resolved and then, once this is
2554 : done the interface is checked. */
2555 :
2556 :
2557 : static bool
2558 14943 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2559 : {
2560 14943 : if (!gsym_ns->proc_name->attr.recursive)
2561 : return true;
2562 :
2563 151 : if (sym->ns == gsym_ns)
2564 : return false;
2565 :
2566 151 : if (sym->ns->parent && sym->ns->parent == gsym_ns)
2567 0 : return false;
2568 :
2569 : return true;
2570 : }
2571 :
2572 : static bool
2573 14943 : not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2574 : {
2575 14943 : if (gsym_ns->entries)
2576 : {
2577 : gfc_entry_list *entry = gsym_ns->entries;
2578 :
2579 3312 : for (; entry; entry = entry->next)
2580 : {
2581 2333 : if (strcmp (sym->name, entry->sym->name) == 0)
2582 : {
2583 971 : if (strcmp (gsym_ns->proc_name->name,
2584 971 : sym->ns->proc_name->name) == 0)
2585 : return false;
2586 :
2587 971 : if (sym->ns->parent
2588 0 : && strcmp (gsym_ns->proc_name->name,
2589 0 : sym->ns->parent->proc_name->name) == 0)
2590 : return false;
2591 : }
2592 : }
2593 : }
2594 : return true;
2595 : }
2596 :
2597 :
2598 : /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2599 :
2600 : bool
2601 15731 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2602 : {
2603 15731 : gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2604 :
2605 58844 : for ( ; arg; arg = arg->next)
2606 : {
2607 27766 : if (!arg->sym)
2608 157 : continue;
2609 :
2610 27609 : if (arg->sym->attr.allocatable) /* (2a) */
2611 : {
2612 0 : strncpy (errmsg, _("allocatable argument"), err_len);
2613 0 : return true;
2614 : }
2615 27609 : else if (arg->sym->attr.asynchronous)
2616 : {
2617 0 : strncpy (errmsg, _("asynchronous argument"), err_len);
2618 0 : return true;
2619 : }
2620 27609 : else if (arg->sym->attr.optional)
2621 : {
2622 75 : strncpy (errmsg, _("optional argument"), err_len);
2623 75 : return true;
2624 : }
2625 27534 : else if (arg->sym->attr.pointer)
2626 : {
2627 12 : strncpy (errmsg, _("pointer argument"), err_len);
2628 12 : return true;
2629 : }
2630 27522 : else if (arg->sym->attr.target)
2631 : {
2632 72 : strncpy (errmsg, _("target argument"), err_len);
2633 72 : return true;
2634 : }
2635 27450 : else if (arg->sym->attr.value)
2636 : {
2637 12 : strncpy (errmsg, _("value argument"), err_len);
2638 12 : return true;
2639 : }
2640 27438 : else if (arg->sym->attr.volatile_)
2641 : {
2642 1 : strncpy (errmsg, _("volatile argument"), err_len);
2643 1 : return true;
2644 : }
2645 27437 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2646 : {
2647 45 : strncpy (errmsg, _("assumed-shape argument"), err_len);
2648 45 : return true;
2649 : }
2650 27392 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2651 : {
2652 1 : strncpy (errmsg, _("assumed-rank argument"), err_len);
2653 1 : return true;
2654 : }
2655 27391 : else if (arg->sym->attr.codimension) /* (2c) */
2656 : {
2657 1 : strncpy (errmsg, _("coarray argument"), err_len);
2658 1 : return true;
2659 : }
2660 27390 : else if (false) /* (2d) TODO: parametrized derived type */
2661 : {
2662 : strncpy (errmsg, _("parametrized derived type argument"), err_len);
2663 : return true;
2664 : }
2665 27390 : else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2666 : {
2667 164 : strncpy (errmsg, _("polymorphic argument"), err_len);
2668 164 : return true;
2669 : }
2670 27226 : else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2671 : {
2672 0 : strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2673 0 : return true;
2674 : }
2675 27226 : else if (arg->sym->ts.type == BT_ASSUMED)
2676 : {
2677 : /* As assumed-type is unlimited polymorphic (cf. above).
2678 : See also TS 29113, Note 6.1. */
2679 1 : strncpy (errmsg, _("assumed-type argument"), err_len);
2680 1 : return true;
2681 : }
2682 : }
2683 :
2684 15347 : if (sym->attr.function)
2685 : {
2686 3457 : gfc_symbol *res = sym->result ? sym->result : sym;
2687 :
2688 3457 : if (res->attr.dimension) /* (3a) */
2689 : {
2690 93 : strncpy (errmsg, _("array result"), err_len);
2691 93 : return true;
2692 : }
2693 3364 : else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2694 : {
2695 38 : strncpy (errmsg, _("pointer or allocatable result"), err_len);
2696 38 : return true;
2697 : }
2698 3326 : else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2699 347 : && res->ts.u.cl->length
2700 166 : && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2701 : {
2702 12 : strncpy (errmsg, _("result with non-constant character length"), err_len);
2703 12 : return true;
2704 : }
2705 : }
2706 :
2707 15204 : if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2708 : {
2709 7 : strncpy (errmsg, _("elemental procedure"), err_len);
2710 7 : return true;
2711 : }
2712 15197 : else if (sym->attr.is_bind_c) /* (5) */
2713 : {
2714 0 : strncpy (errmsg, _("bind(c) procedure"), err_len);
2715 0 : return true;
2716 : }
2717 :
2718 : return false;
2719 : }
2720 :
2721 :
2722 : static void
2723 29454 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2724 : {
2725 29454 : gfc_gsymbol * gsym;
2726 29454 : gfc_namespace *ns;
2727 29454 : enum gfc_symbol_type type;
2728 29454 : char reason[200];
2729 :
2730 29454 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2731 :
2732 29454 : gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2733 29454 : sym->binding_label != NULL);
2734 :
2735 29454 : if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2736 9 : gfc_global_used (gsym, where);
2737 :
2738 29454 : if ((sym->attr.if_source == IFSRC_UNKNOWN
2739 9287 : || sym->attr.if_source == IFSRC_IFBODY)
2740 25053 : && gsym->type != GSYM_UNKNOWN
2741 22883 : && !gsym->binding_label
2742 20585 : && gsym->ns
2743 14943 : && gsym->ns->proc_name
2744 14943 : && not_in_recursive (sym, gsym->ns)
2745 44397 : && not_entry_self_reference (sym, gsym->ns))
2746 : {
2747 14943 : gfc_symbol *def_sym;
2748 14943 : def_sym = gsym->ns->proc_name;
2749 :
2750 14943 : if (gsym->ns->resolved != -1)
2751 : {
2752 :
2753 : /* Resolve the gsymbol namespace if needed. */
2754 14921 : if (!gsym->ns->resolved)
2755 : {
2756 2775 : gfc_symbol *old_dt_list;
2757 :
2758 : /* Stash away derived types so that the backend_decls
2759 : do not get mixed up. */
2760 2775 : old_dt_list = gfc_derived_types;
2761 2775 : gfc_derived_types = NULL;
2762 :
2763 2775 : gfc_resolve (gsym->ns);
2764 :
2765 : /* Store the new derived types with the global namespace. */
2766 2775 : if (gfc_derived_types)
2767 306 : gsym->ns->derived_types = gfc_derived_types;
2768 :
2769 : /* Restore the derived types of this namespace. */
2770 2775 : gfc_derived_types = old_dt_list;
2771 : }
2772 :
2773 : /* Make sure that translation for the gsymbol occurs before
2774 : the procedure currently being resolved. */
2775 14921 : ns = gfc_global_ns_list;
2776 25310 : for (; ns && ns != gsym->ns; ns = ns->sibling)
2777 : {
2778 16913 : if (ns->sibling == gsym->ns)
2779 : {
2780 6524 : ns->sibling = gsym->ns->sibling;
2781 6524 : gsym->ns->sibling = gfc_global_ns_list;
2782 6524 : gfc_global_ns_list = gsym->ns;
2783 6524 : break;
2784 : }
2785 : }
2786 :
2787 : /* This can happen if a binding name has been specified. */
2788 14921 : if (gsym->binding_label && gsym->sym_name != def_sym->name)
2789 0 : gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2790 : }
2791 :
2792 : /* Look up the specific entry symbol so that interface checks use
2793 : the entry's own formal argument list, not the entry master's.
2794 : This must run even when resolved == -1 (recursive resolution in
2795 : progress), because def_sym starts as the namespace proc_name
2796 : which is the entry master with the combined formals. */
2797 14943 : if (def_sym->attr.entry_master || def_sym->attr.entry)
2798 : {
2799 979 : gfc_entry_list *entry;
2800 1699 : for (entry = gsym->ns->entries; entry; entry = entry->next)
2801 1699 : if (strcmp (entry->sym->name, sym->name) == 0)
2802 : {
2803 979 : def_sym = entry->sym;
2804 979 : break;
2805 : }
2806 : }
2807 :
2808 14943 : if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2809 : {
2810 6 : gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2811 : sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2812 6 : gfc_typename (&def_sym->ts));
2813 28 : goto done;
2814 : }
2815 :
2816 14937 : if (sym->attr.if_source == IFSRC_UNKNOWN
2817 14937 : && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2818 : {
2819 8 : gfc_error ("Explicit interface required for %qs at %L: %s",
2820 : sym->name, &sym->declared_at, reason);
2821 8 : goto done;
2822 : }
2823 :
2824 14929 : bool bad_result_characteristics;
2825 14929 : if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2826 : reason, sizeof(reason), NULL, NULL,
2827 : &bad_result_characteristics))
2828 : {
2829 : /* Turn erros into warnings with -std=gnu and -std=legacy,
2830 : unless a function returns a wrong type, which can lead
2831 : to all kinds of ICEs and wrong code. */
2832 :
2833 14 : if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2834 2 : && !bad_result_characteristics)
2835 2 : gfc_errors_to_warnings (true);
2836 :
2837 14 : gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2838 : sym->name, &sym->declared_at, reason);
2839 14 : sym->error = 1;
2840 14 : gfc_errors_to_warnings (false);
2841 14 : goto done;
2842 : }
2843 : }
2844 :
2845 29454 : done:
2846 :
2847 29454 : if (gsym->type == GSYM_UNKNOWN)
2848 : {
2849 3972 : gsym->type = type;
2850 3972 : gsym->where = *where;
2851 : }
2852 :
2853 29454 : gsym->used = 1;
2854 29454 : }
2855 :
2856 :
2857 : /************* Function resolution *************/
2858 :
2859 : /* Resolve a function call known to be generic.
2860 : Section 14.1.2.4.1. */
2861 :
2862 : static match
2863 27503 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2864 : {
2865 27503 : gfc_symbol *s;
2866 :
2867 27503 : if (sym->attr.generic)
2868 : {
2869 26398 : s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2870 26398 : if (s != NULL)
2871 : {
2872 19776 : expr->value.function.name = s->name;
2873 19776 : expr->value.function.esym = s;
2874 :
2875 19776 : if (s->ts.type != BT_UNKNOWN)
2876 19759 : expr->ts = s->ts;
2877 17 : else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2878 15 : expr->ts = s->result->ts;
2879 :
2880 19776 : if (s->as != NULL)
2881 : {
2882 55 : expr->rank = s->as->rank;
2883 55 : expr->corank = s->as->corank;
2884 : }
2885 19721 : else if (s->result != NULL && s->result->as != NULL)
2886 : {
2887 0 : expr->rank = s->result->as->rank;
2888 0 : expr->corank = s->result->as->corank;
2889 : }
2890 :
2891 19776 : gfc_set_sym_referenced (expr->value.function.esym);
2892 :
2893 19776 : return MATCH_YES;
2894 : }
2895 :
2896 : /* TODO: Need to search for elemental references in generic
2897 : interface. */
2898 : }
2899 :
2900 7727 : if (sym->attr.intrinsic)
2901 1062 : return gfc_intrinsic_func_interface (expr, 0);
2902 :
2903 : return MATCH_NO;
2904 : }
2905 :
2906 :
2907 : static bool
2908 27362 : resolve_generic_f (gfc_expr *expr)
2909 : {
2910 27362 : gfc_symbol *sym;
2911 27362 : match m;
2912 27362 : gfc_interface *intr = NULL;
2913 :
2914 27362 : sym = expr->symtree->n.sym;
2915 :
2916 27503 : for (;;)
2917 : {
2918 27503 : m = resolve_generic_f0 (expr, sym);
2919 27503 : if (m == MATCH_YES)
2920 : return true;
2921 6667 : else if (m == MATCH_ERROR)
2922 : return false;
2923 :
2924 6667 : generic:
2925 6670 : if (!intr)
2926 6641 : for (intr = sym->generic; intr; intr = intr->next)
2927 6557 : if (gfc_fl_struct (intr->sym->attr.flavor))
2928 : break;
2929 :
2930 6670 : if (sym->ns->parent == NULL)
2931 : break;
2932 289 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2933 :
2934 289 : if (sym == NULL)
2935 : break;
2936 144 : if (!generic_sym (sym))
2937 3 : goto generic;
2938 : }
2939 :
2940 : /* Last ditch attempt. See if the reference is to an intrinsic
2941 : that possesses a matching interface. 14.1.2.4 */
2942 6526 : if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2943 : {
2944 5 : if (gfc_init_expr_flag)
2945 1 : gfc_error ("Function %qs in initialization expression at %L "
2946 : "must be an intrinsic function",
2947 1 : expr->symtree->n.sym->name, &expr->where);
2948 : else
2949 4 : gfc_error ("There is no specific function for the generic %qs "
2950 4 : "at %L", expr->symtree->n.sym->name, &expr->where);
2951 5 : return false;
2952 : }
2953 :
2954 6521 : if (intr)
2955 : {
2956 6486 : if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2957 : NULL, false))
2958 : return false;
2959 6459 : if (!gfc_use_derived (expr->ts.u.derived))
2960 : return false;
2961 6459 : return resolve_structure_cons (expr, 0);
2962 : }
2963 :
2964 35 : m = gfc_intrinsic_func_interface (expr, 0);
2965 35 : if (m == MATCH_YES)
2966 : return true;
2967 :
2968 3 : if (m == MATCH_NO)
2969 3 : gfc_error ("Generic function %qs at %L is not consistent with a "
2970 3 : "specific intrinsic interface", expr->symtree->n.sym->name,
2971 : &expr->where);
2972 :
2973 : return false;
2974 : }
2975 :
2976 :
2977 : /* Resolve a function call known to be specific. */
2978 :
2979 : static match
2980 28074 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2981 : {
2982 28074 : match m;
2983 :
2984 28074 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2985 : {
2986 8123 : if (sym->attr.dummy)
2987 : {
2988 276 : sym->attr.proc = PROC_DUMMY;
2989 276 : goto found;
2990 : }
2991 :
2992 7847 : sym->attr.proc = PROC_EXTERNAL;
2993 7847 : goto found;
2994 : }
2995 :
2996 19951 : if (sym->attr.proc == PROC_MODULE
2997 : || sym->attr.proc == PROC_ST_FUNCTION
2998 : || sym->attr.proc == PROC_INTERNAL)
2999 19213 : goto found;
3000 :
3001 738 : if (sym->attr.intrinsic)
3002 : {
3003 731 : m = gfc_intrinsic_func_interface (expr, 1);
3004 731 : if (m == MATCH_YES)
3005 : return MATCH_YES;
3006 0 : if (m == MATCH_NO)
3007 0 : gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
3008 : "with an intrinsic", sym->name, &expr->where);
3009 :
3010 0 : return MATCH_ERROR;
3011 : }
3012 :
3013 : return MATCH_NO;
3014 :
3015 27336 : found:
3016 27336 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3017 :
3018 27336 : if (sym->result)
3019 27336 : expr->ts = sym->result->ts;
3020 : else
3021 0 : expr->ts = sym->ts;
3022 27336 : expr->value.function.name = sym->name;
3023 27336 : expr->value.function.esym = sym;
3024 : /* Prevent crash when sym->ts.u.derived->components is not set due to previous
3025 : error(s). */
3026 27336 : if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
3027 : return MATCH_ERROR;
3028 27335 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
3029 : {
3030 322 : expr->rank = CLASS_DATA (sym)->as->rank;
3031 322 : expr->corank = CLASS_DATA (sym)->as->corank;
3032 : }
3033 27013 : else if (sym->as != NULL)
3034 : {
3035 2323 : expr->rank = sym->as->rank;
3036 2323 : expr->corank = sym->as->corank;
3037 : }
3038 :
3039 : return MATCH_YES;
3040 : }
3041 :
3042 :
3043 : static bool
3044 28067 : resolve_specific_f (gfc_expr *expr)
3045 : {
3046 28067 : gfc_symbol *sym;
3047 28067 : match m;
3048 :
3049 28067 : sym = expr->symtree->n.sym;
3050 :
3051 28074 : for (;;)
3052 : {
3053 28074 : m = resolve_specific_f0 (sym, expr);
3054 28074 : if (m == MATCH_YES)
3055 : return true;
3056 8 : if (m == MATCH_ERROR)
3057 : return false;
3058 :
3059 7 : if (sym->ns->parent == NULL)
3060 : break;
3061 :
3062 7 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3063 :
3064 7 : if (sym == NULL)
3065 : break;
3066 : }
3067 :
3068 0 : gfc_error ("Unable to resolve the specific function %qs at %L",
3069 0 : expr->symtree->n.sym->name, &expr->where);
3070 :
3071 0 : return true;
3072 : }
3073 :
3074 : /* Recursively append candidate SYM to CANDIDATES. Store the number of
3075 : candidates in CANDIDATES_LEN. */
3076 :
3077 : static void
3078 212 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3079 : char **&candidates,
3080 : size_t &candidates_len)
3081 : {
3082 388 : gfc_symtree *p;
3083 :
3084 388 : if (sym == NULL)
3085 : return;
3086 388 : if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3087 126 : && sym->n.sym->attr.flavor == FL_PROCEDURE)
3088 51 : vec_push (candidates, candidates_len, sym->name);
3089 :
3090 388 : p = sym->left;
3091 388 : if (p)
3092 155 : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3093 :
3094 388 : p = sym->right;
3095 388 : if (p)
3096 : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3097 : }
3098 :
3099 :
3100 : /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3101 :
3102 : const char*
3103 57 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3104 : {
3105 57 : char **candidates = NULL;
3106 57 : size_t candidates_len = 0;
3107 57 : lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3108 57 : return gfc_closest_fuzzy_match (fn, candidates);
3109 : }
3110 :
3111 :
3112 : /* Resolve a procedure call not known to be generic nor specific. */
3113 :
3114 : static bool
3115 276351 : resolve_unknown_f (gfc_expr *expr)
3116 : {
3117 276351 : gfc_symbol *sym;
3118 276351 : gfc_typespec *ts;
3119 :
3120 276351 : sym = expr->symtree->n.sym;
3121 :
3122 276351 : if (sym->attr.dummy)
3123 : {
3124 289 : sym->attr.proc = PROC_DUMMY;
3125 289 : expr->value.function.name = sym->name;
3126 289 : goto set_type;
3127 : }
3128 :
3129 : /* See if we have an intrinsic function reference. */
3130 :
3131 276062 : if (gfc_is_intrinsic (sym, 0, expr->where))
3132 : {
3133 273805 : if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3134 : return true;
3135 : return false;
3136 : }
3137 :
3138 : /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3139 : /* Intrinsics were handled above, only non-intrinsics left here. */
3140 2257 : if (sym->attr.flavor == FL_PROCEDURE
3141 2254 : && sym->attr.implicit_type
3142 371 : && sym->ns
3143 371 : && sym->ns->has_implicit_none_export)
3144 : {
3145 3 : gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3146 : "for symbol %qs at %L", sym->name, &sym->declared_at);
3147 3 : sym->error = 1;
3148 3 : return false;
3149 : }
3150 :
3151 : /* The reference is to an external name. */
3152 :
3153 2254 : sym->attr.proc = PROC_EXTERNAL;
3154 2254 : expr->value.function.name = sym->name;
3155 2254 : expr->value.function.esym = expr->symtree->n.sym;
3156 :
3157 2254 : if (sym->as != NULL)
3158 : {
3159 1 : expr->rank = sym->as->rank;
3160 1 : expr->corank = sym->as->corank;
3161 : }
3162 :
3163 : /* Type of the expression is either the type of the symbol or the
3164 : default type of the symbol. */
3165 :
3166 2253 : set_type:
3167 2543 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3168 :
3169 2543 : if (sym->ts.type != BT_UNKNOWN)
3170 2492 : expr->ts = sym->ts;
3171 : else
3172 : {
3173 51 : ts = gfc_get_default_type (sym->name, sym->ns);
3174 :
3175 51 : if (ts->type == BT_UNKNOWN)
3176 : {
3177 41 : const char *guessed
3178 41 : = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3179 41 : if (guessed)
3180 3 : gfc_error ("Function %qs at %L has no IMPLICIT type"
3181 : "; did you mean %qs?",
3182 : sym->name, &expr->where, guessed);
3183 : else
3184 38 : gfc_error ("Function %qs at %L has no IMPLICIT type",
3185 : sym->name, &expr->where);
3186 41 : return false;
3187 : }
3188 : else
3189 10 : expr->ts = *ts;
3190 : }
3191 :
3192 : return true;
3193 : }
3194 :
3195 :
3196 : /* Return true, if the symbol is an external procedure. */
3197 : static bool
3198 852120 : is_external_proc (gfc_symbol *sym)
3199 : {
3200 850429 : if (!sym->attr.dummy && !sym->attr.contained
3201 742151 : && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3202 161563 : && sym->attr.proc != PROC_ST_FUNCTION
3203 160968 : && !sym->attr.proc_pointer
3204 159774 : && !sym->attr.use_assoc
3205 911059 : && sym->name)
3206 : return true;
3207 :
3208 : return false;
3209 : }
3210 :
3211 :
3212 : /* Figure out if a function reference is pure or not. Also set the name
3213 : of the function for a potential error message. Return nonzero if the
3214 : function is PURE, zero if not. */
3215 : static bool
3216 : pure_stmt_function (gfc_expr *, gfc_symbol *);
3217 :
3218 : bool
3219 256174 : gfc_pure_function (gfc_expr *e, const char **name)
3220 : {
3221 256174 : bool pure;
3222 256174 : gfc_component *comp;
3223 :
3224 256174 : *name = NULL;
3225 :
3226 256174 : if (e->symtree != NULL
3227 255820 : && e->symtree->n.sym != NULL
3228 255820 : && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3229 305 : return pure_stmt_function (e, e->symtree->n.sym);
3230 :
3231 255869 : comp = gfc_get_proc_ptr_comp (e);
3232 255869 : if (comp)
3233 : {
3234 465 : pure = gfc_pure (comp->ts.interface);
3235 465 : *name = comp->name;
3236 : }
3237 255404 : else if (e->value.function.esym)
3238 : {
3239 52657 : pure = gfc_pure (e->value.function.esym);
3240 52657 : *name = e->value.function.esym->name;
3241 : }
3242 202747 : else if (e->value.function.isym)
3243 : {
3244 403356 : pure = e->value.function.isym->pure
3245 201678 : || e->value.function.isym->elemental;
3246 201678 : *name = e->value.function.isym->name;
3247 : }
3248 1069 : else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
3249 : {
3250 : /* The function has been resolved, but esym is not yet set.
3251 : This can happen with functions as dummy argument. */
3252 287 : pure = e->symtree->n.sym->attr.pure;
3253 287 : *name = e->symtree->n.sym->name;
3254 : }
3255 : else
3256 : {
3257 : /* Implicit functions are not pure. */
3258 782 : pure = 0;
3259 782 : *name = e->value.function.name;
3260 : }
3261 :
3262 : return pure;
3263 : }
3264 :
3265 :
3266 : /* Check if the expression is a reference to an implicitly pure function. */
3267 :
3268 : bool
3269 38062 : gfc_implicit_pure_function (gfc_expr *e)
3270 : {
3271 38062 : gfc_component *comp = gfc_get_proc_ptr_comp (e);
3272 38062 : if (comp)
3273 449 : return gfc_implicit_pure (comp->ts.interface);
3274 37613 : else if (e->value.function.esym)
3275 32210 : return gfc_implicit_pure (e->value.function.esym);
3276 : else
3277 : return 0;
3278 : }
3279 :
3280 :
3281 : static bool
3282 981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3283 : int *f ATTRIBUTE_UNUSED)
3284 : {
3285 981 : const char *name;
3286 :
3287 : /* Don't bother recursing into other statement functions
3288 : since they will be checked individually for purity. */
3289 981 : if (e->expr_type != EXPR_FUNCTION
3290 343 : || !e->symtree
3291 343 : || e->symtree->n.sym == sym
3292 20 : || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3293 : return false;
3294 :
3295 19 : return gfc_pure_function (e, &name) ? false : true;
3296 : }
3297 :
3298 :
3299 : static bool
3300 305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3301 : {
3302 305 : return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3303 : }
3304 :
3305 :
3306 : /* Check if an impure function is allowed in the current context. */
3307 :
3308 244285 : static bool check_pure_function (gfc_expr *e)
3309 : {
3310 244285 : const char *name = NULL;
3311 244285 : code_stack *stack;
3312 244285 : bool saw_block = false;
3313 :
3314 : /* A BLOCK construct within a DO CONCURRENT construct leads to
3315 : gfc_do_concurrent_flag = 0 when the check for an impure function
3316 : occurs. Check the stack to see if the source code has a nested
3317 : BLOCK construct. */
3318 :
3319 565286 : for (stack = cs_base; stack; stack = stack->prev)
3320 : {
3321 321003 : if (!saw_block && stack->current->op == EXEC_BLOCK)
3322 : {
3323 7282 : saw_block = true;
3324 7282 : continue;
3325 : }
3326 :
3327 5221 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3328 : {
3329 10 : bool is_pure;
3330 321001 : is_pure = (e->value.function.isym
3331 9 : && (e->value.function.isym->pure
3332 1 : || e->value.function.isym->elemental))
3333 11 : || (e->value.function.esym
3334 1 : && (e->value.function.esym->attr.pure
3335 1 : || e->value.function.esym->attr.elemental));
3336 2 : if (!is_pure)
3337 : {
3338 2 : gfc_error ("Reference to impure function at %L inside a "
3339 : "DO CONCURRENT", &e->where);
3340 2 : return false;
3341 : }
3342 : }
3343 : }
3344 :
3345 244283 : if (!gfc_pure_function (e, &name) && name)
3346 : {
3347 36783 : if (forall_flag)
3348 : {
3349 4 : gfc_error ("Reference to impure function %qs at %L inside a "
3350 : "FORALL %s", name, &e->where,
3351 : forall_flag == 2 ? "mask" : "block");
3352 4 : return false;
3353 : }
3354 36779 : else if (gfc_do_concurrent_flag)
3355 : {
3356 2 : gfc_error ("Reference to impure function %qs at %L inside a "
3357 : "DO CONCURRENT %s", name, &e->where,
3358 : gfc_do_concurrent_flag == 2 ? "mask" : "block");
3359 2 : return false;
3360 : }
3361 36777 : else if (gfc_pure (NULL))
3362 : {
3363 5 : gfc_error ("Reference to impure function %qs at %L "
3364 : "within a PURE procedure", name, &e->where);
3365 5 : return false;
3366 : }
3367 36772 : if (!gfc_implicit_pure_function (e))
3368 30336 : gfc_unset_implicit_pure (NULL);
3369 : }
3370 : return true;
3371 : }
3372 :
3373 :
3374 : /* Update current procedure's array_outer_dependency flag, considering
3375 : a call to procedure SYM. */
3376 :
3377 : static void
3378 132606 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
3379 : {
3380 : /* Check to see if this is a sibling function that has not yet
3381 : been resolved. */
3382 132606 : gfc_namespace *sibling = gfc_current_ns->sibling;
3383 250213 : for (; sibling; sibling = sibling->sibling)
3384 : {
3385 124647 : if (sibling->proc_name == sym)
3386 : {
3387 7040 : gfc_resolve (sibling);
3388 7040 : break;
3389 : }
3390 : }
3391 :
3392 : /* If SYM has references to outer arrays, so has the procedure calling
3393 : SYM. If SYM is a procedure pointer, we can assume the worst. */
3394 132606 : if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3395 68041 : && gfc_current_ns->proc_name)
3396 67997 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3397 132606 : }
3398 :
3399 :
3400 : /* Resolve a function call, which means resolving the arguments, then figuring
3401 : out which entity the name refers to. */
3402 :
3403 : static bool
3404 344968 : resolve_function (gfc_expr *expr)
3405 : {
3406 344968 : gfc_actual_arglist *arg;
3407 344968 : gfc_symbol *sym;
3408 344968 : bool t;
3409 344968 : int temp;
3410 344968 : procedure_type p = PROC_INTRINSIC;
3411 344968 : bool no_formal_args;
3412 :
3413 344968 : sym = NULL;
3414 344968 : if (expr->symtree)
3415 344614 : sym = expr->symtree->n.sym;
3416 :
3417 : /* If this is a procedure pointer component, it has already been resolved. */
3418 344968 : if (gfc_is_proc_ptr_comp (expr))
3419 : return true;
3420 :
3421 : /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3422 : another caf_get. */
3423 344570 : if (sym && sym->attr.intrinsic
3424 8660 : && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3425 8660 : || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3426 : return true;
3427 :
3428 344570 : if (expr->ref)
3429 : {
3430 1 : gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3431 : &expr->where);
3432 1 : return false;
3433 : }
3434 :
3435 344215 : if (sym && sym->attr.intrinsic
3436 353229 : && !gfc_resolve_intrinsic (sym, &expr->where))
3437 : return false;
3438 :
3439 344569 : if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3440 : {
3441 4 : gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3442 4 : return false;
3443 : }
3444 :
3445 : /* If this is a deferred TBP with an abstract interface (which may
3446 : of course be referenced), expr->value.function.esym will be set. */
3447 344211 : if (sym && sym->attr.abstract && !expr->value.function.esym)
3448 : {
3449 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3450 : sym->name, &expr->where);
3451 1 : return false;
3452 : }
3453 :
3454 : /* If this is a deferred TBP with an abstract interface, its result
3455 : cannot be an assumed length character (F2003: C418). */
3456 344210 : if (sym && sym->attr.abstract && sym->attr.function
3457 192 : && sym->result->ts.u.cl
3458 158 : && sym->result->ts.u.cl->length == NULL
3459 2 : && !sym->result->ts.deferred)
3460 : {
3461 1 : gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3462 : "character length result (F2008: C418)", sym->name,
3463 : &sym->declared_at);
3464 1 : return false;
3465 : }
3466 :
3467 : /* Switch off assumed size checking and do this again for certain kinds
3468 : of procedure, once the procedure itself is resolved. */
3469 344563 : need_full_assumed_size++;
3470 :
3471 344563 : if (expr->symtree && expr->symtree->n.sym)
3472 344209 : p = expr->symtree->n.sym->attr.proc;
3473 :
3474 344563 : if (expr->value.function.isym && expr->value.function.isym->inquiry)
3475 1105 : inquiry_argument = true;
3476 344209 : no_formal_args = sym && is_external_proc (sym)
3477 358376 : && gfc_sym_get_dummy_args (sym) == NULL;
3478 :
3479 344563 : if (!resolve_actual_arglist (expr->value.function.actual,
3480 : p, no_formal_args))
3481 : {
3482 67 : inquiry_argument = false;
3483 67 : return false;
3484 : }
3485 :
3486 344496 : inquiry_argument = false;
3487 :
3488 : /* Resume assumed_size checking. */
3489 344496 : need_full_assumed_size--;
3490 :
3491 : /* If the procedure is external, check for usage. */
3492 344496 : if (sym && is_external_proc (sym))
3493 13793 : resolve_global_procedure (sym, &expr->where, 0);
3494 :
3495 344496 : if (sym && sym->ts.type == BT_CHARACTER
3496 3328 : && sym->ts.u.cl
3497 3234 : && sym->ts.u.cl->length == NULL
3498 671 : && !sym->attr.dummy
3499 664 : && !sym->ts.deferred
3500 2 : && expr->value.function.esym == NULL
3501 2 : && !sym->attr.contained)
3502 : {
3503 : /* Internal procedures are taken care of in resolve_contained_fntype. */
3504 1 : gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3505 : "be used at %L since it is not a dummy argument",
3506 : sym->name, &expr->where);
3507 1 : return false;
3508 : }
3509 :
3510 : /* Add and check formal interface when -fc-prototypes-external is in
3511 : force, see comment in resolve_call(). */
3512 :
3513 344495 : if (warn_external_argument_mismatch && sym && sym->attr.dummy
3514 18 : && sym->attr.external)
3515 : {
3516 18 : if (sym->formal)
3517 : {
3518 6 : bool conflict;
3519 6 : conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
3520 : sym->formal, 0, 0, 0, NULL);
3521 6 : if (conflict)
3522 : {
3523 6 : sym->ext_dummy_arglist_mismatch = 1;
3524 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
3525 : "Different argument lists in external dummy "
3526 : "function %s at %L and %L", sym->name,
3527 : &expr->where, &sym->formal_at);
3528 : }
3529 : }
3530 12 : else if (!sym->formal_resolved)
3531 : {
3532 6 : gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
3533 6 : sym->formal_at = expr->where;
3534 : }
3535 : }
3536 : /* See if function is already resolved. */
3537 :
3538 344495 : if (expr->value.function.name != NULL
3539 332578 : || expr->value.function.isym != NULL)
3540 : {
3541 12715 : if (expr->ts.type == BT_UNKNOWN)
3542 3 : expr->ts = sym->ts;
3543 : t = true;
3544 : }
3545 : else
3546 : {
3547 : /* Apply the rules of section 14.1.2. */
3548 :
3549 331780 : switch (procedure_kind (sym))
3550 : {
3551 27362 : case PTYPE_GENERIC:
3552 27362 : t = resolve_generic_f (expr);
3553 27362 : break;
3554 :
3555 28067 : case PTYPE_SPECIFIC:
3556 28067 : t = resolve_specific_f (expr);
3557 28067 : break;
3558 :
3559 276351 : case PTYPE_UNKNOWN:
3560 276351 : t = resolve_unknown_f (expr);
3561 276351 : break;
3562 :
3563 : default:
3564 : gfc_internal_error ("resolve_function(): bad function type");
3565 : }
3566 : }
3567 :
3568 : /* If the expression is still a function (it might have simplified),
3569 : then we check to see if we are calling an elemental function. */
3570 :
3571 344495 : if (expr->expr_type != EXPR_FUNCTION)
3572 : return t;
3573 :
3574 : /* Walk the argument list looking for invalid BOZ. */
3575 739830 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3576 495987 : if (arg->expr && arg->expr->ts.type == BT_BOZ)
3577 : {
3578 5 : gfc_error ("A BOZ literal constant at %L cannot appear as an "
3579 : "actual argument in a function reference",
3580 : &arg->expr->where);
3581 5 : return false;
3582 : }
3583 :
3584 243843 : temp = need_full_assumed_size;
3585 243843 : need_full_assumed_size = 0;
3586 :
3587 243843 : if (!resolve_elemental_actual (expr, NULL))
3588 : return false;
3589 :
3590 243840 : if (omp_workshare_flag
3591 32 : && expr->value.function.esym
3592 243845 : && ! gfc_elemental (expr->value.function.esym))
3593 : {
3594 4 : gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3595 4 : "in WORKSHARE construct", expr->value.function.esym->name,
3596 : &expr->where);
3597 4 : t = false;
3598 : }
3599 :
3600 : #define GENERIC_ID expr->value.function.isym->id
3601 243836 : else if (expr->value.function.actual != NULL
3602 235812 : && expr->value.function.isym != NULL
3603 190935 : && GENERIC_ID != GFC_ISYM_LBOUND
3604 : && GENERIC_ID != GFC_ISYM_LCOBOUND
3605 : && GENERIC_ID != GFC_ISYM_UCOBOUND
3606 : && GENERIC_ID != GFC_ISYM_LEN
3607 : && GENERIC_ID != GFC_ISYM_LOC
3608 : && GENERIC_ID != GFC_ISYM_C_LOC
3609 : && GENERIC_ID != GFC_ISYM_PRESENT)
3610 : {
3611 : /* Array intrinsics must also have the last upper bound of an
3612 : assumed size array argument. UBOUND and SIZE have to be
3613 : excluded from the check if the second argument is anything
3614 : than a constant. */
3615 :
3616 537407 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3617 : {
3618 372400 : if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3619 45629 : && arg == expr->value.function.actual
3620 16813 : && arg->next != NULL && arg->next->expr)
3621 : {
3622 8260 : if (arg->next->expr->expr_type != EXPR_CONSTANT)
3623 : break;
3624 :
3625 8036 : if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3626 : break;
3627 :
3628 8036 : if ((int)mpz_get_si (arg->next->expr->value.integer)
3629 8036 : < arg->expr->rank)
3630 : break;
3631 : }
3632 :
3633 369997 : if (arg->expr != NULL
3634 246746 : && arg->expr->rank > 0
3635 488874 : && resolve_assumed_size_actual (arg->expr))
3636 : return false;
3637 : }
3638 : }
3639 : #undef GENERIC_ID
3640 :
3641 243837 : need_full_assumed_size = temp;
3642 :
3643 243837 : if (!check_pure_function(expr))
3644 12 : t = false;
3645 :
3646 : /* Functions without the RECURSIVE attribution are not allowed to
3647 : * call themselves. */
3648 243837 : if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3649 : {
3650 51400 : gfc_symbol *esym;
3651 51400 : esym = expr->value.function.esym;
3652 :
3653 51400 : if (is_illegal_recursion (esym, gfc_current_ns))
3654 : {
3655 5 : if (esym->attr.entry && esym->ns->entries)
3656 3 : gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3657 : " function %qs is not RECURSIVE",
3658 3 : esym->name, &expr->where, esym->ns->entries->sym->name);
3659 : else
3660 2 : gfc_error ("Function %qs at %L cannot be called recursively, as it"
3661 : " is not RECURSIVE", esym->name, &expr->where);
3662 :
3663 : t = false;
3664 : }
3665 : }
3666 :
3667 : /* Character lengths of use associated functions may contains references to
3668 : symbols not referenced from the current program unit otherwise. Make sure
3669 : those symbols are marked as referenced. */
3670 :
3671 243837 : if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3672 3432 : && expr->value.function.esym->attr.use_assoc)
3673 : {
3674 1238 : gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3675 : }
3676 :
3677 : /* Make sure that the expression has a typespec that works. */
3678 243837 : if (expr->ts.type == BT_UNKNOWN)
3679 : {
3680 921 : if (expr->symtree->n.sym->result
3681 912 : && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3682 560 : && !expr->symtree->n.sym->result->attr.proc_pointer)
3683 560 : expr->ts = expr->symtree->n.sym->result->ts;
3684 : }
3685 :
3686 : /* These derived types with an incomplete namespace, arising from use
3687 : association, cause gfc_get_derived_vtab to segfault. If the function
3688 : namespace does not suffice, something is badly wrong. */
3689 243837 : if (expr->ts.type == BT_DERIVED
3690 9436 : && !expr->ts.u.derived->ns->proc_name)
3691 : {
3692 3 : gfc_symbol *der;
3693 3 : gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3694 3 : if (der)
3695 : {
3696 3 : expr->ts.u.derived->refs--;
3697 3 : expr->ts.u.derived = der;
3698 3 : der->refs++;
3699 : }
3700 : else
3701 0 : expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3702 : }
3703 :
3704 243837 : if (!expr->ref && !expr->value.function.isym)
3705 : {
3706 52781 : if (expr->value.function.esym)
3707 51712 : update_current_proc_array_outer_dependency (expr->value.function.esym);
3708 : else
3709 1069 : update_current_proc_array_outer_dependency (sym);
3710 : }
3711 191056 : else if (expr->ref)
3712 : /* typebound procedure: Assume the worst. */
3713 0 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3714 :
3715 243837 : if (expr->value.function.esym
3716 51712 : && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3717 26 : gfc_warning (OPT_Wdeprecated_declarations,
3718 : "Using function %qs at %L is deprecated",
3719 : sym->name, &expr->where);
3720 :
3721 : /* Check an external function supplied as a dummy argument has an external
3722 : attribute when a program unit uses 'implicit none (external)'. */
3723 243837 : if (expr->expr_type == EXPR_FUNCTION
3724 243837 : && expr->symtree
3725 243483 : && expr->symtree->n.sym->attr.dummy
3726 564 : && expr->symtree->n.sym->ns->has_implicit_none_export
3727 243838 : && !gfc_is_intrinsic(expr->symtree->n.sym, 0, expr->where))
3728 : {
3729 1 : gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
3730 : sym->name, &expr->where);
3731 1 : return false;
3732 : }
3733 :
3734 : return t;
3735 : }
3736 :
3737 :
3738 : /************* Subroutine resolution *************/
3739 :
3740 : static bool
3741 77260 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3742 : {
3743 77260 : code_stack *stack;
3744 77260 : bool saw_block = false;
3745 :
3746 77260 : if (gfc_pure (sym))
3747 : return true;
3748 :
3749 : /* A BLOCK construct within a DO CONCURRENT construct leads to
3750 : gfc_do_concurrent_flag = 0 when the check for an impure subroutine
3751 : occurs. Walk up the stack to see if the source code has a nested
3752 : construct. */
3753 :
3754 159313 : for (stack = cs_base; stack; stack = stack->prev)
3755 : {
3756 87698 : if (stack->current->op == EXEC_BLOCK)
3757 : {
3758 1896 : saw_block = true;
3759 1896 : continue;
3760 : }
3761 :
3762 85802 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3763 : {
3764 :
3765 2 : bool is_pure = true;
3766 87698 : is_pure = sym->attr.pure || sym->attr.elemental;
3767 :
3768 2 : if (!is_pure)
3769 : {
3770 2 : gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
3771 : "is not PURE", loc);
3772 2 : return false;
3773 : }
3774 : }
3775 : }
3776 :
3777 71615 : if (forall_flag)
3778 : {
3779 0 : gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3780 : name, loc);
3781 0 : return false;
3782 : }
3783 71615 : else if (gfc_do_concurrent_flag)
3784 : {
3785 6 : gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3786 : "PURE", name, loc);
3787 6 : return false;
3788 : }
3789 71609 : else if (gfc_pure (NULL))
3790 : {
3791 4 : gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3792 4 : return false;
3793 : }
3794 :
3795 71605 : gfc_unset_implicit_pure (NULL);
3796 71605 : return true;
3797 : }
3798 :
3799 :
3800 : static match
3801 2811 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3802 : {
3803 2811 : gfc_symbol *s;
3804 :
3805 2811 : if (sym->attr.generic)
3806 : {
3807 2810 : s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3808 2810 : if (s != NULL)
3809 : {
3810 2801 : c->resolved_sym = s;
3811 2801 : if (!pure_subroutine (s, s->name, &c->loc))
3812 : return MATCH_ERROR;
3813 2801 : return MATCH_YES;
3814 : }
3815 :
3816 : /* TODO: Need to search for elemental references in generic interface. */
3817 : }
3818 :
3819 10 : if (sym->attr.intrinsic)
3820 1 : return gfc_intrinsic_sub_interface (c, 0);
3821 :
3822 : return MATCH_NO;
3823 : }
3824 :
3825 :
3826 : static bool
3827 2809 : resolve_generic_s (gfc_code *c)
3828 : {
3829 2809 : gfc_symbol *sym;
3830 2809 : match m;
3831 :
3832 2809 : sym = c->symtree->n.sym;
3833 :
3834 2811 : for (;;)
3835 : {
3836 2811 : m = resolve_generic_s0 (c, sym);
3837 2811 : if (m == MATCH_YES)
3838 : return true;
3839 9 : else if (m == MATCH_ERROR)
3840 : return false;
3841 :
3842 9 : generic:
3843 9 : if (sym->ns->parent == NULL)
3844 : break;
3845 3 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3846 :
3847 3 : if (sym == NULL)
3848 : break;
3849 2 : if (!generic_sym (sym))
3850 0 : goto generic;
3851 : }
3852 :
3853 : /* Last ditch attempt. See if the reference is to an intrinsic
3854 : that possesses a matching interface. 14.1.2.4 */
3855 7 : sym = c->symtree->n.sym;
3856 :
3857 7 : if (!gfc_is_intrinsic (sym, 1, c->loc))
3858 : {
3859 4 : gfc_error ("There is no specific subroutine for the generic %qs at %L",
3860 : sym->name, &c->loc);
3861 4 : return false;
3862 : }
3863 :
3864 3 : m = gfc_intrinsic_sub_interface (c, 0);
3865 3 : if (m == MATCH_YES)
3866 : return true;
3867 1 : if (m == MATCH_NO)
3868 1 : gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3869 : "intrinsic subroutine interface", sym->name, &c->loc);
3870 :
3871 : return false;
3872 : }
3873 :
3874 :
3875 : /* Resolve a subroutine call known to be specific. */
3876 :
3877 : static match
3878 62765 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3879 : {
3880 62765 : match m;
3881 :
3882 62765 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3883 : {
3884 5683 : if (sym->attr.dummy)
3885 : {
3886 257 : sym->attr.proc = PROC_DUMMY;
3887 257 : goto found;
3888 : }
3889 :
3890 5426 : sym->attr.proc = PROC_EXTERNAL;
3891 5426 : goto found;
3892 : }
3893 :
3894 57082 : if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3895 57082 : goto found;
3896 :
3897 0 : if (sym->attr.intrinsic)
3898 : {
3899 0 : m = gfc_intrinsic_sub_interface (c, 1);
3900 0 : if (m == MATCH_YES)
3901 : return MATCH_YES;
3902 0 : if (m == MATCH_NO)
3903 0 : gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3904 : "with an intrinsic", sym->name, &c->loc);
3905 :
3906 0 : return MATCH_ERROR;
3907 : }
3908 :
3909 : return MATCH_NO;
3910 :
3911 62765 : found:
3912 62765 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3913 :
3914 62765 : c->resolved_sym = sym;
3915 62765 : if (!pure_subroutine (sym, sym->name, &c->loc))
3916 : return MATCH_ERROR;
3917 :
3918 : return MATCH_YES;
3919 : }
3920 :
3921 :
3922 : static bool
3923 62765 : resolve_specific_s (gfc_code *c)
3924 : {
3925 62765 : gfc_symbol *sym;
3926 62765 : match m;
3927 :
3928 62765 : sym = c->symtree->n.sym;
3929 :
3930 62765 : for (;;)
3931 : {
3932 62765 : m = resolve_specific_s0 (c, sym);
3933 62765 : if (m == MATCH_YES)
3934 : return true;
3935 7 : if (m == MATCH_ERROR)
3936 : return false;
3937 :
3938 0 : if (sym->ns->parent == NULL)
3939 : break;
3940 :
3941 0 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3942 :
3943 0 : if (sym == NULL)
3944 : break;
3945 : }
3946 :
3947 0 : sym = c->symtree->n.sym;
3948 0 : gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3949 : sym->name, &c->loc);
3950 :
3951 0 : return false;
3952 : }
3953 :
3954 :
3955 : /* Resolve a subroutine call not known to be generic nor specific. */
3956 :
3957 : static bool
3958 15870 : resolve_unknown_s (gfc_code *c)
3959 : {
3960 15870 : gfc_symbol *sym;
3961 :
3962 15870 : sym = c->symtree->n.sym;
3963 :
3964 15870 : if (sym->attr.dummy)
3965 : {
3966 20 : sym->attr.proc = PROC_DUMMY;
3967 20 : goto found;
3968 : }
3969 :
3970 : /* See if we have an intrinsic function reference. */
3971 :
3972 15850 : if (gfc_is_intrinsic (sym, 1, c->loc))
3973 : {
3974 4299 : if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3975 : return true;
3976 319 : return false;
3977 : }
3978 :
3979 : /* The reference is to an external name. */
3980 :
3981 11551 : found:
3982 11571 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3983 :
3984 11571 : c->resolved_sym = sym;
3985 :
3986 11571 : return pure_subroutine (sym, sym->name, &c->loc);
3987 : }
3988 :
3989 :
3990 :
3991 : static bool
3992 805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
3993 : gfc_code *c, gfc_namespace *ns)
3994 : {
3995 805 : locus *here;
3996 :
3997 : /* If the type has been imported then its vtype functions are OK. */
3998 805 : if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
3999 : return true;
4000 :
4001 : if (e)
4002 791 : here = &e->where;
4003 : else
4004 7 : here = &c->loc;
4005 :
4006 798 : if (s && !s->import_only)
4007 705 : s = gfc_find_symtree (ns->sym_root, sym->name);
4008 :
4009 798 : if (ns->import_state == IMPORT_ONLY
4010 75 : && sym->ns != ns
4011 58 : && (!s || !s->import_only))
4012 : {
4013 21 : gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
4014 : "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
4015 21 : return false;
4016 : }
4017 777 : else if (ns->import_state == IMPORT_NONE
4018 27 : && sym->ns != ns)
4019 : {
4020 12 : gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
4021 : "has IMPORT, NONE", sym->name, here);
4022 12 : return false;
4023 : }
4024 : return true;
4025 : }
4026 :
4027 :
4028 : static bool
4029 7300 : check_import_status (gfc_expr *e)
4030 : {
4031 7300 : gfc_symtree *st;
4032 7300 : gfc_ref *ref;
4033 7300 : gfc_symbol *sym, *der;
4034 7300 : gfc_namespace *ns = gfc_current_ns;
4035 :
4036 7300 : switch (e->expr_type)
4037 : {
4038 727 : case EXPR_VARIABLE:
4039 727 : case EXPR_FUNCTION:
4040 727 : case EXPR_SUBSTRING:
4041 727 : sym = e->symtree ? e->symtree->n.sym : NULL;
4042 :
4043 : /* Check the symbol itself. */
4044 727 : if (sym
4045 727 : && !(ns->proc_name
4046 : && (sym == ns->proc_name))
4047 1450 : && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
4048 : return false;
4049 :
4050 : /* Check the declared derived type. */
4051 717 : if (sym->ts.type == BT_DERIVED)
4052 : {
4053 16 : der = sym->ts.u.derived;
4054 16 : st = gfc_find_symtree (ns->sym_root, der->name);
4055 :
4056 16 : if (!check_sym_import_status (der, st, e, NULL, ns))
4057 : return false;
4058 : }
4059 701 : else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
4060 : {
4061 44 : der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
4062 : : sym->ts.u.derived;
4063 44 : st = gfc_find_symtree (ns->sym_root, der->name);
4064 :
4065 44 : if (!check_sym_import_status (der, st, e, NULL, ns))
4066 : return false;
4067 : }
4068 :
4069 : /* Check the declared derived types of component references. */
4070 724 : for (ref = e->ref; ref; ref = ref->next)
4071 20 : if (ref->type == REF_COMPONENT)
4072 : {
4073 19 : gfc_component *c = ref->u.c.component;
4074 19 : if (c->ts.type == BT_DERIVED)
4075 : {
4076 7 : der = c->ts.u.derived;
4077 7 : st = gfc_find_symtree (ns->sym_root, der->name);
4078 7 : if (!check_sym_import_status (der, st, e, NULL, ns))
4079 : return false;
4080 : }
4081 12 : else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
4082 : {
4083 0 : der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
4084 : : c->ts.u.derived;
4085 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4086 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4087 : return false;
4088 : }
4089 : }
4090 :
4091 : break;
4092 :
4093 8 : case EXPR_ARRAY:
4094 8 : case EXPR_STRUCTURE:
4095 : /* Check the declared derived type. */
4096 8 : if (e->ts.type == BT_DERIVED)
4097 : {
4098 8 : der = e->ts.u.derived;
4099 8 : st = gfc_find_symtree (ns->sym_root, der->name);
4100 :
4101 8 : if (!check_sym_import_status (der, st, e, NULL, ns))
4102 : return false;
4103 : }
4104 0 : else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
4105 : {
4106 0 : der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
4107 : : e->ts.u.derived;
4108 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4109 :
4110 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4111 : return false;
4112 : }
4113 :
4114 : break;
4115 :
4116 : /* Either not applicable or resolved away
4117 : case EXPR_OP:
4118 : case EXPR_UNKNOWN:
4119 : case EXPR_CONSTANT:
4120 : case EXPR_NULL:
4121 : case EXPR_COMPCALL:
4122 : case EXPR_PPC: */
4123 :
4124 : default:
4125 : break;
4126 : }
4127 :
4128 : return true;
4129 : }
4130 :
4131 :
4132 : /* If an elemental call has an INTENT_IN argument that has a dependency on an
4133 : argument which is not INTENT_IN and requires a temporary, build a temporary
4134 : for the INTENT_IN actual argument as well. */
4135 :
4136 : static void
4137 : add_temp_assign_before_call (gfc_code *, gfc_namespace *, gfc_expr **);
4138 :
4139 : static void
4140 5257 : resolve_elemental_dependencies (gfc_code *c)
4141 : {
4142 5257 : gfc_actual_arglist *arg1 = c->ext.actual;
4143 5257 : gfc_actual_arglist *arg2 = NULL;
4144 5257 : gfc_formal_arglist *formal1 = c->resolved_sym->formal;
4145 5257 : gfc_formal_arglist *formal2 = NULL;
4146 5257 : gfc_expr *expr1;
4147 5257 : gfc_expr **expr2;
4148 :
4149 16645 : for (; arg1 && formal1; arg1 = arg1->next, formal1 = formal1->next)
4150 : {
4151 11388 : if (formal1->sym
4152 11388 : && (formal1->sym->attr.intent == INTENT_IN
4153 3536 : || formal1->sym->attr.value))
4154 8110 : continue;
4155 :
4156 3278 : if (!arg1->expr || arg1->expr->expr_type != EXPR_VARIABLE)
4157 0 : continue;
4158 :
4159 3278 : arg2 = c->ext.actual;
4160 3278 : formal2 = c->resolved_sym->formal;
4161 10696 : for (; arg2 && formal2; arg2 = arg2->next, formal2 = formal2->next)
4162 : {
4163 7418 : if (arg2 == arg1 || !arg2->expr
4164 4128 : || !(formal2->sym && formal2->sym->attr.intent == INTENT_IN))
4165 3304 : continue;
4166 :
4167 4114 : expr1 = arg1->expr;
4168 4114 : expr2 = &arg2->expr;
4169 :
4170 : /* If the arg1 has something horrible like a vector index and
4171 : there is a dependency between arg1 and arg2, build a
4172 : temporary from arg2, assign the arg2 to it and use the
4173 : temporary in the call expression. */
4174 2009 : if (expr1->rank && gfc_ref_needs_temporary_p (expr1->ref)
4175 4234 : && gfc_check_dependency (expr1, *expr2, false))
4176 36 : add_temp_assign_before_call (c, gfc_current_ns, expr2);
4177 : }
4178 : }
4179 5257 : }
4180 :
4181 : /* Resolve a subroutine call. Although it was tempting to use the same code
4182 : for functions, subroutines and functions are stored differently and this
4183 : makes things awkward. */
4184 :
4185 :
4186 : static bool
4187 81589 : resolve_call (gfc_code *c)
4188 : {
4189 81589 : bool t;
4190 81589 : procedure_type ptype = PROC_INTRINSIC;
4191 81589 : gfc_symbol *csym, *sym;
4192 81589 : bool no_formal_args;
4193 :
4194 81589 : csym = c->symtree ? c->symtree->n.sym : NULL;
4195 :
4196 81589 : if (csym && csym->ts.type != BT_UNKNOWN)
4197 : {
4198 4 : gfc_error ("%qs at %L has a type, which is not consistent with "
4199 : "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
4200 4 : return false;
4201 : }
4202 :
4203 81585 : if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
4204 : {
4205 17308 : gfc_symtree *st;
4206 17308 : gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
4207 17308 : sym = st ? st->n.sym : NULL;
4208 17308 : if (sym && csym != sym
4209 3 : && sym->ns == gfc_current_ns
4210 3 : && sym->attr.flavor == FL_PROCEDURE
4211 3 : && sym->attr.contained)
4212 : {
4213 3 : sym->refs++;
4214 3 : if (csym->attr.generic)
4215 2 : c->symtree->n.sym = sym;
4216 : else
4217 1 : c->symtree = st;
4218 3 : csym = c->symtree->n.sym;
4219 : }
4220 : }
4221 :
4222 : /* If this ia a deferred TBP, c->expr1 will be set. */
4223 81585 : if (!c->expr1 && csym)
4224 : {
4225 79870 : if (csym->attr.abstract)
4226 : {
4227 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
4228 : csym->name, &c->loc);
4229 1 : return false;
4230 : }
4231 :
4232 : /* Subroutines without the RECURSIVE attribution are not allowed to
4233 : call themselves. */
4234 79869 : if (is_illegal_recursion (csym, gfc_current_ns))
4235 : {
4236 4 : if (csym->attr.entry && csym->ns->entries)
4237 2 : gfc_error ("ENTRY %qs at %L cannot be called recursively, "
4238 : "as subroutine %qs is not RECURSIVE",
4239 2 : csym->name, &c->loc, csym->ns->entries->sym->name);
4240 : else
4241 2 : gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
4242 : "as it is not RECURSIVE", csym->name, &c->loc);
4243 :
4244 81584 : t = false;
4245 : }
4246 : }
4247 :
4248 : /* Switch off assumed size checking and do this again for certain kinds
4249 : of procedure, once the procedure itself is resolved. */
4250 81584 : need_full_assumed_size++;
4251 :
4252 81584 : if (csym)
4253 81584 : ptype = csym->attr.proc;
4254 :
4255 81584 : no_formal_args = csym && is_external_proc (csym)
4256 15667 : && gfc_sym_get_dummy_args (csym) == NULL;
4257 81584 : if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
4258 : return false;
4259 :
4260 : /* Resume assumed_size checking. */
4261 81550 : need_full_assumed_size--;
4262 :
4263 : /* If 'implicit none (external)' and the symbol is a dummy argument,
4264 : check for an 'external' attribute. */
4265 81550 : if (csym->ns->has_implicit_none_export
4266 4470 : && csym->attr.external == 0 && csym->attr.dummy == 1)
4267 : {
4268 1 : gfc_error ("Dummy procedure %qs at %L requires an EXTERNAL attribute",
4269 : csym->name, &c->loc);
4270 1 : return false;
4271 : }
4272 :
4273 : /* If external, check for usage. */
4274 81549 : if (csym && is_external_proc (csym))
4275 15661 : resolve_global_procedure (csym, &c->loc, 1);
4276 :
4277 : /* If we have an external dummy argument, we want to write out its arguments
4278 : with -fc-prototypes-external. Code like
4279 :
4280 : subroutine foo(a,n)
4281 : external a
4282 : if (n == 1) call a(1)
4283 : if (n == 2) call a(2,3)
4284 : end subroutine foo
4285 :
4286 : is actually legal Fortran, but it is not possible to generate a C23-
4287 : compliant prototype for this, so we just record the fact here and
4288 : handle that during -fc-prototypes-external processing. */
4289 :
4290 81549 : if (warn_external_argument_mismatch && csym && csym->attr.dummy
4291 14 : && csym->attr.external)
4292 : {
4293 14 : if (csym->formal)
4294 : {
4295 6 : bool conflict;
4296 6 : conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4297 : 0, 0, 0, NULL);
4298 6 : if (conflict)
4299 : {
4300 6 : csym->ext_dummy_arglist_mismatch = 1;
4301 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
4302 : "Different argument lists in external dummy "
4303 : "subroutine %s at %L and %L", csym->name,
4304 : &c->loc, &csym->formal_at);
4305 : }
4306 : }
4307 8 : else if (!csym->formal_resolved)
4308 : {
4309 7 : gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4310 7 : csym->formal_at = c->loc;
4311 : }
4312 : }
4313 :
4314 81549 : t = true;
4315 81549 : if (c->resolved_sym == NULL)
4316 : {
4317 81444 : c->resolved_isym = NULL;
4318 81444 : switch (procedure_kind (csym))
4319 : {
4320 2809 : case PTYPE_GENERIC:
4321 2809 : t = resolve_generic_s (c);
4322 2809 : break;
4323 :
4324 62765 : case PTYPE_SPECIFIC:
4325 62765 : t = resolve_specific_s (c);
4326 62765 : break;
4327 :
4328 15870 : case PTYPE_UNKNOWN:
4329 15870 : t = resolve_unknown_s (c);
4330 15870 : break;
4331 :
4332 : default:
4333 : gfc_internal_error ("resolve_subroutine(): bad function type");
4334 : }
4335 : }
4336 :
4337 : /* Some checks of elemental subroutine actual arguments. */
4338 81548 : if (!resolve_elemental_actual (NULL, c))
4339 : return false;
4340 :
4341 : /* Deal with complicated dependencies that the scalarizer cannot handle. */
4342 81540 : if (c->resolved_sym && c->resolved_sym->attr.elemental && !no_formal_args
4343 6200 : && c->ext.actual && c->ext.actual->next)
4344 5257 : resolve_elemental_dependencies (c);
4345 :
4346 81540 : if (!c->expr1)
4347 79825 : update_current_proc_array_outer_dependency (csym);
4348 : else
4349 : /* Typebound procedure: Assume the worst. */
4350 1715 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4351 :
4352 81540 : if (c->resolved_sym
4353 81217 : && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4354 34 : gfc_warning (OPT_Wdeprecated_declarations,
4355 : "Using subroutine %qs at %L is deprecated",
4356 : c->resolved_sym->name, &c->loc);
4357 :
4358 81540 : csym = c->resolved_sym ? c->resolved_sym : csym;
4359 81540 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
4360 2 : && csym != gfc_current_ns->proc_name)
4361 1 : return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
4362 :
4363 : return t;
4364 : }
4365 :
4366 :
4367 : /* Compare the shapes of two arrays that have non-NULL shapes. If both
4368 : op1->shape and op2->shape are non-NULL return true if their shapes
4369 : match. If both op1->shape and op2->shape are non-NULL return false
4370 : if their shapes do not match. If either op1->shape or op2->shape is
4371 : NULL, return true. */
4372 :
4373 : static bool
4374 32484 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
4375 : {
4376 32484 : bool t;
4377 32484 : int i;
4378 :
4379 32484 : t = true;
4380 :
4381 32484 : if (op1->shape != NULL && op2->shape != NULL)
4382 : {
4383 43060 : for (i = 0; i < op1->rank; i++)
4384 : {
4385 22967 : if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4386 : {
4387 3 : gfc_error ("Shapes for operands at %L and %L are not conformable",
4388 : &op1->where, &op2->where);
4389 3 : t = false;
4390 3 : break;
4391 : }
4392 : }
4393 : }
4394 :
4395 32484 : return t;
4396 : }
4397 :
4398 : /* Convert a logical operator to the corresponding bitwise intrinsic call.
4399 : For example A .AND. B becomes IAND(A, B). */
4400 : static gfc_expr *
4401 668 : logical_to_bitwise (gfc_expr *e)
4402 : {
4403 668 : gfc_expr *tmp, *op1, *op2;
4404 668 : gfc_isym_id isym;
4405 668 : gfc_actual_arglist *args = NULL;
4406 :
4407 668 : gcc_assert (e->expr_type == EXPR_OP);
4408 :
4409 668 : isym = GFC_ISYM_NONE;
4410 668 : op1 = e->value.op.op1;
4411 668 : op2 = e->value.op.op2;
4412 :
4413 668 : switch (e->value.op.op)
4414 : {
4415 : case INTRINSIC_NOT:
4416 : isym = GFC_ISYM_NOT;
4417 : break;
4418 126 : case INTRINSIC_AND:
4419 126 : isym = GFC_ISYM_IAND;
4420 126 : break;
4421 127 : case INTRINSIC_OR:
4422 127 : isym = GFC_ISYM_IOR;
4423 127 : break;
4424 270 : case INTRINSIC_NEQV:
4425 270 : isym = GFC_ISYM_IEOR;
4426 270 : break;
4427 126 : case INTRINSIC_EQV:
4428 : /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4429 : Change the old expression to NEQV, which will get replaced by IEOR,
4430 : and wrap it in NOT. */
4431 126 : tmp = gfc_copy_expr (e);
4432 126 : tmp->value.op.op = INTRINSIC_NEQV;
4433 126 : tmp = logical_to_bitwise (tmp);
4434 126 : isym = GFC_ISYM_NOT;
4435 126 : op1 = tmp;
4436 126 : op2 = NULL;
4437 126 : break;
4438 0 : default:
4439 0 : gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4440 : }
4441 :
4442 : /* Inherit the original operation's operands as arguments. */
4443 668 : args = gfc_get_actual_arglist ();
4444 668 : args->expr = op1;
4445 668 : if (op2)
4446 : {
4447 523 : args->next = gfc_get_actual_arglist ();
4448 523 : args->next->expr = op2;
4449 : }
4450 :
4451 : /* Convert the expression to a function call. */
4452 668 : e->expr_type = EXPR_FUNCTION;
4453 668 : e->value.function.actual = args;
4454 668 : e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4455 668 : e->value.function.name = e->value.function.isym->name;
4456 668 : e->value.function.esym = NULL;
4457 :
4458 : /* Make up a pre-resolved function call symtree if we need to. */
4459 668 : if (!e->symtree || !e->symtree->n.sym)
4460 : {
4461 668 : gfc_symbol *sym;
4462 668 : gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4463 668 : sym = e->symtree->n.sym;
4464 668 : sym->result = sym;
4465 668 : sym->attr.flavor = FL_PROCEDURE;
4466 668 : sym->attr.function = 1;
4467 668 : sym->attr.elemental = 1;
4468 668 : sym->attr.pure = 1;
4469 668 : sym->attr.referenced = 1;
4470 668 : gfc_intrinsic_symbol (sym);
4471 668 : gfc_commit_symbol (sym);
4472 : }
4473 :
4474 668 : args->name = e->value.function.isym->formal->name;
4475 668 : if (e->value.function.isym->formal->next)
4476 523 : args->next->name = e->value.function.isym->formal->next->name;
4477 :
4478 668 : return e;
4479 : }
4480 :
4481 : /* Recursively append candidate UOP to CANDIDATES. Store the number of
4482 : candidates in CANDIDATES_LEN. */
4483 : static void
4484 57 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4485 : char **&candidates,
4486 : size_t &candidates_len)
4487 : {
4488 59 : gfc_symtree *p;
4489 :
4490 59 : if (uop == NULL)
4491 : return;
4492 :
4493 : /* Not sure how to properly filter here. Use all for a start.
4494 : n.uop.op is NULL for empty interface operators (is that legal?) disregard
4495 : these as i suppose they don't make terribly sense. */
4496 :
4497 59 : if (uop->n.uop->op != NULL)
4498 2 : vec_push (candidates, candidates_len, uop->name);
4499 :
4500 59 : p = uop->left;
4501 59 : if (p)
4502 0 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4503 :
4504 59 : p = uop->right;
4505 59 : if (p)
4506 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4507 : }
4508 :
4509 : /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4510 :
4511 : static const char*
4512 57 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4513 : {
4514 57 : char **candidates = NULL;
4515 57 : size_t candidates_len = 0;
4516 57 : lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4517 57 : return gfc_closest_fuzzy_match (op, candidates);
4518 : }
4519 :
4520 :
4521 : /* Callback finding an impure function as an operand to an .and. or
4522 : .or. expression. Remember the last function warned about to
4523 : avoid double warnings when recursing. */
4524 :
4525 : static int
4526 193223 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4527 : void *data)
4528 : {
4529 193223 : gfc_expr *f = *e;
4530 193223 : const char *name;
4531 193223 : static gfc_expr *last = NULL;
4532 193223 : bool *found = (bool *) data;
4533 :
4534 193223 : if (f->expr_type == EXPR_FUNCTION)
4535 : {
4536 11860 : *found = 1;
4537 11860 : if (f != last && !gfc_pure_function (f, &name)
4538 13145 : && !gfc_implicit_pure_function (f))
4539 : {
4540 1146 : if (name)
4541 1146 : gfc_warning (OPT_Wfunction_elimination,
4542 : "Impure function %qs at %L might not be evaluated",
4543 : name, &f->where);
4544 : else
4545 0 : gfc_warning (OPT_Wfunction_elimination,
4546 : "Impure function at %L might not be evaluated",
4547 : &f->where);
4548 : }
4549 11860 : last = f;
4550 : }
4551 :
4552 193223 : return 0;
4553 : }
4554 :
4555 : /* Return true if TYPE is character based, false otherwise. */
4556 :
4557 : static int
4558 1373 : is_character_based (bt type)
4559 : {
4560 1373 : return type == BT_CHARACTER || type == BT_HOLLERITH;
4561 : }
4562 :
4563 :
4564 : /* If expression is a hollerith, convert it to character and issue a warning
4565 : for the conversion. */
4566 :
4567 : static void
4568 408 : convert_hollerith_to_character (gfc_expr *e)
4569 : {
4570 408 : if (e->ts.type == BT_HOLLERITH)
4571 : {
4572 108 : gfc_typespec t;
4573 108 : gfc_clear_ts (&t);
4574 108 : t.type = BT_CHARACTER;
4575 108 : t.kind = e->ts.kind;
4576 108 : gfc_convert_type_warn (e, &t, 2, 1);
4577 : }
4578 408 : }
4579 :
4580 : /* Convert to numeric and issue a warning for the conversion. */
4581 :
4582 : static void
4583 240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
4584 : {
4585 240 : gfc_typespec t;
4586 240 : gfc_clear_ts (&t);
4587 240 : t.type = b->ts.type;
4588 240 : t.kind = b->ts.kind;
4589 240 : gfc_convert_type_warn (a, &t, 2, 1);
4590 240 : }
4591 :
4592 : /* Resolve an operator expression node. This can involve replacing the
4593 : operation with a user defined function call. CHECK_INTERFACES is a
4594 : helper macro. */
4595 :
4596 : #define CHECK_INTERFACES \
4597 : { \
4598 : match m = gfc_extend_expr (e); \
4599 : if (m == MATCH_YES) \
4600 : return true; \
4601 : if (m == MATCH_ERROR) \
4602 : return false; \
4603 : }
4604 :
4605 : static bool
4606 533239 : resolve_operator (gfc_expr *e)
4607 : {
4608 533239 : gfc_expr *op1, *op2;
4609 : /* One error uses 3 names; additional space for wording (also via gettext). */
4610 533239 : bool t = true;
4611 :
4612 : /* Reduce stacked parentheses to single pair */
4613 533239 : while (e->expr_type == EXPR_OP
4614 533397 : && e->value.op.op == INTRINSIC_PARENTHESES
4615 23541 : && e->value.op.op1->expr_type == EXPR_OP
4616 550241 : && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4617 : {
4618 158 : gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4619 158 : gfc_replace_expr (e, tmp);
4620 : }
4621 :
4622 : /* Resolve all subnodes-- give them types. */
4623 :
4624 533239 : switch (e->value.op.op)
4625 : {
4626 481190 : default:
4627 481190 : if (!gfc_resolve_expr (e->value.op.op2))
4628 533239 : t = false;
4629 :
4630 : /* Fall through. */
4631 :
4632 533239 : case INTRINSIC_NOT:
4633 533239 : case INTRINSIC_UPLUS:
4634 533239 : case INTRINSIC_UMINUS:
4635 533239 : case INTRINSIC_PARENTHESES:
4636 533239 : if (!gfc_resolve_expr (e->value.op.op1))
4637 : return false;
4638 533078 : if (e->value.op.op1
4639 533069 : && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4640 : {
4641 0 : gfc_error ("BOZ literal constant at %L cannot be an operand of "
4642 0 : "unary operator %qs", &e->value.op.op1->where,
4643 : gfc_op2string (e->value.op.op));
4644 0 : return false;
4645 : }
4646 533078 : if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4647 6 : && e->value.op.op == INTRINSIC_UMINUS)
4648 : {
4649 2 : gfc_error ("Negation of unsigned expression at %L not permitted ",
4650 : &e->value.op.op1->where);
4651 2 : return false;
4652 : }
4653 533076 : break;
4654 : }
4655 :
4656 : /* Typecheck the new node. */
4657 :
4658 533076 : op1 = e->value.op.op1;
4659 533076 : op2 = e->value.op.op2;
4660 533076 : if (op1 == NULL && op2 == NULL)
4661 : return false;
4662 : /* Error out if op2 did not resolve. We already diagnosed op1. */
4663 533067 : if (t == false)
4664 : return false;
4665 :
4666 : /* op1 and op2 cannot both be BOZ. */
4667 533001 : if (op1 && op1->ts.type == BT_BOZ
4668 0 : && op2 && op2->ts.type == BT_BOZ)
4669 : {
4670 0 : gfc_error ("Operands at %L and %L cannot appear as operands of "
4671 0 : "binary operator %qs", &op1->where, &op2->where,
4672 : gfc_op2string (e->value.op.op));
4673 0 : return false;
4674 : }
4675 :
4676 533001 : if ((op1 && op1->expr_type == EXPR_NULL)
4677 532999 : || (op2 && op2->expr_type == EXPR_NULL))
4678 : {
4679 3 : CHECK_INTERFACES
4680 3 : gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4681 3 : return false;
4682 : }
4683 :
4684 532998 : switch (e->value.op.op)
4685 : {
4686 8124 : case INTRINSIC_UPLUS:
4687 8124 : case INTRINSIC_UMINUS:
4688 8124 : if (op1->ts.type == BT_INTEGER
4689 : || op1->ts.type == BT_REAL
4690 : || op1->ts.type == BT_COMPLEX
4691 : || op1->ts.type == BT_UNSIGNED)
4692 : {
4693 8055 : e->ts = op1->ts;
4694 8055 : break;
4695 : }
4696 :
4697 69 : CHECK_INTERFACES
4698 43 : gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4699 : gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4700 43 : return false;
4701 :
4702 155602 : case INTRINSIC_POWER:
4703 155602 : case INTRINSIC_PLUS:
4704 155602 : case INTRINSIC_MINUS:
4705 155602 : case INTRINSIC_TIMES:
4706 155602 : case INTRINSIC_DIVIDE:
4707 :
4708 : /* UNSIGNED cannot appear in a mixed expression without explicit
4709 : conversion. */
4710 155602 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4711 : {
4712 3 : CHECK_INTERFACES
4713 3 : gfc_error ("Operands of binary numeric operator %qs at %L are "
4714 : "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4715 : gfc_typename (op1), gfc_typename (op2));
4716 3 : return false;
4717 : }
4718 :
4719 155599 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4720 : {
4721 : /* Do not perform conversions if operands are not conformable as
4722 : required for the binary intrinsic operators (F2018:10.1.5).
4723 : Defer to a possibly overloading user-defined operator. */
4724 155145 : if (!gfc_op_rank_conformable (op1, op2))
4725 : {
4726 36 : CHECK_INTERFACES
4727 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4728 0 : &op1->where, &op2->where);
4729 0 : return false;
4730 : }
4731 :
4732 155109 : gfc_type_convert_binary (e, 1);
4733 155109 : break;
4734 : }
4735 :
4736 454 : if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4737 : {
4738 225 : CHECK_INTERFACES
4739 2 : gfc_error ("Unexpected derived-type entities in binary intrinsic "
4740 : "numeric operator %qs at %L",
4741 : gfc_op2string (e->value.op.op), &e->where);
4742 2 : return false;
4743 : }
4744 : else
4745 : {
4746 229 : CHECK_INTERFACES
4747 3 : gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4748 : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4749 : gfc_typename (op2));
4750 3 : return false;
4751 : }
4752 :
4753 2268 : case INTRINSIC_CONCAT:
4754 2268 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4755 2243 : && op1->ts.kind == op2->ts.kind)
4756 : {
4757 2234 : e->ts.type = BT_CHARACTER;
4758 2234 : e->ts.kind = op1->ts.kind;
4759 2234 : break;
4760 : }
4761 :
4762 34 : CHECK_INTERFACES
4763 10 : gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4764 : &e->where, gfc_typename (op1), gfc_typename (op2));
4765 10 : return false;
4766 :
4767 69671 : case INTRINSIC_AND:
4768 69671 : case INTRINSIC_OR:
4769 69671 : case INTRINSIC_EQV:
4770 69671 : case INTRINSIC_NEQV:
4771 69671 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4772 : {
4773 69120 : e->ts.type = BT_LOGICAL;
4774 69120 : e->ts.kind = gfc_kind_max (op1, op2);
4775 69120 : if (op1->ts.kind < e->ts.kind)
4776 140 : gfc_convert_type (op1, &e->ts, 2);
4777 68980 : else if (op2->ts.kind < e->ts.kind)
4778 117 : gfc_convert_type (op2, &e->ts, 2);
4779 :
4780 69120 : if (flag_frontend_optimize &&
4781 58062 : (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4782 : {
4783 : /* Warn about short-circuiting
4784 : with impure function as second operand. */
4785 52057 : bool op2_f = false;
4786 52057 : gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4787 : }
4788 : break;
4789 : }
4790 :
4791 : /* Logical ops on integers become bitwise ops with -fdec. */
4792 551 : else if (flag_dec
4793 523 : && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4794 : {
4795 523 : e->ts.type = BT_INTEGER;
4796 523 : e->ts.kind = gfc_kind_max (op1, op2);
4797 523 : if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4798 289 : gfc_convert_type (op1, &e->ts, 1);
4799 523 : if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4800 144 : gfc_convert_type (op2, &e->ts, 1);
4801 523 : e = logical_to_bitwise (e);
4802 523 : goto simplify_op;
4803 : }
4804 :
4805 28 : CHECK_INTERFACES
4806 16 : gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4807 : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4808 : gfc_typename (op2));
4809 16 : return false;
4810 :
4811 20526 : case INTRINSIC_NOT:
4812 : /* Logical ops on integers become bitwise ops with -fdec. */
4813 20526 : if (flag_dec && op1->ts.type == BT_INTEGER)
4814 : {
4815 19 : e->ts.type = BT_INTEGER;
4816 19 : e->ts.kind = op1->ts.kind;
4817 19 : e = logical_to_bitwise (e);
4818 19 : goto simplify_op;
4819 : }
4820 :
4821 20507 : if (op1->ts.type == BT_LOGICAL)
4822 : {
4823 20501 : e->ts.type = BT_LOGICAL;
4824 20501 : e->ts.kind = op1->ts.kind;
4825 20501 : break;
4826 : }
4827 :
4828 6 : CHECK_INTERFACES
4829 3 : gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4830 : gfc_typename (op1));
4831 3 : return false;
4832 :
4833 21329 : case INTRINSIC_GT:
4834 21329 : case INTRINSIC_GT_OS:
4835 21329 : case INTRINSIC_GE:
4836 21329 : case INTRINSIC_GE_OS:
4837 21329 : case INTRINSIC_LT:
4838 21329 : case INTRINSIC_LT_OS:
4839 21329 : case INTRINSIC_LE:
4840 21329 : case INTRINSIC_LE_OS:
4841 21329 : if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4842 : {
4843 18 : CHECK_INTERFACES
4844 0 : gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4845 0 : return false;
4846 : }
4847 :
4848 : /* Fall through. */
4849 :
4850 253163 : case INTRINSIC_EQ:
4851 253163 : case INTRINSIC_EQ_OS:
4852 253163 : case INTRINSIC_NE:
4853 253163 : case INTRINSIC_NE_OS:
4854 :
4855 253163 : if (flag_dec
4856 1038 : && is_character_based (op1->ts.type)
4857 253498 : && is_character_based (op2->ts.type))
4858 : {
4859 204 : convert_hollerith_to_character (op1);
4860 204 : convert_hollerith_to_character (op2);
4861 : }
4862 :
4863 253163 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4864 38475 : && op1->ts.kind == op2->ts.kind)
4865 : {
4866 38438 : e->ts.type = BT_LOGICAL;
4867 38438 : e->ts.kind = gfc_default_logical_kind;
4868 38438 : break;
4869 : }
4870 :
4871 : /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4872 214725 : if (op1->ts.type == BT_BOZ)
4873 : {
4874 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4875 : "as an operand of a relational operator"),
4876 : &op1->where))
4877 : return false;
4878 :
4879 0 : if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4880 : return false;
4881 :
4882 0 : if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4883 : return false;
4884 : }
4885 :
4886 : /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4887 214725 : if (op2->ts.type == BT_BOZ)
4888 : {
4889 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4890 : " as an operand of a relational operator"),
4891 : &op2->where))
4892 : return false;
4893 :
4894 0 : if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4895 : return false;
4896 :
4897 0 : if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4898 : return false;
4899 : }
4900 214725 : if (flag_dec
4901 214725 : && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4902 120 : convert_to_numeric (op1, op2);
4903 :
4904 214725 : if (flag_dec
4905 214725 : && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4906 120 : convert_to_numeric (op2, op1);
4907 :
4908 214725 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4909 : {
4910 : /* Do not perform conversions if operands are not conformable as
4911 : required for the binary intrinsic operators (F2018:10.1.5).
4912 : Defer to a possibly overloading user-defined operator. */
4913 213596 : if (!gfc_op_rank_conformable (op1, op2))
4914 : {
4915 70 : CHECK_INTERFACES
4916 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4917 0 : &op1->where, &op2->where);
4918 0 : return false;
4919 : }
4920 :
4921 213526 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4922 : {
4923 1 : CHECK_INTERFACES
4924 1 : gfc_error ("Inconsistent types for operator at %L and %L: "
4925 1 : "%s and %s", &op1->where, &op2->where,
4926 : gfc_typename (op1), gfc_typename (op2));
4927 1 : return false;
4928 : }
4929 :
4930 213525 : gfc_type_convert_binary (e, 1);
4931 :
4932 213525 : e->ts.type = BT_LOGICAL;
4933 213525 : e->ts.kind = gfc_default_logical_kind;
4934 :
4935 213525 : if (warn_compare_reals)
4936 : {
4937 69 : gfc_intrinsic_op op = e->value.op.op;
4938 :
4939 : /* Type conversion has made sure that the types of op1 and op2
4940 : agree, so it is only necessary to check the first one. */
4941 69 : if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4942 13 : && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4943 6 : || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4944 : {
4945 13 : const char *msg;
4946 :
4947 13 : if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4948 : msg = G_("Equality comparison for %s at %L");
4949 : else
4950 6 : msg = G_("Inequality comparison for %s at %L");
4951 :
4952 13 : gfc_warning (OPT_Wcompare_reals, msg,
4953 : gfc_typename (op1), &op1->where);
4954 : }
4955 : }
4956 :
4957 : break;
4958 : }
4959 :
4960 1129 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4961 : {
4962 2 : CHECK_INTERFACES
4963 4 : gfc_error ("Logicals at %L must be compared with %s instead of %s",
4964 : &e->where,
4965 2 : (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4966 : ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4967 2 : }
4968 : else
4969 : {
4970 1127 : CHECK_INTERFACES
4971 113 : gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4972 : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4973 : gfc_typename (op2));
4974 : }
4975 :
4976 : return false;
4977 :
4978 282 : case INTRINSIC_USER:
4979 282 : if (e->value.op.uop->op == NULL)
4980 : {
4981 57 : const char *name = e->value.op.uop->name;
4982 57 : const char *guessed;
4983 57 : guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4984 57 : CHECK_INTERFACES
4985 5 : if (guessed)
4986 1 : gfc_error ("Unknown operator %qs at %L; did you mean "
4987 : "%qs?", name, &e->where, guessed);
4988 : else
4989 4 : gfc_error ("Unknown operator %qs at %L", name, &e->where);
4990 : }
4991 225 : else if (op2 == NULL)
4992 : {
4993 48 : CHECK_INTERFACES
4994 0 : gfc_error ("Operand of user operator %qs at %L is %s",
4995 0 : e->value.op.uop->name, &e->where, gfc_typename (op1));
4996 : }
4997 : else
4998 : {
4999 177 : e->value.op.uop->op->sym->attr.referenced = 1;
5000 177 : CHECK_INTERFACES
5001 5 : gfc_error ("Operands of user operator %qs at %L are %s/%s",
5002 5 : e->value.op.uop->name, &e->where, gfc_typename (op1),
5003 : gfc_typename (op2));
5004 : }
5005 :
5006 : return false;
5007 :
5008 23344 : case INTRINSIC_PARENTHESES:
5009 23344 : e->ts = op1->ts;
5010 23344 : if (e->ts.type == BT_CHARACTER)
5011 321 : e->ts.u.cl = op1->ts.u.cl;
5012 : break;
5013 :
5014 0 : default:
5015 0 : gfc_internal_error ("resolve_operator(): Bad intrinsic");
5016 : }
5017 :
5018 : /* Deal with arrayness of an operand through an operator. */
5019 :
5020 530326 : switch (e->value.op.op)
5021 : {
5022 478426 : case INTRINSIC_PLUS:
5023 478426 : case INTRINSIC_MINUS:
5024 478426 : case INTRINSIC_TIMES:
5025 478426 : case INTRINSIC_DIVIDE:
5026 478426 : case INTRINSIC_POWER:
5027 478426 : case INTRINSIC_CONCAT:
5028 478426 : case INTRINSIC_AND:
5029 478426 : case INTRINSIC_OR:
5030 478426 : case INTRINSIC_EQV:
5031 478426 : case INTRINSIC_NEQV:
5032 478426 : case INTRINSIC_EQ:
5033 478426 : case INTRINSIC_EQ_OS:
5034 478426 : case INTRINSIC_NE:
5035 478426 : case INTRINSIC_NE_OS:
5036 478426 : case INTRINSIC_GT:
5037 478426 : case INTRINSIC_GT_OS:
5038 478426 : case INTRINSIC_GE:
5039 478426 : case INTRINSIC_GE_OS:
5040 478426 : case INTRINSIC_LT:
5041 478426 : case INTRINSIC_LT_OS:
5042 478426 : case INTRINSIC_LE:
5043 478426 : case INTRINSIC_LE_OS:
5044 :
5045 478426 : if (op1->rank == 0 && op2->rank == 0)
5046 426189 : e->rank = 0;
5047 :
5048 478426 : if (op1->rank == 0 && op2->rank != 0)
5049 : {
5050 2529 : e->rank = op2->rank;
5051 :
5052 2529 : if (e->shape == NULL)
5053 2499 : e->shape = gfc_copy_shape (op2->shape, op2->rank);
5054 : }
5055 :
5056 478426 : if (op1->rank != 0 && op2->rank == 0)
5057 : {
5058 17163 : e->rank = op1->rank;
5059 :
5060 17163 : if (e->shape == NULL)
5061 17139 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
5062 : }
5063 :
5064 478426 : if (op1->rank != 0 && op2->rank != 0)
5065 : {
5066 32545 : if (op1->rank == op2->rank)
5067 : {
5068 32545 : e->rank = op1->rank;
5069 32545 : if (e->shape == NULL)
5070 : {
5071 32484 : t = compare_shapes (op1, op2);
5072 32484 : if (!t)
5073 3 : e->shape = NULL;
5074 : else
5075 32481 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
5076 : }
5077 : }
5078 : else
5079 : {
5080 : /* Allow higher level expressions to work. */
5081 0 : e->rank = 0;
5082 :
5083 : /* Try user-defined operators, and otherwise throw an error. */
5084 0 : CHECK_INTERFACES
5085 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
5086 0 : &op1->where, &op2->where);
5087 0 : return false;
5088 : }
5089 : }
5090 : break;
5091 :
5092 51900 : case INTRINSIC_PARENTHESES:
5093 51900 : case INTRINSIC_NOT:
5094 51900 : case INTRINSIC_UPLUS:
5095 51900 : case INTRINSIC_UMINUS:
5096 : /* Simply copy arrayness attribute */
5097 51900 : e->rank = op1->rank;
5098 51900 : e->corank = op1->corank;
5099 :
5100 51900 : if (e->shape == NULL)
5101 51893 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
5102 :
5103 : break;
5104 :
5105 : default:
5106 : break;
5107 : }
5108 :
5109 530868 : simplify_op:
5110 :
5111 : /* Attempt to simplify the expression. */
5112 3 : if (t)
5113 : {
5114 530865 : t = gfc_simplify_expr (e, 0);
5115 : /* Some calls do not succeed in simplification and return false
5116 : even though there is no error; e.g. variable references to
5117 : PARAMETER arrays. */
5118 530865 : if (!gfc_is_constant_expr (e))
5119 485049 : t = true;
5120 : }
5121 : return t;
5122 : }
5123 :
5124 : static bool
5125 162 : resolve_conditional (gfc_expr *expr)
5126 : {
5127 162 : gfc_expr *condition, *true_expr, *false_expr;
5128 :
5129 162 : condition = expr->value.conditional.condition;
5130 162 : true_expr = expr->value.conditional.true_expr;
5131 162 : false_expr = expr->value.conditional.false_expr;
5132 :
5133 324 : if (!gfc_resolve_expr (condition) || !gfc_resolve_expr (true_expr)
5134 324 : || !gfc_resolve_expr (false_expr))
5135 0 : return false;
5136 :
5137 162 : if (condition->ts.type != BT_LOGICAL || condition->rank != 0)
5138 : {
5139 2 : gfc_error (
5140 : "Condition in conditional expression must be a scalar logical at %L",
5141 : &condition->where);
5142 2 : return false;
5143 : }
5144 :
5145 160 : if (true_expr->ts.type != false_expr->ts.type)
5146 : {
5147 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5148 : "must have the same declared type",
5149 : &true_expr->where, &false_expr->where);
5150 1 : return false;
5151 : }
5152 :
5153 159 : if (true_expr->ts.kind != false_expr->ts.kind)
5154 : {
5155 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5156 : "must have the same kind parameter",
5157 : &true_expr->where, &false_expr->where);
5158 1 : return false;
5159 : }
5160 :
5161 158 : if (true_expr->rank != false_expr->rank)
5162 : {
5163 1 : gfc_error ("expr at %L and expr at %L in conditional expression "
5164 : "must have the same rank",
5165 : &true_expr->where, &false_expr->where);
5166 1 : return false;
5167 : }
5168 :
5169 : /* TODO: support more data types for conditional expressions */
5170 157 : if (true_expr->ts.type != BT_INTEGER && true_expr->ts.type != BT_LOGICAL
5171 157 : && true_expr->ts.type != BT_REAL && true_expr->ts.type != BT_COMPLEX
5172 67 : && true_expr->ts.type != BT_CHARACTER)
5173 : {
5174 1 : gfc_error (
5175 : "Sorry, only integer, logical, real, complex and character types are "
5176 : "currently supported for conditional expressions at %L",
5177 : &expr->where);
5178 1 : return false;
5179 : }
5180 :
5181 : /* TODO: support arrays in conditional expressions */
5182 156 : if (true_expr->rank > 0)
5183 : {
5184 1 : gfc_error ("Sorry, array is currently unsupported for conditional "
5185 : "expressions at %L",
5186 : &expr->where);
5187 1 : return false;
5188 : }
5189 :
5190 155 : expr->ts = true_expr->ts;
5191 155 : expr->rank = true_expr->rank;
5192 155 : return true;
5193 : }
5194 :
5195 : /************** Array resolution subroutines **************/
5196 :
5197 : enum compare_result
5198 : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
5199 :
5200 : /* Compare two integer expressions. */
5201 :
5202 : static compare_result
5203 467230 : compare_bound (gfc_expr *a, gfc_expr *b)
5204 : {
5205 467230 : int i;
5206 :
5207 467230 : if (a == NULL || a->expr_type != EXPR_CONSTANT
5208 307587 : || b == NULL || b->expr_type != EXPR_CONSTANT)
5209 : return CMP_UNKNOWN;
5210 :
5211 : /* If either of the types isn't INTEGER, we must have
5212 : raised an error earlier. */
5213 :
5214 212484 : if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
5215 : return CMP_UNKNOWN;
5216 :
5217 212480 : i = mpz_cmp (a->value.integer, b->value.integer);
5218 :
5219 212480 : if (i < 0)
5220 : return CMP_LT;
5221 99725 : if (i > 0)
5222 39657 : return CMP_GT;
5223 : return CMP_EQ;
5224 : }
5225 :
5226 :
5227 : /* Compare an integer expression with an integer. */
5228 :
5229 : static compare_result
5230 75104 : compare_bound_int (gfc_expr *a, int b)
5231 : {
5232 75104 : int i;
5233 :
5234 75104 : if (a == NULL
5235 32355 : || a->expr_type != EXPR_CONSTANT
5236 29407 : || a->ts.type != BT_INTEGER)
5237 : return CMP_UNKNOWN;
5238 :
5239 29407 : i = mpz_cmp_si (a->value.integer, b);
5240 :
5241 29407 : if (i < 0)
5242 : return CMP_LT;
5243 24933 : if (i > 0)
5244 21434 : return CMP_GT;
5245 : return CMP_EQ;
5246 : }
5247 :
5248 :
5249 : /* Compare an integer expression with a mpz_t. */
5250 :
5251 : static compare_result
5252 69848 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
5253 : {
5254 69848 : int i;
5255 :
5256 69848 : if (a == NULL
5257 57023 : || a->expr_type != EXPR_CONSTANT
5258 54900 : || a->ts.type != BT_INTEGER)
5259 : return CMP_UNKNOWN;
5260 :
5261 54897 : i = mpz_cmp (a->value.integer, b);
5262 :
5263 54897 : if (i < 0)
5264 : return CMP_LT;
5265 25003 : if (i > 0)
5266 10710 : return CMP_GT;
5267 : return CMP_EQ;
5268 : }
5269 :
5270 :
5271 : /* Compute the last value of a sequence given by a triplet.
5272 : Return 0 if it wasn't able to compute the last value, or if the
5273 : sequence if empty, and 1 otherwise. */
5274 :
5275 : static int
5276 52182 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
5277 : gfc_expr *stride, mpz_t last)
5278 : {
5279 52182 : mpz_t rem;
5280 :
5281 52182 : if (start == NULL || start->expr_type != EXPR_CONSTANT
5282 37067 : || end == NULL || end->expr_type != EXPR_CONSTANT
5283 32358 : || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
5284 : return 0;
5285 :
5286 32039 : if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
5287 32038 : || (stride != NULL && stride->ts.type != BT_INTEGER))
5288 : return 0;
5289 :
5290 6647 : if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
5291 : {
5292 25517 : if (compare_bound (start, end) == CMP_GT)
5293 : return 0;
5294 24128 : mpz_set (last, end->value.integer);
5295 24128 : return 1;
5296 : }
5297 :
5298 6521 : if (compare_bound_int (stride, 0) == CMP_GT)
5299 : {
5300 : /* Stride is positive */
5301 5156 : if (mpz_cmp (start->value.integer, end->value.integer) > 0)
5302 : return 0;
5303 : }
5304 : else
5305 : {
5306 : /* Stride is negative */
5307 1365 : if (mpz_cmp (start->value.integer, end->value.integer) < 0)
5308 : return 0;
5309 : }
5310 :
5311 6501 : mpz_init (rem);
5312 6501 : mpz_sub (rem, end->value.integer, start->value.integer);
5313 6501 : mpz_tdiv_r (rem, rem, stride->value.integer);
5314 6501 : mpz_sub (last, end->value.integer, rem);
5315 6501 : mpz_clear (rem);
5316 :
5317 6501 : return 1;
5318 : }
5319 :
5320 :
5321 : /* Compare a single dimension of an array reference to the array
5322 : specification. */
5323 :
5324 : static bool
5325 216503 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5326 : {
5327 216503 : mpz_t last_value;
5328 :
5329 216503 : if (ar->dimen_type[i] == DIMEN_STAR)
5330 : {
5331 498 : gcc_assert (ar->stride[i] == NULL);
5332 : /* This implies [*] as [*:] and [*:3] are not possible. */
5333 498 : if (ar->start[i] == NULL)
5334 : {
5335 406 : gcc_assert (ar->end[i] == NULL);
5336 : return true;
5337 : }
5338 : }
5339 :
5340 : /* Given start, end and stride values, calculate the minimum and
5341 : maximum referenced indexes. */
5342 :
5343 216097 : switch (ar->dimen_type[i])
5344 : {
5345 : case DIMEN_VECTOR:
5346 : case DIMEN_THIS_IMAGE:
5347 : break;
5348 :
5349 155627 : case DIMEN_STAR:
5350 155627 : case DIMEN_ELEMENT:
5351 155627 : if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5352 : {
5353 2 : if (i < as->rank)
5354 2 : gfc_warning (0, "Array reference at %L is out of bounds "
5355 : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5356 2 : mpz_get_si (ar->start[i]->value.integer),
5357 2 : mpz_get_si (as->lower[i]->value.integer), i+1);
5358 : else
5359 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5360 : "(%ld < %ld) in codimension %d", &ar->c_where[i],
5361 0 : mpz_get_si (ar->start[i]->value.integer),
5362 0 : mpz_get_si (as->lower[i]->value.integer),
5363 0 : i + 1 - as->rank);
5364 2 : return true;
5365 : }
5366 155625 : if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5367 : {
5368 39 : if (i < as->rank)
5369 39 : gfc_warning (0, "Array reference at %L is out of bounds "
5370 : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5371 39 : mpz_get_si (ar->start[i]->value.integer),
5372 39 : mpz_get_si (as->upper[i]->value.integer), i+1);
5373 : else
5374 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5375 : "(%ld > %ld) in codimension %d", &ar->c_where[i],
5376 0 : mpz_get_si (ar->start[i]->value.integer),
5377 0 : mpz_get_si (as->upper[i]->value.integer),
5378 0 : i + 1 - as->rank);
5379 39 : return true;
5380 : }
5381 :
5382 : break;
5383 :
5384 52227 : case DIMEN_RANGE:
5385 52227 : {
5386 : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5387 : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5388 :
5389 52227 : compare_result comp_start_end = compare_bound (AR_START, AR_END);
5390 52227 : compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5391 :
5392 : /* Check for zero stride, which is not allowed. */
5393 52227 : if (comp_stride_zero == CMP_EQ)
5394 : {
5395 1 : gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5396 1 : return false;
5397 : }
5398 :
5399 : /* if start == end || (stride > 0 && start < end)
5400 : || (stride < 0 && start > end),
5401 : then the array section contains at least one element. In this
5402 : case, there is an out-of-bounds access if
5403 : (start < lower || start > upper). */
5404 52226 : if (comp_start_end == CMP_EQ
5405 51464 : || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5406 48675 : && comp_start_end == CMP_LT)
5407 22896 : || (comp_stride_zero == CMP_LT
5408 22896 : && comp_start_end == CMP_GT))
5409 : {
5410 30675 : if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5411 : {
5412 27 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5413 : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5414 27 : mpz_get_si (AR_START->value.integer),
5415 27 : mpz_get_si (as->lower[i]->value.integer), i+1);
5416 27 : return true;
5417 : }
5418 30648 : if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5419 : {
5420 17 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5421 : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5422 17 : mpz_get_si (AR_START->value.integer),
5423 17 : mpz_get_si (as->upper[i]->value.integer), i+1);
5424 17 : return true;
5425 : }
5426 : }
5427 :
5428 : /* If we can compute the highest index of the array section,
5429 : then it also has to be between lower and upper. */
5430 52182 : mpz_init (last_value);
5431 52182 : if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5432 : last_value))
5433 : {
5434 30629 : if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5435 : {
5436 3 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5437 : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5438 : mpz_get_si (last_value),
5439 3 : mpz_get_si (as->lower[i]->value.integer), i+1);
5440 3 : mpz_clear (last_value);
5441 3 : return true;
5442 : }
5443 30626 : if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5444 : {
5445 7 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5446 : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5447 : mpz_get_si (last_value),
5448 7 : mpz_get_si (as->upper[i]->value.integer), i+1);
5449 7 : mpz_clear (last_value);
5450 7 : return true;
5451 : }
5452 : }
5453 52172 : mpz_clear (last_value);
5454 :
5455 : #undef AR_START
5456 : #undef AR_END
5457 : }
5458 52172 : break;
5459 :
5460 0 : default:
5461 0 : gfc_internal_error ("check_dimension(): Bad array reference");
5462 : }
5463 :
5464 : return true;
5465 : }
5466 :
5467 :
5468 : /* Compare an array reference with an array specification. */
5469 :
5470 : static bool
5471 426488 : compare_spec_to_ref (gfc_array_ref *ar)
5472 : {
5473 426488 : gfc_array_spec *as;
5474 426488 : int i;
5475 :
5476 426488 : as = ar->as;
5477 426488 : i = as->rank - 1;
5478 : /* TODO: Full array sections are only allowed as actual parameters. */
5479 426488 : if (as->type == AS_ASSUMED_SIZE
5480 5804 : && (/*ar->type == AR_FULL
5481 5804 : ||*/ (ar->type == AR_SECTION
5482 517 : && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5483 : {
5484 5 : gfc_error ("Rightmost upper bound of assumed size array section "
5485 : "not specified at %L", &ar->where);
5486 5 : return false;
5487 : }
5488 :
5489 426483 : if (ar->type == AR_FULL)
5490 : return true;
5491 :
5492 164415 : if (as->rank != ar->dimen)
5493 : {
5494 28 : gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5495 : &ar->where, ar->dimen, as->rank);
5496 28 : return false;
5497 : }
5498 :
5499 : /* ar->codimen == 0 is a local array. */
5500 164387 : if (as->corank != ar->codimen && ar->codimen != 0)
5501 : {
5502 0 : gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5503 : &ar->where, ar->codimen, as->corank);
5504 0 : return false;
5505 : }
5506 :
5507 371075 : for (i = 0; i < as->rank; i++)
5508 206689 : if (!check_dimension (i, ar, as))
5509 : return false;
5510 :
5511 : /* Local access has no coarray spec. */
5512 164386 : if (ar->codimen != 0)
5513 18870 : for (i = as->rank; i < as->rank + as->corank; i++)
5514 : {
5515 9816 : if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5516 6831 : && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5517 : {
5518 2 : gfc_error ("Coindex of codimension %d must be a scalar at %L",
5519 2 : i + 1 - as->rank, &ar->where);
5520 2 : return false;
5521 : }
5522 9814 : if (!check_dimension (i, ar, as))
5523 : return false;
5524 : }
5525 :
5526 : return true;
5527 : }
5528 :
5529 :
5530 : /* Resolve one part of an array index. */
5531 :
5532 : static bool
5533 734539 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5534 : int force_index_integer_kind)
5535 : {
5536 734539 : gfc_typespec ts;
5537 :
5538 734539 : if (index == NULL)
5539 : return true;
5540 :
5541 218216 : if (!gfc_resolve_expr (index))
5542 : return false;
5543 :
5544 218193 : if (check_scalar && index->rank != 0)
5545 : {
5546 2 : gfc_error ("Array index at %L must be scalar", &index->where);
5547 2 : return false;
5548 : }
5549 :
5550 218191 : if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5551 : {
5552 4 : gfc_error ("Array index at %L must be of INTEGER type, found %s",
5553 : &index->where, gfc_basic_typename (index->ts.type));
5554 4 : return false;
5555 : }
5556 :
5557 218187 : if (index->ts.type == BT_REAL)
5558 337 : if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5559 : &index->where))
5560 : return false;
5561 :
5562 218187 : if ((index->ts.kind != gfc_index_integer_kind
5563 213248 : && force_index_integer_kind)
5564 186716 : || (index->ts.type != BT_INTEGER
5565 : && index->ts.type != BT_UNKNOWN))
5566 : {
5567 31807 : gfc_clear_ts (&ts);
5568 31807 : ts.type = BT_INTEGER;
5569 31807 : ts.kind = gfc_index_integer_kind;
5570 :
5571 31807 : gfc_convert_type_warn (index, &ts, 2, 0);
5572 : }
5573 :
5574 : return true;
5575 : }
5576 :
5577 : /* Resolve one part of an array index. */
5578 :
5579 : bool
5580 489943 : gfc_resolve_index (gfc_expr *index, int check_scalar)
5581 : {
5582 489943 : return gfc_resolve_index_1 (index, check_scalar, 1);
5583 : }
5584 :
5585 : /* Resolve a dim argument to an intrinsic function. */
5586 :
5587 : bool
5588 23915 : gfc_resolve_dim_arg (gfc_expr *dim)
5589 : {
5590 23915 : if (dim == NULL)
5591 : return true;
5592 :
5593 23915 : if (!gfc_resolve_expr (dim))
5594 : return false;
5595 :
5596 23915 : if (dim->rank != 0)
5597 : {
5598 0 : gfc_error ("Argument dim at %L must be scalar", &dim->where);
5599 0 : return false;
5600 :
5601 : }
5602 :
5603 23915 : if (dim->ts.type != BT_INTEGER)
5604 : {
5605 0 : gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5606 0 : return false;
5607 : }
5608 :
5609 23915 : if (dim->ts.kind != gfc_index_integer_kind)
5610 : {
5611 15306 : gfc_typespec ts;
5612 :
5613 15306 : gfc_clear_ts (&ts);
5614 15306 : ts.type = BT_INTEGER;
5615 15306 : ts.kind = gfc_index_integer_kind;
5616 :
5617 15306 : gfc_convert_type_warn (dim, &ts, 2, 0);
5618 : }
5619 :
5620 : return true;
5621 : }
5622 :
5623 : /* Given an expression that contains array references, update those array
5624 : references to point to the right array specifications. While this is
5625 : filled in during matching, this information is difficult to save and load
5626 : in a module, so we take care of it here.
5627 :
5628 : The idea here is that the original array reference comes from the
5629 : base symbol. We traverse the list of reference structures, setting
5630 : the stored reference to references. Component references can
5631 : provide an additional array specification. */
5632 : static void
5633 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5634 :
5635 : static bool
5636 909 : find_array_spec (gfc_expr *e)
5637 : {
5638 909 : gfc_array_spec *as;
5639 909 : gfc_component *c;
5640 909 : gfc_ref *ref;
5641 909 : bool class_as = false;
5642 :
5643 909 : if (e->symtree->n.sym->assoc)
5644 : {
5645 217 : if (e->symtree->n.sym->assoc->target)
5646 217 : gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5647 217 : resolve_assoc_var (e->symtree->n.sym, false);
5648 : }
5649 :
5650 909 : if (e->symtree->n.sym->ts.type == BT_CLASS)
5651 : {
5652 124 : as = CLASS_DATA (e->symtree->n.sym)->as;
5653 124 : class_as = true;
5654 : }
5655 : else
5656 785 : as = e->symtree->n.sym->as;
5657 :
5658 2070 : for (ref = e->ref; ref; ref = ref->next)
5659 1168 : switch (ref->type)
5660 : {
5661 911 : case REF_ARRAY:
5662 911 : if (as == NULL)
5663 : {
5664 7 : locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5665 14 : ? ref->u.ar.where : e->where);
5666 7 : gfc_error ("Invalid array reference of a non-array entity at %L",
5667 : &loc);
5668 7 : return false;
5669 : }
5670 :
5671 904 : ref->u.ar.as = as;
5672 904 : if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5673 : as = NULL;
5674 : break;
5675 :
5676 233 : case REF_COMPONENT:
5677 233 : c = ref->u.c.component;
5678 233 : if (c->attr.dimension)
5679 : {
5680 102 : if (as != NULL && !(class_as && as == c->as))
5681 0 : gfc_internal_error ("find_array_spec(): unused as(1)");
5682 102 : as = c->as;
5683 : }
5684 :
5685 : break;
5686 :
5687 : case REF_SUBSTRING:
5688 : case REF_INQUIRY:
5689 : break;
5690 : }
5691 :
5692 902 : if (as != NULL)
5693 0 : gfc_internal_error ("find_array_spec(): unused as(2)");
5694 :
5695 : return true;
5696 : }
5697 :
5698 :
5699 : /* Resolve an array reference. */
5700 :
5701 : static bool
5702 427214 : resolve_array_ref (gfc_array_ref *ar)
5703 : {
5704 427214 : int i, check_scalar;
5705 427214 : gfc_expr *e;
5706 :
5707 671781 : for (i = 0; i < ar->dimen + ar->codimen; i++)
5708 : {
5709 244596 : check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5710 :
5711 : /* Do not force gfc_index_integer_kind for the start. We can
5712 : do fine with any integer kind. This avoids temporary arrays
5713 : created for indexing with a vector. */
5714 244596 : if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5715 : return false;
5716 244569 : if (!gfc_resolve_index (ar->end[i], check_scalar))
5717 : return false;
5718 244567 : if (!gfc_resolve_index (ar->stride[i], check_scalar))
5719 : return false;
5720 :
5721 244567 : e = ar->start[i];
5722 :
5723 244567 : if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5724 145754 : switch (e->rank)
5725 : {
5726 144662 : case 0:
5727 144662 : ar->dimen_type[i] = DIMEN_ELEMENT;
5728 144662 : break;
5729 :
5730 1092 : case 1:
5731 1092 : ar->dimen_type[i] = DIMEN_VECTOR;
5732 1092 : if (e->expr_type == EXPR_VARIABLE
5733 470 : && e->symtree->n.sym->ts.type == BT_DERIVED)
5734 13 : ar->start[i] = gfc_get_parentheses (e);
5735 : break;
5736 :
5737 0 : default:
5738 0 : gfc_error ("Array index at %L is an array of rank %d",
5739 : &ar->c_where[i], e->rank);
5740 0 : return false;
5741 : }
5742 :
5743 : /* Fill in the upper bound, which may be lower than the
5744 : specified one for something like a(2:10:5), which is
5745 : identical to a(2:7:5). Only relevant for strides not equal
5746 : to one. Don't try a division by zero. */
5747 244567 : if (ar->dimen_type[i] == DIMEN_RANGE
5748 71932 : && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5749 8384 : && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5750 8237 : && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5751 : {
5752 8236 : mpz_t size, end;
5753 :
5754 8236 : if (gfc_ref_dimen_size (ar, i, &size, &end))
5755 : {
5756 6531 : if (ar->end[i] == NULL)
5757 : {
5758 7926 : ar->end[i] =
5759 3963 : gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5760 : &ar->where);
5761 3963 : mpz_set (ar->end[i]->value.integer, end);
5762 : }
5763 2568 : else if (ar->end[i]->ts.type == BT_INTEGER
5764 2568 : && ar->end[i]->expr_type == EXPR_CONSTANT)
5765 : {
5766 2568 : mpz_set (ar->end[i]->value.integer, end);
5767 : }
5768 : else
5769 0 : gcc_unreachable ();
5770 :
5771 6531 : mpz_clear (size);
5772 6531 : mpz_clear (end);
5773 : }
5774 : }
5775 : }
5776 :
5777 427185 : if (ar->type == AR_FULL)
5778 : {
5779 265514 : if (ar->as->rank == 0)
5780 3412 : ar->type = AR_ELEMENT;
5781 :
5782 : /* Make sure array is the same as array(:,:), this way
5783 : we don't need to special case all the time. */
5784 265514 : ar->dimen = ar->as->rank;
5785 633010 : for (i = 0; i < ar->dimen; i++)
5786 : {
5787 367496 : ar->dimen_type[i] = DIMEN_RANGE;
5788 :
5789 367496 : gcc_assert (ar->start[i] == NULL);
5790 367496 : gcc_assert (ar->end[i] == NULL);
5791 367496 : gcc_assert (ar->stride[i] == NULL);
5792 : }
5793 : }
5794 :
5795 : /* If the reference type is unknown, figure out what kind it is. */
5796 :
5797 427185 : if (ar->type == AR_UNKNOWN)
5798 : {
5799 148596 : ar->type = AR_ELEMENT;
5800 287611 : for (i = 0; i < ar->dimen; i++)
5801 177156 : if (ar->dimen_type[i] == DIMEN_RANGE
5802 177156 : || ar->dimen_type[i] == DIMEN_VECTOR)
5803 : {
5804 38141 : ar->type = AR_SECTION;
5805 38141 : break;
5806 : }
5807 : }
5808 :
5809 427185 : if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5810 : return false;
5811 :
5812 427149 : if (ar->as->corank && ar->codimen == 0)
5813 : {
5814 2075 : int n;
5815 2075 : ar->codimen = ar->as->corank;
5816 5916 : for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5817 3841 : ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5818 : }
5819 :
5820 427149 : if (ar->codimen)
5821 : {
5822 13631 : if (ar->team_type == TEAM_NUMBER)
5823 : {
5824 60 : if (!gfc_resolve_expr (ar->team))
5825 : return false;
5826 :
5827 60 : if (ar->team->rank != 0)
5828 : {
5829 0 : gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5830 : &ar->team->where);
5831 0 : return false;
5832 : }
5833 :
5834 60 : if (ar->team->ts.type != BT_INTEGER)
5835 : {
5836 6 : gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5837 : "type, found %s",
5838 6 : &ar->team->where,
5839 : gfc_basic_typename (ar->team->ts.type));
5840 6 : return false;
5841 : }
5842 : }
5843 13571 : else if (ar->team_type == TEAM_TEAM)
5844 : {
5845 42 : if (!gfc_resolve_expr (ar->team))
5846 : return false;
5847 :
5848 42 : if (ar->team->rank != 0)
5849 : {
5850 3 : gfc_error ("TEAM argument at %L must be scalar",
5851 : &ar->team->where);
5852 3 : return false;
5853 : }
5854 :
5855 39 : if (ar->team->ts.type != BT_DERIVED
5856 36 : || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5857 36 : || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5858 : {
5859 3 : gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5860 : "the intrinsic module ISO_FORTRAN_ENV, found %s",
5861 3 : &ar->team->where,
5862 : gfc_basic_typename (ar->team->ts.type));
5863 3 : return false;
5864 : }
5865 : }
5866 13619 : if (ar->stat)
5867 : {
5868 62 : if (!gfc_resolve_expr (ar->stat))
5869 : return false;
5870 :
5871 62 : if (ar->stat->rank != 0)
5872 : {
5873 3 : gfc_error ("STAT argument at %L must be scalar",
5874 : &ar->stat->where);
5875 3 : return false;
5876 : }
5877 :
5878 59 : if (ar->stat->ts.type != BT_INTEGER)
5879 : {
5880 3 : gfc_error ("STAT argument at %L must be of INTEGER "
5881 : "type, found %s",
5882 3 : &ar->stat->where,
5883 : gfc_basic_typename (ar->stat->ts.type));
5884 3 : return false;
5885 : }
5886 :
5887 56 : if (ar->stat->expr_type != EXPR_VARIABLE)
5888 : {
5889 0 : gfc_error ("STAT's expression at %L must be a variable",
5890 : &ar->stat->where);
5891 0 : return false;
5892 : }
5893 : }
5894 : }
5895 : return true;
5896 : }
5897 :
5898 :
5899 : bool
5900 8794 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5901 : {
5902 8794 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5903 :
5904 8794 : if (ref->u.ss.start != NULL)
5905 : {
5906 8794 : if (!gfc_resolve_expr (ref->u.ss.start))
5907 : return false;
5908 :
5909 8794 : if (ref->u.ss.start->ts.type != BT_INTEGER)
5910 : {
5911 1 : gfc_error ("Substring start index at %L must be of type INTEGER",
5912 : &ref->u.ss.start->where);
5913 1 : return false;
5914 : }
5915 :
5916 8793 : if (ref->u.ss.start->rank != 0)
5917 : {
5918 0 : gfc_error ("Substring start index at %L must be scalar",
5919 : &ref->u.ss.start->where);
5920 0 : return false;
5921 : }
5922 :
5923 8793 : if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5924 8793 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5925 37 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5926 : {
5927 1 : gfc_error ("Substring start index at %L is less than one",
5928 : &ref->u.ss.start->where);
5929 1 : return false;
5930 : }
5931 : }
5932 :
5933 8792 : if (ref->u.ss.end != NULL)
5934 : {
5935 8598 : if (!gfc_resolve_expr (ref->u.ss.end))
5936 : return false;
5937 :
5938 8598 : if (ref->u.ss.end->ts.type != BT_INTEGER)
5939 : {
5940 1 : gfc_error ("Substring end index at %L must be of type INTEGER",
5941 : &ref->u.ss.end->where);
5942 1 : return false;
5943 : }
5944 :
5945 8597 : if (ref->u.ss.end->rank != 0)
5946 : {
5947 0 : gfc_error ("Substring end index at %L must be scalar",
5948 : &ref->u.ss.end->where);
5949 0 : return false;
5950 : }
5951 :
5952 8597 : if (ref->u.ss.length != NULL
5953 8261 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5954 8609 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5955 12 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5956 : {
5957 4 : gfc_error ("Substring end index at %L exceeds the string length",
5958 : &ref->u.ss.start->where);
5959 4 : return false;
5960 : }
5961 :
5962 8593 : if (compare_bound_mpz_t (ref->u.ss.end,
5963 8593 : gfc_integer_kinds[k].huge) == CMP_GT
5964 8593 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5965 7 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5966 : {
5967 4 : gfc_error ("Substring end index at %L is too large",
5968 : &ref->u.ss.end->where);
5969 4 : return false;
5970 : }
5971 : /* If the substring has the same length as the original
5972 : variable, the reference itself can be deleted. */
5973 :
5974 8589 : if (ref->u.ss.length != NULL
5975 8253 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5976 9505 : && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5977 230 : *equal_length = true;
5978 : }
5979 :
5980 : return true;
5981 : }
5982 :
5983 :
5984 : /* This function supplies missing substring charlens. */
5985 :
5986 : void
5987 4564 : gfc_resolve_substring_charlen (gfc_expr *e)
5988 : {
5989 4564 : gfc_ref *char_ref;
5990 4564 : gfc_expr *start, *end;
5991 4564 : gfc_typespec *ts = NULL;
5992 4564 : mpz_t diff;
5993 :
5994 8889 : for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5995 : {
5996 7042 : if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5997 : break;
5998 4325 : if (char_ref->type == REF_COMPONENT)
5999 328 : ts = &char_ref->u.c.component->ts;
6000 : }
6001 :
6002 4564 : if (!char_ref || char_ref->type == REF_INQUIRY)
6003 1909 : return;
6004 :
6005 2717 : gcc_assert (char_ref->next == NULL);
6006 :
6007 2717 : if (e->ts.u.cl)
6008 : {
6009 120 : if (e->ts.u.cl->length)
6010 108 : gfc_free_expr (e->ts.u.cl->length);
6011 12 : else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
6012 : return;
6013 : }
6014 :
6015 2705 : if (!e->ts.u.cl)
6016 2597 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6017 :
6018 2705 : if (char_ref->u.ss.start)
6019 2705 : start = gfc_copy_expr (char_ref->u.ss.start);
6020 : else
6021 0 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
6022 :
6023 2705 : if (char_ref->u.ss.end)
6024 2655 : end = gfc_copy_expr (char_ref->u.ss.end);
6025 50 : else if (e->expr_type == EXPR_VARIABLE)
6026 : {
6027 50 : if (!ts)
6028 32 : ts = &e->symtree->n.sym->ts;
6029 50 : end = gfc_copy_expr (ts->u.cl->length);
6030 : }
6031 : else
6032 : end = NULL;
6033 :
6034 2705 : if (!start || !end)
6035 : {
6036 50 : gfc_free_expr (start);
6037 50 : gfc_free_expr (end);
6038 50 : return;
6039 : }
6040 :
6041 : /* Length = (end - start + 1).
6042 : Check first whether it has a constant length. */
6043 2655 : if (gfc_dep_difference (end, start, &diff))
6044 : {
6045 2539 : gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
6046 : &e->where);
6047 :
6048 2539 : mpz_add_ui (len->value.integer, diff, 1);
6049 2539 : mpz_clear (diff);
6050 2539 : e->ts.u.cl->length = len;
6051 : /* The check for length < 0 is handled below */
6052 : }
6053 : else
6054 : {
6055 116 : e->ts.u.cl->length = gfc_subtract (end, start);
6056 116 : e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
6057 : gfc_get_int_expr (gfc_charlen_int_kind,
6058 : NULL, 1));
6059 : }
6060 :
6061 : /* F2008, 6.4.1: Both the starting point and the ending point shall
6062 : be within the range 1, 2, ..., n unless the starting point exceeds
6063 : the ending point, in which case the substring has length zero. */
6064 :
6065 2655 : if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
6066 15 : mpz_set_si (e->ts.u.cl->length->value.integer, 0);
6067 :
6068 2655 : e->ts.u.cl->length->ts.type = BT_INTEGER;
6069 2655 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6070 :
6071 : /* Make sure that the length is simplified. */
6072 2655 : gfc_simplify_expr (e->ts.u.cl->length, 1);
6073 2655 : gfc_resolve_expr (e->ts.u.cl->length);
6074 : }
6075 :
6076 :
6077 : /* Convert an array reference to an array element so that PDT KIND and LEN
6078 : or inquiry references are always scalar. */
6079 :
6080 : static void
6081 21 : reset_array_ref_to_scalar (gfc_expr *expr, gfc_ref *array_ref)
6082 : {
6083 21 : gfc_expr *unity = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6084 21 : int dim;
6085 :
6086 21 : array_ref->u.ar.type = AR_ELEMENT;
6087 21 : expr->rank = 0;
6088 : /* Suppress the runtime bounds check. */
6089 21 : expr->no_bounds_check = 1;
6090 42 : for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
6091 : {
6092 21 : array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
6093 21 : if (array_ref->u.ar.start[dim])
6094 0 : gfc_free_expr (array_ref->u.ar.start[dim]);
6095 :
6096 21 : if (array_ref->u.ar.as && array_ref->u.ar.as->lower[dim])
6097 9 : array_ref->u.ar.start[dim]
6098 9 : = gfc_copy_expr (array_ref->u.ar.as->lower[dim]);
6099 : else
6100 12 : array_ref->u.ar.start[dim] = gfc_copy_expr (unity);
6101 :
6102 21 : if (array_ref->u.ar.end[dim])
6103 0 : gfc_free_expr (array_ref->u.ar.end[dim]);
6104 21 : if (array_ref->u.ar.stride[dim])
6105 0 : gfc_free_expr (array_ref->u.ar.stride[dim]);
6106 : }
6107 21 : gfc_free_expr (unity);
6108 21 : }
6109 :
6110 :
6111 : /* Resolve subtype references. */
6112 :
6113 : bool
6114 544059 : gfc_resolve_ref (gfc_expr *expr)
6115 : {
6116 544059 : int current_part_dimension, n_components, seen_part_dimension;
6117 544059 : gfc_ref *ref, **prev, *array_ref;
6118 544059 : bool equal_length;
6119 544059 : gfc_symbol *last_pdt = NULL;
6120 :
6121 1068640 : for (ref = expr->ref; ref; ref = ref->next)
6122 525490 : if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
6123 : {
6124 909 : if (!find_array_spec (expr))
6125 : return false;
6126 : break;
6127 : }
6128 :
6129 1594935 : for (prev = &expr->ref; *prev != NULL;
6130 525544 : prev = *prev == NULL ? prev : &(*prev)->next)
6131 525635 : switch ((*prev)->type)
6132 : {
6133 427214 : case REF_ARRAY:
6134 427214 : if (!resolve_array_ref (&(*prev)->u.ar))
6135 : return false;
6136 : break;
6137 :
6138 : case REF_COMPONENT:
6139 : case REF_INQUIRY:
6140 : break;
6141 :
6142 8513 : case REF_SUBSTRING:
6143 8513 : equal_length = false;
6144 8513 : if (!gfc_resolve_substring (*prev, &equal_length))
6145 : return false;
6146 :
6147 8505 : if (expr->expr_type != EXPR_SUBSTRING && equal_length)
6148 : {
6149 : /* Remove the reference and move the charlen, if any. */
6150 205 : ref = *prev;
6151 205 : *prev = ref->next;
6152 205 : ref->next = NULL;
6153 205 : expr->ts.u.cl = ref->u.ss.length;
6154 205 : ref->u.ss.length = NULL;
6155 205 : gfc_free_ref_list (ref);
6156 : }
6157 : break;
6158 : }
6159 :
6160 : /* Check constraints on part references. */
6161 :
6162 543961 : current_part_dimension = 0;
6163 543961 : seen_part_dimension = 0;
6164 543961 : n_components = 0;
6165 543961 : array_ref = NULL;
6166 :
6167 543961 : if (expr->expr_type == EXPR_VARIABLE && IS_PDT (expr))
6168 534 : last_pdt = expr->symtree->n.sym->ts.u.derived;
6169 :
6170 1069275 : for (ref = expr->ref; ref; ref = ref->next)
6171 : {
6172 525325 : switch (ref->type)
6173 : {
6174 427124 : case REF_ARRAY:
6175 427124 : array_ref = ref;
6176 427124 : switch (ref->u.ar.type)
6177 : {
6178 262100 : case AR_FULL:
6179 : /* Coarray scalar. */
6180 262100 : if (ref->u.ar.as->rank == 0)
6181 : {
6182 : current_part_dimension = 0;
6183 : break;
6184 : }
6185 : /* Fall through. */
6186 303305 : case AR_SECTION:
6187 303305 : current_part_dimension = 1;
6188 303305 : break;
6189 :
6190 123819 : case AR_ELEMENT:
6191 123819 : array_ref = NULL;
6192 123819 : current_part_dimension = 0;
6193 123819 : break;
6194 :
6195 0 : case AR_UNKNOWN:
6196 0 : gfc_internal_error ("resolve_ref(): Bad array reference");
6197 : }
6198 :
6199 : break;
6200 :
6201 89080 : case REF_COMPONENT:
6202 89080 : if (current_part_dimension || seen_part_dimension)
6203 : {
6204 : /* F03:C614. */
6205 6846 : if (ref->u.c.component->attr.pointer
6206 6843 : || ref->u.c.component->attr.proc_pointer
6207 6842 : || (ref->u.c.component->ts.type == BT_CLASS
6208 1 : && CLASS_DATA (ref->u.c.component)->attr.pointer))
6209 : {
6210 4 : gfc_error ("Component to the right of a part reference "
6211 : "with nonzero rank must not have the POINTER "
6212 : "attribute at %L", &expr->where);
6213 4 : return false;
6214 : }
6215 6842 : else if (ref->u.c.component->attr.allocatable
6216 6836 : || (ref->u.c.component->ts.type == BT_CLASS
6217 1 : && CLASS_DATA (ref->u.c.component)->attr.allocatable))
6218 :
6219 : {
6220 7 : gfc_error ("Component to the right of a part reference "
6221 : "with nonzero rank must not have the ALLOCATABLE "
6222 : "attribute at %L", &expr->where);
6223 7 : return false;
6224 : }
6225 : }
6226 :
6227 : /* Sometimes the component in a component reference is that of the
6228 : pdt_template. Point to the component of pdt_type instead. This
6229 : ensures that the component gets a backend_decl in translation. */
6230 89069 : if (last_pdt)
6231 : {
6232 501 : gfc_component *cmp = last_pdt->components;
6233 1207 : for (; cmp; cmp = cmp->next)
6234 1202 : if (!strcmp (cmp->name, ref->u.c.component->name))
6235 : {
6236 496 : ref->u.c.component = cmp;
6237 496 : break;
6238 : }
6239 501 : ref->u.c.sym = last_pdt;
6240 : }
6241 :
6242 : /* Convert pdt_templates, if necessary, and update 'last_pdt'. */
6243 89069 : if (ref->u.c.component->ts.type == BT_DERIVED)
6244 : {
6245 20809 : if (ref->u.c.component->ts.u.derived->attr.pdt_template)
6246 : {
6247 0 : if (gfc_get_pdt_instance (ref->u.c.component->param_list,
6248 : &ref->u.c.component->ts.u.derived,
6249 : NULL) != MATCH_YES)
6250 : return false;
6251 0 : last_pdt = ref->u.c.component->ts.u.derived;
6252 : }
6253 20809 : else if (ref->u.c.component->ts.u.derived->attr.pdt_type)
6254 521 : last_pdt = ref->u.c.component->ts.u.derived;
6255 : else
6256 : last_pdt = NULL;
6257 : }
6258 :
6259 : /* The F08 standard requires(See R425, R431, R435, and in particular
6260 : Note 6.7) that a PDT parameter reference be a scalar even if
6261 : the designator is an array." */
6262 89069 : if (array_ref && last_pdt && last_pdt->attr.pdt_type
6263 83 : && (ref->u.c.component->attr.pdt_kind
6264 83 : || ref->u.c.component->attr.pdt_len))
6265 7 : reset_array_ref_to_scalar (expr, array_ref);
6266 :
6267 89069 : n_components++;
6268 89069 : break;
6269 :
6270 : case REF_SUBSTRING:
6271 : break;
6272 :
6273 821 : case REF_INQUIRY:
6274 : /* Implement requirement in note 9.7 of F2018 that the result of the
6275 : LEN inquiry be a scalar. */
6276 821 : if (ref->u.i == INQUIRY_LEN && array_ref
6277 40 : && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
6278 40 : || expr->ts.type == BT_INTEGER))
6279 14 : reset_array_ref_to_scalar (expr, array_ref);
6280 : break;
6281 : }
6282 :
6283 525314 : if (((ref->type == REF_COMPONENT && n_components > 1)
6284 512025 : || ref->next == NULL)
6285 : && current_part_dimension
6286 460731 : && seen_part_dimension)
6287 : {
6288 0 : gfc_error ("Two or more part references with nonzero rank must "
6289 : "not be specified at %L", &expr->where);
6290 0 : return false;
6291 : }
6292 :
6293 525314 : if (ref->type == REF_COMPONENT)
6294 : {
6295 89069 : if (current_part_dimension)
6296 6648 : seen_part_dimension = 1;
6297 :
6298 : /* reset to make sure */
6299 : current_part_dimension = 0;
6300 : }
6301 : }
6302 :
6303 : return true;
6304 : }
6305 :
6306 :
6307 : /* Given an expression, determine its shape. This is easier than it sounds.
6308 : Leaves the shape array NULL if it is not possible to determine the shape. */
6309 :
6310 : static void
6311 2599685 : expression_shape (gfc_expr *e)
6312 : {
6313 2599685 : mpz_t array[GFC_MAX_DIMENSIONS];
6314 2599685 : int i;
6315 :
6316 2599685 : if (e->rank <= 0 || e->shape != NULL)
6317 2423217 : return;
6318 :
6319 706432 : for (i = 0; i < e->rank; i++)
6320 477031 : if (!gfc_array_dimen_size (e, i, &array[i]))
6321 176468 : goto fail;
6322 :
6323 229401 : e->shape = gfc_get_shape (e->rank);
6324 :
6325 229401 : memcpy (e->shape, array, e->rank * sizeof (mpz_t));
6326 :
6327 229401 : return;
6328 :
6329 176468 : fail:
6330 178139 : for (i--; i >= 0; i--)
6331 1671 : mpz_clear (array[i]);
6332 : }
6333 :
6334 :
6335 : /* Given a variable expression node, compute the rank of the expression by
6336 : examining the base symbol and any reference structures it may have. */
6337 :
6338 : void
6339 2599685 : gfc_expression_rank (gfc_expr *e)
6340 : {
6341 2599685 : gfc_ref *ref, *last_arr_ref = nullptr;
6342 2599685 : int i, rank, corank;
6343 :
6344 : /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
6345 : could lead to serious confusion... */
6346 2599685 : gcc_assert (e->expr_type != EXPR_COMPCALL);
6347 :
6348 2599685 : if (e->ref == NULL)
6349 : {
6350 1916324 : if (e->expr_type == EXPR_ARRAY)
6351 72116 : goto done;
6352 : /* Constructors can have a rank different from one via RESHAPE(). */
6353 :
6354 1844208 : if (e->symtree != NULL)
6355 : {
6356 : /* After errors the ts.u.derived of a CLASS might not be set. */
6357 1844196 : gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
6358 13865 : && e->symtree->n.sym->ts.u.derived
6359 13860 : && CLASS_DATA (e->symtree->n.sym))
6360 1844196 : ? CLASS_DATA (e->symtree->n.sym)->as
6361 : : e->symtree->n.sym->as;
6362 1844196 : if (as)
6363 : {
6364 595 : e->rank = as->rank;
6365 595 : e->corank = as->corank;
6366 595 : goto done;
6367 : }
6368 : }
6369 1843613 : e->rank = 0;
6370 1843613 : e->corank = 0;
6371 1843613 : goto done;
6372 : }
6373 :
6374 : rank = 0;
6375 : corank = 0;
6376 :
6377 1080071 : for (ref = e->ref; ref; ref = ref->next)
6378 : {
6379 790441 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6380 553 : && ref->u.c.component->attr.function && !ref->next)
6381 : {
6382 357 : rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6383 357 : corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6384 : }
6385 :
6386 790441 : if (ref->type != REF_ARRAY)
6387 157817 : continue;
6388 :
6389 632624 : last_arr_ref = ref;
6390 632624 : if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6391 : {
6392 347858 : rank = ref->u.ar.as->rank;
6393 347858 : break;
6394 : }
6395 :
6396 284766 : if (ref->u.ar.type == AR_SECTION)
6397 : {
6398 : /* Figure out the rank of the section. */
6399 45873 : if (rank != 0)
6400 0 : gfc_internal_error ("gfc_expression_rank(): Two array specs");
6401 :
6402 114262 : for (i = 0; i < ref->u.ar.dimen; i++)
6403 68389 : if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6404 68389 : || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6405 59665 : rank++;
6406 :
6407 : break;
6408 : }
6409 : }
6410 683361 : if (last_arr_ref && last_arr_ref->u.ar.as
6411 613042 : && last_arr_ref->u.ar.as->rank != -1)
6412 : {
6413 19272 : for (i = last_arr_ref->u.ar.as->rank;
6414 624175 : i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6415 : {
6416 : /* For unknown dimen in non-resolved as assume full corank. */
6417 20162 : if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6418 19595 : || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6419 323 : && !last_arr_ref->u.ar.as->resolved))
6420 : {
6421 : corank = last_arr_ref->u.ar.as->corank;
6422 : break;
6423 : }
6424 19272 : else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6425 19272 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6426 19174 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6427 16683 : corank++;
6428 2589 : else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6429 0 : gfc_internal_error ("Illegal coarray index");
6430 : }
6431 : }
6432 :
6433 683361 : e->rank = rank;
6434 683361 : e->corank = corank;
6435 :
6436 2599685 : done:
6437 2599685 : expression_shape (e);
6438 2599685 : }
6439 :
6440 :
6441 : /* Given two expressions, check that their rank is conformable, i.e. either
6442 : both have the same rank or at least one is a scalar. */
6443 :
6444 : bool
6445 12237389 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6446 : {
6447 12237389 : if (op1->expr_type == EXPR_VARIABLE)
6448 733600 : gfc_expression_rank (op1);
6449 12237389 : if (op2->expr_type == EXPR_VARIABLE)
6450 446481 : gfc_expression_rank (op2);
6451 :
6452 76972 : return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6453 12314035 : && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
6454 30 : || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
6455 : }
6456 :
6457 : /* Resolve a variable expression. */
6458 :
6459 : static bool
6460 1328823 : resolve_variable (gfc_expr *e)
6461 : {
6462 1328823 : gfc_symbol *sym;
6463 1328823 : bool t;
6464 :
6465 1328823 : t = true;
6466 :
6467 1328823 : if (e->symtree == NULL)
6468 : return false;
6469 1328378 : sym = e->symtree->n.sym;
6470 :
6471 : /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6472 : as ts.type is set to BT_ASSUMED in resolve_symbol. */
6473 1328378 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6474 : {
6475 183 : if (!actual_arg || inquiry_argument)
6476 : {
6477 2 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6478 : "be used as actual argument", sym->name, &e->where);
6479 2 : return false;
6480 : }
6481 : }
6482 : /* TS 29113, 407b. */
6483 1328195 : else if (e->ts.type == BT_ASSUMED)
6484 : {
6485 571 : if (!actual_arg)
6486 : {
6487 20 : gfc_error ("Assumed-type variable %s at %L may only be used "
6488 : "as actual argument", sym->name, &e->where);
6489 20 : return false;
6490 : }
6491 551 : else if (inquiry_argument && !first_actual_arg)
6492 : {
6493 : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6494 : for all inquiry functions in resolve_function; the reason is
6495 : that the function-name resolution happens too late in that
6496 : function. */
6497 0 : gfc_error ("Assumed-type variable %s at %L as actual argument to "
6498 : "an inquiry function shall be the first argument",
6499 : sym->name, &e->where);
6500 0 : return false;
6501 : }
6502 : }
6503 : /* TS 29113, C535b. */
6504 1327624 : else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6505 37375 : && sym->ts.u.derived && CLASS_DATA (sym)
6506 37370 : && CLASS_DATA (sym)->as
6507 14588 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6508 1326714 : || (sym->ts.type != BT_CLASS && sym->as
6509 363703 : && sym->as->type == AS_ASSUMED_RANK))
6510 7931 : && !sym->attr.select_rank_temporary
6511 7931 : && !(sym->assoc && sym->assoc->ar))
6512 : {
6513 7931 : if (!actual_arg
6514 1253 : && !(cs_base && cs_base->current
6515 1252 : && (cs_base->current->op == EXEC_SELECT_RANK
6516 188 : || sym->attr.target)))
6517 : {
6518 144 : gfc_error ("Assumed-rank variable %s at %L may only be used as "
6519 : "actual argument", sym->name, &e->where);
6520 144 : return false;
6521 : }
6522 7787 : else if (inquiry_argument && !first_actual_arg)
6523 : {
6524 : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6525 : for all inquiry functions in resolve_function; the reason is
6526 : that the function-name resolution happens too late in that
6527 : function. */
6528 0 : gfc_error ("Assumed-rank variable %s at %L as actual argument "
6529 : "to an inquiry function shall be the first argument",
6530 : sym->name, &e->where);
6531 0 : return false;
6532 : }
6533 : }
6534 :
6535 1328212 : if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6536 181 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6537 180 : && e->ref->next == NULL))
6538 : {
6539 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6540 : "a subobject reference", sym->name, &e->ref->u.ar.where);
6541 1 : return false;
6542 : }
6543 : /* TS 29113, 407b. */
6544 1328211 : else if (e->ts.type == BT_ASSUMED && e->ref
6545 687 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6546 680 : && e->ref->next == NULL))
6547 : {
6548 7 : gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6549 : "reference", sym->name, &e->ref->u.ar.where);
6550 7 : return false;
6551 : }
6552 :
6553 : /* TS 29113, C535b. */
6554 1328204 : if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6555 37375 : && sym->ts.u.derived && CLASS_DATA (sym)
6556 37370 : && CLASS_DATA (sym)->as
6557 14588 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6558 1327294 : || (sym->ts.type != BT_CLASS && sym->as
6559 364239 : && sym->as->type == AS_ASSUMED_RANK))
6560 8071 : && !(sym->assoc && sym->assoc->ar)
6561 8071 : && e->ref
6562 8071 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6563 8067 : && e->ref->next == NULL))
6564 : {
6565 4 : gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6566 : "reference", sym->name, &e->ref->u.ar.where);
6567 4 : return false;
6568 : }
6569 :
6570 : /* Guessed type variables are associate_names whose selector had not been
6571 : parsed at the time that the construct was parsed. Now the namespace is
6572 : being resolved, the TKR of the selector will be available for fixup of
6573 : the associate_name. */
6574 1328200 : if (IS_INFERRED_TYPE (e) && e->ref)
6575 : {
6576 408 : gfc_fixup_inferred_type_refs (e);
6577 : /* KIND inquiry ref returns the kind of the target. */
6578 408 : if (e->expr_type == EXPR_CONSTANT)
6579 : return true;
6580 : }
6581 1327792 : else if (IS_INFERRED_TYPE (e)
6582 391 : && sym->ts.type != BT_UNKNOWN
6583 391 : && (sym->ts.type != e->ts.type || sym->ts.kind != e->ts.kind))
6584 : /* No subobject ref, but the expression's typespec was set at parse
6585 : time before the target's actual type/kind was known. Refresh from
6586 : the now-resolved associate-name symbol. */
6587 133 : e->ts = sym->ts;
6588 1327659 : else if (sym->attr.select_type_temporary
6589 8972 : && sym->ns->assoc_name_inferred)
6590 92 : gfc_fixup_inferred_type_refs (e);
6591 :
6592 : /* For variables that are used in an associate (target => object) where
6593 : the object's basetype is array valued while the target is scalar,
6594 : the ts' type of the component refs is still array valued, which
6595 : can't be translated that way. */
6596 1328188 : if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6597 603 : && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6598 603 : && sym->assoc->target->ts.u.derived
6599 603 : && CLASS_DATA (sym->assoc->target)
6600 603 : && CLASS_DATA (sym->assoc->target)->as)
6601 : {
6602 : gfc_ref *ref = e->ref;
6603 697 : while (ref)
6604 : {
6605 539 : switch (ref->type)
6606 : {
6607 236 : case REF_COMPONENT:
6608 236 : ref->u.c.sym = sym->ts.u.derived;
6609 : /* Stop the loop. */
6610 236 : ref = NULL;
6611 236 : break;
6612 303 : default:
6613 303 : ref = ref->next;
6614 303 : break;
6615 : }
6616 : }
6617 : }
6618 :
6619 : /* If this is an associate-name, it may be parsed with an array reference
6620 : in error even though the target is scalar. Fail directly in this case.
6621 : TODO Understand why class scalar expressions must be excluded. */
6622 1328188 : if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6623 : {
6624 11628 : if (sym->ts.type == BT_CLASS)
6625 242 : gfc_fix_class_refs (e);
6626 11628 : if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6627 2151 : && e->ref->type == REF_ARRAY)
6628 : {
6629 : /* Unambiguously scalar! */
6630 3 : if (sym->assoc->target
6631 3 : && (sym->assoc->target->expr_type == EXPR_CONSTANT
6632 1 : || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6633 2 : gfc_error ("Scalar variable %qs has an array reference at %L",
6634 : sym->name, &e->where);
6635 3 : return false;
6636 : }
6637 11625 : else if ((sym->attr.dimension || sym->attr.codimension)
6638 6989 : && (!e->ref || e->ref->type != REF_ARRAY))
6639 : {
6640 : /* This can happen because the parser did not detect that the
6641 : associate name is an array and the expression had no array
6642 : part_ref. */
6643 147 : gfc_ref *ref = gfc_get_ref ();
6644 147 : ref->type = REF_ARRAY;
6645 147 : ref->u.ar.type = AR_FULL;
6646 147 : if (sym->as)
6647 : {
6648 146 : ref->u.ar.as = sym->as;
6649 146 : ref->u.ar.dimen = sym->as->rank;
6650 : }
6651 147 : ref->next = e->ref;
6652 147 : e->ref = ref;
6653 : }
6654 : }
6655 :
6656 1328185 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6657 0 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6658 :
6659 : /* On the other hand, the parser may not have known this is an array;
6660 : in this case, we have to add a FULL reference. */
6661 1328185 : if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6662 : {
6663 0 : e->ref = gfc_get_ref ();
6664 0 : e->ref->type = REF_ARRAY;
6665 0 : e->ref->u.ar.type = AR_FULL;
6666 0 : e->ref->u.ar.dimen = 0;
6667 : }
6668 :
6669 : /* Like above, but for class types, where the checking whether an array
6670 : ref is present is more complicated. Furthermore make sure not to add
6671 : the full array ref to _vptr or _len refs. */
6672 1328185 : if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6673 1018 : && CLASS_DATA (sym)
6674 1018 : && (CLASS_DATA (sym)->attr.dimension
6675 449 : || CLASS_DATA (sym)->attr.codimension)
6676 575 : && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6677 : {
6678 551 : gfc_ref *ref, *newref;
6679 :
6680 551 : newref = gfc_get_ref ();
6681 551 : newref->type = REF_ARRAY;
6682 551 : newref->u.ar.type = AR_FULL;
6683 551 : newref->u.ar.dimen = 0;
6684 :
6685 : /* Because this is an associate var and the first ref either is a ref to
6686 : the _data component or not, no traversal of the ref chain is
6687 : needed. The array ref needs to be inserted after the _data ref,
6688 : or when that is not present, which may happened for polymorphic
6689 : types, then at the first position. */
6690 551 : ref = e->ref;
6691 551 : if (!ref)
6692 18 : e->ref = newref;
6693 533 : else if (ref->type == REF_COMPONENT
6694 230 : && strcmp ("_data", ref->u.c.component->name) == 0)
6695 : {
6696 230 : if (!ref->next || ref->next->type != REF_ARRAY)
6697 : {
6698 12 : newref->next = ref->next;
6699 12 : ref->next = newref;
6700 : }
6701 : else
6702 : /* Array ref present already. */
6703 218 : gfc_free_ref_list (newref);
6704 : }
6705 303 : else if (ref->type == REF_ARRAY)
6706 : /* Array ref present already. */
6707 303 : gfc_free_ref_list (newref);
6708 : else
6709 : {
6710 0 : newref->next = ref;
6711 0 : e->ref = newref;
6712 : }
6713 : }
6714 1327634 : else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6715 : {
6716 486 : gfc_ref *ref;
6717 910 : for (ref = e->ref; ref; ref = ref->next)
6718 454 : if (ref->type == REF_SUBSTRING)
6719 : break;
6720 486 : if (ref == NULL)
6721 456 : e->ts = sym->ts;
6722 : }
6723 :
6724 1328185 : if (e->ref && !gfc_resolve_ref (e))
6725 : return false;
6726 :
6727 1328092 : if (sym->attr.flavor == FL_PROCEDURE
6728 31704 : && (!sym->attr.function
6729 18602 : || (sym->attr.function && sym->result
6730 18154 : && sym->result->attr.proc_pointer
6731 713 : && !sym->result->attr.function)))
6732 : {
6733 13102 : e->ts.type = BT_PROCEDURE;
6734 13102 : goto resolve_procedure;
6735 : }
6736 :
6737 1314990 : if (sym->ts.type != BT_UNKNOWN)
6738 1314230 : gfc_variable_attr (e, &e->ts);
6739 760 : else if (sym->attr.flavor == FL_PROCEDURE
6740 12 : && sym->attr.function && sym->result
6741 12 : && sym->result->ts.type != BT_UNKNOWN
6742 10 : && sym->result->attr.proc_pointer)
6743 10 : e->ts = sym->result->ts;
6744 : else
6745 : {
6746 : /* Must be a simple variable reference. */
6747 750 : if (!gfc_set_default_type (sym, 1, sym->ns))
6748 : return false;
6749 624 : e->ts = sym->ts;
6750 : }
6751 :
6752 1314864 : if (check_assumed_size_reference (sym, e))
6753 : return false;
6754 :
6755 : /* Deal with forward references to entries during gfc_resolve_code, to
6756 : satisfy, at least partially, 12.5.2.5. */
6757 1314845 : if (gfc_current_ns->entries
6758 3229 : && current_entry_id == sym->entry_id
6759 1050 : && cs_base
6760 964 : && cs_base->current
6761 964 : && cs_base->current->op != EXEC_ENTRY)
6762 : {
6763 964 : int n;
6764 964 : bool saved_specification_expr;
6765 964 : gfc_symbol *saved_specification_expr_symbol;
6766 :
6767 : /* If the symbol is a dummy... */
6768 964 : if (sym->attr.dummy && sym->ns == gfc_current_ns)
6769 : {
6770 : /* If it has not been seen as a dummy, this is an error. */
6771 462 : if (!entry_dummy_seen_p (sym))
6772 : {
6773 5 : if (specification_expr
6774 4 : && specification_expr_symbol
6775 4 : && specification_expr_symbol->attr.dummy
6776 2 : && specification_expr_symbol->ns == gfc_current_ns
6777 7 : && !entry_dummy_seen_p (specification_expr_symbol))
6778 : ;
6779 3 : else if (specification_expr)
6780 2 : gfc_error ("Variable %qs, used in a specification expression"
6781 : ", is referenced at %L before the ENTRY statement "
6782 : "in which it is a parameter",
6783 : sym->name, &cs_base->current->loc);
6784 : else
6785 1 : gfc_error ("Variable %qs is used at %L before the ENTRY "
6786 : "statement in which it is a parameter",
6787 : sym->name, &cs_base->current->loc);
6788 : t = false;
6789 : }
6790 : }
6791 :
6792 : /* Now do the same check on the specification expressions. */
6793 964 : saved_specification_expr = specification_expr;
6794 964 : saved_specification_expr_symbol = specification_expr_symbol;
6795 964 : specification_expr = true;
6796 964 : specification_expr_symbol = sym;
6797 964 : if (sym->ts.type == BT_CHARACTER
6798 964 : && !gfc_resolve_expr (sym->ts.u.cl->length))
6799 : t = false;
6800 :
6801 964 : if (sym->as)
6802 : {
6803 279 : for (n = 0; n < sym->as->rank; n++)
6804 : {
6805 164 : if (!gfc_resolve_expr (sym->as->lower[n]))
6806 0 : t = false;
6807 164 : if (!gfc_resolve_expr (sym->as->upper[n]))
6808 1 : t = false;
6809 : }
6810 : }
6811 964 : specification_expr = saved_specification_expr;
6812 964 : specification_expr_symbol = saved_specification_expr_symbol;
6813 :
6814 964 : if (t)
6815 : /* Update the symbol's entry level. */
6816 957 : sym->entry_id = current_entry_id + 1;
6817 : }
6818 :
6819 : /* If a symbol has been host_associated mark it. This is used latter,
6820 : to identify if aliasing is possible via host association. */
6821 1314845 : if (sym->attr.flavor == FL_VARIABLE
6822 1276717 : && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6823 6172 : || !sym->ns->code->ext.block.assoc)
6824 1274609 : && gfc_current_ns->parent
6825 608652 : && (gfc_current_ns->parent == sym->ns
6826 570452 : || (gfc_current_ns->parent->parent
6827 11306 : && gfc_current_ns->parent->parent == sym->ns)))
6828 44830 : sym->attr.host_assoc = 1;
6829 :
6830 1314845 : if (gfc_current_ns->proc_name
6831 1310682 : && sym->attr.dimension
6832 357695 : && (sym->ns != gfc_current_ns
6833 333902 : || sym->attr.use_assoc
6834 329921 : || sym->attr.in_common))
6835 32562 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6836 :
6837 1327947 : resolve_procedure:
6838 1327947 : if (t && !resolve_procedure_expression (e))
6839 : t = false;
6840 :
6841 : /* F2008, C617 and C1229. */
6842 1326907 : if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6843 1426222 : && gfc_is_coindexed (e))
6844 : {
6845 359 : gfc_ref *ref, *ref2 = NULL;
6846 :
6847 442 : for (ref = e->ref; ref; ref = ref->next)
6848 : {
6849 442 : if (ref->type == REF_COMPONENT)
6850 83 : ref2 = ref;
6851 442 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6852 : break;
6853 : }
6854 :
6855 718 : for ( ; ref; ref = ref->next)
6856 371 : if (ref->type == REF_COMPONENT)
6857 : break;
6858 :
6859 : /* Expression itself is not coindexed object. */
6860 359 : if (ref && e->ts.type == BT_CLASS)
6861 : {
6862 3 : gfc_error ("Polymorphic subobject of coindexed object at %L",
6863 : &e->where);
6864 3 : t = false;
6865 : }
6866 :
6867 : /* Expression itself is coindexed object. */
6868 347 : if (ref == NULL)
6869 : {
6870 347 : gfc_component *c;
6871 347 : c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6872 467 : for ( ; c; c = c->next)
6873 120 : if (c->attr.allocatable && c->ts.type == BT_CLASS)
6874 : {
6875 0 : gfc_error ("Coindexed object with polymorphic allocatable "
6876 : "subcomponent at %L", &e->where);
6877 0 : t = false;
6878 0 : break;
6879 : }
6880 : }
6881 : }
6882 :
6883 1327947 : if (t)
6884 1327937 : gfc_expression_rank (e);
6885 :
6886 1327947 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6887 3 : gfc_warning (OPT_Wdeprecated_declarations,
6888 : "Using variable %qs at %L is deprecated",
6889 : sym->name, &e->where);
6890 : /* Simplify cases where access to a parameter array results in a
6891 : single constant. Suppress errors since those will have been
6892 : issued before, as warnings. */
6893 1327947 : if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6894 : {
6895 2727 : gfc_push_suppress_errors ();
6896 2727 : gfc_simplify_expr (e, 1);
6897 2727 : gfc_pop_suppress_errors ();
6898 : }
6899 :
6900 : return t;
6901 : }
6902 :
6903 :
6904 : /* 'sym' was initially guessed to be derived type but has been corrected
6905 : in resolve_assoc_var to be a class entity or the derived type correcting.
6906 : If a class entity it will certainly need the _data reference or the
6907 : reference derived type symbol correcting in the first component ref if
6908 : a derived type. */
6909 :
6910 : void
6911 916 : gfc_fixup_inferred_type_refs (gfc_expr *e)
6912 : {
6913 916 : gfc_ref *ref, *new_ref;
6914 916 : gfc_symbol *sym, *derived;
6915 916 : gfc_expr *target;
6916 916 : sym = e->symtree->n.sym;
6917 :
6918 : /* An associate_name whose selector is (i) a component ref of a selector
6919 : that is a inferred type associate_name; or (ii) an intrinsic type that
6920 : has been inferred from an inquiry ref. */
6921 916 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6922 : {
6923 318 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6924 318 : sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6925 318 : if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6926 : {
6927 60 : ref = e->ref;
6928 : /* A substring misidentified as an array section. */
6929 60 : if (sym->ts.type == BT_CHARACTER
6930 30 : && ref->u.ar.start[0] && ref->u.ar.end[0]
6931 6 : && !ref->u.ar.stride[0])
6932 : {
6933 6 : new_ref = gfc_get_ref ();
6934 6 : new_ref->type = REF_SUBSTRING;
6935 6 : new_ref->u.ss.start = ref->u.ar.start[0];
6936 6 : new_ref->u.ss.end = ref->u.ar.end[0];
6937 6 : new_ref->u.ss.length = sym->ts.u.cl;
6938 6 : *ref = *new_ref;
6939 6 : free (new_ref);
6940 : }
6941 : else
6942 : {
6943 54 : if (e->ref->u.ar.type == AR_UNKNOWN)
6944 24 : gfc_error ("Invalid array reference at %L", &e->where);
6945 54 : e->ref = ref->next;
6946 54 : free (ref);
6947 : }
6948 : }
6949 :
6950 : /* It is possible for an inquiry reference to be mistaken for a
6951 : component reference. Correct this now. */
6952 318 : ref = e->ref;
6953 318 : if (ref && ref->type == REF_ARRAY)
6954 138 : ref = ref->next;
6955 186 : if (ref && ref->type == REF_COMPONENT
6956 150 : && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6957 : {
6958 12 : e->symtree->n.sym = sym;
6959 12 : *ref = *new_ref;
6960 12 : gfc_free_ref_list (new_ref);
6961 : }
6962 :
6963 : /* The kind of the associate name is best evaluated directly from the
6964 : selector because of the guesses made in primary.cc, when the type
6965 : is still unknown. */
6966 318 : if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6967 : {
6968 24 : gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6969 12 : sym->assoc->target->ts.kind);
6970 12 : gfc_replace_expr (e, ne);
6971 12 : }
6972 174 : else if (ref && ref->type == REF_INQUIRY
6973 150 : && (ref->u.i == INQUIRY_RE || ref->u.i == INQUIRY_IM)
6974 114 : && sym->ts.type == BT_COMPLEX
6975 114 : && e->ts.type == BT_REAL
6976 114 : && e->ts.kind != sym->ts.kind)
6977 : /* primary.cc set the inquiry-result kind to the default real kind
6978 : when the associate-name's type was inferred from %re/%im before
6979 : the target was resolved. Now use the (resolved) selector kind. */
6980 24 : e->ts.kind = sym->ts.kind;
6981 :
6982 : /* Now that the references are all sorted out, set the expression rank
6983 : and return. */
6984 318 : gfc_expression_rank (e);
6985 318 : return;
6986 : }
6987 :
6988 598 : derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6989 : : sym->ts.u.derived;
6990 :
6991 : /* Ensure that class symbols have an array spec and ensure that there
6992 : is a _data field reference following class type references. */
6993 598 : if (sym->ts.type == BT_CLASS
6994 196 : && sym->assoc->target->ts.type == BT_CLASS)
6995 : {
6996 196 : e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6997 196 : e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6998 196 : sym->attr.dimension = 0;
6999 196 : sym->attr.codimension = 0;
7000 196 : CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
7001 196 : CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
7002 196 : if (e->ref && (e->ref->type != REF_COMPONENT
7003 160 : || e->ref->u.c.component->name[0] != '_'))
7004 : {
7005 82 : ref = gfc_get_ref ();
7006 82 : ref->type = REF_COMPONENT;
7007 82 : ref->next = e->ref;
7008 82 : e->ref = ref;
7009 82 : ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
7010 : true, true, NULL);
7011 82 : ref->u.c.sym = sym->ts.u.derived;
7012 : }
7013 : }
7014 :
7015 : /* Proceed as far as the first component reference and ensure that the
7016 : correct derived type is being used. */
7017 861 : for (ref = e->ref; ref; ref = ref->next)
7018 825 : if (ref->type == REF_COMPONENT)
7019 : {
7020 562 : if (ref->u.c.component->name[0] != '_')
7021 366 : ref->u.c.sym = derived;
7022 : else
7023 196 : ref->u.c.sym = sym->ts.u.derived;
7024 : break;
7025 : }
7026 :
7027 : /* Verify that the type inferrence mechanism has not introduced a spurious
7028 : array reference. This can happen with an associate name, whose selector
7029 : is an element of another inferred type. */
7030 598 : target = e->symtree->n.sym->assoc->target;
7031 598 : if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
7032 186 : && e != target && !target->rank)
7033 : {
7034 : /* First case: array ref after the scalar class or derived
7035 : associate_name. */
7036 186 : if (e->ref && e->ref->type == REF_ARRAY
7037 7 : && e->ref->u.ar.type != AR_ELEMENT)
7038 : {
7039 7 : ref = e->ref;
7040 7 : if (ref->u.ar.type == AR_UNKNOWN)
7041 1 : gfc_error ("Invalid array reference at %L", &e->where);
7042 7 : e->ref = ref->next;
7043 7 : free (ref);
7044 :
7045 : /* If it hasn't a ref to the '_data' field supply one. */
7046 7 : if (sym->ts.type == BT_CLASS
7047 0 : && !(e->ref->type == REF_COMPONENT
7048 0 : && strcmp (e->ref->u.c.component->name, "_data")))
7049 : {
7050 0 : gfc_ref *new_ref;
7051 0 : gfc_find_component (e->symtree->n.sym->ts.u.derived,
7052 : "_data", true, true, &new_ref);
7053 0 : new_ref->next = e->ref;
7054 0 : e->ref = new_ref;
7055 : }
7056 : }
7057 : /* 2nd case: a ref to the '_data' field followed by an array ref. */
7058 179 : else if (e->ref && e->ref->type == REF_COMPONENT
7059 179 : && strcmp (e->ref->u.c.component->name, "_data") == 0
7060 64 : && e->ref->next && e->ref->next->type == REF_ARRAY
7061 0 : && e->ref->next->u.ar.type != AR_ELEMENT)
7062 : {
7063 0 : ref = e->ref->next;
7064 0 : if (ref->u.ar.type == AR_UNKNOWN)
7065 0 : gfc_error ("Invalid array reference at %L", &e->where);
7066 0 : e->ref->next = e->ref->next->next;
7067 0 : free (ref);
7068 : }
7069 : }
7070 :
7071 : /* Now that all the references are OK, get the expression rank. */
7072 598 : gfc_expression_rank (e);
7073 : }
7074 :
7075 :
7076 : /* Checks to see that the correct symbol has been host associated.
7077 : The only situations where this arises are:
7078 : (i) That in which a twice contained function is parsed after
7079 : the host association is made. On detecting this, change
7080 : the symbol in the expression and convert the array reference
7081 : into an actual arglist if the old symbol is a variable; or
7082 : (ii) That in which an external function is typed but not declared
7083 : explicitly to be external. Here, the old symbol is changed
7084 : from a variable to an external function. */
7085 : static bool
7086 1673791 : check_host_association (gfc_expr *e)
7087 : {
7088 1673791 : gfc_symbol *sym, *old_sym;
7089 1673791 : gfc_symtree *st;
7090 1673791 : int n;
7091 1673791 : gfc_ref *ref;
7092 1673791 : gfc_actual_arglist *arg, *tail = NULL;
7093 1673791 : bool retval = e->expr_type == EXPR_FUNCTION;
7094 :
7095 : /* If the expression is the result of substitution in
7096 : interface.cc(gfc_extend_expr) because there is no way in
7097 : which the host association can be wrong. */
7098 1673791 : if (e->symtree == NULL
7099 1672992 : || e->symtree->n.sym == NULL
7100 1672992 : || e->user_operator)
7101 : return retval;
7102 :
7103 1671227 : old_sym = e->symtree->n.sym;
7104 :
7105 1671227 : if (gfc_current_ns->parent
7106 734213 : && old_sym->ns != gfc_current_ns)
7107 : {
7108 : /* Use the 'USE' name so that renamed module symbols are
7109 : correctly handled. */
7110 91458 : gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
7111 :
7112 91458 : if (sym && old_sym != sym
7113 695 : && sym->attr.flavor == FL_PROCEDURE
7114 105 : && sym->attr.contained)
7115 : {
7116 : /* Clear the shape, since it might not be valid. */
7117 83 : gfc_free_shape (&e->shape, e->rank);
7118 :
7119 : /* Give the expression the right symtree! */
7120 83 : gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
7121 83 : gcc_assert (st != NULL);
7122 :
7123 83 : if (old_sym->attr.flavor == FL_PROCEDURE
7124 59 : || e->expr_type == EXPR_FUNCTION)
7125 : {
7126 : /* Original was function so point to the new symbol, since
7127 : the actual argument list is already attached to the
7128 : expression. */
7129 30 : e->value.function.esym = NULL;
7130 30 : e->symtree = st;
7131 : }
7132 : else
7133 : {
7134 : /* Original was variable so convert array references into
7135 : an actual arglist. This does not need any checking now
7136 : since resolve_function will take care of it. */
7137 53 : e->value.function.actual = NULL;
7138 53 : e->expr_type = EXPR_FUNCTION;
7139 53 : e->symtree = st;
7140 :
7141 : /* Ambiguity will not arise if the array reference is not
7142 : the last reference. */
7143 55 : for (ref = e->ref; ref; ref = ref->next)
7144 38 : if (ref->type == REF_ARRAY && ref->next == NULL)
7145 : break;
7146 :
7147 53 : if ((ref == NULL || ref->type != REF_ARRAY)
7148 17 : && sym->attr.proc == PROC_INTERNAL)
7149 : {
7150 4 : gfc_error ("%qs at %L is host associated at %L into "
7151 : "a contained procedure with an internal "
7152 : "procedure of the same name", sym->name,
7153 : &old_sym->declared_at, &e->where);
7154 4 : return false;
7155 : }
7156 :
7157 13 : if (ref == NULL)
7158 : return false;
7159 :
7160 36 : gcc_assert (ref->type == REF_ARRAY);
7161 :
7162 : /* Grab the start expressions from the array ref and
7163 : copy them into actual arguments. */
7164 84 : for (n = 0; n < ref->u.ar.dimen; n++)
7165 : {
7166 48 : arg = gfc_get_actual_arglist ();
7167 48 : arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
7168 48 : if (e->value.function.actual == NULL)
7169 36 : tail = e->value.function.actual = arg;
7170 : else
7171 : {
7172 12 : tail->next = arg;
7173 12 : tail = arg;
7174 : }
7175 : }
7176 :
7177 : /* Dump the reference list and set the rank. */
7178 36 : gfc_free_ref_list (e->ref);
7179 36 : e->ref = NULL;
7180 36 : e->rank = sym->as ? sym->as->rank : 0;
7181 36 : e->corank = sym->as ? sym->as->corank : 0;
7182 : }
7183 :
7184 66 : gfc_resolve_expr (e);
7185 66 : sym->refs++;
7186 : }
7187 : /* This case corresponds to a call, from a block or a contained
7188 : procedure, to an external function, which has not been declared
7189 : as being external in the main program but has been typed. */
7190 91375 : else if (sym && old_sym != sym
7191 612 : && !e->ref
7192 340 : && sym->ts.type == BT_UNKNOWN
7193 21 : && old_sym->ts.type != BT_UNKNOWN
7194 19 : && sym->attr.flavor == FL_PROCEDURE
7195 19 : && old_sym->attr.flavor == FL_VARIABLE
7196 7 : && sym->ns->parent == old_sym->ns
7197 7 : && sym->ns->proc_name
7198 7 : && sym->ns->proc_name->attr.proc != PROC_MODULE
7199 6 : && (sym->ns->proc_name->attr.flavor == FL_LABEL
7200 6 : || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
7201 : {
7202 6 : old_sym->attr.flavor = FL_PROCEDURE;
7203 6 : old_sym->attr.external = 1;
7204 6 : old_sym->attr.function = 1;
7205 6 : old_sym->result = old_sym;
7206 6 : gfc_resolve_expr (e);
7207 : }
7208 : }
7209 : /* This might have changed! */
7210 1671210 : return e->expr_type == EXPR_FUNCTION;
7211 : }
7212 :
7213 :
7214 : static void
7215 1442 : gfc_resolve_character_operator (gfc_expr *e)
7216 : {
7217 1442 : gfc_expr *op1 = e->value.op.op1;
7218 1442 : gfc_expr *op2 = e->value.op.op2;
7219 1442 : gfc_expr *e1 = NULL;
7220 1442 : gfc_expr *e2 = NULL;
7221 :
7222 1442 : gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
7223 :
7224 1442 : if (op1->ts.u.cl && op1->ts.u.cl->length)
7225 761 : e1 = gfc_copy_expr (op1->ts.u.cl->length);
7226 681 : else if (op1->expr_type == EXPR_CONSTANT)
7227 268 : e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7228 268 : op1->value.character.length);
7229 :
7230 1442 : if (op2->ts.u.cl && op2->ts.u.cl->length)
7231 749 : e2 = gfc_copy_expr (op2->ts.u.cl->length);
7232 693 : else if (op2->expr_type == EXPR_CONSTANT)
7233 462 : e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
7234 462 : op2->value.character.length);
7235 :
7236 1442 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7237 :
7238 1442 : if (!e1 || !e2)
7239 : {
7240 541 : gfc_free_expr (e1);
7241 541 : gfc_free_expr (e2);
7242 :
7243 541 : return;
7244 : }
7245 :
7246 901 : e->ts.u.cl->length = gfc_add (e1, e2);
7247 901 : e->ts.u.cl->length->ts.type = BT_INTEGER;
7248 901 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
7249 901 : gfc_simplify_expr (e->ts.u.cl->length, 0);
7250 901 : gfc_resolve_expr (e->ts.u.cl->length);
7251 :
7252 901 : return;
7253 : }
7254 :
7255 :
7256 : /* Ensure that an character expression has a charlen and, if possible, a
7257 : length expression. */
7258 :
7259 : static void
7260 183227 : fixup_charlen (gfc_expr *e)
7261 : {
7262 : /* The cases fall through so that changes in expression type and the need
7263 : for multiple fixes are picked up. In all circumstances, a charlen should
7264 : be available for the middle end to hang a backend_decl on. */
7265 183227 : switch (e->expr_type)
7266 : {
7267 1442 : case EXPR_OP:
7268 1442 : gfc_resolve_character_operator (e);
7269 : /* FALLTHRU */
7270 :
7271 1509 : case EXPR_ARRAY:
7272 1509 : if (e->expr_type == EXPR_ARRAY)
7273 67 : gfc_resolve_character_array_constructor (e);
7274 : /* FALLTHRU */
7275 :
7276 1966 : case EXPR_SUBSTRING:
7277 1966 : if (!e->ts.u.cl && e->ref)
7278 453 : gfc_resolve_substring_charlen (e);
7279 : /* FALLTHRU */
7280 :
7281 183227 : default:
7282 183227 : if (!e->ts.u.cl)
7283 181265 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7284 :
7285 183227 : break;
7286 : }
7287 183227 : }
7288 :
7289 :
7290 : /* Update an actual argument to include the passed-object for type-bound
7291 : procedures at the right position. */
7292 :
7293 : static gfc_actual_arglist*
7294 2968 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
7295 : const char *name)
7296 : {
7297 2992 : gcc_assert (argpos > 0);
7298 :
7299 2992 : if (argpos == 1)
7300 : {
7301 2843 : gfc_actual_arglist* result;
7302 :
7303 2843 : result = gfc_get_actual_arglist ();
7304 2843 : result->expr = po;
7305 2843 : result->next = lst;
7306 2843 : if (name)
7307 514 : result->name = name;
7308 :
7309 2843 : return result;
7310 : }
7311 :
7312 149 : if (lst)
7313 125 : lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
7314 : else
7315 24 : lst = update_arglist_pass (NULL, po, argpos - 1, name);
7316 : return lst;
7317 : }
7318 :
7319 :
7320 : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
7321 :
7322 : static gfc_expr*
7323 7214 : extract_compcall_passed_object (gfc_expr* e)
7324 : {
7325 7214 : gfc_expr* po;
7326 :
7327 7214 : if (e->expr_type == EXPR_UNKNOWN)
7328 : {
7329 0 : gfc_error ("Error in typebound call at %L",
7330 : &e->where);
7331 0 : return NULL;
7332 : }
7333 :
7334 7214 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7335 :
7336 7214 : if (e->value.compcall.base_object)
7337 1584 : po = gfc_copy_expr (e->value.compcall.base_object);
7338 : else
7339 : {
7340 5630 : po = gfc_get_expr ();
7341 5630 : po->expr_type = EXPR_VARIABLE;
7342 5630 : po->symtree = e->symtree;
7343 5630 : po->ref = gfc_copy_ref (e->ref);
7344 5630 : po->where = e->where;
7345 : }
7346 :
7347 7214 : if (!gfc_resolve_expr (po))
7348 : return NULL;
7349 :
7350 : return po;
7351 : }
7352 :
7353 :
7354 : /* Update the arglist of an EXPR_COMPCALL expression to include the
7355 : passed-object. */
7356 :
7357 : static bool
7358 3327 : update_compcall_arglist (gfc_expr* e)
7359 : {
7360 3327 : gfc_expr* po;
7361 3327 : gfc_typebound_proc* tbp;
7362 :
7363 3327 : tbp = e->value.compcall.tbp;
7364 :
7365 3327 : if (tbp->error)
7366 : return false;
7367 :
7368 3326 : po = extract_compcall_passed_object (e);
7369 3326 : if (!po)
7370 : return false;
7371 :
7372 3326 : if (tbp->nopass || e->value.compcall.ignore_pass)
7373 : {
7374 1116 : gfc_free_expr (po);
7375 1116 : return true;
7376 : }
7377 :
7378 2210 : if (tbp->pass_arg_num <= 0)
7379 : return false;
7380 :
7381 2209 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7382 : tbp->pass_arg_num,
7383 : tbp->pass_arg);
7384 :
7385 2209 : return true;
7386 : }
7387 :
7388 :
7389 : /* Extract the passed object from a PPC call (a copy of it). */
7390 :
7391 : static gfc_expr*
7392 85 : extract_ppc_passed_object (gfc_expr *e)
7393 : {
7394 85 : gfc_expr *po;
7395 85 : gfc_ref **ref;
7396 :
7397 85 : po = gfc_get_expr ();
7398 85 : po->expr_type = EXPR_VARIABLE;
7399 85 : po->symtree = e->symtree;
7400 85 : po->ref = gfc_copy_ref (e->ref);
7401 85 : po->where = e->where;
7402 :
7403 : /* Remove PPC reference. */
7404 85 : ref = &po->ref;
7405 91 : while ((*ref)->next)
7406 6 : ref = &(*ref)->next;
7407 85 : gfc_free_ref_list (*ref);
7408 85 : *ref = NULL;
7409 :
7410 85 : if (!gfc_resolve_expr (po))
7411 0 : return NULL;
7412 :
7413 : return po;
7414 : }
7415 :
7416 :
7417 : /* Update the actual arglist of a procedure pointer component to include the
7418 : passed-object. */
7419 :
7420 : static bool
7421 574 : update_ppc_arglist (gfc_expr* e)
7422 : {
7423 574 : gfc_expr* po;
7424 574 : gfc_component *ppc;
7425 574 : gfc_typebound_proc* tb;
7426 :
7427 574 : ppc = gfc_get_proc_ptr_comp (e);
7428 574 : if (!ppc)
7429 : return false;
7430 :
7431 574 : tb = ppc->tb;
7432 :
7433 574 : if (tb->error)
7434 : return false;
7435 572 : else if (tb->nopass)
7436 : return true;
7437 :
7438 85 : po = extract_ppc_passed_object (e);
7439 85 : if (!po)
7440 : return false;
7441 :
7442 : /* F08:R739. */
7443 85 : if (po->rank != 0)
7444 : {
7445 0 : gfc_error ("Passed-object at %L must be scalar", &e->where);
7446 0 : return false;
7447 : }
7448 :
7449 : /* F08:C611. */
7450 85 : if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7451 : {
7452 1 : gfc_error ("Base object for procedure-pointer component call at %L is of"
7453 : " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7454 1 : return false;
7455 : }
7456 :
7457 84 : gcc_assert (tb->pass_arg_num > 0);
7458 84 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7459 : tb->pass_arg_num,
7460 : tb->pass_arg);
7461 :
7462 84 : return true;
7463 : }
7464 :
7465 :
7466 : /* Check that the object a TBP is called on is valid, i.e. it must not be
7467 : of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7468 :
7469 : static bool
7470 3338 : check_typebound_baseobject (gfc_expr* e)
7471 : {
7472 3338 : gfc_expr* base;
7473 3338 : bool return_value = false;
7474 :
7475 3338 : base = extract_compcall_passed_object (e);
7476 3338 : if (!base)
7477 : return false;
7478 :
7479 3335 : if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7480 : {
7481 1 : gfc_error ("Error in typebound call at %L", &e->where);
7482 1 : goto cleanup;
7483 : }
7484 :
7485 3334 : if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7486 1 : return false;
7487 :
7488 : /* F08:C611. */
7489 3333 : if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7490 : {
7491 3 : gfc_error ("Base object for type-bound procedure call at %L is of"
7492 : " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7493 3 : goto cleanup;
7494 : }
7495 :
7496 : /* F08:C1230. If the procedure called is NOPASS,
7497 : the base object must be scalar. */
7498 3330 : if (e->value.compcall.tbp->nopass && base->rank != 0)
7499 : {
7500 1 : gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7501 : " be scalar", &e->where);
7502 1 : goto cleanup;
7503 : }
7504 :
7505 : return_value = true;
7506 :
7507 3334 : cleanup:
7508 3334 : gfc_free_expr (base);
7509 3334 : return return_value;
7510 : }
7511 :
7512 :
7513 : /* Resolve a call to a type-bound procedure, either function or subroutine,
7514 : statically from the data in an EXPR_COMPCALL expression. The adapted
7515 : arglist and the target-procedure symtree are returned. */
7516 :
7517 : static bool
7518 3327 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7519 : gfc_actual_arglist** actual)
7520 : {
7521 3327 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7522 3327 : gcc_assert (!e->value.compcall.tbp->is_generic);
7523 :
7524 : /* Update the actual arglist for PASS. */
7525 3327 : if (!update_compcall_arglist (e))
7526 : return false;
7527 :
7528 3325 : *actual = e->value.compcall.actual;
7529 3325 : *target = e->value.compcall.tbp->u.specific;
7530 :
7531 3325 : gfc_free_ref_list (e->ref);
7532 3325 : e->ref = NULL;
7533 3325 : e->value.compcall.actual = NULL;
7534 :
7535 : /* If we find a deferred typebound procedure, check for derived types
7536 : that an overriding typebound procedure has not been missed. */
7537 3325 : if (e->value.compcall.name
7538 3325 : && !e->value.compcall.tbp->non_overridable
7539 3307 : && e->value.compcall.base_object
7540 792 : && e->value.compcall.base_object->ts.type == BT_DERIVED)
7541 : {
7542 499 : gfc_symtree *st;
7543 499 : gfc_symbol *derived;
7544 :
7545 : /* Use the derived type of the base_object. */
7546 499 : derived = e->value.compcall.base_object->ts.u.derived;
7547 499 : st = NULL;
7548 :
7549 : /* If necessary, go through the inheritance chain. */
7550 1505 : while (!st && derived)
7551 : {
7552 : /* Look for the typebound procedure 'name'. */
7553 507 : if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7554 499 : st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7555 : e->value.compcall.name);
7556 507 : if (!st)
7557 8 : derived = gfc_get_derived_super_type (derived);
7558 : }
7559 :
7560 : /* Now find the specific name in the derived type namespace. */
7561 499 : if (st && st->n.tb && st->n.tb->u.specific)
7562 499 : gfc_find_sym_tree (st->n.tb->u.specific->name,
7563 499 : derived->ns, 1, &st);
7564 499 : if (st)
7565 499 : *target = st;
7566 : }
7567 :
7568 3325 : if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7569 3325 : && !e->value.compcall.tbp->deferred)
7570 1 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7571 : " itself recursively. Declare it RECURSIVE or use"
7572 : " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7573 :
7574 : return true;
7575 : }
7576 :
7577 :
7578 : /* Get the ultimate declared type from an expression. In addition,
7579 : return the last class/derived type reference and the copy of the
7580 : reference list. If check_types is set true, derived types are
7581 : identified as well as class references. */
7582 : static gfc_symbol*
7583 3269 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7584 : gfc_expr *e, bool check_types)
7585 : {
7586 3269 : gfc_symbol *declared;
7587 3269 : gfc_ref *ref;
7588 :
7589 3269 : declared = NULL;
7590 3269 : if (class_ref)
7591 2861 : *class_ref = NULL;
7592 3269 : if (new_ref)
7593 2568 : *new_ref = gfc_copy_ref (e->ref);
7594 :
7595 4064 : for (ref = e->ref; ref; ref = ref->next)
7596 : {
7597 795 : if (ref->type != REF_COMPONENT)
7598 292 : continue;
7599 :
7600 503 : if ((ref->u.c.component->ts.type == BT_CLASS
7601 256 : || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7602 428 : && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7603 : {
7604 354 : declared = ref->u.c.component->ts.u.derived;
7605 354 : if (class_ref)
7606 332 : *class_ref = ref;
7607 : }
7608 : }
7609 :
7610 3269 : if (declared == NULL)
7611 2941 : declared = e->symtree->n.sym->ts.u.derived;
7612 :
7613 3269 : return declared;
7614 : }
7615 :
7616 :
7617 : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7618 : which of the specific bindings (if any) matches the arglist and transform
7619 : the expression into a call of that binding. */
7620 :
7621 : static bool
7622 3329 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
7623 : {
7624 3329 : gfc_typebound_proc* genproc;
7625 3329 : const char* genname;
7626 3329 : gfc_symtree *st;
7627 3329 : gfc_symbol *derived;
7628 :
7629 3329 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7630 3329 : genname = e->value.compcall.name;
7631 3329 : genproc = e->value.compcall.tbp;
7632 :
7633 3329 : if (!genproc->is_generic)
7634 : return true;
7635 :
7636 : /* Try the bindings on this type and in the inheritance hierarchy. */
7637 420 : for (; genproc; genproc = genproc->overridden)
7638 : {
7639 418 : gfc_tbp_generic* g;
7640 :
7641 418 : gcc_assert (genproc->is_generic);
7642 646 : for (g = genproc->u.generic; g; g = g->next)
7643 : {
7644 636 : gfc_symbol* target;
7645 636 : gfc_actual_arglist* args;
7646 636 : bool matches;
7647 :
7648 636 : gcc_assert (g->specific);
7649 :
7650 636 : if (g->specific->error)
7651 0 : continue;
7652 :
7653 636 : target = g->specific->u.specific->n.sym;
7654 :
7655 : /* Get the right arglist by handling PASS/NOPASS. */
7656 636 : args = gfc_copy_actual_arglist (e->value.compcall.actual);
7657 636 : if (!g->specific->nopass)
7658 : {
7659 550 : gfc_expr* po;
7660 550 : po = extract_compcall_passed_object (e);
7661 550 : if (!po)
7662 : {
7663 0 : gfc_free_actual_arglist (args);
7664 0 : return false;
7665 : }
7666 :
7667 550 : gcc_assert (g->specific->pass_arg_num > 0);
7668 550 : gcc_assert (!g->specific->error);
7669 550 : args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7670 : g->specific->pass_arg);
7671 : }
7672 636 : resolve_actual_arglist (args, target->attr.proc,
7673 636 : is_external_proc (target)
7674 636 : && gfc_sym_get_dummy_args (target) == NULL);
7675 :
7676 : /* Check if this arglist matches the formal. */
7677 636 : matches = gfc_arglist_matches_symbol (&args, target);
7678 :
7679 : /* Clean up and break out of the loop if we've found it. */
7680 636 : gfc_free_actual_arglist (args);
7681 636 : if (matches)
7682 : {
7683 408 : e->value.compcall.tbp = g->specific;
7684 408 : genname = g->specific_st->name;
7685 : /* Pass along the name for CLASS methods, where the vtab
7686 : procedure pointer component has to be referenced. */
7687 408 : if (name)
7688 161 : *name = genname;
7689 408 : goto success;
7690 : }
7691 : }
7692 : }
7693 :
7694 : /* Nothing matching found! */
7695 2 : gfc_error ("Found no matching specific binding for the call to the GENERIC"
7696 : " %qs at %L", genname, &e->where);
7697 2 : return false;
7698 :
7699 408 : success:
7700 : /* Make sure that we have the right specific instance for the name. */
7701 408 : derived = get_declared_from_expr (NULL, NULL, e, true);
7702 :
7703 408 : st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7704 408 : if (st)
7705 408 : e->value.compcall.tbp = st->n.tb;
7706 :
7707 : return true;
7708 : }
7709 :
7710 :
7711 : /* Resolve a call to a type-bound subroutine. */
7712 :
7713 : static bool
7714 1730 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7715 : {
7716 1730 : gfc_actual_arglist* newactual;
7717 1730 : gfc_symtree* target;
7718 :
7719 : /* Check that's really a SUBROUTINE. */
7720 1730 : if (!c->expr1->value.compcall.tbp->subroutine)
7721 : {
7722 17 : if (!c->expr1->value.compcall.tbp->is_generic
7723 15 : && c->expr1->value.compcall.tbp->u.specific
7724 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym
7725 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7726 12 : c->expr1->value.compcall.tbp->subroutine = 1;
7727 : else
7728 : {
7729 5 : gfc_error ("%qs at %L should be a SUBROUTINE",
7730 : c->expr1->value.compcall.name, &c->loc);
7731 5 : return false;
7732 : }
7733 : }
7734 :
7735 1725 : if (!check_typebound_baseobject (c->expr1))
7736 : return false;
7737 :
7738 : /* Pass along the name for CLASS methods, where the vtab
7739 : procedure pointer component has to be referenced. */
7740 1718 : if (name)
7741 480 : *name = c->expr1->value.compcall.name;
7742 :
7743 1718 : if (!resolve_typebound_generic_call (c->expr1, name))
7744 : return false;
7745 :
7746 : /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7747 1717 : if (overridable)
7748 371 : *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7749 :
7750 : /* Transform into an ordinary EXEC_CALL for now. */
7751 :
7752 1717 : if (!resolve_typebound_static (c->expr1, &target, &newactual))
7753 : return false;
7754 :
7755 1715 : c->ext.actual = newactual;
7756 1715 : c->symtree = target;
7757 1715 : c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7758 :
7759 1715 : gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7760 :
7761 1715 : gfc_free_expr (c->expr1);
7762 1715 : c->expr1 = gfc_get_expr ();
7763 1715 : c->expr1->expr_type = EXPR_FUNCTION;
7764 1715 : c->expr1->symtree = target;
7765 1715 : c->expr1->where = c->loc;
7766 :
7767 1715 : return resolve_call (c);
7768 : }
7769 :
7770 :
7771 : /* Resolve a component-call expression. */
7772 : static bool
7773 1632 : resolve_compcall (gfc_expr* e, const char **name)
7774 : {
7775 1632 : gfc_actual_arglist* newactual;
7776 1632 : gfc_symtree* target;
7777 :
7778 : /* Check that's really a FUNCTION. */
7779 1632 : if (!e->value.compcall.tbp->function)
7780 : {
7781 19 : if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7782 5 : gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7783 : &e->where);
7784 19 : return false;
7785 : }
7786 :
7787 :
7788 : /* These must not be assign-calls! */
7789 1613 : gcc_assert (!e->value.compcall.assign);
7790 :
7791 1613 : if (!check_typebound_baseobject (e))
7792 : return false;
7793 :
7794 : /* Pass along the name for CLASS methods, where the vtab
7795 : procedure pointer component has to be referenced. */
7796 1611 : if (name)
7797 864 : *name = e->value.compcall.name;
7798 :
7799 1611 : if (!resolve_typebound_generic_call (e, name))
7800 : return false;
7801 1610 : gcc_assert (!e->value.compcall.tbp->is_generic);
7802 :
7803 : /* Take the rank from the function's symbol. */
7804 1610 : if (e->value.compcall.tbp->u.specific->n.sym->as)
7805 : {
7806 155 : e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7807 155 : e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7808 : }
7809 :
7810 : /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7811 : arglist to the TBP's binding target. */
7812 :
7813 1610 : if (!resolve_typebound_static (e, &target, &newactual))
7814 : return false;
7815 :
7816 1610 : e->value.function.actual = newactual;
7817 1610 : e->value.function.name = NULL;
7818 1610 : e->value.function.esym = target->n.sym;
7819 1610 : e->value.function.isym = NULL;
7820 1610 : e->symtree = target;
7821 1610 : e->ts = target->n.sym->ts;
7822 1610 : e->expr_type = EXPR_FUNCTION;
7823 :
7824 : /* Resolution is not necessary if this is a class subroutine; this
7825 : function only has to identify the specific proc. Resolution of
7826 : the call will be done next in resolve_typebound_call. */
7827 1610 : return gfc_resolve_expr (e);
7828 : }
7829 :
7830 :
7831 : static bool resolve_fl_derived (gfc_symbol *sym);
7832 :
7833 :
7834 : /* Resolve a typebound function, or 'method'. First separate all
7835 : the non-CLASS references by calling resolve_compcall directly. */
7836 :
7837 : static bool
7838 1632 : resolve_typebound_function (gfc_expr* e)
7839 : {
7840 1632 : gfc_symbol *declared;
7841 1632 : gfc_component *c;
7842 1632 : gfc_ref *new_ref;
7843 1632 : gfc_ref *class_ref;
7844 1632 : gfc_symtree *st;
7845 1632 : const char *name;
7846 1632 : gfc_typespec ts;
7847 1632 : gfc_expr *expr;
7848 1632 : bool overridable;
7849 :
7850 1632 : st = e->symtree;
7851 :
7852 : /* Deal with typebound operators for CLASS objects. */
7853 1632 : expr = e->value.compcall.base_object;
7854 1632 : overridable = !e->value.compcall.tbp->non_overridable;
7855 1632 : if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7856 : {
7857 : /* Since the typebound operators are generic, we have to ensure
7858 : that any delays in resolution are corrected and that the vtab
7859 : is present. */
7860 184 : ts = expr->ts;
7861 184 : declared = ts.u.derived;
7862 184 : if (!resolve_fl_derived (declared))
7863 : return false;
7864 :
7865 184 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7866 184 : if (c->ts.u.derived == NULL)
7867 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7868 :
7869 184 : if (!resolve_compcall (e, &name))
7870 : return false;
7871 :
7872 : /* Use the generic name if it is there. */
7873 184 : name = name ? name : e->value.function.esym->name;
7874 184 : e->symtree = expr->symtree;
7875 184 : e->ref = gfc_copy_ref (expr->ref);
7876 184 : get_declared_from_expr (&class_ref, NULL, e, false);
7877 :
7878 : /* Trim away the extraneous references that emerge from nested
7879 : use of interface.cc (extend_expr). */
7880 184 : if (class_ref && class_ref->next)
7881 : {
7882 0 : gfc_free_ref_list (class_ref->next);
7883 0 : class_ref->next = NULL;
7884 : }
7885 184 : else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7886 : {
7887 0 : gfc_free_ref_list (e->ref);
7888 0 : e->ref = NULL;
7889 : }
7890 :
7891 184 : gfc_add_vptr_component (e);
7892 184 : gfc_add_component_ref (e, name);
7893 184 : e->value.function.esym = NULL;
7894 184 : if (expr->expr_type != EXPR_VARIABLE)
7895 80 : e->base_expr = expr;
7896 184 : return true;
7897 : }
7898 :
7899 1448 : if (st == NULL)
7900 159 : return resolve_compcall (e, NULL);
7901 :
7902 1289 : if (!gfc_resolve_ref (e))
7903 : return false;
7904 :
7905 : /* It can happen that a generic, typebound procedure is marked as overridable
7906 : with all of the specific procedures being non-overridable. If this is the
7907 : case, it is safe to resolve the compcall. */
7908 1289 : if (!expr && overridable
7909 1281 : && e->value.compcall.tbp->is_generic
7910 186 : && e->value.compcall.tbp->u.generic->specific
7911 185 : && e->value.compcall.tbp->u.generic->specific->non_overridable)
7912 : {
7913 : gfc_tbp_generic *g = e->value.compcall.tbp->u.generic;
7914 6 : for (; g; g = g->next)
7915 4 : if (!g->specific->non_overridable)
7916 : break;
7917 2 : if (g == NULL && resolve_compcall (e, &name))
7918 : return true;
7919 : }
7920 :
7921 : /* Get the CLASS declared type. */
7922 1287 : declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7923 :
7924 1287 : if (!resolve_fl_derived (declared))
7925 : return false;
7926 :
7927 : /* Weed out cases of the ultimate component being a derived type. */
7928 1287 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7929 1193 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7930 : {
7931 595 : gfc_free_ref_list (new_ref);
7932 595 : return resolve_compcall (e, NULL);
7933 : }
7934 :
7935 692 : c = gfc_find_component (declared, "_data", true, true, NULL);
7936 :
7937 : /* Treat the call as if it is a typebound procedure, in order to roll
7938 : out the correct name for the specific function. */
7939 692 : if (!resolve_compcall (e, &name))
7940 : {
7941 15 : gfc_free_ref_list (new_ref);
7942 15 : return false;
7943 : }
7944 677 : ts = e->ts;
7945 :
7946 677 : if (overridable)
7947 : {
7948 : /* Convert the expression to a procedure pointer component call. */
7949 675 : e->value.function.esym = NULL;
7950 675 : e->symtree = st;
7951 :
7952 675 : if (new_ref)
7953 125 : e->ref = new_ref;
7954 :
7955 : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7956 675 : gfc_add_vptr_component (e);
7957 675 : gfc_add_component_ref (e, name);
7958 :
7959 : /* Recover the typespec for the expression. This is really only
7960 : necessary for generic procedures, where the additional call
7961 : to gfc_add_component_ref seems to throw the collection of the
7962 : correct typespec. */
7963 675 : e->ts = ts;
7964 : }
7965 2 : else if (new_ref)
7966 0 : gfc_free_ref_list (new_ref);
7967 :
7968 : return true;
7969 : }
7970 :
7971 : /* Resolve a typebound subroutine, or 'method'. First separate all
7972 : the non-CLASS references by calling resolve_typebound_call
7973 : directly. */
7974 :
7975 : static bool
7976 1730 : resolve_typebound_subroutine (gfc_code *code)
7977 : {
7978 1730 : gfc_symbol *declared;
7979 1730 : gfc_component *c;
7980 1730 : gfc_ref *new_ref;
7981 1730 : gfc_ref *class_ref;
7982 1730 : gfc_symtree *st;
7983 1730 : const char *name;
7984 1730 : gfc_typespec ts;
7985 1730 : gfc_expr *expr;
7986 1730 : bool overridable;
7987 :
7988 1730 : st = code->expr1->symtree;
7989 :
7990 : /* Deal with typebound operators for CLASS objects. */
7991 1730 : expr = code->expr1->value.compcall.base_object;
7992 1730 : overridable = !code->expr1->value.compcall.tbp->non_overridable;
7993 1730 : if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7994 : {
7995 : /* If the base_object is not a variable, the corresponding actual
7996 : argument expression must be stored in e->base_expression so
7997 : that the corresponding tree temporary can be used as the base
7998 : object in gfc_conv_procedure_call. */
7999 109 : if (expr->expr_type != EXPR_VARIABLE)
8000 : {
8001 : gfc_actual_arglist *args;
8002 :
8003 : args= code->expr1->value.function.actual;
8004 : for (; args; args = args->next)
8005 : if (expr == args->expr)
8006 : expr = args->expr;
8007 : }
8008 :
8009 : /* Since the typebound operators are generic, we have to ensure
8010 : that any delays in resolution are corrected and that the vtab
8011 : is present. */
8012 109 : declared = expr->ts.u.derived;
8013 109 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
8014 109 : if (c->ts.u.derived == NULL)
8015 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
8016 :
8017 109 : if (!resolve_typebound_call (code, &name, NULL))
8018 : return false;
8019 :
8020 : /* Use the generic name if it is there. */
8021 109 : name = name ? name : code->expr1->value.function.esym->name;
8022 109 : code->expr1->symtree = expr->symtree;
8023 109 : code->expr1->ref = gfc_copy_ref (expr->ref);
8024 :
8025 : /* Trim away the extraneous references that emerge from nested
8026 : use of interface.cc (extend_expr). */
8027 109 : get_declared_from_expr (&class_ref, NULL, code->expr1, false);
8028 109 : if (class_ref && class_ref->next)
8029 : {
8030 0 : gfc_free_ref_list (class_ref->next);
8031 0 : class_ref->next = NULL;
8032 : }
8033 109 : else if (code->expr1->ref && !class_ref)
8034 : {
8035 18 : gfc_free_ref_list (code->expr1->ref);
8036 18 : code->expr1->ref = NULL;
8037 : }
8038 :
8039 : /* Now use the procedure in the vtable. */
8040 109 : gfc_add_vptr_component (code->expr1);
8041 109 : gfc_add_component_ref (code->expr1, name);
8042 109 : code->expr1->value.function.esym = NULL;
8043 109 : if (expr->expr_type != EXPR_VARIABLE)
8044 0 : code->expr1->base_expr = expr;
8045 109 : return true;
8046 : }
8047 :
8048 1621 : if (st == NULL)
8049 340 : return resolve_typebound_call (code, NULL, NULL);
8050 :
8051 1281 : if (!gfc_resolve_ref (code->expr1))
8052 : return false;
8053 :
8054 : /* Get the CLASS declared type. */
8055 1281 : get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
8056 :
8057 : /* Weed out cases of the ultimate component being a derived type. */
8058 1281 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
8059 1216 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
8060 : {
8061 905 : gfc_free_ref_list (new_ref);
8062 905 : return resolve_typebound_call (code, NULL, NULL);
8063 : }
8064 :
8065 376 : if (!resolve_typebound_call (code, &name, &overridable))
8066 : {
8067 5 : gfc_free_ref_list (new_ref);
8068 5 : return false;
8069 : }
8070 371 : ts = code->expr1->ts;
8071 :
8072 371 : if (overridable)
8073 : {
8074 : /* Convert the expression to a procedure pointer component call. */
8075 369 : code->expr1->value.function.esym = NULL;
8076 369 : code->expr1->symtree = st;
8077 :
8078 369 : if (new_ref)
8079 93 : code->expr1->ref = new_ref;
8080 :
8081 : /* '_vptr' points to the vtab, which contains the procedure pointers. */
8082 369 : gfc_add_vptr_component (code->expr1);
8083 369 : gfc_add_component_ref (code->expr1, name);
8084 :
8085 : /* Recover the typespec for the expression. This is really only
8086 : necessary for generic procedures, where the additional call
8087 : to gfc_add_component_ref seems to throw the collection of the
8088 : correct typespec. */
8089 369 : code->expr1->ts = ts;
8090 : }
8091 2 : else if (new_ref)
8092 0 : gfc_free_ref_list (new_ref);
8093 :
8094 : return true;
8095 : }
8096 :
8097 :
8098 : /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
8099 :
8100 : static bool
8101 124 : resolve_ppc_call (gfc_code* c)
8102 : {
8103 124 : gfc_component *comp;
8104 :
8105 124 : comp = gfc_get_proc_ptr_comp (c->expr1);
8106 124 : gcc_assert (comp != NULL);
8107 :
8108 124 : c->resolved_sym = c->expr1->symtree->n.sym;
8109 124 : c->expr1->expr_type = EXPR_VARIABLE;
8110 :
8111 124 : if (!comp->attr.subroutine)
8112 1 : gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
8113 :
8114 124 : if (!gfc_resolve_ref (c->expr1))
8115 : return false;
8116 :
8117 124 : if (!update_ppc_arglist (c->expr1))
8118 : return false;
8119 :
8120 123 : c->ext.actual = c->expr1->value.compcall.actual;
8121 :
8122 123 : if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
8123 123 : !(comp->ts.interface
8124 93 : && comp->ts.interface->formal)))
8125 : return false;
8126 :
8127 123 : if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
8128 : return false;
8129 :
8130 122 : gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
8131 :
8132 122 : return true;
8133 : }
8134 :
8135 :
8136 : /* Resolve a Function Call to a Procedure Pointer Component (Function). */
8137 :
8138 : static bool
8139 450 : resolve_expr_ppc (gfc_expr* e)
8140 : {
8141 450 : gfc_component *comp;
8142 :
8143 450 : comp = gfc_get_proc_ptr_comp (e);
8144 450 : gcc_assert (comp != NULL);
8145 :
8146 : /* Convert to EXPR_FUNCTION. */
8147 450 : e->expr_type = EXPR_FUNCTION;
8148 450 : e->value.function.isym = NULL;
8149 450 : e->value.function.actual = e->value.compcall.actual;
8150 450 : e->ts = comp->ts;
8151 450 : if (comp->as != NULL)
8152 : {
8153 28 : e->rank = comp->as->rank;
8154 28 : e->corank = comp->as->corank;
8155 : }
8156 :
8157 450 : if (!comp->attr.function)
8158 3 : gfc_add_function (&comp->attr, comp->name, &e->where);
8159 :
8160 450 : if (!gfc_resolve_ref (e))
8161 : return false;
8162 :
8163 450 : if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
8164 450 : !(comp->ts.interface
8165 449 : && comp->ts.interface->formal)))
8166 : return false;
8167 :
8168 450 : if (!update_ppc_arglist (e))
8169 : return false;
8170 :
8171 448 : if (!check_pure_function(e))
8172 : return false;
8173 :
8174 447 : gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
8175 :
8176 447 : return true;
8177 : }
8178 :
8179 :
8180 : static bool
8181 11883 : gfc_is_expandable_expr (gfc_expr *e)
8182 : {
8183 11883 : gfc_constructor *con;
8184 :
8185 11883 : if (e->expr_type == EXPR_ARRAY)
8186 : {
8187 : /* Traverse the constructor looking for variables that are flavor
8188 : parameter. Parameters must be expanded since they are fully used at
8189 : compile time. */
8190 11883 : con = gfc_constructor_first (e->value.constructor);
8191 31385 : for (; con; con = gfc_constructor_next (con))
8192 : {
8193 13788 : if (con->expr->expr_type == EXPR_VARIABLE
8194 5397 : && con->expr->symtree
8195 5397 : && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
8196 5315 : || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
8197 : return true;
8198 8391 : if (con->expr->expr_type == EXPR_ARRAY
8199 8391 : && gfc_is_expandable_expr (con->expr))
8200 : return true;
8201 : }
8202 : }
8203 :
8204 : return false;
8205 : }
8206 :
8207 :
8208 : /* Sometimes variables in specification expressions of the result
8209 : of module procedures in submodules wind up not being the 'real'
8210 : dummy. Find this, if possible, in the namespace of the first
8211 : formal argument. */
8212 :
8213 : static void
8214 3453 : fixup_unique_dummy (gfc_expr *e)
8215 : {
8216 3453 : gfc_symtree *st = NULL;
8217 3453 : gfc_symbol *s = NULL;
8218 :
8219 3453 : if (e->symtree->n.sym->ns->proc_name
8220 3423 : && e->symtree->n.sym->ns->proc_name->formal)
8221 3423 : s = e->symtree->n.sym->ns->proc_name->formal->sym;
8222 :
8223 3423 : if (s != NULL)
8224 3423 : st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
8225 :
8226 3453 : if (st != NULL
8227 14 : && st->n.sym != NULL
8228 14 : && st->n.sym->attr.dummy)
8229 14 : e->symtree = st;
8230 3453 : }
8231 :
8232 :
8233 : /* Resolve an expression. That is, make sure that types of operands agree
8234 : with their operators, intrinsic operators are converted to function calls
8235 : for overloaded types and unresolved function references are resolved. */
8236 :
8237 : bool
8238 7173806 : gfc_resolve_expr (gfc_expr *e)
8239 : {
8240 7173806 : bool t;
8241 7173806 : bool inquiry_save, actual_arg_save, first_actual_arg_save;
8242 :
8243 7173806 : if (e == NULL || e->do_not_resolve_again)
8244 : return true;
8245 :
8246 : /* inquiry_argument only applies to variables. */
8247 5249697 : inquiry_save = inquiry_argument;
8248 5249697 : actual_arg_save = actual_arg;
8249 5249697 : first_actual_arg_save = first_actual_arg;
8250 :
8251 5249697 : if (e->expr_type != EXPR_VARIABLE)
8252 : {
8253 3920838 : inquiry_argument = false;
8254 3920838 : actual_arg = false;
8255 3920838 : first_actual_arg = false;
8256 : }
8257 1328859 : else if (e->symtree != NULL
8258 1328414 : && *e->symtree->name == '@'
8259 4160 : && e->symtree->n.sym->attr.dummy)
8260 : {
8261 : /* Deal with submodule specification expressions that are not
8262 : found to be referenced in module.cc(read_cleanup). */
8263 3453 : fixup_unique_dummy (e);
8264 : }
8265 :
8266 5249697 : switch (e->expr_type)
8267 : {
8268 533239 : case EXPR_OP:
8269 533239 : t = resolve_operator (e);
8270 533239 : break;
8271 :
8272 162 : case EXPR_CONDITIONAL:
8273 162 : t = resolve_conditional (e);
8274 162 : break;
8275 :
8276 1673791 : case EXPR_FUNCTION:
8277 1673791 : case EXPR_VARIABLE:
8278 :
8279 1673791 : if (check_host_association (e))
8280 344968 : t = resolve_function (e);
8281 : else
8282 1328823 : t = resolve_variable (e);
8283 :
8284 1673791 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
8285 7326 : && e->ref->type != REF_SUBSTRING)
8286 2162 : gfc_resolve_substring_charlen (e);
8287 :
8288 : break;
8289 :
8290 1632 : case EXPR_COMPCALL:
8291 1632 : t = resolve_typebound_function (e);
8292 1632 : break;
8293 :
8294 508 : case EXPR_SUBSTRING:
8295 508 : t = gfc_resolve_ref (e);
8296 508 : break;
8297 :
8298 : case EXPR_CONSTANT:
8299 : case EXPR_NULL:
8300 : t = true;
8301 : break;
8302 :
8303 450 : case EXPR_PPC:
8304 450 : t = resolve_expr_ppc (e);
8305 450 : break;
8306 :
8307 72345 : case EXPR_ARRAY:
8308 72345 : t = false;
8309 72345 : if (!gfc_resolve_ref (e))
8310 : break;
8311 :
8312 72345 : t = gfc_resolve_array_constructor (e);
8313 : /* Also try to expand a constructor. */
8314 72345 : if (t)
8315 : {
8316 72243 : gfc_expression_rank (e);
8317 72243 : if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
8318 67691 : gfc_expand_constructor (e, false);
8319 : }
8320 :
8321 : /* This provides the opportunity for the length of constructors with
8322 : character valued function elements to propagate the string length
8323 : to the expression. */
8324 72243 : if (t && e->ts.type == BT_CHARACTER)
8325 : {
8326 : /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
8327 : here rather then add a duplicate test for it above. */
8328 10762 : gfc_expand_constructor (e, false);
8329 10762 : t = gfc_resolve_character_array_constructor (e);
8330 : }
8331 :
8332 : break;
8333 :
8334 16590 : case EXPR_STRUCTURE:
8335 16590 : t = gfc_resolve_ref (e);
8336 16590 : if (!t)
8337 : break;
8338 :
8339 16590 : t = resolve_structure_cons (e, 0);
8340 16590 : if (!t)
8341 : break;
8342 :
8343 16578 : t = gfc_simplify_expr (e, 0);
8344 16578 : break;
8345 :
8346 0 : default:
8347 0 : gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
8348 : }
8349 :
8350 5249697 : if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
8351 183227 : fixup_charlen (e);
8352 :
8353 5249697 : inquiry_argument = inquiry_save;
8354 5249697 : actual_arg = actual_arg_save;
8355 5249697 : first_actual_arg = first_actual_arg_save;
8356 :
8357 : /* For some reason, resolving these expressions a second time mangles
8358 : the typespec of the expression itself. */
8359 5249697 : if (t && e->expr_type == EXPR_VARIABLE
8360 1325980 : && e->symtree->n.sym->attr.select_rank_temporary
8361 3428 : && UNLIMITED_POLY (e->symtree->n.sym))
8362 83 : e->do_not_resolve_again = 1;
8363 :
8364 5247157 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
8365 7300 : t = check_import_status (e);
8366 :
8367 : return t;
8368 : }
8369 :
8370 :
8371 : /* Resolve an expression from an iterator. They must be scalar and have
8372 : INTEGER or (optionally) REAL type. */
8373 :
8374 : static bool
8375 152697 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
8376 : const char *name_msgid)
8377 : {
8378 152697 : if (!gfc_resolve_expr (expr))
8379 : return false;
8380 :
8381 152692 : if (expr->rank != 0)
8382 : {
8383 0 : gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
8384 0 : return false;
8385 : }
8386 :
8387 152692 : if (expr->ts.type != BT_INTEGER)
8388 : {
8389 274 : if (expr->ts.type == BT_REAL)
8390 : {
8391 274 : if (real_ok)
8392 271 : return gfc_notify_std (GFC_STD_F95_DEL,
8393 : "%s at %L must be integer",
8394 271 : _(name_msgid), &expr->where);
8395 : else
8396 : {
8397 3 : gfc_error ("%s at %L must be INTEGER", _(name_msgid),
8398 : &expr->where);
8399 3 : return false;
8400 : }
8401 : }
8402 : else
8403 : {
8404 0 : gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
8405 0 : return false;
8406 : }
8407 : }
8408 : return true;
8409 : }
8410 :
8411 :
8412 : /* Resolve the expressions in an iterator structure. If REAL_OK is
8413 : false allow only INTEGER type iterators, otherwise allow REAL types.
8414 : Set own_scope to true for ac-implied-do and data-implied-do as those
8415 : have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8416 :
8417 : bool
8418 38183 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8419 : {
8420 38183 : if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8421 : return false;
8422 :
8423 38179 : if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8424 38179 : _("iterator variable")))
8425 : return false;
8426 :
8427 38173 : if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8428 : "Start expression in DO loop"))
8429 : return false;
8430 :
8431 38172 : if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8432 : "End expression in DO loop"))
8433 : return false;
8434 :
8435 38169 : if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8436 : "Step expression in DO loop"))
8437 : return false;
8438 :
8439 : /* Convert start, end, and step to the same type as var. */
8440 38168 : if (iter->start->ts.kind != iter->var->ts.kind
8441 37888 : || iter->start->ts.type != iter->var->ts.type)
8442 315 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8443 :
8444 38168 : if (iter->end->ts.kind != iter->var->ts.kind
8445 37915 : || iter->end->ts.type != iter->var->ts.type)
8446 278 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8447 :
8448 38168 : if (iter->step->ts.kind != iter->var->ts.kind
8449 37924 : || iter->step->ts.type != iter->var->ts.type)
8450 280 : gfc_convert_type (iter->step, &iter->var->ts, 1);
8451 :
8452 38168 : if (iter->step->expr_type == EXPR_CONSTANT)
8453 : {
8454 37046 : if ((iter->step->ts.type == BT_INTEGER
8455 36963 : && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8456 74007 : || (iter->step->ts.type == BT_REAL
8457 83 : && mpfr_sgn (iter->step->value.real) == 0))
8458 : {
8459 3 : gfc_error ("Step expression in DO loop at %L cannot be zero",
8460 3 : &iter->step->where);
8461 3 : return false;
8462 : }
8463 : }
8464 :
8465 38165 : if (iter->start->expr_type == EXPR_CONSTANT
8466 35033 : && iter->end->expr_type == EXPR_CONSTANT
8467 27383 : && iter->step->expr_type == EXPR_CONSTANT)
8468 : {
8469 27116 : int sgn, cmp;
8470 27116 : if (iter->start->ts.type == BT_INTEGER)
8471 : {
8472 27062 : sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8473 27062 : cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8474 : }
8475 : else
8476 : {
8477 54 : sgn = mpfr_sgn (iter->step->value.real);
8478 54 : cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8479 : }
8480 27116 : if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8481 146 : gfc_warning (OPT_Wzerotrip,
8482 : "DO loop at %L will be executed zero times",
8483 146 : &iter->step->where);
8484 : }
8485 :
8486 38165 : if (iter->end->expr_type == EXPR_CONSTANT
8487 27751 : && iter->end->ts.type == BT_INTEGER
8488 27697 : && iter->step->expr_type == EXPR_CONSTANT
8489 27387 : && iter->step->ts.type == BT_INTEGER
8490 27387 : && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8491 27016 : || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8492 : {
8493 26230 : bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8494 26230 : int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8495 :
8496 26230 : if (is_step_positive
8497 25859 : && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8498 7 : gfc_warning (OPT_Wundefined_do_loop,
8499 : "DO loop at %L is undefined as it overflows",
8500 7 : &iter->step->where);
8501 : else if (!is_step_positive
8502 371 : && mpz_cmp (iter->end->value.integer,
8503 371 : gfc_integer_kinds[k].min_int) == 0)
8504 7 : gfc_warning (OPT_Wundefined_do_loop,
8505 : "DO loop at %L is undefined as it underflows",
8506 7 : &iter->step->where);
8507 : }
8508 :
8509 : return true;
8510 : }
8511 :
8512 :
8513 : /* Traversal function for find_forall_index. f == 2 signals that
8514 : that variable itself is not to be checked - only the references. */
8515 :
8516 : static bool
8517 42620 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8518 : {
8519 42620 : if (expr->expr_type != EXPR_VARIABLE)
8520 : return false;
8521 :
8522 : /* A scalar assignment */
8523 18188 : if (!expr->ref || *f == 1)
8524 : {
8525 12128 : if (expr->symtree->n.sym == sym)
8526 : return true;
8527 : else
8528 : return false;
8529 : }
8530 :
8531 6060 : if (*f == 2)
8532 1731 : *f = 1;
8533 : return false;
8534 : }
8535 :
8536 :
8537 : /* Check whether the FORALL index appears in the expression or not.
8538 : Returns true if SYM is found in EXPR. */
8539 :
8540 : bool
8541 27001 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8542 : {
8543 27001 : if (gfc_traverse_expr (expr, sym, forall_index, f))
8544 : return true;
8545 : else
8546 : return false;
8547 : }
8548 :
8549 : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8550 : This constraint specifies rules for variables in locality-specs. */
8551 :
8552 : static int
8553 717 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8554 : {
8555 717 : struct check_default_none_data *dt = (struct check_default_none_data *) data;
8556 :
8557 717 : if ((*expr)->expr_type == EXPR_VARIABLE)
8558 : {
8559 22 : gfc_symbol *sym = (*expr)->symtree->n.sym;
8560 22 : for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8561 24 : list; list = list->next)
8562 : {
8563 5 : if (list->expr->symtree->n.sym == sym)
8564 : {
8565 3 : gfc_error ("Variable %qs referenced in concurrent-header at %L "
8566 : "must not appear in LOCAL locality-spec at %L",
8567 : sym->name, &(*expr)->where, &list->expr->where);
8568 3 : *walk_subtrees = 0;
8569 3 : return 1;
8570 : }
8571 : }
8572 : }
8573 :
8574 714 : *walk_subtrees = 1;
8575 714 : return 0;
8576 : }
8577 :
8578 : static int
8579 3969 : check_default_none_expr (gfc_expr **e, int *, void *data)
8580 : {
8581 3969 : struct check_default_none_data *d = (struct check_default_none_data*) data;
8582 :
8583 3969 : if ((*e)->expr_type == EXPR_VARIABLE)
8584 : {
8585 1798 : gfc_symbol *sym = (*e)->symtree->n.sym;
8586 :
8587 1798 : if (d->sym_hash->contains (sym))
8588 1263 : sym->mark = 1;
8589 :
8590 535 : else if (d->default_none)
8591 : {
8592 6 : gfc_namespace *ns2 = d->ns;
8593 10 : while (ns2)
8594 : {
8595 6 : if (ns2 == sym->ns)
8596 : break;
8597 4 : ns2 = ns2->parent;
8598 : }
8599 :
8600 : /* A DO CONCURRENT iterator cannot appear in a locality spec. */
8601 6 : if (sym->ns->code->ext.concur.forall_iterator)
8602 : {
8603 : gfc_forall_iterator *iter
8604 : = sym->ns->code->ext.concur.forall_iterator;
8605 5 : for (; iter; iter = iter->next)
8606 3 : if (iter->var->symtree
8607 1 : && strcmp(sym->name, iter->var->symtree->name) == 0)
8608 1 : return 0;
8609 : }
8610 :
8611 : /* A named constant is not a variable, so skip test. */
8612 5 : if (ns2 != NULL && sym->attr.flavor != FL_PARAMETER)
8613 : {
8614 1 : gfc_error ("Variable %qs at %L not specified in a locality spec "
8615 : "of DO CONCURRENT at %L but required due to "
8616 : "DEFAULT (NONE)",
8617 1 : sym->name, &(*e)->where, &d->code->loc);
8618 1 : d->sym_hash->add (sym);
8619 : }
8620 : }
8621 : }
8622 : return 0;
8623 : }
8624 :
8625 : static void
8626 210 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8627 : {
8628 210 : struct check_default_none_data data;
8629 210 : data.code = code;
8630 210 : data.sym_hash = new hash_set<gfc_symbol *>;
8631 210 : data.ns = ns;
8632 210 : data.default_none = code->ext.concur.default_none;
8633 :
8634 1050 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8635 : {
8636 840 : const char *name;
8637 840 : switch (locality)
8638 : {
8639 : case LOCALITY_LOCAL: name = "LOCAL"; break;
8640 210 : case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8641 210 : case LOCALITY_SHARED: name = "SHARED"; break;
8642 210 : case LOCALITY_REDUCE: name = "REDUCE"; break;
8643 : default: gcc_unreachable ();
8644 : }
8645 :
8646 1227 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8647 387 : list = list->next)
8648 : {
8649 387 : gfc_expr *expr = list->expr;
8650 :
8651 387 : if (locality == LOCALITY_REDUCE
8652 72 : && (expr->expr_type == EXPR_FUNCTION
8653 48 : || expr->expr_type == EXPR_OP))
8654 35 : continue;
8655 :
8656 363 : if (!gfc_resolve_expr (expr))
8657 3 : continue;
8658 :
8659 360 : if (expr->expr_type != EXPR_VARIABLE
8660 360 : || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8661 360 : || (expr->ref
8662 147 : && (expr->ref->type != REF_ARRAY
8663 147 : || expr->ref->u.ar.type != AR_FULL
8664 143 : || expr->ref->next)))
8665 : {
8666 4 : gfc_error ("Expected variable name in %s locality spec at %L",
8667 : name, &expr->where);
8668 4 : continue;
8669 : }
8670 :
8671 356 : gfc_symbol *sym = expr->symtree->n.sym;
8672 :
8673 356 : if (data.sym_hash->contains (sym))
8674 : {
8675 4 : gfc_error ("Variable %qs at %L has already been specified in a "
8676 : "locality-spec", sym->name, &expr->where);
8677 4 : continue;
8678 : }
8679 :
8680 352 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8681 704 : iter; iter = iter->next)
8682 : {
8683 352 : if (iter->var->symtree->n.sym == sym)
8684 : {
8685 1 : gfc_error ("Index variable %qs at %L cannot be specified in a "
8686 : "locality-spec", sym->name, &expr->where);
8687 1 : continue;
8688 : }
8689 :
8690 351 : data.sym_hash->add (iter->var->symtree->n.sym);
8691 : }
8692 :
8693 352 : if (locality == LOCALITY_LOCAL
8694 352 : || locality == LOCALITY_LOCAL_INIT
8695 352 : || locality == LOCALITY_REDUCE)
8696 : {
8697 198 : if (sym->attr.optional)
8698 3 : gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8699 : "locality-spec at %L",
8700 : sym->name, name, &expr->where);
8701 :
8702 198 : if (sym->attr.dimension
8703 66 : && sym->as
8704 66 : && sym->as->type == AS_ASSUMED_SIZE)
8705 0 : gfc_error ("Assumed-size array not permitted for %qs in %s "
8706 : "locality-spec at %L",
8707 : sym->name, name, &expr->where);
8708 :
8709 198 : gfc_check_vardef_context (expr, false, false, false, name);
8710 : }
8711 :
8712 198 : if (locality == LOCALITY_LOCAL
8713 : || locality == LOCALITY_LOCAL_INIT)
8714 : {
8715 181 : symbol_attribute attr = gfc_expr_attr (expr);
8716 :
8717 181 : if (attr.allocatable)
8718 2 : gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8719 : "locality-spec at %L",
8720 : sym->name, name, &expr->where);
8721 :
8722 179 : else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8723 2 : gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8724 : " for %qs in %s locality-spec at %L",
8725 : sym->name, name, &expr->where);
8726 :
8727 177 : else if (attr.codimension)
8728 0 : gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8729 : "at %L",
8730 : sym->name, name, &expr->where);
8731 :
8732 177 : else if (expr->ts.type == BT_DERIVED
8733 177 : && gfc_is_finalizable (expr->ts.u.derived, NULL))
8734 0 : gfc_error ("Finalizable type not permitted for %qs in %s "
8735 : "locality-spec at %L",
8736 : sym->name, name, &expr->where);
8737 :
8738 177 : else if (gfc_has_ultimate_allocatable (expr))
8739 4 : gfc_error ("Type with ultimate allocatable component not "
8740 : "permitted for %qs in %s locality-spec at %L",
8741 : sym->name, name, &expr->where);
8742 : }
8743 :
8744 171 : else if (locality == LOCALITY_REDUCE)
8745 : {
8746 17 : if (sym->attr.asynchronous)
8747 1 : gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8748 : "REDUCE locality-spec at %L",
8749 : sym->name, &expr->where);
8750 17 : if (sym->attr.volatile_)
8751 1 : gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8752 : "locality-spec at %L", sym->name, &expr->where);
8753 : }
8754 :
8755 352 : data.sym_hash->add (sym);
8756 : }
8757 :
8758 840 : if (locality == LOCALITY_LOCAL)
8759 : {
8760 210 : gcc_assert (locality == 0);
8761 :
8762 210 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8763 437 : iter; iter = iter->next)
8764 : {
8765 227 : gfc_expr_walker (&iter->start,
8766 : do_concur_locality_specs_f2023,
8767 : &data);
8768 :
8769 227 : gfc_expr_walker (&iter->end,
8770 : do_concur_locality_specs_f2023,
8771 : &data);
8772 :
8773 227 : gfc_expr_walker (&iter->stride,
8774 : do_concur_locality_specs_f2023,
8775 : &data);
8776 : }
8777 :
8778 210 : if (code->expr1)
8779 7 : gfc_expr_walker (&code->expr1,
8780 : do_concur_locality_specs_f2023,
8781 : &data);
8782 : }
8783 : }
8784 :
8785 210 : gfc_expr *reduce_op = NULL;
8786 :
8787 210 : for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8788 258 : list; list = list->next)
8789 : {
8790 48 : gfc_expr *expr = list->expr;
8791 :
8792 48 : if (expr->expr_type != EXPR_VARIABLE)
8793 : {
8794 24 : reduce_op = expr;
8795 24 : continue;
8796 : }
8797 :
8798 24 : if (reduce_op->expr_type == EXPR_OP)
8799 : {
8800 17 : switch (reduce_op->value.op.op)
8801 : {
8802 17 : case INTRINSIC_PLUS:
8803 17 : case INTRINSIC_TIMES:
8804 17 : if (!gfc_numeric_ts (&expr->ts))
8805 3 : gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8806 3 : "got %s", expr->symtree->n.sym->name,
8807 : &expr->where, gfc_basic_typename (expr->ts.type));
8808 : break;
8809 0 : case INTRINSIC_AND:
8810 0 : case INTRINSIC_OR:
8811 0 : case INTRINSIC_EQV:
8812 0 : case INTRINSIC_NEQV:
8813 0 : if (expr->ts.type != BT_LOGICAL)
8814 0 : gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8815 0 : "got %qs", expr->symtree->n.sym->name,
8816 : &expr->where, gfc_basic_typename (expr->ts.type));
8817 : break;
8818 0 : default:
8819 0 : gcc_unreachable ();
8820 : }
8821 : }
8822 :
8823 7 : else if (reduce_op->expr_type == EXPR_FUNCTION)
8824 : {
8825 7 : switch (reduce_op->value.function.isym->id)
8826 : {
8827 6 : case GFC_ISYM_MIN:
8828 6 : case GFC_ISYM_MAX:
8829 6 : if (expr->ts.type != BT_INTEGER
8830 : && expr->ts.type != BT_REAL
8831 : && expr->ts.type != BT_CHARACTER)
8832 2 : gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8833 : "in REDUCE with MIN/MAX at %L, got %s",
8834 2 : expr->symtree->n.sym->name, &expr->where,
8835 : gfc_basic_typename (expr->ts.type));
8836 : break;
8837 1 : case GFC_ISYM_IAND:
8838 1 : case GFC_ISYM_IOR:
8839 1 : case GFC_ISYM_IEOR:
8840 1 : if (expr->ts.type != BT_INTEGER)
8841 1 : gfc_error ("Expected integer type for %qs in REDUCE with "
8842 : "IAND/IOR/IEOR at %L, got %s",
8843 1 : expr->symtree->n.sym->name, &expr->where,
8844 : gfc_basic_typename (expr->ts.type));
8845 : break;
8846 0 : default:
8847 0 : gcc_unreachable ();
8848 : }
8849 : }
8850 :
8851 : else
8852 0 : gcc_unreachable ();
8853 : }
8854 :
8855 1050 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8856 : {
8857 1227 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8858 387 : list = list->next)
8859 : {
8860 387 : if (list->expr->expr_type == EXPR_VARIABLE)
8861 363 : list->expr->symtree->n.sym->mark = 0;
8862 : }
8863 : }
8864 :
8865 210 : gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8866 : check_default_none_expr, &data);
8867 :
8868 1050 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8869 : {
8870 840 : gfc_expr_list **plist = &code->ext.concur.locality[locality];
8871 1227 : while (*plist)
8872 : {
8873 387 : gfc_expr *expr = (*plist)->expr;
8874 387 : if (expr->expr_type == EXPR_VARIABLE)
8875 : {
8876 363 : gfc_symbol *sym = expr->symtree->n.sym;
8877 363 : if (sym->mark == 0)
8878 : {
8879 70 : gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8880 : "locality-spec at %L is not used",
8881 : sym->name, &expr->where);
8882 70 : gfc_expr_list *tmp = *plist;
8883 70 : *plist = (*plist)->next;
8884 70 : gfc_free_expr (tmp->expr);
8885 70 : free (tmp);
8886 70 : continue;
8887 70 : }
8888 : }
8889 317 : plist = &((*plist)->next);
8890 : }
8891 : }
8892 :
8893 420 : delete data.sym_hash;
8894 210 : }
8895 :
8896 : /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8897 : to be a scalar INTEGER variable. The subscripts and stride are scalar
8898 : INTEGERs, and if stride is a constant it must be nonzero.
8899 : Furthermore "A subscript or stride in a forall-triplet-spec shall
8900 : not contain a reference to any index-name in the
8901 : forall-triplet-spec-list in which it appears." (7.5.4.1) */
8902 :
8903 : static void
8904 2202 : resolve_forall_iterators (gfc_forall_iterator *it)
8905 : {
8906 2202 : gfc_forall_iterator *iter, *iter2;
8907 :
8908 6320 : for (iter = it; iter; iter = iter->next)
8909 : {
8910 4118 : if (gfc_resolve_expr (iter->var)
8911 4118 : && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8912 0 : gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8913 : &iter->var->where);
8914 :
8915 4118 : if (gfc_resolve_expr (iter->start)
8916 4118 : && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8917 0 : gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8918 : &iter->start->where);
8919 4118 : if (iter->var->ts.kind != iter->start->ts.kind)
8920 1 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8921 :
8922 4118 : if (gfc_resolve_expr (iter->end)
8923 4118 : && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8924 0 : gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8925 : &iter->end->where);
8926 4118 : if (iter->var->ts.kind != iter->end->ts.kind)
8927 2 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8928 :
8929 4118 : if (gfc_resolve_expr (iter->stride))
8930 : {
8931 4118 : if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8932 0 : gfc_error ("FORALL stride expression at %L must be a scalar %s",
8933 : &iter->stride->where, "INTEGER");
8934 :
8935 4118 : if (iter->stride->expr_type == EXPR_CONSTANT
8936 4115 : && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8937 1 : gfc_error ("FORALL stride expression at %L cannot be zero",
8938 : &iter->stride->where);
8939 : }
8940 4118 : if (iter->var->ts.kind != iter->stride->ts.kind)
8941 1 : gfc_convert_type (iter->stride, &iter->var->ts, 1);
8942 : }
8943 :
8944 6320 : for (iter = it; iter; iter = iter->next)
8945 11078 : for (iter2 = iter; iter2; iter2 = iter2->next)
8946 : {
8947 6960 : if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8948 6958 : || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8949 13916 : || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8950 6 : gfc_error ("FORALL index %qs may not appear in triplet "
8951 6 : "specification at %L", iter->var->symtree->name,
8952 6 : &iter2->start->where);
8953 : }
8954 2202 : }
8955 :
8956 :
8957 : /* Given a pointer to a symbol that is a derived type, see if it's
8958 : inaccessible, i.e. if it's defined in another module and the components are
8959 : PRIVATE. The search is recursive if necessary. Returns zero if no
8960 : inaccessible components are found, nonzero otherwise. */
8961 :
8962 : static bool
8963 1352 : derived_inaccessible (gfc_symbol *sym)
8964 : {
8965 1352 : gfc_component *c;
8966 :
8967 1352 : if (sym->attr.use_assoc && sym->attr.private_comp)
8968 : return 1;
8969 :
8970 4001 : for (c = sym->components; c; c = c->next)
8971 : {
8972 : /* Prevent an infinite loop through this function. */
8973 2662 : if (c->ts.type == BT_DERIVED
8974 289 : && (c->attr.pointer || c->attr.allocatable)
8975 72 : && sym == c->ts.u.derived)
8976 72 : continue;
8977 :
8978 2590 : if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8979 : return 1;
8980 : }
8981 :
8982 : return 0;
8983 : }
8984 :
8985 :
8986 : /* Resolve the argument of a deallocate expression. The expression must be
8987 : a pointer or a full array. */
8988 :
8989 : static bool
8990 8351 : resolve_deallocate_expr (gfc_expr *e)
8991 : {
8992 8351 : symbol_attribute attr;
8993 8351 : int allocatable, pointer;
8994 8351 : gfc_ref *ref;
8995 8351 : gfc_symbol *sym;
8996 8351 : gfc_component *c;
8997 8351 : bool unlimited;
8998 :
8999 8351 : if (!gfc_resolve_expr (e))
9000 : return false;
9001 :
9002 8351 : if (e->expr_type != EXPR_VARIABLE)
9003 0 : goto bad;
9004 :
9005 8351 : sym = e->symtree->n.sym;
9006 8351 : unlimited = UNLIMITED_POLY(sym);
9007 :
9008 8351 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
9009 : {
9010 1574 : allocatable = CLASS_DATA (sym)->attr.allocatable;
9011 1574 : pointer = CLASS_DATA (sym)->attr.class_pointer;
9012 : }
9013 : else
9014 : {
9015 6777 : allocatable = sym->attr.allocatable;
9016 6777 : pointer = sym->attr.pointer;
9017 : }
9018 16773 : for (ref = e->ref; ref; ref = ref->next)
9019 : {
9020 8422 : switch (ref->type)
9021 : {
9022 6284 : case REF_ARRAY:
9023 6284 : if (ref->u.ar.type != AR_FULL
9024 6492 : && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
9025 208 : && ref->u.ar.codimen && gfc_ref_this_image (ref)))
9026 : allocatable = 0;
9027 : break;
9028 :
9029 2138 : case REF_COMPONENT:
9030 2138 : c = ref->u.c.component;
9031 2138 : if (c->ts.type == BT_CLASS)
9032 : {
9033 297 : allocatable = CLASS_DATA (c)->attr.allocatable;
9034 297 : pointer = CLASS_DATA (c)->attr.class_pointer;
9035 : }
9036 : else
9037 : {
9038 1841 : allocatable = c->attr.allocatable;
9039 1841 : pointer = c->attr.pointer;
9040 : }
9041 : break;
9042 :
9043 : case REF_SUBSTRING:
9044 : case REF_INQUIRY:
9045 513 : allocatable = 0;
9046 : break;
9047 : }
9048 : }
9049 :
9050 8351 : attr = gfc_expr_attr (e);
9051 :
9052 8351 : if (allocatable == 0 && attr.pointer == 0 && !unlimited)
9053 : {
9054 3 : bad:
9055 3 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
9056 : &e->where);
9057 3 : return false;
9058 : }
9059 :
9060 : /* F2008, C644. */
9061 8348 : if (gfc_is_coindexed (e))
9062 : {
9063 1 : gfc_error ("Coindexed allocatable object at %L", &e->where);
9064 1 : return false;
9065 : }
9066 :
9067 8347 : if (pointer
9068 10715 : && !gfc_check_vardef_context (e, true, true, false,
9069 2368 : _("DEALLOCATE object")))
9070 : return false;
9071 8345 : if (!gfc_check_vardef_context (e, false, true, false,
9072 8345 : _("DEALLOCATE object")))
9073 : return false;
9074 :
9075 : return true;
9076 : }
9077 :
9078 :
9079 : /* Returns true if the expression e contains a reference to the symbol sym. */
9080 : static bool
9081 47371 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
9082 : {
9083 47371 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
9084 2081 : return true;
9085 :
9086 : return false;
9087 : }
9088 :
9089 : bool
9090 20080 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
9091 : {
9092 20080 : return gfc_traverse_expr (e, sym, sym_in_expr, 0);
9093 : }
9094 :
9095 : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
9096 : of character expressions. */
9097 : static bool
9098 20468 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
9099 : {
9100 0 : return gfc_traverse_expr (e, sym, sym_in_expr, -1);
9101 : }
9102 :
9103 :
9104 : /* Given the expression node e for an allocatable/pointer of derived type to be
9105 : allocated, get the expression node to be initialized afterwards (needed for
9106 : derived types with default initializers, and derived types with allocatable
9107 : components that need nullification.) */
9108 :
9109 : gfc_expr *
9110 5798 : gfc_expr_to_initialize (gfc_expr *e)
9111 : {
9112 5798 : gfc_expr *result;
9113 5798 : gfc_ref *ref;
9114 5798 : int i;
9115 :
9116 5798 : result = gfc_copy_expr (e);
9117 :
9118 : /* Change the last array reference from AR_ELEMENT to AR_FULL. */
9119 11467 : for (ref = result->ref; ref; ref = ref->next)
9120 9038 : if (ref->type == REF_ARRAY && ref->next == NULL)
9121 : {
9122 3369 : if (ref->u.ar.dimen == 0
9123 77 : && ref->u.ar.as && ref->u.ar.as->corank)
9124 : return result;
9125 :
9126 3292 : ref->u.ar.type = AR_FULL;
9127 :
9128 7436 : for (i = 0; i < ref->u.ar.dimen; i++)
9129 4144 : ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
9130 :
9131 : break;
9132 : }
9133 :
9134 5721 : gfc_free_shape (&result->shape, result->rank);
9135 :
9136 : /* Recalculate rank, shape, etc. */
9137 5721 : gfc_resolve_expr (result);
9138 5721 : return result;
9139 : }
9140 :
9141 :
9142 : /* If the last ref of an expression is an array ref, return a copy of the
9143 : expression with that one removed. Otherwise, a copy of the original
9144 : expression. This is used for allocate-expressions and pointer assignment
9145 : LHS, where there may be an array specification that needs to be stripped
9146 : off when using gfc_check_vardef_context. */
9147 :
9148 : static gfc_expr*
9149 27722 : remove_last_array_ref (gfc_expr* e)
9150 : {
9151 27722 : gfc_expr* e2;
9152 27722 : gfc_ref** r;
9153 :
9154 27722 : e2 = gfc_copy_expr (e);
9155 35840 : for (r = &e2->ref; *r; r = &(*r)->next)
9156 24497 : if ((*r)->type == REF_ARRAY && !(*r)->next)
9157 : {
9158 16379 : gfc_free_ref_list (*r);
9159 16379 : *r = NULL;
9160 16379 : break;
9161 : }
9162 :
9163 27722 : return e2;
9164 : }
9165 :
9166 :
9167 : /* Used in resolve_allocate_expr to check that a allocation-object and
9168 : a source-expr are conformable. This does not catch all possible
9169 : cases; in particular a runtime checking is needed. */
9170 :
9171 : static bool
9172 1909 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
9173 : {
9174 1909 : gfc_ref *tail;
9175 1909 : bool scalar;
9176 :
9177 2641 : for (tail = e2->ref; tail && tail->next; tail = tail->next);
9178 :
9179 : /* If MOLD= is present and is not scalar, and the allocate-object has an
9180 : explicit-shape-spec, the ranks need not agree. This may be unintended,
9181 : so let's emit a warning if -Wsurprising is given. */
9182 1909 : scalar = !tail || tail->type == REF_COMPONENT;
9183 1909 : if (e1->mold && e1->rank > 0
9184 165 : && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
9185 : {
9186 27 : if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
9187 15 : gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
9188 : "but MOLD= expression at %L has rank %d",
9189 6 : &e2->where, scalar ? 0 : tail->u.ar.as->rank,
9190 : &e1->where, e1->rank);
9191 30 : return true;
9192 : }
9193 :
9194 : /* First compare rank. */
9195 1879 : if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
9196 2 : || (!tail && e1->rank != e2->rank))
9197 : {
9198 7 : gfc_error ("Source-expr at %L must be scalar or have the "
9199 : "same rank as the allocate-object at %L",
9200 : &e1->where, &e2->where);
9201 7 : return false;
9202 : }
9203 :
9204 1872 : if (e1->shape)
9205 : {
9206 1373 : int i;
9207 1373 : mpz_t s;
9208 :
9209 1373 : mpz_init (s);
9210 :
9211 3165 : for (i = 0; i < e1->rank; i++)
9212 : {
9213 1379 : if (tail->u.ar.start[i] == NULL)
9214 : break;
9215 :
9216 419 : if (tail->u.ar.end[i])
9217 : {
9218 54 : mpz_set (s, tail->u.ar.end[i]->value.integer);
9219 54 : mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
9220 54 : mpz_add_ui (s, s, 1);
9221 : }
9222 : else
9223 : {
9224 365 : mpz_set (s, tail->u.ar.start[i]->value.integer);
9225 : }
9226 :
9227 419 : if (mpz_cmp (e1->shape[i], s) != 0)
9228 : {
9229 0 : gfc_error ("Source-expr at %L and allocate-object at %L must "
9230 : "have the same shape", &e1->where, &e2->where);
9231 0 : mpz_clear (s);
9232 0 : return false;
9233 : }
9234 : }
9235 :
9236 1373 : mpz_clear (s);
9237 : }
9238 :
9239 : return true;
9240 : }
9241 :
9242 :
9243 : /* Resolve the expression in an ALLOCATE statement, doing the additional
9244 : checks to see whether the expression is OK or not. The expression must
9245 : have a trailing array reference that gives the size of the array. */
9246 :
9247 : static bool
9248 17365 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
9249 : {
9250 17365 : int i, pointer, allocatable, dimension, is_abstract;
9251 17365 : int codimension;
9252 17365 : bool coindexed;
9253 17365 : bool unlimited;
9254 17365 : symbol_attribute attr;
9255 17365 : gfc_ref *ref, *ref2;
9256 17365 : gfc_expr *e2;
9257 17365 : gfc_array_ref *ar;
9258 17365 : gfc_symbol *sym = NULL;
9259 17365 : gfc_alloc *a;
9260 17365 : gfc_component *c;
9261 17365 : bool t;
9262 :
9263 : /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
9264 : checking of coarrays. */
9265 22152 : for (ref = e->ref; ref; ref = ref->next)
9266 17980 : if (ref->next == NULL)
9267 : break;
9268 :
9269 17365 : if (ref && ref->type == REF_ARRAY)
9270 11998 : ref->u.ar.in_allocate = true;
9271 :
9272 17365 : if (!gfc_resolve_expr (e))
9273 1 : goto failure;
9274 :
9275 : /* Make sure the expression is allocatable or a pointer. If it is
9276 : pointer, the next-to-last reference must be a pointer. */
9277 :
9278 17364 : ref2 = NULL;
9279 17364 : if (e->symtree)
9280 17364 : sym = e->symtree->n.sym;
9281 :
9282 : /* Check whether ultimate component is abstract and CLASS. */
9283 34728 : is_abstract = 0;
9284 :
9285 : /* Is the allocate-object unlimited polymorphic? */
9286 17364 : unlimited = UNLIMITED_POLY(e);
9287 :
9288 17364 : if (e->expr_type != EXPR_VARIABLE)
9289 : {
9290 0 : allocatable = 0;
9291 0 : attr = gfc_expr_attr (e);
9292 0 : pointer = attr.pointer;
9293 0 : dimension = attr.dimension;
9294 0 : codimension = attr.codimension;
9295 : }
9296 : else
9297 : {
9298 17364 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
9299 : {
9300 3426 : allocatable = CLASS_DATA (sym)->attr.allocatable;
9301 3426 : pointer = CLASS_DATA (sym)->attr.class_pointer;
9302 3426 : dimension = CLASS_DATA (sym)->attr.dimension;
9303 3426 : codimension = CLASS_DATA (sym)->attr.codimension;
9304 3426 : is_abstract = CLASS_DATA (sym)->attr.abstract;
9305 : }
9306 : else
9307 : {
9308 13938 : allocatable = sym->attr.allocatable;
9309 13938 : pointer = sym->attr.pointer;
9310 13938 : dimension = sym->attr.dimension;
9311 13938 : codimension = sym->attr.codimension;
9312 : }
9313 :
9314 17364 : coindexed = false;
9315 :
9316 35338 : for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
9317 : {
9318 17976 : switch (ref->type)
9319 : {
9320 13450 : case REF_ARRAY:
9321 13450 : if (ref->u.ar.codimen > 0)
9322 : {
9323 760 : int n;
9324 1061 : for (n = ref->u.ar.dimen;
9325 1061 : n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
9326 801 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
9327 : {
9328 : coindexed = true;
9329 : break;
9330 : }
9331 : }
9332 :
9333 13450 : if (ref->next != NULL)
9334 1454 : pointer = 0;
9335 : break;
9336 :
9337 4526 : case REF_COMPONENT:
9338 : /* F2008, C644. */
9339 4526 : if (coindexed)
9340 : {
9341 2 : gfc_error ("Coindexed allocatable object at %L",
9342 : &e->where);
9343 2 : goto failure;
9344 : }
9345 :
9346 4524 : c = ref->u.c.component;
9347 4524 : if (c->ts.type == BT_CLASS)
9348 : {
9349 988 : allocatable = CLASS_DATA (c)->attr.allocatable;
9350 988 : pointer = CLASS_DATA (c)->attr.class_pointer;
9351 988 : dimension = CLASS_DATA (c)->attr.dimension;
9352 988 : codimension = CLASS_DATA (c)->attr.codimension;
9353 988 : is_abstract = CLASS_DATA (c)->attr.abstract;
9354 : }
9355 : else
9356 : {
9357 3536 : allocatable = c->attr.allocatable;
9358 3536 : pointer = c->attr.pointer;
9359 3536 : dimension = c->attr.dimension;
9360 3536 : codimension = c->attr.codimension;
9361 3536 : is_abstract = c->attr.abstract;
9362 : }
9363 : break;
9364 :
9365 0 : case REF_SUBSTRING:
9366 0 : case REF_INQUIRY:
9367 0 : allocatable = 0;
9368 0 : pointer = 0;
9369 0 : break;
9370 : }
9371 : }
9372 : }
9373 :
9374 : /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
9375 : pointer or an allocatable variable. */
9376 17362 : if (allocatable == 0 && pointer == 0)
9377 : {
9378 4 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
9379 : &e->where);
9380 4 : goto failure;
9381 : }
9382 :
9383 : /* Some checks for the SOURCE tag. */
9384 17358 : if (code->expr3)
9385 : {
9386 : /* Check F03:C632: "The source-expr shall be a scalar or have the same
9387 : rank as allocate-object". This would require the MOLD argument to
9388 : NULL() as source-expr for subsequent checking. However, even the
9389 : resulting disassociated pointer or unallocated array has no shape that
9390 : could be used for SOURCE= or MOLD=. */
9391 3849 : if (code->expr3->expr_type == EXPR_NULL)
9392 : {
9393 4 : gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
9394 : &code->expr3->where);
9395 4 : goto failure;
9396 : }
9397 :
9398 : /* Check F03:C631. */
9399 3845 : if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
9400 : {
9401 10 : gfc_error ("Type of entity at %L is type incompatible with "
9402 10 : "source-expr at %L", &e->where, &code->expr3->where);
9403 10 : goto failure;
9404 : }
9405 :
9406 : /* Check F03:C632 and restriction following Note 6.18. */
9407 3835 : if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
9408 7 : goto failure;
9409 :
9410 : /* Check F03:C633. */
9411 3828 : if (code->expr3->ts.kind != e->ts.kind && !unlimited)
9412 : {
9413 1 : gfc_error ("The allocate-object at %L and the source-expr at %L "
9414 : "shall have the same kind type parameter",
9415 : &e->where, &code->expr3->where);
9416 1 : goto failure;
9417 : }
9418 :
9419 : /* Check F2008, C642. */
9420 3827 : if (code->expr3->ts.type == BT_DERIVED
9421 3827 : && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
9422 1192 : || (code->expr3->ts.u.derived->from_intmod
9423 : == INTMOD_ISO_FORTRAN_ENV
9424 0 : && code->expr3->ts.u.derived->intmod_sym_id
9425 : == ISOFORTRAN_LOCK_TYPE)))
9426 : {
9427 0 : gfc_error ("The source-expr at %L shall neither be of type "
9428 : "LOCK_TYPE nor have a LOCK_TYPE component if "
9429 : "allocate-object at %L is a coarray",
9430 0 : &code->expr3->where, &e->where);
9431 0 : goto failure;
9432 : }
9433 :
9434 : /* Check F2008:C639: "Corresponding kind type parameters of
9435 : allocate-object and source-expr shall have the same values." */
9436 3827 : if (e->ts.type == BT_CHARACTER
9437 816 : && !e->ts.deferred
9438 162 : && e->ts.u.cl->length
9439 162 : && code->expr3->ts.type == BT_CHARACTER
9440 3989 : && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9441 : "SOURCE= or MOLD= specifier"))
9442 17 : goto failure;
9443 :
9444 : /* Check TS18508, C702/C703. */
9445 3810 : if (code->expr3->ts.type == BT_DERIVED
9446 5002 : && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9447 1192 : || (code->expr3->ts.u.derived->from_intmod
9448 : == INTMOD_ISO_FORTRAN_ENV
9449 0 : && code->expr3->ts.u.derived->intmod_sym_id
9450 : == ISOFORTRAN_EVENT_TYPE)))
9451 : {
9452 0 : gfc_error ("The source-expr at %L shall neither be of type "
9453 : "EVENT_TYPE nor have a EVENT_TYPE component if "
9454 : "allocate-object at %L is a coarray",
9455 0 : &code->expr3->where, &e->where);
9456 0 : goto failure;
9457 : }
9458 : }
9459 :
9460 : /* Check F08:C629. */
9461 17319 : if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9462 153 : && !code->expr3)
9463 : {
9464 2 : gcc_assert (e->ts.type == BT_CLASS);
9465 2 : gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9466 : "type-spec or source-expr", sym->name, &e->where);
9467 2 : goto failure;
9468 : }
9469 :
9470 : /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9471 : if and only if each allocate-object is a dummy argument for which the
9472 : corresponding type parameter is assumed. */
9473 17317 : if (code->ext.alloc.ts.type == BT_CHARACTER
9474 513 : && code->ext.alloc.ts.u.cl->length != NULL
9475 498 : && e->ts.type == BT_CHARACTER && !e->ts.deferred
9476 23 : && e->ts.u.cl->length == NULL
9477 2 : && e->symtree->n.sym->attr.dummy)
9478 : {
9479 2 : gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9480 : "shall be an asterisk as allocate object %qs at %L is a "
9481 : "dummy argument with assumed type parameter",
9482 : sym->name, &e->where);
9483 2 : goto failure;
9484 : }
9485 :
9486 : /* Check F08:C632. */
9487 17315 : if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9488 60 : && !UNLIMITED_POLY (e))
9489 : {
9490 36 : int cmp;
9491 :
9492 36 : if (!e->ts.u.cl->length)
9493 15 : goto failure;
9494 :
9495 42 : cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9496 21 : code->ext.alloc.ts.u.cl->length);
9497 21 : if (cmp == 1 || cmp == -1 || cmp == -3)
9498 : {
9499 2 : gfc_error ("Allocating %s at %L with type-spec requires the same "
9500 : "character-length parameter as in the declaration",
9501 : sym->name, &e->where);
9502 2 : goto failure;
9503 : }
9504 : }
9505 :
9506 : /* In the variable definition context checks, gfc_expr_attr is used
9507 : on the expression. This is fooled by the array specification
9508 : present in e, thus we have to eliminate that one temporarily. */
9509 17298 : e2 = remove_last_array_ref (e);
9510 17298 : t = true;
9511 17298 : if (t && pointer)
9512 3869 : t = gfc_check_vardef_context (e2, true, true, false,
9513 3869 : _("ALLOCATE object"));
9514 3869 : if (t)
9515 17290 : t = gfc_check_vardef_context (e2, false, true, false,
9516 17290 : _("ALLOCATE object"));
9517 17298 : gfc_free_expr (e2);
9518 17298 : if (!t)
9519 11 : goto failure;
9520 :
9521 17287 : code->ext.alloc.expr3_not_explicit = 0;
9522 17287 : if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9523 1617 : && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9524 : {
9525 : /* For class arrays, the initialization with SOURCE is done
9526 : using _copy and trans_call. It is convenient to exploit that
9527 : when the allocated type is different from the declared type but
9528 : no SOURCE exists by setting expr3. */
9529 305 : code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9530 305 : code->ext.alloc.expr3_not_explicit = 1;
9531 : }
9532 16982 : else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9533 2628 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9534 6 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9535 : {
9536 : /* We have to zero initialize the integer variable. */
9537 2 : code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9538 2 : code->ext.alloc.expr3_not_explicit = 1;
9539 : }
9540 :
9541 17287 : if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9542 : {
9543 : /* Make sure the vtab symbol is present when
9544 : the module variables are generated. */
9545 2990 : gfc_typespec ts = e->ts;
9546 2990 : if (code->expr3)
9547 1331 : ts = code->expr3->ts;
9548 1659 : else if (code->ext.alloc.ts.type == BT_DERIVED)
9549 726 : ts = code->ext.alloc.ts;
9550 :
9551 : /* Finding the vtab also publishes the type's symbol. Therefore this
9552 : statement is necessary. */
9553 2990 : gfc_find_derived_vtab (ts.u.derived);
9554 2990 : }
9555 14297 : else if (unlimited && !UNLIMITED_POLY (code->expr3))
9556 : {
9557 : /* Again, make sure the vtab symbol is present when
9558 : the module variables are generated. */
9559 434 : gfc_typespec *ts = NULL;
9560 434 : if (code->expr3)
9561 347 : ts = &code->expr3->ts;
9562 : else
9563 87 : ts = &code->ext.alloc.ts;
9564 :
9565 434 : gcc_assert (ts);
9566 :
9567 : /* Finding the vtab also publishes the type's symbol. Therefore this
9568 : statement is necessary. */
9569 434 : gfc_find_vtab (ts);
9570 : }
9571 :
9572 17287 : if (dimension == 0 && codimension == 0)
9573 5320 : goto success;
9574 :
9575 : /* Make sure the last reference node is an array specification. */
9576 :
9577 11967 : if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9578 10735 : || (dimension && ref2->u.ar.dimen == 0))
9579 : {
9580 : /* F08:C633. */
9581 1232 : if (code->expr3)
9582 : {
9583 1231 : if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9584 : "in ALLOCATE statement at %L", &e->where))
9585 0 : goto failure;
9586 1231 : if (code->expr3->rank != 0)
9587 1230 : *array_alloc_wo_spec = true;
9588 : else
9589 : {
9590 1 : gfc_error ("Array specification or array-valued SOURCE= "
9591 : "expression required in ALLOCATE statement at %L",
9592 : &e->where);
9593 1 : goto failure;
9594 : }
9595 : }
9596 : else
9597 : {
9598 1 : gfc_error ("Array specification required in ALLOCATE statement "
9599 : "at %L", &e->where);
9600 1 : goto failure;
9601 : }
9602 : }
9603 :
9604 : /* Make sure that the array section reference makes sense in the
9605 : context of an ALLOCATE specification. */
9606 :
9607 11965 : ar = &ref2->u.ar;
9608 :
9609 11965 : if (codimension)
9610 1179 : for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9611 : {
9612 692 : switch (ar->dimen_type[i])
9613 : {
9614 2 : case DIMEN_THIS_IMAGE:
9615 2 : gfc_error ("Coarray specification required in ALLOCATE statement "
9616 : "at %L", &e->where);
9617 2 : goto failure;
9618 :
9619 98 : case DIMEN_RANGE:
9620 : /* F2018:R937:
9621 : * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9622 : */
9623 98 : if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9624 : {
9625 8 : gfc_error ("Bad coarray specification in ALLOCATE statement "
9626 : "at %L", &e->where);
9627 8 : goto failure;
9628 : }
9629 90 : else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9630 : {
9631 2 : gfc_error ("Upper cobound is less than lower cobound at %L",
9632 2 : &ar->start[i]->where);
9633 2 : goto failure;
9634 : }
9635 : break;
9636 :
9637 105 : case DIMEN_ELEMENT:
9638 105 : if (ar->start[i]->expr_type == EXPR_CONSTANT)
9639 : {
9640 97 : gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9641 97 : if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9642 : {
9643 1 : gfc_error ("Upper cobound is less than lower cobound "
9644 : "of 1 at %L", &ar->start[i]->where);
9645 1 : goto failure;
9646 : }
9647 : }
9648 : break;
9649 :
9650 : case DIMEN_STAR:
9651 : break;
9652 :
9653 0 : default:
9654 0 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9655 : &e->where);
9656 0 : goto failure;
9657 :
9658 : }
9659 : }
9660 29294 : for (i = 0; i < ar->dimen; i++)
9661 : {
9662 17346 : if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9663 14612 : goto check_symbols;
9664 :
9665 2734 : switch (ar->dimen_type[i])
9666 : {
9667 : case DIMEN_ELEMENT:
9668 : break;
9669 :
9670 2468 : case DIMEN_RANGE:
9671 2468 : if (ar->start[i] != NULL
9672 2468 : && ar->end[i] != NULL
9673 2467 : && ar->stride[i] == NULL)
9674 : break;
9675 :
9676 : /* Fall through. */
9677 :
9678 1 : case DIMEN_UNKNOWN:
9679 1 : case DIMEN_VECTOR:
9680 1 : case DIMEN_STAR:
9681 1 : case DIMEN_THIS_IMAGE:
9682 1 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9683 : &e->where);
9684 1 : goto failure;
9685 : }
9686 :
9687 2467 : check_symbols:
9688 45125 : for (a = code->ext.alloc.list; a; a = a->next)
9689 : {
9690 27783 : sym = a->expr->symtree->n.sym;
9691 :
9692 : /* TODO - check derived type components. */
9693 27783 : if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9694 9357 : continue;
9695 :
9696 18426 : if ((ar->start[i] != NULL
9697 17746 : && gfc_find_var_in_expr (sym, ar->start[i]))
9698 36169 : || (ar->end[i] != NULL
9699 2722 : && gfc_find_var_in_expr (sym, ar->end[i])))
9700 : {
9701 3 : gfc_error ("%qs must not appear in the array specification at "
9702 : "%L in the same ALLOCATE statement where it is "
9703 : "itself allocated", sym->name, &ar->where);
9704 3 : goto failure;
9705 : }
9706 : }
9707 : }
9708 :
9709 12139 : for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9710 : {
9711 868 : if (ar->dimen_type[i] == DIMEN_ELEMENT
9712 677 : || ar->dimen_type[i] == DIMEN_RANGE)
9713 : {
9714 191 : if (i == (ar->dimen + ar->codimen - 1))
9715 : {
9716 0 : gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9717 : "statement at %L", &e->where);
9718 0 : goto failure;
9719 : }
9720 191 : continue;
9721 : }
9722 :
9723 486 : if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9724 486 : && ar->stride[i] == NULL)
9725 : break;
9726 :
9727 0 : gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9728 : &e->where);
9729 0 : goto failure;
9730 : }
9731 :
9732 11948 : success:
9733 : return true;
9734 :
9735 : failure:
9736 : return false;
9737 : }
9738 :
9739 :
9740 : static void
9741 20407 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9742 : {
9743 20407 : gfc_expr *stat, *errmsg, *pe, *qe;
9744 20407 : gfc_alloc *a, *p, *q;
9745 :
9746 20407 : stat = code->expr1;
9747 20407 : errmsg = code->expr2;
9748 :
9749 : /* Check the stat variable. */
9750 20407 : if (stat)
9751 : {
9752 661 : if (!gfc_check_vardef_context (stat, false, false, false,
9753 661 : _("STAT variable")))
9754 8 : goto done_stat;
9755 :
9756 653 : if (stat->ts.type != BT_INTEGER
9757 644 : || stat->rank > 0)
9758 11 : gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9759 : "variable", &stat->where);
9760 :
9761 653 : if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9762 0 : goto done_stat;
9763 :
9764 : /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9765 : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9766 : */
9767 1354 : for (p = code->ext.alloc.list; p; p = p->next)
9768 708 : if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9769 : {
9770 9 : gfc_ref *ref1, *ref2;
9771 9 : bool found = true;
9772 :
9773 16 : for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9774 7 : ref1 = ref1->next, ref2 = ref2->next)
9775 : {
9776 9 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9777 5 : continue;
9778 4 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9779 : {
9780 : found = false;
9781 : break;
9782 : }
9783 : }
9784 :
9785 9 : if (found)
9786 : {
9787 7 : gfc_error ("Stat-variable at %L shall not be %sd within "
9788 : "the same %s statement", &stat->where, fcn, fcn);
9789 7 : break;
9790 : }
9791 : }
9792 : }
9793 :
9794 19746 : done_stat:
9795 :
9796 : /* Check the errmsg variable. */
9797 20407 : if (errmsg)
9798 : {
9799 150 : if (!stat)
9800 2 : gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9801 : &errmsg->where);
9802 :
9803 150 : if (!gfc_check_vardef_context (errmsg, false, false, false,
9804 150 : _("ERRMSG variable")))
9805 6 : goto done_errmsg;
9806 :
9807 : /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9808 : F18:R930 errmsg-variable is scalar-default-char-variable
9809 : F18:R906 default-char-variable is variable
9810 : F18:C906 default-char-variable shall be default character. */
9811 144 : if (errmsg->ts.type != BT_CHARACTER
9812 142 : || errmsg->rank > 0
9813 141 : || errmsg->ts.kind != gfc_default_character_kind)
9814 4 : gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9815 : "variable", &errmsg->where);
9816 :
9817 144 : if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9818 0 : goto done_errmsg;
9819 :
9820 : /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9821 : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9822 : */
9823 286 : for (p = code->ext.alloc.list; p; p = p->next)
9824 147 : if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9825 : {
9826 9 : gfc_ref *ref1, *ref2;
9827 9 : bool found = true;
9828 :
9829 16 : for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9830 7 : ref1 = ref1->next, ref2 = ref2->next)
9831 : {
9832 11 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9833 4 : continue;
9834 7 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9835 : {
9836 : found = false;
9837 : break;
9838 : }
9839 : }
9840 :
9841 9 : if (found)
9842 : {
9843 5 : gfc_error ("Errmsg-variable at %L shall not be %sd within "
9844 : "the same %s statement", &errmsg->where, fcn, fcn);
9845 5 : break;
9846 : }
9847 : }
9848 : }
9849 :
9850 20257 : done_errmsg:
9851 :
9852 : /* Check that an allocate-object appears only once in the statement. */
9853 :
9854 46123 : for (p = code->ext.alloc.list; p; p = p->next)
9855 : {
9856 25716 : pe = p->expr;
9857 35003 : for (q = p->next; q; q = q->next)
9858 : {
9859 9287 : qe = q->expr;
9860 9287 : if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9861 : {
9862 : /* This is a potential collision. */
9863 2093 : gfc_ref *pr = pe->ref;
9864 2093 : gfc_ref *qr = qe->ref;
9865 :
9866 : /* Follow the references until
9867 : a) They start to differ, in which case there is no error;
9868 : you can deallocate a%b and a%c in a single statement
9869 : b) Both of them stop, which is an error
9870 : c) One of them stops, which is also an error. */
9871 4517 : while (1)
9872 : {
9873 3305 : if (pr == NULL && qr == NULL)
9874 : {
9875 7 : gfc_error ("Allocate-object at %L also appears at %L",
9876 : &pe->where, &qe->where);
9877 7 : break;
9878 : }
9879 3298 : else if (pr != NULL && qr == NULL)
9880 : {
9881 2 : gfc_error ("Allocate-object at %L is subobject of"
9882 : " object at %L", &pe->where, &qe->where);
9883 2 : break;
9884 : }
9885 3296 : else if (pr == NULL && qr != NULL)
9886 : {
9887 2 : gfc_error ("Allocate-object at %L is subobject of"
9888 : " object at %L", &qe->where, &pe->where);
9889 2 : break;
9890 : }
9891 : /* Here, pr != NULL && qr != NULL */
9892 3294 : gcc_assert(pr->type == qr->type);
9893 3294 : if (pr->type == REF_ARRAY)
9894 : {
9895 : /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9896 : which are legal. */
9897 1065 : gcc_assert (qr->type == REF_ARRAY);
9898 :
9899 1065 : if (pr->next && qr->next)
9900 : {
9901 : int i;
9902 : gfc_array_ref *par = &(pr->u.ar);
9903 : gfc_array_ref *qar = &(qr->u.ar);
9904 :
9905 1840 : for (i=0; i<par->dimen; i++)
9906 : {
9907 954 : if ((par->start[i] != NULL
9908 0 : || qar->start[i] != NULL)
9909 1908 : && gfc_dep_compare_expr (par->start[i],
9910 954 : qar->start[i]) != 0)
9911 168 : goto break_label;
9912 : }
9913 : }
9914 : }
9915 : else
9916 : {
9917 2229 : if (pr->u.c.component->name != qr->u.c.component->name)
9918 : break;
9919 : }
9920 :
9921 1212 : pr = pr->next;
9922 1212 : qr = qr->next;
9923 1212 : }
9924 9287 : break_label:
9925 : ;
9926 : }
9927 : }
9928 : }
9929 :
9930 20407 : if (strcmp (fcn, "ALLOCATE") == 0)
9931 : {
9932 14326 : bool arr_alloc_wo_spec = false;
9933 :
9934 : /* Resolving the expr3 in the loop over all objects to allocate would
9935 : execute loop invariant code for each loop item. Therefore do it just
9936 : once here. */
9937 14326 : if (code->expr3 && code->expr3->mold
9938 350 : && code->expr3->ts.type == BT_DERIVED
9939 24 : && !(code->expr3->ref && code->expr3->ref->type == REF_ARRAY))
9940 : {
9941 : /* Default initialization via MOLD (non-polymorphic). */
9942 22 : gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9943 22 : if (rhs != NULL)
9944 : {
9945 9 : gfc_resolve_expr (rhs);
9946 9 : gfc_free_expr (code->expr3);
9947 9 : code->expr3 = rhs;
9948 : }
9949 : }
9950 31691 : for (a = code->ext.alloc.list; a; a = a->next)
9951 17365 : resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9952 :
9953 14326 : if (arr_alloc_wo_spec && code->expr3)
9954 : {
9955 : /* Mark the allocate to have to take the array specification
9956 : from the expr3. */
9957 1224 : code->ext.alloc.arr_spec_from_expr3 = 1;
9958 : }
9959 : }
9960 : else
9961 : {
9962 14432 : for (a = code->ext.alloc.list; a; a = a->next)
9963 8351 : resolve_deallocate_expr (a->expr);
9964 : }
9965 20407 : }
9966 :
9967 :
9968 : /************ SELECT CASE resolution subroutines ************/
9969 :
9970 : /* Callback function for our mergesort variant. Determines interval
9971 : overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9972 : op1 > op2. Assumes we're not dealing with the default case.
9973 : We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9974 : There are nine situations to check. */
9975 :
9976 : static int
9977 1578 : compare_cases (const gfc_case *op1, const gfc_case *op2)
9978 : {
9979 1578 : int retval;
9980 :
9981 1578 : if (op1->low == NULL) /* op1 = (:L) */
9982 : {
9983 : /* op2 = (:N), so overlap. */
9984 52 : retval = 0;
9985 : /* op2 = (M:) or (M:N), L < M */
9986 52 : if (op2->low != NULL
9987 52 : && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9988 : retval = -1;
9989 : }
9990 1526 : else if (op1->high == NULL) /* op1 = (K:) */
9991 : {
9992 : /* op2 = (M:), so overlap. */
9993 10 : retval = 0;
9994 : /* op2 = (:N) or (M:N), K > N */
9995 10 : if (op2->high != NULL
9996 10 : && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9997 : retval = 1;
9998 : }
9999 : else /* op1 = (K:L) */
10000 : {
10001 1516 : if (op2->low == NULL) /* op2 = (:N), K > N */
10002 18 : retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
10003 18 : ? 1 : 0;
10004 1498 : else if (op2->high == NULL) /* op2 = (M:), L < M */
10005 14 : retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
10006 10 : ? -1 : 0;
10007 : else /* op2 = (M:N) */
10008 : {
10009 1488 : retval = 0;
10010 : /* L < M */
10011 1488 : if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
10012 : retval = -1;
10013 : /* K > N */
10014 412 : else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
10015 438 : retval = 1;
10016 : }
10017 : }
10018 :
10019 1578 : return retval;
10020 : }
10021 :
10022 :
10023 : /* Merge-sort a double linked case list, detecting overlap in the
10024 : process. LIST is the head of the double linked case list before it
10025 : is sorted. Returns the head of the sorted list if we don't see any
10026 : overlap, or NULL otherwise. */
10027 :
10028 : static gfc_case *
10029 646 : check_case_overlap (gfc_case *list)
10030 : {
10031 646 : gfc_case *p, *q, *e, *tail;
10032 646 : int insize, nmerges, psize, qsize, cmp, overlap_seen;
10033 :
10034 : /* If the passed list was empty, return immediately. */
10035 646 : if (!list)
10036 : return NULL;
10037 :
10038 : overlap_seen = 0;
10039 : insize = 1;
10040 :
10041 : /* Loop unconditionally. The only exit from this loop is a return
10042 : statement, when we've finished sorting the case list. */
10043 1350 : for (;;)
10044 : {
10045 998 : p = list;
10046 998 : list = NULL;
10047 998 : tail = NULL;
10048 :
10049 : /* Count the number of merges we do in this pass. */
10050 998 : nmerges = 0;
10051 :
10052 : /* Loop while there exists a merge to be done. */
10053 2523 : while (p)
10054 : {
10055 1525 : int i;
10056 :
10057 : /* Count this merge. */
10058 1525 : nmerges++;
10059 :
10060 : /* Cut the list in two pieces by stepping INSIZE places
10061 : forward in the list, starting from P. */
10062 1525 : psize = 0;
10063 1525 : q = p;
10064 3208 : for (i = 0; i < insize; i++)
10065 : {
10066 2243 : psize++;
10067 2243 : q = q->right;
10068 2243 : if (!q)
10069 : break;
10070 : }
10071 : qsize = insize;
10072 :
10073 : /* Now we have two lists. Merge them! */
10074 5013 : while (psize > 0 || (qsize > 0 && q != NULL))
10075 : {
10076 : /* See from which the next case to merge comes from. */
10077 807 : if (psize == 0)
10078 : {
10079 : /* P is empty so the next case must come from Q. */
10080 807 : e = q;
10081 807 : q = q->right;
10082 807 : qsize--;
10083 : }
10084 2681 : else if (qsize == 0 || q == NULL)
10085 : {
10086 : /* Q is empty. */
10087 1103 : e = p;
10088 1103 : p = p->right;
10089 1103 : psize--;
10090 : }
10091 : else
10092 : {
10093 1578 : cmp = compare_cases (p, q);
10094 1578 : if (cmp < 0)
10095 : {
10096 : /* The whole case range for P is less than the
10097 : one for Q. */
10098 1136 : e = p;
10099 1136 : p = p->right;
10100 1136 : psize--;
10101 : }
10102 442 : else if (cmp > 0)
10103 : {
10104 : /* The whole case range for Q is greater than
10105 : the case range for P. */
10106 438 : e = q;
10107 438 : q = q->right;
10108 438 : qsize--;
10109 : }
10110 : else
10111 : {
10112 : /* The cases overlap, or they are the same
10113 : element in the list. Either way, we must
10114 : issue an error and get the next case from P. */
10115 : /* FIXME: Sort P and Q by line number. */
10116 4 : gfc_error ("CASE label at %L overlaps with CASE "
10117 : "label at %L", &p->where, &q->where);
10118 4 : overlap_seen = 1;
10119 4 : e = p;
10120 4 : p = p->right;
10121 4 : psize--;
10122 : }
10123 : }
10124 :
10125 : /* Add the next element to the merged list. */
10126 3488 : if (tail)
10127 2490 : tail->right = e;
10128 : else
10129 : list = e;
10130 3488 : e->left = tail;
10131 3488 : tail = e;
10132 : }
10133 :
10134 : /* P has now stepped INSIZE places along, and so has Q. So
10135 : they're the same. */
10136 : p = q;
10137 : }
10138 998 : tail->right = NULL;
10139 :
10140 : /* If we have done only one merge or none at all, we've
10141 : finished sorting the cases. */
10142 998 : if (nmerges <= 1)
10143 : {
10144 646 : if (!overlap_seen)
10145 : return list;
10146 : else
10147 : return NULL;
10148 : }
10149 :
10150 : /* Otherwise repeat, merging lists twice the size. */
10151 352 : insize *= 2;
10152 352 : }
10153 : }
10154 :
10155 :
10156 : /* Check to see if an expression is suitable for use in a CASE statement.
10157 : Makes sure that all case expressions are scalar constants of the same
10158 : type. Return false if anything is wrong. */
10159 :
10160 : static bool
10161 3307 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
10162 : {
10163 3307 : if (e == NULL) return true;
10164 :
10165 3214 : if (e->ts.type != case_expr->ts.type)
10166 : {
10167 4 : gfc_error ("Expression in CASE statement at %L must be of type %s",
10168 : &e->where, gfc_basic_typename (case_expr->ts.type));
10169 4 : return false;
10170 : }
10171 :
10172 : /* C805 (R808) For a given case-construct, each case-value shall be of
10173 : the same type as case-expr. For character type, length differences
10174 : are allowed, but the kind type parameters shall be the same. */
10175 :
10176 3210 : if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
10177 : {
10178 4 : gfc_error ("Expression in CASE statement at %L must be of kind %d",
10179 : &e->where, case_expr->ts.kind);
10180 4 : return false;
10181 : }
10182 :
10183 : /* Convert the case value kind to that of case expression kind,
10184 : if needed */
10185 :
10186 3206 : if (e->ts.kind != case_expr->ts.kind)
10187 14 : gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
10188 :
10189 3206 : if (e->rank != 0)
10190 : {
10191 0 : gfc_error ("Expression in CASE statement at %L must be scalar",
10192 : &e->where);
10193 0 : return false;
10194 : }
10195 :
10196 : return true;
10197 : }
10198 :
10199 :
10200 : /* Given a completely parsed select statement, we:
10201 :
10202 : - Validate all expressions and code within the SELECT.
10203 : - Make sure that the selection expression is not of the wrong type.
10204 : - Make sure that no case ranges overlap.
10205 : - Eliminate unreachable cases and unreachable code resulting from
10206 : removing case labels.
10207 :
10208 : The standard does allow unreachable cases, e.g. CASE (5:3). But
10209 : they are a hassle for code generation, and to prevent that, we just
10210 : cut them out here. This is not necessary for overlapping cases
10211 : because they are illegal and we never even try to generate code.
10212 :
10213 : We have the additional caveat that a SELECT construct could have
10214 : been a computed GOTO in the source code. Fortunately we can fairly
10215 : easily work around that here: The case_expr for a "real" SELECT CASE
10216 : is in code->expr1, but for a computed GOTO it is in code->expr2. All
10217 : we have to do is make sure that the case_expr is a scalar integer
10218 : expression. */
10219 :
10220 : static void
10221 687 : resolve_select (gfc_code *code, bool select_type)
10222 : {
10223 687 : gfc_code *body;
10224 687 : gfc_expr *case_expr;
10225 687 : gfc_case *cp, *default_case, *tail, *head;
10226 687 : int seen_unreachable;
10227 687 : int seen_logical;
10228 687 : int ncases;
10229 687 : bt type;
10230 687 : bool t;
10231 :
10232 687 : if (code->expr1 == NULL)
10233 : {
10234 : /* This was actually a computed GOTO statement. */
10235 5 : case_expr = code->expr2;
10236 5 : if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
10237 3 : gfc_error ("Selection expression in computed GOTO statement "
10238 : "at %L must be a scalar integer expression",
10239 : &case_expr->where);
10240 :
10241 : /* Further checking is not necessary because this SELECT was built
10242 : by the compiler, so it should always be OK. Just move the
10243 : case_expr from expr2 to expr so that we can handle computed
10244 : GOTOs as normal SELECTs from here on. */
10245 5 : code->expr1 = code->expr2;
10246 5 : code->expr2 = NULL;
10247 5 : return;
10248 : }
10249 :
10250 682 : case_expr = code->expr1;
10251 682 : type = case_expr->ts.type;
10252 :
10253 : /* F08:C830. */
10254 682 : if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
10255 6 : && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
10256 :
10257 : {
10258 0 : gfc_error ("Argument of SELECT statement at %L cannot be %s",
10259 : &case_expr->where, gfc_typename (case_expr));
10260 :
10261 : /* Punt. Going on here just produce more garbage error messages. */
10262 0 : return;
10263 : }
10264 :
10265 : /* F08:R842. */
10266 682 : if (!select_type && case_expr->rank != 0)
10267 : {
10268 1 : gfc_error ("Argument of SELECT statement at %L must be a scalar "
10269 : "expression", &case_expr->where);
10270 :
10271 : /* Punt. */
10272 1 : return;
10273 : }
10274 :
10275 : /* Raise a warning if an INTEGER case value exceeds the range of
10276 : the case-expr. Later, all expressions will be promoted to the
10277 : largest kind of all case-labels. */
10278 :
10279 681 : if (type == BT_INTEGER)
10280 1927 : for (body = code->block; body; body = body->block)
10281 2852 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10282 : {
10283 1462 : if (cp->low
10284 1462 : && gfc_check_integer_range (cp->low->value.integer,
10285 : case_expr->ts.kind) != ARITH_OK)
10286 6 : gfc_warning (0, "Expression in CASE statement at %L is "
10287 6 : "not in the range of %s", &cp->low->where,
10288 : gfc_typename (case_expr));
10289 :
10290 1462 : if (cp->high
10291 1178 : && cp->low != cp->high
10292 1570 : && gfc_check_integer_range (cp->high->value.integer,
10293 : case_expr->ts.kind) != ARITH_OK)
10294 0 : gfc_warning (0, "Expression in CASE statement at %L is "
10295 0 : "not in the range of %s", &cp->high->where,
10296 : gfc_typename (case_expr));
10297 : }
10298 :
10299 : /* PR 19168 has a long discussion concerning a mismatch of the kinds
10300 : of the SELECT CASE expression and its CASE values. Walk the lists
10301 : of case values, and if we find a mismatch, promote case_expr to
10302 : the appropriate kind. */
10303 :
10304 681 : if (type == BT_LOGICAL || type == BT_INTEGER)
10305 : {
10306 2113 : for (body = code->block; body; body = body->block)
10307 : {
10308 : /* Walk the case label list. */
10309 3113 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10310 : {
10311 : /* Intercept the DEFAULT case. It does not have a kind. */
10312 1597 : if (cp->low == NULL && cp->high == NULL)
10313 292 : continue;
10314 :
10315 : /* Unreachable case ranges are discarded, so ignore. */
10316 1260 : if (cp->low != NULL && cp->high != NULL
10317 1212 : && cp->low != cp->high
10318 1370 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10319 33 : continue;
10320 :
10321 1272 : if (cp->low != NULL
10322 1272 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
10323 17 : gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
10324 :
10325 1272 : if (cp->high != NULL
10326 1272 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
10327 4 : gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
10328 : }
10329 : }
10330 : }
10331 :
10332 : /* Assume there is no DEFAULT case. */
10333 681 : default_case = NULL;
10334 681 : head = tail = NULL;
10335 681 : ncases = 0;
10336 681 : seen_logical = 0;
10337 :
10338 2502 : for (body = code->block; body; body = body->block)
10339 : {
10340 : /* Assume the CASE list is OK, and all CASE labels can be matched. */
10341 1821 : t = true;
10342 1821 : seen_unreachable = 0;
10343 :
10344 : /* Walk the case label list, making sure that all case labels
10345 : are legal. */
10346 3829 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10347 : {
10348 : /* Count the number of cases in the whole construct. */
10349 2019 : ncases++;
10350 :
10351 : /* Intercept the DEFAULT case. */
10352 2019 : if (cp->low == NULL && cp->high == NULL)
10353 : {
10354 362 : if (default_case != NULL)
10355 : {
10356 0 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10357 : "by a second DEFAULT CASE at %L",
10358 : &default_case->where, &cp->where);
10359 0 : t = false;
10360 0 : break;
10361 : }
10362 : else
10363 : {
10364 362 : default_case = cp;
10365 362 : continue;
10366 : }
10367 : }
10368 :
10369 : /* Deal with single value cases and case ranges. Errors are
10370 : issued from the validation function. */
10371 1657 : if (!validate_case_label_expr (cp->low, case_expr)
10372 1657 : || !validate_case_label_expr (cp->high, case_expr))
10373 : {
10374 : t = false;
10375 : break;
10376 : }
10377 :
10378 1649 : if (type == BT_LOGICAL
10379 78 : && ((cp->low == NULL || cp->high == NULL)
10380 76 : || cp->low != cp->high))
10381 : {
10382 2 : gfc_error ("Logical range in CASE statement at %L is not "
10383 : "allowed",
10384 1 : cp->low ? &cp->low->where : &cp->high->where);
10385 2 : t = false;
10386 2 : break;
10387 : }
10388 :
10389 76 : if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
10390 : {
10391 76 : int value;
10392 76 : value = cp->low->value.logical == 0 ? 2 : 1;
10393 76 : if (value & seen_logical)
10394 : {
10395 1 : gfc_error ("Constant logical value in CASE statement "
10396 : "is repeated at %L",
10397 : &cp->low->where);
10398 1 : t = false;
10399 1 : break;
10400 : }
10401 75 : seen_logical |= value;
10402 : }
10403 :
10404 1602 : if (cp->low != NULL && cp->high != NULL
10405 1555 : && cp->low != cp->high
10406 1758 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10407 : {
10408 35 : if (warn_surprising)
10409 1 : gfc_warning (OPT_Wsurprising,
10410 : "Range specification at %L can never be matched",
10411 : &cp->where);
10412 :
10413 35 : cp->unreachable = 1;
10414 35 : seen_unreachable = 1;
10415 : }
10416 : else
10417 : {
10418 : /* If the case range can be matched, it can also overlap with
10419 : other cases. To make sure it does not, we put it in a
10420 : double linked list here. We sort that with a merge sort
10421 : later on to detect any overlapping cases. */
10422 1611 : if (!head)
10423 : {
10424 646 : head = tail = cp;
10425 646 : head->right = head->left = NULL;
10426 : }
10427 : else
10428 : {
10429 965 : tail->right = cp;
10430 965 : tail->right->left = tail;
10431 965 : tail = tail->right;
10432 965 : tail->right = NULL;
10433 : }
10434 : }
10435 : }
10436 :
10437 : /* It there was a failure in the previous case label, give up
10438 : for this case label list. Continue with the next block. */
10439 1821 : if (!t)
10440 11 : continue;
10441 :
10442 : /* See if any case labels that are unreachable have been seen.
10443 : If so, we eliminate them. This is a bit of a kludge because
10444 : the case lists for a single case statement (label) is a
10445 : single forward linked lists. */
10446 1810 : if (seen_unreachable)
10447 : {
10448 : /* Advance until the first case in the list is reachable. */
10449 69 : while (body->ext.block.case_list != NULL
10450 69 : && body->ext.block.case_list->unreachable)
10451 : {
10452 34 : gfc_case *n = body->ext.block.case_list;
10453 34 : body->ext.block.case_list = body->ext.block.case_list->next;
10454 34 : n->next = NULL;
10455 34 : gfc_free_case_list (n);
10456 : }
10457 :
10458 : /* Strip all other unreachable cases. */
10459 35 : if (body->ext.block.case_list)
10460 : {
10461 2 : for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10462 : {
10463 1 : if (cp->next->unreachable)
10464 : {
10465 1 : gfc_case *n = cp->next;
10466 1 : cp->next = cp->next->next;
10467 1 : n->next = NULL;
10468 1 : gfc_free_case_list (n);
10469 : }
10470 : }
10471 : }
10472 : }
10473 : }
10474 :
10475 : /* See if there were overlapping cases. If the check returns NULL,
10476 : there was overlap. In that case we don't do anything. If head
10477 : is non-NULL, we prepend the DEFAULT case. The sorted list can
10478 : then used during code generation for SELECT CASE constructs with
10479 : a case expression of a CHARACTER type. */
10480 681 : if (head)
10481 : {
10482 646 : head = check_case_overlap (head);
10483 :
10484 : /* Prepend the default_case if it is there. */
10485 646 : if (head != NULL && default_case)
10486 : {
10487 345 : default_case->left = NULL;
10488 345 : default_case->right = head;
10489 345 : head->left = default_case;
10490 : }
10491 : }
10492 :
10493 : /* Eliminate dead blocks that may be the result if we've seen
10494 : unreachable case labels for a block. */
10495 2468 : for (body = code; body && body->block; body = body->block)
10496 : {
10497 1787 : if (body->block->ext.block.case_list == NULL)
10498 : {
10499 : /* Cut the unreachable block from the code chain. */
10500 34 : gfc_code *c = body->block;
10501 34 : body->block = c->block;
10502 :
10503 : /* Kill the dead block, but not the blocks below it. */
10504 34 : c->block = NULL;
10505 34 : gfc_free_statements (c);
10506 : }
10507 : }
10508 :
10509 : /* More than two cases is legal but insane for logical selects.
10510 : Issue a warning for it. */
10511 681 : if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10512 0 : gfc_warning (OPT_Wsurprising,
10513 : "Logical SELECT CASE block at %L has more that two cases",
10514 : &code->loc);
10515 : }
10516 :
10517 :
10518 : /* Check if a derived type is extensible. */
10519 :
10520 : bool
10521 24055 : gfc_type_is_extensible (gfc_symbol *sym)
10522 : {
10523 24055 : return !(sym->attr.is_bind_c || sym->attr.sequence
10524 24039 : || (sym->attr.is_class
10525 2208 : && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10526 : }
10527 :
10528 :
10529 : static void
10530 : resolve_types (gfc_namespace *ns);
10531 :
10532 : /* Resolve an associate-name: Resolve target and ensure the type-spec is
10533 : correct as well as possibly the array-spec. */
10534 :
10535 : static void
10536 12851 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10537 : {
10538 12851 : gfc_expr* target;
10539 12851 : bool parentheses = false;
10540 :
10541 12851 : gcc_assert (sym->assoc);
10542 12851 : gcc_assert (sym->attr.flavor == FL_VARIABLE);
10543 :
10544 12851 : if (sym->assoc->target
10545 7673 : && sym->assoc->target->expr_type == EXPR_FUNCTION
10546 552 : && sym->assoc->target->symtree
10547 552 : && sym->assoc->target->symtree->n.sym
10548 552 : && sym->assoc->target->symtree->n.sym->attr.generic)
10549 : {
10550 33 : if (gfc_resolve_expr (sym->assoc->target))
10551 33 : sym->ts = sym->assoc->target->ts;
10552 : else
10553 : {
10554 0 : gfc_error ("%s could not be resolved to a specific function at %L",
10555 0 : sym->assoc->target->symtree->n.sym->name,
10556 0 : &sym->assoc->target->where);
10557 0 : return;
10558 : }
10559 : }
10560 :
10561 : /* If this is for SELECT TYPE, the target may not yet be set. In that
10562 : case, return. Resolution will be called later manually again when
10563 : this is done. */
10564 12851 : target = sym->assoc->target;
10565 12851 : if (!target)
10566 : return;
10567 7673 : gcc_assert (!sym->assoc->dangling);
10568 :
10569 7673 : if (target->expr_type == EXPR_OP
10570 261 : && target->value.op.op == INTRINSIC_PARENTHESES
10571 42 : && target->value.op.op1->expr_type == EXPR_VARIABLE)
10572 : {
10573 23 : sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10574 23 : gfc_free_expr (target);
10575 23 : target = sym->assoc->target;
10576 23 : parentheses = true;
10577 : }
10578 :
10579 7673 : if (resolve_target && !gfc_resolve_expr (target))
10580 : return;
10581 :
10582 7668 : if (sym->assoc->ar)
10583 : {
10584 : int dim;
10585 : gfc_array_ref *ar = sym->assoc->ar;
10586 68 : for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10587 : {
10588 39 : if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10589 39 : && ar->start[dim]->ts.type == BT_INTEGER)
10590 78 : || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10591 39 : && ar->end[dim]->ts.type == BT_INTEGER))
10592 0 : gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10593 : "remapping of associate name %s at %L",
10594 : sym->name, &sym->declared_at);
10595 : }
10596 : }
10597 :
10598 : /* For variable targets, we get some attributes from the target. */
10599 7668 : if (target->expr_type == EXPR_VARIABLE)
10600 : {
10601 6671 : gfc_symbol *tsym, *dsym;
10602 :
10603 6671 : gcc_assert (target->symtree);
10604 6671 : tsym = target->symtree->n.sym;
10605 :
10606 6671 : if (gfc_expr_attr (target).proc_pointer)
10607 : {
10608 0 : gfc_error ("Associating entity %qs at %L is a procedure pointer",
10609 : tsym->name, &target->where);
10610 0 : return;
10611 : }
10612 :
10613 74 : if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10614 2 : && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10615 6672 : && dsym->attr.flavor == FL_DERIVED)
10616 : {
10617 1 : gfc_error ("Derived type %qs cannot be used as a variable at %L",
10618 : tsym->name, &target->where);
10619 1 : return;
10620 : }
10621 :
10622 6670 : if (tsym->attr.flavor == FL_PROCEDURE)
10623 : {
10624 73 : bool is_error = true;
10625 73 : if (tsym->attr.function && tsym->result == tsym)
10626 141 : for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10627 137 : if (tsym == ns->proc_name)
10628 : {
10629 : is_error = false;
10630 : break;
10631 : }
10632 64 : if (is_error)
10633 : {
10634 13 : gfc_error ("Associating entity %qs at %L is a procedure name",
10635 : tsym->name, &target->where);
10636 13 : return;
10637 : }
10638 : }
10639 :
10640 6657 : sym->attr.asynchronous = tsym->attr.asynchronous;
10641 6657 : sym->attr.volatile_ = tsym->attr.volatile_;
10642 :
10643 13314 : sym->attr.target = tsym->attr.target
10644 6657 : || gfc_expr_attr (target).pointer;
10645 6657 : if (is_subref_array (target))
10646 402 : sym->attr.subref_array_pointer = 1;
10647 : }
10648 997 : else if (target->ts.type == BT_PROCEDURE)
10649 : {
10650 0 : gfc_error ("Associating selector-expression at %L yields a procedure",
10651 : &target->where);
10652 0 : return;
10653 : }
10654 :
10655 7654 : if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10656 : {
10657 : /* By now, the type of the target has been fixed up. */
10658 299 : symbol_attribute attr;
10659 :
10660 299 : if (sym->ts.type == BT_DERIVED
10661 166 : && target->ts.type == BT_CLASS
10662 31 : && !UNLIMITED_POLY (target))
10663 : {
10664 : /* Inferred to be derived type but the target has type class. */
10665 31 : sym->ts = CLASS_DATA (target)->ts;
10666 31 : if (!sym->as)
10667 31 : sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10668 31 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10669 31 : sym->attr.dimension = target->rank ? 1 : 0;
10670 31 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10671 : target->corank);
10672 31 : sym->as = NULL;
10673 : }
10674 268 : else if (target->ts.type == BT_DERIVED
10675 135 : && target->symtree && target->symtree->n.sym
10676 111 : && target->symtree->n.sym->ts.type == BT_CLASS
10677 0 : && IS_INFERRED_TYPE (target)
10678 0 : && target->ref && target->ref->next
10679 0 : && target->ref->next->type == REF_ARRAY
10680 0 : && !target->ref->next->next)
10681 : {
10682 : /* A inferred type selector whose symbol has been determined to be
10683 : a class array but which only has an array reference. Change the
10684 : associate name and the selector to class type. */
10685 0 : sym->ts = target->ts;
10686 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10687 0 : sym->attr.dimension = target->rank ? 1 : 0;
10688 0 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10689 : target->corank);
10690 0 : sym->as = NULL;
10691 0 : target->ts = sym->ts;
10692 : }
10693 268 : else if ((target->ts.type == BT_DERIVED)
10694 133 : || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10695 61 : && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10696 : /* Confirmed to be either a derived type or misidentified to be a
10697 : scalar class object, when the selector is a class array. */
10698 141 : sym->ts = target->ts;
10699 127 : else if (sym->assoc->inferred_type
10700 120 : && (sym->ts.type == BT_COMPLEX
10701 78 : || sym->ts.type == BT_CHARACTER)
10702 66 : && target->ts.type == sym->ts.type
10703 66 : && sym->ts.kind != target->ts.kind)
10704 : /* The inferred type was set from a %re, %im or %len inquiry on
10705 : the associate name with the default kind, before the target's
10706 : actual type was known. Now that the target has been resolved,
10707 : update the kind to match. */
10708 6 : sym->ts = target->ts;
10709 : }
10710 :
10711 :
10712 7654 : if (target->expr_type == EXPR_NULL)
10713 : {
10714 1 : gfc_error ("Selector at %L cannot be NULL()", &target->where);
10715 1 : return;
10716 : }
10717 7653 : else if (target->ts.type == BT_UNKNOWN)
10718 : {
10719 2 : gfc_error ("Selector at %L has no type", &target->where);
10720 2 : return;
10721 : }
10722 :
10723 : /* Get type if this was not already set. Note that it can be
10724 : some other type than the target in case this is a SELECT TYPE
10725 : selector! So we must not update when the type is already there. */
10726 7651 : if (sym->ts.type == BT_UNKNOWN)
10727 258 : sym->ts = target->ts;
10728 :
10729 7651 : gcc_assert (sym->ts.type != BT_UNKNOWN);
10730 :
10731 : /* See if this is a valid association-to-variable. */
10732 15302 : sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10733 6657 : && !parentheses
10734 6636 : && !gfc_has_vector_subscript (target))
10735 7699 : || gfc_is_ptr_fcn (target));
10736 :
10737 : /* Finally resolve if this is an array or not. */
10738 7651 : if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10739 191 : && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10740 : {
10741 109 : gfc_expression_rank (target);
10742 109 : if (target->ts.type == BT_DERIVED
10743 62 : && !sym->as
10744 62 : && target->symtree->n.sym->as)
10745 : {
10746 0 : sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10747 0 : sym->attr.dimension = 1;
10748 : }
10749 109 : else if (target->ts.type == BT_CLASS
10750 47 : && CLASS_DATA (target)->as)
10751 : {
10752 0 : target->rank = CLASS_DATA (target)->as->rank;
10753 0 : target->corank = CLASS_DATA (target)->as->corank;
10754 0 : if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10755 : {
10756 0 : sym->ts = target->ts;
10757 0 : sym->attr.dimension = 0;
10758 : }
10759 : }
10760 : }
10761 :
10762 :
10763 7651 : if (sym->attr.dimension && target->rank == 0)
10764 : {
10765 : /* primary.cc makes the assumption that a reference to an associate
10766 : name followed by a left parenthesis is an array reference. */
10767 17 : if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10768 : {
10769 12 : gfc_expression_rank (sym->assoc->target);
10770 12 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10771 12 : if (!sym->attr.dimension && sym->as)
10772 0 : sym->as = NULL;
10773 : }
10774 :
10775 17 : if (sym->attr.dimension && target->rank == 0)
10776 : {
10777 5 : if (sym->ts.type != BT_CHARACTER)
10778 5 : gfc_error ("Associate-name %qs at %L is used as array",
10779 : sym->name, &sym->declared_at);
10780 5 : sym->attr.dimension = 0;
10781 5 : return;
10782 : }
10783 : }
10784 :
10785 : /* We cannot deal with class selectors that need temporaries. */
10786 7646 : if (target->ts.type == BT_CLASS
10787 7646 : && gfc_ref_needs_temporary_p (target->ref))
10788 : {
10789 1 : gfc_error ("CLASS selector at %L needs a temporary which is not "
10790 : "yet implemented", &target->where);
10791 1 : return;
10792 : }
10793 :
10794 7645 : if (target->ts.type == BT_CLASS)
10795 2821 : gfc_fix_class_refs (target);
10796 :
10797 7645 : if ((target->rank > 0 || target->corank > 0)
10798 2744 : && !sym->attr.select_rank_temporary)
10799 : {
10800 2744 : gfc_array_spec *as;
10801 : /* The rank may be incorrectly guessed at parsing, therefore make sure
10802 : it is corrected now. */
10803 2744 : if (sym->ts.type != BT_CLASS
10804 2162 : && (!sym->as || sym->as->corank != target->corank))
10805 : {
10806 141 : if (!sym->as)
10807 134 : sym->as = gfc_get_array_spec ();
10808 141 : as = sym->as;
10809 141 : as->rank = target->rank;
10810 141 : as->type = AS_DEFERRED;
10811 141 : as->corank = target->corank;
10812 141 : sym->attr.dimension = 1;
10813 141 : if (as->corank != 0)
10814 7 : sym->attr.codimension = 1;
10815 : }
10816 2603 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10817 581 : && (!CLASS_DATA (sym)->as
10818 581 : || CLASS_DATA (sym)->as->corank != target->corank))
10819 : {
10820 0 : if (!CLASS_DATA (sym)->as)
10821 0 : CLASS_DATA (sym)->as = gfc_get_array_spec ();
10822 0 : as = CLASS_DATA (sym)->as;
10823 0 : as->rank = target->rank;
10824 0 : as->type = AS_DEFERRED;
10825 0 : as->corank = target->corank;
10826 0 : CLASS_DATA (sym)->attr.dimension = 1;
10827 0 : if (as->corank != 0)
10828 0 : CLASS_DATA (sym)->attr.codimension = 1;
10829 : }
10830 : }
10831 4901 : else if (!sym->attr.select_rank_temporary)
10832 : {
10833 : /* target's rank is 0, but the type of the sym is still array valued,
10834 : which has to be corrected. */
10835 3512 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10836 724 : && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10837 : {
10838 24 : gfc_array_spec *as;
10839 24 : symbol_attribute attr;
10840 : /* The associated variable's type is still the array type
10841 : correct this now. */
10842 24 : gfc_typespec *ts = &target->ts;
10843 24 : gfc_ref *ref;
10844 : /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10845 : */
10846 24 : bool internal_ref = true;
10847 :
10848 72 : for (ref = target->ref; ref != NULL; ref = ref->next)
10849 : {
10850 48 : switch (ref->type)
10851 : {
10852 24 : case REF_COMPONENT:
10853 24 : ts = &ref->u.c.component->ts;
10854 24 : internal_ref
10855 24 : = target->ref == ref && ref->next
10856 48 : && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10857 : break;
10858 24 : case REF_ARRAY:
10859 24 : if (ts->type == BT_CLASS)
10860 0 : ts = &ts->u.derived->components->ts;
10861 24 : if (internal_ref && ref->u.ar.codimen > 0)
10862 0 : for (int i = ref->u.ar.dimen;
10863 : internal_ref
10864 0 : && i < ref->u.ar.dimen + ref->u.ar.codimen;
10865 : ++i)
10866 0 : internal_ref
10867 0 : = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10868 : break;
10869 : default:
10870 : break;
10871 : }
10872 : }
10873 : /* Only rewrite the type of this symbol, when the refs are not the
10874 : internal ones for class and co-array this-image. */
10875 24 : if (!internal_ref)
10876 : {
10877 : /* Create a scalar instance of the current class type. Because
10878 : the rank of a class array goes into its name, the type has to
10879 : be rebuilt. The alternative of (re-)setting just the
10880 : attributes and as in the current type, destroys the type also
10881 : in other places. */
10882 0 : as = NULL;
10883 0 : sym->ts = *ts;
10884 0 : sym->ts.type = BT_CLASS;
10885 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10886 0 : gfc_change_class (&sym->ts, &attr, as, 0, 0);
10887 0 : sym->as = NULL;
10888 : }
10889 : }
10890 : }
10891 :
10892 : /* Mark this as an associate variable. */
10893 7645 : sym->attr.associate_var = 1;
10894 :
10895 : /* Fix up the type-spec for CHARACTER types. */
10896 7645 : if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10897 : {
10898 503 : gfc_ref *ref;
10899 788 : for (ref = target->ref; ref; ref = ref->next)
10900 311 : if (ref->type == REF_SUBSTRING
10901 74 : && (ref->u.ss.start == NULL
10902 74 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
10903 74 : || ref->u.ss.end == NULL
10904 54 : || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10905 : break;
10906 :
10907 503 : if (!sym->ts.u.cl)
10908 182 : sym->ts.u.cl = target->ts.u.cl;
10909 :
10910 503 : if (sym->ts.deferred
10911 189 : && sym->ts.u.cl == target->ts.u.cl)
10912 : {
10913 110 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10914 110 : sym->ts.deferred = 1;
10915 : }
10916 :
10917 503 : if (!sym->ts.u.cl->length
10918 327 : && !sym->ts.deferred
10919 138 : && target->expr_type == EXPR_CONSTANT)
10920 : {
10921 30 : sym->ts.u.cl->length =
10922 30 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10923 30 : target->value.character.length);
10924 : }
10925 473 : else if (((!sym->ts.u.cl->length
10926 176 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10927 303 : && target->expr_type != EXPR_VARIABLE)
10928 350 : || ref)
10929 : {
10930 149 : if (!sym->ts.deferred)
10931 : {
10932 45 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10933 45 : sym->ts.deferred = 1;
10934 : }
10935 :
10936 : /* This is reset in trans-stmt.cc after the assignment
10937 : of the target expression to the associate name. */
10938 149 : if (ref && sym->as)
10939 26 : sym->attr.pointer = 1;
10940 : else
10941 123 : sym->attr.allocatable = 1;
10942 : }
10943 : }
10944 :
10945 7645 : if (sym->ts.type == BT_CLASS
10946 1451 : && IS_INFERRED_TYPE (target)
10947 13 : && target->ts.type == BT_DERIVED
10948 0 : && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10949 0 : && target->ref && target->ref->next && !target->ref->next->next
10950 0 : && target->ref->next->type == REF_ARRAY)
10951 0 : target->ts = target->symtree->n.sym->ts;
10952 :
10953 : /* If the target is a good class object, so is the associate variable. */
10954 7645 : if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10955 725 : sym->attr.class_ok = 1;
10956 :
10957 : /* If the target is a contiguous pointer, so is the associate variable. */
10958 7645 : if (gfc_expr_attr (target).pointer && gfc_expr_attr (target).contiguous)
10959 3 : sym->attr.contiguous = 1;
10960 : }
10961 :
10962 :
10963 : /* Ensure that SELECT TYPE expressions have the correct rank and a full
10964 : array reference, where necessary. The symbols are artificial and so
10965 : the dimension attribute and arrayspec can also be set. In addition,
10966 : sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10967 : This is corrected here as well.*/
10968 :
10969 : static void
10970 1699 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10971 : gfc_ref *ref)
10972 : {
10973 1699 : gfc_ref *nref = (*expr1)->ref;
10974 1699 : gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10975 1699 : gfc_symbol *sym2;
10976 1699 : gfc_expr *selector = gfc_copy_expr (expr2);
10977 :
10978 1699 : (*expr1)->rank = rank;
10979 1699 : (*expr1)->corank = corank;
10980 1699 : if (selector)
10981 : {
10982 317 : gfc_resolve_expr (selector);
10983 317 : if (selector->expr_type == EXPR_OP
10984 2 : && selector->value.op.op == INTRINSIC_PARENTHESES)
10985 2 : sym2 = selector->value.op.op1->symtree->n.sym;
10986 315 : else if (selector->expr_type == EXPR_VARIABLE
10987 7 : || selector->expr_type == EXPR_FUNCTION)
10988 315 : sym2 = selector->symtree->n.sym;
10989 : else
10990 0 : gcc_unreachable ();
10991 : }
10992 : else
10993 : sym2 = NULL;
10994 :
10995 1699 : if (sym1->ts.type == BT_CLASS)
10996 : {
10997 1699 : if ((*expr1)->ts.type != BT_CLASS)
10998 13 : (*expr1)->ts = sym1->ts;
10999 :
11000 1699 : CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
11001 1699 : CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
11002 1699 : if (CLASS_DATA (sym1)->as == NULL && sym2)
11003 1 : CLASS_DATA (sym1)->as
11004 1 : = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
11005 : }
11006 : else
11007 : {
11008 0 : sym1->attr.dimension = rank > 0 ? 1 : 0;
11009 0 : sym1->attr.codimension = corank > 0 ? 1 : 0;
11010 0 : if (sym1->as == NULL && sym2)
11011 0 : sym1->as = gfc_copy_array_spec (sym2->as);
11012 : }
11013 :
11014 3075 : for (; nref; nref = nref->next)
11015 2758 : if (nref->next == NULL)
11016 : break;
11017 :
11018 1699 : if (ref && nref && nref->type != REF_ARRAY)
11019 6 : nref->next = gfc_copy_ref (ref);
11020 1693 : else if (ref && !nref)
11021 308 : (*expr1)->ref = gfc_copy_ref (ref);
11022 1385 : else if (ref && nref->u.ar.codimen != corank)
11023 : {
11024 976 : for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
11025 915 : nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
11026 61 : nref->u.ar.codimen = corank;
11027 : }
11028 1699 : }
11029 :
11030 :
11031 : static gfc_expr *
11032 6788 : build_loc_call (gfc_expr *sym_expr)
11033 : {
11034 6788 : gfc_expr *loc_call;
11035 6788 : loc_call = gfc_get_expr ();
11036 6788 : loc_call->expr_type = EXPR_FUNCTION;
11037 6788 : gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
11038 6788 : loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
11039 6788 : loc_call->symtree->n.sym->attr.intrinsic = 1;
11040 6788 : loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
11041 6788 : gfc_commit_symbol (loc_call->symtree->n.sym);
11042 6788 : loc_call->ts.type = BT_INTEGER;
11043 6788 : loc_call->ts.kind = gfc_index_integer_kind;
11044 6788 : loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
11045 6788 : loc_call->value.function.actual = gfc_get_actual_arglist ();
11046 6788 : loc_call->value.function.actual->expr = sym_expr;
11047 6788 : loc_call->where = sym_expr->where;
11048 6788 : return loc_call;
11049 : }
11050 :
11051 : /* Resolve a SELECT TYPE statement. */
11052 :
11053 : static void
11054 3047 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
11055 : {
11056 3047 : gfc_symbol *selector_type;
11057 3047 : gfc_code *body, *new_st, *if_st, *tail;
11058 3047 : gfc_code *class_is = NULL, *default_case = NULL;
11059 3047 : gfc_case *c;
11060 3047 : gfc_symtree *st;
11061 3047 : char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
11062 3047 : gfc_namespace *ns;
11063 3047 : int error = 0;
11064 3047 : int rank = 0, corank = 0;
11065 3047 : gfc_ref* ref = NULL;
11066 3047 : gfc_expr *selector_expr = NULL;
11067 3047 : gfc_code *old_code = code;
11068 :
11069 3047 : ns = code->ext.block.ns;
11070 3047 : if (code->expr2)
11071 : {
11072 : /* Set this, or coarray checks in resolve will fail. */
11073 657 : code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
11074 : }
11075 3047 : gfc_resolve (ns);
11076 :
11077 : /* Check for F03:C813. */
11078 3047 : if (code->expr1->ts.type != BT_CLASS
11079 36 : && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
11080 : {
11081 13 : gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
11082 : "at %L", &code->loc);
11083 42 : return;
11084 : }
11085 :
11086 : /* Prevent segfault, when class type is not initialized due to previous
11087 : error. */
11088 3034 : if (!code->expr1->symtree->n.sym->attr.class_ok
11089 3032 : || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
11090 : return;
11091 :
11092 3027 : if (code->expr2)
11093 : {
11094 648 : gfc_ref *ref2 = NULL;
11095 1496 : for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
11096 848 : if (ref->type == REF_COMPONENT
11097 432 : && ref->u.c.component->ts.type == BT_CLASS)
11098 848 : ref2 = ref;
11099 :
11100 648 : if (ref2)
11101 : {
11102 340 : if (code->expr1->symtree->n.sym->attr.untyped)
11103 1 : code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
11104 340 : selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
11105 : }
11106 : else
11107 : {
11108 308 : if (code->expr1->symtree->n.sym->attr.untyped)
11109 28 : code->expr1->symtree->n.sym->ts = code->expr2->ts;
11110 : /* Sometimes the selector expression is given the typespec of the
11111 : '_data' field, which is logical enough but inappropriate here. */
11112 308 : if (code->expr2->ts.type == BT_DERIVED
11113 73 : && code->expr2->symtree
11114 73 : && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
11115 73 : code->expr2->ts = code->expr2->symtree->n.sym->ts;
11116 308 : selector_type = CLASS_DATA (code->expr2)
11117 : ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
11118 : }
11119 :
11120 648 : if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
11121 : {
11122 303 : CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
11123 303 : CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
11124 303 : CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
11125 : }
11126 :
11127 : /* F2008: C803 The selector expression must not be coindexed. */
11128 648 : if (gfc_is_coindexed (code->expr2))
11129 : {
11130 4 : gfc_error ("Selector at %L must not be coindexed",
11131 4 : &code->expr2->where);
11132 4 : return;
11133 : }
11134 :
11135 : }
11136 : else
11137 : {
11138 2379 : selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
11139 :
11140 2379 : if (gfc_is_coindexed (code->expr1))
11141 : {
11142 0 : gfc_error ("Selector at %L must not be coindexed",
11143 0 : &code->expr1->where);
11144 0 : return;
11145 : }
11146 : }
11147 :
11148 : /* Loop over TYPE IS / CLASS IS cases. */
11149 8433 : for (body = code->block; body; body = body->block)
11150 : {
11151 5411 : c = body->ext.block.case_list;
11152 :
11153 5411 : if (!error)
11154 : {
11155 : /* Check for repeated cases. */
11156 8394 : for (tail = code->block; tail; tail = tail->block)
11157 : {
11158 8394 : gfc_case *d = tail->ext.block.case_list;
11159 8394 : if (tail == body)
11160 : break;
11161 :
11162 2992 : if (c->ts.type == d->ts.type
11163 516 : && ((c->ts.type == BT_DERIVED
11164 418 : && c->ts.u.derived && d->ts.u.derived
11165 418 : && !strcmp (c->ts.u.derived->name,
11166 : d->ts.u.derived->name))
11167 515 : || c->ts.type == BT_UNKNOWN
11168 515 : || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11169 55 : && c->ts.kind == d->ts.kind)))
11170 : {
11171 1 : gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
11172 : &c->where, &d->where);
11173 1 : return;
11174 : }
11175 : }
11176 : }
11177 :
11178 : /* Check F03:C815. */
11179 3422 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11180 2336 : && selector_type
11181 2336 : && !selector_type->attr.unlimited_polymorphic
11182 7425 : && !gfc_type_is_extensible (c->ts.u.derived))
11183 : {
11184 1 : gfc_error ("Derived type %qs at %L must be extensible",
11185 1 : c->ts.u.derived->name, &c->where);
11186 1 : error++;
11187 1 : continue;
11188 : }
11189 :
11190 : /* Check F03:C816. */
11191 5415 : if (c->ts.type != BT_UNKNOWN
11192 3781 : && selector_type && !selector_type->attr.unlimited_polymorphic
11193 7427 : && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
11194 2014 : || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
11195 : {
11196 6 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11197 2 : gfc_error ("Derived type %qs at %L must be an extension of %qs",
11198 2 : c->ts.u.derived->name, &c->where, selector_type->name);
11199 : else
11200 4 : gfc_error ("Unexpected intrinsic type %qs at %L",
11201 : gfc_basic_typename (c->ts.type), &c->where);
11202 6 : error++;
11203 6 : continue;
11204 : }
11205 :
11206 : /* Check F03:C814. */
11207 5403 : if (c->ts.type == BT_CHARACTER
11208 736 : && (c->ts.u.cl->length != NULL || c->ts.deferred))
11209 : {
11210 0 : gfc_error ("The type-spec at %L shall specify that each length "
11211 : "type parameter is assumed", &c->where);
11212 0 : error++;
11213 0 : continue;
11214 : }
11215 :
11216 : /* Intercept the DEFAULT case. */
11217 5403 : if (c->ts.type == BT_UNKNOWN)
11218 : {
11219 : /* Check F03:C818. */
11220 1628 : if (default_case)
11221 : {
11222 1 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
11223 : "by a second DEFAULT CASE at %L",
11224 1 : &default_case->ext.block.case_list->where, &c->where);
11225 1 : error++;
11226 1 : continue;
11227 : }
11228 :
11229 : default_case = body;
11230 : }
11231 : }
11232 :
11233 3022 : if (error > 0)
11234 : return;
11235 :
11236 : /* Transform SELECT TYPE statement to BLOCK and associate selector to
11237 : target if present. If there are any EXIT statements referring to the
11238 : SELECT TYPE construct, this is no problem because the gfc_code
11239 : reference stays the same and EXIT is equally possible from the BLOCK
11240 : it is changed to. */
11241 3019 : code->op = EXEC_BLOCK;
11242 3019 : if (code->expr2)
11243 : {
11244 644 : gfc_association_list* assoc;
11245 :
11246 644 : assoc = gfc_get_association_list ();
11247 644 : assoc->st = code->expr1->symtree;
11248 644 : assoc->target = gfc_copy_expr (code->expr2);
11249 644 : assoc->target->where = code->expr2->where;
11250 : /* assoc->variable will be set by resolve_assoc_var. */
11251 :
11252 644 : code->ext.block.assoc = assoc;
11253 644 : code->expr1->symtree->n.sym->assoc = assoc;
11254 :
11255 644 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11256 : }
11257 : else
11258 2375 : code->ext.block.assoc = NULL;
11259 :
11260 : /* Ensure that the selector rank and arrayspec are available to
11261 : correct expressions in which they might be missing. */
11262 3019 : if (code->expr2 && (code->expr2->rank || code->expr2->corank))
11263 : {
11264 317 : rank = code->expr2->rank;
11265 317 : corank = code->expr2->corank;
11266 591 : for (ref = code->expr2->ref; ref; ref = ref->next)
11267 582 : if (ref->next == NULL)
11268 : break;
11269 317 : if (ref && ref->type == REF_ARRAY)
11270 308 : ref = gfc_copy_ref (ref);
11271 :
11272 : /* Fixup expr1 if necessary. */
11273 317 : if (rank || corank)
11274 317 : fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
11275 : }
11276 2702 : else if (code->expr1->rank || code->expr1->corank)
11277 : {
11278 884 : rank = code->expr1->rank;
11279 884 : corank = code->expr1->corank;
11280 884 : for (ref = code->expr1->ref; ref; ref = ref->next)
11281 884 : if (ref->next == NULL)
11282 : break;
11283 884 : if (ref && ref->type == REF_ARRAY)
11284 884 : ref = gfc_copy_ref (ref);
11285 : }
11286 :
11287 3019 : gfc_expr *orig_expr1 = code->expr1;
11288 :
11289 : /* Add EXEC_SELECT to switch on type. */
11290 3019 : new_st = gfc_get_code (code->op);
11291 3019 : new_st->expr1 = code->expr1;
11292 3019 : new_st->expr2 = code->expr2;
11293 3019 : new_st->block = code->block;
11294 3019 : code->expr1 = code->expr2 = NULL;
11295 3019 : code->block = NULL;
11296 3019 : if (!ns->code)
11297 3019 : ns->code = new_st;
11298 : else
11299 0 : ns->code->next = new_st;
11300 3019 : code = new_st;
11301 3019 : code->op = EXEC_SELECT_TYPE;
11302 :
11303 : /* Use the intrinsic LOC function to generate an integer expression
11304 : for the vtable of the selector. Note that the rank of the selector
11305 : expression has to be set to zero. */
11306 3019 : gfc_add_vptr_component (code->expr1);
11307 3019 : code->expr1->rank = 0;
11308 3019 : code->expr1->corank = 0;
11309 3019 : code->expr1 = build_loc_call (code->expr1);
11310 3019 : selector_expr = code->expr1->value.function.actual->expr;
11311 :
11312 : /* Loop over TYPE IS / CLASS IS cases. */
11313 8414 : for (body = code->block; body; body = body->block)
11314 : {
11315 5395 : gfc_symbol *vtab;
11316 5395 : c = body->ext.block.case_list;
11317 :
11318 : /* Generate an index integer expression for address of the
11319 : TYPE/CLASS vtable and store it in c->low. The hash expression
11320 : is stored in c->high and is used to resolve intrinsic cases. */
11321 5395 : if (c->ts.type != BT_UNKNOWN)
11322 : {
11323 3769 : gfc_expr *e;
11324 3769 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
11325 : {
11326 2327 : vtab = gfc_find_derived_vtab (c->ts.u.derived);
11327 2327 : gcc_assert (vtab);
11328 2327 : c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
11329 2327 : c->ts.u.derived->hash_value);
11330 : }
11331 : else
11332 : {
11333 1442 : vtab = gfc_find_vtab (&c->ts);
11334 1442 : gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
11335 1442 : e = CLASS_DATA (vtab)->initializer;
11336 1442 : c->high = gfc_copy_expr (e);
11337 1442 : if (c->high->ts.kind != gfc_integer_4_kind)
11338 : {
11339 1 : gfc_typespec ts;
11340 1 : ts.kind = gfc_integer_4_kind;
11341 1 : ts.type = BT_INTEGER;
11342 1 : gfc_convert_type_warn (c->high, &ts, 2, 0);
11343 : }
11344 : }
11345 :
11346 3769 : e = gfc_lval_expr_from_sym (vtab);
11347 3769 : c->low = build_loc_call (e);
11348 : }
11349 : else
11350 1626 : continue;
11351 :
11352 : /* Associate temporary to selector. This should only be done
11353 : when this case is actually true, so build a new ASSOCIATE
11354 : that does precisely this here (instead of using the
11355 : 'global' one). */
11356 :
11357 : /* First check the derived type import status. */
11358 3769 : if (gfc_current_ns->import_state != IMPORT_NOT_SET
11359 6 : && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
11360 : {
11361 12 : st = gfc_find_symtree (gfc_current_ns->sym_root,
11362 6 : c->ts.u.derived->name);
11363 6 : if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
11364 : gfc_current_ns))
11365 6 : error++;
11366 : }
11367 :
11368 3769 : const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
11369 3769 : if (c->ts.type == BT_CLASS)
11370 346 : snprintf (name, sizeof (name), "__tmp_class_%s_%s",
11371 346 : c->ts.u.derived->name, var_name);
11372 3423 : else if (c->ts.type == BT_DERIVED)
11373 1981 : snprintf (name, sizeof (name), "__tmp_type_%s_%s",
11374 1981 : c->ts.u.derived->name, var_name);
11375 1442 : else if (c->ts.type == BT_CHARACTER)
11376 : {
11377 736 : HOST_WIDE_INT charlen = 0;
11378 736 : if (c->ts.u.cl && c->ts.u.cl->length
11379 0 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11380 0 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11381 736 : snprintf (name, sizeof (name),
11382 : "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
11383 : gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
11384 : var_name);
11385 : }
11386 : else
11387 706 : snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
11388 : gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
11389 :
11390 3769 : st = gfc_find_symtree (ns->sym_root, name);
11391 3769 : gcc_assert (st->n.sym->assoc);
11392 3769 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11393 3769 : st->n.sym->assoc->target->where = selector_expr->where;
11394 3769 : if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
11395 : {
11396 3423 : gfc_add_data_component (st->n.sym->assoc->target);
11397 : /* Fixup the target expression if necessary. */
11398 3423 : if (rank || corank)
11399 1382 : fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
11400 : ref);
11401 : }
11402 :
11403 3769 : new_st = gfc_get_code (EXEC_BLOCK);
11404 3769 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11405 3769 : new_st->ext.block.ns->code = body->next;
11406 3769 : body->next = new_st;
11407 :
11408 : /* Chain in the new list only if it is marked as dangling. Otherwise
11409 : there is a CASE label overlap and this is already used. Just ignore,
11410 : the error is diagnosed elsewhere. */
11411 3769 : if (st->n.sym->assoc->dangling)
11412 : {
11413 3768 : new_st->ext.block.assoc = st->n.sym->assoc;
11414 3768 : st->n.sym->assoc->dangling = 0;
11415 : }
11416 :
11417 3769 : resolve_assoc_var (st->n.sym, false);
11418 : }
11419 :
11420 : /* Take out CLASS IS cases for separate treatment. */
11421 : body = code;
11422 8414 : while (body && body->block)
11423 : {
11424 5395 : if (body->block->ext.block.case_list->ts.type == BT_CLASS)
11425 : {
11426 : /* Add to class_is list. */
11427 346 : if (class_is == NULL)
11428 : {
11429 315 : class_is = body->block;
11430 315 : tail = class_is;
11431 : }
11432 : else
11433 : {
11434 43 : for (tail = class_is; tail->block; tail = tail->block) ;
11435 31 : tail->block = body->block;
11436 31 : tail = tail->block;
11437 : }
11438 : /* Remove from EXEC_SELECT list. */
11439 346 : body->block = body->block->block;
11440 346 : tail->block = NULL;
11441 : }
11442 : else
11443 : body = body->block;
11444 : }
11445 :
11446 3019 : if (class_is)
11447 : {
11448 315 : gfc_symbol *vtab;
11449 :
11450 315 : if (!default_case)
11451 : {
11452 : /* Add a default case to hold the CLASS IS cases. */
11453 313 : for (tail = code; tail->block; tail = tail->block) ;
11454 205 : tail->block = gfc_get_code (EXEC_SELECT_TYPE);
11455 205 : tail = tail->block;
11456 205 : tail->ext.block.case_list = gfc_get_case ();
11457 205 : tail->ext.block.case_list->ts.type = BT_UNKNOWN;
11458 205 : tail->next = NULL;
11459 205 : default_case = tail;
11460 : }
11461 :
11462 : /* More than one CLASS IS block? */
11463 315 : if (class_is->block)
11464 : {
11465 37 : gfc_code **c1,*c2;
11466 37 : bool swapped;
11467 : /* Sort CLASS IS blocks by extension level. */
11468 36 : do
11469 : {
11470 37 : swapped = false;
11471 97 : for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
11472 : {
11473 61 : c2 = (*c1)->block;
11474 : /* F03:C817 (check for doubles). */
11475 61 : if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
11476 61 : == c2->ext.block.case_list->ts.u.derived->hash_value)
11477 : {
11478 1 : gfc_error ("Double CLASS IS block in SELECT TYPE "
11479 : "statement at %L",
11480 : &c2->ext.block.case_list->where);
11481 1 : return;
11482 : }
11483 60 : if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11484 60 : < c2->ext.block.case_list->ts.u.derived->attr.extension)
11485 : {
11486 : /* Swap. */
11487 24 : (*c1)->block = c2->block;
11488 24 : c2->block = *c1;
11489 24 : *c1 = c2;
11490 24 : swapped = true;
11491 : }
11492 : }
11493 : }
11494 : while (swapped);
11495 : }
11496 :
11497 : /* Generate IF chain. */
11498 314 : if_st = gfc_get_code (EXEC_IF);
11499 314 : new_st = if_st;
11500 658 : for (body = class_is; body; body = body->block)
11501 : {
11502 344 : new_st->block = gfc_get_code (EXEC_IF);
11503 344 : new_st = new_st->block;
11504 : /* Set up IF condition: Call _gfortran_is_extension_of. */
11505 344 : new_st->expr1 = gfc_get_expr ();
11506 344 : new_st->expr1->expr_type = EXPR_FUNCTION;
11507 344 : new_st->expr1->ts.type = BT_LOGICAL;
11508 344 : new_st->expr1->ts.kind = 4;
11509 344 : new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11510 344 : new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11511 344 : new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11512 : /* Set up arguments. */
11513 344 : new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11514 344 : new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11515 344 : new_st->expr1->value.function.actual->expr->where = code->loc;
11516 344 : new_st->expr1->where = code->loc;
11517 344 : gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11518 344 : vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11519 344 : st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11520 344 : new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11521 344 : new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11522 344 : new_st->expr1->value.function.actual->next->expr->where = code->loc;
11523 : /* Set up types in formal arg list. */
11524 344 : new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11525 344 : new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11526 344 : new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11527 344 : new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11528 :
11529 344 : new_st->next = body->next;
11530 : }
11531 314 : if (default_case->next)
11532 : {
11533 110 : new_st->block = gfc_get_code (EXEC_IF);
11534 110 : new_st = new_st->block;
11535 110 : new_st->next = default_case->next;
11536 : }
11537 :
11538 : /* Replace CLASS DEFAULT code by the IF chain. */
11539 314 : default_case->next = if_st;
11540 : }
11541 :
11542 : /* Resolve the internal code. This cannot be done earlier because
11543 : it requires that the sym->assoc of selectors is set already. */
11544 3018 : gfc_current_ns = ns;
11545 3018 : gfc_resolve_blocks (code->block, gfc_current_ns);
11546 3018 : gfc_current_ns = old_ns;
11547 :
11548 3018 : free (ref);
11549 : }
11550 :
11551 :
11552 : /* Resolve a SELECT RANK statement. */
11553 :
11554 : static void
11555 1024 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11556 : {
11557 1024 : gfc_namespace *ns;
11558 1024 : gfc_code *body, *new_st, *tail;
11559 1024 : gfc_case *c;
11560 1024 : char tname[GFC_MAX_SYMBOL_LEN + 7];
11561 1024 : char name[2 * GFC_MAX_SYMBOL_LEN];
11562 1024 : gfc_symtree *st;
11563 1024 : gfc_expr *selector_expr = NULL;
11564 1024 : int case_value;
11565 1024 : HOST_WIDE_INT charlen = 0;
11566 :
11567 1024 : ns = code->ext.block.ns;
11568 1024 : gfc_resolve (ns);
11569 :
11570 1024 : code->op = EXEC_BLOCK;
11571 1024 : if (code->expr2)
11572 : {
11573 42 : gfc_association_list* assoc;
11574 :
11575 42 : assoc = gfc_get_association_list ();
11576 42 : assoc->st = code->expr1->symtree;
11577 42 : assoc->target = gfc_copy_expr (code->expr2);
11578 42 : assoc->target->where = code->expr2->where;
11579 : /* assoc->variable will be set by resolve_assoc_var. */
11580 :
11581 42 : code->ext.block.assoc = assoc;
11582 42 : code->expr1->symtree->n.sym->assoc = assoc;
11583 :
11584 42 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11585 : }
11586 : else
11587 982 : code->ext.block.assoc = NULL;
11588 :
11589 : /* Loop over RANK cases. Note that returning on the errors causes a
11590 : cascade of further errors because the case blocks do not compile
11591 : correctly. */
11592 3332 : for (body = code->block; body; body = body->block)
11593 : {
11594 2308 : c = body->ext.block.case_list;
11595 2308 : if (c->low)
11596 1389 : case_value = (int) mpz_get_si (c->low->value.integer);
11597 : else
11598 : case_value = -2;
11599 :
11600 : /* Check for repeated cases. */
11601 5842 : for (tail = code->block; tail; tail = tail->block)
11602 : {
11603 5842 : gfc_case *d = tail->ext.block.case_list;
11604 5842 : int case_value2;
11605 :
11606 5842 : if (tail == body)
11607 : break;
11608 :
11609 : /* Check F2018: C1153. */
11610 3534 : if (!c->low && !d->low)
11611 1 : gfc_error ("RANK DEFAULT at %L is repeated at %L",
11612 : &c->where, &d->where);
11613 :
11614 3534 : if (!c->low || !d->low)
11615 1253 : continue;
11616 :
11617 : /* Check F2018: C1153. */
11618 2281 : case_value2 = (int) mpz_get_si (d->low->value.integer);
11619 2281 : if ((case_value == case_value2) && case_value == -1)
11620 1 : gfc_error ("RANK (*) at %L is repeated at %L",
11621 : &c->where, &d->where);
11622 2280 : else if (case_value == case_value2)
11623 1 : gfc_error ("RANK (%i) at %L is repeated at %L",
11624 : case_value, &c->where, &d->where);
11625 : }
11626 :
11627 2308 : if (!c->low)
11628 919 : continue;
11629 :
11630 : /* Check F2018: C1155. */
11631 1389 : if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11632 1387 : || gfc_expr_attr (code->expr1).pointer))
11633 3 : gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11634 3 : "allocatable selector at %L", &c->where, &code->expr1->where);
11635 : }
11636 :
11637 : /* Add EXEC_SELECT to switch on rank. */
11638 1024 : new_st = gfc_get_code (code->op);
11639 1024 : new_st->expr1 = code->expr1;
11640 1024 : new_st->expr2 = code->expr2;
11641 1024 : new_st->block = code->block;
11642 1024 : code->expr1 = code->expr2 = NULL;
11643 1024 : code->block = NULL;
11644 1024 : if (!ns->code)
11645 1024 : ns->code = new_st;
11646 : else
11647 0 : ns->code->next = new_st;
11648 1024 : code = new_st;
11649 1024 : code->op = EXEC_SELECT_RANK;
11650 :
11651 1024 : selector_expr = code->expr1;
11652 :
11653 : /* Loop over SELECT RANK cases. */
11654 3332 : for (body = code->block; body; body = body->block)
11655 : {
11656 2308 : c = body->ext.block.case_list;
11657 2308 : int case_value;
11658 :
11659 : /* Pass on the default case. */
11660 2308 : if (c->low == NULL)
11661 919 : continue;
11662 :
11663 : /* Associate temporary to selector. This should only be done
11664 : when this case is actually true, so build a new ASSOCIATE
11665 : that does precisely this here (instead of using the
11666 : 'global' one). */
11667 1389 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11668 265 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11669 186 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11670 :
11671 1389 : if (c->ts.type == BT_CLASS)
11672 145 : sprintf (tname, "class_%s", c->ts.u.derived->name);
11673 1244 : else if (c->ts.type == BT_DERIVED)
11674 110 : sprintf (tname, "type_%s", c->ts.u.derived->name);
11675 1134 : else if (c->ts.type != BT_CHARACTER)
11676 575 : sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11677 : else
11678 559 : sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11679 : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11680 :
11681 1389 : case_value = (int) mpz_get_si (c->low->value.integer);
11682 1389 : if (case_value >= 0)
11683 1356 : sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11684 : else
11685 33 : sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11686 :
11687 1389 : st = gfc_find_symtree (ns->sym_root, name);
11688 1389 : gcc_assert (st->n.sym->assoc);
11689 :
11690 1389 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11691 1389 : st->n.sym->assoc->target->where = selector_expr->where;
11692 :
11693 1389 : new_st = gfc_get_code (EXEC_BLOCK);
11694 1389 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11695 1389 : new_st->ext.block.ns->code = body->next;
11696 1389 : body->next = new_st;
11697 :
11698 : /* Chain in the new list only if it is marked as dangling. Otherwise
11699 : there is a CASE label overlap and this is already used. Just ignore,
11700 : the error is diagnosed elsewhere. */
11701 1389 : if (st->n.sym->assoc->dangling)
11702 : {
11703 1387 : new_st->ext.block.assoc = st->n.sym->assoc;
11704 1387 : st->n.sym->assoc->dangling = 0;
11705 : }
11706 :
11707 1389 : resolve_assoc_var (st->n.sym, false);
11708 : }
11709 :
11710 1024 : gfc_current_ns = ns;
11711 1024 : gfc_resolve_blocks (code->block, gfc_current_ns);
11712 1024 : gfc_current_ns = old_ns;
11713 1024 : }
11714 :
11715 :
11716 : /* Resolve a transfer statement. This is making sure that:
11717 : -- a derived type being transferred has only non-pointer components
11718 : -- a derived type being transferred doesn't have private components, unless
11719 : it's being transferred from the module where the type was defined
11720 : -- we're not trying to transfer a whole assumed size array. */
11721 :
11722 : static void
11723 47035 : resolve_transfer (gfc_code *code)
11724 : {
11725 47035 : gfc_symbol *sym, *derived;
11726 47035 : gfc_ref *ref;
11727 47035 : gfc_expr *exp;
11728 47035 : bool write = false;
11729 47035 : bool formatted = false;
11730 47035 : gfc_dt *dt = code->ext.dt;
11731 47035 : gfc_symbol *dtio_sub = NULL;
11732 :
11733 47035 : exp = code->expr1;
11734 :
11735 94076 : while (exp != NULL && exp->expr_type == EXPR_OP
11736 47968 : && exp->value.op.op == INTRINSIC_PARENTHESES)
11737 6 : exp = exp->value.op.op1;
11738 :
11739 47035 : if (exp && exp->expr_type == EXPR_NULL
11740 2 : && code->ext.dt)
11741 : {
11742 2 : gfc_error ("Invalid context for NULL () intrinsic at %L",
11743 : &exp->where);
11744 2 : return;
11745 : }
11746 :
11747 : if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11748 : && exp->expr_type != EXPR_FUNCTION
11749 : && exp->expr_type != EXPR_ARRAY
11750 : && exp->expr_type != EXPR_STRUCTURE))
11751 : return;
11752 :
11753 : /* If we are reading, the variable will be changed. Note that
11754 : code->ext.dt may be NULL if the TRANSFER is related to
11755 : an INQUIRE statement -- but in this case, we are not reading, either. */
11756 25816 : if (dt && dt->dt_io_kind->value.iokind == M_READ
11757 33286 : && !gfc_check_vardef_context (exp, false, false, false,
11758 7322 : _("item in READ")))
11759 : return;
11760 :
11761 25960 : const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11762 25960 : || exp->expr_type == EXPR_FUNCTION
11763 21573 : || exp->expr_type == EXPR_ARRAY
11764 47533 : ? &exp->ts : &exp->symtree->n.sym->ts;
11765 :
11766 : /* Go to actual component transferred. */
11767 33728 : for (ref = exp->ref; ref; ref = ref->next)
11768 7768 : if (ref->type == REF_COMPONENT)
11769 2208 : ts = &ref->u.c.component->ts;
11770 :
11771 25960 : if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11772 25812 : && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11773 : {
11774 720 : derived = ts->u.derived;
11775 :
11776 : /* Determine when to use the formatted DTIO procedure. */
11777 720 : if (dt && (dt->format_expr || dt->format_label))
11778 645 : formatted = true;
11779 :
11780 720 : write = dt->dt_io_kind->value.iokind == M_WRITE
11781 720 : || dt->dt_io_kind->value.iokind == M_PRINT;
11782 720 : dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11783 :
11784 720 : if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11785 : {
11786 450 : dt->udtio = exp;
11787 450 : sym = exp->symtree->n.sym->ns->proc_name;
11788 : /* Check to see if this is a nested DTIO call, with the
11789 : dummy as the io-list object. */
11790 450 : if (sym && sym == dtio_sub && sym->formal
11791 30 : && sym->formal->sym == exp->symtree->n.sym
11792 30 : && exp->ref == NULL)
11793 : {
11794 0 : if (!sym->attr.recursive)
11795 : {
11796 0 : gfc_error ("DTIO %s procedure at %L must be recursive",
11797 : sym->name, &sym->declared_at);
11798 0 : return;
11799 : }
11800 : }
11801 : }
11802 : }
11803 :
11804 25960 : if (ts->type == BT_CLASS && dtio_sub == NULL)
11805 : {
11806 3 : gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11807 : "it is processed by a defined input/output procedure",
11808 : &code->loc);
11809 3 : return;
11810 : }
11811 :
11812 25957 : if (ts->type == BT_DERIVED)
11813 : {
11814 : /* Check that transferred derived type doesn't contain POINTER
11815 : components unless it is processed by a defined input/output
11816 : procedure". */
11817 688 : if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11818 : {
11819 2 : gfc_error ("Data transfer element at %L cannot have POINTER "
11820 : "components unless it is processed by a defined "
11821 : "input/output procedure", &code->loc);
11822 2 : return;
11823 : }
11824 :
11825 : /* F08:C935. */
11826 686 : if (ts->u.derived->attr.proc_pointer_comp)
11827 : {
11828 2 : gfc_error ("Data transfer element at %L cannot have "
11829 : "procedure pointer components", &code->loc);
11830 2 : return;
11831 : }
11832 :
11833 684 : if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11834 : {
11835 6 : gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11836 : "components unless it is processed by a defined "
11837 : "input/output procedure", &code->loc);
11838 6 : return;
11839 : }
11840 :
11841 : /* C_PTR and C_FUNPTR have private components which means they cannot
11842 : be printed. However, if -std=gnu and not -pedantic, allow
11843 : the component to be printed to help debugging. */
11844 678 : if (ts->u.derived->ts.f90_type == BT_VOID)
11845 : {
11846 4 : gfc_error ("Data transfer element at %L "
11847 : "cannot have PRIVATE components", &code->loc);
11848 4 : return;
11849 : }
11850 674 : else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11851 : {
11852 4 : gfc_error ("Data transfer element at %L cannot have "
11853 : "PRIVATE components unless it is processed by "
11854 : "a defined input/output procedure", &code->loc);
11855 4 : return;
11856 : }
11857 : }
11858 :
11859 25939 : if (exp->expr_type == EXPR_STRUCTURE)
11860 : return;
11861 :
11862 25894 : if (exp->expr_type == EXPR_ARRAY)
11863 : return;
11864 :
11865 25518 : sym = exp->symtree->n.sym;
11866 :
11867 25518 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11868 81 : && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11869 : {
11870 1 : gfc_error ("Data transfer element at %L cannot be a full reference to "
11871 : "an assumed-size array", &code->loc);
11872 1 : return;
11873 : }
11874 : }
11875 :
11876 :
11877 : /*********** Toplevel code resolution subroutines ***********/
11878 :
11879 : /* Find the set of labels that are reachable from this block. We also
11880 : record the last statement in each block. */
11881 :
11882 : static void
11883 679829 : find_reachable_labels (gfc_code *block)
11884 : {
11885 679829 : gfc_code *c;
11886 :
11887 679829 : if (!block)
11888 : return;
11889 :
11890 425940 : cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11891 :
11892 : /* Collect labels in this block. We don't keep those corresponding
11893 : to END {IF|SELECT}, these are checked in resolve_branch by going
11894 : up through the code_stack. */
11895 1562761 : for (c = block; c; c = c->next)
11896 : {
11897 1136821 : if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11898 3661 : bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11899 : }
11900 :
11901 : /* Merge with labels from parent block. */
11902 425940 : if (cs_base->prev)
11903 : {
11904 349894 : gcc_assert (cs_base->prev->reachable_labels);
11905 349894 : bitmap_ior_into (cs_base->reachable_labels,
11906 : cs_base->prev->reachable_labels);
11907 : }
11908 : }
11909 :
11910 : static void
11911 197 : resolve_lock_unlock_event (gfc_code *code)
11912 : {
11913 197 : if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11914 197 : && (code->expr1->ts.type != BT_DERIVED
11915 137 : || code->expr1->expr_type != EXPR_VARIABLE
11916 137 : || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11917 136 : || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11918 136 : || code->expr1->rank != 0
11919 181 : || (!gfc_is_coarray (code->expr1) &&
11920 46 : !gfc_is_coindexed (code->expr1))))
11921 4 : gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11922 4 : &code->expr1->where);
11923 193 : else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11924 58 : && (code->expr1->ts.type != BT_DERIVED
11925 58 : || code->expr1->expr_type != EXPR_VARIABLE
11926 58 : || code->expr1->ts.u.derived->from_intmod
11927 : != INTMOD_ISO_FORTRAN_ENV
11928 58 : || code->expr1->ts.u.derived->intmod_sym_id
11929 : != ISOFORTRAN_EVENT_TYPE
11930 58 : || code->expr1->rank != 0))
11931 0 : gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11932 : &code->expr1->where);
11933 34 : else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11934 209 : && !gfc_is_coindexed (code->expr1))
11935 0 : gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11936 0 : &code->expr1->where);
11937 193 : else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11938 0 : gfc_error ("Event variable argument at %L must be a coarray but not "
11939 0 : "coindexed", &code->expr1->where);
11940 :
11941 : /* Check STAT. */
11942 197 : if (code->expr2
11943 54 : && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11944 54 : || code->expr2->expr_type != EXPR_VARIABLE))
11945 0 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11946 : &code->expr2->where);
11947 :
11948 197 : if (code->expr2
11949 251 : && !gfc_check_vardef_context (code->expr2, false, false, false,
11950 54 : _("STAT variable")))
11951 : return;
11952 :
11953 : /* Check ERRMSG. */
11954 197 : if (code->expr3
11955 2 : && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11956 2 : || code->expr3->expr_type != EXPR_VARIABLE))
11957 0 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11958 : &code->expr3->where);
11959 :
11960 197 : if (code->expr3
11961 199 : && !gfc_check_vardef_context (code->expr3, false, false, false,
11962 2 : _("ERRMSG variable")))
11963 : return;
11964 :
11965 : /* Check for LOCK the ACQUIRED_LOCK. */
11966 197 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11967 22 : && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11968 22 : || code->expr4->expr_type != EXPR_VARIABLE))
11969 0 : gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11970 : "variable", &code->expr4->where);
11971 :
11972 173 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11973 219 : && !gfc_check_vardef_context (code->expr4, false, false, false,
11974 22 : _("ACQUIRED_LOCK variable")))
11975 : return;
11976 :
11977 : /* Check for EVENT WAIT the UNTIL_COUNT. */
11978 197 : if (code->op == EXEC_EVENT_WAIT && code->expr4)
11979 : {
11980 36 : if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11981 36 : || code->expr4->rank != 0)
11982 0 : gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11983 0 : "expression", &code->expr4->where);
11984 : }
11985 : }
11986 :
11987 : static void
11988 246 : resolve_team_argument (gfc_expr *team)
11989 : {
11990 246 : gfc_resolve_expr (team);
11991 246 : if (team->rank != 0 || team->ts.type != BT_DERIVED
11992 239 : || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11993 239 : || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
11994 : {
11995 7 : gfc_error ("TEAM argument at %L must be a scalar expression "
11996 : "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
11997 : &team->where);
11998 : }
11999 246 : }
12000 :
12001 : static void
12002 1358 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
12003 : gfc_expr *e)
12004 : {
12005 1358 : gfc_resolve_expr (e);
12006 1358 : if (e
12007 139 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
12008 124 : || e->expr_type != EXPR_VARIABLE))
12009 15 : gfc_error ("%s argument at %L must be a scalar %s variable of at least "
12010 : "kind %d", name, &e->where, gfc_basic_typename (exp_type),
12011 : exp_kind);
12012 1358 : }
12013 :
12014 : void
12015 679 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
12016 : {
12017 679 : resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
12018 679 : resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
12019 : gfc_default_character_kind,
12020 : sync_stat->errmsg);
12021 679 : }
12022 :
12023 : static void
12024 260 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
12025 : gfc_expr *e)
12026 : {
12027 260 : gfc_resolve_expr (e);
12028 260 : if (e
12029 161 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
12030 3 : gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
12031 : name, &e->where, gfc_basic_typename (exp_type), exp_kind);
12032 260 : }
12033 :
12034 : static void
12035 130 : resolve_form_team (gfc_code *code)
12036 : {
12037 130 : resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
12038 : code->expr1);
12039 130 : resolve_team_argument (code->expr2);
12040 130 : resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
12041 : code->expr3);
12042 130 : gfc_resolve_sync_stat (&code->ext.sync_stat);
12043 130 : }
12044 :
12045 : static void resolve_block_construct (gfc_code *);
12046 :
12047 : static void
12048 73 : resolve_change_team (gfc_code *code)
12049 : {
12050 73 : resolve_team_argument (code->expr1);
12051 73 : gfc_resolve_sync_stat (&code->ext.block.sync_stat);
12052 146 : resolve_block_construct (code);
12053 : /* Map the coarray bounds as selected. */
12054 76 : for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
12055 3 : if (a->ar)
12056 : {
12057 3 : gfc_array_spec *src = a->ar->as, *dst;
12058 3 : if (a->st->n.sym->ts.type == BT_CLASS)
12059 0 : dst = CLASS_DATA (a->st->n.sym)->as;
12060 : else
12061 3 : dst = a->st->n.sym->as;
12062 3 : dst->corank = src->corank;
12063 3 : dst->cotype = src->cotype;
12064 6 : for (int i = 0; i < src->corank; ++i)
12065 : {
12066 3 : dst->lower[dst->rank + i] = src->lower[i];
12067 3 : dst->upper[dst->rank + i] = src->upper[i];
12068 3 : src->lower[i] = src->upper[i] = nullptr;
12069 : }
12070 3 : gfc_free_array_spec (src);
12071 3 : free (a->ar);
12072 3 : a->ar = nullptr;
12073 3 : dst->resolved = false;
12074 3 : gfc_resolve_array_spec (dst, 0);
12075 : }
12076 73 : }
12077 :
12078 : static void
12079 43 : resolve_sync_team (gfc_code *code)
12080 : {
12081 43 : resolve_team_argument (code->expr1);
12082 43 : gfc_resolve_sync_stat (&code->ext.sync_stat);
12083 43 : }
12084 :
12085 : static void
12086 71 : resolve_end_team (gfc_code *code)
12087 : {
12088 71 : gfc_resolve_sync_stat (&code->ext.sync_stat);
12089 71 : }
12090 :
12091 : static void
12092 54 : resolve_critical (gfc_code *code)
12093 : {
12094 54 : gfc_symtree *symtree;
12095 54 : gfc_symbol *lock_type;
12096 54 : char name[GFC_MAX_SYMBOL_LEN];
12097 54 : static int serial = 0;
12098 :
12099 54 : gfc_resolve_sync_stat (&code->ext.sync_stat);
12100 :
12101 54 : if (flag_coarray != GFC_FCOARRAY_LIB)
12102 30 : return;
12103 :
12104 24 : symtree = gfc_find_symtree (gfc_current_ns->sym_root,
12105 : GFC_PREFIX ("lock_type"));
12106 24 : if (symtree)
12107 12 : lock_type = symtree->n.sym;
12108 : else
12109 : {
12110 12 : if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
12111 : false) != 0)
12112 0 : gcc_unreachable ();
12113 12 : lock_type = symtree->n.sym;
12114 12 : lock_type->attr.flavor = FL_DERIVED;
12115 12 : lock_type->attr.zero_comp = 1;
12116 12 : lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
12117 12 : lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
12118 : }
12119 :
12120 24 : sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
12121 24 : if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
12122 0 : gcc_unreachable ();
12123 :
12124 24 : code->resolved_sym = symtree->n.sym;
12125 24 : symtree->n.sym->attr.flavor = FL_VARIABLE;
12126 24 : symtree->n.sym->attr.referenced = 1;
12127 24 : symtree->n.sym->attr.artificial = 1;
12128 24 : symtree->n.sym->attr.codimension = 1;
12129 24 : symtree->n.sym->ts.type = BT_DERIVED;
12130 24 : symtree->n.sym->ts.u.derived = lock_type;
12131 24 : symtree->n.sym->as = gfc_get_array_spec ();
12132 24 : symtree->n.sym->as->corank = 1;
12133 24 : symtree->n.sym->as->type = AS_EXPLICIT;
12134 24 : symtree->n.sym->as->cotype = AS_EXPLICIT;
12135 24 : symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
12136 : NULL, 1);
12137 24 : gfc_commit_symbols();
12138 : }
12139 :
12140 :
12141 : static void
12142 1317 : resolve_sync (gfc_code *code)
12143 : {
12144 : /* Check imageset. The * case matches expr1 == NULL. */
12145 1317 : if (code->expr1)
12146 : {
12147 71 : if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
12148 1 : gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
12149 : "INTEGER expression", &code->expr1->where);
12150 71 : if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
12151 27 : && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
12152 1 : gfc_error ("Imageset argument at %L must between 1 and num_images()",
12153 : &code->expr1->where);
12154 70 : else if (code->expr1->expr_type == EXPR_ARRAY
12155 70 : && gfc_simplify_expr (code->expr1, 0))
12156 : {
12157 20 : gfc_constructor *cons;
12158 20 : cons = gfc_constructor_first (code->expr1->value.constructor);
12159 60 : for (; cons; cons = gfc_constructor_next (cons))
12160 20 : if (cons->expr->expr_type == EXPR_CONSTANT
12161 20 : && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
12162 0 : gfc_error ("Imageset argument at %L must between 1 and "
12163 : "num_images()", &cons->expr->where);
12164 : }
12165 : }
12166 :
12167 : /* Check STAT. */
12168 1317 : gfc_resolve_expr (code->expr2);
12169 1317 : if (code->expr2)
12170 : {
12171 108 : if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
12172 1 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
12173 : &code->expr2->where);
12174 : else
12175 107 : gfc_check_vardef_context (code->expr2, false, false, false,
12176 107 : _("STAT variable"));
12177 : }
12178 :
12179 : /* Check ERRMSG. */
12180 1317 : gfc_resolve_expr (code->expr3);
12181 1317 : if (code->expr3)
12182 : {
12183 90 : if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
12184 4 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
12185 : &code->expr3->where);
12186 : else
12187 86 : gfc_check_vardef_context (code->expr3, false, false, false,
12188 86 : _("ERRMSG variable"));
12189 : }
12190 1317 : }
12191 :
12192 :
12193 : /* Given a branch to a label, see if the branch is conforming.
12194 : The code node describes where the branch is located. */
12195 :
12196 : static void
12197 110152 : resolve_branch (gfc_st_label *label, gfc_code *code)
12198 : {
12199 110152 : code_stack *stack;
12200 :
12201 110152 : if (label == NULL)
12202 : return;
12203 :
12204 : /* Step one: is this a valid branching target? */
12205 :
12206 2460 : if (label->defined == ST_LABEL_UNKNOWN)
12207 : {
12208 4 : gfc_error ("Label %d referenced at %L is never defined", label->value,
12209 : &code->loc);
12210 4 : return;
12211 : }
12212 :
12213 2456 : if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
12214 : {
12215 4 : gfc_error ("Statement at %L is not a valid branch target statement "
12216 : "for the branch statement at %L", &label->where, &code->loc);
12217 4 : return;
12218 : }
12219 :
12220 : /* Step two: make sure this branch is not a branch to itself ;-) */
12221 :
12222 2452 : if (code->here == label)
12223 : {
12224 0 : gfc_warning (0, "Branch at %L may result in an infinite loop",
12225 : &code->loc);
12226 0 : return;
12227 : }
12228 :
12229 : /* Step three: See if the label is in the same block as the
12230 : branching statement. The hard work has been done by setting up
12231 : the bitmap reachable_labels. */
12232 :
12233 2452 : if (bitmap_bit_p (cs_base->reachable_labels, label->value))
12234 : {
12235 : /* Check now whether there is a CRITICAL construct; if so, check
12236 : whether the label is still visible outside of the CRITICAL block,
12237 : which is invalid. */
12238 6267 : for (stack = cs_base; stack; stack = stack->prev)
12239 : {
12240 3883 : if (stack->current->op == EXEC_CRITICAL
12241 3883 : && bitmap_bit_p (stack->reachable_labels, label->value))
12242 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
12243 : "label at %L", &code->loc, &label->where);
12244 3881 : else if (stack->current->op == EXEC_DO_CONCURRENT
12245 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
12246 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
12247 : "for label at %L", &code->loc, &label->where);
12248 3881 : else if (stack->current->op == EXEC_CHANGE_TEAM
12249 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
12250 1 : gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
12251 : "for label at %L", &code->loc, &label->where);
12252 : }
12253 :
12254 : return;
12255 : }
12256 :
12257 : /* Step four: If we haven't found the label in the bitmap, it may
12258 : still be the label of the END of the enclosing block, in which
12259 : case we find it by going up the code_stack. */
12260 :
12261 167 : for (stack = cs_base; stack; stack = stack->prev)
12262 : {
12263 131 : if (stack->current->next && stack->current->next->here == label)
12264 : break;
12265 101 : if (stack->current->op == EXEC_CRITICAL)
12266 : {
12267 : /* Note: A label at END CRITICAL does not leave the CRITICAL
12268 : construct as END CRITICAL is still part of it. */
12269 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
12270 : " at %L", &code->loc, &label->where);
12271 2 : return;
12272 : }
12273 99 : else if (stack->current->op == EXEC_DO_CONCURRENT)
12274 : {
12275 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
12276 : "label at %L", &code->loc, &label->where);
12277 0 : return;
12278 : }
12279 : }
12280 :
12281 66 : if (stack)
12282 : {
12283 30 : gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
12284 : return;
12285 : }
12286 :
12287 : /* The label is not in an enclosing block, so illegal. This was
12288 : allowed in Fortran 66, so we allow it as extension. No
12289 : further checks are necessary in this case. */
12290 36 : gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
12291 : "as the GOTO statement at %L", &label->where,
12292 : &code->loc);
12293 36 : return;
12294 : }
12295 :
12296 :
12297 : /* Check whether EXPR1 has the same shape as EXPR2. */
12298 :
12299 : static bool
12300 1467 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
12301 : {
12302 1467 : mpz_t shape[GFC_MAX_DIMENSIONS];
12303 1467 : mpz_t shape2[GFC_MAX_DIMENSIONS];
12304 1467 : bool result = false;
12305 1467 : int i;
12306 :
12307 : /* Compare the rank. */
12308 1467 : if (expr1->rank != expr2->rank)
12309 : return result;
12310 :
12311 : /* Compare the size of each dimension. */
12312 2811 : for (i=0; i<expr1->rank; i++)
12313 : {
12314 1495 : if (!gfc_array_dimen_size (expr1, i, &shape[i]))
12315 151 : goto ignore;
12316 :
12317 1344 : if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
12318 0 : goto ignore;
12319 :
12320 1344 : if (mpz_cmp (shape[i], shape2[i]))
12321 0 : goto over;
12322 : }
12323 :
12324 : /* When either of the two expression is an assumed size array, we
12325 : ignore the comparison of dimension sizes. */
12326 1316 : ignore:
12327 : result = true;
12328 :
12329 1467 : over:
12330 1467 : gfc_clear_shape (shape, i);
12331 1467 : gfc_clear_shape (shape2, i);
12332 1467 : return result;
12333 : }
12334 :
12335 :
12336 : /* Check whether a WHERE assignment target or a WHERE mask expression
12337 : has the same shape as the outermost WHERE mask expression. */
12338 :
12339 : static void
12340 509 : resolve_where (gfc_code *code, gfc_expr *mask)
12341 : {
12342 509 : gfc_code *cblock;
12343 509 : gfc_code *cnext;
12344 509 : gfc_expr *e = NULL;
12345 :
12346 509 : cblock = code->block;
12347 :
12348 : /* Store the first WHERE mask-expr of the WHERE statement or construct.
12349 : In case of nested WHERE, only the outermost one is stored. */
12350 509 : if (mask == NULL) /* outermost WHERE */
12351 453 : e = cblock->expr1;
12352 : else /* inner WHERE */
12353 509 : e = mask;
12354 :
12355 1387 : while (cblock)
12356 : {
12357 878 : if (cblock->expr1)
12358 : {
12359 : /* Check if the mask-expr has a consistent shape with the
12360 : outermost WHERE mask-expr. */
12361 714 : if (!resolve_where_shape (cblock->expr1, e))
12362 0 : gfc_error ("WHERE mask at %L has inconsistent shape",
12363 0 : &cblock->expr1->where);
12364 : }
12365 :
12366 : /* the assignment statement of a WHERE statement, or the first
12367 : statement in where-body-construct of a WHERE construct */
12368 878 : cnext = cblock->next;
12369 1733 : while (cnext)
12370 : {
12371 855 : switch (cnext->op)
12372 : {
12373 : /* WHERE assignment statement */
12374 753 : case EXEC_ASSIGN:
12375 :
12376 : /* Check shape consistent for WHERE assignment target. */
12377 753 : if (e && !resolve_where_shape (cnext->expr1, e))
12378 0 : gfc_error ("WHERE assignment target at %L has "
12379 0 : "inconsistent shape", &cnext->expr1->where);
12380 :
12381 753 : if (cnext->op == EXEC_ASSIGN
12382 753 : && gfc_may_be_finalized (cnext->expr1->ts))
12383 0 : cnext->expr1->must_finalize = 1;
12384 :
12385 : break;
12386 :
12387 :
12388 46 : case EXEC_ASSIGN_CALL:
12389 46 : resolve_call (cnext);
12390 46 : if (!cnext->resolved_sym->attr.elemental)
12391 2 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12392 2 : &cnext->ext.actual->expr->where);
12393 : break;
12394 :
12395 : /* WHERE or WHERE construct is part of a where-body-construct */
12396 56 : case EXEC_WHERE:
12397 56 : resolve_where (cnext, e);
12398 56 : break;
12399 :
12400 0 : default:
12401 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12402 : &cnext->loc);
12403 : }
12404 : /* the next statement within the same where-body-construct */
12405 855 : cnext = cnext->next;
12406 : }
12407 : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12408 878 : cblock = cblock->block;
12409 : }
12410 509 : }
12411 :
12412 :
12413 : /* Resolve assignment in FORALL construct.
12414 : NVAR is the number of FORALL index variables, and VAR_EXPR records the
12415 : FORALL index variables. */
12416 :
12417 : static void
12418 2375 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
12419 : {
12420 2375 : int n;
12421 2375 : gfc_symbol *forall_index;
12422 :
12423 6771 : for (n = 0; n < nvar; n++)
12424 : {
12425 4396 : forall_index = var_expr[n]->symtree->n.sym;
12426 :
12427 : /* Check whether the assignment target is one of the FORALL index
12428 : variable. */
12429 4396 : if ((code->expr1->expr_type == EXPR_VARIABLE)
12430 4396 : && (code->expr1->symtree->n.sym == forall_index))
12431 0 : gfc_error ("Assignment to a FORALL index variable at %L",
12432 : &code->expr1->where);
12433 : else
12434 : {
12435 : /* If one of the FORALL index variables doesn't appear in the
12436 : assignment variable, then there could be a many-to-one
12437 : assignment. Emit a warning rather than an error because the
12438 : mask could be resolving this problem.
12439 : DO NOT emit this warning for DO CONCURRENT - reduction-like
12440 : many-to-one assignments are semantically valid (formalized with
12441 : the REDUCE locality-spec in Fortran 2023). */
12442 4396 : if (!find_forall_index (code->expr1, forall_index, 0)
12443 4396 : && !gfc_do_concurrent_flag)
12444 0 : gfc_warning (0, "The FORALL with index %qs is not used on the "
12445 : "left side of the assignment at %L and so might "
12446 : "cause multiple assignment to this object",
12447 0 : var_expr[n]->symtree->name, &code->expr1->where);
12448 : }
12449 : }
12450 2375 : }
12451 :
12452 :
12453 : /* Resolve WHERE statement in FORALL construct. */
12454 :
12455 : static void
12456 47 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
12457 : gfc_expr **var_expr)
12458 : {
12459 47 : gfc_code *cblock;
12460 47 : gfc_code *cnext;
12461 :
12462 47 : cblock = code->block;
12463 113 : while (cblock)
12464 : {
12465 : /* the assignment statement of a WHERE statement, or the first
12466 : statement in where-body-construct of a WHERE construct */
12467 66 : cnext = cblock->next;
12468 132 : while (cnext)
12469 : {
12470 66 : switch (cnext->op)
12471 : {
12472 : /* WHERE assignment statement */
12473 66 : case EXEC_ASSIGN:
12474 66 : gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
12475 :
12476 66 : if (cnext->op == EXEC_ASSIGN
12477 66 : && gfc_may_be_finalized (cnext->expr1->ts))
12478 0 : cnext->expr1->must_finalize = 1;
12479 :
12480 : break;
12481 :
12482 : /* WHERE operator assignment statement */
12483 0 : case EXEC_ASSIGN_CALL:
12484 0 : resolve_call (cnext);
12485 0 : if (!cnext->resolved_sym->attr.elemental)
12486 0 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12487 0 : &cnext->ext.actual->expr->where);
12488 : break;
12489 :
12490 : /* WHERE or WHERE construct is part of a where-body-construct */
12491 0 : case EXEC_WHERE:
12492 0 : gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
12493 0 : break;
12494 :
12495 0 : default:
12496 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12497 : &cnext->loc);
12498 : }
12499 : /* the next statement within the same where-body-construct */
12500 66 : cnext = cnext->next;
12501 : }
12502 : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12503 66 : cblock = cblock->block;
12504 : }
12505 47 : }
12506 :
12507 :
12508 : /* Traverse the FORALL body to check whether the following errors exist:
12509 : 1. For assignment, check if a many-to-one assignment happens.
12510 : 2. For WHERE statement, check the WHERE body to see if there is any
12511 : many-to-one assignment. */
12512 :
12513 : static void
12514 2202 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
12515 : {
12516 2202 : gfc_code *c;
12517 :
12518 2202 : c = code->block->next;
12519 4827 : while (c)
12520 : {
12521 2625 : switch (c->op)
12522 : {
12523 2309 : case EXEC_ASSIGN:
12524 2309 : case EXEC_POINTER_ASSIGN:
12525 2309 : gfc_resolve_assign_in_forall (c, nvar, var_expr);
12526 :
12527 2309 : if (c->op == EXEC_ASSIGN
12528 2309 : && gfc_may_be_finalized (c->expr1->ts))
12529 0 : c->expr1->must_finalize = 1;
12530 :
12531 : break;
12532 :
12533 0 : case EXEC_ASSIGN_CALL:
12534 0 : resolve_call (c);
12535 0 : break;
12536 :
12537 : /* Because the gfc_resolve_blocks() will handle the nested FORALL,
12538 : there is no need to handle it here. */
12539 : case EXEC_FORALL:
12540 : break;
12541 47 : case EXEC_WHERE:
12542 47 : gfc_resolve_where_code_in_forall(c, nvar, var_expr);
12543 47 : break;
12544 : default:
12545 : break;
12546 : }
12547 : /* The next statement in the FORALL body. */
12548 2625 : c = c->next;
12549 : }
12550 2202 : }
12551 :
12552 :
12553 : /* Counts the number of iterators needed inside a forall construct, including
12554 : nested forall constructs. This is used to allocate the needed memory
12555 : in gfc_resolve_forall. */
12556 :
12557 : static int gfc_count_forall_iterators (gfc_code *code);
12558 :
12559 : /* Return the deepest nested FORALL/DO CONCURRENT iterator count in CODE's
12560 : next-chain, descending into block arms such as IF/ELSE branches. */
12561 :
12562 : static int
12563 2387 : gfc_max_forall_iterators_in_chain (gfc_code *code)
12564 : {
12565 2387 : int max_iters = 0;
12566 :
12567 5226 : for (gfc_code *c = code; c; c = c->next)
12568 : {
12569 2839 : int sub_iters = 0;
12570 :
12571 2839 : if (c->op == EXEC_FORALL || c->op == EXEC_DO_CONCURRENT)
12572 94 : sub_iters = gfc_count_forall_iterators (c);
12573 2745 : else if (c->op == EXEC_BLOCK)
12574 : {
12575 : /* BLOCK/ASSOCIATE bodies live in the block namespace code chain,
12576 : not in the generic c->block arm list used by IF/SELECT. */
12577 21 : if (c->ext.block.ns && c->ext.block.ns->code)
12578 21 : sub_iters = gfc_max_forall_iterators_in_chain (c->ext.block.ns->code);
12579 : }
12580 2724 : else if (c->block)
12581 307 : for (gfc_code *b = c->block; b; b = b->block)
12582 : {
12583 164 : int arm_iters = gfc_max_forall_iterators_in_chain (b->next);
12584 164 : if (arm_iters > sub_iters)
12585 : sub_iters = arm_iters;
12586 : }
12587 :
12588 2839 : if (sub_iters > max_iters)
12589 : max_iters = sub_iters;
12590 : }
12591 :
12592 2387 : return max_iters;
12593 : }
12594 :
12595 :
12596 : static int
12597 2202 : gfc_count_forall_iterators (gfc_code *code)
12598 : {
12599 2202 : int current_iters = 0;
12600 2202 : gfc_forall_iterator *fa;
12601 :
12602 2202 : gcc_assert (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT);
12603 :
12604 6320 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12605 4118 : current_iters++;
12606 :
12607 2202 : return current_iters + gfc_max_forall_iterators_in_chain (code->block->next);
12608 : }
12609 :
12610 :
12611 : /* Given a FORALL construct.
12612 : 1) Resolve the FORALL iterator.
12613 : 2) Check for shadow index-name(s) and update code block.
12614 : 3) call gfc_resolve_forall_body to resolve the FORALL body. */
12615 :
12616 : /* Custom recursive expression walker that replaces symbols.
12617 : This ensures we visit ALL expressions including those in array subscripts. */
12618 :
12619 : static void
12620 114 : replace_in_expr_recursive (gfc_expr *expr, gfc_symbol *old_sym, gfc_symtree *new_st)
12621 : {
12622 144 : if (!expr)
12623 : return;
12624 :
12625 : /* Check if this is a variable reference to replace */
12626 108 : if (expr->expr_type == EXPR_VARIABLE && expr->symtree->n.sym == old_sym)
12627 : {
12628 18 : expr->symtree = new_st;
12629 18 : expr->ts = new_st->n.sym->ts;
12630 : }
12631 :
12632 : /* Walk through reference chain (array subscripts, substrings, etc.) */
12633 108 : for (gfc_ref *ref = expr->ref; ref; ref = ref->next)
12634 : {
12635 0 : if (ref->type == REF_ARRAY)
12636 : {
12637 : gfc_array_ref *ar = &ref->u.ar;
12638 0 : for (int i = 0; i < ar->dimen; i++)
12639 : {
12640 0 : replace_in_expr_recursive (ar->start[i], old_sym, new_st);
12641 0 : replace_in_expr_recursive (ar->end[i], old_sym, new_st);
12642 0 : replace_in_expr_recursive (ar->stride[i], old_sym, new_st);
12643 : }
12644 : }
12645 0 : else if (ref->type == REF_SUBSTRING)
12646 : {
12647 0 : replace_in_expr_recursive (ref->u.ss.start, old_sym, new_st);
12648 0 : replace_in_expr_recursive (ref->u.ss.end, old_sym, new_st);
12649 : }
12650 : }
12651 :
12652 : /* Walk through sub-expressions based on expression type */
12653 108 : switch (expr->expr_type)
12654 : {
12655 30 : case EXPR_OP:
12656 30 : replace_in_expr_recursive (expr->value.op.op1, old_sym, new_st);
12657 30 : replace_in_expr_recursive (expr->value.op.op2, old_sym, new_st);
12658 30 : break;
12659 :
12660 6 : case EXPR_FUNCTION:
12661 18 : for (gfc_actual_arglist *a = expr->value.function.actual; a; a = a->next)
12662 12 : replace_in_expr_recursive (a->expr, old_sym, new_st);
12663 : break;
12664 :
12665 0 : case EXPR_ARRAY:
12666 0 : case EXPR_STRUCTURE:
12667 0 : for (gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
12668 0 : c; c = gfc_constructor_next (c))
12669 : {
12670 0 : replace_in_expr_recursive (c->expr, old_sym, new_st);
12671 0 : if (c->iterator)
12672 : {
12673 0 : replace_in_expr_recursive (c->iterator->start, old_sym, new_st);
12674 0 : replace_in_expr_recursive (c->iterator->end, old_sym, new_st);
12675 0 : replace_in_expr_recursive (c->iterator->step, old_sym, new_st);
12676 : }
12677 : }
12678 : break;
12679 :
12680 : default:
12681 : break;
12682 : }
12683 : }
12684 :
12685 :
12686 : /* Walk code tree and replace all variable references */
12687 :
12688 : static void
12689 18 : replace_in_code_recursive (gfc_code *code, gfc_symbol *old_sym, gfc_symtree *new_st)
12690 : {
12691 18 : if (!code)
12692 : return;
12693 :
12694 36 : for (gfc_code *c = code; c; c = c->next)
12695 : {
12696 : /* Replace in expressions associated with this code node */
12697 18 : replace_in_expr_recursive (c->expr1, old_sym, new_st);
12698 18 : replace_in_expr_recursive (c->expr2, old_sym, new_st);
12699 18 : replace_in_expr_recursive (c->expr3, old_sym, new_st);
12700 18 : replace_in_expr_recursive (c->expr4, old_sym, new_st);
12701 :
12702 : /* Handle special code types with additional expressions */
12703 18 : switch (c->op)
12704 : {
12705 0 : case EXEC_DO:
12706 0 : if (c->ext.iterator)
12707 : {
12708 0 : replace_in_expr_recursive (c->ext.iterator->start, old_sym, new_st);
12709 0 : replace_in_expr_recursive (c->ext.iterator->end, old_sym, new_st);
12710 0 : replace_in_expr_recursive (c->ext.iterator->step, old_sym, new_st);
12711 : }
12712 : break;
12713 :
12714 0 : case EXEC_CALL:
12715 0 : case EXEC_ASSIGN_CALL:
12716 0 : for (gfc_actual_arglist *a = c->ext.actual; a; a = a->next)
12717 0 : replace_in_expr_recursive (a->expr, old_sym, new_st);
12718 : break;
12719 :
12720 0 : case EXEC_SELECT:
12721 0 : for (gfc_code *b = c->block; b; b = b->block)
12722 : {
12723 0 : for (gfc_case *cp = b->ext.block.case_list; cp; cp = cp->next)
12724 : {
12725 0 : replace_in_expr_recursive (cp->low, old_sym, new_st);
12726 0 : replace_in_expr_recursive (cp->high, old_sym, new_st);
12727 : }
12728 0 : replace_in_code_recursive (b->next, old_sym, new_st);
12729 : }
12730 : break;
12731 :
12732 0 : case EXEC_FORALL:
12733 0 : case EXEC_DO_CONCURRENT:
12734 0 : for (gfc_forall_iterator *fa = c->ext.concur.forall_iterator; fa; fa = fa->next)
12735 : {
12736 0 : replace_in_expr_recursive (fa->start, old_sym, new_st);
12737 0 : replace_in_expr_recursive (fa->end, old_sym, new_st);
12738 0 : replace_in_expr_recursive (fa->stride, old_sym, new_st);
12739 : }
12740 : /* Don't recurse into nested FORALL/DO CONCURRENT bodies here,
12741 : they'll be handled separately */
12742 : break;
12743 :
12744 : default:
12745 : break;
12746 : }
12747 :
12748 : /* Recurse into blocks */
12749 18 : if (c->block)
12750 0 : replace_in_code_recursive (c->block->next, old_sym, new_st);
12751 : }
12752 : }
12753 :
12754 :
12755 : /* Replace all references to outer_sym with shadow_st in the given code. */
12756 :
12757 : static void
12758 18 : gfc_replace_forall_variable (gfc_code **code_ptr, gfc_symbol *outer_sym,
12759 : gfc_symtree *shadow_st)
12760 : {
12761 : /* Use custom recursive walker to ensure we visit ALL expressions */
12762 0 : replace_in_code_recursive (*code_ptr, outer_sym, shadow_st);
12763 18 : }
12764 :
12765 :
12766 : static void
12767 2202 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
12768 : {
12769 2202 : static gfc_expr **var_expr;
12770 2202 : static int total_var = 0;
12771 2202 : static int nvar = 0;
12772 2202 : int i, old_nvar, tmp;
12773 2202 : gfc_forall_iterator *fa;
12774 2202 : bool shadow = false;
12775 :
12776 2202 : old_nvar = nvar;
12777 :
12778 : /* Only warn about obsolescent FORALL, not DO CONCURRENT */
12779 2202 : if (code->op == EXEC_FORALL
12780 2202 : && !gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12781 : return;
12782 :
12783 : /* Start to resolve a FORALL construct */
12784 : /* Allocate var_expr only at the truly outermost FORALL/DO CONCURRENT level.
12785 : forall_save==0 means we're not nested in a FORALL in the current scope,
12786 : but nvar==0 ensures we're not nested in a parent scope either (prevents
12787 : double allocation when FORALL is nested inside DO CONCURRENT). */
12788 2202 : if (forall_save == 0 && nvar == 0)
12789 : {
12790 : /* Count the total number of FORALL indices in the nested FORALL
12791 : construct in order to allocate the VAR_EXPR with proper size. */
12792 2108 : total_var = gfc_count_forall_iterators (code);
12793 :
12794 : /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12795 2108 : var_expr = XCNEWVEC (gfc_expr *, total_var);
12796 : }
12797 :
12798 : /* The information about FORALL iterator, including FORALL indices start,
12799 : end and stride. An outer FORALL indice cannot appear in start, end or
12800 : stride. Check for a shadow index-name. */
12801 6320 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12802 : {
12803 : /* Fortran 2008: C738 (R753). */
12804 4118 : if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12805 : {
12806 2 : gfc_error ("FORALL index-name at %L must be a scalar variable "
12807 : "of type integer", &fa->var->where);
12808 2 : continue;
12809 : }
12810 :
12811 : /* Check if any outer FORALL index name is the same as the current
12812 : one. Skip this check if the iterator is a shadow variable (from
12813 : DO CONCURRENT type spec) which may not have a symtree yet. */
12814 7125 : for (i = 0; i < nvar; i++)
12815 : {
12816 3009 : if (fa->var && fa->var->symtree && var_expr[i] && var_expr[i]->symtree
12817 3009 : && fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12818 0 : gfc_error ("An outer FORALL construct already has an index "
12819 : "with this name %L", &fa->var->where);
12820 : }
12821 :
12822 4116 : if (fa->shadow)
12823 18 : shadow = true;
12824 :
12825 : /* Record the current FORALL index. */
12826 4116 : var_expr[nvar] = gfc_copy_expr (fa->var);
12827 :
12828 4116 : nvar++;
12829 :
12830 : /* No memory leak. */
12831 4116 : gcc_assert (nvar <= total_var);
12832 : }
12833 :
12834 : /* Need to walk the code and replace references to the index-name with
12835 : references to the shadow index-name. This must be done BEFORE resolving
12836 : the body so that resolution uses the correct shadow variables. */
12837 2202 : if (shadow)
12838 : {
12839 : /* Walk the FORALL/DO CONCURRENT body and replace references to shadowed variables. */
12840 42 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12841 : {
12842 24 : if (fa->shadow)
12843 : {
12844 18 : gfc_symtree *shadow_st;
12845 18 : const char *shadow_name_str;
12846 18 : char *outer_name;
12847 :
12848 : /* fa->var now points to the shadow variable "_name". */
12849 18 : shadow_name_str = fa->var->symtree->name;
12850 18 : shadow_st = fa->var->symtree;
12851 :
12852 18 : if (shadow_name_str[0] != '_')
12853 0 : gfc_internal_error ("Expected shadow variable name to start with _");
12854 :
12855 18 : outer_name = (char *) alloca (strlen (shadow_name_str));
12856 18 : strcpy (outer_name, shadow_name_str + 1);
12857 :
12858 : /* Find the ITERATOR symbol in the current namespace.
12859 : This is the local DO CONCURRENT variable that body expressions reference. */
12860 18 : gfc_symtree *iter_st = gfc_find_symtree (ns->sym_root, outer_name);
12861 :
12862 18 : if (!iter_st)
12863 : /* No iterator variable found - this shouldn't happen */
12864 0 : continue;
12865 :
12866 18 : gfc_symbol *iter_sym = iter_st->n.sym;
12867 :
12868 : /* Walk the FORALL/DO CONCURRENT body and replace all references. */
12869 18 : if (code->block && code->block->next)
12870 18 : gfc_replace_forall_variable (&code->block->next, iter_sym, shadow_st);
12871 : }
12872 : }
12873 : }
12874 :
12875 : /* Resolve the FORALL body. */
12876 2202 : gfc_resolve_forall_body (code, nvar, var_expr);
12877 :
12878 : /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12879 2202 : gfc_resolve_blocks (code->block, ns);
12880 :
12881 2202 : tmp = nvar;
12882 2202 : nvar = old_nvar;
12883 : /* Free only the VAR_EXPRs allocated in this frame. */
12884 6318 : for (i = nvar; i < tmp; i++)
12885 4116 : gfc_free_expr (var_expr[i]);
12886 :
12887 2202 : if (nvar == 0)
12888 : {
12889 : /* We are in the outermost FORALL construct. */
12890 2108 : gcc_assert (forall_save == 0);
12891 :
12892 : /* VAR_EXPR is not needed any more. */
12893 2108 : free (var_expr);
12894 2108 : total_var = 0;
12895 : }
12896 : }
12897 :
12898 :
12899 : /* Resolve a BLOCK construct statement. */
12900 :
12901 : static void
12902 8056 : resolve_block_construct (gfc_code* code)
12903 : {
12904 8056 : gfc_namespace *ns = code->ext.block.ns;
12905 :
12906 : /* For an ASSOCIATE block, the associations (and their targets) will be
12907 : resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12908 : namespace. */
12909 8056 : gfc_resolve (ns);
12910 0 : }
12911 :
12912 :
12913 : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12914 : DO code nodes. */
12915 :
12916 : void
12917 332436 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12918 : {
12919 332436 : bool t;
12920 :
12921 676364 : for (; b; b = b->block)
12922 : {
12923 343928 : t = gfc_resolve_expr (b->expr1);
12924 343928 : if (!gfc_resolve_expr (b->expr2))
12925 0 : t = false;
12926 :
12927 343928 : switch (b->op)
12928 : {
12929 237569 : case EXEC_IF:
12930 237569 : if (t && b->expr1 != NULL
12931 233250 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12932 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12933 : &b->expr1->where);
12934 : break;
12935 :
12936 764 : case EXEC_WHERE:
12937 764 : if (t
12938 764 : && b->expr1 != NULL
12939 631 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12940 0 : gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12941 : &b->expr1->where);
12942 : break;
12943 :
12944 76 : case EXEC_GOTO:
12945 76 : resolve_branch (b->label1, b);
12946 76 : break;
12947 :
12948 0 : case EXEC_BLOCK:
12949 0 : resolve_block_construct (b);
12950 0 : break;
12951 :
12952 : case EXEC_SELECT:
12953 : case EXEC_SELECT_TYPE:
12954 : case EXEC_SELECT_RANK:
12955 : case EXEC_FORALL:
12956 : case EXEC_DO:
12957 : case EXEC_DO_WHILE:
12958 : case EXEC_DO_CONCURRENT:
12959 : case EXEC_CRITICAL:
12960 : case EXEC_READ:
12961 : case EXEC_WRITE:
12962 : case EXEC_IOLENGTH:
12963 : case EXEC_WAIT:
12964 : break;
12965 :
12966 2697 : case EXEC_OMP_ATOMIC:
12967 2697 : case EXEC_OACC_ATOMIC:
12968 2697 : {
12969 : /* Verify this before calling gfc_resolve_code, which might
12970 : change it. */
12971 2697 : gcc_assert (b->op == EXEC_OMP_ATOMIC
12972 : || (b->next && b->next->op == EXEC_ASSIGN));
12973 : }
12974 : break;
12975 :
12976 : case EXEC_OACC_PARALLEL_LOOP:
12977 : case EXEC_OACC_PARALLEL:
12978 : case EXEC_OACC_KERNELS_LOOP:
12979 : case EXEC_OACC_KERNELS:
12980 : case EXEC_OACC_SERIAL_LOOP:
12981 : case EXEC_OACC_SERIAL:
12982 : case EXEC_OACC_DATA:
12983 : case EXEC_OACC_HOST_DATA:
12984 : case EXEC_OACC_LOOP:
12985 : case EXEC_OACC_UPDATE:
12986 : case EXEC_OACC_WAIT:
12987 : case EXEC_OACC_CACHE:
12988 : case EXEC_OACC_ENTER_DATA:
12989 : case EXEC_OACC_EXIT_DATA:
12990 : case EXEC_OACC_ROUTINE:
12991 : case EXEC_OMP_ALLOCATE:
12992 : case EXEC_OMP_ALLOCATORS:
12993 : case EXEC_OMP_ASSUME:
12994 : case EXEC_OMP_CRITICAL:
12995 : case EXEC_OMP_DISPATCH:
12996 : case EXEC_OMP_DISTRIBUTE:
12997 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12998 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12999 : case EXEC_OMP_DISTRIBUTE_SIMD:
13000 : case EXEC_OMP_DO:
13001 : case EXEC_OMP_DO_SIMD:
13002 : case EXEC_OMP_ERROR:
13003 : case EXEC_OMP_LOOP:
13004 : case EXEC_OMP_MASKED:
13005 : case EXEC_OMP_MASKED_TASKLOOP:
13006 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13007 : case EXEC_OMP_MASTER:
13008 : case EXEC_OMP_MASTER_TASKLOOP:
13009 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13010 : case EXEC_OMP_ORDERED:
13011 : case EXEC_OMP_PARALLEL:
13012 : case EXEC_OMP_PARALLEL_DO:
13013 : case EXEC_OMP_PARALLEL_DO_SIMD:
13014 : case EXEC_OMP_PARALLEL_LOOP:
13015 : case EXEC_OMP_PARALLEL_MASKED:
13016 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13017 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13018 : case EXEC_OMP_PARALLEL_MASTER:
13019 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13020 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13021 : case EXEC_OMP_PARALLEL_SECTIONS:
13022 : case EXEC_OMP_PARALLEL_WORKSHARE:
13023 : case EXEC_OMP_SECTIONS:
13024 : case EXEC_OMP_SIMD:
13025 : case EXEC_OMP_SCOPE:
13026 : case EXEC_OMP_SINGLE:
13027 : case EXEC_OMP_TARGET:
13028 : case EXEC_OMP_TARGET_DATA:
13029 : case EXEC_OMP_TARGET_ENTER_DATA:
13030 : case EXEC_OMP_TARGET_EXIT_DATA:
13031 : case EXEC_OMP_TARGET_PARALLEL:
13032 : case EXEC_OMP_TARGET_PARALLEL_DO:
13033 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13034 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
13035 : case EXEC_OMP_TARGET_SIMD:
13036 : case EXEC_OMP_TARGET_TEAMS:
13037 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13038 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13039 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13040 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13041 : case EXEC_OMP_TARGET_TEAMS_LOOP:
13042 : case EXEC_OMP_TARGET_UPDATE:
13043 : case EXEC_OMP_TASK:
13044 : case EXEC_OMP_TASKGROUP:
13045 : case EXEC_OMP_TASKLOOP:
13046 : case EXEC_OMP_TASKLOOP_SIMD:
13047 : case EXEC_OMP_TASKWAIT:
13048 : case EXEC_OMP_TASKYIELD:
13049 : case EXEC_OMP_TEAMS:
13050 : case EXEC_OMP_TEAMS_DISTRIBUTE:
13051 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13052 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13053 : case EXEC_OMP_TEAMS_LOOP:
13054 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13055 : case EXEC_OMP_TILE:
13056 : case EXEC_OMP_UNROLL:
13057 : case EXEC_OMP_WORKSHARE:
13058 : break;
13059 :
13060 0 : default:
13061 0 : gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
13062 : }
13063 :
13064 343928 : gfc_resolve_code (b->next, ns);
13065 : }
13066 332436 : }
13067 :
13068 : bool
13069 0 : caf_possible_reallocate (gfc_expr *e)
13070 : {
13071 0 : symbol_attribute caf_attr;
13072 0 : gfc_ref *last_arr_ref = nullptr;
13073 :
13074 0 : caf_attr = gfc_caf_attr (e);
13075 0 : if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
13076 : return false;
13077 :
13078 : /* Only full array refs can indicate a needed reallocation. */
13079 0 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
13080 0 : if (ref->type == REF_ARRAY && ref->u.ar.dimen)
13081 0 : last_arr_ref = ref;
13082 :
13083 0 : return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
13084 : }
13085 :
13086 : /* Does everything to resolve an ordinary assignment. Returns true
13087 : if this is an interface assignment. */
13088 : static bool
13089 286108 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
13090 : {
13091 286108 : bool rval = false;
13092 286108 : gfc_expr *lhs;
13093 286108 : gfc_expr *rhs;
13094 286108 : int n;
13095 286108 : gfc_ref *ref;
13096 286108 : symbol_attribute attr;
13097 :
13098 286108 : if (gfc_extend_assign (code, ns))
13099 : {
13100 918 : gfc_expr** rhsptr;
13101 :
13102 918 : if (code->op == EXEC_ASSIGN_CALL)
13103 : {
13104 469 : lhs = code->ext.actual->expr;
13105 469 : rhsptr = &code->ext.actual->next->expr;
13106 : }
13107 : else
13108 : {
13109 449 : gfc_actual_arglist* args;
13110 449 : gfc_typebound_proc* tbp;
13111 :
13112 449 : gcc_assert (code->op == EXEC_COMPCALL);
13113 :
13114 449 : args = code->expr1->value.compcall.actual;
13115 449 : lhs = args->expr;
13116 449 : rhsptr = &args->next->expr;
13117 :
13118 449 : tbp = code->expr1->value.compcall.tbp;
13119 449 : gcc_assert (!tbp->is_generic);
13120 : }
13121 :
13122 : /* Make a temporary rhs when there is a default initializer
13123 : and rhs is the same symbol as the lhs. */
13124 918 : if ((*rhsptr)->expr_type == EXPR_VARIABLE
13125 507 : && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
13126 436 : && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
13127 1206 : && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
13128 60 : *rhsptr = gfc_get_parentheses (*rhsptr);
13129 :
13130 918 : return true;
13131 : }
13132 :
13133 285190 : lhs = code->expr1;
13134 285190 : rhs = code->expr2;
13135 :
13136 285190 : if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
13137 265109 : || lhs->symtree->n.sym->ts.type == BT_CLASS)
13138 22705 : && !lhs->symtree->n.sym->attr.proc_pointer
13139 307895 : && gfc_expr_attr (lhs).proc_pointer)
13140 : {
13141 1 : gfc_error ("Variable in the ordinary assignment at %L is a procedure "
13142 : "pointer component",
13143 : &lhs->where);
13144 1 : return false;
13145 : }
13146 :
13147 336064 : if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
13148 249727 : && rhs->ts.type == BT_CHARACTER
13149 285582 : && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
13150 : {
13151 : /* Use of -fdec-char-conversions allows assignment of character data
13152 : to non-character variables. This not permitted for nonconstant
13153 : strings. */
13154 29 : gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
13155 : gfc_typename (lhs), &rhs->where);
13156 29 : return false;
13157 : }
13158 :
13159 285160 : if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
13160 : {
13161 0 : gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
13162 : gfc_typename (lhs), &rhs->where);
13163 0 : return false;
13164 : }
13165 :
13166 : /* Handle the case of a BOZ literal on the RHS. */
13167 285160 : if (rhs->ts.type == BT_BOZ)
13168 : {
13169 3 : if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
13170 : "statement value nor an actual argument of "
13171 : "INT/REAL/DBLE/CMPLX intrinsic subprogram",
13172 : &rhs->where))
13173 : return false;
13174 :
13175 1 : switch (lhs->ts.type)
13176 : {
13177 0 : case BT_INTEGER:
13178 0 : if (!gfc_boz2int (rhs, lhs->ts.kind))
13179 : return false;
13180 : break;
13181 1 : case BT_REAL:
13182 1 : if (!gfc_boz2real (rhs, lhs->ts.kind))
13183 : return false;
13184 : break;
13185 0 : default:
13186 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
13187 0 : return false;
13188 : }
13189 : }
13190 :
13191 285158 : if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
13192 : {
13193 67 : HOST_WIDE_INT llen = 0, rlen = 0;
13194 67 : if (lhs->ts.u.cl != NULL
13195 67 : && lhs->ts.u.cl->length != NULL
13196 56 : && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
13197 56 : llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
13198 :
13199 67 : if (rhs->expr_type == EXPR_CONSTANT)
13200 29 : rlen = rhs->value.character.length;
13201 :
13202 38 : else if (rhs->ts.u.cl != NULL
13203 38 : && rhs->ts.u.cl->length != NULL
13204 35 : && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
13205 35 : rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
13206 :
13207 67 : if (rlen && llen && rlen > llen)
13208 28 : gfc_warning_now (OPT_Wcharacter_truncation,
13209 : "CHARACTER expression will be truncated "
13210 : "in assignment (%wd/%wd) at %L",
13211 : llen, rlen, &code->loc);
13212 : }
13213 :
13214 : /* Ensure that a vector index expression for the lvalue is evaluated
13215 : to a temporary if the lvalue symbol is referenced in it. */
13216 285158 : if (lhs->rank)
13217 : {
13218 112709 : for (ref = lhs->ref; ref; ref= ref->next)
13219 60224 : if (ref->type == REF_ARRAY)
13220 : {
13221 132791 : for (n = 0; n < ref->u.ar.dimen; n++)
13222 78504 : if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
13223 78734 : && gfc_find_sym_in_expr (lhs->symtree->n.sym,
13224 230 : ref->u.ar.start[n]))
13225 14 : ref->u.ar.start[n]
13226 14 : = gfc_get_parentheses (ref->u.ar.start[n]);
13227 : }
13228 : }
13229 :
13230 285158 : if (gfc_pure (NULL))
13231 : {
13232 3376 : if (lhs->ts.type == BT_DERIVED
13233 136 : && lhs->expr_type == EXPR_VARIABLE
13234 136 : && lhs->ts.u.derived->attr.pointer_comp
13235 4 : && rhs->expr_type == EXPR_VARIABLE
13236 3379 : && (gfc_impure_variable (rhs->symtree->n.sym)
13237 2 : || gfc_is_coindexed (rhs)))
13238 : {
13239 : /* F2008, C1283. */
13240 2 : if (gfc_is_coindexed (rhs))
13241 1 : gfc_error ("Coindexed expression at %L is assigned to "
13242 : "a derived type variable with a POINTER "
13243 : "component in a PURE procedure",
13244 : &rhs->where);
13245 : else
13246 : /* F2008, C1283 (4). */
13247 1 : gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
13248 : "shall not be used as the expr at %L of an intrinsic "
13249 : "assignment statement in which the variable is of a "
13250 : "derived type if the derived type has a pointer "
13251 : "component at any level of component selection.",
13252 : &rhs->where);
13253 2 : return rval;
13254 : }
13255 :
13256 : /* Fortran 2008, C1283. */
13257 3374 : if (gfc_is_coindexed (lhs))
13258 : {
13259 1 : gfc_error ("Assignment to coindexed variable at %L in a PURE "
13260 : "procedure", &rhs->where);
13261 1 : return rval;
13262 : }
13263 : }
13264 :
13265 285155 : if (gfc_implicit_pure (NULL))
13266 : {
13267 7249 : if (lhs->expr_type == EXPR_VARIABLE
13268 7249 : && lhs->symtree->n.sym != gfc_current_ns->proc_name
13269 5163 : && lhs->symtree->n.sym->ns != gfc_current_ns)
13270 253 : gfc_unset_implicit_pure (NULL);
13271 :
13272 7249 : if (lhs->ts.type == BT_DERIVED
13273 352 : && lhs->expr_type == EXPR_VARIABLE
13274 352 : && lhs->ts.u.derived->attr.pointer_comp
13275 7 : && rhs->expr_type == EXPR_VARIABLE
13276 7256 : && (gfc_impure_variable (rhs->symtree->n.sym)
13277 7 : || gfc_is_coindexed (rhs)))
13278 0 : gfc_unset_implicit_pure (NULL);
13279 :
13280 : /* Fortran 2008, C1283. */
13281 7249 : if (gfc_is_coindexed (lhs))
13282 0 : gfc_unset_implicit_pure (NULL);
13283 : }
13284 :
13285 : /* F2008, 7.2.1.2. */
13286 285155 : attr = gfc_expr_attr (lhs);
13287 285155 : if (lhs->ts.type == BT_CLASS && attr.allocatable)
13288 : {
13289 987 : if (attr.codimension)
13290 : {
13291 1 : gfc_error ("Assignment to polymorphic coarray at %L is not "
13292 : "permitted", &lhs->where);
13293 1 : return false;
13294 : }
13295 986 : if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
13296 : "polymorphic variable at %L", &lhs->where))
13297 : return false;
13298 985 : if (!flag_realloc_lhs)
13299 : {
13300 1 : gfc_error ("Assignment to an allocatable polymorphic variable at %L "
13301 : "requires %<-frealloc-lhs%>", &lhs->where);
13302 1 : return false;
13303 : }
13304 : }
13305 284168 : else if (lhs->ts.type == BT_CLASS)
13306 : {
13307 9 : gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
13308 : "assignment at %L - check that there is a matching specific "
13309 : "subroutine for %<=%> operator", &lhs->where);
13310 9 : return false;
13311 : }
13312 :
13313 285143 : bool lhs_coindexed = gfc_is_coindexed (lhs);
13314 :
13315 : /* F2008, Section 7.2.1.2. */
13316 285143 : if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
13317 : {
13318 1 : gfc_error ("Coindexed variable must not have an allocatable ultimate "
13319 : "component in assignment at %L", &lhs->where);
13320 1 : return false;
13321 : }
13322 :
13323 : /* Assign the 'data' of a class object to a derived type. */
13324 285142 : if (lhs->ts.type == BT_DERIVED
13325 7245 : && rhs->ts.type == BT_CLASS
13326 168 : && (rhs->expr_type != EXPR_ARRAY
13327 162 : && rhs->expr_type != EXPR_OP))
13328 156 : gfc_add_data_component (rhs);
13329 :
13330 : /* Make sure there is a vtable and, in particular, a _copy for the
13331 : rhs type. */
13332 285142 : if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
13333 615 : gfc_find_vtab (&rhs->ts);
13334 :
13335 285142 : gfc_check_assign (lhs, rhs, 1);
13336 :
13337 285142 : return false;
13338 : }
13339 :
13340 :
13341 : /* Add a component reference onto an expression. */
13342 :
13343 : static void
13344 665 : add_comp_ref (gfc_expr *e, gfc_component *c)
13345 : {
13346 665 : gfc_ref **ref;
13347 665 : ref = &(e->ref);
13348 889 : while (*ref)
13349 224 : ref = &((*ref)->next);
13350 665 : *ref = gfc_get_ref ();
13351 665 : (*ref)->type = REF_COMPONENT;
13352 665 : (*ref)->u.c.sym = e->ts.u.derived;
13353 665 : (*ref)->u.c.component = c;
13354 665 : e->ts = c->ts;
13355 :
13356 : /* Add a full array ref, as necessary. */
13357 665 : if (c->as)
13358 : {
13359 84 : gfc_add_full_array_ref (e, c->as);
13360 84 : e->rank = c->as->rank;
13361 84 : e->corank = c->as->corank;
13362 : }
13363 665 : }
13364 :
13365 :
13366 : /* Build an assignment. Keep the argument 'op' for future use, so that
13367 : pointer assignments can be made. */
13368 :
13369 : static gfc_code *
13370 988 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
13371 : gfc_component *comp1, gfc_component *comp2, locus loc)
13372 : {
13373 988 : gfc_code *this_code;
13374 :
13375 988 : this_code = gfc_get_code (op);
13376 988 : this_code->next = NULL;
13377 988 : this_code->expr1 = gfc_copy_expr (expr1);
13378 988 : this_code->expr2 = gfc_copy_expr (expr2);
13379 988 : this_code->loc = loc;
13380 988 : if (comp1 && comp2)
13381 : {
13382 288 : add_comp_ref (this_code->expr1, comp1);
13383 288 : add_comp_ref (this_code->expr2, comp2);
13384 : }
13385 :
13386 988 : return this_code;
13387 : }
13388 :
13389 :
13390 : /* Makes a temporary variable expression based on the characteristics of
13391 : a given variable expression. If allocatable is set, the temporary is
13392 : unconditionally allocatable*/
13393 :
13394 : static gfc_expr*
13395 482 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns,
13396 : bool allocatable = false)
13397 : {
13398 482 : static int serial = 0;
13399 482 : char name[GFC_MAX_SYMBOL_LEN];
13400 482 : gfc_symtree *tmp;
13401 482 : gfc_array_spec *as;
13402 482 : gfc_array_ref *aref;
13403 482 : gfc_ref *ref;
13404 :
13405 482 : sprintf (name, GFC_PREFIX("DA%d"), serial++);
13406 482 : gfc_get_sym_tree (name, ns, &tmp, false);
13407 482 : gfc_add_type (tmp->n.sym, &e->ts, NULL);
13408 :
13409 482 : if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
13410 0 : tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
13411 : NULL,
13412 0 : e->value.character.length);
13413 :
13414 482 : as = NULL;
13415 482 : ref = NULL;
13416 482 : aref = NULL;
13417 :
13418 : /* Obtain the arrayspec for the temporary. */
13419 482 : if (e->rank && e->expr_type != EXPR_ARRAY
13420 : && e->expr_type != EXPR_FUNCTION
13421 : && e->expr_type != EXPR_OP)
13422 : {
13423 52 : aref = gfc_find_array_ref (e);
13424 52 : if (e->expr_type == EXPR_VARIABLE
13425 52 : && e->symtree->n.sym->as == aref->as)
13426 : as = aref->as;
13427 : else
13428 : {
13429 0 : for (ref = e->ref; ref; ref = ref->next)
13430 0 : if (ref->type == REF_COMPONENT
13431 0 : && ref->u.c.component->as == aref->as)
13432 : {
13433 : as = aref->as;
13434 : break;
13435 : }
13436 : }
13437 : }
13438 :
13439 : /* Add the attributes and the arrayspec to the temporary. */
13440 482 : tmp->n.sym->attr = gfc_expr_attr (e);
13441 482 : tmp->n.sym->attr.function = 0;
13442 482 : tmp->n.sym->attr.proc_pointer = 0;
13443 482 : tmp->n.sym->attr.result = 0;
13444 482 : tmp->n.sym->attr.flavor = FL_VARIABLE;
13445 482 : tmp->n.sym->attr.dummy = 0;
13446 482 : tmp->n.sym->attr.use_assoc = 0;
13447 482 : tmp->n.sym->attr.intent = INTENT_UNKNOWN;
13448 :
13449 :
13450 482 : if (as && !allocatable)
13451 : {
13452 52 : tmp->n.sym->as = gfc_copy_array_spec (as);
13453 52 : if (!ref)
13454 52 : ref = e->ref;
13455 52 : if (as->type == AS_DEFERRED)
13456 46 : tmp->n.sym->attr.allocatable = 1;
13457 : }
13458 430 : else if ((e->rank || e->corank)
13459 130 : && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
13460 24 : || e->expr_type == EXPR_OP || allocatable))
13461 : {
13462 130 : tmp->n.sym->as = gfc_get_array_spec ();
13463 130 : tmp->n.sym->as->type = AS_DEFERRED;
13464 130 : tmp->n.sym->as->rank = e->rank;
13465 130 : tmp->n.sym->as->corank = e->corank;
13466 130 : tmp->n.sym->attr.allocatable = 1;
13467 130 : tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
13468 260 : tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
13469 : }
13470 : else
13471 300 : tmp->n.sym->attr.dimension = 0;
13472 :
13473 482 : gfc_set_sym_referenced (tmp->n.sym);
13474 482 : gfc_commit_symbol (tmp->n.sym);
13475 482 : e = gfc_lval_expr_from_sym (tmp->n.sym);
13476 :
13477 : /* Should the lhs be a section, use its array ref for the
13478 : temporary expression. */
13479 482 : if (aref && aref->type != AR_FULL && !allocatable)
13480 : {
13481 6 : gfc_free_ref_list (e->ref);
13482 6 : e->ref = gfc_copy_ref (ref);
13483 : }
13484 482 : return e;
13485 : }
13486 :
13487 :
13488 : /* Helper function to take an argument in a subroutine call with a dependency
13489 : on another argument, copy it to an allocatable temporary and use the
13490 : temporary in the call expression. The new code is embedded in a block to
13491 : ensure local, automatic deallocation. */
13492 :
13493 : static void
13494 36 : add_temp_assign_before_call (gfc_code *code, gfc_namespace *ns,
13495 : gfc_expr **rhsptr)
13496 : {
13497 36 : gfc_namespace *block_ns;
13498 36 : gfc_expr *tmp_var;
13499 :
13500 : /* Wrap the new code in a block so that the temporary is deallocated. */
13501 36 : block_ns = gfc_build_block_ns (ns);
13502 :
13503 : /* As it stands, the block_ns does not not stand up to resolution because the
13504 : the assignment would be converted to a call and, in any case, the modified
13505 : call fails in gfc_check_conformance. */
13506 36 : block_ns->resolved = 1;
13507 :
13508 : /* Assign the original expression to the temporary. */
13509 36 : tmp_var = get_temp_from_expr (*rhsptr, block_ns, true);
13510 72 : block_ns->code = build_assignment (EXEC_ASSIGN, tmp_var, *rhsptr,
13511 36 : NULL, NULL, (*rhsptr)->where);
13512 :
13513 : /* Transfer the call to the block and terminate block code. */
13514 36 : *rhsptr = gfc_copy_expr (tmp_var);
13515 36 : block_ns->code->next = gfc_get_code (EXEC_NOP);
13516 36 : *(block_ns->code->next) = *code;
13517 36 : block_ns->code->next->next = NULL;
13518 :
13519 : /* Convert the original code to execute the block. */
13520 36 : code->op = EXEC_BLOCK;
13521 36 : code->ext.block.ns = block_ns;
13522 36 : code->ext.block.assoc = NULL;
13523 36 : code->expr1 = code->expr2 = NULL;
13524 36 : }
13525 :
13526 :
13527 : /* Add one line of code to the code chain, making sure that 'head' and
13528 : 'tail' are appropriately updated. */
13529 :
13530 : static void
13531 656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
13532 : {
13533 656 : gcc_assert (this_code);
13534 656 : if (*head == NULL)
13535 308 : *head = *tail = *this_code;
13536 : else
13537 348 : *tail = gfc_append_code (*tail, *this_code);
13538 656 : *this_code = NULL;
13539 656 : }
13540 :
13541 :
13542 : /* Generate a final call from a variable expression */
13543 :
13544 : static void
13545 81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
13546 : {
13547 81 : gfc_code *this_code;
13548 81 : gfc_expr *final_expr = NULL;
13549 81 : gfc_expr *size_expr;
13550 81 : gfc_expr *fini_coarray;
13551 :
13552 81 : gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
13553 81 : if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
13554 75 : return;
13555 :
13556 : /* Now generate the finalizer call. */
13557 6 : this_code = gfc_get_code (EXEC_CALL);
13558 6 : this_code->symtree = final_expr->symtree;
13559 6 : this_code->resolved_sym = final_expr->symtree->n.sym;
13560 :
13561 : //* Expression to be finalized */
13562 6 : this_code->ext.actual = gfc_get_actual_arglist ();
13563 6 : this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
13564 :
13565 : /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
13566 6 : this_code->ext.actual->next = gfc_get_actual_arglist ();
13567 6 : size_expr = gfc_get_expr ();
13568 6 : size_expr->where = gfc_current_locus;
13569 6 : size_expr->expr_type = EXPR_OP;
13570 6 : size_expr->value.op.op = INTRINSIC_DIVIDE;
13571 6 : size_expr->value.op.op1
13572 12 : = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
13573 : "storage_size", gfc_current_locus, 2,
13574 6 : gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
13575 : gfc_get_int_expr (gfc_index_integer_kind,
13576 : NULL, 0));
13577 6 : size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
13578 : gfc_character_storage_size);
13579 6 : size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
13580 6 : size_expr->ts = size_expr->value.op.op1->ts;
13581 6 : this_code->ext.actual->next->expr = size_expr;
13582 :
13583 : /* fini_coarray */
13584 6 : this_code->ext.actual->next->next = gfc_get_actual_arglist ();
13585 6 : fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
13586 : &tmp_expr->where);
13587 6 : fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
13588 6 : this_code->ext.actual->next->next->expr = fini_coarray;
13589 :
13590 6 : add_code_to_chain (&this_code, head, tail);
13591 :
13592 : }
13593 :
13594 : /* Counts the potential number of part array references that would
13595 : result from resolution of typebound defined assignments. */
13596 :
13597 :
13598 : static int
13599 243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
13600 : {
13601 243 : gfc_component *c;
13602 243 : int c_depth = 0, t_depth;
13603 :
13604 584 : for (c= derived->components; c; c = c->next)
13605 : {
13606 341 : if ((!gfc_bt_struct (c->ts.type)
13607 261 : || c->attr.pointer
13608 261 : || c->attr.allocatable
13609 260 : || c->attr.proc_pointer_comp
13610 260 : || c->attr.class_pointer
13611 260 : || c->attr.proc_pointer)
13612 81 : && !c->attr.defined_assign_comp)
13613 81 : continue;
13614 :
13615 260 : if (c->as && c_depth == 0)
13616 260 : c_depth = 1;
13617 :
13618 260 : if (c->ts.u.derived->attr.defined_assign_comp)
13619 110 : t_depth = nonscalar_typebound_assign (c->ts.u.derived,
13620 : c->as ? 1 : 0);
13621 : else
13622 : t_depth = 0;
13623 :
13624 260 : c_depth = t_depth > c_depth ? t_depth : c_depth;
13625 : }
13626 243 : return depth + c_depth;
13627 : }
13628 :
13629 :
13630 : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
13631 : "An intrinsic assignment where the variable is of derived type is performed
13632 : as if each component of the variable were assigned from the corresponding
13633 : component of expr using pointer assignment (10.2.2) for each pointer
13634 : component, defined assignment for each nonpointer nonallocatable component
13635 : of a type that has a type-bound defined assignment consistent with the
13636 : component, intrinsic assignment for each other nonpointer nonallocatable
13637 : component, and intrinsic assignment for each allocated coarray component.
13638 : For unallocated coarray components, the corresponding component of the
13639 : variable shall be unallocated. For a noncoarray allocatable component the
13640 : following sequence of operations is applied.
13641 : (1) If the component of the variable is allocated, it is deallocated.
13642 : (2) If the component of the value of expr is allocated, the
13643 : corresponding component of the variable is allocated with the same
13644 : dynamic type and type parameters as the component of the value of
13645 : expr. If it is an array, it is allocated with the same bounds. The
13646 : value of the component of the value of expr is then assigned to the
13647 : corresponding component of the variable using defined assignment if
13648 : the declared type of the component has a type-bound defined
13649 : assignment consistent with the component, and intrinsic assignment
13650 : for the dynamic type of that component otherwise."
13651 :
13652 : The pointer assignments are taken care of by the intrinsic assignment of the
13653 : structure itself. This function recursively adds defined assignments where
13654 : required. The recursion is accomplished by calling gfc_resolve_code.
13655 :
13656 : When the lhs in a defined assignment has intent INOUT or is intent OUT
13657 : and the component of 'var' is finalizable, we need a temporary for the
13658 : lhs. In pseudo-code for an assignment var = expr:
13659 :
13660 : ! Confine finalization of temporaries, as far as possible.
13661 : Enclose the code for the assignment in a block
13662 : ! Only call function 'expr' once.
13663 : #if ('expr is not a constant or an variable)
13664 : temp_expr = expr
13665 : expr = temp_x
13666 : ! Do the intrinsic assignment
13667 : #if typeof ('var') has a typebound final subroutine
13668 : finalize (var)
13669 : var = expr
13670 : ! Now do the component assignments
13671 : #do over derived type components [%cmp]
13672 : #if (cmp is a pointer of any kind)
13673 : continue
13674 : build the assignment
13675 : resolve the code
13676 : #if the code is a typebound assignment
13677 : #if (arg1 is INOUT or finalizable OUT && !t1)
13678 : t1 = var
13679 : arg1 = t1
13680 : deal with allocatation or not of var and this component
13681 : #elseif the code is an assignment by itself
13682 : #if this component does not need finalization
13683 : delete code and continue
13684 : #else
13685 : remove the leading assignment
13686 : #endif
13687 : commit the code
13688 : #if (t1 and (arg1 is INOUT or finalizable OUT))
13689 : var%cmp = t1%cmp
13690 : #enddo
13691 : put all code chunks involving t1 to the top of the generated code
13692 : insert the generated block in place of the original code
13693 : */
13694 :
13695 : static bool
13696 381 : is_finalizable_type (gfc_typespec ts)
13697 : {
13698 381 : gfc_component *c;
13699 :
13700 381 : if (ts.type != BT_DERIVED)
13701 : return false;
13702 :
13703 : /* (1) Check for FINAL subroutines. */
13704 381 : if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
13705 : return true;
13706 :
13707 : /* (2) Check for components of finalizable type. */
13708 809 : for (c = ts.u.derived->components; c; c = c->next)
13709 470 : if (c->ts.type == BT_DERIVED
13710 243 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
13711 242 : && c->ts.u.derived->f2k_derived
13712 242 : && c->ts.u.derived->f2k_derived->finalizers)
13713 : return true;
13714 :
13715 : return false;
13716 : }
13717 :
13718 : /* The temporary assignments have to be put on top of the additional
13719 : code to avoid the result being changed by the intrinsic assignment.
13720 : */
13721 : static int component_assignment_level = 0;
13722 : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
13723 : static bool finalizable_comp;
13724 :
13725 : static void
13726 188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
13727 : {
13728 188 : gfc_component *comp1, *comp2;
13729 188 : gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
13730 188 : gfc_code *tmp_code = NULL;
13731 188 : gfc_expr *t1 = NULL;
13732 188 : gfc_expr *tmp_expr = NULL;
13733 188 : int error_count, depth;
13734 188 : bool finalizable_lhs;
13735 :
13736 188 : gfc_get_errors (NULL, &error_count);
13737 :
13738 : /* Filter out continuing processing after an error. */
13739 188 : if (error_count
13740 188 : || (*code)->expr1->ts.type != BT_DERIVED
13741 188 : || (*code)->expr2->ts.type != BT_DERIVED)
13742 140 : return;
13743 :
13744 : /* TODO: Handle more than one part array reference in assignments. */
13745 188 : depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
13746 188 : (*code)->expr1->rank ? 1 : 0);
13747 188 : if (depth > 1)
13748 : {
13749 6 : gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
13750 : "done because multiple part array references would "
13751 : "occur in intermediate expressions.", &(*code)->loc);
13752 6 : return;
13753 : }
13754 :
13755 182 : if (!component_assignment_level)
13756 134 : finalizable_comp = true;
13757 :
13758 : /* Build a block so that function result temporaries are finalized
13759 : locally on exiting the rather than enclosing scope. */
13760 182 : if (!component_assignment_level)
13761 : {
13762 134 : ns = gfc_build_block_ns (ns);
13763 134 : tmp_code = gfc_get_code (EXEC_NOP);
13764 134 : *tmp_code = **code;
13765 134 : tmp_code->next = NULL;
13766 134 : (*code)->op = EXEC_BLOCK;
13767 134 : (*code)->ext.block.ns = ns;
13768 134 : (*code)->ext.block.assoc = NULL;
13769 134 : (*code)->expr1 = (*code)->expr2 = NULL;
13770 134 : ns->code = tmp_code;
13771 134 : code = &ns->code;
13772 : }
13773 :
13774 182 : component_assignment_level++;
13775 :
13776 182 : finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
13777 :
13778 : /* Create a temporary so that functions get called only once. */
13779 182 : if ((*code)->expr2->expr_type != EXPR_VARIABLE
13780 182 : && (*code)->expr2->expr_type != EXPR_CONSTANT)
13781 : {
13782 : /* Assign the rhs to the temporary. */
13783 81 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13784 81 : if (tmp_expr->symtree->n.sym->attr.pointer)
13785 : {
13786 : /* Use allocate on assignment for the sake of simplicity. The
13787 : temporary must not take on the optional attribute. Assume
13788 : that the assignment is guarded by a PRESENT condition if the
13789 : lhs is optional. */
13790 25 : tmp_expr->symtree->n.sym->attr.pointer = 0;
13791 25 : tmp_expr->symtree->n.sym->attr.optional = 0;
13792 25 : tmp_expr->symtree->n.sym->attr.allocatable = 1;
13793 : }
13794 162 : this_code = build_assignment (EXEC_ASSIGN,
13795 : tmp_expr, (*code)->expr2,
13796 81 : NULL, NULL, (*code)->loc);
13797 81 : this_code->expr2->must_finalize = 1;
13798 : /* Add the code and substitute the rhs expression. */
13799 81 : add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
13800 81 : gfc_free_expr ((*code)->expr2);
13801 81 : (*code)->expr2 = tmp_expr;
13802 : }
13803 :
13804 : /* Do the intrinsic assignment. This is not needed if the lhs is one
13805 : of the temporaries generated here, since the intrinsic assignment
13806 : to the final result already does this. */
13807 182 : if ((*code)->expr1->symtree->n.sym->name[2] != '.')
13808 : {
13809 182 : if (finalizable_lhs)
13810 18 : (*code)->expr1->must_finalize = 1;
13811 182 : this_code = build_assignment (EXEC_ASSIGN,
13812 : (*code)->expr1, (*code)->expr2,
13813 : NULL, NULL, (*code)->loc);
13814 182 : add_code_to_chain (&this_code, &head, &tail);
13815 : }
13816 :
13817 182 : comp1 = (*code)->expr1->ts.u.derived->components;
13818 182 : comp2 = (*code)->expr2->ts.u.derived->components;
13819 :
13820 449 : for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
13821 : {
13822 267 : bool inout = false;
13823 267 : bool finalizable_out = false;
13824 :
13825 : /* The intrinsic assignment does the right thing for pointers
13826 : of all kinds and allocatable components. */
13827 267 : if (!gfc_bt_struct (comp1->ts.type)
13828 200 : || comp1->attr.pointer
13829 200 : || comp1->attr.allocatable
13830 199 : || comp1->attr.proc_pointer_comp
13831 199 : || comp1->attr.class_pointer
13832 199 : || comp1->attr.proc_pointer)
13833 68 : continue;
13834 :
13835 398 : finalizable_comp = is_finalizable_type (comp1->ts)
13836 199 : && !finalizable_lhs;
13837 :
13838 : /* Make an assignment for this component. */
13839 398 : this_code = build_assignment (EXEC_ASSIGN,
13840 : (*code)->expr1, (*code)->expr2,
13841 199 : comp1, comp2, (*code)->loc);
13842 :
13843 : /* Convert the assignment if there is a defined assignment for
13844 : this type. Otherwise, using the call from gfc_resolve_code,
13845 : recurse into its components. */
13846 199 : gfc_resolve_code (this_code, ns);
13847 :
13848 199 : if (this_code->op == EXEC_ASSIGN_CALL)
13849 : {
13850 144 : gfc_formal_arglist *dummy_args;
13851 144 : gfc_symbol *rsym;
13852 : /* Check that there is a typebound defined assignment. If not,
13853 : then this must be a module defined assignment. We cannot
13854 : use the defined_assign_comp attribute here because it must
13855 : be this derived type that has the defined assignment and not
13856 : a parent type. */
13857 144 : if (!(comp1->ts.u.derived->f2k_derived
13858 : && comp1->ts.u.derived->f2k_derived
13859 144 : ->tb_op[INTRINSIC_ASSIGN]))
13860 : {
13861 1 : gfc_free_statements (this_code);
13862 1 : this_code = NULL;
13863 1 : continue;
13864 : }
13865 :
13866 : /* If the first argument of the subroutine has intent INOUT
13867 : a temporary must be generated and used instead. */
13868 143 : rsym = this_code->resolved_sym;
13869 143 : dummy_args = gfc_sym_get_dummy_args (rsym);
13870 268 : finalizable_out = gfc_may_be_finalized (comp1->ts)
13871 18 : && dummy_args
13872 161 : && dummy_args->sym->attr.intent == INTENT_OUT;
13873 286 : inout = dummy_args
13874 268 : && dummy_args->sym->attr.intent == INTENT_INOUT;
13875 72 : if ((inout || finalizable_out)
13876 89 : && !comp1->attr.allocatable)
13877 : {
13878 89 : gfc_code *temp_code;
13879 89 : inout = true;
13880 :
13881 : /* Build the temporary required for the assignment and put
13882 : it at the head of the generated code. */
13883 89 : if (!t1)
13884 : {
13885 89 : gfc_namespace *tmp_ns = ns;
13886 89 : if (ns->parent && gfc_may_be_finalized (comp1->ts))
13887 18 : tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13888 89 : t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13889 89 : t1->symtree->n.sym->attr.artificial = 1;
13890 178 : temp_code = build_assignment (EXEC_ASSIGN,
13891 : t1, (*code)->expr1,
13892 89 : NULL, NULL, (*code)->loc);
13893 :
13894 : /* For allocatable LHS, check whether it is allocated. Note
13895 : that allocatable components with defined assignment are
13896 : not yet support. See PR 57696. */
13897 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13898 : {
13899 24 : gfc_code *block;
13900 24 : gfc_expr *e =
13901 24 : gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13902 24 : block = gfc_get_code (EXEC_IF);
13903 24 : block->block = gfc_get_code (EXEC_IF);
13904 24 : block->block->expr1
13905 48 : = gfc_build_intrinsic_call (ns,
13906 : GFC_ISYM_ALLOCATED, "allocated",
13907 24 : (*code)->loc, 1, e);
13908 24 : block->block->next = temp_code;
13909 24 : temp_code = block;
13910 : }
13911 89 : add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13912 : }
13913 :
13914 : /* Replace the first actual arg with the component of the
13915 : temporary. */
13916 89 : gfc_free_expr (this_code->ext.actual->expr);
13917 89 : this_code->ext.actual->expr = gfc_copy_expr (t1);
13918 89 : add_comp_ref (this_code->ext.actual->expr, comp1);
13919 :
13920 : /* If the LHS variable is allocatable and wasn't allocated and
13921 : the temporary is allocatable, pointer assign the address of
13922 : the freshly allocated LHS to the temporary. */
13923 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable
13924 89 : && gfc_expr_attr ((*code)->expr1).allocatable)
13925 : {
13926 18 : gfc_code *block;
13927 18 : gfc_expr *cond;
13928 :
13929 18 : cond = gfc_get_expr ();
13930 18 : cond->ts.type = BT_LOGICAL;
13931 18 : cond->ts.kind = gfc_default_logical_kind;
13932 18 : cond->expr_type = EXPR_OP;
13933 18 : cond->where = (*code)->loc;
13934 18 : cond->value.op.op = INTRINSIC_NOT;
13935 18 : cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13936 : GFC_ISYM_ALLOCATED, "allocated",
13937 18 : (*code)->loc, 1, gfc_copy_expr (t1));
13938 18 : block = gfc_get_code (EXEC_IF);
13939 18 : block->block = gfc_get_code (EXEC_IF);
13940 18 : block->block->expr1 = cond;
13941 36 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13942 : t1, (*code)->expr1,
13943 18 : NULL, NULL, (*code)->loc);
13944 18 : add_code_to_chain (&block, &head, &tail);
13945 : }
13946 : }
13947 : }
13948 55 : else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13949 : {
13950 : /* Don't add intrinsic assignments since they are already
13951 : effected by the intrinsic assignment of the structure, unless
13952 : finalization is required. */
13953 7 : if (finalizable_comp)
13954 0 : this_code->expr1->must_finalize = 1;
13955 : else
13956 : {
13957 7 : gfc_free_statements (this_code);
13958 7 : this_code = NULL;
13959 7 : continue;
13960 : }
13961 : }
13962 : else
13963 : {
13964 : /* Resolution has expanded an assignment of a derived type with
13965 : defined assigned components. Remove the redundant, leading
13966 : assignment. */
13967 48 : gcc_assert (this_code->op == EXEC_ASSIGN);
13968 48 : gfc_code *tmp = this_code;
13969 48 : this_code = this_code->next;
13970 48 : tmp->next = NULL;
13971 48 : gfc_free_statements (tmp);
13972 : }
13973 :
13974 191 : add_code_to_chain (&this_code, &head, &tail);
13975 :
13976 191 : if (t1 && (inout || finalizable_out))
13977 : {
13978 : /* Transfer the value to the final result. */
13979 178 : this_code = build_assignment (EXEC_ASSIGN,
13980 : (*code)->expr1, t1,
13981 89 : comp1, comp2, (*code)->loc);
13982 89 : this_code->expr1->must_finalize = 0;
13983 89 : add_code_to_chain (&this_code, &head, &tail);
13984 : }
13985 : }
13986 :
13987 : /* Put the temporary assignments at the top of the generated code. */
13988 182 : if (tmp_head && component_assignment_level == 1)
13989 : {
13990 126 : gfc_append_code (tmp_head, head);
13991 126 : head = tmp_head;
13992 126 : tmp_head = tmp_tail = NULL;
13993 : }
13994 :
13995 : /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13996 : not accidentally deallocated. Hence, nullify t1. */
13997 89 : if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13998 271 : && gfc_expr_attr ((*code)->expr1).allocatable)
13999 : {
14000 18 : gfc_code *block;
14001 18 : gfc_expr *cond;
14002 18 : gfc_expr *e;
14003 :
14004 18 : e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
14005 18 : cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
14006 18 : (*code)->loc, 2, gfc_copy_expr (t1), e);
14007 18 : block = gfc_get_code (EXEC_IF);
14008 18 : block->block = gfc_get_code (EXEC_IF);
14009 18 : block->block->expr1 = cond;
14010 18 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
14011 : t1, gfc_get_null_expr (&(*code)->loc),
14012 18 : NULL, NULL, (*code)->loc);
14013 18 : gfc_append_code (tail, block);
14014 18 : tail = block;
14015 : }
14016 :
14017 182 : component_assignment_level--;
14018 :
14019 : /* Make an explicit final call for the function result. */
14020 182 : if (tmp_expr)
14021 81 : generate_final_call (tmp_expr, &head, &tail);
14022 :
14023 182 : if (tmp_code)
14024 : {
14025 134 : ns->code = head;
14026 134 : return;
14027 : }
14028 :
14029 : /* Now attach the remaining code chain to the input code. Step on
14030 : to the end of the new code since resolution is complete. */
14031 48 : gcc_assert ((*code)->op == EXEC_ASSIGN);
14032 48 : tail->next = (*code)->next;
14033 : /* Overwrite 'code' because this would place the intrinsic assignment
14034 : before the temporary for the lhs is created. */
14035 48 : gfc_free_expr ((*code)->expr1);
14036 48 : gfc_free_expr ((*code)->expr2);
14037 48 : **code = *head;
14038 48 : if (head != tail)
14039 48 : free (head);
14040 48 : *code = tail;
14041 : }
14042 :
14043 :
14044 : /* F2008: Pointer function assignments are of the form:
14045 : ptr_fcn (args) = expr
14046 : This function breaks these assignments into two statements:
14047 : temporary_pointer => ptr_fcn(args)
14048 : temporary_pointer = expr */
14049 :
14050 : static bool
14051 286352 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
14052 : {
14053 286352 : gfc_expr *tmp_ptr_expr;
14054 286352 : gfc_code *this_code;
14055 286352 : gfc_component *comp;
14056 286352 : gfc_symbol *s;
14057 :
14058 286352 : if ((*code)->expr1->expr_type != EXPR_FUNCTION)
14059 : return false;
14060 :
14061 : /* Even if standard does not support this feature, continue to build
14062 : the two statements to avoid upsetting frontend_passes.c. */
14063 205 : gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
14064 : "%L", &(*code)->loc);
14065 :
14066 205 : comp = gfc_get_proc_ptr_comp ((*code)->expr1);
14067 :
14068 205 : if (comp)
14069 6 : s = comp->ts.interface;
14070 : else
14071 199 : s = (*code)->expr1->symtree->n.sym;
14072 :
14073 205 : if (s == NULL || !s->result->attr.pointer)
14074 : {
14075 5 : gfc_error ("The function result on the lhs of the assignment at "
14076 : "%L must have the pointer attribute.",
14077 5 : &(*code)->expr1->where);
14078 5 : (*code)->op = EXEC_NOP;
14079 5 : return false;
14080 : }
14081 :
14082 200 : tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
14083 :
14084 : /* get_temp_from_expression is set up for ordinary assignments. To that
14085 : end, where array bounds are not known, arrays are made allocatable.
14086 : Change the temporary to a pointer here. */
14087 200 : tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
14088 200 : tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
14089 200 : tmp_ptr_expr->where = (*code)->loc;
14090 :
14091 : /* A new charlen is required to ensure that the variable string length
14092 : is different to that of the original lhs for deferred results. */
14093 200 : if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
14094 : {
14095 60 : tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
14096 60 : tmp_ptr_expr->ts.deferred = 1;
14097 60 : tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
14098 60 : gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
14099 60 : tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
14100 : }
14101 :
14102 400 : this_code = build_assignment (EXEC_ASSIGN,
14103 : tmp_ptr_expr, (*code)->expr2,
14104 200 : NULL, NULL, (*code)->loc);
14105 200 : this_code->next = (*code)->next;
14106 200 : (*code)->next = this_code;
14107 200 : (*code)->op = EXEC_POINTER_ASSIGN;
14108 200 : (*code)->expr2 = (*code)->expr1;
14109 200 : (*code)->expr1 = tmp_ptr_expr;
14110 :
14111 200 : return true;
14112 : }
14113 :
14114 :
14115 : /* Deferred character length assignments from an operator expression
14116 : require a temporary because the character length of the lhs can
14117 : change in the course of the assignment. */
14118 :
14119 : static bool
14120 285190 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
14121 : {
14122 285190 : gfc_expr *tmp_expr;
14123 285190 : gfc_code *this_code;
14124 :
14125 285190 : if (!((*code)->expr1->ts.type == BT_CHARACTER
14126 27219 : && (*code)->expr1->ts.deferred && (*code)->expr1->rank
14127 836 : && (*code)->expr2->ts.type == BT_CHARACTER
14128 835 : && (*code)->expr2->expr_type == EXPR_OP))
14129 : return false;
14130 :
14131 34 : if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
14132 : return false;
14133 :
14134 28 : if (gfc_expr_attr ((*code)->expr1).pointer)
14135 : return false;
14136 :
14137 22 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
14138 22 : tmp_expr->where = (*code)->loc;
14139 :
14140 : /* A new charlen is required to ensure that the variable string
14141 : length is different to that of the original lhs. */
14142 22 : tmp_expr->ts.u.cl = gfc_get_charlen();
14143 22 : tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
14144 22 : tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
14145 22 : (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
14146 :
14147 22 : tmp_expr->symtree->n.sym->ts.deferred = 1;
14148 :
14149 22 : this_code = build_assignment (EXEC_ASSIGN,
14150 22 : (*code)->expr1,
14151 : gfc_copy_expr (tmp_expr),
14152 : NULL, NULL, (*code)->loc);
14153 :
14154 22 : (*code)->expr1 = tmp_expr;
14155 :
14156 22 : this_code->next = (*code)->next;
14157 22 : (*code)->next = this_code;
14158 :
14159 22 : return true;
14160 : }
14161 :
14162 :
14163 : /* Given a block of code, recursively resolve everything pointed to by this
14164 : code block. */
14165 :
14166 : void
14167 679829 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
14168 : {
14169 679829 : int omp_workshare_save;
14170 679829 : int forall_save, do_concurrent_save;
14171 679829 : code_stack frame;
14172 679829 : bool t;
14173 :
14174 679829 : frame.prev = cs_base;
14175 679829 : frame.head = code;
14176 679829 : cs_base = &frame;
14177 :
14178 679829 : find_reachable_labels (code);
14179 :
14180 1816924 : for (; code; code = code->next)
14181 : {
14182 1137096 : frame.current = code;
14183 1137096 : forall_save = forall_flag;
14184 1137096 : do_concurrent_save = gfc_do_concurrent_flag;
14185 :
14186 1137096 : if (code->op == EXEC_FORALL || code->op == EXEC_DO_CONCURRENT)
14187 : {
14188 2202 : if (code->op == EXEC_FORALL)
14189 1992 : forall_flag = 1;
14190 210 : else if (code->op == EXEC_DO_CONCURRENT)
14191 210 : gfc_do_concurrent_flag = 1;
14192 2202 : gfc_resolve_forall (code, ns, forall_save);
14193 2202 : if (code->op == EXEC_FORALL)
14194 1992 : forall_flag = 2;
14195 210 : else if (code->op == EXEC_DO_CONCURRENT)
14196 210 : gfc_do_concurrent_flag = 2;
14197 : }
14198 1134894 : else if (code->op == EXEC_OMP_METADIRECTIVE)
14199 138 : for (gfc_omp_variant *variant
14200 : = code->ext.omp_variants;
14201 448 : variant; variant = variant->next)
14202 310 : gfc_resolve_code (variant->code, ns);
14203 1134756 : else if (code->block)
14204 : {
14205 330237 : omp_workshare_save = -1;
14206 330237 : switch (code->op)
14207 : {
14208 10119 : case EXEC_OACC_PARALLEL_LOOP:
14209 10119 : case EXEC_OACC_PARALLEL:
14210 10119 : case EXEC_OACC_KERNELS_LOOP:
14211 10119 : case EXEC_OACC_KERNELS:
14212 10119 : case EXEC_OACC_SERIAL_LOOP:
14213 10119 : case EXEC_OACC_SERIAL:
14214 10119 : case EXEC_OACC_DATA:
14215 10119 : case EXEC_OACC_HOST_DATA:
14216 10119 : case EXEC_OACC_LOOP:
14217 10119 : gfc_resolve_oacc_blocks (code, ns);
14218 10119 : break;
14219 54 : case EXEC_OMP_PARALLEL_WORKSHARE:
14220 54 : omp_workshare_save = omp_workshare_flag;
14221 54 : omp_workshare_flag = 1;
14222 54 : gfc_resolve_omp_parallel_blocks (code, ns);
14223 54 : break;
14224 5977 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
14225 5977 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
14226 5977 : case EXEC_OMP_MASKED_TASKLOOP:
14227 5977 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
14228 5977 : case EXEC_OMP_MASTER_TASKLOOP:
14229 5977 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
14230 5977 : case EXEC_OMP_PARALLEL:
14231 5977 : case EXEC_OMP_PARALLEL_DO:
14232 5977 : case EXEC_OMP_PARALLEL_DO_SIMD:
14233 5977 : case EXEC_OMP_PARALLEL_LOOP:
14234 5977 : case EXEC_OMP_PARALLEL_MASKED:
14235 5977 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14236 5977 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14237 5977 : case EXEC_OMP_PARALLEL_MASTER:
14238 5977 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14239 5977 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14240 5977 : case EXEC_OMP_PARALLEL_SECTIONS:
14241 5977 : case EXEC_OMP_TARGET_PARALLEL:
14242 5977 : case EXEC_OMP_TARGET_PARALLEL_DO:
14243 5977 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14244 5977 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14245 5977 : case EXEC_OMP_TARGET_TEAMS:
14246 5977 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14247 5977 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14248 5977 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14249 5977 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14250 5977 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14251 5977 : case EXEC_OMP_TASK:
14252 5977 : case EXEC_OMP_TASKLOOP:
14253 5977 : case EXEC_OMP_TASKLOOP_SIMD:
14254 5977 : case EXEC_OMP_TEAMS:
14255 5977 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14256 5977 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14257 5977 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14258 5977 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14259 5977 : case EXEC_OMP_TEAMS_LOOP:
14260 5977 : omp_workshare_save = omp_workshare_flag;
14261 5977 : omp_workshare_flag = 0;
14262 5977 : gfc_resolve_omp_parallel_blocks (code, ns);
14263 5977 : break;
14264 3063 : case EXEC_OMP_DISTRIBUTE:
14265 3063 : case EXEC_OMP_DISTRIBUTE_SIMD:
14266 3063 : case EXEC_OMP_DO:
14267 3063 : case EXEC_OMP_DO_SIMD:
14268 3063 : case EXEC_OMP_LOOP:
14269 3063 : case EXEC_OMP_SIMD:
14270 3063 : case EXEC_OMP_TARGET_SIMD:
14271 3063 : case EXEC_OMP_TILE:
14272 3063 : case EXEC_OMP_UNROLL:
14273 3063 : gfc_resolve_omp_do_blocks (code, ns);
14274 3063 : break;
14275 : case EXEC_SELECT_TYPE:
14276 : case EXEC_SELECT_RANK:
14277 : /* Blocks are handled in resolve_select_type/rank because we
14278 : have to transform the SELECT TYPE into ASSOCIATE first. */
14279 : break;
14280 : case EXEC_DO_CONCURRENT:
14281 : gfc_do_concurrent_flag = 1;
14282 : gfc_resolve_blocks (code->block, ns);
14283 : gfc_do_concurrent_flag = 2;
14284 : break;
14285 39 : case EXEC_OMP_WORKSHARE:
14286 39 : omp_workshare_save = omp_workshare_flag;
14287 39 : omp_workshare_flag = 1;
14288 : /* FALL THROUGH */
14289 306979 : default:
14290 306979 : gfc_resolve_blocks (code->block, ns);
14291 306979 : break;
14292 : }
14293 :
14294 326192 : if (omp_workshare_save != -1)
14295 6070 : omp_workshare_flag = omp_workshare_save;
14296 : }
14297 804519 : start:
14298 1137301 : t = true;
14299 1137301 : if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
14300 1135896 : t = gfc_resolve_expr (code->expr1);
14301 :
14302 1137301 : forall_flag = forall_save;
14303 1137301 : gfc_do_concurrent_flag = do_concurrent_save;
14304 :
14305 1137301 : if (!gfc_resolve_expr (code->expr2))
14306 637 : t = false;
14307 :
14308 1137301 : if (code->op == EXEC_ALLOCATE
14309 1137301 : && !gfc_resolve_expr (code->expr3))
14310 : t = false;
14311 :
14312 1137301 : switch (code->op)
14313 : {
14314 : case EXEC_NOP:
14315 : case EXEC_END_BLOCK:
14316 : case EXEC_END_NESTED_BLOCK:
14317 : case EXEC_CYCLE:
14318 : case EXEC_PAUSE:
14319 : break;
14320 :
14321 217924 : case EXEC_STOP:
14322 217924 : case EXEC_ERROR_STOP:
14323 217924 : if (code->expr2 != NULL
14324 37 : && (code->expr2->ts.type != BT_LOGICAL
14325 37 : || code->expr2->rank != 0))
14326 0 : gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
14327 : &code->expr2->where);
14328 : break;
14329 :
14330 : case EXEC_EXIT:
14331 : case EXEC_CONTINUE:
14332 : case EXEC_DT_END:
14333 : case EXEC_ASSIGN_CALL:
14334 : break;
14335 :
14336 54 : case EXEC_CRITICAL:
14337 54 : resolve_critical (code);
14338 54 : break;
14339 :
14340 1317 : case EXEC_SYNC_ALL:
14341 1317 : case EXEC_SYNC_IMAGES:
14342 1317 : case EXEC_SYNC_MEMORY:
14343 1317 : resolve_sync (code);
14344 1317 : break;
14345 :
14346 197 : case EXEC_LOCK:
14347 197 : case EXEC_UNLOCK:
14348 197 : case EXEC_EVENT_POST:
14349 197 : case EXEC_EVENT_WAIT:
14350 197 : resolve_lock_unlock_event (code);
14351 197 : break;
14352 :
14353 : case EXEC_FAIL_IMAGE:
14354 : break;
14355 :
14356 130 : case EXEC_FORM_TEAM:
14357 130 : resolve_form_team (code);
14358 130 : break;
14359 :
14360 73 : case EXEC_CHANGE_TEAM:
14361 73 : resolve_change_team (code);
14362 73 : break;
14363 :
14364 71 : case EXEC_END_TEAM:
14365 71 : resolve_end_team (code);
14366 71 : break;
14367 :
14368 43 : case EXEC_SYNC_TEAM:
14369 43 : resolve_sync_team (code);
14370 43 : break;
14371 :
14372 1491 : case EXEC_ENTRY:
14373 : /* Keep track of which entry we are up to. */
14374 1491 : current_entry_id = code->ext.entry->id;
14375 1491 : break;
14376 :
14377 453 : case EXEC_WHERE:
14378 453 : resolve_where (code, NULL);
14379 453 : break;
14380 :
14381 1250 : case EXEC_GOTO:
14382 1250 : if (code->expr1 != NULL)
14383 : {
14384 78 : if (code->expr1->expr_type != EXPR_VARIABLE
14385 76 : || code->expr1->ts.type != BT_INTEGER
14386 76 : || (code->expr1->ref
14387 1 : && code->expr1->ref->type == REF_ARRAY)
14388 75 : || code->expr1->symtree == NULL
14389 75 : || (code->expr1->symtree->n.sym
14390 75 : && (code->expr1->symtree->n.sym->attr.flavor
14391 75 : == FL_PARAMETER)))
14392 4 : gfc_error ("ASSIGNED GOTO statement at %L requires a "
14393 : "scalar INTEGER variable", &code->expr1->where);
14394 74 : else if (code->expr1->symtree->n.sym
14395 74 : && code->expr1->symtree->n.sym->attr.assign != 1)
14396 1 : gfc_error ("Variable %qs has not been assigned a target "
14397 : "label at %L", code->expr1->symtree->n.sym->name,
14398 : &code->expr1->where);
14399 : }
14400 : else
14401 1172 : resolve_branch (code->label1, code);
14402 : break;
14403 :
14404 3224 : case EXEC_RETURN:
14405 3224 : if (code->expr1 != NULL
14406 53 : && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
14407 1 : gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
14408 : "INTEGER return specifier", &code->expr1->where);
14409 : break;
14410 :
14411 : case EXEC_INIT_ASSIGN:
14412 : case EXEC_END_PROCEDURE:
14413 : break;
14414 :
14415 287527 : case EXEC_ASSIGN:
14416 287527 : if (!t)
14417 : break;
14418 :
14419 286852 : if (flag_coarray == GFC_FCOARRAY_LIB
14420 286852 : && gfc_is_coindexed (code->expr1))
14421 : {
14422 : /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
14423 : coindexed variable. */
14424 500 : code->op = EXEC_CALL;
14425 500 : gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
14426 : true);
14427 500 : code->resolved_sym = code->symtree->n.sym;
14428 500 : code->resolved_sym->attr.flavor = FL_PROCEDURE;
14429 500 : code->resolved_sym->attr.intrinsic = 1;
14430 500 : code->resolved_sym->attr.subroutine = 1;
14431 500 : code->resolved_isym
14432 500 : = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
14433 500 : gfc_commit_symbol (code->resolved_sym);
14434 500 : code->ext.actual = gfc_get_actual_arglist ();
14435 500 : code->ext.actual->expr = code->expr1;
14436 500 : code->ext.actual->next = gfc_get_actual_arglist ();
14437 500 : if (code->expr2->expr_type != EXPR_VARIABLE
14438 500 : && code->expr2->expr_type != EXPR_CONSTANT)
14439 : {
14440 : /* Convert assignments of expr1[...] = expr2 into
14441 : tvar = expr2
14442 : expr1[...] = tvar
14443 : when expr2 is not trivial. */
14444 54 : gfc_expr *tvar = get_temp_from_expr (code->expr2, ns);
14445 54 : gfc_code next_code = *code;
14446 54 : gfc_code *rhs_code
14447 108 : = build_assignment (EXEC_ASSIGN, tvar, code->expr2, NULL,
14448 54 : NULL, code->expr2->where);
14449 54 : *code = *rhs_code;
14450 54 : code->next = rhs_code;
14451 54 : *rhs_code = next_code;
14452 :
14453 54 : rhs_code->ext.actual->next->expr = tvar;
14454 54 : rhs_code->expr1 = NULL;
14455 54 : rhs_code->expr2 = NULL;
14456 : }
14457 : else
14458 : {
14459 446 : code->ext.actual->next->expr = code->expr2;
14460 :
14461 446 : code->expr1 = NULL;
14462 446 : code->expr2 = NULL;
14463 : }
14464 : break;
14465 : }
14466 :
14467 286352 : if (code->expr1->ts.type == BT_CLASS)
14468 1114 : gfc_find_vtab (&code->expr2->ts);
14469 :
14470 : /* If this is a pointer function in an lvalue variable context,
14471 : the new code will have to be resolved afresh. This is also the
14472 : case with an error, where the code is transformed into NOP to
14473 : prevent ICEs downstream. */
14474 286352 : if (resolve_ptr_fcn_assign (&code, ns)
14475 286352 : || code->op == EXEC_NOP)
14476 205 : goto start;
14477 :
14478 286147 : if (!gfc_check_vardef_context (code->expr1, false, false, false,
14479 286147 : _("assignment")))
14480 : break;
14481 :
14482 286108 : if (resolve_ordinary_assign (code, ns))
14483 : {
14484 918 : if (omp_workshare_flag)
14485 : {
14486 1 : gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
14487 1 : "at %L", &code->loc);
14488 1 : break;
14489 : }
14490 917 : if (code->op == EXEC_COMPCALL)
14491 449 : goto compcall;
14492 : else
14493 468 : goto call;
14494 : }
14495 :
14496 : /* Check for dependencies in deferred character length array
14497 : assignments and generate a temporary, if necessary. */
14498 285190 : if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
14499 : break;
14500 :
14501 : /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
14502 285168 : if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
14503 7248 : && code->expr1->ts.u.derived
14504 7248 : && code->expr1->ts.u.derived->attr.defined_assign_comp)
14505 188 : generate_component_assignments (&code, ns);
14506 284980 : else if (code->op == EXEC_ASSIGN)
14507 : {
14508 284980 : if (gfc_may_be_finalized (code->expr1->ts))
14509 1289 : code->expr1->must_finalize = 1;
14510 284980 : if (code->expr2->expr_type == EXPR_ARRAY
14511 284980 : && gfc_may_be_finalized (code->expr2->ts))
14512 73 : code->expr2->must_finalize = 1;
14513 : }
14514 :
14515 : break;
14516 :
14517 126 : case EXEC_LABEL_ASSIGN:
14518 126 : if (code->label1->defined == ST_LABEL_UNKNOWN)
14519 0 : gfc_error ("Label %d referenced at %L is never defined",
14520 : code->label1->value, &code->label1->where);
14521 126 : if (t
14522 126 : && (code->expr1->expr_type != EXPR_VARIABLE
14523 126 : || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
14524 126 : || code->expr1->symtree->n.sym->ts.kind
14525 126 : != gfc_default_integer_kind
14526 126 : || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
14527 125 : || code->expr1->symtree->n.sym->as != NULL))
14528 2 : gfc_error ("ASSIGN statement at %L requires a scalar "
14529 : "default INTEGER variable", &code->expr1->where);
14530 : break;
14531 :
14532 10429 : case EXEC_POINTER_ASSIGN:
14533 10429 : {
14534 10429 : gfc_expr* e;
14535 :
14536 10429 : if (!t)
14537 : break;
14538 :
14539 : /* This is both a variable definition and pointer assignment
14540 : context, so check both of them. For rank remapping, a final
14541 : array ref may be present on the LHS and fool gfc_expr_attr
14542 : used in gfc_check_vardef_context. Remove it. */
14543 10424 : e = remove_last_array_ref (code->expr1);
14544 20848 : t = gfc_check_vardef_context (e, true, false, false,
14545 10424 : _("pointer assignment"));
14546 10424 : if (t)
14547 10395 : t = gfc_check_vardef_context (e, false, false, false,
14548 10395 : _("pointer assignment"));
14549 10424 : gfc_free_expr (e);
14550 :
14551 1147377 : t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
14552 :
14553 10282 : if (!t)
14554 : break;
14555 :
14556 : /* Assigning a class object always is a regular assign. */
14557 10282 : if (code->expr2->ts.type == BT_CLASS
14558 581 : && code->expr1->ts.type == BT_CLASS
14559 490 : && CLASS_DATA (code->expr2)
14560 489 : && !CLASS_DATA (code->expr2)->attr.dimension
14561 10918 : && !(gfc_expr_attr (code->expr1).proc_pointer
14562 55 : && code->expr2->expr_type == EXPR_VARIABLE
14563 43 : && code->expr2->symtree->n.sym->attr.flavor
14564 43 : == FL_PROCEDURE))
14565 339 : code->op = EXEC_ASSIGN;
14566 : break;
14567 : }
14568 :
14569 72 : case EXEC_ARITHMETIC_IF:
14570 72 : {
14571 72 : gfc_expr *e = code->expr1;
14572 :
14573 72 : gfc_resolve_expr (e);
14574 72 : if (e->expr_type == EXPR_NULL)
14575 1 : gfc_error ("Invalid NULL at %L", &e->where);
14576 :
14577 72 : if (t && (e->rank > 0
14578 68 : || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
14579 5 : gfc_error ("Arithmetic IF statement at %L requires a scalar "
14580 : "REAL or INTEGER expression", &e->where);
14581 :
14582 72 : resolve_branch (code->label1, code);
14583 72 : resolve_branch (code->label2, code);
14584 72 : resolve_branch (code->label3, code);
14585 : }
14586 72 : break;
14587 :
14588 231372 : case EXEC_IF:
14589 231372 : if (t && code->expr1 != NULL
14590 0 : && (code->expr1->ts.type != BT_LOGICAL
14591 0 : || code->expr1->rank != 0))
14592 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
14593 : &code->expr1->where);
14594 : break;
14595 :
14596 79828 : case EXEC_CALL:
14597 79828 : call:
14598 79828 : resolve_call (code);
14599 79828 : break;
14600 :
14601 1730 : case EXEC_COMPCALL:
14602 1730 : compcall:
14603 1730 : resolve_typebound_subroutine (code);
14604 1730 : break;
14605 :
14606 124 : case EXEC_CALL_PPC:
14607 124 : resolve_ppc_call (code);
14608 124 : break;
14609 :
14610 687 : case EXEC_SELECT:
14611 : /* Select is complicated. Also, a SELECT construct could be
14612 : a transformed computed GOTO. */
14613 687 : resolve_select (code, false);
14614 687 : break;
14615 :
14616 3047 : case EXEC_SELECT_TYPE:
14617 3047 : resolve_select_type (code, ns);
14618 3047 : break;
14619 :
14620 1024 : case EXEC_SELECT_RANK:
14621 1024 : resolve_select_rank (code, ns);
14622 1024 : break;
14623 :
14624 7983 : case EXEC_BLOCK:
14625 7983 : resolve_block_construct (code);
14626 7983 : break;
14627 :
14628 32840 : case EXEC_DO:
14629 32840 : if (code->ext.iterator != NULL)
14630 : {
14631 32840 : gfc_iterator *iter = code->ext.iterator;
14632 32840 : if (gfc_resolve_iterator (iter, true, false))
14633 32826 : gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
14634 : true);
14635 : }
14636 : break;
14637 :
14638 531 : case EXEC_DO_WHILE:
14639 531 : if (code->expr1 == NULL)
14640 0 : gfc_internal_error ("gfc_resolve_code(): No expression on "
14641 : "DO WHILE");
14642 531 : if (t
14643 531 : && (code->expr1->rank != 0
14644 531 : || code->expr1->ts.type != BT_LOGICAL))
14645 0 : gfc_error ("Exit condition of DO WHILE loop at %L must be "
14646 : "a scalar LOGICAL expression", &code->expr1->where);
14647 : break;
14648 :
14649 14328 : case EXEC_ALLOCATE:
14650 14328 : if (t)
14651 14326 : resolve_allocate_deallocate (code, "ALLOCATE");
14652 :
14653 : break;
14654 :
14655 6081 : case EXEC_DEALLOCATE:
14656 6081 : if (t)
14657 6081 : resolve_allocate_deallocate (code, "DEALLOCATE");
14658 :
14659 : break;
14660 :
14661 3906 : case EXEC_OPEN:
14662 3906 : if (!gfc_resolve_open (code->ext.open, &code->loc))
14663 : break;
14664 :
14665 3679 : resolve_branch (code->ext.open->err, code);
14666 3679 : break;
14667 :
14668 3094 : case EXEC_CLOSE:
14669 3094 : if (!gfc_resolve_close (code->ext.close, &code->loc))
14670 : break;
14671 :
14672 3060 : resolve_branch (code->ext.close->err, code);
14673 3060 : break;
14674 :
14675 2809 : case EXEC_BACKSPACE:
14676 2809 : case EXEC_ENDFILE:
14677 2809 : case EXEC_REWIND:
14678 2809 : case EXEC_FLUSH:
14679 2809 : if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
14680 : break;
14681 :
14682 2743 : resolve_branch (code->ext.filepos->err, code);
14683 2743 : break;
14684 :
14685 836 : case EXEC_INQUIRE:
14686 836 : if (!gfc_resolve_inquire (code->ext.inquire))
14687 : break;
14688 :
14689 788 : resolve_branch (code->ext.inquire->err, code);
14690 788 : break;
14691 :
14692 92 : case EXEC_IOLENGTH:
14693 92 : gcc_assert (code->ext.inquire != NULL);
14694 92 : if (!gfc_resolve_inquire (code->ext.inquire))
14695 : break;
14696 :
14697 90 : resolve_branch (code->ext.inquire->err, code);
14698 90 : break;
14699 :
14700 89 : case EXEC_WAIT:
14701 89 : if (!gfc_resolve_wait (code->ext.wait))
14702 : break;
14703 :
14704 74 : resolve_branch (code->ext.wait->err, code);
14705 74 : resolve_branch (code->ext.wait->end, code);
14706 74 : resolve_branch (code->ext.wait->eor, code);
14707 74 : break;
14708 :
14709 33010 : case EXEC_READ:
14710 33010 : case EXEC_WRITE:
14711 33010 : if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
14712 : break;
14713 :
14714 32702 : resolve_branch (code->ext.dt->err, code);
14715 32702 : resolve_branch (code->ext.dt->end, code);
14716 32702 : resolve_branch (code->ext.dt->eor, code);
14717 32702 : break;
14718 :
14719 47035 : case EXEC_TRANSFER:
14720 47035 : resolve_transfer (code);
14721 47035 : break;
14722 :
14723 2202 : case EXEC_DO_CONCURRENT:
14724 2202 : case EXEC_FORALL:
14725 2202 : resolve_forall_iterators (code->ext.concur.forall_iterator);
14726 :
14727 2202 : if (code->expr1 != NULL
14728 732 : && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
14729 2 : gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
14730 : "expression", &code->expr1->where);
14731 :
14732 2202 : if (code->op == EXEC_DO_CONCURRENT)
14733 210 : resolve_locality_spec (code, ns);
14734 : break;
14735 :
14736 13168 : case EXEC_OACC_PARALLEL_LOOP:
14737 13168 : case EXEC_OACC_PARALLEL:
14738 13168 : case EXEC_OACC_KERNELS_LOOP:
14739 13168 : case EXEC_OACC_KERNELS:
14740 13168 : case EXEC_OACC_SERIAL_LOOP:
14741 13168 : case EXEC_OACC_SERIAL:
14742 13168 : case EXEC_OACC_DATA:
14743 13168 : case EXEC_OACC_HOST_DATA:
14744 13168 : case EXEC_OACC_LOOP:
14745 13168 : case EXEC_OACC_UPDATE:
14746 13168 : case EXEC_OACC_WAIT:
14747 13168 : case EXEC_OACC_CACHE:
14748 13168 : case EXEC_OACC_ENTER_DATA:
14749 13168 : case EXEC_OACC_EXIT_DATA:
14750 13168 : case EXEC_OACC_ATOMIC:
14751 13168 : case EXEC_OACC_DECLARE:
14752 13168 : gfc_resolve_oacc_directive (code, ns);
14753 13168 : break;
14754 :
14755 17174 : case EXEC_OMP_ALLOCATE:
14756 17174 : case EXEC_OMP_ALLOCATORS:
14757 17174 : case EXEC_OMP_ASSUME:
14758 17174 : case EXEC_OMP_ATOMIC:
14759 17174 : case EXEC_OMP_BARRIER:
14760 17174 : case EXEC_OMP_CANCEL:
14761 17174 : case EXEC_OMP_CANCELLATION_POINT:
14762 17174 : case EXEC_OMP_CRITICAL:
14763 17174 : case EXEC_OMP_FLUSH:
14764 17174 : case EXEC_OMP_DEPOBJ:
14765 17174 : case EXEC_OMP_DISPATCH:
14766 17174 : case EXEC_OMP_DISTRIBUTE:
14767 17174 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
14768 17174 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
14769 17174 : case EXEC_OMP_DISTRIBUTE_SIMD:
14770 17174 : case EXEC_OMP_DO:
14771 17174 : case EXEC_OMP_DO_SIMD:
14772 17174 : case EXEC_OMP_ERROR:
14773 17174 : case EXEC_OMP_INTEROP:
14774 17174 : case EXEC_OMP_LOOP:
14775 17174 : case EXEC_OMP_MASTER:
14776 17174 : case EXEC_OMP_MASTER_TASKLOOP:
14777 17174 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
14778 17174 : case EXEC_OMP_MASKED:
14779 17174 : case EXEC_OMP_MASKED_TASKLOOP:
14780 17174 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
14781 17174 : case EXEC_OMP_METADIRECTIVE:
14782 17174 : case EXEC_OMP_ORDERED:
14783 17174 : case EXEC_OMP_SCAN:
14784 17174 : case EXEC_OMP_SCOPE:
14785 17174 : case EXEC_OMP_SECTIONS:
14786 17174 : case EXEC_OMP_SIMD:
14787 17174 : case EXEC_OMP_SINGLE:
14788 17174 : case EXEC_OMP_TARGET:
14789 17174 : case EXEC_OMP_TARGET_DATA:
14790 17174 : case EXEC_OMP_TARGET_ENTER_DATA:
14791 17174 : case EXEC_OMP_TARGET_EXIT_DATA:
14792 17174 : case EXEC_OMP_TARGET_PARALLEL:
14793 17174 : case EXEC_OMP_TARGET_PARALLEL_DO:
14794 17174 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14795 17174 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14796 17174 : case EXEC_OMP_TARGET_SIMD:
14797 17174 : case EXEC_OMP_TARGET_TEAMS:
14798 17174 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14799 17174 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14800 17174 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14801 17174 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14802 17174 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14803 17174 : case EXEC_OMP_TARGET_UPDATE:
14804 17174 : case EXEC_OMP_TASK:
14805 17174 : case EXEC_OMP_TASKGROUP:
14806 17174 : case EXEC_OMP_TASKLOOP:
14807 17174 : case EXEC_OMP_TASKLOOP_SIMD:
14808 17174 : case EXEC_OMP_TASKWAIT:
14809 17174 : case EXEC_OMP_TASKYIELD:
14810 17174 : case EXEC_OMP_TEAMS:
14811 17174 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14812 17174 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14813 17174 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14814 17174 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14815 17174 : case EXEC_OMP_TEAMS_LOOP:
14816 17174 : case EXEC_OMP_TILE:
14817 17174 : case EXEC_OMP_UNROLL:
14818 17174 : case EXEC_OMP_WORKSHARE:
14819 17174 : gfc_resolve_omp_directive (code, ns);
14820 17174 : break;
14821 :
14822 3888 : case EXEC_OMP_PARALLEL:
14823 3888 : case EXEC_OMP_PARALLEL_DO:
14824 3888 : case EXEC_OMP_PARALLEL_DO_SIMD:
14825 3888 : case EXEC_OMP_PARALLEL_LOOP:
14826 3888 : case EXEC_OMP_PARALLEL_MASKED:
14827 3888 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14828 3888 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14829 3888 : case EXEC_OMP_PARALLEL_MASTER:
14830 3888 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14831 3888 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14832 3888 : case EXEC_OMP_PARALLEL_SECTIONS:
14833 3888 : case EXEC_OMP_PARALLEL_WORKSHARE:
14834 3888 : omp_workshare_save = omp_workshare_flag;
14835 3888 : omp_workshare_flag = 0;
14836 3888 : gfc_resolve_omp_directive (code, ns);
14837 3888 : omp_workshare_flag = omp_workshare_save;
14838 3888 : break;
14839 :
14840 0 : default:
14841 0 : gfc_internal_error ("gfc_resolve_code(): Bad statement code");
14842 : }
14843 : }
14844 :
14845 679828 : cs_base = frame.prev;
14846 679828 : }
14847 :
14848 :
14849 : /* Resolve initial values and make sure they are compatible with
14850 : the variable. */
14851 :
14852 : static void
14853 1883232 : resolve_values (gfc_symbol *sym)
14854 : {
14855 1883232 : bool t;
14856 :
14857 1883232 : if (sym->value == NULL)
14858 : return;
14859 :
14860 438558 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
14861 14 : gfc_warning (OPT_Wdeprecated_declarations,
14862 : "Using parameter %qs declared at %L is deprecated",
14863 : sym->name, &sym->declared_at);
14864 :
14865 438558 : if (sym->value->expr_type == EXPR_STRUCTURE)
14866 39921 : t= resolve_structure_cons (sym->value, 1);
14867 : else
14868 398637 : t = gfc_resolve_expr (sym->value);
14869 :
14870 438558 : if (!t)
14871 : return;
14872 :
14873 438556 : gfc_check_assign_symbol (sym, NULL, sym->value);
14874 : }
14875 :
14876 :
14877 : /* Verify any BIND(C) derived types in the namespace so we can report errors
14878 : for them once, rather than for each variable declared of that type. */
14879 :
14880 : static void
14881 1853789 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14882 : {
14883 1853789 : if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14884 83580 : && derived_sym->attr.is_bind_c == 1)
14885 27151 : verify_bind_c_derived_type (derived_sym);
14886 :
14887 1853789 : return;
14888 : }
14889 :
14890 :
14891 : /* Check the interfaces of DTIO procedures associated with derived
14892 : type 'sym'. These procedures can either have typebound bindings or
14893 : can appear in DTIO generic interfaces. */
14894 :
14895 : static void
14896 1884202 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
14897 : {
14898 1884202 : if (!sym || sym->attr.flavor != FL_DERIVED)
14899 : return;
14900 :
14901 92929 : gfc_check_dtio_interfaces (sym);
14902 :
14903 92929 : return;
14904 : }
14905 :
14906 : /* Auxiliary function, checks if an argument decays to a pointer. */
14907 :
14908 : static bool
14909 66024 : decays_to_pointer (gfc_symbol *sym)
14910 : {
14911 66024 : if (!sym->as)
14912 : return true;
14913 :
14914 18865 : if (sym->as->type == AS_ASSUMED_SHAPE)
14915 : return false;
14916 :
14917 15114 : if (sym->as->type == AS_ASSUMED_RANK)
14918 : return false;
14919 :
14920 10016 : if (sym->as->type == AS_DEFERRED && sym->attr.dummy)
14921 964 : return false;
14922 :
14923 : return true;
14924 : }
14925 :
14926 : /* Helper function, returns true if the types conform according to the C
14927 : standard, when they are not equal on the Fortran side. If we decide to
14928 : include or exclude any types from this, this is the place to change. */
14929 :
14930 : static bool
14931 390 : c_types_conform (gfc_typespec *ts1, gfc_typespec *ts2)
14932 : {
14933 390 : if (ts1->type == BT_ASSUMED || ts2->type == BT_ASSUMED)
14934 : return true;
14935 :
14936 384 : if (ts1->kind == ts2->kind
14937 : && (ts1->type == BT_CHARACTER || ts1->type == BT_INTEGER
14938 : || ts1->type == BT_UNSIGNED)
14939 : && (ts2->type == BT_CHARACTER || ts2->type == BT_INTEGER
14940 : || ts2->type == BT_UNSIGNED))
14941 384 : return true;
14942 :
14943 : return false;
14944 :
14945 : }
14946 :
14947 : /* Check argument lists of BIND(C) procedures against each other, return
14948 : false if they do not. */
14949 :
14950 : static bool
14951 11452 : compare_c_binding_arglists (gfc_symbol *osym, gfc_symbol *nsym)
14952 : {
14953 11452 : gfc_formal_arglist *oarg, *narg;
14954 11452 : bool ret = true;
14955 11452 : locus *oloc, *nloc;
14956 :
14957 11452 : oarg = osym->formal;
14958 11452 : narg = nsym->formal;
14959 11452 : oloc = &osym->declared_at;
14960 11452 : nloc = &nsym->declared_at;
14961 44474 : for ( ; oarg && narg ; oarg = oarg->next, narg = narg->next)
14962 : {
14963 33022 : oloc = &oarg->sym->declared_at;
14964 33022 : nloc = &narg->sym->declared_at;
14965 :
14966 33022 : if (!gfc_compare_types (&oarg->sym->ts, &narg->sym->ts)
14967 33022 : && (pedantic || !c_types_conform (&oarg->sym->ts, &narg->sym->ts)))
14968 : {
14969 24 : gfc_error ("Type mismatch in argument %qs at %L (%s/%s) "
14970 8 : "originally declared at %L", narg->sym->name,
14971 8 : nloc, gfc_typename (&narg->sym->ts),
14972 8 : gfc_typename (&oarg->sym->ts), oloc);
14973 8 : ret = false;
14974 8 : continue;
14975 : }
14976 33014 : if (oarg->sym->attr.value != narg->sym->attr.value)
14977 : {
14978 1 : gfc_error ("VALUE attribute mismatch in argument %qs at %L "
14979 : "originally declared at %L",narg->sym->name,
14980 : nloc, oloc);
14981 1 : ret = false;
14982 1 : continue;
14983 : }
14984 :
14985 : /* According to the Fortran standard, ranks have to match for arguments.
14986 : In this case, this makes little sense because both decay to C
14987 : pointers. Only issue an error if -pedantic or if the argument does
14988 : not decay to a pointer. Same thing for CFI_desc arrays, which include
14989 : assumed rank. */
14990 :
14991 33013 : int orank = gfc_symbol_rank (oarg->sym);
14992 33013 : int nrank = gfc_symbol_rank (narg->sym);
14993 33013 : if (orank != nrank && pedantic)
14994 : {
14995 1 : gfc_error ("Rank mismatch in argument %qs (%d/%d) at %L originally "
14996 1 : "declared at %L", narg->sym->name, nrank, orank, nloc,
14997 : oloc);
14998 1 : ret = false;
14999 1 : continue;
15000 : }
15001 :
15002 : /* Confusion between CFI_desc and "normal" arrays. */
15003 :
15004 33012 : if (decays_to_pointer (oarg->sym) != decays_to_pointer (narg->sym))
15005 : {
15006 1 : gfc_error ("Array specification mismatch in argument %qs at %L "
15007 : "originally declared at %L", narg->sym->name,
15008 : nloc, oloc);
15009 1 : ret = false;
15010 1 : continue;
15011 : }
15012 : }
15013 :
15014 11452 : if (oarg && !narg)
15015 : {
15016 0 : gfc_error ("Not enough arguments for procedure %qs with binding label "
15017 : "%qs after %L, originally declared at %L", nsym->name,
15018 0 : nsym->binding_label, nloc, &oarg->sym->declared_at);
15019 0 : ret = false;
15020 : }
15021 :
15022 11452 : if (!oarg && narg)
15023 : {
15024 2 : gfc_error ("Too many arguments for procedure %qs with binding label "
15025 : "%qs at %L, originally declared at %L", nsym->name,
15026 2 : nsym->binding_label, &narg->sym->declared_at, oloc);
15027 2 : ret = false;
15028 : }
15029 :
15030 11452 : return ret;
15031 : }
15032 :
15033 :
15034 : /* Verify that any binding labels used in a given namespace do not collide
15035 : with the names or binding labels of any global symbols. Multiple INTERFACE
15036 : for the same procedure are permitted. Abstract interfaces and dummy
15037 : arguments are not checked. */
15038 :
15039 : static void
15040 1884202 : gfc_verify_binding_labels (gfc_symbol *sym)
15041 : {
15042 1884202 : gfc_gsymbol *gsym;
15043 1884202 : const char *module;
15044 :
15045 1884202 : if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
15046 62254 : || sym->attr.flavor == FL_DERIVED || !sym->binding_label
15047 34162 : || sym->attr.abstract || sym->attr.dummy)
15048 : return;
15049 :
15050 : /* Avoid double error reporting. */
15051 34026 : if (sym->error)
15052 : return;
15053 :
15054 : /* TODO: Check the names of reserved external C identifiers here, see
15055 : PR 125251. */
15056 :
15057 : /* According to the Fortran standard, global identifiers are case
15058 : insensitive, which also holds for C identifiers. This was probably done
15059 : for systems which had case-insensitive linkers. Such systems could not
15060 : accomodate the C standards referenced, so this restriction makes little
15061 : sense for modern systems. Therefore, check case-sensitive labels unless
15062 : -pedantic is in force. */
15063 :
15064 34026 : if (pedantic)
15065 4650 : gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
15066 : else
15067 29376 : gsym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
15068 :
15069 34026 : if (sym->module)
15070 : module = sym->module;
15071 12161 : else if (sym->ns && sym->ns->proc_name
15072 12161 : && sym->ns->proc_name->attr.flavor == FL_MODULE)
15073 4544 : module = sym->ns->proc_name->name;
15074 7617 : else if (sym->ns && sym->ns->parent
15075 358 : && sym->ns && sym->ns->parent->proc_name
15076 358 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
15077 272 : module = sym->ns->parent->proc_name->name;
15078 : else
15079 : module = NULL;
15080 :
15081 34026 : if (gsym)
15082 : {
15083 11496 : if (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)
15084 : {
15085 11455 : gfc_symbol *global_sym;
15086 11455 : gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &global_sym);
15087 :
15088 : /* For when the symtree does not match the symbol name, which can happen
15089 : in modules with PRIVATE. */
15090 :
15091 11455 : if (global_sym == NULL)
15092 1 : gfc_find_symbol_by_name (gsym->sym_name, gsym->ns, &global_sym);
15093 :
15094 11455 : gcc_assert (global_sym);
15095 :
15096 : /* If subroutines and functions are conflated, there is little point
15097 : in continuing checks. */
15098 11455 : if ((sym->attr.function && gsym->type == GSYM_SUBROUTINE)
15099 11455 : || (sym->attr.subroutine && gsym->type == GSYM_FUNCTION))
15100 : {
15101 1 : gfc_global_used (gsym, &sym->declared_at);
15102 1 : sym->binding_label = NULL;
15103 1 : sym->error = 1;
15104 13 : return;
15105 : }
15106 :
15107 6001 : if (gsym->type == GSYM_FUNCTION && sym->attr.function
15108 17455 : && !gfc_compare_types (&sym->ts, &global_sym->ts))
15109 : {
15110 2 : gfc_error ("Return type mismatch of function %qs with binding "
15111 : "label %qs at %L (%s/%s), originally declared at %L",
15112 : sym->name, sym->binding_label,
15113 : &sym->declared_at,
15114 : gfc_typename (&sym->ts),
15115 2 : gfc_typename (&global_sym->ts),
15116 : &gsym->where);
15117 2 : sym->binding_label = NULL;
15118 2 : sym->error = 1;
15119 2 : return;
15120 : }
15121 11452 : if (!compare_c_binding_arglists (global_sym, sym))
15122 : {
15123 10 : sym->binding_label = NULL;
15124 10 : sym->error = 1;
15125 10 : return;
15126 : }
15127 : }
15128 : }
15129 :
15130 11442 : if (!gsym
15131 11483 : || (!gsym->defined
15132 8536 : && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
15133 : {
15134 22530 : if (!gsym)
15135 22530 : gsym = gfc_get_gsymbol (sym->binding_label, true);
15136 31066 : gsym->where = sym->declared_at;
15137 31066 : gsym->sym_name = sym->name;
15138 31066 : gsym->binding_label = sym->binding_label;
15139 31066 : gsym->ns = sym->ns;
15140 31066 : gsym->mod_name = module;
15141 31066 : if (sym->attr.function)
15142 20139 : gsym->type = GSYM_FUNCTION;
15143 10927 : else if (sym->attr.subroutine)
15144 10788 : gsym->type = GSYM_SUBROUTINE;
15145 : /* Mark as variable/procedure as defined, unless its an INTERFACE. */
15146 31066 : gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
15147 31066 : return;
15148 : }
15149 :
15150 2947 : if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
15151 : {
15152 1 : gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
15153 : "identifier as entity at %L", sym->name,
15154 : sym->binding_label, &sym->declared_at, &gsym->where);
15155 : /* Clear the binding label to prevent checking multiple times. */
15156 1 : sym->binding_label = NULL;
15157 1 : return;
15158 : }
15159 :
15160 2946 : if (sym->attr.flavor == FL_VARIABLE && module
15161 37 : && (strcmp (module, gsym->mod_name) != 0
15162 35 : || strcmp (sym->name, gsym->sym_name) != 0))
15163 : {
15164 : /* This can only happen if the variable is defined in a module - if it
15165 : isn't the same module, reject it. */
15166 3 : gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
15167 : "uses the same global identifier as entity at %L from module %qs",
15168 : sym->name, module, sym->binding_label,
15169 : &sym->declared_at, &gsym->where, gsym->mod_name);
15170 3 : sym->binding_label = NULL;
15171 3 : return;
15172 : }
15173 :
15174 2943 : if ((sym->attr.function || sym->attr.subroutine)
15175 2907 : && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
15176 2905 : || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
15177 2522 : && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
15178 2091 : && (module != gsym->mod_name
15179 2087 : || strcmp (gsym->sym_name, sym->name) != 0
15180 2087 : || (module && strcmp (module, gsym->mod_name) != 0)))
15181 : {
15182 : /* Print an error if the procedure is defined multiple times; we have to
15183 : exclude references to the same procedure via module association or
15184 : multiple checks for the same procedure. */
15185 4 : gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
15186 : "global identifier as entity at %L", sym->name,
15187 : sym->binding_label, &sym->declared_at, &gsym->where);
15188 4 : sym->binding_label = NULL;
15189 4 : return;
15190 : }
15191 : }
15192 :
15193 :
15194 : /* Resolve an index expression. */
15195 :
15196 : static bool
15197 266212 : resolve_index_expr (gfc_expr *e)
15198 : {
15199 266212 : if (!gfc_resolve_expr (e))
15200 : return false;
15201 :
15202 266202 : if (!gfc_simplify_expr (e, 0))
15203 : return false;
15204 :
15205 266200 : if (!gfc_specification_expr (e))
15206 : return false;
15207 :
15208 : return true;
15209 : }
15210 :
15211 :
15212 : /* Resolve a charlen structure. */
15213 :
15214 : static bool
15215 104210 : resolve_charlen (gfc_charlen *cl)
15216 : {
15217 104210 : int k;
15218 104210 : bool saved_specification_expr;
15219 :
15220 104210 : if (cl->resolved)
15221 : return true;
15222 :
15223 95714 : cl->resolved = 1;
15224 95714 : saved_specification_expr = specification_expr;
15225 95714 : specification_expr = true;
15226 :
15227 95714 : if (cl->length_from_typespec)
15228 : {
15229 2136 : if (!gfc_resolve_expr (cl->length))
15230 : {
15231 1 : specification_expr = saved_specification_expr;
15232 1 : return false;
15233 : }
15234 :
15235 2135 : if (!gfc_simplify_expr (cl->length, 0))
15236 : {
15237 0 : specification_expr = saved_specification_expr;
15238 0 : return false;
15239 : }
15240 :
15241 : /* cl->length has been resolved. It should have an integer type. */
15242 2135 : if (cl->length
15243 2134 : && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
15244 : {
15245 4 : gfc_error ("Scalar INTEGER expression expected at %L",
15246 : &cl->length->where);
15247 4 : return false;
15248 : }
15249 : }
15250 : else
15251 : {
15252 93578 : if (!resolve_index_expr (cl->length))
15253 : {
15254 19 : specification_expr = saved_specification_expr;
15255 19 : return false;
15256 : }
15257 : }
15258 :
15259 : /* F2008, 4.4.3.2: If the character length parameter value evaluates to
15260 : a negative value, the length of character entities declared is zero. */
15261 95690 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
15262 57031 : && mpz_sgn (cl->length->value.integer) < 0)
15263 0 : gfc_replace_expr (cl->length,
15264 : gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
15265 :
15266 : /* Check that the character length is not too large. */
15267 95690 : k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
15268 95690 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
15269 57031 : && cl->length->ts.type == BT_INTEGER
15270 57031 : && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
15271 : {
15272 4 : gfc_error ("String length at %L is too large", &cl->length->where);
15273 4 : specification_expr = saved_specification_expr;
15274 4 : return false;
15275 : }
15276 :
15277 95686 : specification_expr = saved_specification_expr;
15278 95686 : return true;
15279 : }
15280 :
15281 :
15282 : /* Test for non-constant shape arrays. */
15283 :
15284 : static bool
15285 117909 : is_non_constant_shape_array (gfc_symbol *sym)
15286 : {
15287 117909 : gfc_expr *e;
15288 117909 : int i;
15289 117909 : bool not_constant;
15290 :
15291 117909 : not_constant = false;
15292 117909 : if (sym->as != NULL)
15293 : {
15294 : /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
15295 : has not been simplified; parameter array references. Do the
15296 : simplification now. */
15297 155424 : for (i = 0; i < sym->as->rank + sym->as->corank; i++)
15298 : {
15299 89733 : if (i == GFC_MAX_DIMENSIONS)
15300 : break;
15301 :
15302 89731 : e = sym->as->lower[i];
15303 89731 : if (e && (!resolve_index_expr(e)
15304 86914 : || !gfc_is_constant_expr (e)))
15305 : not_constant = true;
15306 89731 : e = sym->as->upper[i];
15307 89731 : if (e && (!resolve_index_expr(e)
15308 85692 : || !gfc_is_constant_expr (e)))
15309 : not_constant = true;
15310 : }
15311 : }
15312 117909 : return not_constant;
15313 : }
15314 :
15315 : /* Given a symbol and an initialization expression, add code to initialize
15316 : the symbol to the function entry. */
15317 : static void
15318 2099 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
15319 : {
15320 2099 : gfc_expr *lval;
15321 2099 : gfc_code *init_st;
15322 2099 : gfc_namespace *ns = sym->ns;
15323 :
15324 2099 : if (sym->attr.function && sym->result == sym && IS_PDT (sym))
15325 : {
15326 46 : gfc_free_expr (init);
15327 46 : return;
15328 : }
15329 :
15330 : /* Search for the function namespace if this is a contained
15331 : function without an explicit result. */
15332 2053 : if (sym->attr.function && sym == sym->result
15333 299 : && sym->name != sym->ns->proc_name->name)
15334 : {
15335 298 : ns = ns->contained;
15336 1376 : for (;ns; ns = ns->sibling)
15337 1315 : if (strcmp (ns->proc_name->name, sym->name) == 0)
15338 : break;
15339 : }
15340 :
15341 2053 : if (ns == NULL)
15342 : {
15343 61 : gfc_free_expr (init);
15344 61 : return;
15345 : }
15346 :
15347 : /* Build an l-value expression for the result. */
15348 1992 : lval = gfc_lval_expr_from_sym (sym);
15349 :
15350 : /* Add the code at scope entry. */
15351 1992 : init_st = gfc_get_code (EXEC_INIT_ASSIGN);
15352 1992 : init_st->next = ns->code;
15353 1992 : ns->code = init_st;
15354 :
15355 : /* Assign the default initializer to the l-value. */
15356 1992 : init_st->loc = sym->declared_at;
15357 1992 : init_st->expr1 = lval;
15358 1992 : init_st->expr2 = init;
15359 : }
15360 :
15361 :
15362 : /* Whether or not we can generate a default initializer for a symbol. */
15363 :
15364 : static bool
15365 30200 : can_generate_init (gfc_symbol *sym)
15366 : {
15367 30200 : symbol_attribute *a;
15368 30200 : if (!sym)
15369 : return false;
15370 30200 : a = &sym->attr;
15371 :
15372 : /* These symbols should never have a default initialization. */
15373 49661 : return !(
15374 30200 : a->allocatable
15375 30200 : || a->external
15376 29031 : || a->pointer
15377 29031 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
15378 5752 : && (CLASS_DATA (sym)->attr.class_pointer
15379 3779 : || CLASS_DATA (sym)->attr.proc_pointer))
15380 27058 : || a->in_equivalence
15381 26937 : || a->in_common
15382 26890 : || a->data
15383 26712 : || sym->module
15384 22884 : || a->cray_pointee
15385 22822 : || a->cray_pointer
15386 22822 : || sym->assoc
15387 20132 : || (!a->referenced && !a->result)
15388 19461 : || (a->dummy && (a->intent != INTENT_OUT
15389 1081 : || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
15390 19461 : || (a->function && sym != sym->result)
15391 : );
15392 : }
15393 :
15394 :
15395 : /* Assign the default initializer to a derived type variable or result. */
15396 :
15397 : static void
15398 11525 : apply_default_init (gfc_symbol *sym)
15399 : {
15400 11525 : gfc_expr *init = NULL;
15401 :
15402 11525 : if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
15403 : return;
15404 :
15405 11280 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
15406 10427 : init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
15407 :
15408 11280 : if (init == NULL && sym->ts.type != BT_CLASS)
15409 : return;
15410 :
15411 1717 : build_init_assign (sym, init);
15412 1717 : sym->attr.referenced = 1;
15413 : }
15414 :
15415 :
15416 : /* Build an initializer for a local. Returns null if the symbol should not have
15417 : a default initialization. */
15418 :
15419 : static gfc_expr *
15420 204993 : build_default_init_expr (gfc_symbol *sym)
15421 : {
15422 : /* These symbols should never have a default initialization. */
15423 204993 : if (sym->attr.allocatable
15424 191251 : || sym->attr.external
15425 191251 : || sym->attr.dummy
15426 125707 : || sym->attr.pointer
15427 117540 : || sym->attr.in_equivalence
15428 115164 : || sym->attr.in_common
15429 112063 : || sym->attr.data
15430 109765 : || sym->module
15431 107230 : || sym->attr.cray_pointee
15432 106929 : || sym->attr.cray_pointer
15433 106627 : || sym->assoc)
15434 : return NULL;
15435 :
15436 : /* Get the appropriate init expression. */
15437 101877 : return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
15438 : }
15439 :
15440 : /* Add an initialization expression to a local variable. */
15441 : static void
15442 204993 : apply_default_init_local (gfc_symbol *sym)
15443 : {
15444 204993 : gfc_expr *init = NULL;
15445 :
15446 : /* The symbol should be a variable or a function return value. */
15447 204993 : if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
15448 204993 : || (sym->attr.function && sym->result != sym))
15449 : return;
15450 :
15451 : /* Try to build the initializer expression. If we can't initialize
15452 : this symbol, then init will be NULL. */
15453 204993 : init = build_default_init_expr (sym);
15454 204993 : if (init == NULL)
15455 : return;
15456 :
15457 : /* For saved variables, we don't want to add an initializer at function
15458 : entry, so we just add a static initializer. Note that automatic variables
15459 : are stack allocated even with -fno-automatic; we have also to exclude
15460 : result variable, which are also nonstatic. */
15461 419 : if (!sym->attr.automatic
15462 419 : && (sym->attr.save || sym->ns->save_all
15463 377 : || (flag_max_stack_var_size == 0 && !sym->attr.result
15464 27 : && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
15465 14 : && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
15466 : {
15467 : /* Don't clobber an existing initializer! */
15468 37 : gcc_assert (sym->value == NULL);
15469 37 : sym->value = init;
15470 37 : return;
15471 : }
15472 :
15473 382 : build_init_assign (sym, init);
15474 : }
15475 :
15476 :
15477 : /* Resolution of common features of flavors variable and procedure. */
15478 :
15479 : static bool
15480 973257 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
15481 : {
15482 973257 : gfc_array_spec *as;
15483 :
15484 973257 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
15485 19447 : && sym->ts.u.derived && CLASS_DATA (sym))
15486 19441 : as = CLASS_DATA (sym)->as;
15487 : else
15488 953816 : as = sym->as;
15489 :
15490 : /* Constraints on deferred shape variable. */
15491 973257 : if (as == NULL || as->type != AS_DEFERRED)
15492 : {
15493 948913 : bool pointer, allocatable, dimension;
15494 :
15495 948913 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
15496 16222 : && sym->ts.u.derived && CLASS_DATA (sym))
15497 : {
15498 16216 : pointer = CLASS_DATA (sym)->attr.class_pointer;
15499 16216 : allocatable = CLASS_DATA (sym)->attr.allocatable;
15500 16216 : dimension = CLASS_DATA (sym)->attr.dimension;
15501 : }
15502 : else
15503 : {
15504 932697 : pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
15505 932697 : allocatable = sym->attr.allocatable;
15506 932697 : dimension = sym->attr.dimension;
15507 : }
15508 :
15509 948913 : if (allocatable)
15510 : {
15511 8067 : if (dimension
15512 8067 : && as
15513 524 : && as->type != AS_ASSUMED_RANK
15514 5 : && !sym->attr.select_rank_temporary)
15515 : {
15516 3 : gfc_error ("Allocatable array %qs at %L must have a deferred "
15517 : "shape or assumed rank", sym->name, &sym->declared_at);
15518 3 : return false;
15519 : }
15520 8064 : else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
15521 : "%qs at %L may not be ALLOCATABLE",
15522 : sym->name, &sym->declared_at))
15523 : return false;
15524 : }
15525 :
15526 948909 : if (pointer && dimension && as->type != AS_ASSUMED_RANK)
15527 : {
15528 4 : gfc_error ("Array pointer %qs at %L must have a deferred shape or "
15529 : "assumed rank", sym->name, &sym->declared_at);
15530 4 : sym->error = 1;
15531 4 : return false;
15532 : }
15533 : }
15534 : else
15535 : {
15536 24344 : if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
15537 4690 : && sym->ts.type != BT_CLASS && !sym->assoc)
15538 : {
15539 3 : gfc_error ("Array %qs at %L cannot have a deferred shape",
15540 : sym->name, &sym->declared_at);
15541 3 : return false;
15542 : }
15543 : }
15544 :
15545 : /* Constraints on polymorphic variables. */
15546 973246 : if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
15547 : {
15548 : /* F03:C502. */
15549 18780 : if (sym->attr.class_ok
15550 18724 : && sym->ts.u.derived
15551 18719 : && !sym->attr.select_type_temporary
15552 17600 : && !UNLIMITED_POLY (sym)
15553 15090 : && CLASS_DATA (sym)
15554 15089 : && CLASS_DATA (sym)->ts.u.derived
15555 33868 : && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
15556 : {
15557 5 : gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
15558 5 : CLASS_DATA (sym)->ts.u.derived->name, sym->name,
15559 : &sym->declared_at);
15560 5 : return false;
15561 : }
15562 :
15563 : /* F03:C509. */
15564 : /* Assume that use associated symbols were checked in the module ns.
15565 : Class-variables that are associate-names are also something special
15566 : and excepted from the test. */
15567 18775 : if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
15568 54 : && !sym->attr.select_type_temporary
15569 54 : && !sym->attr.select_rank_temporary)
15570 : {
15571 54 : gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
15572 : "or pointer", sym->name, &sym->declared_at);
15573 54 : return false;
15574 : }
15575 : }
15576 :
15577 : return true;
15578 : }
15579 :
15580 :
15581 : /* Additional checks for symbols with flavor variable and derived
15582 : type. To be called from resolve_fl_variable. */
15583 :
15584 : static bool
15585 82498 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
15586 : {
15587 82498 : gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
15588 :
15589 : /* Check to see if a derived type is blocked from being host
15590 : associated by the presence of another class I symbol in the same
15591 : namespace. 14.6.1.3 of the standard and the discussion on
15592 : comp.lang.fortran. */
15593 82498 : if (sym->ts.u.derived
15594 82493 : && sym->ns != sym->ts.u.derived->ns
15595 47370 : && !sym->ts.u.derived->attr.use_assoc
15596 17583 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
15597 : {
15598 16613 : gfc_symbol *s;
15599 16613 : gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
15600 16613 : if (s && s->attr.generic)
15601 2 : s = gfc_find_dt_in_generic (s);
15602 16613 : if (s && !gfc_fl_struct (s->attr.flavor))
15603 : {
15604 2 : gfc_error ("The type %qs cannot be host associated at %L "
15605 : "because it is blocked by an incompatible object "
15606 : "of the same name declared at %L",
15607 2 : sym->ts.u.derived->name, &sym->declared_at,
15608 : &s->declared_at);
15609 2 : return false;
15610 : }
15611 : }
15612 :
15613 : /* 4th constraint in section 11.3: "If an object of a type for which
15614 : component-initialization is specified (R429) appears in the
15615 : specification-part of a module and does not have the ALLOCATABLE
15616 : or POINTER attribute, the object shall have the SAVE attribute."
15617 :
15618 : The check for initializers is performed with
15619 : gfc_has_default_initializer because gfc_default_initializer generates
15620 : a hidden default for allocatable components. */
15621 81819 : if (!(sym->value || no_init_flag) && sym->ns->proc_name
15622 18542 : && sym->ns->proc_name->attr.flavor == FL_MODULE
15623 416 : && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
15624 21 : && !sym->attr.pointer && !sym->attr.allocatable
15625 21 : && gfc_has_default_initializer (sym->ts.u.derived)
15626 82505 : && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
15627 : "%qs at %L, needed due to the default "
15628 : "initialization", sym->name, &sym->declared_at))
15629 : return false;
15630 :
15631 : /* Assign default initializer. */
15632 82494 : if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
15633 76236 : && (!no_init_flag
15634 59514 : || (sym->attr.intent == INTENT_OUT
15635 3225 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
15636 19773 : sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
15637 :
15638 : return true;
15639 : }
15640 :
15641 :
15642 : /* F2008, C402 (R401): A colon shall not be used as a type-param-value
15643 : except in the declaration of an entity or component that has the POINTER
15644 : or ALLOCATABLE attribute. */
15645 :
15646 : static bool
15647 1537762 : deferred_requirements (gfc_symbol *sym)
15648 : {
15649 1537762 : if (sym->ts.deferred
15650 8010 : && !(sym->attr.pointer
15651 2397 : || sym->attr.allocatable
15652 92 : || sym->attr.associate_var
15653 7 : || sym->attr.omp_udr_artificial_var))
15654 : {
15655 : /* If a function has a result variable, only check the variable. */
15656 7 : if (sym->result && sym->name != sym->result->name)
15657 : return true;
15658 :
15659 6 : gfc_error ("Entity %qs at %L has a deferred type parameter and "
15660 : "requires either the POINTER or ALLOCATABLE attribute",
15661 : sym->name, &sym->declared_at);
15662 6 : return false;
15663 : }
15664 : return true;
15665 : }
15666 :
15667 :
15668 : /* Resolve symbols with flavor variable. */
15669 :
15670 : static bool
15671 652430 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
15672 : {
15673 652430 : const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
15674 : "SAVE attribute");
15675 :
15676 652430 : if (!resolve_fl_var_and_proc (sym, mp_flag))
15677 : return false;
15678 :
15679 : /* Set this flag to check that variables are parameters of all entries.
15680 : This check is effected by the call to gfc_resolve_expr through
15681 : is_non_constant_shape_array. */
15682 652370 : bool saved_specification_expr = specification_expr;
15683 652370 : gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
15684 652370 : specification_expr = true;
15685 652370 : specification_expr_symbol = sym;
15686 :
15687 652370 : if (sym->ns->proc_name
15688 652275 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
15689 647248 : || sym->ns->proc_name->attr.is_main_program)
15690 82792 : && !sym->attr.use_assoc
15691 79624 : && !sym->attr.allocatable
15692 73862 : && !sym->attr.pointer
15693 722582 : && is_non_constant_shape_array (sym))
15694 : {
15695 : /* F08:C541. The shape of an array defined in a main program or module
15696 : * needs to be constant. */
15697 3 : gfc_error ("The module or main program array %qs at %L must "
15698 : "have constant shape", sym->name, &sym->declared_at);
15699 3 : specification_expr = saved_specification_expr;
15700 3 : specification_expr_symbol = saved_specification_expr_symbol;
15701 3 : return false;
15702 : }
15703 :
15704 : /* Constraints on deferred type parameter. */
15705 652367 : if (!deferred_requirements (sym))
15706 : return false;
15707 :
15708 652363 : if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
15709 : {
15710 : /* Make sure that character string variables with assumed length are
15711 : dummy arguments. */
15712 36119 : gfc_expr *e = NULL;
15713 :
15714 36119 : if (sym->ts.u.cl)
15715 36119 : e = sym->ts.u.cl->length;
15716 : else
15717 : return false;
15718 :
15719 36119 : if (e == NULL && !sym->attr.dummy && !sym->attr.result
15720 2638 : && !sym->ts.deferred && !sym->attr.select_type_temporary
15721 2 : && !sym->attr.omp_udr_artificial_var)
15722 : {
15723 2 : gfc_error ("Entity with assumed character length at %L must be a "
15724 : "dummy argument or a PARAMETER", &sym->declared_at);
15725 2 : specification_expr = saved_specification_expr;
15726 2 : specification_expr_symbol = saved_specification_expr_symbol;
15727 2 : return false;
15728 : }
15729 :
15730 20910 : if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
15731 : {
15732 1 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15733 1 : specification_expr = saved_specification_expr;
15734 1 : specification_expr_symbol = saved_specification_expr_symbol;
15735 1 : return false;
15736 : }
15737 :
15738 36116 : if (!gfc_is_constant_expr (e)
15739 36116 : && !(e->expr_type == EXPR_VARIABLE
15740 1388 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
15741 : {
15742 2184 : if (!sym->attr.use_assoc && sym->ns->proc_name
15743 1680 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
15744 1679 : || sym->ns->proc_name->attr.is_main_program))
15745 : {
15746 3 : gfc_error ("%qs at %L must have constant character length "
15747 : "in this context", sym->name, &sym->declared_at);
15748 3 : specification_expr = saved_specification_expr;
15749 3 : specification_expr_symbol = saved_specification_expr_symbol;
15750 3 : return false;
15751 : }
15752 2181 : if (sym->attr.in_common)
15753 : {
15754 1 : gfc_error ("COMMON variable %qs at %L must have constant "
15755 : "character length", sym->name, &sym->declared_at);
15756 1 : specification_expr = saved_specification_expr;
15757 1 : specification_expr_symbol = saved_specification_expr_symbol;
15758 1 : return false;
15759 : }
15760 : }
15761 : }
15762 :
15763 652356 : if (sym->value == NULL && sym->attr.referenced
15764 206909 : && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
15765 204993 : apply_default_init_local (sym); /* Try to apply a default initialization. */
15766 :
15767 : /* Determine if the symbol may not have an initializer. */
15768 652356 : int no_init_flag = 0, automatic_flag = 0;
15769 652356 : if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
15770 171098 : || sym->attr.intrinsic || sym->attr.result)
15771 : no_init_flag = 1;
15772 138799 : else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
15773 173487 : && is_non_constant_shape_array (sym))
15774 : {
15775 1345 : no_init_flag = automatic_flag = 1;
15776 :
15777 : /* Also, they must not have the SAVE attribute.
15778 : SAVE_IMPLICIT is checked below. */
15779 1345 : if (sym->as && sym->attr.codimension)
15780 : {
15781 7 : int corank = sym->as->corank;
15782 7 : sym->as->corank = 0;
15783 7 : no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
15784 7 : sym->as->corank = corank;
15785 : }
15786 1345 : if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
15787 : {
15788 2 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
15789 2 : specification_expr = saved_specification_expr;
15790 2 : specification_expr_symbol = saved_specification_expr_symbol;
15791 2 : return false;
15792 : }
15793 : }
15794 :
15795 : /* Ensure that any initializer is simplified. */
15796 652354 : if (sym->value)
15797 8137 : gfc_simplify_expr (sym->value, 1);
15798 :
15799 : /* Reject illegal initializers. */
15800 652354 : if (!sym->mark && sym->value)
15801 : {
15802 8137 : if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
15803 67 : && CLASS_DATA (sym)->attr.allocatable))
15804 1 : gfc_error ("Allocatable %qs at %L cannot have an initializer",
15805 : sym->name, &sym->declared_at);
15806 8136 : else if (sym->attr.external)
15807 0 : gfc_error ("External %qs at %L cannot have an initializer",
15808 : sym->name, &sym->declared_at);
15809 8136 : else if (sym->attr.dummy)
15810 3 : gfc_error ("Dummy %qs at %L cannot have an initializer",
15811 : sym->name, &sym->declared_at);
15812 8133 : else if (sym->attr.intrinsic)
15813 0 : gfc_error ("Intrinsic %qs at %L cannot have an initializer",
15814 : sym->name, &sym->declared_at);
15815 8133 : else if (sym->attr.result)
15816 1 : gfc_error ("Function result %qs at %L cannot have an initializer",
15817 : sym->name, &sym->declared_at);
15818 8132 : else if (automatic_flag)
15819 5 : gfc_error ("Automatic array %qs at %L cannot have an initializer",
15820 : sym->name, &sym->declared_at);
15821 : else
15822 8127 : goto no_init_error;
15823 10 : specification_expr = saved_specification_expr;
15824 10 : specification_expr_symbol = saved_specification_expr_symbol;
15825 10 : return false;
15826 : }
15827 :
15828 644217 : no_init_error:
15829 652344 : if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
15830 : {
15831 82498 : bool res = resolve_fl_variable_derived (sym, no_init_flag);
15832 82498 : specification_expr = saved_specification_expr;
15833 82498 : specification_expr_symbol = saved_specification_expr_symbol;
15834 82498 : return res;
15835 : }
15836 :
15837 569846 : specification_expr = saved_specification_expr;
15838 569846 : specification_expr_symbol = saved_specification_expr_symbol;
15839 569846 : return true;
15840 : }
15841 :
15842 :
15843 : /* Compare the dummy characteristics of a module procedure interface
15844 : declaration with the corresponding declaration in a submodule. */
15845 : static gfc_formal_arglist *new_formal;
15846 : static char errmsg[200];
15847 :
15848 : static void
15849 1330 : compare_fsyms (gfc_symbol *sym)
15850 : {
15851 1330 : gfc_symbol *fsym;
15852 :
15853 1330 : if (sym == NULL || new_formal == NULL)
15854 : return;
15855 :
15856 1330 : fsym = new_formal->sym;
15857 :
15858 1330 : if (sym == fsym)
15859 : return;
15860 :
15861 1306 : if (strcmp (sym->name, fsym->name) == 0)
15862 : {
15863 505 : if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
15864 2 : gfc_error ("%s at %L", errmsg, &fsym->declared_at);
15865 : }
15866 : }
15867 :
15868 :
15869 : /* Resolve a procedure. */
15870 :
15871 : static bool
15872 481234 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
15873 : {
15874 481234 : gfc_formal_arglist *arg;
15875 481234 : bool allocatable_or_pointer = false;
15876 :
15877 481234 : if (sym->attr.function
15878 481234 : && !resolve_fl_var_and_proc (sym, mp_flag))
15879 : return false;
15880 :
15881 : /* Constraints on deferred type parameter. */
15882 481224 : if (!deferred_requirements (sym))
15883 : return false;
15884 :
15885 481223 : if (sym->ts.type == BT_CHARACTER)
15886 : {
15887 11800 : gfc_charlen *cl = sym->ts.u.cl;
15888 :
15889 7642 : if (cl && cl->length && gfc_is_constant_expr (cl->length)
15890 13093 : && !resolve_charlen (cl))
15891 : return false;
15892 :
15893 11799 : if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15894 10507 : && sym->attr.proc == PROC_ST_FUNCTION)
15895 : {
15896 0 : gfc_error ("Character-valued statement function %qs at %L must "
15897 : "have constant length", sym->name, &sym->declared_at);
15898 0 : return false;
15899 : }
15900 : }
15901 :
15902 : /* Ensure that derived type for are not of a private type. Internal
15903 : module procedures are excluded by 2.2.3.3 - i.e., they are not
15904 : externally accessible and can access all the objects accessible in
15905 : the host. */
15906 111162 : if (!(sym->ns->parent && sym->ns->parent->proc_name
15907 111162 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
15908 567720 : && gfc_check_symbol_access (sym))
15909 : {
15910 449364 : gfc_interface *iface;
15911 :
15912 954341 : for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
15913 : {
15914 504978 : if (arg->sym
15915 504837 : && arg->sym->ts.type == BT_DERIVED
15916 43121 : && arg->sym->ts.u.derived
15917 43121 : && !arg->sym->ts.u.derived->attr.use_assoc
15918 4462 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15919 504987 : && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
15920 : "and cannot be a dummy argument"
15921 : " of %qs, which is PUBLIC at %L",
15922 9 : arg->sym->name, sym->name,
15923 : &sym->declared_at))
15924 : {
15925 : /* Stop this message from recurring. */
15926 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15927 1 : return false;
15928 : }
15929 : }
15930 :
15931 : /* PUBLIC interfaces may expose PRIVATE procedures that take types
15932 : PRIVATE to the containing module. */
15933 638670 : for (iface = sym->generic; iface; iface = iface->next)
15934 : {
15935 442497 : for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
15936 : {
15937 253190 : if (arg->sym
15938 253158 : && arg->sym->ts.type == BT_DERIVED
15939 8031 : && !arg->sym->ts.u.derived->attr.use_assoc
15940 244 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15941 253194 : && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
15942 : "PUBLIC interface %qs at %L "
15943 : "takes dummy arguments of %qs which "
15944 : "is PRIVATE", iface->sym->name,
15945 4 : sym->name, &iface->sym->declared_at,
15946 4 : gfc_typename(&arg->sym->ts)))
15947 : {
15948 : /* Stop this message from recurring. */
15949 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15950 1 : return false;
15951 : }
15952 : }
15953 : }
15954 : }
15955 :
15956 481220 : if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
15957 79 : && !sym->attr.proc_pointer)
15958 : {
15959 2 : gfc_error ("Function %qs at %L cannot have an initializer",
15960 : sym->name, &sym->declared_at);
15961 :
15962 : /* Make sure no second error is issued for this. */
15963 2 : sym->value->error = 1;
15964 2 : return false;
15965 : }
15966 :
15967 : /* An external symbol may not have an initializer because it is taken to be
15968 : a procedure. Exception: Procedure Pointers. */
15969 481218 : if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
15970 : {
15971 0 : gfc_error ("External object %qs at %L may not have an initializer",
15972 : sym->name, &sym->declared_at);
15973 0 : return false;
15974 : }
15975 :
15976 : /* An elemental function is required to return a scalar 12.7.1 */
15977 481218 : if (sym->attr.elemental && sym->attr.function
15978 86370 : && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15979 2 : && CLASS_DATA (sym)->as)))
15980 : {
15981 3 : gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
15982 : "result", sym->name, &sym->declared_at);
15983 : /* Reset so that the error only occurs once. */
15984 3 : sym->attr.elemental = 0;
15985 3 : return false;
15986 : }
15987 :
15988 481215 : if (sym->attr.proc == PROC_ST_FUNCTION
15989 223 : && (sym->attr.allocatable || sym->attr.pointer))
15990 : {
15991 2 : gfc_error ("Statement function %qs at %L may not have pointer or "
15992 : "allocatable attribute", sym->name, &sym->declared_at);
15993 2 : return false;
15994 : }
15995 :
15996 : /* 5.1.1.5 of the Standard: A function name declared with an asterisk
15997 : char-len-param shall not be array-valued, pointer-valued, recursive
15998 : or pure. ....snip... A character value of * may only be used in the
15999 : following ways: (i) Dummy arg of procedure - dummy associates with
16000 : actual length; (ii) To declare a named constant; or (iii) External
16001 : function - but length must be declared in calling scoping unit. */
16002 481213 : if (sym->attr.function
16003 320808 : && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
16004 6743 : && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
16005 : {
16006 180 : if ((sym->as && sym->as->rank) || (sym->attr.pointer)
16007 178 : || (sym->attr.recursive) || (sym->attr.pure))
16008 : {
16009 4 : if (sym->as && sym->as->rank)
16010 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
16011 : "array-valued", sym->name, &sym->declared_at);
16012 :
16013 4 : if (sym->attr.pointer)
16014 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
16015 : "pointer-valued", sym->name, &sym->declared_at);
16016 :
16017 4 : if (sym->attr.pure)
16018 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
16019 : "pure", sym->name, &sym->declared_at);
16020 :
16021 4 : if (sym->attr.recursive)
16022 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
16023 : "recursive", sym->name, &sym->declared_at);
16024 :
16025 4 : return false;
16026 : }
16027 :
16028 : /* Appendix B.2 of the standard. Contained functions give an
16029 : error anyway. Deferred character length is an F2003 feature.
16030 : Don't warn on intrinsic conversion functions, which start
16031 : with two underscores. */
16032 176 : if (!sym->attr.contained && !sym->ts.deferred
16033 172 : && (sym->name[0] != '_' || sym->name[1] != '_'))
16034 172 : gfc_notify_std (GFC_STD_F95_OBS,
16035 : "CHARACTER(*) function %qs at %L",
16036 : sym->name, &sym->declared_at);
16037 : }
16038 :
16039 : /* F2008, C1218. */
16040 481209 : if (sym->attr.elemental)
16041 : {
16042 89648 : if (sym->attr.proc_pointer)
16043 : {
16044 7 : const char* name = (sym->attr.result ? sym->ns->proc_name->name
16045 : : sym->name);
16046 7 : gfc_error ("Procedure pointer %qs at %L shall not be elemental",
16047 : name, &sym->declared_at);
16048 7 : return false;
16049 : }
16050 89641 : if (sym->attr.dummy)
16051 : {
16052 3 : gfc_error ("Dummy procedure %qs at %L shall not be elemental",
16053 : sym->name, &sym->declared_at);
16054 3 : return false;
16055 : }
16056 : }
16057 :
16058 : /* F2018, C15100: "The result of an elemental function shall be scalar,
16059 : and shall not have the POINTER or ALLOCATABLE attribute." The scalar
16060 : pointer is tested and caught elsewhere. */
16061 481199 : if (sym->result)
16062 269359 : allocatable_or_pointer = sym->result->ts.type == BT_CLASS
16063 269359 : && CLASS_DATA (sym->result) ?
16064 1663 : (CLASS_DATA (sym->result)->attr.allocatable
16065 1663 : || CLASS_DATA (sym->result)->attr.pointer) :
16066 267696 : (sym->result->attr.allocatable
16067 267696 : || sym->result->attr.pointer);
16068 :
16069 481199 : if (sym->attr.elemental && sym->result
16070 85995 : && allocatable_or_pointer)
16071 : {
16072 4 : gfc_error ("Function result variable %qs at %L of elemental "
16073 : "function %qs shall not have an ALLOCATABLE or POINTER "
16074 : "attribute", sym->result->name,
16075 : &sym->result->declared_at, sym->name);
16076 4 : return false;
16077 : }
16078 :
16079 : /* F2018:C1585: "The function result of a pure function shall not be both
16080 : polymorphic and allocatable, or have a polymorphic allocatable ultimate
16081 : component." */
16082 481195 : if (sym->attr.pure && sym->result && sym->ts.u.derived)
16083 : {
16084 2460 : if (sym->ts.type == BT_CLASS
16085 5 : && sym->attr.class_ok
16086 4 : && CLASS_DATA (sym->result)
16087 4 : && CLASS_DATA (sym->result)->attr.allocatable)
16088 : {
16089 4 : gfc_error ("Result variable %qs of pure function at %L is "
16090 : "polymorphic allocatable",
16091 : sym->result->name, &sym->result->declared_at);
16092 4 : return false;
16093 : }
16094 :
16095 2456 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components)
16096 : {
16097 : gfc_component *c = sym->ts.u.derived->components;
16098 4493 : for (; c; c = c->next)
16099 2346 : if (c->ts.type == BT_CLASS
16100 2 : && CLASS_DATA (c)
16101 2 : && CLASS_DATA (c)->attr.allocatable)
16102 : {
16103 2 : gfc_error ("Result variable %qs of pure function at %L has "
16104 : "polymorphic allocatable component %qs",
16105 : sym->result->name, &sym->result->declared_at,
16106 : c->name);
16107 2 : return false;
16108 : }
16109 : }
16110 : }
16111 :
16112 481189 : if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
16113 : {
16114 6752 : gfc_formal_arglist *curr_arg;
16115 6752 : int has_non_interop_arg = 0;
16116 :
16117 6752 : if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
16118 6752 : sym->common_block))
16119 : {
16120 : /* Clear these to prevent looking at them again if there was an
16121 : error. */
16122 2 : sym->attr.is_bind_c = 0;
16123 2 : sym->attr.is_c_interop = 0;
16124 2 : sym->ts.is_c_interop = 0;
16125 : }
16126 : else
16127 : {
16128 : /* So far, no errors have been found. */
16129 6750 : sym->attr.is_c_interop = 1;
16130 6750 : sym->ts.is_c_interop = 1;
16131 : }
16132 :
16133 6752 : curr_arg = gfc_sym_get_dummy_args (sym);
16134 30088 : while (curr_arg != NULL)
16135 : {
16136 : /* Skip implicitly typed dummy args here. */
16137 16584 : if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
16138 16527 : if (!gfc_verify_c_interop_param (curr_arg->sym))
16139 : /* If something is found to fail, record the fact so we
16140 : can mark the symbol for the procedure as not being
16141 : BIND(C) to try and prevent multiple errors being
16142 : reported. */
16143 16584 : has_non_interop_arg = 1;
16144 :
16145 16584 : curr_arg = curr_arg->next;
16146 : }
16147 :
16148 : /* See if any of the arguments were not interoperable and if so, clear
16149 : the procedure symbol to prevent duplicate error messages. */
16150 6752 : if (has_non_interop_arg != 0)
16151 : {
16152 128 : sym->attr.is_c_interop = 0;
16153 128 : sym->ts.is_c_interop = 0;
16154 128 : sym->attr.is_bind_c = 0;
16155 : }
16156 : }
16157 :
16158 481189 : if (!sym->attr.proc_pointer)
16159 : {
16160 480108 : if (sym->attr.save == SAVE_EXPLICIT)
16161 : {
16162 5 : gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
16163 : "in %qs at %L", sym->name, &sym->declared_at);
16164 5 : return false;
16165 : }
16166 480103 : if (sym->attr.intent)
16167 : {
16168 1 : gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
16169 : "in %qs at %L", sym->name, &sym->declared_at);
16170 1 : return false;
16171 : }
16172 480102 : if (sym->attr.subroutine && sym->attr.result)
16173 : {
16174 2 : gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
16175 2 : "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
16176 2 : return false;
16177 : }
16178 480100 : if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
16179 136804 : && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
16180 136801 : || sym->attr.contained))
16181 : {
16182 3 : gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
16183 : "in %qs at %L", sym->name, &sym->declared_at);
16184 3 : return false;
16185 : }
16186 480097 : if (strcmp ("ppr@", sym->name) == 0)
16187 : {
16188 0 : gfc_error ("Procedure pointer result %qs at %L "
16189 : "is missing the pointer attribute",
16190 0 : sym->ns->proc_name->name, &sym->declared_at);
16191 0 : return false;
16192 : }
16193 : }
16194 :
16195 : /* Assume that a procedure whose body is not known has references
16196 : to external arrays. */
16197 481178 : if (sym->attr.if_source != IFSRC_DECL)
16198 328886 : sym->attr.array_outer_dependency = 1;
16199 :
16200 : /* Compare the characteristics of a module procedure with the
16201 : interface declaration. Ideally this would be done with
16202 : gfc_compare_interfaces but, at present, the formal interface
16203 : cannot be copied to the ts.interface. */
16204 481178 : if (sym->attr.module_procedure
16205 1535 : && sym->attr.if_source == IFSRC_DECL)
16206 : {
16207 635 : gfc_symbol *iface;
16208 635 : char name[2*GFC_MAX_SYMBOL_LEN + 1];
16209 635 : char *module_name;
16210 635 : char *submodule_name;
16211 635 : strcpy (name, sym->ns->proc_name->name);
16212 635 : module_name = strtok (name, ".");
16213 635 : submodule_name = strtok (NULL, ".");
16214 :
16215 635 : iface = sym->tlink;
16216 635 : sym->tlink = NULL;
16217 :
16218 : /* Make sure that the result uses the correct charlen for deferred
16219 : length results. */
16220 635 : if (iface && sym->result
16221 189 : && iface->ts.type == BT_CHARACTER
16222 19 : && iface->ts.deferred)
16223 6 : sym->result->ts.u.cl = iface->ts.u.cl;
16224 :
16225 6 : if (iface == NULL)
16226 195 : goto check_formal;
16227 :
16228 : /* Check the procedure characteristics. */
16229 440 : if (sym->attr.elemental != iface->attr.elemental)
16230 : {
16231 1 : gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
16232 : "PROCEDURE at %L and its interface in %s",
16233 : &sym->declared_at, module_name);
16234 10 : return false;
16235 : }
16236 :
16237 439 : if (sym->attr.pure != iface->attr.pure)
16238 : {
16239 2 : gfc_error ("Mismatch in PURE attribute between MODULE "
16240 : "PROCEDURE at %L and its interface in %s",
16241 : &sym->declared_at, module_name);
16242 2 : return false;
16243 : }
16244 :
16245 437 : if (sym->attr.recursive != iface->attr.recursive)
16246 : {
16247 2 : gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
16248 : "PROCEDURE at %L and its interface in %s",
16249 : &sym->declared_at, module_name);
16250 2 : return false;
16251 : }
16252 :
16253 : /* Check the result characteristics. */
16254 435 : if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
16255 : {
16256 5 : gfc_error ("%s between the MODULE PROCEDURE declaration "
16257 : "in MODULE %qs and the declaration at %L in "
16258 : "(SUB)MODULE %qs",
16259 : errmsg, module_name, &sym->declared_at,
16260 : submodule_name ? submodule_name : module_name);
16261 5 : return false;
16262 : }
16263 :
16264 430 : check_formal:
16265 : /* Check the characteristics of the formal arguments. */
16266 625 : if (sym->formal && sym->formal_ns)
16267 : {
16268 1224 : for (arg = sym->formal; arg && arg->sym; arg = arg->next)
16269 : {
16270 703 : new_formal = arg;
16271 703 : gfc_traverse_ns (sym->formal_ns, compare_fsyms);
16272 : }
16273 : }
16274 : }
16275 :
16276 : /* F2018:15.4.2.2 requires an explicit interface for procedures with the
16277 : BIND(C) attribute. */
16278 481168 : if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
16279 : {
16280 1 : gfc_error ("Interface of %qs at %L must be explicit",
16281 : sym->name, &sym->declared_at);
16282 1 : return false;
16283 : }
16284 :
16285 : return true;
16286 : }
16287 :
16288 :
16289 : /* Resolve a list of finalizer procedures. That is, after they have hopefully
16290 : been defined and we now know their defined arguments, check that they fulfill
16291 : the requirements of the standard for procedures used as finalizers. */
16292 :
16293 : static bool
16294 112412 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
16295 : {
16296 112412 : gfc_finalizer *list, *pdt_finalizers = NULL;
16297 112412 : gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
16298 112412 : bool result = true;
16299 112412 : bool seen_scalar = false;
16300 112412 : gfc_symbol *vtab;
16301 112412 : gfc_component *c;
16302 112412 : gfc_symbol *parent = gfc_get_derived_super_type (derived);
16303 :
16304 112412 : if (parent)
16305 15637 : gfc_resolve_finalizers (parent, finalizable);
16306 :
16307 : /* Ensure that derived-type components have a their finalizers resolved. */
16308 112412 : bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
16309 353820 : for (c = derived->components; c; c = c->next)
16310 241408 : if (c->ts.type == BT_DERIVED
16311 67869 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
16312 : {
16313 8336 : bool has_final2 = false;
16314 8336 : if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
16315 0 : return false; /* Error. */
16316 8336 : has_final = has_final || has_final2;
16317 : }
16318 : /* Return early if not finalizable. */
16319 112412 : if (!has_final)
16320 : {
16321 109817 : if (finalizable)
16322 8226 : *finalizable = false;
16323 109817 : return true;
16324 : }
16325 :
16326 : /* If a PDT has finalizers, the pdt_type's f2k_derived is a copy of that of
16327 : the template. If the finalizers field has the same value, it needs to be
16328 : supplied with finalizers of the same pdt_type. */
16329 2595 : if (derived->attr.pdt_type
16330 30 : && derived->template_sym
16331 12 : && derived->template_sym->f2k_derived
16332 12 : && (pdt_finalizers = derived->template_sym->f2k_derived->finalizers)
16333 2607 : && derived->f2k_derived->finalizers == pdt_finalizers)
16334 : {
16335 12 : gfc_finalizer *tmp = NULL;
16336 12 : derived->f2k_derived->finalizers = NULL;
16337 12 : prev_link = &derived->f2k_derived->finalizers;
16338 48 : for (list = pdt_finalizers; list; list = list->next)
16339 : {
16340 36 : gfc_formal_arglist *args = gfc_sym_get_dummy_args (list->proc_sym);
16341 36 : if (args->sym
16342 36 : && args->sym->ts.type == BT_DERIVED
16343 36 : && args->sym->ts.u.derived
16344 36 : && !strcmp (args->sym->ts.u.derived->name, derived->name))
16345 : {
16346 18 : tmp = gfc_get_finalizer ();
16347 18 : *tmp = *list;
16348 18 : tmp->next = NULL;
16349 18 : if (*prev_link)
16350 : {
16351 6 : (*prev_link)->next = tmp;
16352 6 : prev_link = &tmp;
16353 : }
16354 : else
16355 12 : *prev_link = tmp;
16356 18 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
16357 : }
16358 : }
16359 : }
16360 :
16361 : /* Walk over the list of finalizer-procedures, check them, and if any one
16362 : does not fit in with the standard's definition, print an error and remove
16363 : it from the list. */
16364 2595 : prev_link = &derived->f2k_derived->finalizers;
16365 5326 : for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
16366 : {
16367 2731 : gfc_formal_arglist *dummy_args;
16368 2731 : gfc_symbol* arg;
16369 2731 : gfc_finalizer* i;
16370 2731 : int my_rank;
16371 :
16372 : /* Skip this finalizer if we already resolved it. */
16373 2731 : if (list->proc_tree)
16374 : {
16375 2192 : if (list->proc_tree->n.sym->formal->sym->as == NULL
16376 584 : || list->proc_tree->n.sym->formal->sym->as->rank == 0)
16377 1608 : seen_scalar = true;
16378 2192 : prev_link = &(list->next);
16379 2192 : continue;
16380 : }
16381 :
16382 : /* Check this exists and is a SUBROUTINE. */
16383 539 : if (!list->proc_sym->attr.subroutine)
16384 : {
16385 3 : gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
16386 : list->proc_sym->name, &list->where);
16387 3 : goto error;
16388 : }
16389 :
16390 : /* We should have exactly one argument. */
16391 536 : dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
16392 536 : if (!dummy_args || dummy_args->next)
16393 : {
16394 2 : gfc_error ("FINAL procedure at %L must have exactly one argument",
16395 : &list->where);
16396 2 : goto error;
16397 : }
16398 534 : arg = dummy_args->sym;
16399 :
16400 534 : if (!arg)
16401 : {
16402 1 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
16403 1 : &list->proc_sym->declared_at, derived->name);
16404 1 : goto error;
16405 : }
16406 :
16407 533 : if (arg->as && arg->as->type == AS_ASSUMED_RANK
16408 6 : && ((list != derived->f2k_derived->finalizers) || list->next))
16409 : {
16410 0 : gfc_error ("FINAL procedure at %L with assumed rank argument must "
16411 : "be the only finalizer with the same kind/type "
16412 : "(F2018: C790)", &list->where);
16413 0 : goto error;
16414 : }
16415 :
16416 : /* This argument must be of our type. */
16417 533 : if (!derived->attr.pdt_template
16418 533 : && (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived))
16419 : {
16420 2 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
16421 : &arg->declared_at, derived->name);
16422 2 : goto error;
16423 : }
16424 :
16425 : /* It must neither be a pointer nor allocatable nor optional. */
16426 531 : if (arg->attr.pointer)
16427 : {
16428 1 : gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
16429 : &arg->declared_at);
16430 1 : goto error;
16431 : }
16432 530 : if (arg->attr.allocatable)
16433 : {
16434 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
16435 : " ALLOCATABLE", &arg->declared_at);
16436 1 : goto error;
16437 : }
16438 529 : if (arg->attr.optional)
16439 : {
16440 1 : gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
16441 : &arg->declared_at);
16442 1 : goto error;
16443 : }
16444 :
16445 : /* It must not be INTENT(OUT). */
16446 528 : if (arg->attr.intent == INTENT_OUT)
16447 : {
16448 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
16449 : " INTENT(OUT)", &arg->declared_at);
16450 1 : goto error;
16451 : }
16452 :
16453 : /* Warn if the procedure is non-scalar and not assumed shape. */
16454 527 : if (warn_surprising && arg->as && arg->as->rank != 0
16455 3 : && arg->as->type != AS_ASSUMED_SHAPE)
16456 2 : gfc_warning (OPT_Wsurprising,
16457 : "Non-scalar FINAL procedure at %L should have assumed"
16458 : " shape argument", &arg->declared_at);
16459 :
16460 : /* Check that it does not match in kind and rank with a FINAL procedure
16461 : defined earlier. To really loop over the *earlier* declarations,
16462 : we need to walk the tail of the list as new ones were pushed at the
16463 : front. */
16464 : /* TODO: Handle kind parameters once they are implemented. */
16465 527 : my_rank = (arg->as ? arg->as->rank : 0);
16466 622 : for (i = list->next; i; i = i->next)
16467 : {
16468 97 : gfc_formal_arglist *dummy_args;
16469 :
16470 : /* Argument list might be empty; that is an error signalled earlier,
16471 : but we nevertheless continued resolving. */
16472 97 : dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
16473 97 : if (dummy_args && !derived->attr.pdt_template)
16474 : {
16475 95 : gfc_symbol* i_arg = dummy_args->sym;
16476 95 : const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
16477 95 : if (i_rank == my_rank)
16478 : {
16479 2 : gfc_error ("FINAL procedure %qs declared at %L has the same"
16480 : " rank (%d) as %qs",
16481 2 : list->proc_sym->name, &list->where, my_rank,
16482 2 : i->proc_sym->name);
16483 2 : goto error;
16484 : }
16485 : }
16486 : }
16487 :
16488 : /* Is this the/a scalar finalizer procedure? */
16489 525 : if (my_rank == 0)
16490 399 : seen_scalar = true;
16491 :
16492 : /* Find the symtree for this procedure. */
16493 525 : gcc_assert (!list->proc_tree);
16494 525 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
16495 :
16496 525 : prev_link = &list->next;
16497 525 : continue;
16498 :
16499 : /* Remove wrong nodes immediately from the list so we don't risk any
16500 : troubles in the future when they might fail later expectations. */
16501 14 : error:
16502 14 : i = list;
16503 14 : *prev_link = list->next;
16504 14 : gfc_free_finalizer (i);
16505 14 : result = false;
16506 525 : }
16507 :
16508 2595 : if (result == false)
16509 : return false;
16510 :
16511 : /* Warn if we haven't seen a scalar finalizer procedure (but we know there
16512 : were nodes in the list, must have been for arrays. It is surely a good
16513 : idea to have a scalar version there if there's something to finalize. */
16514 2591 : if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
16515 1 : gfc_warning (OPT_Wsurprising,
16516 : "Only array FINAL procedures declared for derived type %qs"
16517 : " defined at %L, suggest also scalar one unless an assumed"
16518 : " rank finalizer has been declared",
16519 : derived->name, &derived->declared_at);
16520 :
16521 2591 : if (!derived->attr.pdt_template)
16522 : {
16523 2567 : vtab = gfc_find_derived_vtab (derived);
16524 2567 : c = vtab->ts.u.derived->components->next->next->next->next->next;
16525 2567 : if (c && c->initializer && c->initializer->symtree && c->initializer->symtree->n.sym)
16526 2567 : gfc_set_sym_referenced (c->initializer->symtree->n.sym);
16527 : }
16528 :
16529 2591 : if (finalizable)
16530 664 : *finalizable = true;
16531 :
16532 : return true;
16533 : }
16534 :
16535 :
16536 : static gfc_symbol * containing_dt;
16537 :
16538 : /* Helper function for check_generic_tbp_ambiguity, which ensures that passed
16539 : arguments whose declared types are PDT instances only transmit the PASS arg
16540 : if they match the enclosing derived type. */
16541 :
16542 : static bool
16543 1460 : check_pdt_args (gfc_tbp_generic* t, const char *pass)
16544 : {
16545 1460 : gfc_formal_arglist *dummy_args;
16546 1460 : if (pass && containing_dt != NULL && containing_dt->attr.pdt_type)
16547 : {
16548 532 : dummy_args = gfc_sym_get_dummy_args (t->specific->u.specific->n.sym);
16549 1190 : while (dummy_args && strcmp (pass, dummy_args->sym->name))
16550 126 : dummy_args = dummy_args->next;
16551 532 : gcc_assert (strcmp (pass, dummy_args->sym->name) == 0);
16552 532 : if (dummy_args->sym->ts.type == BT_CLASS
16553 532 : && strcmp (CLASS_DATA (dummy_args->sym)->ts.u.derived->name,
16554 : containing_dt->name))
16555 : return true;
16556 : }
16557 : return false;
16558 : }
16559 :
16560 :
16561 : /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
16562 :
16563 : static bool
16564 732 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
16565 : const char* generic_name, locus where)
16566 : {
16567 732 : gfc_symbol *sym1, *sym2;
16568 732 : const char *pass1, *pass2;
16569 732 : gfc_formal_arglist *dummy_args;
16570 :
16571 732 : gcc_assert (t1->specific && t2->specific);
16572 732 : gcc_assert (!t1->specific->is_generic);
16573 732 : gcc_assert (!t2->specific->is_generic);
16574 732 : gcc_assert (t1->is_operator == t2->is_operator);
16575 :
16576 732 : sym1 = t1->specific->u.specific->n.sym;
16577 732 : sym2 = t2->specific->u.specific->n.sym;
16578 :
16579 732 : if (sym1 == sym2)
16580 : return true;
16581 :
16582 : /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
16583 732 : if (sym1->attr.subroutine != sym2->attr.subroutine
16584 730 : || sym1->attr.function != sym2->attr.function)
16585 : {
16586 2 : gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
16587 : " GENERIC %qs at %L",
16588 : sym1->name, sym2->name, generic_name, &where);
16589 2 : return false;
16590 : }
16591 :
16592 : /* Determine PASS arguments. */
16593 730 : if (t1->specific->nopass)
16594 : pass1 = NULL;
16595 679 : else if (t1->specific->pass_arg)
16596 : pass1 = t1->specific->pass_arg;
16597 : else
16598 : {
16599 420 : dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
16600 420 : if (dummy_args)
16601 419 : pass1 = dummy_args->sym->name;
16602 : else
16603 : pass1 = NULL;
16604 : }
16605 730 : if (t2->specific->nopass)
16606 : pass2 = NULL;
16607 678 : else if (t2->specific->pass_arg)
16608 : pass2 = t2->specific->pass_arg;
16609 : else
16610 : {
16611 541 : dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
16612 541 : if (dummy_args)
16613 540 : pass2 = dummy_args->sym->name;
16614 : else
16615 : pass2 = NULL;
16616 : }
16617 :
16618 : /* Care must be taken with pdt types and templates because the declared type
16619 : of the argument that is not 'no_pass' need not be the same as the
16620 : containing derived type. If this is the case, subject the argument to
16621 : the full interface check, even though it cannot be used in the type
16622 : bound context. */
16623 730 : pass1 = check_pdt_args (t1, pass1) ? NULL : pass1;
16624 730 : pass2 = check_pdt_args (t2, pass2) ? NULL : pass2;
16625 :
16626 730 : if (containing_dt != NULL && containing_dt->attr.pdt_template)
16627 730 : pass1 = pass2 = NULL;
16628 :
16629 : /* Compare the interfaces. */
16630 730 : if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
16631 : NULL, 0, pass1, pass2))
16632 : {
16633 8 : gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
16634 : sym1->name, sym2->name, generic_name, &where);
16635 8 : return false;
16636 : }
16637 :
16638 : return true;
16639 : }
16640 :
16641 :
16642 : /* Worker function for resolving a generic procedure binding; this is used to
16643 : resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
16644 :
16645 : The difference between those cases is finding possible inherited bindings
16646 : that are overridden, as one has to look for them in tb_sym_root,
16647 : tb_uop_root or tb_op, respectively. Thus the caller must already find
16648 : the super-type and set p->overridden correctly. */
16649 :
16650 : static bool
16651 2310 : resolve_tb_generic_targets (gfc_symbol* super_type,
16652 : gfc_typebound_proc* p, const char* name)
16653 : {
16654 2310 : gfc_tbp_generic* target;
16655 2310 : gfc_symtree* first_target;
16656 2310 : gfc_symtree* inherited;
16657 :
16658 2310 : gcc_assert (p && p->is_generic);
16659 :
16660 : /* Try to find the specific bindings for the symtrees in our target-list. */
16661 2310 : gcc_assert (p->u.generic);
16662 5200 : for (target = p->u.generic; target; target = target->next)
16663 2907 : if (!target->specific)
16664 : {
16665 2528 : gfc_typebound_proc* overridden_tbp;
16666 2528 : gfc_tbp_generic* g;
16667 2528 : const char* target_name;
16668 :
16669 2528 : target_name = target->specific_st->name;
16670 :
16671 : /* Defined for this type directly. */
16672 2528 : if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
16673 : {
16674 2519 : target->specific = target->specific_st->n.tb;
16675 2519 : goto specific_found;
16676 : }
16677 :
16678 : /* Look for an inherited specific binding. */
16679 9 : if (super_type)
16680 : {
16681 5 : inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
16682 : true, NULL);
16683 :
16684 5 : if (inherited)
16685 : {
16686 5 : gcc_assert (inherited->n.tb);
16687 5 : target->specific = inherited->n.tb;
16688 5 : goto specific_found;
16689 : }
16690 : }
16691 :
16692 4 : gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
16693 : " at %L", target_name, name, &p->where);
16694 4 : return false;
16695 :
16696 : /* Once we've found the specific binding, check it is not ambiguous with
16697 : other specifics already found or inherited for the same GENERIC. */
16698 2524 : specific_found:
16699 2524 : gcc_assert (target->specific);
16700 :
16701 : /* This must really be a specific binding! */
16702 2524 : if (target->specific->is_generic)
16703 : {
16704 3 : gfc_error ("GENERIC %qs at %L must target a specific binding,"
16705 : " %qs is GENERIC, too", name, &p->where, target_name);
16706 3 : return false;
16707 : }
16708 :
16709 : /* Check those already resolved on this type directly. */
16710 6456 : for (g = p->u.generic; g; g = g->next)
16711 1428 : if (g != target && g->specific
16712 4656 : && !check_generic_tbp_ambiguity (target, g, name, p->where))
16713 : return false;
16714 :
16715 : /* Check for ambiguity with inherited specific targets. */
16716 2530 : for (overridden_tbp = p->overridden; overridden_tbp;
16717 16 : overridden_tbp = overridden_tbp->overridden)
16718 19 : if (overridden_tbp->is_generic)
16719 : {
16720 33 : for (g = overridden_tbp->u.generic; g; g = g->next)
16721 : {
16722 18 : gcc_assert (g->specific);
16723 18 : if (!check_generic_tbp_ambiguity (target, g, name, p->where))
16724 : return false;
16725 : }
16726 : }
16727 : }
16728 :
16729 : /* If we attempt to "overwrite" a specific binding, this is an error. */
16730 2293 : if (p->overridden && !p->overridden->is_generic)
16731 : {
16732 1 : gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
16733 : " the same name", name, &p->where);
16734 1 : return false;
16735 : }
16736 :
16737 : /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
16738 : all must have the same attributes here. */
16739 2292 : first_target = p->u.generic->specific->u.specific;
16740 2292 : gcc_assert (first_target);
16741 2292 : p->subroutine = first_target->n.sym->attr.subroutine;
16742 2292 : p->function = first_target->n.sym->attr.function;
16743 :
16744 2292 : return true;
16745 : }
16746 :
16747 :
16748 : /* Resolve a GENERIC procedure binding for a derived type. */
16749 :
16750 : static bool
16751 1204 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
16752 : {
16753 1204 : gfc_symbol* super_type;
16754 :
16755 : /* Find the overridden binding if any. */
16756 1204 : st->n.tb->overridden = NULL;
16757 1204 : super_type = gfc_get_derived_super_type (derived);
16758 1204 : if (super_type)
16759 : {
16760 40 : gfc_symtree* overridden;
16761 40 : overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
16762 : true, NULL);
16763 :
16764 40 : if (overridden && overridden->n.tb)
16765 21 : st->n.tb->overridden = overridden->n.tb;
16766 : }
16767 :
16768 : /* Resolve using worker function. */
16769 1204 : return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
16770 : }
16771 :
16772 :
16773 : /* Retrieve the target-procedure of an operator binding and do some checks in
16774 : common for intrinsic and user-defined type-bound operators. */
16775 :
16776 : static gfc_symbol*
16777 1178 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
16778 : {
16779 1178 : gfc_symbol* target_proc;
16780 :
16781 1178 : gcc_assert (target->specific && !target->specific->is_generic);
16782 1178 : target_proc = target->specific->u.specific->n.sym;
16783 1178 : gcc_assert (target_proc);
16784 :
16785 : /* F08:C468. All operator bindings must have a passed-object dummy argument. */
16786 1178 : if (target->specific->nopass)
16787 : {
16788 2 : gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
16789 2 : return NULL;
16790 : }
16791 :
16792 : return target_proc;
16793 : }
16794 :
16795 :
16796 : /* Resolve a type-bound intrinsic operator. */
16797 :
16798 : static bool
16799 1047 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
16800 : gfc_typebound_proc* p)
16801 : {
16802 1047 : gfc_symbol* super_type;
16803 1047 : gfc_tbp_generic* target;
16804 :
16805 : /* If there's already an error here, do nothing (but don't fail again). */
16806 1047 : if (p->error)
16807 : return true;
16808 :
16809 : /* Operators should always be GENERIC bindings. */
16810 1047 : gcc_assert (p->is_generic);
16811 :
16812 : /* Look for an overridden binding. */
16813 1047 : super_type = gfc_get_derived_super_type (derived);
16814 1047 : if (super_type && super_type->f2k_derived)
16815 1 : p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
16816 : op, true, NULL);
16817 : else
16818 1046 : p->overridden = NULL;
16819 :
16820 : /* Resolve general GENERIC properties using worker function. */
16821 1047 : if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
16822 1 : goto error;
16823 :
16824 : /* Check the targets to be procedures of correct interface. */
16825 2139 : for (target = p->u.generic; target; target = target->next)
16826 : {
16827 1118 : gfc_symbol* target_proc;
16828 :
16829 1118 : target_proc = get_checked_tb_operator_target (target, p->where);
16830 1118 : if (!target_proc)
16831 1 : goto error;
16832 :
16833 1117 : if (!gfc_check_operator_interface (target_proc, op, p->where))
16834 3 : goto error;
16835 :
16836 : /* Add target to non-typebound operator list. */
16837 1114 : if (!target->specific->deferred && !derived->attr.use_assoc
16838 391 : && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
16839 : {
16840 389 : gfc_interface *head, *intr;
16841 :
16842 : /* Preempt 'gfc_check_new_interface' for submodules, where the
16843 : mechanism for handling module procedures winds up resolving
16844 : operator interfaces twice and would otherwise cause an error.
16845 : Likewise, new instances of PDTs can cause the operator inter-
16846 : faces to be resolved multiple times. */
16847 461 : for (intr = derived->ns->op[op]; intr; intr = intr->next)
16848 91 : if (intr->sym == target_proc
16849 21 : && (target_proc->attr.used_in_submodule
16850 4 : || derived->attr.pdt_type
16851 2 : || derived->attr.pdt_template))
16852 : return true;
16853 :
16854 370 : if (!gfc_check_new_interface (derived->ns->op[op],
16855 : target_proc, p->where))
16856 : return false;
16857 368 : head = derived->ns->op[op];
16858 368 : intr = gfc_get_interface ();
16859 368 : intr->sym = target_proc;
16860 368 : intr->where = p->where;
16861 368 : intr->next = head;
16862 368 : derived->ns->op[op] = intr;
16863 : }
16864 : }
16865 :
16866 : return true;
16867 :
16868 5 : error:
16869 5 : p->error = 1;
16870 5 : return false;
16871 : }
16872 :
16873 :
16874 : /* Resolve a type-bound user operator (tree-walker callback). */
16875 :
16876 : static gfc_symbol* resolve_bindings_derived;
16877 : static bool resolve_bindings_result;
16878 :
16879 : static bool check_uop_procedure (gfc_symbol* sym, locus where);
16880 :
16881 : static void
16882 59 : resolve_typebound_user_op (gfc_symtree* stree)
16883 : {
16884 59 : gfc_symbol* super_type;
16885 59 : gfc_tbp_generic* target;
16886 :
16887 59 : gcc_assert (stree && stree->n.tb);
16888 :
16889 59 : if (stree->n.tb->error)
16890 : return;
16891 :
16892 : /* Operators should always be GENERIC bindings. */
16893 59 : gcc_assert (stree->n.tb->is_generic);
16894 :
16895 : /* Find overridden procedure, if any. */
16896 59 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16897 59 : if (super_type && super_type->f2k_derived)
16898 : {
16899 0 : gfc_symtree* overridden;
16900 0 : overridden = gfc_find_typebound_user_op (super_type, NULL,
16901 : stree->name, true, NULL);
16902 :
16903 0 : if (overridden && overridden->n.tb)
16904 0 : stree->n.tb->overridden = overridden->n.tb;
16905 : }
16906 : else
16907 59 : stree->n.tb->overridden = NULL;
16908 :
16909 : /* Resolve basically using worker function. */
16910 59 : if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
16911 0 : goto error;
16912 :
16913 : /* Check the targets to be functions of correct interface. */
16914 116 : for (target = stree->n.tb->u.generic; target; target = target->next)
16915 : {
16916 60 : gfc_symbol* target_proc;
16917 :
16918 60 : target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
16919 60 : if (!target_proc)
16920 1 : goto error;
16921 :
16922 59 : if (!check_uop_procedure (target_proc, stree->n.tb->where))
16923 2 : goto error;
16924 : }
16925 :
16926 : return;
16927 :
16928 3 : error:
16929 3 : resolve_bindings_result = false;
16930 3 : stree->n.tb->error = 1;
16931 : }
16932 :
16933 :
16934 : /* Resolve the type-bound procedures for a derived type. */
16935 :
16936 : static void
16937 9951 : resolve_typebound_procedure (gfc_symtree* stree)
16938 : {
16939 9951 : gfc_symbol* proc;
16940 9951 : locus where;
16941 9951 : gfc_symbol* me_arg;
16942 9951 : gfc_symbol* super_type;
16943 9951 : gfc_component* comp;
16944 :
16945 9951 : gcc_assert (stree);
16946 :
16947 : /* Undefined specific symbol from GENERIC target definition. */
16948 9951 : if (!stree->n.tb)
16949 9869 : return;
16950 :
16951 9945 : if (stree->n.tb->error)
16952 : return;
16953 :
16954 : /* If this is a GENERIC binding, use that routine. */
16955 9929 : if (stree->n.tb->is_generic)
16956 : {
16957 1204 : if (!resolve_typebound_generic (resolve_bindings_derived, stree))
16958 17 : goto error;
16959 : return;
16960 : }
16961 :
16962 : /* Get the target-procedure to check it. */
16963 8725 : gcc_assert (!stree->n.tb->is_generic);
16964 8725 : gcc_assert (stree->n.tb->u.specific);
16965 8725 : proc = stree->n.tb->u.specific->n.sym;
16966 8725 : where = stree->n.tb->where;
16967 :
16968 : /* Default access should already be resolved from the parser. */
16969 8725 : gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
16970 :
16971 8725 : if (stree->n.tb->deferred)
16972 : {
16973 676 : if (!check_proc_interface (proc, &where))
16974 5 : goto error;
16975 : }
16976 : else
16977 : {
16978 : /* If proc has not been resolved at this point, proc->name may
16979 : actually be a USE associated entity. See PR fortran/89647. */
16980 8049 : if (!proc->resolve_symbol_called
16981 5365 : && proc->attr.function == 0 && proc->attr.subroutine == 0)
16982 : {
16983 11 : gfc_symbol *tmp;
16984 11 : gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
16985 11 : if (tmp && tmp->attr.use_assoc)
16986 : {
16987 1 : proc->module = tmp->module;
16988 1 : proc->attr.proc = tmp->attr.proc;
16989 1 : proc->attr.function = tmp->attr.function;
16990 1 : proc->attr.subroutine = tmp->attr.subroutine;
16991 1 : proc->attr.use_assoc = tmp->attr.use_assoc;
16992 1 : proc->ts = tmp->ts;
16993 1 : proc->result = tmp->result;
16994 : }
16995 : }
16996 :
16997 : /* Check for F08:C465. */
16998 8049 : if ((!proc->attr.subroutine && !proc->attr.function)
16999 8039 : || (proc->attr.proc != PROC_MODULE
17000 70 : && proc->attr.if_source != IFSRC_IFBODY
17001 7 : && !proc->attr.module_procedure)
17002 8038 : || proc->attr.abstract)
17003 : {
17004 12 : gfc_error ("%qs must be a module procedure or an external "
17005 : "procedure with an explicit interface at %L",
17006 : proc->name, &where);
17007 12 : goto error;
17008 : }
17009 : }
17010 :
17011 8708 : stree->n.tb->subroutine = proc->attr.subroutine;
17012 8708 : stree->n.tb->function = proc->attr.function;
17013 :
17014 : /* Find the super-type of the current derived type. We could do this once and
17015 : store in a global if speed is needed, but as long as not I believe this is
17016 : more readable and clearer. */
17017 8708 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
17018 :
17019 : /* If PASS, resolve and check arguments if not already resolved / loaded
17020 : from a .mod file. */
17021 8708 : if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
17022 : {
17023 2774 : gfc_formal_arglist *dummy_args;
17024 :
17025 2774 : dummy_args = gfc_sym_get_dummy_args (proc);
17026 2774 : if (stree->n.tb->pass_arg)
17027 : {
17028 468 : gfc_formal_arglist *i;
17029 :
17030 : /* If an explicit passing argument name is given, walk the arg-list
17031 : and look for it. */
17032 :
17033 468 : me_arg = NULL;
17034 468 : stree->n.tb->pass_arg_num = 1;
17035 601 : for (i = dummy_args; i; i = i->next)
17036 : {
17037 599 : if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
17038 : {
17039 : me_arg = i->sym;
17040 : break;
17041 : }
17042 133 : ++stree->n.tb->pass_arg_num;
17043 : }
17044 :
17045 468 : if (!me_arg)
17046 : {
17047 2 : gfc_error ("Procedure %qs with PASS(%s) at %L has no"
17048 : " argument %qs",
17049 : proc->name, stree->n.tb->pass_arg, &where,
17050 : stree->n.tb->pass_arg);
17051 2 : goto error;
17052 : }
17053 : }
17054 : else
17055 : {
17056 : /* Otherwise, take the first one; there should in fact be at least
17057 : one. */
17058 2306 : stree->n.tb->pass_arg_num = 1;
17059 2306 : if (!dummy_args)
17060 : {
17061 2 : gfc_error ("Procedure %qs with PASS at %L must have at"
17062 : " least one argument", proc->name, &where);
17063 2 : goto error;
17064 : }
17065 2304 : me_arg = dummy_args->sym;
17066 : }
17067 :
17068 : /* Now check that the argument-type matches and the passed-object
17069 : dummy argument is generally fine. */
17070 :
17071 2304 : gcc_assert (me_arg);
17072 :
17073 2770 : if (me_arg->ts.type != BT_CLASS)
17074 : {
17075 5 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
17076 : " at %L", proc->name, &where);
17077 5 : goto error;
17078 : }
17079 :
17080 : /* The derived type is not a PDT template or type. Resolve as usual. */
17081 2765 : if (!resolve_bindings_derived->attr.pdt_template
17082 2756 : && !(containing_dt && containing_dt->attr.pdt_type
17083 60 : && CLASS_DATA (me_arg)->ts.u.derived != containing_dt)
17084 2736 : && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
17085 : {
17086 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
17087 : "the derived-type %qs", me_arg->name, proc->name,
17088 : me_arg->name, &where, resolve_bindings_derived->name);
17089 0 : goto error;
17090 : }
17091 :
17092 2765 : if (resolve_bindings_derived->attr.pdt_template
17093 2774 : && !gfc_pdt_is_instance_of (resolve_bindings_derived,
17094 9 : CLASS_DATA (me_arg)->ts.u.derived))
17095 : {
17096 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
17097 : "the parametric derived-type %qs", me_arg->name,
17098 : proc->name, me_arg->name, &where,
17099 : resolve_bindings_derived->name);
17100 0 : goto error;
17101 : }
17102 :
17103 2765 : if (((resolve_bindings_derived->attr.pdt_template
17104 9 : && gfc_pdt_is_instance_of (resolve_bindings_derived,
17105 9 : CLASS_DATA (me_arg)->ts.u.derived))
17106 2756 : || resolve_bindings_derived->attr.pdt_type)
17107 69 : && (me_arg->param_list != NULL)
17108 2834 : && (gfc_spec_list_type (me_arg->param_list,
17109 69 : CLASS_DATA(me_arg)->ts.u.derived)
17110 : != SPEC_ASSUMED))
17111 : {
17112 :
17113 : /* Add a check to verify if there are any LEN parameters in the
17114 : first place. If there are LEN parameters, throw this error.
17115 : If there are only KIND parameters, then don't trigger
17116 : this error. */
17117 6 : gfc_component *c;
17118 6 : bool seen_len_param = false;
17119 6 : gfc_actual_arglist *me_arg_param = me_arg->param_list;
17120 :
17121 6 : for (; me_arg_param; me_arg_param = me_arg_param->next)
17122 : {
17123 6 : c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
17124 : me_arg_param->name, true, true, NULL);
17125 :
17126 6 : gcc_assert (c != NULL);
17127 :
17128 6 : if (c->attr.pdt_kind)
17129 0 : continue;
17130 :
17131 : /* Getting here implies that there is a pdt_len parameter
17132 : in the list. */
17133 : seen_len_param = true;
17134 : break;
17135 : }
17136 :
17137 6 : if (seen_len_param)
17138 : {
17139 6 : gfc_error ("All LEN type parameters of the passed dummy "
17140 : "argument %qs of %qs at %L must be ASSUMED.",
17141 : me_arg->name, proc->name, &where);
17142 6 : goto error;
17143 : }
17144 : }
17145 :
17146 2759 : gcc_assert (me_arg->ts.type == BT_CLASS);
17147 2759 : if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
17148 : {
17149 1 : gfc_error ("Passed-object dummy argument of %qs at %L must be"
17150 : " scalar", proc->name, &where);
17151 1 : goto error;
17152 : }
17153 2758 : if (CLASS_DATA (me_arg)->attr.allocatable)
17154 : {
17155 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
17156 : " be ALLOCATABLE", proc->name, &where);
17157 2 : goto error;
17158 : }
17159 2756 : if (CLASS_DATA (me_arg)->attr.class_pointer)
17160 : {
17161 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
17162 : " be POINTER", proc->name, &where);
17163 2 : goto error;
17164 : }
17165 : }
17166 :
17167 : /* If we are extending some type, check that we don't override a procedure
17168 : flagged NON_OVERRIDABLE. */
17169 8688 : stree->n.tb->overridden = NULL;
17170 8688 : if (super_type)
17171 : {
17172 1491 : gfc_symtree* overridden;
17173 1491 : overridden = gfc_find_typebound_proc (super_type, NULL,
17174 : stree->name, true, NULL);
17175 :
17176 1491 : if (overridden)
17177 : {
17178 1214 : if (overridden->n.tb)
17179 1214 : stree->n.tb->overridden = overridden->n.tb;
17180 :
17181 1214 : if (!gfc_check_typebound_override (stree, overridden))
17182 26 : goto error;
17183 : }
17184 : }
17185 :
17186 : /* See if there's a name collision with a component directly in this type. */
17187 20866 : for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
17188 12205 : if (!strcmp (comp->name, stree->name))
17189 : {
17190 1 : gfc_error ("Procedure %qs at %L has the same name as a component of"
17191 : " %qs",
17192 : stree->name, &where, resolve_bindings_derived->name);
17193 1 : goto error;
17194 : }
17195 :
17196 : /* Try to find a name collision with an inherited component. */
17197 8661 : if (super_type && gfc_find_component (super_type, stree->name, true, true,
17198 : NULL))
17199 : {
17200 1 : gfc_error ("Procedure %qs at %L has the same name as an inherited"
17201 : " component of %qs",
17202 : stree->name, &where, resolve_bindings_derived->name);
17203 1 : goto error;
17204 : }
17205 :
17206 8660 : stree->n.tb->error = 0;
17207 8660 : return;
17208 :
17209 82 : error:
17210 82 : resolve_bindings_result = false;
17211 82 : stree->n.tb->error = 1;
17212 : }
17213 :
17214 :
17215 : static bool
17216 86347 : resolve_typebound_procedures (gfc_symbol* derived)
17217 : {
17218 86347 : int op;
17219 86347 : gfc_symbol* super_type;
17220 :
17221 86347 : if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
17222 : return true;
17223 :
17224 4770 : super_type = gfc_get_derived_super_type (derived);
17225 4770 : if (super_type)
17226 857 : resolve_symbol (super_type);
17227 :
17228 4770 : resolve_bindings_derived = derived;
17229 4770 : resolve_bindings_result = true;
17230 :
17231 4770 : containing_dt = derived; /* Needed for checks of PDTs. */
17232 4770 : if (derived->f2k_derived->tb_sym_root)
17233 4770 : gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
17234 : &resolve_typebound_procedure);
17235 :
17236 4770 : if (derived->f2k_derived->tb_uop_root)
17237 55 : gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
17238 : &resolve_typebound_user_op);
17239 4770 : containing_dt = NULL;
17240 :
17241 138330 : for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
17242 : {
17243 133560 : gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
17244 133560 : if (p && !resolve_typebound_intrinsic_op (derived,
17245 : (gfc_intrinsic_op)op, p))
17246 7 : resolve_bindings_result = false;
17247 : }
17248 :
17249 4770 : return resolve_bindings_result;
17250 : }
17251 :
17252 :
17253 : /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
17254 : to give all identical derived types the same backend_decl. */
17255 : static void
17256 177153 : add_dt_to_dt_list (gfc_symbol *derived)
17257 : {
17258 177153 : if (!derived->dt_next)
17259 : {
17260 82449 : if (gfc_derived_types)
17261 : {
17262 67609 : derived->dt_next = gfc_derived_types->dt_next;
17263 67609 : gfc_derived_types->dt_next = derived;
17264 : }
17265 : else
17266 : {
17267 14840 : derived->dt_next = derived;
17268 : }
17269 82449 : gfc_derived_types = derived;
17270 : }
17271 177153 : }
17272 :
17273 :
17274 : /* Ensure that a derived-type is really not abstract, meaning that every
17275 : inherited DEFERRED binding is overridden by a non-DEFERRED one. */
17276 :
17277 : static bool
17278 7086 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
17279 : {
17280 7086 : if (!st)
17281 : return true;
17282 :
17283 2772 : if (!ensure_not_abstract_walker (sub, st->left))
17284 : return false;
17285 2772 : if (!ensure_not_abstract_walker (sub, st->right))
17286 : return false;
17287 :
17288 2771 : if (st->n.tb && st->n.tb->deferred)
17289 : {
17290 2019 : gfc_symtree* overriding;
17291 2019 : overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
17292 2019 : if (!overriding)
17293 : return false;
17294 2018 : gcc_assert (overriding->n.tb);
17295 2018 : if (overriding->n.tb->deferred)
17296 : {
17297 5 : gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
17298 : " %qs is DEFERRED and not overridden",
17299 : sub->name, &sub->declared_at, st->name);
17300 5 : return false;
17301 : }
17302 : }
17303 :
17304 : return true;
17305 : }
17306 :
17307 : static bool
17308 1394 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
17309 : {
17310 : /* The algorithm used here is to recursively travel up the ancestry of sub
17311 : and for each ancestor-type, check all bindings. If any of them is
17312 : DEFERRED, look it up starting from sub and see if the found (overriding)
17313 : binding is not DEFERRED.
17314 : This is not the most efficient way to do this, but it should be ok and is
17315 : clearer than something sophisticated. */
17316 :
17317 1543 : gcc_assert (ancestor && !sub->attr.abstract);
17318 :
17319 1543 : if (!ancestor->attr.abstract)
17320 : return true;
17321 :
17322 : /* Walk bindings of this ancestor. */
17323 1542 : if (ancestor->f2k_derived)
17324 : {
17325 1542 : bool t;
17326 1542 : t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
17327 1542 : if (!t)
17328 : return false;
17329 : }
17330 :
17331 : /* Find next ancestor type and recurse on it. */
17332 1536 : ancestor = gfc_get_derived_super_type (ancestor);
17333 1536 : if (ancestor)
17334 : return ensure_not_abstract (sub, ancestor);
17335 :
17336 : return true;
17337 : }
17338 :
17339 :
17340 : /* This check for typebound defined assignments is done recursively
17341 : since the order in which derived types are resolved is not always in
17342 : order of the declarations. */
17343 :
17344 : static void
17345 181704 : check_defined_assignments (gfc_symbol *derived)
17346 : {
17347 181704 : gfc_component *c;
17348 :
17349 609078 : for (c = derived->components; c; c = c->next)
17350 : {
17351 429151 : if (!gfc_bt_struct (c->ts.type)
17352 103750 : || c->attr.pointer
17353 20566 : || c->attr.proc_pointer_comp
17354 20566 : || c->attr.class_pointer
17355 20560 : || c->attr.proc_pointer)
17356 409035 : continue;
17357 :
17358 20116 : if (c->ts.u.derived->attr.defined_assign_comp
17359 19881 : || (c->ts.u.derived->f2k_derived
17360 19299 : && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
17361 : {
17362 1753 : derived->attr.defined_assign_comp = 1;
17363 1753 : return;
17364 : }
17365 :
17366 18363 : if (c->attr.allocatable)
17367 6691 : continue;
17368 :
17369 11672 : check_defined_assignments (c->ts.u.derived);
17370 11672 : if (c->ts.u.derived->attr.defined_assign_comp)
17371 : {
17372 24 : derived->attr.defined_assign_comp = 1;
17373 24 : return;
17374 : }
17375 : }
17376 : }
17377 :
17378 :
17379 : /* Resolve a single component of a derived type or structure. */
17380 :
17381 : static bool
17382 409256 : resolve_component (gfc_component *c, gfc_symbol *sym)
17383 : {
17384 409256 : gfc_symbol *super_type;
17385 409256 : symbol_attribute *attr;
17386 :
17387 409256 : if (c->attr.artificial)
17388 : return true;
17389 :
17390 : /* Do not allow vtype components to be resolved in nameless namespaces
17391 : such as block data because the procedure pointers will cause ICEs
17392 : and vtables are not needed in these contexts. */
17393 279347 : if (sym->attr.vtype && sym->attr.use_assoc
17394 48710 : && sym->ns->proc_name == NULL)
17395 : return true;
17396 :
17397 : /* F2008, C442. */
17398 279338 : if ((!sym->attr.is_class || c != sym->components)
17399 279338 : && c->attr.codimension
17400 208 : && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
17401 : {
17402 4 : gfc_error ("Coarray component %qs at %L must be allocatable with "
17403 : "deferred shape", c->name, &c->loc);
17404 4 : return false;
17405 : }
17406 :
17407 : /* F2008, C443. */
17408 279334 : if (c->attr.codimension && c->ts.type == BT_DERIVED
17409 85 : && c->ts.u.derived->ts.is_iso_c)
17410 : {
17411 1 : gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
17412 : "shall not be a coarray", c->name, &c->loc);
17413 1 : return false;
17414 : }
17415 :
17416 : /* F2008, C444. */
17417 279333 : if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
17418 28 : && (c->attr.codimension || c->attr.pointer || c->attr.dimension
17419 26 : || c->attr.allocatable))
17420 : {
17421 3 : gfc_error ("Component %qs at %L with coarray component "
17422 : "shall be a nonpointer, nonallocatable scalar",
17423 : c->name, &c->loc);
17424 3 : return false;
17425 : }
17426 :
17427 : /* F2008, C448. */
17428 279330 : if (c->ts.type == BT_CLASS)
17429 : {
17430 6916 : if (c->attr.class_ok && CLASS_DATA (c))
17431 : {
17432 6908 : attr = &(CLASS_DATA (c)->attr);
17433 :
17434 : /* Fix up contiguous attribute. */
17435 6908 : if (c->attr.contiguous)
17436 11 : attr->contiguous = 1;
17437 : }
17438 : else
17439 : attr = NULL;
17440 : }
17441 : else
17442 272414 : attr = &c->attr;
17443 :
17444 279333 : if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
17445 : {
17446 5 : gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
17447 : "is not an array pointer", c->name, &c->loc);
17448 5 : return false;
17449 : }
17450 :
17451 : /* F2003, 15.2.1 - length has to be one. */
17452 40846 : if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
17453 279344 : && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
17454 19 : || !gfc_is_constant_expr (c->ts.u.cl->length)
17455 19 : || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
17456 : {
17457 1 : gfc_error ("Component %qs of BIND(C) type at %L must have length one",
17458 : c->name, &c->loc);
17459 1 : return false;
17460 : }
17461 :
17462 51970 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.pdt_template
17463 307 : && !sym->attr.pdt_type && !sym->attr.pdt_template
17464 279332 : && !(gfc_get_derived_super_type (sym)
17465 0 : && (gfc_get_derived_super_type (sym)->attr.pdt_type
17466 0 : || gfc_get_derived_super_type (sym)->attr.pdt_template)))
17467 : {
17468 8 : gfc_actual_arglist *type_spec_list;
17469 8 : if (gfc_get_pdt_instance (c->param_list, &c->ts.u.derived,
17470 : &type_spec_list)
17471 : != MATCH_YES)
17472 0 : return false;
17473 8 : gfc_free_actual_arglist (c->param_list);
17474 8 : c->param_list = type_spec_list;
17475 8 : if (!sym->attr.pdt_type)
17476 8 : sym->attr.pdt_comp = 1;
17477 : }
17478 279316 : else if (IS_PDT (c) && !sym->attr.pdt_type)
17479 54 : sym->attr.pdt_comp = 1;
17480 :
17481 279324 : if (c->attr.proc_pointer && c->ts.interface)
17482 : {
17483 14620 : gfc_symbol *ifc = c->ts.interface;
17484 :
17485 14620 : if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
17486 : {
17487 6 : c->tb->error = 1;
17488 6 : return false;
17489 : }
17490 :
17491 14614 : if (ifc->attr.if_source || ifc->attr.intrinsic)
17492 : {
17493 : /* Resolve interface and copy attributes. */
17494 14565 : if (ifc->formal && !ifc->formal_ns)
17495 2560 : resolve_symbol (ifc);
17496 14565 : if (ifc->attr.intrinsic)
17497 0 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
17498 :
17499 14565 : if (ifc->result)
17500 : {
17501 7601 : c->ts = ifc->result->ts;
17502 7601 : c->attr.allocatable = ifc->result->attr.allocatable;
17503 7601 : c->attr.pointer = ifc->result->attr.pointer;
17504 7601 : c->attr.dimension = ifc->result->attr.dimension;
17505 7601 : c->as = gfc_copy_array_spec (ifc->result->as);
17506 7601 : c->attr.class_ok = ifc->result->attr.class_ok;
17507 : }
17508 : else
17509 : {
17510 6964 : c->ts = ifc->ts;
17511 6964 : c->attr.allocatable = ifc->attr.allocatable;
17512 6964 : c->attr.pointer = ifc->attr.pointer;
17513 6964 : c->attr.dimension = ifc->attr.dimension;
17514 6964 : c->as = gfc_copy_array_spec (ifc->as);
17515 6964 : c->attr.class_ok = ifc->attr.class_ok;
17516 : }
17517 14565 : c->ts.interface = ifc;
17518 14565 : c->attr.function = ifc->attr.function;
17519 14565 : c->attr.subroutine = ifc->attr.subroutine;
17520 :
17521 14565 : c->attr.pure = ifc->attr.pure;
17522 14565 : c->attr.elemental = ifc->attr.elemental;
17523 14565 : c->attr.recursive = ifc->attr.recursive;
17524 14565 : c->attr.always_explicit = ifc->attr.always_explicit;
17525 14565 : c->attr.ext_attr |= ifc->attr.ext_attr;
17526 : /* Copy char length. */
17527 14565 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
17528 : {
17529 491 : gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
17530 454 : if (cl->length && !cl->resolved
17531 601 : && !gfc_resolve_expr (cl->length))
17532 : {
17533 0 : c->tb->error = 1;
17534 0 : return false;
17535 : }
17536 491 : c->ts.u.cl = cl;
17537 : }
17538 : }
17539 : }
17540 264704 : else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
17541 : {
17542 : /* Since PPCs are not implicitly typed, a PPC without an explicit
17543 : interface must be a subroutine. */
17544 116 : gfc_add_subroutine (&c->attr, c->name, &c->loc);
17545 : }
17546 :
17547 : /* Procedure pointer components: Check PASS arg. */
17548 279318 : if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
17549 805 : && !sym->attr.vtype)
17550 : {
17551 95 : gfc_symbol* me_arg;
17552 :
17553 95 : if (c->tb->pass_arg)
17554 : {
17555 20 : gfc_formal_arglist* i;
17556 :
17557 : /* If an explicit passing argument name is given, walk the arg-list
17558 : and look for it. */
17559 :
17560 20 : me_arg = NULL;
17561 20 : c->tb->pass_arg_num = 1;
17562 34 : for (i = c->ts.interface->formal; i; i = i->next)
17563 : {
17564 33 : if (!strcmp (i->sym->name, c->tb->pass_arg))
17565 : {
17566 : me_arg = i->sym;
17567 : break;
17568 : }
17569 14 : c->tb->pass_arg_num++;
17570 : }
17571 :
17572 20 : if (!me_arg)
17573 : {
17574 1 : gfc_error ("Procedure pointer component %qs with PASS(%s) "
17575 : "at %L has no argument %qs", c->name,
17576 : c->tb->pass_arg, &c->loc, c->tb->pass_arg);
17577 1 : c->tb->error = 1;
17578 1 : return false;
17579 : }
17580 : }
17581 : else
17582 : {
17583 : /* Otherwise, take the first one; there should in fact be at least
17584 : one. */
17585 75 : c->tb->pass_arg_num = 1;
17586 75 : if (!c->ts.interface->formal)
17587 : {
17588 3 : gfc_error ("Procedure pointer component %qs with PASS at %L "
17589 : "must have at least one argument",
17590 : c->name, &c->loc);
17591 3 : c->tb->error = 1;
17592 3 : return false;
17593 : }
17594 72 : me_arg = c->ts.interface->formal->sym;
17595 : }
17596 :
17597 : /* Now check that the argument-type matches. */
17598 72 : gcc_assert (me_arg);
17599 91 : if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
17600 90 : || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
17601 90 : || (me_arg->ts.type == BT_CLASS
17602 82 : && CLASS_DATA (me_arg)->ts.u.derived != sym))
17603 : {
17604 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
17605 : " the derived type %qs", me_arg->name, c->name,
17606 : me_arg->name, &c->loc, sym->name);
17607 1 : c->tb->error = 1;
17608 1 : return false;
17609 : }
17610 :
17611 : /* Check for F03:C453. */
17612 90 : if (CLASS_DATA (me_arg)->attr.dimension)
17613 : {
17614 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
17615 : "must be scalar", me_arg->name, c->name, me_arg->name,
17616 : &c->loc);
17617 1 : c->tb->error = 1;
17618 1 : return false;
17619 : }
17620 :
17621 89 : if (CLASS_DATA (me_arg)->attr.class_pointer)
17622 : {
17623 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
17624 : "may not have the POINTER attribute", me_arg->name,
17625 : c->name, me_arg->name, &c->loc);
17626 1 : c->tb->error = 1;
17627 1 : return false;
17628 : }
17629 :
17630 88 : if (CLASS_DATA (me_arg)->attr.allocatable)
17631 : {
17632 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
17633 : "may not be ALLOCATABLE", me_arg->name, c->name,
17634 : me_arg->name, &c->loc);
17635 1 : c->tb->error = 1;
17636 1 : return false;
17637 : }
17638 :
17639 87 : if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
17640 : {
17641 2 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
17642 : " at %L", c->name, &c->loc);
17643 2 : return false;
17644 : }
17645 :
17646 : }
17647 :
17648 : /* Check type-spec if this is not the parent-type component. */
17649 279308 : if (((sym->attr.is_class
17650 12428 : && (!sym->components->ts.u.derived->attr.extension
17651 2385 : || c != CLASS_DATA (sym->components)))
17652 268216 : || (!sym->attr.is_class
17653 266880 : && (!sym->attr.extension || c != sym->components)))
17654 271215 : && !sym->attr.vtype
17655 442135 : && !resolve_typespec_used (&c->ts, &c->loc, c->name))
17656 : return false;
17657 :
17658 279307 : super_type = gfc_get_derived_super_type (sym);
17659 :
17660 : /* If this type is an extension, set the accessibility of the parent
17661 : component. */
17662 279307 : if (super_type
17663 25580 : && ((sym->attr.is_class
17664 12428 : && c == CLASS_DATA (sym->components))
17665 16920 : || (!sym->attr.is_class && c == sym->components))
17666 15417 : && strcmp (super_type->name, c->name) == 0)
17667 6595 : c->attr.access = super_type->attr.access;
17668 :
17669 : /* If this type is an extension, see if this component has the same name
17670 : as an inherited type-bound procedure. */
17671 25580 : if (super_type && !sym->attr.is_class
17672 13152 : && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
17673 : {
17674 1 : gfc_error ("Component %qs of %qs at %L has the same name as an"
17675 : " inherited type-bound procedure",
17676 : c->name, sym->name, &c->loc);
17677 1 : return false;
17678 : }
17679 :
17680 279306 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
17681 9425 : && !c->ts.deferred)
17682 : {
17683 7196 : if (sym->attr.pdt_template || c->attr.pdt_string)
17684 258 : gfc_correct_parm_expr (sym, &c->ts.u.cl->length);
17685 :
17686 7196 : if (c->ts.u.cl->length == NULL
17687 7190 : || !resolve_charlen(c->ts.u.cl)
17688 14385 : || !gfc_is_constant_expr (c->ts.u.cl->length))
17689 : {
17690 9 : gfc_error ("Character length of component %qs needs to "
17691 : "be a constant specification expression at %L",
17692 : c->name,
17693 9 : c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
17694 9 : return false;
17695 : }
17696 :
17697 7187 : if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
17698 : {
17699 2 : if (!c->ts.u.cl->length->error)
17700 : {
17701 1 : gfc_error ("Character length expression of component %qs at %L "
17702 : "must be of INTEGER type, found %s",
17703 1 : c->name, &c->ts.u.cl->length->where,
17704 : gfc_basic_typename (c->ts.u.cl->length->ts.type));
17705 1 : c->ts.u.cl->length->error = 1;
17706 : }
17707 2 : return false;
17708 : }
17709 : }
17710 :
17711 279295 : if (c->ts.type == BT_CHARACTER && c->ts.deferred
17712 2265 : && !c->attr.pointer && !c->attr.allocatable)
17713 : {
17714 1 : gfc_error ("Character component %qs of %qs at %L with deferred "
17715 : "length must be a POINTER or ALLOCATABLE",
17716 : c->name, sym->name, &c->loc);
17717 1 : return false;
17718 : }
17719 :
17720 : /* Add the hidden deferred length field. */
17721 279294 : if (c->ts.type == BT_CHARACTER
17722 9925 : && (c->ts.deferred || c->attr.pdt_string)
17723 2441 : && !c->attr.function
17724 2405 : && !sym->attr.is_class)
17725 : {
17726 2258 : char name[GFC_MAX_SYMBOL_LEN+9];
17727 2258 : gfc_component *strlen;
17728 2258 : sprintf (name, "_%s_length", c->name);
17729 2258 : strlen = gfc_find_component (sym, name, true, true, NULL);
17730 2258 : if (strlen == NULL)
17731 : {
17732 480 : if (!gfc_add_component (sym, name, &strlen))
17733 0 : return false;
17734 480 : strlen->ts.type = BT_INTEGER;
17735 480 : strlen->ts.kind = gfc_charlen_int_kind;
17736 480 : strlen->attr.access = ACCESS_PRIVATE;
17737 480 : strlen->attr.artificial = 1;
17738 : }
17739 : }
17740 :
17741 279294 : if (c->ts.type == BT_DERIVED
17742 52150 : && sym->component_access != ACCESS_PRIVATE
17743 51130 : && gfc_check_symbol_access (sym)
17744 100224 : && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
17745 50060 : && !c->ts.u.derived->attr.use_assoc
17746 26850 : && !gfc_check_symbol_access (c->ts.u.derived)
17747 279490 : && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
17748 : "PRIVATE type and cannot be a component of "
17749 : "%qs, which is PUBLIC at %L", c->name,
17750 : sym->name, &sym->declared_at))
17751 : return false;
17752 :
17753 279293 : if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
17754 : {
17755 2 : gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
17756 : "type %s", c->name, &c->loc, sym->name);
17757 2 : return false;
17758 : }
17759 :
17760 279291 : if (sym->attr.sequence)
17761 : {
17762 2506 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
17763 : {
17764 0 : gfc_error ("Component %s of SEQUENCE type declared at %L does "
17765 : "not have the SEQUENCE attribute",
17766 : c->ts.u.derived->name, &sym->declared_at);
17767 0 : return false;
17768 : }
17769 : }
17770 :
17771 279291 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
17772 0 : c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
17773 279291 : else if (c->ts.type == BT_CLASS && c->attr.class_ok
17774 7248 : && CLASS_DATA (c)->ts.u.derived->attr.generic)
17775 0 : CLASS_DATA (c)->ts.u.derived
17776 0 : = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
17777 :
17778 : /* If an allocatable component derived type is of the same type as
17779 : the enclosing derived type, we need a vtable generating so that
17780 : the __deallocate procedure is created. */
17781 279291 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
17782 59408 : && c->ts.u.derived == sym && c->attr.allocatable == 1)
17783 399 : gfc_find_vtab (&c->ts);
17784 :
17785 : /* Ensure that all the derived type components are put on the
17786 : derived type list; even in formal namespaces, where derived type
17787 : pointer components might not have been declared. */
17788 279291 : if (c->ts.type == BT_DERIVED
17789 52149 : && c->ts.u.derived
17790 52149 : && c->ts.u.derived->components
17791 48885 : && c->attr.pointer
17792 33507 : && sym != c->ts.u.derived)
17793 4260 : add_dt_to_dt_list (c->ts.u.derived);
17794 :
17795 279291 : if (c->as && c->as->type != AS_DEFERRED
17796 6376 : && (c->attr.pointer || c->attr.allocatable))
17797 : return false;
17798 :
17799 279277 : if (!gfc_resolve_array_spec (c->as,
17800 279277 : !(c->attr.pointer || c->attr.proc_pointer
17801 227371 : || c->attr.allocatable)))
17802 : return false;
17803 :
17804 105452 : if (c->initializer && !sym->attr.vtype
17805 32239 : && !c->attr.pdt_kind && !c->attr.pdt_len
17806 308430 : && !gfc_check_assign_symbol (sym, c, c->initializer))
17807 : return false;
17808 :
17809 : return true;
17810 : }
17811 :
17812 :
17813 : /* Be nice about the locus for a structure expression - show the locus of the
17814 : first non-null sub-expression if we can. */
17815 :
17816 : static locus *
17817 4 : cons_where (gfc_expr *struct_expr)
17818 : {
17819 4 : gfc_constructor *cons;
17820 :
17821 4 : gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
17822 :
17823 4 : cons = gfc_constructor_first (struct_expr->value.constructor);
17824 12 : for (; cons; cons = gfc_constructor_next (cons))
17825 : {
17826 8 : if (cons->expr && cons->expr->expr_type != EXPR_NULL)
17827 4 : return &cons->expr->where;
17828 : }
17829 :
17830 0 : return &struct_expr->where;
17831 : }
17832 :
17833 : /* Resolve the components of a structure type. Much less work than derived
17834 : types. */
17835 :
17836 : static bool
17837 913 : resolve_fl_struct (gfc_symbol *sym)
17838 : {
17839 913 : gfc_component *c;
17840 913 : gfc_expr *init = NULL;
17841 913 : bool success;
17842 :
17843 : /* Make sure UNIONs do not have overlapping initializers. */
17844 913 : if (sym->attr.flavor == FL_UNION)
17845 : {
17846 498 : for (c = sym->components; c; c = c->next)
17847 : {
17848 331 : if (init && c->initializer)
17849 : {
17850 2 : gfc_error ("Conflicting initializers in union at %L and %L",
17851 : cons_where (init), cons_where (c->initializer));
17852 2 : gfc_free_expr (c->initializer);
17853 2 : c->initializer = NULL;
17854 : }
17855 291 : if (init == NULL)
17856 291 : init = c->initializer;
17857 : }
17858 : }
17859 :
17860 913 : success = true;
17861 2830 : for (c = sym->components; c; c = c->next)
17862 1917 : if (!resolve_component (c, sym))
17863 0 : success = false;
17864 :
17865 913 : if (!success)
17866 : return false;
17867 :
17868 913 : if (sym->components)
17869 862 : add_dt_to_dt_list (sym);
17870 :
17871 : return true;
17872 : }
17873 :
17874 : /* Figure if the derived type is using itself directly in one of its components
17875 : or through referencing other derived types. The information is required to
17876 : generate the __deallocate and __final type bound procedures to ensure
17877 : freeing larger hierarchies of derived types with allocatable objects. */
17878 :
17879 : static void
17880 138019 : resolve_cyclic_derived_type (gfc_symbol *derived)
17881 : {
17882 138019 : hash_set<gfc_symbol *> seen, to_examin;
17883 138019 : gfc_component *c;
17884 138019 : seen.add (derived);
17885 138019 : to_examin.add (derived);
17886 462861 : while (!to_examin.is_empty ())
17887 : {
17888 189015 : gfc_symbol *cand = *to_examin.begin ();
17889 189015 : to_examin.remove (cand);
17890 509311 : for (c = cand->components; c; c = c->next)
17891 322488 : if (c->ts.type == BT_DERIVED)
17892 : {
17893 71204 : if (c->ts.u.derived == derived)
17894 : {
17895 1168 : derived->attr.recursive = 1;
17896 2192 : return;
17897 : }
17898 70036 : else if (!seen.contains (c->ts.u.derived))
17899 : {
17900 46445 : seen.add (c->ts.u.derived);
17901 46445 : to_examin.add (c->ts.u.derived);
17902 : }
17903 : }
17904 251284 : else if (c->ts.type == BT_CLASS)
17905 : {
17906 9560 : if (!c->attr.class_ok)
17907 7 : continue;
17908 9553 : if (CLASS_DATA (c)->ts.u.derived == derived)
17909 : {
17910 1024 : derived->attr.recursive = 1;
17911 1024 : return;
17912 : }
17913 8529 : else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
17914 : {
17915 4776 : seen.add (CLASS_DATA (c)->ts.u.derived);
17916 4776 : to_examin.add (CLASS_DATA (c)->ts.u.derived);
17917 : }
17918 : }
17919 : }
17920 138019 : }
17921 :
17922 : /* Resolve the components of a derived type. This does not have to wait until
17923 : resolution stage, but can be done as soon as the dt declaration has been
17924 : parsed. */
17925 :
17926 : static bool
17927 170128 : resolve_fl_derived0 (gfc_symbol *sym)
17928 : {
17929 170128 : gfc_symbol* super_type;
17930 170128 : gfc_component *c;
17931 170128 : gfc_formal_arglist *f;
17932 170128 : bool success;
17933 :
17934 170128 : if (sym->attr.unlimited_polymorphic)
17935 : return true;
17936 :
17937 170128 : super_type = gfc_get_derived_super_type (sym);
17938 :
17939 : /* F2008, C432. */
17940 170128 : if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
17941 : {
17942 2 : gfc_error ("As extending type %qs at %L has a coarray component, "
17943 : "parent type %qs shall also have one", sym->name,
17944 : &sym->declared_at, super_type->name);
17945 2 : return false;
17946 : }
17947 :
17948 : /* Ensure the extended type gets resolved before we do. */
17949 17451 : if (super_type && !resolve_fl_derived0 (super_type))
17950 : return false;
17951 :
17952 : /* An ABSTRACT type must be extensible. */
17953 170120 : if (sym->attr.abstract && !gfc_type_is_extensible (sym))
17954 : {
17955 2 : gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
17956 : sym->name, &sym->declared_at);
17957 2 : return false;
17958 : }
17959 :
17960 : /* Resolving components below, may create vtabs for which the cyclic type
17961 : information needs to be present. */
17962 170118 : if (!sym->attr.vtype)
17963 138019 : resolve_cyclic_derived_type (sym);
17964 :
17965 170118 : c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
17966 : : sym->components;
17967 :
17968 : success = true;
17969 577457 : for ( ; c != NULL; c = c->next)
17970 407339 : if (!resolve_component (c, sym))
17971 96 : success = false;
17972 :
17973 170118 : if (!success)
17974 : return false;
17975 :
17976 : /* Now add the caf token field, where needed. */
17977 170032 : if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
17978 1000 : && !sym->attr.vtype)
17979 : {
17980 2238 : for (c = sym->components; c; c = c->next)
17981 1441 : if (!c->attr.dimension && !c->attr.codimension
17982 795 : && (c->attr.allocatable || c->attr.pointer))
17983 : {
17984 146 : char name[GFC_MAX_SYMBOL_LEN+9];
17985 146 : gfc_component *token;
17986 146 : sprintf (name, "_caf_%s", c->name);
17987 146 : token = gfc_find_component (sym, name, true, true, NULL);
17988 146 : if (token == NULL)
17989 : {
17990 82 : if (!gfc_add_component (sym, name, &token))
17991 0 : return false;
17992 82 : token->ts.type = BT_VOID;
17993 82 : token->ts.kind = gfc_default_integer_kind;
17994 82 : token->attr.access = ACCESS_PRIVATE;
17995 82 : token->attr.artificial = 1;
17996 82 : token->attr.caf_token = 1;
17997 : }
17998 146 : c->caf_token = token;
17999 : }
18000 : }
18001 :
18002 170032 : check_defined_assignments (sym);
18003 :
18004 170032 : if (!sym->attr.defined_assign_comp && super_type)
18005 16444 : sym->attr.defined_assign_comp
18006 16444 : = super_type->attr.defined_assign_comp;
18007 :
18008 : /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
18009 : all DEFERRED bindings are overridden. */
18010 17444 : if (super_type && super_type->attr.abstract && !sym->attr.abstract
18011 1397 : && !sym->attr.is_class
18012 3147 : && !ensure_not_abstract (sym, super_type))
18013 : return false;
18014 :
18015 : /* Check that there is a component for every PDT parameter. */
18016 170026 : if (sym->attr.pdt_template)
18017 : {
18018 2340 : for (f = sym->formal; f; f = f->next)
18019 : {
18020 1362 : if (!f->sym)
18021 1 : continue;
18022 1361 : c = gfc_find_component (sym, f->sym->name, true, true, NULL);
18023 1361 : if (c == NULL)
18024 : {
18025 9 : gfc_error ("Parameterized type %qs does not have a component "
18026 : "corresponding to parameter %qs at %L", sym->name,
18027 9 : f->sym->name, &sym->declared_at);
18028 9 : break;
18029 : }
18030 : }
18031 : }
18032 :
18033 : /* Add derived type to the derived type list. */
18034 170026 : add_dt_to_dt_list (sym);
18035 :
18036 170026 : return true;
18037 : }
18038 :
18039 : /* The following procedure does the full resolution of a derived type,
18040 : including resolution of all type-bound procedures (if present). In contrast
18041 : to 'resolve_fl_derived0' this can only be done after the module has been
18042 : parsed completely. */
18043 :
18044 : static bool
18045 88456 : resolve_fl_derived (gfc_symbol *sym)
18046 : {
18047 88456 : gfc_symbol *gen_dt = NULL;
18048 :
18049 88456 : if (sym->attr.unlimited_polymorphic)
18050 : return true;
18051 :
18052 88456 : if (!sym->attr.is_class)
18053 75758 : gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
18054 56699 : if (gen_dt && gen_dt->generic && gen_dt->generic->next
18055 2289 : && (!gen_dt->generic->sym->attr.use_assoc
18056 2146 : || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
18057 88632 : && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
18058 : "%qs at %L being the same name as derived "
18059 : "type at %L", sym->name,
18060 : gen_dt->generic->sym == sym
18061 11 : ? gen_dt->generic->next->sym->name
18062 : : gen_dt->generic->sym->name,
18063 : gen_dt->generic->sym == sym
18064 11 : ? &gen_dt->generic->next->sym->declared_at
18065 : : &gen_dt->generic->sym->declared_at,
18066 : &sym->declared_at))
18067 : return false;
18068 :
18069 88452 : if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
18070 : {
18071 13 : gfc_error ("Derived type %qs at %L has not been declared",
18072 : sym->name, &sym->declared_at);
18073 13 : return false;
18074 : }
18075 :
18076 : /* Resolve the finalizer procedures. */
18077 88439 : if (!gfc_resolve_finalizers (sym, NULL))
18078 : return false;
18079 :
18080 88436 : if (sym->attr.is_class && sym->ts.u.derived == NULL)
18081 : {
18082 : /* Fix up incomplete CLASS symbols. */
18083 12698 : gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
18084 12698 : gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
18085 :
18086 12698 : if (data->ts.u.derived->attr.pdt_template)
18087 : {
18088 6 : match m;
18089 6 : m = gfc_get_pdt_instance (sym->param_list, &data->ts.u.derived,
18090 : &data->param_list);
18091 6 : if (m != MATCH_YES
18092 6 : || !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
18093 : {
18094 0 : gfc_error ("Failed to build PDT class component at %L",
18095 : &sym->declared_at);
18096 0 : return false;
18097 : }
18098 6 : data = gfc_find_component (sym, "_data", true, true, NULL);
18099 6 : vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
18100 : }
18101 :
18102 : /* Nothing more to do for unlimited polymorphic entities. */
18103 12698 : if (data->ts.u.derived->attr.unlimited_polymorphic)
18104 : {
18105 2005 : add_dt_to_dt_list (sym);
18106 2005 : return true;
18107 : }
18108 10693 : else if (vptr->ts.u.derived == NULL)
18109 : {
18110 6317 : gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
18111 6317 : gcc_assert (vtab);
18112 6317 : vptr->ts.u.derived = vtab->ts.u.derived;
18113 6317 : if (vptr->ts.u.derived && !resolve_fl_derived0 (vptr->ts.u.derived))
18114 : return false;
18115 : }
18116 : }
18117 :
18118 86431 : if (!resolve_fl_derived0 (sym))
18119 : return false;
18120 :
18121 : /* Resolve the type-bound procedures. */
18122 86347 : if (!resolve_typebound_procedures (sym))
18123 : return false;
18124 :
18125 : /* Generate module vtables subject to their accessibility and their not
18126 : being vtables or pdt templates. If this is not done class declarations
18127 : in external procedures wind up with their own version and so SELECT TYPE
18128 : fails because the vptrs do not have the same address. */
18129 86306 : if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
18130 86245 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
18131 64662 : || (sym->attr.recursive && sym->attr.alloc_comp))
18132 21737 : && sym->attr.access != ACCESS_PRIVATE
18133 21704 : && !(sym->attr.vtype || sym->attr.pdt_template))
18134 : {
18135 19528 : gfc_symbol *vtab = gfc_find_derived_vtab (sym);
18136 19528 : gfc_set_sym_referenced (vtab);
18137 : }
18138 :
18139 : return true;
18140 : }
18141 :
18142 :
18143 : static bool
18144 855 : resolve_fl_namelist (gfc_symbol *sym)
18145 : {
18146 855 : gfc_namelist *nl;
18147 855 : gfc_symbol *nlsym;
18148 :
18149 3024 : for (nl = sym->namelist; nl; nl = nl->next)
18150 : {
18151 : /* Check again, the check in match only works if NAMELIST comes
18152 : after the decl. */
18153 2174 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
18154 : {
18155 1 : gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
18156 : "allowed", nl->sym->name, sym->name, &sym->declared_at);
18157 1 : return false;
18158 : }
18159 :
18160 672 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
18161 2181 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
18162 : "with assumed shape in namelist %qs at %L",
18163 : nl->sym->name, sym->name, &sym->declared_at))
18164 : return false;
18165 :
18166 2172 : if (is_non_constant_shape_array (nl->sym)
18167 2222 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
18168 : "with nonconstant shape in namelist %qs at %L",
18169 50 : nl->sym->name, sym->name, &sym->declared_at))
18170 : return false;
18171 :
18172 2171 : if (nl->sym->ts.type == BT_CHARACTER
18173 593 : && (nl->sym->ts.u.cl->length == NULL
18174 554 : || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
18175 2253 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
18176 : "nonconstant character length in "
18177 82 : "namelist %qs at %L", nl->sym->name,
18178 : sym->name, &sym->declared_at))
18179 : return false;
18180 :
18181 : }
18182 :
18183 : /* Reject PRIVATE objects in a PUBLIC namelist. */
18184 850 : if (gfc_check_symbol_access (sym))
18185 : {
18186 3005 : for (nl = sym->namelist; nl; nl = nl->next)
18187 : {
18188 2168 : if (!nl->sym->attr.use_assoc
18189 4040 : && !is_sym_host_assoc (nl->sym, sym->ns)
18190 4166 : && !gfc_check_symbol_access (nl->sym))
18191 : {
18192 2 : gfc_error ("NAMELIST object %qs was declared PRIVATE and "
18193 : "cannot be member of PUBLIC namelist %qs at %L",
18194 2 : nl->sym->name, sym->name, &sym->declared_at);
18195 2 : return false;
18196 : }
18197 :
18198 2166 : if (nl->sym->ts.type == BT_DERIVED
18199 466 : && (nl->sym->ts.u.derived->attr.alloc_comp
18200 464 : || nl->sym->ts.u.derived->attr.pointer_comp))
18201 : {
18202 5 : if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
18203 : "namelist %qs at %L with ALLOCATABLE "
18204 : "or POINTER components", nl->sym->name,
18205 : sym->name, &sym->declared_at))
18206 : return false;
18207 : return true;
18208 : }
18209 :
18210 : /* Types with private components that came here by USE-association. */
18211 2161 : if (nl->sym->ts.type == BT_DERIVED
18212 2161 : && derived_inaccessible (nl->sym->ts.u.derived))
18213 : {
18214 6 : gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
18215 : "components and cannot be member of namelist %qs at %L",
18216 : nl->sym->name, sym->name, &sym->declared_at);
18217 6 : return false;
18218 : }
18219 :
18220 : /* Types with private components that are defined in the same module. */
18221 2155 : if (nl->sym->ts.type == BT_DERIVED
18222 910 : && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
18223 2433 : && nl->sym->ts.u.derived->attr.private_comp)
18224 : {
18225 0 : gfc_error ("NAMELIST object %qs has PRIVATE components and "
18226 : "cannot be a member of PUBLIC namelist %qs at %L",
18227 : nl->sym->name, sym->name, &sym->declared_at);
18228 0 : return false;
18229 : }
18230 : }
18231 : }
18232 :
18233 :
18234 : /* 14.1.2 A module or internal procedure represent local entities
18235 : of the same type as a namelist member and so are not allowed. */
18236 2989 : for (nl = sym->namelist; nl; nl = nl->next)
18237 : {
18238 2155 : if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
18239 1596 : continue;
18240 :
18241 559 : if (nl->sym->attr.function && nl->sym == nl->sym->result)
18242 7 : if ((nl->sym == sym->ns->proc_name)
18243 1 : ||
18244 1 : (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
18245 6 : continue;
18246 :
18247 553 : nlsym = NULL;
18248 553 : if (nl->sym->name)
18249 553 : gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
18250 553 : if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
18251 : {
18252 3 : gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
18253 : "attribute in %qs at %L", nlsym->name,
18254 : &sym->declared_at);
18255 3 : return false;
18256 : }
18257 : }
18258 :
18259 : return true;
18260 : }
18261 :
18262 :
18263 : static bool
18264 404188 : resolve_fl_parameter (gfc_symbol *sym)
18265 : {
18266 : /* A parameter array's shape needs to be constant. */
18267 404188 : if (sym->as != NULL
18268 404188 : && (sym->as->type == AS_DEFERRED
18269 6256 : || is_non_constant_shape_array (sym)))
18270 : {
18271 17 : gfc_error ("Parameter array %qs at %L cannot be automatic "
18272 : "or of deferred shape", sym->name, &sym->declared_at);
18273 17 : return false;
18274 : }
18275 :
18276 : /* Constraints on deferred type parameter. */
18277 404171 : if (!deferred_requirements (sym))
18278 : return false;
18279 :
18280 : /* Make sure a parameter that has been implicitly typed still
18281 : matches the implicit type, since PARAMETER statements can precede
18282 : IMPLICIT statements. */
18283 404170 : if (sym->attr.implicit_type
18284 404883 : && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
18285 713 : sym->ns)))
18286 : {
18287 0 : gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
18288 : "later IMPLICIT type", sym->name, &sym->declared_at);
18289 0 : return false;
18290 : }
18291 :
18292 : /* Make sure the types of derived parameters are consistent. This
18293 : type checking is deferred until resolution because the type may
18294 : refer to a derived type from the host. */
18295 404170 : if (sym->ts.type == BT_DERIVED
18296 404170 : && !gfc_compare_types (&sym->ts, &sym->value->ts))
18297 : {
18298 0 : gfc_error ("Incompatible derived type in PARAMETER at %L",
18299 0 : &sym->value->where);
18300 0 : return false;
18301 : }
18302 :
18303 : /* F03:C509,C514. */
18304 404170 : if (sym->ts.type == BT_CLASS)
18305 : {
18306 0 : gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
18307 : sym->name, &sym->declared_at);
18308 0 : return false;
18309 : }
18310 :
18311 : /* Some programmers can have a typo when using an implied-do loop to
18312 : initialize an array constant. For example,
18313 : INTEGER I,J
18314 : INTEGER, PARAMETER :: A(3) = [(I, I = 1, 3)] ! OK
18315 : INTEGER, PARAMETER :: B(3) = [(A(J), I = 1, 3)] ! Not OK, J undefined
18316 : This check catches the typo. */
18317 404170 : if (sym->attr.dimension
18318 6249 : && sym->value && sym->value->expr_type == EXPR_ARRAY
18319 410415 : && !gfc_is_constant_expr (sym->value))
18320 : {
18321 : /* PR fortran/117070 argues a nonconstant proc pointer can appear in
18322 : the array constructor of a paramater. This seems inconsistant with
18323 : the concept of a parameter. TODO: Needs an interpretation. */
18324 20 : if (sym->value->ts.type == BT_DERIVED
18325 18 : && sym->value->ts.u.derived
18326 18 : && sym->value->ts.u.derived->attr.proc_pointer_comp)
18327 : return true;
18328 2 : gfc_error ("Expecting constant expression near %L", &sym->value->where);
18329 2 : return false;
18330 : }
18331 :
18332 : return true;
18333 : }
18334 :
18335 :
18336 : /* Called by resolve_symbol to check PDTs. */
18337 :
18338 : static void
18339 1377 : resolve_pdt (gfc_symbol* sym)
18340 : {
18341 1377 : gfc_symbol *derived = NULL;
18342 1377 : gfc_actual_arglist *param;
18343 1377 : gfc_component *c;
18344 1377 : bool const_len_exprs = true;
18345 1377 : bool assumed_len_exprs = false;
18346 1377 : symbol_attribute *attr;
18347 :
18348 1377 : if (sym->ts.type == BT_DERIVED)
18349 : {
18350 1150 : derived = sym->ts.u.derived;
18351 1150 : attr = &(sym->attr);
18352 : }
18353 227 : else if (sym->ts.type == BT_CLASS)
18354 : {
18355 227 : derived = CLASS_DATA (sym)->ts.u.derived;
18356 227 : attr = &(CLASS_DATA (sym)->attr);
18357 : }
18358 : else
18359 0 : gcc_unreachable ();
18360 :
18361 1377 : gcc_assert (derived->attr.pdt_type);
18362 :
18363 3276 : for (param = sym->param_list; param; param = param->next)
18364 : {
18365 1899 : c = gfc_find_component (derived, param->name, false, true, NULL);
18366 1899 : gcc_assert (c);
18367 1899 : if (c->attr.pdt_kind)
18368 1016 : continue;
18369 :
18370 614 : if (param->expr && !gfc_is_constant_expr (param->expr)
18371 967 : && c->attr.pdt_len)
18372 : const_len_exprs = false;
18373 799 : else if (param->spec_type == SPEC_ASSUMED)
18374 291 : assumed_len_exprs = true;
18375 :
18376 883 : if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
18377 18 : && ((sym->ts.type == BT_DERIVED && !attr->pointer)
18378 16 : || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
18379 3 : gfc_error ("Entity %qs at %L has a deferred LEN "
18380 : "parameter %qs and requires either the POINTER "
18381 : "or ALLOCATABLE attribute",
18382 : sym->name, &sym->declared_at,
18383 : param->name);
18384 :
18385 : }
18386 :
18387 1377 : if (!const_len_exprs
18388 84 : && (sym->ns->proc_name->attr.is_main_program
18389 83 : || sym->ns->proc_name->attr.flavor == FL_MODULE
18390 82 : || sym->attr.save != SAVE_NONE))
18391 2 : gfc_error ("The AUTOMATIC object %qs at %L must not have the "
18392 : "SAVE attribute or be a variable declared in the "
18393 : "main program, a module or a submodule(F08/C513)",
18394 : sym->name, &sym->declared_at);
18395 :
18396 1377 : if (assumed_len_exprs && !(sym->attr.dummy
18397 1 : || sym->attr.select_type_temporary || sym->attr.associate_var))
18398 1 : gfc_error ("The object %qs at %L with ASSUMED type parameters "
18399 : "must be a dummy or a SELECT TYPE selector(F08/4.2)",
18400 : sym->name, &sym->declared_at);
18401 1377 : }
18402 :
18403 :
18404 : /* Resolve the symbol's array spec. */
18405 :
18406 : static bool
18407 1725873 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
18408 : {
18409 1725873 : gfc_namespace *orig_current_ns = gfc_current_ns;
18410 1725873 : gfc_current_ns = gfc_get_spec_ns (sym);
18411 :
18412 1725873 : bool saved_specification_expr = specification_expr;
18413 1725873 : gfc_symbol *saved_specification_expr_symbol = specification_expr_symbol;
18414 1725873 : specification_expr = true;
18415 1725873 : specification_expr_symbol = sym;
18416 :
18417 1725873 : bool result = gfc_resolve_array_spec (sym->as, check_constant);
18418 :
18419 1725873 : specification_expr = saved_specification_expr;
18420 1725873 : specification_expr_symbol = saved_specification_expr_symbol;
18421 1725873 : gfc_current_ns = orig_current_ns;
18422 :
18423 1725873 : return result;
18424 : }
18425 :
18426 :
18427 : /* Do anything necessary to resolve a symbol. Right now, we just
18428 : assume that an otherwise unknown symbol is a variable. This sort
18429 : of thing commonly happens for symbols in module. */
18430 :
18431 : static void
18432 1868539 : resolve_symbol (gfc_symbol *sym)
18433 : {
18434 1868539 : int check_constant, mp_flag;
18435 1868539 : gfc_symtree *symtree;
18436 1868539 : gfc_symtree *this_symtree;
18437 1868539 : gfc_namespace *ns;
18438 1868539 : gfc_component *c;
18439 1868539 : symbol_attribute class_attr;
18440 1868539 : gfc_array_spec *as;
18441 :
18442 1868539 : if (sym->resolve_symbol_called >= 1)
18443 173987 : return;
18444 1794117 : sym->resolve_symbol_called = 1;
18445 :
18446 : /* No symbol will ever have union type; only components can be unions.
18447 : Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
18448 : (just like derived type declaration symbols have flavor FL_DERIVED). */
18449 1794117 : gcc_assert (sym->ts.type != BT_UNION);
18450 :
18451 : /* Coarrayed polymorphic objects with allocatable or pointer components are
18452 : yet unsupported for -fcoarray=lib. */
18453 1794117 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
18454 112 : && sym->ts.u.derived && CLASS_DATA (sym)
18455 112 : && CLASS_DATA (sym)->attr.codimension
18456 94 : && CLASS_DATA (sym)->ts.u.derived
18457 93 : && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
18458 90 : || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
18459 : {
18460 6 : gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
18461 : "type coarrays at %L are unsupported", &sym->declared_at);
18462 6 : return;
18463 : }
18464 :
18465 1794111 : if (sym->attr.artificial)
18466 : return;
18467 :
18468 1697243 : if (sym->attr.unlimited_polymorphic)
18469 : return;
18470 :
18471 1695786 : if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
18472 : {
18473 4 : gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
18474 : "the OpenMP DEPEND clause", &sym->declared_at);
18475 4 : return;
18476 : }
18477 :
18478 1695782 : if (sym->attr.flavor == FL_UNKNOWN
18479 1674525 : || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
18480 445509 : && !sym->attr.generic && !sym->attr.external
18481 180418 : && sym->attr.if_source == IFSRC_UNKNOWN
18482 81262 : && sym->ts.type == BT_UNKNOWN))
18483 : {
18484 : /* A symbol in a common block might not have been resolved yet properly.
18485 : Do not try to find an interface with the same name. */
18486 94010 : if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
18487 21253 : && !sym->attr.generic && !sym->attr.external
18488 21202 : && sym->attr.in_common)
18489 2594 : goto skip_interfaces;
18490 :
18491 : /* If we find that a flavorless symbol is an interface in one of the
18492 : parent namespaces, find its symtree in this namespace, free the
18493 : symbol and set the symtree to point to the interface symbol. */
18494 130438 : for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
18495 : {
18496 39724 : symtree = gfc_find_symtree (ns->sym_root, sym->name);
18497 39724 : if (symtree && (symtree->n.sym->generic ||
18498 748 : (symtree->n.sym->attr.flavor == FL_PROCEDURE
18499 658 : && sym->ns->construct_entities)))
18500 : {
18501 710 : this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
18502 : sym->name);
18503 710 : if (this_symtree->n.sym == sym)
18504 : {
18505 702 : symtree->n.sym->refs++;
18506 702 : gfc_release_symbol (sym);
18507 702 : this_symtree->n.sym = symtree->n.sym;
18508 702 : return;
18509 : }
18510 : }
18511 : }
18512 :
18513 90714 : skip_interfaces:
18514 : /* Otherwise give it a flavor according to such attributes as
18515 : it has. */
18516 93308 : if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
18517 21072 : && sym->attr.intrinsic == 0)
18518 21068 : sym->attr.flavor = FL_VARIABLE;
18519 72240 : else if (sym->attr.flavor == FL_UNKNOWN)
18520 : {
18521 55 : sym->attr.flavor = FL_PROCEDURE;
18522 55 : if (sym->attr.dimension)
18523 0 : sym->attr.function = 1;
18524 : }
18525 : }
18526 :
18527 1695080 : if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
18528 2346 : gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
18529 :
18530 1492 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
18531 1696572 : && !resolve_procedure_interface (sym))
18532 : return;
18533 :
18534 1695069 : if (sym->attr.is_protected && !sym->attr.proc_pointer
18535 130 : && (sym->attr.procedure || sym->attr.external))
18536 : {
18537 0 : if (sym->attr.external)
18538 0 : gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
18539 : "at %L", &sym->declared_at);
18540 : else
18541 0 : gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
18542 : "at %L", &sym->declared_at);
18543 :
18544 0 : return;
18545 : }
18546 :
18547 : /* Ensure that variables of derived or class type having a finalizer are
18548 : marked used even when the variable is not used anything else in the scope.
18549 : This fixes PR118730. */
18550 652547 : if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
18551 446891 : && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
18552 1744681 : && gfc_may_be_finalized (sym->ts))
18553 8525 : gfc_set_sym_referenced (sym);
18554 :
18555 1695069 : if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
18556 : return;
18557 :
18558 1694293 : else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
18559 1695056 : && !resolve_fl_struct (sym))
18560 : return;
18561 :
18562 : /* Symbols that are module procedures with results (functions) have
18563 : the types and array specification copied for type checking in
18564 : procedures that call them, as well as for saving to a module
18565 : file. These symbols can't stand the scrutiny that their results
18566 : can. */
18567 1694924 : mp_flag = (sym->result != NULL && sym->result != sym);
18568 :
18569 : /* Make sure that the intrinsic is consistent with its internal
18570 : representation. This needs to be done before assigning a default
18571 : type to avoid spurious warnings. */
18572 1660729 : if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
18573 1731228 : && !gfc_resolve_intrinsic (sym, &sym->declared_at))
18574 : return;
18575 :
18576 : /* Resolve associate names. */
18577 1694888 : if (sym->assoc)
18578 6790 : resolve_assoc_var (sym, true);
18579 :
18580 : /* Assign default type to symbols that need one and don't have one. */
18581 1694888 : if (sym->ts.type == BT_UNKNOWN)
18582 : {
18583 405476 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
18584 : {
18585 11759 : gfc_set_default_type (sym, 1, NULL);
18586 : }
18587 :
18588 262963 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
18589 61358 : && !sym->attr.function && !sym->attr.subroutine
18590 407122 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
18591 595 : gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
18592 :
18593 405476 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
18594 : {
18595 : /* The specific case of an external procedure should emit an error
18596 : in the case that there is no implicit type. */
18597 102558 : if (!mp_flag)
18598 : {
18599 96548 : if (!sym->attr.mixed_entry_master)
18600 96440 : gfc_set_default_type (sym, sym->attr.external, NULL);
18601 : }
18602 : else
18603 : {
18604 : /* Result may be in another namespace. */
18605 6010 : resolve_symbol (sym->result);
18606 :
18607 6010 : if (!sym->result->attr.proc_pointer)
18608 : {
18609 5831 : sym->ts = sym->result->ts;
18610 5831 : sym->as = gfc_copy_array_spec (sym->result->as);
18611 5831 : sym->attr.dimension = sym->result->attr.dimension;
18612 5831 : sym->attr.codimension = sym->result->attr.codimension;
18613 5831 : sym->attr.pointer = sym->result->attr.pointer;
18614 5831 : sym->attr.allocatable = sym->result->attr.allocatable;
18615 5831 : sym->attr.contiguous = sym->result->attr.contiguous;
18616 : }
18617 : }
18618 : }
18619 : }
18620 1289412 : else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
18621 31318 : resolve_symbol_array_spec (sym->result, false);
18622 :
18623 : /* For a CLASS-valued function with a result variable, affirm that it has
18624 : been resolved also when looking at the symbol 'sym'. */
18625 436794 : if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
18626 720 : sym->attr.class_ok = sym->result->attr.class_ok;
18627 :
18628 1694888 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
18629 19450 : && CLASS_DATA (sym))
18630 : {
18631 19449 : as = CLASS_DATA (sym)->as;
18632 19449 : class_attr = CLASS_DATA (sym)->attr;
18633 19449 : class_attr.pointer = class_attr.class_pointer;
18634 : }
18635 : else
18636 : {
18637 1675439 : class_attr = sym->attr;
18638 1675439 : as = sym->as;
18639 : }
18640 :
18641 : /* F2008, C530. */
18642 1694888 : if (sym->attr.contiguous
18643 7730 : && !sym->attr.associate_var
18644 7729 : && (!class_attr.dimension
18645 7726 : || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
18646 140 : && !class_attr.pointer)))
18647 : {
18648 7 : gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
18649 : "array pointer or an assumed-shape or assumed-rank array",
18650 : sym->name, &sym->declared_at);
18651 7 : return;
18652 : }
18653 :
18654 : /* Assumed size arrays and assumed shape arrays must be dummy
18655 : arguments. Array-spec's of implied-shape should have been resolved to
18656 : AS_EXPLICIT already. */
18657 :
18658 1687295 : if (as)
18659 : {
18660 : /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
18661 : specification expression. */
18662 145887 : if (as->type == AS_IMPLIED_SHAPE)
18663 : {
18664 : int i;
18665 1 : for (i=0; i<as->rank; i++)
18666 : {
18667 1 : if (as->lower[i] != NULL && as->upper[i] == NULL)
18668 : {
18669 1 : gfc_error ("Bad specification for assumed size array at %L",
18670 : &as->lower[i]->where);
18671 1 : return;
18672 : }
18673 : }
18674 0 : gcc_unreachable();
18675 : }
18676 :
18677 145886 : if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
18678 112815 : || as->type == AS_ASSUMED_SHAPE)
18679 44726 : && !sym->attr.dummy && !sym->attr.select_type_temporary
18680 8 : && !sym->attr.associate_var)
18681 : {
18682 7 : if (as->type == AS_ASSUMED_SIZE)
18683 7 : gfc_error ("Assumed size array at %L must be a dummy argument",
18684 : &sym->declared_at);
18685 : else
18686 0 : gfc_error ("Assumed shape array at %L must be a dummy argument",
18687 : &sym->declared_at);
18688 7 : return;
18689 : }
18690 : /* TS 29113, C535a. */
18691 145879 : if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
18692 60 : && !sym->attr.select_type_temporary
18693 60 : && !(cs_base && cs_base->current
18694 45 : && (cs_base->current->op == EXEC_SELECT_RANK
18695 3 : || ((gfc_option.allow_std & GFC_STD_F202Y)
18696 0 : && cs_base->current->op == EXEC_BLOCK))))
18697 : {
18698 18 : gfc_error ("Assumed-rank array at %L must be a dummy argument",
18699 : &sym->declared_at);
18700 18 : return;
18701 : }
18702 145861 : if (as->type == AS_ASSUMED_RANK
18703 26257 : && (sym->attr.codimension || sym->attr.value))
18704 : {
18705 2 : gfc_error ("Assumed-rank array at %L may not have the VALUE or "
18706 : "CODIMENSION attribute", &sym->declared_at);
18707 2 : return;
18708 : }
18709 : }
18710 :
18711 : /* Make sure symbols with known intent or optional are really dummy
18712 : variable. Because of ENTRY statement, this has to be deferred
18713 : until resolution time. */
18714 :
18715 1694853 : if (!sym->attr.dummy
18716 1224577 : && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
18717 : {
18718 2 : gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
18719 2 : return;
18720 : }
18721 :
18722 1694851 : if (sym->attr.value && !sym->attr.dummy)
18723 : {
18724 2 : gfc_error ("%qs at %L cannot have the VALUE attribute because "
18725 : "it is not a dummy argument", sym->name, &sym->declared_at);
18726 2 : return;
18727 : }
18728 :
18729 1694849 : if (sym->attr.value && sym->ts.type == BT_CHARACTER)
18730 : {
18731 616 : gfc_charlen *cl = sym->ts.u.cl;
18732 616 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
18733 : {
18734 2 : gfc_error ("Character dummy variable %qs at %L with VALUE "
18735 : "attribute must have constant length",
18736 : sym->name, &sym->declared_at);
18737 2 : return;
18738 : }
18739 :
18740 614 : if (sym->ts.is_c_interop
18741 381 : && mpz_cmp_si (cl->length->value.integer, 1) != 0)
18742 : {
18743 1 : gfc_error ("C interoperable character dummy variable %qs at %L "
18744 : "with VALUE attribute must have length one",
18745 : sym->name, &sym->declared_at);
18746 1 : return;
18747 : }
18748 : }
18749 :
18750 1694846 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
18751 123535 : && sym->ts.u.derived->attr.generic)
18752 : {
18753 20 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
18754 20 : if (!sym->ts.u.derived)
18755 : {
18756 0 : gfc_error ("The derived type %qs at %L is of type %qs, "
18757 : "which has not been defined", sym->name,
18758 : &sym->declared_at, sym->ts.u.derived->name);
18759 0 : sym->ts.type = BT_UNKNOWN;
18760 0 : return;
18761 : }
18762 : }
18763 :
18764 : /* Use the same constraints as TYPE(*), except for the type check
18765 : and that only scalars and assumed-size arrays are permitted. */
18766 1694846 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
18767 : {
18768 12960 : if (!sym->attr.dummy)
18769 : {
18770 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
18771 : "a dummy argument", sym->name, &sym->declared_at);
18772 1 : return;
18773 : }
18774 :
18775 12959 : if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
18776 8 : && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
18777 0 : && sym->ts.type != BT_COMPLEX)
18778 : {
18779 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
18780 : "of type TYPE(*) or of an numeric intrinsic type",
18781 : sym->name, &sym->declared_at);
18782 0 : return;
18783 : }
18784 :
18785 12959 : if (sym->attr.allocatable || sym->attr.codimension
18786 12957 : || sym->attr.pointer || sym->attr.value)
18787 : {
18788 4 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
18789 : "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
18790 : "attribute", sym->name, &sym->declared_at);
18791 4 : return;
18792 : }
18793 :
18794 12955 : if (sym->attr.intent == INTENT_OUT)
18795 : {
18796 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
18797 : "have the INTENT(OUT) attribute",
18798 : sym->name, &sym->declared_at);
18799 0 : return;
18800 : }
18801 12955 : if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
18802 : {
18803 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
18804 : "either be a scalar or an assumed-size array",
18805 : sym->name, &sym->declared_at);
18806 1 : return;
18807 : }
18808 :
18809 : /* Set the type to TYPE(*) and add a dimension(*) to ensure
18810 : NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
18811 : packing. */
18812 12954 : sym->ts.type = BT_ASSUMED;
18813 12954 : sym->as = gfc_get_array_spec ();
18814 12954 : sym->as->type = AS_ASSUMED_SIZE;
18815 12954 : sym->as->rank = 1;
18816 12954 : sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
18817 : }
18818 1681886 : else if (sym->ts.type == BT_ASSUMED)
18819 : {
18820 : /* TS 29113, C407a. */
18821 11006 : if (!sym->attr.dummy)
18822 : {
18823 7 : gfc_error ("Assumed type of variable %s at %L is only permitted "
18824 : "for dummy variables", sym->name, &sym->declared_at);
18825 7 : return;
18826 : }
18827 10999 : if (sym->attr.allocatable || sym->attr.codimension
18828 10995 : || sym->attr.pointer || sym->attr.value)
18829 : {
18830 8 : gfc_error ("Assumed-type variable %s at %L may not have the "
18831 : "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
18832 : sym->name, &sym->declared_at);
18833 8 : return;
18834 : }
18835 10991 : if (sym->attr.intent == INTENT_OUT)
18836 : {
18837 2 : gfc_error ("Assumed-type variable %s at %L may not have the "
18838 : "INTENT(OUT) attribute",
18839 : sym->name, &sym->declared_at);
18840 2 : return;
18841 : }
18842 10989 : if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
18843 : {
18844 3 : gfc_error ("Assumed-type variable %s at %L shall not be an "
18845 : "explicit-shape array", sym->name, &sym->declared_at);
18846 3 : return;
18847 : }
18848 : }
18849 :
18850 : /* If the symbol is marked as bind(c), that it is declared at module level
18851 : scope and verify its type and kind. Do not do the latter for symbols
18852 : that are implicitly typed because that is handled in
18853 : gfc_set_default_type. Handle dummy arguments and procedure definitions
18854 : separately. Also, anything that is use associated is not handled here
18855 : but instead is handled in the module it is declared in. Finally, derived
18856 : type definitions are allowed to be BIND(C) since that only implies that
18857 : they're interoperable, and they are checked fully for interoperability
18858 : when a variable is declared of that type. */
18859 1694820 : if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
18860 7327 : && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
18861 567 : && sym->attr.flavor != FL_DERIVED)
18862 : {
18863 167 : bool t = true;
18864 :
18865 : /* First, make sure the variable is declared at the
18866 : module-level scope (J3/04-007, Section 15.3). */
18867 167 : if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
18868 7 : && !sym->attr.in_common)
18869 : {
18870 6 : gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
18871 : "is neither a COMMON block nor declared at the "
18872 : "module level scope", sym->name, &(sym->declared_at));
18873 6 : t = false;
18874 : }
18875 161 : else if (sym->ts.type == BT_CHARACTER
18876 161 : && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
18877 1 : || !gfc_is_constant_expr (sym->ts.u.cl->length)
18878 1 : || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
18879 : {
18880 1 : gfc_error ("BIND(C) Variable %qs at %L must have length one",
18881 1 : sym->name, &sym->declared_at);
18882 1 : t = false;
18883 : }
18884 160 : else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
18885 : {
18886 1 : t = verify_com_block_vars_c_interop (sym->common_head);
18887 : }
18888 159 : else if (sym->attr.implicit_type == 0)
18889 : {
18890 : /* If type() declaration, we need to verify that the components
18891 : of the given type are all C interoperable, etc. */
18892 157 : if (sym->ts.type == BT_DERIVED &&
18893 24 : sym->ts.u.derived->attr.is_c_interop != 1)
18894 : {
18895 : /* Make sure the user marked the derived type as BIND(C). If
18896 : not, call the verify routine. This could print an error
18897 : for the derived type more than once if multiple variables
18898 : of that type are declared. */
18899 14 : if (sym->ts.u.derived->attr.is_bind_c != 1)
18900 1 : verify_bind_c_derived_type (sym->ts.u.derived);
18901 157 : t = false;
18902 : }
18903 :
18904 : /* Verify the variable itself as C interoperable if it
18905 : is BIND(C). It is not possible for this to succeed if
18906 : the verify_bind_c_derived_type failed, so don't have to handle
18907 : any error returned by verify_bind_c_derived_type. */
18908 157 : t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
18909 157 : sym->common_block);
18910 : }
18911 :
18912 165 : if (!t)
18913 : {
18914 : /* clear the is_bind_c flag to prevent reporting errors more than
18915 : once if something failed. */
18916 10 : sym->attr.is_bind_c = 0;
18917 10 : return;
18918 : }
18919 : }
18920 :
18921 : /* If a derived type symbol has reached this point, without its
18922 : type being declared, we have an error. Notice that most
18923 : conditions that produce undefined derived types have already
18924 : been dealt with. However, the likes of:
18925 : implicit type(t) (t) ..... call foo (t) will get us here if
18926 : the type is not declared in the scope of the implicit
18927 : statement. Change the type to BT_UNKNOWN, both because it is so
18928 : and to prevent an ICE. */
18929 1694810 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
18930 123533 : && sym->ts.u.derived->components == NULL
18931 1138 : && !sym->ts.u.derived->attr.zero_comp)
18932 : {
18933 3 : gfc_error ("The derived type %qs at %L is of type %qs, "
18934 : "which has not been defined", sym->name,
18935 : &sym->declared_at, sym->ts.u.derived->name);
18936 3 : sym->ts.type = BT_UNKNOWN;
18937 3 : return;
18938 : }
18939 :
18940 : /* Make sure that the derived type has been resolved and that the
18941 : derived type is visible in the symbol's namespace, if it is a
18942 : module function and is not PRIVATE. */
18943 1694807 : if (sym->ts.type == BT_DERIVED
18944 130554 : && sym->ts.u.derived->attr.use_assoc
18945 113088 : && sym->ns->proc_name
18946 113080 : && sym->ns->proc_name->attr.flavor == FL_MODULE
18947 1700732 : && !resolve_fl_derived (sym->ts.u.derived))
18948 : return;
18949 :
18950 : /* Unless the derived-type declaration is use associated, Fortran 95
18951 : does not allow public entries of private derived types.
18952 : See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
18953 : 161 in 95-006r3. */
18954 1694807 : if (sym->ts.type == BT_DERIVED
18955 130554 : && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
18956 8008 : && !sym->ts.u.derived->attr.use_assoc
18957 2083 : && gfc_check_symbol_access (sym)
18958 1870 : && !gfc_check_symbol_access (sym->ts.u.derived)
18959 1694821 : && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
18960 : "derived type %qs",
18961 14 : (sym->attr.flavor == FL_PARAMETER)
18962 : ? "parameter" : "variable",
18963 : sym->name, &sym->declared_at,
18964 14 : sym->ts.u.derived->name))
18965 : return;
18966 :
18967 : /* F2008, C1302. */
18968 1694800 : if (sym->ts.type == BT_DERIVED
18969 130547 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18970 154 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
18971 130516 : || sym->ts.u.derived->attr.lock_comp)
18972 44 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18973 : {
18974 4 : gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
18975 : "type LOCK_TYPE must be a coarray", sym->name,
18976 : &sym->declared_at);
18977 4 : return;
18978 : }
18979 :
18980 : /* TS18508, C702/C703. */
18981 1694796 : if (sym->ts.type == BT_DERIVED
18982 130543 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
18983 153 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
18984 130526 : || sym->ts.u.derived->attr.event_comp)
18985 17 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
18986 : {
18987 1 : gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
18988 : "type EVENT_TYPE must be a coarray", sym->name,
18989 : &sym->declared_at);
18990 1 : return;
18991 : }
18992 :
18993 : /* An assumed-size array with INTENT(OUT) shall not be of a type for which
18994 : default initialization is defined (5.1.2.4.4). */
18995 1694795 : if (sym->ts.type == BT_DERIVED
18996 130542 : && sym->attr.dummy
18997 44985 : && sym->attr.intent == INTENT_OUT
18998 2356 : && sym->as
18999 381 : && sym->as->type == AS_ASSUMED_SIZE)
19000 : {
19001 1 : for (c = sym->ts.u.derived->components; c; c = c->next)
19002 : {
19003 1 : if (c->initializer)
19004 : {
19005 1 : gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
19006 : "ASSUMED SIZE and so cannot have a default initializer",
19007 : sym->name, &sym->declared_at);
19008 1 : return;
19009 : }
19010 : }
19011 : }
19012 :
19013 : /* F2008, C542. */
19014 1694794 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
19015 44984 : && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
19016 : {
19017 0 : gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
19018 : "INTENT(OUT)", sym->name, &sym->declared_at);
19019 0 : return;
19020 : }
19021 :
19022 : /* TS18508. */
19023 1694794 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
19024 44984 : && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
19025 : {
19026 0 : gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
19027 : "INTENT(OUT)", sym->name, &sym->declared_at);
19028 0 : return;
19029 : }
19030 :
19031 : /* F2008, C525. */
19032 1694794 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
19033 1694694 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
19034 19453 : && sym->ts.u.derived && CLASS_DATA (sym)
19035 19447 : && CLASS_DATA (sym)->attr.coarray_comp))
19036 1694694 : || class_attr.codimension)
19037 1796 : && (sym->attr.result || sym->result == sym))
19038 : {
19039 8 : gfc_error ("Function result %qs at %L shall not be a coarray or have "
19040 : "a coarray component", sym->name, &sym->declared_at);
19041 8 : return;
19042 : }
19043 :
19044 : /* F2008, C524. */
19045 1694786 : if (sym->attr.codimension && sym->ts.type == BT_DERIVED
19046 420 : && sym->ts.u.derived->ts.is_iso_c)
19047 : {
19048 3 : gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
19049 : "shall not be a coarray", sym->name, &sym->declared_at);
19050 3 : return;
19051 : }
19052 :
19053 : /* F2008, C525. */
19054 1694783 : if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
19055 1694686 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
19056 19452 : && sym->ts.u.derived && CLASS_DATA (sym)
19057 19446 : && CLASS_DATA (sym)->attr.coarray_comp))
19058 97 : && (class_attr.codimension || class_attr.pointer || class_attr.dimension
19059 93 : || class_attr.allocatable))
19060 : {
19061 4 : gfc_error ("Variable %qs at %L with coarray component shall be a "
19062 : "nonpointer, nonallocatable scalar, which is not a coarray",
19063 : sym->name, &sym->declared_at);
19064 4 : return;
19065 : }
19066 :
19067 : /* F2008, C526. The function-result case was handled above. */
19068 1694779 : if (class_attr.codimension
19069 1688 : && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
19070 350 : || sym->attr.select_type_temporary
19071 274 : || sym->attr.associate_var
19072 256 : || (sym->ns->save_all && !sym->attr.automatic)
19073 256 : || sym->ns->proc_name->attr.flavor == FL_MODULE
19074 256 : || sym->ns->proc_name->attr.is_main_program
19075 5 : || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
19076 : {
19077 4 : gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
19078 : "nor a dummy argument", sym->name, &sym->declared_at);
19079 4 : return;
19080 : }
19081 : /* F2008, C528. */
19082 1694775 : else if (class_attr.codimension && !sym->attr.select_type_temporary
19083 1608 : && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
19084 : {
19085 6 : gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
19086 : "deferred shape without allocatable", sym->name,
19087 : &sym->declared_at);
19088 6 : return;
19089 : }
19090 1694769 : else if (class_attr.codimension && class_attr.allocatable && as
19091 614 : && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
19092 : {
19093 9 : gfc_error ("Allocatable coarray variable %qs at %L must have "
19094 : "deferred shape", sym->name, &sym->declared_at);
19095 9 : return;
19096 : }
19097 :
19098 : /* F2008, C541. */
19099 1694760 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
19100 1694667 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
19101 19447 : && sym->ts.u.derived && CLASS_DATA (sym)
19102 19441 : && CLASS_DATA (sym)->attr.coarray_comp))
19103 1694667 : || (class_attr.codimension && class_attr.allocatable))
19104 698 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
19105 : {
19106 3 : gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
19107 : "allocatable coarray or have coarray components",
19108 : sym->name, &sym->declared_at);
19109 3 : return;
19110 : }
19111 :
19112 1694757 : if (class_attr.codimension && sym->attr.dummy
19113 469 : && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
19114 : {
19115 2 : gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
19116 : "procedure %qs", sym->name, &sym->declared_at,
19117 : sym->ns->proc_name->name);
19118 2 : return;
19119 : }
19120 :
19121 1694755 : if (sym->ts.type == BT_LOGICAL
19122 112249 : && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
19123 112246 : || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
19124 30997 : && sym->ns->proc_name->attr.is_bind_c)))
19125 : {
19126 : int i;
19127 200 : for (i = 0; gfc_logical_kinds[i].kind; i++)
19128 200 : if (gfc_logical_kinds[i].kind == sym->ts.kind)
19129 : break;
19130 16 : if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
19131 181 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
19132 : "%L with non-C_Bool kind in BIND(C) procedure "
19133 : "%qs", sym->name, &sym->declared_at,
19134 13 : sym->ns->proc_name->name))
19135 : return;
19136 167 : else if (!gfc_logical_kinds[i].c_bool
19137 182 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
19138 : "%qs at %L with non-C_Bool kind in "
19139 : "BIND(C) procedure %qs", sym->name,
19140 : &sym->declared_at,
19141 15 : sym->attr.function ? sym->name
19142 13 : : sym->ns->proc_name->name))
19143 : return;
19144 : }
19145 :
19146 1694752 : switch (sym->attr.flavor)
19147 : {
19148 652430 : case FL_VARIABLE:
19149 652430 : if (!resolve_fl_variable (sym, mp_flag))
19150 : return;
19151 : break;
19152 :
19153 481235 : case FL_PROCEDURE:
19154 481235 : if (sym->formal && !sym->formal_ns)
19155 : {
19156 : /* Check that none of the arguments are a namelist. */
19157 : gfc_formal_arglist *formal = sym->formal;
19158 :
19159 105489 : for (; formal; formal = formal->next)
19160 71602 : if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
19161 : {
19162 1 : gfc_error ("Namelist %qs cannot be an argument to "
19163 : "subroutine or function at %L",
19164 : formal->sym->name, &sym->declared_at);
19165 1 : return;
19166 : }
19167 : }
19168 :
19169 481234 : if (!resolve_fl_procedure (sym, mp_flag))
19170 : return;
19171 : break;
19172 :
19173 855 : case FL_NAMELIST:
19174 855 : if (!resolve_fl_namelist (sym))
19175 : return;
19176 : break;
19177 :
19178 404188 : case FL_PARAMETER:
19179 404188 : if (!resolve_fl_parameter (sym))
19180 : return;
19181 : break;
19182 :
19183 : default:
19184 : break;
19185 : }
19186 :
19187 : /* Resolve array specifier. Check as well some constraints
19188 : on COMMON blocks. */
19189 :
19190 1694555 : check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
19191 :
19192 1694555 : resolve_symbol_array_spec (sym, check_constant);
19193 :
19194 : /* Resolve formal namespaces. */
19195 1694555 : if (sym->formal_ns && sym->formal_ns != gfc_current_ns
19196 265399 : && !sym->attr.contained && !sym->attr.intrinsic)
19197 236484 : gfc_resolve (sym->formal_ns);
19198 :
19199 : /* Make sure the formal namespace is present. */
19200 1694555 : if (sym->formal && !sym->formal_ns)
19201 : {
19202 : gfc_formal_arglist *formal = sym->formal;
19203 34331 : while (formal && !formal->sym)
19204 11 : formal = formal->next;
19205 :
19206 34320 : if (formal)
19207 : {
19208 34309 : sym->formal_ns = formal->sym->ns;
19209 34309 : if (sym->formal_ns && sym->ns != formal->sym->ns)
19210 25997 : sym->formal_ns->refs++;
19211 : }
19212 : }
19213 :
19214 : /* Check threadprivate restrictions. */
19215 1694555 : if ((sym->attr.threadprivate || sym->attr.omp_groupprivate)
19216 384 : && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
19217 33 : && !(sym->ns->save_all && !sym->attr.automatic)
19218 32 : && sym->module == NULL
19219 17 : && (sym->ns->proc_name == NULL
19220 17 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
19221 4 : && !sym->ns->proc_name->attr.is_main_program)))
19222 : {
19223 2 : if (sym->attr.threadprivate)
19224 1 : gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
19225 : else
19226 1 : gfc_error ("OpenMP groupprivate variable %qs at %L must have the SAVE "
19227 : "attribute", sym->name, &sym->declared_at);
19228 : }
19229 :
19230 1694555 : if (sym->attr.omp_groupprivate && sym->value)
19231 2 : gfc_error ("!$OMP GROUPPRIVATE variable %qs at %L must not have an "
19232 : "initializer", sym->name, &sym->declared_at);
19233 :
19234 : /* Check omp declare target restrictions. */
19235 1694555 : if ((sym->attr.omp_declare_target
19236 1693139 : || sym->attr.omp_declare_target_link
19237 1693091 : || sym->attr.omp_declare_target_local)
19238 1504 : && !sym->attr.omp_groupprivate /* already warned. */
19239 1457 : && sym->attr.flavor == FL_VARIABLE
19240 616 : && !sym->attr.save
19241 199 : && !(sym->ns->save_all && !sym->attr.automatic)
19242 199 : && (!sym->attr.in_common
19243 186 : && sym->module == NULL
19244 96 : && (sym->ns->proc_name == NULL
19245 96 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
19246 6 : && !sym->ns->proc_name->attr.is_main_program))))
19247 4 : gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
19248 : sym->name, &sym->declared_at);
19249 :
19250 : /* If we have come this far we can apply default-initializers, as
19251 : described in 14.7.5, to those variables that have not already
19252 : been assigned one. */
19253 1694555 : if (sym->ts.type == BT_DERIVED
19254 130512 : && !sym->value
19255 105559 : && !sym->attr.allocatable
19256 102585 : && !sym->attr.alloc_comp)
19257 : {
19258 102527 : symbol_attribute *a = &sym->attr;
19259 :
19260 102527 : if ((!a->save && !a->dummy && !a->pointer
19261 56216 : && !a->in_common && !a->use_assoc
19262 10360 : && a->referenced
19263 8117 : && !((a->function || a->result)
19264 1608 : && (!a->dimension
19265 160 : || sym->ts.u.derived->attr.alloc_comp
19266 95 : || sym->ts.u.derived->attr.pointer_comp))
19267 6590 : && !(a->function && sym != sym->result))
19268 95957 : || (a->dummy && !a->pointer && a->intent == INTENT_OUT
19269 1528 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
19270 7999 : apply_default_init (sym);
19271 94528 : else if (a->function && !a->pointer && !a->allocatable
19272 20556 : && !a->use_assoc && !a->used_in_submodule && sym->result)
19273 : /* Default initialization for function results. */
19274 2673 : apply_default_init (sym->result);
19275 91855 : else if (a->function && sym->result && a->access != ACCESS_PRIVATE
19276 11759 : && (sym->ts.u.derived->attr.alloc_comp
19277 11236 : || sym->ts.u.derived->attr.pointer_comp))
19278 : /* Mark the result symbol to be referenced, when it has allocatable
19279 : components. */
19280 582 : sym->result->attr.referenced = 1;
19281 : }
19282 :
19283 1694555 : if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
19284 18948 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT
19285 1226 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
19286 1151 : && !CLASS_DATA (sym)->attr.class_pointer
19287 1125 : && !CLASS_DATA (sym)->attr.allocatable)
19288 853 : apply_default_init (sym);
19289 :
19290 : /* If this symbol has a type-spec, check it. */
19291 1694555 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
19292 638047 : || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
19293 1377277 : if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
19294 : return;
19295 :
19296 1694552 : if (sym->param_list)
19297 1377 : resolve_pdt (sym);
19298 : }
19299 :
19300 :
19301 3979 : void gfc_resolve_symbol (gfc_symbol *sym)
19302 : {
19303 3979 : resolve_symbol (sym);
19304 3979 : return;
19305 : }
19306 :
19307 :
19308 : /************* Resolve DATA statements *************/
19309 :
19310 : static struct
19311 : {
19312 : gfc_data_value *vnode;
19313 : mpz_t left;
19314 : }
19315 : values;
19316 :
19317 :
19318 : /* Advance the values structure to point to the next value in the data list. */
19319 :
19320 : static bool
19321 10892 : next_data_value (void)
19322 : {
19323 16660 : while (mpz_cmp_ui (values.left, 0) == 0)
19324 : {
19325 :
19326 8198 : if (values.vnode->next == NULL)
19327 : return false;
19328 :
19329 5768 : values.vnode = values.vnode->next;
19330 5768 : mpz_set (values.left, values.vnode->repeat);
19331 : }
19332 :
19333 : return true;
19334 : }
19335 :
19336 :
19337 : static bool
19338 3557 : check_data_variable (gfc_data_variable *var, locus *where)
19339 : {
19340 3557 : gfc_expr *e;
19341 3557 : mpz_t size;
19342 3557 : mpz_t offset;
19343 3557 : bool t;
19344 3557 : ar_type mark = AR_UNKNOWN;
19345 3557 : int i;
19346 3557 : mpz_t section_index[GFC_MAX_DIMENSIONS];
19347 3557 : int vector_offset[GFC_MAX_DIMENSIONS];
19348 3557 : gfc_ref *ref;
19349 3557 : gfc_array_ref *ar;
19350 3557 : gfc_symbol *sym;
19351 3557 : int has_pointer;
19352 :
19353 3557 : if (!gfc_resolve_expr (var->expr))
19354 : return false;
19355 :
19356 3557 : ar = NULL;
19357 3557 : e = var->expr;
19358 :
19359 3557 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
19360 0 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
19361 0 : e = e->value.function.actual->expr;
19362 :
19363 3557 : if (e->expr_type != EXPR_VARIABLE)
19364 : {
19365 0 : gfc_error ("Expecting definable entity near %L", where);
19366 0 : return false;
19367 : }
19368 :
19369 3557 : sym = e->symtree->n.sym;
19370 :
19371 3557 : if (sym->ns->is_block_data && !sym->attr.in_common)
19372 : {
19373 2 : gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
19374 : sym->name, &sym->declared_at);
19375 2 : return false;
19376 : }
19377 :
19378 3555 : if (e->ref == NULL && sym->as)
19379 : {
19380 1 : gfc_error ("DATA array %qs at %L must be specified in a previous"
19381 : " declaration", sym->name, where);
19382 1 : return false;
19383 : }
19384 :
19385 3554 : if (gfc_is_coindexed (e))
19386 : {
19387 7 : gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
19388 : where);
19389 7 : return false;
19390 : }
19391 :
19392 3547 : has_pointer = sym->attr.pointer;
19393 :
19394 5988 : for (ref = e->ref; ref; ref = ref->next)
19395 : {
19396 2445 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
19397 : has_pointer = 1;
19398 :
19399 2419 : if (has_pointer)
19400 : {
19401 29 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
19402 : {
19403 1 : gfc_error ("DATA element %qs at %L is a pointer and so must "
19404 : "be a full array", sym->name, where);
19405 1 : return false;
19406 : }
19407 :
19408 28 : if (values.vnode->expr->expr_type == EXPR_CONSTANT)
19409 : {
19410 1 : gfc_error ("DATA object near %L has the pointer attribute "
19411 : "and the corresponding DATA value is not a valid "
19412 : "initial-data-target", where);
19413 1 : return false;
19414 : }
19415 : }
19416 :
19417 2443 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
19418 : {
19419 1 : gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
19420 : "attribute", ref->u.c.component->name, &e->where);
19421 1 : return false;
19422 : }
19423 :
19424 : /* Reject substrings of strings of non-constant length. */
19425 2442 : if (ref->type == REF_SUBSTRING
19426 73 : && ref->u.ss.length
19427 73 : && ref->u.ss.length->length
19428 2515 : && !gfc_is_constant_expr (ref->u.ss.length->length))
19429 1 : goto bad_charlen;
19430 : }
19431 :
19432 : /* Reject strings with deferred length or non-constant length. */
19433 3543 : if (e->ts.type == BT_CHARACTER
19434 3543 : && (e->ts.deferred
19435 374 : || (e->ts.u.cl->length
19436 323 : && !gfc_is_constant_expr (e->ts.u.cl->length))))
19437 5 : goto bad_charlen;
19438 :
19439 3538 : mpz_init_set_si (offset, 0);
19440 :
19441 3538 : if (e->rank == 0 || has_pointer)
19442 : {
19443 2691 : mpz_init_set_ui (size, 1);
19444 2691 : ref = NULL;
19445 : }
19446 : else
19447 : {
19448 847 : ref = e->ref;
19449 :
19450 : /* Find the array section reference. */
19451 1030 : for (ref = e->ref; ref; ref = ref->next)
19452 : {
19453 1030 : if (ref->type != REF_ARRAY)
19454 92 : continue;
19455 938 : if (ref->u.ar.type == AR_ELEMENT)
19456 91 : continue;
19457 : break;
19458 : }
19459 847 : gcc_assert (ref);
19460 :
19461 : /* Set marks according to the reference pattern. */
19462 847 : switch (ref->u.ar.type)
19463 : {
19464 : case AR_FULL:
19465 : mark = AR_FULL;
19466 : break;
19467 :
19468 151 : case AR_SECTION:
19469 151 : ar = &ref->u.ar;
19470 : /* Get the start position of array section. */
19471 151 : gfc_get_section_index (ar, section_index, &offset, vector_offset);
19472 151 : mark = AR_SECTION;
19473 151 : break;
19474 :
19475 0 : default:
19476 0 : gcc_unreachable ();
19477 : }
19478 :
19479 847 : if (!gfc_array_size (e, &size))
19480 : {
19481 1 : gfc_error ("Nonconstant array section at %L in DATA statement",
19482 : where);
19483 1 : mpz_clear (offset);
19484 1 : return false;
19485 : }
19486 : }
19487 :
19488 3537 : t = true;
19489 :
19490 11937 : while (mpz_cmp_ui (size, 0) > 0)
19491 : {
19492 8463 : if (!next_data_value ())
19493 : {
19494 1 : gfc_error ("DATA statement at %L has more variables than values",
19495 : where);
19496 1 : t = false;
19497 1 : break;
19498 : }
19499 :
19500 8462 : t = gfc_check_assign (var->expr, values.vnode->expr, 0);
19501 8462 : if (!t)
19502 : break;
19503 :
19504 : /* If we have more than one element left in the repeat count,
19505 : and we have more than one element left in the target variable,
19506 : then create a range assignment. */
19507 : /* FIXME: Only done for full arrays for now, since array sections
19508 : seem tricky. */
19509 8443 : if (mark == AR_FULL && ref && ref->next == NULL
19510 5364 : && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
19511 : {
19512 137 : mpz_t range;
19513 :
19514 137 : if (mpz_cmp (size, values.left) >= 0)
19515 : {
19516 126 : mpz_init_set (range, values.left);
19517 126 : mpz_sub (size, size, values.left);
19518 126 : mpz_set_ui (values.left, 0);
19519 : }
19520 : else
19521 : {
19522 11 : mpz_init_set (range, size);
19523 11 : mpz_sub (values.left, values.left, size);
19524 11 : mpz_set_ui (size, 0);
19525 : }
19526 :
19527 137 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
19528 : offset, &range);
19529 :
19530 137 : mpz_add (offset, offset, range);
19531 137 : mpz_clear (range);
19532 :
19533 137 : if (!t)
19534 : break;
19535 129 : }
19536 :
19537 : /* Assign initial value to symbol. */
19538 : else
19539 : {
19540 8306 : mpz_sub_ui (values.left, values.left, 1);
19541 8306 : mpz_sub_ui (size, size, 1);
19542 :
19543 8306 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
19544 : offset, NULL);
19545 8306 : if (!t)
19546 : break;
19547 :
19548 8271 : if (mark == AR_FULL)
19549 5259 : mpz_add_ui (offset, offset, 1);
19550 :
19551 : /* Modify the array section indexes and recalculate the offset
19552 : for next element. */
19553 3012 : else if (mark == AR_SECTION)
19554 366 : gfc_advance_section (section_index, ar, &offset, vector_offset);
19555 : }
19556 : }
19557 :
19558 3537 : if (mark == AR_SECTION)
19559 : {
19560 344 : for (i = 0; i < ar->dimen; i++)
19561 194 : mpz_clear (section_index[i]);
19562 : }
19563 :
19564 3537 : mpz_clear (size);
19565 3537 : mpz_clear (offset);
19566 :
19567 3537 : return t;
19568 :
19569 6 : bad_charlen:
19570 6 : gfc_error ("Non-constant character length at %L in DATA statement",
19571 : &e->where);
19572 6 : return false;
19573 : }
19574 :
19575 :
19576 : static bool traverse_data_var (gfc_data_variable *, locus *);
19577 :
19578 : /* Iterate over a list of elements in a DATA statement. */
19579 :
19580 : static bool
19581 237 : traverse_data_list (gfc_data_variable *var, locus *where)
19582 : {
19583 237 : mpz_t trip;
19584 237 : iterator_stack frame;
19585 237 : gfc_expr *e, *start, *end, *step;
19586 237 : bool retval = true;
19587 :
19588 237 : mpz_init (frame.value);
19589 237 : mpz_init (trip);
19590 :
19591 237 : start = gfc_copy_expr (var->iter.start);
19592 237 : end = gfc_copy_expr (var->iter.end);
19593 237 : step = gfc_copy_expr (var->iter.step);
19594 :
19595 237 : if (!gfc_simplify_expr (start, 1)
19596 237 : || start->expr_type != EXPR_CONSTANT)
19597 : {
19598 0 : gfc_error ("start of implied-do loop at %L could not be "
19599 : "simplified to a constant value", &start->where);
19600 0 : retval = false;
19601 0 : goto cleanup;
19602 : }
19603 237 : if (!gfc_simplify_expr (end, 1)
19604 237 : || end->expr_type != EXPR_CONSTANT)
19605 : {
19606 0 : gfc_error ("end of implied-do loop at %L could not be "
19607 : "simplified to a constant value", &end->where);
19608 0 : retval = false;
19609 0 : goto cleanup;
19610 : }
19611 237 : if (!gfc_simplify_expr (step, 1)
19612 237 : || step->expr_type != EXPR_CONSTANT)
19613 : {
19614 0 : gfc_error ("step of implied-do loop at %L could not be "
19615 : "simplified to a constant value", &step->where);
19616 0 : retval = false;
19617 0 : goto cleanup;
19618 : }
19619 237 : if (mpz_cmp_si (step->value.integer, 0) == 0)
19620 : {
19621 1 : gfc_error ("step of implied-do loop at %L shall not be zero",
19622 : &step->where);
19623 1 : retval = false;
19624 1 : goto cleanup;
19625 : }
19626 :
19627 236 : mpz_set (trip, end->value.integer);
19628 236 : mpz_sub (trip, trip, start->value.integer);
19629 236 : mpz_add (trip, trip, step->value.integer);
19630 :
19631 236 : mpz_div (trip, trip, step->value.integer);
19632 :
19633 236 : mpz_set (frame.value, start->value.integer);
19634 :
19635 236 : frame.prev = iter_stack;
19636 236 : frame.variable = var->iter.var->symtree;
19637 236 : iter_stack = &frame;
19638 :
19639 1127 : while (mpz_cmp_ui (trip, 0) > 0)
19640 : {
19641 905 : if (!traverse_data_var (var->list, where))
19642 : {
19643 14 : retval = false;
19644 14 : goto cleanup;
19645 : }
19646 :
19647 891 : e = gfc_copy_expr (var->expr);
19648 891 : if (!gfc_simplify_expr (e, 1))
19649 : {
19650 0 : gfc_free_expr (e);
19651 0 : retval = false;
19652 0 : goto cleanup;
19653 : }
19654 :
19655 891 : mpz_add (frame.value, frame.value, step->value.integer);
19656 :
19657 891 : mpz_sub_ui (trip, trip, 1);
19658 : }
19659 :
19660 222 : cleanup:
19661 237 : mpz_clear (frame.value);
19662 237 : mpz_clear (trip);
19663 :
19664 237 : gfc_free_expr (start);
19665 237 : gfc_free_expr (end);
19666 237 : gfc_free_expr (step);
19667 :
19668 237 : iter_stack = frame.prev;
19669 237 : return retval;
19670 : }
19671 :
19672 :
19673 : /* Type resolve variables in the variable list of a DATA statement. */
19674 :
19675 : static bool
19676 3418 : traverse_data_var (gfc_data_variable *var, locus *where)
19677 : {
19678 3418 : bool t;
19679 :
19680 7114 : for (; var; var = var->next)
19681 : {
19682 3794 : if (var->expr == NULL)
19683 237 : t = traverse_data_list (var, where);
19684 : else
19685 3557 : t = check_data_variable (var, where);
19686 :
19687 3794 : if (!t)
19688 : return false;
19689 : }
19690 :
19691 : return true;
19692 : }
19693 :
19694 :
19695 : /* Resolve the expressions and iterators associated with a data statement.
19696 : This is separate from the assignment checking because data lists should
19697 : only be resolved once. */
19698 :
19699 : static bool
19700 2668 : resolve_data_variables (gfc_data_variable *d)
19701 : {
19702 5707 : for (; d; d = d->next)
19703 : {
19704 3044 : if (d->list == NULL)
19705 : {
19706 2891 : if (!gfc_resolve_expr (d->expr))
19707 : return false;
19708 : }
19709 : else
19710 : {
19711 153 : if (!gfc_resolve_iterator (&d->iter, false, true))
19712 : return false;
19713 :
19714 150 : if (!resolve_data_variables (d->list))
19715 : return false;
19716 : }
19717 : }
19718 :
19719 : return true;
19720 : }
19721 :
19722 :
19723 : /* Resolve a single DATA statement. We implement this by storing a pointer to
19724 : the value list into static variables, and then recursively traversing the
19725 : variables list, expanding iterators and such. */
19726 :
19727 : static void
19728 2518 : resolve_data (gfc_data *d)
19729 : {
19730 :
19731 2518 : if (!resolve_data_variables (d->var))
19732 : return;
19733 :
19734 2513 : values.vnode = d->value;
19735 2513 : if (d->value == NULL)
19736 0 : mpz_set_ui (values.left, 0);
19737 : else
19738 2513 : mpz_set (values.left, d->value->repeat);
19739 :
19740 2513 : if (!traverse_data_var (d->var, &d->where))
19741 : return;
19742 :
19743 : /* At this point, we better not have any values left. */
19744 :
19745 2429 : if (next_data_value ())
19746 0 : gfc_error ("DATA statement at %L has more values than variables",
19747 : &d->where);
19748 : }
19749 :
19750 :
19751 : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
19752 : accessed by host or use association, is a dummy argument to a pure function,
19753 : is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
19754 : is storage associated with any such variable, shall not be used in the
19755 : following contexts: (clients of this function). */
19756 :
19757 : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
19758 : procedure. Returns zero if assignment is OK, nonzero if there is a
19759 : problem. */
19760 : bool
19761 55718 : gfc_impure_variable (gfc_symbol *sym)
19762 : {
19763 55718 : gfc_symbol *proc;
19764 55718 : gfc_namespace *ns;
19765 :
19766 55718 : if (sym->attr.use_assoc || sym->attr.in_common)
19767 : return 1;
19768 :
19769 : /* The namespace of a module procedure interface holds the arguments and
19770 : symbols, and so the symbol namespace can be different to that of the
19771 : procedure. */
19772 55101 : if (sym->ns != gfc_current_ns
19773 5869 : && gfc_current_ns->proc_name->abr_modproc_decl
19774 48 : && sym->ns->proc_name->attr.function
19775 12 : && sym->attr.result
19776 12 : && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
19777 : return 0;
19778 :
19779 : /* Check if the symbol's ns is inside the pure procedure. */
19780 59757 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19781 : {
19782 59466 : if (ns == sym->ns)
19783 : break;
19784 6175 : if (ns->proc_name->attr.flavor == FL_PROCEDURE
19785 5113 : && !(sym->attr.function || sym->attr.result))
19786 : return 1;
19787 : }
19788 :
19789 53582 : proc = sym->ns->proc_name;
19790 53582 : if (sym->attr.dummy
19791 5945 : && !sym->attr.value
19792 5823 : && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
19793 5620 : || proc->attr.function))
19794 691 : return 1;
19795 :
19796 : /* TODO: Sort out what can be storage associated, if anything, and include
19797 : it here. In principle equivalences should be scanned but it does not
19798 : seem to be possible to storage associate an impure variable this way. */
19799 : return 0;
19800 : }
19801 :
19802 :
19803 : /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
19804 : current namespace is inside a pure procedure. */
19805 :
19806 : bool
19807 2322424 : gfc_pure (gfc_symbol *sym)
19808 : {
19809 2322424 : symbol_attribute attr;
19810 2322424 : gfc_namespace *ns;
19811 :
19812 2322424 : if (sym == NULL)
19813 : {
19814 : /* Check if the current namespace or one of its parents
19815 : belongs to a pure procedure. */
19816 3182685 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19817 : {
19818 1879987 : sym = ns->proc_name;
19819 1879987 : if (sym == NULL)
19820 : return 0;
19821 1878848 : attr = sym->attr;
19822 1878848 : if (attr.flavor == FL_PROCEDURE && attr.pure)
19823 : return 1;
19824 : }
19825 : return 0;
19826 : }
19827 :
19828 1011364 : attr = sym->attr;
19829 :
19830 1011364 : return attr.flavor == FL_PROCEDURE && attr.pure;
19831 : }
19832 :
19833 :
19834 : /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
19835 : checks if the current namespace is implicitly pure. Note that this
19836 : function returns false for a PURE procedure. */
19837 :
19838 : bool
19839 725046 : gfc_implicit_pure (gfc_symbol *sym)
19840 : {
19841 725046 : gfc_namespace *ns;
19842 :
19843 725046 : if (sym == NULL)
19844 : {
19845 : /* Check if the current procedure is implicit_pure. Walk up
19846 : the procedure list until we find a procedure. */
19847 999138 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19848 : {
19849 713133 : sym = ns->proc_name;
19850 713133 : if (sym == NULL)
19851 : return 0;
19852 :
19853 713060 : if (sym->attr.flavor == FL_PROCEDURE)
19854 : break;
19855 : }
19856 : }
19857 :
19858 438965 : return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
19859 752142 : && !sym->attr.pure;
19860 : }
19861 :
19862 :
19863 : void
19864 424921 : gfc_unset_implicit_pure (gfc_symbol *sym)
19865 : {
19866 424921 : gfc_namespace *ns;
19867 :
19868 424921 : if (sym == NULL)
19869 : {
19870 : /* Check if the current procedure is implicit_pure. Walk up
19871 : the procedure list until we find a procedure. */
19872 694260 : for (ns = gfc_current_ns; ns; ns = ns->parent)
19873 : {
19874 429375 : sym = ns->proc_name;
19875 429375 : if (sym == NULL)
19876 : return;
19877 :
19878 428544 : if (sym->attr.flavor == FL_PROCEDURE)
19879 : break;
19880 : }
19881 : }
19882 :
19883 424090 : if (sym->attr.flavor == FL_PROCEDURE)
19884 150914 : sym->attr.implicit_pure = 0;
19885 : else
19886 273176 : sym->attr.pure = 0;
19887 : }
19888 :
19889 :
19890 : /* Test whether the current procedure is elemental or not. */
19891 :
19892 : bool
19893 1356968 : gfc_elemental (gfc_symbol *sym)
19894 : {
19895 1356968 : symbol_attribute attr;
19896 :
19897 1356968 : if (sym == NULL)
19898 0 : sym = gfc_current_ns->proc_name;
19899 0 : if (sym == NULL)
19900 : return 0;
19901 1356968 : attr = sym->attr;
19902 :
19903 1356968 : return attr.flavor == FL_PROCEDURE && attr.elemental;
19904 : }
19905 :
19906 :
19907 : /* Warn about unused labels. */
19908 :
19909 : static void
19910 4668 : warn_unused_fortran_label (gfc_st_label *label)
19911 : {
19912 4694 : if (label == NULL)
19913 : return;
19914 :
19915 27 : warn_unused_fortran_label (label->left);
19916 :
19917 27 : if (label->defined == ST_LABEL_UNKNOWN)
19918 : return;
19919 :
19920 26 : switch (label->referenced)
19921 : {
19922 2 : case ST_LABEL_UNKNOWN:
19923 2 : gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
19924 : label->value, &label->where);
19925 2 : break;
19926 :
19927 1 : case ST_LABEL_BAD_TARGET:
19928 1 : gfc_warning (OPT_Wunused_label,
19929 : "Label %d at %L defined but cannot be used",
19930 : label->value, &label->where);
19931 1 : break;
19932 :
19933 : default:
19934 : break;
19935 : }
19936 :
19937 26 : warn_unused_fortran_label (label->right);
19938 : }
19939 :
19940 :
19941 : /* Returns the sequence type of a symbol or sequence. */
19942 :
19943 : static seq_type
19944 1076 : sequence_type (gfc_typespec ts)
19945 : {
19946 1076 : seq_type result;
19947 1076 : gfc_component *c;
19948 :
19949 1076 : switch (ts.type)
19950 : {
19951 49 : case BT_DERIVED:
19952 :
19953 49 : if (ts.u.derived->components == NULL)
19954 : return SEQ_NONDEFAULT;
19955 :
19956 49 : result = sequence_type (ts.u.derived->components->ts);
19957 103 : for (c = ts.u.derived->components->next; c; c = c->next)
19958 67 : if (sequence_type (c->ts) != result)
19959 : return SEQ_MIXED;
19960 :
19961 : return result;
19962 :
19963 129 : case BT_CHARACTER:
19964 129 : if (ts.kind != gfc_default_character_kind)
19965 0 : return SEQ_NONDEFAULT;
19966 :
19967 : return SEQ_CHARACTER;
19968 :
19969 240 : case BT_INTEGER:
19970 240 : if (ts.kind != gfc_default_integer_kind)
19971 25 : return SEQ_NONDEFAULT;
19972 :
19973 : return SEQ_NUMERIC;
19974 :
19975 559 : case BT_REAL:
19976 559 : if (!(ts.kind == gfc_default_real_kind
19977 269 : || ts.kind == gfc_default_double_kind))
19978 0 : return SEQ_NONDEFAULT;
19979 :
19980 : return SEQ_NUMERIC;
19981 :
19982 81 : case BT_COMPLEX:
19983 81 : if (ts.kind != gfc_default_complex_kind)
19984 48 : return SEQ_NONDEFAULT;
19985 :
19986 : return SEQ_NUMERIC;
19987 :
19988 17 : case BT_LOGICAL:
19989 17 : if (ts.kind != gfc_default_logical_kind)
19990 0 : return SEQ_NONDEFAULT;
19991 :
19992 : return SEQ_NUMERIC;
19993 :
19994 : default:
19995 : return SEQ_NONDEFAULT;
19996 : }
19997 : }
19998 :
19999 :
20000 : /* Resolve derived type EQUIVALENCE object. */
20001 :
20002 : static bool
20003 80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
20004 : {
20005 80 : gfc_component *c = derived->components;
20006 :
20007 80 : if (!derived)
20008 : return true;
20009 :
20010 : /* Shall not be an object of nonsequence derived type. */
20011 80 : if (!derived->attr.sequence)
20012 : {
20013 0 : gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
20014 : "attribute to be an EQUIVALENCE object", sym->name,
20015 : &e->where);
20016 0 : return false;
20017 : }
20018 :
20019 : /* Shall not have allocatable components. */
20020 80 : if (derived->attr.alloc_comp)
20021 : {
20022 1 : gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
20023 : "components to be an EQUIVALENCE object",sym->name,
20024 : &e->where);
20025 1 : return false;
20026 : }
20027 :
20028 79 : if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
20029 : {
20030 1 : gfc_error ("Derived type variable %qs at %L with default "
20031 : "initialization cannot be in EQUIVALENCE with a variable "
20032 : "in COMMON", sym->name, &e->where);
20033 1 : return false;
20034 : }
20035 :
20036 245 : for (; c ; c = c->next)
20037 : {
20038 167 : if (gfc_bt_struct (c->ts.type)
20039 167 : && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
20040 : return false;
20041 :
20042 : /* Shall not be an object of sequence derived type containing a pointer
20043 : in the structure. */
20044 167 : if (c->attr.pointer)
20045 : {
20046 0 : gfc_error ("Derived type variable %qs at %L with pointer "
20047 : "component(s) cannot be an EQUIVALENCE object",
20048 : sym->name, &e->where);
20049 0 : return false;
20050 : }
20051 : }
20052 : return true;
20053 : }
20054 :
20055 :
20056 : /* Resolve equivalence object.
20057 : An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
20058 : an allocatable array, an object of nonsequence derived type, an object of
20059 : sequence derived type containing a pointer at any level of component
20060 : selection, an automatic object, a function name, an entry name, a result
20061 : name, a named constant, a structure component, or a subobject of any of
20062 : the preceding objects. A substring shall not have length zero. A
20063 : derived type shall not have components with default initialization nor
20064 : shall two objects of an equivalence group be initialized.
20065 : Either all or none of the objects shall have an protected attribute.
20066 : The simple constraints are done in symbol.cc(check_conflict) and the rest
20067 : are implemented here. */
20068 :
20069 : static void
20070 1565 : resolve_equivalence (gfc_equiv *eq)
20071 : {
20072 1565 : gfc_symbol *sym;
20073 1565 : gfc_symbol *first_sym;
20074 1565 : gfc_expr *e;
20075 1565 : gfc_ref *r;
20076 1565 : locus *last_where = NULL;
20077 1565 : seq_type eq_type, last_eq_type;
20078 1565 : gfc_typespec *last_ts;
20079 1565 : int object, cnt_protected;
20080 1565 : const char *msg;
20081 :
20082 1565 : last_ts = &eq->expr->symtree->n.sym->ts;
20083 :
20084 1565 : first_sym = eq->expr->symtree->n.sym;
20085 :
20086 1565 : cnt_protected = 0;
20087 :
20088 4727 : for (object = 1; eq; eq = eq->eq, object++)
20089 : {
20090 3171 : e = eq->expr;
20091 :
20092 3171 : e->ts = e->symtree->n.sym->ts;
20093 : /* match_varspec might not know yet if it is seeing
20094 : array reference or substring reference, as it doesn't
20095 : know the types. */
20096 3171 : if (e->ref && e->ref->type == REF_ARRAY)
20097 : {
20098 2152 : gfc_ref *ref = e->ref;
20099 2152 : sym = e->symtree->n.sym;
20100 :
20101 2152 : if (sym->attr.dimension)
20102 : {
20103 1855 : ref->u.ar.as = sym->as;
20104 1855 : ref = ref->next;
20105 : }
20106 :
20107 : /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
20108 2152 : if (e->ts.type == BT_CHARACTER
20109 592 : && ref
20110 371 : && ref->type == REF_ARRAY
20111 371 : && ref->u.ar.dimen == 1
20112 371 : && ref->u.ar.dimen_type[0] == DIMEN_RANGE
20113 371 : && ref->u.ar.stride[0] == NULL)
20114 : {
20115 370 : gfc_expr *start = ref->u.ar.start[0];
20116 370 : gfc_expr *end = ref->u.ar.end[0];
20117 370 : void *mem = NULL;
20118 :
20119 : /* Optimize away the (:) reference. */
20120 370 : if (start == NULL && end == NULL)
20121 : {
20122 9 : if (e->ref == ref)
20123 0 : e->ref = ref->next;
20124 : else
20125 9 : e->ref->next = ref->next;
20126 : mem = ref;
20127 : }
20128 : else
20129 : {
20130 361 : ref->type = REF_SUBSTRING;
20131 361 : if (start == NULL)
20132 9 : start = gfc_get_int_expr (gfc_charlen_int_kind,
20133 : NULL, 1);
20134 361 : ref->u.ss.start = start;
20135 361 : if (end == NULL && e->ts.u.cl)
20136 27 : end = gfc_copy_expr (e->ts.u.cl->length);
20137 361 : ref->u.ss.end = end;
20138 361 : ref->u.ss.length = e->ts.u.cl;
20139 361 : e->ts.u.cl = NULL;
20140 : }
20141 370 : ref = ref->next;
20142 370 : free (mem);
20143 : }
20144 :
20145 : /* Any further ref is an error. */
20146 1930 : if (ref)
20147 : {
20148 1 : gcc_assert (ref->type == REF_ARRAY);
20149 1 : gfc_error ("Syntax error in EQUIVALENCE statement at %L",
20150 : &ref->u.ar.where);
20151 1 : continue;
20152 : }
20153 : }
20154 :
20155 3170 : if (!gfc_resolve_expr (e))
20156 2 : continue;
20157 :
20158 3168 : sym = e->symtree->n.sym;
20159 :
20160 3168 : if (sym->attr.is_protected)
20161 2 : cnt_protected++;
20162 3168 : if (cnt_protected > 0 && cnt_protected != object)
20163 : {
20164 2 : gfc_error ("Either all or none of the objects in the "
20165 : "EQUIVALENCE set at %L shall have the "
20166 : "PROTECTED attribute",
20167 : &e->where);
20168 2 : break;
20169 : }
20170 :
20171 : /* Shall not equivalence common block variables in a PURE procedure. */
20172 3166 : if (sym->ns->proc_name
20173 3150 : && sym->ns->proc_name->attr.pure
20174 7 : && sym->attr.in_common)
20175 : {
20176 : /* Need to check for symbols that may have entered the pure
20177 : procedure via a USE statement. */
20178 7 : bool saw_sym = false;
20179 7 : if (sym->ns->use_stmts)
20180 : {
20181 6 : gfc_use_rename *r;
20182 10 : for (r = sym->ns->use_stmts->rename; r; r = r->next)
20183 4 : if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
20184 : }
20185 : else
20186 : saw_sym = true;
20187 :
20188 6 : if (saw_sym)
20189 3 : gfc_error ("COMMON block member %qs at %L cannot be an "
20190 : "EQUIVALENCE object in the pure procedure %qs",
20191 : sym->name, &e->where, sym->ns->proc_name->name);
20192 : break;
20193 : }
20194 :
20195 : /* Shall not be a named constant. */
20196 3159 : if (e->expr_type == EXPR_CONSTANT)
20197 : {
20198 0 : gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
20199 : "object", sym->name, &e->where);
20200 0 : continue;
20201 : }
20202 :
20203 3161 : if (e->ts.type == BT_DERIVED
20204 3159 : && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
20205 2 : continue;
20206 :
20207 : /* Check that the types correspond correctly:
20208 : Note 5.28:
20209 : A numeric sequence structure may be equivalenced to another sequence
20210 : structure, an object of default integer type, default real type, double
20211 : precision real type, default logical type such that components of the
20212 : structure ultimately only become associated to objects of the same
20213 : kind. A character sequence structure may be equivalenced to an object
20214 : of default character kind or another character sequence structure.
20215 : Other objects may be equivalenced only to objects of the same type and
20216 : kind parameters. */
20217 :
20218 : /* Identical types are unconditionally OK. */
20219 3157 : if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
20220 2677 : goto identical_types;
20221 :
20222 480 : last_eq_type = sequence_type (*last_ts);
20223 480 : eq_type = sequence_type (sym->ts);
20224 :
20225 : /* Since the pair of objects is not of the same type, mixed or
20226 : non-default sequences can be rejected. */
20227 :
20228 480 : msg = G_("Sequence %s with mixed components in EQUIVALENCE "
20229 : "statement at %L with different type objects");
20230 481 : if ((object ==2
20231 480 : && last_eq_type == SEQ_MIXED
20232 7 : && last_where
20233 7 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
20234 486 : || (eq_type == SEQ_MIXED
20235 6 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
20236 1 : continue;
20237 :
20238 479 : msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
20239 : "statement at %L with objects of different type");
20240 483 : if ((object ==2
20241 479 : && last_eq_type == SEQ_NONDEFAULT
20242 50 : && last_where
20243 49 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
20244 525 : || (eq_type == SEQ_NONDEFAULT
20245 24 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
20246 4 : continue;
20247 :
20248 475 : msg = G_("Non-CHARACTER object %qs in default CHARACTER "
20249 : "EQUIVALENCE statement at %L");
20250 479 : if (last_eq_type == SEQ_CHARACTER
20251 475 : && eq_type != SEQ_CHARACTER
20252 475 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
20253 4 : continue;
20254 :
20255 471 : msg = G_("Non-NUMERIC object %qs in default NUMERIC "
20256 : "EQUIVALENCE statement at %L");
20257 473 : if (last_eq_type == SEQ_NUMERIC
20258 471 : && eq_type != SEQ_NUMERIC
20259 471 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
20260 2 : continue;
20261 :
20262 3146 : identical_types:
20263 :
20264 3146 : last_ts =&sym->ts;
20265 3146 : last_where = &e->where;
20266 :
20267 3146 : if (!e->ref)
20268 1003 : continue;
20269 :
20270 : /* Shall not be an automatic array. */
20271 2143 : if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
20272 : {
20273 3 : gfc_error ("Array %qs at %L with non-constant bounds cannot be "
20274 : "an EQUIVALENCE object", sym->name, &e->where);
20275 3 : continue;
20276 : }
20277 :
20278 2140 : r = e->ref;
20279 4326 : while (r)
20280 : {
20281 : /* Shall not be a structure component. */
20282 2187 : if (r->type == REF_COMPONENT)
20283 : {
20284 0 : gfc_error ("Structure component %qs at %L cannot be an "
20285 : "EQUIVALENCE object",
20286 0 : r->u.c.component->name, &e->where);
20287 0 : break;
20288 : }
20289 :
20290 : /* A substring shall not have length zero. */
20291 2187 : if (r->type == REF_SUBSTRING)
20292 : {
20293 341 : if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
20294 : {
20295 1 : gfc_error ("Substring at %L has length zero",
20296 : &r->u.ss.start->where);
20297 1 : break;
20298 : }
20299 : }
20300 2186 : r = r->next;
20301 : }
20302 : }
20303 1565 : }
20304 :
20305 :
20306 : /* Function called by resolve_fntype to flag other symbols used in the
20307 : length type parameter specification of function results. */
20308 :
20309 : static bool
20310 4211 : flag_fn_result_spec (gfc_expr *expr,
20311 : gfc_symbol *sym,
20312 : int *f ATTRIBUTE_UNUSED)
20313 : {
20314 4211 : gfc_namespace *ns;
20315 4211 : gfc_symbol *s;
20316 :
20317 4211 : if (expr->expr_type == EXPR_VARIABLE)
20318 : {
20319 1378 : s = expr->symtree->n.sym;
20320 2153 : for (ns = s->ns; ns; ns = ns->parent)
20321 2153 : if (!ns->parent)
20322 : break;
20323 :
20324 1378 : if (sym == s)
20325 : {
20326 1 : gfc_error ("Self reference in character length expression "
20327 : "for %qs at %L", sym->name, &expr->where);
20328 1 : return true;
20329 : }
20330 :
20331 1377 : if (!s->fn_result_spec
20332 1377 : && s->attr.flavor == FL_PARAMETER)
20333 : {
20334 : /* Function contained in a module.... */
20335 63 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
20336 : {
20337 32 : gfc_symtree *st;
20338 32 : s->fn_result_spec = 1;
20339 : /* Make sure that this symbol is translated as a module
20340 : variable. */
20341 32 : st = gfc_get_unique_symtree (ns);
20342 32 : st->n.sym = s;
20343 32 : s->refs++;
20344 32 : }
20345 : /* ... which is use associated and called. */
20346 31 : else if (s->attr.use_assoc || s->attr.used_in_submodule
20347 0 : ||
20348 : /* External function matched with an interface. */
20349 0 : (s->ns->proc_name
20350 0 : && ((s->ns == ns
20351 0 : && s->ns->proc_name->attr.if_source == IFSRC_DECL)
20352 0 : || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
20353 0 : && s->ns->proc_name->attr.function))
20354 31 : s->fn_result_spec = 1;
20355 : }
20356 : }
20357 : return false;
20358 : }
20359 :
20360 :
20361 : /* Resolve function and ENTRY types, issue diagnostics if needed. */
20362 :
20363 : static void
20364 345787 : resolve_fntype (gfc_namespace *ns)
20365 : {
20366 345787 : gfc_entry_list *el;
20367 345787 : gfc_symbol *sym;
20368 :
20369 345787 : if (ns->proc_name == NULL || !ns->proc_name->attr.function)
20370 : return;
20371 :
20372 : /* If there are any entries, ns->proc_name is the entry master
20373 : synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
20374 180644 : if (ns->entries)
20375 596 : sym = ns->entries->sym;
20376 : else
20377 : sym = ns->proc_name;
20378 180644 : if (sym->result == sym
20379 145494 : && sym->ts.type == BT_UNKNOWN
20380 6 : && !gfc_set_default_type (sym, 0, NULL)
20381 180648 : && !sym->attr.untyped)
20382 : {
20383 3 : gfc_error ("Function %qs at %L has no IMPLICIT type",
20384 : sym->name, &sym->declared_at);
20385 3 : sym->attr.untyped = 1;
20386 : }
20387 :
20388 13680 : if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
20389 1813 : && !sym->attr.contained
20390 299 : && !gfc_check_symbol_access (sym->ts.u.derived)
20391 180644 : && gfc_check_symbol_access (sym))
20392 : {
20393 0 : gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
20394 : "%L of PRIVATE type %qs", sym->name,
20395 0 : &sym->declared_at, sym->ts.u.derived->name);
20396 : }
20397 :
20398 180644 : if (ns->entries)
20399 1253 : for (el = ns->entries->next; el; el = el->next)
20400 : {
20401 657 : if (el->sym->result == el->sym
20402 445 : && el->sym->ts.type == BT_UNKNOWN
20403 2 : && !gfc_set_default_type (el->sym, 0, NULL)
20404 659 : && !el->sym->attr.untyped)
20405 : {
20406 2 : gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
20407 : el->sym->name, &el->sym->declared_at);
20408 2 : el->sym->attr.untyped = 1;
20409 : }
20410 : }
20411 :
20412 180644 : if (sym->ts.type == BT_CHARACTER
20413 7000 : && sym->ts.u.cl->length
20414 1863 : && sym->ts.u.cl->length->ts.type == BT_INTEGER)
20415 1858 : gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
20416 : }
20417 :
20418 :
20419 : /* 12.3.2.1.1 Defined operators. */
20420 :
20421 : static bool
20422 452 : check_uop_procedure (gfc_symbol *sym, locus where)
20423 : {
20424 452 : gfc_formal_arglist *formal;
20425 :
20426 452 : if (!sym->attr.function)
20427 : {
20428 4 : gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
20429 : sym->name, &where);
20430 4 : return false;
20431 : }
20432 :
20433 448 : if (sym->ts.type == BT_CHARACTER
20434 15 : && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
20435 2 : && !(sym->result && ((sym->result->ts.u.cl
20436 2 : && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
20437 : {
20438 2 : gfc_error ("User operator procedure %qs at %L cannot be assumed "
20439 : "character length", sym->name, &where);
20440 2 : return false;
20441 : }
20442 :
20443 446 : formal = gfc_sym_get_dummy_args (sym);
20444 446 : if (!formal || !formal->sym)
20445 : {
20446 1 : gfc_error ("User operator procedure %qs at %L must have at least "
20447 : "one argument", sym->name, &where);
20448 1 : return false;
20449 : }
20450 :
20451 445 : if (formal->sym->attr.intent != INTENT_IN)
20452 : {
20453 0 : gfc_error ("First argument of operator interface at %L must be "
20454 : "INTENT(IN)", &where);
20455 0 : return false;
20456 : }
20457 :
20458 445 : if (formal->sym->attr.optional)
20459 : {
20460 0 : gfc_error ("First argument of operator interface at %L cannot be "
20461 : "optional", &where);
20462 0 : return false;
20463 : }
20464 :
20465 445 : formal = formal->next;
20466 445 : if (!formal || !formal->sym)
20467 : return true;
20468 :
20469 295 : if (formal->sym->attr.intent != INTENT_IN)
20470 : {
20471 0 : gfc_error ("Second argument of operator interface at %L must be "
20472 : "INTENT(IN)", &where);
20473 0 : return false;
20474 : }
20475 :
20476 295 : if (formal->sym->attr.optional)
20477 : {
20478 1 : gfc_error ("Second argument of operator interface at %L cannot be "
20479 : "optional", &where);
20480 1 : return false;
20481 : }
20482 :
20483 294 : if (formal->next)
20484 : {
20485 2 : gfc_error ("Operator interface at %L must have, at most, two "
20486 : "arguments", &where);
20487 2 : return false;
20488 : }
20489 :
20490 : return true;
20491 : }
20492 :
20493 : static void
20494 346547 : gfc_resolve_uops (gfc_symtree *symtree)
20495 : {
20496 346547 : gfc_interface *itr;
20497 :
20498 346547 : if (symtree == NULL)
20499 : return;
20500 :
20501 380 : gfc_resolve_uops (symtree->left);
20502 380 : gfc_resolve_uops (symtree->right);
20503 :
20504 773 : for (itr = symtree->n.uop->op; itr; itr = itr->next)
20505 393 : check_uop_procedure (itr->sym, itr->sym->declared_at);
20506 : }
20507 :
20508 :
20509 : /* Examine all of the expressions associated with a program unit,
20510 : assign types to all intermediate expressions, make sure that all
20511 : assignments are to compatible types and figure out which names
20512 : refer to which functions or subroutines. It doesn't check code
20513 : block, which is handled by gfc_resolve_code. */
20514 :
20515 : static void
20516 348294 : resolve_types (gfc_namespace *ns)
20517 : {
20518 348294 : gfc_namespace *n;
20519 348294 : gfc_charlen *cl;
20520 348294 : gfc_data *d;
20521 348294 : gfc_equiv *eq;
20522 348294 : gfc_namespace* old_ns = gfc_current_ns;
20523 348294 : bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
20524 :
20525 348294 : if (ns->types_resolved)
20526 : return;
20527 :
20528 : /* Check that all IMPLICIT types are ok. */
20529 345788 : if (!ns->seen_implicit_none)
20530 : {
20531 : unsigned letter;
20532 8699617 : for (letter = 0; letter != GFC_LETTERS; ++letter)
20533 8377409 : if (ns->set_flag[letter]
20534 8377409 : && !resolve_typespec_used (&ns->default_type[letter],
20535 : &ns->implicit_loc[letter], NULL))
20536 : return;
20537 : }
20538 :
20539 345787 : gfc_current_ns = ns;
20540 :
20541 345787 : resolve_entries (ns);
20542 :
20543 345787 : resolve_common_vars (&ns->blank_common, false);
20544 345787 : resolve_common_blocks (ns->common_root);
20545 :
20546 345787 : resolve_contained_functions (ns);
20547 :
20548 345787 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
20549 295752 : && ns->proc_name->attr.if_source == IFSRC_IFBODY)
20550 193769 : gfc_resolve_formal_arglist (ns->proc_name);
20551 :
20552 345787 : gfc_traverse_ns (ns, resolve_bind_c_derived_types);
20553 :
20554 441514 : for (cl = ns->cl_list; cl; cl = cl->next)
20555 95727 : resolve_charlen (cl);
20556 :
20557 345787 : gfc_traverse_ns (ns, resolve_symbol);
20558 :
20559 345787 : resolve_fntype (ns);
20560 :
20561 393883 : for (n = ns->contained; n; n = n->sibling)
20562 : {
20563 : /* Exclude final wrappers with the test for the artificial attribute. */
20564 48096 : if (gfc_pure (ns->proc_name)
20565 5 : && !gfc_pure (n->proc_name)
20566 48096 : && !n->proc_name->attr.artificial)
20567 0 : gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
20568 : "also be PURE", n->proc_name->name,
20569 : &n->proc_name->declared_at);
20570 :
20571 48096 : resolve_types (n);
20572 : }
20573 :
20574 345787 : forall_flag = 0;
20575 345787 : gfc_do_concurrent_flag = 0;
20576 345787 : gfc_check_interfaces (ns);
20577 :
20578 345787 : gfc_traverse_ns (ns, resolve_values);
20579 :
20580 345787 : if (ns->save_all || (!flag_automatic && !recursive))
20581 315 : gfc_save_all (ns);
20582 :
20583 345787 : iter_stack = NULL;
20584 348305 : for (d = ns->data; d; d = d->next)
20585 2518 : resolve_data (d);
20586 :
20587 345787 : iter_stack = NULL;
20588 345787 : gfc_traverse_ns (ns, gfc_formalize_init_value);
20589 :
20590 345787 : gfc_traverse_ns (ns, gfc_verify_binding_labels);
20591 :
20592 347352 : for (eq = ns->equiv; eq; eq = eq->next)
20593 1565 : resolve_equivalence (eq);
20594 :
20595 : /* Warn about unused labels. */
20596 345787 : if (warn_unused_label)
20597 4641 : warn_unused_fortran_label (ns->st_labels);
20598 :
20599 345787 : gfc_resolve_uops (ns->uop_root);
20600 :
20601 345787 : gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
20602 :
20603 345787 : gfc_resolve_omp_declare (ns);
20604 :
20605 345787 : gfc_resolve_omp_udrs (ns->omp_udr_root);
20606 :
20607 345787 : gfc_resolve_omp_udms (ns->omp_udm_root);
20608 345787 : if (ns->omp_udm_root)
20609 4 : gfc_error ("Sorry, %<declare mapper%>, used at %L, is not yet implemented",
20610 4 : &ns->omp_udm_root->n.omp_udm->where);
20611 :
20612 345787 : ns->types_resolved = 1;
20613 :
20614 345787 : gfc_current_ns = old_ns;
20615 : }
20616 :
20617 :
20618 : /* Call gfc_resolve_code recursively. */
20619 :
20620 : static void
20621 348350 : resolve_codes (gfc_namespace *ns)
20622 : {
20623 348350 : gfc_namespace *n;
20624 348350 : bitmap_obstack old_obstack;
20625 :
20626 348350 : if (ns->resolved == 1)
20627 14065 : return;
20628 :
20629 382437 : for (n = ns->contained; n; n = n->sibling)
20630 48152 : resolve_codes (n);
20631 :
20632 334285 : gfc_current_ns = ns;
20633 :
20634 : /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
20635 334285 : if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
20636 322166 : cs_base = NULL;
20637 :
20638 : /* Set to an out of range value. */
20639 334285 : current_entry_id = -1;
20640 :
20641 334285 : old_obstack = labels_obstack;
20642 334285 : bitmap_obstack_initialize (&labels_obstack);
20643 :
20644 334285 : gfc_resolve_oacc_declare (ns);
20645 334285 : gfc_resolve_oacc_routines (ns);
20646 334285 : gfc_resolve_omp_local_vars (ns);
20647 334285 : if (ns->omp_allocate)
20648 62 : gfc_resolve_omp_allocate (ns, ns->omp_allocate);
20649 334285 : gfc_resolve_code (ns->code, ns);
20650 :
20651 334284 : bitmap_obstack_release (&labels_obstack);
20652 334284 : labels_obstack = old_obstack;
20653 : }
20654 :
20655 :
20656 : /* This function is called after a complete program unit has been compiled.
20657 : Its purpose is to examine all of the expressions associated with a program
20658 : unit, assign types to all intermediate expressions, make sure that all
20659 : assignments are to compatible types and figure out which names refer to
20660 : which functions or subroutines. */
20661 :
20662 : void
20663 304960 : gfc_resolve (gfc_namespace *ns)
20664 : {
20665 304960 : gfc_namespace *old_ns;
20666 304960 : code_stack *old_cs_base;
20667 304960 : struct gfc_omp_saved_state old_omp_state;
20668 :
20669 304960 : if (ns->resolved)
20670 4762 : return;
20671 :
20672 300198 : ns->resolved = -1;
20673 300198 : old_ns = gfc_current_ns;
20674 300198 : old_cs_base = cs_base;
20675 :
20676 : /* As gfc_resolve can be called during resolution of an OpenMP construct
20677 : body, we should clear any state associated to it, so that say NS's
20678 : DO loops are not interpreted as OpenMP loops. */
20679 300198 : if (!ns->construct_entities)
20680 288079 : gfc_omp_save_and_clear_state (&old_omp_state);
20681 :
20682 300198 : resolve_types (ns);
20683 300198 : component_assignment_level = 0;
20684 300198 : resolve_codes (ns);
20685 :
20686 300197 : if (ns->omp_assumes)
20687 13 : gfc_resolve_omp_assumptions (ns->omp_assumes);
20688 :
20689 300197 : gfc_current_ns = old_ns;
20690 300197 : cs_base = old_cs_base;
20691 300197 : ns->resolved = 1;
20692 :
20693 300197 : gfc_run_passes (ns);
20694 :
20695 300197 : if (!ns->construct_entities)
20696 288078 : gfc_omp_restore_state (&old_omp_state);
20697 : }
|