Branch data Line data Source code
1 : : /* Perform type resolution on the various structures.
2 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 : : Contributed by Andy Vaught
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : #include "config.h"
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "options.h"
25 : : #include "bitmap.h"
26 : : #include "gfortran.h"
27 : : #include "arith.h" /* For gfc_compare_expr(). */
28 : : #include "dependency.h"
29 : : #include "data.h"
30 : : #include "target-memory.h" /* for gfc_simplify_transfer */
31 : : #include "constructor.h"
32 : :
33 : : /* Types used in equivalence statements. */
34 : :
35 : : enum seq_type
36 : : {
37 : : SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38 : : };
39 : :
40 : : /* Stack to keep track of the nesting of blocks as we move through the
41 : : code. See resolve_branch() and gfc_resolve_code(). */
42 : :
43 : : typedef struct code_stack
44 : : {
45 : : struct gfc_code *head, *current;
46 : : struct code_stack *prev;
47 : :
48 : : /* This bitmap keeps track of the targets valid for a branch from
49 : : inside this block except for END {IF|SELECT}s of enclosing
50 : : blocks. */
51 : : bitmap reachable_labels;
52 : : }
53 : : code_stack;
54 : :
55 : : static code_stack *cs_base = NULL;
56 : :
57 : : struct check_default_none_data
58 : : {
59 : : gfc_code *code;
60 : : hash_set<gfc_symbol *> *sym_hash;
61 : : gfc_namespace *ns;
62 : : bool default_none;
63 : : };
64 : :
65 : : /* Nonzero if we're inside a FORALL or DO CONCURRENT block. */
66 : :
67 : : static int forall_flag;
68 : : int gfc_do_concurrent_flag;
69 : :
70 : : /* True when we are resolving an expression that is an actual argument to
71 : : a procedure. */
72 : : static bool actual_arg = false;
73 : : /* True when we are resolving an expression that is the first actual argument
74 : : to a procedure. */
75 : : static bool first_actual_arg = false;
76 : :
77 : :
78 : : /* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
79 : :
80 : : static int omp_workshare_flag;
81 : :
82 : :
83 : : /* True if we are resolving a specification expression. */
84 : : static bool specification_expr = false;
85 : :
86 : : /* The id of the last entry seen. */
87 : : static int current_entry_id;
88 : :
89 : : /* We use bitmaps to determine if a branch target is valid. */
90 : : static bitmap_obstack labels_obstack;
91 : :
92 : : /* True when simplifying a EXPR_VARIABLE argument to an inquiry function. */
93 : : static bool inquiry_argument = false;
94 : :
95 : :
96 : : /* Is the symbol host associated? */
97 : : static bool
98 : 48659 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
99 : : {
100 : 53063 : for (ns = ns->parent; ns; ns = ns->parent)
101 : : {
102 : 4655 : if (sym->ns == ns)
103 : : return true;
104 : : }
105 : :
106 : : return false;
107 : : }
108 : :
109 : : /* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
110 : : an ABSTRACT derived-type. If where is not NULL, an error message with that
111 : : locus is printed, optionally using name. */
112 : :
113 : : static bool
114 : 1460698 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
115 : : {
116 : 1460698 : if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
117 : : {
118 : 5 : if (where)
119 : : {
120 : 5 : if (name)
121 : 4 : gfc_error ("%qs at %L is of the ABSTRACT type %qs",
122 : : name, where, ts->u.derived->name);
123 : : else
124 : 1 : gfc_error ("ABSTRACT type %qs used at %L",
125 : : ts->u.derived->name, where);
126 : : }
127 : :
128 : 5 : return false;
129 : : }
130 : :
131 : : return true;
132 : : }
133 : :
134 : :
135 : : static bool
136 : 4999 : check_proc_interface (gfc_symbol *ifc, locus *where)
137 : : {
138 : : /* Several checks for F08:C1216. */
139 : 4999 : if (ifc->attr.procedure)
140 : : {
141 : 2 : gfc_error ("Interface %qs at %L is declared "
142 : : "in a later PROCEDURE statement", ifc->name, where);
143 : 2 : return false;
144 : : }
145 : 4997 : if (ifc->generic)
146 : : {
147 : : /* For generic interfaces, check if there is
148 : : a specific procedure with the same name. */
149 : : gfc_interface *gen = ifc->generic;
150 : 12 : while (gen && strcmp (gen->sym->name, ifc->name) != 0)
151 : 5 : gen = gen->next;
152 : 7 : if (!gen)
153 : : {
154 : 4 : gfc_error ("Interface %qs at %L may not be generic",
155 : : ifc->name, where);
156 : 4 : return false;
157 : : }
158 : : }
159 : 4993 : if (ifc->attr.proc == PROC_ST_FUNCTION)
160 : : {
161 : 4 : gfc_error ("Interface %qs at %L may not be a statement function",
162 : : ifc->name, where);
163 : 4 : return false;
164 : : }
165 : 4989 : if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
166 : 4989 : || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
167 : 17 : ifc->attr.intrinsic = 1;
168 : 4989 : if (ifc->attr.intrinsic && !gfc_intrinsic_actual_ok (ifc->name, 0))
169 : : {
170 : 3 : gfc_error ("Intrinsic procedure %qs not allowed in "
171 : : "PROCEDURE statement at %L", ifc->name, where);
172 : 3 : return false;
173 : : }
174 : 4986 : if (!ifc->attr.if_source && !ifc->attr.intrinsic && ifc->name[0] != '\0')
175 : : {
176 : 7 : gfc_error ("Interface %qs at %L must be explicit", ifc->name, where);
177 : 7 : return false;
178 : : }
179 : : return true;
180 : : }
181 : :
182 : :
183 : : static void resolve_symbol (gfc_symbol *sym);
184 : :
185 : :
186 : : /* Resolve the interface for a PROCEDURE declaration or procedure pointer. */
187 : :
188 : : static bool
189 : 1987 : resolve_procedure_interface (gfc_symbol *sym)
190 : : {
191 : 1987 : gfc_symbol *ifc = sym->ts.interface;
192 : :
193 : 1987 : if (!ifc)
194 : : return true;
195 : :
196 : 1831 : if (ifc == sym)
197 : : {
198 : 2 : gfc_error ("PROCEDURE %qs at %L may not be used as its own interface",
199 : : sym->name, &sym->declared_at);
200 : 2 : return false;
201 : : }
202 : 1829 : if (!check_proc_interface (ifc, &sym->declared_at))
203 : : return false;
204 : :
205 : 1820 : if (ifc->attr.if_source || ifc->attr.intrinsic)
206 : : {
207 : : /* Resolve interface and copy attributes. */
208 : 1541 : resolve_symbol (ifc);
209 : 1541 : if (ifc->attr.intrinsic)
210 : 14 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
211 : :
212 : 1541 : if (ifc->result)
213 : : {
214 : 676 : sym->ts = ifc->result->ts;
215 : 676 : sym->attr.allocatable = ifc->result->attr.allocatable;
216 : 676 : sym->attr.pointer = ifc->result->attr.pointer;
217 : 676 : sym->attr.dimension = ifc->result->attr.dimension;
218 : 676 : sym->attr.class_ok = ifc->result->attr.class_ok;
219 : 676 : sym->as = gfc_copy_array_spec (ifc->result->as);
220 : 676 : sym->result = sym;
221 : : }
222 : : else
223 : : {
224 : 865 : sym->ts = ifc->ts;
225 : 865 : sym->attr.allocatable = ifc->attr.allocatable;
226 : 865 : sym->attr.pointer = ifc->attr.pointer;
227 : 865 : sym->attr.dimension = ifc->attr.dimension;
228 : 865 : sym->attr.class_ok = ifc->attr.class_ok;
229 : 865 : sym->as = gfc_copy_array_spec (ifc->as);
230 : : }
231 : 1541 : sym->ts.interface = ifc;
232 : 1541 : sym->attr.function = ifc->attr.function;
233 : 1541 : sym->attr.subroutine = ifc->attr.subroutine;
234 : :
235 : 1541 : sym->attr.pure = ifc->attr.pure;
236 : 1541 : sym->attr.elemental = ifc->attr.elemental;
237 : 1541 : sym->attr.contiguous = ifc->attr.contiguous;
238 : 1541 : sym->attr.recursive = ifc->attr.recursive;
239 : 1541 : sym->attr.always_explicit = ifc->attr.always_explicit;
240 : 1541 : sym->attr.ext_attr |= ifc->attr.ext_attr;
241 : 1541 : sym->attr.is_bind_c = ifc->attr.is_bind_c;
242 : : /* Copy char length. */
243 : 1541 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
244 : : {
245 : 45 : sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
246 : 45 : if (sym->ts.u.cl->length && !sym->ts.u.cl->resolved
247 : 53 : && !gfc_resolve_expr (sym->ts.u.cl->length))
248 : : return false;
249 : : }
250 : : }
251 : :
252 : : return true;
253 : : }
254 : :
255 : :
256 : : /* Resolve types of formal argument lists. These have to be done early so that
257 : : the formal argument lists of module procedures can be copied to the
258 : : containing module before the individual procedures are resolved
259 : : individually. We also resolve argument lists of procedures in interface
260 : : blocks because they are self-contained scoping units.
261 : :
262 : : Since a dummy argument cannot be a non-dummy procedure, the only
263 : : resort left for untyped names are the IMPLICIT types. */
264 : :
265 : : void
266 : 497746 : gfc_resolve_formal_arglist (gfc_symbol *proc)
267 : : {
268 : 497746 : gfc_formal_arglist *f;
269 : 497746 : gfc_symbol *sym;
270 : 497746 : bool saved_specification_expr;
271 : 497746 : int i;
272 : :
273 : 497746 : if (proc->result != NULL)
274 : 316422 : sym = proc->result;
275 : : else
276 : : sym = proc;
277 : :
278 : 497746 : if (gfc_elemental (proc)
279 : 336420 : || sym->attr.pointer || sym->attr.allocatable
280 : 822564 : || (sym->as && sym->as->rank != 0))
281 : : {
282 : 175203 : proc->attr.always_explicit = 1;
283 : 175203 : sym->attr.always_explicit = 1;
284 : : }
285 : :
286 : 497746 : gfc_namespace *orig_current_ns = gfc_current_ns;
287 : 497746 : gfc_current_ns = gfc_get_procedure_ns (proc);
288 : :
289 : 1286297 : for (f = proc->formal; f; f = f->next)
290 : : {
291 : 788553 : gfc_array_spec *as;
292 : :
293 : 788553 : sym = f->sym;
294 : :
295 : 788553 : if (sym == NULL)
296 : : {
297 : : /* Alternate return placeholder. */
298 : 171 : if (gfc_elemental (proc))
299 : 1 : gfc_error ("Alternate return specifier in elemental subroutine "
300 : : "%qs at %L is not allowed", proc->name,
301 : : &proc->declared_at);
302 : 171 : if (proc->attr.function)
303 : 1 : gfc_error ("Alternate return specifier in function "
304 : : "%qs at %L is not allowed", proc->name,
305 : : &proc->declared_at);
306 : 171 : continue;
307 : : }
308 : :
309 : 550 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
310 : 788932 : && !resolve_procedure_interface (sym))
311 : : break;
312 : :
313 : 788382 : if (strcmp (proc->name, sym->name) == 0)
314 : : {
315 : 2 : gfc_error ("Self-referential argument "
316 : : "%qs at %L is not allowed", sym->name,
317 : : &proc->declared_at);
318 : 2 : break;
319 : : }
320 : :
321 : 788380 : if (sym->attr.if_source != IFSRC_UNKNOWN)
322 : 813 : gfc_resolve_formal_arglist (sym);
323 : :
324 : 788380 : if (sym->attr.subroutine || sym->attr.external)
325 : : {
326 : 813 : if (sym->attr.flavor == FL_UNKNOWN)
327 : 9 : gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, &sym->declared_at);
328 : : }
329 : : else
330 : : {
331 : 787567 : if (sym->ts.type == BT_UNKNOWN && !proc->attr.intrinsic
332 : 3660 : && (!sym->attr.function || sym->result == sym))
333 : 3622 : gfc_set_default_type (sym, 1, sym->ns);
334 : : }
335 : :
336 : 788380 : as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 : 801374 : ? CLASS_DATA (sym)->as : sym->as;
338 : :
339 : 788380 : saved_specification_expr = specification_expr;
340 : 788380 : specification_expr = true;
341 : 788380 : gfc_resolve_array_spec (as, 0);
342 : 788380 : specification_expr = saved_specification_expr;
343 : :
344 : : /* We can't tell if an array with dimension (:) is assumed or deferred
345 : : shape until we know if it has the pointer or allocatable attributes.
346 : : */
347 : 788380 : if (as && as->rank > 0 && as->type == AS_DEFERRED
348 : 11904 : && ((sym->ts.type != BT_CLASS
349 : 10841 : && !(sym->attr.pointer || sym->attr.allocatable))
350 : 5184 : || (sym->ts.type == BT_CLASS
351 : 1063 : && !(CLASS_DATA (sym)->attr.class_pointer
352 : 869 : || CLASS_DATA (sym)->attr.allocatable)))
353 : 7175 : && sym->attr.flavor != FL_PROCEDURE)
354 : : {
355 : 7174 : as->type = AS_ASSUMED_SHAPE;
356 : 16677 : for (i = 0; i < as->rank; i++)
357 : 9503 : as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
358 : : }
359 : :
360 : 116985 : if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 : 103851 : || (as && as->type == AS_ASSUMED_RANK)
362 : 743672 : || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 : 733712 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 : 10963 : && (CLASS_DATA (sym)->attr.class_pointer
365 : 10486 : || CLASS_DATA (sym)->attr.allocatable
366 : 9609 : || CLASS_DATA (sym)->attr.target))
367 : 732358 : || sym->attr.optional)
368 : : {
369 : 70805 : proc->attr.always_explicit = 1;
370 : 70805 : if (proc->result)
371 : 35241 : proc->result->attr.always_explicit = 1;
372 : : }
373 : :
374 : : /* If the flavor is unknown at this point, it has to be a variable.
375 : : A procedure specification would have already set the type. */
376 : :
377 : 788380 : if (sym->attr.flavor == FL_UNKNOWN)
378 : 48044 : gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379 : :
380 : 788380 : if (gfc_pure (proc))
381 : : {
382 : 325429 : if (sym->attr.flavor == FL_PROCEDURE)
383 : : {
384 : : /* F08:C1279. */
385 : 29 : if (!gfc_pure (sym))
386 : : {
387 : 1 : gfc_error ("Dummy procedure %qs of PURE procedure at %L must "
388 : : "also be PURE", sym->name, &sym->declared_at);
389 : 1 : continue;
390 : : }
391 : : }
392 : 325400 : else if (!sym->attr.pointer)
393 : : {
394 : 325392 : if (proc->attr.function && sym->attr.intent != INTENT_IN)
395 : : {
396 : 109 : if (sym->attr.value)
397 : 108 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
398 : : " of pure function %qs at %L with VALUE "
399 : : "attribute but without INTENT(IN)",
400 : : sym->name, proc->name, &sym->declared_at);
401 : : else
402 : 1 : gfc_error ("Argument %qs of pure function %qs at %L must "
403 : : "be INTENT(IN) or VALUE", sym->name, proc->name,
404 : : &sym->declared_at);
405 : : }
406 : :
407 : 325392 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
408 : : {
409 : 159 : if (sym->attr.value)
410 : 159 : gfc_notify_std (GFC_STD_F2008, "Argument %qs"
411 : : " of pure subroutine %qs at %L with VALUE "
412 : : "attribute but without INTENT", sym->name,
413 : : proc->name, &sym->declared_at);
414 : : else
415 : 0 : gfc_error ("Argument %qs of pure subroutine %qs at %L "
416 : : "must have its INTENT specified or have the "
417 : : "VALUE attribute", sym->name, proc->name,
418 : : &sym->declared_at);
419 : : }
420 : : }
421 : :
422 : : /* F08:C1278a. */
423 : 325428 : if (sym->ts.type == BT_CLASS && sym->attr.intent == INTENT_OUT)
424 : : {
425 : 1 : gfc_error ("INTENT(OUT) argument %qs of pure procedure %qs at %L"
426 : : " may not be polymorphic", sym->name, proc->name,
427 : : &sym->declared_at);
428 : 1 : continue;
429 : : }
430 : : }
431 : :
432 : 788378 : if (proc->attr.implicit_pure)
433 : : {
434 : 23607 : if (sym->attr.flavor == FL_PROCEDURE)
435 : : {
436 : 294 : if (!gfc_pure (sym))
437 : 274 : proc->attr.implicit_pure = 0;
438 : : }
439 : 23313 : else if (!sym->attr.pointer)
440 : : {
441 : 22532 : if (proc->attr.function && sym->attr.intent != INTENT_IN
442 : 2606 : && !sym->value)
443 : 2606 : proc->attr.implicit_pure = 0;
444 : :
445 : 22532 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 : 4056 : && !sym->value)
447 : 4056 : proc->attr.implicit_pure = 0;
448 : : }
449 : : }
450 : :
451 : 788378 : if (gfc_elemental (proc))
452 : : {
453 : : /* F08:C1289. */
454 : 300043 : if (sym->attr.codimension
455 : 300042 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
456 : 889 : && CLASS_DATA (sym)->attr.codimension))
457 : : {
458 : 3 : gfc_error ("Coarray dummy argument %qs at %L to elemental "
459 : : "procedure", sym->name, &sym->declared_at);
460 : 3 : continue;
461 : : }
462 : :
463 : 300040 : if (sym->as || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
464 : 887 : && CLASS_DATA (sym)->as))
465 : : {
466 : 2 : gfc_error ("Argument %qs of elemental procedure at %L must "
467 : : "be scalar", sym->name, &sym->declared_at);
468 : 2 : continue;
469 : : }
470 : :
471 : 300038 : if (sym->attr.allocatable
472 : 300037 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
473 : 886 : && CLASS_DATA (sym)->attr.allocatable))
474 : : {
475 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
476 : : "have the ALLOCATABLE attribute", sym->name,
477 : : &sym->declared_at);
478 : 2 : continue;
479 : : }
480 : :
481 : 300036 : if (sym->attr.pointer
482 : 300035 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
483 : 885 : && CLASS_DATA (sym)->attr.class_pointer))
484 : : {
485 : 2 : gfc_error ("Argument %qs of elemental procedure at %L cannot "
486 : : "have the POINTER attribute", sym->name,
487 : : &sym->declared_at);
488 : 2 : continue;
489 : : }
490 : :
491 : 300034 : if (sym->attr.flavor == FL_PROCEDURE)
492 : : {
493 : 2 : gfc_error ("Dummy procedure %qs not allowed in elemental "
494 : : "procedure %qs at %L", sym->name, proc->name,
495 : : &sym->declared_at);
496 : 2 : continue;
497 : : }
498 : :
499 : : /* Fortran 2008 Corrigendum 1, C1290a. */
500 : 300032 : if (sym->attr.intent == INTENT_UNKNOWN && !sym->attr.value)
501 : : {
502 : 2 : gfc_error ("Argument %qs of elemental procedure %qs at %L must "
503 : : "have its INTENT specified or have the VALUE "
504 : : "attribute", sym->name, proc->name,
505 : : &sym->declared_at);
506 : 2 : continue;
507 : : }
508 : : }
509 : :
510 : : /* Each dummy shall be specified to be scalar. */
511 : 788365 : if (proc->attr.proc == PROC_ST_FUNCTION)
512 : : {
513 : 305 : if (sym->as != NULL)
514 : : {
515 : : /* F03:C1263 (R1238) The function-name and each dummy-arg-name
516 : : shall be specified, explicitly or implicitly, to be scalar. */
517 : 1 : gfc_error ("Argument %qs of statement function %qs at %L "
518 : : "must be scalar", sym->name, proc->name,
519 : : &proc->declared_at);
520 : 1 : continue;
521 : : }
522 : :
523 : 304 : if (sym->ts.type == BT_CHARACTER)
524 : : {
525 : 48 : gfc_charlen *cl = sym->ts.u.cl;
526 : 48 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
527 : : {
528 : 0 : gfc_error ("Character-valued argument %qs of statement "
529 : : "function at %L must have constant length",
530 : : sym->name, &sym->declared_at);
531 : 0 : continue;
532 : : }
533 : : }
534 : : }
535 : : }
536 : 497746 : if (sym)
537 : 497654 : sym->formal_resolved = 1;
538 : 497746 : gfc_current_ns = orig_current_ns;
539 : 497746 : }
540 : :
541 : :
542 : : /* Work function called when searching for symbols that have argument lists
543 : : associated with them. */
544 : :
545 : : static void
546 : 1742549 : find_arglists (gfc_symbol *sym)
547 : : {
548 : 1742549 : if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
549 : 315313 : || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
550 : : return;
551 : :
552 : 313683 : gfc_resolve_formal_arglist (sym);
553 : : }
554 : :
555 : :
556 : : /* Given a namespace, resolve all formal argument lists within the namespace.
557 : : */
558 : :
559 : : static void
560 : 329458 : resolve_formal_arglists (gfc_namespace *ns)
561 : : {
562 : 0 : if (ns == NULL)
563 : : return;
564 : :
565 : 329458 : gfc_traverse_ns (ns, find_arglists);
566 : : }
567 : :
568 : :
569 : : static void
570 : 35590 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
571 : : {
572 : 35590 : bool t;
573 : :
574 : 35590 : if (sym && sym->attr.flavor == FL_PROCEDURE
575 : 35590 : && sym->ns->parent
576 : 1062 : && sym->ns->parent->proc_name
577 : 1062 : && sym->ns->parent->proc_name->attr.flavor == FL_PROCEDURE
578 : 1 : && !strcmp (sym->name, sym->ns->parent->proc_name->name))
579 : 0 : gfc_error ("Contained procedure %qs at %L has the same name as its "
580 : : "encompassing procedure", sym->name, &sym->declared_at);
581 : :
582 : : /* If this namespace is not a function or an entry master function,
583 : : ignore it. */
584 : 35590 : if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
585 : 10366 : || sym->attr.entry_master)
586 : 25412 : return;
587 : :
588 : 10178 : if (!sym->result)
589 : : return;
590 : :
591 : : /* Try to find out of what the return type is. */
592 : 10178 : if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
593 : : {
594 : 55 : t = gfc_set_default_type (sym->result, 0, ns);
595 : :
596 : 55 : if (!t && !sym->result->attr.untyped)
597 : : {
598 : 19 : if (sym->result == sym)
599 : 1 : gfc_error ("Contained function %qs at %L has no IMPLICIT type",
600 : : sym->name, &sym->declared_at);
601 : 18 : else if (!sym->result->attr.proc_pointer)
602 : 0 : gfc_error ("Result %qs of contained function %qs at %L has "
603 : : "no IMPLICIT type", sym->result->name, sym->name,
604 : : &sym->result->declared_at);
605 : 19 : sym->result->attr.untyped = 1;
606 : : }
607 : : }
608 : :
609 : : /* Fortran 2008 Draft Standard, page 535, C418, on type-param-value
610 : : type, lists the only ways a character length value of * can be used:
611 : : dummy arguments of procedures, named constants, function results and
612 : : in allocate statements if the allocate_object is an assumed length dummy
613 : : in external functions. Internal function results and results of module
614 : : procedures are not on this list, ergo, not permitted. */
615 : :
616 : 10178 : if (sym->result->ts.type == BT_CHARACTER)
617 : : {
618 : 1151 : gfc_charlen *cl = sym->result->ts.u.cl;
619 : 1151 : if ((!cl || !cl->length) && !sym->result->ts.deferred)
620 : : {
621 : : /* See if this is a module-procedure and adapt error message
622 : : accordingly. */
623 : 4 : bool module_proc;
624 : 4 : gcc_assert (ns->parent && ns->parent->proc_name);
625 : 4 : module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
626 : :
627 : 7 : gfc_error (module_proc
628 : : ? G_("Character-valued module procedure %qs at %L"
629 : : " must not be assumed length")
630 : : : G_("Character-valued internal function %qs at %L"
631 : : " must not be assumed length"),
632 : : sym->name, &sym->declared_at);
633 : : }
634 : : }
635 : : }
636 : :
637 : :
638 : : /* Add NEW_ARGS to the formal argument list of PROC, taking care not to
639 : : introduce duplicates. */
640 : :
641 : : static void
642 : 1420 : merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
643 : : {
644 : 1420 : gfc_formal_arglist *f, *new_arglist;
645 : 1420 : gfc_symbol *new_sym;
646 : :
647 : 2561 : for (; new_args != NULL; new_args = new_args->next)
648 : : {
649 : 1141 : new_sym = new_args->sym;
650 : : /* See if this arg is already in the formal argument list. */
651 : 2165 : for (f = proc->formal; f; f = f->next)
652 : : {
653 : 1470 : if (new_sym == f->sym)
654 : : break;
655 : : }
656 : :
657 : 1141 : if (f)
658 : 446 : continue;
659 : :
660 : : /* Add a new argument. Argument order is not important. */
661 : 695 : new_arglist = gfc_get_formal_arglist ();
662 : 695 : new_arglist->sym = new_sym;
663 : 695 : new_arglist->next = proc->formal;
664 : 695 : proc->formal = new_arglist;
665 : : }
666 : 1420 : }
667 : :
668 : :
669 : : /* Flag the arguments that are not present in all entries. */
670 : :
671 : : static void
672 : 1420 : check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
673 : : {
674 : 1420 : gfc_formal_arglist *f, *head;
675 : 1420 : head = new_args;
676 : :
677 : 2994 : for (f = proc->formal; f; f = f->next)
678 : : {
679 : 1574 : if (f->sym == NULL)
680 : 36 : continue;
681 : :
682 : 2704 : for (new_args = head; new_args; new_args = new_args->next)
683 : : {
684 : 2262 : if (new_args->sym == f->sym)
685 : : break;
686 : : }
687 : :
688 : 1538 : if (new_args)
689 : 1096 : continue;
690 : :
691 : 442 : f->sym->attr.not_always_present = 1;
692 : : }
693 : 1420 : }
694 : :
695 : :
696 : : /* Resolve alternate entry points. If a symbol has multiple entry points we
697 : : create a new master symbol for the main routine, and turn the existing
698 : : symbol into an entry point. */
699 : :
700 : : static void
701 : 364543 : resolve_entries (gfc_namespace *ns)
702 : : {
703 : 364543 : gfc_namespace *old_ns;
704 : 364543 : gfc_code *c;
705 : 364543 : gfc_symbol *proc;
706 : 364543 : gfc_entry_list *el;
707 : : /* Provide sufficient space to hold "master.%d.%s". */
708 : 364543 : char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
709 : 364543 : static int master_count = 0;
710 : :
711 : 364543 : if (ns->proc_name == NULL)
712 : 363875 : return;
713 : :
714 : : /* No need to do anything if this procedure doesn't have alternate entry
715 : : points. */
716 : 364495 : if (!ns->entries)
717 : : return;
718 : :
719 : : /* We may already have resolved alternate entry points. */
720 : 918 : if (ns->proc_name->attr.entry_master)
721 : : return;
722 : :
723 : : /* If this isn't a procedure something has gone horribly wrong. */
724 : 668 : gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
725 : :
726 : : /* Remember the current namespace. */
727 : 668 : old_ns = gfc_current_ns;
728 : :
729 : 668 : gfc_current_ns = ns;
730 : :
731 : : /* Add the main entry point to the list of entry points. */
732 : 668 : el = gfc_get_entry_list ();
733 : 668 : el->sym = ns->proc_name;
734 : 668 : el->id = 0;
735 : 668 : el->next = ns->entries;
736 : 668 : ns->entries = el;
737 : 668 : ns->proc_name->attr.entry = 1;
738 : :
739 : : /* If it is a module function, it needs to be in the right namespace
740 : : so that gfc_get_fake_result_decl can gather up the results. The
741 : : need for this arose in get_proc_name, where these beasts were
742 : : left in their own namespace, to keep prior references linked to
743 : : the entry declaration.*/
744 : 668 : if (ns->proc_name->attr.function
745 : 564 : && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
746 : 188 : el->sym->ns = ns;
747 : :
748 : : /* Do the same for entries where the master is not a module
749 : : procedure. These are retained in the module namespace because
750 : : of the module procedure declaration. */
751 : 1420 : for (el = el->next; el; el = el->next)
752 : 752 : if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
753 : 0 : && el->sym->attr.mod_proc)
754 : 0 : el->sym->ns = ns;
755 : 668 : el = ns->entries;
756 : :
757 : : /* Add an entry statement for it. */
758 : 668 : c = gfc_get_code (EXEC_ENTRY);
759 : 668 : c->ext.entry = el;
760 : 668 : c->next = ns->code;
761 : 668 : ns->code = c;
762 : :
763 : : /* Create a new symbol for the master function. */
764 : : /* Give the internal function a unique name (within this file).
765 : : Also include the function name so the user has some hope of figuring
766 : : out what is going on. */
767 : 668 : snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
768 : 668 : master_count++, ns->proc_name->name);
769 : 668 : gfc_get_ha_symbol (name, &proc);
770 : 668 : gcc_assert (proc != NULL);
771 : :
772 : 668 : gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
773 : 668 : if (ns->proc_name->attr.subroutine)
774 : 104 : gfc_add_subroutine (&proc->attr, proc->name, NULL);
775 : : else
776 : : {
777 : 564 : gfc_symbol *sym;
778 : 564 : gfc_typespec *ts, *fts;
779 : 564 : gfc_array_spec *as, *fas;
780 : 564 : gfc_add_function (&proc->attr, proc->name, NULL);
781 : 564 : proc->result = proc;
782 : 564 : fas = ns->entries->sym->as;
783 : 564 : fas = fas ? fas : ns->entries->sym->result->as;
784 : 564 : fts = &ns->entries->sym->result->ts;
785 : 564 : if (fts->type == BT_UNKNOWN)
786 : 51 : fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
787 : 1058 : for (el = ns->entries->next; el; el = el->next)
788 : : {
789 : 603 : ts = &el->sym->result->ts;
790 : 603 : as = el->sym->as;
791 : 603 : as = as ? as : el->sym->result->as;
792 : 603 : if (ts->type == BT_UNKNOWN)
793 : 61 : ts = gfc_get_default_type (el->sym->result->name, NULL);
794 : :
795 : 603 : if (! gfc_compare_types (ts, fts)
796 : 497 : || (el->sym->result->attr.dimension
797 : 497 : != ns->entries->sym->result->attr.dimension)
798 : 603 : || (el->sym->result->attr.pointer
799 : 497 : != ns->entries->sym->result->attr.pointer))
800 : : break;
801 : 65 : else if (as && fas && ns->entries->sym->result != el->sym->result
802 : 559 : && gfc_compare_array_spec (as, fas) == 0)
803 : 5 : gfc_error ("Function %s at %L has entries with mismatched "
804 : : "array specifications", ns->entries->sym->name,
805 : 5 : &ns->entries->sym->declared_at);
806 : : /* The characteristics need to match and thus both need to have
807 : : the same string length, i.e. both len=*, or both len=4.
808 : : Having both len=<variable> is also possible, but difficult to
809 : : check at compile time. */
810 : 492 : else if (ts->type == BT_CHARACTER
811 : 89 : && (el->sym->result->attr.allocatable
812 : 89 : != ns->entries->sym->result->attr.allocatable))
813 : : {
814 : 3 : gfc_error ("Function %s at %L has entry %s with mismatched "
815 : : "characteristics", ns->entries->sym->name,
816 : : &ns->entries->sym->declared_at, el->sym->name);
817 : 3 : goto cleanup;
818 : : }
819 : 489 : else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
820 : 86 : && (((ts->u.cl->length && !fts->u.cl->length)
821 : 85 : ||(!ts->u.cl->length && fts->u.cl->length))
822 : 66 : || (ts->u.cl->length
823 : 29 : && ts->u.cl->length->expr_type
824 : 29 : != fts->u.cl->length->expr_type)
825 : 66 : || (ts->u.cl->length
826 : 29 : && ts->u.cl->length->expr_type == EXPR_CONSTANT
827 : 28 : && mpz_cmp (ts->u.cl->length->value.integer,
828 : 28 : fts->u.cl->length->value.integer) != 0)))
829 : 21 : gfc_notify_std (GFC_STD_GNU, "Function %s at %L with "
830 : : "entries returning variables of different "
831 : : "string lengths", ns->entries->sym->name,
832 : 21 : &ns->entries->sym->declared_at);
833 : 468 : else if (el->sym->result->attr.allocatable
834 : 468 : != ns->entries->sym->result->attr.allocatable)
835 : : break;
836 : : }
837 : :
838 : 561 : if (el == NULL)
839 : : {
840 : 455 : sym = ns->entries->sym->result;
841 : : /* All result types the same. */
842 : 455 : proc->ts = *fts;
843 : 455 : if (sym->attr.dimension)
844 : 63 : gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
845 : 455 : if (sym->attr.pointer)
846 : 78 : gfc_add_pointer (&proc->attr, NULL);
847 : 455 : if (sym->attr.allocatable)
848 : 24 : gfc_add_allocatable (&proc->attr, NULL);
849 : : }
850 : : else
851 : : {
852 : : /* Otherwise the result will be passed through a union by
853 : : reference. */
854 : 106 : proc->attr.mixed_entry_master = 1;
855 : 340 : for (el = ns->entries; el; el = el->next)
856 : : {
857 : 234 : sym = el->sym->result;
858 : 234 : if (sym->attr.dimension)
859 : : {
860 : 1 : if (el == ns->entries)
861 : 0 : gfc_error ("FUNCTION result %s cannot be an array in "
862 : : "FUNCTION %s at %L", sym->name,
863 : 0 : ns->entries->sym->name, &sym->declared_at);
864 : : else
865 : 1 : gfc_error ("ENTRY result %s cannot be an array in "
866 : : "FUNCTION %s at %L", sym->name,
867 : 1 : ns->entries->sym->name, &sym->declared_at);
868 : : }
869 : 233 : else if (sym->attr.pointer)
870 : : {
871 : 1 : if (el == ns->entries)
872 : 1 : gfc_error ("FUNCTION result %s cannot be a POINTER in "
873 : : "FUNCTION %s at %L", sym->name,
874 : 1 : ns->entries->sym->name, &sym->declared_at);
875 : : else
876 : 0 : gfc_error ("ENTRY result %s cannot be a POINTER in "
877 : : "FUNCTION %s at %L", sym->name,
878 : 0 : ns->entries->sym->name, &sym->declared_at);
879 : : }
880 : 232 : else if (sym->attr.allocatable)
881 : : {
882 : 0 : if (el == ns->entries)
883 : 0 : gfc_error ("FUNCTION result %s cannot be ALLOCATABLE in "
884 : : "FUNCTION %s at %L", sym->name,
885 : 0 : ns->entries->sym->name, &sym->declared_at);
886 : : else
887 : 0 : gfc_error ("ENTRY result %s cannot be ALLOCATABLE in "
888 : : "FUNCTION %s at %L", sym->name,
889 : 0 : ns->entries->sym->name, &sym->declared_at);
890 : : }
891 : : else
892 : : {
893 : 232 : ts = &sym->ts;
894 : 232 : if (ts->type == BT_UNKNOWN)
895 : 9 : ts = gfc_get_default_type (sym->name, NULL);
896 : 232 : switch (ts->type)
897 : : {
898 : 84 : case BT_INTEGER:
899 : 84 : if (ts->kind == gfc_default_integer_kind)
900 : : sym = NULL;
901 : : break;
902 : 99 : case BT_REAL:
903 : 99 : if (ts->kind == gfc_default_real_kind
904 : 18 : || ts->kind == gfc_default_double_kind)
905 : : sym = NULL;
906 : : break;
907 : 19 : case BT_COMPLEX:
908 : 19 : if (ts->kind == gfc_default_complex_kind)
909 : : sym = NULL;
910 : : break;
911 : 27 : case BT_LOGICAL:
912 : 27 : if (ts->kind == gfc_default_logical_kind)
913 : : sym = NULL;
914 : : break;
915 : : case BT_UNKNOWN:
916 : : /* We will issue error elsewhere. */
917 : : sym = NULL;
918 : : break;
919 : : default:
920 : : break;
921 : : }
922 : 3 : if (sym)
923 : : {
924 : 3 : if (el == ns->entries)
925 : 1 : gfc_error ("FUNCTION result %s cannot be of type %s "
926 : : "in FUNCTION %s at %L", sym->name,
927 : 1 : gfc_typename (ts), ns->entries->sym->name,
928 : : &sym->declared_at);
929 : : else
930 : 2 : gfc_error ("ENTRY result %s cannot be of type %s "
931 : : "in FUNCTION %s at %L", sym->name,
932 : 2 : gfc_typename (ts), ns->entries->sym->name,
933 : : &sym->declared_at);
934 : : }
935 : : }
936 : : }
937 : : }
938 : : }
939 : :
940 : 106 : cleanup:
941 : 668 : proc->attr.access = ACCESS_PRIVATE;
942 : 668 : proc->attr.entry_master = 1;
943 : :
944 : : /* Merge all the entry point arguments. */
945 : 2088 : for (el = ns->entries; el; el = el->next)
946 : 1420 : merge_argument_lists (proc, el->sym->formal);
947 : :
948 : : /* Check the master formal arguments for any that are not
949 : : present in all entry points. */
950 : 2088 : for (el = ns->entries; el; el = el->next)
951 : 1420 : check_argument_lists (proc, el->sym->formal);
952 : :
953 : : /* Use the master function for the function body. */
954 : 668 : ns->proc_name = proc;
955 : :
956 : : /* Finalize the new symbols. */
957 : 668 : gfc_commit_symbols ();
958 : :
959 : : /* Restore the original namespace. */
960 : 668 : gfc_current_ns = old_ns;
961 : : }
962 : :
963 : :
964 : : /* Forward declaration. */
965 : : static bool is_non_constant_shape_array (gfc_symbol *sym);
966 : :
967 : :
968 : : /* Resolve common variables. */
969 : : static void
970 : 331353 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
971 : : {
972 : 331353 : gfc_symbol *csym = common_block->head;
973 : 331353 : gfc_gsymbol *gsym;
974 : :
975 : 337322 : for (; csym; csym = csym->common_next)
976 : : {
977 : 5969 : gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
978 : 5969 : if (gsym && (gsym->type == GSYM_MODULE || gsym->type == GSYM_PROGRAM))
979 : : {
980 : 3 : if (csym->common_block)
981 : 2 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
982 : : "COMMON block at %L", gsym->name,
983 : : &gsym->where, &csym->common_block->where);
984 : : else
985 : 1 : gfc_error_now ("Global entity %qs at %L cannot appear in a "
986 : : "COMMON block", gsym->name, &gsym->where);
987 : : }
988 : :
989 : : /* gfc_add_in_common may have been called before, but the reported errors
990 : : have been ignored to continue parsing.
991 : : We do the checks again here, unless the symbol is USE associated. */
992 : 5969 : if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
993 : : {
994 : 5696 : gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
995 : 5696 : gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
996 : : &common_block->where);
997 : : }
998 : :
999 : 5969 : if (csym->value || csym->attr.data)
1000 : : {
1001 : 131 : if (!csym->ns->is_block_data)
1002 : 32 : gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1003 : : "but only in BLOCK DATA initialization is "
1004 : : "allowed", csym->name, &csym->declared_at);
1005 : 99 : else if (!named_common)
1006 : 8 : gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1007 : : "in a blank COMMON but initialization is only "
1008 : : "allowed in named common blocks", csym->name,
1009 : : &csym->declared_at);
1010 : : }
1011 : :
1012 : 5969 : if (UNLIMITED_POLY (csym))
1013 : 1 : gfc_error_now ("%qs at %L cannot appear in COMMON "
1014 : : "[F2008:C5100]", csym->name, &csym->declared_at);
1015 : :
1016 : 5969 : if (csym->attr.dimension && is_non_constant_shape_array (csym))
1017 : : {
1018 : 1 : gfc_error_now ("Automatic object %qs at %L cannot appear in "
1019 : : "COMMON at %L", csym->name, &csym->declared_at,
1020 : : &common_block->where);
1021 : : /* Avoid confusing follow-on error. */
1022 : 1 : csym->error = 1;
1023 : : }
1024 : :
1025 : 5969 : if (csym->ts.type != BT_DERIVED)
1026 : 5922 : continue;
1027 : :
1028 : 47 : if (!(csym->ts.u.derived->attr.sequence
1029 : 3 : || csym->ts.u.derived->attr.is_bind_c))
1030 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1031 : : "has neither the SEQUENCE nor the BIND(C) "
1032 : : "attribute", csym->name, &csym->declared_at);
1033 : 47 : if (csym->ts.u.derived->attr.alloc_comp)
1034 : 3 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1035 : : "has an ultimate component that is "
1036 : : "allocatable", csym->name, &csym->declared_at);
1037 : 47 : if (gfc_has_default_initializer (csym->ts.u.derived))
1038 : 2 : gfc_error_now ("Derived type variable %qs in COMMON at %L "
1039 : : "may not have default initializer", csym->name,
1040 : : &csym->declared_at);
1041 : :
1042 : 47 : if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
1043 : 16 : gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
1044 : : }
1045 : 331353 : }
1046 : :
1047 : : /* Resolve common blocks. */
1048 : : static void
1049 : 329923 : resolve_common_blocks (gfc_symtree *common_root)
1050 : : {
1051 : 329923 : gfc_symbol *sym = NULL;
1052 : 329923 : gfc_gsymbol * gsym;
1053 : :
1054 : 329923 : if (common_root == NULL)
1055 : 329801 : return;
1056 : :
1057 : 1895 : if (common_root->left)
1058 : 221 : resolve_common_blocks (common_root->left);
1059 : 1895 : if (common_root->right)
1060 : 244 : resolve_common_blocks (common_root->right);
1061 : :
1062 : 1895 : resolve_common_vars (common_root->n.common, true);
1063 : :
1064 : : /* The common name is a global name - in Fortran 2003 also if it has a
1065 : : C binding name, since Fortran 2008 only the C binding name is a global
1066 : : identifier. */
1067 : 1895 : if (!common_root->n.common->binding_label
1068 : 1895 : || gfc_notification_std (GFC_STD_F2008))
1069 : : {
1070 : 3646 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1071 : 1823 : common_root->n.common->name);
1072 : :
1073 : 820 : if (gsym && gfc_notification_std (GFC_STD_F2008)
1074 : 14 : && gsym->type == GSYM_COMMON
1075 : 1836 : && ((common_root->n.common->binding_label
1076 : 6 : && (!gsym->binding_label
1077 : 0 : || strcmp (common_root->n.common->binding_label,
1078 : : gsym->binding_label) != 0))
1079 : 7 : || (!common_root->n.common->binding_label
1080 : 7 : && gsym->binding_label)))
1081 : : {
1082 : 6 : gfc_error ("In Fortran 2003 COMMON %qs block at %L is a global "
1083 : : "identifier and must thus have the same binding name "
1084 : : "as the same-named COMMON block at %L: %s vs %s",
1085 : 6 : common_root->n.common->name, &common_root->n.common->where,
1086 : : &gsym->where,
1087 : : common_root->n.common->binding_label
1088 : : ? common_root->n.common->binding_label : "(blank)",
1089 : 6 : gsym->binding_label ? gsym->binding_label : "(blank)");
1090 : 6 : return;
1091 : : }
1092 : :
1093 : 1817 : if (gsym && gsym->type != GSYM_COMMON
1094 : 1 : && !common_root->n.common->binding_label)
1095 : : {
1096 : 0 : gfc_error ("COMMON block %qs at %L uses the same global identifier "
1097 : : "as entity at %L",
1098 : 0 : common_root->n.common->name, &common_root->n.common->where,
1099 : : &gsym->where);
1100 : 0 : return;
1101 : : }
1102 : 814 : if (gsym && gsym->type != GSYM_COMMON)
1103 : : {
1104 : 1 : gfc_error ("Fortran 2008: COMMON block %qs with binding label at "
1105 : : "%L sharing the identifier with global non-COMMON-block "
1106 : 1 : "entity at %L", common_root->n.common->name,
1107 : 1 : &common_root->n.common->where, &gsym->where);
1108 : 1 : return;
1109 : : }
1110 : 1003 : if (!gsym)
1111 : : {
1112 : 1003 : gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1113 : 1003 : gsym->type = GSYM_COMMON;
1114 : 1003 : gsym->where = common_root->n.common->where;
1115 : 1003 : gsym->defined = 1;
1116 : : }
1117 : 1816 : gsym->used = 1;
1118 : : }
1119 : :
1120 : 1888 : if (common_root->n.common->binding_label)
1121 : : {
1122 : 76 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1123 : : common_root->n.common->binding_label);
1124 : 76 : if (gsym && gsym->type != GSYM_COMMON)
1125 : : {
1126 : 1 : gfc_error ("COMMON block at %L with binding label %qs uses the same "
1127 : : "global identifier as entity at %L",
1128 : : &common_root->n.common->where,
1129 : 1 : common_root->n.common->binding_label, &gsym->where);
1130 : 1 : return;
1131 : : }
1132 : 57 : if (!gsym)
1133 : : {
1134 : 57 : gsym = gfc_get_gsymbol (common_root->n.common->binding_label, true);
1135 : 57 : gsym->type = GSYM_COMMON;
1136 : 57 : gsym->where = common_root->n.common->where;
1137 : 57 : gsym->defined = 1;
1138 : : }
1139 : 75 : gsym->used = 1;
1140 : : }
1141 : :
1142 : 1887 : gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1143 : 1887 : if (sym == NULL)
1144 : : return;
1145 : :
1146 : 122 : if (sym->attr.flavor == FL_PARAMETER)
1147 : 2 : gfc_error ("COMMON block %qs at %L is used as PARAMETER at %L",
1148 : 2 : sym->name, &common_root->n.common->where, &sym->declared_at);
1149 : :
1150 : 122 : if (sym->attr.external)
1151 : 1 : gfc_error ("COMMON block %qs at %L cannot have the EXTERNAL attribute",
1152 : 1 : sym->name, &common_root->n.common->where);
1153 : :
1154 : 122 : if (sym->attr.intrinsic)
1155 : 2 : gfc_error ("COMMON block %qs at %L is also an intrinsic procedure",
1156 : 2 : sym->name, &common_root->n.common->where);
1157 : 120 : else if (sym->attr.result
1158 : 120 : || gfc_is_function_return_value (sym, gfc_current_ns))
1159 : 1 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1160 : : "that is also a function result", sym->name,
1161 : 1 : &common_root->n.common->where);
1162 : 119 : else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
1163 : 5 : && sym->attr.proc != PROC_ST_FUNCTION)
1164 : 3 : gfc_notify_std (GFC_STD_F2003, "COMMON block %qs at %L "
1165 : : "that is also a global procedure", sym->name,
1166 : 3 : &common_root->n.common->where);
1167 : : }
1168 : :
1169 : :
1170 : : /* Resolve contained function types. Because contained functions can call one
1171 : : another, they have to be worked out before any of the contained procedures
1172 : : can be resolved.
1173 : :
1174 : : The good news is that if a function doesn't already have a type, the only
1175 : : way it can get one is through an IMPLICIT type or a RESULT variable, because
1176 : : by definition contained functions are contained namespace they're contained
1177 : : in, not in a sibling or parent namespace. */
1178 : :
1179 : : static void
1180 : 329458 : resolve_contained_functions (gfc_namespace *ns)
1181 : : {
1182 : 329458 : gfc_namespace *child;
1183 : 329458 : gfc_entry_list *el;
1184 : :
1185 : 329458 : resolve_formal_arglists (ns);
1186 : :
1187 : 364543 : for (child = ns->contained; child; child = child->sibling)
1188 : : {
1189 : : /* Resolve alternate entry points first. */
1190 : 35085 : resolve_entries (child);
1191 : :
1192 : : /* Then check function return types. */
1193 : 35085 : resolve_contained_fntype (child->proc_name, child);
1194 : 35590 : for (el = child->entries; el; el = el->next)
1195 : 505 : resolve_contained_fntype (el->sym, child);
1196 : : }
1197 : 329458 : }
1198 : :
1199 : :
1200 : :
1201 : : /* A Parameterized Derived Type constructor must contain values for
1202 : : the PDT KIND parameters or they must have a default initializer.
1203 : : Go through the constructor picking out the KIND expressions,
1204 : : storing them in 'param_list' and then call gfc_get_pdt_instance
1205 : : to obtain the PDT instance. */
1206 : :
1207 : : static gfc_actual_arglist *param_list, *param_tail, *param;
1208 : :
1209 : : static bool
1210 : 24 : get_pdt_spec_expr (gfc_component *c, gfc_expr *expr)
1211 : : {
1212 : 24 : param = gfc_get_actual_arglist ();
1213 : 24 : if (!param_list)
1214 : 18 : param_list = param_tail = param;
1215 : : else
1216 : : {
1217 : 6 : param_tail->next = param;
1218 : 6 : param_tail = param_tail->next;
1219 : : }
1220 : :
1221 : 24 : param_tail->name = c->name;
1222 : 24 : if (expr)
1223 : 24 : param_tail->expr = gfc_copy_expr (expr);
1224 : 0 : else if (c->initializer)
1225 : 0 : param_tail->expr = gfc_copy_expr (c->initializer);
1226 : : else
1227 : : {
1228 : 0 : param_tail->spec_type = SPEC_ASSUMED;
1229 : 0 : if (c->attr.pdt_kind)
1230 : : {
1231 : 0 : gfc_error ("The KIND parameter %qs in the PDT constructor "
1232 : : "at %C has no value", param->name);
1233 : 0 : return false;
1234 : : }
1235 : : }
1236 : :
1237 : : return true;
1238 : : }
1239 : :
1240 : : static bool
1241 : 18 : get_pdt_constructor (gfc_expr *expr, gfc_constructor **constr,
1242 : : gfc_symbol *derived)
1243 : : {
1244 : 18 : gfc_constructor *cons = NULL;
1245 : 18 : gfc_component *comp;
1246 : 18 : bool t = true;
1247 : :
1248 : 18 : if (expr && expr->expr_type == EXPR_STRUCTURE)
1249 : 18 : cons = gfc_constructor_first (expr->value.constructor);
1250 : 0 : else if (constr)
1251 : 0 : cons = *constr;
1252 : 18 : gcc_assert (cons);
1253 : :
1254 : 18 : comp = derived->components;
1255 : :
1256 : 66 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1257 : : {
1258 : 48 : if (cons->expr
1259 : 48 : && cons->expr->expr_type == EXPR_STRUCTURE
1260 : 0 : && comp->ts.type == BT_DERIVED)
1261 : : {
1262 : 0 : t = get_pdt_constructor (cons->expr, NULL, comp->ts.u.derived);
1263 : 0 : if (!t)
1264 : : return t;
1265 : : }
1266 : 48 : else if (comp->ts.type == BT_DERIVED)
1267 : : {
1268 : 0 : t = get_pdt_constructor (NULL, &cons, comp->ts.u.derived);
1269 : 0 : if (!t)
1270 : : return t;
1271 : : }
1272 : 48 : else if ((comp->attr.pdt_kind || comp->attr.pdt_len)
1273 : 24 : && derived->attr.pdt_template)
1274 : : {
1275 : 24 : t = get_pdt_spec_expr (comp, cons->expr);
1276 : 24 : if (!t)
1277 : : return t;
1278 : : }
1279 : : }
1280 : : return t;
1281 : : }
1282 : :
1283 : :
1284 : : static bool resolve_fl_derived0 (gfc_symbol *sym);
1285 : : static bool resolve_fl_struct (gfc_symbol *sym);
1286 : :
1287 : :
1288 : : /* Resolve all of the elements of a structure constructor and make sure that
1289 : : the types are correct. The 'init' flag indicates that the given
1290 : : constructor is an initializer. */
1291 : :
1292 : : static bool
1293 : 59367 : resolve_structure_cons (gfc_expr *expr, int init)
1294 : : {
1295 : 59367 : gfc_constructor *cons;
1296 : 59367 : gfc_component *comp;
1297 : 59367 : bool t;
1298 : 59367 : symbol_attribute a;
1299 : :
1300 : 59367 : t = true;
1301 : :
1302 : 59367 : if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1303 : : {
1304 : 56599 : if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1305 : 56449 : resolve_fl_derived0 (expr->ts.u.derived);
1306 : : else
1307 : 150 : resolve_fl_struct (expr->ts.u.derived);
1308 : :
1309 : : /* If this is a Parameterized Derived Type template, find the
1310 : : instance corresponding to the PDT kind parameters. */
1311 : 56599 : if (expr->ts.u.derived->attr.pdt_template)
1312 : : {
1313 : 18 : param_list = NULL;
1314 : 18 : t = get_pdt_constructor (expr, NULL, expr->ts.u.derived);
1315 : 18 : if (!t)
1316 : : return t;
1317 : 18 : gfc_get_pdt_instance (param_list, &expr->ts.u.derived, NULL);
1318 : :
1319 : 18 : expr->param_list = gfc_copy_actual_arglist (param_list);
1320 : :
1321 : 18 : if (param_list)
1322 : 18 : gfc_free_actual_arglist (param_list);
1323 : :
1324 : 18 : if (!expr->ts.u.derived->attr.pdt_type)
1325 : : return false;
1326 : : }
1327 : : }
1328 : :
1329 : : /* A constructor may have references if it is the result of substituting a
1330 : : parameter variable. In this case we just pull out the component we
1331 : : want. */
1332 : 59367 : if (expr->ref)
1333 : 160 : comp = expr->ref->u.c.sym->components;
1334 : 59207 : else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1335 : : || expr->ts.type == BT_UNION)
1336 : 59205 : && expr->ts.u.derived)
1337 : 59205 : comp = expr->ts.u.derived->components;
1338 : : else
1339 : : return false;
1340 : :
1341 : 59365 : cons = gfc_constructor_first (expr->value.constructor);
1342 : :
1343 : 195535 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1344 : : {
1345 : 136172 : int rank;
1346 : :
1347 : 136172 : if (!cons->expr)
1348 : 9019 : continue;
1349 : :
1350 : : /* Unions use an EXPR_NULL contrived expression to tell the translation
1351 : : phase to generate an initializer of the appropriate length.
1352 : : Ignore it here. */
1353 : 127153 : if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1354 : 15 : continue;
1355 : :
1356 : 127138 : if (!gfc_resolve_expr (cons->expr))
1357 : : {
1358 : 0 : t = false;
1359 : 0 : continue;
1360 : : }
1361 : :
1362 : 127138 : rank = comp->as ? comp->as->rank : 0;
1363 : 127138 : if (comp->ts.type == BT_CLASS
1364 : 1693 : && !comp->ts.u.derived->attr.unlimited_polymorphic
1365 : 1692 : && CLASS_DATA (comp)->as)
1366 : 511 : rank = CLASS_DATA (comp)->as->rank;
1367 : :
1368 : 127138 : if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1369 : 191 : gfc_find_vtab (&cons->expr->ts);
1370 : :
1371 : 127138 : if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
1372 : 452 : && (comp->attr.allocatable || comp->attr.pointer || cons->expr->rank))
1373 : : {
1374 : 4 : gfc_error ("The rank of the element in the structure "
1375 : : "constructor at %L does not match that of the "
1376 : : "component (%d/%d)", &cons->expr->where,
1377 : : cons->expr->rank, rank);
1378 : 4 : t = false;
1379 : : }
1380 : :
1381 : : /* If we don't have the right type, try to convert it. */
1382 : :
1383 : 222657 : if (!comp->attr.proc_pointer &&
1384 : 95519 : !gfc_compare_types (&cons->expr->ts, &comp->ts))
1385 : : {
1386 : 11539 : if (strcmp (comp->name, "_extends") == 0)
1387 : : {
1388 : : /* Can afford to be brutal with the _extends initializer.
1389 : : The derived type can get lost because it is PRIVATE
1390 : : but it is not usage constrained by the standard. */
1391 : 8385 : cons->expr->ts = comp->ts;
1392 : : }
1393 : 3154 : else if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
1394 : : {
1395 : 2 : gfc_error ("The element in the structure constructor at %L, "
1396 : : "for pointer component %qs, is %s but should be %s",
1397 : 2 : &cons->expr->where, comp->name,
1398 : 2 : gfc_basic_typename (cons->expr->ts.type),
1399 : : gfc_basic_typename (comp->ts.type));
1400 : 2 : t = false;
1401 : : }
1402 : 3152 : else if (!UNLIMITED_POLY (comp))
1403 : : {
1404 : 3114 : bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1405 : 3114 : if (t)
1406 : 127138 : t = t2;
1407 : : }
1408 : : }
1409 : :
1410 : : /* For strings, the length of the constructor should be the same as
1411 : : the one of the structure, ensure this if the lengths are known at
1412 : : compile time and when we are dealing with PARAMETER or structure
1413 : : constructors. */
1414 : 127138 : if (cons->expr->ts.type == BT_CHARACTER
1415 : 3812 : && comp->ts.type == BT_CHARACTER
1416 : 3793 : && comp->ts.u.cl && comp->ts.u.cl->length
1417 : 2465 : && comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
1418 : 2436 : && cons->expr->ts.u.cl && cons->expr->ts.u.cl->length
1419 : 920 : && cons->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT
1420 : 920 : && cons->expr->ts.u.cl->length->ts.type == BT_INTEGER
1421 : 920 : && comp->ts.u.cl->length->ts.type == BT_INTEGER
1422 : 920 : && mpz_cmp (cons->expr->ts.u.cl->length->value.integer,
1423 : 920 : comp->ts.u.cl->length->value.integer) != 0)
1424 : : {
1425 : 11 : if (comp->attr.pointer)
1426 : : {
1427 : 3 : HOST_WIDE_INT la, lb;
1428 : 3 : la = gfc_mpz_get_hwi (comp->ts.u.cl->length->value.integer);
1429 : 3 : lb = gfc_mpz_get_hwi (cons->expr->ts.u.cl->length->value.integer);
1430 : 3 : gfc_error ("Unequal character lengths (%wd/%wd) for pointer "
1431 : : "component %qs in constructor at %L",
1432 : 3 : la, lb, comp->name, &cons->expr->where);
1433 : 3 : t = false;
1434 : : }
1435 : :
1436 : 11 : if (cons->expr->expr_type == EXPR_VARIABLE
1437 : 4 : && cons->expr->rank != 0
1438 : 2 : && cons->expr->symtree->n.sym->attr.flavor == FL_PARAMETER)
1439 : : {
1440 : : /* Wrap the parameter in an array constructor (EXPR_ARRAY)
1441 : : to make use of the gfc_resolve_character_array_constructor
1442 : : machinery. The expression is later simplified away to
1443 : : an array of string literals. */
1444 : 1 : gfc_expr *para = cons->expr;
1445 : 1 : cons->expr = gfc_get_expr ();
1446 : 1 : cons->expr->ts = para->ts;
1447 : 1 : cons->expr->where = para->where;
1448 : 1 : cons->expr->expr_type = EXPR_ARRAY;
1449 : 1 : cons->expr->rank = para->rank;
1450 : 1 : cons->expr->corank = para->corank;
1451 : 1 : cons->expr->shape = gfc_copy_shape (para->shape, para->rank);
1452 : 1 : gfc_constructor_append_expr (&cons->expr->value.constructor,
1453 : 1 : para, &cons->expr->where);
1454 : : }
1455 : :
1456 : 11 : if (cons->expr->expr_type == EXPR_ARRAY)
1457 : : {
1458 : : /* Rely on the cleanup of the namespace to deal correctly with
1459 : : the old charlen. (There was a block here that attempted to
1460 : : remove the charlen but broke the chain in so doing.) */
1461 : 5 : cons->expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1462 : 5 : cons->expr->ts.u.cl->length_from_typespec = true;
1463 : 5 : cons->expr->ts.u.cl->length = gfc_copy_expr (comp->ts.u.cl->length);
1464 : 5 : gfc_resolve_character_array_constructor (cons->expr);
1465 : : }
1466 : : }
1467 : :
1468 : 127138 : if (cons->expr->expr_type == EXPR_NULL
1469 : 37782 : && !(comp->attr.pointer || comp->attr.allocatable
1470 : 19171 : || comp->attr.proc_pointer || comp->ts.f90_type == BT_VOID
1471 : 1079 : || (comp->ts.type == BT_CLASS
1472 : 1077 : && (CLASS_DATA (comp)->attr.class_pointer
1473 : 866 : || CLASS_DATA (comp)->attr.allocatable))))
1474 : : {
1475 : 2 : t = false;
1476 : 2 : gfc_error ("The NULL in the structure constructor at %L is "
1477 : : "being applied to component %qs, which is neither "
1478 : : "a POINTER nor ALLOCATABLE", &cons->expr->where,
1479 : : comp->name);
1480 : : }
1481 : :
1482 : 127138 : if (comp->attr.proc_pointer && comp->ts.interface)
1483 : : {
1484 : : /* Check procedure pointer interface. */
1485 : 13804 : gfc_symbol *s2 = NULL;
1486 : 13804 : gfc_component *c2;
1487 : 13804 : const char *name;
1488 : 13804 : char err[200];
1489 : :
1490 : 13804 : c2 = gfc_get_proc_ptr_comp (cons->expr);
1491 : 13804 : if (c2)
1492 : : {
1493 : 12 : s2 = c2->ts.interface;
1494 : 12 : name = c2->name;
1495 : : }
1496 : 13792 : else if (cons->expr->expr_type == EXPR_FUNCTION)
1497 : : {
1498 : 0 : s2 = cons->expr->symtree->n.sym->result;
1499 : 0 : name = cons->expr->symtree->n.sym->result->name;
1500 : : }
1501 : 13792 : else if (cons->expr->expr_type != EXPR_NULL)
1502 : : {
1503 : 13422 : s2 = cons->expr->symtree->n.sym;
1504 : 13422 : name = cons->expr->symtree->n.sym->name;
1505 : : }
1506 : :
1507 : 13434 : if (s2 && !gfc_compare_interfaces (comp->ts.interface, s2, name, 0, 1,
1508 : : err, sizeof (err), NULL, NULL))
1509 : : {
1510 : 2 : gfc_error_opt (0, "Interface mismatch for procedure-pointer "
1511 : : "component %qs in structure constructor at %L:"
1512 : 2 : " %s", comp->name, &cons->expr->where, err);
1513 : 2 : return false;
1514 : : }
1515 : : }
1516 : :
1517 : : /* Validate shape, except for dynamic or PDT arrays. */
1518 : 127136 : if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank
1519 : 1970 : && comp->as && !comp->attr.allocatable && !comp->attr.pointer
1520 : 1281 : && !comp->attr.pdt_array)
1521 : : {
1522 : 1221 : mpz_t len;
1523 : 1221 : mpz_init (len);
1524 : 2539 : for (int n = 0; n < rank; n++)
1525 : : {
1526 : 1319 : if (comp->as->upper[n]->expr_type != EXPR_CONSTANT
1527 : 1318 : || comp->as->lower[n]->expr_type != EXPR_CONSTANT)
1528 : : {
1529 : 1 : gfc_error ("Bad array spec of component %qs referenced in "
1530 : : "structure constructor at %L",
1531 : 1 : comp->name, &cons->expr->where);
1532 : 1 : t = false;
1533 : 1 : break;
1534 : 1318 : };
1535 : 1318 : if (cons->expr->shape == NULL)
1536 : 12 : continue;
1537 : 1306 : mpz_set_ui (len, 1);
1538 : 1306 : mpz_add (len, len, comp->as->upper[n]->value.integer);
1539 : 1306 : mpz_sub (len, len, comp->as->lower[n]->value.integer);
1540 : 1306 : if (mpz_cmp (cons->expr->shape[n], len) != 0)
1541 : : {
1542 : 9 : gfc_error ("The shape of component %qs in the structure "
1543 : : "constructor at %L differs from the shape of the "
1544 : : "declared component for dimension %d (%ld/%ld)",
1545 : : comp->name, &cons->expr->where, n+1,
1546 : : mpz_get_si (cons->expr->shape[n]),
1547 : : mpz_get_si (len));
1548 : 9 : t = false;
1549 : : }
1550 : : }
1551 : 1221 : mpz_clear (len);
1552 : : }
1553 : :
1554 : 127136 : if (!comp->attr.pointer || comp->attr.proc_pointer
1555 : 20339 : || cons->expr->expr_type == EXPR_NULL)
1556 : 117798 : continue;
1557 : :
1558 : 9338 : a = gfc_expr_attr (cons->expr);
1559 : :
1560 : 9338 : if (!a.pointer && !a.target)
1561 : : {
1562 : 1 : t = false;
1563 : 1 : gfc_error ("The element in the structure constructor at %L, "
1564 : : "for pointer component %qs should be a POINTER or "
1565 : 1 : "a TARGET", &cons->expr->where, comp->name);
1566 : : }
1567 : :
1568 : 9338 : if (init)
1569 : : {
1570 : : /* F08:C461. Additional checks for pointer initialization. */
1571 : 9270 : if (a.allocatable)
1572 : : {
1573 : 0 : t = false;
1574 : 0 : gfc_error ("Pointer initialization target at %L "
1575 : 0 : "must not be ALLOCATABLE", &cons->expr->where);
1576 : : }
1577 : 9270 : if (!a.save)
1578 : : {
1579 : 0 : t = false;
1580 : 0 : gfc_error ("Pointer initialization target at %L "
1581 : 0 : "must have the SAVE attribute", &cons->expr->where);
1582 : : }
1583 : : }
1584 : :
1585 : : /* F2023:C770: A designator that is an initial-data-target shall ...
1586 : : not have a vector subscript. */
1587 : 9338 : if (comp->attr.pointer && (a.pointer || a.target)
1588 : 18675 : && gfc_has_vector_index (cons->expr))
1589 : : {
1590 : 1 : gfc_error ("Pointer assignment target at %L has a vector subscript",
1591 : 1 : &cons->expr->where);
1592 : 1 : t = false;
1593 : : }
1594 : :
1595 : : /* F2003, C1272 (3). */
1596 : 9338 : bool impure = cons->expr->expr_type == EXPR_VARIABLE
1597 : 9338 : && (gfc_impure_variable (cons->expr->symtree->n.sym)
1598 : 9302 : || gfc_is_coindexed (cons->expr));
1599 : 33 : if (impure && gfc_pure (NULL))
1600 : : {
1601 : 1 : t = false;
1602 : 1 : gfc_error ("Invalid expression in the structure constructor for "
1603 : : "pointer component %qs at %L in PURE procedure",
1604 : 1 : comp->name, &cons->expr->where);
1605 : : }
1606 : :
1607 : 9338 : if (impure)
1608 : 33 : gfc_unset_implicit_pure (NULL);
1609 : : }
1610 : :
1611 : : return t;
1612 : : }
1613 : :
1614 : :
1615 : : /****************** Expression name resolution ******************/
1616 : :
1617 : : /* Returns 0 if a symbol was not declared with a type or
1618 : : attribute declaration statement, nonzero otherwise. */
1619 : :
1620 : : static bool
1621 : 724684 : was_declared (gfc_symbol *sym)
1622 : : {
1623 : 724684 : symbol_attribute a;
1624 : :
1625 : 724684 : a = sym->attr;
1626 : :
1627 : 724684 : if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1628 : : return 1;
1629 : :
1630 : 614419 : if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1631 : 606205 : || a.optional || a.pointer || a.save || a.target || a.volatile_
1632 : 606203 : || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1633 : 606149 : || a.asynchronous || a.codimension)
1634 : : return 1;
1635 : :
1636 : : return 0;
1637 : : }
1638 : :
1639 : :
1640 : : /* Determine if a symbol is generic or not. */
1641 : :
1642 : : static int
1643 : 402301 : generic_sym (gfc_symbol *sym)
1644 : : {
1645 : 402301 : gfc_symbol *s;
1646 : :
1647 : 402301 : if (sym->attr.generic ||
1648 : 374454 : (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1649 : 28910 : return 1;
1650 : :
1651 : 373391 : if (was_declared (sym) || sym->ns->parent == NULL)
1652 : : return 0;
1653 : :
1654 : 77182 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1655 : :
1656 : 77182 : if (s != NULL)
1657 : : {
1658 : 1264 : if (s == sym)
1659 : : return 0;
1660 : : else
1661 : 1253 : return generic_sym (s);
1662 : : }
1663 : :
1664 : : return 0;
1665 : : }
1666 : :
1667 : :
1668 : : /* Determine if a symbol is specific or not. */
1669 : :
1670 : : static int
1671 : 372196 : specific_sym (gfc_symbol *sym)
1672 : : {
1673 : 372196 : gfc_symbol *s;
1674 : :
1675 : 372196 : if (sym->attr.if_source == IFSRC_IFBODY
1676 : 361298 : || sym->attr.proc == PROC_MODULE
1677 : : || sym->attr.proc == PROC_INTERNAL
1678 : : || sym->attr.proc == PROC_ST_FUNCTION
1679 : 286975 : || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1680 : 658440 : || sym->attr.external)
1681 : 88333 : return 1;
1682 : :
1683 : 283863 : if (was_declared (sym) || sym->ns->parent == NULL)
1684 : : return 0;
1685 : :
1686 : 75639 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1687 : :
1688 : 75639 : return (s == NULL) ? 0 : specific_sym (s);
1689 : : }
1690 : :
1691 : :
1692 : : /* Figure out if the procedure is specific, generic or unknown. */
1693 : :
1694 : : enum proc_type
1695 : : { PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN };
1696 : :
1697 : : static proc_type
1698 : 400902 : procedure_kind (gfc_symbol *sym)
1699 : : {
1700 : 400902 : if (generic_sym (sym))
1701 : : return PTYPE_GENERIC;
1702 : :
1703 : 372135 : if (specific_sym (sym))
1704 : 88333 : return PTYPE_SPECIFIC;
1705 : :
1706 : : return PTYPE_UNKNOWN;
1707 : : }
1708 : :
1709 : : /* Check references to assumed size arrays. The flag need_full_assumed_size
1710 : : is nonzero when matching actual arguments. */
1711 : :
1712 : : static int need_full_assumed_size = 0;
1713 : :
1714 : : static bool
1715 : 1381649 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1716 : : {
1717 : 1381649 : if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
1718 : : return false;
1719 : :
1720 : : /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1721 : : What should it be? */
1722 : 3782 : if (e->ref
1723 : 3780 : && e->ref->u.ar.as
1724 : 3779 : && (e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1725 : 3284 : && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
1726 : 3284 : && (e->ref->u.ar.type == AR_FULL))
1727 : : {
1728 : 25 : gfc_error ("The upper bound in the last dimension must "
1729 : : "appear in the reference to the assumed size "
1730 : : "array %qs at %L", sym->name, &e->where);
1731 : 25 : return true;
1732 : : }
1733 : : return false;
1734 : : }
1735 : :
1736 : :
1737 : : /* Look for bad assumed size array references in argument expressions
1738 : : of elemental and array valued intrinsic procedures. Since this is
1739 : : called from procedure resolution functions, it only recurses at
1740 : : operators. */
1741 : :
1742 : : static bool
1743 : 223549 : resolve_assumed_size_actual (gfc_expr *e)
1744 : : {
1745 : 223549 : if (e == NULL)
1746 : : return false;
1747 : :
1748 : 223070 : switch (e->expr_type)
1749 : : {
1750 : 107554 : case EXPR_VARIABLE:
1751 : 107554 : if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1752 : : return true;
1753 : : break;
1754 : :
1755 : 46909 : case EXPR_OP:
1756 : 46909 : if (resolve_assumed_size_actual (e->value.op.op1)
1757 : 46909 : || resolve_assumed_size_actual (e->value.op.op2))
1758 : 0 : return true;
1759 : : break;
1760 : :
1761 : : default:
1762 : : break;
1763 : : }
1764 : : return false;
1765 : : }
1766 : :
1767 : :
1768 : : /* Check a generic procedure, passed as an actual argument, to see if
1769 : : there is a matching specific name. If none, it is an error, and if
1770 : : more than one, the reference is ambiguous. */
1771 : : static int
1772 : 8 : count_specific_procs (gfc_expr *e)
1773 : : {
1774 : 8 : int n;
1775 : 8 : gfc_interface *p;
1776 : 8 : gfc_symbol *sym;
1777 : :
1778 : 8 : n = 0;
1779 : 8 : sym = e->symtree->n.sym;
1780 : :
1781 : 22 : for (p = sym->generic; p; p = p->next)
1782 : 14 : if (strcmp (sym->name, p->sym->name) == 0)
1783 : : {
1784 : 8 : e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1785 : : sym->name);
1786 : 8 : n++;
1787 : : }
1788 : :
1789 : 8 : if (n > 1)
1790 : 1 : gfc_error ("%qs at %L is ambiguous", e->symtree->n.sym->name,
1791 : : &e->where);
1792 : :
1793 : 8 : if (n == 0)
1794 : 1 : gfc_error ("GENERIC procedure %qs is not allowed as an actual "
1795 : : "argument at %L", sym->name, &e->where);
1796 : :
1797 : 8 : return n;
1798 : : }
1799 : :
1800 : :
1801 : : /* See if a call to sym could possibly be a not allowed RECURSION because of
1802 : : a missing RECURSIVE declaration. This means that either sym is the current
1803 : : context itself, or sym is the parent of a contained procedure calling its
1804 : : non-RECURSIVE containing procedure.
1805 : : This also works if sym is an ENTRY. */
1806 : :
1807 : : static bool
1808 : 144504 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1809 : : {
1810 : 144504 : gfc_symbol* proc_sym;
1811 : 144504 : gfc_symbol* context_proc;
1812 : 144504 : gfc_namespace* real_context;
1813 : :
1814 : 144504 : if (sym->attr.flavor == FL_PROGRAM
1815 : : || gfc_fl_struct (sym->attr.flavor))
1816 : : return false;
1817 : :
1818 : : /* If we've got an ENTRY, find real procedure. */
1819 : 144503 : if (sym->attr.entry && sym->ns->entries)
1820 : 45 : proc_sym = sym->ns->entries->sym;
1821 : : else
1822 : : proc_sym = sym;
1823 : :
1824 : : /* If sym is RECURSIVE, all is well of course. */
1825 : 144503 : if (proc_sym->attr.recursive || flag_recursive)
1826 : : return false;
1827 : :
1828 : : /* Find the context procedure's "real" symbol if it has entries.
1829 : : We look for a procedure symbol, so recurse on the parents if we don't
1830 : : find one (like in case of a BLOCK construct). */
1831 : 1646 : for (real_context = context; ; real_context = real_context->parent)
1832 : : {
1833 : : /* We should find something, eventually! */
1834 : 123993 : gcc_assert (real_context);
1835 : :
1836 : 123993 : context_proc = (real_context->entries ? real_context->entries->sym
1837 : : : real_context->proc_name);
1838 : :
1839 : : /* In some special cases, there may not be a proc_name, like for this
1840 : : invalid code:
1841 : : real(bad_kind()) function foo () ...
1842 : : when checking the call to bad_kind ().
1843 : : In these cases, we simply return here and assume that the
1844 : : call is ok. */
1845 : 123993 : if (!context_proc)
1846 : : return false;
1847 : :
1848 : 123729 : if (context_proc->attr.flavor != FL_LABEL)
1849 : : break;
1850 : : }
1851 : :
1852 : : /* A call from sym's body to itself is recursion, of course. */
1853 : 122083 : if (context_proc == proc_sym)
1854 : : return true;
1855 : :
1856 : : /* The same is true if context is a contained procedure and sym the
1857 : : containing one. */
1858 : 122069 : if (context_proc->attr.contained)
1859 : : {
1860 : 20314 : gfc_symbol* parent_proc;
1861 : :
1862 : 20314 : gcc_assert (context->parent);
1863 : 20314 : parent_proc = (context->parent->entries ? context->parent->entries->sym
1864 : : : context->parent->proc_name);
1865 : :
1866 : 20314 : if (parent_proc == proc_sym)
1867 : 9 : return true;
1868 : : }
1869 : :
1870 : : return false;
1871 : : }
1872 : :
1873 : :
1874 : : /* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1875 : : its typespec and formal argument list. */
1876 : :
1877 : : bool
1878 : 40893 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1879 : : {
1880 : 40893 : gfc_intrinsic_sym* isym = NULL;
1881 : 40893 : const char* symstd;
1882 : :
1883 : 40893 : if (sym->resolve_symbol_called >= 2)
1884 : : return true;
1885 : :
1886 : 31292 : sym->resolve_symbol_called = 2;
1887 : :
1888 : : /* Already resolved. */
1889 : 31292 : if (sym->from_intmod && sym->ts.type != BT_UNKNOWN)
1890 : : return true;
1891 : :
1892 : : /* We already know this one is an intrinsic, so we don't call
1893 : : gfc_is_intrinsic for full checking but rather use gfc_find_function and
1894 : : gfc_find_subroutine directly to check whether it is a function or
1895 : : subroutine. */
1896 : :
1897 : 23556 : if (sym->intmod_sym_id && sym->attr.subroutine)
1898 : : {
1899 : 8489 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1900 : 8489 : isym = gfc_intrinsic_subroutine_by_id (id);
1901 : 8489 : }
1902 : 15067 : else if (sym->intmod_sym_id)
1903 : : {
1904 : 11725 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1905 : 11725 : isym = gfc_intrinsic_function_by_id (id);
1906 : : }
1907 : 3342 : else if (!sym->attr.subroutine)
1908 : 3270 : isym = gfc_find_function (sym->name);
1909 : :
1910 : 23484 : if (isym && !sym->attr.subroutine)
1911 : : {
1912 : 14956 : if (sym->ts.type != BT_UNKNOWN && warn_surprising
1913 : 24 : && !sym->attr.implicit_type)
1914 : 10 : gfc_warning (OPT_Wsurprising,
1915 : : "Type specified for intrinsic function %qs at %L is"
1916 : : " ignored", sym->name, &sym->declared_at);
1917 : :
1918 : 18469 : if (!sym->attr.function &&
1919 : 3513 : !gfc_add_function(&sym->attr, sym->name, loc))
1920 : : return false;
1921 : :
1922 : 14956 : sym->ts = isym->ts;
1923 : : }
1924 : 8600 : else if (isym || (isym = gfc_find_subroutine (sym->name)))
1925 : : {
1926 : 8597 : if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1927 : : {
1928 : 1 : gfc_error ("Intrinsic subroutine %qs at %L shall not have a type"
1929 : : " specifier", sym->name, &sym->declared_at);
1930 : 1 : return false;
1931 : : }
1932 : :
1933 : 8631 : if (!sym->attr.subroutine &&
1934 : 35 : !gfc_add_subroutine(&sym->attr, sym->name, loc))
1935 : : return false;
1936 : : }
1937 : : else
1938 : : {
1939 : 3 : gfc_error ("%qs declared INTRINSIC at %L does not exist", sym->name,
1940 : : &sym->declared_at);
1941 : 3 : return false;
1942 : : }
1943 : :
1944 : 23551 : gfc_copy_formal_args_intr (sym, isym, NULL);
1945 : :
1946 : 23551 : sym->attr.pure = isym->pure;
1947 : 23551 : sym->attr.elemental = isym->elemental;
1948 : :
1949 : : /* Check it is actually available in the standard settings. */
1950 : 23551 : if (!gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at))
1951 : : {
1952 : 31 : gfc_error ("The intrinsic %qs declared INTRINSIC at %L is not "
1953 : : "available in the current standard settings but %s. Use "
1954 : : "an appropriate %<-std=*%> option or enable "
1955 : : "%<-fall-intrinsics%> in order to use it.",
1956 : : sym->name, &sym->declared_at, symstd);
1957 : 31 : return false;
1958 : : }
1959 : :
1960 : : return true;
1961 : : }
1962 : :
1963 : :
1964 : : /* Resolve a procedure expression, like passing it to a called procedure or as
1965 : : RHS for a procedure pointer assignment. */
1966 : :
1967 : : static bool
1968 : 1286333 : resolve_procedure_expression (gfc_expr* expr)
1969 : : {
1970 : 1286333 : gfc_symbol* sym;
1971 : :
1972 : 1286333 : if (expr->expr_type != EXPR_VARIABLE)
1973 : : return true;
1974 : 1286328 : gcc_assert (expr->symtree);
1975 : :
1976 : 1286328 : sym = expr->symtree->n.sym;
1977 : :
1978 : 1286328 : if (sym->attr.intrinsic)
1979 : 1346 : gfc_resolve_intrinsic (sym, &expr->where);
1980 : :
1981 : 1286328 : if (sym->attr.flavor != FL_PROCEDURE
1982 : 29369 : || (sym->attr.function && sym->result == sym))
1983 : : return true;
1984 : :
1985 : : /* A non-RECURSIVE procedure that is used as procedure expression within its
1986 : : own body is in danger of being called recursively. */
1987 : 13844 : if (is_illegal_recursion (sym, gfc_current_ns))
1988 : : {
1989 : 10 : if (sym->attr.use_assoc && expr->symtree->name[0] == '@')
1990 : 0 : gfc_warning (0, "Non-RECURSIVE procedure %qs from module %qs is"
1991 : : " possibly calling itself recursively in procedure %qs. "
1992 : : " Declare it RECURSIVE or use %<-frecursive%>",
1993 : 0 : sym->name, sym->module, gfc_current_ns->proc_name->name);
1994 : : else
1995 : 10 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
1996 : : " itself recursively. Declare it RECURSIVE or use"
1997 : : " %<-frecursive%>", sym->name, &expr->where);
1998 : : }
1999 : :
2000 : : return true;
2001 : : }
2002 : :
2003 : :
2004 : : /* Check that name is not a derived type. */
2005 : :
2006 : : static bool
2007 : 3100 : is_dt_name (const char *name)
2008 : : {
2009 : 3100 : gfc_symbol *dt_list, *dt_first;
2010 : :
2011 : 3100 : dt_list = dt_first = gfc_derived_types;
2012 : 5512 : for (; dt_list; dt_list = dt_list->dt_next)
2013 : : {
2014 : 3427 : if (strcmp(dt_list->name, name) == 0)
2015 : : return true;
2016 : 3424 : if (dt_first == dt_list->dt_next)
2017 : : break;
2018 : : }
2019 : : return false;
2020 : : }
2021 : :
2022 : :
2023 : : /* Resolve an actual argument list. Most of the time, this is just
2024 : : resolving the expressions in the list.
2025 : : The exception is that we sometimes have to decide whether arguments
2026 : : that look like procedure arguments are really simple variable
2027 : : references. */
2028 : :
2029 : : static bool
2030 : 414394 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2031 : : bool no_formal_args)
2032 : : {
2033 : 414394 : gfc_symbol *sym;
2034 : 414394 : gfc_symtree *parent_st;
2035 : 414394 : gfc_expr *e;
2036 : 414394 : gfc_component *comp;
2037 : 414394 : int save_need_full_assumed_size;
2038 : 414394 : bool return_value = false;
2039 : 414394 : bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2040 : :
2041 : 414394 : actual_arg = true;
2042 : 414394 : first_actual_arg = true;
2043 : :
2044 : 1067400 : for (; arg; arg = arg->next)
2045 : : {
2046 : 653105 : e = arg->expr;
2047 : 653105 : if (e == NULL)
2048 : : {
2049 : : /* Check the label is a valid branching target. */
2050 : 2334 : if (arg->label)
2051 : : {
2052 : 236 : if (arg->label->defined == ST_LABEL_UNKNOWN)
2053 : : {
2054 : 0 : gfc_error ("Label %d referenced at %L is never defined",
2055 : : arg->label->value, &arg->label->where);
2056 : 0 : goto cleanup;
2057 : : }
2058 : : }
2059 : 2334 : first_actual_arg = false;
2060 : 2334 : continue;
2061 : : }
2062 : :
2063 : 650771 : if (e->expr_type == EXPR_VARIABLE
2064 : 287076 : && e->symtree->n.sym->attr.generic
2065 : 8 : && no_formal_args
2066 : 650776 : && count_specific_procs (e) != 1)
2067 : 2 : goto cleanup;
2068 : :
2069 : 650769 : if (e->ts.type != BT_PROCEDURE)
2070 : : {
2071 : 580239 : save_need_full_assumed_size = need_full_assumed_size;
2072 : 580239 : if (e->expr_type != EXPR_VARIABLE)
2073 : 363695 : need_full_assumed_size = 0;
2074 : 580239 : if (!gfc_resolve_expr (e))
2075 : 60 : goto cleanup;
2076 : 580179 : need_full_assumed_size = save_need_full_assumed_size;
2077 : 580179 : goto argument_list;
2078 : : }
2079 : :
2080 : : /* See if the expression node should really be a variable reference. */
2081 : :
2082 : 70530 : sym = e->symtree->n.sym;
2083 : :
2084 : 70530 : if (sym->attr.flavor == FL_PROCEDURE && is_dt_name (sym->name))
2085 : : {
2086 : 3 : gfc_error ("Derived type %qs is used as an actual "
2087 : : "argument at %L", sym->name, &e->where);
2088 : 3 : goto cleanup;
2089 : : }
2090 : :
2091 : 70527 : if (sym->attr.flavor == FL_PROCEDURE
2092 : 67430 : || sym->attr.intrinsic
2093 : 67430 : || sym->attr.external)
2094 : : {
2095 : 3097 : int actual_ok;
2096 : :
2097 : : /* If a procedure is not already determined to be something else
2098 : : check if it is intrinsic. */
2099 : 3097 : if (gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
2100 : 1254 : sym->attr.intrinsic = 1;
2101 : :
2102 : 3097 : if (sym->attr.proc == PROC_ST_FUNCTION)
2103 : : {
2104 : 2 : gfc_error ("Statement function %qs at %L is not allowed as an "
2105 : : "actual argument", sym->name, &e->where);
2106 : : }
2107 : :
2108 : 6194 : actual_ok = gfc_intrinsic_actual_ok (sym->name,
2109 : 3097 : sym->attr.subroutine);
2110 : 3097 : if (sym->attr.intrinsic && actual_ok == 0)
2111 : : {
2112 : 0 : gfc_error ("Intrinsic %qs at %L is not allowed as an "
2113 : : "actual argument", sym->name, &e->where);
2114 : : }
2115 : :
2116 : 3097 : if (sym->attr.contained && !sym->attr.use_assoc
2117 : 404 : && sym->ns->proc_name->attr.flavor != FL_MODULE)
2118 : : {
2119 : 216 : if (!gfc_notify_std (GFC_STD_F2008, "Internal procedure %qs is"
2120 : : " used as actual argument at %L",
2121 : : sym->name, &e->where))
2122 : 3 : goto cleanup;
2123 : : }
2124 : :
2125 : 3094 : if (sym->attr.elemental && !sym->attr.intrinsic)
2126 : : {
2127 : 2 : gfc_error ("ELEMENTAL non-INTRINSIC procedure %qs is not "
2128 : : "allowed as an actual argument at %L", sym->name,
2129 : : &e->where);
2130 : : }
2131 : :
2132 : : /* Check if a generic interface has a specific procedure
2133 : : with the same name before emitting an error. */
2134 : 3094 : if (sym->attr.generic && count_specific_procs (e) != 1)
2135 : 0 : goto cleanup;
2136 : :
2137 : : /* Just in case a specific was found for the expression. */
2138 : 3094 : sym = e->symtree->n.sym;
2139 : :
2140 : : /* If the symbol is the function that names the current (or
2141 : : parent) scope, then we really have a variable reference. */
2142 : :
2143 : 3094 : if (gfc_is_function_return_value (sym, sym->ns))
2144 : 0 : goto got_variable;
2145 : :
2146 : : /* If all else fails, see if we have a specific intrinsic. */
2147 : 3094 : if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
2148 : : {
2149 : 0 : gfc_intrinsic_sym *isym;
2150 : :
2151 : 0 : isym = gfc_find_function (sym->name);
2152 : 0 : if (isym == NULL || !isym->specific)
2153 : : {
2154 : 0 : gfc_error ("Unable to find a specific INTRINSIC procedure "
2155 : : "for the reference %qs at %L", sym->name,
2156 : : &e->where);
2157 : 0 : goto cleanup;
2158 : : }
2159 : 0 : sym->ts = isym->ts;
2160 : 0 : sym->attr.intrinsic = 1;
2161 : 0 : sym->attr.function = 1;
2162 : : }
2163 : :
2164 : 3094 : if (!gfc_resolve_expr (e))
2165 : 0 : goto cleanup;
2166 : 3094 : goto argument_list;
2167 : : }
2168 : :
2169 : : /* See if the name is a module procedure in a parent unit. */
2170 : :
2171 : 67430 : if (was_declared (sym) || sym->ns->parent == NULL)
2172 : 67337 : goto got_variable;
2173 : :
2174 : 93 : if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
2175 : : {
2176 : 0 : gfc_error ("Symbol %qs at %L is ambiguous", sym->name, &e->where);
2177 : 0 : goto cleanup;
2178 : : }
2179 : :
2180 : 93 : if (parent_st == NULL)
2181 : 93 : goto got_variable;
2182 : :
2183 : 0 : sym = parent_st->n.sym;
2184 : 0 : e->symtree = parent_st; /* Point to the right thing. */
2185 : :
2186 : 0 : if (sym->attr.flavor == FL_PROCEDURE
2187 : 0 : || sym->attr.intrinsic
2188 : 0 : || sym->attr.external)
2189 : : {
2190 : 0 : if (!gfc_resolve_expr (e))
2191 : 0 : goto cleanup;
2192 : 0 : goto argument_list;
2193 : : }
2194 : :
2195 : 0 : got_variable:
2196 : 67430 : e->expr_type = EXPR_VARIABLE;
2197 : 67430 : e->ts = sym->ts;
2198 : 67430 : if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2199 : 34952 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2200 : 3756 : && CLASS_DATA (sym)->as))
2201 : : {
2202 : 37888 : gfc_array_spec *as
2203 : 35183 : = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2204 : 35183 : e->rank = as->rank;
2205 : 35183 : e->corank = as->corank;
2206 : 35183 : e->ref = gfc_get_ref ();
2207 : 35183 : e->ref->type = REF_ARRAY;
2208 : 35183 : e->ref->u.ar.type = AR_FULL;
2209 : 35183 : e->ref->u.ar.as = as;
2210 : : }
2211 : :
2212 : : /* These symbols are set untyped by calls to gfc_set_default_type
2213 : : with 'error_flag' = false. Reset the untyped attribute so that
2214 : : the error will be generated in gfc_resolve_expr. */
2215 : 67430 : if (e->expr_type == EXPR_VARIABLE
2216 : 67430 : && sym->ts.type == BT_UNKNOWN
2217 : 36 : && sym->attr.untyped)
2218 : 5 : sym->attr.untyped = 0;
2219 : :
2220 : : /* Expressions are assigned a default ts.type of BT_PROCEDURE in
2221 : : primary.cc (match_actual_arg). If above code determines that it
2222 : : is a variable instead, it needs to be resolved as it was not
2223 : : done at the beginning of this function. */
2224 : 67430 : save_need_full_assumed_size = need_full_assumed_size;
2225 : 67430 : if (e->expr_type != EXPR_VARIABLE)
2226 : 0 : need_full_assumed_size = 0;
2227 : 67430 : if (!gfc_resolve_expr (e))
2228 : 22 : goto cleanup;
2229 : 67408 : need_full_assumed_size = save_need_full_assumed_size;
2230 : :
2231 : 650681 : argument_list:
2232 : : /* Check argument list functions %VAL, %LOC and %REF. There is
2233 : : nothing to do for %REF. */
2234 : 650681 : if (arg->name && arg->name[0] == '%')
2235 : : {
2236 : 42 : if (strcmp ("%VAL", arg->name) == 0)
2237 : : {
2238 : 28 : if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
2239 : : {
2240 : 2 : gfc_error ("By-value argument at %L is not of numeric "
2241 : : "type", &e->where);
2242 : 2 : goto cleanup;
2243 : : }
2244 : :
2245 : 26 : if (e->rank)
2246 : : {
2247 : 1 : gfc_error ("By-value argument at %L cannot be an array or "
2248 : : "an array section", &e->where);
2249 : 1 : goto cleanup;
2250 : : }
2251 : :
2252 : : /* Intrinsics are still PROC_UNKNOWN here. However,
2253 : : since same file external procedures are not resolvable
2254 : : in gfortran, it is a good deal easier to leave them to
2255 : : intrinsic.cc. */
2256 : 25 : if (ptype != PROC_UNKNOWN
2257 : 25 : && ptype != PROC_DUMMY
2258 : 9 : && ptype != PROC_EXTERNAL
2259 : 9 : && ptype != PROC_MODULE)
2260 : : {
2261 : 3 : gfc_error ("By-value argument at %L is not allowed "
2262 : : "in this context", &e->where);
2263 : 3 : goto cleanup;
2264 : : }
2265 : : }
2266 : :
2267 : : /* Statement functions have already been excluded above. */
2268 : 14 : else if (strcmp ("%LOC", arg->name) == 0
2269 : 8 : && e->ts.type == BT_PROCEDURE)
2270 : : {
2271 : 0 : if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
2272 : : {
2273 : 0 : gfc_error ("Passing internal procedure at %L by location "
2274 : : "not allowed", &e->where);
2275 : 0 : goto cleanup;
2276 : : }
2277 : : }
2278 : : }
2279 : :
2280 : 650675 : comp = gfc_get_proc_ptr_comp(e);
2281 : 650675 : if (e->expr_type == EXPR_VARIABLE
2282 : 285698 : && comp && comp->attr.elemental)
2283 : : {
2284 : 1 : gfc_error ("ELEMENTAL procedure pointer component %qs is not "
2285 : : "allowed as an actual argument at %L", comp->name,
2286 : : &e->where);
2287 : : }
2288 : :
2289 : : /* Fortran 2008, C1237. */
2290 : 285698 : if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2291 : 650981 : && gfc_has_ultimate_pointer (e))
2292 : : {
2293 : 3 : gfc_error ("Coindexed actual argument at %L with ultimate pointer "
2294 : : "component", &e->where);
2295 : 3 : goto cleanup;
2296 : : }
2297 : :
2298 : 650672 : first_actual_arg = false;
2299 : : }
2300 : :
2301 : : return_value = true;
2302 : :
2303 : 414394 : cleanup:
2304 : 414394 : actual_arg = actual_arg_sav;
2305 : 414394 : first_actual_arg = first_actual_arg_sav;
2306 : :
2307 : 414394 : return return_value;
2308 : : }
2309 : :
2310 : :
2311 : : /* Do the checks of the actual argument list that are specific to elemental
2312 : : procedures. If called with c == NULL, we have a function, otherwise if
2313 : : expr == NULL, we have a subroutine. */
2314 : :
2315 : : static bool
2316 : 316073 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2317 : : {
2318 : 316073 : gfc_actual_arglist *arg0;
2319 : 316073 : gfc_actual_arglist *arg;
2320 : 316073 : gfc_symbol *esym = NULL;
2321 : 316073 : gfc_intrinsic_sym *isym = NULL;
2322 : 316073 : gfc_expr *e = NULL;
2323 : 316073 : gfc_intrinsic_arg *iformal = NULL;
2324 : 316073 : gfc_formal_arglist *eformal = NULL;
2325 : 316073 : bool formal_optional = false;
2326 : 316073 : bool set_by_optional = false;
2327 : 316073 : int i;
2328 : 316073 : int rank = 0;
2329 : :
2330 : : /* Is this an elemental procedure? */
2331 : 316073 : if (expr && expr->value.function.actual != NULL)
2332 : : {
2333 : 229480 : if (expr->value.function.esym != NULL
2334 : 42914 : && expr->value.function.esym->attr.elemental)
2335 : : {
2336 : : arg0 = expr->value.function.actual;
2337 : : esym = expr->value.function.esym;
2338 : : }
2339 : 213208 : else if (expr->value.function.isym != NULL
2340 : 185545 : && expr->value.function.isym->elemental)
2341 : : {
2342 : : arg0 = expr->value.function.actual;
2343 : : isym = expr->value.function.isym;
2344 : : }
2345 : : else
2346 : : return true;
2347 : : }
2348 : 86593 : else if (c && c->ext.actual != NULL)
2349 : : {
2350 : 68621 : arg0 = c->ext.actual;
2351 : :
2352 : 68621 : if (c->resolved_sym)
2353 : : esym = c->resolved_sym;
2354 : : else
2355 : 307 : esym = c->symtree->n.sym;
2356 : 68621 : gcc_assert (esym);
2357 : :
2358 : 68621 : if (!esym->attr.elemental)
2359 : : return true;
2360 : : }
2361 : : else
2362 : : return true;
2363 : :
2364 : : /* The rank of an elemental is the rank of its array argument(s). */
2365 : 171151 : for (arg = arg0; arg; arg = arg->next)
2366 : : {
2367 : 110909 : if (arg->expr != NULL && arg->expr->rank != 0)
2368 : : {
2369 : 10316 : rank = arg->expr->rank;
2370 : 10316 : if (arg->expr->expr_type == EXPR_VARIABLE
2371 : 5204 : && arg->expr->symtree->n.sym->attr.optional)
2372 : 10316 : set_by_optional = true;
2373 : :
2374 : : /* Function specific; set the result rank and shape. */
2375 : 10316 : if (expr)
2376 : : {
2377 : 8154 : expr->rank = rank;
2378 : 8154 : expr->corank = arg->expr->corank;
2379 : 8154 : if (!expr->shape && arg->expr->shape)
2380 : : {
2381 : 3899 : expr->shape = gfc_get_shape (rank);
2382 : 8590 : for (i = 0; i < rank; i++)
2383 : 4691 : mpz_init_set (expr->shape[i], arg->expr->shape[i]);
2384 : : }
2385 : : }
2386 : : break;
2387 : : }
2388 : : }
2389 : :
2390 : : /* If it is an array, it shall not be supplied as an actual argument
2391 : : to an elemental procedure unless an array of the same rank is supplied
2392 : : as an actual argument corresponding to a nonoptional dummy argument of
2393 : : that elemental procedure(12.4.1.5). */
2394 : 70558 : formal_optional = false;
2395 : 70558 : if (isym)
2396 : 48325 : iformal = isym->formal;
2397 : : else
2398 : 22233 : eformal = esym->formal;
2399 : :
2400 : 186728 : for (arg = arg0; arg; arg = arg->next)
2401 : : {
2402 : 116170 : if (eformal)
2403 : : {
2404 : 39879 : if (eformal->sym && eformal->sym->attr.optional)
2405 : 39879 : formal_optional = true;
2406 : 39879 : eformal = eformal->next;
2407 : : }
2408 : 76291 : else if (isym && iformal)
2409 : : {
2410 : 66303 : if (iformal->optional)
2411 : 13182 : formal_optional = true;
2412 : 66303 : iformal = iformal->next;
2413 : : }
2414 : 9988 : else if (isym)
2415 : 9980 : formal_optional = true;
2416 : :
2417 : 116170 : if (pedantic && arg->expr != NULL
2418 : 70915 : && arg->expr->expr_type == EXPR_VARIABLE
2419 : 34350 : && arg->expr->symtree->n.sym->attr.optional
2420 : 572 : && formal_optional
2421 : 479 : && arg->expr->rank
2422 : 153 : && (set_by_optional || arg->expr->rank != rank)
2423 : 42 : && !(isym && isym->id == GFC_ISYM_CONVERSION))
2424 : : {
2425 : 114 : bool t = false;
2426 : : gfc_actual_arglist *a;
2427 : :
2428 : : /* Scan the argument list for a non-optional argument with the
2429 : : same rank as arg. */
2430 : 114 : for (a = arg0; a; a = a->next)
2431 : 87 : if (a != arg
2432 : 45 : && a->expr->rank == arg->expr->rank
2433 : 39 : && (a->expr->expr_type != EXPR_VARIABLE
2434 : 37 : || (a->expr->expr_type == EXPR_VARIABLE
2435 : 37 : && !a->expr->symtree->n.sym->attr.optional)))
2436 : : {
2437 : : t = true;
2438 : : break;
2439 : : }
2440 : :
2441 : 42 : if (!t)
2442 : 27 : gfc_warning (OPT_Wpedantic,
2443 : : "%qs at %L is an array and OPTIONAL; If it is not "
2444 : : "present, then it cannot be the actual argument of "
2445 : : "an ELEMENTAL procedure unless there is a non-optional"
2446 : : " argument with the same rank "
2447 : : "(Fortran 2018, 15.5.2.12)",
2448 : : arg->expr->symtree->n.sym->name, &arg->expr->where);
2449 : : }
2450 : : }
2451 : :
2452 : 186717 : for (arg = arg0; arg; arg = arg->next)
2453 : : {
2454 : 116168 : if (arg->expr == NULL || arg->expr->rank == 0)
2455 : 103210 : continue;
2456 : :
2457 : : /* Being elemental, the last upper bound of an assumed size array
2458 : : argument must be present. */
2459 : 12958 : if (resolve_assumed_size_actual (arg->expr))
2460 : : return false;
2461 : :
2462 : : /* Elemental procedure's array actual arguments must conform. */
2463 : 12955 : if (e != NULL)
2464 : : {
2465 : 2642 : if (!gfc_check_conformance (arg->expr, e, _("elemental procedure")))
2466 : : return false;
2467 : : }
2468 : : else
2469 : 10313 : e = arg->expr;
2470 : : }
2471 : :
2472 : : /* INTENT(OUT) is only allowed for subroutines; if any actual argument
2473 : : is an array, the intent inout/out variable needs to be also an array. */
2474 : 70549 : if (rank > 0 && esym && expr == NULL)
2475 : 6613 : for (eformal = esym->formal, arg = arg0; arg && eformal;
2476 : 4457 : arg = arg->next, eformal = eformal->next)
2477 : 4459 : if (eformal->sym
2478 : 4458 : && (eformal->sym->attr.intent == INTENT_OUT
2479 : 3382 : || eformal->sym->attr.intent == INTENT_INOUT)
2480 : 1470 : && arg->expr && arg->expr->rank == 0)
2481 : : {
2482 : 2 : gfc_error ("Actual argument at %L for INTENT(%s) dummy %qs of "
2483 : : "ELEMENTAL subroutine %qs is a scalar, but another "
2484 : : "actual argument is an array", &arg->expr->where,
2485 : : (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
2486 : : : "INOUT", eformal->sym->name, esym->name);
2487 : 2 : return false;
2488 : : }
2489 : : return true;
2490 : : }
2491 : :
2492 : :
2493 : : /* This function does the checking of references to global procedures
2494 : : as defined in sections 18.1 and 14.1, respectively, of the Fortran
2495 : : 77 and 95 standards. It checks for a gsymbol for the name, making
2496 : : one if it does not already exist. If it already exists, then the
2497 : : reference being resolved must correspond to the type of gsymbol.
2498 : : Otherwise, the new symbol is equipped with the attributes of the
2499 : : reference. The corresponding code that is called in creating
2500 : : global entities is parse.cc.
2501 : :
2502 : : In addition, for all but -std=legacy, the gsymbols are used to
2503 : : check the interfaces of external procedures from the same file.
2504 : : The namespace of the gsymbol is resolved and then, once this is
2505 : : done the interface is checked. */
2506 : :
2507 : :
2508 : : static bool
2509 : 14804 : not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
2510 : : {
2511 : 14804 : if (!gsym_ns->proc_name->attr.recursive)
2512 : : return true;
2513 : :
2514 : 151 : if (sym->ns == gsym_ns)
2515 : : return false;
2516 : :
2517 : 151 : if (sym->ns->parent && sym->ns->parent == gsym_ns)
2518 : 0 : return false;
2519 : :
2520 : : return true;
2521 : : }
2522 : :
2523 : : static bool
2524 : 14804 : not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
2525 : : {
2526 : 14804 : if (gsym_ns->entries)
2527 : : {
2528 : : gfc_entry_list *entry = gsym_ns->entries;
2529 : :
2530 : 3234 : for (; entry; entry = entry->next)
2531 : : {
2532 : 2281 : if (strcmp (sym->name, entry->sym->name) == 0)
2533 : : {
2534 : 946 : if (strcmp (gsym_ns->proc_name->name,
2535 : 946 : sym->ns->proc_name->name) == 0)
2536 : : return false;
2537 : :
2538 : 946 : if (sym->ns->parent
2539 : 0 : && strcmp (gsym_ns->proc_name->name,
2540 : 0 : sym->ns->parent->proc_name->name) == 0)
2541 : : return false;
2542 : : }
2543 : : }
2544 : : }
2545 : : return true;
2546 : : }
2547 : :
2548 : :
2549 : : /* Check for the requirement of an explicit interface. F08:12.4.2.2. */
2550 : :
2551 : : bool
2552 : 15599 : gfc_explicit_interface_required (gfc_symbol *sym, char *errmsg, int err_len)
2553 : : {
2554 : 15599 : gfc_formal_arglist *arg = gfc_sym_get_dummy_args (sym);
2555 : :
2556 : 58552 : for ( ; arg; arg = arg->next)
2557 : : {
2558 : 27724 : if (!arg->sym)
2559 : 157 : continue;
2560 : :
2561 : 27567 : if (arg->sym->attr.allocatable) /* (2a) */
2562 : : {
2563 : 0 : strncpy (errmsg, _("allocatable argument"), err_len);
2564 : 0 : return true;
2565 : : }
2566 : 27567 : else if (arg->sym->attr.asynchronous)
2567 : : {
2568 : 0 : strncpy (errmsg, _("asynchronous argument"), err_len);
2569 : 0 : return true;
2570 : : }
2571 : 27567 : else if (arg->sym->attr.optional)
2572 : : {
2573 : 75 : strncpy (errmsg, _("optional argument"), err_len);
2574 : 75 : return true;
2575 : : }
2576 : 27492 : else if (arg->sym->attr.pointer)
2577 : : {
2578 : 12 : strncpy (errmsg, _("pointer argument"), err_len);
2579 : 12 : return true;
2580 : : }
2581 : 27480 : else if (arg->sym->attr.target)
2582 : : {
2583 : 72 : strncpy (errmsg, _("target argument"), err_len);
2584 : 72 : return true;
2585 : : }
2586 : 27408 : else if (arg->sym->attr.value)
2587 : : {
2588 : 0 : strncpy (errmsg, _("value argument"), err_len);
2589 : 0 : return true;
2590 : : }
2591 : 27408 : else if (arg->sym->attr.volatile_)
2592 : : {
2593 : 1 : strncpy (errmsg, _("volatile argument"), err_len);
2594 : 1 : return true;
2595 : : }
2596 : 27407 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_SHAPE) /* (2b) */
2597 : : {
2598 : 45 : strncpy (errmsg, _("assumed-shape argument"), err_len);
2599 : 45 : return true;
2600 : : }
2601 : 27362 : else if (arg->sym->as && arg->sym->as->type == AS_ASSUMED_RANK) /* TS 29113, 6.2. */
2602 : : {
2603 : 1 : strncpy (errmsg, _("assumed-rank argument"), err_len);
2604 : 1 : return true;
2605 : : }
2606 : 27361 : else if (arg->sym->attr.codimension) /* (2c) */
2607 : : {
2608 : 1 : strncpy (errmsg, _("coarray argument"), err_len);
2609 : 1 : return true;
2610 : : }
2611 : 27360 : else if (false) /* (2d) TODO: parametrized derived type */
2612 : : {
2613 : : strncpy (errmsg, _("parametrized derived type argument"), err_len);
2614 : : return true;
2615 : : }
2616 : 27360 : else if (arg->sym->ts.type == BT_CLASS) /* (2e) */
2617 : : {
2618 : 162 : strncpy (errmsg, _("polymorphic argument"), err_len);
2619 : 162 : return true;
2620 : : }
2621 : 27198 : else if (arg->sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
2622 : : {
2623 : 0 : strncpy (errmsg, _("NO_ARG_CHECK attribute"), err_len);
2624 : 0 : return true;
2625 : : }
2626 : 27198 : else if (arg->sym->ts.type == BT_ASSUMED)
2627 : : {
2628 : : /* As assumed-type is unlimited polymorphic (cf. above).
2629 : : See also TS 29113, Note 6.1. */
2630 : 1 : strncpy (errmsg, _("assumed-type argument"), err_len);
2631 : 1 : return true;
2632 : : }
2633 : : }
2634 : :
2635 : 15229 : if (sym->attr.function)
2636 : : {
2637 : 3431 : gfc_symbol *res = sym->result ? sym->result : sym;
2638 : :
2639 : 3431 : if (res->attr.dimension) /* (3a) */
2640 : : {
2641 : 93 : strncpy (errmsg, _("array result"), err_len);
2642 : 93 : return true;
2643 : : }
2644 : 3338 : else if (res->attr.pointer || res->attr.allocatable) /* (3b) */
2645 : : {
2646 : 38 : strncpy (errmsg, _("pointer or allocatable result"), err_len);
2647 : 38 : return true;
2648 : : }
2649 : 3300 : else if (res->ts.type == BT_CHARACTER && res->ts.u.cl
2650 : 347 : && res->ts.u.cl->length
2651 : 166 : && res->ts.u.cl->length->expr_type != EXPR_CONSTANT) /* (3c) */
2652 : : {
2653 : 12 : strncpy (errmsg, _("result with non-constant character length"), err_len);
2654 : 12 : return true;
2655 : : }
2656 : : }
2657 : :
2658 : 15086 : if (sym->attr.elemental && !sym->attr.intrinsic) /* (4) */
2659 : : {
2660 : 7 : strncpy (errmsg, _("elemental procedure"), err_len);
2661 : 7 : return true;
2662 : : }
2663 : 15079 : else if (sym->attr.is_bind_c) /* (5) */
2664 : : {
2665 : 0 : strncpy (errmsg, _("bind(c) procedure"), err_len);
2666 : 0 : return true;
2667 : : }
2668 : :
2669 : : return false;
2670 : : }
2671 : :
2672 : :
2673 : : static void
2674 : 28774 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2675 : : {
2676 : 28774 : gfc_gsymbol * gsym;
2677 : 28774 : gfc_namespace *ns;
2678 : 28774 : enum gfc_symbol_type type;
2679 : 28774 : char reason[200];
2680 : :
2681 : 28774 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2682 : :
2683 : 28774 : gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2684 : 28774 : sym->binding_label != NULL);
2685 : :
2686 : 28774 : if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2687 : 10 : gfc_global_used (gsym, where);
2688 : :
2689 : 28774 : if ((sym->attr.if_source == IFSRC_UNKNOWN
2690 : 8769 : || sym->attr.if_source == IFSRC_IFBODY)
2691 : 24793 : && gsym->type != GSYM_UNKNOWN
2692 : 22653 : && !gsym->binding_label
2693 : 20393 : && gsym->ns
2694 : 14804 : && gsym->ns->proc_name
2695 : 14804 : && not_in_recursive (sym, gsym->ns)
2696 : 43578 : && not_entry_self_reference (sym, gsym->ns))
2697 : : {
2698 : 14804 : gfc_symbol *def_sym;
2699 : 14804 : def_sym = gsym->ns->proc_name;
2700 : :
2701 : 14804 : if (gsym->ns->resolved != -1)
2702 : : {
2703 : :
2704 : : /* Resolve the gsymbol namespace if needed. */
2705 : 14783 : if (!gsym->ns->resolved)
2706 : : {
2707 : 2740 : gfc_symbol *old_dt_list;
2708 : :
2709 : : /* Stash away derived types so that the backend_decls
2710 : : do not get mixed up. */
2711 : 2740 : old_dt_list = gfc_derived_types;
2712 : 2740 : gfc_derived_types = NULL;
2713 : :
2714 : 2740 : gfc_resolve (gsym->ns);
2715 : :
2716 : : /* Store the new derived types with the global namespace. */
2717 : 2740 : if (gfc_derived_types)
2718 : 293 : gsym->ns->derived_types = gfc_derived_types;
2719 : :
2720 : : /* Restore the derived types of this namespace. */
2721 : 2740 : gfc_derived_types = old_dt_list;
2722 : : }
2723 : :
2724 : : /* Make sure that translation for the gsymbol occurs before
2725 : : the procedure currently being resolved. */
2726 : 14783 : ns = gfc_global_ns_list;
2727 : 25074 : for (; ns && ns != gsym->ns; ns = ns->sibling)
2728 : : {
2729 : 16714 : if (ns->sibling == gsym->ns)
2730 : : {
2731 : 6423 : ns->sibling = gsym->ns->sibling;
2732 : 6423 : gsym->ns->sibling = gfc_global_ns_list;
2733 : 6423 : gfc_global_ns_list = gsym->ns;
2734 : 6423 : break;
2735 : : }
2736 : : }
2737 : :
2738 : : /* This can happen if a binding name has been specified. */
2739 : 14783 : if (gsym->binding_label && gsym->sym_name != def_sym->name)
2740 : 0 : gfc_find_symbol (gsym->sym_name, gsym->ns, 0, &def_sym);
2741 : :
2742 : 14783 : if (def_sym->attr.entry_master || def_sym->attr.entry)
2743 : : {
2744 : 953 : gfc_entry_list *entry;
2745 : 1659 : for (entry = gsym->ns->entries; entry; entry = entry->next)
2746 : 1659 : if (strcmp (entry->sym->name, sym->name) == 0)
2747 : : {
2748 : 953 : def_sym = entry->sym;
2749 : 953 : break;
2750 : : }
2751 : : }
2752 : : }
2753 : :
2754 : 14804 : if (sym->attr.function && !gfc_compare_types (&sym->ts, &def_sym->ts))
2755 : : {
2756 : 6 : gfc_error ("Return type mismatch of function %qs at %L (%s/%s)",
2757 : : sym->name, &sym->declared_at, gfc_typename (&sym->ts),
2758 : 6 : gfc_typename (&def_sym->ts));
2759 : 29 : goto done;
2760 : : }
2761 : :
2762 : 14798 : if (sym->attr.if_source == IFSRC_UNKNOWN
2763 : 14798 : && gfc_explicit_interface_required (def_sym, reason, sizeof(reason)))
2764 : : {
2765 : 8 : gfc_error ("Explicit interface required for %qs at %L: %s",
2766 : : sym->name, &sym->declared_at, reason);
2767 : 8 : goto done;
2768 : : }
2769 : :
2770 : 14790 : bool bad_result_characteristics;
2771 : 14790 : if (!gfc_compare_interfaces (sym, def_sym, sym->name, 0, 1,
2772 : : reason, sizeof(reason), NULL, NULL,
2773 : : &bad_result_characteristics))
2774 : : {
2775 : : /* Turn erros into warnings with -std=gnu and -std=legacy,
2776 : : unless a function returns a wrong type, which can lead
2777 : : to all kinds of ICEs and wrong code. */
2778 : :
2779 : 15 : if (!pedantic && (gfc_option.allow_std & GFC_STD_GNU)
2780 : 2 : && !bad_result_characteristics)
2781 : 2 : gfc_errors_to_warnings (true);
2782 : :
2783 : 15 : gfc_error ("Interface mismatch in global procedure %qs at %L: %s",
2784 : : sym->name, &sym->declared_at, reason);
2785 : 15 : sym->error = 1;
2786 : 15 : gfc_errors_to_warnings (false);
2787 : 15 : goto done;
2788 : : }
2789 : : }
2790 : :
2791 : 28774 : done:
2792 : :
2793 : 28774 : if (gsym->type == GSYM_UNKNOWN)
2794 : : {
2795 : 3797 : gsym->type = type;
2796 : 3797 : gsym->where = *where;
2797 : : }
2798 : :
2799 : 28774 : gsym->used = 1;
2800 : 28774 : }
2801 : :
2802 : :
2803 : : /************* Function resolution *************/
2804 : :
2805 : : /* Resolve a function call known to be generic.
2806 : : Section 14.1.2.4.1. */
2807 : :
2808 : : static match
2809 : 26731 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2810 : : {
2811 : 26731 : gfc_symbol *s;
2812 : :
2813 : 26731 : if (sym->attr.generic)
2814 : : {
2815 : 25626 : s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2816 : 25626 : if (s != NULL)
2817 : : {
2818 : 19639 : expr->value.function.name = s->name;
2819 : 19639 : expr->value.function.esym = s;
2820 : :
2821 : 19639 : if (s->ts.type != BT_UNKNOWN)
2822 : 19622 : expr->ts = s->ts;
2823 : 17 : else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
2824 : 15 : expr->ts = s->result->ts;
2825 : :
2826 : 19639 : if (s->as != NULL)
2827 : : {
2828 : 54 : expr->rank = s->as->rank;
2829 : 54 : expr->corank = s->as->corank;
2830 : : }
2831 : 19585 : else if (s->result != NULL && s->result->as != NULL)
2832 : : {
2833 : 0 : expr->rank = s->result->as->rank;
2834 : 0 : expr->corank = s->result->as->corank;
2835 : : }
2836 : :
2837 : 19639 : gfc_set_sym_referenced (expr->value.function.esym);
2838 : :
2839 : 19639 : return MATCH_YES;
2840 : : }
2841 : :
2842 : : /* TODO: Need to search for elemental references in generic
2843 : : interface. */
2844 : : }
2845 : :
2846 : 7092 : if (sym->attr.intrinsic)
2847 : 1062 : return gfc_intrinsic_func_interface (expr, 0);
2848 : :
2849 : : return MATCH_NO;
2850 : : }
2851 : :
2852 : :
2853 : : static bool
2854 : 26590 : resolve_generic_f (gfc_expr *expr)
2855 : : {
2856 : 26590 : gfc_symbol *sym;
2857 : 26590 : match m;
2858 : 26590 : gfc_interface *intr = NULL;
2859 : :
2860 : 26590 : sym = expr->symtree->n.sym;
2861 : :
2862 : 26731 : for (;;)
2863 : : {
2864 : 26731 : m = resolve_generic_f0 (expr, sym);
2865 : 26731 : if (m == MATCH_YES)
2866 : : return true;
2867 : 6032 : else if (m == MATCH_ERROR)
2868 : : return false;
2869 : :
2870 : 6032 : generic:
2871 : 6035 : if (!intr)
2872 : 5989 : for (intr = sym->generic; intr; intr = intr->next)
2873 : 5906 : if (gfc_fl_struct (intr->sym->attr.flavor))
2874 : : break;
2875 : :
2876 : 6035 : if (sym->ns->parent == NULL)
2877 : : break;
2878 : 258 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2879 : :
2880 : 258 : if (sym == NULL)
2881 : : break;
2882 : 144 : if (!generic_sym (sym))
2883 : 3 : goto generic;
2884 : : }
2885 : :
2886 : : /* Last ditch attempt. See if the reference is to an intrinsic
2887 : : that possesses a matching interface. 14.1.2.4 */
2888 : 5891 : if (sym && !intr && !gfc_is_intrinsic (sym, 0, expr->where))
2889 : : {
2890 : 4 : if (gfc_init_expr_flag)
2891 : 1 : gfc_error ("Function %qs in initialization expression at %L "
2892 : : "must be an intrinsic function",
2893 : 1 : expr->symtree->n.sym->name, &expr->where);
2894 : : else
2895 : 3 : gfc_error ("There is no specific function for the generic %qs "
2896 : 3 : "at %L", expr->symtree->n.sym->name, &expr->where);
2897 : 4 : return false;
2898 : : }
2899 : :
2900 : 5887 : if (intr)
2901 : : {
2902 : 5852 : if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2903 : : NULL, false))
2904 : : return false;
2905 : 5832 : if (!gfc_use_derived (expr->ts.u.derived))
2906 : : return false;
2907 : 5831 : return resolve_structure_cons (expr, 0);
2908 : : }
2909 : :
2910 : 35 : m = gfc_intrinsic_func_interface (expr, 0);
2911 : 35 : if (m == MATCH_YES)
2912 : : return true;
2913 : :
2914 : 3 : if (m == MATCH_NO)
2915 : 3 : gfc_error ("Generic function %qs at %L is not consistent with a "
2916 : 3 : "specific intrinsic interface", expr->symtree->n.sym->name,
2917 : : &expr->where);
2918 : :
2919 : : return false;
2920 : : }
2921 : :
2922 : :
2923 : : /* Resolve a function call known to be specific. */
2924 : :
2925 : : static match
2926 : 27070 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2927 : : {
2928 : 27070 : match m;
2929 : :
2930 : 27070 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2931 : : {
2932 : 7822 : if (sym->attr.dummy)
2933 : : {
2934 : 274 : sym->attr.proc = PROC_DUMMY;
2935 : 274 : goto found;
2936 : : }
2937 : :
2938 : 7548 : sym->attr.proc = PROC_EXTERNAL;
2939 : 7548 : goto found;
2940 : : }
2941 : :
2942 : 19248 : if (sym->attr.proc == PROC_MODULE
2943 : : || sym->attr.proc == PROC_ST_FUNCTION
2944 : : || sym->attr.proc == PROC_INTERNAL)
2945 : 18510 : goto found;
2946 : :
2947 : 738 : if (sym->attr.intrinsic)
2948 : : {
2949 : 731 : m = gfc_intrinsic_func_interface (expr, 1);
2950 : 731 : if (m == MATCH_YES)
2951 : : return MATCH_YES;
2952 : 0 : if (m == MATCH_NO)
2953 : 0 : gfc_error ("Function %qs at %L is INTRINSIC but is not compatible "
2954 : : "with an intrinsic", sym->name, &expr->where);
2955 : :
2956 : 0 : return MATCH_ERROR;
2957 : : }
2958 : :
2959 : : return MATCH_NO;
2960 : :
2961 : 26332 : found:
2962 : 26332 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2963 : :
2964 : 26332 : if (sym->result)
2965 : 26332 : expr->ts = sym->result->ts;
2966 : : else
2967 : 0 : expr->ts = sym->ts;
2968 : 26332 : expr->value.function.name = sym->name;
2969 : 26332 : expr->value.function.esym = sym;
2970 : : /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2971 : : error(s). */
2972 : 26332 : if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2973 : : return MATCH_ERROR;
2974 : 26331 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as)
2975 : : {
2976 : 310 : expr->rank = CLASS_DATA (sym)->as->rank;
2977 : 310 : expr->corank = CLASS_DATA (sym)->as->corank;
2978 : : }
2979 : 26021 : else if (sym->as != NULL)
2980 : : {
2981 : 2261 : expr->rank = sym->as->rank;
2982 : 2261 : expr->corank = sym->as->corank;
2983 : : }
2984 : :
2985 : : return MATCH_YES;
2986 : : }
2987 : :
2988 : :
2989 : : static bool
2990 : 27063 : resolve_specific_f (gfc_expr *expr)
2991 : : {
2992 : 27063 : gfc_symbol *sym;
2993 : 27063 : match m;
2994 : :
2995 : 27063 : sym = expr->symtree->n.sym;
2996 : :
2997 : 27070 : for (;;)
2998 : : {
2999 : 27070 : m = resolve_specific_f0 (sym, expr);
3000 : 27070 : if (m == MATCH_YES)
3001 : : return true;
3002 : 8 : if (m == MATCH_ERROR)
3003 : : return false;
3004 : :
3005 : 7 : if (sym->ns->parent == NULL)
3006 : : break;
3007 : :
3008 : 7 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3009 : :
3010 : 7 : if (sym == NULL)
3011 : : break;
3012 : : }
3013 : :
3014 : 0 : gfc_error ("Unable to resolve the specific function %qs at %L",
3015 : 0 : expr->symtree->n.sym->name, &expr->where);
3016 : :
3017 : 0 : return true;
3018 : : }
3019 : :
3020 : : /* Recursively append candidate SYM to CANDIDATES. Store the number of
3021 : : candidates in CANDIDATES_LEN. */
3022 : :
3023 : : static void
3024 : 204 : lookup_function_fuzzy_find_candidates (gfc_symtree *sym,
3025 : : char **&candidates,
3026 : : size_t &candidates_len)
3027 : : {
3028 : 370 : gfc_symtree *p;
3029 : :
3030 : 370 : if (sym == NULL)
3031 : : return;
3032 : 370 : if ((sym->n.sym->ts.type != BT_UNKNOWN || sym->n.sym->attr.external)
3033 : 120 : && sym->n.sym->attr.flavor == FL_PROCEDURE)
3034 : 47 : vec_push (candidates, candidates_len, sym->name);
3035 : :
3036 : 370 : p = sym->left;
3037 : 370 : if (p)
3038 : 148 : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3039 : :
3040 : 370 : p = sym->right;
3041 : 370 : if (p)
3042 : : lookup_function_fuzzy_find_candidates (p, candidates, candidates_len);
3043 : : }
3044 : :
3045 : :
3046 : : /* Lookup function FN fuzzily, taking names in SYMROOT into account. */
3047 : :
3048 : : const char*
3049 : 56 : gfc_lookup_function_fuzzy (const char *fn, gfc_symtree *symroot)
3050 : : {
3051 : 56 : char **candidates = NULL;
3052 : 56 : size_t candidates_len = 0;
3053 : 56 : lookup_function_fuzzy_find_candidates (symroot, candidates, candidates_len);
3054 : 56 : return gfc_closest_fuzzy_match (fn, candidates);
3055 : : }
3056 : :
3057 : :
3058 : : /* Resolve a procedure call not known to be generic nor specific. */
3059 : :
3060 : : static bool
3061 : 268473 : resolve_unknown_f (gfc_expr *expr)
3062 : : {
3063 : 268473 : gfc_symbol *sym;
3064 : 268473 : gfc_typespec *ts;
3065 : :
3066 : 268473 : sym = expr->symtree->n.sym;
3067 : :
3068 : 268473 : if (sym->attr.dummy)
3069 : : {
3070 : 288 : sym->attr.proc = PROC_DUMMY;
3071 : 288 : expr->value.function.name = sym->name;
3072 : 288 : goto set_type;
3073 : : }
3074 : :
3075 : : /* See if we have an intrinsic function reference. */
3076 : :
3077 : 268185 : if (gfc_is_intrinsic (sym, 0, expr->where))
3078 : : {
3079 : 265926 : if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
3080 : : return true;
3081 : : return false;
3082 : : }
3083 : :
3084 : : /* IMPLICIT NONE (external) procedures require an explicit EXTERNAL attr. */
3085 : : /* Intrinsics were handled above, only non-intrinsics left here. */
3086 : 2259 : if (sym->attr.flavor == FL_PROCEDURE
3087 : 2256 : && sym->attr.implicit_type
3088 : 375 : && sym->ns
3089 : 375 : && sym->ns->has_implicit_none_export)
3090 : : {
3091 : 3 : gfc_error ("Missing explicit declaration with EXTERNAL attribute "
3092 : : "for symbol %qs at %L", sym->name, &sym->declared_at);
3093 : 3 : sym->error = 1;
3094 : 3 : return false;
3095 : : }
3096 : :
3097 : : /* The reference is to an external name. */
3098 : :
3099 : 2256 : sym->attr.proc = PROC_EXTERNAL;
3100 : 2256 : expr->value.function.name = sym->name;
3101 : 2256 : expr->value.function.esym = expr->symtree->n.sym;
3102 : :
3103 : 2256 : if (sym->as != NULL)
3104 : : {
3105 : 1 : expr->rank = sym->as->rank;
3106 : 1 : expr->corank = sym->as->corank;
3107 : : }
3108 : :
3109 : : /* Type of the expression is either the type of the symbol or the
3110 : : default type of the symbol. */
3111 : :
3112 : 2255 : set_type:
3113 : 2544 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
3114 : :
3115 : 2544 : if (sym->ts.type != BT_UNKNOWN)
3116 : 2493 : expr->ts = sym->ts;
3117 : : else
3118 : : {
3119 : 51 : ts = gfc_get_default_type (sym->name, sym->ns);
3120 : :
3121 : 51 : if (ts->type == BT_UNKNOWN)
3122 : : {
3123 : 41 : const char *guessed
3124 : 41 : = gfc_lookup_function_fuzzy (sym->name, sym->ns->sym_root);
3125 : 41 : if (guessed)
3126 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type"
3127 : : "; did you mean %qs?",
3128 : : sym->name, &expr->where, guessed);
3129 : : else
3130 : 38 : gfc_error ("Function %qs at %L has no IMPLICIT type",
3131 : : sym->name, &expr->where);
3132 : 41 : return false;
3133 : : }
3134 : : else
3135 : 10 : expr->ts = *ts;
3136 : : }
3137 : :
3138 : : return true;
3139 : : }
3140 : :
3141 : :
3142 : : /* Return true, if the symbol is an external procedure. */
3143 : : static bool
3144 : 826702 : is_external_proc (gfc_symbol *sym)
3145 : : {
3146 : 825020 : if (!sym->attr.dummy && !sym->attr.contained
3147 : 719778 : && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3148 : 156472 : && sym->attr.proc != PROC_ST_FUNCTION
3149 : 155877 : && !sym->attr.proc_pointer
3150 : 154769 : && !sym->attr.use_assoc
3151 : 884274 : && sym->name)
3152 : : return true;
3153 : :
3154 : : return false;
3155 : : }
3156 : :
3157 : :
3158 : : /* Figure out if a function reference is pure or not. Also set the name
3159 : : of the function for a potential error message. Return nonzero if the
3160 : : function is PURE, zero if not. */
3161 : : static bool
3162 : : pure_stmt_function (gfc_expr *, gfc_symbol *);
3163 : :
3164 : : bool
3165 : 249245 : gfc_pure_function (gfc_expr *e, const char **name)
3166 : : {
3167 : 249245 : bool pure;
3168 : 249245 : gfc_component *comp;
3169 : :
3170 : 249245 : *name = NULL;
3171 : :
3172 : 249245 : if (e->symtree != NULL
3173 : 248929 : && e->symtree->n.sym != NULL
3174 : 248929 : && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3175 : 305 : return pure_stmt_function (e, e->symtree->n.sym);
3176 : :
3177 : 248940 : comp = gfc_get_proc_ptr_comp (e);
3178 : 248940 : if (comp)
3179 : : {
3180 : 290 : pure = gfc_pure (comp->ts.interface);
3181 : 290 : *name = comp->name;
3182 : : }
3183 : 248650 : else if (e->value.function.esym)
3184 : : {
3185 : 51471 : pure = gfc_pure (e->value.function.esym);
3186 : 51471 : *name = e->value.function.esym->name;
3187 : : }
3188 : 197179 : else if (e->value.function.isym)
3189 : : {
3190 : 392288 : pure = e->value.function.isym->pure
3191 : 196144 : || e->value.function.isym->elemental;
3192 : 196144 : *name = e->value.function.isym->name;
3193 : : }
3194 : 1035 : else if (e->symtree && e->symtree->n.sym && e->symtree->n.sym->attr.dummy)
3195 : : {
3196 : : /* The function has been resolved, but esym is not yet set.
3197 : : This can happen with functions as dummy argument. */
3198 : 286 : pure = e->symtree->n.sym->attr.pure;
3199 : 286 : *name = e->symtree->n.sym->name;
3200 : : }
3201 : : else
3202 : : {
3203 : : /* Implicit functions are not pure. */
3204 : 749 : pure = 0;
3205 : 749 : *name = e->value.function.name;
3206 : : }
3207 : :
3208 : : return pure;
3209 : : }
3210 : :
3211 : :
3212 : : /* Check if the expression is a reference to an implicitly pure function. */
3213 : :
3214 : : bool
3215 : 36792 : gfc_implicit_pure_function (gfc_expr *e)
3216 : : {
3217 : 36792 : gfc_component *comp = gfc_get_proc_ptr_comp (e);
3218 : 36792 : if (comp)
3219 : 274 : return gfc_implicit_pure (comp->ts.interface);
3220 : 36518 : else if (e->value.function.esym)
3221 : 31168 : return gfc_implicit_pure (e->value.function.esym);
3222 : : else
3223 : : return 0;
3224 : : }
3225 : :
3226 : :
3227 : : static bool
3228 : 981 : impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
3229 : : int *f ATTRIBUTE_UNUSED)
3230 : : {
3231 : 981 : const char *name;
3232 : :
3233 : : /* Don't bother recursing into other statement functions
3234 : : since they will be checked individually for purity. */
3235 : 981 : if (e->expr_type != EXPR_FUNCTION
3236 : 343 : || !e->symtree
3237 : 343 : || e->symtree->n.sym == sym
3238 : 20 : || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3239 : : return false;
3240 : :
3241 : 19 : return gfc_pure_function (e, &name) ? false : true;
3242 : : }
3243 : :
3244 : :
3245 : : static bool
3246 : 305 : pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
3247 : : {
3248 : 305 : return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
3249 : : }
3250 : :
3251 : :
3252 : : /* Check if an impure function is allowed in the current context. */
3253 : :
3254 : 237515 : static bool check_pure_function (gfc_expr *e)
3255 : : {
3256 : 237515 : const char *name = NULL;
3257 : 237515 : code_stack *stack;
3258 : 237515 : bool saw_block = false;
3259 : :
3260 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3261 : : gfc_do_concurrent_flag = 0 when the check for an impure function
3262 : : occurs. Check the stack to see if the source code has a nested
3263 : : BLOCK construct. */
3264 : :
3265 : 549359 : for (stack = cs_base; stack; stack = stack->prev)
3266 : : {
3267 : 311846 : if (!saw_block && stack->current->op == EXEC_BLOCK)
3268 : : {
3269 : 6679 : saw_block = true;
3270 : 6679 : continue;
3271 : : }
3272 : :
3273 : 5087 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3274 : : {
3275 : 9 : bool is_pure;
3276 : 311844 : is_pure = (e->value.function.isym
3277 : 8 : && (e->value.function.isym->pure
3278 : 1 : || e->value.function.isym->elemental))
3279 : 10 : || (e->value.function.esym
3280 : 1 : && (e->value.function.esym->attr.pure
3281 : 1 : || e->value.function.esym->attr.elemental));
3282 : 2 : if (!is_pure)
3283 : : {
3284 : 2 : gfc_error ("Reference to impure function at %L inside a "
3285 : : "DO CONCURRENT", &e->where);
3286 : 2 : return false;
3287 : : }
3288 : : }
3289 : : }
3290 : :
3291 : 237513 : if (!gfc_pure_function (e, &name) && name)
3292 : : {
3293 : 35526 : if (forall_flag)
3294 : : {
3295 : 4 : gfc_error ("Reference to impure function %qs at %L inside a "
3296 : : "FORALL %s", name, &e->where,
3297 : : forall_flag == 2 ? "mask" : "block");
3298 : 4 : return false;
3299 : : }
3300 : 35522 : else if (gfc_do_concurrent_flag)
3301 : : {
3302 : 2 : gfc_error ("Reference to impure function %qs at %L inside a "
3303 : : "DO CONCURRENT %s", name, &e->where,
3304 : : gfc_do_concurrent_flag == 2 ? "mask" : "block");
3305 : 2 : return false;
3306 : : }
3307 : 35520 : else if (gfc_pure (NULL))
3308 : : {
3309 : 5 : gfc_error ("Reference to impure function %qs at %L "
3310 : : "within a PURE procedure", name, &e->where);
3311 : 5 : return false;
3312 : : }
3313 : 35515 : if (!gfc_implicit_pure_function (e))
3314 : 29573 : gfc_unset_implicit_pure (NULL);
3315 : : }
3316 : : return true;
3317 : : }
3318 : :
3319 : :
3320 : : /* Update current procedure's array_outer_dependency flag, considering
3321 : : a call to procedure SYM. */
3322 : :
3323 : : static void
3324 : 128734 : update_current_proc_array_outer_dependency (gfc_symbol *sym)
3325 : : {
3326 : : /* Check to see if this is a sibling function that has not yet
3327 : : been resolved. */
3328 : 128734 : gfc_namespace *sibling = gfc_current_ns->sibling;
3329 : 242874 : for (; sibling; sibling = sibling->sibling)
3330 : : {
3331 : 120849 : if (sibling->proc_name == sym)
3332 : : {
3333 : 6709 : gfc_resolve (sibling);
3334 : 6709 : break;
3335 : : }
3336 : : }
3337 : :
3338 : : /* If SYM has references to outer arrays, so has the procedure calling
3339 : : SYM. If SYM is a procedure pointer, we can assume the worst. */
3340 : 128734 : if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3341 : 66460 : && gfc_current_ns->proc_name)
3342 : 66416 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3343 : 128734 : }
3344 : :
3345 : :
3346 : : /* Resolve a function call, which means resolving the arguments, then figuring
3347 : : out which entity the name refers to. */
3348 : :
3349 : : static bool
3350 : 334981 : resolve_function (gfc_expr *expr)
3351 : : {
3352 : 334981 : gfc_actual_arglist *arg;
3353 : 334981 : gfc_symbol *sym;
3354 : 334981 : bool t;
3355 : 334981 : int temp;
3356 : 334981 : procedure_type p = PROC_INTRINSIC;
3357 : 334981 : bool no_formal_args;
3358 : :
3359 : 334981 : sym = NULL;
3360 : 334981 : if (expr->symtree)
3361 : 334665 : sym = expr->symtree->n.sym;
3362 : :
3363 : : /* If this is a procedure pointer component, it has already been resolved. */
3364 : 334981 : if (gfc_is_proc_ptr_comp (expr))
3365 : : return true;
3366 : :
3367 : : /* Avoid re-resolving the arguments of caf_get, which can lead to inserting
3368 : : another caf_get. */
3369 : 334584 : if (sym && sym->attr.intrinsic
3370 : 8328 : && (sym->intmod_sym_id == GFC_ISYM_CAF_GET
3371 : 8328 : || sym->intmod_sym_id == GFC_ISYM_CAF_SEND))
3372 : : return true;
3373 : :
3374 : 334584 : if (expr->ref)
3375 : : {
3376 : 1 : gfc_error ("Unexpected junk after %qs at %L", expr->symtree->n.sym->name,
3377 : : &expr->where);
3378 : 1 : return false;
3379 : : }
3380 : :
3381 : 334267 : if (sym && sym->attr.intrinsic
3382 : 342911 : && !gfc_resolve_intrinsic (sym, &expr->where))
3383 : : return false;
3384 : :
3385 : 334583 : if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
3386 : : {
3387 : 4 : gfc_error ("%qs at %L is not a function", sym->name, &expr->where);
3388 : 4 : return false;
3389 : : }
3390 : :
3391 : : /* If this is a deferred TBP with an abstract interface (which may
3392 : : of course be referenced), expr->value.function.esym will be set. */
3393 : 334263 : if (sym && sym->attr.abstract && !expr->value.function.esym)
3394 : : {
3395 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3396 : : sym->name, &expr->where);
3397 : 1 : return false;
3398 : : }
3399 : :
3400 : : /* If this is a deferred TBP with an abstract interface, its result
3401 : : cannot be an assumed length character (F2003: C418). */
3402 : 334262 : if (sym && sym->attr.abstract && sym->attr.function
3403 : 190 : && sym->result->ts.u.cl
3404 : 157 : && sym->result->ts.u.cl->length == NULL
3405 : 2 : && !sym->result->ts.deferred)
3406 : : {
3407 : 1 : gfc_error ("ABSTRACT INTERFACE %qs at %L must not have an assumed "
3408 : : "character length result (F2008: C418)", sym->name,
3409 : : &sym->declared_at);
3410 : 1 : return false;
3411 : : }
3412 : :
3413 : : /* Switch off assumed size checking and do this again for certain kinds
3414 : : of procedure, once the procedure itself is resolved. */
3415 : 334577 : need_full_assumed_size++;
3416 : :
3417 : 334577 : if (expr->symtree && expr->symtree->n.sym)
3418 : 334261 : p = expr->symtree->n.sym->attr.proc;
3419 : :
3420 : 334577 : if (expr->value.function.isym && expr->value.function.isym->inquiry)
3421 : 1078 : inquiry_argument = true;
3422 : 334261 : no_formal_args = sym && is_external_proc (sym)
3423 : 347866 : && gfc_sym_get_dummy_args (sym) == NULL;
3424 : :
3425 : 334577 : if (!resolve_actual_arglist (expr->value.function.actual,
3426 : : p, no_formal_args))
3427 : : {
3428 : 66 : inquiry_argument = false;
3429 : 66 : return false;
3430 : : }
3431 : :
3432 : 334511 : inquiry_argument = false;
3433 : :
3434 : : /* Resume assumed_size checking. */
3435 : 334511 : need_full_assumed_size--;
3436 : :
3437 : : /* If the procedure is external, check for usage. */
3438 : 334511 : if (sym && is_external_proc (sym))
3439 : 13270 : resolve_global_procedure (sym, &expr->where, 0);
3440 : :
3441 : 334511 : if (sym && sym->ts.type == BT_CHARACTER
3442 : 3155 : && sym->ts.u.cl
3443 : 3095 : && sym->ts.u.cl->length == NULL
3444 : 583 : && !sym->attr.dummy
3445 : 578 : && !sym->ts.deferred
3446 : 2 : && expr->value.function.esym == NULL
3447 : 2 : && !sym->attr.contained)
3448 : : {
3449 : : /* Internal procedures are taken care of in resolve_contained_fntype. */
3450 : 1 : gfc_error ("Function %qs is declared CHARACTER(*) and cannot "
3451 : : "be used at %L since it is not a dummy argument",
3452 : : sym->name, &expr->where);
3453 : 1 : return false;
3454 : : }
3455 : :
3456 : : /* Add and check formal interface when -fc-prototypes-external is in
3457 : : force, see comment in resolve_call(). */
3458 : :
3459 : 334510 : if (warn_external_argument_mismatch && sym && sym->attr.dummy
3460 : 18 : && sym->attr.external)
3461 : : {
3462 : 18 : if (sym->formal)
3463 : : {
3464 : 6 : bool conflict;
3465 : 6 : conflict = !gfc_compare_actual_formal (&expr->value.function.actual,
3466 : : sym->formal, 0, 0, 0, NULL);
3467 : 6 : if (conflict)
3468 : : {
3469 : 6 : sym->ext_dummy_arglist_mismatch = 1;
3470 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
3471 : : "Different argument lists in external dummy "
3472 : : "function %s at %L and %L", sym->name,
3473 : : &expr->where, &sym->formal_at);
3474 : : }
3475 : : }
3476 : 12 : else if (!sym->formal_resolved)
3477 : : {
3478 : 6 : gfc_get_formal_from_actual_arglist (sym, expr->value.function.actual);
3479 : 6 : sym->formal_at = expr->where;
3480 : : }
3481 : : }
3482 : : /* See if function is already resolved. */
3483 : :
3484 : 334510 : if (expr->value.function.name != NULL
3485 : 322912 : || expr->value.function.isym != NULL)
3486 : : {
3487 : 12384 : if (expr->ts.type == BT_UNKNOWN)
3488 : 3 : expr->ts = sym->ts;
3489 : : t = true;
3490 : : }
3491 : : else
3492 : : {
3493 : : /* Apply the rules of section 14.1.2. */
3494 : :
3495 : 322126 : switch (procedure_kind (sym))
3496 : : {
3497 : 26590 : case PTYPE_GENERIC:
3498 : 26590 : t = resolve_generic_f (expr);
3499 : 26590 : break;
3500 : :
3501 : 27063 : case PTYPE_SPECIFIC:
3502 : 27063 : t = resolve_specific_f (expr);
3503 : 27063 : break;
3504 : :
3505 : 268473 : case PTYPE_UNKNOWN:
3506 : 268473 : t = resolve_unknown_f (expr);
3507 : 268473 : break;
3508 : :
3509 : : default:
3510 : : gfc_internal_error ("resolve_function(): bad function type");
3511 : : }
3512 : : }
3513 : :
3514 : : /* If the expression is still a function (it might have simplified),
3515 : : then we check to see if we are calling an elemental function. */
3516 : :
3517 : 334510 : if (expr->expr_type != EXPR_FUNCTION)
3518 : : return t;
3519 : :
3520 : : /* Walk the argument list looking for invalid BOZ. */
3521 : 722134 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3522 : 484887 : if (arg->expr && arg->expr->ts.type == BT_BOZ)
3523 : : {
3524 : 5 : gfc_error ("A BOZ literal constant at %L cannot appear as an "
3525 : : "actual argument in a function reference",
3526 : : &arg->expr->where);
3527 : 5 : return false;
3528 : : }
3529 : :
3530 : 237247 : temp = need_full_assumed_size;
3531 : 237247 : need_full_assumed_size = 0;
3532 : :
3533 : 237247 : if (!resolve_elemental_actual (expr, NULL))
3534 : : return false;
3535 : :
3536 : 237244 : if (omp_workshare_flag
3537 : 32 : && expr->value.function.esym
3538 : 237249 : && ! gfc_elemental (expr->value.function.esym))
3539 : : {
3540 : 4 : gfc_error ("User defined non-ELEMENTAL function %qs at %L not allowed "
3541 : 4 : "in WORKSHARE construct", expr->value.function.esym->name,
3542 : : &expr->where);
3543 : 4 : t = false;
3544 : : }
3545 : :
3546 : : #define GENERIC_ID expr->value.function.isym->id
3547 : 237240 : else if (expr->value.function.actual != NULL
3548 : 229477 : && expr->value.function.isym != NULL
3549 : 185544 : && GENERIC_ID != GFC_ISYM_LBOUND
3550 : : && GENERIC_ID != GFC_ISYM_LCOBOUND
3551 : : && GENERIC_ID != GFC_ISYM_UCOBOUND
3552 : : && GENERIC_ID != GFC_ISYM_LEN
3553 : : && GENERIC_ID != GFC_ISYM_LOC
3554 : : && GENERIC_ID != GFC_ISYM_C_LOC
3555 : : && GENERIC_ID != GFC_ISYM_PRESENT)
3556 : : {
3557 : : /* Array intrinsics must also have the last upper bound of an
3558 : : assumed size array argument. UBOUND and SIZE have to be
3559 : : excluded from the check if the second argument is anything
3560 : : than a constant. */
3561 : :
3562 : 524120 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3563 : : {
3564 : 363864 : if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
3565 : 44708 : && arg == expr->value.function.actual
3566 : 16506 : && arg->next != NULL && arg->next->expr)
3567 : : {
3568 : 8208 : if (arg->next->expr->expr_type != EXPR_CONSTANT)
3569 : : break;
3570 : :
3571 : 7984 : if (arg->next->name && strcmp (arg->next->name, "kind") == 0)
3572 : : break;
3573 : :
3574 : 7984 : if ((int)mpz_get_si (arg->next->expr->value.integer)
3575 : 7984 : < arg->expr->rank)
3576 : : break;
3577 : : }
3578 : :
3579 : 361461 : if (arg->expr != NULL
3580 : 242028 : && arg->expr->rank > 0
3581 : 478234 : && resolve_assumed_size_actual (arg->expr))
3582 : : return false;
3583 : : }
3584 : : }
3585 : : #undef GENERIC_ID
3586 : :
3587 : 237241 : need_full_assumed_size = temp;
3588 : :
3589 : 237241 : if (!check_pure_function(expr))
3590 : 12 : t = false;
3591 : :
3592 : : /* Functions without the RECURSIVE attribution are not allowed to
3593 : : * call themselves. */
3594 : 237241 : if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3595 : : {
3596 : 50266 : gfc_symbol *esym;
3597 : 50266 : esym = expr->value.function.esym;
3598 : :
3599 : 50266 : if (is_illegal_recursion (esym, gfc_current_ns))
3600 : : {
3601 : 5 : if (esym->attr.entry && esym->ns->entries)
3602 : 3 : gfc_error ("ENTRY %qs at %L cannot be called recursively, as"
3603 : : " function %qs is not RECURSIVE",
3604 : 3 : esym->name, &expr->where, esym->ns->entries->sym->name);
3605 : : else
3606 : 2 : gfc_error ("Function %qs at %L cannot be called recursively, as it"
3607 : : " is not RECURSIVE", esym->name, &expr->where);
3608 : :
3609 : : t = false;
3610 : : }
3611 : : }
3612 : :
3613 : : /* Character lengths of use associated functions may contains references to
3614 : : symbols not referenced from the current program unit otherwise. Make sure
3615 : : those symbols are marked as referenced. */
3616 : :
3617 : 237241 : if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
3618 : 3293 : && expr->value.function.esym->attr.use_assoc)
3619 : : {
3620 : 1236 : gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
3621 : : }
3622 : :
3623 : : /* Make sure that the expression has a typespec that works. */
3624 : 237241 : if (expr->ts.type == BT_UNKNOWN)
3625 : : {
3626 : 888 : if (expr->symtree->n.sym->result
3627 : 880 : && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
3628 : 559 : && !expr->symtree->n.sym->result->attr.proc_pointer)
3629 : 559 : expr->ts = expr->symtree->n.sym->result->ts;
3630 : : }
3631 : :
3632 : : /* These derived types with an incomplete namespace, arising from use
3633 : : association, cause gfc_get_derived_vtab to segfault. If the function
3634 : : namespace does not suffice, something is badly wrong. */
3635 : 237241 : if (expr->ts.type == BT_DERIVED
3636 : 8860 : && !expr->ts.u.derived->ns->proc_name)
3637 : : {
3638 : 3 : gfc_symbol *der;
3639 : 3 : gfc_find_symbol (expr->ts.u.derived->name, expr->symtree->n.sym->ns, 1, &der);
3640 : 3 : if (der)
3641 : : {
3642 : 3 : expr->ts.u.derived->refs--;
3643 : 3 : expr->ts.u.derived = der;
3644 : 3 : der->refs++;
3645 : : }
3646 : : else
3647 : 0 : expr->ts.u.derived->ns = expr->symtree->n.sym->ns;
3648 : : }
3649 : :
3650 : 237241 : if (!expr->ref && !expr->value.function.isym)
3651 : : {
3652 : 51576 : if (expr->value.function.esym)
3653 : 50541 : update_current_proc_array_outer_dependency (expr->value.function.esym);
3654 : : else
3655 : 1035 : update_current_proc_array_outer_dependency (sym);
3656 : : }
3657 : 185665 : else if (expr->ref)
3658 : : /* typebound procedure: Assume the worst. */
3659 : 0 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3660 : :
3661 : 237241 : if (expr->value.function.esym
3662 : 50541 : && expr->value.function.esym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
3663 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
3664 : : "Using function %qs at %L is deprecated",
3665 : : sym->name, &expr->where);
3666 : : return t;
3667 : : }
3668 : :
3669 : :
3670 : : /************* Subroutine resolution *************/
3671 : :
3672 : : static bool
3673 : 75039 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3674 : : {
3675 : 75039 : code_stack *stack;
3676 : 75039 : bool saw_block = false;
3677 : :
3678 : 75039 : if (gfc_pure (sym))
3679 : : return true;
3680 : :
3681 : : /* A BLOCK construct within a DO CONCURRENT construct leads to
3682 : : gfc_do_concurrent_flag = 0 when the check for an impure subroutine
3683 : : occurs. Walk up the stack to see if the source code has a nested
3684 : : construct. */
3685 : :
3686 : 155322 : for (stack = cs_base; stack; stack = stack->prev)
3687 : : {
3688 : 85682 : if (stack->current->op == EXEC_BLOCK)
3689 : : {
3690 : 1875 : saw_block = true;
3691 : 1875 : continue;
3692 : : }
3693 : :
3694 : 83807 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3695 : : {
3696 : :
3697 : 2 : bool is_pure = true;
3698 : 85682 : is_pure = sym->attr.pure || sym->attr.elemental;
3699 : :
3700 : 2 : if (!is_pure)
3701 : : {
3702 : 2 : gfc_error ("Subroutine call at %L in a DO CONCURRENT block "
3703 : : "is not PURE", loc);
3704 : 2 : return false;
3705 : : }
3706 : : }
3707 : : }
3708 : :
3709 : 69640 : if (forall_flag)
3710 : : {
3711 : 0 : gfc_error ("Subroutine call to %qs in FORALL block at %L is not PURE",
3712 : : name, loc);
3713 : 0 : return false;
3714 : : }
3715 : 69640 : else if (gfc_do_concurrent_flag)
3716 : : {
3717 : 6 : gfc_error ("Subroutine call to %qs in DO CONCURRENT block at %L is not "
3718 : : "PURE", name, loc);
3719 : 6 : return false;
3720 : : }
3721 : 69634 : else if (gfc_pure (NULL))
3722 : : {
3723 : 4 : gfc_error ("Subroutine call to %qs at %L is not PURE", name, loc);
3724 : 4 : return false;
3725 : : }
3726 : :
3727 : 69630 : gfc_unset_implicit_pure (NULL);
3728 : 69630 : return true;
3729 : : }
3730 : :
3731 : :
3732 : : static match
3733 : 2179 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3734 : : {
3735 : 2179 : gfc_symbol *s;
3736 : :
3737 : 2179 : if (sym->attr.generic)
3738 : : {
3739 : 2178 : s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3740 : 2178 : if (s != NULL)
3741 : : {
3742 : 2169 : c->resolved_sym = s;
3743 : 2169 : if (!pure_subroutine (s, s->name, &c->loc))
3744 : : return MATCH_ERROR;
3745 : 2169 : return MATCH_YES;
3746 : : }
3747 : :
3748 : : /* TODO: Need to search for elemental references in generic interface. */
3749 : : }
3750 : :
3751 : 10 : if (sym->attr.intrinsic)
3752 : 1 : return gfc_intrinsic_sub_interface (c, 0);
3753 : :
3754 : : return MATCH_NO;
3755 : : }
3756 : :
3757 : :
3758 : : static bool
3759 : 2177 : resolve_generic_s (gfc_code *c)
3760 : : {
3761 : 2177 : gfc_symbol *sym;
3762 : 2177 : match m;
3763 : :
3764 : 2177 : sym = c->symtree->n.sym;
3765 : :
3766 : 2179 : for (;;)
3767 : : {
3768 : 2179 : m = resolve_generic_s0 (c, sym);
3769 : 2179 : if (m == MATCH_YES)
3770 : : return true;
3771 : 9 : else if (m == MATCH_ERROR)
3772 : : return false;
3773 : :
3774 : 9 : generic:
3775 : 9 : if (sym->ns->parent == NULL)
3776 : : break;
3777 : 3 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3778 : :
3779 : 3 : if (sym == NULL)
3780 : : break;
3781 : 2 : if (!generic_sym (sym))
3782 : 0 : goto generic;
3783 : : }
3784 : :
3785 : : /* Last ditch attempt. See if the reference is to an intrinsic
3786 : : that possesses a matching interface. 14.1.2.4 */
3787 : 7 : sym = c->symtree->n.sym;
3788 : :
3789 : 7 : if (!gfc_is_intrinsic (sym, 1, c->loc))
3790 : : {
3791 : 4 : gfc_error ("There is no specific subroutine for the generic %qs at %L",
3792 : : sym->name, &c->loc);
3793 : 4 : return false;
3794 : : }
3795 : :
3796 : 3 : m = gfc_intrinsic_sub_interface (c, 0);
3797 : 3 : if (m == MATCH_YES)
3798 : : return true;
3799 : 1 : if (m == MATCH_NO)
3800 : 1 : gfc_error ("Generic subroutine %qs at %L is not consistent with an "
3801 : : "intrinsic subroutine interface", sym->name, &c->loc);
3802 : :
3803 : : return false;
3804 : : }
3805 : :
3806 : :
3807 : : /* Resolve a subroutine call known to be specific. */
3808 : :
3809 : : static match
3810 : 61270 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3811 : : {
3812 : 61270 : match m;
3813 : :
3814 : 61270 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3815 : : {
3816 : 5543 : if (sym->attr.dummy)
3817 : : {
3818 : 256 : sym->attr.proc = PROC_DUMMY;
3819 : 256 : goto found;
3820 : : }
3821 : :
3822 : 5287 : sym->attr.proc = PROC_EXTERNAL;
3823 : 5287 : goto found;
3824 : : }
3825 : :
3826 : 55727 : if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3827 : 55727 : goto found;
3828 : :
3829 : 0 : if (sym->attr.intrinsic)
3830 : : {
3831 : 0 : m = gfc_intrinsic_sub_interface (c, 1);
3832 : 0 : if (m == MATCH_YES)
3833 : : return MATCH_YES;
3834 : 0 : if (m == MATCH_NO)
3835 : 0 : gfc_error ("Subroutine %qs at %L is INTRINSIC but is not compatible "
3836 : : "with an intrinsic", sym->name, &c->loc);
3837 : :
3838 : 0 : return MATCH_ERROR;
3839 : : }
3840 : :
3841 : : return MATCH_NO;
3842 : :
3843 : 61270 : found:
3844 : 61270 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3845 : :
3846 : 61270 : c->resolved_sym = sym;
3847 : 61270 : if (!pure_subroutine (sym, sym->name, &c->loc))
3848 : : return MATCH_ERROR;
3849 : :
3850 : : return MATCH_YES;
3851 : : }
3852 : :
3853 : :
3854 : : static bool
3855 : 61270 : resolve_specific_s (gfc_code *c)
3856 : : {
3857 : 61270 : gfc_symbol *sym;
3858 : 61270 : match m;
3859 : :
3860 : 61270 : sym = c->symtree->n.sym;
3861 : :
3862 : 61270 : for (;;)
3863 : : {
3864 : 61270 : m = resolve_specific_s0 (c, sym);
3865 : 61270 : if (m == MATCH_YES)
3866 : : return true;
3867 : 7 : if (m == MATCH_ERROR)
3868 : : return false;
3869 : :
3870 : 0 : if (sym->ns->parent == NULL)
3871 : : break;
3872 : :
3873 : 0 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3874 : :
3875 : 0 : if (sym == NULL)
3876 : : break;
3877 : : }
3878 : :
3879 : 0 : sym = c->symtree->n.sym;
3880 : 0 : gfc_error ("Unable to resolve the specific subroutine %qs at %L",
3881 : : sym->name, &c->loc);
3882 : :
3883 : 0 : return false;
3884 : : }
3885 : :
3886 : :
3887 : : /* Resolve a subroutine call not known to be generic nor specific. */
3888 : :
3889 : : static bool
3890 : 15329 : resolve_unknown_s (gfc_code *c)
3891 : : {
3892 : 15329 : gfc_symbol *sym;
3893 : :
3894 : 15329 : sym = c->symtree->n.sym;
3895 : :
3896 : 15329 : if (sym->attr.dummy)
3897 : : {
3898 : 20 : sym->attr.proc = PROC_DUMMY;
3899 : 20 : goto found;
3900 : : }
3901 : :
3902 : : /* See if we have an intrinsic function reference. */
3903 : :
3904 : 15309 : if (gfc_is_intrinsic (sym, 1, c->loc))
3905 : : {
3906 : 3851 : if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3907 : : return true;
3908 : 303 : return false;
3909 : : }
3910 : :
3911 : : /* The reference is to an external name. */
3912 : :
3913 : 11458 : found:
3914 : 11478 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3915 : :
3916 : 11478 : c->resolved_sym = sym;
3917 : :
3918 : 11478 : return pure_subroutine (sym, sym->name, &c->loc);
3919 : : }
3920 : :
3921 : :
3922 : : /* Resolve a subroutine call. Although it was tempting to use the same code
3923 : : for functions, subroutines and functions are stored differently and this
3924 : : makes things awkward. */
3925 : :
3926 : : static bool
3927 : 78865 : resolve_call (gfc_code *c)
3928 : : {
3929 : 78865 : bool t;
3930 : 78865 : procedure_type ptype = PROC_INTRINSIC;
3931 : 78865 : gfc_symbol *csym, *sym;
3932 : 78865 : bool no_formal_args;
3933 : :
3934 : 78865 : csym = c->symtree ? c->symtree->n.sym : NULL;
3935 : :
3936 : 78865 : if (csym && csym->ts.type != BT_UNKNOWN)
3937 : : {
3938 : 4 : gfc_error ("%qs at %L has a type, which is not consistent with "
3939 : : "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
3940 : 4 : return false;
3941 : : }
3942 : :
3943 : 78861 : if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3944 : : {
3945 : 16701 : gfc_symtree *st;
3946 : 16701 : gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
3947 : 16701 : sym = st ? st->n.sym : NULL;
3948 : 16701 : if (sym && csym != sym
3949 : 3 : && sym->ns == gfc_current_ns
3950 : 3 : && sym->attr.flavor == FL_PROCEDURE
3951 : 3 : && sym->attr.contained)
3952 : : {
3953 : 3 : sym->refs++;
3954 : 3 : if (csym->attr.generic)
3955 : 2 : c->symtree->n.sym = sym;
3956 : : else
3957 : 1 : c->symtree = st;
3958 : 3 : csym = c->symtree->n.sym;
3959 : : }
3960 : : }
3961 : :
3962 : : /* If this ia a deferred TBP, c->expr1 will be set. */
3963 : 78861 : if (!c->expr1 && csym)
3964 : : {
3965 : 77201 : if (csym->attr.abstract)
3966 : : {
3967 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
3968 : : csym->name, &c->loc);
3969 : 1 : return false;
3970 : : }
3971 : :
3972 : : /* Subroutines without the RECURSIVE attribution are not allowed to
3973 : : call themselves. */
3974 : 77200 : if (is_illegal_recursion (csym, gfc_current_ns))
3975 : : {
3976 : 4 : if (csym->attr.entry && csym->ns->entries)
3977 : 2 : gfc_error ("ENTRY %qs at %L cannot be called recursively, "
3978 : : "as subroutine %qs is not RECURSIVE",
3979 : 2 : csym->name, &c->loc, csym->ns->entries->sym->name);
3980 : : else
3981 : 2 : gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
3982 : : "as it is not RECURSIVE", csym->name, &c->loc);
3983 : :
3984 : 78860 : t = false;
3985 : : }
3986 : : }
3987 : :
3988 : : /* Switch off assumed size checking and do this again for certain kinds
3989 : : of procedure, once the procedure itself is resolved. */
3990 : 78860 : need_full_assumed_size++;
3991 : :
3992 : 78860 : if (csym)
3993 : 78860 : ptype = csym->attr.proc;
3994 : :
3995 : 78860 : no_formal_args = csym && is_external_proc (csym)
3996 : 15509 : && gfc_sym_get_dummy_args (csym) == NULL;
3997 : 78860 : if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
3998 : : return false;
3999 : :
4000 : : /* Resume assumed_size checking. */
4001 : 78827 : need_full_assumed_size--;
4002 : :
4003 : : /* If external, check for usage. */
4004 : 78827 : if (csym && is_external_proc (csym))
4005 : 15504 : resolve_global_procedure (csym, &c->loc, 1);
4006 : :
4007 : : /* If we have an external dummy argument, we want to write out its arguments
4008 : : with -fc-prototypes-external. Code like
4009 : :
4010 : : subroutine foo(a,n)
4011 : : external a
4012 : : if (n == 1) call a(1)
4013 : : if (n == 2) call a(2,3)
4014 : : end subroutine foo
4015 : :
4016 : : is actually legal Fortran, but it is not possible to generate a C23-
4017 : : compliant prototype for this, so we just record the fact here and
4018 : : handle that during -fc-prototypes-external processing. */
4019 : :
4020 : 78827 : if (warn_external_argument_mismatch && csym && csym->attr.dummy
4021 : 14 : && csym->attr.external)
4022 : : {
4023 : 14 : if (csym->formal)
4024 : : {
4025 : 6 : bool conflict;
4026 : 6 : conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4027 : : 0, 0, 0, NULL);
4028 : 6 : if (conflict)
4029 : : {
4030 : 6 : csym->ext_dummy_arglist_mismatch = 1;
4031 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
4032 : : "Different argument lists in external dummy "
4033 : : "subroutine %s at %L and %L", csym->name,
4034 : : &c->loc, &csym->formal_at);
4035 : : }
4036 : : }
4037 : 8 : else if (!csym->formal_resolved)
4038 : : {
4039 : 7 : gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4040 : 7 : csym->formal_at = c->loc;
4041 : : }
4042 : : }
4043 : :
4044 : 78827 : t = true;
4045 : 78827 : if (c->resolved_sym == NULL)
4046 : : {
4047 : 78776 : c->resolved_isym = NULL;
4048 : 78776 : switch (procedure_kind (csym))
4049 : : {
4050 : 2177 : case PTYPE_GENERIC:
4051 : 2177 : t = resolve_generic_s (c);
4052 : 2177 : break;
4053 : :
4054 : 61270 : case PTYPE_SPECIFIC:
4055 : 61270 : t = resolve_specific_s (c);
4056 : 61270 : break;
4057 : :
4058 : 15329 : case PTYPE_UNKNOWN:
4059 : 15329 : t = resolve_unknown_s (c);
4060 : 15329 : break;
4061 : :
4062 : : default:
4063 : : gfc_internal_error ("resolve_subroutine(): bad function type");
4064 : : }
4065 : : }
4066 : :
4067 : : /* Some checks of elemental subroutine actual arguments. */
4068 : 78826 : if (!resolve_elemental_actual (NULL, c))
4069 : : return false;
4070 : :
4071 : 78818 : if (!c->expr1)
4072 : 77158 : update_current_proc_array_outer_dependency (csym);
4073 : : else
4074 : : /* Typebound procedure: Assume the worst. */
4075 : 1660 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4076 : :
4077 : 78818 : if (c->resolved_sym
4078 : 78511 : && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4079 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
4080 : : "Using subroutine %qs at %L is deprecated",
4081 : : c->resolved_sym->name, &c->loc);
4082 : :
4083 : : return t;
4084 : : }
4085 : :
4086 : :
4087 : : /* Compare the shapes of two arrays that have non-NULL shapes. If both
4088 : : op1->shape and op2->shape are non-NULL return true if their shapes
4089 : : match. If both op1->shape and op2->shape are non-NULL return false
4090 : : if their shapes do not match. If either op1->shape or op2->shape is
4091 : : NULL, return true. */
4092 : :
4093 : : static bool
4094 : 31347 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
4095 : : {
4096 : 31347 : bool t;
4097 : 31347 : int i;
4098 : :
4099 : 31347 : t = true;
4100 : :
4101 : 31347 : if (op1->shape != NULL && op2->shape != NULL)
4102 : : {
4103 : 42344 : for (i = 0; i < op1->rank; i++)
4104 : : {
4105 : 22596 : if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4106 : : {
4107 : 3 : gfc_error ("Shapes for operands at %L and %L are not conformable",
4108 : : &op1->where, &op2->where);
4109 : 3 : t = false;
4110 : 3 : break;
4111 : : }
4112 : : }
4113 : : }
4114 : :
4115 : 31347 : return t;
4116 : : }
4117 : :
4118 : : /* Convert a logical operator to the corresponding bitwise intrinsic call.
4119 : : For example A .AND. B becomes IAND(A, B). */
4120 : : static gfc_expr *
4121 : 668 : logical_to_bitwise (gfc_expr *e)
4122 : : {
4123 : 668 : gfc_expr *tmp, *op1, *op2;
4124 : 668 : gfc_isym_id isym;
4125 : 668 : gfc_actual_arglist *args = NULL;
4126 : :
4127 : 668 : gcc_assert (e->expr_type == EXPR_OP);
4128 : :
4129 : 668 : isym = GFC_ISYM_NONE;
4130 : 668 : op1 = e->value.op.op1;
4131 : 668 : op2 = e->value.op.op2;
4132 : :
4133 : 668 : switch (e->value.op.op)
4134 : : {
4135 : : case INTRINSIC_NOT:
4136 : : isym = GFC_ISYM_NOT;
4137 : : break;
4138 : 126 : case INTRINSIC_AND:
4139 : 126 : isym = GFC_ISYM_IAND;
4140 : 126 : break;
4141 : 127 : case INTRINSIC_OR:
4142 : 127 : isym = GFC_ISYM_IOR;
4143 : 127 : break;
4144 : 270 : case INTRINSIC_NEQV:
4145 : 270 : isym = GFC_ISYM_IEOR;
4146 : 270 : break;
4147 : 126 : case INTRINSIC_EQV:
4148 : : /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4149 : : Change the old expression to NEQV, which will get replaced by IEOR,
4150 : : and wrap it in NOT. */
4151 : 126 : tmp = gfc_copy_expr (e);
4152 : 126 : tmp->value.op.op = INTRINSIC_NEQV;
4153 : 126 : tmp = logical_to_bitwise (tmp);
4154 : 126 : isym = GFC_ISYM_NOT;
4155 : 126 : op1 = tmp;
4156 : 126 : op2 = NULL;
4157 : 126 : break;
4158 : 0 : default:
4159 : 0 : gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4160 : : }
4161 : :
4162 : : /* Inherit the original operation's operands as arguments. */
4163 : 668 : args = gfc_get_actual_arglist ();
4164 : 668 : args->expr = op1;
4165 : 668 : if (op2)
4166 : : {
4167 : 523 : args->next = gfc_get_actual_arglist ();
4168 : 523 : args->next->expr = op2;
4169 : : }
4170 : :
4171 : : /* Convert the expression to a function call. */
4172 : 668 : e->expr_type = EXPR_FUNCTION;
4173 : 668 : e->value.function.actual = args;
4174 : 668 : e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4175 : 668 : e->value.function.name = e->value.function.isym->name;
4176 : 668 : e->value.function.esym = NULL;
4177 : :
4178 : : /* Make up a pre-resolved function call symtree if we need to. */
4179 : 668 : if (!e->symtree || !e->symtree->n.sym)
4180 : : {
4181 : 668 : gfc_symbol *sym;
4182 : 668 : gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4183 : 668 : sym = e->symtree->n.sym;
4184 : 668 : sym->result = sym;
4185 : 668 : sym->attr.flavor = FL_PROCEDURE;
4186 : 668 : sym->attr.function = 1;
4187 : 668 : sym->attr.elemental = 1;
4188 : 668 : sym->attr.pure = 1;
4189 : 668 : sym->attr.referenced = 1;
4190 : 668 : gfc_intrinsic_symbol (sym);
4191 : 668 : gfc_commit_symbol (sym);
4192 : : }
4193 : :
4194 : 668 : args->name = e->value.function.isym->formal->name;
4195 : 668 : if (e->value.function.isym->formal->next)
4196 : 523 : args->next->name = e->value.function.isym->formal->next->name;
4197 : :
4198 : 668 : return e;
4199 : : }
4200 : :
4201 : : /* Recursively append candidate UOP to CANDIDATES. Store the number of
4202 : : candidates in CANDIDATES_LEN. */
4203 : : static void
4204 : 43 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4205 : : char **&candidates,
4206 : : size_t &candidates_len)
4207 : : {
4208 : 45 : gfc_symtree *p;
4209 : :
4210 : 45 : if (uop == NULL)
4211 : : return;
4212 : :
4213 : : /* Not sure how to properly filter here. Use all for a start.
4214 : : n.uop.op is NULL for empty interface operators (is that legal?) disregard
4215 : : these as i suppose they don't make terribly sense. */
4216 : :
4217 : 45 : if (uop->n.uop->op != NULL)
4218 : 2 : vec_push (candidates, candidates_len, uop->name);
4219 : :
4220 : 45 : p = uop->left;
4221 : 45 : if (p)
4222 : 0 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4223 : :
4224 : 45 : p = uop->right;
4225 : 45 : if (p)
4226 : : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4227 : : }
4228 : :
4229 : : /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4230 : :
4231 : : static const char*
4232 : 43 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4233 : : {
4234 : 43 : char **candidates = NULL;
4235 : 43 : size_t candidates_len = 0;
4236 : 43 : lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4237 : 43 : return gfc_closest_fuzzy_match (op, candidates);
4238 : : }
4239 : :
4240 : :
4241 : : /* Callback finding an impure function as an operand to an .and. or
4242 : : .or. expression. Remember the last function warned about to
4243 : : avoid double warnings when recursing. */
4244 : :
4245 : : static int
4246 : 190856 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4247 : : void *data)
4248 : : {
4249 : 190856 : gfc_expr *f = *e;
4250 : 190856 : const char *name;
4251 : 190856 : static gfc_expr *last = NULL;
4252 : 190856 : bool *found = (bool *) data;
4253 : :
4254 : 190856 : if (f->expr_type == EXPR_FUNCTION)
4255 : : {
4256 : 11707 : *found = 1;
4257 : 11707 : if (f != last && !gfc_pure_function (f, &name)
4258 : 12982 : && !gfc_implicit_pure_function (f))
4259 : : {
4260 : 1136 : if (name)
4261 : 1136 : gfc_warning (OPT_Wfunction_elimination,
4262 : : "Impure function %qs at %L might not be evaluated",
4263 : : name, &f->where);
4264 : : else
4265 : 0 : gfc_warning (OPT_Wfunction_elimination,
4266 : : "Impure function at %L might not be evaluated",
4267 : : &f->where);
4268 : : }
4269 : 11707 : last = f;
4270 : : }
4271 : :
4272 : 190856 : return 0;
4273 : : }
4274 : :
4275 : : /* Return true if TYPE is character based, false otherwise. */
4276 : :
4277 : : static int
4278 : 1373 : is_character_based (bt type)
4279 : : {
4280 : 1373 : return type == BT_CHARACTER || type == BT_HOLLERITH;
4281 : : }
4282 : :
4283 : :
4284 : : /* If expression is a hollerith, convert it to character and issue a warning
4285 : : for the conversion. */
4286 : :
4287 : : static void
4288 : 408 : convert_hollerith_to_character (gfc_expr *e)
4289 : : {
4290 : 408 : if (e->ts.type == BT_HOLLERITH)
4291 : : {
4292 : 108 : gfc_typespec t;
4293 : 108 : gfc_clear_ts (&t);
4294 : 108 : t.type = BT_CHARACTER;
4295 : 108 : t.kind = e->ts.kind;
4296 : 108 : gfc_convert_type_warn (e, &t, 2, 1);
4297 : : }
4298 : 408 : }
4299 : :
4300 : : /* Convert to numeric and issue a warning for the conversion. */
4301 : :
4302 : : static void
4303 : 240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
4304 : : {
4305 : 240 : gfc_typespec t;
4306 : 240 : gfc_clear_ts (&t);
4307 : 240 : t.type = b->ts.type;
4308 : 240 : t.kind = b->ts.kind;
4309 : 240 : gfc_convert_type_warn (a, &t, 2, 1);
4310 : 240 : }
4311 : :
4312 : : /* Resolve an operator expression node. This can involve replacing the
4313 : : operation with a user defined function call. CHECK_INTERFACES is a
4314 : : helper macro. */
4315 : :
4316 : : #define CHECK_INTERFACES \
4317 : : { \
4318 : : match m = gfc_extend_expr (e); \
4319 : : if (m == MATCH_YES) \
4320 : : return true; \
4321 : : if (m == MATCH_ERROR) \
4322 : : return false; \
4323 : : }
4324 : :
4325 : : static bool
4326 : 517302 : resolve_operator (gfc_expr *e)
4327 : : {
4328 : 517302 : gfc_expr *op1, *op2;
4329 : : /* One error uses 3 names; additional space for wording (also via gettext). */
4330 : 517302 : bool t = true;
4331 : :
4332 : : /* Reduce stacked parentheses to single pair */
4333 : 517302 : while (e->expr_type == EXPR_OP
4334 : 517412 : && e->value.op.op == INTRINSIC_PARENTHESES
4335 : 20478 : && e->value.op.op1->expr_type == EXPR_OP
4336 : 533614 : && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4337 : : {
4338 : 110 : gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4339 : 110 : gfc_replace_expr (e, tmp);
4340 : : }
4341 : :
4342 : : /* Resolve all subnodes-- give them types. */
4343 : :
4344 : 517302 : switch (e->value.op.op)
4345 : : {
4346 : 468794 : default:
4347 : 468794 : if (!gfc_resolve_expr (e->value.op.op2))
4348 : 517302 : t = false;
4349 : :
4350 : : /* Fall through. */
4351 : :
4352 : 517302 : case INTRINSIC_NOT:
4353 : 517302 : case INTRINSIC_UPLUS:
4354 : 517302 : case INTRINSIC_UMINUS:
4355 : 517302 : case INTRINSIC_PARENTHESES:
4356 : 517302 : if (!gfc_resolve_expr (e->value.op.op1))
4357 : : return false;
4358 : 517135 : if (e->value.op.op1
4359 : 517126 : && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4360 : : {
4361 : 0 : gfc_error ("BOZ literal constant at %L cannot be an operand of "
4362 : 0 : "unary operator %qs", &e->value.op.op1->where,
4363 : : gfc_op2string (e->value.op.op));
4364 : 0 : return false;
4365 : : }
4366 : 517135 : if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4367 : 6 : && e->value.op.op == INTRINSIC_UMINUS)
4368 : : {
4369 : 2 : gfc_error ("Negation of unsigned expression at %L not permitted ",
4370 : : &e->value.op.op1->where);
4371 : 2 : return false;
4372 : : }
4373 : 517133 : break;
4374 : : }
4375 : :
4376 : : /* Typecheck the new node. */
4377 : :
4378 : 517133 : op1 = e->value.op.op1;
4379 : 517133 : op2 = e->value.op.op2;
4380 : 517133 : if (op1 == NULL && op2 == NULL)
4381 : : return false;
4382 : : /* Error out if op2 did not resolve. We already diagnosed op1. */
4383 : 517124 : if (t == false)
4384 : : return false;
4385 : :
4386 : : /* op1 and op2 cannot both be BOZ. */
4387 : 517062 : if (op1 && op1->ts.type == BT_BOZ
4388 : 0 : && op2 && op2->ts.type == BT_BOZ)
4389 : : {
4390 : 0 : gfc_error ("Operands at %L and %L cannot appear as operands of "
4391 : 0 : "binary operator %qs", &op1->where, &op2->where,
4392 : : gfc_op2string (e->value.op.op));
4393 : 0 : return false;
4394 : : }
4395 : :
4396 : 517062 : if ((op1 && op1->expr_type == EXPR_NULL)
4397 : 517060 : || (op2 && op2->expr_type == EXPR_NULL))
4398 : : {
4399 : 3 : CHECK_INTERFACES
4400 : 3 : gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4401 : 3 : return false;
4402 : : }
4403 : :
4404 : 517059 : switch (e->value.op.op)
4405 : : {
4406 : 8007 : case INTRINSIC_UPLUS:
4407 : 8007 : case INTRINSIC_UMINUS:
4408 : 8007 : if (op1->ts.type == BT_INTEGER
4409 : : || op1->ts.type == BT_REAL
4410 : : || op1->ts.type == BT_COMPLEX
4411 : : || op1->ts.type == BT_UNSIGNED)
4412 : : {
4413 : 7938 : e->ts = op1->ts;
4414 : 7938 : break;
4415 : : }
4416 : :
4417 : 69 : CHECK_INTERFACES
4418 : 43 : gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4419 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4420 : 43 : return false;
4421 : :
4422 : 150825 : case INTRINSIC_POWER:
4423 : 150825 : case INTRINSIC_PLUS:
4424 : 150825 : case INTRINSIC_MINUS:
4425 : 150825 : case INTRINSIC_TIMES:
4426 : 150825 : case INTRINSIC_DIVIDE:
4427 : :
4428 : : /* UNSIGNED cannot appear in a mixed expression without explicit
4429 : : conversion. */
4430 : 150825 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4431 : : {
4432 : 3 : CHECK_INTERFACES
4433 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are "
4434 : : "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4435 : : gfc_typename (op1), gfc_typename (op2));
4436 : 3 : return false;
4437 : : }
4438 : :
4439 : 150822 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4440 : : {
4441 : : /* Do not perform conversions if operands are not conformable as
4442 : : required for the binary intrinsic operators (F2018:10.1.5).
4443 : : Defer to a possibly overloading user-defined operator. */
4444 : 150378 : if (!gfc_op_rank_conformable (op1, op2))
4445 : : {
4446 : 36 : CHECK_INTERFACES
4447 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4448 : 0 : &op1->where, &op2->where);
4449 : 0 : return false;
4450 : : }
4451 : :
4452 : 150342 : gfc_type_convert_binary (e, 1);
4453 : 150342 : break;
4454 : : }
4455 : :
4456 : 444 : if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4457 : : {
4458 : 215 : CHECK_INTERFACES
4459 : 2 : gfc_error ("Unexpected derived-type entities in binary intrinsic "
4460 : : "numeric operator %qs at %L",
4461 : : gfc_op2string (e->value.op.op), &e->where);
4462 : 2 : return false;
4463 : : }
4464 : : else
4465 : : {
4466 : 229 : CHECK_INTERFACES
4467 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4468 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4469 : : gfc_typename (op2));
4470 : 3 : return false;
4471 : : }
4472 : :
4473 : 2118 : case INTRINSIC_CONCAT:
4474 : 2118 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4475 : 2093 : && op1->ts.kind == op2->ts.kind)
4476 : : {
4477 : 2084 : e->ts.type = BT_CHARACTER;
4478 : 2084 : e->ts.kind = op1->ts.kind;
4479 : 2084 : break;
4480 : : }
4481 : :
4482 : 34 : CHECK_INTERFACES
4483 : 10 : gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4484 : : &e->where, gfc_typename (op1), gfc_typename (op2));
4485 : 10 : return false;
4486 : :
4487 : 68997 : case INTRINSIC_AND:
4488 : 68997 : case INTRINSIC_OR:
4489 : 68997 : case INTRINSIC_EQV:
4490 : 68997 : case INTRINSIC_NEQV:
4491 : 68997 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4492 : : {
4493 : 68446 : e->ts.type = BT_LOGICAL;
4494 : 68446 : e->ts.kind = gfc_kind_max (op1, op2);
4495 : 68446 : if (op1->ts.kind < e->ts.kind)
4496 : 138 : gfc_convert_type (op1, &e->ts, 2);
4497 : 68308 : else if (op2->ts.kind < e->ts.kind)
4498 : 117 : gfc_convert_type (op2, &e->ts, 2);
4499 : :
4500 : 68446 : if (flag_frontend_optimize &&
4501 : 57435 : (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4502 : : {
4503 : : /* Warn about short-circuiting
4504 : : with impure function as second operand. */
4505 : 51469 : bool op2_f = false;
4506 : 51469 : gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4507 : : }
4508 : : break;
4509 : : }
4510 : :
4511 : : /* Logical ops on integers become bitwise ops with -fdec. */
4512 : 551 : else if (flag_dec
4513 : 523 : && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4514 : : {
4515 : 523 : e->ts.type = BT_INTEGER;
4516 : 523 : e->ts.kind = gfc_kind_max (op1, op2);
4517 : 523 : if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4518 : 289 : gfc_convert_type (op1, &e->ts, 1);
4519 : 523 : if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4520 : 144 : gfc_convert_type (op2, &e->ts, 1);
4521 : 523 : e = logical_to_bitwise (e);
4522 : 523 : goto simplify_op;
4523 : : }
4524 : :
4525 : 28 : CHECK_INTERFACES
4526 : 16 : gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4527 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4528 : : gfc_typename (op2));
4529 : 16 : return false;
4530 : :
4531 : 20117 : case INTRINSIC_NOT:
4532 : : /* Logical ops on integers become bitwise ops with -fdec. */
4533 : 20117 : if (flag_dec && op1->ts.type == BT_INTEGER)
4534 : : {
4535 : 19 : e->ts.type = BT_INTEGER;
4536 : 19 : e->ts.kind = op1->ts.kind;
4537 : 19 : e = logical_to_bitwise (e);
4538 : 19 : goto simplify_op;
4539 : : }
4540 : :
4541 : 20098 : if (op1->ts.type == BT_LOGICAL)
4542 : : {
4543 : 20092 : e->ts.type = BT_LOGICAL;
4544 : 20092 : e->ts.kind = op1->ts.kind;
4545 : 20092 : break;
4546 : : }
4547 : :
4548 : 6 : CHECK_INTERFACES
4549 : 3 : gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4550 : : gfc_typename (op1));
4551 : 3 : return false;
4552 : :
4553 : 20992 : case INTRINSIC_GT:
4554 : 20992 : case INTRINSIC_GT_OS:
4555 : 20992 : case INTRINSIC_GE:
4556 : 20992 : case INTRINSIC_GE_OS:
4557 : 20992 : case INTRINSIC_LT:
4558 : 20992 : case INTRINSIC_LT_OS:
4559 : 20992 : case INTRINSIC_LE:
4560 : 20992 : case INTRINSIC_LE_OS:
4561 : 20992 : if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4562 : : {
4563 : 18 : CHECK_INTERFACES
4564 : 0 : gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4565 : 0 : return false;
4566 : : }
4567 : :
4568 : : /* Fall through. */
4569 : :
4570 : 246423 : case INTRINSIC_EQ:
4571 : 246423 : case INTRINSIC_EQ_OS:
4572 : 246423 : case INTRINSIC_NE:
4573 : 246423 : case INTRINSIC_NE_OS:
4574 : :
4575 : 246423 : if (flag_dec
4576 : 1038 : && is_character_based (op1->ts.type)
4577 : 246758 : && is_character_based (op2->ts.type))
4578 : : {
4579 : 204 : convert_hollerith_to_character (op1);
4580 : 204 : convert_hollerith_to_character (op2);
4581 : : }
4582 : :
4583 : 246423 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4584 : 36408 : && op1->ts.kind == op2->ts.kind)
4585 : : {
4586 : 36371 : e->ts.type = BT_LOGICAL;
4587 : 36371 : e->ts.kind = gfc_default_logical_kind;
4588 : 36371 : break;
4589 : : }
4590 : :
4591 : : /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4592 : 210052 : if (op1->ts.type == BT_BOZ)
4593 : : {
4594 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4595 : : "as an operand of a relational operator"),
4596 : : &op1->where))
4597 : : return false;
4598 : :
4599 : 0 : if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4600 : : return false;
4601 : :
4602 : 0 : if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4603 : : return false;
4604 : : }
4605 : :
4606 : : /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4607 : 210052 : if (op2->ts.type == BT_BOZ)
4608 : : {
4609 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4610 : : " as an operand of a relational operator"),
4611 : : &op2->where))
4612 : : return false;
4613 : :
4614 : 0 : if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4615 : : return false;
4616 : :
4617 : 0 : if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4618 : : return false;
4619 : : }
4620 : 210052 : if (flag_dec
4621 : 210052 : && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4622 : 120 : convert_to_numeric (op1, op2);
4623 : :
4624 : 210052 : if (flag_dec
4625 : 210052 : && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4626 : 120 : convert_to_numeric (op2, op1);
4627 : :
4628 : 210052 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4629 : : {
4630 : : /* Do not perform conversions if operands are not conformable as
4631 : : required for the binary intrinsic operators (F2018:10.1.5).
4632 : : Defer to a possibly overloading user-defined operator. */
4633 : 208927 : if (!gfc_op_rank_conformable (op1, op2))
4634 : : {
4635 : 70 : CHECK_INTERFACES
4636 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4637 : 0 : &op1->where, &op2->where);
4638 : 0 : return false;
4639 : : }
4640 : :
4641 : 208857 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4642 : : {
4643 : 1 : CHECK_INTERFACES
4644 : 1 : gfc_error ("Inconsistent types for operator at %L and %L: "
4645 : 1 : "%s and %s", &op1->where, &op2->where,
4646 : : gfc_typename (op1), gfc_typename (op2));
4647 : 1 : return false;
4648 : : }
4649 : :
4650 : 208856 : gfc_type_convert_binary (e, 1);
4651 : :
4652 : 208856 : e->ts.type = BT_LOGICAL;
4653 : 208856 : e->ts.kind = gfc_default_logical_kind;
4654 : :
4655 : 208856 : if (warn_compare_reals)
4656 : : {
4657 : 69 : gfc_intrinsic_op op = e->value.op.op;
4658 : :
4659 : : /* Type conversion has made sure that the types of op1 and op2
4660 : : agree, so it is only necessary to check the first one. */
4661 : 69 : if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4662 : 13 : && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4663 : 6 : || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4664 : : {
4665 : 13 : const char *msg;
4666 : :
4667 : 13 : if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4668 : : msg = G_("Equality comparison for %s at %L");
4669 : : else
4670 : 6 : msg = G_("Inequality comparison for %s at %L");
4671 : :
4672 : 13 : gfc_warning (OPT_Wcompare_reals, msg,
4673 : : gfc_typename (op1), &op1->where);
4674 : : }
4675 : : }
4676 : :
4677 : : break;
4678 : : }
4679 : :
4680 : 1125 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4681 : : {
4682 : 2 : CHECK_INTERFACES
4683 : 4 : gfc_error ("Logicals at %L must be compared with %s instead of %s",
4684 : : &e->where,
4685 : 2 : (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4686 : : ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4687 : 2 : }
4688 : : else
4689 : : {
4690 : 1123 : CHECK_INTERFACES
4691 : 113 : gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4692 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4693 : : gfc_typename (op2));
4694 : : }
4695 : :
4696 : : return false;
4697 : :
4698 : 232 : case INTRINSIC_USER:
4699 : 232 : if (e->value.op.uop->op == NULL)
4700 : : {
4701 : 43 : const char *name = e->value.op.uop->name;
4702 : 43 : const char *guessed;
4703 : 43 : guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4704 : 43 : CHECK_INTERFACES
4705 : 4 : if (guessed)
4706 : 1 : gfc_error ("Unknown operator %qs at %L; did you mean "
4707 : : "%qs?", name, &e->where, guessed);
4708 : : else
4709 : 3 : gfc_error ("Unknown operator %qs at %L", name, &e->where);
4710 : : }
4711 : 189 : else if (op2 == NULL)
4712 : : {
4713 : 48 : CHECK_INTERFACES
4714 : 0 : gfc_error ("Operand of user operator %qs at %L is %s",
4715 : 0 : e->value.op.uop->name, &e->where, gfc_typename (op1));
4716 : : }
4717 : : else
4718 : : {
4719 : 141 : e->value.op.uop->op->sym->attr.referenced = 1;
4720 : 141 : CHECK_INTERFACES
4721 : 5 : gfc_error ("Operands of user operator %qs at %L are %s/%s",
4722 : 5 : e->value.op.uop->name, &e->where, gfc_typename (op1),
4723 : : gfc_typename (op2));
4724 : : }
4725 : :
4726 : : return false;
4727 : :
4728 : 20322 : case INTRINSIC_PARENTHESES:
4729 : 20322 : e->ts = op1->ts;
4730 : 20322 : if (e->ts.type == BT_CHARACTER)
4731 : 301 : e->ts.u.cl = op1->ts.u.cl;
4732 : : break;
4733 : :
4734 : 0 : default:
4735 : 0 : gfc_internal_error ("resolve_operator(): Bad intrinsic");
4736 : : }
4737 : :
4738 : : /* Deal with arrayness of an operand through an operator. */
4739 : :
4740 : 514451 : switch (e->value.op.op)
4741 : : {
4742 : 466099 : case INTRINSIC_PLUS:
4743 : 466099 : case INTRINSIC_MINUS:
4744 : 466099 : case INTRINSIC_TIMES:
4745 : 466099 : case INTRINSIC_DIVIDE:
4746 : 466099 : case INTRINSIC_POWER:
4747 : 466099 : case INTRINSIC_CONCAT:
4748 : 466099 : case INTRINSIC_AND:
4749 : 466099 : case INTRINSIC_OR:
4750 : 466099 : case INTRINSIC_EQV:
4751 : 466099 : case INTRINSIC_NEQV:
4752 : 466099 : case INTRINSIC_EQ:
4753 : 466099 : case INTRINSIC_EQ_OS:
4754 : 466099 : case INTRINSIC_NE:
4755 : 466099 : case INTRINSIC_NE_OS:
4756 : 466099 : case INTRINSIC_GT:
4757 : 466099 : case INTRINSIC_GT_OS:
4758 : 466099 : case INTRINSIC_GE:
4759 : 466099 : case INTRINSIC_GE_OS:
4760 : 466099 : case INTRINSIC_LT:
4761 : 466099 : case INTRINSIC_LT_OS:
4762 : 466099 : case INTRINSIC_LE:
4763 : 466099 : case INTRINSIC_LE_OS:
4764 : :
4765 : 466099 : if (op1->rank == 0 && op2->rank == 0)
4766 : 415632 : e->rank = 0;
4767 : :
4768 : 466099 : if (op1->rank == 0 && op2->rank != 0)
4769 : : {
4770 : 2587 : e->rank = op2->rank;
4771 : :
4772 : 2587 : if (e->shape == NULL)
4773 : 2557 : e->shape = gfc_copy_shape (op2->shape, op2->rank);
4774 : : }
4775 : :
4776 : 466099 : if (op1->rank != 0 && op2->rank == 0)
4777 : : {
4778 : 16472 : e->rank = op1->rank;
4779 : :
4780 : 16472 : if (e->shape == NULL)
4781 : 16454 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4782 : : }
4783 : :
4784 : 466099 : if (op1->rank != 0 && op2->rank != 0)
4785 : : {
4786 : 31408 : if (op1->rank == op2->rank)
4787 : : {
4788 : 31408 : e->rank = op1->rank;
4789 : 31408 : if (e->shape == NULL)
4790 : : {
4791 : 31347 : t = compare_shapes (op1, op2);
4792 : 31347 : if (!t)
4793 : 3 : e->shape = NULL;
4794 : : else
4795 : 31344 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4796 : : }
4797 : : }
4798 : : else
4799 : : {
4800 : : /* Allow higher level expressions to work. */
4801 : 0 : e->rank = 0;
4802 : :
4803 : : /* Try user-defined operators, and otherwise throw an error. */
4804 : 0 : CHECK_INTERFACES
4805 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4806 : 0 : &op1->where, &op2->where);
4807 : 0 : return false;
4808 : : }
4809 : : }
4810 : :
4811 : : /* coranks have to be equal or one has to be zero to be combinable. */
4812 : 466099 : if (op1->corank == op2->corank || (op1->corank != 0 && op2->corank == 0))
4813 : : {
4814 : 465982 : e->corank = op1->corank;
4815 : : /* Only do this, when regular array has not set a shape yet. */
4816 : 465982 : if (e->shape == NULL)
4817 : : {
4818 : 434696 : if (op1->corank != 0)
4819 : : {
4820 : 1410 : e->shape = gfc_copy_shape (op1->shape, op1->corank);
4821 : : }
4822 : : }
4823 : : }
4824 : 117 : else if (op1->corank == 0 && op2->corank != 0)
4825 : : {
4826 : 117 : e->corank = op2->corank;
4827 : : /* Only do this, when regular array has not set a shape yet. */
4828 : 117 : if (e->shape == NULL)
4829 : 89 : e->shape = gfc_copy_shape (op2->shape, op2->corank);
4830 : : }
4831 : : else
4832 : : {
4833 : 0 : gfc_error ("Inconsistent coranks for operator at %L and %L",
4834 : : &op1->where, &op2->where);
4835 : 0 : return false;
4836 : : }
4837 : :
4838 : : break;
4839 : :
4840 : 48352 : case INTRINSIC_PARENTHESES:
4841 : 48352 : case INTRINSIC_NOT:
4842 : 48352 : case INTRINSIC_UPLUS:
4843 : 48352 : case INTRINSIC_UMINUS:
4844 : : /* Simply copy arrayness attribute */
4845 : 48352 : e->rank = op1->rank;
4846 : 48352 : e->corank = op1->corank;
4847 : :
4848 : 48352 : if (e->shape == NULL)
4849 : 48346 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4850 : :
4851 : : break;
4852 : :
4853 : : default:
4854 : : break;
4855 : : }
4856 : :
4857 : 514993 : simplify_op:
4858 : :
4859 : : /* Attempt to simplify the expression. */
4860 : 514993 : if (t)
4861 : : {
4862 : 514990 : t = gfc_simplify_expr (e, 0);
4863 : : /* Some calls do not succeed in simplification and return false
4864 : : even though there is no error; e.g. variable references to
4865 : : PARAMETER arrays. */
4866 : 514990 : if (!gfc_is_constant_expr (e))
4867 : 472138 : t = true;
4868 : : }
4869 : : return t;
4870 : : }
4871 : :
4872 : :
4873 : : /************** Array resolution subroutines **************/
4874 : :
4875 : : enum compare_result
4876 : : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4877 : :
4878 : : /* Compare two integer expressions. */
4879 : :
4880 : : static compare_result
4881 : 444685 : compare_bound (gfc_expr *a, gfc_expr *b)
4882 : : {
4883 : 444685 : int i;
4884 : :
4885 : 444685 : if (a == NULL || a->expr_type != EXPR_CONSTANT
4886 : 291562 : || b == NULL || b->expr_type != EXPR_CONSTANT)
4887 : : return CMP_UNKNOWN;
4888 : :
4889 : : /* If either of the types isn't INTEGER, we must have
4890 : : raised an error earlier. */
4891 : :
4892 : 203916 : if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
4893 : : return CMP_UNKNOWN;
4894 : :
4895 : 203912 : i = mpz_cmp (a->value.integer, b->value.integer);
4896 : :
4897 : 203912 : if (i < 0)
4898 : : return CMP_LT;
4899 : 95782 : if (i > 0)
4900 : 37727 : return CMP_GT;
4901 : : return CMP_EQ;
4902 : : }
4903 : :
4904 : :
4905 : : /* Compare an integer expression with an integer. */
4906 : :
4907 : : static compare_result
4908 : 71727 : compare_bound_int (gfc_expr *a, int b)
4909 : : {
4910 : 71727 : int i;
4911 : :
4912 : 71727 : if (a == NULL
4913 : 30294 : || a->expr_type != EXPR_CONSTANT
4914 : 27503 : || a->ts.type != BT_INTEGER)
4915 : : return CMP_UNKNOWN;
4916 : :
4917 : 27503 : i = mpz_cmp_si (a->value.integer, b);
4918 : :
4919 : 27503 : if (i < 0)
4920 : : return CMP_LT;
4921 : 24220 : if (i > 0)
4922 : 21152 : return CMP_GT;
4923 : : return CMP_EQ;
4924 : : }
4925 : :
4926 : :
4927 : : /* Compare an integer expression with a mpz_t. */
4928 : :
4929 : : static compare_result
4930 : 66748 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
4931 : : {
4932 : 66748 : int i;
4933 : :
4934 : 66748 : if (a == NULL
4935 : 54828 : || a->expr_type != EXPR_CONSTANT
4936 : 52745 : || a->ts.type != BT_INTEGER)
4937 : : return CMP_UNKNOWN;
4938 : :
4939 : 52742 : i = mpz_cmp (a->value.integer, b);
4940 : :
4941 : 52742 : if (i < 0)
4942 : : return CMP_LT;
4943 : 23966 : if (i > 0)
4944 : 10270 : return CMP_GT;
4945 : : return CMP_EQ;
4946 : : }
4947 : :
4948 : :
4949 : : /* Compute the last value of a sequence given by a triplet.
4950 : : Return 0 if it wasn't able to compute the last value, or if the
4951 : : sequence if empty, and 1 otherwise. */
4952 : :
4953 : : static int
4954 : 50162 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
4955 : : gfc_expr *stride, mpz_t last)
4956 : : {
4957 : 50162 : mpz_t rem;
4958 : :
4959 : 50162 : if (start == NULL || start->expr_type != EXPR_CONSTANT
4960 : 35427 : || end == NULL || end->expr_type != EXPR_CONSTANT
4961 : 31038 : || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
4962 : : return 0;
4963 : :
4964 : 30719 : if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
4965 : 30718 : || (stride != NULL && stride->ts.type != BT_INTEGER))
4966 : : return 0;
4967 : :
4968 : 6212 : if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
4969 : : {
4970 : 24632 : if (compare_bound (start, end) == CMP_GT)
4971 : : return 0;
4972 : 23243 : mpz_set (last, end->value.integer);
4973 : 23243 : return 1;
4974 : : }
4975 : :
4976 : 6086 : if (compare_bound_int (stride, 0) == CMP_GT)
4977 : : {
4978 : : /* Stride is positive */
4979 : 5118 : if (mpz_cmp (start->value.integer, end->value.integer) > 0)
4980 : : return 0;
4981 : : }
4982 : : else
4983 : : {
4984 : : /* Stride is negative */
4985 : 968 : if (mpz_cmp (start->value.integer, end->value.integer) < 0)
4986 : : return 0;
4987 : : }
4988 : :
4989 : 6066 : mpz_init (rem);
4990 : 6066 : mpz_sub (rem, end->value.integer, start->value.integer);
4991 : 6066 : mpz_tdiv_r (rem, rem, stride->value.integer);
4992 : 6066 : mpz_sub (last, end->value.integer, rem);
4993 : 6066 : mpz_clear (rem);
4994 : :
4995 : 6066 : return 1;
4996 : : }
4997 : :
4998 : :
4999 : : /* Compare a single dimension of an array reference to the array
5000 : : specification. */
5001 : :
5002 : : static bool
5003 : 204422 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5004 : : {
5005 : 204422 : mpz_t last_value;
5006 : :
5007 : 204422 : if (ar->dimen_type[i] == DIMEN_STAR)
5008 : : {
5009 : 407 : gcc_assert (ar->stride[i] == NULL);
5010 : : /* This implies [*] as [*:] and [*:3] are not possible. */
5011 : 407 : if (ar->start[i] == NULL)
5012 : : {
5013 : 333 : gcc_assert (ar->end[i] == NULL);
5014 : : return true;
5015 : : }
5016 : : }
5017 : :
5018 : : /* Given start, end and stride values, calculate the minimum and
5019 : : maximum referenced indexes. */
5020 : :
5021 : 204089 : switch (ar->dimen_type[i])
5022 : : {
5023 : : case DIMEN_VECTOR:
5024 : : case DIMEN_THIS_IMAGE:
5025 : : break;
5026 : :
5027 : 147582 : case DIMEN_STAR:
5028 : 147582 : case DIMEN_ELEMENT:
5029 : 147582 : if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5030 : : {
5031 : 2 : if (i < as->rank)
5032 : 2 : gfc_warning (0, "Array reference at %L is out of bounds "
5033 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5034 : 2 : mpz_get_si (ar->start[i]->value.integer),
5035 : 2 : mpz_get_si (as->lower[i]->value.integer), i+1);
5036 : : else
5037 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5038 : : "(%ld < %ld) in codimension %d", &ar->c_where[i],
5039 : 0 : mpz_get_si (ar->start[i]->value.integer),
5040 : 0 : mpz_get_si (as->lower[i]->value.integer),
5041 : 0 : i + 1 - as->rank);
5042 : 2 : return true;
5043 : : }
5044 : 147580 : if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5045 : : {
5046 : 39 : if (i < as->rank)
5047 : 39 : gfc_warning (0, "Array reference at %L is out of bounds "
5048 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5049 : 39 : mpz_get_si (ar->start[i]->value.integer),
5050 : 39 : mpz_get_si (as->upper[i]->value.integer), i+1);
5051 : : else
5052 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5053 : : "(%ld > %ld) in codimension %d", &ar->c_where[i],
5054 : 0 : mpz_get_si (ar->start[i]->value.integer),
5055 : 0 : mpz_get_si (as->upper[i]->value.integer),
5056 : 0 : i + 1 - as->rank);
5057 : 39 : return true;
5058 : : }
5059 : :
5060 : : break;
5061 : :
5062 : 50207 : case DIMEN_RANGE:
5063 : 50207 : {
5064 : : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5065 : : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5066 : :
5067 : 50207 : compare_result comp_start_end = compare_bound (AR_START, AR_END);
5068 : 50207 : compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5069 : :
5070 : : /* Check for zero stride, which is not allowed. */
5071 : 50207 : if (comp_stride_zero == CMP_EQ)
5072 : : {
5073 : 1 : gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5074 : 1 : return false;
5075 : : }
5076 : :
5077 : : /* if start == end || (stride > 0 && start < end)
5078 : : || (stride < 0 && start > end),
5079 : : then the array section contains at least one element. In this
5080 : : case, there is an out-of-bounds access if
5081 : : (start < lower || start > upper). */
5082 : 50206 : if (comp_start_end == CMP_EQ
5083 : 49521 : || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5084 : 47246 : && comp_start_end == CMP_LT)
5085 : 21799 : || (comp_stride_zero == CMP_LT
5086 : 21799 : && comp_start_end == CMP_GT))
5087 : : {
5088 : 29355 : if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5089 : : {
5090 : 27 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5091 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5092 : 27 : mpz_get_si (AR_START->value.integer),
5093 : 27 : mpz_get_si (as->lower[i]->value.integer), i+1);
5094 : 27 : return true;
5095 : : }
5096 : 29328 : if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5097 : : {
5098 : 17 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5099 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5100 : 17 : mpz_get_si (AR_START->value.integer),
5101 : 17 : mpz_get_si (as->upper[i]->value.integer), i+1);
5102 : 17 : return true;
5103 : : }
5104 : : }
5105 : :
5106 : : /* If we can compute the highest index of the array section,
5107 : : then it also has to be between lower and upper. */
5108 : 50162 : mpz_init (last_value);
5109 : 50162 : if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5110 : : last_value))
5111 : : {
5112 : 29309 : if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5113 : : {
5114 : 3 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5115 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5116 : : mpz_get_si (last_value),
5117 : 3 : mpz_get_si (as->lower[i]->value.integer), i+1);
5118 : 3 : mpz_clear (last_value);
5119 : 3 : return true;
5120 : : }
5121 : 29306 : if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5122 : : {
5123 : 7 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5124 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5125 : : mpz_get_si (last_value),
5126 : 7 : mpz_get_si (as->upper[i]->value.integer), i+1);
5127 : 7 : mpz_clear (last_value);
5128 : 7 : return true;
5129 : : }
5130 : : }
5131 : 50152 : mpz_clear (last_value);
5132 : :
5133 : : #undef AR_START
5134 : : #undef AR_END
5135 : : }
5136 : 50152 : break;
5137 : :
5138 : 0 : default:
5139 : 0 : gfc_internal_error ("check_dimension(): Bad array reference");
5140 : : }
5141 : :
5142 : : return true;
5143 : : }
5144 : :
5145 : :
5146 : : /* Compare an array reference with an array specification. */
5147 : :
5148 : : static bool
5149 : 407849 : compare_spec_to_ref (gfc_array_ref *ar)
5150 : : {
5151 : 407849 : gfc_array_spec *as;
5152 : 407849 : int i;
5153 : :
5154 : 407849 : as = ar->as;
5155 : 407849 : i = as->rank - 1;
5156 : : /* TODO: Full array sections are only allowed as actual parameters. */
5157 : 407849 : if (as->type == AS_ASSUMED_SIZE
5158 : 5741 : && (/*ar->type == AR_FULL
5159 : 5741 : ||*/ (ar->type == AR_SECTION
5160 : 514 : && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5161 : : {
5162 : 5 : gfc_error ("Rightmost upper bound of assumed size array section "
5163 : : "not specified at %L", &ar->where);
5164 : 5 : return false;
5165 : : }
5166 : :
5167 : 407844 : if (ar->type == AR_FULL)
5168 : : return true;
5169 : :
5170 : 154286 : if (as->rank != ar->dimen)
5171 : : {
5172 : 28 : gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5173 : : &ar->where, ar->dimen, as->rank);
5174 : 28 : return false;
5175 : : }
5176 : :
5177 : : /* ar->codimen == 0 is a local array. */
5178 : 154258 : if (as->corank != ar->codimen && ar->codimen != 0)
5179 : : {
5180 : 0 : gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5181 : : &ar->where, ar->codimen, as->corank);
5182 : 0 : return false;
5183 : : }
5184 : :
5185 : 351006 : for (i = 0; i < as->rank; i++)
5186 : 196749 : if (!check_dimension (i, ar, as))
5187 : : return false;
5188 : :
5189 : : /* Local access has no coarray spec. */
5190 : 154257 : if (ar->codimen != 0)
5191 : 14691 : for (i = as->rank; i < as->rank + as->corank; i++)
5192 : : {
5193 : 7675 : if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5194 : 5249 : && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5195 : : {
5196 : 2 : gfc_error ("Coindex of codimension %d must be a scalar at %L",
5197 : 2 : i + 1 - as->rank, &ar->where);
5198 : 2 : return false;
5199 : : }
5200 : 7673 : if (!check_dimension (i, ar, as))
5201 : : return false;
5202 : : }
5203 : :
5204 : : return true;
5205 : : }
5206 : :
5207 : :
5208 : : /* Resolve one part of an array index. */
5209 : :
5210 : : static bool
5211 : 695005 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5212 : : int force_index_integer_kind)
5213 : : {
5214 : 695005 : gfc_typespec ts;
5215 : :
5216 : 695005 : if (index == NULL)
5217 : : return true;
5218 : :
5219 : 206137 : if (!gfc_resolve_expr (index))
5220 : : return false;
5221 : :
5222 : 206114 : if (check_scalar && index->rank != 0)
5223 : : {
5224 : 2 : gfc_error ("Array index at %L must be scalar", &index->where);
5225 : 2 : return false;
5226 : : }
5227 : :
5228 : 206112 : if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5229 : : {
5230 : 3 : gfc_error ("Array index at %L must be of INTEGER type, found %s",
5231 : : &index->where, gfc_basic_typename (index->ts.type));
5232 : 3 : return false;
5233 : : }
5234 : :
5235 : 206109 : if (index->ts.type == BT_REAL)
5236 : 336 : if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5237 : : &index->where))
5238 : : return false;
5239 : :
5240 : 206109 : if ((index->ts.kind != gfc_index_integer_kind
5241 : 201425 : && force_index_integer_kind)
5242 : 176525 : || (index->ts.type != BT_INTEGER
5243 : : && index->ts.type != BT_UNKNOWN))
5244 : : {
5245 : 29919 : gfc_clear_ts (&ts);
5246 : 29919 : ts.type = BT_INTEGER;
5247 : 29919 : ts.kind = gfc_index_integer_kind;
5248 : :
5249 : 29919 : gfc_convert_type_warn (index, &ts, 2, 0);
5250 : : }
5251 : :
5252 : : return true;
5253 : : }
5254 : :
5255 : : /* Resolve one part of an array index. */
5256 : :
5257 : : bool
5258 : 463555 : gfc_resolve_index (gfc_expr *index, int check_scalar)
5259 : : {
5260 : 463555 : return gfc_resolve_index_1 (index, check_scalar, 1);
5261 : : }
5262 : :
5263 : : /* Resolve a dim argument to an intrinsic function. */
5264 : :
5265 : : bool
5266 : 23817 : gfc_resolve_dim_arg (gfc_expr *dim)
5267 : : {
5268 : 23817 : if (dim == NULL)
5269 : : return true;
5270 : :
5271 : 23817 : if (!gfc_resolve_expr (dim))
5272 : : return false;
5273 : :
5274 : 23817 : if (dim->rank != 0)
5275 : : {
5276 : 0 : gfc_error ("Argument dim at %L must be scalar", &dim->where);
5277 : 0 : return false;
5278 : :
5279 : : }
5280 : :
5281 : 23817 : if (dim->ts.type != BT_INTEGER)
5282 : : {
5283 : 0 : gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5284 : 0 : return false;
5285 : : }
5286 : :
5287 : 23817 : if (dim->ts.kind != gfc_index_integer_kind)
5288 : : {
5289 : 15209 : gfc_typespec ts;
5290 : :
5291 : 15209 : gfc_clear_ts (&ts);
5292 : 15209 : ts.type = BT_INTEGER;
5293 : 15209 : ts.kind = gfc_index_integer_kind;
5294 : :
5295 : 15209 : gfc_convert_type_warn (dim, &ts, 2, 0);
5296 : : }
5297 : :
5298 : : return true;
5299 : : }
5300 : :
5301 : : /* Given an expression that contains array references, update those array
5302 : : references to point to the right array specifications. While this is
5303 : : filled in during matching, this information is difficult to save and load
5304 : : in a module, so we take care of it here.
5305 : :
5306 : : The idea here is that the original array reference comes from the
5307 : : base symbol. We traverse the list of reference structures, setting
5308 : : the stored reference to references. Component references can
5309 : : provide an additional array specification. */
5310 : : static void
5311 : : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5312 : :
5313 : : static bool
5314 : 897 : find_array_spec (gfc_expr *e)
5315 : : {
5316 : 897 : gfc_array_spec *as;
5317 : 897 : gfc_component *c;
5318 : 897 : gfc_ref *ref;
5319 : 897 : bool class_as = false;
5320 : :
5321 : 897 : if (e->symtree->n.sym->assoc)
5322 : : {
5323 : 217 : if (e->symtree->n.sym->assoc->target)
5324 : 217 : gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5325 : 217 : resolve_assoc_var (e->symtree->n.sym, false);
5326 : : }
5327 : :
5328 : 897 : if (e->symtree->n.sym->ts.type == BT_CLASS)
5329 : : {
5330 : 112 : as = CLASS_DATA (e->symtree->n.sym)->as;
5331 : 112 : class_as = true;
5332 : : }
5333 : : else
5334 : 785 : as = e->symtree->n.sym->as;
5335 : :
5336 : 2034 : for (ref = e->ref; ref; ref = ref->next)
5337 : 1144 : switch (ref->type)
5338 : : {
5339 : 899 : case REF_ARRAY:
5340 : 899 : if (as == NULL)
5341 : : {
5342 : 7 : locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5343 : 14 : ? ref->u.ar.where : e->where);
5344 : 7 : gfc_error ("Invalid array reference of a non-array entity at %L",
5345 : : &loc);
5346 : 7 : return false;
5347 : : }
5348 : :
5349 : 892 : ref->u.ar.as = as;
5350 : 892 : if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5351 : : as = NULL;
5352 : : break;
5353 : :
5354 : 221 : case REF_COMPONENT:
5355 : 221 : c = ref->u.c.component;
5356 : 221 : if (c->attr.dimension)
5357 : : {
5358 : 90 : if (as != NULL && !(class_as && as == c->as))
5359 : 0 : gfc_internal_error ("find_array_spec(): unused as(1)");
5360 : 90 : as = c->as;
5361 : : }
5362 : :
5363 : : break;
5364 : :
5365 : : case REF_SUBSTRING:
5366 : : case REF_INQUIRY:
5367 : : break;
5368 : : }
5369 : :
5370 : 890 : if (as != NULL)
5371 : 0 : gfc_internal_error ("find_array_spec(): unused as(2)");
5372 : :
5373 : : return true;
5374 : : }
5375 : :
5376 : :
5377 : : /* Resolve an array reference. */
5378 : :
5379 : : static bool
5380 : 408574 : resolve_array_ref (gfc_array_ref *ar)
5381 : : {
5382 : 408574 : int i, check_scalar;
5383 : 408574 : gfc_expr *e;
5384 : :
5385 : 639996 : for (i = 0; i < ar->dimen + ar->codimen; i++)
5386 : : {
5387 : 231450 : check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5388 : :
5389 : : /* Do not force gfc_index_integer_kind for the start. We can
5390 : : do fine with any integer kind. This avoids temporary arrays
5391 : : created for indexing with a vector. */
5392 : 231450 : if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5393 : : return false;
5394 : 231424 : if (!gfc_resolve_index (ar->end[i], check_scalar))
5395 : : return false;
5396 : 231422 : if (!gfc_resolve_index (ar->stride[i], check_scalar))
5397 : : return false;
5398 : :
5399 : 231422 : e = ar->start[i];
5400 : :
5401 : 231422 : if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5402 : 139758 : switch (e->rank)
5403 : : {
5404 : 138894 : case 0:
5405 : 138894 : ar->dimen_type[i] = DIMEN_ELEMENT;
5406 : 138894 : break;
5407 : :
5408 : 864 : case 1:
5409 : 864 : ar->dimen_type[i] = DIMEN_VECTOR;
5410 : 864 : if (e->expr_type == EXPR_VARIABLE
5411 : 446 : && e->symtree->n.sym->ts.type == BT_DERIVED)
5412 : 13 : ar->start[i] = gfc_get_parentheses (e);
5413 : : break;
5414 : :
5415 : 0 : default:
5416 : 0 : gfc_error ("Array index at %L is an array of rank %d",
5417 : : &ar->c_where[i], e->rank);
5418 : 0 : return false;
5419 : : }
5420 : :
5421 : : /* Fill in the upper bound, which may be lower than the
5422 : : specified one for something like a(2:10:5), which is
5423 : : identical to a(2:7:5). Only relevant for strides not equal
5424 : : to one. Don't try a division by zero. */
5425 : 231422 : if (ar->dimen_type[i] == DIMEN_RANGE
5426 : 69895 : && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5427 : 7797 : && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5428 : 7650 : && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5429 : : {
5430 : 7649 : mpz_t size, end;
5431 : :
5432 : 7649 : if (gfc_ref_dimen_size (ar, i, &size, &end))
5433 : : {
5434 : 6096 : if (ar->end[i] == NULL)
5435 : : {
5436 : 7864 : ar->end[i] =
5437 : 3932 : gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5438 : : &ar->where);
5439 : 3932 : mpz_set (ar->end[i]->value.integer, end);
5440 : : }
5441 : 2164 : else if (ar->end[i]->ts.type == BT_INTEGER
5442 : 2164 : && ar->end[i]->expr_type == EXPR_CONSTANT)
5443 : : {
5444 : 2164 : mpz_set (ar->end[i]->value.integer, end);
5445 : : }
5446 : : else
5447 : 0 : gcc_unreachable ();
5448 : :
5449 : 6096 : mpz_clear (size);
5450 : 6096 : mpz_clear (end);
5451 : : }
5452 : : }
5453 : : }
5454 : :
5455 : 408546 : if (ar->type == AR_FULL)
5456 : : {
5457 : 256505 : if (ar->as->rank == 0)
5458 : 2913 : ar->type = AR_ELEMENT;
5459 : :
5460 : : /* Make sure array is the same as array(:,:), this way
5461 : : we don't need to special case all the time. */
5462 : 256505 : ar->dimen = ar->as->rank;
5463 : 613734 : for (i = 0; i < ar->dimen; i++)
5464 : : {
5465 : 357229 : ar->dimen_type[i] = DIMEN_RANGE;
5466 : :
5467 : 357229 : gcc_assert (ar->start[i] == NULL);
5468 : 357229 : gcc_assert (ar->end[i] == NULL);
5469 : 357229 : gcc_assert (ar->stride[i] == NULL);
5470 : : }
5471 : : }
5472 : :
5473 : : /* If the reference type is unknown, figure out what kind it is. */
5474 : :
5475 : 408546 : if (ar->type == AR_UNKNOWN)
5476 : : {
5477 : 141512 : ar->type = AR_ELEMENT;
5478 : 275292 : for (i = 0; i < ar->dimen; i++)
5479 : 170124 : if (ar->dimen_type[i] == DIMEN_RANGE
5480 : 170124 : || ar->dimen_type[i] == DIMEN_VECTOR)
5481 : : {
5482 : 36344 : ar->type = AR_SECTION;
5483 : 36344 : break;
5484 : : }
5485 : : }
5486 : :
5487 : 408546 : if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5488 : : return false;
5489 : :
5490 : 408510 : if (ar->as->corank && ar->codimen == 0)
5491 : : {
5492 : 1654 : int n;
5493 : 1654 : ar->codimen = ar->as->corank;
5494 : 4544 : for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5495 : 2890 : ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5496 : : }
5497 : :
5498 : 408510 : if (ar->codimen)
5499 : : {
5500 : 10617 : if (ar->team_type == TEAM_NUMBER)
5501 : : {
5502 : 40 : if (!gfc_resolve_expr (ar->team))
5503 : : return false;
5504 : :
5505 : 40 : if (ar->team->rank != 0)
5506 : : {
5507 : 0 : gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5508 : : &ar->team->where);
5509 : 0 : return false;
5510 : : }
5511 : :
5512 : 40 : if (ar->team->ts.type != BT_INTEGER)
5513 : : {
5514 : 4 : gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5515 : : "type, found %s",
5516 : 4 : &ar->team->where,
5517 : : gfc_basic_typename (ar->team->ts.type));
5518 : 4 : return false;
5519 : : }
5520 : : }
5521 : 10577 : else if (ar->team_type == TEAM_TEAM)
5522 : : {
5523 : 28 : if (!gfc_resolve_expr (ar->team))
5524 : : return false;
5525 : :
5526 : 28 : if (ar->team->rank != 0)
5527 : : {
5528 : 2 : gfc_error ("TEAM argument at %L must be scalar",
5529 : : &ar->team->where);
5530 : 2 : return false;
5531 : : }
5532 : :
5533 : 26 : if (ar->team->ts.type != BT_DERIVED
5534 : 24 : || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5535 : 24 : || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5536 : : {
5537 : 2 : gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5538 : : "the intrinsic module ISO_FORTRAN_ENV, found %s",
5539 : 2 : &ar->team->where,
5540 : : gfc_basic_typename (ar->team->ts.type));
5541 : 2 : return false;
5542 : : }
5543 : : }
5544 : 10609 : if (ar->stat)
5545 : : {
5546 : 45 : if (!gfc_resolve_expr (ar->stat))
5547 : : return false;
5548 : :
5549 : 45 : if (ar->stat->rank != 0)
5550 : : {
5551 : 2 : gfc_error ("STAT argument at %L must be scalar",
5552 : : &ar->stat->where);
5553 : 2 : return false;
5554 : : }
5555 : :
5556 : 43 : if (ar->stat->ts.type != BT_INTEGER)
5557 : : {
5558 : 2 : gfc_error ("STAT argument at %L must be of INTEGER "
5559 : : "type, found %s",
5560 : 2 : &ar->stat->where,
5561 : : gfc_basic_typename (ar->stat->ts.type));
5562 : 2 : return false;
5563 : : }
5564 : :
5565 : 41 : if (ar->stat->expr_type != EXPR_VARIABLE)
5566 : : {
5567 : 0 : gfc_error ("STAT's expression at %L must be a variable",
5568 : : &ar->stat->where);
5569 : 0 : return false;
5570 : : }
5571 : : }
5572 : : }
5573 : : return true;
5574 : : }
5575 : :
5576 : :
5577 : : bool
5578 : 8309 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5579 : : {
5580 : 8309 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5581 : :
5582 : 8309 : if (ref->u.ss.start != NULL)
5583 : : {
5584 : 8309 : if (!gfc_resolve_expr (ref->u.ss.start))
5585 : : return false;
5586 : :
5587 : 8309 : if (ref->u.ss.start->ts.type != BT_INTEGER)
5588 : : {
5589 : 1 : gfc_error ("Substring start index at %L must be of type INTEGER",
5590 : : &ref->u.ss.start->where);
5591 : 1 : return false;
5592 : : }
5593 : :
5594 : 8308 : if (ref->u.ss.start->rank != 0)
5595 : : {
5596 : 0 : gfc_error ("Substring start index at %L must be scalar",
5597 : : &ref->u.ss.start->where);
5598 : 0 : return false;
5599 : : }
5600 : :
5601 : 8308 : if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5602 : 8308 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5603 : 37 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5604 : : {
5605 : 1 : gfc_error ("Substring start index at %L is less than one",
5606 : : &ref->u.ss.start->where);
5607 : 1 : return false;
5608 : : }
5609 : : }
5610 : :
5611 : 8307 : if (ref->u.ss.end != NULL)
5612 : : {
5613 : 8138 : if (!gfc_resolve_expr (ref->u.ss.end))
5614 : : return false;
5615 : :
5616 : 8138 : if (ref->u.ss.end->ts.type != BT_INTEGER)
5617 : : {
5618 : 1 : gfc_error ("Substring end index at %L must be of type INTEGER",
5619 : : &ref->u.ss.end->where);
5620 : 1 : return false;
5621 : : }
5622 : :
5623 : 8137 : if (ref->u.ss.end->rank != 0)
5624 : : {
5625 : 0 : gfc_error ("Substring end index at %L must be scalar",
5626 : : &ref->u.ss.end->where);
5627 : 0 : return false;
5628 : : }
5629 : :
5630 : 8137 : if (ref->u.ss.length != NULL
5631 : 7806 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5632 : 8149 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5633 : 12 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5634 : : {
5635 : 4 : gfc_error ("Substring end index at %L exceeds the string length",
5636 : : &ref->u.ss.start->where);
5637 : 4 : return false;
5638 : : }
5639 : :
5640 : 8133 : if (compare_bound_mpz_t (ref->u.ss.end,
5641 : 8133 : gfc_integer_kinds[k].huge) == CMP_GT
5642 : 8133 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5643 : 7 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5644 : : {
5645 : 4 : gfc_error ("Substring end index at %L is too large",
5646 : : &ref->u.ss.end->where);
5647 : 4 : return false;
5648 : : }
5649 : : /* If the substring has the same length as the original
5650 : : variable, the reference itself can be deleted. */
5651 : :
5652 : 8129 : if (ref->u.ss.length != NULL
5653 : 7798 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5654 : 9043 : && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5655 : 228 : *equal_length = true;
5656 : : }
5657 : :
5658 : : return true;
5659 : : }
5660 : :
5661 : :
5662 : : /* This function supplies missing substring charlens. */
5663 : :
5664 : : void
5665 : 4543 : gfc_resolve_substring_charlen (gfc_expr *e)
5666 : : {
5667 : 4543 : gfc_ref *char_ref;
5668 : 4543 : gfc_expr *start, *end;
5669 : 4543 : gfc_typespec *ts = NULL;
5670 : 4543 : mpz_t diff;
5671 : :
5672 : 8842 : for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5673 : : {
5674 : 7008 : if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5675 : : break;
5676 : 4299 : if (char_ref->type == REF_COMPONENT)
5677 : 322 : ts = &char_ref->u.c.component->ts;
5678 : : }
5679 : :
5680 : 4543 : if (!char_ref || char_ref->type == REF_INQUIRY)
5681 : 1896 : return;
5682 : :
5683 : 2709 : gcc_assert (char_ref->next == NULL);
5684 : :
5685 : 2709 : if (e->ts.u.cl)
5686 : : {
5687 : 120 : if (e->ts.u.cl->length)
5688 : 108 : gfc_free_expr (e->ts.u.cl->length);
5689 : 12 : else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5690 : : return;
5691 : : }
5692 : :
5693 : 2697 : if (!e->ts.u.cl)
5694 : 2589 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5695 : :
5696 : 2697 : if (char_ref->u.ss.start)
5697 : 2697 : start = gfc_copy_expr (char_ref->u.ss.start);
5698 : : else
5699 : 0 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5700 : :
5701 : 2697 : if (char_ref->u.ss.end)
5702 : 2647 : end = gfc_copy_expr (char_ref->u.ss.end);
5703 : 50 : else if (e->expr_type == EXPR_VARIABLE)
5704 : : {
5705 : 50 : if (!ts)
5706 : 32 : ts = &e->symtree->n.sym->ts;
5707 : 50 : end = gfc_copy_expr (ts->u.cl->length);
5708 : : }
5709 : : else
5710 : : end = NULL;
5711 : :
5712 : 2697 : if (!start || !end)
5713 : : {
5714 : 50 : gfc_free_expr (start);
5715 : 50 : gfc_free_expr (end);
5716 : 50 : return;
5717 : : }
5718 : :
5719 : : /* Length = (end - start + 1).
5720 : : Check first whether it has a constant length. */
5721 : 2647 : if (gfc_dep_difference (end, start, &diff))
5722 : : {
5723 : 2532 : gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5724 : : &e->where);
5725 : :
5726 : 2532 : mpz_add_ui (len->value.integer, diff, 1);
5727 : 2532 : mpz_clear (diff);
5728 : 2532 : e->ts.u.cl->length = len;
5729 : : /* The check for length < 0 is handled below */
5730 : : }
5731 : : else
5732 : : {
5733 : 115 : e->ts.u.cl->length = gfc_subtract (end, start);
5734 : 115 : e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5735 : : gfc_get_int_expr (gfc_charlen_int_kind,
5736 : : NULL, 1));
5737 : : }
5738 : :
5739 : : /* F2008, 6.4.1: Both the starting point and the ending point shall
5740 : : be within the range 1, 2, ..., n unless the starting point exceeds
5741 : : the ending point, in which case the substring has length zero. */
5742 : :
5743 : 2647 : if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5744 : 15 : mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5745 : :
5746 : 2647 : e->ts.u.cl->length->ts.type = BT_INTEGER;
5747 : 2647 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5748 : :
5749 : : /* Make sure that the length is simplified. */
5750 : 2647 : gfc_simplify_expr (e->ts.u.cl->length, 1);
5751 : 2647 : gfc_resolve_expr (e->ts.u.cl->length);
5752 : : }
5753 : :
5754 : :
5755 : : /* Resolve subtype references. */
5756 : :
5757 : : bool
5758 : 519450 : gfc_resolve_ref (gfc_expr *expr)
5759 : : {
5760 : 519450 : int current_part_dimension, n_components, seen_part_dimension, dim;
5761 : 519450 : gfc_ref *ref, **prev, *array_ref;
5762 : 519450 : bool equal_length;
5763 : :
5764 : 1017202 : for (ref = expr->ref; ref; ref = ref->next)
5765 : 498649 : if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5766 : : {
5767 : 897 : if (!find_array_spec (expr))
5768 : : return false;
5769 : : break;
5770 : : }
5771 : :
5772 : 1516660 : for (prev = &expr->ref; *prev != NULL;
5773 : 498710 : prev = *prev == NULL ? prev : &(*prev)->next)
5774 : 498794 : switch ((*prev)->type)
5775 : : {
5776 : 408574 : case REF_ARRAY:
5777 : 408574 : if (!resolve_array_ref (&(*prev)->u.ar))
5778 : : return false;
5779 : : break;
5780 : :
5781 : : case REF_COMPONENT:
5782 : : case REF_INQUIRY:
5783 : : break;
5784 : :
5785 : 8031 : case REF_SUBSTRING:
5786 : 8031 : equal_length = false;
5787 : 8031 : if (!gfc_resolve_substring (*prev, &equal_length))
5788 : : return false;
5789 : :
5790 : 8023 : if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5791 : : {
5792 : : /* Remove the reference and move the charlen, if any. */
5793 : 203 : ref = *prev;
5794 : 203 : *prev = ref->next;
5795 : 203 : ref->next = NULL;
5796 : 203 : expr->ts.u.cl = ref->u.ss.length;
5797 : 203 : ref->u.ss.length = NULL;
5798 : 203 : gfc_free_ref_list (ref);
5799 : : }
5800 : : break;
5801 : : }
5802 : :
5803 : : /* Check constraints on part references. */
5804 : :
5805 : 519359 : current_part_dimension = 0;
5806 : 519359 : seen_part_dimension = 0;
5807 : 519359 : n_components = 0;
5808 : 519359 : array_ref = NULL;
5809 : :
5810 : 1017841 : for (ref = expr->ref; ref; ref = ref->next)
5811 : : {
5812 : 498493 : switch (ref->type)
5813 : : {
5814 : 408491 : case REF_ARRAY:
5815 : 408491 : array_ref = ref;
5816 : 408491 : switch (ref->u.ar.type)
5817 : : {
5818 : 253590 : case AR_FULL:
5819 : : /* Coarray scalar. */
5820 : 253590 : if (ref->u.ar.as->rank == 0)
5821 : : {
5822 : : current_part_dimension = 0;
5823 : : break;
5824 : : }
5825 : : /* Fall through. */
5826 : 292634 : case AR_SECTION:
5827 : 292634 : current_part_dimension = 1;
5828 : 292634 : break;
5829 : :
5830 : 115857 : case AR_ELEMENT:
5831 : 115857 : array_ref = NULL;
5832 : 115857 : current_part_dimension = 0;
5833 : 115857 : break;
5834 : :
5835 : 0 : case AR_UNKNOWN:
5836 : 0 : gfc_internal_error ("resolve_ref(): Bad array reference");
5837 : : }
5838 : :
5839 : : break;
5840 : :
5841 : 81457 : case REF_COMPONENT:
5842 : 81457 : if (current_part_dimension || seen_part_dimension)
5843 : : {
5844 : : /* F03:C614. */
5845 : 5960 : if (ref->u.c.component->attr.pointer
5846 : 5957 : || ref->u.c.component->attr.proc_pointer
5847 : 5956 : || (ref->u.c.component->ts.type == BT_CLASS
5848 : 1 : && CLASS_DATA (ref->u.c.component)->attr.pointer))
5849 : : {
5850 : 4 : gfc_error ("Component to the right of a part reference "
5851 : : "with nonzero rank must not have the POINTER "
5852 : : "attribute at %L", &expr->where);
5853 : 4 : return false;
5854 : : }
5855 : 5956 : else if (ref->u.c.component->attr.allocatable
5856 : 5950 : || (ref->u.c.component->ts.type == BT_CLASS
5857 : 1 : && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5858 : :
5859 : : {
5860 : 7 : gfc_error ("Component to the right of a part reference "
5861 : : "with nonzero rank must not have the ALLOCATABLE "
5862 : : "attribute at %L", &expr->where);
5863 : 7 : return false;
5864 : : }
5865 : : }
5866 : :
5867 : 81446 : n_components++;
5868 : 81446 : break;
5869 : :
5870 : : case REF_SUBSTRING:
5871 : : break;
5872 : :
5873 : 725 : case REF_INQUIRY:
5874 : : /* Implement requirement in note 9.7 of F2018 that the result of the
5875 : : LEN inquiry be a scalar. */
5876 : 725 : if (ref->u.i == INQUIRY_LEN && array_ref
5877 : 40 : && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
5878 : 40 : || expr->ts.type == BT_INTEGER))
5879 : : {
5880 : 14 : array_ref->u.ar.type = AR_ELEMENT;
5881 : 14 : expr->rank = 0;
5882 : : /* INQUIRY_LEN is not evaluated from the rest of the expr
5883 : : but directly from the string length. This means that setting
5884 : : the array indices to one does not matter but might trigger
5885 : : a runtime bounds error. Suppress the check. */
5886 : 14 : expr->no_bounds_check = 1;
5887 : 28 : for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
5888 : : {
5889 : 14 : array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
5890 : 14 : if (array_ref->u.ar.start[dim])
5891 : 0 : gfc_free_expr (array_ref->u.ar.start[dim]);
5892 : 14 : array_ref->u.ar.start[dim]
5893 : 14 : = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
5894 : 14 : if (array_ref->u.ar.end[dim])
5895 : 0 : gfc_free_expr (array_ref->u.ar.end[dim]);
5896 : 14 : if (array_ref->u.ar.stride[dim])
5897 : 0 : gfc_free_expr (array_ref->u.ar.stride[dim]);
5898 : : }
5899 : : }
5900 : : break;
5901 : : }
5902 : :
5903 : 498482 : if (((ref->type == REF_COMPONENT && n_components > 1)
5904 : 486872 : || ref->next == NULL)
5905 : : && current_part_dimension
5906 : 440620 : && seen_part_dimension)
5907 : : {
5908 : 0 : gfc_error ("Two or more part references with nonzero rank must "
5909 : : "not be specified at %L", &expr->where);
5910 : 0 : return false;
5911 : : }
5912 : :
5913 : 498482 : if (ref->type == REF_COMPONENT)
5914 : : {
5915 : 81446 : if (current_part_dimension)
5916 : 5768 : seen_part_dimension = 1;
5917 : :
5918 : : /* reset to make sure */
5919 : : current_part_dimension = 0;
5920 : : }
5921 : : }
5922 : :
5923 : : return true;
5924 : : }
5925 : :
5926 : :
5927 : : /* Given an expression, determine its shape. This is easier than it sounds.
5928 : : Leaves the shape array NULL if it is not possible to determine the shape. */
5929 : :
5930 : : static void
5931 : 2532627 : expression_shape (gfc_expr *e)
5932 : : {
5933 : 2532627 : mpz_t array[GFC_MAX_DIMENSIONS];
5934 : 2532627 : int i;
5935 : :
5936 : 2532627 : if (e->rank <= 0 || e->shape != NULL)
5937 : 2365414 : return;
5938 : :
5939 : 678710 : for (i = 0; i < e->rank; i++)
5940 : 458222 : if (!gfc_array_dimen_size (e, i, &array[i]))
5941 : 167213 : goto fail;
5942 : :
5943 : 220488 : e->shape = gfc_get_shape (e->rank);
5944 : :
5945 : 220488 : memcpy (e->shape, array, e->rank * sizeof (mpz_t));
5946 : :
5947 : 220488 : return;
5948 : :
5949 : 167213 : fail:
5950 : 168884 : for (i--; i >= 0; i--)
5951 : 1671 : mpz_clear (array[i]);
5952 : : }
5953 : :
5954 : :
5955 : : /* Given a variable expression node, compute the rank of the expression by
5956 : : examining the base symbol and any reference structures it may have. */
5957 : :
5958 : : void
5959 : 2532627 : gfc_expression_rank (gfc_expr *e)
5960 : : {
5961 : 2532627 : gfc_ref *ref, *last_arr_ref = nullptr;
5962 : 2532627 : int i, rank, corank;
5963 : :
5964 : : /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
5965 : : could lead to serious confusion... */
5966 : 2532627 : gcc_assert (e->expr_type != EXPR_COMPCALL);
5967 : :
5968 : 2532627 : if (e->ref == NULL)
5969 : : {
5970 : 1875083 : if (e->expr_type == EXPR_ARRAY)
5971 : 67042 : goto done;
5972 : : /* Constructors can have a rank different from one via RESHAPE(). */
5973 : :
5974 : 1808041 : if (e->symtree != NULL)
5975 : : {
5976 : : /* After errors the ts.u.derived of a CLASS might not be set. */
5977 : 1808029 : gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
5978 : 13571 : && e->symtree->n.sym->ts.u.derived
5979 : 13566 : && CLASS_DATA (e->symtree->n.sym))
5980 : 1808029 : ? CLASS_DATA (e->symtree->n.sym)->as
5981 : : : e->symtree->n.sym->as;
5982 : 1808029 : if (as)
5983 : : {
5984 : 580 : e->rank = as->rank;
5985 : 580 : e->corank = as->corank;
5986 : 580 : goto done;
5987 : : }
5988 : : }
5989 : 1807461 : e->rank = 0;
5990 : 1807461 : e->corank = 0;
5991 : 1807461 : goto done;
5992 : : }
5993 : :
5994 : : rank = 0;
5995 : : corank = 0;
5996 : :
5997 : 1031385 : for (ref = e->ref; ref; ref = ref->next)
5998 : : {
5999 : 753047 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6000 : 535 : && ref->u.c.component->attr.function && !ref->next)
6001 : : {
6002 : 344 : rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6003 : 344 : corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6004 : : }
6005 : :
6006 : 753047 : if (ref->type != REF_ARRAY)
6007 : 145582 : continue;
6008 : :
6009 : 607465 : last_arr_ref = ref;
6010 : 607465 : if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6011 : : {
6012 : 335716 : rank = ref->u.ar.as->rank;
6013 : 335716 : break;
6014 : : }
6015 : :
6016 : 271749 : if (ref->u.ar.type == AR_SECTION)
6017 : : {
6018 : : /* Figure out the rank of the section. */
6019 : 43490 : if (rank != 0)
6020 : 0 : gfc_internal_error ("gfc_expression_rank(): Two array specs");
6021 : :
6022 : 108750 : for (i = 0; i < ref->u.ar.dimen; i++)
6023 : 65260 : if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6024 : 65260 : || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6025 : 57044 : rank++;
6026 : :
6027 : : break;
6028 : : }
6029 : : }
6030 : 657544 : if (last_arr_ref && last_arr_ref->u.ar.as
6031 : 590674 : && last_arr_ref->u.ar.as->rank != -1)
6032 : : {
6033 : 14821 : for (i = last_arr_ref->u.ar.as->rank;
6034 : 597415 : i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6035 : : {
6036 : : /* For unknown dimen in non-resolved as assume full corank. */
6037 : 15548 : if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6038 : 15087 : || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6039 : 266 : && !last_arr_ref->u.ar.as->resolved))
6040 : : {
6041 : : corank = last_arr_ref->u.ar.as->corank;
6042 : : break;
6043 : : }
6044 : 14821 : else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6045 : 14821 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6046 : 14738 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6047 : 12831 : corank++;
6048 : 1990 : else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6049 : 0 : gfc_internal_error ("Illegal coarray index");
6050 : : }
6051 : : }
6052 : :
6053 : 657544 : e->rank = rank;
6054 : 657544 : e->corank = corank;
6055 : :
6056 : 2532627 : done:
6057 : 2532627 : expression_shape (e);
6058 : 2532627 : }
6059 : :
6060 : :
6061 : : /* Given two expressions, check that their rank is conformable, i.e. either
6062 : : both have the same rank or at least one is a scalar. */
6063 : :
6064 : : bool
6065 : 12175604 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6066 : : {
6067 : 12175604 : if (op1->expr_type == EXPR_VARIABLE)
6068 : 719412 : gfc_expression_rank (op1);
6069 : 12175604 : if (op2->expr_type == EXPR_VARIABLE)
6070 : 442186 : gfc_expression_rank (op2);
6071 : :
6072 : 73862 : return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6073 : 12249140 : && (op1->corank == 0 || op2->corank == 0
6074 : 146 : || op1->corank == op2->corank);
6075 : : }
6076 : :
6077 : : /* Resolve a variable expression. */
6078 : :
6079 : : static bool
6080 : 1287180 : resolve_variable (gfc_expr *e)
6081 : : {
6082 : 1287180 : gfc_symbol *sym;
6083 : 1287180 : bool t;
6084 : :
6085 : 1287180 : t = true;
6086 : :
6087 : 1287180 : if (e->symtree == NULL)
6088 : : return false;
6089 : 1286761 : sym = e->symtree->n.sym;
6090 : :
6091 : : /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6092 : : as ts.type is set to BT_ASSUMED in resolve_symbol. */
6093 : 1286761 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6094 : : {
6095 : 167 : if (!actual_arg || inquiry_argument)
6096 : : {
6097 : 2 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6098 : : "be used as actual argument", sym->name, &e->where);
6099 : 2 : return false;
6100 : : }
6101 : : }
6102 : : /* TS 29113, 407b. */
6103 : 1286594 : else if (e->ts.type == BT_ASSUMED)
6104 : : {
6105 : 555 : if (!actual_arg)
6106 : : {
6107 : 20 : gfc_error ("Assumed-type variable %s at %L may only be used "
6108 : : "as actual argument", sym->name, &e->where);
6109 : 20 : return false;
6110 : : }
6111 : 535 : else if (inquiry_argument && !first_actual_arg)
6112 : : {
6113 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6114 : : for all inquiry functions in resolve_function; the reason is
6115 : : that the function-name resolution happens too late in that
6116 : : function. */
6117 : 0 : gfc_error ("Assumed-type variable %s at %L as actual argument to "
6118 : : "an inquiry function shall be the first argument",
6119 : : sym->name, &e->where);
6120 : 0 : return false;
6121 : : }
6122 : : }
6123 : : /* TS 29113, C535b. */
6124 : 1286039 : else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6125 : 35686 : && sym->ts.u.derived && CLASS_DATA (sym)
6126 : 35681 : && CLASS_DATA (sym)->as
6127 : 13695 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6128 : 1285129 : || (sym->ts.type != BT_CLASS && sym->as
6129 : 351908 : && sym->as->type == AS_ASSUMED_RANK))
6130 : 7888 : && !sym->attr.select_rank_temporary
6131 : 7888 : && !(sym->assoc && sym->assoc->ar))
6132 : : {
6133 : 7888 : if (!actual_arg
6134 : 1247 : && !(cs_base && cs_base->current
6135 : 1246 : && (cs_base->current->op == EXEC_SELECT_RANK
6136 : 188 : || sym->attr.target)))
6137 : : {
6138 : 144 : gfc_error ("Assumed-rank variable %s at %L may only be used as "
6139 : : "actual argument", sym->name, &e->where);
6140 : 144 : return false;
6141 : : }
6142 : 7744 : else if (inquiry_argument && !first_actual_arg)
6143 : : {
6144 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6145 : : for all inquiry functions in resolve_function; the reason is
6146 : : that the function-name resolution happens too late in that
6147 : : function. */
6148 : 0 : gfc_error ("Assumed-rank variable %s at %L as actual argument "
6149 : : "to an inquiry function shall be the first argument",
6150 : : sym->name, &e->where);
6151 : 0 : return false;
6152 : : }
6153 : : }
6154 : :
6155 : 1286595 : if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6156 : 165 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6157 : 164 : && e->ref->next == NULL))
6158 : : {
6159 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6160 : : "a subobject reference", sym->name, &e->ref->u.ar.where);
6161 : 1 : return false;
6162 : : }
6163 : : /* TS 29113, 407b. */
6164 : 1286594 : else if (e->ts.type == BT_ASSUMED && e->ref
6165 : 655 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6166 : 648 : && e->ref->next == NULL))
6167 : : {
6168 : 7 : gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6169 : : "reference", sym->name, &e->ref->u.ar.where);
6170 : 7 : return false;
6171 : : }
6172 : :
6173 : : /* TS 29113, C535b. */
6174 : 1286587 : if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6175 : 35686 : && sym->ts.u.derived && CLASS_DATA (sym)
6176 : 35681 : && CLASS_DATA (sym)->as
6177 : 13695 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6178 : 1285677 : || (sym->ts.type != BT_CLASS && sym->as
6179 : 352412 : && sym->as->type == AS_ASSUMED_RANK))
6180 : 8012 : && !(sym->assoc && sym->assoc->ar)
6181 : 8012 : && e->ref
6182 : 8012 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6183 : 8008 : && e->ref->next == NULL))
6184 : : {
6185 : 4 : gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6186 : : "reference", sym->name, &e->ref->u.ar.where);
6187 : 4 : return false;
6188 : : }
6189 : :
6190 : : /* Guessed type variables are associate_names whose selector had not been
6191 : : parsed at the time that the construct was parsed. Now the namespace is
6192 : : being resolved, the TKR of the selector will be available for fixup of
6193 : : the associate_name. */
6194 : 1286583 : if (IS_INFERRED_TYPE (e) && e->ref)
6195 : : {
6196 : 372 : gfc_fixup_inferred_type_refs (e);
6197 : : /* KIND inquiry ref returns the kind of the target. */
6198 : 372 : if (e->expr_type == EXPR_CONSTANT)
6199 : : return true;
6200 : : }
6201 : 1286211 : else if (sym->attr.select_type_temporary
6202 : 8789 : && sym->ns->assoc_name_inferred)
6203 : 92 : gfc_fixup_inferred_type_refs (e);
6204 : :
6205 : : /* For variables that are used in an associate (target => object) where
6206 : : the object's basetype is array valued while the target is scalar,
6207 : : the ts' type of the component refs is still array valued, which
6208 : : can't be translated that way. */
6209 : 1286571 : if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6210 : 561 : && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6211 : 561 : && sym->assoc->target->ts.u.derived
6212 : 561 : && CLASS_DATA (sym->assoc->target)
6213 : 561 : && CLASS_DATA (sym->assoc->target)->as)
6214 : : {
6215 : : gfc_ref *ref = e->ref;
6216 : 657 : while (ref)
6217 : : {
6218 : 499 : switch (ref->type)
6219 : : {
6220 : 216 : case REF_COMPONENT:
6221 : 216 : ref->u.c.sym = sym->ts.u.derived;
6222 : : /* Stop the loop. */
6223 : 216 : ref = NULL;
6224 : 216 : break;
6225 : 283 : default:
6226 : 283 : ref = ref->next;
6227 : 283 : break;
6228 : : }
6229 : : }
6230 : : }
6231 : :
6232 : : /* If this is an associate-name, it may be parsed with an array reference
6233 : : in error even though the target is scalar. Fail directly in this case.
6234 : : TODO Understand why class scalar expressions must be excluded. */
6235 : 1286571 : if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6236 : : {
6237 : 10852 : if (sym->ts.type == BT_CLASS)
6238 : 242 : gfc_fix_class_refs (e);
6239 : 10852 : if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6240 : 2009 : && e->ref->type == REF_ARRAY)
6241 : : {
6242 : : /* Unambiguously scalar! */
6243 : 3 : if (sym->assoc->target
6244 : 3 : && (sym->assoc->target->expr_type == EXPR_CONSTANT
6245 : 1 : || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6246 : 2 : gfc_error ("Scalar variable %qs has an array reference at %L",
6247 : : sym->name, &e->where);
6248 : 3 : return false;
6249 : : }
6250 : 10849 : else if ((sym->attr.dimension || sym->attr.codimension)
6251 : 6797 : && (!e->ref || e->ref->type != REF_ARRAY))
6252 : : {
6253 : : /* This can happen because the parser did not detect that the
6254 : : associate name is an array and the expression had no array
6255 : : part_ref. */
6256 : 140 : gfc_ref *ref = gfc_get_ref ();
6257 : 140 : ref->type = REF_ARRAY;
6258 : 140 : ref->u.ar.type = AR_FULL;
6259 : 140 : if (sym->as)
6260 : : {
6261 : 139 : ref->u.ar.as = sym->as;
6262 : 139 : ref->u.ar.dimen = sym->as->rank;
6263 : : }
6264 : 140 : ref->next = e->ref;
6265 : 140 : e->ref = ref;
6266 : : }
6267 : : }
6268 : :
6269 : 1286568 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6270 : 0 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6271 : :
6272 : : /* On the other hand, the parser may not have known this is an array;
6273 : : in this case, we have to add a FULL reference. */
6274 : 1286568 : if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6275 : : {
6276 : 0 : e->ref = gfc_get_ref ();
6277 : 0 : e->ref->type = REF_ARRAY;
6278 : 0 : e->ref->u.ar.type = AR_FULL;
6279 : 0 : e->ref->u.ar.dimen = 0;
6280 : : }
6281 : :
6282 : : /* Like above, but for class types, where the checking whether an array
6283 : : ref is present is more complicated. Furthermore make sure not to add
6284 : : the full array ref to _vptr or _len refs. */
6285 : 1286568 : if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6286 : 966 : && CLASS_DATA (sym)
6287 : 966 : && (CLASS_DATA (sym)->attr.dimension
6288 : 415 : || CLASS_DATA (sym)->attr.codimension)
6289 : 555 : && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6290 : : {
6291 : 531 : gfc_ref *ref, *newref;
6292 : :
6293 : 531 : newref = gfc_get_ref ();
6294 : 531 : newref->type = REF_ARRAY;
6295 : 531 : newref->u.ar.type = AR_FULL;
6296 : 531 : newref->u.ar.dimen = 0;
6297 : :
6298 : : /* Because this is an associate var and the first ref either is a ref to
6299 : : the _data component or not, no traversal of the ref chain is
6300 : : needed. The array ref needs to be inserted after the _data ref,
6301 : : or when that is not present, which may happened for polymorphic
6302 : : types, then at the first position. */
6303 : 531 : ref = e->ref;
6304 : 531 : if (!ref)
6305 : 18 : e->ref = newref;
6306 : 513 : else if (ref->type == REF_COMPONENT
6307 : 230 : && strcmp ("_data", ref->u.c.component->name) == 0)
6308 : : {
6309 : 230 : if (!ref->next || ref->next->type != REF_ARRAY)
6310 : : {
6311 : 12 : newref->next = ref->next;
6312 : 12 : ref->next = newref;
6313 : : }
6314 : : else
6315 : : /* Array ref present already. */
6316 : 218 : gfc_free_ref_list (newref);
6317 : : }
6318 : 283 : else if (ref->type == REF_ARRAY)
6319 : : /* Array ref present already. */
6320 : 283 : gfc_free_ref_list (newref);
6321 : : else
6322 : : {
6323 : 0 : newref->next = ref;
6324 : 0 : e->ref = newref;
6325 : : }
6326 : : }
6327 : 1286037 : else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6328 : : {
6329 : 485 : gfc_ref *ref;
6330 : 908 : for (ref = e->ref; ref; ref = ref->next)
6331 : 453 : if (ref->type == REF_SUBSTRING)
6332 : : break;
6333 : 485 : if (ref == NULL)
6334 : 455 : e->ts = sym->ts;
6335 : : }
6336 : :
6337 : 1286568 : if (e->ref && !gfc_resolve_ref (e))
6338 : : return false;
6339 : :
6340 : 1286482 : if (sym->attr.flavor == FL_PROCEDURE
6341 : 29375 : && (!sym->attr.function
6342 : 17113 : || (sym->attr.function && sym->result
6343 : 16667 : && sym->result->attr.proc_pointer
6344 : 562 : && !sym->result->attr.function)))
6345 : : {
6346 : 12262 : e->ts.type = BT_PROCEDURE;
6347 : 12262 : goto resolve_procedure;
6348 : : }
6349 : :
6350 : 1274220 : if (sym->ts.type != BT_UNKNOWN)
6351 : 1273581 : gfc_variable_attr (e, &e->ts);
6352 : 639 : else if (sym->attr.flavor == FL_PROCEDURE
6353 : 12 : && sym->attr.function && sym->result
6354 : 12 : && sym->result->ts.type != BT_UNKNOWN
6355 : 10 : && sym->result->attr.proc_pointer)
6356 : 10 : e->ts = sym->result->ts;
6357 : : else
6358 : : {
6359 : : /* Must be a simple variable reference. */
6360 : 629 : if (!gfc_set_default_type (sym, 1, sym->ns))
6361 : : return false;
6362 : 504 : e->ts = sym->ts;
6363 : : }
6364 : :
6365 : 1274095 : if (check_assumed_size_reference (sym, e))
6366 : : return false;
6367 : :
6368 : : /* Deal with forward references to entries during gfc_resolve_code, to
6369 : : satisfy, at least partially, 12.5.2.5. */
6370 : 1274076 : if (gfc_current_ns->entries
6371 : 3060 : && current_entry_id == sym->entry_id
6372 : 1000 : && cs_base
6373 : 914 : && cs_base->current
6374 : 914 : && cs_base->current->op != EXEC_ENTRY)
6375 : : {
6376 : 914 : gfc_entry_list *entry;
6377 : 914 : gfc_formal_arglist *formal;
6378 : 914 : int n;
6379 : 914 : bool seen, saved_specification_expr;
6380 : :
6381 : : /* If the symbol is a dummy... */
6382 : 914 : if (sym->attr.dummy && sym->ns == gfc_current_ns)
6383 : : {
6384 : : entry = gfc_current_ns->entries;
6385 : : seen = false;
6386 : :
6387 : : /* ...test if the symbol is a parameter of previous entries. */
6388 : 1033 : for (; entry && entry->id <= current_entry_id; entry = entry->next)
6389 : 1006 : for (formal = entry->sym->formal; formal; formal = formal->next)
6390 : : {
6391 : 997 : if (formal->sym && sym->name == formal->sym->name)
6392 : : {
6393 : : seen = true;
6394 : : break;
6395 : : }
6396 : : }
6397 : :
6398 : : /* If it has not been seen as a dummy, this is an error. */
6399 : 453 : if (!seen)
6400 : : {
6401 : 3 : if (specification_expr)
6402 : 2 : gfc_error ("Variable %qs, used in a specification expression"
6403 : : ", is referenced at %L before the ENTRY statement "
6404 : : "in which it is a parameter",
6405 : : sym->name, &cs_base->current->loc);
6406 : : else
6407 : 1 : gfc_error ("Variable %qs is used at %L before the ENTRY "
6408 : : "statement in which it is a parameter",
6409 : : sym->name, &cs_base->current->loc);
6410 : : t = false;
6411 : : }
6412 : : }
6413 : :
6414 : : /* Now do the same check on the specification expressions. */
6415 : 914 : saved_specification_expr = specification_expr;
6416 : 914 : specification_expr = true;
6417 : 914 : if (sym->ts.type == BT_CHARACTER
6418 : 914 : && !gfc_resolve_expr (sym->ts.u.cl->length))
6419 : : t = false;
6420 : :
6421 : 914 : if (sym->as)
6422 : : {
6423 : 271 : for (n = 0; n < sym->as->rank; n++)
6424 : : {
6425 : 159 : if (!gfc_resolve_expr (sym->as->lower[n]))
6426 : 0 : t = false;
6427 : 159 : if (!gfc_resolve_expr (sym->as->upper[n]))
6428 : 1 : t = false;
6429 : : }
6430 : : }
6431 : 914 : specification_expr = saved_specification_expr;
6432 : :
6433 : 914 : if (t)
6434 : : /* Update the symbol's entry level. */
6435 : 909 : sym->entry_id = current_entry_id + 1;
6436 : : }
6437 : :
6438 : : /* If a symbol has been host_associated mark it. This is used latter,
6439 : : to identify if aliasing is possible via host association. */
6440 : 1274076 : if (sym->attr.flavor == FL_VARIABLE
6441 : 1238207 : && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6442 : 5280 : || !sym->ns->code->ext.block.assoc)
6443 : 1236746 : && gfc_current_ns->parent
6444 : 592051 : && (gfc_current_ns->parent == sym->ns
6445 : 555234 : || (gfc_current_ns->parent->parent
6446 : 10618 : && gfc_current_ns->parent->parent == sym->ns)))
6447 : 43169 : sym->attr.host_assoc = 1;
6448 : :
6449 : 1274076 : if (gfc_current_ns->proc_name
6450 : 1270705 : && sym->attr.dimension
6451 : 347036 : && (sym->ns != gfc_current_ns
6452 : 323507 : || sym->attr.use_assoc
6453 : 319556 : || sym->attr.in_common))
6454 : 32266 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6455 : :
6456 : 1286338 : resolve_procedure:
6457 : 1286338 : if (t && !resolve_procedure_expression (e))
6458 : : t = false;
6459 : :
6460 : : /* F2008, C617 and C1229. */
6461 : 1285311 : if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6462 : 1378653 : && gfc_is_coindexed (e))
6463 : : {
6464 : 304 : gfc_ref *ref, *ref2 = NULL;
6465 : :
6466 : 383 : for (ref = e->ref; ref; ref = ref->next)
6467 : : {
6468 : 383 : if (ref->type == REF_COMPONENT)
6469 : 79 : ref2 = ref;
6470 : 383 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6471 : : break;
6472 : : }
6473 : :
6474 : 608 : for ( ; ref; ref = ref->next)
6475 : 316 : if (ref->type == REF_COMPONENT)
6476 : : break;
6477 : :
6478 : : /* Expression itself is not coindexed object. */
6479 : 304 : if (ref && e->ts.type == BT_CLASS)
6480 : : {
6481 : 3 : gfc_error ("Polymorphic subobject of coindexed object at %L",
6482 : : &e->where);
6483 : 3 : t = false;
6484 : : }
6485 : :
6486 : : /* Expression itself is coindexed object. */
6487 : 292 : if (ref == NULL)
6488 : : {
6489 : 292 : gfc_component *c;
6490 : 292 : c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6491 : 408 : for ( ; c; c = c->next)
6492 : 116 : if (c->attr.allocatable && c->ts.type == BT_CLASS)
6493 : : {
6494 : 0 : gfc_error ("Coindexed object with polymorphic allocatable "
6495 : : "subcomponent at %L", &e->where);
6496 : 0 : t = false;
6497 : 0 : break;
6498 : : }
6499 : : }
6500 : : }
6501 : :
6502 : 1286338 : if (t)
6503 : 1286330 : gfc_expression_rank (e);
6504 : :
6505 : 1286338 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6506 : 3 : gfc_warning (OPT_Wdeprecated_declarations,
6507 : : "Using variable %qs at %L is deprecated",
6508 : : sym->name, &e->where);
6509 : : /* Simplify cases where access to a parameter array results in a
6510 : : single constant. Suppress errors since those will have been
6511 : : issued before, as warnings. */
6512 : 1286338 : if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6513 : : {
6514 : 2590 : gfc_push_suppress_errors ();
6515 : 2590 : gfc_simplify_expr (e, 1);
6516 : 2590 : gfc_pop_suppress_errors ();
6517 : : }
6518 : :
6519 : : return t;
6520 : : }
6521 : :
6522 : :
6523 : : /* 'sym' was initially guessed to be derived type but has been corrected
6524 : : in resolve_assoc_var to be a class entity or the derived type correcting.
6525 : : If a class entity it will certainly need the _data reference or the
6526 : : reference derived type symbol correcting in the first component ref if
6527 : : a derived type. */
6528 : :
6529 : : void
6530 : 844 : gfc_fixup_inferred_type_refs (gfc_expr *e)
6531 : : {
6532 : 844 : gfc_ref *ref, *new_ref;
6533 : 844 : gfc_symbol *sym, *derived;
6534 : 844 : gfc_expr *target;
6535 : 844 : sym = e->symtree->n.sym;
6536 : :
6537 : : /* An associate_name whose selector is (i) a component ref of a selector
6538 : : that is a inferred type associate_name; or (ii) an intrinsic type that
6539 : : has been inferred from an inquiry ref. */
6540 : 844 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6541 : : {
6542 : 282 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6543 : 282 : sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6544 : 282 : if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6545 : : {
6546 : 60 : ref = e->ref;
6547 : : /* A substring misidentified as an array section. */
6548 : 60 : if (sym->ts.type == BT_CHARACTER
6549 : 30 : && ref->u.ar.start[0] && ref->u.ar.end[0]
6550 : 6 : && !ref->u.ar.stride[0])
6551 : : {
6552 : 6 : new_ref = gfc_get_ref ();
6553 : 6 : new_ref->type = REF_SUBSTRING;
6554 : 6 : new_ref->u.ss.start = ref->u.ar.start[0];
6555 : 6 : new_ref->u.ss.end = ref->u.ar.end[0];
6556 : 6 : new_ref->u.ss.length = sym->ts.u.cl;
6557 : 6 : *ref = *new_ref;
6558 : 6 : free (new_ref);
6559 : : }
6560 : : else
6561 : : {
6562 : 54 : if (e->ref->u.ar.type == AR_UNKNOWN)
6563 : 24 : gfc_error ("Invalid array reference at %L", &e->where);
6564 : 54 : e->ref = ref->next;
6565 : 54 : free (ref);
6566 : : }
6567 : : }
6568 : :
6569 : : /* It is possible for an inquiry reference to be mistaken for a
6570 : : component reference. Correct this now. */
6571 : 282 : ref = e->ref;
6572 : 282 : if (ref && ref->type == REF_ARRAY)
6573 : 138 : ref = ref->next;
6574 : 150 : if (ref && ref->type == REF_COMPONENT
6575 : 150 : && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6576 : : {
6577 : 12 : e->symtree->n.sym = sym;
6578 : 12 : *ref = *new_ref;
6579 : 12 : gfc_free_ref_list (new_ref);
6580 : : }
6581 : :
6582 : : /* The kind of the associate name is best evaluated directly from the
6583 : : selector because of the guesses made in primary.cc, when the type
6584 : : is still unknown. */
6585 : 282 : if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6586 : : {
6587 : 24 : gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6588 : 12 : sym->assoc->target->ts.kind);
6589 : 12 : gfc_replace_expr (e, ne);
6590 : : }
6591 : :
6592 : : /* Now that the references are all sorted out, set the expression rank
6593 : : and return. */
6594 : 282 : gfc_expression_rank (e);
6595 : 282 : return;
6596 : : }
6597 : :
6598 : 562 : derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6599 : : : sym->ts.u.derived;
6600 : :
6601 : : /* Ensure that class symbols have an array spec and ensure that there
6602 : : is a _data field reference following class type references. */
6603 : 562 : if (sym->ts.type == BT_CLASS
6604 : 196 : && sym->assoc->target->ts.type == BT_CLASS)
6605 : : {
6606 : 196 : e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6607 : 196 : e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6608 : 196 : sym->attr.dimension = 0;
6609 : 196 : sym->attr.codimension = 0;
6610 : 196 : CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6611 : 196 : CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6612 : 196 : if (e->ref && (e->ref->type != REF_COMPONENT
6613 : 160 : || e->ref->u.c.component->name[0] != '_'))
6614 : : {
6615 : 82 : ref = gfc_get_ref ();
6616 : 82 : ref->type = REF_COMPONENT;
6617 : 82 : ref->next = e->ref;
6618 : 82 : e->ref = ref;
6619 : 82 : ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6620 : : true, true, NULL);
6621 : 82 : ref->u.c.sym = sym->ts.u.derived;
6622 : : }
6623 : : }
6624 : :
6625 : : /* Proceed as far as the first component reference and ensure that the
6626 : : correct derived type is being used. */
6627 : 825 : for (ref = e->ref; ref; ref = ref->next)
6628 : 789 : if (ref->type == REF_COMPONENT)
6629 : : {
6630 : 526 : if (ref->u.c.component->name[0] != '_')
6631 : 330 : ref->u.c.sym = derived;
6632 : : else
6633 : 196 : ref->u.c.sym = sym->ts.u.derived;
6634 : : break;
6635 : : }
6636 : :
6637 : : /* Verify that the type inferrence mechanism has not introduced a spurious
6638 : : array reference. This can happen with an associate name, whose selector
6639 : : is an element of another inferred type. */
6640 : 562 : target = e->symtree->n.sym->assoc->target;
6641 : 562 : if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6642 : 150 : && e != target && !target->rank)
6643 : : {
6644 : : /* First case: array ref after the scalar class or derived
6645 : : associate_name. */
6646 : 150 : if (e->ref && e->ref->type == REF_ARRAY
6647 : 7 : && e->ref->u.ar.type != AR_ELEMENT)
6648 : : {
6649 : 7 : ref = e->ref;
6650 : 7 : if (ref->u.ar.type == AR_UNKNOWN)
6651 : 1 : gfc_error ("Invalid array reference at %L", &e->where);
6652 : 7 : e->ref = ref->next;
6653 : 7 : free (ref);
6654 : :
6655 : : /* If it hasn't a ref to the '_data' field supply one. */
6656 : 7 : if (sym->ts.type == BT_CLASS
6657 : 0 : && !(e->ref->type == REF_COMPONENT
6658 : 0 : && strcmp (e->ref->u.c.component->name, "_data")))
6659 : : {
6660 : 0 : gfc_ref *new_ref;
6661 : 0 : gfc_find_component (e->symtree->n.sym->ts.u.derived,
6662 : : "_data", true, true, &new_ref);
6663 : 0 : new_ref->next = e->ref;
6664 : 0 : e->ref = new_ref;
6665 : : }
6666 : : }
6667 : : /* 2nd case: a ref to the '_data' field followed by an array ref. */
6668 : 143 : else if (e->ref && e->ref->type == REF_COMPONENT
6669 : 143 : && strcmp (e->ref->u.c.component->name, "_data") == 0
6670 : 64 : && e->ref->next && e->ref->next->type == REF_ARRAY
6671 : 0 : && e->ref->next->u.ar.type != AR_ELEMENT)
6672 : : {
6673 : 0 : ref = e->ref->next;
6674 : 0 : if (ref->u.ar.type == AR_UNKNOWN)
6675 : 0 : gfc_error ("Invalid array reference at %L", &e->where);
6676 : 0 : e->ref->next = e->ref->next->next;
6677 : 0 : free (ref);
6678 : : }
6679 : : }
6680 : :
6681 : : /* Now that all the references are OK, get the expression rank. */
6682 : 562 : gfc_expression_rank (e);
6683 : : }
6684 : :
6685 : :
6686 : : /* Checks to see that the correct symbol has been host associated.
6687 : : The only situations where this arises are:
6688 : : (i) That in which a twice contained function is parsed after
6689 : : the host association is made. On detecting this, change
6690 : : the symbol in the expression and convert the array reference
6691 : : into an actual arglist if the old symbol is a variable; or
6692 : : (ii) That in which an external function is typed but not declared
6693 : : explicitly to be external. Here, the old symbol is changed
6694 : : from a variable to an external function. */
6695 : : static bool
6696 : 1622161 : check_host_association (gfc_expr *e)
6697 : : {
6698 : 1622161 : gfc_symbol *sym, *old_sym;
6699 : 1622161 : gfc_symtree *st;
6700 : 1622161 : int n;
6701 : 1622161 : gfc_ref *ref;
6702 : 1622161 : gfc_actual_arglist *arg, *tail = NULL;
6703 : 1622161 : bool retval = e->expr_type == EXPR_FUNCTION;
6704 : :
6705 : : /* If the expression is the result of substitution in
6706 : : interface.cc(gfc_extend_expr) because there is no way in
6707 : : which the host association can be wrong. */
6708 : 1622161 : if (e->symtree == NULL
6709 : 1621426 : || e->symtree->n.sym == NULL
6710 : 1621426 : || e->user_operator)
6711 : : return retval;
6712 : :
6713 : 1619716 : old_sym = e->symtree->n.sym;
6714 : :
6715 : 1619716 : if (gfc_current_ns->parent
6716 : 712484 : && old_sym->ns != gfc_current_ns)
6717 : : {
6718 : : /* Use the 'USE' name so that renamed module symbols are
6719 : : correctly handled. */
6720 : 86869 : gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6721 : :
6722 : 86869 : if (sym && old_sym != sym
6723 : 546 : && sym->attr.flavor == FL_PROCEDURE
6724 : 93 : && sym->attr.contained)
6725 : : {
6726 : : /* Clear the shape, since it might not be valid. */
6727 : 71 : gfc_free_shape (&e->shape, e->rank);
6728 : :
6729 : : /* Give the expression the right symtree! */
6730 : 71 : gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6731 : 71 : gcc_assert (st != NULL);
6732 : :
6733 : 71 : if (old_sym->attr.flavor == FL_PROCEDURE
6734 : 47 : || e->expr_type == EXPR_FUNCTION)
6735 : : {
6736 : : /* Original was function so point to the new symbol, since
6737 : : the actual argument list is already attached to the
6738 : : expression. */
6739 : 30 : e->value.function.esym = NULL;
6740 : 30 : e->symtree = st;
6741 : : }
6742 : : else
6743 : : {
6744 : : /* Original was variable so convert array references into
6745 : : an actual arglist. This does not need any checking now
6746 : : since resolve_function will take care of it. */
6747 : 41 : e->value.function.actual = NULL;
6748 : 41 : e->expr_type = EXPR_FUNCTION;
6749 : 41 : e->symtree = st;
6750 : :
6751 : : /* Ambiguity will not arise if the array reference is not
6752 : : the last reference. */
6753 : 43 : for (ref = e->ref; ref; ref = ref->next)
6754 : 38 : if (ref->type == REF_ARRAY && ref->next == NULL)
6755 : : break;
6756 : :
6757 : 41 : if ((ref == NULL || ref->type != REF_ARRAY)
6758 : 5 : && sym->attr.proc == PROC_INTERNAL)
6759 : : {
6760 : 4 : gfc_error ("%qs at %L is host associated at %L into "
6761 : : "a contained procedure with an internal "
6762 : : "procedure of the same name", sym->name,
6763 : : &old_sym->declared_at, &e->where);
6764 : 4 : return false;
6765 : : }
6766 : :
6767 : 1 : if (ref == NULL)
6768 : : return false;
6769 : :
6770 : 36 : gcc_assert (ref->type == REF_ARRAY);
6771 : :
6772 : : /* Grab the start expressions from the array ref and
6773 : : copy them into actual arguments. */
6774 : 84 : for (n = 0; n < ref->u.ar.dimen; n++)
6775 : : {
6776 : 48 : arg = gfc_get_actual_arglist ();
6777 : 48 : arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6778 : 48 : if (e->value.function.actual == NULL)
6779 : 36 : tail = e->value.function.actual = arg;
6780 : : else
6781 : : {
6782 : 12 : tail->next = arg;
6783 : 12 : tail = arg;
6784 : : }
6785 : : }
6786 : :
6787 : : /* Dump the reference list and set the rank. */
6788 : 36 : gfc_free_ref_list (e->ref);
6789 : 36 : e->ref = NULL;
6790 : 36 : e->rank = sym->as ? sym->as->rank : 0;
6791 : 36 : e->corank = sym->as ? sym->as->corank : 0;
6792 : : }
6793 : :
6794 : 66 : gfc_resolve_expr (e);
6795 : 66 : sym->refs++;
6796 : : }
6797 : : /* This case corresponds to a call, from a block or a contained
6798 : : procedure, to an external function, which has not been declared
6799 : : as being external in the main program but has been typed. */
6800 : 86798 : else if (sym && old_sym != sym
6801 : 475 : && !e->ref
6802 : 267 : && sym->ts.type == BT_UNKNOWN
6803 : 21 : && old_sym->ts.type != BT_UNKNOWN
6804 : 19 : && sym->attr.flavor == FL_PROCEDURE
6805 : 19 : && old_sym->attr.flavor == FL_VARIABLE
6806 : 7 : && sym->ns->parent == old_sym->ns
6807 : 7 : && sym->ns->proc_name
6808 : 7 : && sym->ns->proc_name->attr.proc != PROC_MODULE
6809 : 6 : && (sym->ns->proc_name->attr.flavor == FL_LABEL
6810 : 6 : || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
6811 : : {
6812 : 6 : old_sym->attr.flavor = FL_PROCEDURE;
6813 : 6 : old_sym->attr.external = 1;
6814 : 6 : old_sym->attr.function = 1;
6815 : 6 : old_sym->result = old_sym;
6816 : 6 : gfc_resolve_expr (e);
6817 : : }
6818 : : }
6819 : : /* This might have changed! */
6820 : 1619711 : return e->expr_type == EXPR_FUNCTION;
6821 : : }
6822 : :
6823 : :
6824 : : static void
6825 : 1328 : gfc_resolve_character_operator (gfc_expr *e)
6826 : : {
6827 : 1328 : gfc_expr *op1 = e->value.op.op1;
6828 : 1328 : gfc_expr *op2 = e->value.op.op2;
6829 : 1328 : gfc_expr *e1 = NULL;
6830 : 1328 : gfc_expr *e2 = NULL;
6831 : :
6832 : 1328 : gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6833 : :
6834 : 1328 : if (op1->ts.u.cl && op1->ts.u.cl->length)
6835 : 653 : e1 = gfc_copy_expr (op1->ts.u.cl->length);
6836 : 675 : else if (op1->expr_type == EXPR_CONSTANT)
6837 : 268 : e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6838 : 268 : op1->value.character.length);
6839 : :
6840 : 1328 : if (op2->ts.u.cl && op2->ts.u.cl->length)
6841 : 641 : e2 = gfc_copy_expr (op2->ts.u.cl->length);
6842 : 687 : else if (op2->expr_type == EXPR_CONSTANT)
6843 : 457 : e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6844 : 457 : op2->value.character.length);
6845 : :
6846 : 1328 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6847 : :
6848 : 1328 : if (!e1 || !e2)
6849 : : {
6850 : 535 : gfc_free_expr (e1);
6851 : 535 : gfc_free_expr (e2);
6852 : :
6853 : 535 : return;
6854 : : }
6855 : :
6856 : 793 : e->ts.u.cl->length = gfc_add (e1, e2);
6857 : 793 : e->ts.u.cl->length->ts.type = BT_INTEGER;
6858 : 793 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6859 : 793 : gfc_simplify_expr (e->ts.u.cl->length, 0);
6860 : 793 : gfc_resolve_expr (e->ts.u.cl->length);
6861 : :
6862 : 793 : return;
6863 : : }
6864 : :
6865 : :
6866 : : /* Ensure that an character expression has a charlen and, if possible, a
6867 : : length expression. */
6868 : :
6869 : : static void
6870 : 175637 : fixup_charlen (gfc_expr *e)
6871 : : {
6872 : : /* The cases fall through so that changes in expression type and the need
6873 : : for multiple fixes are picked up. In all circumstances, a charlen should
6874 : : be available for the middle end to hang a backend_decl on. */
6875 : 175637 : switch (e->expr_type)
6876 : : {
6877 : 1328 : case EXPR_OP:
6878 : 1328 : gfc_resolve_character_operator (e);
6879 : : /* FALLTHRU */
6880 : :
6881 : 1383 : case EXPR_ARRAY:
6882 : 1383 : if (e->expr_type == EXPR_ARRAY)
6883 : 55 : gfc_resolve_character_array_constructor (e);
6884 : : /* FALLTHRU */
6885 : :
6886 : 1839 : case EXPR_SUBSTRING:
6887 : 1839 : if (!e->ts.u.cl && e->ref)
6888 : 452 : gfc_resolve_substring_charlen (e);
6889 : : /* FALLTHRU */
6890 : :
6891 : 175637 : default:
6892 : 175637 : if (!e->ts.u.cl)
6893 : 173802 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6894 : :
6895 : 175637 : break;
6896 : : }
6897 : 175637 : }
6898 : :
6899 : :
6900 : : /* Update an actual argument to include the passed-object for type-bound
6901 : : procedures at the right position. */
6902 : :
6903 : : static gfc_actual_arglist*
6904 : 2793 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
6905 : : const char *name)
6906 : : {
6907 : 2817 : gcc_assert (argpos > 0);
6908 : :
6909 : 2817 : if (argpos == 1)
6910 : : {
6911 : 2680 : gfc_actual_arglist* result;
6912 : :
6913 : 2680 : result = gfc_get_actual_arglist ();
6914 : 2680 : result->expr = po;
6915 : 2680 : result->next = lst;
6916 : 2680 : if (name)
6917 : 502 : result->name = name;
6918 : :
6919 : 2680 : return result;
6920 : : }
6921 : :
6922 : 137 : if (lst)
6923 : 113 : lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
6924 : : else
6925 : 24 : lst = update_arglist_pass (NULL, po, argpos - 1, name);
6926 : : return lst;
6927 : : }
6928 : :
6929 : :
6930 : : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
6931 : :
6932 : : static gfc_expr*
6933 : 6875 : extract_compcall_passed_object (gfc_expr* e)
6934 : : {
6935 : 6875 : gfc_expr* po;
6936 : :
6937 : 6875 : if (e->expr_type == EXPR_UNKNOWN)
6938 : : {
6939 : 0 : gfc_error ("Error in typebound call at %L",
6940 : : &e->where);
6941 : 0 : return NULL;
6942 : : }
6943 : :
6944 : 6875 : gcc_assert (e->expr_type == EXPR_COMPCALL);
6945 : :
6946 : 6875 : if (e->value.compcall.base_object)
6947 : 1494 : po = gfc_copy_expr (e->value.compcall.base_object);
6948 : : else
6949 : : {
6950 : 5381 : po = gfc_get_expr ();
6951 : 5381 : po->expr_type = EXPR_VARIABLE;
6952 : 5381 : po->symtree = e->symtree;
6953 : 5381 : po->ref = gfc_copy_ref (e->ref);
6954 : 5381 : po->where = e->where;
6955 : : }
6956 : :
6957 : 6875 : if (!gfc_resolve_expr (po))
6958 : : return NULL;
6959 : :
6960 : : return po;
6961 : : }
6962 : :
6963 : :
6964 : : /* Update the arglist of an EXPR_COMPCALL expression to include the
6965 : : passed-object. */
6966 : :
6967 : : static bool
6968 : 3196 : update_compcall_arglist (gfc_expr* e)
6969 : : {
6970 : 3196 : gfc_expr* po;
6971 : 3196 : gfc_typebound_proc* tbp;
6972 : :
6973 : 3196 : tbp = e->value.compcall.tbp;
6974 : :
6975 : 3196 : if (tbp->error)
6976 : : return false;
6977 : :
6978 : 3195 : po = extract_compcall_passed_object (e);
6979 : 3195 : if (!po)
6980 : : return false;
6981 : :
6982 : 3195 : if (tbp->nopass || e->value.compcall.ignore_pass)
6983 : : {
6984 : 1071 : gfc_free_expr (po);
6985 : 1071 : return true;
6986 : : }
6987 : :
6988 : 2124 : if (tbp->pass_arg_num <= 0)
6989 : : return false;
6990 : :
6991 : 2123 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
6992 : : tbp->pass_arg_num,
6993 : : tbp->pass_arg);
6994 : :
6995 : 2123 : return true;
6996 : : }
6997 : :
6998 : :
6999 : : /* Extract the passed object from a PPC call (a copy of it). */
7000 : :
7001 : : static gfc_expr*
7002 : 85 : extract_ppc_passed_object (gfc_expr *e)
7003 : : {
7004 : 85 : gfc_expr *po;
7005 : 85 : gfc_ref **ref;
7006 : :
7007 : 85 : po = gfc_get_expr ();
7008 : 85 : po->expr_type = EXPR_VARIABLE;
7009 : 85 : po->symtree = e->symtree;
7010 : 85 : po->ref = gfc_copy_ref (e->ref);
7011 : 85 : po->where = e->where;
7012 : :
7013 : : /* Remove PPC reference. */
7014 : 85 : ref = &po->ref;
7015 : 91 : while ((*ref)->next)
7016 : 6 : ref = &(*ref)->next;
7017 : 85 : gfc_free_ref_list (*ref);
7018 : 85 : *ref = NULL;
7019 : :
7020 : 85 : if (!gfc_resolve_expr (po))
7021 : 0 : return NULL;
7022 : :
7023 : : return po;
7024 : : }
7025 : :
7026 : :
7027 : : /* Update the actual arglist of a procedure pointer component to include the
7028 : : passed-object. */
7029 : :
7030 : : static bool
7031 : 399 : update_ppc_arglist (gfc_expr* e)
7032 : : {
7033 : 399 : gfc_expr* po;
7034 : 399 : gfc_component *ppc;
7035 : 399 : gfc_typebound_proc* tb;
7036 : :
7037 : 399 : ppc = gfc_get_proc_ptr_comp (e);
7038 : 399 : if (!ppc)
7039 : : return false;
7040 : :
7041 : 399 : tb = ppc->tb;
7042 : :
7043 : 399 : if (tb->error)
7044 : : return false;
7045 : 397 : else if (tb->nopass)
7046 : : return true;
7047 : :
7048 : 85 : po = extract_ppc_passed_object (e);
7049 : 85 : if (!po)
7050 : : return false;
7051 : :
7052 : : /* F08:R739. */
7053 : 85 : if (po->rank != 0)
7054 : : {
7055 : 0 : gfc_error ("Passed-object at %L must be scalar", &e->where);
7056 : 0 : return false;
7057 : : }
7058 : :
7059 : : /* F08:C611. */
7060 : 85 : if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7061 : : {
7062 : 1 : gfc_error ("Base object for procedure-pointer component call at %L is of"
7063 : : " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7064 : 1 : return false;
7065 : : }
7066 : :
7067 : 84 : gcc_assert (tb->pass_arg_num > 0);
7068 : 84 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7069 : : tb->pass_arg_num,
7070 : : tb->pass_arg);
7071 : :
7072 : 84 : return true;
7073 : : }
7074 : :
7075 : :
7076 : : /* Check that the object a TBP is called on is valid, i.e. it must not be
7077 : : of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7078 : :
7079 : : static bool
7080 : 3207 : check_typebound_baseobject (gfc_expr* e)
7081 : : {
7082 : 3207 : gfc_expr* base;
7083 : 3207 : bool return_value = false;
7084 : :
7085 : 3207 : base = extract_compcall_passed_object (e);
7086 : 3207 : if (!base)
7087 : : return false;
7088 : :
7089 : 3204 : if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7090 : : {
7091 : 1 : gfc_error ("Error in typebound call at %L", &e->where);
7092 : 1 : goto cleanup;
7093 : : }
7094 : :
7095 : 3203 : if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7096 : 1 : return false;
7097 : :
7098 : : /* F08:C611. */
7099 : 3202 : if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7100 : : {
7101 : 3 : gfc_error ("Base object for type-bound procedure call at %L is of"
7102 : : " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7103 : 3 : goto cleanup;
7104 : : }
7105 : :
7106 : : /* F08:C1230. If the procedure called is NOPASS,
7107 : : the base object must be scalar. */
7108 : 3199 : if (e->value.compcall.tbp->nopass && base->rank != 0)
7109 : : {
7110 : 1 : gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7111 : : " be scalar", &e->where);
7112 : 1 : goto cleanup;
7113 : : }
7114 : :
7115 : : return_value = true;
7116 : :
7117 : 3203 : cleanup:
7118 : 3203 : gfc_free_expr (base);
7119 : 3203 : return return_value;
7120 : : }
7121 : :
7122 : :
7123 : : /* Resolve a call to a type-bound procedure, either function or subroutine,
7124 : : statically from the data in an EXPR_COMPCALL expression. The adapted
7125 : : arglist and the target-procedure symtree are returned. */
7126 : :
7127 : : static bool
7128 : 3196 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7129 : : gfc_actual_arglist** actual)
7130 : : {
7131 : 3196 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7132 : 3196 : gcc_assert (!e->value.compcall.tbp->is_generic);
7133 : :
7134 : : /* Update the actual arglist for PASS. */
7135 : 3196 : if (!update_compcall_arglist (e))
7136 : : return false;
7137 : :
7138 : 3194 : *actual = e->value.compcall.actual;
7139 : 3194 : *target = e->value.compcall.tbp->u.specific;
7140 : :
7141 : 3194 : gfc_free_ref_list (e->ref);
7142 : 3194 : e->ref = NULL;
7143 : 3194 : e->value.compcall.actual = NULL;
7144 : :
7145 : : /* If we find a deferred typebound procedure, check for derived types
7146 : : that an overriding typebound procedure has not been missed. */
7147 : 3194 : if (e->value.compcall.name
7148 : 3194 : && !e->value.compcall.tbp->non_overridable
7149 : 3178 : && e->value.compcall.base_object
7150 : 747 : && e->value.compcall.base_object->ts.type == BT_DERIVED)
7151 : : {
7152 : 460 : gfc_symtree *st;
7153 : 460 : gfc_symbol *derived;
7154 : :
7155 : : /* Use the derived type of the base_object. */
7156 : 460 : derived = e->value.compcall.base_object->ts.u.derived;
7157 : 460 : st = NULL;
7158 : :
7159 : : /* If necessary, go through the inheritance chain. */
7160 : 1381 : while (!st && derived)
7161 : : {
7162 : : /* Look for the typebound procedure 'name'. */
7163 : 461 : if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7164 : 460 : st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7165 : : e->value.compcall.name);
7166 : 461 : if (!st)
7167 : 1 : derived = gfc_get_derived_super_type (derived);
7168 : : }
7169 : :
7170 : : /* Now find the specific name in the derived type namespace. */
7171 : 460 : if (st && st->n.tb && st->n.tb->u.specific)
7172 : 460 : gfc_find_sym_tree (st->n.tb->u.specific->name,
7173 : 460 : derived->ns, 1, &st);
7174 : 460 : if (st)
7175 : 460 : *target = st;
7176 : : }
7177 : :
7178 : 3194 : if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7179 : 3194 : && !e->value.compcall.tbp->deferred)
7180 : 1 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7181 : : " itself recursively. Declare it RECURSIVE or use"
7182 : : " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7183 : :
7184 : : return true;
7185 : : }
7186 : :
7187 : :
7188 : : /* Get the ultimate declared type from an expression. In addition,
7189 : : return the last class/derived type reference and the copy of the
7190 : : reference list. If check_types is set true, derived types are
7191 : : identified as well as class references. */
7192 : : static gfc_symbol*
7193 : 3142 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7194 : : gfc_expr *e, bool check_types)
7195 : : {
7196 : 3142 : gfc_symbol *declared;
7197 : 3142 : gfc_ref *ref;
7198 : :
7199 : 3142 : declared = NULL;
7200 : 3142 : if (class_ref)
7201 : 2771 : *class_ref = NULL;
7202 : 3142 : if (new_ref)
7203 : 2484 : *new_ref = gfc_copy_ref (e->ref);
7204 : :
7205 : 3911 : for (ref = e->ref; ref; ref = ref->next)
7206 : : {
7207 : 769 : if (ref->type != REF_COMPONENT)
7208 : 283 : continue;
7209 : :
7210 : 486 : if ((ref->u.c.component->ts.type == BT_CLASS
7211 : 240 : || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7212 : 411 : && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7213 : : {
7214 : 337 : declared = ref->u.c.component->ts.u.derived;
7215 : 337 : if (class_ref)
7216 : 319 : *class_ref = ref;
7217 : : }
7218 : : }
7219 : :
7220 : 3142 : if (declared == NULL)
7221 : 2831 : declared = e->symtree->n.sym->ts.u.derived;
7222 : :
7223 : 3142 : return declared;
7224 : : }
7225 : :
7226 : :
7227 : : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7228 : : which of the specific bindings (if any) matches the arglist and transform
7229 : : the expression into a call of that binding. */
7230 : :
7231 : : static bool
7232 : 3198 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
7233 : : {
7234 : 3198 : gfc_typebound_proc* genproc;
7235 : 3198 : const char* genname;
7236 : 3198 : gfc_symtree *st;
7237 : 3198 : gfc_symbol *derived;
7238 : :
7239 : 3198 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7240 : 3198 : genname = e->value.compcall.name;
7241 : 3198 : genproc = e->value.compcall.tbp;
7242 : :
7243 : 3198 : if (!genproc->is_generic)
7244 : : return true;
7245 : :
7246 : : /* Try the bindings on this type and in the inheritance hierarchy. */
7247 : 383 : for (; genproc; genproc = genproc->overridden)
7248 : : {
7249 : 381 : gfc_tbp_generic* g;
7250 : :
7251 : 381 : gcc_assert (genproc->is_generic);
7252 : 569 : for (g = genproc->u.generic; g; g = g->next)
7253 : : {
7254 : 559 : gfc_symbol* target;
7255 : 559 : gfc_actual_arglist* args;
7256 : 559 : bool matches;
7257 : :
7258 : 559 : gcc_assert (g->specific);
7259 : :
7260 : 559 : if (g->specific->error)
7261 : 0 : continue;
7262 : :
7263 : 559 : target = g->specific->u.specific->n.sym;
7264 : :
7265 : : /* Get the right arglist by handling PASS/NOPASS. */
7266 : 559 : args = gfc_copy_actual_arglist (e->value.compcall.actual);
7267 : 559 : if (!g->specific->nopass)
7268 : : {
7269 : 473 : gfc_expr* po;
7270 : 473 : po = extract_compcall_passed_object (e);
7271 : 473 : if (!po)
7272 : : {
7273 : 0 : gfc_free_actual_arglist (args);
7274 : 0 : return false;
7275 : : }
7276 : :
7277 : 473 : gcc_assert (g->specific->pass_arg_num > 0);
7278 : 473 : gcc_assert (!g->specific->error);
7279 : 473 : args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7280 : : g->specific->pass_arg);
7281 : : }
7282 : 559 : resolve_actual_arglist (args, target->attr.proc,
7283 : 559 : is_external_proc (target)
7284 : 559 : && gfc_sym_get_dummy_args (target) == NULL);
7285 : :
7286 : : /* Check if this arglist matches the formal. */
7287 : 559 : matches = gfc_arglist_matches_symbol (&args, target);
7288 : :
7289 : : /* Clean up and break out of the loop if we've found it. */
7290 : 559 : gfc_free_actual_arglist (args);
7291 : 559 : if (matches)
7292 : : {
7293 : 371 : e->value.compcall.tbp = g->specific;
7294 : 371 : genname = g->specific_st->name;
7295 : : /* Pass along the name for CLASS methods, where the vtab
7296 : : procedure pointer component has to be referenced. */
7297 : 371 : if (name)
7298 : 159 : *name = genname;
7299 : 371 : goto success;
7300 : : }
7301 : : }
7302 : : }
7303 : :
7304 : : /* Nothing matching found! */
7305 : 2 : gfc_error ("Found no matching specific binding for the call to the GENERIC"
7306 : : " %qs at %L", genname, &e->where);
7307 : 2 : return false;
7308 : :
7309 : 371 : success:
7310 : : /* Make sure that we have the right specific instance for the name. */
7311 : 371 : derived = get_declared_from_expr (NULL, NULL, e, true);
7312 : :
7313 : 371 : st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7314 : 371 : if (st)
7315 : 371 : e->value.compcall.tbp = st->n.tb;
7316 : :
7317 : : return true;
7318 : : }
7319 : :
7320 : :
7321 : : /* Resolve a call to a type-bound subroutine. */
7322 : :
7323 : : static bool
7324 : 1675 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7325 : : {
7326 : 1675 : gfc_actual_arglist* newactual;
7327 : 1675 : gfc_symtree* target;
7328 : :
7329 : : /* Check that's really a SUBROUTINE. */
7330 : 1675 : if (!c->expr1->value.compcall.tbp->subroutine)
7331 : : {
7332 : 17 : if (!c->expr1->value.compcall.tbp->is_generic
7333 : 15 : && c->expr1->value.compcall.tbp->u.specific
7334 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym
7335 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7336 : 12 : c->expr1->value.compcall.tbp->subroutine = 1;
7337 : : else
7338 : : {
7339 : 5 : gfc_error ("%qs at %L should be a SUBROUTINE",
7340 : : c->expr1->value.compcall.name, &c->loc);
7341 : 5 : return false;
7342 : : }
7343 : : }
7344 : :
7345 : 1670 : if (!check_typebound_baseobject (c->expr1))
7346 : : return false;
7347 : :
7348 : : /* Pass along the name for CLASS methods, where the vtab
7349 : : procedure pointer component has to be referenced. */
7350 : 1663 : if (name)
7351 : 473 : *name = c->expr1->value.compcall.name;
7352 : :
7353 : 1663 : if (!resolve_typebound_generic_call (c->expr1, name))
7354 : : return false;
7355 : :
7356 : : /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7357 : 1662 : if (overridable)
7358 : 370 : *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7359 : :
7360 : : /* Transform into an ordinary EXEC_CALL for now. */
7361 : :
7362 : 1662 : if (!resolve_typebound_static (c->expr1, &target, &newactual))
7363 : : return false;
7364 : :
7365 : 1660 : c->ext.actual = newactual;
7366 : 1660 : c->symtree = target;
7367 : 1660 : c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7368 : :
7369 : 1660 : gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7370 : :
7371 : 1660 : gfc_free_expr (c->expr1);
7372 : 1660 : c->expr1 = gfc_get_expr ();
7373 : 1660 : c->expr1->expr_type = EXPR_FUNCTION;
7374 : 1660 : c->expr1->symtree = target;
7375 : 1660 : c->expr1->where = c->loc;
7376 : :
7377 : 1660 : return resolve_call (c);
7378 : : }
7379 : :
7380 : :
7381 : : /* Resolve a component-call expression. */
7382 : : static bool
7383 : 1556 : resolve_compcall (gfc_expr* e, const char **name)
7384 : : {
7385 : 1556 : gfc_actual_arglist* newactual;
7386 : 1556 : gfc_symtree* target;
7387 : :
7388 : : /* Check that's really a FUNCTION. */
7389 : 1556 : if (!e->value.compcall.tbp->function)
7390 : : {
7391 : 19 : if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7392 : 5 : gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7393 : : &e->where);
7394 : 19 : return false;
7395 : : }
7396 : :
7397 : :
7398 : : /* These must not be assign-calls! */
7399 : 1537 : gcc_assert (!e->value.compcall.assign);
7400 : :
7401 : 1537 : if (!check_typebound_baseobject (e))
7402 : : return false;
7403 : :
7404 : : /* Pass along the name for CLASS methods, where the vtab
7405 : : procedure pointer component has to be referenced. */
7406 : 1535 : if (name)
7407 : 857 : *name = e->value.compcall.name;
7408 : :
7409 : 1535 : if (!resolve_typebound_generic_call (e, name))
7410 : : return false;
7411 : 1534 : gcc_assert (!e->value.compcall.tbp->is_generic);
7412 : :
7413 : : /* Take the rank from the function's symbol. */
7414 : 1534 : if (e->value.compcall.tbp->u.specific->n.sym->as)
7415 : : {
7416 : 149 : e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7417 : 149 : e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7418 : : }
7419 : :
7420 : : /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7421 : : arglist to the TBP's binding target. */
7422 : :
7423 : 1534 : if (!resolve_typebound_static (e, &target, &newactual))
7424 : : return false;
7425 : :
7426 : 1534 : e->value.function.actual = newactual;
7427 : 1534 : e->value.function.name = NULL;
7428 : 1534 : e->value.function.esym = target->n.sym;
7429 : 1534 : e->value.function.isym = NULL;
7430 : 1534 : e->symtree = target;
7431 : 1534 : e->ts = target->n.sym->ts;
7432 : 1534 : e->expr_type = EXPR_FUNCTION;
7433 : :
7434 : : /* Resolution is not necessary if this is a class subroutine; this
7435 : : function only has to identify the specific proc. Resolution of
7436 : : the call will be done next in resolve_typebound_call. */
7437 : 1534 : return gfc_resolve_expr (e);
7438 : : }
7439 : :
7440 : :
7441 : : static bool resolve_fl_derived (gfc_symbol *sym);
7442 : :
7443 : :
7444 : : /* Resolve a typebound function, or 'method'. First separate all
7445 : : the non-CLASS references by calling resolve_compcall directly. */
7446 : :
7447 : : static bool
7448 : 1556 : resolve_typebound_function (gfc_expr* e)
7449 : : {
7450 : 1556 : gfc_symbol *declared;
7451 : 1556 : gfc_component *c;
7452 : 1556 : gfc_ref *new_ref;
7453 : 1556 : gfc_ref *class_ref;
7454 : 1556 : gfc_symtree *st;
7455 : 1556 : const char *name;
7456 : 1556 : gfc_typespec ts;
7457 : 1556 : gfc_expr *expr;
7458 : 1556 : bool overridable;
7459 : :
7460 : 1556 : st = e->symtree;
7461 : :
7462 : : /* Deal with typebound operators for CLASS objects. */
7463 : 1556 : expr = e->value.compcall.base_object;
7464 : 1556 : overridable = !e->value.compcall.tbp->non_overridable;
7465 : 1556 : if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7466 : : {
7467 : : /* Since the typebound operators are generic, we have to ensure
7468 : : that any delays in resolution are corrected and that the vtab
7469 : : is present. */
7470 : 184 : ts = expr->ts;
7471 : 184 : declared = ts.u.derived;
7472 : 184 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7473 : 184 : if (c->ts.u.derived == NULL)
7474 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7475 : :
7476 : 184 : if (!resolve_compcall (e, &name))
7477 : : return false;
7478 : :
7479 : : /* Use the generic name if it is there. */
7480 : 184 : name = name ? name : e->value.function.esym->name;
7481 : 184 : e->symtree = expr->symtree;
7482 : 184 : e->ref = gfc_copy_ref (expr->ref);
7483 : 184 : get_declared_from_expr (&class_ref, NULL, e, false);
7484 : :
7485 : : /* Trim away the extraneous references that emerge from nested
7486 : : use of interface.cc (extend_expr). */
7487 : 184 : if (class_ref && class_ref->next)
7488 : : {
7489 : 0 : gfc_free_ref_list (class_ref->next);
7490 : 0 : class_ref->next = NULL;
7491 : : }
7492 : 184 : else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7493 : : {
7494 : 0 : gfc_free_ref_list (e->ref);
7495 : 0 : e->ref = NULL;
7496 : : }
7497 : :
7498 : 184 : gfc_add_vptr_component (e);
7499 : 184 : gfc_add_component_ref (e, name);
7500 : 184 : e->value.function.esym = NULL;
7501 : 184 : if (expr->expr_type != EXPR_VARIABLE)
7502 : 80 : e->base_expr = expr;
7503 : 184 : return true;
7504 : : }
7505 : :
7506 : 1372 : if (st == NULL)
7507 : 147 : return resolve_compcall (e, NULL);
7508 : :
7509 : 1225 : if (!gfc_resolve_ref (e))
7510 : : return false;
7511 : :
7512 : : /* Get the CLASS declared type. */
7513 : 1225 : declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7514 : :
7515 : 1225 : if (!resolve_fl_derived (declared))
7516 : : return false;
7517 : :
7518 : : /* Weed out cases of the ultimate component being a derived type. */
7519 : 1225 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7520 : 1141 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7521 : : {
7522 : 538 : gfc_free_ref_list (new_ref);
7523 : 538 : return resolve_compcall (e, NULL);
7524 : : }
7525 : :
7526 : 687 : c = gfc_find_component (declared, "_data", true, true, NULL);
7527 : :
7528 : : /* Treat the call as if it is a typebound procedure, in order to roll
7529 : : out the correct name for the specific function. */
7530 : 687 : if (!resolve_compcall (e, &name))
7531 : : {
7532 : 15 : gfc_free_ref_list (new_ref);
7533 : 15 : return false;
7534 : : }
7535 : 672 : ts = e->ts;
7536 : :
7537 : 672 : if (overridable)
7538 : : {
7539 : : /* Convert the expression to a procedure pointer component call. */
7540 : 670 : e->value.function.esym = NULL;
7541 : 670 : e->symtree = st;
7542 : :
7543 : 670 : if (new_ref)
7544 : 124 : e->ref = new_ref;
7545 : :
7546 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7547 : 670 : gfc_add_vptr_component (e);
7548 : 670 : gfc_add_component_ref (e, name);
7549 : :
7550 : : /* Recover the typespec for the expression. This is really only
7551 : : necessary for generic procedures, where the additional call
7552 : : to gfc_add_component_ref seems to throw the collection of the
7553 : : correct typespec. */
7554 : 670 : e->ts = ts;
7555 : : }
7556 : 2 : else if (new_ref)
7557 : 0 : gfc_free_ref_list (new_ref);
7558 : :
7559 : : return true;
7560 : : }
7561 : :
7562 : : /* Resolve a typebound subroutine, or 'method'. First separate all
7563 : : the non-CLASS references by calling resolve_typebound_call
7564 : : directly. */
7565 : :
7566 : : static bool
7567 : 1675 : resolve_typebound_subroutine (gfc_code *code)
7568 : : {
7569 : 1675 : gfc_symbol *declared;
7570 : 1675 : gfc_component *c;
7571 : 1675 : gfc_ref *new_ref;
7572 : 1675 : gfc_ref *class_ref;
7573 : 1675 : gfc_symtree *st;
7574 : 1675 : const char *name;
7575 : 1675 : gfc_typespec ts;
7576 : 1675 : gfc_expr *expr;
7577 : 1675 : bool overridable;
7578 : :
7579 : 1675 : st = code->expr1->symtree;
7580 : :
7581 : : /* Deal with typebound operators for CLASS objects. */
7582 : 1675 : expr = code->expr1->value.compcall.base_object;
7583 : 1675 : overridable = !code->expr1->value.compcall.tbp->non_overridable;
7584 : 1675 : if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7585 : : {
7586 : : /* If the base_object is not a variable, the corresponding actual
7587 : : argument expression must be stored in e->base_expression so
7588 : : that the corresponding tree temporary can be used as the base
7589 : : object in gfc_conv_procedure_call. */
7590 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7591 : : {
7592 : : gfc_actual_arglist *args;
7593 : :
7594 : : args= code->expr1->value.function.actual;
7595 : : for (; args; args = args->next)
7596 : : if (expr == args->expr)
7597 : : expr = args->expr;
7598 : : }
7599 : :
7600 : : /* Since the typebound operators are generic, we have to ensure
7601 : : that any delays in resolution are corrected and that the vtab
7602 : : is present. */
7603 : 103 : declared = expr->ts.u.derived;
7604 : 103 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7605 : 103 : if (c->ts.u.derived == NULL)
7606 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7607 : :
7608 : 103 : if (!resolve_typebound_call (code, &name, NULL))
7609 : : return false;
7610 : :
7611 : : /* Use the generic name if it is there. */
7612 : 103 : name = name ? name : code->expr1->value.function.esym->name;
7613 : 103 : code->expr1->symtree = expr->symtree;
7614 : 103 : code->expr1->ref = gfc_copy_ref (expr->ref);
7615 : :
7616 : : /* Trim away the extraneous references that emerge from nested
7617 : : use of interface.cc (extend_expr). */
7618 : 103 : get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7619 : 103 : if (class_ref && class_ref->next)
7620 : : {
7621 : 0 : gfc_free_ref_list (class_ref->next);
7622 : 0 : class_ref->next = NULL;
7623 : : }
7624 : 103 : else if (code->expr1->ref && !class_ref)
7625 : : {
7626 : 12 : gfc_free_ref_list (code->expr1->ref);
7627 : 12 : code->expr1->ref = NULL;
7628 : : }
7629 : :
7630 : : /* Now use the procedure in the vtable. */
7631 : 103 : gfc_add_vptr_component (code->expr1);
7632 : 103 : gfc_add_component_ref (code->expr1, name);
7633 : 103 : code->expr1->value.function.esym = NULL;
7634 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7635 : 0 : code->expr1->base_expr = expr;
7636 : 103 : return true;
7637 : : }
7638 : :
7639 : 1572 : if (st == NULL)
7640 : 313 : return resolve_typebound_call (code, NULL, NULL);
7641 : :
7642 : 1259 : if (!gfc_resolve_ref (code->expr1))
7643 : : return false;
7644 : :
7645 : : /* Get the CLASS declared type. */
7646 : 1259 : get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7647 : :
7648 : : /* Weed out cases of the ultimate component being a derived type. */
7649 : 1259 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7650 : 1196 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7651 : : {
7652 : 884 : gfc_free_ref_list (new_ref);
7653 : 884 : return resolve_typebound_call (code, NULL, NULL);
7654 : : }
7655 : :
7656 : 375 : if (!resolve_typebound_call (code, &name, &overridable))
7657 : : {
7658 : 5 : gfc_free_ref_list (new_ref);
7659 : 5 : return false;
7660 : : }
7661 : 370 : ts = code->expr1->ts;
7662 : :
7663 : 370 : if (overridable)
7664 : : {
7665 : : /* Convert the expression to a procedure pointer component call. */
7666 : 368 : code->expr1->value.function.esym = NULL;
7667 : 368 : code->expr1->symtree = st;
7668 : :
7669 : 368 : if (new_ref)
7670 : 92 : code->expr1->ref = new_ref;
7671 : :
7672 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7673 : 368 : gfc_add_vptr_component (code->expr1);
7674 : 368 : gfc_add_component_ref (code->expr1, name);
7675 : :
7676 : : /* Recover the typespec for the expression. This is really only
7677 : : necessary for generic procedures, where the additional call
7678 : : to gfc_add_component_ref seems to throw the collection of the
7679 : : correct typespec. */
7680 : 368 : code->expr1->ts = ts;
7681 : : }
7682 : 2 : else if (new_ref)
7683 : 0 : gfc_free_ref_list (new_ref);
7684 : :
7685 : : return true;
7686 : : }
7687 : :
7688 : :
7689 : : /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7690 : :
7691 : : static bool
7692 : 123 : resolve_ppc_call (gfc_code* c)
7693 : : {
7694 : 123 : gfc_component *comp;
7695 : :
7696 : 123 : comp = gfc_get_proc_ptr_comp (c->expr1);
7697 : 123 : gcc_assert (comp != NULL);
7698 : :
7699 : 123 : c->resolved_sym = c->expr1->symtree->n.sym;
7700 : 123 : c->expr1->expr_type = EXPR_VARIABLE;
7701 : :
7702 : 123 : if (!comp->attr.subroutine)
7703 : 1 : gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7704 : :
7705 : 123 : if (!gfc_resolve_ref (c->expr1))
7706 : : return false;
7707 : :
7708 : 123 : if (!update_ppc_arglist (c->expr1))
7709 : : return false;
7710 : :
7711 : 122 : c->ext.actual = c->expr1->value.compcall.actual;
7712 : :
7713 : 122 : if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7714 : 122 : !(comp->ts.interface
7715 : 93 : && comp->ts.interface->formal)))
7716 : : return false;
7717 : :
7718 : 122 : if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7719 : : return false;
7720 : :
7721 : 121 : gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7722 : :
7723 : 121 : return true;
7724 : : }
7725 : :
7726 : :
7727 : : /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7728 : :
7729 : : static bool
7730 : 276 : resolve_expr_ppc (gfc_expr* e)
7731 : : {
7732 : 276 : gfc_component *comp;
7733 : :
7734 : 276 : comp = gfc_get_proc_ptr_comp (e);
7735 : 276 : gcc_assert (comp != NULL);
7736 : :
7737 : : /* Convert to EXPR_FUNCTION. */
7738 : 276 : e->expr_type = EXPR_FUNCTION;
7739 : 276 : e->value.function.isym = NULL;
7740 : 276 : e->value.function.actual = e->value.compcall.actual;
7741 : 276 : e->ts = comp->ts;
7742 : 276 : if (comp->as != NULL)
7743 : : {
7744 : 28 : e->rank = comp->as->rank;
7745 : 28 : e->corank = comp->as->corank;
7746 : : }
7747 : :
7748 : 276 : if (!comp->attr.function)
7749 : 3 : gfc_add_function (&comp->attr, comp->name, &e->where);
7750 : :
7751 : 276 : if (!gfc_resolve_ref (e))
7752 : : return false;
7753 : :
7754 : 276 : if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7755 : 276 : !(comp->ts.interface
7756 : 275 : && comp->ts.interface->formal)))
7757 : : return false;
7758 : :
7759 : 276 : if (!update_ppc_arglist (e))
7760 : : return false;
7761 : :
7762 : 274 : if (!check_pure_function(e))
7763 : : return false;
7764 : :
7765 : 273 : gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7766 : :
7767 : 273 : return true;
7768 : : }
7769 : :
7770 : :
7771 : : static bool
7772 : 11615 : gfc_is_expandable_expr (gfc_expr *e)
7773 : : {
7774 : 11615 : gfc_constructor *con;
7775 : :
7776 : 11615 : if (e->expr_type == EXPR_ARRAY)
7777 : : {
7778 : : /* Traverse the constructor looking for variables that are flavor
7779 : : parameter. Parameters must be expanded since they are fully used at
7780 : : compile time. */
7781 : 11615 : con = gfc_constructor_first (e->value.constructor);
7782 : 30934 : for (; con; con = gfc_constructor_next (con))
7783 : : {
7784 : 13295 : if (con->expr->expr_type == EXPR_VARIABLE
7785 : 4922 : && con->expr->symtree
7786 : 4922 : && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7787 : 4867 : || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7788 : : return true;
7789 : 8373 : if (con->expr->expr_type == EXPR_ARRAY
7790 : 8373 : && gfc_is_expandable_expr (con->expr))
7791 : : return true;
7792 : : }
7793 : : }
7794 : :
7795 : : return false;
7796 : : }
7797 : :
7798 : :
7799 : : /* Sometimes variables in specification expressions of the result
7800 : : of module procedures in submodules wind up not being the 'real'
7801 : : dummy. Find this, if possible, in the namespace of the first
7802 : : formal argument. */
7803 : :
7804 : : static void
7805 : 3340 : fixup_unique_dummy (gfc_expr *e)
7806 : : {
7807 : 3340 : gfc_symtree *st = NULL;
7808 : 3340 : gfc_symbol *s = NULL;
7809 : :
7810 : 3340 : if (e->symtree->n.sym->ns->proc_name
7811 : 3340 : && e->symtree->n.sym->ns->proc_name->formal)
7812 : 3340 : s = e->symtree->n.sym->ns->proc_name->formal->sym;
7813 : :
7814 : 3340 : if (s != NULL)
7815 : 3340 : st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7816 : :
7817 : 3340 : if (st != NULL
7818 : 14 : && st->n.sym != NULL
7819 : 14 : && st->n.sym->attr.dummy)
7820 : 14 : e->symtree = st;
7821 : 3340 : }
7822 : :
7823 : : /* Resolve an expression. That is, make sure that types of operands agree
7824 : : with their operators, intrinsic operators are converted to function calls
7825 : : for overloaded types and unresolved function references are resolved. */
7826 : :
7827 : : bool
7828 : 6922525 : gfc_resolve_expr (gfc_expr *e)
7829 : : {
7830 : 6922525 : bool t;
7831 : 6922525 : bool inquiry_save, actual_arg_save, first_actual_arg_save;
7832 : :
7833 : 6922525 : if (e == NULL || e->do_not_resolve_again)
7834 : : return true;
7835 : :
7836 : : /* inquiry_argument only applies to variables. */
7837 : 5060903 : inquiry_save = inquiry_argument;
7838 : 5060903 : actual_arg_save = actual_arg;
7839 : 5060903 : first_actual_arg_save = first_actual_arg;
7840 : :
7841 : 5060903 : if (e->expr_type != EXPR_VARIABLE)
7842 : : {
7843 : 3773687 : inquiry_argument = false;
7844 : 3773687 : actual_arg = false;
7845 : 3773687 : first_actual_arg = false;
7846 : : }
7847 : 1287216 : else if (e->symtree != NULL
7848 : 1286797 : && *e->symtree->name == '@'
7849 : 4047 : && e->symtree->n.sym->attr.dummy)
7850 : : {
7851 : : /* Deal with submodule specification expressions that are not
7852 : : found to be referenced in module.cc(read_cleanup). */
7853 : 3340 : fixup_unique_dummy (e);
7854 : : }
7855 : :
7856 : 5060903 : switch (e->expr_type)
7857 : : {
7858 : 517302 : case EXPR_OP:
7859 : 517302 : t = resolve_operator (e);
7860 : 517302 : break;
7861 : :
7862 : 1622161 : case EXPR_FUNCTION:
7863 : 1622161 : case EXPR_VARIABLE:
7864 : :
7865 : 1622161 : if (check_host_association (e))
7866 : 334981 : t = resolve_function (e);
7867 : : else
7868 : 1287180 : t = resolve_variable (e);
7869 : :
7870 : 1622161 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7871 : 6852 : && e->ref->type != REF_SUBSTRING)
7872 : 2143 : gfc_resolve_substring_charlen (e);
7873 : :
7874 : : break;
7875 : :
7876 : 1556 : case EXPR_COMPCALL:
7877 : 1556 : t = resolve_typebound_function (e);
7878 : 1556 : break;
7879 : :
7880 : 507 : case EXPR_SUBSTRING:
7881 : 507 : t = gfc_resolve_ref (e);
7882 : 507 : break;
7883 : :
7884 : : case EXPR_CONSTANT:
7885 : : case EXPR_NULL:
7886 : : t = true;
7887 : : break;
7888 : :
7889 : 276 : case EXPR_PPC:
7890 : 276 : t = resolve_expr_ppc (e);
7891 : 276 : break;
7892 : :
7893 : 67263 : case EXPR_ARRAY:
7894 : 67263 : t = false;
7895 : 67263 : if (!gfc_resolve_ref (e))
7896 : : break;
7897 : :
7898 : 67263 : t = gfc_resolve_array_constructor (e);
7899 : : /* Also try to expand a constructor. */
7900 : 67263 : if (t)
7901 : : {
7902 : 67169 : gfc_expression_rank (e);
7903 : 67169 : if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
7904 : 62218 : gfc_expand_constructor (e, false);
7905 : : }
7906 : :
7907 : : /* This provides the opportunity for the length of constructors with
7908 : : character valued function elements to propagate the string length
7909 : : to the expression. */
7910 : 67169 : if (t && e->ts.type == BT_CHARACTER)
7911 : : {
7912 : : /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
7913 : : here rather then add a duplicate test for it above. */
7914 : 9869 : gfc_expand_constructor (e, false);
7915 : 9869 : t = gfc_resolve_character_array_constructor (e);
7916 : : }
7917 : :
7918 : : break;
7919 : :
7920 : 15859 : case EXPR_STRUCTURE:
7921 : 15859 : t = gfc_resolve_ref (e);
7922 : 15859 : if (!t)
7923 : : break;
7924 : :
7925 : 15859 : t = resolve_structure_cons (e, 0);
7926 : 15859 : if (!t)
7927 : : break;
7928 : :
7929 : 15847 : t = gfc_simplify_expr (e, 0);
7930 : 15847 : break;
7931 : :
7932 : 0 : default:
7933 : 0 : gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
7934 : : }
7935 : :
7936 : 5060903 : if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
7937 : 175637 : fixup_charlen (e);
7938 : :
7939 : 5060903 : inquiry_argument = inquiry_save;
7940 : 5060903 : actual_arg = actual_arg_save;
7941 : 5060903 : first_actual_arg = first_actual_arg_save;
7942 : :
7943 : : /* For some reason, resolving these expressions a second time mangles
7944 : : the typespec of the expression itself. */
7945 : 5060903 : if (t && e->expr_type == EXPR_VARIABLE
7946 : 1284370 : && e->symtree->n.sym->attr.select_rank_temporary
7947 : 3422 : && UNLIMITED_POLY (e->symtree->n.sym))
7948 : 83 : e->do_not_resolve_again = 1;
7949 : :
7950 : : return t;
7951 : : }
7952 : :
7953 : :
7954 : : /* Resolve an expression from an iterator. They must be scalar and have
7955 : : INTEGER or (optionally) REAL type. */
7956 : :
7957 : : static bool
7958 : 148449 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
7959 : : const char *name_msgid)
7960 : : {
7961 : 148449 : if (!gfc_resolve_expr (expr))
7962 : : return false;
7963 : :
7964 : 148444 : if (expr->rank != 0)
7965 : : {
7966 : 0 : gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
7967 : 0 : return false;
7968 : : }
7969 : :
7970 : 148444 : if (expr->ts.type != BT_INTEGER)
7971 : : {
7972 : 274 : if (expr->ts.type == BT_REAL)
7973 : : {
7974 : 274 : if (real_ok)
7975 : 271 : return gfc_notify_std (GFC_STD_F95_DEL,
7976 : : "%s at %L must be integer",
7977 : 271 : _(name_msgid), &expr->where);
7978 : : else
7979 : : {
7980 : 3 : gfc_error ("%s at %L must be INTEGER", _(name_msgid),
7981 : : &expr->where);
7982 : 3 : return false;
7983 : : }
7984 : : }
7985 : : else
7986 : : {
7987 : 0 : gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
7988 : 0 : return false;
7989 : : }
7990 : : }
7991 : : return true;
7992 : : }
7993 : :
7994 : :
7995 : : /* Resolve the expressions in an iterator structure. If REAL_OK is
7996 : : false allow only INTEGER type iterators, otherwise allow REAL types.
7997 : : Set own_scope to true for ac-implied-do and data-implied-do as those
7998 : : have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
7999 : :
8000 : : bool
8001 : 37121 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8002 : : {
8003 : 37121 : if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8004 : : return false;
8005 : :
8006 : 37117 : if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8007 : 37117 : _("iterator variable")))
8008 : : return false;
8009 : :
8010 : 37111 : if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8011 : : "Start expression in DO loop"))
8012 : : return false;
8013 : :
8014 : 37110 : if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8015 : : "End expression in DO loop"))
8016 : : return false;
8017 : :
8018 : 37107 : if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8019 : : "Step expression in DO loop"))
8020 : : return false;
8021 : :
8022 : : /* Convert start, end, and step to the same type as var. */
8023 : 37106 : if (iter->start->ts.kind != iter->var->ts.kind
8024 : 36826 : || iter->start->ts.type != iter->var->ts.type)
8025 : 315 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8026 : :
8027 : 37106 : if (iter->end->ts.kind != iter->var->ts.kind
8028 : 36853 : || iter->end->ts.type != iter->var->ts.type)
8029 : 278 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8030 : :
8031 : 37106 : if (iter->step->ts.kind != iter->var->ts.kind
8032 : 36862 : || iter->step->ts.type != iter->var->ts.type)
8033 : 280 : gfc_convert_type (iter->step, &iter->var->ts, 1);
8034 : :
8035 : 37106 : if (iter->step->expr_type == EXPR_CONSTANT)
8036 : : {
8037 : 35984 : if ((iter->step->ts.type == BT_INTEGER
8038 : 35901 : && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8039 : 71883 : || (iter->step->ts.type == BT_REAL
8040 : 83 : && mpfr_sgn (iter->step->value.real) == 0))
8041 : : {
8042 : 3 : gfc_error ("Step expression in DO loop at %L cannot be zero",
8043 : 3 : &iter->step->where);
8044 : 3 : return false;
8045 : : }
8046 : : }
8047 : :
8048 : 37103 : if (iter->start->expr_type == EXPR_CONSTANT
8049 : 34055 : && iter->end->expr_type == EXPR_CONSTANT
8050 : 26691 : && iter->step->expr_type == EXPR_CONSTANT)
8051 : : {
8052 : 26424 : int sgn, cmp;
8053 : 26424 : if (iter->start->ts.type == BT_INTEGER)
8054 : : {
8055 : 26370 : sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8056 : 26370 : cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8057 : : }
8058 : : else
8059 : : {
8060 : 54 : sgn = mpfr_sgn (iter->step->value.real);
8061 : 54 : cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8062 : : }
8063 : 26424 : if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8064 : 146 : gfc_warning (OPT_Wzerotrip,
8065 : : "DO loop at %L will be executed zero times",
8066 : 146 : &iter->step->where);
8067 : : }
8068 : :
8069 : 37103 : if (iter->end->expr_type == EXPR_CONSTANT
8070 : 27056 : && iter->end->ts.type == BT_INTEGER
8071 : 27002 : && iter->step->expr_type == EXPR_CONSTANT
8072 : 26692 : && iter->step->ts.type == BT_INTEGER
8073 : 26692 : && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8074 : 26324 : || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8075 : : {
8076 : 25578 : bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8077 : 25578 : int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8078 : :
8079 : 25578 : if (is_step_positive
8080 : 25210 : && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8081 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8082 : : "DO loop at %L is undefined as it overflows",
8083 : 7 : &iter->step->where);
8084 : : else if (!is_step_positive
8085 : 368 : && mpz_cmp (iter->end->value.integer,
8086 : 368 : gfc_integer_kinds[k].min_int) == 0)
8087 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8088 : : "DO loop at %L is undefined as it underflows",
8089 : 7 : &iter->step->where);
8090 : : }
8091 : :
8092 : : return true;
8093 : : }
8094 : :
8095 : :
8096 : : /* Traversal function for find_forall_index. f == 2 signals that
8097 : : that variable itself is not to be checked - only the references. */
8098 : :
8099 : : static bool
8100 : 41664 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8101 : : {
8102 : 41664 : if (expr->expr_type != EXPR_VARIABLE)
8103 : : return false;
8104 : :
8105 : : /* A scalar assignment */
8106 : 17614 : if (!expr->ref || *f == 1)
8107 : : {
8108 : 11914 : if (expr->symtree->n.sym == sym)
8109 : : return true;
8110 : : else
8111 : : return false;
8112 : : }
8113 : :
8114 : 5700 : if (*f == 2)
8115 : 1730 : *f = 1;
8116 : : return false;
8117 : : }
8118 : :
8119 : :
8120 : : /* Check whether the FORALL index appears in the expression or not.
8121 : : Returns true if SYM is found in EXPR. */
8122 : :
8123 : : bool
8124 : 26366 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8125 : : {
8126 : 26366 : if (gfc_traverse_expr (expr, sym, forall_index, f))
8127 : : return true;
8128 : : else
8129 : : return false;
8130 : : }
8131 : :
8132 : : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8133 : : This constraint specifies rules for variables in locality-specs. */
8134 : :
8135 : : static int
8136 : 573 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8137 : : {
8138 : 573 : struct check_default_none_data *dt = (struct check_default_none_data *) data;
8139 : :
8140 : 573 : if ((*expr)->expr_type == EXPR_VARIABLE)
8141 : : {
8142 : 18 : gfc_symbol *sym = (*expr)->symtree->n.sym;
8143 : 18 : for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8144 : 20 : list; list = list->next)
8145 : : {
8146 : 2 : if (list->expr->symtree->n.sym == sym)
8147 : : {
8148 : 0 : gfc_error ("Variable %qs referenced in concurrent-header at %L "
8149 : : "must not appear in LOCAL locality-spec at %L",
8150 : : sym->name, &(*expr)->where, &list->expr->where);
8151 : 0 : *walk_subtrees = 0;
8152 : 0 : return 1;
8153 : : }
8154 : : }
8155 : : }
8156 : :
8157 : 573 : *walk_subtrees = 1;
8158 : 573 : return 0;
8159 : : }
8160 : :
8161 : : static int
8162 : 3761 : check_default_none_expr (gfc_expr **e, int *, void *data)
8163 : : {
8164 : 3761 : struct check_default_none_data *d = (struct check_default_none_data*) data;
8165 : :
8166 : 3761 : if ((*e)->expr_type == EXPR_VARIABLE)
8167 : : {
8168 : 1685 : gfc_symbol *sym = (*e)->symtree->n.sym;
8169 : :
8170 : 1685 : if (d->sym_hash->contains (sym))
8171 : 1262 : sym->mark = 1;
8172 : :
8173 : 423 : else if (d->default_none)
8174 : : {
8175 : 3 : gfc_namespace *ns2 = d->ns;
8176 : 5 : while (ns2)
8177 : : {
8178 : 3 : if (ns2 == sym->ns)
8179 : : break;
8180 : 2 : ns2 = ns2->parent;
8181 : : }
8182 : 3 : if (ns2 != NULL)
8183 : : {
8184 : 1 : gfc_error ("Variable %qs at %L not specified in a locality spec "
8185 : : "of DO CONCURRENT at %L but required due to "
8186 : : "DEFAULT (NONE)",
8187 : 1 : sym->name, &(*e)->where, &d->code->loc);
8188 : 1 : d->sym_hash->add (sym);
8189 : : }
8190 : : }
8191 : : }
8192 : 3761 : return 0;
8193 : : }
8194 : :
8195 : : static void
8196 : 171 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8197 : : {
8198 : 171 : struct check_default_none_data data;
8199 : 171 : data.code = code;
8200 : 171 : data.sym_hash = new hash_set<gfc_symbol *>;
8201 : 171 : data.ns = ns;
8202 : 171 : data.default_none = code->ext.concur.default_none;
8203 : :
8204 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8205 : : {
8206 : 684 : const char *name;
8207 : 684 : switch (locality)
8208 : : {
8209 : : case LOCALITY_LOCAL: name = "LOCAL"; break;
8210 : 171 : case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8211 : 171 : case LOCALITY_SHARED: name = "SHARED"; break;
8212 : 171 : case LOCALITY_REDUCE: name = "REDUCE"; break;
8213 : : default: gcc_unreachable ();
8214 : : }
8215 : :
8216 : 1067 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8217 : 383 : list = list->next)
8218 : : {
8219 : 383 : gfc_expr *expr = list->expr;
8220 : :
8221 : 383 : if (locality == LOCALITY_REDUCE
8222 : 72 : && (expr->expr_type == EXPR_FUNCTION
8223 : 48 : || expr->expr_type == EXPR_OP))
8224 : 35 : continue;
8225 : :
8226 : 359 : if (!gfc_resolve_expr (expr))
8227 : 3 : continue;
8228 : :
8229 : 356 : if (expr->expr_type != EXPR_VARIABLE
8230 : 356 : || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8231 : 356 : || (expr->ref
8232 : 147 : && (expr->ref->type != REF_ARRAY
8233 : 147 : || expr->ref->u.ar.type != AR_FULL
8234 : 143 : || expr->ref->next)))
8235 : : {
8236 : 4 : gfc_error ("Expected variable name in %s locality spec at %L",
8237 : : name, &expr->where);
8238 : 4 : continue;
8239 : : }
8240 : :
8241 : 352 : gfc_symbol *sym = expr->symtree->n.sym;
8242 : :
8243 : 352 : if (data.sym_hash->contains (sym))
8244 : : {
8245 : 4 : gfc_error ("Variable %qs at %L has already been specified in a "
8246 : : "locality-spec", sym->name, &expr->where);
8247 : 4 : continue;
8248 : : }
8249 : :
8250 : 348 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8251 : 696 : iter; iter = iter->next)
8252 : : {
8253 : 348 : if (iter->var->symtree->n.sym == sym)
8254 : : {
8255 : 0 : gfc_error ("Index variable %qs at %L cannot be specified in a "
8256 : : "locality-spec", sym->name, &expr->where);
8257 : 0 : continue;
8258 : : }
8259 : :
8260 : 348 : data.sym_hash->add (iter->var->symtree->n.sym);
8261 : : }
8262 : :
8263 : 348 : if (locality == LOCALITY_LOCAL
8264 : 348 : || locality == LOCALITY_LOCAL_INIT
8265 : 348 : || locality == LOCALITY_REDUCE)
8266 : : {
8267 : 194 : if (sym->attr.optional)
8268 : 3 : gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8269 : : "locality-spec at %L",
8270 : : sym->name, name, &expr->where);
8271 : :
8272 : 194 : if (sym->attr.dimension
8273 : 66 : && sym->as
8274 : 66 : && sym->as->type == AS_ASSUMED_SIZE)
8275 : 0 : gfc_error ("Assumed-size array not permitted for %qs in %s "
8276 : : "locality-spec at %L",
8277 : : sym->name, name, &expr->where);
8278 : :
8279 : 194 : gfc_check_vardef_context (expr, false, false, false, name);
8280 : : }
8281 : :
8282 : 194 : if (locality == LOCALITY_LOCAL
8283 : : || locality == LOCALITY_LOCAL_INIT)
8284 : : {
8285 : 177 : symbol_attribute attr = gfc_expr_attr (expr);
8286 : :
8287 : 177 : if (attr.allocatable)
8288 : 2 : gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8289 : : "locality-spec at %L",
8290 : : sym->name, name, &expr->where);
8291 : :
8292 : 175 : else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8293 : 2 : gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8294 : : " for %qs in %s locality-spec at %L",
8295 : : sym->name, name, &expr->where);
8296 : :
8297 : 173 : else if (attr.codimension)
8298 : 0 : gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8299 : : "at %L",
8300 : : sym->name, name, &expr->where);
8301 : :
8302 : 173 : else if (expr->ts.type == BT_DERIVED
8303 : 173 : && gfc_is_finalizable (expr->ts.u.derived, NULL))
8304 : 0 : gfc_error ("Finalizable type not permitted for %qs in %s "
8305 : : "locality-spec at %L",
8306 : : sym->name, name, &expr->where);
8307 : :
8308 : 173 : else if (gfc_has_ultimate_allocatable (expr))
8309 : 4 : gfc_error ("Type with ultimate allocatable component not "
8310 : : "permitted for %qs in %s locality-spec at %L",
8311 : : sym->name, name, &expr->where);
8312 : : }
8313 : :
8314 : 171 : else if (locality == LOCALITY_REDUCE)
8315 : : {
8316 : 17 : if (sym->attr.asynchronous)
8317 : 1 : gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8318 : : "REDUCE locality-spec at %L",
8319 : : sym->name, &expr->where);
8320 : 17 : if (sym->attr.volatile_)
8321 : 1 : gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8322 : : "locality-spec at %L", sym->name, &expr->where);
8323 : : }
8324 : :
8325 : 348 : data.sym_hash->add (sym);
8326 : : }
8327 : :
8328 : 684 : if (locality == LOCALITY_LOCAL)
8329 : : {
8330 : 171 : gcc_assert (locality == 0);
8331 : :
8332 : 171 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8333 : 353 : iter; iter = iter->next)
8334 : : {
8335 : 182 : gfc_expr_walker (&iter->start,
8336 : : do_concur_locality_specs_f2023,
8337 : : &data);
8338 : :
8339 : 182 : gfc_expr_walker (&iter->end,
8340 : : do_concur_locality_specs_f2023,
8341 : : &data);
8342 : :
8343 : 182 : gfc_expr_walker (&iter->stride,
8344 : : do_concur_locality_specs_f2023,
8345 : : &data);
8346 : : }
8347 : :
8348 : 171 : if (code->expr1)
8349 : 7 : gfc_expr_walker (&code->expr1,
8350 : : do_concur_locality_specs_f2023,
8351 : : &data);
8352 : : }
8353 : : }
8354 : :
8355 : 171 : gfc_expr *reduce_op = NULL;
8356 : :
8357 : 171 : for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8358 : 219 : list; list = list->next)
8359 : : {
8360 : 48 : gfc_expr *expr = list->expr;
8361 : :
8362 : 48 : if (expr->expr_type != EXPR_VARIABLE)
8363 : : {
8364 : 24 : reduce_op = expr;
8365 : 24 : continue;
8366 : : }
8367 : :
8368 : 24 : if (reduce_op->expr_type == EXPR_OP)
8369 : : {
8370 : 17 : switch (reduce_op->value.op.op)
8371 : : {
8372 : 17 : case INTRINSIC_PLUS:
8373 : 17 : case INTRINSIC_TIMES:
8374 : 17 : if (!gfc_numeric_ts (&expr->ts))
8375 : 3 : gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8376 : 3 : "got %s", expr->symtree->n.sym->name,
8377 : : &expr->where, gfc_basic_typename (expr->ts.type));
8378 : : break;
8379 : 0 : case INTRINSIC_AND:
8380 : 0 : case INTRINSIC_OR:
8381 : 0 : case INTRINSIC_EQV:
8382 : 0 : case INTRINSIC_NEQV:
8383 : 0 : if (expr->ts.type != BT_LOGICAL)
8384 : 0 : gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8385 : 0 : "got %qs", expr->symtree->n.sym->name,
8386 : : &expr->where, gfc_basic_typename (expr->ts.type));
8387 : : break;
8388 : 0 : default:
8389 : 0 : gcc_unreachable ();
8390 : : }
8391 : : }
8392 : :
8393 : 7 : else if (reduce_op->expr_type == EXPR_FUNCTION)
8394 : : {
8395 : 7 : switch (reduce_op->value.function.isym->id)
8396 : : {
8397 : 6 : case GFC_ISYM_MIN:
8398 : 6 : case GFC_ISYM_MAX:
8399 : 6 : if (expr->ts.type != BT_INTEGER
8400 : : && expr->ts.type != BT_REAL
8401 : : && expr->ts.type != BT_CHARACTER)
8402 : 2 : gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8403 : : "in REDUCE with MIN/MAX at %L, got %s",
8404 : 2 : expr->symtree->n.sym->name, &expr->where,
8405 : : gfc_basic_typename (expr->ts.type));
8406 : : break;
8407 : 1 : case GFC_ISYM_IAND:
8408 : 1 : case GFC_ISYM_IOR:
8409 : 1 : case GFC_ISYM_IEOR:
8410 : 1 : if (expr->ts.type != BT_INTEGER)
8411 : 1 : gfc_error ("Expected integer type for %qs in REDUCE with "
8412 : : "IAND/IOR/IEOR at %L, got %s",
8413 : 1 : expr->symtree->n.sym->name, &expr->where,
8414 : : gfc_basic_typename (expr->ts.type));
8415 : : break;
8416 : 0 : default:
8417 : 0 : gcc_unreachable ();
8418 : : }
8419 : : }
8420 : :
8421 : : else
8422 : 0 : gcc_unreachable ();
8423 : : }
8424 : :
8425 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8426 : : {
8427 : 1067 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8428 : 383 : list = list->next)
8429 : : {
8430 : 383 : if (list->expr->expr_type == EXPR_VARIABLE)
8431 : 359 : list->expr->symtree->n.sym->mark = 0;
8432 : : }
8433 : : }
8434 : :
8435 : 171 : gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8436 : : check_default_none_expr, &data);
8437 : :
8438 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8439 : : {
8440 : 684 : gfc_expr_list **plist = &code->ext.concur.locality[locality];
8441 : 1067 : while (*plist)
8442 : : {
8443 : 383 : gfc_expr *expr = (*plist)->expr;
8444 : 383 : if (expr->expr_type == EXPR_VARIABLE)
8445 : : {
8446 : 359 : gfc_symbol *sym = expr->symtree->n.sym;
8447 : 359 : if (sym->mark == 0)
8448 : : {
8449 : 67 : gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8450 : : "locality-spec at %L is not used",
8451 : : sym->name, &expr->where);
8452 : 67 : gfc_expr_list *tmp = *plist;
8453 : 67 : *plist = (*plist)->next;
8454 : 67 : gfc_free_expr (tmp->expr);
8455 : 67 : free (tmp);
8456 : 67 : continue;
8457 : 67 : }
8458 : : }
8459 : 316 : plist = &((*plist)->next);
8460 : : }
8461 : : }
8462 : 171 : }
8463 : :
8464 : : /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8465 : : to be a scalar INTEGER variable. The subscripts and stride are scalar
8466 : : INTEGERs, and if stride is a constant it must be nonzero.
8467 : : Furthermore "A subscript or stride in a forall-triplet-spec shall
8468 : : not contain a reference to any index-name in the
8469 : : forall-triplet-spec-list in which it appears." (7.5.4.1) */
8470 : :
8471 : : static void
8472 : 2161 : resolve_forall_iterators (gfc_forall_iterator *it)
8473 : : {
8474 : 2161 : gfc_forall_iterator *iter, *iter2;
8475 : :
8476 : 6231 : for (iter = it; iter; iter = iter->next)
8477 : : {
8478 : 4070 : if (gfc_resolve_expr (iter->var)
8479 : 4070 : && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8480 : 0 : gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8481 : : &iter->var->where);
8482 : :
8483 : 4070 : if (gfc_resolve_expr (iter->start)
8484 : 4070 : && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8485 : 0 : gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8486 : : &iter->start->where);
8487 : 4070 : if (iter->var->ts.kind != iter->start->ts.kind)
8488 : 1 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8489 : :
8490 : 4070 : if (gfc_resolve_expr (iter->end)
8491 : 4070 : && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8492 : 0 : gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8493 : : &iter->end->where);
8494 : 4070 : if (iter->var->ts.kind != iter->end->ts.kind)
8495 : 2 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8496 : :
8497 : 4070 : if (gfc_resolve_expr (iter->stride))
8498 : : {
8499 : 4070 : if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8500 : 0 : gfc_error ("FORALL stride expression at %L must be a scalar %s",
8501 : : &iter->stride->where, "INTEGER");
8502 : :
8503 : 4070 : if (iter->stride->expr_type == EXPR_CONSTANT
8504 : 4067 : && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8505 : 1 : gfc_error ("FORALL stride expression at %L cannot be zero",
8506 : : &iter->stride->where);
8507 : : }
8508 : 4070 : if (iter->var->ts.kind != iter->stride->ts.kind)
8509 : 1 : gfc_convert_type (iter->stride, &iter->var->ts, 1);
8510 : : }
8511 : :
8512 : 6231 : for (iter = it; iter; iter = iter->next)
8513 : 10975 : for (iter2 = iter; iter2; iter2 = iter2->next)
8514 : : {
8515 : 6905 : if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8516 : 6903 : || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8517 : 13806 : || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8518 : 6 : gfc_error ("FORALL index %qs may not appear in triplet "
8519 : 6 : "specification at %L", iter->var->symtree->name,
8520 : 6 : &iter2->start->where);
8521 : : }
8522 : 2161 : }
8523 : :
8524 : :
8525 : : /* Given a pointer to a symbol that is a derived type, see if it's
8526 : : inaccessible, i.e. if it's defined in another module and the components are
8527 : : PRIVATE. The search is recursive if necessary. Returns zero if no
8528 : : inaccessible components are found, nonzero otherwise. */
8529 : :
8530 : : static bool
8531 : 1311 : derived_inaccessible (gfc_symbol *sym)
8532 : : {
8533 : 1311 : gfc_component *c;
8534 : :
8535 : 1311 : if (sym->attr.use_assoc && sym->attr.private_comp)
8536 : : return 1;
8537 : :
8538 : 3895 : for (c = sym->components; c; c = c->next)
8539 : : {
8540 : : /* Prevent an infinite loop through this function. */
8541 : 2597 : if (c->ts.type == BT_DERIVED
8542 : 282 : && (c->attr.pointer || c->attr.allocatable)
8543 : 72 : && sym == c->ts.u.derived)
8544 : 72 : continue;
8545 : :
8546 : 2525 : if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8547 : : return 1;
8548 : : }
8549 : :
8550 : : return 0;
8551 : : }
8552 : :
8553 : :
8554 : : /* Resolve the argument of a deallocate expression. The expression must be
8555 : : a pointer or a full array. */
8556 : :
8557 : : static bool
8558 : 7854 : resolve_deallocate_expr (gfc_expr *e)
8559 : : {
8560 : 7854 : symbol_attribute attr;
8561 : 7854 : int allocatable, pointer;
8562 : 7854 : gfc_ref *ref;
8563 : 7854 : gfc_symbol *sym;
8564 : 7854 : gfc_component *c;
8565 : 7854 : bool unlimited;
8566 : :
8567 : 7854 : if (!gfc_resolve_expr (e))
8568 : : return false;
8569 : :
8570 : 7854 : if (e->expr_type != EXPR_VARIABLE)
8571 : 0 : goto bad;
8572 : :
8573 : 7854 : sym = e->symtree->n.sym;
8574 : 7854 : unlimited = UNLIMITED_POLY(sym);
8575 : :
8576 : 7854 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8577 : : {
8578 : 1486 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8579 : 1486 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8580 : : }
8581 : : else
8582 : : {
8583 : 6368 : allocatable = sym->attr.allocatable;
8584 : 6368 : pointer = sym->attr.pointer;
8585 : : }
8586 : 15706 : for (ref = e->ref; ref; ref = ref->next)
8587 : : {
8588 : 7852 : switch (ref->type)
8589 : : {
8590 : 5840 : case REF_ARRAY:
8591 : 5840 : if (ref->u.ar.type != AR_FULL
8592 : 6028 : && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8593 : 188 : && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8594 : : allocatable = 0;
8595 : : break;
8596 : :
8597 : 2012 : case REF_COMPONENT:
8598 : 2012 : c = ref->u.c.component;
8599 : 2012 : if (c->ts.type == BT_CLASS)
8600 : : {
8601 : 272 : allocatable = CLASS_DATA (c)->attr.allocatable;
8602 : 272 : pointer = CLASS_DATA (c)->attr.class_pointer;
8603 : : }
8604 : : else
8605 : : {
8606 : 1740 : allocatable = c->attr.allocatable;
8607 : 1740 : pointer = c->attr.pointer;
8608 : : }
8609 : : break;
8610 : :
8611 : : case REF_SUBSTRING:
8612 : : case REF_INQUIRY:
8613 : 471 : allocatable = 0;
8614 : : break;
8615 : : }
8616 : : }
8617 : :
8618 : 7854 : attr = gfc_expr_attr (e);
8619 : :
8620 : 7854 : if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8621 : : {
8622 : 3 : bad:
8623 : 3 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8624 : : &e->where);
8625 : 3 : return false;
8626 : : }
8627 : :
8628 : : /* F2008, C644. */
8629 : 7851 : if (gfc_is_coindexed (e))
8630 : : {
8631 : 1 : gfc_error ("Coindexed allocatable object at %L", &e->where);
8632 : 1 : return false;
8633 : : }
8634 : :
8635 : 7850 : if (pointer
8636 : 10126 : && !gfc_check_vardef_context (e, true, true, false,
8637 : 2276 : _("DEALLOCATE object")))
8638 : : return false;
8639 : 7848 : if (!gfc_check_vardef_context (e, false, true, false,
8640 : 7848 : _("DEALLOCATE object")))
8641 : : return false;
8642 : :
8643 : : return true;
8644 : : }
8645 : :
8646 : :
8647 : : /* Returns true if the expression e contains a reference to the symbol sym. */
8648 : : static bool
8649 : 46591 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8650 : : {
8651 : 46591 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8652 : 2081 : return true;
8653 : :
8654 : : return false;
8655 : : }
8656 : :
8657 : : bool
8658 : 20027 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8659 : : {
8660 : 20027 : return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8661 : : }
8662 : :
8663 : : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
8664 : : of character expressions. */
8665 : : static bool
8666 : 20179 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
8667 : : {
8668 : 0 : return gfc_traverse_expr (e, sym, sym_in_expr, -1);
8669 : : }
8670 : :
8671 : :
8672 : : /* Given the expression node e for an allocatable/pointer of derived type to be
8673 : : allocated, get the expression node to be initialized afterwards (needed for
8674 : : derived types with default initializers, and derived types with allocatable
8675 : : components that need nullification.) */
8676 : :
8677 : : gfc_expr *
8678 : 5455 : gfc_expr_to_initialize (gfc_expr *e)
8679 : : {
8680 : 5455 : gfc_expr *result;
8681 : 5455 : gfc_ref *ref;
8682 : 5455 : int i;
8683 : :
8684 : 5455 : result = gfc_copy_expr (e);
8685 : :
8686 : : /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8687 : 10769 : for (ref = result->ref; ref; ref = ref->next)
8688 : 8412 : if (ref->type == REF_ARRAY && ref->next == NULL)
8689 : : {
8690 : 3098 : if (ref->u.ar.dimen == 0
8691 : 64 : && ref->u.ar.as && ref->u.ar.as->corank)
8692 : : return result;
8693 : :
8694 : 3034 : ref->u.ar.type = AR_FULL;
8695 : :
8696 : 6857 : for (i = 0; i < ref->u.ar.dimen; i++)
8697 : 3823 : ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8698 : :
8699 : : break;
8700 : : }
8701 : :
8702 : 5391 : gfc_free_shape (&result->shape, result->rank);
8703 : :
8704 : : /* Recalculate rank, shape, etc. */
8705 : 5391 : gfc_resolve_expr (result);
8706 : 5391 : return result;
8707 : : }
8708 : :
8709 : :
8710 : : /* If the last ref of an expression is an array ref, return a copy of the
8711 : : expression with that one removed. Otherwise, a copy of the original
8712 : : expression. This is used for allocate-expressions and pointer assignment
8713 : : LHS, where there may be an array specification that needs to be stripped
8714 : : off when using gfc_check_vardef_context. */
8715 : :
8716 : : static gfc_expr*
8717 : 26748 : remove_last_array_ref (gfc_expr* e)
8718 : : {
8719 : 26748 : gfc_expr* e2;
8720 : 26748 : gfc_ref** r;
8721 : :
8722 : 26748 : e2 = gfc_copy_expr (e);
8723 : 34130 : for (r = &e2->ref; *r; r = &(*r)->next)
8724 : 23056 : if ((*r)->type == REF_ARRAY && !(*r)->next)
8725 : : {
8726 : 15674 : gfc_free_ref_list (*r);
8727 : 15674 : *r = NULL;
8728 : 15674 : break;
8729 : : }
8730 : :
8731 : 26748 : return e2;
8732 : : }
8733 : :
8734 : :
8735 : : /* Used in resolve_allocate_expr to check that a allocation-object and
8736 : : a source-expr are conformable. This does not catch all possible
8737 : : cases; in particular a runtime checking is needed. */
8738 : :
8739 : : static bool
8740 : 1835 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
8741 : : {
8742 : 1835 : gfc_ref *tail;
8743 : 1835 : bool scalar;
8744 : :
8745 : 2554 : for (tail = e2->ref; tail && tail->next; tail = tail->next);
8746 : :
8747 : : /* If MOLD= is present and is not scalar, and the allocate-object has an
8748 : : explicit-shape-spec, the ranks need not agree. This may be unintended,
8749 : : so let's emit a warning if -Wsurprising is given. */
8750 : 1835 : scalar = !tail || tail->type == REF_COMPONENT;
8751 : 1835 : if (e1->mold && e1->rank > 0
8752 : 163 : && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
8753 : : {
8754 : 26 : if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
8755 : 15 : gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
8756 : : "but MOLD= expression at %L has rank %d",
8757 : 6 : &e2->where, scalar ? 0 : tail->u.ar.as->rank,
8758 : : &e1->where, e1->rank);
8759 : 29 : return true;
8760 : : }
8761 : :
8762 : : /* First compare rank. */
8763 : 1806 : if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
8764 : 2 : || (!tail && e1->rank != e2->rank))
8765 : : {
8766 : 7 : gfc_error ("Source-expr at %L must be scalar or have the "
8767 : : "same rank as the allocate-object at %L",
8768 : : &e1->where, &e2->where);
8769 : 7 : return false;
8770 : : }
8771 : :
8772 : 1799 : if (e1->shape)
8773 : : {
8774 : 1312 : int i;
8775 : 1312 : mpz_t s;
8776 : :
8777 : 1312 : mpz_init (s);
8778 : :
8779 : 3040 : for (i = 0; i < e1->rank; i++)
8780 : : {
8781 : 1318 : if (tail->u.ar.start[i] == NULL)
8782 : : break;
8783 : :
8784 : 416 : if (tail->u.ar.end[i])
8785 : : {
8786 : 54 : mpz_set (s, tail->u.ar.end[i]->value.integer);
8787 : 54 : mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8788 : 54 : mpz_add_ui (s, s, 1);
8789 : : }
8790 : : else
8791 : : {
8792 : 362 : mpz_set (s, tail->u.ar.start[i]->value.integer);
8793 : : }
8794 : :
8795 : 416 : if (mpz_cmp (e1->shape[i], s) != 0)
8796 : : {
8797 : 0 : gfc_error ("Source-expr at %L and allocate-object at %L must "
8798 : : "have the same shape", &e1->where, &e2->where);
8799 : 0 : mpz_clear (s);
8800 : 0 : return false;
8801 : : }
8802 : : }
8803 : :
8804 : 1312 : mpz_clear (s);
8805 : : }
8806 : :
8807 : : return true;
8808 : : }
8809 : :
8810 : :
8811 : : /* Resolve the expression in an ALLOCATE statement, doing the additional
8812 : : checks to see whether the expression is OK or not. The expression must
8813 : : have a trailing array reference that gives the size of the array. */
8814 : :
8815 : : static bool
8816 : 16645 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
8817 : : {
8818 : 16645 : int i, pointer, allocatable, dimension, is_abstract;
8819 : 16645 : int codimension;
8820 : 16645 : bool coindexed;
8821 : 16645 : bool unlimited;
8822 : 16645 : symbol_attribute attr;
8823 : 16645 : gfc_ref *ref, *ref2;
8824 : 16645 : gfc_expr *e2;
8825 : 16645 : gfc_array_ref *ar;
8826 : 16645 : gfc_symbol *sym = NULL;
8827 : 16645 : gfc_alloc *a;
8828 : 16645 : gfc_component *c;
8829 : 16645 : bool t;
8830 : :
8831 : : /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
8832 : : checking of coarrays. */
8833 : 20837 : for (ref = e->ref; ref; ref = ref->next)
8834 : 16770 : if (ref->next == NULL)
8835 : : break;
8836 : :
8837 : 16645 : if (ref && ref->type == REF_ARRAY)
8838 : 11444 : ref->u.ar.in_allocate = true;
8839 : :
8840 : 16645 : if (!gfc_resolve_expr (e))
8841 : 1 : goto failure;
8842 : :
8843 : : /* Make sure the expression is allocatable or a pointer. If it is
8844 : : pointer, the next-to-last reference must be a pointer. */
8845 : :
8846 : 16644 : ref2 = NULL;
8847 : 16644 : if (e->symtree)
8848 : 16644 : sym = e->symtree->n.sym;
8849 : :
8850 : : /* Check whether ultimate component is abstract and CLASS. */
8851 : 33288 : is_abstract = 0;
8852 : :
8853 : : /* Is the allocate-object unlimited polymorphic? */
8854 : 16644 : unlimited = UNLIMITED_POLY(e);
8855 : :
8856 : 16644 : if (e->expr_type != EXPR_VARIABLE)
8857 : : {
8858 : 0 : allocatable = 0;
8859 : 0 : attr = gfc_expr_attr (e);
8860 : 0 : pointer = attr.pointer;
8861 : 0 : dimension = attr.dimension;
8862 : 0 : codimension = attr.codimension;
8863 : : }
8864 : : else
8865 : : {
8866 : 16644 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
8867 : : {
8868 : 3284 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8869 : 3284 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8870 : 3284 : dimension = CLASS_DATA (sym)->attr.dimension;
8871 : 3284 : codimension = CLASS_DATA (sym)->attr.codimension;
8872 : 3284 : is_abstract = CLASS_DATA (sym)->attr.abstract;
8873 : : }
8874 : : else
8875 : : {
8876 : 13360 : allocatable = sym->attr.allocatable;
8877 : 13360 : pointer = sym->attr.pointer;
8878 : 13360 : dimension = sym->attr.dimension;
8879 : 13360 : codimension = sym->attr.codimension;
8880 : : }
8881 : :
8882 : 16644 : coindexed = false;
8883 : :
8884 : 33408 : for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
8885 : : {
8886 : 16766 : switch (ref->type)
8887 : : {
8888 : 12736 : case REF_ARRAY:
8889 : 12736 : if (ref->u.ar.codimen > 0)
8890 : : {
8891 : 653 : int n;
8892 : 934 : for (n = ref->u.ar.dimen;
8893 : 934 : n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
8894 : 690 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
8895 : : {
8896 : : coindexed = true;
8897 : : break;
8898 : : }
8899 : : }
8900 : :
8901 : 12736 : if (ref->next != NULL)
8902 : 1294 : pointer = 0;
8903 : : break;
8904 : :
8905 : 4030 : case REF_COMPONENT:
8906 : : /* F2008, C644. */
8907 : 4030 : if (coindexed)
8908 : : {
8909 : 2 : gfc_error ("Coindexed allocatable object at %L",
8910 : : &e->where);
8911 : 2 : goto failure;
8912 : : }
8913 : :
8914 : 4028 : c = ref->u.c.component;
8915 : 4028 : if (c->ts.type == BT_CLASS)
8916 : : {
8917 : 977 : allocatable = CLASS_DATA (c)->attr.allocatable;
8918 : 977 : pointer = CLASS_DATA (c)->attr.class_pointer;
8919 : 977 : dimension = CLASS_DATA (c)->attr.dimension;
8920 : 977 : codimension = CLASS_DATA (c)->attr.codimension;
8921 : 977 : is_abstract = CLASS_DATA (c)->attr.abstract;
8922 : : }
8923 : : else
8924 : : {
8925 : 3051 : allocatable = c->attr.allocatable;
8926 : 3051 : pointer = c->attr.pointer;
8927 : 3051 : dimension = c->attr.dimension;
8928 : 3051 : codimension = c->attr.codimension;
8929 : 3051 : is_abstract = c->attr.abstract;
8930 : : }
8931 : : break;
8932 : :
8933 : 0 : case REF_SUBSTRING:
8934 : 0 : case REF_INQUIRY:
8935 : 0 : allocatable = 0;
8936 : 0 : pointer = 0;
8937 : 0 : break;
8938 : : }
8939 : : }
8940 : : }
8941 : :
8942 : : /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
8943 : : pointer or an allocatable variable. */
8944 : 16642 : if (allocatable == 0 && pointer == 0)
8945 : : {
8946 : 4 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8947 : : &e->where);
8948 : 4 : goto failure;
8949 : : }
8950 : :
8951 : : /* Some checks for the SOURCE tag. */
8952 : 16638 : if (code->expr3)
8953 : : {
8954 : : /* Check F03:C632: "The source-expr shall be a scalar or have the same
8955 : : rank as allocate-object". This would require the MOLD argument to
8956 : : NULL() as source-expr for subsequent checking. However, even the
8957 : : resulting disassociated pointer or unallocated array has no shape that
8958 : : could be used for SOURCE= or MOLD=. */
8959 : 3716 : if (code->expr3->expr_type == EXPR_NULL)
8960 : : {
8961 : 4 : gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
8962 : : &code->expr3->where);
8963 : 4 : goto failure;
8964 : : }
8965 : :
8966 : : /* Check F03:C631. */
8967 : 3712 : if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
8968 : : {
8969 : 10 : gfc_error ("Type of entity at %L is type incompatible with "
8970 : 10 : "source-expr at %L", &e->where, &code->expr3->where);
8971 : 10 : goto failure;
8972 : : }
8973 : :
8974 : : /* Check F03:C632 and restriction following Note 6.18. */
8975 : 3702 : if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
8976 : 7 : goto failure;
8977 : :
8978 : : /* Check F03:C633. */
8979 : 3695 : if (code->expr3->ts.kind != e->ts.kind && !unlimited)
8980 : : {
8981 : 1 : gfc_error ("The allocate-object at %L and the source-expr at %L "
8982 : : "shall have the same kind type parameter",
8983 : : &e->where, &code->expr3->where);
8984 : 1 : goto failure;
8985 : : }
8986 : :
8987 : : /* Check F2008, C642. */
8988 : 3694 : if (code->expr3->ts.type == BT_DERIVED
8989 : 3694 : && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
8990 : 1163 : || (code->expr3->ts.u.derived->from_intmod
8991 : : == INTMOD_ISO_FORTRAN_ENV
8992 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
8993 : : == ISOFORTRAN_LOCK_TYPE)))
8994 : : {
8995 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
8996 : : "LOCK_TYPE nor have a LOCK_TYPE component if "
8997 : : "allocate-object at %L is a coarray",
8998 : 0 : &code->expr3->where, &e->where);
8999 : 0 : goto failure;
9000 : : }
9001 : :
9002 : : /* Check F2008:C639: "Corresponding kind type parameters of
9003 : : allocate-object and source-expr shall have the same values." */
9004 : 3694 : if (e->ts.type == BT_CHARACTER
9005 : 758 : && !e->ts.deferred
9006 : 138 : && e->ts.u.cl->length
9007 : 138 : && code->expr3->ts.type == BT_CHARACTER
9008 : 3832 : && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9009 : : "SOURCE= or MOLD= specifier"))
9010 : 17 : goto failure;
9011 : :
9012 : : /* Check TS18508, C702/C703. */
9013 : 3677 : if (code->expr3->ts.type == BT_DERIVED
9014 : 4840 : && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9015 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9016 : : == INTMOD_ISO_FORTRAN_ENV
9017 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9018 : : == ISOFORTRAN_EVENT_TYPE)))
9019 : : {
9020 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9021 : : "EVENT_TYPE nor have a EVENT_TYPE component if "
9022 : : "allocate-object at %L is a coarray",
9023 : 0 : &code->expr3->where, &e->where);
9024 : 0 : goto failure;
9025 : : }
9026 : : }
9027 : :
9028 : : /* Check F08:C629. */
9029 : 16599 : if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9030 : 153 : && !code->expr3)
9031 : : {
9032 : 2 : gcc_assert (e->ts.type == BT_CLASS);
9033 : 2 : gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9034 : : "type-spec or source-expr", sym->name, &e->where);
9035 : 2 : goto failure;
9036 : : }
9037 : :
9038 : : /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9039 : : if and only if each allocate-object is a dummy argument for which the
9040 : : corresponding type parameter is assumed. */
9041 : 16597 : if (code->ext.alloc.ts.type == BT_CHARACTER
9042 : 489 : && code->ext.alloc.ts.u.cl->length != NULL
9043 : 474 : && e->ts.type == BT_CHARACTER && !e->ts.deferred
9044 : 23 : && e->ts.u.cl->length == NULL
9045 : 2 : && e->symtree->n.sym->attr.dummy)
9046 : : {
9047 : 2 : gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9048 : : "shall be an asterisk as allocate object %qs at %L is a "
9049 : : "dummy argument with assumed type parameter",
9050 : : sym->name, &e->where);
9051 : 2 : goto failure;
9052 : : }
9053 : :
9054 : : /* Check F08:C632. */
9055 : 16595 : if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9056 : 60 : && !UNLIMITED_POLY (e))
9057 : : {
9058 : 36 : int cmp;
9059 : :
9060 : 36 : if (!e->ts.u.cl->length)
9061 : 15 : goto failure;
9062 : :
9063 : 42 : cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9064 : 21 : code->ext.alloc.ts.u.cl->length);
9065 : 21 : if (cmp == 1 || cmp == -1 || cmp == -3)
9066 : : {
9067 : 2 : gfc_error ("Allocating %s at %L with type-spec requires the same "
9068 : : "character-length parameter as in the declaration",
9069 : : sym->name, &e->where);
9070 : 2 : goto failure;
9071 : : }
9072 : : }
9073 : :
9074 : : /* In the variable definition context checks, gfc_expr_attr is used
9075 : : on the expression. This is fooled by the array specification
9076 : : present in e, thus we have to eliminate that one temporarily. */
9077 : 16578 : e2 = remove_last_array_ref (e);
9078 : 16578 : t = true;
9079 : 16578 : if (t && pointer)
9080 : 3733 : t = gfc_check_vardef_context (e2, true, true, false,
9081 : 3733 : _("ALLOCATE object"));
9082 : 3733 : if (t)
9083 : 16570 : t = gfc_check_vardef_context (e2, false, true, false,
9084 : 16570 : _("ALLOCATE object"));
9085 : 16578 : gfc_free_expr (e2);
9086 : 16578 : if (!t)
9087 : 11 : goto failure;
9088 : :
9089 : 16567 : code->ext.alloc.expr3_not_explicit = 0;
9090 : 16567 : if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9091 : 1568 : && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9092 : : {
9093 : : /* For class arrays, the initialization with SOURCE is done
9094 : : using _copy and trans_call. It is convenient to exploit that
9095 : : when the allocated type is different from the declared type but
9096 : : no SOURCE exists by setting expr3. */
9097 : 284 : code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9098 : 284 : code->ext.alloc.expr3_not_explicit = 1;
9099 : : }
9100 : 16283 : else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9101 : 2370 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9102 : 5 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9103 : : {
9104 : : /* We have to zero initialize the integer variable. */
9105 : 1 : code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9106 : 1 : code->ext.alloc.expr3_not_explicit = 1;
9107 : : }
9108 : :
9109 : 16567 : if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9110 : : {
9111 : : /* Make sure the vtab symbol is present when
9112 : : the module variables are generated. */
9113 : 2899 : gfc_typespec ts = e->ts;
9114 : 2899 : if (code->expr3)
9115 : 1305 : ts = code->expr3->ts;
9116 : 1594 : else if (code->ext.alloc.ts.type == BT_DERIVED)
9117 : 695 : ts = code->ext.alloc.ts;
9118 : :
9119 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9120 : : statement is necessary. */
9121 : 2899 : gfc_find_derived_vtab (ts.u.derived);
9122 : 2899 : }
9123 : 13668 : else if (unlimited && !UNLIMITED_POLY (code->expr3))
9124 : : {
9125 : : /* Again, make sure the vtab symbol is present when
9126 : : the module variables are generated. */
9127 : 433 : gfc_typespec *ts = NULL;
9128 : 433 : if (code->expr3)
9129 : 347 : ts = &code->expr3->ts;
9130 : : else
9131 : 86 : ts = &code->ext.alloc.ts;
9132 : :
9133 : 433 : gcc_assert (ts);
9134 : :
9135 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9136 : : statement is necessary. */
9137 : 433 : gfc_find_vtab (ts);
9138 : : }
9139 : :
9140 : 16567 : if (dimension == 0 && codimension == 0)
9141 : 5154 : goto success;
9142 : :
9143 : : /* Make sure the last reference node is an array specification. */
9144 : :
9145 : 11413 : if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9146 : 10251 : || (dimension && ref2->u.ar.dimen == 0))
9147 : : {
9148 : : /* F08:C633. */
9149 : 1162 : if (code->expr3)
9150 : : {
9151 : 1161 : if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9152 : : "in ALLOCATE statement at %L", &e->where))
9153 : 0 : goto failure;
9154 : 1161 : if (code->expr3->rank != 0)
9155 : 1160 : *array_alloc_wo_spec = true;
9156 : : else
9157 : : {
9158 : 1 : gfc_error ("Array specification or array-valued SOURCE= "
9159 : : "expression required in ALLOCATE statement at %L",
9160 : : &e->where);
9161 : 1 : goto failure;
9162 : : }
9163 : : }
9164 : : else
9165 : : {
9166 : 1 : gfc_error ("Array specification required in ALLOCATE statement "
9167 : : "at %L", &e->where);
9168 : 1 : goto failure;
9169 : : }
9170 : : }
9171 : :
9172 : : /* Make sure that the array section reference makes sense in the
9173 : : context of an ALLOCATE specification. */
9174 : :
9175 : 11411 : ar = &ref2->u.ar;
9176 : :
9177 : 11411 : if (codimension)
9178 : 966 : for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9179 : : {
9180 : 570 : switch (ar->dimen_type[i])
9181 : : {
9182 : 2 : case DIMEN_THIS_IMAGE:
9183 : 2 : gfc_error ("Coarray specification required in ALLOCATE statement "
9184 : : "at %L", &e->where);
9185 : 2 : goto failure;
9186 : :
9187 : 83 : case DIMEN_RANGE:
9188 : : /* F2018:R937:
9189 : : * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9190 : : */
9191 : 83 : if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9192 : : {
9193 : 8 : gfc_error ("Bad coarray specification in ALLOCATE statement "
9194 : : "at %L", &e->where);
9195 : 8 : goto failure;
9196 : : }
9197 : 75 : else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9198 : : {
9199 : 2 : gfc_error ("Upper cobound is less than lower cobound at %L",
9200 : 2 : &ar->start[i]->where);
9201 : 2 : goto failure;
9202 : : }
9203 : : break;
9204 : :
9205 : 89 : case DIMEN_ELEMENT:
9206 : 89 : if (ar->start[i]->expr_type == EXPR_CONSTANT)
9207 : : {
9208 : 81 : gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9209 : 81 : if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9210 : : {
9211 : 1 : gfc_error ("Upper cobound is less than lower cobound "
9212 : : "of 1 at %L", &ar->start[i]->where);
9213 : 1 : goto failure;
9214 : : }
9215 : : }
9216 : : break;
9217 : :
9218 : : case DIMEN_STAR:
9219 : : break;
9220 : :
9221 : 0 : default:
9222 : 0 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9223 : : &e->where);
9224 : 0 : goto failure;
9225 : :
9226 : : }
9227 : : }
9228 : 28105 : for (i = 0; i < ar->dimen; i++)
9229 : : {
9230 : 16711 : if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9231 : 14057 : goto check_symbols;
9232 : :
9233 : 2654 : switch (ar->dimen_type[i])
9234 : : {
9235 : : case DIMEN_ELEMENT:
9236 : : break;
9237 : :
9238 : 2389 : case DIMEN_RANGE:
9239 : 2389 : if (ar->start[i] != NULL
9240 : 2389 : && ar->end[i] != NULL
9241 : 2388 : && ar->stride[i] == NULL)
9242 : : break;
9243 : :
9244 : : /* Fall through. */
9245 : :
9246 : 1 : case DIMEN_UNKNOWN:
9247 : 1 : case DIMEN_VECTOR:
9248 : 1 : case DIMEN_STAR:
9249 : 1 : case DIMEN_THIS_IMAGE:
9250 : 1 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9251 : : &e->where);
9252 : 1 : goto failure;
9253 : : }
9254 : :
9255 : 2388 : check_symbols:
9256 : 43762 : for (a = code->ext.alloc.list; a; a = a->next)
9257 : : {
9258 : 27055 : sym = a->expr->symtree->n.sym;
9259 : :
9260 : : /* TODO - check derived type components. */
9261 : 27055 : if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9262 : 8914 : continue;
9263 : :
9264 : 18141 : if ((ar->start[i] != NULL
9265 : 17559 : && gfc_find_var_in_expr (sym, ar->start[i]))
9266 : 35697 : || (ar->end[i] != NULL
9267 : 2620 : && gfc_find_var_in_expr (sym, ar->end[i])))
9268 : : {
9269 : 3 : gfc_error ("%qs must not appear in the array specification at "
9270 : : "%L in the same ALLOCATE statement where it is "
9271 : : "itself allocated", sym->name, &ar->where);
9272 : 3 : goto failure;
9273 : : }
9274 : : }
9275 : : }
9276 : :
9277 : 11554 : for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9278 : : {
9279 : 715 : if (ar->dimen_type[i] == DIMEN_ELEMENT
9280 : 555 : || ar->dimen_type[i] == DIMEN_RANGE)
9281 : : {
9282 : 160 : if (i == (ar->dimen + ar->codimen - 1))
9283 : : {
9284 : 0 : gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9285 : : "statement at %L", &e->where);
9286 : 0 : goto failure;
9287 : : }
9288 : 160 : continue;
9289 : : }
9290 : :
9291 : 395 : if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9292 : 395 : && ar->stride[i] == NULL)
9293 : : break;
9294 : :
9295 : 0 : gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9296 : : &e->where);
9297 : 0 : goto failure;
9298 : : }
9299 : :
9300 : 11394 : success:
9301 : : return true;
9302 : :
9303 : : failure:
9304 : : return false;
9305 : : }
9306 : :
9307 : :
9308 : : static void
9309 : 19346 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9310 : : {
9311 : 19346 : gfc_expr *stat, *errmsg, *pe, *qe;
9312 : 19346 : gfc_alloc *a, *p, *q;
9313 : :
9314 : 19346 : stat = code->expr1;
9315 : 19346 : errmsg = code->expr2;
9316 : :
9317 : : /* Check the stat variable. */
9318 : 19346 : if (stat)
9319 : : {
9320 : 619 : if (!gfc_check_vardef_context (stat, false, false, false,
9321 : 619 : _("STAT variable")))
9322 : 8 : goto done_stat;
9323 : :
9324 : 611 : if (stat->ts.type != BT_INTEGER
9325 : 602 : || stat->rank > 0)
9326 : 11 : gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9327 : : "variable", &stat->where);
9328 : :
9329 : 611 : if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9330 : 0 : goto done_stat;
9331 : :
9332 : : /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9333 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9334 : : */
9335 : 1267 : for (p = code->ext.alloc.list; p; p = p->next)
9336 : 663 : if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9337 : : {
9338 : 9 : gfc_ref *ref1, *ref2;
9339 : 9 : bool found = true;
9340 : :
9341 : 16 : for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9342 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9343 : : {
9344 : 9 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9345 : 5 : continue;
9346 : 4 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9347 : : {
9348 : : found = false;
9349 : : break;
9350 : : }
9351 : : }
9352 : :
9353 : 9 : if (found)
9354 : : {
9355 : 7 : gfc_error ("Stat-variable at %L shall not be %sd within "
9356 : : "the same %s statement", &stat->where, fcn, fcn);
9357 : 7 : break;
9358 : : }
9359 : : }
9360 : : }
9361 : :
9362 : 18727 : done_stat:
9363 : :
9364 : : /* Check the errmsg variable. */
9365 : 19346 : if (errmsg)
9366 : : {
9367 : 146 : if (!stat)
9368 : 2 : gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9369 : : &errmsg->where);
9370 : :
9371 : 146 : if (!gfc_check_vardef_context (errmsg, false, false, false,
9372 : 146 : _("ERRMSG variable")))
9373 : 6 : goto done_errmsg;
9374 : :
9375 : : /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9376 : : F18:R930 errmsg-variable is scalar-default-char-variable
9377 : : F18:R906 default-char-variable is variable
9378 : : F18:C906 default-char-variable shall be default character. */
9379 : 140 : if (errmsg->ts.type != BT_CHARACTER
9380 : 138 : || errmsg->rank > 0
9381 : 137 : || errmsg->ts.kind != gfc_default_character_kind)
9382 : 4 : gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9383 : : "variable", &errmsg->where);
9384 : :
9385 : 140 : if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9386 : 0 : goto done_errmsg;
9387 : :
9388 : : /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9389 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9390 : : */
9391 : 278 : for (p = code->ext.alloc.list; p; p = p->next)
9392 : 143 : if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9393 : : {
9394 : 9 : gfc_ref *ref1, *ref2;
9395 : 9 : bool found = true;
9396 : :
9397 : 16 : for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9398 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9399 : : {
9400 : 11 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9401 : 4 : continue;
9402 : 7 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9403 : : {
9404 : : found = false;
9405 : : break;
9406 : : }
9407 : : }
9408 : :
9409 : 9 : if (found)
9410 : : {
9411 : 5 : gfc_error ("Errmsg-variable at %L shall not be %sd within "
9412 : : "the same %s statement", &errmsg->where, fcn, fcn);
9413 : 5 : break;
9414 : : }
9415 : : }
9416 : : }
9417 : :
9418 : 19200 : done_errmsg:
9419 : :
9420 : : /* Check that an allocate-object appears only once in the statement. */
9421 : :
9422 : 43845 : for (p = code->ext.alloc.list; p; p = p->next)
9423 : : {
9424 : 24499 : pe = p->expr;
9425 : 33540 : for (q = p->next; q; q = q->next)
9426 : : {
9427 : 9041 : qe = q->expr;
9428 : 9041 : if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9429 : : {
9430 : : /* This is a potential collision. */
9431 : 2093 : gfc_ref *pr = pe->ref;
9432 : 2093 : gfc_ref *qr = qe->ref;
9433 : :
9434 : : /* Follow the references until
9435 : : a) They start to differ, in which case there is no error;
9436 : : you can deallocate a%b and a%c in a single statement
9437 : : b) Both of them stop, which is an error
9438 : : c) One of them stops, which is also an error. */
9439 : 4517 : while (1)
9440 : : {
9441 : 3305 : if (pr == NULL && qr == NULL)
9442 : : {
9443 : 7 : gfc_error ("Allocate-object at %L also appears at %L",
9444 : : &pe->where, &qe->where);
9445 : 7 : break;
9446 : : }
9447 : 3298 : else if (pr != NULL && qr == NULL)
9448 : : {
9449 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9450 : : " object at %L", &pe->where, &qe->where);
9451 : 2 : break;
9452 : : }
9453 : 3296 : else if (pr == NULL && qr != NULL)
9454 : : {
9455 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9456 : : " object at %L", &qe->where, &pe->where);
9457 : 2 : break;
9458 : : }
9459 : : /* Here, pr != NULL && qr != NULL */
9460 : 3294 : gcc_assert(pr->type == qr->type);
9461 : 3294 : if (pr->type == REF_ARRAY)
9462 : : {
9463 : : /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9464 : : which are legal. */
9465 : 1065 : gcc_assert (qr->type == REF_ARRAY);
9466 : :
9467 : 1065 : if (pr->next && qr->next)
9468 : : {
9469 : : int i;
9470 : : gfc_array_ref *par = &(pr->u.ar);
9471 : : gfc_array_ref *qar = &(qr->u.ar);
9472 : :
9473 : 1840 : for (i=0; i<par->dimen; i++)
9474 : : {
9475 : 954 : if ((par->start[i] != NULL
9476 : 0 : || qar->start[i] != NULL)
9477 : 1908 : && gfc_dep_compare_expr (par->start[i],
9478 : 954 : qar->start[i]) != 0)
9479 : 168 : goto break_label;
9480 : : }
9481 : : }
9482 : : }
9483 : : else
9484 : : {
9485 : 2229 : if (pr->u.c.component->name != qr->u.c.component->name)
9486 : : break;
9487 : : }
9488 : :
9489 : 1212 : pr = pr->next;
9490 : 1212 : qr = qr->next;
9491 : 1212 : }
9492 : 9041 : break_label:
9493 : : ;
9494 : : }
9495 : : }
9496 : : }
9497 : :
9498 : 19346 : if (strcmp (fcn, "ALLOCATE") == 0)
9499 : : {
9500 : 13646 : bool arr_alloc_wo_spec = false;
9501 : :
9502 : : /* Resolving the expr3 in the loop over all objects to allocate would
9503 : : execute loop invariant code for each loop item. Therefore do it just
9504 : : once here. */
9505 : 13646 : if (code->expr3 && code->expr3->mold
9506 : 343 : && code->expr3->ts.type == BT_DERIVED)
9507 : : {
9508 : : /* Default initialization via MOLD (non-polymorphic). */
9509 : 20 : gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9510 : 20 : if (rhs != NULL)
9511 : : {
9512 : 7 : gfc_resolve_expr (rhs);
9513 : 7 : gfc_free_expr (code->expr3);
9514 : 7 : code->expr3 = rhs;
9515 : : }
9516 : : }
9517 : 30291 : for (a = code->ext.alloc.list; a; a = a->next)
9518 : 16645 : resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9519 : :
9520 : 13646 : if (arr_alloc_wo_spec && code->expr3)
9521 : : {
9522 : : /* Mark the allocate to have to take the array specification
9523 : : from the expr3. */
9524 : 1154 : code->ext.alloc.arr_spec_from_expr3 = 1;
9525 : : }
9526 : : }
9527 : : else
9528 : : {
9529 : 13554 : for (a = code->ext.alloc.list; a; a = a->next)
9530 : 7854 : resolve_deallocate_expr (a->expr);
9531 : : }
9532 : 19346 : }
9533 : :
9534 : :
9535 : : /************ SELECT CASE resolution subroutines ************/
9536 : :
9537 : : /* Callback function for our mergesort variant. Determines interval
9538 : : overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9539 : : op1 > op2. Assumes we're not dealing with the default case.
9540 : : We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9541 : : There are nine situations to check. */
9542 : :
9543 : : static int
9544 : 1542 : compare_cases (const gfc_case *op1, const gfc_case *op2)
9545 : : {
9546 : 1542 : int retval;
9547 : :
9548 : 1542 : if (op1->low == NULL) /* op1 = (:L) */
9549 : : {
9550 : : /* op2 = (:N), so overlap. */
9551 : 52 : retval = 0;
9552 : : /* op2 = (M:) or (M:N), L < M */
9553 : 52 : if (op2->low != NULL
9554 : 52 : && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9555 : : retval = -1;
9556 : : }
9557 : 1490 : else if (op1->high == NULL) /* op1 = (K:) */
9558 : : {
9559 : : /* op2 = (M:), so overlap. */
9560 : 10 : retval = 0;
9561 : : /* op2 = (:N) or (M:N), K > N */
9562 : 10 : if (op2->high != NULL
9563 : 10 : && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9564 : : retval = 1;
9565 : : }
9566 : : else /* op1 = (K:L) */
9567 : : {
9568 : 1480 : if (op2->low == NULL) /* op2 = (:N), K > N */
9569 : 18 : retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9570 : 18 : ? 1 : 0;
9571 : 1462 : else if (op2->high == NULL) /* op2 = (M:), L < M */
9572 : 14 : retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9573 : 10 : ? -1 : 0;
9574 : : else /* op2 = (M:N) */
9575 : : {
9576 : 1452 : retval = 0;
9577 : : /* L < M */
9578 : 1452 : if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9579 : : retval = -1;
9580 : : /* K > N */
9581 : 412 : else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9582 : 438 : retval = 1;
9583 : : }
9584 : : }
9585 : :
9586 : 1542 : return retval;
9587 : : }
9588 : :
9589 : :
9590 : : /* Merge-sort a double linked case list, detecting overlap in the
9591 : : process. LIST is the head of the double linked case list before it
9592 : : is sorted. Returns the head of the sorted list if we don't see any
9593 : : overlap, or NULL otherwise. */
9594 : :
9595 : : static gfc_case *
9596 : 642 : check_case_overlap (gfc_case *list)
9597 : : {
9598 : 642 : gfc_case *p, *q, *e, *tail;
9599 : 642 : int insize, nmerges, psize, qsize, cmp, overlap_seen;
9600 : :
9601 : : /* If the passed list was empty, return immediately. */
9602 : 642 : if (!list)
9603 : : return NULL;
9604 : :
9605 : : overlap_seen = 0;
9606 : : insize = 1;
9607 : :
9608 : : /* Loop unconditionally. The only exit from this loop is a return
9609 : : statement, when we've finished sorting the case list. */
9610 : 1334 : for (;;)
9611 : : {
9612 : 988 : p = list;
9613 : 988 : list = NULL;
9614 : 988 : tail = NULL;
9615 : :
9616 : : /* Count the number of merges we do in this pass. */
9617 : 988 : nmerges = 0;
9618 : :
9619 : : /* Loop while there exists a merge to be done. */
9620 : 2491 : while (p)
9621 : : {
9622 : 1503 : int i;
9623 : :
9624 : : /* Count this merge. */
9625 : 1503 : nmerges++;
9626 : :
9627 : : /* Cut the list in two pieces by stepping INSIZE places
9628 : : forward in the list, starting from P. */
9629 : 1503 : psize = 0;
9630 : 1503 : q = p;
9631 : 3150 : for (i = 0; i < insize; i++)
9632 : : {
9633 : 2206 : psize++;
9634 : 2206 : q = q->right;
9635 : 2206 : if (!q)
9636 : : break;
9637 : : }
9638 : : qsize = insize;
9639 : :
9640 : : /* Now we have two lists. Merge them! */
9641 : 4918 : while (psize > 0 || (qsize > 0 && q != NULL))
9642 : : {
9643 : : /* See from which the next case to merge comes from. */
9644 : 771 : if (psize == 0)
9645 : : {
9646 : : /* P is empty so the next case must come from Q. */
9647 : 771 : e = q;
9648 : 771 : q = q->right;
9649 : 771 : qsize--;
9650 : : }
9651 : 2644 : else if (qsize == 0 || q == NULL)
9652 : : {
9653 : : /* Q is empty. */
9654 : 1102 : e = p;
9655 : 1102 : p = p->right;
9656 : 1102 : psize--;
9657 : : }
9658 : : else
9659 : : {
9660 : 1542 : cmp = compare_cases (p, q);
9661 : 1542 : if (cmp < 0)
9662 : : {
9663 : : /* The whole case range for P is less than the
9664 : : one for Q. */
9665 : 1100 : e = p;
9666 : 1100 : p = p->right;
9667 : 1100 : psize--;
9668 : : }
9669 : 442 : else if (cmp > 0)
9670 : : {
9671 : : /* The whole case range for Q is greater than
9672 : : the case range for P. */
9673 : 438 : e = q;
9674 : 438 : q = q->right;
9675 : 438 : qsize--;
9676 : : }
9677 : : else
9678 : : {
9679 : : /* The cases overlap, or they are the same
9680 : : element in the list. Either way, we must
9681 : : issue an error and get the next case from P. */
9682 : : /* FIXME: Sort P and Q by line number. */
9683 : 4 : gfc_error ("CASE label at %L overlaps with CASE "
9684 : : "label at %L", &p->where, &q->where);
9685 : 4 : overlap_seen = 1;
9686 : 4 : e = p;
9687 : 4 : p = p->right;
9688 : 4 : psize--;
9689 : : }
9690 : : }
9691 : :
9692 : : /* Add the next element to the merged list. */
9693 : 3415 : if (tail)
9694 : 2427 : tail->right = e;
9695 : : else
9696 : : list = e;
9697 : 3415 : e->left = tail;
9698 : 3415 : tail = e;
9699 : : }
9700 : :
9701 : : /* P has now stepped INSIZE places along, and so has Q. So
9702 : : they're the same. */
9703 : : p = q;
9704 : : }
9705 : 988 : tail->right = NULL;
9706 : :
9707 : : /* If we have done only one merge or none at all, we've
9708 : : finished sorting the cases. */
9709 : 988 : if (nmerges <= 1)
9710 : : {
9711 : 642 : if (!overlap_seen)
9712 : : return list;
9713 : : else
9714 : : return NULL;
9715 : : }
9716 : :
9717 : : /* Otherwise repeat, merging lists twice the size. */
9718 : 346 : insize *= 2;
9719 : 346 : }
9720 : : }
9721 : :
9722 : :
9723 : : /* Check to see if an expression is suitable for use in a CASE statement.
9724 : : Makes sure that all case expressions are scalar constants of the same
9725 : : type. Return false if anything is wrong. */
9726 : :
9727 : : static bool
9728 : 3257 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
9729 : : {
9730 : 3257 : if (e == NULL) return true;
9731 : :
9732 : 3164 : if (e->ts.type != case_expr->ts.type)
9733 : : {
9734 : 4 : gfc_error ("Expression in CASE statement at %L must be of type %s",
9735 : : &e->where, gfc_basic_typename (case_expr->ts.type));
9736 : 4 : return false;
9737 : : }
9738 : :
9739 : : /* C805 (R808) For a given case-construct, each case-value shall be of
9740 : : the same type as case-expr. For character type, length differences
9741 : : are allowed, but the kind type parameters shall be the same. */
9742 : :
9743 : 3160 : if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
9744 : : {
9745 : 4 : gfc_error ("Expression in CASE statement at %L must be of kind %d",
9746 : : &e->where, case_expr->ts.kind);
9747 : 4 : return false;
9748 : : }
9749 : :
9750 : : /* Convert the case value kind to that of case expression kind,
9751 : : if needed */
9752 : :
9753 : 3156 : if (e->ts.kind != case_expr->ts.kind)
9754 : 14 : gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
9755 : :
9756 : 3156 : if (e->rank != 0)
9757 : : {
9758 : 0 : gfc_error ("Expression in CASE statement at %L must be scalar",
9759 : : &e->where);
9760 : 0 : return false;
9761 : : }
9762 : :
9763 : : return true;
9764 : : }
9765 : :
9766 : :
9767 : : /* Given a completely parsed select statement, we:
9768 : :
9769 : : - Validate all expressions and code within the SELECT.
9770 : : - Make sure that the selection expression is not of the wrong type.
9771 : : - Make sure that no case ranges overlap.
9772 : : - Eliminate unreachable cases and unreachable code resulting from
9773 : : removing case labels.
9774 : :
9775 : : The standard does allow unreachable cases, e.g. CASE (5:3). But
9776 : : they are a hassle for code generation, and to prevent that, we just
9777 : : cut them out here. This is not necessary for overlapping cases
9778 : : because they are illegal and we never even try to generate code.
9779 : :
9780 : : We have the additional caveat that a SELECT construct could have
9781 : : been a computed GOTO in the source code. Fortunately we can fairly
9782 : : easily work around that here: The case_expr for a "real" SELECT CASE
9783 : : is in code->expr1, but for a computed GOTO it is in code->expr2. All
9784 : : we have to do is make sure that the case_expr is a scalar integer
9785 : : expression. */
9786 : :
9787 : : static void
9788 : 683 : resolve_select (gfc_code *code, bool select_type)
9789 : : {
9790 : 683 : gfc_code *body;
9791 : 683 : gfc_expr *case_expr;
9792 : 683 : gfc_case *cp, *default_case, *tail, *head;
9793 : 683 : int seen_unreachable;
9794 : 683 : int seen_logical;
9795 : 683 : int ncases;
9796 : 683 : bt type;
9797 : 683 : bool t;
9798 : :
9799 : 683 : if (code->expr1 == NULL)
9800 : : {
9801 : : /* This was actually a computed GOTO statement. */
9802 : 5 : case_expr = code->expr2;
9803 : 5 : if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
9804 : 3 : gfc_error ("Selection expression in computed GOTO statement "
9805 : : "at %L must be a scalar integer expression",
9806 : : &case_expr->where);
9807 : :
9808 : : /* Further checking is not necessary because this SELECT was built
9809 : : by the compiler, so it should always be OK. Just move the
9810 : : case_expr from expr2 to expr so that we can handle computed
9811 : : GOTOs as normal SELECTs from here on. */
9812 : 5 : code->expr1 = code->expr2;
9813 : 5 : code->expr2 = NULL;
9814 : 5 : return;
9815 : : }
9816 : :
9817 : 678 : case_expr = code->expr1;
9818 : 678 : type = case_expr->ts.type;
9819 : :
9820 : : /* F08:C830. */
9821 : 678 : if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
9822 : 6 : && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
9823 : :
9824 : : {
9825 : 0 : gfc_error ("Argument of SELECT statement at %L cannot be %s",
9826 : : &case_expr->where, gfc_typename (case_expr));
9827 : :
9828 : : /* Punt. Going on here just produce more garbage error messages. */
9829 : 0 : return;
9830 : : }
9831 : :
9832 : : /* F08:R842. */
9833 : 678 : if (!select_type && case_expr->rank != 0)
9834 : : {
9835 : 1 : gfc_error ("Argument of SELECT statement at %L must be a scalar "
9836 : : "expression", &case_expr->where);
9837 : :
9838 : : /* Punt. */
9839 : 1 : return;
9840 : : }
9841 : :
9842 : : /* Raise a warning if an INTEGER case value exceeds the range of
9843 : : the case-expr. Later, all expressions will be promoted to the
9844 : : largest kind of all case-labels. */
9845 : :
9846 : 677 : if (type == BT_INTEGER)
9847 : 1897 : for (body = code->block; body; body = body->block)
9848 : 2800 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
9849 : : {
9850 : 1436 : if (cp->low
9851 : 1436 : && gfc_check_integer_range (cp->low->value.integer,
9852 : : case_expr->ts.kind) != ARITH_OK)
9853 : 6 : gfc_warning (0, "Expression in CASE statement at %L is "
9854 : 6 : "not in the range of %s", &cp->low->where,
9855 : : gfc_typename (case_expr));
9856 : :
9857 : 1436 : if (cp->high
9858 : 1153 : && cp->low != cp->high
9859 : 1544 : && gfc_check_integer_range (cp->high->value.integer,
9860 : : case_expr->ts.kind) != ARITH_OK)
9861 : 0 : gfc_warning (0, "Expression in CASE statement at %L is "
9862 : 0 : "not in the range of %s", &cp->high->where,
9863 : : gfc_typename (case_expr));
9864 : : }
9865 : :
9866 : : /* PR 19168 has a long discussion concerning a mismatch of the kinds
9867 : : of the SELECT CASE expression and its CASE values. Walk the lists
9868 : : of case values, and if we find a mismatch, promote case_expr to
9869 : : the appropriate kind. */
9870 : :
9871 : 677 : if (type == BT_LOGICAL || type == BT_INTEGER)
9872 : : {
9873 : 2083 : for (body = code->block; body; body = body->block)
9874 : : {
9875 : : /* Walk the case label list. */
9876 : 3061 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
9877 : : {
9878 : : /* Intercept the DEFAULT case. It does not have a kind. */
9879 : 1571 : if (cp->low == NULL && cp->high == NULL)
9880 : 291 : continue;
9881 : :
9882 : : /* Unreachable case ranges are discarded, so ignore. */
9883 : 1235 : if (cp->low != NULL && cp->high != NULL
9884 : 1187 : && cp->low != cp->high
9885 : 1345 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9886 : 33 : continue;
9887 : :
9888 : 1247 : if (cp->low != NULL
9889 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
9890 : 17 : gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
9891 : :
9892 : 1247 : if (cp->high != NULL
9893 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
9894 : 4 : gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
9895 : : }
9896 : : }
9897 : : }
9898 : :
9899 : : /* Assume there is no DEFAULT case. */
9900 : 677 : default_case = NULL;
9901 : 677 : head = tail = NULL;
9902 : 677 : ncases = 0;
9903 : 677 : seen_logical = 0;
9904 : :
9905 : 2472 : for (body = code->block; body; body = body->block)
9906 : : {
9907 : : /* Assume the CASE list is OK, and all CASE labels can be matched. */
9908 : 1795 : t = true;
9909 : 1795 : seen_unreachable = 0;
9910 : :
9911 : : /* Walk the case label list, making sure that all case labels
9912 : : are legal. */
9913 : 3777 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
9914 : : {
9915 : : /* Count the number of cases in the whole construct. */
9916 : 1993 : ncases++;
9917 : :
9918 : : /* Intercept the DEFAULT case. */
9919 : 1993 : if (cp->low == NULL && cp->high == NULL)
9920 : : {
9921 : 361 : if (default_case != NULL)
9922 : : {
9923 : 0 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
9924 : : "by a second DEFAULT CASE at %L",
9925 : : &default_case->where, &cp->where);
9926 : 0 : t = false;
9927 : 0 : break;
9928 : : }
9929 : : else
9930 : : {
9931 : 361 : default_case = cp;
9932 : 361 : continue;
9933 : : }
9934 : : }
9935 : :
9936 : : /* Deal with single value cases and case ranges. Errors are
9937 : : issued from the validation function. */
9938 : 1632 : if (!validate_case_label_expr (cp->low, case_expr)
9939 : 1632 : || !validate_case_label_expr (cp->high, case_expr))
9940 : : {
9941 : : t = false;
9942 : : break;
9943 : : }
9944 : :
9945 : 1624 : if (type == BT_LOGICAL
9946 : 78 : && ((cp->low == NULL || cp->high == NULL)
9947 : 76 : || cp->low != cp->high))
9948 : : {
9949 : 2 : gfc_error ("Logical range in CASE statement at %L is not "
9950 : : "allowed",
9951 : 1 : cp->low ? &cp->low->where : &cp->high->where);
9952 : 2 : t = false;
9953 : 2 : break;
9954 : : }
9955 : :
9956 : 76 : if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
9957 : : {
9958 : 76 : int value;
9959 : 76 : value = cp->low->value.logical == 0 ? 2 : 1;
9960 : 76 : if (value & seen_logical)
9961 : : {
9962 : 1 : gfc_error ("Constant logical value in CASE statement "
9963 : : "is repeated at %L",
9964 : : &cp->low->where);
9965 : 1 : t = false;
9966 : 1 : break;
9967 : : }
9968 : 75 : seen_logical |= value;
9969 : : }
9970 : :
9971 : 1577 : if (cp->low != NULL && cp->high != NULL
9972 : 1530 : && cp->low != cp->high
9973 : 1733 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
9974 : : {
9975 : 35 : if (warn_surprising)
9976 : 1 : gfc_warning (OPT_Wsurprising,
9977 : : "Range specification at %L can never be matched",
9978 : : &cp->where);
9979 : :
9980 : 35 : cp->unreachable = 1;
9981 : 35 : seen_unreachable = 1;
9982 : : }
9983 : : else
9984 : : {
9985 : : /* If the case range can be matched, it can also overlap with
9986 : : other cases. To make sure it does not, we put it in a
9987 : : double linked list here. We sort that with a merge sort
9988 : : later on to detect any overlapping cases. */
9989 : 1586 : if (!head)
9990 : : {
9991 : 642 : head = tail = cp;
9992 : 642 : head->right = head->left = NULL;
9993 : : }
9994 : : else
9995 : : {
9996 : 944 : tail->right = cp;
9997 : 944 : tail->right->left = tail;
9998 : 944 : tail = tail->right;
9999 : 944 : tail->right = NULL;
10000 : : }
10001 : : }
10002 : : }
10003 : :
10004 : : /* It there was a failure in the previous case label, give up
10005 : : for this case label list. Continue with the next block. */
10006 : 1795 : if (!t)
10007 : 11 : continue;
10008 : :
10009 : : /* See if any case labels that are unreachable have been seen.
10010 : : If so, we eliminate them. This is a bit of a kludge because
10011 : : the case lists for a single case statement (label) is a
10012 : : single forward linked lists. */
10013 : 1784 : if (seen_unreachable)
10014 : : {
10015 : : /* Advance until the first case in the list is reachable. */
10016 : 69 : while (body->ext.block.case_list != NULL
10017 : 69 : && body->ext.block.case_list->unreachable)
10018 : : {
10019 : 34 : gfc_case *n = body->ext.block.case_list;
10020 : 34 : body->ext.block.case_list = body->ext.block.case_list->next;
10021 : 34 : n->next = NULL;
10022 : 34 : gfc_free_case_list (n);
10023 : : }
10024 : :
10025 : : /* Strip all other unreachable cases. */
10026 : 35 : if (body->ext.block.case_list)
10027 : : {
10028 : 2 : for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10029 : : {
10030 : 1 : if (cp->next->unreachable)
10031 : : {
10032 : 1 : gfc_case *n = cp->next;
10033 : 1 : cp->next = cp->next->next;
10034 : 1 : n->next = NULL;
10035 : 1 : gfc_free_case_list (n);
10036 : : }
10037 : : }
10038 : : }
10039 : : }
10040 : : }
10041 : :
10042 : : /* See if there were overlapping cases. If the check returns NULL,
10043 : : there was overlap. In that case we don't do anything. If head
10044 : : is non-NULL, we prepend the DEFAULT case. The sorted list can
10045 : : then used during code generation for SELECT CASE constructs with
10046 : : a case expression of a CHARACTER type. */
10047 : 677 : if (head)
10048 : : {
10049 : 642 : head = check_case_overlap (head);
10050 : :
10051 : : /* Prepend the default_case if it is there. */
10052 : 642 : if (head != NULL && default_case)
10053 : : {
10054 : 344 : default_case->left = NULL;
10055 : 344 : default_case->right = head;
10056 : 344 : head->left = default_case;
10057 : : }
10058 : : }
10059 : :
10060 : : /* Eliminate dead blocks that may be the result if we've seen
10061 : : unreachable case labels for a block. */
10062 : 2438 : for (body = code; body && body->block; body = body->block)
10063 : : {
10064 : 1761 : if (body->block->ext.block.case_list == NULL)
10065 : : {
10066 : : /* Cut the unreachable block from the code chain. */
10067 : 34 : gfc_code *c = body->block;
10068 : 34 : body->block = c->block;
10069 : :
10070 : : /* Kill the dead block, but not the blocks below it. */
10071 : 34 : c->block = NULL;
10072 : 34 : gfc_free_statements (c);
10073 : : }
10074 : : }
10075 : :
10076 : : /* More than two cases is legal but insane for logical selects.
10077 : : Issue a warning for it. */
10078 : 677 : if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10079 : 0 : gfc_warning (OPT_Wsurprising,
10080 : : "Logical SELECT CASE block at %L has more that two cases",
10081 : : &code->loc);
10082 : : }
10083 : :
10084 : :
10085 : : /* Check if a derived type is extensible. */
10086 : :
10087 : : bool
10088 : 22788 : gfc_type_is_extensible (gfc_symbol *sym)
10089 : : {
10090 : 22788 : return !(sym->attr.is_bind_c || sym->attr.sequence
10091 : 22772 : || (sym->attr.is_class
10092 : 2056 : && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10093 : : }
10094 : :
10095 : :
10096 : : static void
10097 : : resolve_types (gfc_namespace *ns);
10098 : :
10099 : : /* Resolve an associate-name: Resolve target and ensure the type-spec is
10100 : : correct as well as possibly the array-spec. */
10101 : :
10102 : : static void
10103 : 12225 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10104 : : {
10105 : 12225 : gfc_expr* target;
10106 : 12225 : bool parentheses = false;
10107 : :
10108 : 12225 : gcc_assert (sym->assoc);
10109 : 12225 : gcc_assert (sym->attr.flavor == FL_VARIABLE);
10110 : :
10111 : : /* If this is for SELECT TYPE, the target may not yet be set. In that
10112 : : case, return. Resolution will be called later manually again when
10113 : : this is done. */
10114 : 12225 : target = sym->assoc->target;
10115 : 12225 : if (!target)
10116 : : return;
10117 : 7238 : gcc_assert (!sym->assoc->dangling);
10118 : :
10119 : 7238 : if (target->expr_type == EXPR_OP
10120 : 198 : && target->value.op.op == INTRINSIC_PARENTHESES
10121 : 42 : && target->value.op.op1->expr_type == EXPR_VARIABLE)
10122 : : {
10123 : 23 : sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10124 : 23 : gfc_free_expr (target);
10125 : 23 : target = sym->assoc->target;
10126 : 23 : parentheses = true;
10127 : : }
10128 : :
10129 : 7238 : if (resolve_target && !gfc_resolve_expr (target))
10130 : : return;
10131 : :
10132 : 7234 : if (sym->assoc->ar)
10133 : : {
10134 : : int dim;
10135 : : gfc_array_ref *ar = sym->assoc->ar;
10136 : 67 : for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10137 : : {
10138 : 39 : if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10139 : 39 : && ar->start[dim]->ts.type == BT_INTEGER)
10140 : 78 : || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10141 : 39 : && ar->end[dim]->ts.type == BT_INTEGER))
10142 : 0 : gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10143 : : "remapping of associate name %s at %L",
10144 : : sym->name, &sym->declared_at);
10145 : : }
10146 : : }
10147 : :
10148 : : /* For variable targets, we get some attributes from the target. */
10149 : 7234 : if (target->expr_type == EXPR_VARIABLE)
10150 : : {
10151 : 6377 : gfc_symbol *tsym, *dsym;
10152 : :
10153 : 6377 : gcc_assert (target->symtree);
10154 : 6377 : tsym = target->symtree->n.sym;
10155 : :
10156 : 6377 : if (gfc_expr_attr (target).proc_pointer)
10157 : : {
10158 : 0 : gfc_error ("Associating entity %qs at %L is a procedure pointer",
10159 : : tsym->name, &target->where);
10160 : 0 : return;
10161 : : }
10162 : :
10163 : 74 : if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10164 : 2 : && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10165 : 6378 : && dsym->attr.flavor == FL_DERIVED)
10166 : : {
10167 : 1 : gfc_error ("Derived type %qs cannot be used as a variable at %L",
10168 : : tsym->name, &target->where);
10169 : 1 : return;
10170 : : }
10171 : :
10172 : 6376 : if (tsym->attr.flavor == FL_PROCEDURE)
10173 : : {
10174 : 73 : bool is_error = true;
10175 : 73 : if (tsym->attr.function && tsym->result == tsym)
10176 : 141 : for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10177 : 137 : if (tsym == ns->proc_name)
10178 : : {
10179 : : is_error = false;
10180 : : break;
10181 : : }
10182 : 64 : if (is_error)
10183 : : {
10184 : 13 : gfc_error ("Associating entity %qs at %L is a procedure name",
10185 : : tsym->name, &target->where);
10186 : 13 : return;
10187 : : }
10188 : : }
10189 : :
10190 : 6363 : sym->attr.asynchronous = tsym->attr.asynchronous;
10191 : 6363 : sym->attr.volatile_ = tsym->attr.volatile_;
10192 : :
10193 : 12726 : sym->attr.target = tsym->attr.target
10194 : 6363 : || gfc_expr_attr (target).pointer;
10195 : 6363 : if (is_subref_array (target))
10196 : 378 : sym->attr.subref_array_pointer = 1;
10197 : : }
10198 : 857 : else if (target->ts.type == BT_PROCEDURE)
10199 : : {
10200 : 0 : gfc_error ("Associating selector-expression at %L yields a procedure",
10201 : : &target->where);
10202 : 0 : return;
10203 : : }
10204 : :
10205 : 7220 : if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10206 : : {
10207 : : /* By now, the type of the target has been fixed up. */
10208 : 287 : symbol_attribute attr;
10209 : :
10210 : 287 : if (sym->ts.type == BT_DERIVED
10211 : 160 : && target->ts.type == BT_CLASS
10212 : 31 : && !UNLIMITED_POLY (target))
10213 : : {
10214 : : /* Inferred to be derived type but the target has type class. */
10215 : 31 : sym->ts = CLASS_DATA (target)->ts;
10216 : 31 : if (!sym->as)
10217 : 31 : sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10218 : 31 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10219 : 31 : sym->attr.dimension = target->rank ? 1 : 0;
10220 : 31 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10221 : : target->corank);
10222 : 31 : sym->as = NULL;
10223 : : }
10224 : 256 : else if (target->ts.type == BT_DERIVED
10225 : 129 : && target->symtree && target->symtree->n.sym
10226 : 105 : && target->symtree->n.sym->ts.type == BT_CLASS
10227 : 0 : && IS_INFERRED_TYPE (target)
10228 : 0 : && target->ref && target->ref->next
10229 : 0 : && target->ref->next->type == REF_ARRAY
10230 : 0 : && !target->ref->next->next)
10231 : : {
10232 : : /* A inferred type selector whose symbol has been determined to be
10233 : : a class array but which only has an array reference. Change the
10234 : : associate name and the selector to class type. */
10235 : 0 : sym->ts = target->ts;
10236 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10237 : 0 : sym->attr.dimension = target->rank ? 1 : 0;
10238 : 0 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10239 : : target->corank);
10240 : 0 : sym->as = NULL;
10241 : 0 : target->ts = sym->ts;
10242 : : }
10243 : 256 : else if ((target->ts.type == BT_DERIVED)
10244 : 127 : || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10245 : 61 : && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10246 : : /* Confirmed to be either a derived type or misidentified to be a
10247 : : scalar class object, when the selector is a class array. */
10248 : 135 : sym->ts = target->ts;
10249 : : }
10250 : :
10251 : :
10252 : 7220 : if (target->expr_type == EXPR_NULL)
10253 : : {
10254 : 1 : gfc_error ("Selector at %L cannot be NULL()", &target->where);
10255 : 1 : return;
10256 : : }
10257 : 7219 : else if (target->ts.type == BT_UNKNOWN)
10258 : : {
10259 : 2 : gfc_error ("Selector at %L has no type", &target->where);
10260 : 2 : return;
10261 : : }
10262 : :
10263 : : /* Get type if this was not already set. Note that it can be
10264 : : some other type than the target in case this is a SELECT TYPE
10265 : : selector! So we must not update when the type is already there. */
10266 : 7217 : if (sym->ts.type == BT_UNKNOWN)
10267 : 220 : sym->ts = target->ts;
10268 : :
10269 : 7217 : gcc_assert (sym->ts.type != BT_UNKNOWN);
10270 : :
10271 : : /* See if this is a valid association-to-variable. */
10272 : 14434 : sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10273 : 6363 : && !parentheses
10274 : 6342 : && !gfc_has_vector_subscript (target))
10275 : 7265 : || gfc_is_ptr_fcn (target));
10276 : :
10277 : : /* Finally resolve if this is an array or not. */
10278 : 7217 : if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10279 : 131 : && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10280 : : {
10281 : 86 : gfc_expression_rank (target);
10282 : 86 : if (target->ts.type == BT_DERIVED
10283 : 39 : && !sym->as
10284 : 39 : && target->symtree->n.sym->as)
10285 : : {
10286 : 0 : sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10287 : 0 : sym->attr.dimension = 1;
10288 : : }
10289 : 86 : else if (target->ts.type == BT_CLASS
10290 : 47 : && CLASS_DATA (target)->as)
10291 : : {
10292 : 0 : target->rank = CLASS_DATA (target)->as->rank;
10293 : 0 : target->corank = CLASS_DATA (target)->as->corank;
10294 : 0 : if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10295 : : {
10296 : 0 : sym->ts = target->ts;
10297 : 0 : sym->attr.dimension = 0;
10298 : : }
10299 : : }
10300 : : }
10301 : :
10302 : :
10303 : 7217 : if (sym->attr.dimension && target->rank == 0)
10304 : : {
10305 : : /* primary.cc makes the assumption that a reference to an associate
10306 : : name followed by a left parenthesis is an array reference. */
10307 : 17 : if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10308 : : {
10309 : 12 : gfc_expression_rank (sym->assoc->target);
10310 : 12 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10311 : 12 : if (!sym->attr.dimension && sym->as)
10312 : 0 : sym->as = NULL;
10313 : : }
10314 : :
10315 : 17 : if (sym->attr.dimension && target->rank == 0)
10316 : : {
10317 : 5 : if (sym->ts.type != BT_CHARACTER)
10318 : 5 : gfc_error ("Associate-name %qs at %L is used as array",
10319 : : sym->name, &sym->declared_at);
10320 : 5 : sym->attr.dimension = 0;
10321 : 5 : return;
10322 : : }
10323 : : }
10324 : :
10325 : : /* We cannot deal with class selectors that need temporaries. */
10326 : 7212 : if (target->ts.type == BT_CLASS
10327 : 7212 : && gfc_ref_needs_temporary_p (target->ref))
10328 : : {
10329 : 1 : gfc_error ("CLASS selector at %L needs a temporary which is not "
10330 : : "yet implemented", &target->where);
10331 : 1 : return;
10332 : : }
10333 : :
10334 : 7211 : if (target->ts.type == BT_CLASS)
10335 : 2676 : gfc_fix_class_refs (target);
10336 : :
10337 : 7211 : if ((target->rank > 0 || target->corank > 0)
10338 : 2623 : && !sym->attr.select_rank_temporary)
10339 : : {
10340 : 2623 : gfc_array_spec *as;
10341 : : /* The rank may be incorrectly guessed at parsing, therefore make sure
10342 : : it is corrected now. */
10343 : 2623 : if (sym->ts.type != BT_CLASS
10344 : 2068 : && (!sym->as || sym->as->corank != target->corank))
10345 : : {
10346 : 135 : if (!sym->as)
10347 : 128 : sym->as = gfc_get_array_spec ();
10348 : 135 : as = sym->as;
10349 : 135 : as->rank = target->rank;
10350 : 135 : as->type = AS_DEFERRED;
10351 : 135 : as->corank = target->corank;
10352 : 135 : sym->attr.dimension = 1;
10353 : 135 : if (as->corank != 0)
10354 : 7 : sym->attr.codimension = 1;
10355 : : }
10356 : 2488 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10357 : 554 : && (!CLASS_DATA (sym)->as
10358 : 554 : || CLASS_DATA (sym)->as->corank != target->corank))
10359 : : {
10360 : 0 : if (!CLASS_DATA (sym)->as)
10361 : 0 : CLASS_DATA (sym)->as = gfc_get_array_spec ();
10362 : 0 : as = CLASS_DATA (sym)->as;
10363 : 0 : as->rank = target->rank;
10364 : 0 : as->type = AS_DEFERRED;
10365 : 0 : as->corank = target->corank;
10366 : 0 : CLASS_DATA (sym)->attr.dimension = 1;
10367 : 0 : if (as->corank != 0)
10368 : 0 : CLASS_DATA (sym)->attr.codimension = 1;
10369 : : }
10370 : : }
10371 : 4588 : else if (!sym->attr.select_rank_temporary)
10372 : : {
10373 : : /* target's rank is 0, but the type of the sym is still array valued,
10374 : : which has to be corrected. */
10375 : 3205 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10376 : 653 : && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10377 : : {
10378 : 24 : gfc_array_spec *as;
10379 : 24 : symbol_attribute attr;
10380 : : /* The associated variable's type is still the array type
10381 : : correct this now. */
10382 : 24 : gfc_typespec *ts = &target->ts;
10383 : 24 : gfc_ref *ref;
10384 : : /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10385 : : */
10386 : 24 : bool internal_ref = true;
10387 : :
10388 : 72 : for (ref = target->ref; ref != NULL; ref = ref->next)
10389 : : {
10390 : 48 : switch (ref->type)
10391 : : {
10392 : 24 : case REF_COMPONENT:
10393 : 24 : ts = &ref->u.c.component->ts;
10394 : 24 : internal_ref
10395 : 24 : = target->ref == ref && ref->next
10396 : 48 : && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10397 : : break;
10398 : 24 : case REF_ARRAY:
10399 : 24 : if (ts->type == BT_CLASS)
10400 : 0 : ts = &ts->u.derived->components->ts;
10401 : 24 : if (internal_ref && ref->u.ar.codimen > 0)
10402 : 0 : for (int i = ref->u.ar.dimen;
10403 : : internal_ref
10404 : 0 : && i < ref->u.ar.dimen + ref->u.ar.codimen;
10405 : : ++i)
10406 : 0 : internal_ref
10407 : 0 : = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10408 : : break;
10409 : : default:
10410 : : break;
10411 : : }
10412 : : }
10413 : : /* Only rewrite the type of this symbol, when the refs are not the
10414 : : internal ones for class and co-array this-image. */
10415 : 24 : if (!internal_ref)
10416 : : {
10417 : : /* Create a scalar instance of the current class type. Because
10418 : : the rank of a class array goes into its name, the type has to
10419 : : be rebuilt. The alternative of (re-)setting just the
10420 : : attributes and as in the current type, destroys the type also
10421 : : in other places. */
10422 : 0 : as = NULL;
10423 : 0 : sym->ts = *ts;
10424 : 0 : sym->ts.type = BT_CLASS;
10425 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10426 : 0 : gfc_change_class (&sym->ts, &attr, as, 0, 0);
10427 : 0 : sym->as = NULL;
10428 : : }
10429 : : }
10430 : : }
10431 : :
10432 : : /* Mark this as an associate variable. */
10433 : 7211 : sym->attr.associate_var = 1;
10434 : :
10435 : : /* Fix up the type-spec for CHARACTER types. */
10436 : 7211 : if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10437 : : {
10438 : 442 : gfc_ref *ref;
10439 : 709 : for (ref = target->ref; ref; ref = ref->next)
10440 : 293 : if (ref->type == REF_SUBSTRING
10441 : 68 : && (ref->u.ss.start == NULL
10442 : 68 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
10443 : 68 : || ref->u.ss.end == NULL
10444 : 48 : || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10445 : : break;
10446 : :
10447 : 442 : if (!sym->ts.u.cl)
10448 : 122 : sym->ts.u.cl = target->ts.u.cl;
10449 : :
10450 : 442 : if (sym->ts.deferred
10451 : 189 : && sym->ts.u.cl == target->ts.u.cl)
10452 : : {
10453 : 110 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10454 : 110 : sym->ts.deferred = 1;
10455 : : }
10456 : :
10457 : 442 : if (!sym->ts.u.cl->length
10458 : 326 : && !sym->ts.deferred
10459 : 137 : && target->expr_type == EXPR_CONSTANT)
10460 : : {
10461 : 30 : sym->ts.u.cl->length =
10462 : 30 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10463 : 30 : target->value.character.length);
10464 : : }
10465 : 412 : else if (((!sym->ts.u.cl->length
10466 : 116 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10467 : 302 : && target->expr_type != EXPR_VARIABLE)
10468 : 290 : || ref)
10469 : : {
10470 : 148 : if (!sym->ts.deferred)
10471 : : {
10472 : 44 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10473 : 44 : sym->ts.deferred = 1;
10474 : : }
10475 : :
10476 : : /* This is reset in trans-stmt.cc after the assignment
10477 : : of the target expression to the associate name. */
10478 : 148 : if (ref && sym->as)
10479 : 26 : sym->attr.pointer = 1;
10480 : : else
10481 : 122 : sym->attr.allocatable = 1;
10482 : : }
10483 : : }
10484 : :
10485 : 7211 : if (sym->ts.type == BT_CLASS
10486 : 1353 : && IS_INFERRED_TYPE (target)
10487 : 13 : && target->ts.type == BT_DERIVED
10488 : 0 : && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10489 : 0 : && target->ref && target->ref->next && !target->ref->next->next
10490 : 0 : && target->ref->next->type == REF_ARRAY)
10491 : 0 : target->ts = target->symtree->n.sym->ts;
10492 : :
10493 : : /* If the target is a good class object, so is the associate variable. */
10494 : 7211 : if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10495 : 666 : sym->attr.class_ok = 1;
10496 : : }
10497 : :
10498 : :
10499 : : /* Ensure that SELECT TYPE expressions have the correct rank and a full
10500 : : array reference, where necessary. The symbols are artificial and so
10501 : : the dimension attribute and arrayspec can also be set. In addition,
10502 : : sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10503 : : This is corrected here as well.*/
10504 : :
10505 : : static void
10506 : 1644 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10507 : : gfc_ref *ref)
10508 : : {
10509 : 1644 : gfc_ref *nref = (*expr1)->ref;
10510 : 1644 : gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10511 : 1644 : gfc_symbol *sym2;
10512 : 1644 : gfc_expr *selector = gfc_copy_expr (expr2);
10513 : :
10514 : 1644 : (*expr1)->rank = rank;
10515 : 1644 : (*expr1)->corank = corank;
10516 : 1644 : if (selector)
10517 : : {
10518 : 309 : gfc_resolve_expr (selector);
10519 : 309 : if (selector->expr_type == EXPR_OP
10520 : 2 : && selector->value.op.op == INTRINSIC_PARENTHESES)
10521 : 2 : sym2 = selector->value.op.op1->symtree->n.sym;
10522 : 307 : else if (selector->expr_type == EXPR_VARIABLE
10523 : 7 : || selector->expr_type == EXPR_FUNCTION)
10524 : 307 : sym2 = selector->symtree->n.sym;
10525 : : else
10526 : 0 : gcc_unreachable ();
10527 : : }
10528 : : else
10529 : : sym2 = NULL;
10530 : :
10531 : 1644 : if (sym1->ts.type == BT_CLASS)
10532 : : {
10533 : 1644 : if ((*expr1)->ts.type != BT_CLASS)
10534 : 13 : (*expr1)->ts = sym1->ts;
10535 : :
10536 : 1644 : CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
10537 : 1644 : CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
10538 : 1644 : if (CLASS_DATA (sym1)->as == NULL && sym2)
10539 : 1 : CLASS_DATA (sym1)->as
10540 : 1 : = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
10541 : : }
10542 : : else
10543 : : {
10544 : 0 : sym1->attr.dimension = rank > 0 ? 1 : 0;
10545 : 0 : sym1->attr.codimension = corank > 0 ? 1 : 0;
10546 : 0 : if (sym1->as == NULL && sym2)
10547 : 0 : sym1->as = gfc_copy_array_spec (sym2->as);
10548 : : }
10549 : :
10550 : 2973 : for (; nref; nref = nref->next)
10551 : 2664 : if (nref->next == NULL)
10552 : : break;
10553 : :
10554 : 1644 : if (ref && nref && nref->type != REF_ARRAY)
10555 : 6 : nref->next = gfc_copy_ref (ref);
10556 : 1638 : else if (ref && !nref)
10557 : 300 : (*expr1)->ref = gfc_copy_ref (ref);
10558 : 1338 : else if (ref && nref->u.ar.codimen != corank)
10559 : : {
10560 : 912 : for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
10561 : 855 : nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
10562 : 57 : nref->u.ar.codimen = corank;
10563 : : }
10564 : 1644 : }
10565 : :
10566 : :
10567 : : static gfc_expr *
10568 : 6440 : build_loc_call (gfc_expr *sym_expr)
10569 : : {
10570 : 6440 : gfc_expr *loc_call;
10571 : 6440 : loc_call = gfc_get_expr ();
10572 : 6440 : loc_call->expr_type = EXPR_FUNCTION;
10573 : 6440 : gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
10574 : 6440 : loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
10575 : 6440 : loc_call->symtree->n.sym->attr.intrinsic = 1;
10576 : 6440 : loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
10577 : 6440 : gfc_commit_symbol (loc_call->symtree->n.sym);
10578 : 6440 : loc_call->ts.type = BT_INTEGER;
10579 : 6440 : loc_call->ts.kind = gfc_index_integer_kind;
10580 : 6440 : loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
10581 : 6440 : loc_call->value.function.actual = gfc_get_actual_arglist ();
10582 : 6440 : loc_call->value.function.actual->expr = sym_expr;
10583 : 6440 : loc_call->where = sym_expr->where;
10584 : 6440 : return loc_call;
10585 : : }
10586 : :
10587 : : /* Resolve a SELECT TYPE statement. */
10588 : :
10589 : : static void
10590 : 2882 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
10591 : : {
10592 : 2882 : gfc_symbol *selector_type;
10593 : 2882 : gfc_code *body, *new_st, *if_st, *tail;
10594 : 2882 : gfc_code *class_is = NULL, *default_case = NULL;
10595 : 2882 : gfc_case *c;
10596 : 2882 : gfc_symtree *st;
10597 : 2882 : char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
10598 : 2882 : gfc_namespace *ns;
10599 : 2882 : int error = 0;
10600 : 2882 : int rank = 0, corank = 0;
10601 : 2882 : gfc_ref* ref = NULL;
10602 : 2882 : gfc_expr *selector_expr = NULL;
10603 : :
10604 : 2882 : ns = code->ext.block.ns;
10605 : 2882 : if (code->expr2)
10606 : : {
10607 : : /* Set this, or coarray checks in resolve will fail. */
10608 : 614 : code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10609 : : }
10610 : 2882 : gfc_resolve (ns);
10611 : :
10612 : : /* Check for F03:C813. */
10613 : 2882 : if (code->expr1->ts.type != BT_CLASS
10614 : 36 : && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10615 : : {
10616 : 13 : gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10617 : : "at %L", &code->loc);
10618 : 41 : return;
10619 : : }
10620 : :
10621 : : /* Prevent segfault, when class type is not initialized due to previous
10622 : : error. */
10623 : 2869 : if (!code->expr1->symtree->n.sym->attr.class_ok
10624 : 2867 : || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10625 : : return;
10626 : :
10627 : 2862 : if (code->expr2)
10628 : : {
10629 : 605 : gfc_ref *ref2 = NULL;
10630 : 1431 : for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10631 : 826 : if (ref->type == REF_COMPONENT
10632 : 425 : && ref->u.c.component->ts.type == BT_CLASS)
10633 : 826 : ref2 = ref;
10634 : :
10635 : 605 : if (ref2)
10636 : : {
10637 : 333 : if (code->expr1->symtree->n.sym->attr.untyped)
10638 : 1 : code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10639 : 333 : selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10640 : : }
10641 : : else
10642 : : {
10643 : 272 : if (code->expr1->symtree->n.sym->attr.untyped)
10644 : 28 : code->expr1->symtree->n.sym->ts = code->expr2->ts;
10645 : : /* Sometimes the selector expression is given the typespec of the
10646 : : '_data' field, which is logical enough but inappropriate here. */
10647 : 272 : if (code->expr2->ts.type == BT_DERIVED
10648 : 80 : && code->expr2->symtree
10649 : 80 : && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10650 : 80 : code->expr2->ts = code->expr2->symtree->n.sym->ts;
10651 : 272 : selector_type = CLASS_DATA (code->expr2)
10652 : : ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10653 : : }
10654 : :
10655 : 605 : if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10656 : : {
10657 : 295 : CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10658 : 295 : CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10659 : 295 : CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10660 : : }
10661 : :
10662 : : /* F2008: C803 The selector expression must not be coindexed. */
10663 : 605 : if (gfc_is_coindexed (code->expr2))
10664 : : {
10665 : 3 : gfc_error ("Selector at %L must not be coindexed",
10666 : 3 : &code->expr2->where);
10667 : 3 : return;
10668 : : }
10669 : :
10670 : : }
10671 : : else
10672 : : {
10673 : 2257 : selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10674 : :
10675 : 2257 : if (gfc_is_coindexed (code->expr1))
10676 : : {
10677 : 0 : gfc_error ("Selector at %L must not be coindexed",
10678 : 0 : &code->expr1->where);
10679 : 0 : return;
10680 : : }
10681 : : }
10682 : :
10683 : : /* Loop over TYPE IS / CLASS IS cases. */
10684 : 8031 : for (body = code->block; body; body = body->block)
10685 : : {
10686 : 5173 : c = body->ext.block.case_list;
10687 : :
10688 : 5173 : if (!error)
10689 : : {
10690 : : /* Check for repeated cases. */
10691 : 8045 : for (tail = code->block; tail; tail = tail->block)
10692 : : {
10693 : 8045 : gfc_case *d = tail->ext.block.case_list;
10694 : 8045 : if (tail == body)
10695 : : break;
10696 : :
10697 : 2881 : if (c->ts.type == d->ts.type
10698 : 513 : && ((c->ts.type == BT_DERIVED
10699 : 415 : && c->ts.u.derived && d->ts.u.derived
10700 : 415 : && !strcmp (c->ts.u.derived->name,
10701 : : d->ts.u.derived->name))
10702 : 512 : || c->ts.type == BT_UNKNOWN
10703 : 512 : || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10704 : 55 : && c->ts.kind == d->ts.kind)))
10705 : : {
10706 : 1 : gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10707 : : &c->where, &d->where);
10708 : 1 : return;
10709 : : }
10710 : : }
10711 : : }
10712 : :
10713 : : /* Check F03:C815. */
10714 : 3267 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10715 : 2215 : && selector_type
10716 : 2215 : && !selector_type->attr.unlimited_polymorphic
10717 : 7092 : && !gfc_type_is_extensible (c->ts.u.derived))
10718 : : {
10719 : 1 : gfc_error ("Derived type %qs at %L must be extensible",
10720 : 1 : c->ts.u.derived->name, &c->where);
10721 : 1 : error++;
10722 : 1 : continue;
10723 : : }
10724 : :
10725 : : /* Check F03:C816. */
10726 : 5177 : if (c->ts.type != BT_UNKNOWN
10727 : 3597 : && selector_type && !selector_type->attr.unlimited_polymorphic
10728 : 7094 : && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
10729 : 1919 : || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
10730 : : {
10731 : 6 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10732 : 2 : gfc_error ("Derived type %qs at %L must be an extension of %qs",
10733 : 2 : c->ts.u.derived->name, &c->where, selector_type->name);
10734 : : else
10735 : 4 : gfc_error ("Unexpected intrinsic type %qs at %L",
10736 : : gfc_basic_typename (c->ts.type), &c->where);
10737 : 6 : error++;
10738 : 6 : continue;
10739 : : }
10740 : :
10741 : : /* Check F03:C814. */
10742 : 5165 : if (c->ts.type == BT_CHARACTER
10743 : 730 : && (c->ts.u.cl->length != NULL || c->ts.deferred))
10744 : : {
10745 : 0 : gfc_error ("The type-spec at %L shall specify that each length "
10746 : : "type parameter is assumed", &c->where);
10747 : 0 : error++;
10748 : 0 : continue;
10749 : : }
10750 : :
10751 : : /* Intercept the DEFAULT case. */
10752 : 5165 : if (c->ts.type == BT_UNKNOWN)
10753 : : {
10754 : : /* Check F03:C818. */
10755 : 1574 : if (default_case)
10756 : : {
10757 : 1 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10758 : : "by a second DEFAULT CASE at %L",
10759 : 1 : &default_case->ext.block.case_list->where, &c->where);
10760 : 1 : error++;
10761 : 1 : continue;
10762 : : }
10763 : :
10764 : : default_case = body;
10765 : : }
10766 : : }
10767 : :
10768 : 2858 : if (error > 0)
10769 : : return;
10770 : :
10771 : : /* Transform SELECT TYPE statement to BLOCK and associate selector to
10772 : : target if present. If there are any EXIT statements referring to the
10773 : : SELECT TYPE construct, this is no problem because the gfc_code
10774 : : reference stays the same and EXIT is equally possible from the BLOCK
10775 : : it is changed to. */
10776 : 2855 : code->op = EXEC_BLOCK;
10777 : 2855 : if (code->expr2)
10778 : : {
10779 : 602 : gfc_association_list* assoc;
10780 : :
10781 : 602 : assoc = gfc_get_association_list ();
10782 : 602 : assoc->st = code->expr1->symtree;
10783 : 602 : assoc->target = gfc_copy_expr (code->expr2);
10784 : 602 : assoc->target->where = code->expr2->where;
10785 : : /* assoc->variable will be set by resolve_assoc_var. */
10786 : :
10787 : 602 : code->ext.block.assoc = assoc;
10788 : 602 : code->expr1->symtree->n.sym->assoc = assoc;
10789 : :
10790 : 602 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
10791 : : }
10792 : : else
10793 : 2253 : code->ext.block.assoc = NULL;
10794 : :
10795 : : /* Ensure that the selector rank and arrayspec are available to
10796 : : correct expressions in which they might be missing. */
10797 : 2855 : if (code->expr2 && (code->expr2->rank || code->expr2->corank))
10798 : : {
10799 : 309 : rank = code->expr2->rank;
10800 : 309 : corank = code->expr2->corank;
10801 : 582 : for (ref = code->expr2->ref; ref; ref = ref->next)
10802 : 573 : if (ref->next == NULL)
10803 : : break;
10804 : 309 : if (ref && ref->type == REF_ARRAY)
10805 : 300 : ref = gfc_copy_ref (ref);
10806 : :
10807 : : /* Fixup expr1 if necessary. */
10808 : 309 : if (rank || corank)
10809 : 309 : fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
10810 : : }
10811 : 2546 : else if (code->expr1->rank || code->expr1->corank)
10812 : : {
10813 : 827 : rank = code->expr1->rank;
10814 : 827 : corank = code->expr1->corank;
10815 : 827 : for (ref = code->expr1->ref; ref; ref = ref->next)
10816 : 827 : if (ref->next == NULL)
10817 : : break;
10818 : 827 : if (ref && ref->type == REF_ARRAY)
10819 : 827 : ref = gfc_copy_ref (ref);
10820 : : }
10821 : :
10822 : : /* Add EXEC_SELECT to switch on type. */
10823 : 2855 : new_st = gfc_get_code (code->op);
10824 : 2855 : new_st->expr1 = code->expr1;
10825 : 2855 : new_st->expr2 = code->expr2;
10826 : 2855 : new_st->block = code->block;
10827 : 2855 : code->expr1 = code->expr2 = NULL;
10828 : 2855 : code->block = NULL;
10829 : 2855 : if (!ns->code)
10830 : 2855 : ns->code = new_st;
10831 : : else
10832 : 0 : ns->code->next = new_st;
10833 : 2855 : code = new_st;
10834 : 2855 : code->op = EXEC_SELECT_TYPE;
10835 : :
10836 : : /* Use the intrinsic LOC function to generate an integer expression
10837 : : for the vtable of the selector. Note that the rank of the selector
10838 : : expression has to be set to zero. */
10839 : 2855 : gfc_add_vptr_component (code->expr1);
10840 : 2855 : code->expr1->rank = 0;
10841 : 2855 : code->expr1->corank = 0;
10842 : 2855 : code->expr1 = build_loc_call (code->expr1);
10843 : 2855 : selector_expr = code->expr1->value.function.actual->expr;
10844 : :
10845 : : /* Loop over TYPE IS / CLASS IS cases. */
10846 : 8012 : for (body = code->block; body; body = body->block)
10847 : : {
10848 : 5157 : gfc_symbol *vtab;
10849 : 5157 : gfc_expr *e;
10850 : 5157 : c = body->ext.block.case_list;
10851 : :
10852 : : /* Generate an index integer expression for address of the
10853 : : TYPE/CLASS vtable and store it in c->low. The hash expression
10854 : : is stored in c->high and is used to resolve intrinsic cases. */
10855 : 5157 : if (c->ts.type != BT_UNKNOWN)
10856 : : {
10857 : 3585 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10858 : : {
10859 : 2206 : vtab = gfc_find_derived_vtab (c->ts.u.derived);
10860 : 2206 : gcc_assert (vtab);
10861 : 2206 : c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
10862 : 2206 : c->ts.u.derived->hash_value);
10863 : : }
10864 : : else
10865 : : {
10866 : 1379 : vtab = gfc_find_vtab (&c->ts);
10867 : 1379 : gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
10868 : 1379 : e = CLASS_DATA (vtab)->initializer;
10869 : 1379 : c->high = gfc_copy_expr (e);
10870 : 1379 : if (c->high->ts.kind != gfc_integer_4_kind)
10871 : : {
10872 : 1 : gfc_typespec ts;
10873 : 1 : ts.kind = gfc_integer_4_kind;
10874 : 1 : ts.type = BT_INTEGER;
10875 : 1 : gfc_convert_type_warn (c->high, &ts, 2, 0);
10876 : : }
10877 : : }
10878 : :
10879 : 3585 : e = gfc_lval_expr_from_sym (vtab);
10880 : 3585 : c->low = build_loc_call (e);
10881 : : }
10882 : : else
10883 : 1572 : continue;
10884 : :
10885 : : /* Associate temporary to selector. This should only be done
10886 : : when this case is actually true, so build a new ASSOCIATE
10887 : : that does precisely this here (instead of using the
10888 : : 'global' one). */
10889 : :
10890 : 3585 : if (c->ts.type == BT_CLASS)
10891 : 308 : sprintf (name, "__tmp_class_%s", c->ts.u.derived->name);
10892 : 3277 : else if (c->ts.type == BT_DERIVED)
10893 : 1898 : sprintf (name, "__tmp_type_%s", c->ts.u.derived->name);
10894 : 1379 : else if (c->ts.type == BT_CHARACTER)
10895 : : {
10896 : 730 : HOST_WIDE_INT charlen = 0;
10897 : 730 : if (c->ts.u.cl && c->ts.u.cl->length
10898 : 0 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
10899 : 0 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
10900 : 730 : snprintf (name, sizeof (name),
10901 : : "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
10902 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
10903 : : }
10904 : : else
10905 : 649 : sprintf (name, "__tmp_%s_%d", gfc_basic_typename (c->ts.type),
10906 : : c->ts.kind);
10907 : :
10908 : 3585 : st = gfc_find_symtree (ns->sym_root, name);
10909 : 3585 : gcc_assert (st->n.sym->assoc);
10910 : 3585 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
10911 : 3585 : st->n.sym->assoc->target->where = selector_expr->where;
10912 : 3585 : if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
10913 : : {
10914 : 3277 : gfc_add_data_component (st->n.sym->assoc->target);
10915 : : /* Fixup the target expression if necessary. */
10916 : 3277 : if (rank || corank)
10917 : 1335 : fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
10918 : : ref);
10919 : : }
10920 : :
10921 : 3585 : new_st = gfc_get_code (EXEC_BLOCK);
10922 : 3585 : new_st->ext.block.ns = gfc_build_block_ns (ns);
10923 : 3585 : new_st->ext.block.ns->code = body->next;
10924 : 3585 : body->next = new_st;
10925 : :
10926 : : /* Chain in the new list only if it is marked as dangling. Otherwise
10927 : : there is a CASE label overlap and this is already used. Just ignore,
10928 : : the error is diagnosed elsewhere. */
10929 : 3585 : if (st->n.sym->assoc->dangling)
10930 : : {
10931 : 3584 : new_st->ext.block.assoc = st->n.sym->assoc;
10932 : 3584 : st->n.sym->assoc->dangling = 0;
10933 : : }
10934 : :
10935 : 3585 : resolve_assoc_var (st->n.sym, false);
10936 : : }
10937 : :
10938 : : /* Take out CLASS IS cases for separate treatment. */
10939 : : body = code;
10940 : 8012 : while (body && body->block)
10941 : : {
10942 : 5157 : if (body->block->ext.block.case_list->ts.type == BT_CLASS)
10943 : : {
10944 : : /* Add to class_is list. */
10945 : 308 : if (class_is == NULL)
10946 : : {
10947 : 277 : class_is = body->block;
10948 : 277 : tail = class_is;
10949 : : }
10950 : : else
10951 : : {
10952 : 43 : for (tail = class_is; tail->block; tail = tail->block) ;
10953 : 31 : tail->block = body->block;
10954 : 31 : tail = tail->block;
10955 : : }
10956 : : /* Remove from EXEC_SELECT list. */
10957 : 308 : body->block = body->block->block;
10958 : 308 : tail->block = NULL;
10959 : : }
10960 : : else
10961 : : body = body->block;
10962 : : }
10963 : :
10964 : 2855 : if (class_is)
10965 : : {
10966 : 277 : gfc_symbol *vtab;
10967 : :
10968 : 277 : if (!default_case)
10969 : : {
10970 : : /* Add a default case to hold the CLASS IS cases. */
10971 : 275 : for (tail = code; tail->block; tail = tail->block) ;
10972 : 167 : tail->block = gfc_get_code (EXEC_SELECT_TYPE);
10973 : 167 : tail = tail->block;
10974 : 167 : tail->ext.block.case_list = gfc_get_case ();
10975 : 167 : tail->ext.block.case_list->ts.type = BT_UNKNOWN;
10976 : 167 : tail->next = NULL;
10977 : 167 : default_case = tail;
10978 : : }
10979 : :
10980 : : /* More than one CLASS IS block? */
10981 : 277 : if (class_is->block)
10982 : : {
10983 : 37 : gfc_code **c1,*c2;
10984 : 37 : bool swapped;
10985 : : /* Sort CLASS IS blocks by extension level. */
10986 : 36 : do
10987 : : {
10988 : 37 : swapped = false;
10989 : 97 : for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
10990 : : {
10991 : 61 : c2 = (*c1)->block;
10992 : : /* F03:C817 (check for doubles). */
10993 : 61 : if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
10994 : 61 : == c2->ext.block.case_list->ts.u.derived->hash_value)
10995 : : {
10996 : 1 : gfc_error ("Double CLASS IS block in SELECT TYPE "
10997 : : "statement at %L",
10998 : : &c2->ext.block.case_list->where);
10999 : 1 : return;
11000 : : }
11001 : 60 : if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11002 : 60 : < c2->ext.block.case_list->ts.u.derived->attr.extension)
11003 : : {
11004 : : /* Swap. */
11005 : 24 : (*c1)->block = c2->block;
11006 : 24 : c2->block = *c1;
11007 : 24 : *c1 = c2;
11008 : 24 : swapped = true;
11009 : : }
11010 : : }
11011 : : }
11012 : : while (swapped);
11013 : : }
11014 : :
11015 : : /* Generate IF chain. */
11016 : 276 : if_st = gfc_get_code (EXEC_IF);
11017 : 276 : new_st = if_st;
11018 : 582 : for (body = class_is; body; body = body->block)
11019 : : {
11020 : 306 : new_st->block = gfc_get_code (EXEC_IF);
11021 : 306 : new_st = new_st->block;
11022 : : /* Set up IF condition: Call _gfortran_is_extension_of. */
11023 : 306 : new_st->expr1 = gfc_get_expr ();
11024 : 306 : new_st->expr1->expr_type = EXPR_FUNCTION;
11025 : 306 : new_st->expr1->ts.type = BT_LOGICAL;
11026 : 306 : new_st->expr1->ts.kind = 4;
11027 : 306 : new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11028 : 306 : new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11029 : 306 : new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11030 : : /* Set up arguments. */
11031 : 306 : new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11032 : 306 : new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11033 : 306 : new_st->expr1->value.function.actual->expr->where = code->loc;
11034 : 306 : new_st->expr1->where = code->loc;
11035 : 306 : gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11036 : 306 : vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11037 : 306 : st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11038 : 306 : new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11039 : 306 : new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11040 : 306 : new_st->expr1->value.function.actual->next->expr->where = code->loc;
11041 : : /* Set up types in formal arg list. */
11042 : 306 : new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11043 : 306 : new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11044 : 306 : new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11045 : 306 : new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11046 : :
11047 : 306 : new_st->next = body->next;
11048 : : }
11049 : 276 : if (default_case->next)
11050 : : {
11051 : 110 : new_st->block = gfc_get_code (EXEC_IF);
11052 : 110 : new_st = new_st->block;
11053 : 110 : new_st->next = default_case->next;
11054 : : }
11055 : :
11056 : : /* Replace CLASS DEFAULT code by the IF chain. */
11057 : 276 : default_case->next = if_st;
11058 : : }
11059 : :
11060 : : /* Resolve the internal code. This cannot be done earlier because
11061 : : it requires that the sym->assoc of selectors is set already. */
11062 : 2854 : gfc_current_ns = ns;
11063 : 2854 : gfc_resolve_blocks (code->block, gfc_current_ns);
11064 : 2854 : gfc_current_ns = old_ns;
11065 : :
11066 : 2854 : free (ref);
11067 : : }
11068 : :
11069 : :
11070 : : /* Resolve a SELECT RANK statement. */
11071 : :
11072 : : static void
11073 : 1018 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11074 : : {
11075 : 1018 : gfc_namespace *ns;
11076 : 1018 : gfc_code *body, *new_st, *tail;
11077 : 1018 : gfc_case *c;
11078 : 1018 : char tname[GFC_MAX_SYMBOL_LEN + 7];
11079 : 1018 : char name[2 * GFC_MAX_SYMBOL_LEN];
11080 : 1018 : gfc_symtree *st;
11081 : 1018 : gfc_expr *selector_expr = NULL;
11082 : 1018 : int case_value;
11083 : 1018 : HOST_WIDE_INT charlen = 0;
11084 : :
11085 : 1018 : ns = code->ext.block.ns;
11086 : 1018 : gfc_resolve (ns);
11087 : :
11088 : 1018 : code->op = EXEC_BLOCK;
11089 : 1018 : if (code->expr2)
11090 : : {
11091 : 42 : gfc_association_list* assoc;
11092 : :
11093 : 42 : assoc = gfc_get_association_list ();
11094 : 42 : assoc->st = code->expr1->symtree;
11095 : 42 : assoc->target = gfc_copy_expr (code->expr2);
11096 : 42 : assoc->target->where = code->expr2->where;
11097 : : /* assoc->variable will be set by resolve_assoc_var. */
11098 : :
11099 : 42 : code->ext.block.assoc = assoc;
11100 : 42 : code->expr1->symtree->n.sym->assoc = assoc;
11101 : :
11102 : 42 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11103 : : }
11104 : : else
11105 : 976 : code->ext.block.assoc = NULL;
11106 : :
11107 : : /* Loop over RANK cases. Note that returning on the errors causes a
11108 : : cascade of further errors because the case blocks do not compile
11109 : : correctly. */
11110 : 3320 : for (body = code->block; body; body = body->block)
11111 : : {
11112 : 2302 : c = body->ext.block.case_list;
11113 : 2302 : if (c->low)
11114 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11115 : : else
11116 : : case_value = -2;
11117 : :
11118 : : /* Check for repeated cases. */
11119 : 5836 : for (tail = code->block; tail; tail = tail->block)
11120 : : {
11121 : 5836 : gfc_case *d = tail->ext.block.case_list;
11122 : 5836 : int case_value2;
11123 : :
11124 : 5836 : if (tail == body)
11125 : : break;
11126 : :
11127 : : /* Check F2018: C1153. */
11128 : 3534 : if (!c->low && !d->low)
11129 : 1 : gfc_error ("RANK DEFAULT at %L is repeated at %L",
11130 : : &c->where, &d->where);
11131 : :
11132 : 3534 : if (!c->low || !d->low)
11133 : 1253 : continue;
11134 : :
11135 : : /* Check F2018: C1153. */
11136 : 2281 : case_value2 = (int) mpz_get_si (d->low->value.integer);
11137 : 2281 : if ((case_value == case_value2) && case_value == -1)
11138 : 1 : gfc_error ("RANK (*) at %L is repeated at %L",
11139 : : &c->where, &d->where);
11140 : 2280 : else if (case_value == case_value2)
11141 : 1 : gfc_error ("RANK (%i) at %L is repeated at %L",
11142 : : case_value, &c->where, &d->where);
11143 : : }
11144 : :
11145 : 2302 : if (!c->low)
11146 : 919 : continue;
11147 : :
11148 : : /* Check F2018: C1155. */
11149 : 1383 : if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11150 : 1381 : || gfc_expr_attr (code->expr1).pointer))
11151 : 3 : gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11152 : 3 : "allocatable selector at %L", &c->where, &code->expr1->where);
11153 : : }
11154 : :
11155 : : /* Add EXEC_SELECT to switch on rank. */
11156 : 1018 : new_st = gfc_get_code (code->op);
11157 : 1018 : new_st->expr1 = code->expr1;
11158 : 1018 : new_st->expr2 = code->expr2;
11159 : 1018 : new_st->block = code->block;
11160 : 1018 : code->expr1 = code->expr2 = NULL;
11161 : 1018 : code->block = NULL;
11162 : 1018 : if (!ns->code)
11163 : 1018 : ns->code = new_st;
11164 : : else
11165 : 0 : ns->code->next = new_st;
11166 : 1018 : code = new_st;
11167 : 1018 : code->op = EXEC_SELECT_RANK;
11168 : :
11169 : 1018 : selector_expr = code->expr1;
11170 : :
11171 : : /* Loop over SELECT RANK cases. */
11172 : 3320 : for (body = code->block; body; body = body->block)
11173 : : {
11174 : 2302 : c = body->ext.block.case_list;
11175 : 2302 : int case_value;
11176 : :
11177 : : /* Pass on the default case. */
11178 : 2302 : if (c->low == NULL)
11179 : 919 : continue;
11180 : :
11181 : : /* Associate temporary to selector. This should only be done
11182 : : when this case is actually true, so build a new ASSOCIATE
11183 : : that does precisely this here (instead of using the
11184 : : 'global' one). */
11185 : 1383 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11186 : 265 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11187 : 186 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11188 : :
11189 : 1383 : if (c->ts.type == BT_CLASS)
11190 : 145 : sprintf (tname, "class_%s", c->ts.u.derived->name);
11191 : 1238 : else if (c->ts.type == BT_DERIVED)
11192 : 110 : sprintf (tname, "type_%s", c->ts.u.derived->name);
11193 : 1128 : else if (c->ts.type != BT_CHARACTER)
11194 : 569 : sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11195 : : else
11196 : 559 : sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11197 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11198 : :
11199 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11200 : 1383 : if (case_value >= 0)
11201 : 1350 : sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11202 : : else
11203 : 33 : sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11204 : :
11205 : 1383 : st = gfc_find_symtree (ns->sym_root, name);
11206 : 1383 : gcc_assert (st->n.sym->assoc);
11207 : :
11208 : 1383 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11209 : 1383 : st->n.sym->assoc->target->where = selector_expr->where;
11210 : :
11211 : 1383 : new_st = gfc_get_code (EXEC_BLOCK);
11212 : 1383 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11213 : 1383 : new_st->ext.block.ns->code = body->next;
11214 : 1383 : body->next = new_st;
11215 : :
11216 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11217 : : there is a CASE label overlap and this is already used. Just ignore,
11218 : : the error is diagnosed elsewhere. */
11219 : 1383 : if (st->n.sym->assoc->dangling)
11220 : : {
11221 : 1381 : new_st->ext.block.assoc = st->n.sym->assoc;
11222 : 1381 : st->n.sym->assoc->dangling = 0;
11223 : : }
11224 : :
11225 : 1383 : resolve_assoc_var (st->n.sym, false);
11226 : : }
11227 : :
11228 : 1018 : gfc_current_ns = ns;
11229 : 1018 : gfc_resolve_blocks (code->block, gfc_current_ns);
11230 : 1018 : gfc_current_ns = old_ns;
11231 : 1018 : }
11232 : :
11233 : :
11234 : : /* Resolve a transfer statement. This is making sure that:
11235 : : -- a derived type being transferred has only non-pointer components
11236 : : -- a derived type being transferred doesn't have private components, unless
11237 : : it's being transferred from the module where the type was defined
11238 : : -- we're not trying to transfer a whole assumed size array. */
11239 : :
11240 : : static void
11241 : 45093 : resolve_transfer (gfc_code *code)
11242 : : {
11243 : 45093 : gfc_symbol *sym, *derived;
11244 : 45093 : gfc_ref *ref;
11245 : 45093 : gfc_expr *exp;
11246 : 45093 : bool write = false;
11247 : 45093 : bool formatted = false;
11248 : 45093 : gfc_dt *dt = code->ext.dt;
11249 : 45093 : gfc_symbol *dtio_sub = NULL;
11250 : :
11251 : 45093 : exp = code->expr1;
11252 : :
11253 : 90192 : while (exp != NULL && exp->expr_type == EXPR_OP
11254 : 45979 : && exp->value.op.op == INTRINSIC_PARENTHESES)
11255 : 6 : exp = exp->value.op.op1;
11256 : :
11257 : 45093 : if (exp && exp->expr_type == EXPR_NULL
11258 : 2 : && code->ext.dt)
11259 : : {
11260 : 2 : gfc_error ("Invalid context for NULL () intrinsic at %L",
11261 : : &exp->where);
11262 : 2 : return;
11263 : : }
11264 : :
11265 : : if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11266 : : && exp->expr_type != EXPR_FUNCTION
11267 : : && exp->expr_type != EXPR_ARRAY
11268 : : && exp->expr_type != EXPR_STRUCTURE))
11269 : : return;
11270 : :
11271 : : /* If we are reading, the variable will be changed. Note that
11272 : : code->ext.dt may be NULL if the TRANSFER is related to
11273 : : an INQUIRE statement -- but in this case, we are not reading, either. */
11274 : 24499 : if (dt && dt->dt_io_kind->value.iokind == M_READ
11275 : 31865 : && !gfc_check_vardef_context (exp, false, false, false,
11276 : 7218 : _("item in READ")))
11277 : : return;
11278 : :
11279 : 24643 : const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11280 : 24643 : || exp->expr_type == EXPR_FUNCTION
11281 : 20302 : || exp->expr_type == EXPR_ARRAY
11282 : 44945 : ? &exp->ts : &exp->symtree->n.sym->ts;
11283 : :
11284 : : /* Go to actual component transferred. */
11285 : 31862 : for (ref = exp->ref; ref; ref = ref->next)
11286 : 7219 : if (ref->type == REF_COMPONENT)
11287 : 2124 : ts = &ref->u.c.component->ts;
11288 : :
11289 : 24643 : if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11290 : 24495 : && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11291 : : {
11292 : 686 : derived = ts->u.derived;
11293 : :
11294 : : /* Determine when to use the formatted DTIO procedure. */
11295 : 686 : if (dt && (dt->format_expr || dt->format_label))
11296 : 611 : formatted = true;
11297 : :
11298 : 686 : write = dt->dt_io_kind->value.iokind == M_WRITE
11299 : 686 : || dt->dt_io_kind->value.iokind == M_PRINT;
11300 : 686 : dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11301 : :
11302 : 686 : if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11303 : : {
11304 : 425 : dt->udtio = exp;
11305 : 425 : sym = exp->symtree->n.sym->ns->proc_name;
11306 : : /* Check to see if this is a nested DTIO call, with the
11307 : : dummy as the io-list object. */
11308 : 425 : if (sym && sym == dtio_sub && sym->formal
11309 : 30 : && sym->formal->sym == exp->symtree->n.sym
11310 : 30 : && exp->ref == NULL)
11311 : : {
11312 : 0 : if (!sym->attr.recursive)
11313 : : {
11314 : 0 : gfc_error ("DTIO %s procedure at %L must be recursive",
11315 : : sym->name, &sym->declared_at);
11316 : 0 : return;
11317 : : }
11318 : : }
11319 : : }
11320 : : }
11321 : :
11322 : 24643 : if (ts->type == BT_CLASS && dtio_sub == NULL)
11323 : : {
11324 : 3 : gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11325 : : "it is processed by a defined input/output procedure",
11326 : : &code->loc);
11327 : 3 : return;
11328 : : }
11329 : :
11330 : 24640 : if (ts->type == BT_DERIVED)
11331 : : {
11332 : : /* Check that transferred derived type doesn't contain POINTER
11333 : : components unless it is processed by a defined input/output
11334 : : procedure". */
11335 : 654 : if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11336 : : {
11337 : 2 : gfc_error ("Data transfer element at %L cannot have POINTER "
11338 : : "components unless it is processed by a defined "
11339 : : "input/output procedure", &code->loc);
11340 : 2 : return;
11341 : : }
11342 : :
11343 : : /* F08:C935. */
11344 : 652 : if (ts->u.derived->attr.proc_pointer_comp)
11345 : : {
11346 : 2 : gfc_error ("Data transfer element at %L cannot have "
11347 : : "procedure pointer components", &code->loc);
11348 : 2 : return;
11349 : : }
11350 : :
11351 : 650 : if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11352 : : {
11353 : 6 : gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11354 : : "components unless it is processed by a defined "
11355 : : "input/output procedure", &code->loc);
11356 : 6 : return;
11357 : : }
11358 : :
11359 : : /* C_PTR and C_FUNPTR have private components which means they cannot
11360 : : be printed. However, if -std=gnu and not -pedantic, allow
11361 : : the component to be printed to help debugging. */
11362 : 644 : if (ts->u.derived->ts.f90_type == BT_VOID)
11363 : : {
11364 : 4 : gfc_error ("Data transfer element at %L "
11365 : : "cannot have PRIVATE components", &code->loc);
11366 : 4 : return;
11367 : : }
11368 : 640 : else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11369 : : {
11370 : 4 : gfc_error ("Data transfer element at %L cannot have "
11371 : : "PRIVATE components unless it is processed by "
11372 : : "a defined input/output procedure", &code->loc);
11373 : 4 : return;
11374 : : }
11375 : : }
11376 : :
11377 : 24622 : if (exp->expr_type == EXPR_STRUCTURE)
11378 : : return;
11379 : :
11380 : 24577 : if (exp->expr_type == EXPR_ARRAY)
11381 : : return;
11382 : :
11383 : 24201 : sym = exp->symtree->n.sym;
11384 : :
11385 : 24201 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11386 : 75 : && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11387 : : {
11388 : 1 : gfc_error ("Data transfer element at %L cannot be a full reference to "
11389 : : "an assumed-size array", &code->loc);
11390 : 1 : return;
11391 : : }
11392 : : }
11393 : :
11394 : :
11395 : : /*********** Toplevel code resolution subroutines ***********/
11396 : :
11397 : : /* Find the set of labels that are reachable from this block. We also
11398 : : record the last statement in each block. */
11399 : :
11400 : : static void
11401 : 654469 : find_reachable_labels (gfc_code *block)
11402 : : {
11403 : 654469 : gfc_code *c;
11404 : :
11405 : 654469 : if (!block)
11406 : : return;
11407 : :
11408 : 412658 : cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11409 : :
11410 : : /* Collect labels in this block. We don't keep those corresponding
11411 : : to END {IF|SELECT}, these are checked in resolve_branch by going
11412 : : up through the code_stack. */
11413 : 1514383 : for (c = block; c; c = c->next)
11414 : : {
11415 : 1101725 : if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11416 : 3589 : bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11417 : : }
11418 : :
11419 : : /* Merge with labels from parent block. */
11420 : 412658 : if (cs_base->prev)
11421 : : {
11422 : 339314 : gcc_assert (cs_base->prev->reachable_labels);
11423 : 339314 : bitmap_ior_into (cs_base->reachable_labels,
11424 : : cs_base->prev->reachable_labels);
11425 : : }
11426 : : }
11427 : :
11428 : : static void
11429 : 136 : resolve_lock_unlock_event (gfc_code *code)
11430 : : {
11431 : 136 : if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11432 : 136 : && (code->expr1->ts.type != BT_DERIVED
11433 : 95 : || code->expr1->expr_type != EXPR_VARIABLE
11434 : 95 : || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11435 : 94 : || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11436 : 94 : || code->expr1->rank != 0
11437 : 124 : || (!gfc_is_coarray (code->expr1) &&
11438 : 31 : !gfc_is_coindexed (code->expr1))))
11439 : 4 : gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11440 : 4 : &code->expr1->where);
11441 : 132 : else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11442 : 39 : && (code->expr1->ts.type != BT_DERIVED
11443 : 39 : || code->expr1->expr_type != EXPR_VARIABLE
11444 : 39 : || code->expr1->ts.u.derived->from_intmod
11445 : : != INTMOD_ISO_FORTRAN_ENV
11446 : 39 : || code->expr1->ts.u.derived->intmod_sym_id
11447 : : != ISOFORTRAN_EVENT_TYPE
11448 : 39 : || code->expr1->rank != 0))
11449 : 0 : gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11450 : : &code->expr1->where);
11451 : 23 : else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11452 : 143 : && !gfc_is_coindexed (code->expr1))
11453 : 0 : gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11454 : 0 : &code->expr1->where);
11455 : 132 : else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11456 : 0 : gfc_error ("Event variable argument at %L must be a coarray but not "
11457 : 0 : "coindexed", &code->expr1->where);
11458 : :
11459 : : /* Check STAT. */
11460 : 136 : if (code->expr2
11461 : 38 : && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11462 : 38 : || code->expr2->expr_type != EXPR_VARIABLE))
11463 : 0 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11464 : : &code->expr2->where);
11465 : :
11466 : 136 : if (code->expr2
11467 : 174 : && !gfc_check_vardef_context (code->expr2, false, false, false,
11468 : 38 : _("STAT variable")))
11469 : : return;
11470 : :
11471 : : /* Check ERRMSG. */
11472 : 136 : if (code->expr3
11473 : 2 : && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11474 : 2 : || code->expr3->expr_type != EXPR_VARIABLE))
11475 : 0 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11476 : : &code->expr3->where);
11477 : :
11478 : 136 : if (code->expr3
11479 : 138 : && !gfc_check_vardef_context (code->expr3, false, false, false,
11480 : 2 : _("ERRMSG variable")))
11481 : : return;
11482 : :
11483 : : /* Check for LOCK the ACQUIRED_LOCK. */
11484 : 136 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11485 : 16 : && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11486 : 16 : || code->expr4->expr_type != EXPR_VARIABLE))
11487 : 0 : gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11488 : : "variable", &code->expr4->where);
11489 : :
11490 : 120 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11491 : 152 : && !gfc_check_vardef_context (code->expr4, false, false, false,
11492 : 16 : _("ACQUIRED_LOCK variable")))
11493 : : return;
11494 : :
11495 : : /* Check for EVENT WAIT the UNTIL_COUNT. */
11496 : 136 : if (code->op == EXEC_EVENT_WAIT && code->expr4)
11497 : : {
11498 : 24 : if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11499 : 24 : || code->expr4->rank != 0)
11500 : 0 : gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11501 : 0 : "expression", &code->expr4->where);
11502 : : }
11503 : : }
11504 : :
11505 : : static void
11506 : 195 : resolve_team_argument (gfc_expr *team)
11507 : : {
11508 : 195 : gfc_resolve_expr (team);
11509 : 195 : if (team->rank != 0 || team->ts.type != BT_DERIVED
11510 : 188 : || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11511 : 188 : || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
11512 : : {
11513 : 7 : gfc_error ("TEAM argument at %L must be a scalar expression "
11514 : : "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
11515 : : &team->where);
11516 : : }
11517 : 195 : }
11518 : :
11519 : : static void
11520 : 1180 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
11521 : : gfc_expr *e)
11522 : : {
11523 : 1180 : gfc_resolve_expr (e);
11524 : 1180 : if (e
11525 : 139 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
11526 : 124 : || e->expr_type != EXPR_VARIABLE))
11527 : 15 : gfc_error ("%s argument at %L must be a scalar %s variable of at least "
11528 : : "kind %d", name, &e->where, gfc_basic_typename (exp_type),
11529 : : exp_kind);
11530 : 1180 : }
11531 : :
11532 : : void
11533 : 590 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
11534 : : {
11535 : 590 : resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
11536 : 590 : resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
11537 : : gfc_default_character_kind,
11538 : : sync_stat->errmsg);
11539 : 590 : }
11540 : :
11541 : : static void
11542 : 214 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
11543 : : gfc_expr *e)
11544 : : {
11545 : 214 : gfc_resolve_expr (e);
11546 : 214 : if (e
11547 : 140 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
11548 : 3 : gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
11549 : : name, &e->where, gfc_basic_typename (exp_type), exp_kind);
11550 : 214 : }
11551 : :
11552 : : static void
11553 : 107 : resolve_form_team (gfc_code *code)
11554 : : {
11555 : 107 : resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
11556 : : code->expr1);
11557 : 107 : resolve_team_argument (code->expr2);
11558 : 107 : resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
11559 : : code->expr3);
11560 : 107 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11561 : 107 : }
11562 : :
11563 : : static void resolve_block_construct (gfc_code *);
11564 : :
11565 : : static void
11566 : 66 : resolve_change_team (gfc_code *code)
11567 : : {
11568 : 66 : resolve_team_argument (code->expr1);
11569 : 66 : gfc_resolve_sync_stat (&code->ext.block.sync_stat);
11570 : 132 : resolve_block_construct (code);
11571 : : /* Map the coarray bounds as selected. */
11572 : 68 : for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
11573 : 2 : if (a->ar)
11574 : : {
11575 : 2 : gfc_array_spec *src = a->ar->as, *dst;
11576 : 2 : if (a->st->n.sym->ts.type == BT_CLASS)
11577 : 0 : dst = CLASS_DATA (a->st->n.sym)->as;
11578 : : else
11579 : 2 : dst = a->st->n.sym->as;
11580 : 2 : dst->corank = src->corank;
11581 : 2 : dst->cotype = src->cotype;
11582 : 4 : for (int i = 0; i < src->corank; ++i)
11583 : : {
11584 : 2 : dst->lower[dst->rank + i] = src->lower[i];
11585 : 2 : dst->upper[dst->rank + i] = src->upper[i];
11586 : 2 : src->lower[i] = src->upper[i] = nullptr;
11587 : : }
11588 : 2 : gfc_free_array_spec (src);
11589 : 2 : free (a->ar);
11590 : 2 : a->ar = nullptr;
11591 : 2 : dst->resolved = false;
11592 : 2 : gfc_resolve_array_spec (dst, 0);
11593 : : }
11594 : 66 : }
11595 : :
11596 : : static void
11597 : 22 : resolve_sync_team (gfc_code *code)
11598 : : {
11599 : 22 : resolve_team_argument (code->expr1);
11600 : 22 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11601 : 22 : }
11602 : :
11603 : : static void
11604 : 64 : resolve_end_team (gfc_code *code)
11605 : : {
11606 : 64 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11607 : 64 : }
11608 : :
11609 : : static void
11610 : 50 : resolve_critical (gfc_code *code)
11611 : : {
11612 : 50 : gfc_symtree *symtree;
11613 : 50 : gfc_symbol *lock_type;
11614 : 50 : char name[GFC_MAX_SYMBOL_LEN];
11615 : 50 : static int serial = 0;
11616 : :
11617 : 50 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11618 : :
11619 : 50 : if (flag_coarray != GFC_FCOARRAY_LIB)
11620 : 30 : return;
11621 : :
11622 : 20 : symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11623 : : GFC_PREFIX ("lock_type"));
11624 : 20 : if (symtree)
11625 : 10 : lock_type = symtree->n.sym;
11626 : : else
11627 : : {
11628 : 10 : if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
11629 : : false) != 0)
11630 : 0 : gcc_unreachable ();
11631 : 10 : lock_type = symtree->n.sym;
11632 : 10 : lock_type->attr.flavor = FL_DERIVED;
11633 : 10 : lock_type->attr.zero_comp = 1;
11634 : 10 : lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
11635 : 10 : lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
11636 : : }
11637 : :
11638 : 20 : sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
11639 : 20 : if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
11640 : 0 : gcc_unreachable ();
11641 : :
11642 : 20 : code->resolved_sym = symtree->n.sym;
11643 : 20 : symtree->n.sym->attr.flavor = FL_VARIABLE;
11644 : 20 : symtree->n.sym->attr.referenced = 1;
11645 : 20 : symtree->n.sym->attr.artificial = 1;
11646 : 20 : symtree->n.sym->attr.codimension = 1;
11647 : 20 : symtree->n.sym->ts.type = BT_DERIVED;
11648 : 20 : symtree->n.sym->ts.u.derived = lock_type;
11649 : 20 : symtree->n.sym->as = gfc_get_array_spec ();
11650 : 20 : symtree->n.sym->as->corank = 1;
11651 : 20 : symtree->n.sym->as->type = AS_EXPLICIT;
11652 : 20 : symtree->n.sym->as->cotype = AS_EXPLICIT;
11653 : 20 : symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
11654 : : NULL, 1);
11655 : 20 : gfc_commit_symbols();
11656 : : }
11657 : :
11658 : :
11659 : : static void
11660 : 747 : resolve_sync (gfc_code *code)
11661 : : {
11662 : : /* Check imageset. The * case matches expr1 == NULL. */
11663 : 747 : if (code->expr1)
11664 : : {
11665 : 48 : if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
11666 : 1 : gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
11667 : : "INTEGER expression", &code->expr1->where);
11668 : 48 : if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
11669 : 23 : && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
11670 : 1 : gfc_error ("Imageset argument at %L must between 1 and num_images()",
11671 : : &code->expr1->where);
11672 : 47 : else if (code->expr1->expr_type == EXPR_ARRAY
11673 : 47 : && gfc_simplify_expr (code->expr1, 0))
11674 : : {
11675 : 18 : gfc_constructor *cons;
11676 : 18 : cons = gfc_constructor_first (code->expr1->value.constructor);
11677 : 54 : for (; cons; cons = gfc_constructor_next (cons))
11678 : 18 : if (cons->expr->expr_type == EXPR_CONSTANT
11679 : 18 : && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
11680 : 0 : gfc_error ("Imageset argument at %L must between 1 and "
11681 : : "num_images()", &cons->expr->where);
11682 : : }
11683 : : }
11684 : :
11685 : : /* Check STAT. */
11686 : 747 : gfc_resolve_expr (code->expr2);
11687 : 747 : if (code->expr2)
11688 : : {
11689 : 84 : if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
11690 : 1 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11691 : : &code->expr2->where);
11692 : : else
11693 : 83 : gfc_check_vardef_context (code->expr2, false, false, false,
11694 : 83 : _("STAT variable"));
11695 : : }
11696 : :
11697 : : /* Check ERRMSG. */
11698 : 747 : gfc_resolve_expr (code->expr3);
11699 : 747 : if (code->expr3)
11700 : : {
11701 : 75 : if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
11702 : 4 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11703 : : &code->expr3->where);
11704 : : else
11705 : 71 : gfc_check_vardef_context (code->expr3, false, false, false,
11706 : 71 : _("ERRMSG variable"));
11707 : : }
11708 : 747 : }
11709 : :
11710 : :
11711 : : /* Given a branch to a label, see if the branch is conforming.
11712 : : The code node describes where the branch is located. */
11713 : :
11714 : : static void
11715 : 105326 : resolve_branch (gfc_st_label *label, gfc_code *code)
11716 : : {
11717 : 105326 : code_stack *stack;
11718 : :
11719 : 105326 : if (label == NULL)
11720 : : return;
11721 : :
11722 : : /* Step one: is this a valid branching target? */
11723 : :
11724 : 2459 : if (label->defined == ST_LABEL_UNKNOWN)
11725 : : {
11726 : 4 : gfc_error ("Label %d referenced at %L is never defined", label->value,
11727 : : &code->loc);
11728 : 4 : return;
11729 : : }
11730 : :
11731 : 2455 : if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
11732 : : {
11733 : 4 : gfc_error ("Statement at %L is not a valid branch target statement "
11734 : : "for the branch statement at %L", &label->where, &code->loc);
11735 : 4 : return;
11736 : : }
11737 : :
11738 : : /* Step two: make sure this branch is not a branch to itself ;-) */
11739 : :
11740 : 2451 : if (code->here == label)
11741 : : {
11742 : 0 : gfc_warning (0, "Branch at %L may result in an infinite loop",
11743 : : &code->loc);
11744 : 0 : return;
11745 : : }
11746 : :
11747 : : /* Step three: See if the label is in the same block as the
11748 : : branching statement. The hard work has been done by setting up
11749 : : the bitmap reachable_labels. */
11750 : :
11751 : 2451 : if (bitmap_bit_p (cs_base->reachable_labels, label->value))
11752 : : {
11753 : : /* Check now whether there is a CRITICAL construct; if so, check
11754 : : whether the label is still visible outside of the CRITICAL block,
11755 : : which is invalid. */
11756 : 6264 : for (stack = cs_base; stack; stack = stack->prev)
11757 : : {
11758 : 3881 : if (stack->current->op == EXEC_CRITICAL
11759 : 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
11760 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
11761 : : "label at %L", &code->loc, &label->where);
11762 : 3879 : else if (stack->current->op == EXEC_DO_CONCURRENT
11763 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
11764 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
11765 : : "for label at %L", &code->loc, &label->where);
11766 : 3879 : else if (stack->current->op == EXEC_CHANGE_TEAM
11767 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
11768 : 1 : gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
11769 : : "for label at %L", &code->loc, &label->where);
11770 : : }
11771 : :
11772 : : return;
11773 : : }
11774 : :
11775 : : /* Step four: If we haven't found the label in the bitmap, it may
11776 : : still be the label of the END of the enclosing block, in which
11777 : : case we find it by going up the code_stack. */
11778 : :
11779 : 167 : for (stack = cs_base; stack; stack = stack->prev)
11780 : : {
11781 : 131 : if (stack->current->next && stack->current->next->here == label)
11782 : : break;
11783 : 101 : if (stack->current->op == EXEC_CRITICAL)
11784 : : {
11785 : : /* Note: A label at END CRITICAL does not leave the CRITICAL
11786 : : construct as END CRITICAL is still part of it. */
11787 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
11788 : : " at %L", &code->loc, &label->where);
11789 : 2 : return;
11790 : : }
11791 : 99 : else if (stack->current->op == EXEC_DO_CONCURRENT)
11792 : : {
11793 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
11794 : : "label at %L", &code->loc, &label->where);
11795 : 0 : return;
11796 : : }
11797 : : }
11798 : :
11799 : 66 : if (stack)
11800 : : {
11801 : 30 : gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
11802 : : return;
11803 : : }
11804 : :
11805 : : /* The label is not in an enclosing block, so illegal. This was
11806 : : allowed in Fortran 66, so we allow it as extension. No
11807 : : further checks are necessary in this case. */
11808 : 36 : gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
11809 : : "as the GOTO statement at %L", &label->where,
11810 : : &code->loc);
11811 : 36 : return;
11812 : : }
11813 : :
11814 : :
11815 : : /* Check whether EXPR1 has the same shape as EXPR2. */
11816 : :
11817 : : static bool
11818 : 1461 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
11819 : : {
11820 : 1461 : mpz_t shape[GFC_MAX_DIMENSIONS];
11821 : 1461 : mpz_t shape2[GFC_MAX_DIMENSIONS];
11822 : 1461 : bool result = false;
11823 : 1461 : int i;
11824 : :
11825 : : /* Compare the rank. */
11826 : 1461 : if (expr1->rank != expr2->rank)
11827 : : return result;
11828 : :
11829 : : /* Compare the size of each dimension. */
11830 : 2795 : for (i=0; i<expr1->rank; i++)
11831 : : {
11832 : 1484 : if (!gfc_array_dimen_size (expr1, i, &shape[i]))
11833 : 150 : goto ignore;
11834 : :
11835 : 1334 : if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
11836 : 0 : goto ignore;
11837 : :
11838 : 1334 : if (mpz_cmp (shape[i], shape2[i]))
11839 : 0 : goto over;
11840 : : }
11841 : :
11842 : : /* When either of the two expression is an assumed size array, we
11843 : : ignore the comparison of dimension sizes. */
11844 : 1311 : ignore:
11845 : : result = true;
11846 : :
11847 : 1461 : over:
11848 : 1461 : gfc_clear_shape (shape, i);
11849 : 1461 : gfc_clear_shape (shape2, i);
11850 : 1461 : return result;
11851 : : }
11852 : :
11853 : :
11854 : : /* Check whether a WHERE assignment target or a WHERE mask expression
11855 : : has the same shape as the outmost WHERE mask expression. */
11856 : :
11857 : : static void
11858 : 506 : resolve_where (gfc_code *code, gfc_expr *mask)
11859 : : {
11860 : 506 : gfc_code *cblock;
11861 : 506 : gfc_code *cnext;
11862 : 506 : gfc_expr *e = NULL;
11863 : :
11864 : 506 : cblock = code->block;
11865 : :
11866 : : /* Store the first WHERE mask-expr of the WHERE statement or construct.
11867 : : In case of nested WHERE, only the outmost one is stored. */
11868 : 506 : if (mask == NULL) /* outmost WHERE */
11869 : 450 : e = cblock->expr1;
11870 : : else /* inner WHERE */
11871 : 506 : e = mask;
11872 : :
11873 : 1381 : while (cblock)
11874 : : {
11875 : 875 : if (cblock->expr1)
11876 : : {
11877 : : /* Check if the mask-expr has a consistent shape with the
11878 : : outmost WHERE mask-expr. */
11879 : 711 : if (!resolve_where_shape (cblock->expr1, e))
11880 : 0 : gfc_error ("WHERE mask at %L has inconsistent shape",
11881 : 0 : &cblock->expr1->where);
11882 : : }
11883 : :
11884 : : /* the assignment statement of a WHERE statement, or the first
11885 : : statement in where-body-construct of a WHERE construct */
11886 : 875 : cnext = cblock->next;
11887 : 1727 : while (cnext)
11888 : : {
11889 : 852 : switch (cnext->op)
11890 : : {
11891 : : /* WHERE assignment statement */
11892 : 750 : case EXEC_ASSIGN:
11893 : :
11894 : : /* Check shape consistent for WHERE assignment target. */
11895 : 750 : if (e && !resolve_where_shape (cnext->expr1, e))
11896 : 0 : gfc_error ("WHERE assignment target at %L has "
11897 : 0 : "inconsistent shape", &cnext->expr1->where);
11898 : :
11899 : 750 : if (cnext->op == EXEC_ASSIGN
11900 : 750 : && gfc_may_be_finalized (cnext->expr1->ts))
11901 : 0 : cnext->expr1->must_finalize = 1;
11902 : :
11903 : : break;
11904 : :
11905 : :
11906 : 46 : case EXEC_ASSIGN_CALL:
11907 : 46 : resolve_call (cnext);
11908 : 46 : if (!cnext->resolved_sym->attr.elemental)
11909 : 2 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
11910 : 2 : &cnext->ext.actual->expr->where);
11911 : : break;
11912 : :
11913 : : /* WHERE or WHERE construct is part of a where-body-construct */
11914 : 56 : case EXEC_WHERE:
11915 : 56 : resolve_where (cnext, e);
11916 : 56 : break;
11917 : :
11918 : 0 : default:
11919 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
11920 : : &cnext->loc);
11921 : : }
11922 : : /* the next statement within the same where-body-construct */
11923 : 852 : cnext = cnext->next;
11924 : : }
11925 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
11926 : 875 : cblock = cblock->block;
11927 : : }
11928 : 506 : }
11929 : :
11930 : :
11931 : : /* Resolve assignment in FORALL construct.
11932 : : NVAR is the number of FORALL index variables, and VAR_EXPR records the
11933 : : FORALL index variables. */
11934 : :
11935 : : static void
11936 : 1944 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
11937 : : {
11938 : 1944 : int n;
11939 : :
11940 : 5871 : for (n = 0; n < nvar; n++)
11941 : : {
11942 : 3927 : gfc_symbol *forall_index;
11943 : :
11944 : 3927 : forall_index = var_expr[n]->symtree->n.sym;
11945 : :
11946 : : /* Check whether the assignment target is one of the FORALL index
11947 : : variable. */
11948 : 3927 : if ((code->expr1->expr_type == EXPR_VARIABLE)
11949 : 3927 : && (code->expr1->symtree->n.sym == forall_index))
11950 : 0 : gfc_error ("Assignment to a FORALL index variable at %L",
11951 : : &code->expr1->where);
11952 : : else
11953 : : {
11954 : : /* If one of the FORALL index variables doesn't appear in the
11955 : : assignment variable, then there could be a many-to-one
11956 : : assignment. Emit a warning rather than an error because the
11957 : : mask could be resolving this problem. */
11958 : 3927 : if (!find_forall_index (code->expr1, forall_index, 0))
11959 : 0 : gfc_warning (0, "The FORALL with index %qs is not used on the "
11960 : : "left side of the assignment at %L and so might "
11961 : : "cause multiple assignment to this object",
11962 : 0 : var_expr[n]->symtree->name, &code->expr1->where);
11963 : : }
11964 : : }
11965 : 1944 : }
11966 : :
11967 : :
11968 : : /* Resolve WHERE statement in FORALL construct. */
11969 : :
11970 : : static void
11971 : 46 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
11972 : : gfc_expr **var_expr)
11973 : : {
11974 : 46 : gfc_code *cblock;
11975 : 46 : gfc_code *cnext;
11976 : :
11977 : 46 : cblock = code->block;
11978 : 111 : while (cblock)
11979 : : {
11980 : : /* the assignment statement of a WHERE statement, or the first
11981 : : statement in where-body-construct of a WHERE construct */
11982 : 65 : cnext = cblock->next;
11983 : 130 : while (cnext)
11984 : : {
11985 : 65 : switch (cnext->op)
11986 : : {
11987 : : /* WHERE assignment statement */
11988 : 65 : case EXEC_ASSIGN:
11989 : 65 : gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
11990 : :
11991 : 65 : if (cnext->op == EXEC_ASSIGN
11992 : 65 : && gfc_may_be_finalized (cnext->expr1->ts))
11993 : 0 : cnext->expr1->must_finalize = 1;
11994 : :
11995 : : break;
11996 : :
11997 : : /* WHERE operator assignment statement */
11998 : 0 : case EXEC_ASSIGN_CALL:
11999 : 0 : resolve_call (cnext);
12000 : 0 : if (!cnext->resolved_sym->attr.elemental)
12001 : 0 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12002 : 0 : &cnext->ext.actual->expr->where);
12003 : : break;
12004 : :
12005 : : /* WHERE or WHERE construct is part of a where-body-construct */
12006 : 0 : case EXEC_WHERE:
12007 : 0 : gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
12008 : 0 : break;
12009 : :
12010 : 0 : default:
12011 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12012 : : &cnext->loc);
12013 : : }
12014 : : /* the next statement within the same where-body-construct */
12015 : 65 : cnext = cnext->next;
12016 : : }
12017 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12018 : 65 : cblock = cblock->block;
12019 : : }
12020 : 46 : }
12021 : :
12022 : :
12023 : : /* Traverse the FORALL body to check whether the following errors exist:
12024 : : 1. For assignment, check if a many-to-one assignment happens.
12025 : : 2. For WHERE statement, check the WHERE body to see if there is any
12026 : : many-to-one assignment. */
12027 : :
12028 : : static void
12029 : 1990 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
12030 : : {
12031 : 1990 : gfc_code *c;
12032 : :
12033 : 1990 : c = code->block->next;
12034 : 3993 : while (c)
12035 : : {
12036 : 2003 : switch (c->op)
12037 : : {
12038 : 1879 : case EXEC_ASSIGN:
12039 : 1879 : case EXEC_POINTER_ASSIGN:
12040 : 1879 : gfc_resolve_assign_in_forall (c, nvar, var_expr);
12041 : :
12042 : 1879 : if (c->op == EXEC_ASSIGN
12043 : 1879 : && gfc_may_be_finalized (c->expr1->ts))
12044 : 0 : c->expr1->must_finalize = 1;
12045 : :
12046 : : break;
12047 : :
12048 : 0 : case EXEC_ASSIGN_CALL:
12049 : 0 : resolve_call (c);
12050 : 0 : break;
12051 : :
12052 : : /* Because the gfc_resolve_blocks() will handle the nested FORALL,
12053 : : there is no need to handle it here. */
12054 : : case EXEC_FORALL:
12055 : : break;
12056 : 46 : case EXEC_WHERE:
12057 : 46 : gfc_resolve_where_code_in_forall(c, nvar, var_expr);
12058 : 46 : break;
12059 : : default:
12060 : : break;
12061 : : }
12062 : : /* The next statement in the FORALL body. */
12063 : 2003 : c = c->next;
12064 : : }
12065 : 1990 : }
12066 : :
12067 : :
12068 : : /* Counts the number of iterators needed inside a forall construct, including
12069 : : nested forall constructs. This is used to allocate the needed memory
12070 : : in gfc_resolve_forall. */
12071 : :
12072 : : static int
12073 : 1990 : gfc_count_forall_iterators (gfc_code *code)
12074 : : {
12075 : 1990 : int max_iters, sub_iters, current_iters;
12076 : 1990 : gfc_forall_iterator *fa;
12077 : :
12078 : 1990 : gcc_assert(code->op == EXEC_FORALL);
12079 : 1990 : max_iters = 0;
12080 : 1990 : current_iters = 0;
12081 : :
12082 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12083 : 3888 : current_iters ++;
12084 : :
12085 : 1990 : code = code->block->next;
12086 : :
12087 : 3993 : while (code)
12088 : : {
12089 : 2003 : if (code->op == EXEC_FORALL)
12090 : : {
12091 : 78 : sub_iters = gfc_count_forall_iterators (code);
12092 : 78 : if (sub_iters > max_iters)
12093 : 2003 : max_iters = sub_iters;
12094 : : }
12095 : 2003 : code = code->next;
12096 : : }
12097 : :
12098 : 1990 : return current_iters + max_iters;
12099 : : }
12100 : :
12101 : :
12102 : : /* Given a FORALL construct, first resolve the FORALL iterator, then call
12103 : : gfc_resolve_forall_body to resolve the FORALL body. */
12104 : :
12105 : : static void
12106 : 1990 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
12107 : : {
12108 : 1990 : static gfc_expr **var_expr;
12109 : 1990 : static int total_var = 0;
12110 : 1990 : static int nvar = 0;
12111 : 1990 : int i, old_nvar, tmp;
12112 : 1990 : gfc_forall_iterator *fa;
12113 : :
12114 : 1990 : old_nvar = nvar;
12115 : :
12116 : 1990 : if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12117 : : return;
12118 : :
12119 : : /* Start to resolve a FORALL construct */
12120 : 1990 : if (forall_save == 0)
12121 : : {
12122 : : /* Count the total number of FORALL indices in the nested FORALL
12123 : : construct in order to allocate the VAR_EXPR with proper size. */
12124 : 1912 : total_var = gfc_count_forall_iterators (code);
12125 : :
12126 : : /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12127 : 1912 : var_expr = XCNEWVEC (gfc_expr *, total_var);
12128 : : }
12129 : :
12130 : : /* The information about FORALL iterator, including FORALL indices start, end
12131 : : and stride. An outer FORALL indice cannot appear in start, end or stride. */
12132 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12133 : : {
12134 : : /* Fortran 20008: C738 (R753). */
12135 : 3888 : if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12136 : : {
12137 : 2 : gfc_error ("FORALL index-name at %L must be a scalar variable "
12138 : : "of type integer", &fa->var->where);
12139 : 2 : continue;
12140 : : }
12141 : :
12142 : : /* Check if any outer FORALL index name is the same as the current
12143 : : one. */
12144 : 6859 : for (i = 0; i < nvar; i++)
12145 : : {
12146 : 2973 : if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12147 : 0 : gfc_error ("An outer FORALL construct already has an index "
12148 : : "with this name %L", &fa->var->where);
12149 : : }
12150 : :
12151 : : /* Record the current FORALL index. */
12152 : 3886 : var_expr[nvar] = gfc_copy_expr (fa->var);
12153 : :
12154 : 3886 : nvar++;
12155 : :
12156 : : /* No memory leak. */
12157 : 3886 : gcc_assert (nvar <= total_var);
12158 : : }
12159 : :
12160 : : /* Resolve the FORALL body. */
12161 : 1990 : gfc_resolve_forall_body (code, nvar, var_expr);
12162 : :
12163 : : /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12164 : 1990 : gfc_resolve_blocks (code->block, ns);
12165 : :
12166 : 1990 : tmp = nvar;
12167 : 1990 : nvar = old_nvar;
12168 : : /* Free only the VAR_EXPRs allocated in this frame. */
12169 : 5876 : for (i = nvar; i < tmp; i++)
12170 : 3886 : gfc_free_expr (var_expr[i]);
12171 : :
12172 : 1990 : if (nvar == 0)
12173 : : {
12174 : : /* We are in the outermost FORALL construct. */
12175 : 1912 : gcc_assert (forall_save == 0);
12176 : :
12177 : : /* VAR_EXPR is not needed any more. */
12178 : 1912 : free (var_expr);
12179 : 1912 : total_var = 0;
12180 : : }
12181 : : }
12182 : :
12183 : :
12184 : : /* Resolve a BLOCK construct statement. */
12185 : :
12186 : : static void
12187 : 7593 : resolve_block_construct (gfc_code* code)
12188 : : {
12189 : 7593 : gfc_namespace *ns = code->ext.block.ns;
12190 : :
12191 : : /* For an ASSOCIATE block, the associations (and their targets) will be
12192 : : resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12193 : : namespace. */
12194 : 7593 : gfc_resolve (ns);
12195 : 0 : }
12196 : :
12197 : :
12198 : : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12199 : : DO code nodes. */
12200 : :
12201 : : void
12202 : 322650 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12203 : : {
12204 : 322650 : bool t;
12205 : :
12206 : 656499 : for (; b; b = b->block)
12207 : : {
12208 : 333849 : t = gfc_resolve_expr (b->expr1);
12209 : 333849 : if (!gfc_resolve_expr (b->expr2))
12210 : 0 : t = false;
12211 : :
12212 : 333849 : switch (b->op)
12213 : : {
12214 : 230409 : case EXEC_IF:
12215 : 230409 : if (t && b->expr1 != NULL
12216 : 226283 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12217 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12218 : : &b->expr1->where);
12219 : : break;
12220 : :
12221 : 761 : case EXEC_WHERE:
12222 : 761 : if (t
12223 : 761 : && b->expr1 != NULL
12224 : 628 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12225 : 0 : gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12226 : : &b->expr1->where);
12227 : : break;
12228 : :
12229 : 76 : case EXEC_GOTO:
12230 : 76 : resolve_branch (b->label1, b);
12231 : 76 : break;
12232 : :
12233 : 0 : case EXEC_BLOCK:
12234 : 0 : resolve_block_construct (b);
12235 : 0 : break;
12236 : :
12237 : : case EXEC_SELECT:
12238 : : case EXEC_SELECT_TYPE:
12239 : : case EXEC_SELECT_RANK:
12240 : : case EXEC_FORALL:
12241 : : case EXEC_DO:
12242 : : case EXEC_DO_WHILE:
12243 : : case EXEC_DO_CONCURRENT:
12244 : : case EXEC_CRITICAL:
12245 : : case EXEC_READ:
12246 : : case EXEC_WRITE:
12247 : : case EXEC_IOLENGTH:
12248 : : case EXEC_WAIT:
12249 : : break;
12250 : :
12251 : 2697 : case EXEC_OMP_ATOMIC:
12252 : 2697 : case EXEC_OACC_ATOMIC:
12253 : 2697 : {
12254 : : /* Verify this before calling gfc_resolve_code, which might
12255 : : change it. */
12256 : 2697 : gcc_assert (b->op == EXEC_OMP_ATOMIC
12257 : : || (b->next && b->next->op == EXEC_ASSIGN));
12258 : : }
12259 : : break;
12260 : :
12261 : : case EXEC_OACC_PARALLEL_LOOP:
12262 : : case EXEC_OACC_PARALLEL:
12263 : : case EXEC_OACC_KERNELS_LOOP:
12264 : : case EXEC_OACC_KERNELS:
12265 : : case EXEC_OACC_SERIAL_LOOP:
12266 : : case EXEC_OACC_SERIAL:
12267 : : case EXEC_OACC_DATA:
12268 : : case EXEC_OACC_HOST_DATA:
12269 : : case EXEC_OACC_LOOP:
12270 : : case EXEC_OACC_UPDATE:
12271 : : case EXEC_OACC_WAIT:
12272 : : case EXEC_OACC_CACHE:
12273 : : case EXEC_OACC_ENTER_DATA:
12274 : : case EXEC_OACC_EXIT_DATA:
12275 : : case EXEC_OACC_ROUTINE:
12276 : : case EXEC_OMP_ALLOCATE:
12277 : : case EXEC_OMP_ALLOCATORS:
12278 : : case EXEC_OMP_ASSUME:
12279 : : case EXEC_OMP_CRITICAL:
12280 : : case EXEC_OMP_DISPATCH:
12281 : : case EXEC_OMP_DISTRIBUTE:
12282 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12283 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12284 : : case EXEC_OMP_DISTRIBUTE_SIMD:
12285 : : case EXEC_OMP_DO:
12286 : : case EXEC_OMP_DO_SIMD:
12287 : : case EXEC_OMP_ERROR:
12288 : : case EXEC_OMP_LOOP:
12289 : : case EXEC_OMP_MASKED:
12290 : : case EXEC_OMP_MASKED_TASKLOOP:
12291 : : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12292 : : case EXEC_OMP_MASTER:
12293 : : case EXEC_OMP_MASTER_TASKLOOP:
12294 : : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12295 : : case EXEC_OMP_ORDERED:
12296 : : case EXEC_OMP_PARALLEL:
12297 : : case EXEC_OMP_PARALLEL_DO:
12298 : : case EXEC_OMP_PARALLEL_DO_SIMD:
12299 : : case EXEC_OMP_PARALLEL_LOOP:
12300 : : case EXEC_OMP_PARALLEL_MASKED:
12301 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12302 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12303 : : case EXEC_OMP_PARALLEL_MASTER:
12304 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12305 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12306 : : case EXEC_OMP_PARALLEL_SECTIONS:
12307 : : case EXEC_OMP_PARALLEL_WORKSHARE:
12308 : : case EXEC_OMP_SECTIONS:
12309 : : case EXEC_OMP_SIMD:
12310 : : case EXEC_OMP_SCOPE:
12311 : : case EXEC_OMP_SINGLE:
12312 : : case EXEC_OMP_TARGET:
12313 : : case EXEC_OMP_TARGET_DATA:
12314 : : case EXEC_OMP_TARGET_ENTER_DATA:
12315 : : case EXEC_OMP_TARGET_EXIT_DATA:
12316 : : case EXEC_OMP_TARGET_PARALLEL:
12317 : : case EXEC_OMP_TARGET_PARALLEL_DO:
12318 : : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12319 : : case EXEC_OMP_TARGET_PARALLEL_LOOP:
12320 : : case EXEC_OMP_TARGET_SIMD:
12321 : : case EXEC_OMP_TARGET_TEAMS:
12322 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12323 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12324 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12325 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12326 : : case EXEC_OMP_TARGET_TEAMS_LOOP:
12327 : : case EXEC_OMP_TARGET_UPDATE:
12328 : : case EXEC_OMP_TASK:
12329 : : case EXEC_OMP_TASKGROUP:
12330 : : case EXEC_OMP_TASKLOOP:
12331 : : case EXEC_OMP_TASKLOOP_SIMD:
12332 : : case EXEC_OMP_TASKWAIT:
12333 : : case EXEC_OMP_TASKYIELD:
12334 : : case EXEC_OMP_TEAMS:
12335 : : case EXEC_OMP_TEAMS_DISTRIBUTE:
12336 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12337 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12338 : : case EXEC_OMP_TEAMS_LOOP:
12339 : : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12340 : : case EXEC_OMP_TILE:
12341 : : case EXEC_OMP_UNROLL:
12342 : : case EXEC_OMP_WORKSHARE:
12343 : : break;
12344 : :
12345 : 0 : default:
12346 : 0 : gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
12347 : : }
12348 : :
12349 : 333849 : gfc_resolve_code (b->next, ns);
12350 : : }
12351 : 322650 : }
12352 : :
12353 : : bool
12354 : 0 : caf_possible_reallocate (gfc_expr *e)
12355 : : {
12356 : 0 : symbol_attribute caf_attr;
12357 : 0 : gfc_ref *last_arr_ref = nullptr;
12358 : :
12359 : 0 : caf_attr = gfc_caf_attr (e);
12360 : 0 : if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
12361 : : return false;
12362 : :
12363 : : /* Only full array refs can indicate a needed reallocation. */
12364 : 0 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
12365 : 0 : if (ref->type == REF_ARRAY && ref->u.ar.dimen)
12366 : 0 : last_arr_ref = ref;
12367 : :
12368 : 0 : return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
12369 : : }
12370 : :
12371 : : /* Does everything to resolve an ordinary assignment. Returns true
12372 : : if this is an interface assignment. */
12373 : : static bool
12374 : 278988 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
12375 : : {
12376 : 278988 : bool rval = false;
12377 : 278988 : gfc_expr *lhs;
12378 : 278988 : gfc_expr *rhs;
12379 : 278988 : int n;
12380 : 278988 : gfc_ref *ref;
12381 : 278988 : symbol_attribute attr;
12382 : :
12383 : 278988 : if (gfc_extend_assign (code, ns))
12384 : : {
12385 : 752 : gfc_expr** rhsptr;
12386 : :
12387 : 752 : if (code->op == EXEC_ASSIGN_CALL)
12388 : : {
12389 : 336 : lhs = code->ext.actual->expr;
12390 : 336 : rhsptr = &code->ext.actual->next->expr;
12391 : : }
12392 : : else
12393 : : {
12394 : 416 : gfc_actual_arglist* args;
12395 : 416 : gfc_typebound_proc* tbp;
12396 : :
12397 : 416 : gcc_assert (code->op == EXEC_COMPCALL);
12398 : :
12399 : 416 : args = code->expr1->value.compcall.actual;
12400 : 416 : lhs = args->expr;
12401 : 416 : rhsptr = &args->next->expr;
12402 : :
12403 : 416 : tbp = code->expr1->value.compcall.tbp;
12404 : 416 : gcc_assert (!tbp->is_generic);
12405 : : }
12406 : :
12407 : : /* Make a temporary rhs when there is a default initializer
12408 : : and rhs is the same symbol as the lhs. */
12409 : 752 : if ((*rhsptr)->expr_type == EXPR_VARIABLE
12410 : 380 : && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
12411 : 327 : && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
12412 : 931 : && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
12413 : 24 : *rhsptr = gfc_get_parentheses (*rhsptr);
12414 : :
12415 : 752 : return true;
12416 : : }
12417 : :
12418 : 278236 : lhs = code->expr1;
12419 : 278236 : rhs = code->expr2;
12420 : :
12421 : 278236 : if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
12422 : 260004 : || lhs->symtree->n.sym->ts.type == BT_CLASS)
12423 : 20593 : && !lhs->symtree->n.sym->attr.proc_pointer
12424 : 298829 : && gfc_expr_attr (lhs).proc_pointer)
12425 : : {
12426 : 1 : gfc_error ("Variable in the ordinary assignment at %L is a procedure "
12427 : : "pointer component",
12428 : : &lhs->where);
12429 : 1 : return false;
12430 : : }
12431 : :
12432 : 326464 : if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
12433 : 245286 : && rhs->ts.type == BT_CHARACTER
12434 : 278628 : && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
12435 : : {
12436 : : /* Use of -fdec-char-conversions allows assignment of character data
12437 : : to non-character variables. This not permitted for nonconstant
12438 : : strings. */
12439 : 29 : gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
12440 : : gfc_typename (lhs), &rhs->where);
12441 : 29 : return false;
12442 : : }
12443 : :
12444 : 278206 : if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
12445 : : {
12446 : 0 : gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
12447 : : gfc_typename (lhs), &rhs->where);
12448 : 0 : return false;
12449 : : }
12450 : :
12451 : : /* Handle the case of a BOZ literal on the RHS. */
12452 : 278206 : if (rhs->ts.type == BT_BOZ)
12453 : : {
12454 : 3 : if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
12455 : : "statement value nor an actual argument of "
12456 : : "INT/REAL/DBLE/CMPLX intrinsic subprogram",
12457 : : &rhs->where))
12458 : : return false;
12459 : :
12460 : 1 : switch (lhs->ts.type)
12461 : : {
12462 : 0 : case BT_INTEGER:
12463 : 0 : if (!gfc_boz2int (rhs, lhs->ts.kind))
12464 : : return false;
12465 : : break;
12466 : 1 : case BT_REAL:
12467 : 1 : if (!gfc_boz2real (rhs, lhs->ts.kind))
12468 : : return false;
12469 : : break;
12470 : 0 : default:
12471 : 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
12472 : 0 : return false;
12473 : : }
12474 : : }
12475 : :
12476 : 278204 : if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
12477 : : {
12478 : 64 : HOST_WIDE_INT llen = 0, rlen = 0;
12479 : 64 : if (lhs->ts.u.cl != NULL
12480 : 64 : && lhs->ts.u.cl->length != NULL
12481 : 53 : && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12482 : 53 : llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
12483 : :
12484 : 64 : if (rhs->expr_type == EXPR_CONSTANT)
12485 : 26 : rlen = rhs->value.character.length;
12486 : :
12487 : 38 : else if (rhs->ts.u.cl != NULL
12488 : 38 : && rhs->ts.u.cl->length != NULL
12489 : 35 : && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12490 : 35 : rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
12491 : :
12492 : 64 : if (rlen && llen && rlen > llen)
12493 : 28 : gfc_warning_now (OPT_Wcharacter_truncation,
12494 : : "CHARACTER expression will be truncated "
12495 : : "in assignment (%wd/%wd) at %L",
12496 : : llen, rlen, &code->loc);
12497 : : }
12498 : :
12499 : : /* Ensure that a vector index expression for the lvalue is evaluated
12500 : : to a temporary if the lvalue symbol is referenced in it. */
12501 : 278204 : if (lhs->rank)
12502 : : {
12503 : 106399 : for (ref = lhs->ref; ref; ref= ref->next)
12504 : 56355 : if (ref->type == REF_ARRAY)
12505 : : {
12506 : 126710 : for (n = 0; n < ref->u.ar.dimen; n++)
12507 : 75217 : if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
12508 : 75442 : && gfc_find_sym_in_expr (lhs->symtree->n.sym,
12509 : 225 : ref->u.ar.start[n]))
12510 : 14 : ref->u.ar.start[n]
12511 : 14 : = gfc_get_parentheses (ref->u.ar.start[n]);
12512 : : }
12513 : : }
12514 : :
12515 : 278204 : if (gfc_pure (NULL))
12516 : : {
12517 : 3232 : if (lhs->ts.type == BT_DERIVED
12518 : 84 : && lhs->expr_type == EXPR_VARIABLE
12519 : 84 : && lhs->ts.u.derived->attr.pointer_comp
12520 : 4 : && rhs->expr_type == EXPR_VARIABLE
12521 : 3235 : && (gfc_impure_variable (rhs->symtree->n.sym)
12522 : 2 : || gfc_is_coindexed (rhs)))
12523 : : {
12524 : : /* F2008, C1283. */
12525 : 2 : if (gfc_is_coindexed (rhs))
12526 : 1 : gfc_error ("Coindexed expression at %L is assigned to "
12527 : : "a derived type variable with a POINTER "
12528 : : "component in a PURE procedure",
12529 : : &rhs->where);
12530 : : else
12531 : : /* F2008, C1283 (4). */
12532 : 1 : gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
12533 : : "shall not be used as the expr at %L of an intrinsic "
12534 : : "assignment statement in which the variable is of a "
12535 : : "derived type if the derived type has a pointer "
12536 : : "component at any level of component selection.",
12537 : : &rhs->where);
12538 : 2 : return rval;
12539 : : }
12540 : :
12541 : : /* Fortran 2008, C1283. */
12542 : 3230 : if (gfc_is_coindexed (lhs))
12543 : : {
12544 : 1 : gfc_error ("Assignment to coindexed variable at %L in a PURE "
12545 : : "procedure", &rhs->where);
12546 : 1 : return rval;
12547 : : }
12548 : : }
12549 : :
12550 : 278201 : if (gfc_implicit_pure (NULL))
12551 : : {
12552 : 6916 : if (lhs->expr_type == EXPR_VARIABLE
12553 : 6916 : && lhs->symtree->n.sym != gfc_current_ns->proc_name
12554 : 4961 : && lhs->symtree->n.sym->ns != gfc_current_ns)
12555 : 237 : gfc_unset_implicit_pure (NULL);
12556 : :
12557 : 6916 : if (lhs->ts.type == BT_DERIVED
12558 : 299 : && lhs->expr_type == EXPR_VARIABLE
12559 : 299 : && lhs->ts.u.derived->attr.pointer_comp
12560 : 7 : && rhs->expr_type == EXPR_VARIABLE
12561 : 6923 : && (gfc_impure_variable (rhs->symtree->n.sym)
12562 : 7 : || gfc_is_coindexed (rhs)))
12563 : 0 : gfc_unset_implicit_pure (NULL);
12564 : :
12565 : : /* Fortran 2008, C1283. */
12566 : 6916 : if (gfc_is_coindexed (lhs))
12567 : 0 : gfc_unset_implicit_pure (NULL);
12568 : : }
12569 : :
12570 : : /* F2008, 7.2.1.2. */
12571 : 278201 : attr = gfc_expr_attr (lhs);
12572 : 278201 : if (lhs->ts.type == BT_CLASS && attr.allocatable)
12573 : : {
12574 : 856 : if (attr.codimension)
12575 : : {
12576 : 1 : gfc_error ("Assignment to polymorphic coarray at %L is not "
12577 : : "permitted", &lhs->where);
12578 : 1 : return false;
12579 : : }
12580 : 855 : if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
12581 : : "polymorphic variable at %L", &lhs->where))
12582 : : return false;
12583 : 854 : if (!flag_realloc_lhs)
12584 : : {
12585 : 1 : gfc_error ("Assignment to an allocatable polymorphic variable at %L "
12586 : : "requires %<-frealloc-lhs%>", &lhs->where);
12587 : 1 : return false;
12588 : : }
12589 : : }
12590 : 277345 : else if (lhs->ts.type == BT_CLASS)
12591 : : {
12592 : 9 : gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
12593 : : "assignment at %L - check that there is a matching specific "
12594 : : "subroutine for %<=%> operator", &lhs->where);
12595 : 9 : return false;
12596 : : }
12597 : :
12598 : 278189 : bool lhs_coindexed = gfc_is_coindexed (lhs);
12599 : :
12600 : : /* F2008, Section 7.2.1.2. */
12601 : 278189 : if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
12602 : : {
12603 : 1 : gfc_error ("Coindexed variable must not have an allocatable ultimate "
12604 : : "component in assignment at %L", &lhs->where);
12605 : 1 : return false;
12606 : : }
12607 : :
12608 : : /* Assign the 'data' of a class object to a derived type. */
12609 : 278188 : if (lhs->ts.type == BT_DERIVED
12610 : 6559 : && rhs->ts.type == BT_CLASS
12611 : 137 : && rhs->expr_type != EXPR_ARRAY)
12612 : 131 : gfc_add_data_component (rhs);
12613 : :
12614 : : /* Make sure there is a vtable and, in particular, a _copy for the
12615 : : rhs type. */
12616 : 278188 : if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
12617 : 502 : gfc_find_vtab (&rhs->ts);
12618 : :
12619 : 278188 : gfc_check_assign (lhs, rhs, 1);
12620 : :
12621 : 278188 : return false;
12622 : : }
12623 : :
12624 : :
12625 : : /* Add a component reference onto an expression. */
12626 : :
12627 : : static void
12628 : 665 : add_comp_ref (gfc_expr *e, gfc_component *c)
12629 : : {
12630 : 665 : gfc_ref **ref;
12631 : 665 : ref = &(e->ref);
12632 : 889 : while (*ref)
12633 : 224 : ref = &((*ref)->next);
12634 : 665 : *ref = gfc_get_ref ();
12635 : 665 : (*ref)->type = REF_COMPONENT;
12636 : 665 : (*ref)->u.c.sym = e->ts.u.derived;
12637 : 665 : (*ref)->u.c.component = c;
12638 : 665 : e->ts = c->ts;
12639 : :
12640 : : /* Add a full array ref, as necessary. */
12641 : 665 : if (c->as)
12642 : : {
12643 : 84 : gfc_add_full_array_ref (e, c->as);
12644 : 84 : e->rank = c->as->rank;
12645 : 84 : e->corank = c->as->corank;
12646 : : }
12647 : 665 : }
12648 : :
12649 : :
12650 : : /* Build an assignment. Keep the argument 'op' for future use, so that
12651 : : pointer assignments can be made. */
12652 : :
12653 : : static gfc_code *
12654 : 898 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
12655 : : gfc_component *comp1, gfc_component *comp2, locus loc)
12656 : : {
12657 : 898 : gfc_code *this_code;
12658 : :
12659 : 898 : this_code = gfc_get_code (op);
12660 : 898 : this_code->next = NULL;
12661 : 898 : this_code->expr1 = gfc_copy_expr (expr1);
12662 : 898 : this_code->expr2 = gfc_copy_expr (expr2);
12663 : 898 : this_code->loc = loc;
12664 : 898 : if (comp1 && comp2)
12665 : : {
12666 : 288 : add_comp_ref (this_code->expr1, comp1);
12667 : 288 : add_comp_ref (this_code->expr2, comp2);
12668 : : }
12669 : :
12670 : 898 : return this_code;
12671 : : }
12672 : :
12673 : :
12674 : : /* Makes a temporary variable expression based on the characteristics of
12675 : : a given variable expression. */
12676 : :
12677 : : static gfc_expr*
12678 : 392 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12679 : : {
12680 : 392 : static int serial = 0;
12681 : 392 : char name[GFC_MAX_SYMBOL_LEN];
12682 : 392 : gfc_symtree *tmp;
12683 : 392 : gfc_array_spec *as;
12684 : 392 : gfc_array_ref *aref;
12685 : 392 : gfc_ref *ref;
12686 : :
12687 : 392 : sprintf (name, GFC_PREFIX("DA%d"), serial++);
12688 : 392 : gfc_get_sym_tree (name, ns, &tmp, false);
12689 : 392 : gfc_add_type (tmp->n.sym, &e->ts, NULL);
12690 : :
12691 : 392 : if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12692 : 0 : tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12693 : : NULL,
12694 : 0 : e->value.character.length);
12695 : :
12696 : 392 : as = NULL;
12697 : 392 : ref = NULL;
12698 : 392 : aref = NULL;
12699 : :
12700 : : /* Obtain the arrayspec for the temporary. */
12701 : 392 : if (e->rank && e->expr_type != EXPR_ARRAY
12702 : : && e->expr_type != EXPR_FUNCTION
12703 : : && e->expr_type != EXPR_OP)
12704 : : {
12705 : 52 : aref = gfc_find_array_ref (e);
12706 : 52 : if (e->expr_type == EXPR_VARIABLE
12707 : 52 : && e->symtree->n.sym->as == aref->as)
12708 : : as = aref->as;
12709 : : else
12710 : : {
12711 : 0 : for (ref = e->ref; ref; ref = ref->next)
12712 : 0 : if (ref->type == REF_COMPONENT
12713 : 0 : && ref->u.c.component->as == aref->as)
12714 : : {
12715 : : as = aref->as;
12716 : : break;
12717 : : }
12718 : : }
12719 : : }
12720 : :
12721 : : /* Add the attributes and the arrayspec to the temporary. */
12722 : 392 : tmp->n.sym->attr = gfc_expr_attr (e);
12723 : 392 : tmp->n.sym->attr.function = 0;
12724 : 392 : tmp->n.sym->attr.proc_pointer = 0;
12725 : 392 : tmp->n.sym->attr.result = 0;
12726 : 392 : tmp->n.sym->attr.flavor = FL_VARIABLE;
12727 : 392 : tmp->n.sym->attr.dummy = 0;
12728 : 392 : tmp->n.sym->attr.use_assoc = 0;
12729 : 392 : tmp->n.sym->attr.intent = INTENT_UNKNOWN;
12730 : :
12731 : :
12732 : 392 : if (as)
12733 : : {
12734 : 52 : tmp->n.sym->as = gfc_copy_array_spec (as);
12735 : 52 : if (!ref)
12736 : 52 : ref = e->ref;
12737 : 52 : if (as->type == AS_DEFERRED)
12738 : 46 : tmp->n.sym->attr.allocatable = 1;
12739 : : }
12740 : 340 : else if ((e->rank || e->corank)
12741 : 48 : && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
12742 : 0 : || e->expr_type == EXPR_OP))
12743 : : {
12744 : 48 : tmp->n.sym->as = gfc_get_array_spec ();
12745 : 48 : tmp->n.sym->as->type = AS_DEFERRED;
12746 : 48 : tmp->n.sym->as->rank = e->rank;
12747 : 48 : tmp->n.sym->as->corank = e->corank;
12748 : 48 : tmp->n.sym->attr.allocatable = 1;
12749 : 48 : tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
12750 : 96 : tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
12751 : : }
12752 : : else
12753 : 292 : tmp->n.sym->attr.dimension = 0;
12754 : :
12755 : 392 : gfc_set_sym_referenced (tmp->n.sym);
12756 : 392 : gfc_commit_symbol (tmp->n.sym);
12757 : 392 : e = gfc_lval_expr_from_sym (tmp->n.sym);
12758 : :
12759 : : /* Should the lhs be a section, use its array ref for the
12760 : : temporary expression. */
12761 : 392 : if (aref && aref->type != AR_FULL)
12762 : : {
12763 : 6 : gfc_free_ref_list (e->ref);
12764 : 6 : e->ref = gfc_copy_ref (ref);
12765 : : }
12766 : 392 : return e;
12767 : : }
12768 : :
12769 : :
12770 : : /* Add one line of code to the code chain, making sure that 'head' and
12771 : : 'tail' are appropriately updated. */
12772 : :
12773 : : static void
12774 : 656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
12775 : : {
12776 : 656 : gcc_assert (this_code);
12777 : 656 : if (*head == NULL)
12778 : 308 : *head = *tail = *this_code;
12779 : : else
12780 : 348 : *tail = gfc_append_code (*tail, *this_code);
12781 : 656 : *this_code = NULL;
12782 : 656 : }
12783 : :
12784 : :
12785 : : /* Generate a final call from a variable expression */
12786 : :
12787 : : static void
12788 : 81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
12789 : : {
12790 : 81 : gfc_code *this_code;
12791 : 81 : gfc_expr *final_expr = NULL;
12792 : 81 : gfc_expr *size_expr;
12793 : 81 : gfc_expr *fini_coarray;
12794 : :
12795 : 81 : gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
12796 : 81 : if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
12797 : 75 : return;
12798 : :
12799 : : /* Now generate the finalizer call. */
12800 : 6 : this_code = gfc_get_code (EXEC_CALL);
12801 : 6 : this_code->symtree = final_expr->symtree;
12802 : 6 : this_code->resolved_sym = final_expr->symtree->n.sym;
12803 : :
12804 : : //* Expression to be finalized */
12805 : 6 : this_code->ext.actual = gfc_get_actual_arglist ();
12806 : 6 : this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
12807 : :
12808 : : /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
12809 : 6 : this_code->ext.actual->next = gfc_get_actual_arglist ();
12810 : 6 : size_expr = gfc_get_expr ();
12811 : 6 : size_expr->where = gfc_current_locus;
12812 : 6 : size_expr->expr_type = EXPR_OP;
12813 : 6 : size_expr->value.op.op = INTRINSIC_DIVIDE;
12814 : 6 : size_expr->value.op.op1
12815 : 12 : = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
12816 : : "storage_size", gfc_current_locus, 2,
12817 : 6 : gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
12818 : : gfc_get_int_expr (gfc_index_integer_kind,
12819 : : NULL, 0));
12820 : 6 : size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
12821 : : gfc_character_storage_size);
12822 : 6 : size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
12823 : 6 : size_expr->ts = size_expr->value.op.op1->ts;
12824 : 6 : this_code->ext.actual->next->expr = size_expr;
12825 : :
12826 : : /* fini_coarray */
12827 : 6 : this_code->ext.actual->next->next = gfc_get_actual_arglist ();
12828 : 6 : fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
12829 : : &tmp_expr->where);
12830 : 6 : fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
12831 : 6 : this_code->ext.actual->next->next->expr = fini_coarray;
12832 : :
12833 : 6 : add_code_to_chain (&this_code, head, tail);
12834 : :
12835 : : }
12836 : :
12837 : : /* Counts the potential number of part array references that would
12838 : : result from resolution of typebound defined assignments. */
12839 : :
12840 : :
12841 : : static int
12842 : 243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
12843 : : {
12844 : 243 : gfc_component *c;
12845 : 243 : int c_depth = 0, t_depth;
12846 : :
12847 : 584 : for (c= derived->components; c; c = c->next)
12848 : : {
12849 : 341 : if ((!gfc_bt_struct (c->ts.type)
12850 : 261 : || c->attr.pointer
12851 : 261 : || c->attr.allocatable
12852 : 260 : || c->attr.proc_pointer_comp
12853 : 260 : || c->attr.class_pointer
12854 : 260 : || c->attr.proc_pointer)
12855 : 81 : && !c->attr.defined_assign_comp)
12856 : 81 : continue;
12857 : :
12858 : 260 : if (c->as && c_depth == 0)
12859 : 260 : c_depth = 1;
12860 : :
12861 : 260 : if (c->ts.u.derived->attr.defined_assign_comp)
12862 : 110 : t_depth = nonscalar_typebound_assign (c->ts.u.derived,
12863 : : c->as ? 1 : 0);
12864 : : else
12865 : : t_depth = 0;
12866 : :
12867 : 260 : c_depth = t_depth > c_depth ? t_depth : c_depth;
12868 : : }
12869 : 243 : return depth + c_depth;
12870 : : }
12871 : :
12872 : :
12873 : : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
12874 : : "An intrinsic assignment where the variable is of derived type is performed
12875 : : as if each component of the variable were assigned from the corresponding
12876 : : component of expr using pointer assignment (10.2.2) for each pointer
12877 : : component, defined assignment for each nonpointer nonallocatable component
12878 : : of a type that has a type-bound defined assignment consistent with the
12879 : : component, intrinsic assignment for each other nonpointer nonallocatable
12880 : : component, and intrinsic assignment for each allocated coarray component.
12881 : : For unallocated coarray components, the corresponding component of the
12882 : : variable shall be unallocated. For a noncoarray allocatable component the
12883 : : following sequence of operations is applied.
12884 : : (1) If the component of the variable is allocated, it is deallocated.
12885 : : (2) If the component of the value of expr is allocated, the
12886 : : corresponding component of the variable is allocated with the same
12887 : : dynamic type and type parameters as the component of the value of
12888 : : expr. If it is an array, it is allocated with the same bounds. The
12889 : : value of the component of the value of expr is then assigned to the
12890 : : corresponding component of the variable using defined assignment if
12891 : : the declared type of the component has a type-bound defined
12892 : : assignment consistent with the component, and intrinsic assignment
12893 : : for the dynamic type of that component otherwise."
12894 : :
12895 : : The pointer assignments are taken care of by the intrinsic assignment of the
12896 : : structure itself. This function recursively adds defined assignments where
12897 : : required. The recursion is accomplished by calling gfc_resolve_code.
12898 : :
12899 : : When the lhs in a defined assignment has intent INOUT or is intent OUT
12900 : : and the component of 'var' is finalizable, we need a temporary for the
12901 : : lhs. In pseudo-code for an assignment var = expr:
12902 : :
12903 : : ! Confine finalization of temporaries, as far as possible.
12904 : : Enclose the code for the assignment in a block
12905 : : ! Only call function 'expr' once.
12906 : : #if ('expr is not a constant or an variable)
12907 : : temp_expr = expr
12908 : : expr = temp_x
12909 : : ! Do the intrinsic assignment
12910 : : #if typeof ('var') has a typebound final subroutine
12911 : : finalize (var)
12912 : : var = expr
12913 : : ! Now do the component assignments
12914 : : #do over derived type components [%cmp]
12915 : : #if (cmp is a pointer of any kind)
12916 : : continue
12917 : : build the assignment
12918 : : resolve the code
12919 : : #if the code is a typebound assignment
12920 : : #if (arg1 is INOUT or finalizable OUT && !t1)
12921 : : t1 = var
12922 : : arg1 = t1
12923 : : deal with allocatation or not of var and this component
12924 : : #elseif the code is an assignment by itself
12925 : : #if this component does not need finalization
12926 : : delete code and continue
12927 : : #else
12928 : : remove the leading assignment
12929 : : #endif
12930 : : commit the code
12931 : : #if (t1 and (arg1 is INOUT or finalizable OUT))
12932 : : var%cmp = t1%cmp
12933 : : #enddo
12934 : : put all code chunks involving t1 to the top of the generated code
12935 : : insert the generated block in place of the original code
12936 : : */
12937 : :
12938 : : static bool
12939 : 381 : is_finalizable_type (gfc_typespec ts)
12940 : : {
12941 : 381 : gfc_component *c;
12942 : :
12943 : 381 : if (ts.type != BT_DERIVED)
12944 : : return false;
12945 : :
12946 : : /* (1) Check for FINAL subroutines. */
12947 : 381 : if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
12948 : : return true;
12949 : :
12950 : : /* (2) Check for components of finalizable type. */
12951 : 809 : for (c = ts.u.derived->components; c; c = c->next)
12952 : 470 : if (c->ts.type == BT_DERIVED
12953 : 243 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
12954 : 242 : && c->ts.u.derived->f2k_derived
12955 : 242 : && c->ts.u.derived->f2k_derived->finalizers)
12956 : : return true;
12957 : :
12958 : : return false;
12959 : : }
12960 : :
12961 : : /* The temporary assignments have to be put on top of the additional
12962 : : code to avoid the result being changed by the intrinsic assignment.
12963 : : */
12964 : : static int component_assignment_level = 0;
12965 : : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
12966 : : static bool finalizable_comp;
12967 : :
12968 : : static void
12969 : 188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
12970 : : {
12971 : 188 : gfc_component *comp1, *comp2;
12972 : 188 : gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
12973 : 188 : gfc_code *tmp_code = NULL;
12974 : 188 : gfc_expr *t1 = NULL;
12975 : 188 : gfc_expr *tmp_expr = NULL;
12976 : 188 : int error_count, depth;
12977 : 188 : bool finalizable_lhs;
12978 : :
12979 : 188 : gfc_get_errors (NULL, &error_count);
12980 : :
12981 : : /* Filter out continuing processing after an error. */
12982 : 188 : if (error_count
12983 : 188 : || (*code)->expr1->ts.type != BT_DERIVED
12984 : 188 : || (*code)->expr2->ts.type != BT_DERIVED)
12985 : 140 : return;
12986 : :
12987 : : /* TODO: Handle more than one part array reference in assignments. */
12988 : 188 : depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
12989 : 188 : (*code)->expr1->rank ? 1 : 0);
12990 : 188 : if (depth > 1)
12991 : : {
12992 : 6 : gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
12993 : : "done because multiple part array references would "
12994 : : "occur in intermediate expressions.", &(*code)->loc);
12995 : 6 : return;
12996 : : }
12997 : :
12998 : 182 : if (!component_assignment_level)
12999 : 134 : finalizable_comp = true;
13000 : :
13001 : : /* Build a block so that function result temporaries are finalized
13002 : : locally on exiting the rather than enclosing scope. */
13003 : 182 : if (!component_assignment_level)
13004 : : {
13005 : 134 : ns = gfc_build_block_ns (ns);
13006 : 134 : tmp_code = gfc_get_code (EXEC_NOP);
13007 : 134 : *tmp_code = **code;
13008 : 134 : tmp_code->next = NULL;
13009 : 134 : (*code)->op = EXEC_BLOCK;
13010 : 134 : (*code)->ext.block.ns = ns;
13011 : 134 : (*code)->ext.block.assoc = NULL;
13012 : 134 : (*code)->expr1 = (*code)->expr2 = NULL;
13013 : 134 : ns->code = tmp_code;
13014 : 134 : code = &ns->code;
13015 : : }
13016 : :
13017 : 182 : component_assignment_level++;
13018 : :
13019 : 182 : finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
13020 : :
13021 : : /* Create a temporary so that functions get called only once. */
13022 : 182 : if ((*code)->expr2->expr_type != EXPR_VARIABLE
13023 : 182 : && (*code)->expr2->expr_type != EXPR_CONSTANT)
13024 : : {
13025 : : /* Assign the rhs to the temporary. */
13026 : 81 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13027 : 81 : if (tmp_expr->symtree->n.sym->attr.pointer)
13028 : : {
13029 : : /* Use allocate on assignment for the sake of simplicity. The
13030 : : temporary must not take on the optional attribute. Assume
13031 : : that the assignment is guarded by a PRESENT condition if the
13032 : : lhs is optional. */
13033 : 25 : tmp_expr->symtree->n.sym->attr.pointer = 0;
13034 : 25 : tmp_expr->symtree->n.sym->attr.optional = 0;
13035 : 25 : tmp_expr->symtree->n.sym->attr.allocatable = 1;
13036 : : }
13037 : 162 : this_code = build_assignment (EXEC_ASSIGN,
13038 : : tmp_expr, (*code)->expr2,
13039 : 81 : NULL, NULL, (*code)->loc);
13040 : 81 : this_code->expr2->must_finalize = 1;
13041 : : /* Add the code and substitute the rhs expression. */
13042 : 81 : add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
13043 : 81 : gfc_free_expr ((*code)->expr2);
13044 : 81 : (*code)->expr2 = tmp_expr;
13045 : : }
13046 : :
13047 : : /* Do the intrinsic assignment. This is not needed if the lhs is one
13048 : : of the temporaries generated here, since the intrinsic assignment
13049 : : to the final result already does this. */
13050 : 182 : if ((*code)->expr1->symtree->n.sym->name[2] != '.')
13051 : : {
13052 : 182 : if (finalizable_lhs)
13053 : 18 : (*code)->expr1->must_finalize = 1;
13054 : 182 : this_code = build_assignment (EXEC_ASSIGN,
13055 : : (*code)->expr1, (*code)->expr2,
13056 : : NULL, NULL, (*code)->loc);
13057 : 182 : add_code_to_chain (&this_code, &head, &tail);
13058 : : }
13059 : :
13060 : 182 : comp1 = (*code)->expr1->ts.u.derived->components;
13061 : 182 : comp2 = (*code)->expr2->ts.u.derived->components;
13062 : :
13063 : 449 : for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
13064 : : {
13065 : 267 : bool inout = false;
13066 : 267 : bool finalizable_out = false;
13067 : :
13068 : : /* The intrinsic assignment does the right thing for pointers
13069 : : of all kinds and allocatable components. */
13070 : 267 : if (!gfc_bt_struct (comp1->ts.type)
13071 : 200 : || comp1->attr.pointer
13072 : 200 : || comp1->attr.allocatable
13073 : 199 : || comp1->attr.proc_pointer_comp
13074 : 199 : || comp1->attr.class_pointer
13075 : 199 : || comp1->attr.proc_pointer)
13076 : 68 : continue;
13077 : :
13078 : 398 : finalizable_comp = is_finalizable_type (comp1->ts)
13079 : 199 : && !finalizable_lhs;
13080 : :
13081 : : /* Make an assignment for this component. */
13082 : 398 : this_code = build_assignment (EXEC_ASSIGN,
13083 : : (*code)->expr1, (*code)->expr2,
13084 : 199 : comp1, comp2, (*code)->loc);
13085 : :
13086 : : /* Convert the assignment if there is a defined assignment for
13087 : : this type. Otherwise, using the call from gfc_resolve_code,
13088 : : recurse into its components. */
13089 : 199 : gfc_resolve_code (this_code, ns);
13090 : :
13091 : 199 : if (this_code->op == EXEC_ASSIGN_CALL)
13092 : : {
13093 : 144 : gfc_formal_arglist *dummy_args;
13094 : 144 : gfc_symbol *rsym;
13095 : : /* Check that there is a typebound defined assignment. If not,
13096 : : then this must be a module defined assignment. We cannot
13097 : : use the defined_assign_comp attribute here because it must
13098 : : be this derived type that has the defined assignment and not
13099 : : a parent type. */
13100 : 144 : if (!(comp1->ts.u.derived->f2k_derived
13101 : : && comp1->ts.u.derived->f2k_derived
13102 : 144 : ->tb_op[INTRINSIC_ASSIGN]))
13103 : : {
13104 : 1 : gfc_free_statements (this_code);
13105 : 1 : this_code = NULL;
13106 : 1 : continue;
13107 : : }
13108 : :
13109 : : /* If the first argument of the subroutine has intent INOUT
13110 : : a temporary must be generated and used instead. */
13111 : 143 : rsym = this_code->resolved_sym;
13112 : 143 : dummy_args = gfc_sym_get_dummy_args (rsym);
13113 : 268 : finalizable_out = gfc_may_be_finalized (comp1->ts)
13114 : 18 : && dummy_args
13115 : 161 : && dummy_args->sym->attr.intent == INTENT_OUT;
13116 : 286 : inout = dummy_args
13117 : 268 : && dummy_args->sym->attr.intent == INTENT_INOUT;
13118 : 72 : if ((inout || finalizable_out)
13119 : 89 : && !comp1->attr.allocatable)
13120 : : {
13121 : 89 : gfc_code *temp_code;
13122 : 89 : inout = true;
13123 : :
13124 : : /* Build the temporary required for the assignment and put
13125 : : it at the head of the generated code. */
13126 : 89 : if (!t1)
13127 : : {
13128 : 89 : gfc_namespace *tmp_ns = ns;
13129 : 89 : if (ns->parent && gfc_may_be_finalized (comp1->ts))
13130 : 18 : tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13131 : 89 : t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13132 : 89 : t1->symtree->n.sym->attr.artificial = 1;
13133 : 178 : temp_code = build_assignment (EXEC_ASSIGN,
13134 : : t1, (*code)->expr1,
13135 : 89 : NULL, NULL, (*code)->loc);
13136 : :
13137 : : /* For allocatable LHS, check whether it is allocated. Note
13138 : : that allocatable components with defined assignment are
13139 : : not yet support. See PR 57696. */
13140 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13141 : : {
13142 : 24 : gfc_code *block;
13143 : 24 : gfc_expr *e =
13144 : 24 : gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13145 : 24 : block = gfc_get_code (EXEC_IF);
13146 : 24 : block->block = gfc_get_code (EXEC_IF);
13147 : 24 : block->block->expr1
13148 : 48 : = gfc_build_intrinsic_call (ns,
13149 : : GFC_ISYM_ALLOCATED, "allocated",
13150 : 24 : (*code)->loc, 1, e);
13151 : 24 : block->block->next = temp_code;
13152 : 24 : temp_code = block;
13153 : : }
13154 : 89 : add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13155 : : }
13156 : :
13157 : : /* Replace the first actual arg with the component of the
13158 : : temporary. */
13159 : 89 : gfc_free_expr (this_code->ext.actual->expr);
13160 : 89 : this_code->ext.actual->expr = gfc_copy_expr (t1);
13161 : 89 : add_comp_ref (this_code->ext.actual->expr, comp1);
13162 : :
13163 : : /* If the LHS variable is allocatable and wasn't allocated and
13164 : : the temporary is allocatable, pointer assign the address of
13165 : : the freshly allocated LHS to the temporary. */
13166 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable
13167 : 89 : && gfc_expr_attr ((*code)->expr1).allocatable)
13168 : : {
13169 : 18 : gfc_code *block;
13170 : 18 : gfc_expr *cond;
13171 : :
13172 : 18 : cond = gfc_get_expr ();
13173 : 18 : cond->ts.type = BT_LOGICAL;
13174 : 18 : cond->ts.kind = gfc_default_logical_kind;
13175 : 18 : cond->expr_type = EXPR_OP;
13176 : 18 : cond->where = (*code)->loc;
13177 : 18 : cond->value.op.op = INTRINSIC_NOT;
13178 : 18 : cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13179 : : GFC_ISYM_ALLOCATED, "allocated",
13180 : 18 : (*code)->loc, 1, gfc_copy_expr (t1));
13181 : 18 : block = gfc_get_code (EXEC_IF);
13182 : 18 : block->block = gfc_get_code (EXEC_IF);
13183 : 18 : block->block->expr1 = cond;
13184 : 36 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13185 : : t1, (*code)->expr1,
13186 : 18 : NULL, NULL, (*code)->loc);
13187 : 18 : add_code_to_chain (&block, &head, &tail);
13188 : : }
13189 : : }
13190 : : }
13191 : 55 : else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13192 : : {
13193 : : /* Don't add intrinsic assignments since they are already
13194 : : effected by the intrinsic assignment of the structure, unless
13195 : : finalization is required. */
13196 : 7 : if (finalizable_comp)
13197 : 0 : this_code->expr1->must_finalize = 1;
13198 : : else
13199 : : {
13200 : 7 : gfc_free_statements (this_code);
13201 : 7 : this_code = NULL;
13202 : 7 : continue;
13203 : : }
13204 : : }
13205 : : else
13206 : : {
13207 : : /* Resolution has expanded an assignment of a derived type with
13208 : : defined assigned components. Remove the redundant, leading
13209 : : assignment. */
13210 : 48 : gcc_assert (this_code->op == EXEC_ASSIGN);
13211 : 48 : gfc_code *tmp = this_code;
13212 : 48 : this_code = this_code->next;
13213 : 48 : tmp->next = NULL;
13214 : 48 : gfc_free_statements (tmp);
13215 : : }
13216 : :
13217 : 191 : add_code_to_chain (&this_code, &head, &tail);
13218 : :
13219 : 191 : if (t1 && (inout || finalizable_out))
13220 : : {
13221 : : /* Transfer the value to the final result. */
13222 : 178 : this_code = build_assignment (EXEC_ASSIGN,
13223 : : (*code)->expr1, t1,
13224 : 89 : comp1, comp2, (*code)->loc);
13225 : 89 : this_code->expr1->must_finalize = 0;
13226 : 89 : add_code_to_chain (&this_code, &head, &tail);
13227 : : }
13228 : : }
13229 : :
13230 : : /* Put the temporary assignments at the top of the generated code. */
13231 : 182 : if (tmp_head && component_assignment_level == 1)
13232 : : {
13233 : 126 : gfc_append_code (tmp_head, head);
13234 : 126 : head = tmp_head;
13235 : 126 : tmp_head = tmp_tail = NULL;
13236 : : }
13237 : :
13238 : : /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13239 : : not accidentally deallocated. Hence, nullify t1. */
13240 : 89 : if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13241 : 271 : && gfc_expr_attr ((*code)->expr1).allocatable)
13242 : : {
13243 : 18 : gfc_code *block;
13244 : 18 : gfc_expr *cond;
13245 : 18 : gfc_expr *e;
13246 : :
13247 : 18 : e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13248 : 18 : cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
13249 : 18 : (*code)->loc, 2, gfc_copy_expr (t1), e);
13250 : 18 : block = gfc_get_code (EXEC_IF);
13251 : 18 : block->block = gfc_get_code (EXEC_IF);
13252 : 18 : block->block->expr1 = cond;
13253 : 18 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13254 : : t1, gfc_get_null_expr (&(*code)->loc),
13255 : 18 : NULL, NULL, (*code)->loc);
13256 : 18 : gfc_append_code (tail, block);
13257 : 18 : tail = block;
13258 : : }
13259 : :
13260 : 182 : component_assignment_level--;
13261 : :
13262 : : /* Make an explicit final call for the function result. */
13263 : 182 : if (tmp_expr)
13264 : 81 : generate_final_call (tmp_expr, &head, &tail);
13265 : :
13266 : 182 : if (tmp_code)
13267 : : {
13268 : 134 : ns->code = head;
13269 : 134 : return;
13270 : : }
13271 : :
13272 : : /* Now attach the remaining code chain to the input code. Step on
13273 : : to the end of the new code since resolution is complete. */
13274 : 48 : gcc_assert ((*code)->op == EXEC_ASSIGN);
13275 : 48 : tail->next = (*code)->next;
13276 : : /* Overwrite 'code' because this would place the intrinsic assignment
13277 : : before the temporary for the lhs is created. */
13278 : 48 : gfc_free_expr ((*code)->expr1);
13279 : 48 : gfc_free_expr ((*code)->expr2);
13280 : 48 : **code = *head;
13281 : 48 : if (head != tail)
13282 : 48 : free (head);
13283 : 48 : *code = tail;
13284 : : }
13285 : :
13286 : :
13287 : : /* F2008: Pointer function assignments are of the form:
13288 : : ptr_fcn (args) = expr
13289 : : This function breaks these assignments into two statements:
13290 : : temporary_pointer => ptr_fcn(args)
13291 : : temporary_pointer = expr */
13292 : :
13293 : : static bool
13294 : 279231 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
13295 : : {
13296 : 279231 : gfc_expr *tmp_ptr_expr;
13297 : 279231 : gfc_code *this_code;
13298 : 279231 : gfc_component *comp;
13299 : 279231 : gfc_symbol *s;
13300 : :
13301 : 279231 : if ((*code)->expr1->expr_type != EXPR_FUNCTION)
13302 : : return false;
13303 : :
13304 : : /* Even if standard does not support this feature, continue to build
13305 : : the two statements to avoid upsetting frontend_passes.c. */
13306 : 205 : gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
13307 : : "%L", &(*code)->loc);
13308 : :
13309 : 205 : comp = gfc_get_proc_ptr_comp ((*code)->expr1);
13310 : :
13311 : 205 : if (comp)
13312 : 6 : s = comp->ts.interface;
13313 : : else
13314 : 199 : s = (*code)->expr1->symtree->n.sym;
13315 : :
13316 : 205 : if (s == NULL || !s->result->attr.pointer)
13317 : : {
13318 : 5 : gfc_error ("The function result on the lhs of the assignment at "
13319 : : "%L must have the pointer attribute.",
13320 : 5 : &(*code)->expr1->where);
13321 : 5 : (*code)->op = EXEC_NOP;
13322 : 5 : return false;
13323 : : }
13324 : :
13325 : 200 : tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
13326 : :
13327 : : /* get_temp_from_expression is set up for ordinary assignments. To that
13328 : : end, where array bounds are not known, arrays are made allocatable.
13329 : : Change the temporary to a pointer here. */
13330 : 200 : tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
13331 : 200 : tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
13332 : 200 : tmp_ptr_expr->where = (*code)->loc;
13333 : :
13334 : : /* A new charlen is required to ensure that the variable string length
13335 : : is different to that of the original lhs for deferred results. */
13336 : 200 : if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
13337 : : {
13338 : 60 : tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
13339 : 60 : tmp_ptr_expr->ts.deferred = 1;
13340 : 60 : tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
13341 : 60 : gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
13342 : 60 : tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
13343 : : }
13344 : :
13345 : 400 : this_code = build_assignment (EXEC_ASSIGN,
13346 : : tmp_ptr_expr, (*code)->expr2,
13347 : 200 : NULL, NULL, (*code)->loc);
13348 : 200 : this_code->next = (*code)->next;
13349 : 200 : (*code)->next = this_code;
13350 : 200 : (*code)->op = EXEC_POINTER_ASSIGN;
13351 : 200 : (*code)->expr2 = (*code)->expr1;
13352 : 200 : (*code)->expr1 = tmp_ptr_expr;
13353 : :
13354 : 200 : return true;
13355 : : }
13356 : :
13357 : :
13358 : : /* Deferred character length assignments from an operator expression
13359 : : require a temporary because the character length of the lhs can
13360 : : change in the course of the assignment. */
13361 : :
13362 : : static bool
13363 : 278236 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
13364 : : {
13365 : 278236 : gfc_expr *tmp_expr;
13366 : 278236 : gfc_code *this_code;
13367 : :
13368 : 278236 : if (!((*code)->expr1->ts.type == BT_CHARACTER
13369 : 25523 : && (*code)->expr1->ts.deferred && (*code)->expr1->rank
13370 : 740 : && (*code)->expr2->ts.type == BT_CHARACTER
13371 : 739 : && (*code)->expr2->expr_type == EXPR_OP))
13372 : : return false;
13373 : :
13374 : 34 : if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
13375 : : return false;
13376 : :
13377 : 28 : if (gfc_expr_attr ((*code)->expr1).pointer)
13378 : : return false;
13379 : :
13380 : 22 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13381 : 22 : tmp_expr->where = (*code)->loc;
13382 : :
13383 : : /* A new charlen is required to ensure that the variable string
13384 : : length is different to that of the original lhs. */
13385 : 22 : tmp_expr->ts.u.cl = gfc_get_charlen();
13386 : 22 : tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
13387 : 22 : tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
13388 : 22 : (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
13389 : :
13390 : 22 : tmp_expr->symtree->n.sym->ts.deferred = 1;
13391 : :
13392 : 22 : this_code = build_assignment (EXEC_ASSIGN,
13393 : 22 : (*code)->expr1,
13394 : : gfc_copy_expr (tmp_expr),
13395 : : NULL, NULL, (*code)->loc);
13396 : :
13397 : 22 : (*code)->expr1 = tmp_expr;
13398 : :
13399 : 22 : this_code->next = (*code)->next;
13400 : 22 : (*code)->next = this_code;
13401 : :
13402 : 22 : return true;
13403 : : }
13404 : :
13405 : :
13406 : : /* Given a block of code, recursively resolve everything pointed to by this
13407 : : code block. */
13408 : :
13409 : : void
13410 : 654469 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
13411 : : {
13412 : 654469 : int omp_workshare_save;
13413 : 654469 : int forall_save, do_concurrent_save;
13414 : 654469 : code_stack frame;
13415 : 654469 : bool t;
13416 : :
13417 : 654469 : frame.prev = cs_base;
13418 : 654469 : frame.head = code;
13419 : 654469 : cs_base = &frame;
13420 : :
13421 : 654469 : find_reachable_labels (code);
13422 : :
13423 : 1756414 : for (; code; code = code->next)
13424 : : {
13425 : 1101946 : frame.current = code;
13426 : 1101946 : forall_save = forall_flag;
13427 : 1101946 : do_concurrent_save = gfc_do_concurrent_flag;
13428 : :
13429 : 1101946 : if (code->op == EXEC_FORALL)
13430 : : {
13431 : 1990 : forall_flag = 1;
13432 : 1990 : gfc_resolve_forall (code, ns, forall_save);
13433 : 1990 : forall_flag = 2;
13434 : : }
13435 : 1099956 : else if (code->op == EXEC_OMP_METADIRECTIVE)
13436 : 109 : for (gfc_omp_variant *variant
13437 : : = code->ext.omp_variants;
13438 : 359 : variant; variant = variant->next)
13439 : 250 : gfc_resolve_code (variant->code, ns);
13440 : 1099847 : else if (code->block)
13441 : : {
13442 : 320663 : omp_workshare_save = -1;
13443 : 320663 : switch (code->op)
13444 : : {
13445 : 10100 : case EXEC_OACC_PARALLEL_LOOP:
13446 : 10100 : case EXEC_OACC_PARALLEL:
13447 : 10100 : case EXEC_OACC_KERNELS_LOOP:
13448 : 10100 : case EXEC_OACC_KERNELS:
13449 : 10100 : case EXEC_OACC_SERIAL_LOOP:
13450 : 10100 : case EXEC_OACC_SERIAL:
13451 : 10100 : case EXEC_OACC_DATA:
13452 : 10100 : case EXEC_OACC_HOST_DATA:
13453 : 10100 : case EXEC_OACC_LOOP:
13454 : 10100 : gfc_resolve_oacc_blocks (code, ns);
13455 : 10100 : break;
13456 : 54 : case EXEC_OMP_PARALLEL_WORKSHARE:
13457 : 54 : omp_workshare_save = omp_workshare_flag;
13458 : 54 : omp_workshare_flag = 1;
13459 : 54 : gfc_resolve_omp_parallel_blocks (code, ns);
13460 : 54 : break;
13461 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13462 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13463 : 5905 : case EXEC_OMP_MASKED_TASKLOOP:
13464 : 5905 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13465 : 5905 : case EXEC_OMP_MASTER_TASKLOOP:
13466 : 5905 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13467 : 5905 : case EXEC_OMP_PARALLEL:
13468 : 5905 : case EXEC_OMP_PARALLEL_DO:
13469 : 5905 : case EXEC_OMP_PARALLEL_DO_SIMD:
13470 : 5905 : case EXEC_OMP_PARALLEL_LOOP:
13471 : 5905 : case EXEC_OMP_PARALLEL_MASKED:
13472 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13473 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13474 : 5905 : case EXEC_OMP_PARALLEL_MASTER:
13475 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13476 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13477 : 5905 : case EXEC_OMP_PARALLEL_SECTIONS:
13478 : 5905 : case EXEC_OMP_TARGET_PARALLEL:
13479 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO:
13480 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13481 : 5905 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
13482 : 5905 : case EXEC_OMP_TARGET_TEAMS:
13483 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13484 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13485 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13486 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13487 : 5905 : case EXEC_OMP_TARGET_TEAMS_LOOP:
13488 : 5905 : case EXEC_OMP_TASK:
13489 : 5905 : case EXEC_OMP_TASKLOOP:
13490 : 5905 : case EXEC_OMP_TASKLOOP_SIMD:
13491 : 5905 : case EXEC_OMP_TEAMS:
13492 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE:
13493 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13494 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13495 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13496 : 5905 : case EXEC_OMP_TEAMS_LOOP:
13497 : 5905 : omp_workshare_save = omp_workshare_flag;
13498 : 5905 : omp_workshare_flag = 0;
13499 : 5905 : gfc_resolve_omp_parallel_blocks (code, ns);
13500 : 5905 : break;
13501 : 3039 : case EXEC_OMP_DISTRIBUTE:
13502 : 3039 : case EXEC_OMP_DISTRIBUTE_SIMD:
13503 : 3039 : case EXEC_OMP_DO:
13504 : 3039 : case EXEC_OMP_DO_SIMD:
13505 : 3039 : case EXEC_OMP_LOOP:
13506 : 3039 : case EXEC_OMP_SIMD:
13507 : 3039 : case EXEC_OMP_TARGET_SIMD:
13508 : 3039 : case EXEC_OMP_TILE:
13509 : 3039 : case EXEC_OMP_UNROLL:
13510 : 3039 : gfc_resolve_omp_do_blocks (code, ns);
13511 : 3039 : break;
13512 : : case EXEC_SELECT_TYPE:
13513 : : case EXEC_SELECT_RANK:
13514 : : /* Blocks are handled in resolve_select_type/rank because we
13515 : : have to transform the SELECT TYPE into ASSOCIATE first. */
13516 : : break;
13517 : 171 : case EXEC_DO_CONCURRENT:
13518 : 171 : gfc_do_concurrent_flag = 1;
13519 : 171 : gfc_resolve_blocks (code->block, ns);
13520 : 171 : gfc_do_concurrent_flag = 2;
13521 : 171 : break;
13522 : 39 : case EXEC_OMP_WORKSHARE:
13523 : 39 : omp_workshare_save = omp_workshare_flag;
13524 : 39 : omp_workshare_flag = 1;
13525 : : /* FALL THROUGH */
13526 : 297519 : default:
13527 : 297519 : gfc_resolve_blocks (code->block, ns);
13528 : 297519 : break;
13529 : : }
13530 : :
13531 : 316788 : if (omp_workshare_save != -1)
13532 : 5998 : omp_workshare_flag = omp_workshare_save;
13533 : : }
13534 : 779184 : start:
13535 : 1102151 : t = true;
13536 : 1102151 : if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
13537 : 1100769 : t = gfc_resolve_expr (code->expr1);
13538 : :
13539 : 1102151 : forall_flag = forall_save;
13540 : 1102151 : gfc_do_concurrent_flag = do_concurrent_save;
13541 : :
13542 : 1102151 : if (!gfc_resolve_expr (code->expr2))
13543 : 586 : t = false;
13544 : :
13545 : 1102151 : if (code->op == EXEC_ALLOCATE
13546 : 1102151 : && !gfc_resolve_expr (code->expr3))
13547 : : t = false;
13548 : :
13549 : 1102151 : switch (code->op)
13550 : : {
13551 : : case EXEC_NOP:
13552 : : case EXEC_END_BLOCK:
13553 : : case EXEC_END_NESTED_BLOCK:
13554 : : case EXEC_CYCLE:
13555 : : case EXEC_PAUSE:
13556 : : break;
13557 : :
13558 : 211525 : case EXEC_STOP:
13559 : 211525 : case EXEC_ERROR_STOP:
13560 : 211525 : if (code->expr2 != NULL
13561 : 37 : && (code->expr2->ts.type != BT_LOGICAL
13562 : 37 : || code->expr2->rank != 0))
13563 : 0 : gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
13564 : : &code->expr2->where);
13565 : : break;
13566 : :
13567 : : case EXEC_EXIT:
13568 : : case EXEC_CONTINUE:
13569 : : case EXEC_DT_END:
13570 : : case EXEC_ASSIGN_CALL:
13571 : : break;
13572 : :
13573 : 50 : case EXEC_CRITICAL:
13574 : 50 : resolve_critical (code);
13575 : 50 : break;
13576 : :
13577 : 747 : case EXEC_SYNC_ALL:
13578 : 747 : case EXEC_SYNC_IMAGES:
13579 : 747 : case EXEC_SYNC_MEMORY:
13580 : 747 : resolve_sync (code);
13581 : 747 : break;
13582 : :
13583 : 136 : case EXEC_LOCK:
13584 : 136 : case EXEC_UNLOCK:
13585 : 136 : case EXEC_EVENT_POST:
13586 : 136 : case EXEC_EVENT_WAIT:
13587 : 136 : resolve_lock_unlock_event (code);
13588 : 136 : break;
13589 : :
13590 : : case EXEC_FAIL_IMAGE:
13591 : : break;
13592 : :
13593 : 107 : case EXEC_FORM_TEAM:
13594 : 107 : resolve_form_team (code);
13595 : 107 : break;
13596 : :
13597 : 66 : case EXEC_CHANGE_TEAM:
13598 : 66 : resolve_change_team (code);
13599 : 66 : break;
13600 : :
13601 : 64 : case EXEC_END_TEAM:
13602 : 64 : resolve_end_team (code);
13603 : 64 : break;
13604 : :
13605 : 22 : case EXEC_SYNC_TEAM:
13606 : 22 : resolve_sync_team (code);
13607 : 22 : break;
13608 : :
13609 : 1420 : case EXEC_ENTRY:
13610 : : /* Keep track of which entry we are up to. */
13611 : 1420 : current_entry_id = code->ext.entry->id;
13612 : 1420 : break;
13613 : :
13614 : 450 : case EXEC_WHERE:
13615 : 450 : resolve_where (code, NULL);
13616 : 450 : break;
13617 : :
13618 : 1249 : case EXEC_GOTO:
13619 : 1249 : if (code->expr1 != NULL)
13620 : : {
13621 : 78 : if (code->expr1->expr_type != EXPR_VARIABLE
13622 : 76 : || code->expr1->ts.type != BT_INTEGER
13623 : 76 : || (code->expr1->ref
13624 : 1 : && code->expr1->ref->type == REF_ARRAY)
13625 : 75 : || code->expr1->symtree == NULL
13626 : 75 : || (code->expr1->symtree->n.sym
13627 : 75 : && (code->expr1->symtree->n.sym->attr.flavor
13628 : 75 : == FL_PARAMETER)))
13629 : 4 : gfc_error ("ASSIGNED GOTO statement at %L requires a "
13630 : : "scalar INTEGER variable", &code->expr1->where);
13631 : 74 : else if (code->expr1->symtree->n.sym
13632 : 74 : && code->expr1->symtree->n.sym->attr.assign != 1)
13633 : 1 : gfc_error ("Variable %qs has not been assigned a target "
13634 : : "label at %L", code->expr1->symtree->n.sym->name,
13635 : : &code->expr1->where);
13636 : : }
13637 : : else
13638 : 1171 : resolve_branch (code->label1, code);
13639 : : break;
13640 : :
13641 : 3096 : case EXEC_RETURN:
13642 : 3096 : if (code->expr1 != NULL
13643 : 53 : && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13644 : 1 : gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
13645 : : "INTEGER return specifier", &code->expr1->where);
13646 : : break;
13647 : :
13648 : : case EXEC_INIT_ASSIGN:
13649 : : case EXEC_END_PROCEDURE:
13650 : : break;
13651 : :
13652 : 280241 : case EXEC_ASSIGN:
13653 : 280241 : if (!t)
13654 : : break;
13655 : :
13656 : 279621 : if (flag_coarray == GFC_FCOARRAY_LIB
13657 : 279621 : && gfc_is_coindexed (code->expr1))
13658 : : {
13659 : : /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
13660 : : coindexed variable. */
13661 : 390 : code->op = EXEC_CALL;
13662 : 390 : gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
13663 : : true);
13664 : 390 : code->resolved_sym = code->symtree->n.sym;
13665 : 390 : code->resolved_sym->attr.flavor = FL_PROCEDURE;
13666 : 390 : code->resolved_sym->attr.intrinsic = 1;
13667 : 390 : code->resolved_sym->attr.subroutine = 1;
13668 : 390 : code->resolved_isym
13669 : 390 : = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
13670 : 390 : gfc_commit_symbol (code->resolved_sym);
13671 : 390 : code->ext.actual = gfc_get_actual_arglist ();
13672 : 390 : code->ext.actual->expr = code->expr1;
13673 : 390 : code->ext.actual->next = gfc_get_actual_arglist ();
13674 : 390 : code->ext.actual->next->expr = code->expr2;
13675 : :
13676 : 390 : code->expr1 = NULL;
13677 : 390 : code->expr2 = NULL;
13678 : 390 : break;
13679 : : }
13680 : :
13681 : 279231 : if (code->expr1->ts.type == BT_CLASS)
13682 : 971 : gfc_find_vtab (&code->expr2->ts);
13683 : :
13684 : : /* If this is a pointer function in an lvalue variable context,
13685 : : the new code will have to be resolved afresh. This is also the
13686 : : case with an error, where the code is transformed into NOP to
13687 : : prevent ICEs downstream. */
13688 : 279231 : if (resolve_ptr_fcn_assign (&code, ns)
13689 : 279231 : || code->op == EXEC_NOP)
13690 : 205 : goto start;
13691 : :
13692 : 279026 : if (!gfc_check_vardef_context (code->expr1, false, false, false,
13693 : 279026 : _("assignment")))
13694 : : break;
13695 : :
13696 : 278988 : if (resolve_ordinary_assign (code, ns))
13697 : : {
13698 : 752 : if (omp_workshare_flag)
13699 : : {
13700 : 1 : gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
13701 : 1 : "at %L", &code->loc);
13702 : 1 : break;
13703 : : }
13704 : 751 : if (code->op == EXEC_COMPCALL)
13705 : 416 : goto compcall;
13706 : : else
13707 : 335 : goto call;
13708 : : }
13709 : :
13710 : : /* Check for dependencies in deferred character length array
13711 : : assignments and generate a temporary, if necessary. */
13712 : 278236 : if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
13713 : : break;
13714 : :
13715 : : /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
13716 : 278214 : if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
13717 : 6562 : && code->expr1->ts.u.derived
13718 : 6562 : && code->expr1->ts.u.derived->attr.defined_assign_comp)
13719 : 188 : generate_component_assignments (&code, ns);
13720 : 278026 : else if (code->op == EXEC_ASSIGN)
13721 : : {
13722 : 278026 : if (gfc_may_be_finalized (code->expr1->ts))
13723 : 1079 : code->expr1->must_finalize = 1;
13724 : 278026 : if (code->expr2->expr_type == EXPR_ARRAY
13725 : 278026 : && gfc_may_be_finalized (code->expr2->ts))
13726 : 43 : code->expr2->must_finalize = 1;
13727 : : }
13728 : :
13729 : : break;
13730 : :
13731 : 126 : case EXEC_LABEL_ASSIGN:
13732 : 126 : if (code->label1->defined == ST_LABEL_UNKNOWN)
13733 : 0 : gfc_error ("Label %d referenced at %L is never defined",
13734 : : code->label1->value, &code->label1->where);
13735 : 126 : if (t
13736 : 126 : && (code->expr1->expr_type != EXPR_VARIABLE
13737 : 126 : || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
13738 : 126 : || code->expr1->symtree->n.sym->ts.kind
13739 : 126 : != gfc_default_integer_kind
13740 : 126 : || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
13741 : 125 : || code->expr1->symtree->n.sym->as != NULL))
13742 : 2 : gfc_error ("ASSIGN statement at %L requires a scalar "
13743 : : "default INTEGER variable", &code->expr1->where);
13744 : : break;
13745 : :
13746 : 10175 : case EXEC_POINTER_ASSIGN:
13747 : 10175 : {
13748 : 10175 : gfc_expr* e;
13749 : :
13750 : 10175 : if (!t)
13751 : : break;
13752 : :
13753 : : /* This is both a variable definition and pointer assignment
13754 : : context, so check both of them. For rank remapping, a final
13755 : : array ref may be present on the LHS and fool gfc_expr_attr
13756 : : used in gfc_check_vardef_context. Remove it. */
13757 : 10170 : e = remove_last_array_ref (code->expr1);
13758 : 20340 : t = gfc_check_vardef_context (e, true, false, false,
13759 : 10170 : _("pointer assignment"));
13760 : 10170 : if (t)
13761 : 10149 : t = gfc_check_vardef_context (e, false, false, false,
13762 : 10149 : _("pointer assignment"));
13763 : 10170 : gfc_free_expr (e);
13764 : :
13765 : 1111981 : t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
13766 : :
13767 : 10036 : if (!t)
13768 : : break;
13769 : :
13770 : : /* Assigning a class object always is a regular assign. */
13771 : 10036 : if (code->expr2->ts.type == BT_CLASS
13772 : 553 : && code->expr1->ts.type == BT_CLASS
13773 : 468 : && CLASS_DATA (code->expr2)
13774 : 467 : && !CLASS_DATA (code->expr2)->attr.dimension
13775 : 10643 : && !(gfc_expr_attr (code->expr1).proc_pointer
13776 : 54 : && code->expr2->expr_type == EXPR_VARIABLE
13777 : 42 : && code->expr2->symtree->n.sym->attr.flavor
13778 : 42 : == FL_PROCEDURE))
13779 : 324 : code->op = EXEC_ASSIGN;
13780 : : break;
13781 : : }
13782 : :
13783 : 72 : case EXEC_ARITHMETIC_IF:
13784 : 72 : {
13785 : 72 : gfc_expr *e = code->expr1;
13786 : :
13787 : 72 : gfc_resolve_expr (e);
13788 : 72 : if (e->expr_type == EXPR_NULL)
13789 : 1 : gfc_error ("Invalid NULL at %L", &e->where);
13790 : :
13791 : 72 : if (t && (e->rank > 0
13792 : 68 : || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
13793 : 5 : gfc_error ("Arithmetic IF statement at %L requires a scalar "
13794 : : "REAL or INTEGER expression", &e->where);
13795 : :
13796 : 72 : resolve_branch (code->label1, code);
13797 : 72 : resolve_branch (code->label2, code);
13798 : 72 : resolve_branch (code->label3, code);
13799 : : }
13800 : 72 : break;
13801 : :
13802 : 224409 : case EXEC_IF:
13803 : 224409 : if (t && code->expr1 != NULL
13804 : 0 : && (code->expr1->ts.type != BT_LOGICAL
13805 : 0 : || code->expr1->rank != 0))
13806 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
13807 : : &code->expr1->where);
13808 : : break;
13809 : :
13810 : 77159 : case EXEC_CALL:
13811 : 77159 : call:
13812 : 77159 : resolve_call (code);
13813 : 77159 : break;
13814 : :
13815 : 1675 : case EXEC_COMPCALL:
13816 : 1675 : compcall:
13817 : 1675 : resolve_typebound_subroutine (code);
13818 : 1675 : break;
13819 : :
13820 : 123 : case EXEC_CALL_PPC:
13821 : 123 : resolve_ppc_call (code);
13822 : 123 : break;
13823 : :
13824 : 683 : case EXEC_SELECT:
13825 : : /* Select is complicated. Also, a SELECT construct could be
13826 : : a transformed computed GOTO. */
13827 : 683 : resolve_select (code, false);
13828 : 683 : break;
13829 : :
13830 : 2882 : case EXEC_SELECT_TYPE:
13831 : 2882 : resolve_select_type (code, ns);
13832 : 2882 : break;
13833 : :
13834 : 1018 : case EXEC_SELECT_RANK:
13835 : 1018 : resolve_select_rank (code, ns);
13836 : 1018 : break;
13837 : :
13838 : 7527 : case EXEC_BLOCK:
13839 : 7527 : resolve_block_construct (code);
13840 : 7527 : break;
13841 : :
13842 : 32245 : case EXEC_DO:
13843 : 32245 : if (code->ext.iterator != NULL)
13844 : : {
13845 : 32245 : gfc_iterator *iter = code->ext.iterator;
13846 : 32245 : if (gfc_resolve_iterator (iter, true, false))
13847 : 32231 : gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
13848 : : true);
13849 : : }
13850 : : break;
13851 : :
13852 : 524 : case EXEC_DO_WHILE:
13853 : 524 : if (code->expr1 == NULL)
13854 : 0 : gfc_internal_error ("gfc_resolve_code(): No expression on "
13855 : : "DO WHILE");
13856 : 524 : if (t
13857 : 524 : && (code->expr1->rank != 0
13858 : 524 : || code->expr1->ts.type != BT_LOGICAL))
13859 : 0 : gfc_error ("Exit condition of DO WHILE loop at %L must be "
13860 : : "a scalar LOGICAL expression", &code->expr1->where);
13861 : : break;
13862 : :
13863 : 13648 : case EXEC_ALLOCATE:
13864 : 13648 : if (t)
13865 : 13646 : resolve_allocate_deallocate (code, "ALLOCATE");
13866 : :
13867 : : break;
13868 : :
13869 : 5700 : case EXEC_DEALLOCATE:
13870 : 5700 : if (t)
13871 : 5700 : resolve_allocate_deallocate (code, "DEALLOCATE");
13872 : :
13873 : : break;
13874 : :
13875 : 3873 : case EXEC_OPEN:
13876 : 3873 : if (!gfc_resolve_open (code->ext.open, &code->loc))
13877 : : break;
13878 : :
13879 : 3646 : resolve_branch (code->ext.open->err, code);
13880 : 3646 : break;
13881 : :
13882 : 3061 : case EXEC_CLOSE:
13883 : 3061 : if (!gfc_resolve_close (code->ext.close, &code->loc))
13884 : : break;
13885 : :
13886 : 3027 : resolve_branch (code->ext.close->err, code);
13887 : 3027 : break;
13888 : :
13889 : 2773 : case EXEC_BACKSPACE:
13890 : 2773 : case EXEC_ENDFILE:
13891 : 2773 : case EXEC_REWIND:
13892 : 2773 : case EXEC_FLUSH:
13893 : 2773 : if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
13894 : : break;
13895 : :
13896 : 2707 : resolve_branch (code->ext.filepos->err, code);
13897 : 2707 : break;
13898 : :
13899 : 817 : case EXEC_INQUIRE:
13900 : 817 : if (!gfc_resolve_inquire (code->ext.inquire))
13901 : : break;
13902 : :
13903 : 769 : resolve_branch (code->ext.inquire->err, code);
13904 : 769 : break;
13905 : :
13906 : 92 : case EXEC_IOLENGTH:
13907 : 92 : gcc_assert (code->ext.inquire != NULL);
13908 : 92 : if (!gfc_resolve_inquire (code->ext.inquire))
13909 : : break;
13910 : :
13911 : 90 : resolve_branch (code->ext.inquire->err, code);
13912 : 90 : break;
13913 : :
13914 : 89 : case EXEC_WAIT:
13915 : 89 : if (!gfc_resolve_wait (code->ext.wait))
13916 : : break;
13917 : :
13918 : 74 : resolve_branch (code->ext.wait->err, code);
13919 : 74 : resolve_branch (code->ext.wait->end, code);
13920 : 74 : resolve_branch (code->ext.wait->eor, code);
13921 : 74 : break;
13922 : :
13923 : 31443 : case EXEC_READ:
13924 : 31443 : case EXEC_WRITE:
13925 : 31443 : if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
13926 : : break;
13927 : :
13928 : 31134 : resolve_branch (code->ext.dt->err, code);
13929 : 31134 : resolve_branch (code->ext.dt->end, code);
13930 : 31134 : resolve_branch (code->ext.dt->eor, code);
13931 : 31134 : break;
13932 : :
13933 : 45093 : case EXEC_TRANSFER:
13934 : 45093 : resolve_transfer (code);
13935 : 45093 : break;
13936 : :
13937 : 2161 : case EXEC_DO_CONCURRENT:
13938 : 2161 : case EXEC_FORALL:
13939 : 2161 : resolve_forall_iterators (code->ext.concur.forall_iterator);
13940 : :
13941 : 2161 : if (code->expr1 != NULL
13942 : 730 : && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
13943 : 2 : gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
13944 : : "expression", &code->expr1->where);
13945 : :
13946 : 2161 : if (code->op == EXEC_DO_CONCURRENT)
13947 : 171 : resolve_locality_spec (code, ns);
13948 : : break;
13949 : :
13950 : 13093 : case EXEC_OACC_PARALLEL_LOOP:
13951 : 13093 : case EXEC_OACC_PARALLEL:
13952 : 13093 : case EXEC_OACC_KERNELS_LOOP:
13953 : 13093 : case EXEC_OACC_KERNELS:
13954 : 13093 : case EXEC_OACC_SERIAL_LOOP:
13955 : 13093 : case EXEC_OACC_SERIAL:
13956 : 13093 : case EXEC_OACC_DATA:
13957 : 13093 : case EXEC_OACC_HOST_DATA:
13958 : 13093 : case EXEC_OACC_LOOP:
13959 : 13093 : case EXEC_OACC_UPDATE:
13960 : 13093 : case EXEC_OACC_WAIT:
13961 : 13093 : case EXEC_OACC_CACHE:
13962 : 13093 : case EXEC_OACC_ENTER_DATA:
13963 : 13093 : case EXEC_OACC_EXIT_DATA:
13964 : 13093 : case EXEC_OACC_ATOMIC:
13965 : 13093 : case EXEC_OACC_DECLARE:
13966 : 13093 : gfc_resolve_oacc_directive (code, ns);
13967 : 13093 : break;
13968 : :
13969 : 16584 : case EXEC_OMP_ALLOCATE:
13970 : 16584 : case EXEC_OMP_ALLOCATORS:
13971 : 16584 : case EXEC_OMP_ASSUME:
13972 : 16584 : case EXEC_OMP_ATOMIC:
13973 : 16584 : case EXEC_OMP_BARRIER:
13974 : 16584 : case EXEC_OMP_CANCEL:
13975 : 16584 : case EXEC_OMP_CANCELLATION_POINT:
13976 : 16584 : case EXEC_OMP_CRITICAL:
13977 : 16584 : case EXEC_OMP_FLUSH:
13978 : 16584 : case EXEC_OMP_DEPOBJ:
13979 : 16584 : case EXEC_OMP_DISPATCH:
13980 : 16584 : case EXEC_OMP_DISTRIBUTE:
13981 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13982 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13983 : 16584 : case EXEC_OMP_DISTRIBUTE_SIMD:
13984 : 16584 : case EXEC_OMP_DO:
13985 : 16584 : case EXEC_OMP_DO_SIMD:
13986 : 16584 : case EXEC_OMP_ERROR:
13987 : 16584 : case EXEC_OMP_INTEROP:
13988 : 16584 : case EXEC_OMP_LOOP:
13989 : 16584 : case EXEC_OMP_MASTER:
13990 : 16584 : case EXEC_OMP_MASTER_TASKLOOP:
13991 : 16584 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13992 : 16584 : case EXEC_OMP_MASKED:
13993 : 16584 : case EXEC_OMP_MASKED_TASKLOOP:
13994 : 16584 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13995 : 16584 : case EXEC_OMP_METADIRECTIVE:
13996 : 16584 : case EXEC_OMP_ORDERED:
13997 : 16584 : case EXEC_OMP_SCAN:
13998 : 16584 : case EXEC_OMP_SCOPE:
13999 : 16584 : case EXEC_OMP_SECTIONS:
14000 : 16584 : case EXEC_OMP_SIMD:
14001 : 16584 : case EXEC_OMP_SINGLE:
14002 : 16584 : case EXEC_OMP_TARGET:
14003 : 16584 : case EXEC_OMP_TARGET_DATA:
14004 : 16584 : case EXEC_OMP_TARGET_ENTER_DATA:
14005 : 16584 : case EXEC_OMP_TARGET_EXIT_DATA:
14006 : 16584 : case EXEC_OMP_TARGET_PARALLEL:
14007 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO:
14008 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14009 : 16584 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14010 : 16584 : case EXEC_OMP_TARGET_SIMD:
14011 : 16584 : case EXEC_OMP_TARGET_TEAMS:
14012 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14013 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14014 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14015 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14016 : 16584 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14017 : 16584 : case EXEC_OMP_TARGET_UPDATE:
14018 : 16584 : case EXEC_OMP_TASK:
14019 : 16584 : case EXEC_OMP_TASKGROUP:
14020 : 16584 : case EXEC_OMP_TASKLOOP:
14021 : 16584 : case EXEC_OMP_TASKLOOP_SIMD:
14022 : 16584 : case EXEC_OMP_TASKWAIT:
14023 : 16584 : case EXEC_OMP_TASKYIELD:
14024 : 16584 : case EXEC_OMP_TEAMS:
14025 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14026 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14027 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14028 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14029 : 16584 : case EXEC_OMP_TEAMS_LOOP:
14030 : 16584 : case EXEC_OMP_TILE:
14031 : 16584 : case EXEC_OMP_UNROLL:
14032 : 16584 : case EXEC_OMP_WORKSHARE:
14033 : 16584 : gfc_resolve_omp_directive (code, ns);
14034 : 16584 : break;
14035 : :
14036 : 3833 : case EXEC_OMP_PARALLEL:
14037 : 3833 : case EXEC_OMP_PARALLEL_DO:
14038 : 3833 : case EXEC_OMP_PARALLEL_DO_SIMD:
14039 : 3833 : case EXEC_OMP_PARALLEL_LOOP:
14040 : 3833 : case EXEC_OMP_PARALLEL_MASKED:
14041 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14042 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14043 : 3833 : case EXEC_OMP_PARALLEL_MASTER:
14044 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14045 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14046 : 3833 : case EXEC_OMP_PARALLEL_SECTIONS:
14047 : 3833 : case EXEC_OMP_PARALLEL_WORKSHARE:
14048 : 3833 : omp_workshare_save = omp_workshare_flag;
14049 : 3833 : omp_workshare_flag = 0;
14050 : 3833 : gfc_resolve_omp_directive (code, ns);
14051 : 3833 : omp_workshare_flag = omp_workshare_save;
14052 : 3833 : break;
14053 : :
14054 : 0 : default:
14055 : 0 : gfc_internal_error ("gfc_resolve_code(): Bad statement code");
14056 : : }
14057 : : }
14058 : :
14059 : 654468 : cs_base = frame.prev;
14060 : 654468 : }
14061 : :
14062 : :
14063 : : /* Resolve initial values and make sure they are compatible with
14064 : : the variable. */
14065 : :
14066 : : static void
14067 : 1769614 : resolve_values (gfc_symbol *sym)
14068 : : {
14069 : 1769614 : bool t;
14070 : :
14071 : 1769614 : if (sym->value == NULL)
14072 : : return;
14073 : :
14074 : 401086 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
14075 : 4 : gfc_warning (OPT_Wdeprecated_declarations,
14076 : : "Using parameter %qs declared at %L is deprecated",
14077 : : sym->name, &sym->declared_at);
14078 : :
14079 : 401086 : if (sym->value->expr_type == EXPR_STRUCTURE)
14080 : 37677 : t= resolve_structure_cons (sym->value, 1);
14081 : : else
14082 : 363409 : t = gfc_resolve_expr (sym->value);
14083 : :
14084 : 401086 : if (!t)
14085 : : return;
14086 : :
14087 : 401084 : gfc_check_assign_symbol (sym, NULL, sym->value);
14088 : : }
14089 : :
14090 : :
14091 : : /* Verify any BIND(C) derived types in the namespace so we can report errors
14092 : : for them once, rather than for each variable declared of that type. */
14093 : :
14094 : : static void
14095 : 1742686 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14096 : : {
14097 : 1742686 : if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14098 : 78823 : && derived_sym->attr.is_bind_c == 1)
14099 : 26286 : verify_bind_c_derived_type (derived_sym);
14100 : :
14101 : 1742686 : return;
14102 : : }
14103 : :
14104 : :
14105 : : /* Check the interfaces of DTIO procedures associated with derived
14106 : : type 'sym'. These procedures can either have typebound bindings or
14107 : : can appear in DTIO generic interfaces. */
14108 : :
14109 : : static void
14110 : 1770582 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
14111 : : {
14112 : 1770582 : if (!sym || sym->attr.flavor != FL_DERIVED)
14113 : : return;
14114 : :
14115 : 87429 : gfc_check_dtio_interfaces (sym);
14116 : :
14117 : 87429 : return;
14118 : : }
14119 : :
14120 : : /* Verify that any binding labels used in a given namespace do not collide
14121 : : with the names or binding labels of any global symbols. Multiple INTERFACE
14122 : : for the same procedure are permitted. Abstract interfaces and dummy
14123 : : arguments are not checked. */
14124 : :
14125 : : static void
14126 : 1770582 : gfc_verify_binding_labels (gfc_symbol *sym)
14127 : : {
14128 : 1770582 : gfc_gsymbol *gsym;
14129 : 1770582 : const char *module;
14130 : :
14131 : 1770582 : if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
14132 : 57817 : || sym->attr.flavor == FL_DERIVED || !sym->binding_label
14133 : 30617 : || sym->attr.abstract || sym->attr.dummy)
14134 : : return;
14135 : :
14136 : 30519 : gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
14137 : :
14138 : 30519 : if (sym->module)
14139 : : module = sym->module;
14140 : 11083 : else if (sym->ns && sym->ns->proc_name
14141 : 11083 : && sym->ns->proc_name->attr.flavor == FL_MODULE)
14142 : 4429 : module = sym->ns->proc_name->name;
14143 : 6654 : else if (sym->ns && sym->ns->parent
14144 : 358 : && sym->ns && sym->ns->parent->proc_name
14145 : 358 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14146 : 272 : module = sym->ns->parent->proc_name->name;
14147 : : else
14148 : : module = NULL;
14149 : :
14150 : 30519 : if (!gsym
14151 : 10584 : || (!gsym->defined
14152 : 7758 : && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
14153 : : {
14154 : 19935 : if (!gsym)
14155 : 19935 : gsym = gfc_get_gsymbol (sym->binding_label, true);
14156 : 27693 : gsym->where = sym->declared_at;
14157 : 27693 : gsym->sym_name = sym->name;
14158 : 27693 : gsym->binding_label = sym->binding_label;
14159 : 27693 : gsym->ns = sym->ns;
14160 : 27693 : gsym->mod_name = module;
14161 : 27693 : if (sym->attr.function)
14162 : 19337 : gsym->type = GSYM_FUNCTION;
14163 : 8356 : else if (sym->attr.subroutine)
14164 : 8217 : gsym->type = GSYM_SUBROUTINE;
14165 : : /* Mark as variable/procedure as defined, unless its an INTERFACE. */
14166 : 27693 : gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
14167 : 27693 : return;
14168 : : }
14169 : :
14170 : 2826 : if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
14171 : : {
14172 : 1 : gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
14173 : : "identifier as entity at %L", sym->name,
14174 : : sym->binding_label, &sym->declared_at, &gsym->where);
14175 : : /* Clear the binding label to prevent checking multiple times. */
14176 : 1 : sym->binding_label = NULL;
14177 : 1 : return;
14178 : : }
14179 : :
14180 : 2825 : if (sym->attr.flavor == FL_VARIABLE && module
14181 : 37 : && (strcmp (module, gsym->mod_name) != 0
14182 : 35 : || strcmp (sym->name, gsym->sym_name) != 0))
14183 : : {
14184 : : /* This can only happen if the variable is defined in a module - if it
14185 : : isn't the same module, reject it. */
14186 : 3 : gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
14187 : : "uses the same global identifier as entity at %L from module %qs",
14188 : : sym->name, module, sym->binding_label,
14189 : : &sym->declared_at, &gsym->where, gsym->mod_name);
14190 : 3 : sym->binding_label = NULL;
14191 : 3 : return;
14192 : : }
14193 : :
14194 : 2822 : if ((sym->attr.function || sym->attr.subroutine)
14195 : 2786 : && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
14196 : 2784 : || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
14197 : 2471 : && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
14198 : 2083 : && (module != gsym->mod_name
14199 : 2079 : || strcmp (gsym->sym_name, sym->name) != 0
14200 : 2079 : || (module && strcmp (module, gsym->mod_name) != 0)))
14201 : : {
14202 : : /* Print an error if the procedure is defined multiple times; we have to
14203 : : exclude references to the same procedure via module association or
14204 : : multiple checks for the same procedure. */
14205 : 4 : gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
14206 : : "global identifier as entity at %L", sym->name,
14207 : : sym->binding_label, &sym->declared_at, &gsym->where);
14208 : 4 : sym->binding_label = NULL;
14209 : : }
14210 : : }
14211 : :
14212 : :
14213 : : /* Resolve an index expression. */
14214 : :
14215 : : static bool
14216 : 260051 : resolve_index_expr (gfc_expr *e)
14217 : : {
14218 : 260051 : if (!gfc_resolve_expr (e))
14219 : : return false;
14220 : :
14221 : 260041 : if (!gfc_simplify_expr (e, 0))
14222 : : return false;
14223 : :
14224 : 260039 : if (!gfc_specification_expr (e))
14225 : : return false;
14226 : :
14227 : : return true;
14228 : : }
14229 : :
14230 : :
14231 : : /* Resolve a charlen structure. */
14232 : :
14233 : : static bool
14234 : 99792 : resolve_charlen (gfc_charlen *cl)
14235 : : {
14236 : 99792 : int k;
14237 : 99792 : bool saved_specification_expr;
14238 : :
14239 : 99792 : if (cl->resolved)
14240 : : return true;
14241 : :
14242 : 91657 : cl->resolved = 1;
14243 : 91657 : saved_specification_expr = specification_expr;
14244 : 91657 : specification_expr = true;
14245 : :
14246 : 91657 : if (cl->length_from_typespec)
14247 : : {
14248 : 1090 : if (!gfc_resolve_expr (cl->length))
14249 : : {
14250 : 1 : specification_expr = saved_specification_expr;
14251 : 1 : return false;
14252 : : }
14253 : :
14254 : 1089 : if (!gfc_simplify_expr (cl->length, 0))
14255 : : {
14256 : 0 : specification_expr = saved_specification_expr;
14257 : 0 : return false;
14258 : : }
14259 : :
14260 : : /* cl->length has been resolved. It should have an integer type. */
14261 : 1089 : if (cl->length
14262 : 1088 : && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
14263 : : {
14264 : 4 : gfc_error ("Scalar INTEGER expression expected at %L",
14265 : : &cl->length->where);
14266 : 4 : return false;
14267 : : }
14268 : : }
14269 : : else
14270 : : {
14271 : 90567 : if (!resolve_index_expr (cl->length))
14272 : : {
14273 : 19 : specification_expr = saved_specification_expr;
14274 : 19 : return false;
14275 : : }
14276 : : }
14277 : :
14278 : : /* F2008, 4.4.3.2: If the character length parameter value evaluates to
14279 : : a negative value, the length of character entities declared is zero. */
14280 : 91633 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14281 : 54707 : && mpz_sgn (cl->length->value.integer) < 0)
14282 : 0 : gfc_replace_expr (cl->length,
14283 : : gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
14284 : :
14285 : : /* Check that the character length is not too large. */
14286 : 91633 : k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
14287 : 91633 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14288 : 54707 : && cl->length->ts.type == BT_INTEGER
14289 : 54707 : && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
14290 : : {
14291 : 4 : gfc_error ("String length at %L is too large", &cl->length->where);
14292 : 4 : specification_expr = saved_specification_expr;
14293 : 4 : return false;
14294 : : }
14295 : :
14296 : 91629 : specification_expr = saved_specification_expr;
14297 : 91629 : return true;
14298 : : }
14299 : :
14300 : :
14301 : : /* Test for non-constant shape arrays. */
14302 : :
14303 : : static bool
14304 : 114725 : is_non_constant_shape_array (gfc_symbol *sym)
14305 : : {
14306 : 114725 : gfc_expr *e;
14307 : 114725 : int i;
14308 : 114725 : bool not_constant;
14309 : :
14310 : 114725 : not_constant = false;
14311 : 114725 : if (sym->as != NULL)
14312 : : {
14313 : : /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
14314 : : has not been simplified; parameter array references. Do the
14315 : : simplification now. */
14316 : 152288 : for (i = 0; i < sym->as->rank + sym->as->corank; i++)
14317 : : {
14318 : 88003 : if (i == GFC_MAX_DIMENSIONS)
14319 : : break;
14320 : :
14321 : 88001 : e = sym->as->lower[i];
14322 : 88001 : if (e && (!resolve_index_expr(e)
14323 : 85246 : || !gfc_is_constant_expr (e)))
14324 : : not_constant = true;
14325 : 88001 : e = sym->as->upper[i];
14326 : 88001 : if (e && (!resolve_index_expr(e)
14327 : 84210 : || !gfc_is_constant_expr (e)))
14328 : : not_constant = true;
14329 : : }
14330 : : }
14331 : 114725 : return not_constant;
14332 : : }
14333 : :
14334 : : /* Given a symbol and an initialization expression, add code to initialize
14335 : : the symbol to the function entry. */
14336 : : static void
14337 : 1906 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
14338 : : {
14339 : 1906 : gfc_expr *lval;
14340 : 1906 : gfc_code *init_st;
14341 : 1906 : gfc_namespace *ns = sym->ns;
14342 : :
14343 : : /* Search for the function namespace if this is a contained
14344 : : function without an explicit result. */
14345 : 1906 : if (sym->attr.function && sym == sym->result
14346 : 297 : && sym->name != sym->ns->proc_name->name)
14347 : : {
14348 : 296 : ns = ns->contained;
14349 : 1353 : for (;ns; ns = ns->sibling)
14350 : 1287 : if (strcmp (ns->proc_name->name, sym->name) == 0)
14351 : : break;
14352 : : }
14353 : :
14354 : 1906 : if (ns == NULL)
14355 : : {
14356 : 66 : gfc_free_expr (init);
14357 : 66 : return;
14358 : : }
14359 : :
14360 : : /* Build an l-value expression for the result. */
14361 : 1840 : lval = gfc_lval_expr_from_sym (sym);
14362 : :
14363 : : /* Add the code at scope entry. */
14364 : 1840 : init_st = gfc_get_code (EXEC_INIT_ASSIGN);
14365 : 1840 : init_st->next = ns->code;
14366 : 1840 : ns->code = init_st;
14367 : :
14368 : : /* Assign the default initializer to the l-value. */
14369 : 1840 : init_st->loc = sym->declared_at;
14370 : 1840 : init_st->expr1 = lval;
14371 : 1840 : init_st->expr2 = init;
14372 : : }
14373 : :
14374 : :
14375 : : /* Whether or not we can generate a default initializer for a symbol. */
14376 : :
14377 : : static bool
14378 : 28581 : can_generate_init (gfc_symbol *sym)
14379 : : {
14380 : 28581 : symbol_attribute *a;
14381 : 28581 : if (!sym)
14382 : : return false;
14383 : 28581 : a = &sym->attr;
14384 : :
14385 : : /* These symbols should never have a default initialization. */
14386 : 46828 : return !(
14387 : 28581 : a->allocatable
14388 : 28581 : || a->external
14389 : 27436 : || a->pointer
14390 : 27436 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
14391 : 5461 : && (CLASS_DATA (sym)->attr.class_pointer
14392 : 3574 : || CLASS_DATA (sym)->attr.proc_pointer))
14393 : 25549 : || a->in_equivalence
14394 : 25428 : || a->in_common
14395 : 25381 : || a->data
14396 : 25203 : || sym->module
14397 : 21470 : || a->cray_pointee
14398 : 21408 : || a->cray_pointer
14399 : 21408 : || sym->assoc
14400 : 18872 : || (!a->referenced && !a->result)
14401 : 18247 : || (a->dummy && (a->intent != INTENT_OUT
14402 : 1049 : || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
14403 : 18247 : || (a->function && sym != sym->result)
14404 : : );
14405 : : }
14406 : :
14407 : :
14408 : : /* Assign the default initializer to a derived type variable or result. */
14409 : :
14410 : : static void
14411 : 10777 : apply_default_init (gfc_symbol *sym)
14412 : : {
14413 : 10777 : gfc_expr *init = NULL;
14414 : :
14415 : 10777 : if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14416 : : return;
14417 : :
14418 : 10777 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
14419 : 9970 : init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14420 : :
14421 : 10777 : if (init == NULL && sym->ts.type != BT_CLASS)
14422 : : return;
14423 : :
14424 : 1524 : build_init_assign (sym, init);
14425 : 1524 : sym->attr.referenced = 1;
14426 : : }
14427 : :
14428 : :
14429 : : /* Build an initializer for a local. Returns null if the symbol should not have
14430 : : a default initialization. */
14431 : :
14432 : : static gfc_expr *
14433 : 198586 : build_default_init_expr (gfc_symbol *sym)
14434 : : {
14435 : : /* These symbols should never have a default initialization. */
14436 : 198586 : if (sym->attr.allocatable
14437 : 185367 : || sym->attr.external
14438 : 185367 : || sym->attr.dummy
14439 : 122065 : || sym->attr.pointer
14440 : 114081 : || sym->attr.in_equivalence
14441 : 111705 : || sym->attr.in_common
14442 : 108654 : || sym->attr.data
14443 : 106358 : || sym->module
14444 : 104027 : || sym->attr.cray_pointee
14445 : 103726 : || sym->attr.cray_pointer
14446 : 103424 : || sym->assoc)
14447 : : return NULL;
14448 : :
14449 : : /* Get the appropriate init expression. */
14450 : 99005 : return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
14451 : : }
14452 : :
14453 : : /* Add an initialization expression to a local variable. */
14454 : : static void
14455 : 198586 : apply_default_init_local (gfc_symbol *sym)
14456 : : {
14457 : 198586 : gfc_expr *init = NULL;
14458 : :
14459 : : /* The symbol should be a variable or a function return value. */
14460 : 198586 : if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14461 : 198586 : || (sym->attr.function && sym->result != sym))
14462 : : return;
14463 : :
14464 : : /* Try to build the initializer expression. If we can't initialize
14465 : : this symbol, then init will be NULL. */
14466 : 198586 : init = build_default_init_expr (sym);
14467 : 198586 : if (init == NULL)
14468 : : return;
14469 : :
14470 : : /* For saved variables, we don't want to add an initializer at function
14471 : : entry, so we just add a static initializer. Note that automatic variables
14472 : : are stack allocated even with -fno-automatic; we have also to exclude
14473 : : result variable, which are also nonstatic. */
14474 : 419 : if (!sym->attr.automatic
14475 : 419 : && (sym->attr.save || sym->ns->save_all
14476 : 377 : || (flag_max_stack_var_size == 0 && !sym->attr.result
14477 : 27 : && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
14478 : 14 : && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
14479 : : {
14480 : : /* Don't clobber an existing initializer! */
14481 : 37 : gcc_assert (sym->value == NULL);
14482 : 37 : sym->value = init;
14483 : 37 : return;
14484 : : }
14485 : :
14486 : 382 : build_init_assign (sym, init);
14487 : : }
14488 : :
14489 : :
14490 : : /* Resolution of common features of flavors variable and procedure. */
14491 : :
14492 : : static bool
14493 : 934235 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
14494 : : {
14495 : 934235 : gfc_array_spec *as;
14496 : :
14497 : 934235 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14498 : 18418 : && sym->ts.u.derived && CLASS_DATA (sym))
14499 : 18412 : as = CLASS_DATA (sym)->as;
14500 : : else
14501 : 915823 : as = sym->as;
14502 : :
14503 : : /* Constraints on deferred shape variable. */
14504 : 934235 : if (as == NULL || as->type != AS_DEFERRED)
14505 : : {
14506 : 910724 : bool pointer, allocatable, dimension;
14507 : :
14508 : 910724 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14509 : 15345 : && sym->ts.u.derived && CLASS_DATA (sym))
14510 : : {
14511 : 15339 : pointer = CLASS_DATA (sym)->attr.class_pointer;
14512 : 15339 : allocatable = CLASS_DATA (sym)->attr.allocatable;
14513 : 15339 : dimension = CLASS_DATA (sym)->attr.dimension;
14514 : : }
14515 : : else
14516 : : {
14517 : 895385 : pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
14518 : 895385 : allocatable = sym->attr.allocatable;
14519 : 895385 : dimension = sym->attr.dimension;
14520 : : }
14521 : :
14522 : 910724 : if (allocatable)
14523 : : {
14524 : 7797 : if (dimension
14525 : 7797 : && as
14526 : 523 : && as->type != AS_ASSUMED_RANK
14527 : 5 : && !sym->attr.select_rank_temporary)
14528 : : {
14529 : 3 : gfc_error ("Allocatable array %qs at %L must have a deferred "
14530 : : "shape or assumed rank", sym->name, &sym->declared_at);
14531 : 3 : return false;
14532 : : }
14533 : 7794 : else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
14534 : : "%qs at %L may not be ALLOCATABLE",
14535 : : sym->name, &sym->declared_at))
14536 : : return false;
14537 : : }
14538 : :
14539 : 910720 : if (pointer && dimension && as->type != AS_ASSUMED_RANK)
14540 : : {
14541 : 4 : gfc_error ("Array pointer %qs at %L must have a deferred shape or "
14542 : : "assumed rank", sym->name, &sym->declared_at);
14543 : 4 : sym->error = 1;
14544 : 4 : return false;
14545 : : }
14546 : : }
14547 : : else
14548 : : {
14549 : 23511 : if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
14550 : 4481 : && sym->ts.type != BT_CLASS && !sym->assoc)
14551 : : {
14552 : 3 : gfc_error ("Array %qs at %L cannot have a deferred shape",
14553 : : sym->name, &sym->declared_at);
14554 : 3 : return false;
14555 : : }
14556 : : }
14557 : :
14558 : : /* Constraints on polymorphic variables. */
14559 : 934224 : if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
14560 : : {
14561 : : /* F03:C502. */
14562 : 17758 : if (sym->attr.class_ok
14563 : 17702 : && sym->ts.u.derived
14564 : 17697 : && !sym->attr.select_type_temporary
14565 : 16659 : && !UNLIMITED_POLY (sym)
14566 : 14283 : && CLASS_DATA (sym)
14567 : 14282 : && CLASS_DATA (sym)->ts.u.derived
14568 : 32039 : && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
14569 : : {
14570 : 5 : gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
14571 : 5 : CLASS_DATA (sym)->ts.u.derived->name, sym->name,
14572 : : &sym->declared_at);
14573 : 5 : return false;
14574 : : }
14575 : :
14576 : : /* F03:C509. */
14577 : : /* Assume that use associated symbols were checked in the module ns.
14578 : : Class-variables that are associate-names are also something special
14579 : : and excepted from the test. */
14580 : 17753 : if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
14581 : 54 : && !sym->attr.select_type_temporary
14582 : 54 : && !sym->attr.select_rank_temporary)
14583 : : {
14584 : 54 : gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
14585 : : "or pointer", sym->name, &sym->declared_at);
14586 : 54 : return false;
14587 : : }
14588 : : }
14589 : :
14590 : : return true;
14591 : : }
14592 : :
14593 : :
14594 : : /* Additional checks for symbols with flavor variable and derived
14595 : : type. To be called from resolve_fl_variable. */
14596 : :
14597 : : static bool
14598 : 78587 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
14599 : : {
14600 : 78587 : gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
14601 : :
14602 : : /* Check to see if a derived type is blocked from being host
14603 : : associated by the presence of another class I symbol in the same
14604 : : namespace. 14.6.1.3 of the standard and the discussion on
14605 : : comp.lang.fortran. */
14606 : 78587 : if (sym->ts.u.derived
14607 : 78582 : && sym->ns != sym->ts.u.derived->ns
14608 : 45140 : && !sym->ts.u.derived->attr.use_assoc
14609 : 16391 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
14610 : : {
14611 : 15488 : gfc_symbol *s;
14612 : 15488 : gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
14613 : 15488 : if (s && s->attr.generic)
14614 : 0 : s = gfc_find_dt_in_generic (s);
14615 : 15488 : if (s && !gfc_fl_struct (s->attr.flavor))
14616 : : {
14617 : 2 : gfc_error ("The type %qs cannot be host associated at %L "
14618 : : "because it is blocked by an incompatible object "
14619 : : "of the same name declared at %L",
14620 : 2 : sym->ts.u.derived->name, &sym->declared_at,
14621 : : &s->declared_at);
14622 : 2 : return false;
14623 : : }
14624 : : }
14625 : :
14626 : : /* 4th constraint in section 11.3: "If an object of a type for which
14627 : : component-initialization is specified (R429) appears in the
14628 : : specification-part of a module and does not have the ALLOCATABLE
14629 : : or POINTER attribute, the object shall have the SAVE attribute."
14630 : :
14631 : : The check for initializers is performed with
14632 : : gfc_has_default_initializer because gfc_default_initializer generates
14633 : : a hidden default for allocatable components. */
14634 : 77994 : if (!(sym->value || no_init_flag) && sym->ns->proc_name
14635 : 17381 : && sym->ns->proc_name->attr.flavor == FL_MODULE
14636 : 348 : && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
14637 : 20 : && !sym->attr.pointer && !sym->attr.allocatable
14638 : 20 : && gfc_has_default_initializer (sym->ts.u.derived)
14639 : 78594 : && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
14640 : : "%qs at %L, needed due to the default "
14641 : : "initialization", sym->name, &sym->declared_at))
14642 : : return false;
14643 : :
14644 : : /* Assign default initializer. */
14645 : 78583 : if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
14646 : 72661 : && (!no_init_flag
14647 : 57049 : || (sym->attr.intent == INTENT_OUT
14648 : 3173 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
14649 : 18611 : sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14650 : :
14651 : : return true;
14652 : : }
14653 : :
14654 : :
14655 : : /* F2008, C402 (R401): A colon shall not be used as a type-param-value
14656 : : except in the declaration of an entity or component that has the POINTER
14657 : : or ALLOCATABLE attribute. */
14658 : :
14659 : : static bool
14660 : 1451368 : deferred_requirements (gfc_symbol *sym)
14661 : : {
14662 : 1451368 : if (sym->ts.deferred
14663 : 7588 : && !(sym->attr.pointer
14664 : 2271 : || sym->attr.allocatable
14665 : 92 : || sym->attr.associate_var
14666 : 7 : || sym->attr.omp_udr_artificial_var))
14667 : : {
14668 : : /* If a function has a result variable, only check the variable. */
14669 : 7 : if (sym->result && sym->name != sym->result->name)
14670 : : return true;
14671 : :
14672 : 6 : gfc_error ("Entity %qs at %L has a deferred type parameter and "
14673 : : "requires either the POINTER or ALLOCATABLE attribute",
14674 : : sym->name, &sym->declared_at);
14675 : 6 : return false;
14676 : : }
14677 : : return true;
14678 : : }
14679 : :
14680 : :
14681 : : /* Resolve symbols with flavor variable. */
14682 : :
14683 : : static bool
14684 : 624764 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
14685 : : {
14686 : 624764 : const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
14687 : : "SAVE attribute");
14688 : :
14689 : 624764 : if (!resolve_fl_var_and_proc (sym, mp_flag))
14690 : : return false;
14691 : :
14692 : : /* Set this flag to check that variables are parameters of all entries.
14693 : : This check is effected by the call to gfc_resolve_expr through
14694 : : is_non_constant_shape_array. */
14695 : 624704 : bool saved_specification_expr = specification_expr;
14696 : 624704 : specification_expr = true;
14697 : :
14698 : 624704 : if (sym->ns->proc_name
14699 : 624617 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
14700 : 619863 : || sym->ns->proc_name->attr.is_main_program)
14701 : 80065 : && !sym->attr.use_assoc
14702 : 77055 : && !sym->attr.allocatable
14703 : 71636 : && !sym->attr.pointer
14704 : 692770 : && is_non_constant_shape_array (sym))
14705 : : {
14706 : : /* F08:C541. The shape of an array defined in a main program or module
14707 : : * needs to be constant. */
14708 : 3 : gfc_error ("The module or main program array %qs at %L must "
14709 : : "have constant shape", sym->name, &sym->declared_at);
14710 : 3 : specification_expr = saved_specification_expr;
14711 : 3 : return false;
14712 : : }
14713 : :
14714 : : /* Constraints on deferred type parameter. */
14715 : 624701 : if (!deferred_requirements (sym))
14716 : : return false;
14717 : :
14718 : 624697 : if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
14719 : : {
14720 : : /* Make sure that character string variables with assumed length are
14721 : : dummy arguments. */
14722 : 34989 : gfc_expr *e = NULL;
14723 : :
14724 : 34989 : if (sym->ts.u.cl)
14725 : 34989 : e = sym->ts.u.cl->length;
14726 : : else
14727 : : return false;
14728 : :
14729 : 34989 : if (e == NULL && !sym->attr.dummy && !sym->attr.result
14730 : 2464 : && !sym->ts.deferred && !sym->attr.select_type_temporary
14731 : 2 : && !sym->attr.omp_udr_artificial_var)
14732 : : {
14733 : 2 : gfc_error ("Entity with assumed character length at %L must be a "
14734 : : "dummy argument or a PARAMETER", &sym->declared_at);
14735 : 2 : specification_expr = saved_specification_expr;
14736 : 2 : return false;
14737 : : }
14738 : :
14739 : 20285 : if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
14740 : : {
14741 : 1 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14742 : 1 : specification_expr = saved_specification_expr;
14743 : 1 : return false;
14744 : : }
14745 : :
14746 : 34986 : if (!gfc_is_constant_expr (e)
14747 : 34986 : && !(e->expr_type == EXPR_VARIABLE
14748 : 1388 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
14749 : : {
14750 : 2182 : if (!sym->attr.use_assoc && sym->ns->proc_name
14751 : 1678 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
14752 : 1677 : || sym->ns->proc_name->attr.is_main_program))
14753 : : {
14754 : 3 : gfc_error ("%qs at %L must have constant character length "
14755 : : "in this context", sym->name, &sym->declared_at);
14756 : 3 : specification_expr = saved_specification_expr;
14757 : 3 : return false;
14758 : : }
14759 : 2179 : if (sym->attr.in_common)
14760 : : {
14761 : 1 : gfc_error ("COMMON variable %qs at %L must have constant "
14762 : : "character length", sym->name, &sym->declared_at);
14763 : 1 : specification_expr = saved_specification_expr;
14764 : 1 : return false;
14765 : : }
14766 : : }
14767 : : }
14768 : :
14769 : 624690 : if (sym->value == NULL && sym->attr.referenced
14770 : 200457 : && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
14771 : 198586 : apply_default_init_local (sym); /* Try to apply a default initialization. */
14772 : :
14773 : : /* Determine if the symbol may not have an initializer. */
14774 : 624690 : int no_init_flag = 0, automatic_flag = 0;
14775 : 624690 : if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
14776 : 166301 : || sym->attr.intrinsic || sym->attr.result)
14777 : : no_init_flag = 1;
14778 : 134415 : else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
14779 : 168330 : && is_non_constant_shape_array (sym))
14780 : : {
14781 : 1329 : no_init_flag = automatic_flag = 1;
14782 : :
14783 : : /* Also, they must not have the SAVE attribute.
14784 : : SAVE_IMPLICIT is checked below. */
14785 : 1329 : if (sym->as && sym->attr.codimension)
14786 : : {
14787 : 7 : int corank = sym->as->corank;
14788 : 7 : sym->as->corank = 0;
14789 : 7 : no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
14790 : 7 : sym->as->corank = corank;
14791 : : }
14792 : 1329 : if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
14793 : : {
14794 : 2 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14795 : 2 : specification_expr = saved_specification_expr;
14796 : 2 : return false;
14797 : : }
14798 : : }
14799 : :
14800 : : /* Ensure that any initializer is simplified. */
14801 : 624688 : if (sym->value)
14802 : 7452 : gfc_simplify_expr (sym->value, 1);
14803 : :
14804 : : /* Reject illegal initializers. */
14805 : 624688 : if (!sym->mark && sym->value)
14806 : : {
14807 : 7452 : if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
14808 : 67 : && CLASS_DATA (sym)->attr.allocatable))
14809 : 1 : gfc_error ("Allocatable %qs at %L cannot have an initializer",
14810 : : sym->name, &sym->declared_at);
14811 : 7451 : else if (sym->attr.external)
14812 : 0 : gfc_error ("External %qs at %L cannot have an initializer",
14813 : : sym->name, &sym->declared_at);
14814 : 7451 : else if (sym->attr.dummy)
14815 : 3 : gfc_error ("Dummy %qs at %L cannot have an initializer",
14816 : : sym->name, &sym->declared_at);
14817 : 7448 : else if (sym->attr.intrinsic)
14818 : 0 : gfc_error ("Intrinsic %qs at %L cannot have an initializer",
14819 : : sym->name, &sym->declared_at);
14820 : 7448 : else if (sym->attr.result)
14821 : 1 : gfc_error ("Function result %qs at %L cannot have an initializer",
14822 : : sym->name, &sym->declared_at);
14823 : 7447 : else if (automatic_flag)
14824 : 5 : gfc_error ("Automatic array %qs at %L cannot have an initializer",
14825 : : sym->name, &sym->declared_at);
14826 : : else
14827 : 7442 : goto no_init_error;
14828 : 10 : specification_expr = saved_specification_expr;
14829 : 10 : return false;
14830 : : }
14831 : :
14832 : 617236 : no_init_error:
14833 : 624678 : if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
14834 : : {
14835 : 78587 : bool res = resolve_fl_variable_derived (sym, no_init_flag);
14836 : 78587 : specification_expr = saved_specification_expr;
14837 : 78587 : return res;
14838 : : }
14839 : :
14840 : 546091 : specification_expr = saved_specification_expr;
14841 : 546091 : return true;
14842 : : }
14843 : :
14844 : :
14845 : : /* Compare the dummy characteristics of a module procedure interface
14846 : : declaration with the corresponding declaration in a submodule. */
14847 : : static gfc_formal_arglist *new_formal;
14848 : : static char errmsg[200];
14849 : :
14850 : : static void
14851 : 1146 : compare_fsyms (gfc_symbol *sym)
14852 : : {
14853 : 1146 : gfc_symbol *fsym;
14854 : :
14855 : 1146 : if (sym == NULL || new_formal == NULL)
14856 : : return;
14857 : :
14858 : 1146 : fsym = new_formal->sym;
14859 : :
14860 : 1146 : if (sym == fsym)
14861 : : return;
14862 : :
14863 : 1122 : if (strcmp (sym->name, fsym->name) == 0)
14864 : : {
14865 : 421 : if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
14866 : 2 : gfc_error ("%s at %L", errmsg, &fsym->declared_at);
14867 : : }
14868 : : }
14869 : :
14870 : :
14871 : : /* Resolve a procedure. */
14872 : :
14873 : : static bool
14874 : 456594 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
14875 : : {
14876 : 456594 : gfc_formal_arglist *arg;
14877 : 456594 : bool allocatable_or_pointer = false;
14878 : :
14879 : 456594 : if (sym->attr.function
14880 : 456594 : && !resolve_fl_var_and_proc (sym, mp_flag))
14881 : : return false;
14882 : :
14883 : : /* Constraints on deferred type parameter. */
14884 : 456584 : if (!deferred_requirements (sym))
14885 : : return false;
14886 : :
14887 : 456583 : if (sym->ts.type == BT_CHARACTER)
14888 : : {
14889 : 11231 : gfc_charlen *cl = sym->ts.u.cl;
14890 : :
14891 : 7195 : if (cl && cl->length && gfc_is_constant_expr (cl->length)
14892 : 12315 : && !resolve_charlen (cl))
14893 : : return false;
14894 : :
14895 : 11230 : if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
14896 : 10147 : && sym->attr.proc == PROC_ST_FUNCTION)
14897 : : {
14898 : 0 : gfc_error ("Character-valued statement function %qs at %L must "
14899 : : "have constant length", sym->name, &sym->declared_at);
14900 : 0 : return false;
14901 : : }
14902 : : }
14903 : :
14904 : : /* Ensure that derived type for are not of a private type. Internal
14905 : : module procedures are excluded by 2.2.3.3 - i.e., they are not
14906 : : externally accessible and can access all the objects accessible in
14907 : : the host. */
14908 : 106584 : if (!(sym->ns->parent && sym->ns->parent->proc_name
14909 : 106584 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14910 : 540438 : && gfc_check_symbol_access (sym))
14911 : : {
14912 : 427208 : gfc_interface *iface;
14913 : :
14914 : 896822 : for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
14915 : : {
14916 : 469615 : if (arg->sym
14917 : 469474 : && arg->sym->ts.type == BT_DERIVED
14918 : 41709 : && arg->sym->ts.u.derived
14919 : 41709 : && !arg->sym->ts.u.derived->attr.use_assoc
14920 : 4188 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14921 : 469624 : && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
14922 : : "and cannot be a dummy argument"
14923 : : " of %qs, which is PUBLIC at %L",
14924 : 9 : arg->sym->name, sym->name,
14925 : : &sym->declared_at))
14926 : : {
14927 : : /* Stop this message from recurring. */
14928 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14929 : 1 : return false;
14930 : : }
14931 : : }
14932 : :
14933 : : /* PUBLIC interfaces may expose PRIVATE procedures that take types
14934 : : PRIVATE to the containing module. */
14935 : 608932 : for (iface = sym->generic; iface; iface = iface->next)
14936 : : {
14937 : 422608 : for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
14938 : : {
14939 : 240883 : if (arg->sym
14940 : 240851 : && arg->sym->ts.type == BT_DERIVED
14941 : 7876 : && !arg->sym->ts.u.derived->attr.use_assoc
14942 : 208 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
14943 : 240887 : && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
14944 : : "PUBLIC interface %qs at %L "
14945 : : "takes dummy arguments of %qs which "
14946 : : "is PRIVATE", iface->sym->name,
14947 : 4 : sym->name, &iface->sym->declared_at,
14948 : 4 : gfc_typename(&arg->sym->ts)))
14949 : : {
14950 : : /* Stop this message from recurring. */
14951 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
14952 : 1 : return false;
14953 : : }
14954 : : }
14955 : : }
14956 : : }
14957 : :
14958 : 456580 : if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
14959 : 67 : && !sym->attr.proc_pointer)
14960 : : {
14961 : 2 : gfc_error ("Function %qs at %L cannot have an initializer",
14962 : : sym->name, &sym->declared_at);
14963 : :
14964 : : /* Make sure no second error is issued for this. */
14965 : 2 : sym->value->error = 1;
14966 : 2 : return false;
14967 : : }
14968 : :
14969 : : /* An external symbol may not have an initializer because it is taken to be
14970 : : a procedure. Exception: Procedure Pointers. */
14971 : 456578 : if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
14972 : : {
14973 : 0 : gfc_error ("External object %qs at %L may not have an initializer",
14974 : : sym->name, &sym->declared_at);
14975 : 0 : return false;
14976 : : }
14977 : :
14978 : : /* An elemental function is required to return a scalar 12.7.1 */
14979 : 456578 : if (sym->attr.elemental && sym->attr.function
14980 : 86046 : && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
14981 : 2 : && CLASS_DATA (sym)->as)))
14982 : : {
14983 : 3 : gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
14984 : : "result", sym->name, &sym->declared_at);
14985 : : /* Reset so that the error only occurs once. */
14986 : 3 : sym->attr.elemental = 0;
14987 : 3 : return false;
14988 : : }
14989 : :
14990 : 456575 : if (sym->attr.proc == PROC_ST_FUNCTION
14991 : 220 : && (sym->attr.allocatable || sym->attr.pointer))
14992 : : {
14993 : 2 : gfc_error ("Statement function %qs at %L may not have pointer or "
14994 : : "allocatable attribute", sym->name, &sym->declared_at);
14995 : 2 : return false;
14996 : : }
14997 : :
14998 : : /* 5.1.1.5 of the Standard: A function name declared with an asterisk
14999 : : char-len-param shall not be array-valued, pointer-valued, recursive
15000 : : or pure. ....snip... A character value of * may only be used in the
15001 : : following ways: (i) Dummy arg of procedure - dummy associates with
15002 : : actual length; (ii) To declare a named constant; or (iii) External
15003 : : function - but length must be declared in calling scoping unit. */
15004 : 456573 : if (sym->attr.function
15005 : 309452 : && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
15006 : 6408 : && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
15007 : : {
15008 : 178 : if ((sym->as && sym->as->rank) || (sym->attr.pointer)
15009 : 176 : || (sym->attr.recursive) || (sym->attr.pure))
15010 : : {
15011 : 4 : if (sym->as && sym->as->rank)
15012 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15013 : : "array-valued", sym->name, &sym->declared_at);
15014 : :
15015 : 4 : if (sym->attr.pointer)
15016 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15017 : : "pointer-valued", sym->name, &sym->declared_at);
15018 : :
15019 : 4 : if (sym->attr.pure)
15020 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15021 : : "pure", sym->name, &sym->declared_at);
15022 : :
15023 : 4 : if (sym->attr.recursive)
15024 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15025 : : "recursive", sym->name, &sym->declared_at);
15026 : :
15027 : 4 : return false;
15028 : : }
15029 : :
15030 : : /* Appendix B.2 of the standard. Contained functions give an
15031 : : error anyway. Deferred character length is an F2003 feature.
15032 : : Don't warn on intrinsic conversion functions, which start
15033 : : with two underscores. */
15034 : 174 : if (!sym->attr.contained && !sym->ts.deferred
15035 : 170 : && (sym->name[0] != '_' || sym->name[1] != '_'))
15036 : 170 : gfc_notify_std (GFC_STD_F95_OBS,
15037 : : "CHARACTER(*) function %qs at %L",
15038 : : sym->name, &sym->declared_at);
15039 : : }
15040 : :
15041 : : /* F2008, C1218. */
15042 : 456569 : if (sym->attr.elemental)
15043 : : {
15044 : 89210 : if (sym->attr.proc_pointer)
15045 : : {
15046 : 7 : const char* name = (sym->attr.result ? sym->ns->proc_name->name
15047 : : : sym->name);
15048 : 7 : gfc_error ("Procedure pointer %qs at %L shall not be elemental",
15049 : : name, &sym->declared_at);
15050 : 7 : return false;
15051 : : }
15052 : 89203 : if (sym->attr.dummy)
15053 : : {
15054 : 3 : gfc_error ("Dummy procedure %qs at %L shall not be elemental",
15055 : : sym->name, &sym->declared_at);
15056 : 3 : return false;
15057 : : }
15058 : : }
15059 : :
15060 : : /* F2018, C15100: "The result of an elemental function shall be scalar,
15061 : : and shall not have the POINTER or ALLOCATABLE attribute." The scalar
15062 : : pointer is tested and caught elsewhere. */
15063 : 456559 : if (sym->result)
15064 : 259938 : allocatable_or_pointer = sym->result->ts.type == BT_CLASS
15065 : 259938 : && CLASS_DATA (sym->result) ?
15066 : 1641 : (CLASS_DATA (sym->result)->attr.allocatable
15067 : 1641 : || CLASS_DATA (sym->result)->attr.pointer) :
15068 : 258297 : (sym->result->attr.allocatable
15069 : 258297 : || sym->result->attr.pointer);
15070 : :
15071 : 456559 : if (sym->attr.elemental && sym->result
15072 : 85672 : && allocatable_or_pointer)
15073 : : {
15074 : 4 : gfc_error ("Function result variable %qs at %L of elemental "
15075 : : "function %qs shall not have an ALLOCATABLE or POINTER "
15076 : : "attribute", sym->result->name,
15077 : : &sym->result->declared_at, sym->name);
15078 : 4 : return false;
15079 : : }
15080 : :
15081 : 456555 : if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
15082 : : {
15083 : 6141 : gfc_formal_arglist *curr_arg;
15084 : 6141 : int has_non_interop_arg = 0;
15085 : :
15086 : 6141 : if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15087 : 6141 : sym->common_block))
15088 : : {
15089 : : /* Clear these to prevent looking at them again if there was an
15090 : : error. */
15091 : 2 : sym->attr.is_bind_c = 0;
15092 : 2 : sym->attr.is_c_interop = 0;
15093 : 2 : sym->ts.is_c_interop = 0;
15094 : : }
15095 : : else
15096 : : {
15097 : : /* So far, no errors have been found. */
15098 : 6139 : sym->attr.is_c_interop = 1;
15099 : 6139 : sym->ts.is_c_interop = 1;
15100 : : }
15101 : :
15102 : 6141 : curr_arg = gfc_sym_get_dummy_args (sym);
15103 : 28021 : while (curr_arg != NULL)
15104 : : {
15105 : : /* Skip implicitly typed dummy args here. */
15106 : 15739 : if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
15107 : 15683 : if (!gfc_verify_c_interop_param (curr_arg->sym))
15108 : : /* If something is found to fail, record the fact so we
15109 : : can mark the symbol for the procedure as not being
15110 : : BIND(C) to try and prevent multiple errors being
15111 : : reported. */
15112 : 15739 : has_non_interop_arg = 1;
15113 : :
15114 : 15739 : curr_arg = curr_arg->next;
15115 : : }
15116 : :
15117 : : /* See if any of the arguments were not interoperable and if so, clear
15118 : : the procedure symbol to prevent duplicate error messages. */
15119 : 6141 : if (has_non_interop_arg != 0)
15120 : : {
15121 : 128 : sym->attr.is_c_interop = 0;
15122 : 128 : sym->ts.is_c_interop = 0;
15123 : 128 : sym->attr.is_bind_c = 0;
15124 : : }
15125 : : }
15126 : :
15127 : 456555 : if (!sym->attr.proc_pointer)
15128 : : {
15129 : 455513 : if (sym->attr.save == SAVE_EXPLICIT)
15130 : : {
15131 : 5 : gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
15132 : : "in %qs at %L", sym->name, &sym->declared_at);
15133 : 5 : return false;
15134 : : }
15135 : 455508 : if (sym->attr.intent)
15136 : : {
15137 : 1 : gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
15138 : : "in %qs at %L", sym->name, &sym->declared_at);
15139 : 1 : return false;
15140 : : }
15141 : 455507 : if (sym->attr.subroutine && sym->attr.result)
15142 : : {
15143 : 2 : gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
15144 : 2 : "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
15145 : 2 : return false;
15146 : : }
15147 : 455505 : if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
15148 : 132357 : && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
15149 : 132354 : || sym->attr.contained))
15150 : : {
15151 : 3 : gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
15152 : : "in %qs at %L", sym->name, &sym->declared_at);
15153 : 3 : return false;
15154 : : }
15155 : 455502 : if (strcmp ("ppr@", sym->name) == 0)
15156 : : {
15157 : 0 : gfc_error ("Procedure pointer result %qs at %L "
15158 : : "is missing the pointer attribute",
15159 : 0 : sym->ns->proc_name->name, &sym->declared_at);
15160 : 0 : return false;
15161 : : }
15162 : : }
15163 : :
15164 : : /* Assume that a procedure whose body is not known has references
15165 : : to external arrays. */
15166 : 456544 : if (sym->attr.if_source != IFSRC_DECL)
15167 : 313475 : sym->attr.array_outer_dependency = 1;
15168 : :
15169 : : /* Compare the characteristics of a module procedure with the
15170 : : interface declaration. Ideally this would be done with
15171 : : gfc_compare_interfaces but, at present, the formal interface
15172 : : cannot be copied to the ts.interface. */
15173 : 456544 : if (sym->attr.module_procedure
15174 : 1346 : && sym->attr.if_source == IFSRC_DECL)
15175 : : {
15176 : 574 : gfc_symbol *iface;
15177 : 574 : char name[2*GFC_MAX_SYMBOL_LEN + 1];
15178 : 574 : char *module_name;
15179 : 574 : char *submodule_name;
15180 : 574 : strcpy (name, sym->ns->proc_name->name);
15181 : 574 : module_name = strtok (name, ".");
15182 : 574 : submodule_name = strtok (NULL, ".");
15183 : :
15184 : 574 : iface = sym->tlink;
15185 : 574 : sym->tlink = NULL;
15186 : :
15187 : : /* Make sure that the result uses the correct charlen for deferred
15188 : : length results. */
15189 : 574 : if (iface && sym->result
15190 : 140 : && iface->ts.type == BT_CHARACTER
15191 : 19 : && iface->ts.deferred)
15192 : 6 : sym->result->ts.u.cl = iface->ts.u.cl;
15193 : :
15194 : 6 : if (iface == NULL)
15195 : 193 : goto check_formal;
15196 : :
15197 : : /* Check the procedure characteristics. */
15198 : 381 : if (sym->attr.elemental != iface->attr.elemental)
15199 : : {
15200 : 1 : gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
15201 : : "PROCEDURE at %L and its interface in %s",
15202 : : &sym->declared_at, module_name);
15203 : 10 : return false;
15204 : : }
15205 : :
15206 : 380 : if (sym->attr.pure != iface->attr.pure)
15207 : : {
15208 : 2 : gfc_error ("Mismatch in PURE attribute between MODULE "
15209 : : "PROCEDURE at %L and its interface in %s",
15210 : : &sym->declared_at, module_name);
15211 : 2 : return false;
15212 : : }
15213 : :
15214 : 378 : if (sym->attr.recursive != iface->attr.recursive)
15215 : : {
15216 : 2 : gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
15217 : : "PROCEDURE at %L and its interface in %s",
15218 : : &sym->declared_at, module_name);
15219 : 2 : return false;
15220 : : }
15221 : :
15222 : : /* Check the result characteristics. */
15223 : 376 : if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
15224 : : {
15225 : 5 : gfc_error ("%s between the MODULE PROCEDURE declaration "
15226 : : "in MODULE %qs and the declaration at %L in "
15227 : : "(SUB)MODULE %qs",
15228 : : errmsg, module_name, &sym->declared_at,
15229 : : submodule_name ? submodule_name : module_name);
15230 : 5 : return false;
15231 : : }
15232 : :
15233 : 371 : check_formal:
15234 : : /* Check the characteristics of the formal arguments. */
15235 : 564 : if (sym->formal && sym->formal_ns)
15236 : : {
15237 : 1084 : for (arg = sym->formal; arg && arg->sym; arg = arg->next)
15238 : : {
15239 : 619 : new_formal = arg;
15240 : 619 : gfc_traverse_ns (sym->formal_ns, compare_fsyms);
15241 : : }
15242 : : }
15243 : : }
15244 : :
15245 : : /* F2018:15.4.2.2 requires an explicit interface for procedures with the
15246 : : BIND(C) attribute. */
15247 : 456534 : if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
15248 : : {
15249 : 1 : gfc_error ("Interface of %qs at %L must be explicit",
15250 : : sym->name, &sym->declared_at);
15251 : 1 : return false;
15252 : : }
15253 : :
15254 : : return true;
15255 : : }
15256 : :
15257 : :
15258 : : /* Resolve a list of finalizer procedures. That is, after they have hopefully
15259 : : been defined and we now know their defined arguments, check that they fulfill
15260 : : the requirements of the standard for procedures used as finalizers. */
15261 : :
15262 : : static bool
15263 : 105801 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
15264 : : {
15265 : 105801 : gfc_finalizer* list;
15266 : 105801 : gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
15267 : 105801 : bool result = true;
15268 : 105801 : bool seen_scalar = false;
15269 : 105801 : gfc_symbol *vtab;
15270 : 105801 : gfc_component *c;
15271 : 105801 : gfc_symbol *parent = gfc_get_derived_super_type (derived);
15272 : :
15273 : 105801 : if (parent)
15274 : 14693 : gfc_resolve_finalizers (parent, finalizable);
15275 : :
15276 : : /* Ensure that derived-type components have a their finalizers resolved. */
15277 : 105801 : bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
15278 : 332781 : for (c = derived->components; c; c = c->next)
15279 : 226980 : if (c->ts.type == BT_DERIVED
15280 : 63383 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
15281 : : {
15282 : 7676 : bool has_final2 = false;
15283 : 7676 : if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
15284 : 0 : return false; /* Error. */
15285 : 7676 : has_final = has_final || has_final2;
15286 : : }
15287 : : /* Return early if not finalizable. */
15288 : 105801 : if (!has_final)
15289 : : {
15290 : 103549 : if (finalizable)
15291 : 7634 : *finalizable = false;
15292 : 103549 : return true;
15293 : : }
15294 : :
15295 : : /* Walk over the list of finalizer-procedures, check them, and if any one
15296 : : does not fit in with the standard's definition, print an error and remove
15297 : : it from the list. */
15298 : 2252 : prev_link = &derived->f2k_derived->finalizers;
15299 : 4648 : for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
15300 : : {
15301 : 2396 : gfc_formal_arglist *dummy_args;
15302 : 2396 : gfc_symbol* arg;
15303 : 2396 : gfc_finalizer* i;
15304 : 2396 : int my_rank;
15305 : :
15306 : : /* Skip this finalizer if we already resolved it. */
15307 : 2396 : if (list->proc_tree)
15308 : : {
15309 : 1933 : if (list->proc_tree->n.sym->formal->sym->as == NULL
15310 : 566 : || list->proc_tree->n.sym->formal->sym->as->rank == 0)
15311 : 1367 : seen_scalar = true;
15312 : 1933 : prev_link = &(list->next);
15313 : 1933 : continue;
15314 : : }
15315 : :
15316 : : /* Check this exists and is a SUBROUTINE. */
15317 : 463 : if (!list->proc_sym->attr.subroutine)
15318 : : {
15319 : 3 : gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
15320 : : list->proc_sym->name, &list->where);
15321 : 3 : goto error;
15322 : : }
15323 : :
15324 : : /* We should have exactly one argument. */
15325 : 460 : dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
15326 : 460 : if (!dummy_args || dummy_args->next)
15327 : : {
15328 : 2 : gfc_error ("FINAL procedure at %L must have exactly one argument",
15329 : : &list->where);
15330 : 2 : goto error;
15331 : : }
15332 : 458 : arg = dummy_args->sym;
15333 : :
15334 : 458 : if (!arg)
15335 : : {
15336 : 1 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15337 : 1 : &list->proc_sym->declared_at, derived->name);
15338 : 1 : goto error;
15339 : : }
15340 : :
15341 : 457 : if (arg->as && arg->as->type == AS_ASSUMED_RANK
15342 : 6 : && ((list != derived->f2k_derived->finalizers) || list->next))
15343 : : {
15344 : 0 : gfc_error ("FINAL procedure at %L with assumed rank argument must "
15345 : : "be the only finalizer with the same kind/type "
15346 : : "(F2018: C790)", &list->where);
15347 : 0 : goto error;
15348 : : }
15349 : :
15350 : : /* This argument must be of our type. */
15351 : 457 : if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
15352 : : {
15353 : 2 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15354 : : &arg->declared_at, derived->name);
15355 : 2 : goto error;
15356 : : }
15357 : :
15358 : : /* It must neither be a pointer nor allocatable nor optional. */
15359 : 455 : if (arg->attr.pointer)
15360 : : {
15361 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
15362 : : &arg->declared_at);
15363 : 1 : goto error;
15364 : : }
15365 : 454 : if (arg->attr.allocatable)
15366 : : {
15367 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15368 : : " ALLOCATABLE", &arg->declared_at);
15369 : 1 : goto error;
15370 : : }
15371 : 453 : if (arg->attr.optional)
15372 : : {
15373 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
15374 : : &arg->declared_at);
15375 : 1 : goto error;
15376 : : }
15377 : :
15378 : : /* It must not be INTENT(OUT). */
15379 : 452 : if (arg->attr.intent == INTENT_OUT)
15380 : : {
15381 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15382 : : " INTENT(OUT)", &arg->declared_at);
15383 : 1 : goto error;
15384 : : }
15385 : :
15386 : : /* Warn if the procedure is non-scalar and not assumed shape. */
15387 : 451 : if (warn_surprising && arg->as && arg->as->rank != 0
15388 : 3 : && arg->as->type != AS_ASSUMED_SHAPE)
15389 : 2 : gfc_warning (OPT_Wsurprising,
15390 : : "Non-scalar FINAL procedure at %L should have assumed"
15391 : : " shape argument", &arg->declared_at);
15392 : :
15393 : : /* Check that it does not match in kind and rank with a FINAL procedure
15394 : : defined earlier. To really loop over the *earlier* declarations,
15395 : : we need to walk the tail of the list as new ones were pushed at the
15396 : : front. */
15397 : : /* TODO: Handle kind parameters once they are implemented. */
15398 : 451 : my_rank = (arg->as ? arg->as->rank : 0);
15399 : 540 : for (i = list->next; i; i = i->next)
15400 : : {
15401 : 91 : gfc_formal_arglist *dummy_args;
15402 : :
15403 : : /* Argument list might be empty; that is an error signalled earlier,
15404 : : but we nevertheless continued resolving. */
15405 : 91 : dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
15406 : 91 : if (dummy_args)
15407 : : {
15408 : 89 : gfc_symbol* i_arg = dummy_args->sym;
15409 : 89 : const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
15410 : 89 : if (i_rank == my_rank)
15411 : : {
15412 : 2 : gfc_error ("FINAL procedure %qs declared at %L has the same"
15413 : : " rank (%d) as %qs",
15414 : 2 : list->proc_sym->name, &list->where, my_rank,
15415 : 2 : i->proc_sym->name);
15416 : 2 : goto error;
15417 : : }
15418 : : }
15419 : : }
15420 : :
15421 : : /* Is this the/a scalar finalizer procedure? */
15422 : 449 : if (my_rank == 0)
15423 : 329 : seen_scalar = true;
15424 : :
15425 : : /* Find the symtree for this procedure. */
15426 : 449 : gcc_assert (!list->proc_tree);
15427 : 449 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
15428 : :
15429 : 449 : prev_link = &list->next;
15430 : 449 : continue;
15431 : :
15432 : : /* Remove wrong nodes immediately from the list so we don't risk any
15433 : : troubles in the future when they might fail later expectations. */
15434 : 14 : error:
15435 : 14 : i = list;
15436 : 14 : *prev_link = list->next;
15437 : 14 : gfc_free_finalizer (i);
15438 : 14 : result = false;
15439 : 449 : }
15440 : :
15441 : 2252 : if (result == false)
15442 : : return false;
15443 : :
15444 : : /* Warn if we haven't seen a scalar finalizer procedure (but we know there
15445 : : were nodes in the list, must have been for arrays. It is surely a good
15446 : : idea to have a scalar version there if there's something to finalize. */
15447 : 2248 : if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
15448 : 1 : gfc_warning (OPT_Wsurprising,
15449 : : "Only array FINAL procedures declared for derived type %qs"
15450 : : " defined at %L, suggest also scalar one unless an assumed"
15451 : : " rank finalizer has been declared",
15452 : : derived->name, &derived->declared_at);
15453 : :
15454 : 2248 : vtab = gfc_find_derived_vtab (derived);
15455 : 2248 : c = vtab->ts.u.derived->components->next->next->next->next->next;
15456 : 2248 : gfc_set_sym_referenced (c->initializer->symtree->n.sym);
15457 : :
15458 : 2248 : if (finalizable)
15459 : 590 : *finalizable = true;
15460 : :
15461 : : return true;
15462 : : }
15463 : :
15464 : :
15465 : : /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
15466 : :
15467 : : static bool
15468 : 380 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
15469 : : const char* generic_name, locus where)
15470 : : {
15471 : 380 : gfc_symbol *sym1, *sym2;
15472 : 380 : const char *pass1, *pass2;
15473 : 380 : gfc_formal_arglist *dummy_args;
15474 : :
15475 : 380 : gcc_assert (t1->specific && t2->specific);
15476 : 380 : gcc_assert (!t1->specific->is_generic);
15477 : 380 : gcc_assert (!t2->specific->is_generic);
15478 : 380 : gcc_assert (t1->is_operator == t2->is_operator);
15479 : :
15480 : 380 : sym1 = t1->specific->u.specific->n.sym;
15481 : 380 : sym2 = t2->specific->u.specific->n.sym;
15482 : :
15483 : 380 : if (sym1 == sym2)
15484 : : return true;
15485 : :
15486 : : /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
15487 : 380 : if (sym1->attr.subroutine != sym2->attr.subroutine
15488 : 378 : || sym1->attr.function != sym2->attr.function)
15489 : : {
15490 : 2 : gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
15491 : : " GENERIC %qs at %L",
15492 : : sym1->name, sym2->name, generic_name, &where);
15493 : 2 : return false;
15494 : : }
15495 : :
15496 : : /* Determine PASS arguments. */
15497 : 378 : if (t1->specific->nopass)
15498 : : pass1 = NULL;
15499 : 327 : else if (t1->specific->pass_arg)
15500 : : pass1 = t1->specific->pass_arg;
15501 : : else
15502 : : {
15503 : 212 : dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
15504 : 212 : if (dummy_args)
15505 : 211 : pass1 = dummy_args->sym->name;
15506 : : else
15507 : : pass1 = NULL;
15508 : : }
15509 : 378 : if (t2->specific->nopass)
15510 : : pass2 = NULL;
15511 : 326 : else if (t2->specific->pass_arg)
15512 : : pass2 = t2->specific->pass_arg;
15513 : : else
15514 : : {
15515 : 207 : dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
15516 : 207 : if (dummy_args)
15517 : 206 : pass2 = dummy_args->sym->name;
15518 : : else
15519 : : pass2 = NULL;
15520 : : }
15521 : :
15522 : : /* Compare the interfaces. */
15523 : 378 : if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
15524 : : NULL, 0, pass1, pass2))
15525 : : {
15526 : 8 : gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
15527 : : sym1->name, sym2->name, generic_name, &where);
15528 : 8 : return false;
15529 : : }
15530 : :
15531 : : return true;
15532 : : }
15533 : :
15534 : :
15535 : : /* Worker function for resolving a generic procedure binding; this is used to
15536 : : resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
15537 : :
15538 : : The difference between those cases is finding possible inherited bindings
15539 : : that are overridden, as one has to look for them in tb_sym_root,
15540 : : tb_uop_root or tb_op, respectively. Thus the caller must already find
15541 : : the super-type and set p->overridden correctly. */
15542 : :
15543 : : static bool
15544 : 2045 : resolve_tb_generic_targets (gfc_symbol* super_type,
15545 : : gfc_typebound_proc* p, const char* name)
15546 : : {
15547 : 2045 : gfc_tbp_generic* target;
15548 : 2045 : gfc_symtree* first_target;
15549 : 2045 : gfc_symtree* inherited;
15550 : :
15551 : 2045 : gcc_assert (p && p->is_generic);
15552 : :
15553 : : /* Try to find the specific bindings for the symtrees in our target-list. */
15554 : 2045 : gcc_assert (p->u.generic);
15555 : 4334 : for (target = p->u.generic; target; target = target->next)
15556 : 2306 : if (!target->specific)
15557 : : {
15558 : 2168 : gfc_typebound_proc* overridden_tbp;
15559 : 2168 : gfc_tbp_generic* g;
15560 : 2168 : const char* target_name;
15561 : :
15562 : 2168 : target_name = target->specific_st->name;
15563 : :
15564 : : /* Defined for this type directly. */
15565 : 2168 : if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
15566 : : {
15567 : 2159 : target->specific = target->specific_st->n.tb;
15568 : 2159 : goto specific_found;
15569 : : }
15570 : :
15571 : : /* Look for an inherited specific binding. */
15572 : 9 : if (super_type)
15573 : : {
15574 : 5 : inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
15575 : : true, NULL);
15576 : :
15577 : 5 : if (inherited)
15578 : : {
15579 : 5 : gcc_assert (inherited->n.tb);
15580 : 5 : target->specific = inherited->n.tb;
15581 : 5 : goto specific_found;
15582 : : }
15583 : : }
15584 : :
15585 : 4 : gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
15586 : : " at %L", target_name, name, &p->where);
15587 : 4 : return false;
15588 : :
15589 : : /* Once we've found the specific binding, check it is not ambiguous with
15590 : : other specifics already found or inherited for the same GENERIC. */
15591 : 2164 : specific_found:
15592 : 2164 : gcc_assert (target->specific);
15593 : :
15594 : : /* This must really be a specific binding! */
15595 : 2164 : if (target->specific->is_generic)
15596 : : {
15597 : 3 : gfc_error ("GENERIC %qs at %L must target a specific binding,"
15598 : : " %qs is GENERIC, too", name, &p->where, target_name);
15599 : 3 : return false;
15600 : : }
15601 : :
15602 : : /* Check those already resolved on this type directly. */
15603 : 5032 : for (g = p->u.generic; g; g = g->next)
15604 : 724 : if (g != target && g->specific
15605 : 3240 : && !check_generic_tbp_ambiguity (target, g, name, p->where))
15606 : : return false;
15607 : :
15608 : : /* Check for ambiguity with inherited specific targets. */
15609 : 2170 : for (overridden_tbp = p->overridden; overridden_tbp;
15610 : 16 : overridden_tbp = overridden_tbp->overridden)
15611 : 19 : if (overridden_tbp->is_generic)
15612 : : {
15613 : 33 : for (g = overridden_tbp->u.generic; g; g = g->next)
15614 : : {
15615 : 18 : gcc_assert (g->specific);
15616 : 18 : if (!check_generic_tbp_ambiguity (target, g, name, p->where))
15617 : : return false;
15618 : : }
15619 : : }
15620 : : }
15621 : :
15622 : : /* If we attempt to "overwrite" a specific binding, this is an error. */
15623 : 2028 : if (p->overridden && !p->overridden->is_generic)
15624 : : {
15625 : 1 : gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
15626 : : " the same name", name, &p->where);
15627 : 1 : return false;
15628 : : }
15629 : :
15630 : : /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
15631 : : all must have the same attributes here. */
15632 : 2027 : first_target = p->u.generic->specific->u.specific;
15633 : 2027 : gcc_assert (first_target);
15634 : 2027 : p->subroutine = first_target->n.sym->attr.subroutine;
15635 : 2027 : p->function = first_target->n.sym->attr.function;
15636 : :
15637 : 2027 : return true;
15638 : : }
15639 : :
15640 : :
15641 : : /* Resolve a GENERIC procedure binding for a derived type. */
15642 : :
15643 : : static bool
15644 : 1045 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
15645 : : {
15646 : 1045 : gfc_symbol* super_type;
15647 : :
15648 : : /* Find the overridden binding if any. */
15649 : 1045 : st->n.tb->overridden = NULL;
15650 : 1045 : super_type = gfc_get_derived_super_type (derived);
15651 : 1045 : if (super_type)
15652 : : {
15653 : 40 : gfc_symtree* overridden;
15654 : 40 : overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
15655 : : true, NULL);
15656 : :
15657 : 40 : if (overridden && overridden->n.tb)
15658 : 21 : st->n.tb->overridden = overridden->n.tb;
15659 : : }
15660 : :
15661 : : /* Resolve using worker function. */
15662 : 1045 : return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
15663 : : }
15664 : :
15665 : :
15666 : : /* Retrieve the target-procedure of an operator binding and do some checks in
15667 : : common for intrinsic and user-defined type-bound operators. */
15668 : :
15669 : : static gfc_symbol*
15670 : 1064 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
15671 : : {
15672 : 1064 : gfc_symbol* target_proc;
15673 : :
15674 : 1064 : gcc_assert (target->specific && !target->specific->is_generic);
15675 : 1064 : target_proc = target->specific->u.specific->n.sym;
15676 : 1064 : gcc_assert (target_proc);
15677 : :
15678 : : /* F08:C468. All operator bindings must have a passed-object dummy argument. */
15679 : 1064 : if (target->specific->nopass)
15680 : : {
15681 : 2 : gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
15682 : 2 : return NULL;
15683 : : }
15684 : :
15685 : : return target_proc;
15686 : : }
15687 : :
15688 : :
15689 : : /* Resolve a type-bound intrinsic operator. */
15690 : :
15691 : : static bool
15692 : 958 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
15693 : : gfc_typebound_proc* p)
15694 : : {
15695 : 958 : gfc_symbol* super_type;
15696 : 958 : gfc_tbp_generic* target;
15697 : :
15698 : : /* If there's already an error here, do nothing (but don't fail again). */
15699 : 958 : if (p->error)
15700 : : return true;
15701 : :
15702 : : /* Operators should always be GENERIC bindings. */
15703 : 958 : gcc_assert (p->is_generic);
15704 : :
15705 : : /* Look for an overridden binding. */
15706 : 958 : super_type = gfc_get_derived_super_type (derived);
15707 : 958 : if (super_type && super_type->f2k_derived)
15708 : 1 : p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
15709 : : op, true, NULL);
15710 : : else
15711 : 957 : p->overridden = NULL;
15712 : :
15713 : : /* Resolve general GENERIC properties using worker function. */
15714 : 958 : if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
15715 : 1 : goto error;
15716 : :
15717 : : /* Check the targets to be procedures of correct interface. */
15718 : 1958 : for (target = p->u.generic; target; target = target->next)
15719 : : {
15720 : 1021 : gfc_symbol* target_proc;
15721 : :
15722 : 1021 : target_proc = get_checked_tb_operator_target (target, p->where);
15723 : 1021 : if (!target_proc)
15724 : 1 : goto error;
15725 : :
15726 : 1020 : if (!gfc_check_operator_interface (target_proc, op, p->where))
15727 : 3 : goto error;
15728 : :
15729 : : /* Add target to non-typebound operator list. */
15730 : 1017 : if (!target->specific->deferred && !derived->attr.use_assoc
15731 : 358 : && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
15732 : : {
15733 : 356 : gfc_interface *head, *intr;
15734 : :
15735 : : /* Preempt 'gfc_check_new_interface' for submodules, where the
15736 : : mechanism for handling module procedures winds up resolving
15737 : : operator interfaces twice and would otherwise cause an error. */
15738 : 424 : for (intr = derived->ns->op[op]; intr; intr = intr->next)
15739 : 82 : if (intr->sym == target_proc
15740 : 16 : && target_proc->attr.used_in_submodule)
15741 : : return true;
15742 : :
15743 : 342 : if (!gfc_check_new_interface (derived->ns->op[op],
15744 : : target_proc, p->where))
15745 : : return false;
15746 : 340 : head = derived->ns->op[op];
15747 : 340 : intr = gfc_get_interface ();
15748 : 340 : intr->sym = target_proc;
15749 : 340 : intr->where = p->where;
15750 : 340 : intr->next = head;
15751 : 340 : derived->ns->op[op] = intr;
15752 : : }
15753 : : }
15754 : :
15755 : : return true;
15756 : :
15757 : 5 : error:
15758 : 5 : p->error = 1;
15759 : 5 : return false;
15760 : : }
15761 : :
15762 : :
15763 : : /* Resolve a type-bound user operator (tree-walker callback). */
15764 : :
15765 : : static gfc_symbol* resolve_bindings_derived;
15766 : : static bool resolve_bindings_result;
15767 : :
15768 : : static bool check_uop_procedure (gfc_symbol* sym, locus where);
15769 : :
15770 : : static void
15771 : 42 : resolve_typebound_user_op (gfc_symtree* stree)
15772 : : {
15773 : 42 : gfc_symbol* super_type;
15774 : 42 : gfc_tbp_generic* target;
15775 : :
15776 : 42 : gcc_assert (stree && stree->n.tb);
15777 : :
15778 : 42 : if (stree->n.tb->error)
15779 : : return;
15780 : :
15781 : : /* Operators should always be GENERIC bindings. */
15782 : 42 : gcc_assert (stree->n.tb->is_generic);
15783 : :
15784 : : /* Find overridden procedure, if any. */
15785 : 42 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15786 : 42 : if (super_type && super_type->f2k_derived)
15787 : : {
15788 : 0 : gfc_symtree* overridden;
15789 : 0 : overridden = gfc_find_typebound_user_op (super_type, NULL,
15790 : : stree->name, true, NULL);
15791 : :
15792 : 0 : if (overridden && overridden->n.tb)
15793 : 0 : stree->n.tb->overridden = overridden->n.tb;
15794 : : }
15795 : : else
15796 : 42 : stree->n.tb->overridden = NULL;
15797 : :
15798 : : /* Resolve basically using worker function. */
15799 : 42 : if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
15800 : 0 : goto error;
15801 : :
15802 : : /* Check the targets to be functions of correct interface. */
15803 : 82 : for (target = stree->n.tb->u.generic; target; target = target->next)
15804 : : {
15805 : 43 : gfc_symbol* target_proc;
15806 : :
15807 : 43 : target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
15808 : 43 : if (!target_proc)
15809 : 1 : goto error;
15810 : :
15811 : 42 : if (!check_uop_procedure (target_proc, stree->n.tb->where))
15812 : 2 : goto error;
15813 : : }
15814 : :
15815 : : return;
15816 : :
15817 : 3 : error:
15818 : 3 : resolve_bindings_result = false;
15819 : 3 : stree->n.tb->error = 1;
15820 : : }
15821 : :
15822 : :
15823 : : /* Resolve the type-bound procedures for a derived type. */
15824 : :
15825 : : static void
15826 : 9084 : resolve_typebound_procedure (gfc_symtree* stree)
15827 : : {
15828 : 9084 : gfc_symbol* proc;
15829 : 9084 : locus where;
15830 : 9084 : gfc_symbol* me_arg;
15831 : 9084 : gfc_symbol* super_type;
15832 : 9084 : gfc_component* comp;
15833 : :
15834 : 9084 : gcc_assert (stree);
15835 : :
15836 : : /* Undefined specific symbol from GENERIC target definition. */
15837 : 9084 : if (!stree->n.tb)
15838 : 9002 : return;
15839 : :
15840 : 9078 : if (stree->n.tb->error)
15841 : : return;
15842 : :
15843 : : /* If this is a GENERIC binding, use that routine. */
15844 : 9062 : if (stree->n.tb->is_generic)
15845 : : {
15846 : 1045 : if (!resolve_typebound_generic (resolve_bindings_derived, stree))
15847 : 17 : goto error;
15848 : : return;
15849 : : }
15850 : :
15851 : : /* Get the target-procedure to check it. */
15852 : 8017 : gcc_assert (!stree->n.tb->is_generic);
15853 : 8017 : gcc_assert (stree->n.tb->u.specific);
15854 : 8017 : proc = stree->n.tb->u.specific->n.sym;
15855 : 8017 : where = stree->n.tb->where;
15856 : :
15857 : : /* Default access should already be resolved from the parser. */
15858 : 8017 : gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
15859 : :
15860 : 8017 : if (stree->n.tb->deferred)
15861 : : {
15862 : 669 : if (!check_proc_interface (proc, &where))
15863 : 5 : goto error;
15864 : : }
15865 : : else
15866 : : {
15867 : : /* If proc has not been resolved at this point, proc->name may
15868 : : actually be a USE associated entity. See PR fortran/89647. */
15869 : 7348 : if (!proc->resolve_symbol_called
15870 : 4986 : && proc->attr.function == 0 && proc->attr.subroutine == 0)
15871 : : {
15872 : 11 : gfc_symbol *tmp;
15873 : 11 : gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
15874 : 11 : if (tmp && tmp->attr.use_assoc)
15875 : : {
15876 : 1 : proc->module = tmp->module;
15877 : 1 : proc->attr.proc = tmp->attr.proc;
15878 : 1 : proc->attr.function = tmp->attr.function;
15879 : 1 : proc->attr.subroutine = tmp->attr.subroutine;
15880 : 1 : proc->attr.use_assoc = tmp->attr.use_assoc;
15881 : 1 : proc->ts = tmp->ts;
15882 : 1 : proc->result = tmp->result;
15883 : : }
15884 : : }
15885 : :
15886 : : /* Check for F08:C465. */
15887 : 7348 : if ((!proc->attr.subroutine && !proc->attr.function)
15888 : 7338 : || (proc->attr.proc != PROC_MODULE
15889 : 21 : && proc->attr.if_source != IFSRC_IFBODY
15890 : 7 : && !proc->attr.module_procedure)
15891 : 7337 : || proc->attr.abstract)
15892 : : {
15893 : 12 : gfc_error ("%qs must be a module procedure or an external "
15894 : : "procedure with an explicit interface at %L",
15895 : : proc->name, &where);
15896 : 12 : goto error;
15897 : : }
15898 : : }
15899 : :
15900 : 8000 : stree->n.tb->subroutine = proc->attr.subroutine;
15901 : 8000 : stree->n.tb->function = proc->attr.function;
15902 : :
15903 : : /* Find the super-type of the current derived type. We could do this once and
15904 : : store in a global if speed is needed, but as long as not I believe this is
15905 : : more readable and clearer. */
15906 : 8000 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15907 : :
15908 : : /* If PASS, resolve and check arguments if not already resolved / loaded
15909 : : from a .mod file. */
15910 : 8000 : if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
15911 : : {
15912 : 2654 : gfc_formal_arglist *dummy_args;
15913 : :
15914 : 2654 : dummy_args = gfc_sym_get_dummy_args (proc);
15915 : 2654 : if (stree->n.tb->pass_arg)
15916 : : {
15917 : 453 : gfc_formal_arglist *i;
15918 : :
15919 : : /* If an explicit passing argument name is given, walk the arg-list
15920 : : and look for it. */
15921 : :
15922 : 453 : me_arg = NULL;
15923 : 453 : stree->n.tb->pass_arg_num = 1;
15924 : 573 : for (i = dummy_args; i; i = i->next)
15925 : : {
15926 : 571 : if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
15927 : : {
15928 : : me_arg = i->sym;
15929 : : break;
15930 : : }
15931 : 120 : ++stree->n.tb->pass_arg_num;
15932 : : }
15933 : :
15934 : 453 : if (!me_arg)
15935 : : {
15936 : 2 : gfc_error ("Procedure %qs with PASS(%s) at %L has no"
15937 : : " argument %qs",
15938 : : proc->name, stree->n.tb->pass_arg, &where,
15939 : : stree->n.tb->pass_arg);
15940 : 2 : goto error;
15941 : : }
15942 : : }
15943 : : else
15944 : : {
15945 : : /* Otherwise, take the first one; there should in fact be at least
15946 : : one. */
15947 : 2201 : stree->n.tb->pass_arg_num = 1;
15948 : 2201 : if (!dummy_args)
15949 : : {
15950 : 2 : gfc_error ("Procedure %qs with PASS at %L must have at"
15951 : : " least one argument", proc->name, &where);
15952 : 2 : goto error;
15953 : : }
15954 : 2199 : me_arg = dummy_args->sym;
15955 : : }
15956 : :
15957 : : /* Now check that the argument-type matches and the passed-object
15958 : : dummy argument is generally fine. */
15959 : :
15960 : 2199 : gcc_assert (me_arg);
15961 : :
15962 : 2650 : if (me_arg->ts.type != BT_CLASS)
15963 : : {
15964 : 5 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
15965 : : " at %L", proc->name, &where);
15966 : 5 : goto error;
15967 : : }
15968 : :
15969 : : /* The derived type is not a PDT template. Resolve as usual. */
15970 : 2645 : if (!resolve_bindings_derived->attr.pdt_template
15971 : 2630 : && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
15972 : : {
15973 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15974 : : "the derived-type %qs", me_arg->name, proc->name,
15975 : : me_arg->name, &where, resolve_bindings_derived->name);
15976 : 0 : goto error;
15977 : : }
15978 : :
15979 : 2645 : if (resolve_bindings_derived->attr.pdt_template
15980 : 2660 : && !gfc_pdt_is_instance_of (resolve_bindings_derived,
15981 : 15 : CLASS_DATA (me_arg)->ts.u.derived))
15982 : : {
15983 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
15984 : : "the parametric derived-type %qs", me_arg->name,
15985 : : proc->name, me_arg->name, &where,
15986 : : resolve_bindings_derived->name);
15987 : 0 : goto error;
15988 : : }
15989 : :
15990 : 2645 : if (resolve_bindings_derived->attr.pdt_template
15991 : 15 : && gfc_pdt_is_instance_of (resolve_bindings_derived,
15992 : 15 : CLASS_DATA (me_arg)->ts.u.derived)
15993 : 15 : && (me_arg->param_list != NULL)
15994 : 2660 : && (gfc_spec_list_type (me_arg->param_list,
15995 : 15 : CLASS_DATA(me_arg)->ts.u.derived)
15996 : : != SPEC_ASSUMED))
15997 : : {
15998 : :
15999 : : /* Add a check to verify if there are any LEN parameters in the
16000 : : first place. If there are LEN parameters, throw this error.
16001 : : If there are only KIND parameters, then don't trigger
16002 : : this error. */
16003 : 7 : gfc_component *c;
16004 : 7 : bool seen_len_param = false;
16005 : 7 : gfc_actual_arglist *me_arg_param = me_arg->param_list;
16006 : :
16007 : 8 : for (; me_arg_param; me_arg_param = me_arg_param->next)
16008 : : {
16009 : 7 : c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
16010 : : me_arg_param->name, true, true, NULL);
16011 : :
16012 : 7 : gcc_assert (c != NULL);
16013 : :
16014 : 7 : if (c->attr.pdt_kind)
16015 : 1 : continue;
16016 : :
16017 : : /* Getting here implies that there is a pdt_len parameter
16018 : : in the list. */
16019 : : seen_len_param = true;
16020 : : break;
16021 : : }
16022 : :
16023 : 7 : if (seen_len_param)
16024 : : {
16025 : 6 : gfc_error ("All LEN type parameters of the passed dummy "
16026 : : "argument %qs of %qs at %L must be ASSUMED.",
16027 : : me_arg->name, proc->name, &where);
16028 : 6 : goto error;
16029 : : }
16030 : : }
16031 : :
16032 : 2639 : gcc_assert (me_arg->ts.type == BT_CLASS);
16033 : 2639 : if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
16034 : : {
16035 : 1 : gfc_error ("Passed-object dummy argument of %qs at %L must be"
16036 : : " scalar", proc->name, &where);
16037 : 1 : goto error;
16038 : : }
16039 : 2638 : if (CLASS_DATA (me_arg)->attr.allocatable)
16040 : : {
16041 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16042 : : " be ALLOCATABLE", proc->name, &where);
16043 : 2 : goto error;
16044 : : }
16045 : 2636 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16046 : : {
16047 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16048 : : " be POINTER", proc->name, &where);
16049 : 2 : goto error;
16050 : : }
16051 : : }
16052 : :
16053 : : /* If we are extending some type, check that we don't override a procedure
16054 : : flagged NON_OVERRIDABLE. */
16055 : 7980 : stree->n.tb->overridden = NULL;
16056 : 7980 : if (super_type)
16057 : : {
16058 : 1480 : gfc_symtree* overridden;
16059 : 1480 : overridden = gfc_find_typebound_proc (super_type, NULL,
16060 : : stree->name, true, NULL);
16061 : :
16062 : 1480 : if (overridden)
16063 : : {
16064 : 1210 : if (overridden->n.tb)
16065 : 1210 : stree->n.tb->overridden = overridden->n.tb;
16066 : :
16067 : 1210 : if (!gfc_check_typebound_override (stree, overridden))
16068 : 26 : goto error;
16069 : : }
16070 : : }
16071 : :
16072 : : /* See if there's a name collision with a component directly in this type. */
16073 : 18983 : for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
16074 : 11030 : if (!strcmp (comp->name, stree->name))
16075 : : {
16076 : 1 : gfc_error ("Procedure %qs at %L has the same name as a component of"
16077 : : " %qs",
16078 : : stree->name, &where, resolve_bindings_derived->name);
16079 : 1 : goto error;
16080 : : }
16081 : :
16082 : : /* Try to find a name collision with an inherited component. */
16083 : 7953 : if (super_type && gfc_find_component (super_type, stree->name, true, true,
16084 : : NULL))
16085 : : {
16086 : 1 : gfc_error ("Procedure %qs at %L has the same name as an inherited"
16087 : : " component of %qs",
16088 : : stree->name, &where, resolve_bindings_derived->name);
16089 : 1 : goto error;
16090 : : }
16091 : :
16092 : 7952 : stree->n.tb->error = 0;
16093 : 7952 : return;
16094 : :
16095 : 82 : error:
16096 : 82 : resolve_bindings_result = false;
16097 : 82 : stree->n.tb->error = 1;
16098 : : }
16099 : :
16100 : :
16101 : : static bool
16102 : 81418 : resolve_typebound_procedures (gfc_symbol* derived)
16103 : : {
16104 : 81418 : int op;
16105 : 81418 : gfc_symbol* super_type;
16106 : :
16107 : 81418 : if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
16108 : : return true;
16109 : :
16110 : 4453 : super_type = gfc_get_derived_super_type (derived);
16111 : 4453 : if (super_type)
16112 : 847 : resolve_symbol (super_type);
16113 : :
16114 : 4453 : resolve_bindings_derived = derived;
16115 : 4453 : resolve_bindings_result = true;
16116 : :
16117 : 4453 : if (derived->f2k_derived->tb_sym_root)
16118 : 4453 : gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
16119 : : &resolve_typebound_procedure);
16120 : :
16121 : 4453 : if (derived->f2k_derived->tb_uop_root)
16122 : 38 : gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
16123 : : &resolve_typebound_user_op);
16124 : :
16125 : 129137 : for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
16126 : : {
16127 : 124684 : gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
16128 : 124684 : if (p && !resolve_typebound_intrinsic_op (derived,
16129 : : (gfc_intrinsic_op)op, p))
16130 : 7 : resolve_bindings_result = false;
16131 : : }
16132 : :
16133 : 4453 : return resolve_bindings_result;
16134 : : }
16135 : :
16136 : :
16137 : : /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
16138 : : to give all identical derived types the same backend_decl. */
16139 : : static void
16140 : 167175 : add_dt_to_dt_list (gfc_symbol *derived)
16141 : : {
16142 : 167175 : if (!derived->dt_next)
16143 : : {
16144 : 77256 : if (gfc_derived_types)
16145 : : {
16146 : 63458 : derived->dt_next = gfc_derived_types->dt_next;
16147 : 63458 : gfc_derived_types->dt_next = derived;
16148 : : }
16149 : : else
16150 : : {
16151 : 13798 : derived->dt_next = derived;
16152 : : }
16153 : 77256 : gfc_derived_types = derived;
16154 : : }
16155 : 167175 : }
16156 : :
16157 : :
16158 : : /* Ensure that a derived-type is really not abstract, meaning that every
16159 : : inherited DEFERRED binding is overridden by a non-DEFERRED one. */
16160 : :
16161 : : static bool
16162 : 6924 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
16163 : : {
16164 : 6924 : if (!st)
16165 : : return true;
16166 : :
16167 : 2712 : if (!ensure_not_abstract_walker (sub, st->left))
16168 : : return false;
16169 : 2712 : if (!ensure_not_abstract_walker (sub, st->right))
16170 : : return false;
16171 : :
16172 : 2711 : if (st->n.tb && st->n.tb->deferred)
16173 : : {
16174 : 1959 : gfc_symtree* overriding;
16175 : 1959 : overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
16176 : 1959 : if (!overriding)
16177 : : return false;
16178 : 1958 : gcc_assert (overriding->n.tb);
16179 : 1958 : if (overriding->n.tb->deferred)
16180 : : {
16181 : 5 : gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
16182 : : " %qs is DEFERRED and not overridden",
16183 : : sub->name, &sub->declared_at, st->name);
16184 : 5 : return false;
16185 : : }
16186 : : }
16187 : :
16188 : : return true;
16189 : : }
16190 : :
16191 : : static bool
16192 : 1352 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
16193 : : {
16194 : : /* The algorithm used here is to recursively travel up the ancestry of sub
16195 : : and for each ancestor-type, check all bindings. If any of them is
16196 : : DEFERRED, look it up starting from sub and see if the found (overriding)
16197 : : binding is not DEFERRED.
16198 : : This is not the most efficient way to do this, but it should be ok and is
16199 : : clearer than something sophisticated. */
16200 : :
16201 : 1501 : gcc_assert (ancestor && !sub->attr.abstract);
16202 : :
16203 : 1501 : if (!ancestor->attr.abstract)
16204 : : return true;
16205 : :
16206 : : /* Walk bindings of this ancestor. */
16207 : 1500 : if (ancestor->f2k_derived)
16208 : : {
16209 : 1500 : bool t;
16210 : 1500 : t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
16211 : 1500 : if (!t)
16212 : : return false;
16213 : : }
16214 : :
16215 : : /* Find next ancestor type and recurse on it. */
16216 : 1494 : ancestor = gfc_get_derived_super_type (ancestor);
16217 : 1494 : if (ancestor)
16218 : : return ensure_not_abstract (sub, ancestor);
16219 : :
16220 : : return true;
16221 : : }
16222 : :
16223 : :
16224 : : /* This check for typebound defined assignments is done recursively
16225 : : since the order in which derived types are resolved is not always in
16226 : : order of the declarations. */
16227 : :
16228 : : static void
16229 : 170944 : check_defined_assignments (gfc_symbol *derived)
16230 : : {
16231 : 170944 : gfc_component *c;
16232 : :
16233 : 571128 : for (c = derived->components; c; c = c->next)
16234 : : {
16235 : 401736 : if (!gfc_bt_struct (c->ts.type)
16236 : 96317 : || c->attr.pointer
16237 : 18208 : || c->attr.proc_pointer_comp
16238 : 18208 : || c->attr.class_pointer
16239 : 18202 : || c->attr.proc_pointer)
16240 : 383927 : continue;
16241 : :
16242 : 17809 : if (c->ts.u.derived->attr.defined_assign_comp
16243 : 17592 : || (c->ts.u.derived->f2k_derived
16244 : 17023 : && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
16245 : : {
16246 : 1528 : derived->attr.defined_assign_comp = 1;
16247 : 1528 : return;
16248 : : }
16249 : :
16250 : 16281 : if (c->attr.allocatable)
16251 : 5568 : continue;
16252 : :
16253 : 10713 : check_defined_assignments (c->ts.u.derived);
16254 : 10713 : if (c->ts.u.derived->attr.defined_assign_comp)
16255 : : {
16256 : 24 : derived->attr.defined_assign_comp = 1;
16257 : 24 : return;
16258 : : }
16259 : : }
16260 : : }
16261 : :
16262 : :
16263 : : /* Resolve a single component of a derived type or structure. */
16264 : :
16265 : : static bool
16266 : 383846 : resolve_component (gfc_component *c, gfc_symbol *sym)
16267 : : {
16268 : 383846 : gfc_symbol *super_type;
16269 : 383846 : symbol_attribute *attr;
16270 : :
16271 : 383846 : if (c->attr.artificial)
16272 : : return true;
16273 : :
16274 : : /* Do not allow vtype components to be resolved in nameless namespaces
16275 : : such as block data because the procedure pointers will cause ICEs
16276 : : and vtables are not needed in these contexts. */
16277 : 261232 : if (sym->attr.vtype && sym->attr.use_assoc
16278 : 46302 : && sym->ns->proc_name == NULL)
16279 : : return true;
16280 : :
16281 : : /* F2008, C442. */
16282 : 261223 : if ((!sym->attr.is_class || c != sym->components)
16283 : 261223 : && c->attr.codimension
16284 : 169 : && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
16285 : : {
16286 : 4 : gfc_error ("Coarray component %qs at %L must be allocatable with "
16287 : : "deferred shape", c->name, &c->loc);
16288 : 4 : return false;
16289 : : }
16290 : :
16291 : : /* F2008, C443. */
16292 : 261219 : if (c->attr.codimension && c->ts.type == BT_DERIVED
16293 : 77 : && c->ts.u.derived->ts.is_iso_c)
16294 : : {
16295 : 1 : gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16296 : : "shall not be a coarray", c->name, &c->loc);
16297 : 1 : return false;
16298 : : }
16299 : :
16300 : : /* F2008, C444. */
16301 : 261218 : if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
16302 : 22 : && (c->attr.codimension || c->attr.pointer || c->attr.dimension
16303 : 20 : || c->attr.allocatable))
16304 : : {
16305 : 3 : gfc_error ("Component %qs at %L with coarray component "
16306 : : "shall be a nonpointer, nonallocatable scalar",
16307 : : c->name, &c->loc);
16308 : 3 : return false;
16309 : : }
16310 : :
16311 : : /* F2008, C448. */
16312 : 261215 : if (c->ts.type == BT_CLASS)
16313 : : {
16314 : 6579 : if (c->attr.class_ok && CLASS_DATA (c))
16315 : : {
16316 : 6571 : attr = &(CLASS_DATA (c)->attr);
16317 : :
16318 : : /* Fix up contiguous attribute. */
16319 : 6571 : if (c->attr.contiguous)
16320 : 3 : attr->contiguous = 1;
16321 : : }
16322 : : else
16323 : : attr = NULL;
16324 : : }
16325 : : else
16326 : 254636 : attr = &c->attr;
16327 : :
16328 : 261210 : if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
16329 : : {
16330 : 5 : gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
16331 : : "is not an array pointer", c->name, &c->loc);
16332 : 5 : return false;
16333 : : }
16334 : :
16335 : : /* F2003, 15.2.1 - length has to be one. */
16336 : 39698 : if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
16337 : 261229 : && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
16338 : 19 : || !gfc_is_constant_expr (c->ts.u.cl->length)
16339 : 19 : || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
16340 : : {
16341 : 1 : gfc_error ("Component %qs of BIND(C) type at %L must have length one",
16342 : : c->name, &c->loc);
16343 : 1 : return false;
16344 : : }
16345 : :
16346 : 261209 : if (c->attr.proc_pointer && c->ts.interface)
16347 : : {
16348 : 13473 : gfc_symbol *ifc = c->ts.interface;
16349 : :
16350 : 13473 : if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
16351 : : {
16352 : 6 : c->tb->error = 1;
16353 : 6 : return false;
16354 : : }
16355 : :
16356 : 13467 : if (ifc->attr.if_source || ifc->attr.intrinsic)
16357 : : {
16358 : : /* Resolve interface and copy attributes. */
16359 : 13436 : if (ifc->formal && !ifc->formal_ns)
16360 : 2483 : resolve_symbol (ifc);
16361 : 13436 : if (ifc->attr.intrinsic)
16362 : 0 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
16363 : :
16364 : 13436 : if (ifc->result)
16365 : : {
16366 : 6661 : c->ts = ifc->result->ts;
16367 : 6661 : c->attr.allocatable = ifc->result->attr.allocatable;
16368 : 6661 : c->attr.pointer = ifc->result->attr.pointer;
16369 : 6661 : c->attr.dimension = ifc->result->attr.dimension;
16370 : 6661 : c->as = gfc_copy_array_spec (ifc->result->as);
16371 : 6661 : c->attr.class_ok = ifc->result->attr.class_ok;
16372 : : }
16373 : : else
16374 : : {
16375 : 6775 : c->ts = ifc->ts;
16376 : 6775 : c->attr.allocatable = ifc->attr.allocatable;
16377 : 6775 : c->attr.pointer = ifc->attr.pointer;
16378 : 6775 : c->attr.dimension = ifc->attr.dimension;
16379 : 6775 : c->as = gfc_copy_array_spec (ifc->as);
16380 : 6775 : c->attr.class_ok = ifc->attr.class_ok;
16381 : : }
16382 : 13436 : c->ts.interface = ifc;
16383 : 13436 : c->attr.function = ifc->attr.function;
16384 : 13436 : c->attr.subroutine = ifc->attr.subroutine;
16385 : :
16386 : 13436 : c->attr.pure = ifc->attr.pure;
16387 : 13436 : c->attr.elemental = ifc->attr.elemental;
16388 : 13436 : c->attr.recursive = ifc->attr.recursive;
16389 : 13436 : c->attr.always_explicit = ifc->attr.always_explicit;
16390 : 13436 : c->attr.ext_attr |= ifc->attr.ext_attr;
16391 : : /* Copy char length. */
16392 : 13436 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
16393 : : {
16394 : 220 : gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
16395 : 183 : if (cl->length && !cl->resolved
16396 : 240 : && !gfc_resolve_expr (cl->length))
16397 : : {
16398 : 0 : c->tb->error = 1;
16399 : 0 : return false;
16400 : : }
16401 : 220 : c->ts.u.cl = cl;
16402 : : }
16403 : : }
16404 : : }
16405 : 247736 : else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
16406 : : {
16407 : : /* Since PPCs are not implicitly typed, a PPC without an explicit
16408 : : interface must be a subroutine. */
16409 : 115 : gfc_add_subroutine (&c->attr, c->name, &c->loc);
16410 : : }
16411 : :
16412 : : /* Procedure pointer components: Check PASS arg. */
16413 : 261203 : if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
16414 : 717 : && !sym->attr.vtype)
16415 : : {
16416 : 94 : gfc_symbol* me_arg;
16417 : :
16418 : 94 : if (c->tb->pass_arg)
16419 : : {
16420 : 19 : gfc_formal_arglist* i;
16421 : :
16422 : : /* If an explicit passing argument name is given, walk the arg-list
16423 : : and look for it. */
16424 : :
16425 : 19 : me_arg = NULL;
16426 : 19 : c->tb->pass_arg_num = 1;
16427 : 33 : for (i = c->ts.interface->formal; i; i = i->next)
16428 : : {
16429 : 32 : if (!strcmp (i->sym->name, c->tb->pass_arg))
16430 : : {
16431 : : me_arg = i->sym;
16432 : : break;
16433 : : }
16434 : 14 : c->tb->pass_arg_num++;
16435 : : }
16436 : :
16437 : 19 : if (!me_arg)
16438 : : {
16439 : 1 : gfc_error ("Procedure pointer component %qs with PASS(%s) "
16440 : : "at %L has no argument %qs", c->name,
16441 : : c->tb->pass_arg, &c->loc, c->tb->pass_arg);
16442 : 1 : c->tb->error = 1;
16443 : 1 : return false;
16444 : : }
16445 : : }
16446 : : else
16447 : : {
16448 : : /* Otherwise, take the first one; there should in fact be at least
16449 : : one. */
16450 : 75 : c->tb->pass_arg_num = 1;
16451 : 75 : if (!c->ts.interface->formal)
16452 : : {
16453 : 3 : gfc_error ("Procedure pointer component %qs with PASS at %L "
16454 : : "must have at least one argument",
16455 : : c->name, &c->loc);
16456 : 3 : c->tb->error = 1;
16457 : 3 : return false;
16458 : : }
16459 : 72 : me_arg = c->ts.interface->formal->sym;
16460 : : }
16461 : :
16462 : : /* Now check that the argument-type matches. */
16463 : 72 : gcc_assert (me_arg);
16464 : 90 : if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
16465 : 89 : || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
16466 : 89 : || (me_arg->ts.type == BT_CLASS
16467 : 81 : && CLASS_DATA (me_arg)->ts.u.derived != sym))
16468 : : {
16469 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
16470 : : " the derived type %qs", me_arg->name, c->name,
16471 : : me_arg->name, &c->loc, sym->name);
16472 : 1 : c->tb->error = 1;
16473 : 1 : return false;
16474 : : }
16475 : :
16476 : : /* Check for F03:C453. */
16477 : 89 : if (CLASS_DATA (me_arg)->attr.dimension)
16478 : : {
16479 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16480 : : "must be scalar", me_arg->name, c->name, me_arg->name,
16481 : : &c->loc);
16482 : 1 : c->tb->error = 1;
16483 : 1 : return false;
16484 : : }
16485 : :
16486 : 88 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16487 : : {
16488 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16489 : : "may not have the POINTER attribute", me_arg->name,
16490 : : c->name, me_arg->name, &c->loc);
16491 : 1 : c->tb->error = 1;
16492 : 1 : return false;
16493 : : }
16494 : :
16495 : 87 : if (CLASS_DATA (me_arg)->attr.allocatable)
16496 : : {
16497 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16498 : : "may not be ALLOCATABLE", me_arg->name, c->name,
16499 : : me_arg->name, &c->loc);
16500 : 1 : c->tb->error = 1;
16501 : 1 : return false;
16502 : : }
16503 : :
16504 : 86 : if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
16505 : : {
16506 : 2 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16507 : : " at %L", c->name, &c->loc);
16508 : 2 : return false;
16509 : : }
16510 : :
16511 : : }
16512 : :
16513 : : /* Check type-spec if this is not the parent-type component. */
16514 : 261193 : if (((sym->attr.is_class
16515 : 11500 : && (!sym->components->ts.u.derived->attr.extension
16516 : 2318 : || c != CLASS_DATA (sym->components)))
16517 : 250989 : || (!sym->attr.is_class
16518 : 249693 : && (!sym->attr.extension || c != sym->components)))
16519 : 253407 : && !sym->attr.vtype
16520 : 412747 : && !resolve_typespec_used (&c->ts, &c->loc, c->name))
16521 : : return false;
16522 : :
16523 : 261192 : super_type = gfc_get_derived_super_type (sym);
16524 : :
16525 : : /* If this type is an extension, set the accessibility of the parent
16526 : : component. */
16527 : 261192 : if (super_type
16528 : 24112 : && ((sym->attr.is_class
16529 : 11500 : && c == CLASS_DATA (sym->components))
16530 : 16094 : || (!sym->attr.is_class && c == sym->components))
16531 : 14508 : && strcmp (super_type->name, c->name) == 0)
16532 : 6364 : c->attr.access = super_type->attr.access;
16533 : :
16534 : : /* If this type is an extension, see if this component has the same name
16535 : : as an inherited type-bound procedure. */
16536 : 24112 : if (super_type && !sym->attr.is_class
16537 : 12612 : && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
16538 : : {
16539 : 1 : gfc_error ("Component %qs of %qs at %L has the same name as an"
16540 : : " inherited type-bound procedure",
16541 : : c->name, sym->name, &c->loc);
16542 : 1 : return false;
16543 : : }
16544 : :
16545 : 261191 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
16546 : 9195 : && !c->ts.deferred)
16547 : : {
16548 : 7044 : if (c->ts.u.cl->length == NULL
16549 : 7038 : || (!resolve_charlen(c->ts.u.cl))
16550 : 14081 : || !gfc_is_constant_expr (c->ts.u.cl->length))
16551 : : {
16552 : 9 : gfc_error ("Character length of component %qs needs to "
16553 : : "be a constant specification expression at %L",
16554 : : c->name,
16555 : 9 : c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
16556 : 9 : return false;
16557 : : }
16558 : :
16559 : 7035 : if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
16560 : : {
16561 : 2 : if (!c->ts.u.cl->length->error)
16562 : : {
16563 : 1 : gfc_error ("Character length expression of component %qs at %L "
16564 : : "must be of INTEGER type, found %s",
16565 : 1 : c->name, &c->ts.u.cl->length->where,
16566 : : gfc_basic_typename (c->ts.u.cl->length->ts.type));
16567 : 1 : c->ts.u.cl->length->error = 1;
16568 : : }
16569 : 2 : return false;
16570 : : }
16571 : : }
16572 : :
16573 : 261180 : if (c->ts.type == BT_CHARACTER && c->ts.deferred
16574 : 2187 : && !c->attr.pointer && !c->attr.allocatable)
16575 : : {
16576 : 1 : gfc_error ("Character component %qs of %qs at %L with deferred "
16577 : : "length must be a POINTER or ALLOCATABLE",
16578 : : c->name, sym->name, &c->loc);
16579 : 1 : return false;
16580 : : }
16581 : :
16582 : : /* Add the hidden deferred length field. */
16583 : 261179 : if (c->ts.type == BT_CHARACTER
16584 : 9423 : && (c->ts.deferred || c->attr.pdt_string)
16585 : 2322 : && !c->attr.function
16586 : 2286 : && !sym->attr.is_class)
16587 : : {
16588 : 2139 : char name[GFC_MAX_SYMBOL_LEN+9];
16589 : 2139 : gfc_component *strlen;
16590 : 2139 : sprintf (name, "_%s_length", c->name);
16591 : 2139 : strlen = gfc_find_component (sym, name, true, true, NULL);
16592 : 2139 : if (strlen == NULL)
16593 : : {
16594 : 447 : if (!gfc_add_component (sym, name, &strlen))
16595 : 0 : return false;
16596 : 447 : strlen->ts.type = BT_INTEGER;
16597 : 447 : strlen->ts.kind = gfc_charlen_int_kind;
16598 : 447 : strlen->attr.access = ACCESS_PRIVATE;
16599 : 447 : strlen->attr.artificial = 1;
16600 : : }
16601 : : }
16602 : :
16603 : 261179 : if (c->ts.type == BT_DERIVED
16604 : 48156 : && sym->component_access != ACCESS_PRIVATE
16605 : 47184 : && gfc_check_symbol_access (sym)
16606 : 92460 : && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
16607 : 46178 : && !c->ts.u.derived->attr.use_assoc
16608 : 24544 : && !gfc_check_symbol_access (c->ts.u.derived)
16609 : 261373 : && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
16610 : : "PRIVATE type and cannot be a component of "
16611 : : "%qs, which is PUBLIC at %L", c->name,
16612 : : sym->name, &sym->declared_at))
16613 : : return false;
16614 : :
16615 : 261178 : if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
16616 : : {
16617 : 2 : gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
16618 : : "type %s", c->name, &c->loc, sym->name);
16619 : 2 : return false;
16620 : : }
16621 : :
16622 : 261176 : if (sym->attr.sequence)
16623 : : {
16624 : 2506 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
16625 : : {
16626 : 0 : gfc_error ("Component %s of SEQUENCE type declared at %L does "
16627 : : "not have the SEQUENCE attribute",
16628 : : c->ts.u.derived->name, &sym->declared_at);
16629 : 0 : return false;
16630 : : }
16631 : : }
16632 : :
16633 : 261176 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
16634 : 0 : c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
16635 : 261176 : else if (c->ts.type == BT_CLASS && c->attr.class_ok
16636 : 6911 : && CLASS_DATA (c)->ts.u.derived->attr.generic)
16637 : 0 : CLASS_DATA (c)->ts.u.derived
16638 : 0 : = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
16639 : :
16640 : : /* If an allocatable component derived type is of the same type as
16641 : : the enclosing derived type, we need a vtable generating so that
16642 : : the __deallocate procedure is created. */
16643 : 261176 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
16644 : 55077 : && c->ts.u.derived == sym && c->attr.allocatable == 1)
16645 : 190 : gfc_find_vtab (&c->ts);
16646 : :
16647 : : /* Ensure that all the derived type components are put on the
16648 : : derived type list; even in formal namespaces, where derived type
16649 : : pointer components might not have been declared. */
16650 : 261176 : if (c->ts.type == BT_DERIVED
16651 : 48155 : && c->ts.u.derived
16652 : 48155 : && c->ts.u.derived->components
16653 : 45017 : && c->attr.pointer
16654 : 31522 : && sym != c->ts.u.derived)
16655 : 4155 : add_dt_to_dt_list (c->ts.u.derived);
16656 : :
16657 : 261176 : if (c->as && c->as->type != AS_DEFERRED
16658 : 5509 : && (c->attr.pointer || c->attr.allocatable))
16659 : : return false;
16660 : :
16661 : 261162 : if (!gfc_resolve_array_spec (c->as,
16662 : 261162 : !(c->attr.pointer || c->attr.proc_pointer
16663 : 212622 : || c->attr.allocatable)))
16664 : : return false;
16665 : :
16666 : 96557 : if (c->initializer && !sym->attr.vtype
16667 : 28020 : && !c->attr.pdt_kind && !c->attr.pdt_len
16668 : 288011 : && !gfc_check_assign_symbol (sym, c, c->initializer))
16669 : : return false;
16670 : :
16671 : : return true;
16672 : : }
16673 : :
16674 : :
16675 : : /* Be nice about the locus for a structure expression - show the locus of the
16676 : : first non-null sub-expression if we can. */
16677 : :
16678 : : static locus *
16679 : 4 : cons_where (gfc_expr *struct_expr)
16680 : : {
16681 : 4 : gfc_constructor *cons;
16682 : :
16683 : 4 : gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
16684 : :
16685 : 4 : cons = gfc_constructor_first (struct_expr->value.constructor);
16686 : 12 : for (; cons; cons = gfc_constructor_next (cons))
16687 : : {
16688 : 8 : if (cons->expr && cons->expr->expr_type != EXPR_NULL)
16689 : 4 : return &cons->expr->where;
16690 : : }
16691 : :
16692 : 0 : return &struct_expr->where;
16693 : : }
16694 : :
16695 : : /* Resolve the components of a structure type. Much less work than derived
16696 : : types. */
16697 : :
16698 : : static bool
16699 : 913 : resolve_fl_struct (gfc_symbol *sym)
16700 : : {
16701 : 913 : gfc_component *c;
16702 : 913 : gfc_expr *init = NULL;
16703 : 913 : bool success;
16704 : :
16705 : : /* Make sure UNIONs do not have overlapping initializers. */
16706 : 913 : if (sym->attr.flavor == FL_UNION)
16707 : : {
16708 : 498 : for (c = sym->components; c; c = c->next)
16709 : : {
16710 : 331 : if (init && c->initializer)
16711 : : {
16712 : 2 : gfc_error ("Conflicting initializers in union at %L and %L",
16713 : : cons_where (init), cons_where (c->initializer));
16714 : 2 : gfc_free_expr (c->initializer);
16715 : 2 : c->initializer = NULL;
16716 : : }
16717 : 291 : if (init == NULL)
16718 : 291 : init = c->initializer;
16719 : : }
16720 : : }
16721 : :
16722 : 913 : success = true;
16723 : 2830 : for (c = sym->components; c; c = c->next)
16724 : 1917 : if (!resolve_component (c, sym))
16725 : 0 : success = false;
16726 : :
16727 : 913 : if (!success)
16728 : : return false;
16729 : :
16730 : 913 : if (sym->components)
16731 : 862 : add_dt_to_dt_list (sym);
16732 : :
16733 : : return true;
16734 : : }
16735 : :
16736 : : /* Figure if the derived type is using itself directly in one of its components
16737 : : or through referencing other derived types. The information is required to
16738 : : generate the __deallocate and __final type bound procedures to ensure
16739 : : freeing larger hierarchies of derived types with allocatable objects. */
16740 : :
16741 : : static void
16742 : 130169 : resolve_cyclic_derived_type (gfc_symbol *derived)
16743 : : {
16744 : 130169 : hash_set<gfc_symbol *> seen, to_examin;
16745 : 130169 : gfc_component *c;
16746 : 130169 : seen.add (derived);
16747 : 130169 : to_examin.add (derived);
16748 : 435682 : while (!to_examin.is_empty ())
16749 : : {
16750 : 177271 : gfc_symbol *cand = *to_examin.begin ();
16751 : 177271 : to_examin.remove (cand);
16752 : 475373 : for (c = cand->components; c; c = c->next)
16753 : 300029 : if (c->ts.type == BT_DERIVED)
16754 : : {
16755 : 65487 : if (c->ts.u.derived == derived)
16756 : : {
16757 : 957 : derived->attr.recursive = 1;
16758 : 1927 : return;
16759 : : }
16760 : 64530 : else if (!seen.contains (c->ts.u.derived))
16761 : : {
16762 : 42731 : seen.add (c->ts.u.derived);
16763 : 42731 : to_examin.add (c->ts.u.derived);
16764 : : }
16765 : : }
16766 : 234542 : else if (c->ts.type == BT_CLASS)
16767 : : {
16768 : 8789 : if (!c->attr.class_ok)
16769 : 7 : continue;
16770 : 8782 : if (CLASS_DATA (c)->ts.u.derived == derived)
16771 : : {
16772 : 970 : derived->attr.recursive = 1;
16773 : 970 : return;
16774 : : }
16775 : 7812 : else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
16776 : : {
16777 : 4583 : seen.add (CLASS_DATA (c)->ts.u.derived);
16778 : 4583 : to_examin.add (CLASS_DATA (c)->ts.u.derived);
16779 : : }
16780 : : }
16781 : : }
16782 : 130169 : }
16783 : :
16784 : : /* Resolve the components of a derived type. This does not have to wait until
16785 : : resolution stage, but can be done as soon as the dt declaration has been
16786 : : parsed. */
16787 : :
16788 : : static bool
16789 : 160315 : resolve_fl_derived0 (gfc_symbol *sym)
16790 : : {
16791 : 160315 : gfc_symbol* super_type;
16792 : 160315 : gfc_component *c;
16793 : 160315 : gfc_formal_arglist *f;
16794 : 160315 : bool success;
16795 : :
16796 : 160315 : if (sym->attr.unlimited_polymorphic)
16797 : : return true;
16798 : :
16799 : 160315 : super_type = gfc_get_derived_super_type (sym);
16800 : :
16801 : : /* F2008, C432. */
16802 : 160315 : if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
16803 : : {
16804 : 2 : gfc_error ("As extending type %qs at %L has a coarray component, "
16805 : : "parent type %qs shall also have one", sym->name,
16806 : : &sym->declared_at, super_type->name);
16807 : 2 : return false;
16808 : : }
16809 : :
16810 : : /* Ensure the extended type gets resolved before we do. */
16811 : 16412 : if (super_type && !resolve_fl_derived0 (super_type))
16812 : : return false;
16813 : :
16814 : : /* An ABSTRACT type must be extensible. */
16815 : 160307 : if (sym->attr.abstract && !gfc_type_is_extensible (sym))
16816 : : {
16817 : 2 : gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
16818 : : sym->name, &sym->declared_at);
16819 : 2 : return false;
16820 : : }
16821 : :
16822 : : /* Resolving components below, may create vtabs for which the cyclic type
16823 : : information needs to be present. */
16824 : 160305 : if (!sym->attr.vtype)
16825 : 130169 : resolve_cyclic_derived_type (sym);
16826 : :
16827 : 160305 : c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
16828 : : : sym->components;
16829 : :
16830 : : success = true;
16831 : 542234 : for ( ; c != NULL; c = c->next)
16832 : 381929 : if (!resolve_component (c, sym))
16833 : 83 : success = false;
16834 : :
16835 : 160305 : if (!success)
16836 : : return false;
16837 : :
16838 : : /* Now add the caf token field, where needed. */
16839 : 160231 : if (flag_coarray != GFC_FCOARRAY_NONE
16840 : 3252 : && !sym->attr.is_class && !sym->attr.vtype)
16841 : : {
16842 : 4929 : for (c = sym->components; c; c = c->next)
16843 : 3010 : if (!c->attr.dimension && !c->attr.codimension
16844 : 2254 : && (c->attr.allocatable || c->attr.pointer))
16845 : : {
16846 : 616 : char name[GFC_MAX_SYMBOL_LEN+9];
16847 : 616 : gfc_component *token;
16848 : 616 : sprintf (name, "_caf_%s", c->name);
16849 : 616 : token = gfc_find_component (sym, name, true, true, NULL);
16850 : 616 : if (token == NULL)
16851 : : {
16852 : 136 : if (!gfc_add_component (sym, name, &token))
16853 : 0 : return false;
16854 : 136 : token->ts.type = BT_VOID;
16855 : 136 : token->ts.kind = gfc_default_integer_kind;
16856 : 136 : token->attr.access = ACCESS_PRIVATE;
16857 : 136 : token->attr.artificial = 1;
16858 : 136 : token->attr.caf_token = 1;
16859 : : }
16860 : 616 : c->caf_token = token;
16861 : : }
16862 : : }
16863 : :
16864 : 160231 : check_defined_assignments (sym);
16865 : :
16866 : 160231 : if (!sym->attr.defined_assign_comp && super_type)
16867 : 15582 : sym->attr.defined_assign_comp
16868 : 15582 : = super_type->attr.defined_assign_comp;
16869 : :
16870 : : /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
16871 : : all DEFERRED bindings are overridden. */
16872 : 16405 : if (super_type && super_type->attr.abstract && !sym->attr.abstract
16873 : 1355 : && !sym->attr.is_class
16874 : 2880 : && !ensure_not_abstract (sym, super_type))
16875 : : return false;
16876 : :
16877 : : /* Check that there is a component for every PDT parameter. */
16878 : 160225 : if (sym->attr.pdt_template)
16879 : : {
16880 : 970 : for (f = sym->formal; f; f = f->next)
16881 : : {
16882 : 642 : if (!f->sym)
16883 : 1 : continue;
16884 : 641 : c = gfc_find_component (sym, f->sym->name, true, true, NULL);
16885 : 641 : if (c == NULL)
16886 : : {
16887 : 9 : gfc_error ("Parameterized type %qs does not have a component "
16888 : : "corresponding to parameter %qs at %L", sym->name,
16889 : 9 : f->sym->name, &sym->declared_at);
16890 : 9 : break;
16891 : : }
16892 : : }
16893 : : }
16894 : :
16895 : : /* Add derived type to the derived type list. */
16896 : 160225 : add_dt_to_dt_list (sym);
16897 : :
16898 : 160225 : return true;
16899 : : }
16900 : :
16901 : : /* The following procedure does the full resolution of a derived type,
16902 : : including resolution of all type-bound procedures (if present). In contrast
16903 : : to 'resolve_fl_derived0' this can only be done after the module has been
16904 : : parsed completely. */
16905 : :
16906 : : static bool
16907 : 83449 : resolve_fl_derived (gfc_symbol *sym)
16908 : : {
16909 : 83449 : gfc_symbol *gen_dt = NULL;
16910 : :
16911 : 83449 : if (sym->attr.unlimited_polymorphic)
16912 : : return true;
16913 : :
16914 : 83449 : if (!sym->attr.is_class)
16915 : 71595 : gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
16916 : 53378 : if (gen_dt && gen_dt->generic && gen_dt->generic->next
16917 : 2169 : && (!gen_dt->generic->sym->attr.use_assoc
16918 : 2069 : || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
16919 : 83582 : && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
16920 : : "%qs at %L being the same name as derived "
16921 : : "type at %L", sym->name,
16922 : : gen_dt->generic->sym == sym
16923 : 11 : ? gen_dt->generic->next->sym->name
16924 : : : gen_dt->generic->sym->name,
16925 : : gen_dt->generic->sym == sym
16926 : 11 : ? &gen_dt->generic->next->sym->declared_at
16927 : : : &gen_dt->generic->sym->declared_at,
16928 : : &sym->declared_at))
16929 : : return false;
16930 : :
16931 : 83445 : if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
16932 : : {
16933 : 13 : gfc_error ("Derived type %qs at %L has not been declared",
16934 : : sym->name, &sym->declared_at);
16935 : 13 : return false;
16936 : : }
16937 : :
16938 : : /* Resolve the finalizer procedures. */
16939 : 83432 : if (!gfc_resolve_finalizers (sym, NULL))
16940 : : return false;
16941 : :
16942 : 83429 : if (sym->attr.is_class && sym->ts.u.derived == NULL)
16943 : : {
16944 : : /* Fix up incomplete CLASS symbols. */
16945 : 11854 : gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
16946 : 11854 : gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
16947 : :
16948 : : /* Nothing more to do for unlimited polymorphic entities. */
16949 : 11854 : if (data->ts.u.derived->attr.unlimited_polymorphic)
16950 : : {
16951 : 1933 : add_dt_to_dt_list (sym);
16952 : 1933 : return true;
16953 : : }
16954 : 9921 : else if (vptr->ts.u.derived == NULL)
16955 : : {
16956 : 5958 : gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
16957 : 5958 : gcc_assert (vtab);
16958 : 5958 : vptr->ts.u.derived = vtab->ts.u.derived;
16959 : 5958 : if (!resolve_fl_derived0 (vptr->ts.u.derived))
16960 : : return false;
16961 : : }
16962 : : }
16963 : :
16964 : 81496 : if (!resolve_fl_derived0 (sym))
16965 : : return false;
16966 : :
16967 : : /* Resolve the type-bound procedures. */
16968 : 81418 : if (!resolve_typebound_procedures (sym))
16969 : : return false;
16970 : :
16971 : : /* Generate module vtables subject to their accessibility and their not
16972 : : being vtables or pdt templates. If this is not done class declarations
16973 : : in external procedures wind up with their own version and so SELECT TYPE
16974 : : fails because the vptrs do not have the same address. */
16975 : 81377 : if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
16976 : 81316 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
16977 : 61224 : || (sym->attr.recursive && sym->attr.alloc_comp))
16978 : 20196 : && sym->attr.access != ACCESS_PRIVATE
16979 : 20163 : && !(sym->attr.vtype || sym->attr.pdt_template))
16980 : : {
16981 : 18305 : gfc_symbol *vtab = gfc_find_derived_vtab (sym);
16982 : 18305 : gfc_set_sym_referenced (vtab);
16983 : : }
16984 : :
16985 : : return true;
16986 : : }
16987 : :
16988 : :
16989 : : static bool
16990 : 805 : resolve_fl_namelist (gfc_symbol *sym)
16991 : : {
16992 : 805 : gfc_namelist *nl;
16993 : 805 : gfc_symbol *nlsym;
16994 : :
16995 : 2892 : for (nl = sym->namelist; nl; nl = nl->next)
16996 : : {
16997 : : /* Check again, the check in match only works if NAMELIST comes
16998 : : after the decl. */
16999 : 2092 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
17000 : : {
17001 : 1 : gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
17002 : : "allowed", nl->sym->name, sym->name, &sym->declared_at);
17003 : 1 : return false;
17004 : : }
17005 : :
17006 : 652 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
17007 : 2099 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17008 : : "with assumed shape in namelist %qs at %L",
17009 : : nl->sym->name, sym->name, &sym->declared_at))
17010 : : return false;
17011 : :
17012 : 2090 : if (is_non_constant_shape_array (nl->sym)
17013 : 2140 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17014 : : "with nonconstant shape in namelist %qs at %L",
17015 : 50 : nl->sym->name, sym->name, &sym->declared_at))
17016 : : return false;
17017 : :
17018 : 2089 : if (nl->sym->ts.type == BT_CHARACTER
17019 : 565 : && (nl->sym->ts.u.cl->length == NULL
17020 : 526 : || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
17021 : 2171 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
17022 : : "nonconstant character length in "
17023 : 82 : "namelist %qs at %L", nl->sym->name,
17024 : : sym->name, &sym->declared_at))
17025 : : return false;
17026 : :
17027 : : }
17028 : :
17029 : : /* Reject PRIVATE objects in a PUBLIC namelist. */
17030 : 800 : if (gfc_check_symbol_access (sym))
17031 : : {
17032 : 2873 : for (nl = sym->namelist; nl; nl = nl->next)
17033 : : {
17034 : 2086 : if (!nl->sym->attr.use_assoc
17035 : 3948 : && !is_sym_host_assoc (nl->sym, sym->ns)
17036 : 4038 : && !gfc_check_symbol_access (nl->sym))
17037 : : {
17038 : 2 : gfc_error ("NAMELIST object %qs was declared PRIVATE and "
17039 : : "cannot be member of PUBLIC namelist %qs at %L",
17040 : 2 : nl->sym->name, sym->name, &sym->declared_at);
17041 : 2 : return false;
17042 : : }
17043 : :
17044 : 2084 : if (nl->sym->ts.type == BT_DERIVED
17045 : 466 : && (nl->sym->ts.u.derived->attr.alloc_comp
17046 : 464 : || nl->sym->ts.u.derived->attr.pointer_comp))
17047 : : {
17048 : 5 : if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
17049 : : "namelist %qs at %L with ALLOCATABLE "
17050 : : "or POINTER components", nl->sym->name,
17051 : : sym->name, &sym->declared_at))
17052 : : return false;
17053 : : return true;
17054 : : }
17055 : :
17056 : : /* Types with private components that came here by USE-association. */
17057 : 2079 : if (nl->sym->ts.type == BT_DERIVED
17058 : 2079 : && derived_inaccessible (nl->sym->ts.u.derived))
17059 : : {
17060 : 6 : gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
17061 : : "components and cannot be member of namelist %qs at %L",
17062 : : nl->sym->name, sym->name, &sym->declared_at);
17063 : 6 : return false;
17064 : : }
17065 : :
17066 : : /* Types with private components that are defined in the same module. */
17067 : 2073 : if (nl->sym->ts.type == BT_DERIVED
17068 : 910 : && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
17069 : 2351 : && nl->sym->ts.u.derived->attr.private_comp)
17070 : : {
17071 : 0 : gfc_error ("NAMELIST object %qs has PRIVATE components and "
17072 : : "cannot be a member of PUBLIC namelist %qs at %L",
17073 : : nl->sym->name, sym->name, &sym->declared_at);
17074 : 0 : return false;
17075 : : }
17076 : : }
17077 : : }
17078 : :
17079 : :
17080 : : /* 14.1.2 A module or internal procedure represent local entities
17081 : : of the same type as a namelist member and so are not allowed. */
17082 : 2857 : for (nl = sym->namelist; nl; nl = nl->next)
17083 : : {
17084 : 2073 : if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
17085 : 1514 : continue;
17086 : :
17087 : 559 : if (nl->sym->attr.function && nl->sym == nl->sym->result)
17088 : 7 : if ((nl->sym == sym->ns->proc_name)
17089 : 1 : ||
17090 : 1 : (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
17091 : 6 : continue;
17092 : :
17093 : 553 : nlsym = NULL;
17094 : 553 : if (nl->sym->name)
17095 : 553 : gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
17096 : 553 : if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
17097 : : {
17098 : 3 : gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
17099 : : "attribute in %qs at %L", nlsym->name,
17100 : : &sym->declared_at);
17101 : 3 : return false;
17102 : : }
17103 : : }
17104 : :
17105 : : return true;
17106 : : }
17107 : :
17108 : :
17109 : : static bool
17110 : 370100 : resolve_fl_parameter (gfc_symbol *sym)
17111 : : {
17112 : : /* A parameter array's shape needs to be constant. */
17113 : 370100 : if (sym->as != NULL
17114 : 370100 : && (sym->as->type == AS_DEFERRED
17115 : 6076 : || is_non_constant_shape_array (sym)))
17116 : : {
17117 : 17 : gfc_error ("Parameter array %qs at %L cannot be automatic "
17118 : : "or of deferred shape", sym->name, &sym->declared_at);
17119 : 17 : return false;
17120 : : }
17121 : :
17122 : : /* Constraints on deferred type parameter. */
17123 : 370083 : if (!deferred_requirements (sym))
17124 : : return false;
17125 : :
17126 : : /* Make sure a parameter that has been implicitly typed still
17127 : : matches the implicit type, since PARAMETER statements can precede
17128 : : IMPLICIT statements. */
17129 : 370082 : if (sym->attr.implicit_type
17130 : 370791 : && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
17131 : 709 : sym->ns)))
17132 : : {
17133 : 0 : gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
17134 : : "later IMPLICIT type", sym->name, &sym->declared_at);
17135 : 0 : return false;
17136 : : }
17137 : :
17138 : : /* Make sure the types of derived parameters are consistent. This
17139 : : type checking is deferred until resolution because the type may
17140 : : refer to a derived type from the host. */
17141 : 370082 : if (sym->ts.type == BT_DERIVED
17142 : 370082 : && !gfc_compare_types (&sym->ts, &sym->value->ts))
17143 : : {
17144 : 0 : gfc_error ("Incompatible derived type in PARAMETER at %L",
17145 : 0 : &sym->value->where);
17146 : 0 : return false;
17147 : : }
17148 : :
17149 : : /* F03:C509,C514. */
17150 : 370082 : if (sym->ts.type == BT_CLASS)
17151 : : {
17152 : 0 : gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
17153 : : sym->name, &sym->declared_at);
17154 : 0 : return false;
17155 : : }
17156 : :
17157 : : return true;
17158 : : }
17159 : :
17160 : :
17161 : : /* Called by resolve_symbol to check PDTs. */
17162 : :
17163 : : static void
17164 : 522 : resolve_pdt (gfc_symbol* sym)
17165 : : {
17166 : 522 : gfc_symbol *derived = NULL;
17167 : 522 : gfc_actual_arglist *param;
17168 : 522 : gfc_component *c;
17169 : 522 : bool const_len_exprs = true;
17170 : 522 : bool assumed_len_exprs = false;
17171 : 522 : symbol_attribute *attr;
17172 : :
17173 : 522 : if (sym->ts.type == BT_DERIVED)
17174 : : {
17175 : 462 : derived = sym->ts.u.derived;
17176 : 462 : attr = &(sym->attr);
17177 : : }
17178 : 60 : else if (sym->ts.type == BT_CLASS)
17179 : : {
17180 : 60 : derived = CLASS_DATA (sym)->ts.u.derived;
17181 : 60 : attr = &(CLASS_DATA (sym)->attr);
17182 : : }
17183 : : else
17184 : 0 : gcc_unreachable ();
17185 : :
17186 : 522 : gcc_assert (derived->attr.pdt_type);
17187 : :
17188 : 1383 : for (param = sym->param_list; param; param = param->next)
17189 : : {
17190 : 861 : c = gfc_find_component (derived, param->name, false, true, NULL);
17191 : 861 : gcc_assert (c);
17192 : 861 : if (c->attr.pdt_kind)
17193 : 386 : continue;
17194 : :
17195 : 292 : if (param->expr && !gfc_is_constant_expr (param->expr)
17196 : 502 : && c->attr.pdt_len)
17197 : : const_len_exprs = false;
17198 : 448 : else if (param->spec_type == SPEC_ASSUMED)
17199 : 142 : assumed_len_exprs = true;
17200 : :
17201 : 475 : if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
17202 : 10 : && ((sym->ts.type == BT_DERIVED && !attr->pointer)
17203 : 8 : || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
17204 : 3 : gfc_error ("Entity %qs at %L has a deferred LEN "
17205 : : "parameter %qs and requires either the POINTER "
17206 : : "or ALLOCATABLE attribute",
17207 : : sym->name, &sym->declared_at,
17208 : : param->name);
17209 : :
17210 : : }
17211 : :
17212 : 522 : if (!const_len_exprs
17213 : 27 : && (sym->ns->proc_name->attr.is_main_program
17214 : 26 : || sym->ns->proc_name->attr.flavor == FL_MODULE
17215 : 25 : || sym->attr.save != SAVE_NONE))
17216 : 2 : gfc_error ("The AUTOMATIC object %qs at %L must not have the "
17217 : : "SAVE attribute or be a variable declared in the "
17218 : : "main program, a module or a submodule(F08/C513)",
17219 : : sym->name, &sym->declared_at);
17220 : :
17221 : 522 : if (assumed_len_exprs && !(sym->attr.dummy
17222 : 1 : || sym->attr.select_type_temporary || sym->attr.associate_var))
17223 : 1 : gfc_error ("The object %qs at %L with ASSUMED type parameters "
17224 : : "must be a dummy or a SELECT TYPE selector(F08/4.2)",
17225 : : sym->name, &sym->declared_at);
17226 : 522 : }
17227 : :
17228 : :
17229 : : /* Resolve the symbol's array spec. */
17230 : :
17231 : : static bool
17232 : 1631420 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
17233 : : {
17234 : 1631420 : gfc_namespace *orig_current_ns = gfc_current_ns;
17235 : 1631420 : gfc_current_ns = gfc_get_spec_ns (sym);
17236 : :
17237 : 1631420 : bool saved_specification_expr = specification_expr;
17238 : 1631420 : specification_expr = true;
17239 : :
17240 : 1631420 : bool result = gfc_resolve_array_spec (sym->as, check_constant);
17241 : :
17242 : 1631420 : specification_expr = saved_specification_expr;
17243 : 1631420 : gfc_current_ns = orig_current_ns;
17244 : :
17245 : 1631420 : return result;
17246 : : }
17247 : :
17248 : :
17249 : : /* Do anything necessary to resolve a symbol. Right now, we just
17250 : : assume that an otherwise unknown symbol is a variable. This sort
17251 : : of thing commonly happens for symbols in module. */
17252 : :
17253 : : static void
17254 : 1753038 : resolve_symbol (gfc_symbol *sym)
17255 : : {
17256 : 1753038 : int check_constant, mp_flag;
17257 : 1753038 : gfc_symtree *symtree;
17258 : 1753038 : gfc_symtree *this_symtree;
17259 : 1753038 : gfc_namespace *ns;
17260 : 1753038 : gfc_component *c;
17261 : 1753038 : symbol_attribute class_attr;
17262 : 1753038 : gfc_array_spec *as;
17263 : :
17264 : 1753038 : if (sym->resolve_symbol_called >= 1)
17265 : 152720 : return;
17266 : 1686491 : sym->resolve_symbol_called = 1;
17267 : :
17268 : : /* No symbol will ever have union type; only components can be unions.
17269 : : Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
17270 : : (just like derived type declaration symbols have flavor FL_DERIVED). */
17271 : 1686491 : gcc_assert (sym->ts.type != BT_UNION);
17272 : :
17273 : : /* Coarrayed polymorphic objects with allocatable or pointer components are
17274 : : yet unsupported for -fcoarray=lib. */
17275 : 1686491 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
17276 : 86 : && sym->ts.u.derived && CLASS_DATA (sym)
17277 : 86 : && CLASS_DATA (sym)->attr.codimension
17278 : 72 : && CLASS_DATA (sym)->ts.u.derived
17279 : 71 : && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
17280 : 68 : || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
17281 : : {
17282 : 6 : gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
17283 : : "type coarrays at %L are unsupported", &sym->declared_at);
17284 : 6 : return;
17285 : : }
17286 : :
17287 : 1686485 : if (sym->attr.artificial)
17288 : : return;
17289 : :
17290 : 1602868 : if (sym->attr.unlimited_polymorphic)
17291 : : return;
17292 : :
17293 : 1601477 : if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
17294 : : {
17295 : 4 : gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
17296 : : "the OpenMP DEPEND clause", &sym->declared_at);
17297 : 4 : return;
17298 : : }
17299 : :
17300 : 1601473 : if (sym->attr.flavor == FL_UNKNOWN
17301 : 1580923 : || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
17302 : 425951 : && !sym->attr.generic && !sym->attr.external
17303 : 174766 : && sym->attr.if_source == IFSRC_UNKNOWN
17304 : 78271 : && sym->ts.type == BT_UNKNOWN))
17305 : : {
17306 : : /* A symbol in a common block might not have been resolved yet properly.
17307 : : Do not try to find an interface with the same name. */
17308 : 90622 : if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
17309 : 20546 : && !sym->attr.generic && !sym->attr.external
17310 : 20496 : && sym->attr.in_common)
17311 : 2530 : goto skip_interfaces;
17312 : :
17313 : : /* If we find that a flavorless symbol is an interface in one of the
17314 : : parent namespaces, find its symtree in this namespace, free the
17315 : : symbol and set the symtree to point to the interface symbol. */
17316 : 125763 : for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
17317 : : {
17318 : 38312 : symtree = gfc_find_symtree (ns->sym_root, sym->name);
17319 : 38312 : if (symtree && (symtree->n.sym->generic ||
17320 : 678 : (symtree->n.sym->attr.flavor == FL_PROCEDURE
17321 : 591 : && sym->ns->construct_entities)))
17322 : : {
17323 : 649 : this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
17324 : : sym->name);
17325 : 649 : if (this_symtree->n.sym == sym)
17326 : : {
17327 : 641 : symtree->n.sym->refs++;
17328 : 641 : gfc_release_symbol (sym);
17329 : 641 : this_symtree->n.sym = symtree->n.sym;
17330 : 641 : return;
17331 : : }
17332 : : }
17333 : : }
17334 : :
17335 : 87451 : skip_interfaces:
17336 : : /* Otherwise give it a flavor according to such attributes as
17337 : : it has. */
17338 : 89981 : if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
17339 : 20372 : && sym->attr.intrinsic == 0)
17340 : 20368 : sym->attr.flavor = FL_VARIABLE;
17341 : 69613 : else if (sym->attr.flavor == FL_UNKNOWN)
17342 : : {
17343 : 54 : sym->attr.flavor = FL_PROCEDURE;
17344 : 54 : if (sym->attr.dimension)
17345 : 0 : sym->attr.function = 1;
17346 : : }
17347 : : }
17348 : :
17349 : 1600832 : if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
17350 : 2303 : gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
17351 : :
17352 : 1437 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
17353 : 1602269 : && !resolve_procedure_interface (sym))
17354 : : return;
17355 : :
17356 : 1600821 : if (sym->attr.is_protected && !sym->attr.proc_pointer
17357 : 130 : && (sym->attr.procedure || sym->attr.external))
17358 : : {
17359 : 0 : if (sym->attr.external)
17360 : 0 : gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
17361 : : "at %L", &sym->declared_at);
17362 : : else
17363 : 0 : gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
17364 : : "at %L", &sym->declared_at);
17365 : :
17366 : 0 : return;
17367 : : }
17368 : :
17369 : : /* Ensure that variables of derived or class type having a finalizer are
17370 : : marked used even when the variable is not used anything else in the scope.
17371 : : This fixes PR118730. */
17372 : 624881 : if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
17373 : 425785 : && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
17374 : 1648676 : && gfc_may_be_finalized (sym->ts))
17375 : 7994 : gfc_set_sym_referenced (sym);
17376 : :
17377 : 1600821 : if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
17378 : : return;
17379 : :
17380 : 1600051 : else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
17381 : 1600814 : && !resolve_fl_struct (sym))
17382 : : return;
17383 : :
17384 : : /* Symbols that are module procedures with results (functions) have
17385 : : the types and array specification copied for type checking in
17386 : : procedures that call them, as well as for saving to a module
17387 : : file. These symbols can't stand the scrutiny that their results
17388 : : can. */
17389 : 1600682 : mp_flag = (sym->result != NULL && sym->result != sym);
17390 : :
17391 : : /* Make sure that the intrinsic is consistent with its internal
17392 : : representation. This needs to be done before assigning a default
17393 : : type to avoid spurious warnings. */
17394 : 1568139 : if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
17395 : 1631850 : && !gfc_resolve_intrinsic (sym, &sym->declared_at))
17396 : : return;
17397 : :
17398 : : /* Resolve associate names. */
17399 : 1600646 : if (sym->assoc)
17400 : 6396 : resolve_assoc_var (sym, true);
17401 : :
17402 : : /* Assign default type to symbols that need one and don't have one. */
17403 : 1600646 : if (sym->ts.type == BT_UNKNOWN)
17404 : : {
17405 : 381267 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
17406 : : {
17407 : 11711 : gfc_set_default_type (sym, 1, NULL);
17408 : : }
17409 : :
17410 : 245358 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
17411 : 55294 : && !sym->attr.function && !sym->attr.subroutine
17412 : 382880 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
17413 : 564 : gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
17414 : :
17415 : 381267 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17416 : : {
17417 : : /* The specific case of an external procedure should emit an error
17418 : : in the case that there is no implicit type. */
17419 : 98237 : if (!mp_flag)
17420 : : {
17421 : 92479 : if (!sym->attr.mixed_entry_master)
17422 : 92373 : gfc_set_default_type (sym, sym->attr.external, NULL);
17423 : : }
17424 : : else
17425 : : {
17426 : : /* Result may be in another namespace. */
17427 : 5758 : resolve_symbol (sym->result);
17428 : :
17429 : 5758 : if (!sym->result->attr.proc_pointer)
17430 : : {
17431 : 5579 : sym->ts = sym->result->ts;
17432 : 5579 : sym->as = gfc_copy_array_spec (sym->result->as);
17433 : 5579 : sym->attr.dimension = sym->result->attr.dimension;
17434 : 5579 : sym->attr.codimension = sym->result->attr.codimension;
17435 : 5579 : sym->attr.pointer = sym->result->attr.pointer;
17436 : 5579 : sym->attr.allocatable = sym->result->attr.allocatable;
17437 : 5579 : sym->attr.contiguous = sym->result->attr.contiguous;
17438 : : }
17439 : : }
17440 : : }
17441 : : }
17442 : 1219379 : else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17443 : 31099 : resolve_symbol_array_spec (sym->result, false);
17444 : :
17445 : : /* For a CLASS-valued function with a result variable, affirm that it has
17446 : : been resolved also when looking at the symbol 'sym'. */
17447 : 412366 : if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
17448 : 713 : sym->attr.class_ok = sym->result->attr.class_ok;
17449 : :
17450 : 1600646 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
17451 : 18421 : && CLASS_DATA (sym))
17452 : : {
17453 : 18420 : as = CLASS_DATA (sym)->as;
17454 : 18420 : class_attr = CLASS_DATA (sym)->attr;
17455 : 18420 : class_attr.pointer = class_attr.class_pointer;
17456 : : }
17457 : : else
17458 : : {
17459 : 1582226 : class_attr = sym->attr;
17460 : 1582226 : as = sym->as;
17461 : : }
17462 : :
17463 : : /* F2008, C530. */
17464 : 1600646 : if (sym->attr.contiguous
17465 : 6888 : && (!class_attr.dimension
17466 : 6885 : || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
17467 : 125 : && !class_attr.pointer)))
17468 : : {
17469 : 7 : gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
17470 : : "array pointer or an assumed-shape or assumed-rank array",
17471 : : sym->name, &sym->declared_at);
17472 : 7 : return;
17473 : : }
17474 : :
17475 : : /* Assumed size arrays and assumed shape arrays must be dummy
17476 : : arguments. Array-spec's of implied-shape should have been resolved to
17477 : : AS_EXPLICIT already. */
17478 : :
17479 : 1593879 : if (as)
17480 : : {
17481 : : /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
17482 : : specification expression. */
17483 : 138315 : if (as->type == AS_IMPLIED_SHAPE)
17484 : : {
17485 : : int i;
17486 : 1 : for (i=0; i<as->rank; i++)
17487 : : {
17488 : 1 : if (as->lower[i] != NULL && as->upper[i] == NULL)
17489 : : {
17490 : 1 : gfc_error ("Bad specification for assumed size array at %L",
17491 : : &as->lower[i]->where);
17492 : 1 : return;
17493 : : }
17494 : : }
17495 : 0 : gcc_unreachable();
17496 : : }
17497 : :
17498 : 138314 : if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
17499 : 107695 : || as->type == AS_ASSUMED_SHAPE)
17500 : 41843 : && !sym->attr.dummy && !sym->attr.select_type_temporary
17501 : 8 : && !sym->attr.associate_var)
17502 : : {
17503 : 7 : if (as->type == AS_ASSUMED_SIZE)
17504 : 7 : gfc_error ("Assumed size array at %L must be a dummy argument",
17505 : : &sym->declared_at);
17506 : : else
17507 : 0 : gfc_error ("Assumed shape array at %L must be a dummy argument",
17508 : : &sym->declared_at);
17509 : 7 : return;
17510 : : }
17511 : : /* TS 29113, C535a. */
17512 : 138307 : if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
17513 : 60 : && !sym->attr.select_type_temporary
17514 : 60 : && !(cs_base && cs_base->current
17515 : 45 : && (cs_base->current->op == EXEC_SELECT_RANK
17516 : 3 : || ((gfc_option.allow_std & GFC_STD_F202Y)
17517 : 0 : && cs_base->current->op == EXEC_BLOCK))))
17518 : : {
17519 : 18 : gfc_error ("Assumed-rank array at %L must be a dummy argument",
17520 : : &sym->declared_at);
17521 : 18 : return;
17522 : : }
17523 : 138289 : if (as->type == AS_ASSUMED_RANK
17524 : 23471 : && (sym->attr.codimension || sym->attr.value))
17525 : : {
17526 : 2 : gfc_error ("Assumed-rank array at %L may not have the VALUE or "
17527 : : "CODIMENSION attribute", &sym->declared_at);
17528 : 2 : return;
17529 : : }
17530 : : }
17531 : :
17532 : : /* Make sure symbols with known intent or optional are really dummy
17533 : : variable. Because of ENTRY statement, this has to be deferred
17534 : : until resolution time. */
17535 : :
17536 : 1600611 : if (!sym->attr.dummy
17537 : 1152765 : && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
17538 : : {
17539 : 2 : gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
17540 : 2 : return;
17541 : : }
17542 : :
17543 : 1600609 : if (sym->attr.value && !sym->attr.dummy)
17544 : : {
17545 : 2 : gfc_error ("%qs at %L cannot have the VALUE attribute because "
17546 : : "it is not a dummy argument", sym->name, &sym->declared_at);
17547 : 2 : return;
17548 : : }
17549 : :
17550 : 1600607 : if (sym->attr.value && sym->ts.type == BT_CHARACTER)
17551 : : {
17552 : 546 : gfc_charlen *cl = sym->ts.u.cl;
17553 : 546 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
17554 : : {
17555 : 2 : gfc_error ("Character dummy variable %qs at %L with VALUE "
17556 : : "attribute must have constant length",
17557 : : sym->name, &sym->declared_at);
17558 : 2 : return;
17559 : : }
17560 : :
17561 : 544 : if (sym->ts.is_c_interop
17562 : 376 : && mpz_cmp_si (cl->length->value.integer, 1) != 0)
17563 : : {
17564 : 1 : gfc_error ("C interoperable character dummy variable %qs at %L "
17565 : : "with VALUE attribute must have length one",
17566 : : sym->name, &sym->declared_at);
17567 : 1 : return;
17568 : : }
17569 : : }
17570 : :
17571 : 1600604 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17572 : 118346 : && sym->ts.u.derived->attr.generic)
17573 : : {
17574 : 20 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
17575 : 20 : if (!sym->ts.u.derived)
17576 : : {
17577 : 0 : gfc_error ("The derived type %qs at %L is of type %qs, "
17578 : : "which has not been defined", sym->name,
17579 : : &sym->declared_at, sym->ts.u.derived->name);
17580 : 0 : sym->ts.type = BT_UNKNOWN;
17581 : 0 : return;
17582 : : }
17583 : : }
17584 : :
17585 : : /* Use the same constraints as TYPE(*), except for the type check
17586 : : and that only scalars and assumed-size arrays are permitted. */
17587 : 1600604 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
17588 : : {
17589 : 11332 : if (!sym->attr.dummy)
17590 : : {
17591 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17592 : : "a dummy argument", sym->name, &sym->declared_at);
17593 : 1 : return;
17594 : : }
17595 : :
17596 : 11331 : if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
17597 : 8 : && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
17598 : 0 : && sym->ts.type != BT_COMPLEX)
17599 : : {
17600 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17601 : : "of type TYPE(*) or of an numeric intrinsic type",
17602 : : sym->name, &sym->declared_at);
17603 : 0 : return;
17604 : : }
17605 : :
17606 : 11331 : if (sym->attr.allocatable || sym->attr.codimension
17607 : 11329 : || sym->attr.pointer || sym->attr.value)
17608 : : {
17609 : 4 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17610 : : "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
17611 : : "attribute", sym->name, &sym->declared_at);
17612 : 4 : return;
17613 : : }
17614 : :
17615 : 11327 : if (sym->attr.intent == INTENT_OUT)
17616 : : {
17617 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17618 : : "have the INTENT(OUT) attribute",
17619 : : sym->name, &sym->declared_at);
17620 : 0 : return;
17621 : : }
17622 : 11327 : if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
17623 : : {
17624 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
17625 : : "either be a scalar or an assumed-size array",
17626 : : sym->name, &sym->declared_at);
17627 : 1 : return;
17628 : : }
17629 : :
17630 : : /* Set the type to TYPE(*) and add a dimension(*) to ensure
17631 : : NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
17632 : : packing. */
17633 : 11326 : sym->ts.type = BT_ASSUMED;
17634 : 11326 : sym->as = gfc_get_array_spec ();
17635 : 11326 : sym->as->type = AS_ASSUMED_SIZE;
17636 : 11326 : sym->as->rank = 1;
17637 : 11326 : sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
17638 : : }
17639 : 1589272 : else if (sym->ts.type == BT_ASSUMED)
17640 : : {
17641 : : /* TS 29113, C407a. */
17642 : 8226 : if (!sym->attr.dummy)
17643 : : {
17644 : 7 : gfc_error ("Assumed type of variable %s at %L is only permitted "
17645 : : "for dummy variables", sym->name, &sym->declared_at);
17646 : 7 : return;
17647 : : }
17648 : 8219 : if (sym->attr.allocatable || sym->attr.codimension
17649 : 8215 : || sym->attr.pointer || sym->attr.value)
17650 : : {
17651 : 8 : gfc_error ("Assumed-type variable %s at %L may not have the "
17652 : : "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
17653 : : sym->name, &sym->declared_at);
17654 : 8 : return;
17655 : : }
17656 : 8211 : if (sym->attr.intent == INTENT_OUT)
17657 : : {
17658 : 2 : gfc_error ("Assumed-type variable %s at %L may not have the "
17659 : : "INTENT(OUT) attribute",
17660 : : sym->name, &sym->declared_at);
17661 : 2 : return;
17662 : : }
17663 : 8209 : if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
17664 : : {
17665 : 3 : gfc_error ("Assumed-type variable %s at %L shall not be an "
17666 : : "explicit-shape array", sym->name, &sym->declared_at);
17667 : 3 : return;
17668 : : }
17669 : : }
17670 : :
17671 : : /* If the symbol is marked as bind(c), that it is declared at module level
17672 : : scope and verify its type and kind. Do not do the latter for symbols
17673 : : that are implicitly typed because that is handled in
17674 : : gfc_set_default_type. Handle dummy arguments and procedure definitions
17675 : : separately. Also, anything that is use associated is not handled here
17676 : : but instead is handled in the module it is declared in. Finally, derived
17677 : : type definitions are allowed to be BIND(C) since that only implies that
17678 : : they're interoperable, and they are checked fully for interoperability
17679 : : when a variable is declared of that type. */
17680 : 1600578 : if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
17681 : 6716 : && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
17682 : 567 : && sym->attr.flavor != FL_DERIVED)
17683 : : {
17684 : 167 : bool t = true;
17685 : :
17686 : : /* First, make sure the variable is declared at the
17687 : : module-level scope (J3/04-007, Section 15.3). */
17688 : 167 : if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
17689 : 7 : && !sym->attr.in_common)
17690 : : {
17691 : 6 : gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
17692 : : "is neither a COMMON block nor declared at the "
17693 : : "module level scope", sym->name, &(sym->declared_at));
17694 : 6 : t = false;
17695 : : }
17696 : 161 : else if (sym->ts.type == BT_CHARACTER
17697 : 161 : && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
17698 : 1 : || !gfc_is_constant_expr (sym->ts.u.cl->length)
17699 : 1 : || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
17700 : : {
17701 : 1 : gfc_error ("BIND(C) Variable %qs at %L must have length one",
17702 : 1 : sym->name, &sym->declared_at);
17703 : 1 : t = false;
17704 : : }
17705 : 160 : else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
17706 : : {
17707 : 1 : t = verify_com_block_vars_c_interop (sym->common_head);
17708 : : }
17709 : 159 : else if (sym->attr.implicit_type == 0)
17710 : : {
17711 : : /* If type() declaration, we need to verify that the components
17712 : : of the given type are all C interoperable, etc. */
17713 : 157 : if (sym->ts.type == BT_DERIVED &&
17714 : 24 : sym->ts.u.derived->attr.is_c_interop != 1)
17715 : : {
17716 : : /* Make sure the user marked the derived type as BIND(C). If
17717 : : not, call the verify routine. This could print an error
17718 : : for the derived type more than once if multiple variables
17719 : : of that type are declared. */
17720 : 14 : if (sym->ts.u.derived->attr.is_bind_c != 1)
17721 : 1 : verify_bind_c_derived_type (sym->ts.u.derived);
17722 : 157 : t = false;
17723 : : }
17724 : :
17725 : : /* Verify the variable itself as C interoperable if it
17726 : : is BIND(C). It is not possible for this to succeed if
17727 : : the verify_bind_c_derived_type failed, so don't have to handle
17728 : : any error returned by verify_bind_c_derived_type. */
17729 : 157 : t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
17730 : 157 : sym->common_block);
17731 : : }
17732 : :
17733 : 165 : if (!t)
17734 : : {
17735 : : /* clear the is_bind_c flag to prevent reporting errors more than
17736 : : once if something failed. */
17737 : 10 : sym->attr.is_bind_c = 0;
17738 : 10 : return;
17739 : : }
17740 : : }
17741 : :
17742 : : /* If a derived type symbol has reached this point, without its
17743 : : type being declared, we have an error. Notice that most
17744 : : conditions that produce undefined derived types have already
17745 : : been dealt with. However, the likes of:
17746 : : implicit type(t) (t) ..... call foo (t) will get us here if
17747 : : the type is not declared in the scope of the implicit
17748 : : statement. Change the type to BT_UNKNOWN, both because it is so
17749 : : and to prevent an ICE. */
17750 : 1600568 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17751 : 118344 : && sym->ts.u.derived->components == NULL
17752 : 1081 : && !sym->ts.u.derived->attr.zero_comp)
17753 : : {
17754 : 3 : gfc_error ("The derived type %qs at %L is of type %qs, "
17755 : : "which has not been defined", sym->name,
17756 : : &sym->declared_at, sym->ts.u.derived->name);
17757 : 3 : sym->ts.type = BT_UNKNOWN;
17758 : 3 : return;
17759 : : }
17760 : :
17761 : : /* Make sure that the derived type has been resolved and that the
17762 : : derived type is visible in the symbol's namespace, if it is a
17763 : : module function and is not PRIVATE. */
17764 : 1600565 : if (sym->ts.type == BT_DERIVED
17765 : 125195 : && sym->ts.u.derived->attr.use_assoc
17766 : 109276 : && sym->ns->proc_name
17767 : 109268 : && sym->ns->proc_name->attr.flavor == FL_MODULE
17768 : 1606349 : && !resolve_fl_derived (sym->ts.u.derived))
17769 : : return;
17770 : :
17771 : : /* Unless the derived-type declaration is use associated, Fortran 95
17772 : : does not allow public entries of private derived types.
17773 : : See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
17774 : : 161 in 95-006r3. */
17775 : 1600565 : if (sym->ts.type == BT_DERIVED
17776 : 125195 : && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
17777 : 7505 : && !sym->ts.u.derived->attr.use_assoc
17778 : 1721 : && gfc_check_symbol_access (sym)
17779 : 1548 : && !gfc_check_symbol_access (sym->ts.u.derived)
17780 : 1600578 : && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
17781 : : "derived type %qs",
17782 : 13 : (sym->attr.flavor == FL_PARAMETER)
17783 : : ? "parameter" : "variable",
17784 : : sym->name, &sym->declared_at,
17785 : 13 : sym->ts.u.derived->name))
17786 : : return;
17787 : :
17788 : : /* F2008, C1302. */
17789 : 1600558 : if (sym->ts.type == BT_DERIVED
17790 : 125188 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17791 : 118 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
17792 : 125164 : || sym->ts.u.derived->attr.lock_comp)
17793 : 37 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17794 : : {
17795 : 4 : gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
17796 : : "type LOCK_TYPE must be a coarray", sym->name,
17797 : : &sym->declared_at);
17798 : 4 : return;
17799 : : }
17800 : :
17801 : : /* TS18508, C702/C703. */
17802 : 1600554 : if (sym->ts.type == BT_DERIVED
17803 : 125184 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17804 : 117 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
17805 : 125174 : || sym->ts.u.derived->attr.event_comp)
17806 : 10 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17807 : : {
17808 : 1 : gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
17809 : : "type EVENT_TYPE must be a coarray", sym->name,
17810 : : &sym->declared_at);
17811 : 1 : return;
17812 : : }
17813 : :
17814 : : /* An assumed-size array with INTENT(OUT) shall not be of a type for which
17815 : : default initialization is defined (5.1.2.4.4). */
17816 : 1600553 : if (sym->ts.type == BT_DERIVED
17817 : 125183 : && sym->attr.dummy
17818 : 43508 : && sym->attr.intent == INTENT_OUT
17819 : 2348 : && sym->as
17820 : 381 : && sym->as->type == AS_ASSUMED_SIZE)
17821 : : {
17822 : 1 : for (c = sym->ts.u.derived->components; c; c = c->next)
17823 : : {
17824 : 1 : if (c->initializer)
17825 : : {
17826 : 1 : gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
17827 : : "ASSUMED SIZE and so cannot have a default initializer",
17828 : : sym->name, &sym->declared_at);
17829 : 1 : return;
17830 : : }
17831 : : }
17832 : : }
17833 : :
17834 : : /* F2008, C542. */
17835 : 1600552 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17836 : 43507 : && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
17837 : : {
17838 : 0 : gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
17839 : : "INTENT(OUT)", sym->name, &sym->declared_at);
17840 : 0 : return;
17841 : : }
17842 : :
17843 : : /* TS18508. */
17844 : 1600552 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17845 : 43507 : && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
17846 : : {
17847 : 0 : gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
17848 : : "INTENT(OUT)", sym->name, &sym->declared_at);
17849 : 0 : return;
17850 : : }
17851 : :
17852 : : /* F2008, C525. */
17853 : 1600552 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17854 : 1600467 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17855 : 18424 : && sym->ts.u.derived && CLASS_DATA (sym)
17856 : 18418 : && CLASS_DATA (sym)->attr.coarray_comp))
17857 : 1600467 : || class_attr.codimension)
17858 : 1534 : && (sym->attr.result || sym->result == sym))
17859 : : {
17860 : 8 : gfc_error ("Function result %qs at %L shall not be a coarray or have "
17861 : : "a coarray component", sym->name, &sym->declared_at);
17862 : 8 : return;
17863 : : }
17864 : :
17865 : : /* F2008, C524. */
17866 : 1600544 : if (sym->attr.codimension && sym->ts.type == BT_DERIVED
17867 : 370 : && sym->ts.u.derived->ts.is_iso_c)
17868 : : {
17869 : 3 : gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
17870 : : "shall not be a coarray", sym->name, &sym->declared_at);
17871 : 3 : return;
17872 : : }
17873 : :
17874 : : /* F2008, C525. */
17875 : 1600541 : if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17876 : 1600459 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17877 : 18423 : && sym->ts.u.derived && CLASS_DATA (sym)
17878 : 18417 : && CLASS_DATA (sym)->attr.coarray_comp))
17879 : 82 : && (class_attr.codimension || class_attr.pointer || class_attr.dimension
17880 : 78 : || class_attr.allocatable))
17881 : : {
17882 : 4 : gfc_error ("Variable %qs at %L with coarray component shall be a "
17883 : : "nonpointer, nonallocatable scalar, which is not a coarray",
17884 : : sym->name, &sym->declared_at);
17885 : 4 : return;
17886 : : }
17887 : :
17888 : : /* F2008, C526. The function-result case was handled above. */
17889 : 1600537 : if (class_attr.codimension
17890 : 1441 : && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
17891 : 294 : || sym->attr.select_type_temporary
17892 : 223 : || sym->attr.associate_var
17893 : 210 : || (sym->ns->save_all && !sym->attr.automatic)
17894 : 210 : || sym->ns->proc_name->attr.flavor == FL_MODULE
17895 : 210 : || sym->ns->proc_name->attr.is_main_program
17896 : 5 : || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
17897 : : {
17898 : 4 : gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
17899 : : "nor a dummy argument", sym->name, &sym->declared_at);
17900 : 4 : return;
17901 : : }
17902 : : /* F2008, C528. */
17903 : 1600533 : else if (class_attr.codimension && !sym->attr.select_type_temporary
17904 : 1366 : && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
17905 : : {
17906 : 6 : gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
17907 : : "deferred shape without allocatable", sym->name,
17908 : : &sym->declared_at);
17909 : 6 : return;
17910 : : }
17911 : 1600527 : else if (class_attr.codimension && class_attr.allocatable && as
17912 : 519 : && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
17913 : : {
17914 : 9 : gfc_error ("Allocatable coarray variable %qs at %L must have "
17915 : : "deferred shape", sym->name, &sym->declared_at);
17916 : 9 : return;
17917 : : }
17918 : :
17919 : : /* F2008, C541. */
17920 : 1600518 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17921 : 1600440 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17922 : 18418 : && sym->ts.u.derived && CLASS_DATA (sym)
17923 : 18412 : && CLASS_DATA (sym)->attr.coarray_comp))
17924 : 1600440 : || (class_attr.codimension && class_attr.allocatable))
17925 : 588 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
17926 : : {
17927 : 3 : gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
17928 : : "allocatable coarray or have coarray components",
17929 : : sym->name, &sym->declared_at);
17930 : 3 : return;
17931 : : }
17932 : :
17933 : 1600515 : if (class_attr.codimension && sym->attr.dummy
17934 : 425 : && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
17935 : : {
17936 : 2 : gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
17937 : : "procedure %qs", sym->name, &sym->declared_at,
17938 : : sym->ns->proc_name->name);
17939 : 2 : return;
17940 : : }
17941 : :
17942 : 1600513 : if (sym->ts.type == BT_LOGICAL
17943 : 111117 : && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
17944 : 111114 : || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
17945 : 30760 : && sym->ns->proc_name->attr.is_bind_c)))
17946 : : {
17947 : : int i;
17948 : 200 : for (i = 0; gfc_logical_kinds[i].kind; i++)
17949 : 200 : if (gfc_logical_kinds[i].kind == sym->ts.kind)
17950 : : break;
17951 : 16 : if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
17952 : 181 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
17953 : : "%L with non-C_Bool kind in BIND(C) procedure "
17954 : : "%qs", sym->name, &sym->declared_at,
17955 : 13 : sym->ns->proc_name->name))
17956 : : return;
17957 : 167 : else if (!gfc_logical_kinds[i].c_bool
17958 : 182 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
17959 : : "%qs at %L with non-C_Bool kind in "
17960 : : "BIND(C) procedure %qs", sym->name,
17961 : : &sym->declared_at,
17962 : 15 : sym->attr.function ? sym->name
17963 : 13 : : sym->ns->proc_name->name))
17964 : : return;
17965 : : }
17966 : :
17967 : 1600510 : switch (sym->attr.flavor)
17968 : : {
17969 : 624764 : case FL_VARIABLE:
17970 : 624764 : if (!resolve_fl_variable (sym, mp_flag))
17971 : : return;
17972 : : break;
17973 : :
17974 : 456595 : case FL_PROCEDURE:
17975 : 456595 : if (sym->formal && !sym->formal_ns)
17976 : : {
17977 : : /* Check that none of the arguments are a namelist. */
17978 : : gfc_formal_arglist *formal = sym->formal;
17979 : :
17980 : 102375 : for (; formal; formal = formal->next)
17981 : 69646 : if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
17982 : : {
17983 : 1 : gfc_error ("Namelist %qs cannot be an argument to "
17984 : : "subroutine or function at %L",
17985 : : formal->sym->name, &sym->declared_at);
17986 : 1 : return;
17987 : : }
17988 : : }
17989 : :
17990 : 456594 : if (!resolve_fl_procedure (sym, mp_flag))
17991 : : return;
17992 : : break;
17993 : :
17994 : 805 : case FL_NAMELIST:
17995 : 805 : if (!resolve_fl_namelist (sym))
17996 : : return;
17997 : : break;
17998 : :
17999 : 370100 : case FL_PARAMETER:
18000 : 370100 : if (!resolve_fl_parameter (sym))
18001 : : return;
18002 : : break;
18003 : :
18004 : : default:
18005 : : break;
18006 : : }
18007 : :
18008 : : /* Resolve array specifier. Check as well some constraints
18009 : : on COMMON blocks. */
18010 : :
18011 : 1600321 : check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
18012 : :
18013 : 1600321 : resolve_symbol_array_spec (sym, check_constant);
18014 : :
18015 : : /* Resolve formal namespaces. */
18016 : 1600321 : if (sym->formal_ns && sym->formal_ns != gfc_current_ns
18017 : 249087 : && !sym->attr.contained && !sym->attr.intrinsic)
18018 : 225161 : gfc_resolve (sym->formal_ns);
18019 : :
18020 : : /* Make sure the formal namespace is present. */
18021 : 1600321 : if (sym->formal && !sym->formal_ns)
18022 : : {
18023 : : gfc_formal_arglist *formal = sym->formal;
18024 : 32930 : while (formal && !formal->sym)
18025 : 11 : formal = formal->next;
18026 : :
18027 : 32919 : if (formal)
18028 : : {
18029 : 32908 : sym->formal_ns = formal->sym->ns;
18030 : 32908 : if (sym->formal_ns && sym->ns != formal->sym->ns)
18031 : 24710 : sym->formal_ns->refs++;
18032 : : }
18033 : : }
18034 : :
18035 : : /* Check threadprivate restrictions. */
18036 : 1600321 : if (sym->attr.threadprivate
18037 : 331 : && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
18038 : 32 : && !(sym->ns->save_all && !sym->attr.automatic)
18039 : 31 : && sym->module == NULL
18040 : 16 : && (sym->ns->proc_name == NULL
18041 : 16 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18042 : 3 : && !sym->ns->proc_name->attr.is_main_program)))
18043 : 1 : gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
18044 : :
18045 : : /* Check omp declare target restrictions. */
18046 : 1600321 : if (sym->attr.omp_declare_target
18047 : 1443 : && sym->attr.flavor == FL_VARIABLE
18048 : 604 : && !sym->attr.save
18049 : 195 : && !(sym->ns->save_all && !sym->attr.automatic)
18050 : 195 : && (!sym->attr.in_common
18051 : 182 : && sym->module == NULL
18052 : 92 : && (sym->ns->proc_name == NULL
18053 : 92 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18054 : 2 : && !sym->ns->proc_name->attr.is_main_program))))
18055 : 1 : gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
18056 : : sym->name, &sym->declared_at);
18057 : :
18058 : : /* If we have come this far we can apply default-initializers, as
18059 : : described in 14.7.5, to those variables that have not already
18060 : : been assigned one. */
18061 : 1600321 : if (sym->ts.type == BT_DERIVED
18062 : 125155 : && !sym->value
18063 : 101082 : && !sym->attr.allocatable
18064 : 98311 : && !sym->attr.alloc_comp)
18065 : : {
18066 : 98311 : symbol_attribute *a = &sym->attr;
18067 : :
18068 : 98311 : if ((!a->save && !a->dummy && !a->pointer
18069 : 53568 : && !a->in_common && !a->use_assoc
18070 : 9463 : && a->referenced
18071 : 7381 : && !((a->function || a->result)
18072 : 1368 : && (!a->dimension
18073 : 124 : || sym->ts.u.derived->attr.alloc_comp
18074 : 83 : || sym->ts.u.derived->attr.pointer_comp))
18075 : 6082 : && !(a->function && sym != sym->result))
18076 : 92249 : || (a->dummy && !a->pointer && a->intent == INTENT_OUT
18077 : 1522 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
18078 : 7485 : apply_default_init (sym);
18079 : 90826 : else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc
18080 : 2489 : && sym->result)
18081 : : /* Default initialization for function results. */
18082 : 2485 : apply_default_init (sym->result);
18083 : 88341 : else if (a->function && sym->result && a->access != ACCESS_PRIVATE
18084 : 11094 : && (sym->ts.u.derived->attr.alloc_comp
18085 : 10695 : || sym->ts.u.derived->attr.pointer_comp))
18086 : : /* Mark the result symbol to be referenced, when it has allocatable
18087 : : components. */
18088 : 431 : sym->result->attr.referenced = 1;
18089 : : }
18090 : :
18091 : 1600321 : if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
18092 : 17928 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT
18093 : 1180 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
18094 : 1105 : && !CLASS_DATA (sym)->attr.class_pointer
18095 : 1079 : && !CLASS_DATA (sym)->attr.allocatable)
18096 : 807 : apply_default_init (sym);
18097 : :
18098 : : /* If this symbol has a type-spec, check it. */
18099 : 1600321 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
18100 : 605565 : || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
18101 : 1304175 : if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
18102 : : return;
18103 : :
18104 : 1600318 : if (sym->param_list)
18105 : 522 : resolve_pdt (sym);
18106 : : }
18107 : :
18108 : :
18109 : : /************* Resolve DATA statements *************/
18110 : :
18111 : : static struct
18112 : : {
18113 : : gfc_data_value *vnode;
18114 : : mpz_t left;
18115 : : }
18116 : : values;
18117 : :
18118 : :
18119 : : /* Advance the values structure to point to the next value in the data list. */
18120 : :
18121 : : static bool
18122 : 10853 : next_data_value (void)
18123 : : {
18124 : 16618 : while (mpz_cmp_ui (values.left, 0) == 0)
18125 : : {
18126 : :
18127 : 8177 : if (values.vnode->next == NULL)
18128 : : return false;
18129 : :
18130 : 5765 : values.vnode = values.vnode->next;
18131 : 5765 : mpz_set (values.left, values.vnode->repeat);
18132 : : }
18133 : :
18134 : : return true;
18135 : : }
18136 : :
18137 : :
18138 : : static bool
18139 : 3536 : check_data_variable (gfc_data_variable *var, locus *where)
18140 : : {
18141 : 3536 : gfc_expr *e;
18142 : 3536 : mpz_t size;
18143 : 3536 : mpz_t offset;
18144 : 3536 : bool t;
18145 : 3536 : ar_type mark = AR_UNKNOWN;
18146 : 3536 : int i;
18147 : 3536 : mpz_t section_index[GFC_MAX_DIMENSIONS];
18148 : 3536 : int vector_offset[GFC_MAX_DIMENSIONS];
18149 : 3536 : gfc_ref *ref;
18150 : 3536 : gfc_array_ref *ar;
18151 : 3536 : gfc_symbol *sym;
18152 : 3536 : int has_pointer;
18153 : :
18154 : 3536 : if (!gfc_resolve_expr (var->expr))
18155 : : return false;
18156 : :
18157 : 3536 : ar = NULL;
18158 : 3536 : e = var->expr;
18159 : :
18160 : 3536 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
18161 : 0 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
18162 : 0 : e = e->value.function.actual->expr;
18163 : :
18164 : 3536 : if (e->expr_type != EXPR_VARIABLE)
18165 : : {
18166 : 0 : gfc_error ("Expecting definable entity near %L", where);
18167 : 0 : return false;
18168 : : }
18169 : :
18170 : 3536 : sym = e->symtree->n.sym;
18171 : :
18172 : 3536 : if (sym->ns->is_block_data && !sym->attr.in_common)
18173 : : {
18174 : 2 : gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
18175 : : sym->name, &sym->declared_at);
18176 : 2 : return false;
18177 : : }
18178 : :
18179 : 3534 : if (e->ref == NULL && sym->as)
18180 : : {
18181 : 1 : gfc_error ("DATA array %qs at %L must be specified in a previous"
18182 : : " declaration", sym->name, where);
18183 : 1 : return false;
18184 : : }
18185 : :
18186 : 3533 : if (gfc_is_coindexed (e))
18187 : : {
18188 : 5 : gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
18189 : : where);
18190 : 5 : return false;
18191 : : }
18192 : :
18193 : 3528 : has_pointer = sym->attr.pointer;
18194 : :
18195 : 5966 : for (ref = e->ref; ref; ref = ref->next)
18196 : : {
18197 : 2442 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
18198 : : has_pointer = 1;
18199 : :
18200 : 2416 : if (has_pointer)
18201 : : {
18202 : 29 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
18203 : : {
18204 : 1 : gfc_error ("DATA element %qs at %L is a pointer and so must "
18205 : : "be a full array", sym->name, where);
18206 : 1 : return false;
18207 : : }
18208 : :
18209 : 28 : if (values.vnode->expr->expr_type == EXPR_CONSTANT)
18210 : : {
18211 : 1 : gfc_error ("DATA object near %L has the pointer attribute "
18212 : : "and the corresponding DATA value is not a valid "
18213 : : "initial-data-target", where);
18214 : 1 : return false;
18215 : : }
18216 : : }
18217 : :
18218 : 2440 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
18219 : : {
18220 : 1 : gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
18221 : : "attribute", ref->u.c.component->name, &e->where);
18222 : 1 : return false;
18223 : : }
18224 : :
18225 : : /* Reject substrings of strings of non-constant length. */
18226 : 2439 : if (ref->type == REF_SUBSTRING
18227 : 73 : && ref->u.ss.length
18228 : 73 : && ref->u.ss.length->length
18229 : 2512 : && !gfc_is_constant_expr (ref->u.ss.length->length))
18230 : 1 : goto bad_charlen;
18231 : : }
18232 : :
18233 : : /* Reject strings with deferred length or non-constant length. */
18234 : 3524 : if (e->ts.type == BT_CHARACTER
18235 : 3524 : && (e->ts.deferred
18236 : 374 : || (e->ts.u.cl->length
18237 : 323 : && !gfc_is_constant_expr (e->ts.u.cl->length))))
18238 : 5 : goto bad_charlen;
18239 : :
18240 : 3519 : mpz_init_set_si (offset, 0);
18241 : :
18242 : 3519 : if (e->rank == 0 || has_pointer)
18243 : : {
18244 : 2675 : mpz_init_set_ui (size, 1);
18245 : 2675 : ref = NULL;
18246 : : }
18247 : : else
18248 : : {
18249 : 844 : ref = e->ref;
18250 : :
18251 : : /* Find the array section reference. */
18252 : 1027 : for (ref = e->ref; ref; ref = ref->next)
18253 : : {
18254 : 1027 : if (ref->type != REF_ARRAY)
18255 : 92 : continue;
18256 : 935 : if (ref->u.ar.type == AR_ELEMENT)
18257 : 91 : continue;
18258 : : break;
18259 : : }
18260 : 844 : gcc_assert (ref);
18261 : :
18262 : : /* Set marks according to the reference pattern. */
18263 : 844 : switch (ref->u.ar.type)
18264 : : {
18265 : : case AR_FULL:
18266 : : mark = AR_FULL;
18267 : : break;
18268 : :
18269 : 149 : case AR_SECTION:
18270 : 149 : ar = &ref->u.ar;
18271 : : /* Get the start position of array section. */
18272 : 149 : gfc_get_section_index (ar, section_index, &offset, vector_offset);
18273 : 149 : mark = AR_SECTION;
18274 : 149 : break;
18275 : :
18276 : 0 : default:
18277 : 0 : gcc_unreachable ();
18278 : : }
18279 : :
18280 : 844 : if (!gfc_array_size (e, &size))
18281 : : {
18282 : 1 : gfc_error ("Nonconstant array section at %L in DATA statement",
18283 : : where);
18284 : 1 : mpz_clear (offset);
18285 : 1 : return false;
18286 : : }
18287 : : }
18288 : :
18289 : 3518 : t = true;
18290 : :
18291 : 11897 : while (mpz_cmp_ui (size, 0) > 0)
18292 : : {
18293 : 8442 : if (!next_data_value ())
18294 : : {
18295 : 1 : gfc_error ("DATA statement at %L has more variables than values",
18296 : : where);
18297 : 1 : t = false;
18298 : 1 : break;
18299 : : }
18300 : :
18301 : 8441 : t = gfc_check_assign (var->expr, values.vnode->expr, 0);
18302 : 8441 : if (!t)
18303 : : break;
18304 : :
18305 : : /* If we have more than one element left in the repeat count,
18306 : : and we have more than one element left in the target variable,
18307 : : then create a range assignment. */
18308 : : /* FIXME: Only done for full arrays for now, since array sections
18309 : : seem tricky. */
18310 : 8422 : if (mark == AR_FULL && ref && ref->next == NULL
18311 : 5362 : && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
18312 : : {
18313 : 137 : mpz_t range;
18314 : :
18315 : 137 : if (mpz_cmp (size, values.left) >= 0)
18316 : : {
18317 : 126 : mpz_init_set (range, values.left);
18318 : 126 : mpz_sub (size, size, values.left);
18319 : 126 : mpz_set_ui (values.left, 0);
18320 : : }
18321 : : else
18322 : : {
18323 : 11 : mpz_init_set (range, size);
18324 : 11 : mpz_sub (values.left, values.left, size);
18325 : 11 : mpz_set_ui (size, 0);
18326 : : }
18327 : :
18328 : 137 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18329 : : offset, &range);
18330 : :
18331 : 137 : mpz_add (offset, offset, range);
18332 : 137 : mpz_clear (range);
18333 : :
18334 : 137 : if (!t)
18335 : : break;
18336 : 129 : }
18337 : :
18338 : : /* Assign initial value to symbol. */
18339 : : else
18340 : : {
18341 : 8285 : mpz_sub_ui (values.left, values.left, 1);
18342 : 8285 : mpz_sub_ui (size, size, 1);
18343 : :
18344 : 8285 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18345 : : offset, NULL);
18346 : 8285 : if (!t)
18347 : : break;
18348 : :
18349 : 8250 : if (mark == AR_FULL)
18350 : 5257 : mpz_add_ui (offset, offset, 1);
18351 : :
18352 : : /* Modify the array section indexes and recalculate the offset
18353 : : for next element. */
18354 : 2993 : else if (mark == AR_SECTION)
18355 : 363 : gfc_advance_section (section_index, ar, &offset, vector_offset);
18356 : : }
18357 : : }
18358 : :
18359 : 3518 : if (mark == AR_SECTION)
18360 : : {
18361 : 340 : for (i = 0; i < ar->dimen; i++)
18362 : 192 : mpz_clear (section_index[i]);
18363 : : }
18364 : :
18365 : 3518 : mpz_clear (size);
18366 : 3518 : mpz_clear (offset);
18367 : :
18368 : 3518 : return t;
18369 : :
18370 : 6 : bad_charlen:
18371 : 6 : gfc_error ("Non-constant character length at %L in DATA statement",
18372 : : &e->where);
18373 : 6 : return false;
18374 : : }
18375 : :
18376 : :
18377 : : static bool traverse_data_var (gfc_data_variable *, locus *);
18378 : :
18379 : : /* Iterate over a list of elements in a DATA statement. */
18380 : :
18381 : : static bool
18382 : 236 : traverse_data_list (gfc_data_variable *var, locus *where)
18383 : : {
18384 : 236 : mpz_t trip;
18385 : 236 : iterator_stack frame;
18386 : 236 : gfc_expr *e, *start, *end, *step;
18387 : 236 : bool retval = true;
18388 : :
18389 : 236 : mpz_init (frame.value);
18390 : 236 : mpz_init (trip);
18391 : :
18392 : 236 : start = gfc_copy_expr (var->iter.start);
18393 : 236 : end = gfc_copy_expr (var->iter.end);
18394 : 236 : step = gfc_copy_expr (var->iter.step);
18395 : :
18396 : 236 : if (!gfc_simplify_expr (start, 1)
18397 : 236 : || start->expr_type != EXPR_CONSTANT)
18398 : : {
18399 : 0 : gfc_error ("start of implied-do loop at %L could not be "
18400 : : "simplified to a constant value", &start->where);
18401 : 0 : retval = false;
18402 : 0 : goto cleanup;
18403 : : }
18404 : 236 : if (!gfc_simplify_expr (end, 1)
18405 : 236 : || end->expr_type != EXPR_CONSTANT)
18406 : : {
18407 : 0 : gfc_error ("end of implied-do loop at %L could not be "
18408 : : "simplified to a constant value", &end->where);
18409 : 0 : retval = false;
18410 : 0 : goto cleanup;
18411 : : }
18412 : 236 : if (!gfc_simplify_expr (step, 1)
18413 : 236 : || step->expr_type != EXPR_CONSTANT)
18414 : : {
18415 : 0 : gfc_error ("step of implied-do loop at %L could not be "
18416 : : "simplified to a constant value", &step->where);
18417 : 0 : retval = false;
18418 : 0 : goto cleanup;
18419 : : }
18420 : 236 : if (mpz_cmp_si (step->value.integer, 0) == 0)
18421 : : {
18422 : 1 : gfc_error ("step of implied-do loop at %L shall not be zero",
18423 : : &step->where);
18424 : 1 : retval = false;
18425 : 1 : goto cleanup;
18426 : : }
18427 : :
18428 : 235 : mpz_set (trip, end->value.integer);
18429 : 235 : mpz_sub (trip, trip, start->value.integer);
18430 : 235 : mpz_add (trip, trip, step->value.integer);
18431 : :
18432 : 235 : mpz_div (trip, trip, step->value.integer);
18433 : :
18434 : 235 : mpz_set (frame.value, start->value.integer);
18435 : :
18436 : 235 : frame.prev = iter_stack;
18437 : 235 : frame.variable = var->iter.var->symtree;
18438 : 235 : iter_stack = &frame;
18439 : :
18440 : 1124 : while (mpz_cmp_ui (trip, 0) > 0)
18441 : : {
18442 : 903 : if (!traverse_data_var (var->list, where))
18443 : : {
18444 : 14 : retval = false;
18445 : 14 : goto cleanup;
18446 : : }
18447 : :
18448 : 889 : e = gfc_copy_expr (var->expr);
18449 : 889 : if (!gfc_simplify_expr (e, 1))
18450 : : {
18451 : 0 : gfc_free_expr (e);
18452 : 0 : retval = false;
18453 : 0 : goto cleanup;
18454 : : }
18455 : :
18456 : 889 : mpz_add (frame.value, frame.value, step->value.integer);
18457 : :
18458 : 889 : mpz_sub_ui (trip, trip, 1);
18459 : : }
18460 : :
18461 : 221 : cleanup:
18462 : 236 : mpz_clear (frame.value);
18463 : 236 : mpz_clear (trip);
18464 : :
18465 : 236 : gfc_free_expr (start);
18466 : 236 : gfc_free_expr (end);
18467 : 236 : gfc_free_expr (step);
18468 : :
18469 : 236 : iter_stack = frame.prev;
18470 : 236 : return retval;
18471 : : }
18472 : :
18473 : :
18474 : : /* Type resolve variables in the variable list of a DATA statement. */
18475 : :
18476 : : static bool
18477 : 3396 : traverse_data_var (gfc_data_variable *var, locus *where)
18478 : : {
18479 : 3396 : bool t;
18480 : :
18481 : 7072 : for (; var; var = var->next)
18482 : : {
18483 : 3772 : if (var->expr == NULL)
18484 : 236 : t = traverse_data_list (var, where);
18485 : : else
18486 : 3536 : t = check_data_variable (var, where);
18487 : :
18488 : 3772 : if (!t)
18489 : : return false;
18490 : : }
18491 : :
18492 : : return true;
18493 : : }
18494 : :
18495 : :
18496 : : /* Resolve the expressions and iterators associated with a data statement.
18497 : : This is separate from the assignment checking because data lists should
18498 : : only be resolved once. */
18499 : :
18500 : : static bool
18501 : 2647 : resolve_data_variables (gfc_data_variable *d)
18502 : : {
18503 : 5665 : for (; d; d = d->next)
18504 : : {
18505 : 3023 : if (d->list == NULL)
18506 : : {
18507 : 2871 : if (!gfc_resolve_expr (d->expr))
18508 : : return false;
18509 : : }
18510 : : else
18511 : : {
18512 : 152 : if (!gfc_resolve_iterator (&d->iter, false, true))
18513 : : return false;
18514 : :
18515 : 149 : if (!resolve_data_variables (d->list))
18516 : : return false;
18517 : : }
18518 : : }
18519 : :
18520 : : return true;
18521 : : }
18522 : :
18523 : :
18524 : : /* Resolve a single DATA statement. We implement this by storing a pointer to
18525 : : the value list into static variables, and then recursively traversing the
18526 : : variables list, expanding iterators and such. */
18527 : :
18528 : : static void
18529 : 2498 : resolve_data (gfc_data *d)
18530 : : {
18531 : :
18532 : 2498 : if (!resolve_data_variables (d->var))
18533 : : return;
18534 : :
18535 : 2493 : values.vnode = d->value;
18536 : 2493 : if (d->value == NULL)
18537 : 0 : mpz_set_ui (values.left, 0);
18538 : : else
18539 : 2493 : mpz_set (values.left, d->value->repeat);
18540 : :
18541 : 2493 : if (!traverse_data_var (d->var, &d->where))
18542 : : return;
18543 : :
18544 : : /* At this point, we better not have any values left. */
18545 : :
18546 : 2411 : if (next_data_value ())
18547 : 0 : gfc_error ("DATA statement at %L has more values than variables",
18548 : : &d->where);
18549 : : }
18550 : :
18551 : :
18552 : : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
18553 : : accessed by host or use association, is a dummy argument to a pure function,
18554 : : is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
18555 : : is storage associated with any such variable, shall not be used in the
18556 : : following contexts: (clients of this function). */
18557 : :
18558 : : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
18559 : : procedure. Returns zero if assignment is OK, nonzero if there is a
18560 : : problem. */
18561 : : bool
18562 : 52792 : gfc_impure_variable (gfc_symbol *sym)
18563 : : {
18564 : 52792 : gfc_symbol *proc;
18565 : 52792 : gfc_namespace *ns;
18566 : :
18567 : 52792 : if (sym->attr.use_assoc || sym->attr.in_common)
18568 : : return 1;
18569 : :
18570 : : /* The namespace of a module procedure interface holds the arguments and
18571 : : symbols, and so the symbol namespace can be different to that of the
18572 : : procedure. */
18573 : 52271 : if (sym->ns != gfc_current_ns
18574 : 5535 : && gfc_current_ns->proc_name->abr_modproc_decl
18575 : 36 : && sym->ns->proc_name->attr.function
18576 : 12 : && sym->attr.result
18577 : 12 : && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
18578 : : return 0;
18579 : :
18580 : : /* Check if the symbol's ns is inside the pure procedure. */
18581 : 56609 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18582 : : {
18583 : 56330 : if (ns == sym->ns)
18584 : : break;
18585 : 5787 : if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
18586 : : return 1;
18587 : : }
18588 : :
18589 : 50822 : proc = sym->ns->proc_name;
18590 : 50822 : if (sym->attr.dummy
18591 : 5624 : && !sym->attr.value
18592 : 5502 : && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
18593 : 5307 : || proc->attr.function))
18594 : 674 : return 1;
18595 : :
18596 : : /* TODO: Sort out what can be storage associated, if anything, and include
18597 : : it here. In principle equivalences should be scanned but it does not
18598 : : seem to be possible to storage associate an impure variable this way. */
18599 : : return 0;
18600 : : }
18601 : :
18602 : :
18603 : : /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
18604 : : current namespace is inside a pure procedure. */
18605 : :
18606 : : bool
18607 : 2233248 : gfc_pure (gfc_symbol *sym)
18608 : : {
18609 : 2233248 : symbol_attribute attr;
18610 : 2233248 : gfc_namespace *ns;
18611 : :
18612 : 2233248 : if (sym == NULL)
18613 : : {
18614 : : /* Check if the current namespace or one of its parents
18615 : : belongs to a pure procedure. */
18616 : 3088387 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18617 : : {
18618 : 1824154 : sym = ns->proc_name;
18619 : 1824154 : if (sym == NULL)
18620 : : return 0;
18621 : 1823040 : attr = sym->attr;
18622 : 1823040 : if (attr.flavor == FL_PROCEDURE && attr.pure)
18623 : : return 1;
18624 : : }
18625 : : return 0;
18626 : : }
18627 : :
18628 : 960973 : attr = sym->attr;
18629 : :
18630 : 960973 : return attr.flavor == FL_PROCEDURE && attr.pure;
18631 : : }
18632 : :
18633 : :
18634 : : /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
18635 : : checks if the current namespace is implicitly pure. Note that this
18636 : : function returns false for a PURE procedure. */
18637 : :
18638 : : bool
18639 : 704509 : gfc_implicit_pure (gfc_symbol *sym)
18640 : : {
18641 : 704509 : gfc_namespace *ns;
18642 : :
18643 : 704509 : if (sym == NULL)
18644 : : {
18645 : : /* Check if the current procedure is implicit_pure. Walk up
18646 : : the procedure list until we find a procedure. */
18647 : 968586 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18648 : : {
18649 : 692488 : sym = ns->proc_name;
18650 : 692488 : if (sym == NULL)
18651 : : return 0;
18652 : :
18653 : 692415 : if (sym->attr.flavor == FL_PROCEDURE)
18654 : : break;
18655 : : }
18656 : : }
18657 : :
18658 : 428335 : return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
18659 : 730119 : && !sym->attr.pure;
18660 : : }
18661 : :
18662 : :
18663 : : void
18664 : 412002 : gfc_unset_implicit_pure (gfc_symbol *sym)
18665 : : {
18666 : 412002 : gfc_namespace *ns;
18667 : :
18668 : 412002 : if (sym == NULL)
18669 : : {
18670 : : /* Check if the current procedure is implicit_pure. Walk up
18671 : : the procedure list until we find a procedure. */
18672 : 672871 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18673 : : {
18674 : 416254 : sym = ns->proc_name;
18675 : 416254 : if (sym == NULL)
18676 : : return;
18677 : :
18678 : 415448 : if (sym->attr.flavor == FL_PROCEDURE)
18679 : : break;
18680 : : }
18681 : : }
18682 : :
18683 : 411196 : if (sym->attr.flavor == FL_PROCEDURE)
18684 : 146819 : sym->attr.implicit_pure = 0;
18685 : : else
18686 : 264377 : sym->attr.pure = 0;
18687 : : }
18688 : :
18689 : :
18690 : : /* Test whether the current procedure is elemental or not. */
18691 : :
18692 : : bool
18693 : 1286300 : gfc_elemental (gfc_symbol *sym)
18694 : : {
18695 : 1286300 : symbol_attribute attr;
18696 : :
18697 : 1286300 : if (sym == NULL)
18698 : 0 : sym = gfc_current_ns->proc_name;
18699 : 0 : if (sym == NULL)
18700 : : return 0;
18701 : 1286300 : attr = sym->attr;
18702 : :
18703 : 1286300 : return attr.flavor == FL_PROCEDURE && attr.elemental;
18704 : : }
18705 : :
18706 : :
18707 : : /* Warn about unused labels. */
18708 : :
18709 : : static void
18710 : 4445 : warn_unused_fortran_label (gfc_st_label *label)
18711 : : {
18712 : 4448 : if (label == NULL)
18713 : : return;
18714 : :
18715 : 4 : warn_unused_fortran_label (label->left);
18716 : :
18717 : 4 : if (label->defined == ST_LABEL_UNKNOWN)
18718 : : return;
18719 : :
18720 : 3 : switch (label->referenced)
18721 : : {
18722 : 1 : case ST_LABEL_UNKNOWN:
18723 : 1 : gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
18724 : : label->value, &label->where);
18725 : 1 : break;
18726 : :
18727 : 1 : case ST_LABEL_BAD_TARGET:
18728 : 1 : gfc_warning (OPT_Wunused_label,
18729 : : "Label %d at %L defined but cannot be used",
18730 : : label->value, &label->where);
18731 : 1 : break;
18732 : :
18733 : : default:
18734 : : break;
18735 : : }
18736 : :
18737 : 3 : warn_unused_fortran_label (label->right);
18738 : : }
18739 : :
18740 : :
18741 : : /* Returns the sequence type of a symbol or sequence. */
18742 : :
18743 : : static seq_type
18744 : 1076 : sequence_type (gfc_typespec ts)
18745 : : {
18746 : 1076 : seq_type result;
18747 : 1076 : gfc_component *c;
18748 : :
18749 : 1076 : switch (ts.type)
18750 : : {
18751 : 49 : case BT_DERIVED:
18752 : :
18753 : 49 : if (ts.u.derived->components == NULL)
18754 : : return SEQ_NONDEFAULT;
18755 : :
18756 : 49 : result = sequence_type (ts.u.derived->components->ts);
18757 : 103 : for (c = ts.u.derived->components->next; c; c = c->next)
18758 : 67 : if (sequence_type (c->ts) != result)
18759 : : return SEQ_MIXED;
18760 : :
18761 : : return result;
18762 : :
18763 : 129 : case BT_CHARACTER:
18764 : 129 : if (ts.kind != gfc_default_character_kind)
18765 : 0 : return SEQ_NONDEFAULT;
18766 : :
18767 : : return SEQ_CHARACTER;
18768 : :
18769 : 240 : case BT_INTEGER:
18770 : 240 : if (ts.kind != gfc_default_integer_kind)
18771 : 25 : return SEQ_NONDEFAULT;
18772 : :
18773 : : return SEQ_NUMERIC;
18774 : :
18775 : 559 : case BT_REAL:
18776 : 559 : if (!(ts.kind == gfc_default_real_kind
18777 : 269 : || ts.kind == gfc_default_double_kind))
18778 : 0 : return SEQ_NONDEFAULT;
18779 : :
18780 : : return SEQ_NUMERIC;
18781 : :
18782 : 81 : case BT_COMPLEX:
18783 : 81 : if (ts.kind != gfc_default_complex_kind)
18784 : 48 : return SEQ_NONDEFAULT;
18785 : :
18786 : : return SEQ_NUMERIC;
18787 : :
18788 : 17 : case BT_LOGICAL:
18789 : 17 : if (ts.kind != gfc_default_logical_kind)
18790 : 0 : return SEQ_NONDEFAULT;
18791 : :
18792 : : return SEQ_NUMERIC;
18793 : :
18794 : : default:
18795 : : return SEQ_NONDEFAULT;
18796 : : }
18797 : : }
18798 : :
18799 : :
18800 : : /* Resolve derived type EQUIVALENCE object. */
18801 : :
18802 : : static bool
18803 : 80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
18804 : : {
18805 : 80 : gfc_component *c = derived->components;
18806 : :
18807 : 80 : if (!derived)
18808 : : return true;
18809 : :
18810 : : /* Shall not be an object of nonsequence derived type. */
18811 : 80 : if (!derived->attr.sequence)
18812 : : {
18813 : 0 : gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
18814 : : "attribute to be an EQUIVALENCE object", sym->name,
18815 : : &e->where);
18816 : 0 : return false;
18817 : : }
18818 : :
18819 : : /* Shall not have allocatable components. */
18820 : 80 : if (derived->attr.alloc_comp)
18821 : : {
18822 : 1 : gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
18823 : : "components to be an EQUIVALENCE object",sym->name,
18824 : : &e->where);
18825 : 1 : return false;
18826 : : }
18827 : :
18828 : 79 : if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
18829 : : {
18830 : 1 : gfc_error ("Derived type variable %qs at %L with default "
18831 : : "initialization cannot be in EQUIVALENCE with a variable "
18832 : : "in COMMON", sym->name, &e->where);
18833 : 1 : return false;
18834 : : }
18835 : :
18836 : 245 : for (; c ; c = c->next)
18837 : : {
18838 : 167 : if (gfc_bt_struct (c->ts.type)
18839 : 167 : && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
18840 : : return false;
18841 : :
18842 : : /* Shall not be an object of sequence derived type containing a pointer
18843 : : in the structure. */
18844 : 167 : if (c->attr.pointer)
18845 : : {
18846 : 0 : gfc_error ("Derived type variable %qs at %L with pointer "
18847 : : "component(s) cannot be an EQUIVALENCE object",
18848 : : sym->name, &e->where);
18849 : 0 : return false;
18850 : : }
18851 : : }
18852 : : return true;
18853 : : }
18854 : :
18855 : :
18856 : : /* Resolve equivalence object.
18857 : : An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
18858 : : an allocatable array, an object of nonsequence derived type, an object of
18859 : : sequence derived type containing a pointer at any level of component
18860 : : selection, an automatic object, a function name, an entry name, a result
18861 : : name, a named constant, a structure component, or a subobject of any of
18862 : : the preceding objects. A substring shall not have length zero. A
18863 : : derived type shall not have components with default initialization nor
18864 : : shall two objects of an equivalence group be initialized.
18865 : : Either all or none of the objects shall have an protected attribute.
18866 : : The simple constraints are done in symbol.cc(check_conflict) and the rest
18867 : : are implemented here. */
18868 : :
18869 : : static void
18870 : 1565 : resolve_equivalence (gfc_equiv *eq)
18871 : : {
18872 : 1565 : gfc_symbol *sym;
18873 : 1565 : gfc_symbol *first_sym;
18874 : 1565 : gfc_expr *e;
18875 : 1565 : gfc_ref *r;
18876 : 1565 : locus *last_where = NULL;
18877 : 1565 : seq_type eq_type, last_eq_type;
18878 : 1565 : gfc_typespec *last_ts;
18879 : 1565 : int object, cnt_protected;
18880 : 1565 : const char *msg;
18881 : :
18882 : 1565 : last_ts = &eq->expr->symtree->n.sym->ts;
18883 : :
18884 : 1565 : first_sym = eq->expr->symtree->n.sym;
18885 : :
18886 : 1565 : cnt_protected = 0;
18887 : :
18888 : 4727 : for (object = 1; eq; eq = eq->eq, object++)
18889 : : {
18890 : 3171 : e = eq->expr;
18891 : :
18892 : 3171 : e->ts = e->symtree->n.sym->ts;
18893 : : /* match_varspec might not know yet if it is seeing
18894 : : array reference or substring reference, as it doesn't
18895 : : know the types. */
18896 : 3171 : if (e->ref && e->ref->type == REF_ARRAY)
18897 : : {
18898 : 2152 : gfc_ref *ref = e->ref;
18899 : 2152 : sym = e->symtree->n.sym;
18900 : :
18901 : 2152 : if (sym->attr.dimension)
18902 : : {
18903 : 1855 : ref->u.ar.as = sym->as;
18904 : 1855 : ref = ref->next;
18905 : : }
18906 : :
18907 : : /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
18908 : 2152 : if (e->ts.type == BT_CHARACTER
18909 : 592 : && ref
18910 : 371 : && ref->type == REF_ARRAY
18911 : 371 : && ref->u.ar.dimen == 1
18912 : 371 : && ref->u.ar.dimen_type[0] == DIMEN_RANGE
18913 : 371 : && ref->u.ar.stride[0] == NULL)
18914 : : {
18915 : 370 : gfc_expr *start = ref->u.ar.start[0];
18916 : 370 : gfc_expr *end = ref->u.ar.end[0];
18917 : 370 : void *mem = NULL;
18918 : :
18919 : : /* Optimize away the (:) reference. */
18920 : 370 : if (start == NULL && end == NULL)
18921 : : {
18922 : 9 : if (e->ref == ref)
18923 : 0 : e->ref = ref->next;
18924 : : else
18925 : 9 : e->ref->next = ref->next;
18926 : : mem = ref;
18927 : : }
18928 : : else
18929 : : {
18930 : 361 : ref->type = REF_SUBSTRING;
18931 : 361 : if (start == NULL)
18932 : 9 : start = gfc_get_int_expr (gfc_charlen_int_kind,
18933 : : NULL, 1);
18934 : 361 : ref->u.ss.start = start;
18935 : 361 : if (end == NULL && e->ts.u.cl)
18936 : 27 : end = gfc_copy_expr (e->ts.u.cl->length);
18937 : 361 : ref->u.ss.end = end;
18938 : 361 : ref->u.ss.length = e->ts.u.cl;
18939 : 361 : e->ts.u.cl = NULL;
18940 : : }
18941 : 370 : ref = ref->next;
18942 : 370 : free (mem);
18943 : : }
18944 : :
18945 : : /* Any further ref is an error. */
18946 : 1930 : if (ref)
18947 : : {
18948 : 1 : gcc_assert (ref->type == REF_ARRAY);
18949 : 1 : gfc_error ("Syntax error in EQUIVALENCE statement at %L",
18950 : : &ref->u.ar.where);
18951 : 1 : continue;
18952 : : }
18953 : : }
18954 : :
18955 : 3170 : if (!gfc_resolve_expr (e))
18956 : 2 : continue;
18957 : :
18958 : 3168 : sym = e->symtree->n.sym;
18959 : :
18960 : 3168 : if (sym->attr.is_protected)
18961 : 2 : cnt_protected++;
18962 : 3168 : if (cnt_protected > 0 && cnt_protected != object)
18963 : : {
18964 : 2 : gfc_error ("Either all or none of the objects in the "
18965 : : "EQUIVALENCE set at %L shall have the "
18966 : : "PROTECTED attribute",
18967 : : &e->where);
18968 : 2 : break;
18969 : : }
18970 : :
18971 : : /* Shall not equivalence common block variables in a PURE procedure. */
18972 : 3166 : if (sym->ns->proc_name
18973 : 3150 : && sym->ns->proc_name->attr.pure
18974 : 7 : && sym->attr.in_common)
18975 : : {
18976 : : /* Need to check for symbols that may have entered the pure
18977 : : procedure via a USE statement. */
18978 : 7 : bool saw_sym = false;
18979 : 7 : if (sym->ns->use_stmts)
18980 : : {
18981 : 6 : gfc_use_rename *r;
18982 : 10 : for (r = sym->ns->use_stmts->rename; r; r = r->next)
18983 : 4 : if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
18984 : : }
18985 : : else
18986 : : saw_sym = true;
18987 : :
18988 : 6 : if (saw_sym)
18989 : 3 : gfc_error ("COMMON block member %qs at %L cannot be an "
18990 : : "EQUIVALENCE object in the pure procedure %qs",
18991 : : sym->name, &e->where, sym->ns->proc_name->name);
18992 : : break;
18993 : : }
18994 : :
18995 : : /* Shall not be a named constant. */
18996 : 3159 : if (e->expr_type == EXPR_CONSTANT)
18997 : : {
18998 : 0 : gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
18999 : : "object", sym->name, &e->where);
19000 : 0 : continue;
19001 : : }
19002 : :
19003 : 3161 : if (e->ts.type == BT_DERIVED
19004 : 3159 : && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
19005 : 2 : continue;
19006 : :
19007 : : /* Check that the types correspond correctly:
19008 : : Note 5.28:
19009 : : A numeric sequence structure may be equivalenced to another sequence
19010 : : structure, an object of default integer type, default real type, double
19011 : : precision real type, default logical type such that components of the
19012 : : structure ultimately only become associated to objects of the same
19013 : : kind. A character sequence structure may be equivalenced to an object
19014 : : of default character kind or another character sequence structure.
19015 : : Other objects may be equivalenced only to objects of the same type and
19016 : : kind parameters. */
19017 : :
19018 : : /* Identical types are unconditionally OK. */
19019 : 3157 : if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
19020 : 2677 : goto identical_types;
19021 : :
19022 : 480 : last_eq_type = sequence_type (*last_ts);
19023 : 480 : eq_type = sequence_type (sym->ts);
19024 : :
19025 : : /* Since the pair of objects is not of the same type, mixed or
19026 : : non-default sequences can be rejected. */
19027 : :
19028 : 480 : msg = G_("Sequence %s with mixed components in EQUIVALENCE "
19029 : : "statement at %L with different type objects");
19030 : 481 : if ((object ==2
19031 : 480 : && last_eq_type == SEQ_MIXED
19032 : 7 : && last_where
19033 : 7 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19034 : 486 : || (eq_type == SEQ_MIXED
19035 : 6 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19036 : 1 : continue;
19037 : :
19038 : 479 : msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
19039 : : "statement at %L with objects of different type");
19040 : 483 : if ((object ==2
19041 : 479 : && last_eq_type == SEQ_NONDEFAULT
19042 : 50 : && last_where
19043 : 49 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19044 : 525 : || (eq_type == SEQ_NONDEFAULT
19045 : 24 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19046 : 4 : continue;
19047 : :
19048 : 475 : msg = G_("Non-CHARACTER object %qs in default CHARACTER "
19049 : : "EQUIVALENCE statement at %L");
19050 : 479 : if (last_eq_type == SEQ_CHARACTER
19051 : 475 : && eq_type != SEQ_CHARACTER
19052 : 475 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19053 : 4 : continue;
19054 : :
19055 : 471 : msg = G_("Non-NUMERIC object %qs in default NUMERIC "
19056 : : "EQUIVALENCE statement at %L");
19057 : 473 : if (last_eq_type == SEQ_NUMERIC
19058 : 471 : && eq_type != SEQ_NUMERIC
19059 : 471 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19060 : 2 : continue;
19061 : :
19062 : 3146 : identical_types:
19063 : :
19064 : 3146 : last_ts =&sym->ts;
19065 : 3146 : last_where = &e->where;
19066 : :
19067 : 3146 : if (!e->ref)
19068 : 1003 : continue;
19069 : :
19070 : : /* Shall not be an automatic array. */
19071 : 2143 : if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
19072 : : {
19073 : 3 : gfc_error ("Array %qs at %L with non-constant bounds cannot be "
19074 : : "an EQUIVALENCE object", sym->name, &e->where);
19075 : 3 : continue;
19076 : : }
19077 : :
19078 : 2140 : r = e->ref;
19079 : 4326 : while (r)
19080 : : {
19081 : : /* Shall not be a structure component. */
19082 : 2187 : if (r->type == REF_COMPONENT)
19083 : : {
19084 : 0 : gfc_error ("Structure component %qs at %L cannot be an "
19085 : : "EQUIVALENCE object",
19086 : 0 : r->u.c.component->name, &e->where);
19087 : 0 : break;
19088 : : }
19089 : :
19090 : : /* A substring shall not have length zero. */
19091 : 2187 : if (r->type == REF_SUBSTRING)
19092 : : {
19093 : 341 : if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
19094 : : {
19095 : 1 : gfc_error ("Substring at %L has length zero",
19096 : : &r->u.ss.start->where);
19097 : 1 : break;
19098 : : }
19099 : : }
19100 : 2186 : r = r->next;
19101 : : }
19102 : : }
19103 : 1565 : }
19104 : :
19105 : :
19106 : : /* Function called by resolve_fntype to flag other symbols used in the
19107 : : length type parameter specification of function results. */
19108 : :
19109 : : static bool
19110 : 4079 : flag_fn_result_spec (gfc_expr *expr,
19111 : : gfc_symbol *sym,
19112 : : int *f ATTRIBUTE_UNUSED)
19113 : : {
19114 : 4079 : gfc_namespace *ns;
19115 : 4079 : gfc_symbol *s;
19116 : :
19117 : 4079 : if (expr->expr_type == EXPR_VARIABLE)
19118 : : {
19119 : 1375 : s = expr->symtree->n.sym;
19120 : 2147 : for (ns = s->ns; ns; ns = ns->parent)
19121 : 2147 : if (!ns->parent)
19122 : : break;
19123 : :
19124 : 1375 : if (sym == s)
19125 : : {
19126 : 1 : gfc_error ("Self reference in character length expression "
19127 : : "for %qs at %L", sym->name, &expr->where);
19128 : 1 : return true;
19129 : : }
19130 : :
19131 : 1374 : if (!s->fn_result_spec
19132 : 1374 : && s->attr.flavor == FL_PARAMETER)
19133 : : {
19134 : : /* Function contained in a module.... */
19135 : 63 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
19136 : : {
19137 : 32 : gfc_symtree *st;
19138 : 32 : s->fn_result_spec = 1;
19139 : : /* Make sure that this symbol is translated as a module
19140 : : variable. */
19141 : 32 : st = gfc_get_unique_symtree (ns);
19142 : 32 : st->n.sym = s;
19143 : 32 : s->refs++;
19144 : 32 : }
19145 : : /* ... which is use associated and called. */
19146 : 31 : else if (s->attr.use_assoc || s->attr.used_in_submodule
19147 : 0 : ||
19148 : : /* External function matched with an interface. */
19149 : 0 : (s->ns->proc_name
19150 : 0 : && ((s->ns == ns
19151 : 0 : && s->ns->proc_name->attr.if_source == IFSRC_DECL)
19152 : 0 : || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
19153 : 0 : && s->ns->proc_name->attr.function))
19154 : 31 : s->fn_result_spec = 1;
19155 : : }
19156 : : }
19157 : : return false;
19158 : : }
19159 : :
19160 : :
19161 : : /* Resolve function and ENTRY types, issue diagnostics if needed. */
19162 : :
19163 : : static void
19164 : 329458 : resolve_fntype (gfc_namespace *ns)
19165 : : {
19166 : 329458 : gfc_entry_list *el;
19167 : 329458 : gfc_symbol *sym;
19168 : :
19169 : 329458 : if (ns->proc_name == NULL || !ns->proc_name->attr.function)
19170 : : return;
19171 : :
19172 : : /* If there are any entries, ns->proc_name is the entry master
19173 : : synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
19174 : 174934 : if (ns->entries)
19175 : 564 : sym = ns->entries->sym;
19176 : : else
19177 : : sym = ns->proc_name;
19178 : 174934 : if (sym->result == sym
19179 : 142487 : && sym->ts.type == BT_UNKNOWN
19180 : 6 : && !gfc_set_default_type (sym, 0, NULL)
19181 : 174938 : && !sym->attr.untyped)
19182 : : {
19183 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type",
19184 : : sym->name, &sym->declared_at);
19185 : 3 : sym->attr.untyped = 1;
19186 : : }
19187 : :
19188 : 12915 : if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
19189 : 1587 : && !sym->attr.contained
19190 : 274 : && !gfc_check_symbol_access (sym->ts.u.derived)
19191 : 174934 : && gfc_check_symbol_access (sym))
19192 : : {
19193 : 0 : gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
19194 : : "%L of PRIVATE type %qs", sym->name,
19195 : 0 : &sym->declared_at, sym->ts.u.derived->name);
19196 : : }
19197 : :
19198 : 174934 : if (ns->entries)
19199 : 1189 : for (el = ns->entries->next; el; el = el->next)
19200 : : {
19201 : 625 : if (el->sym->result == el->sym
19202 : 413 : && el->sym->ts.type == BT_UNKNOWN
19203 : 2 : && !gfc_set_default_type (el->sym, 0, NULL)
19204 : 627 : && !el->sym->attr.untyped)
19205 : : {
19206 : 2 : gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
19207 : : el->sym->name, &el->sym->declared_at);
19208 : 2 : el->sym->attr.untyped = 1;
19209 : : }
19210 : : }
19211 : :
19212 : 174934 : if (sym->ts.type == BT_CHARACTER
19213 : 6640 : && sym->ts.u.cl->length
19214 : 1737 : && sym->ts.u.cl->length->ts.type == BT_INTEGER)
19215 : 1732 : gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
19216 : : }
19217 : :
19218 : :
19219 : : /* 12.3.2.1.1 Defined operators. */
19220 : :
19221 : : static bool
19222 : 371 : check_uop_procedure (gfc_symbol *sym, locus where)
19223 : : {
19224 : 371 : gfc_formal_arglist *formal;
19225 : :
19226 : 371 : if (!sym->attr.function)
19227 : : {
19228 : 3 : gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
19229 : : sym->name, &where);
19230 : 3 : return false;
19231 : : }
19232 : :
19233 : 368 : if (sym->ts.type == BT_CHARACTER
19234 : 15 : && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
19235 : 2 : && !(sym->result && ((sym->result->ts.u.cl
19236 : 2 : && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
19237 : : {
19238 : 2 : gfc_error ("User operator procedure %qs at %L cannot be assumed "
19239 : : "character length", sym->name, &where);
19240 : 2 : return false;
19241 : : }
19242 : :
19243 : 366 : formal = gfc_sym_get_dummy_args (sym);
19244 : 366 : if (!formal || !formal->sym)
19245 : : {
19246 : 1 : gfc_error ("User operator procedure %qs at %L must have at least "
19247 : : "one argument", sym->name, &where);
19248 : 1 : return false;
19249 : : }
19250 : :
19251 : 365 : if (formal->sym->attr.intent != INTENT_IN)
19252 : : {
19253 : 0 : gfc_error ("First argument of operator interface at %L must be "
19254 : : "INTENT(IN)", &where);
19255 : 0 : return false;
19256 : : }
19257 : :
19258 : 365 : if (formal->sym->attr.optional)
19259 : : {
19260 : 0 : gfc_error ("First argument of operator interface at %L cannot be "
19261 : : "optional", &where);
19262 : 0 : return false;
19263 : : }
19264 : :
19265 : 365 : formal = formal->next;
19266 : 365 : if (!formal || !formal->sym)
19267 : : return true;
19268 : :
19269 : 232 : if (formal->sym->attr.intent != INTENT_IN)
19270 : : {
19271 : 0 : gfc_error ("Second argument of operator interface at %L must be "
19272 : : "INTENT(IN)", &where);
19273 : 0 : return false;
19274 : : }
19275 : :
19276 : 232 : if (formal->sym->attr.optional)
19277 : : {
19278 : 1 : gfc_error ("Second argument of operator interface at %L cannot be "
19279 : : "optional", &where);
19280 : 1 : return false;
19281 : : }
19282 : :
19283 : 231 : if (formal->next)
19284 : : {
19285 : 2 : gfc_error ("Operator interface at %L must have, at most, two "
19286 : : "arguments", &where);
19287 : 2 : return false;
19288 : : }
19289 : :
19290 : : return true;
19291 : : }
19292 : :
19293 : : static void
19294 : 330124 : gfc_resolve_uops (gfc_symtree *symtree)
19295 : : {
19296 : 330124 : gfc_interface *itr;
19297 : :
19298 : 330124 : if (symtree == NULL)
19299 : : return;
19300 : :
19301 : 333 : gfc_resolve_uops (symtree->left);
19302 : 333 : gfc_resolve_uops (symtree->right);
19303 : :
19304 : 662 : for (itr = symtree->n.uop->op; itr; itr = itr->next)
19305 : 329 : check_uop_procedure (itr->sym, itr->sym->declared_at);
19306 : : }
19307 : :
19308 : :
19309 : : /* Examine all of the expressions associated with a program unit,
19310 : : assign types to all intermediate expressions, make sure that all
19311 : : assignments are to compatible types and figure out which names
19312 : : refer to which functions or subroutines. It doesn't check code
19313 : : block, which is handled by gfc_resolve_code. */
19314 : :
19315 : : static void
19316 : 331896 : resolve_types (gfc_namespace *ns)
19317 : : {
19318 : 331896 : gfc_namespace *n;
19319 : 331896 : gfc_charlen *cl;
19320 : 331896 : gfc_data *d;
19321 : 331896 : gfc_equiv *eq;
19322 : 331896 : gfc_namespace* old_ns = gfc_current_ns;
19323 : 331896 : bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
19324 : :
19325 : 331896 : if (ns->types_resolved)
19326 : : return;
19327 : :
19328 : : /* Check that all IMPLICIT types are ok. */
19329 : 329459 : if (!ns->seen_implicit_none)
19330 : : {
19331 : : unsigned letter;
19332 : 8290432 : for (letter = 0; letter != GFC_LETTERS; ++letter)
19333 : 7983379 : if (ns->set_flag[letter]
19334 : 7983379 : && !resolve_typespec_used (&ns->default_type[letter],
19335 : : &ns->implicit_loc[letter], NULL))
19336 : : return;
19337 : : }
19338 : :
19339 : 329458 : gfc_current_ns = ns;
19340 : :
19341 : 329458 : resolve_entries (ns);
19342 : :
19343 : 329458 : resolve_common_vars (&ns->blank_common, false);
19344 : 329458 : resolve_common_blocks (ns->common_root);
19345 : :
19346 : 329458 : resolve_contained_functions (ns);
19347 : :
19348 : 329458 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
19349 : 281630 : && ns->proc_name->attr.if_source == IFSRC_IFBODY)
19350 : 183249 : gfc_resolve_formal_arglist (ns->proc_name);
19351 : :
19352 : 329458 : gfc_traverse_ns (ns, resolve_bind_c_derived_types);
19353 : :
19354 : 421128 : for (cl = ns->cl_list; cl; cl = cl->next)
19355 : 91670 : resolve_charlen (cl);
19356 : :
19357 : 329458 : gfc_traverse_ns (ns, resolve_symbol);
19358 : :
19359 : 329458 : resolve_fntype (ns);
19360 : :
19361 : 374851 : for (n = ns->contained; n; n = n->sibling)
19362 : : {
19363 : : /* Exclude final wrappers with the test for the artificial attribute. */
19364 : 45393 : if (gfc_pure (ns->proc_name)
19365 : 5 : && !gfc_pure (n->proc_name)
19366 : 45393 : && !n->proc_name->attr.artificial)
19367 : 0 : gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
19368 : : "also be PURE", n->proc_name->name,
19369 : : &n->proc_name->declared_at);
19370 : :
19371 : 45393 : resolve_types (n);
19372 : : }
19373 : :
19374 : 329458 : forall_flag = 0;
19375 : 329458 : gfc_do_concurrent_flag = 0;
19376 : 329458 : gfc_check_interfaces (ns);
19377 : :
19378 : 329458 : gfc_traverse_ns (ns, resolve_values);
19379 : :
19380 : 329458 : if (ns->save_all || (!flag_automatic && !recursive))
19381 : 296 : gfc_save_all (ns);
19382 : :
19383 : 329458 : iter_stack = NULL;
19384 : 331956 : for (d = ns->data; d; d = d->next)
19385 : 2498 : resolve_data (d);
19386 : :
19387 : 329458 : iter_stack = NULL;
19388 : 329458 : gfc_traverse_ns (ns, gfc_formalize_init_value);
19389 : :
19390 : 329458 : gfc_traverse_ns (ns, gfc_verify_binding_labels);
19391 : :
19392 : 331023 : for (eq = ns->equiv; eq; eq = eq->next)
19393 : 1565 : resolve_equivalence (eq);
19394 : :
19395 : : /* Warn about unused labels. */
19396 : 329458 : if (warn_unused_label)
19397 : 4441 : warn_unused_fortran_label (ns->st_labels);
19398 : :
19399 : 329458 : gfc_resolve_uops (ns->uop_root);
19400 : :
19401 : 329458 : gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
19402 : :
19403 : 329458 : gfc_resolve_omp_declare (ns);
19404 : :
19405 : 329458 : gfc_resolve_omp_udrs (ns->omp_udr_root);
19406 : :
19407 : 329458 : ns->types_resolved = 1;
19408 : :
19409 : 329458 : gfc_current_ns = old_ns;
19410 : : }
19411 : :
19412 : :
19413 : : /* Call gfc_resolve_code recursively. */
19414 : :
19415 : : static void
19416 : 331951 : resolve_codes (gfc_namespace *ns)
19417 : : {
19418 : 331951 : gfc_namespace *n;
19419 : 331951 : bitmap_obstack old_obstack;
19420 : :
19421 : 331951 : if (ns->resolved == 1)
19422 : 12827 : return;
19423 : :
19424 : 364572 : for (n = ns->contained; n; n = n->sibling)
19425 : 45448 : resolve_codes (n);
19426 : :
19427 : 319124 : gfc_current_ns = ns;
19428 : :
19429 : : /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
19430 : 319124 : if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
19431 : 307640 : cs_base = NULL;
19432 : :
19433 : : /* Set to an out of range value. */
19434 : 319124 : current_entry_id = -1;
19435 : :
19436 : 319124 : old_obstack = labels_obstack;
19437 : 319124 : bitmap_obstack_initialize (&labels_obstack);
19438 : :
19439 : 319124 : gfc_resolve_oacc_declare (ns);
19440 : 319124 : gfc_resolve_oacc_routines (ns);
19441 : 319124 : gfc_resolve_omp_local_vars (ns);
19442 : 319124 : if (ns->omp_allocate)
19443 : 55 : gfc_resolve_omp_allocate (ns, ns->omp_allocate);
19444 : 319124 : gfc_resolve_code (ns->code, ns);
19445 : :
19446 : 319123 : bitmap_obstack_release (&labels_obstack);
19447 : 319123 : labels_obstack = old_obstack;
19448 : : }
19449 : :
19450 : :
19451 : : /* This function is called after a complete program unit has been compiled.
19452 : : Its purpose is to examine all of the expressions associated with a program
19453 : : unit, assign types to all intermediate expressions, make sure that all
19454 : : assignments are to compatible types and figure out which names refer to
19455 : : which functions or subroutines. */
19456 : :
19457 : : void
19458 : 290990 : gfc_resolve (gfc_namespace *ns)
19459 : : {
19460 : 290990 : gfc_namespace *old_ns;
19461 : 290990 : code_stack *old_cs_base;
19462 : 290990 : struct gfc_omp_saved_state old_omp_state;
19463 : :
19464 : 290990 : if (ns->resolved)
19465 : 4487 : return;
19466 : :
19467 : 286503 : ns->resolved = -1;
19468 : 286503 : old_ns = gfc_current_ns;
19469 : 286503 : old_cs_base = cs_base;
19470 : :
19471 : : /* As gfc_resolve can be called during resolution of an OpenMP construct
19472 : : body, we should clear any state associated to it, so that say NS's
19473 : : DO loops are not interpreted as OpenMP loops. */
19474 : 286503 : if (!ns->construct_entities)
19475 : 275019 : gfc_omp_save_and_clear_state (&old_omp_state);
19476 : :
19477 : 286503 : resolve_types (ns);
19478 : 286503 : component_assignment_level = 0;
19479 : 286503 : resolve_codes (ns);
19480 : :
19481 : 286502 : if (ns->omp_assumes)
19482 : 13 : gfc_resolve_omp_assumptions (ns->omp_assumes);
19483 : :
19484 : 286502 : gfc_current_ns = old_ns;
19485 : 286502 : cs_base = old_cs_base;
19486 : 286502 : ns->resolved = 1;
19487 : :
19488 : 286502 : gfc_run_passes (ns);
19489 : :
19490 : 286502 : if (!ns->construct_entities)
19491 : 275018 : gfc_omp_restore_state (&old_omp_state);
19492 : : }
|