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 : 48863 : is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
99 : : {
100 : 53267 : 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 : 1465828 : resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
115 : : {
116 : 1465828 : 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 : 5001 : check_proc_interface (gfc_symbol *ifc, locus *where)
137 : : {
138 : : /* Several checks for F08:C1216. */
139 : 5001 : 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 : 4999 : 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 : 4995 : 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 : 4991 : if (gfc_is_intrinsic (ifc, 0, ifc->declared_at)
166 : 4991 : || gfc_is_intrinsic (ifc, 1, ifc->declared_at))
167 : 17 : ifc->attr.intrinsic = 1;
168 : 4991 : 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 : 4988 : 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 : 502959 : gfc_resolve_formal_arglist (gfc_symbol *proc)
267 : : {
268 : 502959 : gfc_formal_arglist *f;
269 : 502959 : gfc_symbol *sym;
270 : 502959 : bool saved_specification_expr;
271 : 502959 : int i;
272 : :
273 : 502959 : if (proc->result != NULL)
274 : 316676 : sym = proc->result;
275 : : else
276 : : sym = proc;
277 : :
278 : 502959 : if (gfc_elemental (proc)
279 : 341601 : || sym->attr.pointer || sym->attr.allocatable
280 : 832958 : || (sym->as && sym->as->rank != 0))
281 : : {
282 : 175235 : proc->attr.always_explicit = 1;
283 : 175235 : sym->attr.always_explicit = 1;
284 : : }
285 : :
286 : 502959 : gfc_namespace *orig_current_ns = gfc_current_ns;
287 : 502959 : gfc_current_ns = gfc_get_procedure_ns (proc);
288 : :
289 : 1299640 : for (f = proc->formal; f; f = f->next)
290 : : {
291 : 796683 : gfc_array_spec *as;
292 : :
293 : 796683 : sym = f->sym;
294 : :
295 : 796683 : 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 : 797062 : && !resolve_procedure_interface (sym))
311 : : break;
312 : :
313 : 796512 : 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 : 796510 : if (sym->attr.if_source != IFSRC_UNKNOWN)
322 : 813 : gfc_resolve_formal_arglist (sym);
323 : :
324 : 796510 : 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 : 795697 : 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 : 796510 : as = sym->ts.type == BT_CLASS && sym->attr.class_ok
337 : 809519 : ? CLASS_DATA (sym)->as : sym->as;
338 : :
339 : 796510 : saved_specification_expr = specification_expr;
340 : 796510 : specification_expr = true;
341 : 796510 : gfc_resolve_array_spec (as, 0);
342 : 796510 : 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 : 796510 : 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 : 121667 : if ((as && as->rank > 0 && as->type == AS_ASSUMED_SHAPE)
361 : 108533 : || (as && as->type == AS_ASSUMED_RANK)
362 : 747694 : || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
363 : 737734 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
364 : 10978 : && (CLASS_DATA (sym)->attr.class_pointer
365 : 10501 : || CLASS_DATA (sym)->attr.allocatable
366 : 9624 : || CLASS_DATA (sym)->attr.target))
367 : 736380 : || sym->attr.optional)
368 : : {
369 : 74913 : proc->attr.always_explicit = 1;
370 : 74913 : if (proc->result)
371 : 35285 : 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 : 796510 : if (sym->attr.flavor == FL_UNKNOWN)
378 : 48691 : gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
379 : :
380 : 796510 : if (gfc_pure (proc))
381 : : {
382 : 325495 : 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 : 325466 : else if (!sym->attr.pointer)
393 : : {
394 : 325458 : 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 : 325458 : 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 : 325494 : 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 : 796508 : if (proc->attr.implicit_pure)
433 : : {
434 : 23662 : if (sym->attr.flavor == FL_PROCEDURE)
435 : : {
436 : 294 : if (!gfc_pure (sym))
437 : 274 : proc->attr.implicit_pure = 0;
438 : : }
439 : 23368 : else if (!sym->attr.pointer)
440 : : {
441 : 22587 : if (proc->attr.function && sym->attr.intent != INTENT_IN
442 : 2625 : && !sym->value)
443 : 2625 : proc->attr.implicit_pure = 0;
444 : :
445 : 22587 : if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN
446 : 4081 : && !sym->value)
447 : 4081 : proc->attr.implicit_pure = 0;
448 : : }
449 : : }
450 : :
451 : 796508 : if (gfc_elemental (proc))
452 : : {
453 : : /* F08:C1289. */
454 : 300107 : if (sym->attr.codimension
455 : 300106 : || (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 : 300104 : 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 : 300102 : if (sym->attr.allocatable
472 : 300101 : || (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 : 300100 : if (sym->attr.pointer
482 : 300099 : || (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 : 300098 : 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 : 300096 : 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 : 796495 : 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 : 502959 : if (sym)
537 : 502867 : sym->formal_resolved = 1;
538 : 502959 : gfc_current_ns = orig_current_ns;
539 : 502959 : }
540 : :
541 : :
542 : : /* Work function called when searching for symbols that have argument lists
543 : : associated with them. */
544 : :
545 : : static void
546 : 1751872 : find_arglists (gfc_symbol *sym)
547 : : {
548 : 1751872 : if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns
549 : 318051 : || gfc_fl_struct (sym->attr.flavor) || sym->attr.intrinsic)
550 : : return;
551 : :
552 : 316421 : 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 : 332209 : resolve_formal_arglists (gfc_namespace *ns)
561 : : {
562 : 0 : if (ns == NULL)
563 : : return;
564 : :
565 : 332209 : gfc_traverse_ns (ns, find_arglists);
566 : : }
567 : :
568 : :
569 : : static void
570 : 35672 : resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
571 : : {
572 : 35672 : bool t;
573 : :
574 : 35672 : if (sym && sym->attr.flavor == FL_PROCEDURE
575 : 35672 : && 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 : 35672 : if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
585 : 10397 : || sym->attr.entry_master)
586 : 25463 : return;
587 : :
588 : 10209 : if (!sym->result)
589 : : return;
590 : :
591 : : /* Try to find out of what the return type is. */
592 : 10209 : 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 : 10209 : 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 : 367376 : resolve_entries (gfc_namespace *ns)
702 : : {
703 : 367376 : gfc_namespace *old_ns;
704 : 367376 : gfc_code *c;
705 : 367376 : gfc_symbol *proc;
706 : 367376 : gfc_entry_list *el;
707 : : /* Provide sufficient space to hold "master.%d.%s". */
708 : 367376 : char name[GFC_MAX_SYMBOL_LEN + 1 + 18];
709 : 367376 : static int master_count = 0;
710 : :
711 : 367376 : if (ns->proc_name == NULL)
712 : 366708 : return;
713 : :
714 : : /* No need to do anything if this procedure doesn't have alternate entry
715 : : points. */
716 : 367328 : 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 : 334105 : resolve_common_vars (gfc_common_head *common_block, bool named_common)
971 : : {
972 : 334105 : gfc_symbol *csym = common_block->head;
973 : 334105 : gfc_gsymbol *gsym;
974 : :
975 : 340075 : for (; csym; csym = csym->common_next)
976 : : {
977 : 5970 : gsym = gfc_find_gsymbol (gfc_gsym_root, csym->name);
978 : 5970 : 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 : 5970 : if (!csym->attr.use_assoc && !csym->attr.used_in_submodule)
993 : : {
994 : 5697 : gfc_add_in_common (&csym->attr, csym->name, &common_block->where);
995 : 5697 : gfc_notify_std (GFC_STD_F2018_OBS, "COMMON block at %L",
996 : : &common_block->where);
997 : : }
998 : :
999 : 5970 : if (csym->value || csym->attr.data)
1000 : : {
1001 : 132 : if (!csym->ns->is_block_data)
1002 : 32 : gfc_notify_std (GFC_STD_GNU, "Variable %qs at %L is in COMMON "
1003 : : "but only in BLOCK DATA initialization is "
1004 : : "allowed", csym->name, &csym->declared_at);
1005 : 100 : else if (!named_common)
1006 : 8 : gfc_notify_std (GFC_STD_GNU, "Initialized variable %qs at %L is "
1007 : : "in a blank COMMON but initialization is only "
1008 : : "allowed in named common blocks", csym->name,
1009 : : &csym->declared_at);
1010 : : }
1011 : :
1012 : 5970 : 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 : 5970 : 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 : 5970 : if (csym->ts.type != BT_DERIVED)
1026 : 5923 : 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 : 334105 : }
1046 : :
1047 : : /* Resolve common blocks. */
1048 : : static void
1049 : 332674 : resolve_common_blocks (gfc_symtree *common_root)
1050 : : {
1051 : 332674 : gfc_symbol *sym = NULL;
1052 : 332674 : gfc_gsymbol * gsym;
1053 : :
1054 : 332674 : if (common_root == NULL)
1055 : 332552 : return;
1056 : :
1057 : 1896 : if (common_root->left)
1058 : 221 : resolve_common_blocks (common_root->left);
1059 : 1896 : if (common_root->right)
1060 : 244 : resolve_common_blocks (common_root->right);
1061 : :
1062 : 1896 : 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 : 1896 : if (!common_root->n.common->binding_label
1068 : 1896 : || gfc_notification_std (GFC_STD_F2008))
1069 : : {
1070 : 3648 : gsym = gfc_find_gsymbol (gfc_gsym_root,
1071 : 1824 : common_root->n.common->name);
1072 : :
1073 : 820 : if (gsym && gfc_notification_std (GFC_STD_F2008)
1074 : 14 : && gsym->type == GSYM_COMMON
1075 : 1837 : && ((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 : 1818 : 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 : 1004 : if (!gsym)
1111 : : {
1112 : 1004 : gsym = gfc_get_gsymbol (common_root->n.common->name, false);
1113 : 1004 : gsym->type = GSYM_COMMON;
1114 : 1004 : gsym->where = common_root->n.common->where;
1115 : 1004 : gsym->defined = 1;
1116 : : }
1117 : 1817 : gsym->used = 1;
1118 : : }
1119 : :
1120 : 1889 : 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 : 1888 : gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
1143 : 1888 : 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 : 332209 : resolve_contained_functions (gfc_namespace *ns)
1181 : : {
1182 : 332209 : gfc_namespace *child;
1183 : 332209 : gfc_entry_list *el;
1184 : :
1185 : 332209 : resolve_formal_arglists (ns);
1186 : :
1187 : 367376 : for (child = ns->contained; child; child = child->sibling)
1188 : : {
1189 : : /* Resolve alternate entry points first. */
1190 : 35167 : resolve_entries (child);
1191 : :
1192 : : /* Then check function return types. */
1193 : 35167 : resolve_contained_fntype (child->proc_name, child);
1194 : 35672 : for (el = child->entries; el; el = el->next)
1195 : 505 : resolve_contained_fntype (el->sym, child);
1196 : : }
1197 : 332209 : }
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 : 59540 : resolve_structure_cons (gfc_expr *expr, int init)
1294 : : {
1295 : 59540 : gfc_constructor *cons;
1296 : 59540 : gfc_component *comp;
1297 : 59540 : bool t;
1298 : 59540 : symbol_attribute a;
1299 : :
1300 : 59540 : t = true;
1301 : :
1302 : 59540 : if (expr->ts.type == BT_DERIVED || expr->ts.type == BT_UNION)
1303 : : {
1304 : 56765 : if (expr->ts.u.derived->attr.flavor == FL_DERIVED)
1305 : 56615 : 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 : 56765 : 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 : 59540 : if (expr->ref)
1333 : 160 : comp = expr->ref->u.c.sym->components;
1334 : 59380 : else if ((expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS
1335 : : || expr->ts.type == BT_UNION)
1336 : 59378 : && expr->ts.u.derived)
1337 : 59378 : comp = expr->ts.u.derived->components;
1338 : : else
1339 : : return false;
1340 : :
1341 : 59538 : cons = gfc_constructor_first (expr->value.constructor);
1342 : :
1343 : 196130 : for (; comp && cons; comp = comp->next, cons = gfc_constructor_next (cons))
1344 : : {
1345 : 136594 : int rank;
1346 : :
1347 : 136594 : if (!cons->expr)
1348 : 8986 : 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 : 127608 : if (cons->expr->ts.type == BT_UNION && cons->expr->expr_type == EXPR_NULL)
1354 : 15 : continue;
1355 : :
1356 : 127593 : if (!gfc_resolve_expr (cons->expr))
1357 : : {
1358 : 0 : t = false;
1359 : 0 : continue;
1360 : : }
1361 : :
1362 : 127593 : rank = comp->as ? comp->as->rank : 0;
1363 : 127593 : 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 : 127593 : if (comp->ts.type == BT_CLASS && cons->expr->ts.type != BT_CLASS)
1369 : 191 : gfc_find_vtab (&cons->expr->ts);
1370 : :
1371 : 127593 : 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 : 223447 : if (!comp->attr.proc_pointer &&
1384 : 95854 : !gfc_compare_types (&cons->expr->ts, &comp->ts))
1385 : : {
1386 : 11599 : 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 : 8425 : cons->expr->ts = comp->ts;
1392 : : }
1393 : 3174 : 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 : 3172 : else if (!UNLIMITED_POLY (comp))
1403 : : {
1404 : 3134 : bool t2 = gfc_convert_type (cons->expr, &comp->ts, 1);
1405 : 3134 : if (t)
1406 : 127593 : 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 : 127593 : 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 : 127593 : if (cons->expr->expr_type == EXPR_NULL
1469 : 37989 : && !(comp->attr.pointer || comp->attr.allocatable
1470 : 19229 : || 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 : 127593 : if (comp->attr.proc_pointer && comp->ts.interface)
1483 : : {
1484 : : /* Check procedure pointer interface. */
1485 : 13866 : gfc_symbol *s2 = NULL;
1486 : 13866 : gfc_component *c2;
1487 : 13866 : const char *name;
1488 : 13866 : char err[200];
1489 : :
1490 : 13866 : c2 = gfc_get_proc_ptr_comp (cons->expr);
1491 : 13866 : if (c2)
1492 : : {
1493 : 12 : s2 = c2->ts.interface;
1494 : 12 : name = c2->name;
1495 : : }
1496 : 13854 : 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 : 13854 : else if (cons->expr->expr_type != EXPR_NULL)
1502 : : {
1503 : 13484 : s2 = cons->expr->symtree->n.sym;
1504 : 13484 : name = cons->expr->symtree->n.sym->name;
1505 : : }
1506 : :
1507 : 13496 : 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 : 127591 : 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 : 127591 : if (!comp->attr.pointer || comp->attr.proc_pointer
1555 : 20431 : || cons->expr->expr_type == EXPR_NULL)
1556 : 118221 : continue;
1557 : :
1558 : 9370 : a = gfc_expr_attr (cons->expr);
1559 : :
1560 : 9370 : 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 : 9370 : if (init)
1569 : : {
1570 : : /* F08:C461. Additional checks for pointer initialization. */
1571 : 9302 : 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 : 9302 : 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 : 9370 : if (comp->attr.pointer && (a.pointer || a.target)
1588 : 18739 : && 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 : 9370 : bool impure = cons->expr->expr_type == EXPR_VARIABLE
1597 : 9370 : && (gfc_impure_variable (cons->expr->symtree->n.sym)
1598 : 9334 : || 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 : 9370 : 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 : 725396 : was_declared (gfc_symbol *sym)
1622 : : {
1623 : 725396 : symbol_attribute a;
1624 : :
1625 : 725396 : a = sym->attr;
1626 : :
1627 : 725396 : if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
1628 : : return 1;
1629 : :
1630 : 615042 : if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
1631 : 606773 : || a.optional || a.pointer || a.save || a.target || a.volatile_
1632 : 606771 : || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
1633 : 606717 : || 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 : 402710 : generic_sym (gfc_symbol *sym)
1644 : : {
1645 : 402710 : gfc_symbol *s;
1646 : :
1647 : 402710 : if (sym->attr.generic ||
1648 : 374849 : (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
1649 : 28924 : return 1;
1650 : :
1651 : 373786 : if (was_declared (sym) || sym->ns->parent == NULL)
1652 : : return 0;
1653 : :
1654 : 77432 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1655 : :
1656 : 77432 : 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 : 372591 : specific_sym (gfc_symbol *sym)
1672 : : {
1673 : 372591 : gfc_symbol *s;
1674 : :
1675 : 372591 : if (sym->attr.if_source == IFSRC_IFBODY
1676 : 361637 : || sym->attr.proc == PROC_MODULE
1677 : : || sym->attr.proc == PROC_INTERNAL
1678 : : || sym->attr.proc == PROC_ST_FUNCTION
1679 : 287249 : || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
1680 : 659109 : || sym->attr.external)
1681 : 88454 : return 1;
1682 : :
1683 : 284137 : if (was_declared (sym) || sym->ns->parent == NULL)
1684 : : return 0;
1685 : :
1686 : 75889 : gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1687 : :
1688 : 75889 : 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 : 401311 : procedure_kind (gfc_symbol *sym)
1699 : : {
1700 : 401311 : if (generic_sym (sym))
1701 : : return PTYPE_GENERIC;
1702 : :
1703 : 372530 : if (specific_sym (sym))
1704 : 88454 : 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 : 1382723 : check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
1716 : : {
1717 : 1382723 : 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 : 223945 : resolve_assumed_size_actual (gfc_expr *e)
1744 : : {
1745 : 223945 : if (e == NULL)
1746 : : return false;
1747 : :
1748 : 223466 : switch (e->expr_type)
1749 : : {
1750 : 107746 : case EXPR_VARIABLE:
1751 : 107746 : if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
1752 : : return true;
1753 : : break;
1754 : :
1755 : 46993 : case EXPR_OP:
1756 : 46993 : if (resolve_assumed_size_actual (e->value.op.op1)
1757 : 46993 : || 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 : 144687 : is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1809 : : {
1810 : 144687 : gfc_symbol* proc_sym;
1811 : 144687 : gfc_symbol* context_proc;
1812 : 144687 : gfc_namespace* real_context;
1813 : :
1814 : 144687 : 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 : 144686 : 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 : 144686 : 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 : 1656 : for (real_context = context; ; real_context = real_context->parent)
1832 : : {
1833 : : /* We should find something, eventually! */
1834 : 124130 : gcc_assert (real_context);
1835 : :
1836 : 124130 : 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 : 124130 : if (!context_proc)
1846 : : return false;
1847 : :
1848 : 123866 : 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 : 122210 : 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 : 122196 : if (context_proc->attr.contained)
1859 : : {
1860 : 20379 : gfc_symbol* parent_proc;
1861 : :
1862 : 20379 : gcc_assert (context->parent);
1863 : 20379 : parent_proc = (context->parent->entries ? context->parent->entries->sym
1864 : : : context->parent->proc_name);
1865 : :
1866 : 20379 : 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 : 40965 : gfc_resolve_intrinsic (gfc_symbol *sym, locus *loc)
1879 : : {
1880 : 40965 : gfc_intrinsic_sym* isym = NULL;
1881 : 40965 : const char* symstd;
1882 : :
1883 : 40965 : if (sym->resolve_symbol_called >= 2)
1884 : : return true;
1885 : :
1886 : 31364 : sym->resolve_symbol_called = 2;
1887 : :
1888 : : /* Already resolved. */
1889 : 31364 : 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 : 23628 : if (sym->intmod_sym_id && sym->attr.subroutine)
1898 : : {
1899 : 8513 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1900 : 8513 : isym = gfc_intrinsic_subroutine_by_id (id);
1901 : 8513 : }
1902 : 15115 : else if (sym->intmod_sym_id)
1903 : : {
1904 : 11749 : gfc_isym_id id = gfc_isym_id_by_intmod_sym (sym);
1905 : 11749 : isym = gfc_intrinsic_function_by_id (id);
1906 : : }
1907 : 3366 : else if (!sym->attr.subroutine)
1908 : 3294 : isym = gfc_find_function (sym->name);
1909 : :
1910 : 23556 : if (isym && !sym->attr.subroutine)
1911 : : {
1912 : 15004 : 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 : 18565 : if (!sym->attr.function &&
1919 : 3561 : !gfc_add_function(&sym->attr, sym->name, loc))
1920 : : return false;
1921 : :
1922 : 15004 : sym->ts = isym->ts;
1923 : : }
1924 : 8624 : else if (isym || (isym = gfc_find_subroutine (sym->name)))
1925 : : {
1926 : 8621 : 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 : 8655 : 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 : 23623 : gfc_copy_formal_args_intr (sym, isym, NULL);
1945 : :
1946 : 23623 : sym->attr.pure = isym->pure;
1947 : 23623 : sym->attr.elemental = isym->elemental;
1948 : :
1949 : : /* Check it is actually available in the standard settings. */
1950 : 23623 : 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 : 1287247 : resolve_procedure_expression (gfc_expr* expr)
1969 : : {
1970 : 1287247 : gfc_symbol* sym;
1971 : :
1972 : 1287247 : if (expr->expr_type != EXPR_VARIABLE)
1973 : : return true;
1974 : 1287242 : gcc_assert (expr->symtree);
1975 : :
1976 : 1287242 : sym = expr->symtree->n.sym;
1977 : :
1978 : 1287242 : if (sym->attr.intrinsic)
1979 : 1346 : gfc_resolve_intrinsic (sym, &expr->where);
1980 : :
1981 : 1287242 : if (sym->attr.flavor != FL_PROCEDURE
1982 : 29456 : || (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 : 13876 : 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 : 414808 : resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
2031 : : bool no_formal_args)
2032 : : {
2033 : 414808 : gfc_symbol *sym;
2034 : 414808 : gfc_symtree *parent_st;
2035 : 414808 : gfc_expr *e;
2036 : 414808 : gfc_component *comp;
2037 : 414808 : int save_need_full_assumed_size;
2038 : 414808 : bool return_value = false;
2039 : 414808 : bool actual_arg_sav = actual_arg, first_actual_arg_sav = first_actual_arg;
2040 : :
2041 : 414808 : actual_arg = true;
2042 : 414808 : first_actual_arg = true;
2043 : :
2044 : 1068239 : for (; arg; arg = arg->next)
2045 : : {
2046 : 653530 : e = arg->expr;
2047 : 653530 : 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 : 651196 : if (e->expr_type == EXPR_VARIABLE
2064 : 287367 : && e->symtree->n.sym->attr.generic
2065 : 8 : && no_formal_args
2066 : 651201 : && count_specific_procs (e) != 1)
2067 : 2 : goto cleanup;
2068 : :
2069 : 651194 : if (e->ts.type != BT_PROCEDURE)
2070 : : {
2071 : 580621 : save_need_full_assumed_size = need_full_assumed_size;
2072 : 580621 : if (e->expr_type != EXPR_VARIABLE)
2073 : 363829 : need_full_assumed_size = 0;
2074 : 580621 : if (!gfc_resolve_expr (e))
2075 : 60 : goto cleanup;
2076 : 580561 : need_full_assumed_size = save_need_full_assumed_size;
2077 : 580561 : goto argument_list;
2078 : : }
2079 : :
2080 : : /* See if the expression node should really be a variable reference. */
2081 : :
2082 : 70573 : sym = e->symtree->n.sym;
2083 : :
2084 : 70573 : 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 : 70570 : if (sym->attr.flavor == FL_PROCEDURE
2092 : 67473 : || sym->attr.intrinsic
2093 : 67473 : || 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 : 67473 : if (was_declared (sym) || sym->ns->parent == NULL)
2172 : 67380 : 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 : 67473 : e->expr_type = EXPR_VARIABLE;
2197 : 67473 : e->ts = sym->ts;
2198 : 67473 : if ((sym->as != NULL && sym->ts.type != BT_CLASS)
2199 : 34968 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
2200 : 3757 : && CLASS_DATA (sym)->as))
2201 : : {
2202 : 37915 : gfc_array_spec *as
2203 : 35210 : = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
2204 : 35210 : e->rank = as->rank;
2205 : 35210 : e->corank = as->corank;
2206 : 35210 : e->ref = gfc_get_ref ();
2207 : 35210 : e->ref->type = REF_ARRAY;
2208 : 35210 : e->ref->u.ar.type = AR_FULL;
2209 : 35210 : 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 : 67473 : if (e->expr_type == EXPR_VARIABLE
2216 : 67473 : && 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 : 67473 : save_need_full_assumed_size = need_full_assumed_size;
2225 : 67473 : if (e->expr_type != EXPR_VARIABLE)
2226 : 0 : need_full_assumed_size = 0;
2227 : 67473 : if (!gfc_resolve_expr (e))
2228 : 22 : goto cleanup;
2229 : 67451 : need_full_assumed_size = save_need_full_assumed_size;
2230 : :
2231 : 651106 : argument_list:
2232 : : /* Check argument list functions %VAL, %LOC and %REF. There is
2233 : : nothing to do for %REF. */
2234 : 651106 : 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 : 651100 : comp = gfc_get_proc_ptr_comp(e);
2281 : 651100 : if (e->expr_type == EXPR_VARIABLE
2282 : 285989 : && 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 : 285989 : if (e->expr_type == EXPR_VARIABLE && gfc_is_coindexed (e)
2291 : 651406 : && 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 : 651097 : first_actual_arg = false;
2299 : : }
2300 : :
2301 : : return_value = true;
2302 : :
2303 : 414808 : cleanup:
2304 : 414808 : actual_arg = actual_arg_sav;
2305 : 414808 : first_actual_arg = first_actual_arg_sav;
2306 : :
2307 : 414808 : 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 : 316475 : resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
2317 : : {
2318 : 316475 : gfc_actual_arglist *arg0;
2319 : 316475 : gfc_actual_arglist *arg;
2320 : 316475 : gfc_symbol *esym = NULL;
2321 : 316475 : gfc_intrinsic_sym *isym = NULL;
2322 : 316475 : gfc_expr *e = NULL;
2323 : 316475 : gfc_intrinsic_arg *iformal = NULL;
2324 : 316475 : gfc_formal_arglist *eformal = NULL;
2325 : 316475 : bool formal_optional = false;
2326 : 316475 : bool set_by_optional = false;
2327 : 316475 : int i;
2328 : 316475 : int rank = 0;
2329 : :
2330 : : /* Is this an elemental procedure? */
2331 : 316475 : if (expr && expr->value.function.actual != NULL)
2332 : : {
2333 : 229763 : if (expr->value.function.esym != NULL
2334 : 42946 : && expr->value.function.esym->attr.elemental)
2335 : : {
2336 : : arg0 = expr->value.function.actual;
2337 : : esym = expr->value.function.esym;
2338 : : }
2339 : 213491 : else if (expr->value.function.isym != NULL
2340 : 185796 : && 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 : 86712 : else if (c && c->ext.actual != NULL)
2349 : : {
2350 : 68708 : arg0 = c->ext.actual;
2351 : :
2352 : 68708 : if (c->resolved_sym)
2353 : : esym = c->resolved_sym;
2354 : : else
2355 : 307 : esym = c->symtree->n.sym;
2356 : 68708 : gcc_assert (esym);
2357 : :
2358 : 68708 : 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 : 171212 : for (arg = arg0; arg; arg = arg->next)
2366 : : {
2367 : 110949 : 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 : 70579 : formal_optional = false;
2395 : 70579 : if (isym)
2396 : 48346 : iformal = isym->formal;
2397 : : else
2398 : 22233 : eformal = esym->formal;
2399 : :
2400 : 186789 : for (arg = arg0; arg; arg = arg->next)
2401 : : {
2402 : 116210 : if (eformal)
2403 : : {
2404 : 39879 : if (eformal->sym && eformal->sym->attr.optional)
2405 : 39879 : formal_optional = true;
2406 : 39879 : eformal = eformal->next;
2407 : : }
2408 : 76331 : else if (isym && iformal)
2409 : : {
2410 : 66343 : if (iformal->optional)
2411 : 13186 : formal_optional = true;
2412 : 66343 : iformal = iformal->next;
2413 : : }
2414 : 9988 : else if (isym)
2415 : 9980 : formal_optional = true;
2416 : :
2417 : 116210 : if (pedantic && arg->expr != NULL
2418 : 70935 : && arg->expr->expr_type == EXPR_VARIABLE
2419 : 34361 : && 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 : 186778 : for (arg = arg0; arg; arg = arg->next)
2453 : : {
2454 : 116208 : if (arg->expr == NULL || arg->expr->rank == 0)
2455 : 103250 : 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 : 70570 : 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 : 28783 : resolve_global_procedure (gfc_symbol *sym, locus *where, int sub)
2675 : : {
2676 : 28783 : gfc_gsymbol * gsym;
2677 : 28783 : gfc_namespace *ns;
2678 : 28783 : enum gfc_symbol_type type;
2679 : 28783 : char reason[200];
2680 : :
2681 : 28783 : type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2682 : :
2683 : 28783 : gsym = gfc_get_gsymbol (sym->binding_label ? sym->binding_label : sym->name,
2684 : 28783 : sym->binding_label != NULL);
2685 : :
2686 : 28783 : if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
2687 : 10 : gfc_global_used (gsym, where);
2688 : :
2689 : 28783 : if ((sym->attr.if_source == IFSRC_UNKNOWN
2690 : 8778 : || sym->attr.if_source == IFSRC_IFBODY)
2691 : 24794 : && 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 : 43587 : && 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 : 28783 : done:
2792 : :
2793 : 28783 : if (gsym->type == GSYM_UNKNOWN)
2794 : : {
2795 : 3799 : gsym->type = type;
2796 : 3799 : gsym->where = *where;
2797 : : }
2798 : :
2799 : 28783 : gsym->used = 1;
2800 : 28783 : }
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 : 26739 : resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
2810 : : {
2811 : 26739 : gfc_symbol *s;
2812 : :
2813 : 26739 : if (sym->attr.generic)
2814 : : {
2815 : 25634 : s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
2816 : 25634 : 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 : 7100 : 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 : 26598 : resolve_generic_f (gfc_expr *expr)
2855 : : {
2856 : 26598 : gfc_symbol *sym;
2857 : 26598 : match m;
2858 : 26598 : gfc_interface *intr = NULL;
2859 : :
2860 : 26598 : sym = expr->symtree->n.sym;
2861 : :
2862 : 26739 : for (;;)
2863 : : {
2864 : 26739 : m = resolve_generic_f0 (expr, sym);
2865 : 26739 : if (m == MATCH_YES)
2866 : : return true;
2867 : 6040 : else if (m == MATCH_ERROR)
2868 : : return false;
2869 : :
2870 : 6040 : generic:
2871 : 6043 : if (!intr)
2872 : 5997 : for (intr = sym->generic; intr; intr = intr->next)
2873 : 5914 : if (gfc_fl_struct (intr->sym->attr.flavor))
2874 : : break;
2875 : :
2876 : 6043 : 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 : 5899 : 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 : 5895 : if (intr)
2901 : : {
2902 : 5860 : if (!gfc_convert_to_structure_constructor (expr, intr->sym, NULL,
2903 : : NULL, false))
2904 : : return false;
2905 : 5840 : if (!gfc_use_derived (expr->ts.u.derived))
2906 : : return false;
2907 : 5839 : 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 : 27116 : resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
2927 : : {
2928 : 27116 : match m;
2929 : :
2930 : 27116 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
2931 : : {
2932 : 7823 : if (sym->attr.dummy)
2933 : : {
2934 : 274 : sym->attr.proc = PROC_DUMMY;
2935 : 274 : goto found;
2936 : : }
2937 : :
2938 : 7549 : sym->attr.proc = PROC_EXTERNAL;
2939 : 7549 : goto found;
2940 : : }
2941 : :
2942 : 19293 : if (sym->attr.proc == PROC_MODULE
2943 : : || sym->attr.proc == PROC_ST_FUNCTION
2944 : : || sym->attr.proc == PROC_INTERNAL)
2945 : 18555 : 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 : 26378 : found:
2962 : 26378 : gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2963 : :
2964 : 26378 : if (sym->result)
2965 : 26378 : expr->ts = sym->result->ts;
2966 : : else
2967 : 0 : expr->ts = sym->ts;
2968 : 26378 : expr->value.function.name = sym->name;
2969 : 26378 : expr->value.function.esym = sym;
2970 : : /* Prevent crash when sym->ts.u.derived->components is not set due to previous
2971 : : error(s). */
2972 : 26378 : if (sym->ts.type == BT_CLASS && !CLASS_DATA (sym))
2973 : : return MATCH_ERROR;
2974 : 26377 : 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 : 26067 : 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 : 27109 : resolve_specific_f (gfc_expr *expr)
2991 : : {
2992 : 27109 : gfc_symbol *sym;
2993 : 27109 : match m;
2994 : :
2995 : 27109 : sym = expr->symtree->n.sym;
2996 : :
2997 : 27116 : for (;;)
2998 : : {
2999 : 27116 : m = resolve_specific_f0 (sym, expr);
3000 : 27116 : 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 : 268723 : resolve_unknown_f (gfc_expr *expr)
3062 : : {
3063 : 268723 : gfc_symbol *sym;
3064 : 268723 : gfc_typespec *ts;
3065 : :
3066 : 268723 : sym = expr->symtree->n.sym;
3067 : :
3068 : 268723 : 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 : 268435 : if (gfc_is_intrinsic (sym, 0, expr->where))
3078 : : {
3079 : 266176 : 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 : 827528 : is_external_proc (gfc_symbol *sym)
3145 : : {
3146 : 825846 : if (!sym->attr.dummy && !sym->attr.contained
3147 : 720498 : && !gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at)
3148 : 156636 : && sym->attr.proc != PROC_ST_FUNCTION
3149 : 156041 : && !sym->attr.proc_pointer
3150 : 154933 : && !sym->attr.use_assoc
3151 : 885118 : && 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 : 249542 : gfc_pure_function (gfc_expr *e, const char **name)
3166 : : {
3167 : 249542 : bool pure;
3168 : 249542 : gfc_component *comp;
3169 : :
3170 : 249542 : *name = NULL;
3171 : :
3172 : 249542 : if (e->symtree != NULL
3173 : 249225 : && e->symtree->n.sym != NULL
3174 : 249225 : && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
3175 : 305 : return pure_stmt_function (e, e->symtree->n.sym);
3176 : :
3177 : 249237 : comp = gfc_get_proc_ptr_comp (e);
3178 : 249237 : if (comp)
3179 : : {
3180 : 290 : pure = gfc_pure (comp->ts.interface);
3181 : 290 : *name = comp->name;
3182 : : }
3183 : 248947 : else if (e->value.function.esym)
3184 : : {
3185 : 51517 : pure = gfc_pure (e->value.function.esym);
3186 : 51517 : *name = e->value.function.esym->name;
3187 : : }
3188 : 197430 : else if (e->value.function.isym)
3189 : : {
3190 : 392790 : pure = e->value.function.isym->pure
3191 : 196395 : || e->value.function.isym->elemental;
3192 : 196395 : *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 : 36837 : gfc_implicit_pure_function (gfc_expr *e)
3216 : : {
3217 : 36837 : gfc_component *comp = gfc_get_proc_ptr_comp (e);
3218 : 36837 : if (comp)
3219 : 274 : return gfc_implicit_pure (comp->ts.interface);
3220 : 36563 : else if (e->value.function.esym)
3221 : 31213 : 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 : 237812 : static bool check_pure_function (gfc_expr *e)
3255 : : {
3256 : 237812 : const char *name = NULL;
3257 : 237812 : code_stack *stack;
3258 : 237812 : 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 : 550012 : for (stack = cs_base; stack; stack = stack->prev)
3266 : : {
3267 : 312202 : if (!saw_block && stack->current->op == EXEC_BLOCK)
3268 : : {
3269 : 6690 : saw_block = true;
3270 : 6690 : continue;
3271 : : }
3272 : :
3273 : 5087 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3274 : : {
3275 : 9 : bool is_pure;
3276 : 312200 : 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 : 237810 : if (!gfc_pure_function (e, &name) && name)
3292 : : {
3293 : 35571 : 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 : 35567 : 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 : 35565 : 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 : 35560 : if (!gfc_implicit_pure_function (e))
3314 : 29604 : 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 : 128885 : 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 : 128885 : gfc_namespace *sibling = gfc_current_ns->sibling;
3329 : 243116 : for (; sibling; sibling = sibling->sibling)
3330 : : {
3331 : 120954 : if (sibling->proc_name == sym)
3332 : : {
3333 : 6723 : gfc_resolve (sibling);
3334 : 6723 : 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 : 128885 : if ((sym->attr.array_outer_dependency || sym->attr.proc_pointer)
3341 : 66557 : && gfc_current_ns->proc_name)
3342 : 66513 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3343 : 128885 : }
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 : 335290 : resolve_function (gfc_expr *expr)
3351 : : {
3352 : 335290 : gfc_actual_arglist *arg;
3353 : 335290 : gfc_symbol *sym;
3354 : 335290 : bool t;
3355 : 335290 : int temp;
3356 : 335290 : procedure_type p = PROC_INTRINSIC;
3357 : 335290 : bool no_formal_args;
3358 : :
3359 : 335290 : sym = NULL;
3360 : 335290 : if (expr->symtree)
3361 : 334973 : sym = expr->symtree->n.sym;
3362 : :
3363 : : /* If this is a procedure pointer component, it has already been resolved. */
3364 : 335290 : 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 : 334893 : 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 : 334893 : 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 : 334575 : if (sym && sym->attr.intrinsic
3382 : 343220 : && !gfc_resolve_intrinsic (sym, &expr->where))
3383 : : return false;
3384 : :
3385 : 334892 : 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 : 334571 : 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 : 334570 : if (sym && sym->attr.abstract && sym->attr.function
3403 : 191 : && 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 : 334886 : need_full_assumed_size++;
3416 : :
3417 : 334886 : if (expr->symtree && expr->symtree->n.sym)
3418 : 334569 : p = expr->symtree->n.sym->attr.proc;
3419 : :
3420 : 334886 : if (expr->value.function.isym && expr->value.function.isym->inquiry)
3421 : 1078 : inquiry_argument = true;
3422 : 334569 : no_formal_args = sym && is_external_proc (sym)
3423 : 348184 : && gfc_sym_get_dummy_args (sym) == NULL;
3424 : :
3425 : 334886 : 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 : 334820 : inquiry_argument = false;
3433 : :
3434 : : /* Resume assumed_size checking. */
3435 : 334820 : need_full_assumed_size--;
3436 : :
3437 : : /* If the procedure is external, check for usage. */
3438 : 334820 : if (sym && is_external_proc (sym))
3439 : 13279 : resolve_global_procedure (sym, &expr->where, 0);
3440 : :
3441 : 334820 : 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 : 334819 : 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 : 334819 : if (expr->value.function.name != NULL
3485 : 323216 : || expr->value.function.isym != NULL)
3486 : : {
3487 : 12389 : 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 : 322430 : switch (procedure_kind (sym))
3496 : : {
3497 : 26598 : case PTYPE_GENERIC:
3498 : 26598 : t = resolve_generic_f (expr);
3499 : 26598 : break;
3500 : :
3501 : 27109 : case PTYPE_SPECIFIC:
3502 : 27109 : t = resolve_specific_f (expr);
3503 : 27109 : break;
3504 : :
3505 : 268723 : case PTYPE_UNKNOWN:
3506 : 268723 : t = resolve_unknown_f (expr);
3507 : 268723 : 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 : 334819 : if (expr->expr_type != EXPR_FUNCTION)
3518 : : return t;
3519 : :
3520 : : /* Walk the argument list looking for invalid BOZ. */
3521 : 722878 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3522 : 485334 : 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 : 237544 : temp = need_full_assumed_size;
3531 : 237544 : need_full_assumed_size = 0;
3532 : :
3533 : 237544 : if (!resolve_elemental_actual (expr, NULL))
3534 : : return false;
3535 : :
3536 : 237541 : if (omp_workshare_flag
3537 : 32 : && expr->value.function.esym
3538 : 237546 : && ! 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 : 237537 : else if (expr->value.function.actual != NULL
3548 : 229760 : && expr->value.function.isym != NULL
3549 : 185795 : && 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 : 524786 : for (arg = expr->value.function.actual; arg; arg = arg->next)
3563 : : {
3564 : 364279 : 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 : 361876 : if (arg->expr != NULL
3580 : 242295 : && arg->expr->rank > 0
3581 : 478877 : && resolve_assumed_size_actual (arg->expr))
3582 : : return false;
3583 : : }
3584 : : }
3585 : : #undef GENERIC_ID
3586 : :
3587 : 237538 : need_full_assumed_size = temp;
3588 : :
3589 : 237538 : if (!check_pure_function(expr))
3590 : 12 : t = false;
3591 : :
3592 : : /* Functions without the RECURSIVE attribution are not allowed to
3593 : : * call themselves. */
3594 : 237538 : if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
3595 : : {
3596 : 50311 : gfc_symbol *esym;
3597 : 50311 : esym = expr->value.function.esym;
3598 : :
3599 : 50311 : 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 : 237538 : 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 : 237538 : 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 : 237538 : if (expr->ts.type == BT_DERIVED
3636 : 8878 : && !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 : 237538 : if (!expr->ref && !expr->value.function.isym)
3651 : : {
3652 : 51622 : if (expr->value.function.esym)
3653 : 50587 : update_current_proc_array_outer_dependency (expr->value.function.esym);
3654 : : else
3655 : 1035 : update_current_proc_array_outer_dependency (sym);
3656 : : }
3657 : 185916 : else if (expr->ref)
3658 : : /* typebound procedure: Assume the worst. */
3659 : 0 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
3660 : :
3661 : 237538 : if (expr->value.function.esym
3662 : 50587 : && 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 : 75120 : pure_subroutine (gfc_symbol *sym, const char *name, locus *loc)
3674 : : {
3675 : 75120 : code_stack *stack;
3676 : 75120 : bool saw_block = false;
3677 : :
3678 : 75120 : 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 : 155491 : for (stack = cs_base; stack; stack = stack->prev)
3687 : : {
3688 : 85770 : if (stack->current->op == EXEC_BLOCK)
3689 : : {
3690 : 1875 : saw_block = true;
3691 : 1875 : continue;
3692 : : }
3693 : :
3694 : 83895 : if (saw_block && stack->current->op == EXEC_DO_CONCURRENT)
3695 : : {
3696 : :
3697 : 2 : bool is_pure = true;
3698 : 85770 : 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 : 69721 : 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 : 69721 : 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 : 69715 : 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 : 69711 : gfc_unset_implicit_pure (NULL);
3728 : 69711 : return true;
3729 : : }
3730 : :
3731 : :
3732 : : static match
3733 : 2185 : resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
3734 : : {
3735 : 2185 : gfc_symbol *s;
3736 : :
3737 : 2185 : if (sym->attr.generic)
3738 : : {
3739 : 2184 : s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
3740 : 2184 : if (s != NULL)
3741 : : {
3742 : 2175 : c->resolved_sym = s;
3743 : 2175 : if (!pure_subroutine (s, s->name, &c->loc))
3744 : : return MATCH_ERROR;
3745 : 2175 : 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 : 2183 : resolve_generic_s (gfc_code *c)
3760 : : {
3761 : 2183 : gfc_symbol *sym;
3762 : 2183 : match m;
3763 : :
3764 : 2183 : sym = c->symtree->n.sym;
3765 : :
3766 : 2185 : for (;;)
3767 : : {
3768 : 2185 : m = resolve_generic_s0 (c, sym);
3769 : 2185 : 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 : 61345 : resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
3811 : : {
3812 : 61345 : match m;
3813 : :
3814 : 61345 : if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3815 : : {
3816 : 5598 : if (sym->attr.dummy)
3817 : : {
3818 : 256 : sym->attr.proc = PROC_DUMMY;
3819 : 256 : goto found;
3820 : : }
3821 : :
3822 : 5342 : sym->attr.proc = PROC_EXTERNAL;
3823 : 5342 : goto found;
3824 : : }
3825 : :
3826 : 55747 : if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3827 : 55747 : 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 : 61345 : found:
3844 : 61345 : gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3845 : :
3846 : 61345 : c->resolved_sym = sym;
3847 : 61345 : if (!pure_subroutine (sym, sym->name, &c->loc))
3848 : : return MATCH_ERROR;
3849 : :
3850 : : return MATCH_YES;
3851 : : }
3852 : :
3853 : :
3854 : : static bool
3855 : 61345 : resolve_specific_s (gfc_code *c)
3856 : : {
3857 : 61345 : gfc_symbol *sym;
3858 : 61345 : match m;
3859 : :
3860 : 61345 : sym = c->symtree->n.sym;
3861 : :
3862 : 61345 : for (;;)
3863 : : {
3864 : 61345 : m = resolve_specific_s0 (c, sym);
3865 : 61345 : 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 : 15353 : resolve_unknown_s (gfc_code *c)
3891 : : {
3892 : 15353 : gfc_symbol *sym;
3893 : :
3894 : 15353 : sym = c->symtree->n.sym;
3895 : :
3896 : 15353 : 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 : 15333 : if (gfc_is_intrinsic (sym, 1, c->loc))
3905 : : {
3906 : 3875 : 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 : :
3923 : : static bool
3924 : 805 : check_sym_import_status (gfc_symbol *sym, gfc_symtree *s, gfc_expr *e,
3925 : : gfc_code *c, gfc_namespace *ns)
3926 : : {
3927 : 805 : locus *here;
3928 : :
3929 : : /* If the type has been imported then its vtype functions are OK. */
3930 : 805 : if (e && e->expr_type == EXPR_FUNCTION && sym->attr.vtype)
3931 : : return true;
3932 : :
3933 : : if (e)
3934 : 791 : here = &e->where;
3935 : : else
3936 : 7 : here = &c->loc;
3937 : :
3938 : 798 : if (s && !s->import_only)
3939 : 705 : s = gfc_find_symtree (ns->sym_root, sym->name);
3940 : :
3941 : 798 : if (ns->import_state == IMPORT_ONLY
3942 : 75 : && sym->ns != ns
3943 : 58 : && (!s || !s->import_only))
3944 : : {
3945 : 21 : gfc_error ("F2018: C8102 %qs at %L is host associated but does not "
3946 : : "appear in an IMPORT or IMPORT, ONLY list", sym->name, here);
3947 : 21 : return false;
3948 : : }
3949 : 777 : else if (ns->import_state == IMPORT_NONE
3950 : 27 : && sym->ns != ns)
3951 : : {
3952 : 12 : gfc_error ("F2018: C8102 %qs at %L is host associated in a scope that "
3953 : : "has IMPORT, NONE", sym->name, here);
3954 : 12 : return false;
3955 : : }
3956 : : return true;
3957 : : }
3958 : :
3959 : :
3960 : : static bool
3961 : 6918 : check_import_status (gfc_expr *e)
3962 : : {
3963 : 6918 : gfc_symtree *st;
3964 : 6918 : gfc_ref *ref;
3965 : 6918 : gfc_symbol *sym, *der;
3966 : 6918 : gfc_namespace *ns = gfc_current_ns;
3967 : :
3968 : 6918 : switch (e->expr_type)
3969 : : {
3970 : 727 : case EXPR_VARIABLE:
3971 : 727 : case EXPR_FUNCTION:
3972 : 727 : case EXPR_SUBSTRING:
3973 : 727 : sym = e->symtree ? e->symtree->n.sym : NULL;
3974 : :
3975 : : /* Check the symbol itself. */
3976 : 727 : if (sym
3977 : 727 : && !(ns->proc_name
3978 : : && (sym == ns->proc_name))
3979 : 1450 : && !check_sym_import_status (sym, e->symtree, e, NULL, ns))
3980 : : return false;
3981 : :
3982 : : /* Check the declared derived type. */
3983 : 717 : if (sym->ts.type == BT_DERIVED)
3984 : : {
3985 : 16 : der = sym->ts.u.derived;
3986 : 16 : st = gfc_find_symtree (ns->sym_root, der->name);
3987 : :
3988 : 16 : if (!check_sym_import_status (der, st, e, NULL, ns))
3989 : : return false;
3990 : : }
3991 : 701 : else if (sym->ts.type == BT_CLASS && !UNLIMITED_POLY (sym))
3992 : : {
3993 : 44 : der = CLASS_DATA (sym) ? CLASS_DATA (sym)->ts.u.derived
3994 : : : sym->ts.u.derived;
3995 : 44 : st = gfc_find_symtree (ns->sym_root, der->name);
3996 : :
3997 : 44 : if (!check_sym_import_status (der, st, e, NULL, ns))
3998 : : return false;
3999 : : }
4000 : :
4001 : : /* Check the declared derived types of component references. */
4002 : 724 : for (ref = e->ref; ref; ref = ref->next)
4003 : 20 : if (ref->type == REF_COMPONENT)
4004 : : {
4005 : 19 : gfc_component *c = ref->u.c.component;
4006 : 19 : if (c->ts.type == BT_DERIVED)
4007 : : {
4008 : 7 : der = c->ts.u.derived;
4009 : 7 : st = gfc_find_symtree (ns->sym_root, der->name);
4010 : 7 : if (!check_sym_import_status (der, st, e, NULL, ns))
4011 : : return false;
4012 : : }
4013 : 12 : else if (c->ts.type == BT_CLASS && !UNLIMITED_POLY (c))
4014 : : {
4015 : 0 : der = CLASS_DATA (c) ? CLASS_DATA (c)->ts.u.derived
4016 : : : c->ts.u.derived;
4017 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4018 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4019 : : return false;
4020 : : }
4021 : : }
4022 : :
4023 : : break;
4024 : :
4025 : 8 : case EXPR_ARRAY:
4026 : 8 : case EXPR_STRUCTURE:
4027 : : /* Check the declared derived type. */
4028 : 8 : if (e->ts.type == BT_DERIVED)
4029 : : {
4030 : 8 : der = e->ts.u.derived;
4031 : 8 : st = gfc_find_symtree (ns->sym_root, der->name);
4032 : :
4033 : 8 : if (!check_sym_import_status (der, st, e, NULL, ns))
4034 : : return false;
4035 : : }
4036 : 0 : else if (e->ts.type == BT_CLASS && !UNLIMITED_POLY (e))
4037 : : {
4038 : 0 : der = CLASS_DATA (e) ? CLASS_DATA (e)->ts.u.derived
4039 : : : e->ts.u.derived;
4040 : 0 : st = gfc_find_symtree (ns->sym_root, der->name);
4041 : :
4042 : 0 : if (!check_sym_import_status (der, st, e, NULL, ns))
4043 : : return false;
4044 : : }
4045 : :
4046 : : break;
4047 : :
4048 : : /* Either not applicable or resolved away
4049 : : case EXPR_OP:
4050 : : case EXPR_UNKNOWN:
4051 : : case EXPR_CONSTANT:
4052 : : case EXPR_NULL:
4053 : : case EXPR_COMPCALL:
4054 : : case EXPR_PPC: */
4055 : :
4056 : : default:
4057 : : break;
4058 : : }
4059 : :
4060 : : return true;
4061 : : }
4062 : :
4063 : :
4064 : : /* Resolve a subroutine call. Although it was tempting to use the same code
4065 : : for functions, subroutines and functions are stored differently and this
4066 : : makes things awkward. */
4067 : :
4068 : :
4069 : : static bool
4070 : 78970 : resolve_call (gfc_code *c)
4071 : : {
4072 : 78970 : bool t;
4073 : 78970 : procedure_type ptype = PROC_INTRINSIC;
4074 : 78970 : gfc_symbol *csym, *sym;
4075 : 78970 : bool no_formal_args;
4076 : :
4077 : 78970 : csym = c->symtree ? c->symtree->n.sym : NULL;
4078 : :
4079 : 78970 : if (csym && csym->ts.type != BT_UNKNOWN)
4080 : : {
4081 : 4 : gfc_error ("%qs at %L has a type, which is not consistent with "
4082 : : "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
4083 : 4 : return false;
4084 : : }
4085 : :
4086 : 78966 : if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
4087 : : {
4088 : 16706 : gfc_symtree *st;
4089 : 16706 : gfc_find_sym_tree (c->symtree->name, gfc_current_ns, 1, &st);
4090 : 16706 : sym = st ? st->n.sym : NULL;
4091 : 16706 : if (sym && csym != sym
4092 : 3 : && sym->ns == gfc_current_ns
4093 : 3 : && sym->attr.flavor == FL_PROCEDURE
4094 : 3 : && sym->attr.contained)
4095 : : {
4096 : 3 : sym->refs++;
4097 : 3 : if (csym->attr.generic)
4098 : 2 : c->symtree->n.sym = sym;
4099 : : else
4100 : 1 : c->symtree = st;
4101 : 3 : csym = c->symtree->n.sym;
4102 : : }
4103 : : }
4104 : :
4105 : : /* If this ia a deferred TBP, c->expr1 will be set. */
4106 : 78966 : if (!c->expr1 && csym)
4107 : : {
4108 : 77306 : if (csym->attr.abstract)
4109 : : {
4110 : 1 : gfc_error ("ABSTRACT INTERFACE %qs must not be referenced at %L",
4111 : : csym->name, &c->loc);
4112 : 1 : return false;
4113 : : }
4114 : :
4115 : : /* Subroutines without the RECURSIVE attribution are not allowed to
4116 : : call themselves. */
4117 : 77305 : if (is_illegal_recursion (csym, gfc_current_ns))
4118 : : {
4119 : 4 : if (csym->attr.entry && csym->ns->entries)
4120 : 2 : gfc_error ("ENTRY %qs at %L cannot be called recursively, "
4121 : : "as subroutine %qs is not RECURSIVE",
4122 : 2 : csym->name, &c->loc, csym->ns->entries->sym->name);
4123 : : else
4124 : 2 : gfc_error ("SUBROUTINE %qs at %L cannot be called recursively, "
4125 : : "as it is not RECURSIVE", csym->name, &c->loc);
4126 : :
4127 : 78965 : t = false;
4128 : : }
4129 : : }
4130 : :
4131 : : /* Switch off assumed size checking and do this again for certain kinds
4132 : : of procedure, once the procedure itself is resolved. */
4133 : 78965 : need_full_assumed_size++;
4134 : :
4135 : 78965 : if (csym)
4136 : 78965 : ptype = csym->attr.proc;
4137 : :
4138 : 78965 : no_formal_args = csym && is_external_proc (csym)
4139 : 15509 : && gfc_sym_get_dummy_args (csym) == NULL;
4140 : 78965 : if (!resolve_actual_arglist (c->ext.actual, ptype, no_formal_args))
4141 : : return false;
4142 : :
4143 : : /* Resume assumed_size checking. */
4144 : 78932 : need_full_assumed_size--;
4145 : :
4146 : : /* If external, check for usage. */
4147 : 78932 : if (csym && is_external_proc (csym))
4148 : 15504 : resolve_global_procedure (csym, &c->loc, 1);
4149 : :
4150 : : /* If we have an external dummy argument, we want to write out its arguments
4151 : : with -fc-prototypes-external. Code like
4152 : :
4153 : : subroutine foo(a,n)
4154 : : external a
4155 : : if (n == 1) call a(1)
4156 : : if (n == 2) call a(2,3)
4157 : : end subroutine foo
4158 : :
4159 : : is actually legal Fortran, but it is not possible to generate a C23-
4160 : : compliant prototype for this, so we just record the fact here and
4161 : : handle that during -fc-prototypes-external processing. */
4162 : :
4163 : 78932 : if (warn_external_argument_mismatch && csym && csym->attr.dummy
4164 : 14 : && csym->attr.external)
4165 : : {
4166 : 14 : if (csym->formal)
4167 : : {
4168 : 6 : bool conflict;
4169 : 6 : conflict = !gfc_compare_actual_formal (&c->ext.actual, csym->formal,
4170 : : 0, 0, 0, NULL);
4171 : 6 : if (conflict)
4172 : : {
4173 : 6 : csym->ext_dummy_arglist_mismatch = 1;
4174 : 6 : gfc_warning (OPT_Wexternal_argument_mismatch,
4175 : : "Different argument lists in external dummy "
4176 : : "subroutine %s at %L and %L", csym->name,
4177 : : &c->loc, &csym->formal_at);
4178 : : }
4179 : : }
4180 : 8 : else if (!csym->formal_resolved)
4181 : : {
4182 : 7 : gfc_get_formal_from_actual_arglist (csym, c->ext.actual);
4183 : 7 : csym->formal_at = c->loc;
4184 : : }
4185 : : }
4186 : :
4187 : 78932 : t = true;
4188 : 78932 : if (c->resolved_sym == NULL)
4189 : : {
4190 : 78881 : c->resolved_isym = NULL;
4191 : 78881 : switch (procedure_kind (csym))
4192 : : {
4193 : 2183 : case PTYPE_GENERIC:
4194 : 2183 : t = resolve_generic_s (c);
4195 : 2183 : break;
4196 : :
4197 : 61345 : case PTYPE_SPECIFIC:
4198 : 61345 : t = resolve_specific_s (c);
4199 : 61345 : break;
4200 : :
4201 : 15353 : case PTYPE_UNKNOWN:
4202 : 15353 : t = resolve_unknown_s (c);
4203 : 15353 : break;
4204 : :
4205 : : default:
4206 : : gfc_internal_error ("resolve_subroutine(): bad function type");
4207 : : }
4208 : : }
4209 : :
4210 : : /* Some checks of elemental subroutine actual arguments. */
4211 : 78931 : if (!resolve_elemental_actual (NULL, c))
4212 : : return false;
4213 : :
4214 : 78923 : if (!c->expr1)
4215 : 77263 : update_current_proc_array_outer_dependency (csym);
4216 : : else
4217 : : /* Typebound procedure: Assume the worst. */
4218 : 1660 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
4219 : :
4220 : 78923 : if (c->resolved_sym
4221 : 78616 : && c->resolved_sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED))
4222 : 2 : gfc_warning (OPT_Wdeprecated_declarations,
4223 : : "Using subroutine %qs at %L is deprecated",
4224 : : c->resolved_sym->name, &c->loc);
4225 : :
4226 : 78923 : csym = c->resolved_sym ? c->resolved_sym : csym;
4227 : 78923 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET && !c->resolved_isym
4228 : 2 : && csym != gfc_current_ns->proc_name)
4229 : 1 : return check_sym_import_status (csym, c->symtree, NULL, c, gfc_current_ns);
4230 : :
4231 : : return t;
4232 : : }
4233 : :
4234 : :
4235 : : /* Compare the shapes of two arrays that have non-NULL shapes. If both
4236 : : op1->shape and op2->shape are non-NULL return true if their shapes
4237 : : match. If both op1->shape and op2->shape are non-NULL return false
4238 : : if their shapes do not match. If either op1->shape or op2->shape is
4239 : : NULL, return true. */
4240 : :
4241 : : static bool
4242 : 31431 : compare_shapes (gfc_expr *op1, gfc_expr *op2)
4243 : : {
4244 : 31431 : bool t;
4245 : 31431 : int i;
4246 : :
4247 : 31431 : t = true;
4248 : :
4249 : 31431 : if (op1->shape != NULL && op2->shape != NULL)
4250 : : {
4251 : 42416 : for (i = 0; i < op1->rank; i++)
4252 : : {
4253 : 22632 : if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
4254 : : {
4255 : 3 : gfc_error ("Shapes for operands at %L and %L are not conformable",
4256 : : &op1->where, &op2->where);
4257 : 3 : t = false;
4258 : 3 : break;
4259 : : }
4260 : : }
4261 : : }
4262 : :
4263 : 31431 : return t;
4264 : : }
4265 : :
4266 : : /* Convert a logical operator to the corresponding bitwise intrinsic call.
4267 : : For example A .AND. B becomes IAND(A, B). */
4268 : : static gfc_expr *
4269 : 668 : logical_to_bitwise (gfc_expr *e)
4270 : : {
4271 : 668 : gfc_expr *tmp, *op1, *op2;
4272 : 668 : gfc_isym_id isym;
4273 : 668 : gfc_actual_arglist *args = NULL;
4274 : :
4275 : 668 : gcc_assert (e->expr_type == EXPR_OP);
4276 : :
4277 : 668 : isym = GFC_ISYM_NONE;
4278 : 668 : op1 = e->value.op.op1;
4279 : 668 : op2 = e->value.op.op2;
4280 : :
4281 : 668 : switch (e->value.op.op)
4282 : : {
4283 : : case INTRINSIC_NOT:
4284 : : isym = GFC_ISYM_NOT;
4285 : : break;
4286 : 126 : case INTRINSIC_AND:
4287 : 126 : isym = GFC_ISYM_IAND;
4288 : 126 : break;
4289 : 127 : case INTRINSIC_OR:
4290 : 127 : isym = GFC_ISYM_IOR;
4291 : 127 : break;
4292 : 270 : case INTRINSIC_NEQV:
4293 : 270 : isym = GFC_ISYM_IEOR;
4294 : 270 : break;
4295 : 126 : case INTRINSIC_EQV:
4296 : : /* "Bitwise eqv" is just the complement of NEQV === IEOR.
4297 : : Change the old expression to NEQV, which will get replaced by IEOR,
4298 : : and wrap it in NOT. */
4299 : 126 : tmp = gfc_copy_expr (e);
4300 : 126 : tmp->value.op.op = INTRINSIC_NEQV;
4301 : 126 : tmp = logical_to_bitwise (tmp);
4302 : 126 : isym = GFC_ISYM_NOT;
4303 : 126 : op1 = tmp;
4304 : 126 : op2 = NULL;
4305 : 126 : break;
4306 : 0 : default:
4307 : 0 : gfc_internal_error ("logical_to_bitwise(): Bad intrinsic");
4308 : : }
4309 : :
4310 : : /* Inherit the original operation's operands as arguments. */
4311 : 668 : args = gfc_get_actual_arglist ();
4312 : 668 : args->expr = op1;
4313 : 668 : if (op2)
4314 : : {
4315 : 523 : args->next = gfc_get_actual_arglist ();
4316 : 523 : args->next->expr = op2;
4317 : : }
4318 : :
4319 : : /* Convert the expression to a function call. */
4320 : 668 : e->expr_type = EXPR_FUNCTION;
4321 : 668 : e->value.function.actual = args;
4322 : 668 : e->value.function.isym = gfc_intrinsic_function_by_id (isym);
4323 : 668 : e->value.function.name = e->value.function.isym->name;
4324 : 668 : e->value.function.esym = NULL;
4325 : :
4326 : : /* Make up a pre-resolved function call symtree if we need to. */
4327 : 668 : if (!e->symtree || !e->symtree->n.sym)
4328 : : {
4329 : 668 : gfc_symbol *sym;
4330 : 668 : gfc_get_ha_sym_tree (e->value.function.isym->name, &e->symtree);
4331 : 668 : sym = e->symtree->n.sym;
4332 : 668 : sym->result = sym;
4333 : 668 : sym->attr.flavor = FL_PROCEDURE;
4334 : 668 : sym->attr.function = 1;
4335 : 668 : sym->attr.elemental = 1;
4336 : 668 : sym->attr.pure = 1;
4337 : 668 : sym->attr.referenced = 1;
4338 : 668 : gfc_intrinsic_symbol (sym);
4339 : 668 : gfc_commit_symbol (sym);
4340 : : }
4341 : :
4342 : 668 : args->name = e->value.function.isym->formal->name;
4343 : 668 : if (e->value.function.isym->formal->next)
4344 : 523 : args->next->name = e->value.function.isym->formal->next->name;
4345 : :
4346 : 668 : return e;
4347 : : }
4348 : :
4349 : : /* Recursively append candidate UOP to CANDIDATES. Store the number of
4350 : : candidates in CANDIDATES_LEN. */
4351 : : static void
4352 : 43 : lookup_uop_fuzzy_find_candidates (gfc_symtree *uop,
4353 : : char **&candidates,
4354 : : size_t &candidates_len)
4355 : : {
4356 : 45 : gfc_symtree *p;
4357 : :
4358 : 45 : if (uop == NULL)
4359 : : return;
4360 : :
4361 : : /* Not sure how to properly filter here. Use all for a start.
4362 : : n.uop.op is NULL for empty interface operators (is that legal?) disregard
4363 : : these as i suppose they don't make terribly sense. */
4364 : :
4365 : 45 : if (uop->n.uop->op != NULL)
4366 : 2 : vec_push (candidates, candidates_len, uop->name);
4367 : :
4368 : 45 : p = uop->left;
4369 : 45 : if (p)
4370 : 0 : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4371 : :
4372 : 45 : p = uop->right;
4373 : 45 : if (p)
4374 : : lookup_uop_fuzzy_find_candidates (p, candidates, candidates_len);
4375 : : }
4376 : :
4377 : : /* Lookup user-operator OP fuzzily, taking names in UOP into account. */
4378 : :
4379 : : static const char*
4380 : 43 : lookup_uop_fuzzy (const char *op, gfc_symtree *uop)
4381 : : {
4382 : 43 : char **candidates = NULL;
4383 : 43 : size_t candidates_len = 0;
4384 : 43 : lookup_uop_fuzzy_find_candidates (uop, candidates, candidates_len);
4385 : 43 : return gfc_closest_fuzzy_match (op, candidates);
4386 : : }
4387 : :
4388 : :
4389 : : /* Callback finding an impure function as an operand to an .and. or
4390 : : .or. expression. Remember the last function warned about to
4391 : : avoid double warnings when recursing. */
4392 : :
4393 : : static int
4394 : 190859 : impure_function_callback (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED,
4395 : : void *data)
4396 : : {
4397 : 190859 : gfc_expr *f = *e;
4398 : 190859 : const char *name;
4399 : 190859 : static gfc_expr *last = NULL;
4400 : 190859 : bool *found = (bool *) data;
4401 : :
4402 : 190859 : if (f->expr_type == EXPR_FUNCTION)
4403 : : {
4404 : 11707 : *found = 1;
4405 : 11707 : if (f != last && !gfc_pure_function (f, &name)
4406 : 12982 : && !gfc_implicit_pure_function (f))
4407 : : {
4408 : 1136 : if (name)
4409 : 1136 : gfc_warning (OPT_Wfunction_elimination,
4410 : : "Impure function %qs at %L might not be evaluated",
4411 : : name, &f->where);
4412 : : else
4413 : 0 : gfc_warning (OPT_Wfunction_elimination,
4414 : : "Impure function at %L might not be evaluated",
4415 : : &f->where);
4416 : : }
4417 : 11707 : last = f;
4418 : : }
4419 : :
4420 : 190859 : return 0;
4421 : : }
4422 : :
4423 : : /* Return true if TYPE is character based, false otherwise. */
4424 : :
4425 : : static int
4426 : 1373 : is_character_based (bt type)
4427 : : {
4428 : 1373 : return type == BT_CHARACTER || type == BT_HOLLERITH;
4429 : : }
4430 : :
4431 : :
4432 : : /* If expression is a hollerith, convert it to character and issue a warning
4433 : : for the conversion. */
4434 : :
4435 : : static void
4436 : 408 : convert_hollerith_to_character (gfc_expr *e)
4437 : : {
4438 : 408 : if (e->ts.type == BT_HOLLERITH)
4439 : : {
4440 : 108 : gfc_typespec t;
4441 : 108 : gfc_clear_ts (&t);
4442 : 108 : t.type = BT_CHARACTER;
4443 : 108 : t.kind = e->ts.kind;
4444 : 108 : gfc_convert_type_warn (e, &t, 2, 1);
4445 : : }
4446 : 408 : }
4447 : :
4448 : : /* Convert to numeric and issue a warning for the conversion. */
4449 : :
4450 : : static void
4451 : 240 : convert_to_numeric (gfc_expr *a, gfc_expr *b)
4452 : : {
4453 : 240 : gfc_typespec t;
4454 : 240 : gfc_clear_ts (&t);
4455 : 240 : t.type = b->ts.type;
4456 : 240 : t.kind = b->ts.kind;
4457 : 240 : gfc_convert_type_warn (a, &t, 2, 1);
4458 : 240 : }
4459 : :
4460 : : /* Resolve an operator expression node. This can involve replacing the
4461 : : operation with a user defined function call. CHECK_INTERFACES is a
4462 : : helper macro. */
4463 : :
4464 : : #define CHECK_INTERFACES \
4465 : : { \
4466 : : match m = gfc_extend_expr (e); \
4467 : : if (m == MATCH_YES) \
4468 : : return true; \
4469 : : if (m == MATCH_ERROR) \
4470 : : return false; \
4471 : : }
4472 : :
4473 : : static bool
4474 : 517508 : resolve_operator (gfc_expr *e)
4475 : : {
4476 : 517508 : gfc_expr *op1, *op2;
4477 : : /* One error uses 3 names; additional space for wording (also via gettext). */
4478 : 517508 : bool t = true;
4479 : :
4480 : : /* Reduce stacked parentheses to single pair */
4481 : 517508 : while (e->expr_type == EXPR_OP
4482 : 517618 : && e->value.op.op == INTRINSIC_PARENTHESES
4483 : 20479 : && e->value.op.op1->expr_type == EXPR_OP
4484 : 533821 : && e->value.op.op1->value.op.op == INTRINSIC_PARENTHESES)
4485 : : {
4486 : 110 : gfc_expr *tmp = gfc_copy_expr (e->value.op.op1);
4487 : 110 : gfc_replace_expr (e, tmp);
4488 : : }
4489 : :
4490 : : /* Resolve all subnodes-- give them types. */
4491 : :
4492 : 517508 : switch (e->value.op.op)
4493 : : {
4494 : 468960 : default:
4495 : 468960 : if (!gfc_resolve_expr (e->value.op.op2))
4496 : 517508 : t = false;
4497 : :
4498 : : /* Fall through. */
4499 : :
4500 : 517508 : case INTRINSIC_NOT:
4501 : 517508 : case INTRINSIC_UPLUS:
4502 : 517508 : case INTRINSIC_UMINUS:
4503 : 517508 : case INTRINSIC_PARENTHESES:
4504 : 517508 : if (!gfc_resolve_expr (e->value.op.op1))
4505 : : return false;
4506 : 517340 : if (e->value.op.op1
4507 : 517331 : && e->value.op.op1->ts.type == BT_BOZ && !e->value.op.op2)
4508 : : {
4509 : 0 : gfc_error ("BOZ literal constant at %L cannot be an operand of "
4510 : 0 : "unary operator %qs", &e->value.op.op1->where,
4511 : : gfc_op2string (e->value.op.op));
4512 : 0 : return false;
4513 : : }
4514 : 517340 : if (flag_unsigned && pedantic && e->ts.type == BT_UNSIGNED
4515 : 6 : && e->value.op.op == INTRINSIC_UMINUS)
4516 : : {
4517 : 2 : gfc_error ("Negation of unsigned expression at %L not permitted ",
4518 : : &e->value.op.op1->where);
4519 : 2 : return false;
4520 : : }
4521 : 517338 : break;
4522 : : }
4523 : :
4524 : : /* Typecheck the new node. */
4525 : :
4526 : 517338 : op1 = e->value.op.op1;
4527 : 517338 : op2 = e->value.op.op2;
4528 : 517338 : if (op1 == NULL && op2 == NULL)
4529 : : return false;
4530 : : /* Error out if op2 did not resolve. We already diagnosed op1. */
4531 : 517329 : if (t == false)
4532 : : return false;
4533 : :
4534 : : /* op1 and op2 cannot both be BOZ. */
4535 : 517263 : if (op1 && op1->ts.type == BT_BOZ
4536 : 0 : && op2 && op2->ts.type == BT_BOZ)
4537 : : {
4538 : 0 : gfc_error ("Operands at %L and %L cannot appear as operands of "
4539 : 0 : "binary operator %qs", &op1->where, &op2->where,
4540 : : gfc_op2string (e->value.op.op));
4541 : 0 : return false;
4542 : : }
4543 : :
4544 : 517263 : if ((op1 && op1->expr_type == EXPR_NULL)
4545 : 517261 : || (op2 && op2->expr_type == EXPR_NULL))
4546 : : {
4547 : 3 : CHECK_INTERFACES
4548 : 3 : gfc_error ("Invalid context for NULL() pointer at %L", &e->where);
4549 : 3 : return false;
4550 : : }
4551 : :
4552 : 517260 : switch (e->value.op.op)
4553 : : {
4554 : 8009 : case INTRINSIC_UPLUS:
4555 : 8009 : case INTRINSIC_UMINUS:
4556 : 8009 : if (op1->ts.type == BT_INTEGER
4557 : : || op1->ts.type == BT_REAL
4558 : : || op1->ts.type == BT_COMPLEX
4559 : : || op1->ts.type == BT_UNSIGNED)
4560 : : {
4561 : 7940 : e->ts = op1->ts;
4562 : 7940 : break;
4563 : : }
4564 : :
4565 : 69 : CHECK_INTERFACES
4566 : 43 : gfc_error ("Operand of unary numeric operator %qs at %L is %s",
4567 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (e));
4568 : 43 : return false;
4569 : :
4570 : 150849 : case INTRINSIC_POWER:
4571 : 150849 : case INTRINSIC_PLUS:
4572 : 150849 : case INTRINSIC_MINUS:
4573 : 150849 : case INTRINSIC_TIMES:
4574 : 150849 : case INTRINSIC_DIVIDE:
4575 : :
4576 : : /* UNSIGNED cannot appear in a mixed expression without explicit
4577 : : conversion. */
4578 : 150849 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4579 : : {
4580 : 3 : CHECK_INTERFACES
4581 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are "
4582 : : "%s/%s", gfc_op2string (e->value.op.op), &e->where,
4583 : : gfc_typename (op1), gfc_typename (op2));
4584 : 3 : return false;
4585 : : }
4586 : :
4587 : 150846 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4588 : : {
4589 : : /* Do not perform conversions if operands are not conformable as
4590 : : required for the binary intrinsic operators (F2018:10.1.5).
4591 : : Defer to a possibly overloading user-defined operator. */
4592 : 150402 : if (!gfc_op_rank_conformable (op1, op2))
4593 : : {
4594 : 36 : CHECK_INTERFACES
4595 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4596 : 0 : &op1->where, &op2->where);
4597 : 0 : return false;
4598 : : }
4599 : :
4600 : 150366 : gfc_type_convert_binary (e, 1);
4601 : 150366 : break;
4602 : : }
4603 : :
4604 : 444 : if (op1->ts.type == BT_DERIVED || op2->ts.type == BT_DERIVED)
4605 : : {
4606 : 215 : CHECK_INTERFACES
4607 : 2 : gfc_error ("Unexpected derived-type entities in binary intrinsic "
4608 : : "numeric operator %qs at %L",
4609 : : gfc_op2string (e->value.op.op), &e->where);
4610 : 2 : return false;
4611 : : }
4612 : : else
4613 : : {
4614 : 229 : CHECK_INTERFACES
4615 : 3 : gfc_error ("Operands of binary numeric operator %qs at %L are %s/%s",
4616 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4617 : : gfc_typename (op2));
4618 : 3 : return false;
4619 : : }
4620 : :
4621 : 2118 : case INTRINSIC_CONCAT:
4622 : 2118 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4623 : 2093 : && op1->ts.kind == op2->ts.kind)
4624 : : {
4625 : 2084 : e->ts.type = BT_CHARACTER;
4626 : 2084 : e->ts.kind = op1->ts.kind;
4627 : 2084 : break;
4628 : : }
4629 : :
4630 : 34 : CHECK_INTERFACES
4631 : 10 : gfc_error ("Operands of string concatenation operator at %L are %s/%s",
4632 : : &e->where, gfc_typename (op1), gfc_typename (op2));
4633 : 10 : return false;
4634 : :
4635 : 68998 : case INTRINSIC_AND:
4636 : 68998 : case INTRINSIC_OR:
4637 : 68998 : case INTRINSIC_EQV:
4638 : 68998 : case INTRINSIC_NEQV:
4639 : 68998 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4640 : : {
4641 : 68447 : e->ts.type = BT_LOGICAL;
4642 : 68447 : e->ts.kind = gfc_kind_max (op1, op2);
4643 : 68447 : if (op1->ts.kind < e->ts.kind)
4644 : 138 : gfc_convert_type (op1, &e->ts, 2);
4645 : 68309 : else if (op2->ts.kind < e->ts.kind)
4646 : 117 : gfc_convert_type (op2, &e->ts, 2);
4647 : :
4648 : 68447 : if (flag_frontend_optimize &&
4649 : 57436 : (e->value.op.op == INTRINSIC_AND || e->value.op.op == INTRINSIC_OR))
4650 : : {
4651 : : /* Warn about short-circuiting
4652 : : with impure function as second operand. */
4653 : 51470 : bool op2_f = false;
4654 : 51470 : gfc_expr_walker (&op2, impure_function_callback, &op2_f);
4655 : : }
4656 : : break;
4657 : : }
4658 : :
4659 : : /* Logical ops on integers become bitwise ops with -fdec. */
4660 : 551 : else if (flag_dec
4661 : 523 : && (op1->ts.type == BT_INTEGER || op2->ts.type == BT_INTEGER))
4662 : : {
4663 : 523 : e->ts.type = BT_INTEGER;
4664 : 523 : e->ts.kind = gfc_kind_max (op1, op2);
4665 : 523 : if (op1->ts.type != e->ts.type || op1->ts.kind != e->ts.kind)
4666 : 289 : gfc_convert_type (op1, &e->ts, 1);
4667 : 523 : if (op2->ts.type != e->ts.type || op2->ts.kind != e->ts.kind)
4668 : 144 : gfc_convert_type (op2, &e->ts, 1);
4669 : 523 : e = logical_to_bitwise (e);
4670 : 523 : goto simplify_op;
4671 : : }
4672 : :
4673 : 28 : CHECK_INTERFACES
4674 : 16 : gfc_error ("Operands of logical operator %qs at %L are %s/%s",
4675 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4676 : : gfc_typename (op2));
4677 : 16 : return false;
4678 : :
4679 : 20154 : case INTRINSIC_NOT:
4680 : : /* Logical ops on integers become bitwise ops with -fdec. */
4681 : 20154 : if (flag_dec && op1->ts.type == BT_INTEGER)
4682 : : {
4683 : 19 : e->ts.type = BT_INTEGER;
4684 : 19 : e->ts.kind = op1->ts.kind;
4685 : 19 : e = logical_to_bitwise (e);
4686 : 19 : goto simplify_op;
4687 : : }
4688 : :
4689 : 20135 : if (op1->ts.type == BT_LOGICAL)
4690 : : {
4691 : 20129 : e->ts.type = BT_LOGICAL;
4692 : 20129 : e->ts.kind = op1->ts.kind;
4693 : 20129 : break;
4694 : : }
4695 : :
4696 : 6 : CHECK_INTERFACES
4697 : 3 : gfc_error ("Operand of .not. operator at %L is %s", &e->where,
4698 : : gfc_typename (op1));
4699 : 3 : return false;
4700 : :
4701 : 20996 : case INTRINSIC_GT:
4702 : 20996 : case INTRINSIC_GT_OS:
4703 : 20996 : case INTRINSIC_GE:
4704 : 20996 : case INTRINSIC_GE_OS:
4705 : 20996 : case INTRINSIC_LT:
4706 : 20996 : case INTRINSIC_LT_OS:
4707 : 20996 : case INTRINSIC_LE:
4708 : 20996 : case INTRINSIC_LE_OS:
4709 : 20996 : if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
4710 : : {
4711 : 18 : CHECK_INTERFACES
4712 : 0 : gfc_error ("COMPLEX quantities cannot be compared at %L", &e->where);
4713 : 0 : return false;
4714 : : }
4715 : :
4716 : : /* Fall through. */
4717 : :
4718 : 246559 : case INTRINSIC_EQ:
4719 : 246559 : case INTRINSIC_EQ_OS:
4720 : 246559 : case INTRINSIC_NE:
4721 : 246559 : case INTRINSIC_NE_OS:
4722 : :
4723 : 246559 : if (flag_dec
4724 : 1038 : && is_character_based (op1->ts.type)
4725 : 246894 : && is_character_based (op2->ts.type))
4726 : : {
4727 : 204 : convert_hollerith_to_character (op1);
4728 : 204 : convert_hollerith_to_character (op2);
4729 : : }
4730 : :
4731 : 246559 : if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
4732 : 36410 : && op1->ts.kind == op2->ts.kind)
4733 : : {
4734 : 36373 : e->ts.type = BT_LOGICAL;
4735 : 36373 : e->ts.kind = gfc_default_logical_kind;
4736 : 36373 : break;
4737 : : }
4738 : :
4739 : : /* If op1 is BOZ, then op2 is not!. Try to convert to type of op2. */
4740 : 210186 : if (op1->ts.type == BT_BOZ)
4741 : : {
4742 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear "
4743 : : "as an operand of a relational operator"),
4744 : : &op1->where))
4745 : : return false;
4746 : :
4747 : 0 : if (op2->ts.type == BT_INTEGER && !gfc_boz2int (op1, op2->ts.kind))
4748 : : return false;
4749 : :
4750 : 0 : if (op2->ts.type == BT_REAL && !gfc_boz2real (op1, op2->ts.kind))
4751 : : return false;
4752 : : }
4753 : :
4754 : : /* If op2 is BOZ, then op1 is not!. Try to convert to type of op2. */
4755 : 210186 : if (op2->ts.type == BT_BOZ)
4756 : : {
4757 : 0 : if (gfc_invalid_boz (G_("BOZ literal constant near %L cannot appear"
4758 : : " as an operand of a relational operator"),
4759 : : &op2->where))
4760 : : return false;
4761 : :
4762 : 0 : if (op1->ts.type == BT_INTEGER && !gfc_boz2int (op2, op1->ts.kind))
4763 : : return false;
4764 : :
4765 : 0 : if (op1->ts.type == BT_REAL && !gfc_boz2real (op2, op1->ts.kind))
4766 : : return false;
4767 : : }
4768 : 210186 : if (flag_dec
4769 : 210186 : && op1->ts.type == BT_HOLLERITH && gfc_numeric_ts (&op2->ts))
4770 : 120 : convert_to_numeric (op1, op2);
4771 : :
4772 : 210186 : if (flag_dec
4773 : 210186 : && gfc_numeric_ts (&op1->ts) && op2->ts.type == BT_HOLLERITH)
4774 : 120 : convert_to_numeric (op2, op1);
4775 : :
4776 : 210186 : if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
4777 : : {
4778 : : /* Do not perform conversions if operands are not conformable as
4779 : : required for the binary intrinsic operators (F2018:10.1.5).
4780 : : Defer to a possibly overloading user-defined operator. */
4781 : 209061 : if (!gfc_op_rank_conformable (op1, op2))
4782 : : {
4783 : 70 : CHECK_INTERFACES
4784 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4785 : 0 : &op1->where, &op2->where);
4786 : 0 : return false;
4787 : : }
4788 : :
4789 : 208991 : if (flag_unsigned && gfc_invalid_unsigned_ops (op1, op2))
4790 : : {
4791 : 1 : CHECK_INTERFACES
4792 : 1 : gfc_error ("Inconsistent types for operator at %L and %L: "
4793 : 1 : "%s and %s", &op1->where, &op2->where,
4794 : : gfc_typename (op1), gfc_typename (op2));
4795 : 1 : return false;
4796 : : }
4797 : :
4798 : 208990 : gfc_type_convert_binary (e, 1);
4799 : :
4800 : 208990 : e->ts.type = BT_LOGICAL;
4801 : 208990 : e->ts.kind = gfc_default_logical_kind;
4802 : :
4803 : 208990 : if (warn_compare_reals)
4804 : : {
4805 : 69 : gfc_intrinsic_op op = e->value.op.op;
4806 : :
4807 : : /* Type conversion has made sure that the types of op1 and op2
4808 : : agree, so it is only necessary to check the first one. */
4809 : 69 : if ((op1->ts.type == BT_REAL || op1->ts.type == BT_COMPLEX)
4810 : 13 : && (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS
4811 : 6 : || op == INTRINSIC_NE || op == INTRINSIC_NE_OS))
4812 : : {
4813 : 13 : const char *msg;
4814 : :
4815 : 13 : if (op == INTRINSIC_EQ || op == INTRINSIC_EQ_OS)
4816 : : msg = G_("Equality comparison for %s at %L");
4817 : : else
4818 : 6 : msg = G_("Inequality comparison for %s at %L");
4819 : :
4820 : 13 : gfc_warning (OPT_Wcompare_reals, msg,
4821 : : gfc_typename (op1), &op1->where);
4822 : : }
4823 : : }
4824 : :
4825 : : break;
4826 : : }
4827 : :
4828 : 1125 : if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
4829 : : {
4830 : 2 : CHECK_INTERFACES
4831 : 4 : gfc_error ("Logicals at %L must be compared with %s instead of %s",
4832 : : &e->where,
4833 : 2 : (e->value.op.op == INTRINSIC_EQ || e->value.op.op == INTRINSIC_EQ_OS)
4834 : : ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
4835 : 2 : }
4836 : : else
4837 : : {
4838 : 1123 : CHECK_INTERFACES
4839 : 113 : gfc_error ("Operands of comparison operator %qs at %L are %s/%s",
4840 : : gfc_op2string (e->value.op.op), &e->where, gfc_typename (op1),
4841 : : gfc_typename (op2));
4842 : : }
4843 : :
4844 : : return false;
4845 : :
4846 : 232 : case INTRINSIC_USER:
4847 : 232 : if (e->value.op.uop->op == NULL)
4848 : : {
4849 : 43 : const char *name = e->value.op.uop->name;
4850 : 43 : const char *guessed;
4851 : 43 : guessed = lookup_uop_fuzzy (name, e->value.op.uop->ns->uop_root);
4852 : 43 : CHECK_INTERFACES
4853 : 4 : if (guessed)
4854 : 1 : gfc_error ("Unknown operator %qs at %L; did you mean "
4855 : : "%qs?", name, &e->where, guessed);
4856 : : else
4857 : 3 : gfc_error ("Unknown operator %qs at %L", name, &e->where);
4858 : : }
4859 : 189 : else if (op2 == NULL)
4860 : : {
4861 : 48 : CHECK_INTERFACES
4862 : 0 : gfc_error ("Operand of user operator %qs at %L is %s",
4863 : 0 : e->value.op.uop->name, &e->where, gfc_typename (op1));
4864 : : }
4865 : : else
4866 : : {
4867 : 141 : e->value.op.uop->op->sym->attr.referenced = 1;
4868 : 141 : CHECK_INTERFACES
4869 : 5 : gfc_error ("Operands of user operator %qs at %L are %s/%s",
4870 : 5 : e->value.op.uop->name, &e->where, gfc_typename (op1),
4871 : : gfc_typename (op2));
4872 : : }
4873 : :
4874 : : return false;
4875 : :
4876 : 20323 : case INTRINSIC_PARENTHESES:
4877 : 20323 : e->ts = op1->ts;
4878 : 20323 : if (e->ts.type == BT_CHARACTER)
4879 : 301 : e->ts.u.cl = op1->ts.u.cl;
4880 : : break;
4881 : :
4882 : 0 : default:
4883 : 0 : gfc_internal_error ("resolve_operator(): Bad intrinsic");
4884 : : }
4885 : :
4886 : : /* Deal with arrayness of an operand through an operator. */
4887 : :
4888 : 514652 : switch (e->value.op.op)
4889 : : {
4890 : 466260 : case INTRINSIC_PLUS:
4891 : 466260 : case INTRINSIC_MINUS:
4892 : 466260 : case INTRINSIC_TIMES:
4893 : 466260 : case INTRINSIC_DIVIDE:
4894 : 466260 : case INTRINSIC_POWER:
4895 : 466260 : case INTRINSIC_CONCAT:
4896 : 466260 : case INTRINSIC_AND:
4897 : 466260 : case INTRINSIC_OR:
4898 : 466260 : case INTRINSIC_EQV:
4899 : 466260 : case INTRINSIC_NEQV:
4900 : 466260 : case INTRINSIC_EQ:
4901 : 466260 : case INTRINSIC_EQ_OS:
4902 : 466260 : case INTRINSIC_NE:
4903 : 466260 : case INTRINSIC_NE_OS:
4904 : 466260 : case INTRINSIC_GT:
4905 : 466260 : case INTRINSIC_GT_OS:
4906 : 466260 : case INTRINSIC_GE:
4907 : 466260 : case INTRINSIC_GE_OS:
4908 : 466260 : case INTRINSIC_LT:
4909 : 466260 : case INTRINSIC_LT_OS:
4910 : 466260 : case INTRINSIC_LE:
4911 : 466260 : case INTRINSIC_LE_OS:
4912 : :
4913 : 466260 : if (op1->rank == 0 && op2->rank == 0)
4914 : 415709 : e->rank = 0;
4915 : :
4916 : 466260 : if (op1->rank == 0 && op2->rank != 0)
4917 : : {
4918 : 2587 : e->rank = op2->rank;
4919 : :
4920 : 2587 : if (e->shape == NULL)
4921 : 2557 : e->shape = gfc_copy_shape (op2->shape, op2->rank);
4922 : : }
4923 : :
4924 : 466260 : if (op1->rank != 0 && op2->rank == 0)
4925 : : {
4926 : 16472 : e->rank = op1->rank;
4927 : :
4928 : 16472 : if (e->shape == NULL)
4929 : 16454 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4930 : : }
4931 : :
4932 : 466260 : if (op1->rank != 0 && op2->rank != 0)
4933 : : {
4934 : 31492 : if (op1->rank == op2->rank)
4935 : : {
4936 : 31492 : e->rank = op1->rank;
4937 : 31492 : if (e->shape == NULL)
4938 : : {
4939 : 31431 : t = compare_shapes (op1, op2);
4940 : 31431 : if (!t)
4941 : 3 : e->shape = NULL;
4942 : : else
4943 : 31428 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4944 : : }
4945 : : }
4946 : : else
4947 : : {
4948 : : /* Allow higher level expressions to work. */
4949 : 0 : e->rank = 0;
4950 : :
4951 : : /* Try user-defined operators, and otherwise throw an error. */
4952 : 0 : CHECK_INTERFACES
4953 : 0 : gfc_error ("Inconsistent ranks for operator at %L and %L",
4954 : 0 : &op1->where, &op2->where);
4955 : 0 : return false;
4956 : : }
4957 : : }
4958 : : break;
4959 : :
4960 : 48392 : case INTRINSIC_PARENTHESES:
4961 : 48392 : case INTRINSIC_NOT:
4962 : 48392 : case INTRINSIC_UPLUS:
4963 : 48392 : case INTRINSIC_UMINUS:
4964 : : /* Simply copy arrayness attribute */
4965 : 48392 : e->rank = op1->rank;
4966 : 48392 : e->corank = op1->corank;
4967 : :
4968 : 48392 : if (e->shape == NULL)
4969 : 48386 : e->shape = gfc_copy_shape (op1->shape, op1->rank);
4970 : :
4971 : : break;
4972 : :
4973 : : default:
4974 : : break;
4975 : : }
4976 : :
4977 : 515194 : simplify_op:
4978 : :
4979 : : /* Attempt to simplify the expression. */
4980 : 3 : if (t)
4981 : : {
4982 : 515191 : t = gfc_simplify_expr (e, 0);
4983 : : /* Some calls do not succeed in simplification and return false
4984 : : even though there is no error; e.g. variable references to
4985 : : PARAMETER arrays. */
4986 : 515191 : if (!gfc_is_constant_expr (e))
4987 : 472335 : t = true;
4988 : : }
4989 : : return t;
4990 : : }
4991 : :
4992 : :
4993 : : /************** Array resolution subroutines **************/
4994 : :
4995 : : enum compare_result
4996 : : { CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN };
4997 : :
4998 : : /* Compare two integer expressions. */
4999 : :
5000 : : static compare_result
5001 : 445043 : compare_bound (gfc_expr *a, gfc_expr *b)
5002 : : {
5003 : 445043 : int i;
5004 : :
5005 : 445043 : if (a == NULL || a->expr_type != EXPR_CONSTANT
5006 : 291770 : || b == NULL || b->expr_type != EXPR_CONSTANT)
5007 : : return CMP_UNKNOWN;
5008 : :
5009 : : /* If either of the types isn't INTEGER, we must have
5010 : : raised an error earlier. */
5011 : :
5012 : 203996 : if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
5013 : : return CMP_UNKNOWN;
5014 : :
5015 : 203992 : i = mpz_cmp (a->value.integer, b->value.integer);
5016 : :
5017 : 203992 : if (i < 0)
5018 : : return CMP_LT;
5019 : 95822 : if (i > 0)
5020 : 37767 : return CMP_GT;
5021 : : return CMP_EQ;
5022 : : }
5023 : :
5024 : :
5025 : : /* Compare an integer expression with an integer. */
5026 : :
5027 : : static compare_result
5028 : 71729 : compare_bound_int (gfc_expr *a, int b)
5029 : : {
5030 : 71729 : int i;
5031 : :
5032 : 71729 : if (a == NULL
5033 : 30296 : || a->expr_type != EXPR_CONSTANT
5034 : 27503 : || a->ts.type != BT_INTEGER)
5035 : : return CMP_UNKNOWN;
5036 : :
5037 : 27503 : i = mpz_cmp_si (a->value.integer, b);
5038 : :
5039 : 27503 : if (i < 0)
5040 : : return CMP_LT;
5041 : 24220 : if (i > 0)
5042 : 21152 : return CMP_GT;
5043 : : return CMP_EQ;
5044 : : }
5045 : :
5046 : :
5047 : : /* Compare an integer expression with a mpz_t. */
5048 : :
5049 : : static compare_result
5050 : 66749 : compare_bound_mpz_t (gfc_expr *a, mpz_t b)
5051 : : {
5052 : 66749 : int i;
5053 : :
5054 : 66749 : if (a == NULL
5055 : 54829 : || a->expr_type != EXPR_CONSTANT
5056 : 52745 : || a->ts.type != BT_INTEGER)
5057 : : return CMP_UNKNOWN;
5058 : :
5059 : 52742 : i = mpz_cmp (a->value.integer, b);
5060 : :
5061 : 52742 : if (i < 0)
5062 : : return CMP_LT;
5063 : 23966 : if (i > 0)
5064 : 10270 : return CMP_GT;
5065 : : return CMP_EQ;
5066 : : }
5067 : :
5068 : :
5069 : : /* Compute the last value of a sequence given by a triplet.
5070 : : Return 0 if it wasn't able to compute the last value, or if the
5071 : : sequence if empty, and 1 otherwise. */
5072 : :
5073 : : static int
5074 : 50162 : compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
5075 : : gfc_expr *stride, mpz_t last)
5076 : : {
5077 : 50162 : mpz_t rem;
5078 : :
5079 : 50162 : if (start == NULL || start->expr_type != EXPR_CONSTANT
5080 : 35427 : || end == NULL || end->expr_type != EXPR_CONSTANT
5081 : 31038 : || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
5082 : : return 0;
5083 : :
5084 : 30719 : if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
5085 : 30718 : || (stride != NULL && stride->ts.type != BT_INTEGER))
5086 : : return 0;
5087 : :
5088 : 6212 : if (stride == NULL || compare_bound_int (stride, 1) == CMP_EQ)
5089 : : {
5090 : 24632 : if (compare_bound (start, end) == CMP_GT)
5091 : : return 0;
5092 : 23243 : mpz_set (last, end->value.integer);
5093 : 23243 : return 1;
5094 : : }
5095 : :
5096 : 6086 : if (compare_bound_int (stride, 0) == CMP_GT)
5097 : : {
5098 : : /* Stride is positive */
5099 : 5118 : if (mpz_cmp (start->value.integer, end->value.integer) > 0)
5100 : : return 0;
5101 : : }
5102 : : else
5103 : : {
5104 : : /* Stride is negative */
5105 : 968 : if (mpz_cmp (start->value.integer, end->value.integer) < 0)
5106 : : return 0;
5107 : : }
5108 : :
5109 : 6066 : mpz_init (rem);
5110 : 6066 : mpz_sub (rem, end->value.integer, start->value.integer);
5111 : 6066 : mpz_tdiv_r (rem, rem, stride->value.integer);
5112 : 6066 : mpz_sub (last, end->value.integer, rem);
5113 : 6066 : mpz_clear (rem);
5114 : :
5115 : 6066 : return 1;
5116 : : }
5117 : :
5118 : :
5119 : : /* Compare a single dimension of an array reference to the array
5120 : : specification. */
5121 : :
5122 : : static bool
5123 : 204625 : check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
5124 : : {
5125 : 204625 : mpz_t last_value;
5126 : :
5127 : 204625 : if (ar->dimen_type[i] == DIMEN_STAR)
5128 : : {
5129 : 413 : gcc_assert (ar->stride[i] == NULL);
5130 : : /* This implies [*] as [*:] and [*:3] are not possible. */
5131 : 413 : if (ar->start[i] == NULL)
5132 : : {
5133 : 339 : gcc_assert (ar->end[i] == NULL);
5134 : : return true;
5135 : : }
5136 : : }
5137 : :
5138 : : /* Given start, end and stride values, calculate the minimum and
5139 : : maximum referenced indexes. */
5140 : :
5141 : 204286 : switch (ar->dimen_type[i])
5142 : : {
5143 : : case DIMEN_VECTOR:
5144 : : case DIMEN_THIS_IMAGE:
5145 : : break;
5146 : :
5147 : 147760 : case DIMEN_STAR:
5148 : 147760 : case DIMEN_ELEMENT:
5149 : 147760 : if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
5150 : : {
5151 : 2 : if (i < as->rank)
5152 : 2 : gfc_warning (0, "Array reference at %L is out of bounds "
5153 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5154 : 2 : mpz_get_si (ar->start[i]->value.integer),
5155 : 2 : mpz_get_si (as->lower[i]->value.integer), i+1);
5156 : : else
5157 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5158 : : "(%ld < %ld) in codimension %d", &ar->c_where[i],
5159 : 0 : mpz_get_si (ar->start[i]->value.integer),
5160 : 0 : mpz_get_si (as->lower[i]->value.integer),
5161 : 0 : i + 1 - as->rank);
5162 : 2 : return true;
5163 : : }
5164 : 147758 : if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
5165 : : {
5166 : 39 : if (i < as->rank)
5167 : 39 : gfc_warning (0, "Array reference at %L is out of bounds "
5168 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5169 : 39 : mpz_get_si (ar->start[i]->value.integer),
5170 : 39 : mpz_get_si (as->upper[i]->value.integer), i+1);
5171 : : else
5172 : 0 : gfc_warning (0, "Array reference at %L is out of bounds "
5173 : : "(%ld > %ld) in codimension %d", &ar->c_where[i],
5174 : 0 : mpz_get_si (ar->start[i]->value.integer),
5175 : 0 : mpz_get_si (as->upper[i]->value.integer),
5176 : 0 : i + 1 - as->rank);
5177 : 39 : return true;
5178 : : }
5179 : :
5180 : : break;
5181 : :
5182 : 50207 : case DIMEN_RANGE:
5183 : 50207 : {
5184 : : #define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
5185 : : #define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
5186 : :
5187 : 50207 : compare_result comp_start_end = compare_bound (AR_START, AR_END);
5188 : 50207 : compare_result comp_stride_zero = compare_bound_int (ar->stride[i], 0);
5189 : :
5190 : : /* Check for zero stride, which is not allowed. */
5191 : 50207 : if (comp_stride_zero == CMP_EQ)
5192 : : {
5193 : 1 : gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
5194 : 1 : return false;
5195 : : }
5196 : :
5197 : : /* if start == end || (stride > 0 && start < end)
5198 : : || (stride < 0 && start > end),
5199 : : then the array section contains at least one element. In this
5200 : : case, there is an out-of-bounds access if
5201 : : (start < lower || start > upper). */
5202 : 50206 : if (comp_start_end == CMP_EQ
5203 : 49521 : || ((comp_stride_zero == CMP_GT || ar->stride[i] == NULL)
5204 : 47246 : && comp_start_end == CMP_LT)
5205 : 21799 : || (comp_stride_zero == CMP_LT
5206 : 21799 : && comp_start_end == CMP_GT))
5207 : : {
5208 : 29355 : if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
5209 : : {
5210 : 27 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5211 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5212 : 27 : mpz_get_si (AR_START->value.integer),
5213 : 27 : mpz_get_si (as->lower[i]->value.integer), i+1);
5214 : 27 : return true;
5215 : : }
5216 : 29328 : if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
5217 : : {
5218 : 17 : gfc_warning (0, "Lower array reference at %L is out of bounds "
5219 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5220 : 17 : mpz_get_si (AR_START->value.integer),
5221 : 17 : mpz_get_si (as->upper[i]->value.integer), i+1);
5222 : 17 : return true;
5223 : : }
5224 : : }
5225 : :
5226 : : /* If we can compute the highest index of the array section,
5227 : : then it also has to be between lower and upper. */
5228 : 50162 : mpz_init (last_value);
5229 : 50162 : if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
5230 : : last_value))
5231 : : {
5232 : 29309 : if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
5233 : : {
5234 : 3 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5235 : : "(%ld < %ld) in dimension %d", &ar->c_where[i],
5236 : : mpz_get_si (last_value),
5237 : 3 : mpz_get_si (as->lower[i]->value.integer), i+1);
5238 : 3 : mpz_clear (last_value);
5239 : 3 : return true;
5240 : : }
5241 : 29306 : if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
5242 : : {
5243 : 7 : gfc_warning (0, "Upper array reference at %L is out of bounds "
5244 : : "(%ld > %ld) in dimension %d", &ar->c_where[i],
5245 : : mpz_get_si (last_value),
5246 : 7 : mpz_get_si (as->upper[i]->value.integer), i+1);
5247 : 7 : mpz_clear (last_value);
5248 : 7 : return true;
5249 : : }
5250 : : }
5251 : 50152 : mpz_clear (last_value);
5252 : :
5253 : : #undef AR_START
5254 : : #undef AR_END
5255 : : }
5256 : 50152 : break;
5257 : :
5258 : 0 : default:
5259 : 0 : gfc_internal_error ("check_dimension(): Bad array reference");
5260 : : }
5261 : :
5262 : : return true;
5263 : : }
5264 : :
5265 : :
5266 : : /* Compare an array reference with an array specification. */
5267 : :
5268 : : static bool
5269 : 408350 : compare_spec_to_ref (gfc_array_ref *ar)
5270 : : {
5271 : 408350 : gfc_array_spec *as;
5272 : 408350 : int i;
5273 : :
5274 : 408350 : as = ar->as;
5275 : 408350 : i = as->rank - 1;
5276 : : /* TODO: Full array sections are only allowed as actual parameters. */
5277 : 408350 : if (as->type == AS_ASSUMED_SIZE
5278 : 5741 : && (/*ar->type == AR_FULL
5279 : 5741 : ||*/ (ar->type == AR_SECTION
5280 : 514 : && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
5281 : : {
5282 : 5 : gfc_error ("Rightmost upper bound of assumed size array section "
5283 : : "not specified at %L", &ar->where);
5284 : 5 : return false;
5285 : : }
5286 : :
5287 : 408345 : if (ar->type == AR_FULL)
5288 : : return true;
5289 : :
5290 : 154434 : if (as->rank != ar->dimen)
5291 : : {
5292 : 28 : gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
5293 : : &ar->where, ar->dimen, as->rank);
5294 : 28 : return false;
5295 : : }
5296 : :
5297 : : /* ar->codimen == 0 is a local array. */
5298 : 154406 : if (as->corank != ar->codimen && ar->codimen != 0)
5299 : : {
5300 : 0 : gfc_error ("Coindex rank mismatch in array reference at %L (%d/%d)",
5301 : : &ar->where, ar->codimen, as->corank);
5302 : 0 : return false;
5303 : : }
5304 : :
5305 : 351326 : for (i = 0; i < as->rank; i++)
5306 : 196921 : if (!check_dimension (i, ar, as))
5307 : : return false;
5308 : :
5309 : : /* Local access has no coarray spec. */
5310 : 154405 : if (ar->codimen != 0)
5311 : 14745 : for (i = as->rank; i < as->rank + as->corank; i++)
5312 : : {
5313 : 7706 : if (ar->dimen_type[i] != DIMEN_ELEMENT && !ar->in_allocate
5314 : 5268 : && ar->dimen_type[i] != DIMEN_THIS_IMAGE)
5315 : : {
5316 : 2 : gfc_error ("Coindex of codimension %d must be a scalar at %L",
5317 : 2 : i + 1 - as->rank, &ar->where);
5318 : 2 : return false;
5319 : : }
5320 : 7704 : if (!check_dimension (i, ar, as))
5321 : : return false;
5322 : : }
5323 : :
5324 : : return true;
5325 : : }
5326 : :
5327 : :
5328 : : /* Resolve one part of an array index. */
5329 : :
5330 : : static bool
5331 : 695695 : gfc_resolve_index_1 (gfc_expr *index, int check_scalar,
5332 : : int force_index_integer_kind)
5333 : : {
5334 : 695695 : gfc_typespec ts;
5335 : :
5336 : 695695 : if (index == NULL)
5337 : : return true;
5338 : :
5339 : 206319 : if (!gfc_resolve_expr (index))
5340 : : return false;
5341 : :
5342 : 206296 : if (check_scalar && index->rank != 0)
5343 : : {
5344 : 2 : gfc_error ("Array index at %L must be scalar", &index->where);
5345 : 2 : return false;
5346 : : }
5347 : :
5348 : 206294 : if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
5349 : : {
5350 : 3 : gfc_error ("Array index at %L must be of INTEGER type, found %s",
5351 : : &index->where, gfc_basic_typename (index->ts.type));
5352 : 3 : return false;
5353 : : }
5354 : :
5355 : 206291 : if (index->ts.type == BT_REAL)
5356 : 336 : if (!gfc_notify_std (GFC_STD_LEGACY, "REAL array index at %L",
5357 : : &index->where))
5358 : : return false;
5359 : :
5360 : 206291 : if ((index->ts.kind != gfc_index_integer_kind
5361 : 201607 : && force_index_integer_kind)
5362 : 176707 : || (index->ts.type != BT_INTEGER
5363 : : && index->ts.type != BT_UNKNOWN))
5364 : : {
5365 : 29919 : gfc_clear_ts (&ts);
5366 : 29919 : ts.type = BT_INTEGER;
5367 : 29919 : ts.kind = gfc_index_integer_kind;
5368 : :
5369 : 29919 : gfc_convert_type_warn (index, &ts, 2, 0);
5370 : : }
5371 : :
5372 : : return true;
5373 : : }
5374 : :
5375 : : /* Resolve one part of an array index. */
5376 : :
5377 : : bool
5378 : 464015 : gfc_resolve_index (gfc_expr *index, int check_scalar)
5379 : : {
5380 : 464015 : return gfc_resolve_index_1 (index, check_scalar, 1);
5381 : : }
5382 : :
5383 : : /* Resolve a dim argument to an intrinsic function. */
5384 : :
5385 : : bool
5386 : 23817 : gfc_resolve_dim_arg (gfc_expr *dim)
5387 : : {
5388 : 23817 : if (dim == NULL)
5389 : : return true;
5390 : :
5391 : 23817 : if (!gfc_resolve_expr (dim))
5392 : : return false;
5393 : :
5394 : 23817 : if (dim->rank != 0)
5395 : : {
5396 : 0 : gfc_error ("Argument dim at %L must be scalar", &dim->where);
5397 : 0 : return false;
5398 : :
5399 : : }
5400 : :
5401 : 23817 : if (dim->ts.type != BT_INTEGER)
5402 : : {
5403 : 0 : gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
5404 : 0 : return false;
5405 : : }
5406 : :
5407 : 23817 : if (dim->ts.kind != gfc_index_integer_kind)
5408 : : {
5409 : 15209 : gfc_typespec ts;
5410 : :
5411 : 15209 : gfc_clear_ts (&ts);
5412 : 15209 : ts.type = BT_INTEGER;
5413 : 15209 : ts.kind = gfc_index_integer_kind;
5414 : :
5415 : 15209 : gfc_convert_type_warn (dim, &ts, 2, 0);
5416 : : }
5417 : :
5418 : : return true;
5419 : : }
5420 : :
5421 : : /* Given an expression that contains array references, update those array
5422 : : references to point to the right array specifications. While this is
5423 : : filled in during matching, this information is difficult to save and load
5424 : : in a module, so we take care of it here.
5425 : :
5426 : : The idea here is that the original array reference comes from the
5427 : : base symbol. We traverse the list of reference structures, setting
5428 : : the stored reference to references. Component references can
5429 : : provide an additional array specification. */
5430 : : static void
5431 : : resolve_assoc_var (gfc_symbol* sym, bool resolve_target);
5432 : :
5433 : : static bool
5434 : 897 : find_array_spec (gfc_expr *e)
5435 : : {
5436 : 897 : gfc_array_spec *as;
5437 : 897 : gfc_component *c;
5438 : 897 : gfc_ref *ref;
5439 : 897 : bool class_as = false;
5440 : :
5441 : 897 : if (e->symtree->n.sym->assoc)
5442 : : {
5443 : 217 : if (e->symtree->n.sym->assoc->target)
5444 : 217 : gfc_resolve_expr (e->symtree->n.sym->assoc->target);
5445 : 217 : resolve_assoc_var (e->symtree->n.sym, false);
5446 : : }
5447 : :
5448 : 897 : if (e->symtree->n.sym->ts.type == BT_CLASS)
5449 : : {
5450 : 112 : as = CLASS_DATA (e->symtree->n.sym)->as;
5451 : 112 : class_as = true;
5452 : : }
5453 : : else
5454 : 785 : as = e->symtree->n.sym->as;
5455 : :
5456 : 2034 : for (ref = e->ref; ref; ref = ref->next)
5457 : 1144 : switch (ref->type)
5458 : : {
5459 : 899 : case REF_ARRAY:
5460 : 899 : if (as == NULL)
5461 : : {
5462 : 7 : locus loc = (GFC_LOCUS_IS_SET (ref->u.ar.where)
5463 : 14 : ? ref->u.ar.where : e->where);
5464 : 7 : gfc_error ("Invalid array reference of a non-array entity at %L",
5465 : : &loc);
5466 : 7 : return false;
5467 : : }
5468 : :
5469 : 892 : ref->u.ar.as = as;
5470 : 892 : if (ref->u.ar.dimen == -1) ref->u.ar.dimen = as->rank;
5471 : : as = NULL;
5472 : : break;
5473 : :
5474 : 221 : case REF_COMPONENT:
5475 : 221 : c = ref->u.c.component;
5476 : 221 : if (c->attr.dimension)
5477 : : {
5478 : 90 : if (as != NULL && !(class_as && as == c->as))
5479 : 0 : gfc_internal_error ("find_array_spec(): unused as(1)");
5480 : 90 : as = c->as;
5481 : : }
5482 : :
5483 : : break;
5484 : :
5485 : : case REF_SUBSTRING:
5486 : : case REF_INQUIRY:
5487 : : break;
5488 : : }
5489 : :
5490 : 890 : if (as != NULL)
5491 : 0 : gfc_internal_error ("find_array_spec(): unused as(2)");
5492 : :
5493 : : return true;
5494 : : }
5495 : :
5496 : :
5497 : : /* Resolve an array reference. */
5498 : :
5499 : : static bool
5500 : 409075 : resolve_array_ref (gfc_array_ref *ar)
5501 : : {
5502 : 409075 : int i, check_scalar;
5503 : 409075 : gfc_expr *e;
5504 : :
5505 : 640727 : for (i = 0; i < ar->dimen + ar->codimen; i++)
5506 : : {
5507 : 231680 : check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
5508 : :
5509 : : /* Do not force gfc_index_integer_kind for the start. We can
5510 : : do fine with any integer kind. This avoids temporary arrays
5511 : : created for indexing with a vector. */
5512 : 231680 : if (!gfc_resolve_index_1 (ar->start[i], check_scalar, 0))
5513 : : return false;
5514 : 231654 : if (!gfc_resolve_index (ar->end[i], check_scalar))
5515 : : return false;
5516 : 231652 : if (!gfc_resolve_index (ar->stride[i], check_scalar))
5517 : : return false;
5518 : :
5519 : 231652 : e = ar->start[i];
5520 : :
5521 : 231652 : if (ar->dimen_type[i] == DIMEN_UNKNOWN)
5522 : 139930 : switch (e->rank)
5523 : : {
5524 : 139066 : case 0:
5525 : 139066 : ar->dimen_type[i] = DIMEN_ELEMENT;
5526 : 139066 : break;
5527 : :
5528 : 864 : case 1:
5529 : 864 : ar->dimen_type[i] = DIMEN_VECTOR;
5530 : 864 : if (e->expr_type == EXPR_VARIABLE
5531 : 446 : && e->symtree->n.sym->ts.type == BT_DERIVED)
5532 : 13 : ar->start[i] = gfc_get_parentheses (e);
5533 : : break;
5534 : :
5535 : 0 : default:
5536 : 0 : gfc_error ("Array index at %L is an array of rank %d",
5537 : : &ar->c_where[i], e->rank);
5538 : 0 : return false;
5539 : : }
5540 : :
5541 : : /* Fill in the upper bound, which may be lower than the
5542 : : specified one for something like a(2:10:5), which is
5543 : : identical to a(2:7:5). Only relevant for strides not equal
5544 : : to one. Don't try a division by zero. */
5545 : 231652 : if (ar->dimen_type[i] == DIMEN_RANGE
5546 : 69896 : && ar->stride[i] != NULL && ar->stride[i]->expr_type == EXPR_CONSTANT
5547 : 7797 : && mpz_cmp_si (ar->stride[i]->value.integer, 1L) != 0
5548 : 7650 : && mpz_cmp_si (ar->stride[i]->value.integer, 0L) != 0)
5549 : : {
5550 : 7649 : mpz_t size, end;
5551 : :
5552 : 7649 : if (gfc_ref_dimen_size (ar, i, &size, &end))
5553 : : {
5554 : 6096 : if (ar->end[i] == NULL)
5555 : : {
5556 : 7864 : ar->end[i] =
5557 : 3932 : gfc_get_constant_expr (BT_INTEGER, gfc_index_integer_kind,
5558 : : &ar->where);
5559 : 3932 : mpz_set (ar->end[i]->value.integer, end);
5560 : : }
5561 : 2164 : else if (ar->end[i]->ts.type == BT_INTEGER
5562 : 2164 : && ar->end[i]->expr_type == EXPR_CONSTANT)
5563 : : {
5564 : 2164 : mpz_set (ar->end[i]->value.integer, end);
5565 : : }
5566 : : else
5567 : 0 : gcc_unreachable ();
5568 : :
5569 : 6096 : mpz_clear (size);
5570 : 6096 : mpz_clear (end);
5571 : : }
5572 : : }
5573 : : }
5574 : :
5575 : 409047 : if (ar->type == AR_FULL)
5576 : : {
5577 : 256868 : if (ar->as->rank == 0)
5578 : 2923 : ar->type = AR_ELEMENT;
5579 : :
5580 : : /* Make sure array is the same as array(:,:), this way
5581 : : we don't need to special case all the time. */
5582 : 256868 : ar->dimen = ar->as->rank;
5583 : 614454 : for (i = 0; i < ar->dimen; i++)
5584 : : {
5585 : 357586 : ar->dimen_type[i] = DIMEN_RANGE;
5586 : :
5587 : 357586 : gcc_assert (ar->start[i] == NULL);
5588 : 357586 : gcc_assert (ar->end[i] == NULL);
5589 : 357586 : gcc_assert (ar->stride[i] == NULL);
5590 : : }
5591 : : }
5592 : :
5593 : : /* If the reference type is unknown, figure out what kind it is. */
5594 : :
5595 : 409047 : if (ar->type == AR_UNKNOWN)
5596 : : {
5597 : 141646 : ar->type = AR_ELEMENT;
5598 : 275592 : for (i = 0; i < ar->dimen; i++)
5599 : 170290 : if (ar->dimen_type[i] == DIMEN_RANGE
5600 : 170290 : || ar->dimen_type[i] == DIMEN_VECTOR)
5601 : : {
5602 : 36344 : ar->type = AR_SECTION;
5603 : 36344 : break;
5604 : : }
5605 : : }
5606 : :
5607 : 409047 : if (!ar->as->cray_pointee && !compare_spec_to_ref (ar))
5608 : : return false;
5609 : :
5610 : 409011 : if (ar->as->corank && ar->codimen == 0)
5611 : : {
5612 : 1655 : int n;
5613 : 1655 : ar->codimen = ar->as->corank;
5614 : 4546 : for (n = ar->dimen; n < ar->dimen + ar->codimen; n++)
5615 : 2891 : ar->dimen_type[n] = DIMEN_THIS_IMAGE;
5616 : : }
5617 : :
5618 : 409011 : if (ar->codimen)
5619 : : {
5620 : 10647 : if (ar->team_type == TEAM_NUMBER)
5621 : : {
5622 : 40 : if (!gfc_resolve_expr (ar->team))
5623 : : return false;
5624 : :
5625 : 40 : if (ar->team->rank != 0)
5626 : : {
5627 : 0 : gfc_error ("TEAM_NUMBER argument at %L must be scalar",
5628 : : &ar->team->where);
5629 : 0 : return false;
5630 : : }
5631 : :
5632 : 40 : if (ar->team->ts.type != BT_INTEGER)
5633 : : {
5634 : 4 : gfc_error ("TEAM_NUMBER argument at %L must be of INTEGER "
5635 : : "type, found %s",
5636 : 4 : &ar->team->where,
5637 : : gfc_basic_typename (ar->team->ts.type));
5638 : 4 : return false;
5639 : : }
5640 : : }
5641 : 10607 : else if (ar->team_type == TEAM_TEAM)
5642 : : {
5643 : 28 : if (!gfc_resolve_expr (ar->team))
5644 : : return false;
5645 : :
5646 : 28 : if (ar->team->rank != 0)
5647 : : {
5648 : 2 : gfc_error ("TEAM argument at %L must be scalar",
5649 : : &ar->team->where);
5650 : 2 : return false;
5651 : : }
5652 : :
5653 : 26 : if (ar->team->ts.type != BT_DERIVED
5654 : 24 : || ar->team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
5655 : 24 : || ar->team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
5656 : : {
5657 : 2 : gfc_error ("TEAM argument at %L must be of TEAM_TYPE from "
5658 : : "the intrinsic module ISO_FORTRAN_ENV, found %s",
5659 : 2 : &ar->team->where,
5660 : : gfc_basic_typename (ar->team->ts.type));
5661 : 2 : return false;
5662 : : }
5663 : : }
5664 : 10639 : if (ar->stat)
5665 : : {
5666 : 45 : if (!gfc_resolve_expr (ar->stat))
5667 : : return false;
5668 : :
5669 : 45 : if (ar->stat->rank != 0)
5670 : : {
5671 : 2 : gfc_error ("STAT argument at %L must be scalar",
5672 : : &ar->stat->where);
5673 : 2 : return false;
5674 : : }
5675 : :
5676 : 43 : if (ar->stat->ts.type != BT_INTEGER)
5677 : : {
5678 : 2 : gfc_error ("STAT argument at %L must be of INTEGER "
5679 : : "type, found %s",
5680 : 2 : &ar->stat->where,
5681 : : gfc_basic_typename (ar->stat->ts.type));
5682 : 2 : return false;
5683 : : }
5684 : :
5685 : 41 : if (ar->stat->expr_type != EXPR_VARIABLE)
5686 : : {
5687 : 0 : gfc_error ("STAT's expression at %L must be a variable",
5688 : : &ar->stat->where);
5689 : 0 : return false;
5690 : : }
5691 : : }
5692 : : }
5693 : : return true;
5694 : : }
5695 : :
5696 : :
5697 : : bool
5698 : 8311 : gfc_resolve_substring (gfc_ref *ref, bool *equal_length)
5699 : : {
5700 : 8311 : int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
5701 : :
5702 : 8311 : if (ref->u.ss.start != NULL)
5703 : : {
5704 : 8311 : if (!gfc_resolve_expr (ref->u.ss.start))
5705 : : return false;
5706 : :
5707 : 8311 : if (ref->u.ss.start->ts.type != BT_INTEGER)
5708 : : {
5709 : 1 : gfc_error ("Substring start index at %L must be of type INTEGER",
5710 : : &ref->u.ss.start->where);
5711 : 1 : return false;
5712 : : }
5713 : :
5714 : 8310 : if (ref->u.ss.start->rank != 0)
5715 : : {
5716 : 0 : gfc_error ("Substring start index at %L must be scalar",
5717 : : &ref->u.ss.start->where);
5718 : 0 : return false;
5719 : : }
5720 : :
5721 : 8310 : if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
5722 : 8310 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5723 : 37 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5724 : : {
5725 : 1 : gfc_error ("Substring start index at %L is less than one",
5726 : : &ref->u.ss.start->where);
5727 : 1 : return false;
5728 : : }
5729 : : }
5730 : :
5731 : 8309 : if (ref->u.ss.end != NULL)
5732 : : {
5733 : 8139 : if (!gfc_resolve_expr (ref->u.ss.end))
5734 : : return false;
5735 : :
5736 : 8139 : if (ref->u.ss.end->ts.type != BT_INTEGER)
5737 : : {
5738 : 1 : gfc_error ("Substring end index at %L must be of type INTEGER",
5739 : : &ref->u.ss.end->where);
5740 : 1 : return false;
5741 : : }
5742 : :
5743 : 8138 : if (ref->u.ss.end->rank != 0)
5744 : : {
5745 : 0 : gfc_error ("Substring end index at %L must be scalar",
5746 : : &ref->u.ss.end->where);
5747 : 0 : return false;
5748 : : }
5749 : :
5750 : 8138 : if (ref->u.ss.length != NULL
5751 : 7807 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
5752 : 8150 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5753 : 12 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5754 : : {
5755 : 4 : gfc_error ("Substring end index at %L exceeds the string length",
5756 : : &ref->u.ss.start->where);
5757 : 4 : return false;
5758 : : }
5759 : :
5760 : 8134 : if (compare_bound_mpz_t (ref->u.ss.end,
5761 : 8134 : gfc_integer_kinds[k].huge) == CMP_GT
5762 : 8134 : && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
5763 : 7 : || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
5764 : : {
5765 : 4 : gfc_error ("Substring end index at %L is too large",
5766 : : &ref->u.ss.end->where);
5767 : 4 : return false;
5768 : : }
5769 : : /* If the substring has the same length as the original
5770 : : variable, the reference itself can be deleted. */
5771 : :
5772 : 8130 : if (ref->u.ss.length != NULL
5773 : 7799 : && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_EQ
5774 : 9044 : && compare_bound_int (ref->u.ss.start, 1) == CMP_EQ)
5775 : 228 : *equal_length = true;
5776 : : }
5777 : :
5778 : : return true;
5779 : : }
5780 : :
5781 : :
5782 : : /* This function supplies missing substring charlens. */
5783 : :
5784 : : void
5785 : 4543 : gfc_resolve_substring_charlen (gfc_expr *e)
5786 : : {
5787 : 4543 : gfc_ref *char_ref;
5788 : 4543 : gfc_expr *start, *end;
5789 : 4543 : gfc_typespec *ts = NULL;
5790 : 4543 : mpz_t diff;
5791 : :
5792 : 8842 : for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
5793 : : {
5794 : 7008 : if (char_ref->type == REF_SUBSTRING || char_ref->type == REF_INQUIRY)
5795 : : break;
5796 : 4299 : if (char_ref->type == REF_COMPONENT)
5797 : 322 : ts = &char_ref->u.c.component->ts;
5798 : : }
5799 : :
5800 : 4543 : if (!char_ref || char_ref->type == REF_INQUIRY)
5801 : 1896 : return;
5802 : :
5803 : 2709 : gcc_assert (char_ref->next == NULL);
5804 : :
5805 : 2709 : if (e->ts.u.cl)
5806 : : {
5807 : 120 : if (e->ts.u.cl->length)
5808 : 108 : gfc_free_expr (e->ts.u.cl->length);
5809 : 12 : else if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym->attr.dummy)
5810 : : return;
5811 : : }
5812 : :
5813 : 2697 : if (!e->ts.u.cl)
5814 : 2589 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
5815 : :
5816 : 2697 : if (char_ref->u.ss.start)
5817 : 2697 : start = gfc_copy_expr (char_ref->u.ss.start);
5818 : : else
5819 : 0 : start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
5820 : :
5821 : 2697 : if (char_ref->u.ss.end)
5822 : 2647 : end = gfc_copy_expr (char_ref->u.ss.end);
5823 : 50 : else if (e->expr_type == EXPR_VARIABLE)
5824 : : {
5825 : 50 : if (!ts)
5826 : 32 : ts = &e->symtree->n.sym->ts;
5827 : 50 : end = gfc_copy_expr (ts->u.cl->length);
5828 : : }
5829 : : else
5830 : : end = NULL;
5831 : :
5832 : 2697 : if (!start || !end)
5833 : : {
5834 : 50 : gfc_free_expr (start);
5835 : 50 : gfc_free_expr (end);
5836 : 50 : return;
5837 : : }
5838 : :
5839 : : /* Length = (end - start + 1).
5840 : : Check first whether it has a constant length. */
5841 : 2647 : if (gfc_dep_difference (end, start, &diff))
5842 : : {
5843 : 2532 : gfc_expr *len = gfc_get_constant_expr (BT_INTEGER, gfc_charlen_int_kind,
5844 : : &e->where);
5845 : :
5846 : 2532 : mpz_add_ui (len->value.integer, diff, 1);
5847 : 2532 : mpz_clear (diff);
5848 : 2532 : e->ts.u.cl->length = len;
5849 : : /* The check for length < 0 is handled below */
5850 : : }
5851 : : else
5852 : : {
5853 : 115 : e->ts.u.cl->length = gfc_subtract (end, start);
5854 : 115 : e->ts.u.cl->length = gfc_add (e->ts.u.cl->length,
5855 : : gfc_get_int_expr (gfc_charlen_int_kind,
5856 : : NULL, 1));
5857 : : }
5858 : :
5859 : : /* F2008, 6.4.1: Both the starting point and the ending point shall
5860 : : be within the range 1, 2, ..., n unless the starting point exceeds
5861 : : the ending point, in which case the substring has length zero. */
5862 : :
5863 : 2647 : if (mpz_cmp_si (e->ts.u.cl->length->value.integer, 0) < 0)
5864 : 15 : mpz_set_si (e->ts.u.cl->length->value.integer, 0);
5865 : :
5866 : 2647 : e->ts.u.cl->length->ts.type = BT_INTEGER;
5867 : 2647 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
5868 : :
5869 : : /* Make sure that the length is simplified. */
5870 : 2647 : gfc_simplify_expr (e->ts.u.cl->length, 1);
5871 : 2647 : gfc_resolve_expr (e->ts.u.cl->length);
5872 : : }
5873 : :
5874 : :
5875 : : /* Resolve subtype references. */
5876 : :
5877 : : bool
5878 : 520149 : gfc_resolve_ref (gfc_expr *expr)
5879 : : {
5880 : 520149 : int current_part_dimension, n_components, seen_part_dimension, dim;
5881 : 520149 : gfc_ref *ref, **prev, *array_ref;
5882 : 520149 : bool equal_length;
5883 : :
5884 : 1018709 : for (ref = expr->ref; ref; ref = ref->next)
5885 : 499457 : if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
5886 : : {
5887 : 897 : if (!find_array_spec (expr))
5888 : : return false;
5889 : : break;
5890 : : }
5891 : :
5892 : 1518975 : for (prev = &expr->ref; *prev != NULL;
5893 : 499518 : prev = *prev == NULL ? prev : &(*prev)->next)
5894 : 499602 : switch ((*prev)->type)
5895 : : {
5896 : 409075 : case REF_ARRAY:
5897 : 409075 : if (!resolve_array_ref (&(*prev)->u.ar))
5898 : : return false;
5899 : : break;
5900 : :
5901 : : case REF_COMPONENT:
5902 : : case REF_INQUIRY:
5903 : : break;
5904 : :
5905 : 8033 : case REF_SUBSTRING:
5906 : 8033 : equal_length = false;
5907 : 8033 : if (!gfc_resolve_substring (*prev, &equal_length))
5908 : : return false;
5909 : :
5910 : 8025 : if (expr->expr_type != EXPR_SUBSTRING && equal_length)
5911 : : {
5912 : : /* Remove the reference and move the charlen, if any. */
5913 : 203 : ref = *prev;
5914 : 203 : *prev = ref->next;
5915 : 203 : ref->next = NULL;
5916 : 203 : expr->ts.u.cl = ref->u.ss.length;
5917 : 203 : ref->u.ss.length = NULL;
5918 : 203 : gfc_free_ref_list (ref);
5919 : : }
5920 : : break;
5921 : : }
5922 : :
5923 : : /* Check constraints on part references. */
5924 : :
5925 : 520058 : current_part_dimension = 0;
5926 : 520058 : seen_part_dimension = 0;
5927 : 520058 : n_components = 0;
5928 : 520058 : array_ref = NULL;
5929 : :
5930 : 1019348 : for (ref = expr->ref; ref; ref = ref->next)
5931 : : {
5932 : 499301 : switch (ref->type)
5933 : : {
5934 : 408992 : case REF_ARRAY:
5935 : 408992 : array_ref = ref;
5936 : 408992 : switch (ref->u.ar.type)
5937 : : {
5938 : 253943 : case AR_FULL:
5939 : : /* Coarray scalar. */
5940 : 253943 : if (ref->u.ar.as->rank == 0)
5941 : : {
5942 : : current_part_dimension = 0;
5943 : : break;
5944 : : }
5945 : : /* Fall through. */
5946 : 292987 : case AR_SECTION:
5947 : 292987 : current_part_dimension = 1;
5948 : 292987 : break;
5949 : :
5950 : 116005 : case AR_ELEMENT:
5951 : 116005 : array_ref = NULL;
5952 : 116005 : current_part_dimension = 0;
5953 : 116005 : break;
5954 : :
5955 : 0 : case AR_UNKNOWN:
5956 : 0 : gfc_internal_error ("resolve_ref(): Bad array reference");
5957 : : }
5958 : :
5959 : : break;
5960 : :
5961 : 81762 : case REF_COMPONENT:
5962 : 81762 : if (current_part_dimension || seen_part_dimension)
5963 : : {
5964 : : /* F03:C614. */
5965 : 5960 : if (ref->u.c.component->attr.pointer
5966 : 5957 : || ref->u.c.component->attr.proc_pointer
5967 : 5956 : || (ref->u.c.component->ts.type == BT_CLASS
5968 : 1 : && CLASS_DATA (ref->u.c.component)->attr.pointer))
5969 : : {
5970 : 4 : gfc_error ("Component to the right of a part reference "
5971 : : "with nonzero rank must not have the POINTER "
5972 : : "attribute at %L", &expr->where);
5973 : 4 : return false;
5974 : : }
5975 : 5956 : else if (ref->u.c.component->attr.allocatable
5976 : 5950 : || (ref->u.c.component->ts.type == BT_CLASS
5977 : 1 : && CLASS_DATA (ref->u.c.component)->attr.allocatable))
5978 : :
5979 : : {
5980 : 7 : gfc_error ("Component to the right of a part reference "
5981 : : "with nonzero rank must not have the ALLOCATABLE "
5982 : : "attribute at %L", &expr->where);
5983 : 7 : return false;
5984 : : }
5985 : : }
5986 : :
5987 : 81751 : n_components++;
5988 : 81751 : break;
5989 : :
5990 : : case REF_SUBSTRING:
5991 : : break;
5992 : :
5993 : 725 : case REF_INQUIRY:
5994 : : /* Implement requirement in note 9.7 of F2018 that the result of the
5995 : : LEN inquiry be a scalar. */
5996 : 725 : if (ref->u.i == INQUIRY_LEN && array_ref
5997 : 40 : && ((expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->length)
5998 : 40 : || expr->ts.type == BT_INTEGER))
5999 : : {
6000 : 14 : array_ref->u.ar.type = AR_ELEMENT;
6001 : 14 : expr->rank = 0;
6002 : : /* INQUIRY_LEN is not evaluated from the rest of the expr
6003 : : but directly from the string length. This means that setting
6004 : : the array indices to one does not matter but might trigger
6005 : : a runtime bounds error. Suppress the check. */
6006 : 14 : expr->no_bounds_check = 1;
6007 : 28 : for (dim = 0; dim < array_ref->u.ar.dimen; dim++)
6008 : : {
6009 : 14 : array_ref->u.ar.dimen_type[dim] = DIMEN_ELEMENT;
6010 : 14 : if (array_ref->u.ar.start[dim])
6011 : 0 : gfc_free_expr (array_ref->u.ar.start[dim]);
6012 : 14 : array_ref->u.ar.start[dim]
6013 : 14 : = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6014 : 14 : if (array_ref->u.ar.end[dim])
6015 : 0 : gfc_free_expr (array_ref->u.ar.end[dim]);
6016 : 14 : if (array_ref->u.ar.stride[dim])
6017 : 0 : gfc_free_expr (array_ref->u.ar.stride[dim]);
6018 : : }
6019 : : }
6020 : : break;
6021 : : }
6022 : :
6023 : 499290 : if (((ref->type == REF_COMPONENT && n_components > 1)
6024 : 487656 : || ref->next == NULL)
6025 : : && current_part_dimension
6026 : 441146 : && seen_part_dimension)
6027 : : {
6028 : 0 : gfc_error ("Two or more part references with nonzero rank must "
6029 : : "not be specified at %L", &expr->where);
6030 : 0 : return false;
6031 : : }
6032 : :
6033 : 499290 : if (ref->type == REF_COMPONENT)
6034 : : {
6035 : 81751 : if (current_part_dimension)
6036 : 5768 : seen_part_dimension = 1;
6037 : :
6038 : : /* reset to make sure */
6039 : : current_part_dimension = 0;
6040 : : }
6041 : : }
6042 : :
6043 : : return true;
6044 : : }
6045 : :
6046 : :
6047 : : /* Given an expression, determine its shape. This is easier than it sounds.
6048 : : Leaves the shape array NULL if it is not possible to determine the shape. */
6049 : :
6050 : : static void
6051 : 2534034 : expression_shape (gfc_expr *e)
6052 : : {
6053 : 2534034 : mpz_t array[GFC_MAX_DIMENSIONS];
6054 : 2534034 : int i;
6055 : :
6056 : 2534034 : if (e->rank <= 0 || e->shape != NULL)
6057 : 2366440 : return;
6058 : :
6059 : 679389 : for (i = 0; i < e->rank; i++)
6060 : 458752 : if (!gfc_array_dimen_size (e, i, &array[i]))
6061 : 167594 : goto fail;
6062 : :
6063 : 220637 : e->shape = gfc_get_shape (e->rank);
6064 : :
6065 : 220637 : memcpy (e->shape, array, e->rank * sizeof (mpz_t));
6066 : :
6067 : 220637 : return;
6068 : :
6069 : 167594 : fail:
6070 : 169265 : for (i--; i >= 0; i--)
6071 : 1671 : mpz_clear (array[i]);
6072 : : }
6073 : :
6074 : :
6075 : : /* Given a variable expression node, compute the rank of the expression by
6076 : : examining the base symbol and any reference structures it may have. */
6077 : :
6078 : : void
6079 : 2534034 : gfc_expression_rank (gfc_expr *e)
6080 : : {
6081 : 2534034 : gfc_ref *ref, *last_arr_ref = nullptr;
6082 : 2534034 : int i, rank, corank;
6083 : :
6084 : : /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
6085 : : could lead to serious confusion... */
6086 : 2534034 : gcc_assert (e->expr_type != EXPR_COMPCALL);
6087 : :
6088 : 2534034 : if (e->ref == NULL)
6089 : : {
6090 : 1875829 : if (e->expr_type == EXPR_ARRAY)
6091 : 67188 : goto done;
6092 : : /* Constructors can have a rank different from one via RESHAPE(). */
6093 : :
6094 : 1808641 : if (e->symtree != NULL)
6095 : : {
6096 : : /* After errors the ts.u.derived of a CLASS might not be set. */
6097 : 1808629 : gfc_array_spec *as = (e->symtree->n.sym->ts.type == BT_CLASS
6098 : 13606 : && e->symtree->n.sym->ts.u.derived
6099 : 13601 : && CLASS_DATA (e->symtree->n.sym))
6100 : 1808629 : ? CLASS_DATA (e->symtree->n.sym)->as
6101 : : : e->symtree->n.sym->as;
6102 : 1808629 : if (as)
6103 : : {
6104 : 580 : e->rank = as->rank;
6105 : 580 : e->corank = as->corank;
6106 : 580 : goto done;
6107 : : }
6108 : : }
6109 : 1808061 : e->rank = 0;
6110 : 1808061 : e->corank = 0;
6111 : 1808061 : goto done;
6112 : : }
6113 : :
6114 : : rank = 0;
6115 : : corank = 0;
6116 : :
6117 : 1032642 : for (ref = e->ref; ref; ref = ref->next)
6118 : : {
6119 : 754060 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.proc_pointer
6120 : 535 : && ref->u.c.component->attr.function && !ref->next)
6121 : : {
6122 : 344 : rank = ref->u.c.component->as ? ref->u.c.component->as->rank : 0;
6123 : 344 : corank = ref->u.c.component->as ? ref->u.c.component->as->corank : 0;
6124 : : }
6125 : :
6126 : 754060 : if (ref->type != REF_ARRAY)
6127 : 145991 : continue;
6128 : :
6129 : 608069 : last_arr_ref = ref;
6130 : 608069 : if (ref->u.ar.type == AR_FULL && ref->u.ar.as)
6131 : : {
6132 : 336133 : rank = ref->u.ar.as->rank;
6133 : 336133 : break;
6134 : : }
6135 : :
6136 : 271936 : if (ref->u.ar.type == AR_SECTION)
6137 : : {
6138 : : /* Figure out the rank of the section. */
6139 : 43490 : if (rank != 0)
6140 : 0 : gfc_internal_error ("gfc_expression_rank(): Two array specs");
6141 : :
6142 : 108750 : for (i = 0; i < ref->u.ar.dimen; i++)
6143 : 65260 : if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6144 : 65260 : || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
6145 : 57044 : rank++;
6146 : :
6147 : : break;
6148 : : }
6149 : : }
6150 : 658205 : if (last_arr_ref && last_arr_ref->u.ar.as
6151 : 591170 : && last_arr_ref->u.ar.as->rank != -1)
6152 : : {
6153 : 14912 : for (i = last_arr_ref->u.ar.as->rank;
6154 : 598002 : i < last_arr_ref->u.ar.as->rank + last_arr_ref->u.ar.as->corank; ++i)
6155 : : {
6156 : : /* For unknown dimen in non-resolved as assume full corank. */
6157 : 15651 : if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_STAR
6158 : 15178 : || (last_arr_ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6159 : 266 : && !last_arr_ref->u.ar.as->resolved))
6160 : : {
6161 : : corank = last_arr_ref->u.ar.as->corank;
6162 : : break;
6163 : : }
6164 : 14912 : else if (last_arr_ref->u.ar.dimen_type[i] == DIMEN_RANGE
6165 : 14912 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6166 : 14829 : || last_arr_ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE)
6167 : 12912 : corank++;
6168 : 2000 : else if (last_arr_ref->u.ar.dimen_type[i] != DIMEN_ELEMENT)
6169 : 0 : gfc_internal_error ("Illegal coarray index");
6170 : : }
6171 : : }
6172 : :
6173 : 658205 : e->rank = rank;
6174 : 658205 : e->corank = corank;
6175 : :
6176 : 2534034 : done:
6177 : 2534034 : expression_shape (e);
6178 : 2534034 : }
6179 : :
6180 : :
6181 : : /* Given two expressions, check that their rank is conformable, i.e. either
6182 : : both have the same rank or at least one is a scalar. */
6183 : :
6184 : : bool
6185 : 12176041 : gfc_op_rank_conformable (gfc_expr *op1, gfc_expr *op2)
6186 : : {
6187 : 12176041 : if (op1->expr_type == EXPR_VARIABLE)
6188 : 719726 : gfc_expression_rank (op1);
6189 : 12176041 : if (op2->expr_type == EXPR_VARIABLE)
6190 : 442213 : gfc_expression_rank (op2);
6191 : :
6192 : 73946 : return (op1->rank == 0 || op2->rank == 0 || op1->rank == op2->rank)
6193 : 12249661 : && (op1->corank == 0 || op2->corank == 0 || op1->corank == op2->corank
6194 : 20 : || (!gfc_is_coindexed (op1) && !gfc_is_coindexed (op2)));
6195 : : }
6196 : :
6197 : : /* Resolve a variable expression. */
6198 : :
6199 : : static bool
6200 : 1288095 : resolve_variable (gfc_expr *e)
6201 : : {
6202 : 1288095 : gfc_symbol *sym;
6203 : 1288095 : bool t;
6204 : :
6205 : 1288095 : t = true;
6206 : :
6207 : 1288095 : if (e->symtree == NULL)
6208 : : return false;
6209 : 1287676 : sym = e->symtree->n.sym;
6210 : :
6211 : : /* Use same check as for TYPE(*) below; this check has to be before TYPE(*)
6212 : : as ts.type is set to BT_ASSUMED in resolve_symbol. */
6213 : 1287676 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
6214 : : {
6215 : 167 : if (!actual_arg || inquiry_argument)
6216 : : {
6217 : 2 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may only "
6218 : : "be used as actual argument", sym->name, &e->where);
6219 : 2 : return false;
6220 : : }
6221 : : }
6222 : : /* TS 29113, 407b. */
6223 : 1287509 : else if (e->ts.type == BT_ASSUMED)
6224 : : {
6225 : 555 : if (!actual_arg)
6226 : : {
6227 : 20 : gfc_error ("Assumed-type variable %s at %L may only be used "
6228 : : "as actual argument", sym->name, &e->where);
6229 : 20 : return false;
6230 : : }
6231 : 535 : else if (inquiry_argument && !first_actual_arg)
6232 : : {
6233 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6234 : : for all inquiry functions in resolve_function; the reason is
6235 : : that the function-name resolution happens too late in that
6236 : : function. */
6237 : 0 : gfc_error ("Assumed-type variable %s at %L as actual argument to "
6238 : : "an inquiry function shall be the first argument",
6239 : : sym->name, &e->where);
6240 : 0 : return false;
6241 : : }
6242 : : }
6243 : : /* TS 29113, C535b. */
6244 : 1286954 : else if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6245 : 35749 : && sym->ts.u.derived && CLASS_DATA (sym)
6246 : 35744 : && CLASS_DATA (sym)->as
6247 : 13695 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6248 : 1286044 : || (sym->ts.type != BT_CLASS && sym->as
6249 : 352220 : && sym->as->type == AS_ASSUMED_RANK))
6250 : 7888 : && !sym->attr.select_rank_temporary
6251 : 7888 : && !(sym->assoc && sym->assoc->ar))
6252 : : {
6253 : 7888 : if (!actual_arg
6254 : 1247 : && !(cs_base && cs_base->current
6255 : 1246 : && (cs_base->current->op == EXEC_SELECT_RANK
6256 : 188 : || sym->attr.target)))
6257 : : {
6258 : 144 : gfc_error ("Assumed-rank variable %s at %L may only be used as "
6259 : : "actual argument", sym->name, &e->where);
6260 : 144 : return false;
6261 : : }
6262 : 7744 : else if (inquiry_argument && !first_actual_arg)
6263 : : {
6264 : : /* FIXME: It doesn't work reliably as inquiry_argument is not set
6265 : : for all inquiry functions in resolve_function; the reason is
6266 : : that the function-name resolution happens too late in that
6267 : : function. */
6268 : 0 : gfc_error ("Assumed-rank variable %s at %L as actual argument "
6269 : : "to an inquiry function shall be the first argument",
6270 : : sym->name, &e->where);
6271 : 0 : return false;
6272 : : }
6273 : : }
6274 : :
6275 : 1287510 : if ((sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK)) && e->ref
6276 : 165 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6277 : 164 : && e->ref->next == NULL))
6278 : : {
6279 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall not have "
6280 : : "a subobject reference", sym->name, &e->ref->u.ar.where);
6281 : 1 : return false;
6282 : : }
6283 : : /* TS 29113, 407b. */
6284 : 1287509 : else if (e->ts.type == BT_ASSUMED && e->ref
6285 : 655 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6286 : 648 : && e->ref->next == NULL))
6287 : : {
6288 : 7 : gfc_error ("Assumed-type variable %s at %L shall not have a subobject "
6289 : : "reference", sym->name, &e->ref->u.ar.where);
6290 : 7 : return false;
6291 : : }
6292 : :
6293 : : /* TS 29113, C535b. */
6294 : 1287502 : if (((sym->ts.type == BT_CLASS && sym->attr.class_ok
6295 : 35749 : && sym->ts.u.derived && CLASS_DATA (sym)
6296 : 35744 : && CLASS_DATA (sym)->as
6297 : 13695 : && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
6298 : 1286592 : || (sym->ts.type != BT_CLASS && sym->as
6299 : 352724 : && sym->as->type == AS_ASSUMED_RANK))
6300 : 8012 : && !(sym->assoc && sym->assoc->ar)
6301 : 8012 : && e->ref
6302 : 8012 : && !(e->ref->type == REF_ARRAY && e->ref->u.ar.type == AR_FULL
6303 : 8008 : && e->ref->next == NULL))
6304 : : {
6305 : 4 : gfc_error ("Assumed-rank variable %s at %L shall not have a subobject "
6306 : : "reference", sym->name, &e->ref->u.ar.where);
6307 : 4 : return false;
6308 : : }
6309 : :
6310 : : /* Guessed type variables are associate_names whose selector had not been
6311 : : parsed at the time that the construct was parsed. Now the namespace is
6312 : : being resolved, the TKR of the selector will be available for fixup of
6313 : : the associate_name. */
6314 : 1287498 : if (IS_INFERRED_TYPE (e) && e->ref)
6315 : : {
6316 : 372 : gfc_fixup_inferred_type_refs (e);
6317 : : /* KIND inquiry ref returns the kind of the target. */
6318 : 372 : if (e->expr_type == EXPR_CONSTANT)
6319 : : return true;
6320 : : }
6321 : 1287126 : else if (sym->attr.select_type_temporary
6322 : 8802 : && sym->ns->assoc_name_inferred)
6323 : 92 : gfc_fixup_inferred_type_refs (e);
6324 : :
6325 : : /* For variables that are used in an associate (target => object) where
6326 : : the object's basetype is array valued while the target is scalar,
6327 : : the ts' type of the component refs is still array valued, which
6328 : : can't be translated that way. */
6329 : 1287486 : if (sym->assoc && e->rank == 0 && e->ref && sym->ts.type == BT_CLASS
6330 : 563 : && sym->assoc->target && sym->assoc->target->ts.type == BT_CLASS
6331 : 563 : && sym->assoc->target->ts.u.derived
6332 : 563 : && CLASS_DATA (sym->assoc->target)
6333 : 563 : && CLASS_DATA (sym->assoc->target)->as)
6334 : : {
6335 : : gfc_ref *ref = e->ref;
6336 : 657 : while (ref)
6337 : : {
6338 : 499 : switch (ref->type)
6339 : : {
6340 : 216 : case REF_COMPONENT:
6341 : 216 : ref->u.c.sym = sym->ts.u.derived;
6342 : : /* Stop the loop. */
6343 : 216 : ref = NULL;
6344 : 216 : break;
6345 : 283 : default:
6346 : 283 : ref = ref->next;
6347 : 283 : break;
6348 : : }
6349 : : }
6350 : : }
6351 : :
6352 : : /* If this is an associate-name, it may be parsed with an array reference
6353 : : in error even though the target is scalar. Fail directly in this case.
6354 : : TODO Understand why class scalar expressions must be excluded. */
6355 : 1287486 : if (sym->assoc && !(sym->ts.type == BT_CLASS && e->rank == 0))
6356 : : {
6357 : 10864 : if (sym->ts.type == BT_CLASS)
6358 : 242 : gfc_fix_class_refs (e);
6359 : 10864 : if (!sym->attr.dimension && !sym->attr.codimension && e->ref
6360 : 2019 : && e->ref->type == REF_ARRAY)
6361 : : {
6362 : : /* Unambiguously scalar! */
6363 : 3 : if (sym->assoc->target
6364 : 3 : && (sym->assoc->target->expr_type == EXPR_CONSTANT
6365 : 1 : || sym->assoc->target->expr_type == EXPR_STRUCTURE))
6366 : 2 : gfc_error ("Scalar variable %qs has an array reference at %L",
6367 : : sym->name, &e->where);
6368 : 3 : return false;
6369 : : }
6370 : 10861 : else if ((sym->attr.dimension || sym->attr.codimension)
6371 : 6797 : && (!e->ref || e->ref->type != REF_ARRAY))
6372 : : {
6373 : : /* This can happen because the parser did not detect that the
6374 : : associate name is an array and the expression had no array
6375 : : part_ref. */
6376 : 140 : gfc_ref *ref = gfc_get_ref ();
6377 : 140 : ref->type = REF_ARRAY;
6378 : 140 : ref->u.ar.type = AR_FULL;
6379 : 140 : if (sym->as)
6380 : : {
6381 : 139 : ref->u.ar.as = sym->as;
6382 : 139 : ref->u.ar.dimen = sym->as->rank;
6383 : : }
6384 : 140 : ref->next = e->ref;
6385 : 140 : e->ref = ref;
6386 : : }
6387 : : }
6388 : :
6389 : 1287483 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.generic)
6390 : 0 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
6391 : :
6392 : : /* On the other hand, the parser may not have known this is an array;
6393 : : in this case, we have to add a FULL reference. */
6394 : 1287483 : if (sym->assoc && (sym->attr.dimension || sym->attr.codimension) && !e->ref)
6395 : : {
6396 : 0 : e->ref = gfc_get_ref ();
6397 : 0 : e->ref->type = REF_ARRAY;
6398 : 0 : e->ref->u.ar.type = AR_FULL;
6399 : 0 : e->ref->u.ar.dimen = 0;
6400 : : }
6401 : :
6402 : : /* Like above, but for class types, where the checking whether an array
6403 : : ref is present is more complicated. Furthermore make sure not to add
6404 : : the full array ref to _vptr or _len refs. */
6405 : 1287483 : if (sym->assoc && sym->ts.type == BT_CLASS && sym->ts.u.derived
6406 : 968 : && CLASS_DATA (sym)
6407 : 968 : && (CLASS_DATA (sym)->attr.dimension
6408 : 417 : || CLASS_DATA (sym)->attr.codimension)
6409 : 555 : && (e->ts.type != BT_DERIVED || !e->ts.u.derived->attr.vtype))
6410 : : {
6411 : 531 : gfc_ref *ref, *newref;
6412 : :
6413 : 531 : newref = gfc_get_ref ();
6414 : 531 : newref->type = REF_ARRAY;
6415 : 531 : newref->u.ar.type = AR_FULL;
6416 : 531 : newref->u.ar.dimen = 0;
6417 : :
6418 : : /* Because this is an associate var and the first ref either is a ref to
6419 : : the _data component or not, no traversal of the ref chain is
6420 : : needed. The array ref needs to be inserted after the _data ref,
6421 : : or when that is not present, which may happened for polymorphic
6422 : : types, then at the first position. */
6423 : 531 : ref = e->ref;
6424 : 531 : if (!ref)
6425 : 18 : e->ref = newref;
6426 : 513 : else if (ref->type == REF_COMPONENT
6427 : 230 : && strcmp ("_data", ref->u.c.component->name) == 0)
6428 : : {
6429 : 230 : if (!ref->next || ref->next->type != REF_ARRAY)
6430 : : {
6431 : 12 : newref->next = ref->next;
6432 : 12 : ref->next = newref;
6433 : : }
6434 : : else
6435 : : /* Array ref present already. */
6436 : 218 : gfc_free_ref_list (newref);
6437 : : }
6438 : 283 : else if (ref->type == REF_ARRAY)
6439 : : /* Array ref present already. */
6440 : 283 : gfc_free_ref_list (newref);
6441 : : else
6442 : : {
6443 : 0 : newref->next = ref;
6444 : 0 : e->ref = newref;
6445 : : }
6446 : : }
6447 : 1286952 : else if (sym->assoc && sym->ts.type == BT_CHARACTER && sym->ts.deferred)
6448 : : {
6449 : 485 : gfc_ref *ref;
6450 : 908 : for (ref = e->ref; ref; ref = ref->next)
6451 : 453 : if (ref->type == REF_SUBSTRING)
6452 : : break;
6453 : 485 : if (ref == NULL)
6454 : 455 : e->ts = sym->ts;
6455 : : }
6456 : :
6457 : 1287483 : if (e->ref && !gfc_resolve_ref (e))
6458 : : return false;
6459 : :
6460 : 1287397 : if (sym->attr.flavor == FL_PROCEDURE
6461 : 29462 : && (!sym->attr.function
6462 : 17168 : || (sym->attr.function && sym->result
6463 : 16722 : && sym->result->attr.proc_pointer
6464 : 562 : && !sym->result->attr.function)))
6465 : : {
6466 : 12294 : e->ts.type = BT_PROCEDURE;
6467 : 12294 : goto resolve_procedure;
6468 : : }
6469 : :
6470 : 1275103 : if (sym->ts.type != BT_UNKNOWN)
6471 : 1274463 : gfc_variable_attr (e, &e->ts);
6472 : 640 : else if (sym->attr.flavor == FL_PROCEDURE
6473 : 12 : && sym->attr.function && sym->result
6474 : 12 : && sym->result->ts.type != BT_UNKNOWN
6475 : 10 : && sym->result->attr.proc_pointer)
6476 : 10 : e->ts = sym->result->ts;
6477 : : else
6478 : : {
6479 : : /* Must be a simple variable reference. */
6480 : 630 : if (!gfc_set_default_type (sym, 1, sym->ns))
6481 : : return false;
6482 : 504 : e->ts = sym->ts;
6483 : : }
6484 : :
6485 : 1274977 : if (check_assumed_size_reference (sym, e))
6486 : : return false;
6487 : :
6488 : : /* Deal with forward references to entries during gfc_resolve_code, to
6489 : : satisfy, at least partially, 12.5.2.5. */
6490 : 1274958 : if (gfc_current_ns->entries
6491 : 3060 : && current_entry_id == sym->entry_id
6492 : 1000 : && cs_base
6493 : 914 : && cs_base->current
6494 : 914 : && cs_base->current->op != EXEC_ENTRY)
6495 : : {
6496 : 914 : gfc_entry_list *entry;
6497 : 914 : gfc_formal_arglist *formal;
6498 : 914 : int n;
6499 : 914 : bool seen, saved_specification_expr;
6500 : :
6501 : : /* If the symbol is a dummy... */
6502 : 914 : if (sym->attr.dummy && sym->ns == gfc_current_ns)
6503 : : {
6504 : : entry = gfc_current_ns->entries;
6505 : : seen = false;
6506 : :
6507 : : /* ...test if the symbol is a parameter of previous entries. */
6508 : 1033 : for (; entry && entry->id <= current_entry_id; entry = entry->next)
6509 : 1006 : for (formal = entry->sym->formal; formal; formal = formal->next)
6510 : : {
6511 : 997 : if (formal->sym && sym->name == formal->sym->name)
6512 : : {
6513 : : seen = true;
6514 : : break;
6515 : : }
6516 : : }
6517 : :
6518 : : /* If it has not been seen as a dummy, this is an error. */
6519 : 453 : if (!seen)
6520 : : {
6521 : 3 : if (specification_expr)
6522 : 2 : gfc_error ("Variable %qs, used in a specification expression"
6523 : : ", is referenced at %L before the ENTRY statement "
6524 : : "in which it is a parameter",
6525 : : sym->name, &cs_base->current->loc);
6526 : : else
6527 : 1 : gfc_error ("Variable %qs is used at %L before the ENTRY "
6528 : : "statement in which it is a parameter",
6529 : : sym->name, &cs_base->current->loc);
6530 : : t = false;
6531 : : }
6532 : : }
6533 : :
6534 : : /* Now do the same check on the specification expressions. */
6535 : 914 : saved_specification_expr = specification_expr;
6536 : 914 : specification_expr = true;
6537 : 914 : if (sym->ts.type == BT_CHARACTER
6538 : 914 : && !gfc_resolve_expr (sym->ts.u.cl->length))
6539 : : t = false;
6540 : :
6541 : 914 : if (sym->as)
6542 : : {
6543 : 271 : for (n = 0; n < sym->as->rank; n++)
6544 : : {
6545 : 159 : if (!gfc_resolve_expr (sym->as->lower[n]))
6546 : 0 : t = false;
6547 : 159 : if (!gfc_resolve_expr (sym->as->upper[n]))
6548 : 1 : t = false;
6549 : : }
6550 : : }
6551 : 914 : specification_expr = saved_specification_expr;
6552 : :
6553 : 914 : if (t)
6554 : : /* Update the symbol's entry level. */
6555 : 909 : sym->entry_id = current_entry_id + 1;
6556 : : }
6557 : :
6558 : : /* If a symbol has been host_associated mark it. This is used latter,
6559 : : to identify if aliasing is possible via host association. */
6560 : 1274958 : if (sym->attr.flavor == FL_VARIABLE
6561 : 1239031 : && (!sym->ns->code || sym->ns->code->op != EXEC_BLOCK
6562 : 5284 : || !sym->ns->code->ext.block.assoc)
6563 : 1237569 : && gfc_current_ns->parent
6564 : 592579 : && (gfc_current_ns->parent == sym->ns
6565 : 555648 : || (gfc_current_ns->parent->parent
6566 : 10621 : && gfc_current_ns->parent->parent == sym->ns)))
6567 : 43284 : sym->attr.host_assoc = 1;
6568 : :
6569 : 1274958 : if (gfc_current_ns->proc_name
6570 : 1271587 : && sym->attr.dimension
6571 : 347338 : && (sym->ns != gfc_current_ns
6572 : 323760 : || sym->attr.use_assoc
6573 : 319809 : || sym->attr.in_common))
6574 : 32317 : gfc_current_ns->proc_name->attr.array_outer_dependency = 1;
6575 : :
6576 : 1287252 : resolve_procedure:
6577 : 1287252 : if (t && !resolve_procedure_expression (e))
6578 : : t = false;
6579 : :
6580 : : /* F2008, C617 and C1229. */
6581 : 1286225 : if (!inquiry_argument && (e->ts.type == BT_CLASS || e->ts.type == BT_DERIVED)
6582 : 1379708 : && gfc_is_coindexed (e))
6583 : : {
6584 : 304 : gfc_ref *ref, *ref2 = NULL;
6585 : :
6586 : 383 : for (ref = e->ref; ref; ref = ref->next)
6587 : : {
6588 : 383 : if (ref->type == REF_COMPONENT)
6589 : 79 : ref2 = ref;
6590 : 383 : if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
6591 : : break;
6592 : : }
6593 : :
6594 : 608 : for ( ; ref; ref = ref->next)
6595 : 316 : if (ref->type == REF_COMPONENT)
6596 : : break;
6597 : :
6598 : : /* Expression itself is not coindexed object. */
6599 : 304 : if (ref && e->ts.type == BT_CLASS)
6600 : : {
6601 : 3 : gfc_error ("Polymorphic subobject of coindexed object at %L",
6602 : : &e->where);
6603 : 3 : t = false;
6604 : : }
6605 : :
6606 : : /* Expression itself is coindexed object. */
6607 : 292 : if (ref == NULL)
6608 : : {
6609 : 292 : gfc_component *c;
6610 : 292 : c = ref2 ? ref2->u.c.component : e->symtree->n.sym->components;
6611 : 408 : for ( ; c; c = c->next)
6612 : 116 : if (c->attr.allocatable && c->ts.type == BT_CLASS)
6613 : : {
6614 : 0 : gfc_error ("Coindexed object with polymorphic allocatable "
6615 : : "subcomponent at %L", &e->where);
6616 : 0 : t = false;
6617 : 0 : break;
6618 : : }
6619 : : }
6620 : : }
6621 : :
6622 : 1287252 : if (t)
6623 : 1287244 : gfc_expression_rank (e);
6624 : :
6625 : 1287252 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym != sym->result)
6626 : 3 : gfc_warning (OPT_Wdeprecated_declarations,
6627 : : "Using variable %qs at %L is deprecated",
6628 : : sym->name, &e->where);
6629 : : /* Simplify cases where access to a parameter array results in a
6630 : : single constant. Suppress errors since those will have been
6631 : : issued before, as warnings. */
6632 : 1287252 : if (e->rank == 0 && sym->as && sym->attr.flavor == FL_PARAMETER)
6633 : : {
6634 : 2593 : gfc_push_suppress_errors ();
6635 : 2593 : gfc_simplify_expr (e, 1);
6636 : 2593 : gfc_pop_suppress_errors ();
6637 : : }
6638 : :
6639 : : return t;
6640 : : }
6641 : :
6642 : :
6643 : : /* 'sym' was initially guessed to be derived type but has been corrected
6644 : : in resolve_assoc_var to be a class entity or the derived type correcting.
6645 : : If a class entity it will certainly need the _data reference or the
6646 : : reference derived type symbol correcting in the first component ref if
6647 : : a derived type. */
6648 : :
6649 : : void
6650 : 844 : gfc_fixup_inferred_type_refs (gfc_expr *e)
6651 : : {
6652 : 844 : gfc_ref *ref, *new_ref;
6653 : 844 : gfc_symbol *sym, *derived;
6654 : 844 : gfc_expr *target;
6655 : 844 : sym = e->symtree->n.sym;
6656 : :
6657 : : /* An associate_name whose selector is (i) a component ref of a selector
6658 : : that is a inferred type associate_name; or (ii) an intrinsic type that
6659 : : has been inferred from an inquiry ref. */
6660 : 844 : if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
6661 : : {
6662 : 282 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
6663 : 282 : sym->attr.codimension = sym->assoc->target->corank ? 1 : 0;
6664 : 282 : if (!sym->attr.dimension && e->ref->type == REF_ARRAY)
6665 : : {
6666 : 60 : ref = e->ref;
6667 : : /* A substring misidentified as an array section. */
6668 : 60 : if (sym->ts.type == BT_CHARACTER
6669 : 30 : && ref->u.ar.start[0] && ref->u.ar.end[0]
6670 : 6 : && !ref->u.ar.stride[0])
6671 : : {
6672 : 6 : new_ref = gfc_get_ref ();
6673 : 6 : new_ref->type = REF_SUBSTRING;
6674 : 6 : new_ref->u.ss.start = ref->u.ar.start[0];
6675 : 6 : new_ref->u.ss.end = ref->u.ar.end[0];
6676 : 6 : new_ref->u.ss.length = sym->ts.u.cl;
6677 : 6 : *ref = *new_ref;
6678 : 6 : free (new_ref);
6679 : : }
6680 : : else
6681 : : {
6682 : 54 : if (e->ref->u.ar.type == AR_UNKNOWN)
6683 : 24 : gfc_error ("Invalid array reference at %L", &e->where);
6684 : 54 : e->ref = ref->next;
6685 : 54 : free (ref);
6686 : : }
6687 : : }
6688 : :
6689 : : /* It is possible for an inquiry reference to be mistaken for a
6690 : : component reference. Correct this now. */
6691 : 282 : ref = e->ref;
6692 : 282 : if (ref && ref->type == REF_ARRAY)
6693 : 138 : ref = ref->next;
6694 : 150 : if (ref && ref->type == REF_COMPONENT
6695 : 150 : && is_inquiry_ref (ref->u.c.component->name, &new_ref))
6696 : : {
6697 : 12 : e->symtree->n.sym = sym;
6698 : 12 : *ref = *new_ref;
6699 : 12 : gfc_free_ref_list (new_ref);
6700 : : }
6701 : :
6702 : : /* The kind of the associate name is best evaluated directly from the
6703 : : selector because of the guesses made in primary.cc, when the type
6704 : : is still unknown. */
6705 : 282 : if (ref && ref->type == REF_INQUIRY && ref->u.i == INQUIRY_KIND)
6706 : : {
6707 : 24 : gfc_expr *ne = gfc_get_int_expr (gfc_default_integer_kind, &e->where,
6708 : 12 : sym->assoc->target->ts.kind);
6709 : 12 : gfc_replace_expr (e, ne);
6710 : : }
6711 : :
6712 : : /* Now that the references are all sorted out, set the expression rank
6713 : : and return. */
6714 : 282 : gfc_expression_rank (e);
6715 : 282 : return;
6716 : : }
6717 : :
6718 : 562 : derived = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->ts.u.derived
6719 : : : sym->ts.u.derived;
6720 : :
6721 : : /* Ensure that class symbols have an array spec and ensure that there
6722 : : is a _data field reference following class type references. */
6723 : 562 : if (sym->ts.type == BT_CLASS
6724 : 196 : && sym->assoc->target->ts.type == BT_CLASS)
6725 : : {
6726 : 196 : e->rank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->rank : 0;
6727 : 196 : e->corank = CLASS_DATA (sym)->as ? CLASS_DATA (sym)->as->corank : 0;
6728 : 196 : sym->attr.dimension = 0;
6729 : 196 : sym->attr.codimension = 0;
6730 : 196 : CLASS_DATA (sym)->attr.dimension = e->rank ? 1 : 0;
6731 : 196 : CLASS_DATA (sym)->attr.codimension = e->corank ? 1 : 0;
6732 : 196 : if (e->ref && (e->ref->type != REF_COMPONENT
6733 : 160 : || e->ref->u.c.component->name[0] != '_'))
6734 : : {
6735 : 82 : ref = gfc_get_ref ();
6736 : 82 : ref->type = REF_COMPONENT;
6737 : 82 : ref->next = e->ref;
6738 : 82 : e->ref = ref;
6739 : 82 : ref->u.c.component = gfc_find_component (sym->ts.u.derived, "_data",
6740 : : true, true, NULL);
6741 : 82 : ref->u.c.sym = sym->ts.u.derived;
6742 : : }
6743 : : }
6744 : :
6745 : : /* Proceed as far as the first component reference and ensure that the
6746 : : correct derived type is being used. */
6747 : 825 : for (ref = e->ref; ref; ref = ref->next)
6748 : 789 : if (ref->type == REF_COMPONENT)
6749 : : {
6750 : 526 : if (ref->u.c.component->name[0] != '_')
6751 : 330 : ref->u.c.sym = derived;
6752 : : else
6753 : 196 : ref->u.c.sym = sym->ts.u.derived;
6754 : : break;
6755 : : }
6756 : :
6757 : : /* Verify that the type inferrence mechanism has not introduced a spurious
6758 : : array reference. This can happen with an associate name, whose selector
6759 : : is an element of another inferred type. */
6760 : 562 : target = e->symtree->n.sym->assoc->target;
6761 : 562 : if (!(sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as)
6762 : 150 : && e != target && !target->rank)
6763 : : {
6764 : : /* First case: array ref after the scalar class or derived
6765 : : associate_name. */
6766 : 150 : if (e->ref && e->ref->type == REF_ARRAY
6767 : 7 : && e->ref->u.ar.type != AR_ELEMENT)
6768 : : {
6769 : 7 : ref = e->ref;
6770 : 7 : if (ref->u.ar.type == AR_UNKNOWN)
6771 : 1 : gfc_error ("Invalid array reference at %L", &e->where);
6772 : 7 : e->ref = ref->next;
6773 : 7 : free (ref);
6774 : :
6775 : : /* If it hasn't a ref to the '_data' field supply one. */
6776 : 7 : if (sym->ts.type == BT_CLASS
6777 : 0 : && !(e->ref->type == REF_COMPONENT
6778 : 0 : && strcmp (e->ref->u.c.component->name, "_data")))
6779 : : {
6780 : 0 : gfc_ref *new_ref;
6781 : 0 : gfc_find_component (e->symtree->n.sym->ts.u.derived,
6782 : : "_data", true, true, &new_ref);
6783 : 0 : new_ref->next = e->ref;
6784 : 0 : e->ref = new_ref;
6785 : : }
6786 : : }
6787 : : /* 2nd case: a ref to the '_data' field followed by an array ref. */
6788 : 143 : else if (e->ref && e->ref->type == REF_COMPONENT
6789 : 143 : && strcmp (e->ref->u.c.component->name, "_data") == 0
6790 : 64 : && e->ref->next && e->ref->next->type == REF_ARRAY
6791 : 0 : && e->ref->next->u.ar.type != AR_ELEMENT)
6792 : : {
6793 : 0 : ref = e->ref->next;
6794 : 0 : if (ref->u.ar.type == AR_UNKNOWN)
6795 : 0 : gfc_error ("Invalid array reference at %L", &e->where);
6796 : 0 : e->ref->next = e->ref->next->next;
6797 : 0 : free (ref);
6798 : : }
6799 : : }
6800 : :
6801 : : /* Now that all the references are OK, get the expression rank. */
6802 : 562 : gfc_expression_rank (e);
6803 : : }
6804 : :
6805 : :
6806 : : /* Checks to see that the correct symbol has been host associated.
6807 : : The only situations where this arises are:
6808 : : (i) That in which a twice contained function is parsed after
6809 : : the host association is made. On detecting this, change
6810 : : the symbol in the expression and convert the array reference
6811 : : into an actual arglist if the old symbol is a variable; or
6812 : : (ii) That in which an external function is typed but not declared
6813 : : explicitly to be external. Here, the old symbol is changed
6814 : : from a variable to an external function. */
6815 : : static bool
6816 : 1623385 : check_host_association (gfc_expr *e)
6817 : : {
6818 : 1623385 : gfc_symbol *sym, *old_sym;
6819 : 1623385 : gfc_symtree *st;
6820 : 1623385 : int n;
6821 : 1623385 : gfc_ref *ref;
6822 : 1623385 : gfc_actual_arglist *arg, *tail = NULL;
6823 : 1623385 : bool retval = e->expr_type == EXPR_FUNCTION;
6824 : :
6825 : : /* If the expression is the result of substitution in
6826 : : interface.cc(gfc_extend_expr) because there is no way in
6827 : : which the host association can be wrong. */
6828 : 1623385 : if (e->symtree == NULL
6829 : 1622649 : || e->symtree->n.sym == NULL
6830 : 1622649 : || e->user_operator)
6831 : : return retval;
6832 : :
6833 : 1620939 : old_sym = e->symtree->n.sym;
6834 : :
6835 : 1620939 : if (gfc_current_ns->parent
6836 : 713309 : && old_sym->ns != gfc_current_ns)
6837 : : {
6838 : : /* Use the 'USE' name so that renamed module symbols are
6839 : : correctly handled. */
6840 : 87057 : gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
6841 : :
6842 : 87057 : if (sym && old_sym != sym
6843 : 488 : && sym->attr.flavor == FL_PROCEDURE
6844 : 93 : && sym->attr.contained)
6845 : : {
6846 : : /* Clear the shape, since it might not be valid. */
6847 : 71 : gfc_free_shape (&e->shape, e->rank);
6848 : :
6849 : : /* Give the expression the right symtree! */
6850 : 71 : gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
6851 : 71 : gcc_assert (st != NULL);
6852 : :
6853 : 71 : if (old_sym->attr.flavor == FL_PROCEDURE
6854 : 47 : || e->expr_type == EXPR_FUNCTION)
6855 : : {
6856 : : /* Original was function so point to the new symbol, since
6857 : : the actual argument list is already attached to the
6858 : : expression. */
6859 : 30 : e->value.function.esym = NULL;
6860 : 30 : e->symtree = st;
6861 : : }
6862 : : else
6863 : : {
6864 : : /* Original was variable so convert array references into
6865 : : an actual arglist. This does not need any checking now
6866 : : since resolve_function will take care of it. */
6867 : 41 : e->value.function.actual = NULL;
6868 : 41 : e->expr_type = EXPR_FUNCTION;
6869 : 41 : e->symtree = st;
6870 : :
6871 : : /* Ambiguity will not arise if the array reference is not
6872 : : the last reference. */
6873 : 43 : for (ref = e->ref; ref; ref = ref->next)
6874 : 38 : if (ref->type == REF_ARRAY && ref->next == NULL)
6875 : : break;
6876 : :
6877 : 41 : if ((ref == NULL || ref->type != REF_ARRAY)
6878 : 5 : && sym->attr.proc == PROC_INTERNAL)
6879 : : {
6880 : 4 : gfc_error ("%qs at %L is host associated at %L into "
6881 : : "a contained procedure with an internal "
6882 : : "procedure of the same name", sym->name,
6883 : : &old_sym->declared_at, &e->where);
6884 : 4 : return false;
6885 : : }
6886 : :
6887 : 1 : if (ref == NULL)
6888 : : return false;
6889 : :
6890 : 36 : gcc_assert (ref->type == REF_ARRAY);
6891 : :
6892 : : /* Grab the start expressions from the array ref and
6893 : : copy them into actual arguments. */
6894 : 84 : for (n = 0; n < ref->u.ar.dimen; n++)
6895 : : {
6896 : 48 : arg = gfc_get_actual_arglist ();
6897 : 48 : arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
6898 : 48 : if (e->value.function.actual == NULL)
6899 : 36 : tail = e->value.function.actual = arg;
6900 : : else
6901 : : {
6902 : 12 : tail->next = arg;
6903 : 12 : tail = arg;
6904 : : }
6905 : : }
6906 : :
6907 : : /* Dump the reference list and set the rank. */
6908 : 36 : gfc_free_ref_list (e->ref);
6909 : 36 : e->ref = NULL;
6910 : 36 : e->rank = sym->as ? sym->as->rank : 0;
6911 : 36 : e->corank = sym->as ? sym->as->corank : 0;
6912 : : }
6913 : :
6914 : 66 : gfc_resolve_expr (e);
6915 : 66 : sym->refs++;
6916 : : }
6917 : : /* This case corresponds to a call, from a block or a contained
6918 : : procedure, to an external function, which has not been declared
6919 : : as being external in the main program but has been typed. */
6920 : 86986 : else if (sym && old_sym != sym
6921 : 417 : && !e->ref
6922 : 255 : && sym->ts.type == BT_UNKNOWN
6923 : 21 : && old_sym->ts.type != BT_UNKNOWN
6924 : 19 : && sym->attr.flavor == FL_PROCEDURE
6925 : 19 : && old_sym->attr.flavor == FL_VARIABLE
6926 : 7 : && sym->ns->parent == old_sym->ns
6927 : 7 : && sym->ns->proc_name
6928 : 7 : && sym->ns->proc_name->attr.proc != PROC_MODULE
6929 : 6 : && (sym->ns->proc_name->attr.flavor == FL_LABEL
6930 : 6 : || sym->ns->proc_name->attr.flavor == FL_PROCEDURE))
6931 : : {
6932 : 6 : old_sym->attr.flavor = FL_PROCEDURE;
6933 : 6 : old_sym->attr.external = 1;
6934 : 6 : old_sym->attr.function = 1;
6935 : 6 : old_sym->result = old_sym;
6936 : 6 : gfc_resolve_expr (e);
6937 : : }
6938 : : }
6939 : : /* This might have changed! */
6940 : 1620934 : return e->expr_type == EXPR_FUNCTION;
6941 : : }
6942 : :
6943 : :
6944 : : static void
6945 : 1328 : gfc_resolve_character_operator (gfc_expr *e)
6946 : : {
6947 : 1328 : gfc_expr *op1 = e->value.op.op1;
6948 : 1328 : gfc_expr *op2 = e->value.op.op2;
6949 : 1328 : gfc_expr *e1 = NULL;
6950 : 1328 : gfc_expr *e2 = NULL;
6951 : :
6952 : 1328 : gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
6953 : :
6954 : 1328 : if (op1->ts.u.cl && op1->ts.u.cl->length)
6955 : 653 : e1 = gfc_copy_expr (op1->ts.u.cl->length);
6956 : 675 : else if (op1->expr_type == EXPR_CONSTANT)
6957 : 268 : e1 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6958 : 268 : op1->value.character.length);
6959 : :
6960 : 1328 : if (op2->ts.u.cl && op2->ts.u.cl->length)
6961 : 641 : e2 = gfc_copy_expr (op2->ts.u.cl->length);
6962 : 687 : else if (op2->expr_type == EXPR_CONSTANT)
6963 : 457 : e2 = gfc_get_int_expr (gfc_charlen_int_kind, NULL,
6964 : 457 : op2->value.character.length);
6965 : :
6966 : 1328 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
6967 : :
6968 : 1328 : if (!e1 || !e2)
6969 : : {
6970 : 535 : gfc_free_expr (e1);
6971 : 535 : gfc_free_expr (e2);
6972 : :
6973 : 535 : return;
6974 : : }
6975 : :
6976 : 793 : e->ts.u.cl->length = gfc_add (e1, e2);
6977 : 793 : e->ts.u.cl->length->ts.type = BT_INTEGER;
6978 : 793 : e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
6979 : 793 : gfc_simplify_expr (e->ts.u.cl->length, 0);
6980 : 793 : gfc_resolve_expr (e->ts.u.cl->length);
6981 : :
6982 : 793 : return;
6983 : : }
6984 : :
6985 : :
6986 : : /* Ensure that an character expression has a charlen and, if possible, a
6987 : : length expression. */
6988 : :
6989 : : static void
6990 : 175643 : fixup_charlen (gfc_expr *e)
6991 : : {
6992 : : /* The cases fall through so that changes in expression type and the need
6993 : : for multiple fixes are picked up. In all circumstances, a charlen should
6994 : : be available for the middle end to hang a backend_decl on. */
6995 : 175643 : switch (e->expr_type)
6996 : : {
6997 : 1328 : case EXPR_OP:
6998 : 1328 : gfc_resolve_character_operator (e);
6999 : : /* FALLTHRU */
7000 : :
7001 : 1383 : case EXPR_ARRAY:
7002 : 1383 : if (e->expr_type == EXPR_ARRAY)
7003 : 55 : gfc_resolve_character_array_constructor (e);
7004 : : /* FALLTHRU */
7005 : :
7006 : 1839 : case EXPR_SUBSTRING:
7007 : 1839 : if (!e->ts.u.cl && e->ref)
7008 : 452 : gfc_resolve_substring_charlen (e);
7009 : : /* FALLTHRU */
7010 : :
7011 : 175643 : default:
7012 : 175643 : if (!e->ts.u.cl)
7013 : 173808 : e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
7014 : :
7015 : 175643 : break;
7016 : : }
7017 : 175643 : }
7018 : :
7019 : :
7020 : : /* Update an actual argument to include the passed-object for type-bound
7021 : : procedures at the right position. */
7022 : :
7023 : : static gfc_actual_arglist*
7024 : 2794 : update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
7025 : : const char *name)
7026 : : {
7027 : 2818 : gcc_assert (argpos > 0);
7028 : :
7029 : 2818 : if (argpos == 1)
7030 : : {
7031 : 2681 : gfc_actual_arglist* result;
7032 : :
7033 : 2681 : result = gfc_get_actual_arglist ();
7034 : 2681 : result->expr = po;
7035 : 2681 : result->next = lst;
7036 : 2681 : if (name)
7037 : 502 : result->name = name;
7038 : :
7039 : 2681 : return result;
7040 : : }
7041 : :
7042 : 137 : if (lst)
7043 : 113 : lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
7044 : : else
7045 : 24 : lst = update_arglist_pass (NULL, po, argpos - 1, name);
7046 : : return lst;
7047 : : }
7048 : :
7049 : :
7050 : : /* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
7051 : :
7052 : : static gfc_expr*
7053 : 6877 : extract_compcall_passed_object (gfc_expr* e)
7054 : : {
7055 : 6877 : gfc_expr* po;
7056 : :
7057 : 6877 : if (e->expr_type == EXPR_UNKNOWN)
7058 : : {
7059 : 0 : gfc_error ("Error in typebound call at %L",
7060 : : &e->where);
7061 : 0 : return NULL;
7062 : : }
7063 : :
7064 : 6877 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7065 : :
7066 : 6877 : if (e->value.compcall.base_object)
7067 : 1494 : po = gfc_copy_expr (e->value.compcall.base_object);
7068 : : else
7069 : : {
7070 : 5383 : po = gfc_get_expr ();
7071 : 5383 : po->expr_type = EXPR_VARIABLE;
7072 : 5383 : po->symtree = e->symtree;
7073 : 5383 : po->ref = gfc_copy_ref (e->ref);
7074 : 5383 : po->where = e->where;
7075 : : }
7076 : :
7077 : 6877 : if (!gfc_resolve_expr (po))
7078 : : return NULL;
7079 : :
7080 : : return po;
7081 : : }
7082 : :
7083 : :
7084 : : /* Update the arglist of an EXPR_COMPCALL expression to include the
7085 : : passed-object. */
7086 : :
7087 : : static bool
7088 : 3197 : update_compcall_arglist (gfc_expr* e)
7089 : : {
7090 : 3197 : gfc_expr* po;
7091 : 3197 : gfc_typebound_proc* tbp;
7092 : :
7093 : 3197 : tbp = e->value.compcall.tbp;
7094 : :
7095 : 3197 : if (tbp->error)
7096 : : return false;
7097 : :
7098 : 3196 : po = extract_compcall_passed_object (e);
7099 : 3196 : if (!po)
7100 : : return false;
7101 : :
7102 : 3196 : if (tbp->nopass || e->value.compcall.ignore_pass)
7103 : : {
7104 : 1071 : gfc_free_expr (po);
7105 : 1071 : return true;
7106 : : }
7107 : :
7108 : 2125 : if (tbp->pass_arg_num <= 0)
7109 : : return false;
7110 : :
7111 : 2124 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7112 : : tbp->pass_arg_num,
7113 : : tbp->pass_arg);
7114 : :
7115 : 2124 : return true;
7116 : : }
7117 : :
7118 : :
7119 : : /* Extract the passed object from a PPC call (a copy of it). */
7120 : :
7121 : : static gfc_expr*
7122 : 85 : extract_ppc_passed_object (gfc_expr *e)
7123 : : {
7124 : 85 : gfc_expr *po;
7125 : 85 : gfc_ref **ref;
7126 : :
7127 : 85 : po = gfc_get_expr ();
7128 : 85 : po->expr_type = EXPR_VARIABLE;
7129 : 85 : po->symtree = e->symtree;
7130 : 85 : po->ref = gfc_copy_ref (e->ref);
7131 : 85 : po->where = e->where;
7132 : :
7133 : : /* Remove PPC reference. */
7134 : 85 : ref = &po->ref;
7135 : 91 : while ((*ref)->next)
7136 : 6 : ref = &(*ref)->next;
7137 : 85 : gfc_free_ref_list (*ref);
7138 : 85 : *ref = NULL;
7139 : :
7140 : 85 : if (!gfc_resolve_expr (po))
7141 : 0 : return NULL;
7142 : :
7143 : : return po;
7144 : : }
7145 : :
7146 : :
7147 : : /* Update the actual arglist of a procedure pointer component to include the
7148 : : passed-object. */
7149 : :
7150 : : static bool
7151 : 399 : update_ppc_arglist (gfc_expr* e)
7152 : : {
7153 : 399 : gfc_expr* po;
7154 : 399 : gfc_component *ppc;
7155 : 399 : gfc_typebound_proc* tb;
7156 : :
7157 : 399 : ppc = gfc_get_proc_ptr_comp (e);
7158 : 399 : if (!ppc)
7159 : : return false;
7160 : :
7161 : 399 : tb = ppc->tb;
7162 : :
7163 : 399 : if (tb->error)
7164 : : return false;
7165 : 397 : else if (tb->nopass)
7166 : : return true;
7167 : :
7168 : 85 : po = extract_ppc_passed_object (e);
7169 : 85 : if (!po)
7170 : : return false;
7171 : :
7172 : : /* F08:R739. */
7173 : 85 : if (po->rank != 0)
7174 : : {
7175 : 0 : gfc_error ("Passed-object at %L must be scalar", &e->where);
7176 : 0 : return false;
7177 : : }
7178 : :
7179 : : /* F08:C611. */
7180 : 85 : if (po->ts.type == BT_DERIVED && po->ts.u.derived->attr.abstract)
7181 : : {
7182 : 1 : gfc_error ("Base object for procedure-pointer component call at %L is of"
7183 : : " ABSTRACT type %qs", &e->where, po->ts.u.derived->name);
7184 : 1 : return false;
7185 : : }
7186 : :
7187 : 84 : gcc_assert (tb->pass_arg_num > 0);
7188 : 84 : e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
7189 : : tb->pass_arg_num,
7190 : : tb->pass_arg);
7191 : :
7192 : 84 : return true;
7193 : : }
7194 : :
7195 : :
7196 : : /* Check that the object a TBP is called on is valid, i.e. it must not be
7197 : : of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
7198 : :
7199 : : static bool
7200 : 3208 : check_typebound_baseobject (gfc_expr* e)
7201 : : {
7202 : 3208 : gfc_expr* base;
7203 : 3208 : bool return_value = false;
7204 : :
7205 : 3208 : base = extract_compcall_passed_object (e);
7206 : 3208 : if (!base)
7207 : : return false;
7208 : :
7209 : 3205 : if (base->ts.type != BT_DERIVED && base->ts.type != BT_CLASS)
7210 : : {
7211 : 1 : gfc_error ("Error in typebound call at %L", &e->where);
7212 : 1 : goto cleanup;
7213 : : }
7214 : :
7215 : 3204 : if (base->ts.type == BT_CLASS && !gfc_expr_attr (base).class_ok)
7216 : 1 : return false;
7217 : :
7218 : : /* F08:C611. */
7219 : 3203 : if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
7220 : : {
7221 : 3 : gfc_error ("Base object for type-bound procedure call at %L is of"
7222 : : " ABSTRACT type %qs", &e->where, base->ts.u.derived->name);
7223 : 3 : goto cleanup;
7224 : : }
7225 : :
7226 : : /* F08:C1230. If the procedure called is NOPASS,
7227 : : the base object must be scalar. */
7228 : 3200 : if (e->value.compcall.tbp->nopass && base->rank != 0)
7229 : : {
7230 : 1 : gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
7231 : : " be scalar", &e->where);
7232 : 1 : goto cleanup;
7233 : : }
7234 : :
7235 : : return_value = true;
7236 : :
7237 : 3204 : cleanup:
7238 : 3204 : gfc_free_expr (base);
7239 : 3204 : return return_value;
7240 : : }
7241 : :
7242 : :
7243 : : /* Resolve a call to a type-bound procedure, either function or subroutine,
7244 : : statically from the data in an EXPR_COMPCALL expression. The adapted
7245 : : arglist and the target-procedure symtree are returned. */
7246 : :
7247 : : static bool
7248 : 3197 : resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
7249 : : gfc_actual_arglist** actual)
7250 : : {
7251 : 3197 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7252 : 3197 : gcc_assert (!e->value.compcall.tbp->is_generic);
7253 : :
7254 : : /* Update the actual arglist for PASS. */
7255 : 3197 : if (!update_compcall_arglist (e))
7256 : : return false;
7257 : :
7258 : 3195 : *actual = e->value.compcall.actual;
7259 : 3195 : *target = e->value.compcall.tbp->u.specific;
7260 : :
7261 : 3195 : gfc_free_ref_list (e->ref);
7262 : 3195 : e->ref = NULL;
7263 : 3195 : e->value.compcall.actual = NULL;
7264 : :
7265 : : /* If we find a deferred typebound procedure, check for derived types
7266 : : that an overriding typebound procedure has not been missed. */
7267 : 3195 : if (e->value.compcall.name
7268 : 3195 : && !e->value.compcall.tbp->non_overridable
7269 : 3179 : && e->value.compcall.base_object
7270 : 747 : && e->value.compcall.base_object->ts.type == BT_DERIVED)
7271 : : {
7272 : 460 : gfc_symtree *st;
7273 : 460 : gfc_symbol *derived;
7274 : :
7275 : : /* Use the derived type of the base_object. */
7276 : 460 : derived = e->value.compcall.base_object->ts.u.derived;
7277 : 460 : st = NULL;
7278 : :
7279 : : /* If necessary, go through the inheritance chain. */
7280 : 1381 : while (!st && derived)
7281 : : {
7282 : : /* Look for the typebound procedure 'name'. */
7283 : 461 : if (derived->f2k_derived && derived->f2k_derived->tb_sym_root)
7284 : 460 : st = gfc_find_symtree (derived->f2k_derived->tb_sym_root,
7285 : : e->value.compcall.name);
7286 : 461 : if (!st)
7287 : 1 : derived = gfc_get_derived_super_type (derived);
7288 : : }
7289 : :
7290 : : /* Now find the specific name in the derived type namespace. */
7291 : 460 : if (st && st->n.tb && st->n.tb->u.specific)
7292 : 460 : gfc_find_sym_tree (st->n.tb->u.specific->name,
7293 : 460 : derived->ns, 1, &st);
7294 : 460 : if (st)
7295 : 460 : *target = st;
7296 : : }
7297 : :
7298 : 3195 : if (is_illegal_recursion ((*target)->n.sym, gfc_current_ns)
7299 : 3195 : && !e->value.compcall.tbp->deferred)
7300 : 1 : gfc_warning (0, "Non-RECURSIVE procedure %qs at %L is possibly calling"
7301 : : " itself recursively. Declare it RECURSIVE or use"
7302 : : " %<-frecursive%>", (*target)->n.sym->name, &e->where);
7303 : :
7304 : : return true;
7305 : : }
7306 : :
7307 : :
7308 : : /* Get the ultimate declared type from an expression. In addition,
7309 : : return the last class/derived type reference and the copy of the
7310 : : reference list. If check_types is set true, derived types are
7311 : : identified as well as class references. */
7312 : : static gfc_symbol*
7313 : 3143 : get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
7314 : : gfc_expr *e, bool check_types)
7315 : : {
7316 : 3143 : gfc_symbol *declared;
7317 : 3143 : gfc_ref *ref;
7318 : :
7319 : 3143 : declared = NULL;
7320 : 3143 : if (class_ref)
7321 : 2772 : *class_ref = NULL;
7322 : 3143 : if (new_ref)
7323 : 2485 : *new_ref = gfc_copy_ref (e->ref);
7324 : :
7325 : 3912 : for (ref = e->ref; ref; ref = ref->next)
7326 : : {
7327 : 769 : if (ref->type != REF_COMPONENT)
7328 : 283 : continue;
7329 : :
7330 : 486 : if ((ref->u.c.component->ts.type == BT_CLASS
7331 : 240 : || (check_types && gfc_bt_struct (ref->u.c.component->ts.type)))
7332 : 411 : && ref->u.c.component->attr.flavor != FL_PROCEDURE)
7333 : : {
7334 : 337 : declared = ref->u.c.component->ts.u.derived;
7335 : 337 : if (class_ref)
7336 : 319 : *class_ref = ref;
7337 : : }
7338 : : }
7339 : :
7340 : 3143 : if (declared == NULL)
7341 : 2832 : declared = e->symtree->n.sym->ts.u.derived;
7342 : :
7343 : 3143 : return declared;
7344 : : }
7345 : :
7346 : :
7347 : : /* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
7348 : : which of the specific bindings (if any) matches the arglist and transform
7349 : : the expression into a call of that binding. */
7350 : :
7351 : : static bool
7352 : 3199 : resolve_typebound_generic_call (gfc_expr* e, const char **name)
7353 : : {
7354 : 3199 : gfc_typebound_proc* genproc;
7355 : 3199 : const char* genname;
7356 : 3199 : gfc_symtree *st;
7357 : 3199 : gfc_symbol *derived;
7358 : :
7359 : 3199 : gcc_assert (e->expr_type == EXPR_COMPCALL);
7360 : 3199 : genname = e->value.compcall.name;
7361 : 3199 : genproc = e->value.compcall.tbp;
7362 : :
7363 : 3199 : if (!genproc->is_generic)
7364 : : return true;
7365 : :
7366 : : /* Try the bindings on this type and in the inheritance hierarchy. */
7367 : 383 : for (; genproc; genproc = genproc->overridden)
7368 : : {
7369 : 381 : gfc_tbp_generic* g;
7370 : :
7371 : 381 : gcc_assert (genproc->is_generic);
7372 : 569 : for (g = genproc->u.generic; g; g = g->next)
7373 : : {
7374 : 559 : gfc_symbol* target;
7375 : 559 : gfc_actual_arglist* args;
7376 : 559 : bool matches;
7377 : :
7378 : 559 : gcc_assert (g->specific);
7379 : :
7380 : 559 : if (g->specific->error)
7381 : 0 : continue;
7382 : :
7383 : 559 : target = g->specific->u.specific->n.sym;
7384 : :
7385 : : /* Get the right arglist by handling PASS/NOPASS. */
7386 : 559 : args = gfc_copy_actual_arglist (e->value.compcall.actual);
7387 : 559 : if (!g->specific->nopass)
7388 : : {
7389 : 473 : gfc_expr* po;
7390 : 473 : po = extract_compcall_passed_object (e);
7391 : 473 : if (!po)
7392 : : {
7393 : 0 : gfc_free_actual_arglist (args);
7394 : 0 : return false;
7395 : : }
7396 : :
7397 : 473 : gcc_assert (g->specific->pass_arg_num > 0);
7398 : 473 : gcc_assert (!g->specific->error);
7399 : 473 : args = update_arglist_pass (args, po, g->specific->pass_arg_num,
7400 : : g->specific->pass_arg);
7401 : : }
7402 : 559 : resolve_actual_arglist (args, target->attr.proc,
7403 : 559 : is_external_proc (target)
7404 : 559 : && gfc_sym_get_dummy_args (target) == NULL);
7405 : :
7406 : : /* Check if this arglist matches the formal. */
7407 : 559 : matches = gfc_arglist_matches_symbol (&args, target);
7408 : :
7409 : : /* Clean up and break out of the loop if we've found it. */
7410 : 559 : gfc_free_actual_arglist (args);
7411 : 559 : if (matches)
7412 : : {
7413 : 371 : e->value.compcall.tbp = g->specific;
7414 : 371 : genname = g->specific_st->name;
7415 : : /* Pass along the name for CLASS methods, where the vtab
7416 : : procedure pointer component has to be referenced. */
7417 : 371 : if (name)
7418 : 159 : *name = genname;
7419 : 371 : goto success;
7420 : : }
7421 : : }
7422 : : }
7423 : :
7424 : : /* Nothing matching found! */
7425 : 2 : gfc_error ("Found no matching specific binding for the call to the GENERIC"
7426 : : " %qs at %L", genname, &e->where);
7427 : 2 : return false;
7428 : :
7429 : 371 : success:
7430 : : /* Make sure that we have the right specific instance for the name. */
7431 : 371 : derived = get_declared_from_expr (NULL, NULL, e, true);
7432 : :
7433 : 371 : st = gfc_find_typebound_proc (derived, NULL, genname, true, &e->where);
7434 : 371 : if (st)
7435 : 371 : e->value.compcall.tbp = st->n.tb;
7436 : :
7437 : : return true;
7438 : : }
7439 : :
7440 : :
7441 : : /* Resolve a call to a type-bound subroutine. */
7442 : :
7443 : : static bool
7444 : 1675 : resolve_typebound_call (gfc_code* c, const char **name, bool *overridable)
7445 : : {
7446 : 1675 : gfc_actual_arglist* newactual;
7447 : 1675 : gfc_symtree* target;
7448 : :
7449 : : /* Check that's really a SUBROUTINE. */
7450 : 1675 : if (!c->expr1->value.compcall.tbp->subroutine)
7451 : : {
7452 : 17 : if (!c->expr1->value.compcall.tbp->is_generic
7453 : 15 : && c->expr1->value.compcall.tbp->u.specific
7454 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym
7455 : 15 : && c->expr1->value.compcall.tbp->u.specific->n.sym->attr.subroutine)
7456 : 12 : c->expr1->value.compcall.tbp->subroutine = 1;
7457 : : else
7458 : : {
7459 : 5 : gfc_error ("%qs at %L should be a SUBROUTINE",
7460 : : c->expr1->value.compcall.name, &c->loc);
7461 : 5 : return false;
7462 : : }
7463 : : }
7464 : :
7465 : 1670 : if (!check_typebound_baseobject (c->expr1))
7466 : : return false;
7467 : :
7468 : : /* Pass along the name for CLASS methods, where the vtab
7469 : : procedure pointer component has to be referenced. */
7470 : 1663 : if (name)
7471 : 473 : *name = c->expr1->value.compcall.name;
7472 : :
7473 : 1663 : if (!resolve_typebound_generic_call (c->expr1, name))
7474 : : return false;
7475 : :
7476 : : /* Pass along the NON_OVERRIDABLE attribute of the specific TBP. */
7477 : 1662 : if (overridable)
7478 : 370 : *overridable = !c->expr1->value.compcall.tbp->non_overridable;
7479 : :
7480 : : /* Transform into an ordinary EXEC_CALL for now. */
7481 : :
7482 : 1662 : if (!resolve_typebound_static (c->expr1, &target, &newactual))
7483 : : return false;
7484 : :
7485 : 1660 : c->ext.actual = newactual;
7486 : 1660 : c->symtree = target;
7487 : 1660 : c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
7488 : :
7489 : 1660 : gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7490 : :
7491 : 1660 : gfc_free_expr (c->expr1);
7492 : 1660 : c->expr1 = gfc_get_expr ();
7493 : 1660 : c->expr1->expr_type = EXPR_FUNCTION;
7494 : 1660 : c->expr1->symtree = target;
7495 : 1660 : c->expr1->where = c->loc;
7496 : :
7497 : 1660 : return resolve_call (c);
7498 : : }
7499 : :
7500 : :
7501 : : /* Resolve a component-call expression. */
7502 : : static bool
7503 : 1557 : resolve_compcall (gfc_expr* e, const char **name)
7504 : : {
7505 : 1557 : gfc_actual_arglist* newactual;
7506 : 1557 : gfc_symtree* target;
7507 : :
7508 : : /* Check that's really a FUNCTION. */
7509 : 1557 : if (!e->value.compcall.tbp->function)
7510 : : {
7511 : 19 : if (e->symtree && e->symtree->n.sym->resolve_symbol_called)
7512 : 5 : gfc_error ("%qs at %L should be a FUNCTION", e->value.compcall.name,
7513 : : &e->where);
7514 : 19 : return false;
7515 : : }
7516 : :
7517 : :
7518 : : /* These must not be assign-calls! */
7519 : 1538 : gcc_assert (!e->value.compcall.assign);
7520 : :
7521 : 1538 : if (!check_typebound_baseobject (e))
7522 : : return false;
7523 : :
7524 : : /* Pass along the name for CLASS methods, where the vtab
7525 : : procedure pointer component has to be referenced. */
7526 : 1536 : if (name)
7527 : 858 : *name = e->value.compcall.name;
7528 : :
7529 : 1536 : if (!resolve_typebound_generic_call (e, name))
7530 : : return false;
7531 : 1535 : gcc_assert (!e->value.compcall.tbp->is_generic);
7532 : :
7533 : : /* Take the rank from the function's symbol. */
7534 : 1535 : if (e->value.compcall.tbp->u.specific->n.sym->as)
7535 : : {
7536 : 149 : e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
7537 : 149 : e->corank = e->value.compcall.tbp->u.specific->n.sym->as->corank;
7538 : : }
7539 : :
7540 : : /* For now, we simply transform it into an EXPR_FUNCTION call with the same
7541 : : arglist to the TBP's binding target. */
7542 : :
7543 : 1535 : if (!resolve_typebound_static (e, &target, &newactual))
7544 : : return false;
7545 : :
7546 : 1535 : e->value.function.actual = newactual;
7547 : 1535 : e->value.function.name = NULL;
7548 : 1535 : e->value.function.esym = target->n.sym;
7549 : 1535 : e->value.function.isym = NULL;
7550 : 1535 : e->symtree = target;
7551 : 1535 : e->ts = target->n.sym->ts;
7552 : 1535 : e->expr_type = EXPR_FUNCTION;
7553 : :
7554 : : /* Resolution is not necessary if this is a class subroutine; this
7555 : : function only has to identify the specific proc. Resolution of
7556 : : the call will be done next in resolve_typebound_call. */
7557 : 1535 : return gfc_resolve_expr (e);
7558 : : }
7559 : :
7560 : :
7561 : : static bool resolve_fl_derived (gfc_symbol *sym);
7562 : :
7563 : :
7564 : : /* Resolve a typebound function, or 'method'. First separate all
7565 : : the non-CLASS references by calling resolve_compcall directly. */
7566 : :
7567 : : static bool
7568 : 1557 : resolve_typebound_function (gfc_expr* e)
7569 : : {
7570 : 1557 : gfc_symbol *declared;
7571 : 1557 : gfc_component *c;
7572 : 1557 : gfc_ref *new_ref;
7573 : 1557 : gfc_ref *class_ref;
7574 : 1557 : gfc_symtree *st;
7575 : 1557 : const char *name;
7576 : 1557 : gfc_typespec ts;
7577 : 1557 : gfc_expr *expr;
7578 : 1557 : bool overridable;
7579 : :
7580 : 1557 : st = e->symtree;
7581 : :
7582 : : /* Deal with typebound operators for CLASS objects. */
7583 : 1557 : expr = e->value.compcall.base_object;
7584 : 1557 : overridable = !e->value.compcall.tbp->non_overridable;
7585 : 1557 : if (expr && expr->ts.type == BT_CLASS && e->value.compcall.name)
7586 : : {
7587 : : /* Since the typebound operators are generic, we have to ensure
7588 : : that any delays in resolution are corrected and that the vtab
7589 : : is present. */
7590 : 184 : ts = expr->ts;
7591 : 184 : declared = ts.u.derived;
7592 : 184 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7593 : 184 : if (c->ts.u.derived == NULL)
7594 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7595 : :
7596 : 184 : if (!resolve_compcall (e, &name))
7597 : : return false;
7598 : :
7599 : : /* Use the generic name if it is there. */
7600 : 184 : name = name ? name : e->value.function.esym->name;
7601 : 184 : e->symtree = expr->symtree;
7602 : 184 : e->ref = gfc_copy_ref (expr->ref);
7603 : 184 : get_declared_from_expr (&class_ref, NULL, e, false);
7604 : :
7605 : : /* Trim away the extraneous references that emerge from nested
7606 : : use of interface.cc (extend_expr). */
7607 : 184 : if (class_ref && class_ref->next)
7608 : : {
7609 : 0 : gfc_free_ref_list (class_ref->next);
7610 : 0 : class_ref->next = NULL;
7611 : : }
7612 : 184 : else if (e->ref && !class_ref && expr->ts.type != BT_CLASS)
7613 : : {
7614 : 0 : gfc_free_ref_list (e->ref);
7615 : 0 : e->ref = NULL;
7616 : : }
7617 : :
7618 : 184 : gfc_add_vptr_component (e);
7619 : 184 : gfc_add_component_ref (e, name);
7620 : 184 : e->value.function.esym = NULL;
7621 : 184 : if (expr->expr_type != EXPR_VARIABLE)
7622 : 80 : e->base_expr = expr;
7623 : 184 : return true;
7624 : : }
7625 : :
7626 : 1373 : if (st == NULL)
7627 : 147 : return resolve_compcall (e, NULL);
7628 : :
7629 : 1226 : if (!gfc_resolve_ref (e))
7630 : : return false;
7631 : :
7632 : : /* Get the CLASS declared type. */
7633 : 1226 : declared = get_declared_from_expr (&class_ref, &new_ref, e, true);
7634 : :
7635 : 1226 : if (!resolve_fl_derived (declared))
7636 : : return false;
7637 : :
7638 : : /* Weed out cases of the ultimate component being a derived type. */
7639 : 1226 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7640 : 1142 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7641 : : {
7642 : 538 : gfc_free_ref_list (new_ref);
7643 : 538 : return resolve_compcall (e, NULL);
7644 : : }
7645 : :
7646 : 688 : c = gfc_find_component (declared, "_data", true, true, NULL);
7647 : :
7648 : : /* Treat the call as if it is a typebound procedure, in order to roll
7649 : : out the correct name for the specific function. */
7650 : 688 : if (!resolve_compcall (e, &name))
7651 : : {
7652 : 15 : gfc_free_ref_list (new_ref);
7653 : 15 : return false;
7654 : : }
7655 : 673 : ts = e->ts;
7656 : :
7657 : 673 : if (overridable)
7658 : : {
7659 : : /* Convert the expression to a procedure pointer component call. */
7660 : 671 : e->value.function.esym = NULL;
7661 : 671 : e->symtree = st;
7662 : :
7663 : 671 : if (new_ref)
7664 : 124 : e->ref = new_ref;
7665 : :
7666 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7667 : 671 : gfc_add_vptr_component (e);
7668 : 671 : gfc_add_component_ref (e, name);
7669 : :
7670 : : /* Recover the typespec for the expression. This is really only
7671 : : necessary for generic procedures, where the additional call
7672 : : to gfc_add_component_ref seems to throw the collection of the
7673 : : correct typespec. */
7674 : 671 : e->ts = ts;
7675 : : }
7676 : 2 : else if (new_ref)
7677 : 0 : gfc_free_ref_list (new_ref);
7678 : :
7679 : : return true;
7680 : : }
7681 : :
7682 : : /* Resolve a typebound subroutine, or 'method'. First separate all
7683 : : the non-CLASS references by calling resolve_typebound_call
7684 : : directly. */
7685 : :
7686 : : static bool
7687 : 1675 : resolve_typebound_subroutine (gfc_code *code)
7688 : : {
7689 : 1675 : gfc_symbol *declared;
7690 : 1675 : gfc_component *c;
7691 : 1675 : gfc_ref *new_ref;
7692 : 1675 : gfc_ref *class_ref;
7693 : 1675 : gfc_symtree *st;
7694 : 1675 : const char *name;
7695 : 1675 : gfc_typespec ts;
7696 : 1675 : gfc_expr *expr;
7697 : 1675 : bool overridable;
7698 : :
7699 : 1675 : st = code->expr1->symtree;
7700 : :
7701 : : /* Deal with typebound operators for CLASS objects. */
7702 : 1675 : expr = code->expr1->value.compcall.base_object;
7703 : 1675 : overridable = !code->expr1->value.compcall.tbp->non_overridable;
7704 : 1675 : if (expr && expr->ts.type == BT_CLASS && code->expr1->value.compcall.name)
7705 : : {
7706 : : /* If the base_object is not a variable, the corresponding actual
7707 : : argument expression must be stored in e->base_expression so
7708 : : that the corresponding tree temporary can be used as the base
7709 : : object in gfc_conv_procedure_call. */
7710 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7711 : : {
7712 : : gfc_actual_arglist *args;
7713 : :
7714 : : args= code->expr1->value.function.actual;
7715 : : for (; args; args = args->next)
7716 : : if (expr == args->expr)
7717 : : expr = args->expr;
7718 : : }
7719 : :
7720 : : /* Since the typebound operators are generic, we have to ensure
7721 : : that any delays in resolution are corrected and that the vtab
7722 : : is present. */
7723 : 103 : declared = expr->ts.u.derived;
7724 : 103 : c = gfc_find_component (declared, "_vptr", true, true, NULL);
7725 : 103 : if (c->ts.u.derived == NULL)
7726 : 0 : c->ts.u.derived = gfc_find_derived_vtab (declared);
7727 : :
7728 : 103 : if (!resolve_typebound_call (code, &name, NULL))
7729 : : return false;
7730 : :
7731 : : /* Use the generic name if it is there. */
7732 : 103 : name = name ? name : code->expr1->value.function.esym->name;
7733 : 103 : code->expr1->symtree = expr->symtree;
7734 : 103 : code->expr1->ref = gfc_copy_ref (expr->ref);
7735 : :
7736 : : /* Trim away the extraneous references that emerge from nested
7737 : : use of interface.cc (extend_expr). */
7738 : 103 : get_declared_from_expr (&class_ref, NULL, code->expr1, false);
7739 : 103 : if (class_ref && class_ref->next)
7740 : : {
7741 : 0 : gfc_free_ref_list (class_ref->next);
7742 : 0 : class_ref->next = NULL;
7743 : : }
7744 : 103 : else if (code->expr1->ref && !class_ref)
7745 : : {
7746 : 12 : gfc_free_ref_list (code->expr1->ref);
7747 : 12 : code->expr1->ref = NULL;
7748 : : }
7749 : :
7750 : : /* Now use the procedure in the vtable. */
7751 : 103 : gfc_add_vptr_component (code->expr1);
7752 : 103 : gfc_add_component_ref (code->expr1, name);
7753 : 103 : code->expr1->value.function.esym = NULL;
7754 : 103 : if (expr->expr_type != EXPR_VARIABLE)
7755 : 0 : code->expr1->base_expr = expr;
7756 : 103 : return true;
7757 : : }
7758 : :
7759 : 1572 : if (st == NULL)
7760 : 313 : return resolve_typebound_call (code, NULL, NULL);
7761 : :
7762 : 1259 : if (!gfc_resolve_ref (code->expr1))
7763 : : return false;
7764 : :
7765 : : /* Get the CLASS declared type. */
7766 : 1259 : get_declared_from_expr (&class_ref, &new_ref, code->expr1, true);
7767 : :
7768 : : /* Weed out cases of the ultimate component being a derived type. */
7769 : 1259 : if ((class_ref && gfc_bt_struct (class_ref->u.c.component->ts.type))
7770 : 1196 : || (!class_ref && st->n.sym->ts.type != BT_CLASS))
7771 : : {
7772 : 884 : gfc_free_ref_list (new_ref);
7773 : 884 : return resolve_typebound_call (code, NULL, NULL);
7774 : : }
7775 : :
7776 : 375 : if (!resolve_typebound_call (code, &name, &overridable))
7777 : : {
7778 : 5 : gfc_free_ref_list (new_ref);
7779 : 5 : return false;
7780 : : }
7781 : 370 : ts = code->expr1->ts;
7782 : :
7783 : 370 : if (overridable)
7784 : : {
7785 : : /* Convert the expression to a procedure pointer component call. */
7786 : 368 : code->expr1->value.function.esym = NULL;
7787 : 368 : code->expr1->symtree = st;
7788 : :
7789 : 368 : if (new_ref)
7790 : 92 : code->expr1->ref = new_ref;
7791 : :
7792 : : /* '_vptr' points to the vtab, which contains the procedure pointers. */
7793 : 368 : gfc_add_vptr_component (code->expr1);
7794 : 368 : gfc_add_component_ref (code->expr1, name);
7795 : :
7796 : : /* Recover the typespec for the expression. This is really only
7797 : : necessary for generic procedures, where the additional call
7798 : : to gfc_add_component_ref seems to throw the collection of the
7799 : : correct typespec. */
7800 : 368 : code->expr1->ts = ts;
7801 : : }
7802 : 2 : else if (new_ref)
7803 : 0 : gfc_free_ref_list (new_ref);
7804 : :
7805 : : return true;
7806 : : }
7807 : :
7808 : :
7809 : : /* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
7810 : :
7811 : : static bool
7812 : 123 : resolve_ppc_call (gfc_code* c)
7813 : : {
7814 : 123 : gfc_component *comp;
7815 : :
7816 : 123 : comp = gfc_get_proc_ptr_comp (c->expr1);
7817 : 123 : gcc_assert (comp != NULL);
7818 : :
7819 : 123 : c->resolved_sym = c->expr1->symtree->n.sym;
7820 : 123 : c->expr1->expr_type = EXPR_VARIABLE;
7821 : :
7822 : 123 : if (!comp->attr.subroutine)
7823 : 1 : gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
7824 : :
7825 : 123 : if (!gfc_resolve_ref (c->expr1))
7826 : : return false;
7827 : :
7828 : 123 : if (!update_ppc_arglist (c->expr1))
7829 : : return false;
7830 : :
7831 : 122 : c->ext.actual = c->expr1->value.compcall.actual;
7832 : :
7833 : 122 : if (!resolve_actual_arglist (c->ext.actual, comp->attr.proc,
7834 : 122 : !(comp->ts.interface
7835 : 93 : && comp->ts.interface->formal)))
7836 : : return false;
7837 : :
7838 : 122 : if (!pure_subroutine (comp->ts.interface, comp->name, &c->expr1->where))
7839 : : return false;
7840 : :
7841 : 121 : gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
7842 : :
7843 : 121 : return true;
7844 : : }
7845 : :
7846 : :
7847 : : /* Resolve a Function Call to a Procedure Pointer Component (Function). */
7848 : :
7849 : : static bool
7850 : 276 : resolve_expr_ppc (gfc_expr* e)
7851 : : {
7852 : 276 : gfc_component *comp;
7853 : :
7854 : 276 : comp = gfc_get_proc_ptr_comp (e);
7855 : 276 : gcc_assert (comp != NULL);
7856 : :
7857 : : /* Convert to EXPR_FUNCTION. */
7858 : 276 : e->expr_type = EXPR_FUNCTION;
7859 : 276 : e->value.function.isym = NULL;
7860 : 276 : e->value.function.actual = e->value.compcall.actual;
7861 : 276 : e->ts = comp->ts;
7862 : 276 : if (comp->as != NULL)
7863 : : {
7864 : 28 : e->rank = comp->as->rank;
7865 : 28 : e->corank = comp->as->corank;
7866 : : }
7867 : :
7868 : 276 : if (!comp->attr.function)
7869 : 3 : gfc_add_function (&comp->attr, comp->name, &e->where);
7870 : :
7871 : 276 : if (!gfc_resolve_ref (e))
7872 : : return false;
7873 : :
7874 : 276 : if (!resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
7875 : 276 : !(comp->ts.interface
7876 : 275 : && comp->ts.interface->formal)))
7877 : : return false;
7878 : :
7879 : 276 : if (!update_ppc_arglist (e))
7880 : : return false;
7881 : :
7882 : 274 : if (!check_pure_function(e))
7883 : : return false;
7884 : :
7885 : 273 : gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
7886 : :
7887 : 273 : return true;
7888 : : }
7889 : :
7890 : :
7891 : : static bool
7892 : 11621 : gfc_is_expandable_expr (gfc_expr *e)
7893 : : {
7894 : 11621 : gfc_constructor *con;
7895 : :
7896 : 11621 : if (e->expr_type == EXPR_ARRAY)
7897 : : {
7898 : : /* Traverse the constructor looking for variables that are flavor
7899 : : parameter. Parameters must be expanded since they are fully used at
7900 : : compile time. */
7901 : 11621 : con = gfc_constructor_first (e->value.constructor);
7902 : 30946 : for (; con; con = gfc_constructor_next (con))
7903 : : {
7904 : 13301 : if (con->expr->expr_type == EXPR_VARIABLE
7905 : 4928 : && con->expr->symtree
7906 : 4928 : && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
7907 : 4873 : || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
7908 : : return true;
7909 : 8373 : if (con->expr->expr_type == EXPR_ARRAY
7910 : 8373 : && gfc_is_expandable_expr (con->expr))
7911 : : return true;
7912 : : }
7913 : : }
7914 : :
7915 : : return false;
7916 : : }
7917 : :
7918 : :
7919 : : /* Sometimes variables in specification expressions of the result
7920 : : of module procedures in submodules wind up not being the 'real'
7921 : : dummy. Find this, if possible, in the namespace of the first
7922 : : formal argument. */
7923 : :
7924 : : static void
7925 : 3340 : fixup_unique_dummy (gfc_expr *e)
7926 : : {
7927 : 3340 : gfc_symtree *st = NULL;
7928 : 3340 : gfc_symbol *s = NULL;
7929 : :
7930 : 3340 : if (e->symtree->n.sym->ns->proc_name
7931 : 3340 : && e->symtree->n.sym->ns->proc_name->formal)
7932 : 3340 : s = e->symtree->n.sym->ns->proc_name->formal->sym;
7933 : :
7934 : 3340 : if (s != NULL)
7935 : 3340 : st = gfc_find_symtree (s->ns->sym_root, e->symtree->n.sym->name);
7936 : :
7937 : 3340 : if (st != NULL
7938 : 14 : && st->n.sym != NULL
7939 : 14 : && st->n.sym->attr.dummy)
7940 : 14 : e->symtree = st;
7941 : 3340 : }
7942 : :
7943 : :
7944 : : /* Resolve an expression. That is, make sure that types of operands agree
7945 : : with their operators, intrinsic operators are converted to function calls
7946 : : for overloaded types and unresolved function references are resolved. */
7947 : :
7948 : : bool
7949 : 6928926 : gfc_resolve_expr (gfc_expr *e)
7950 : : {
7951 : 6928926 : bool t;
7952 : 6928926 : bool inquiry_save, actual_arg_save, first_actual_arg_save;
7953 : :
7954 : 6928926 : if (e == NULL || e->do_not_resolve_again)
7955 : : return true;
7956 : :
7957 : : /* inquiry_argument only applies to variables. */
7958 : 5065301 : inquiry_save = inquiry_argument;
7959 : 5065301 : actual_arg_save = actual_arg;
7960 : 5065301 : first_actual_arg_save = first_actual_arg;
7961 : :
7962 : 5065301 : if (e->expr_type != EXPR_VARIABLE)
7963 : : {
7964 : 3777170 : inquiry_argument = false;
7965 : 3777170 : actual_arg = false;
7966 : 3777170 : first_actual_arg = false;
7967 : : }
7968 : 1288131 : else if (e->symtree != NULL
7969 : 1287712 : && *e->symtree->name == '@'
7970 : 4047 : && e->symtree->n.sym->attr.dummy)
7971 : : {
7972 : : /* Deal with submodule specification expressions that are not
7973 : : found to be referenced in module.cc(read_cleanup). */
7974 : 3340 : fixup_unique_dummy (e);
7975 : : }
7976 : :
7977 : 5065301 : switch (e->expr_type)
7978 : : {
7979 : 517508 : case EXPR_OP:
7980 : 517508 : t = resolve_operator (e);
7981 : 517508 : break;
7982 : :
7983 : 1623385 : case EXPR_FUNCTION:
7984 : 1623385 : case EXPR_VARIABLE:
7985 : :
7986 : 1623385 : if (check_host_association (e))
7987 : 335290 : t = resolve_function (e);
7988 : : else
7989 : 1288095 : t = resolve_variable (e);
7990 : :
7991 : 1623385 : if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
7992 : 6854 : && e->ref->type != REF_SUBSTRING)
7993 : 2143 : gfc_resolve_substring_charlen (e);
7994 : :
7995 : : break;
7996 : :
7997 : 1557 : case EXPR_COMPCALL:
7998 : 1557 : t = resolve_typebound_function (e);
7999 : 1557 : break;
8000 : :
8001 : 507 : case EXPR_SUBSTRING:
8002 : 507 : t = gfc_resolve_ref (e);
8003 : 507 : break;
8004 : :
8005 : : case EXPR_CONSTANT:
8006 : : case EXPR_NULL:
8007 : : t = true;
8008 : : break;
8009 : :
8010 : 276 : case EXPR_PPC:
8011 : 276 : t = resolve_expr_ppc (e);
8012 : 276 : break;
8013 : :
8014 : 67411 : case EXPR_ARRAY:
8015 : 67411 : t = false;
8016 : 67411 : if (!gfc_resolve_ref (e))
8017 : : break;
8018 : :
8019 : 67411 : t = gfc_resolve_array_constructor (e);
8020 : : /* Also try to expand a constructor. */
8021 : 67411 : if (t)
8022 : : {
8023 : 67315 : gfc_expression_rank (e);
8024 : 67315 : if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
8025 : 62364 : gfc_expand_constructor (e, false);
8026 : : }
8027 : :
8028 : : /* This provides the opportunity for the length of constructors with
8029 : : character valued function elements to propagate the string length
8030 : : to the expression. */
8031 : 67315 : if (t && e->ts.type == BT_CHARACTER)
8032 : : {
8033 : : /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
8034 : : here rather then add a duplicate test for it above. */
8035 : 9869 : gfc_expand_constructor (e, false);
8036 : 9869 : t = gfc_resolve_character_array_constructor (e);
8037 : : }
8038 : :
8039 : : break;
8040 : :
8041 : 15907 : case EXPR_STRUCTURE:
8042 : 15907 : t = gfc_resolve_ref (e);
8043 : 15907 : if (!t)
8044 : : break;
8045 : :
8046 : 15907 : t = resolve_structure_cons (e, 0);
8047 : 15907 : if (!t)
8048 : : break;
8049 : :
8050 : 15895 : t = gfc_simplify_expr (e, 0);
8051 : 15895 : break;
8052 : :
8053 : 0 : default:
8054 : 0 : gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
8055 : : }
8056 : :
8057 : 5065301 : if (e->ts.type == BT_CHARACTER && t && !e->ts.u.cl)
8058 : 175643 : fixup_charlen (e);
8059 : :
8060 : 5065301 : inquiry_argument = inquiry_save;
8061 : 5065301 : actual_arg = actual_arg_save;
8062 : 5065301 : first_actual_arg = first_actual_arg_save;
8063 : :
8064 : : /* For some reason, resolving these expressions a second time mangles
8065 : : the typespec of the expression itself. */
8066 : 5065301 : if (t && e->expr_type == EXPR_VARIABLE
8067 : 1285282 : && e->symtree->n.sym->attr.select_rank_temporary
8068 : 3422 : && UNLIMITED_POLY (e->symtree->n.sym))
8069 : 83 : e->do_not_resolve_again = 1;
8070 : :
8071 : 5062845 : if (t && gfc_current_ns->import_state != IMPORT_NOT_SET)
8072 : 6918 : t = check_import_status (e);
8073 : :
8074 : : return t;
8075 : : }
8076 : :
8077 : :
8078 : : /* Resolve an expression from an iterator. They must be scalar and have
8079 : : INTEGER or (optionally) REAL type. */
8080 : :
8081 : : static bool
8082 : 148625 : gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
8083 : : const char *name_msgid)
8084 : : {
8085 : 148625 : if (!gfc_resolve_expr (expr))
8086 : : return false;
8087 : :
8088 : 148620 : if (expr->rank != 0)
8089 : : {
8090 : 0 : gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
8091 : 0 : return false;
8092 : : }
8093 : :
8094 : 148620 : if (expr->ts.type != BT_INTEGER)
8095 : : {
8096 : 274 : if (expr->ts.type == BT_REAL)
8097 : : {
8098 : 274 : if (real_ok)
8099 : 271 : return gfc_notify_std (GFC_STD_F95_DEL,
8100 : : "%s at %L must be integer",
8101 : 271 : _(name_msgid), &expr->where);
8102 : : else
8103 : : {
8104 : 3 : gfc_error ("%s at %L must be INTEGER", _(name_msgid),
8105 : : &expr->where);
8106 : 3 : return false;
8107 : : }
8108 : : }
8109 : : else
8110 : : {
8111 : 0 : gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
8112 : 0 : return false;
8113 : : }
8114 : : }
8115 : : return true;
8116 : : }
8117 : :
8118 : :
8119 : : /* Resolve the expressions in an iterator structure. If REAL_OK is
8120 : : false allow only INTEGER type iterators, otherwise allow REAL types.
8121 : : Set own_scope to true for ac-implied-do and data-implied-do as those
8122 : : have a separate scope such that, e.g., a INTENT(IN) doesn't apply. */
8123 : :
8124 : : bool
8125 : 37165 : gfc_resolve_iterator (gfc_iterator *iter, bool real_ok, bool own_scope)
8126 : : {
8127 : 37165 : if (!gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable"))
8128 : : return false;
8129 : :
8130 : 37161 : if (!gfc_check_vardef_context (iter->var, false, false, own_scope,
8131 : 37161 : _("iterator variable")))
8132 : : return false;
8133 : :
8134 : 37155 : if (!gfc_resolve_iterator_expr (iter->start, real_ok,
8135 : : "Start expression in DO loop"))
8136 : : return false;
8137 : :
8138 : 37154 : if (!gfc_resolve_iterator_expr (iter->end, real_ok,
8139 : : "End expression in DO loop"))
8140 : : return false;
8141 : :
8142 : 37151 : if (!gfc_resolve_iterator_expr (iter->step, real_ok,
8143 : : "Step expression in DO loop"))
8144 : : return false;
8145 : :
8146 : : /* Convert start, end, and step to the same type as var. */
8147 : 37150 : if (iter->start->ts.kind != iter->var->ts.kind
8148 : 36870 : || iter->start->ts.type != iter->var->ts.type)
8149 : 315 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8150 : :
8151 : 37150 : if (iter->end->ts.kind != iter->var->ts.kind
8152 : 36897 : || iter->end->ts.type != iter->var->ts.type)
8153 : 278 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8154 : :
8155 : 37150 : if (iter->step->ts.kind != iter->var->ts.kind
8156 : 36906 : || iter->step->ts.type != iter->var->ts.type)
8157 : 280 : gfc_convert_type (iter->step, &iter->var->ts, 1);
8158 : :
8159 : 37150 : if (iter->step->expr_type == EXPR_CONSTANT)
8160 : : {
8161 : 36028 : if ((iter->step->ts.type == BT_INTEGER
8162 : 35945 : && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
8163 : 71971 : || (iter->step->ts.type == BT_REAL
8164 : 83 : && mpfr_sgn (iter->step->value.real) == 0))
8165 : : {
8166 : 3 : gfc_error ("Step expression in DO loop at %L cannot be zero",
8167 : 3 : &iter->step->where);
8168 : 3 : return false;
8169 : : }
8170 : : }
8171 : :
8172 : 37147 : if (iter->start->expr_type == EXPR_CONSTANT
8173 : 34099 : && iter->end->expr_type == EXPR_CONSTANT
8174 : 26735 : && iter->step->expr_type == EXPR_CONSTANT)
8175 : : {
8176 : 26468 : int sgn, cmp;
8177 : 26468 : if (iter->start->ts.type == BT_INTEGER)
8178 : : {
8179 : 26414 : sgn = mpz_cmp_ui (iter->step->value.integer, 0);
8180 : 26414 : cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
8181 : : }
8182 : : else
8183 : : {
8184 : 54 : sgn = mpfr_sgn (iter->step->value.real);
8185 : 54 : cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
8186 : : }
8187 : 26468 : if (warn_zerotrip && ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0)))
8188 : 146 : gfc_warning (OPT_Wzerotrip,
8189 : : "DO loop at %L will be executed zero times",
8190 : 146 : &iter->step->where);
8191 : : }
8192 : :
8193 : 37147 : if (iter->end->expr_type == EXPR_CONSTANT
8194 : 27100 : && iter->end->ts.type == BT_INTEGER
8195 : 27046 : && iter->step->expr_type == EXPR_CONSTANT
8196 : 26736 : && iter->step->ts.type == BT_INTEGER
8197 : 26736 : && (mpz_cmp_si (iter->step->value.integer, -1L) == 0
8198 : 26368 : || mpz_cmp_si (iter->step->value.integer, 1L) == 0))
8199 : : {
8200 : 25622 : bool is_step_positive = mpz_cmp_ui (iter->step->value.integer, 1) == 0;
8201 : 25622 : int k = gfc_validate_kind (BT_INTEGER, iter->end->ts.kind, false);
8202 : :
8203 : 25622 : if (is_step_positive
8204 : 25254 : && mpz_cmp (iter->end->value.integer, gfc_integer_kinds[k].huge) == 0)
8205 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8206 : : "DO loop at %L is undefined as it overflows",
8207 : 7 : &iter->step->where);
8208 : : else if (!is_step_positive
8209 : 368 : && mpz_cmp (iter->end->value.integer,
8210 : 368 : gfc_integer_kinds[k].min_int) == 0)
8211 : 7 : gfc_warning (OPT_Wundefined_do_loop,
8212 : : "DO loop at %L is undefined as it underflows",
8213 : 7 : &iter->step->where);
8214 : : }
8215 : :
8216 : : return true;
8217 : : }
8218 : :
8219 : :
8220 : : /* Traversal function for find_forall_index. f == 2 signals that
8221 : : that variable itself is not to be checked - only the references. */
8222 : :
8223 : : static bool
8224 : 41664 : forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
8225 : : {
8226 : 41664 : if (expr->expr_type != EXPR_VARIABLE)
8227 : : return false;
8228 : :
8229 : : /* A scalar assignment */
8230 : 17614 : if (!expr->ref || *f == 1)
8231 : : {
8232 : 11914 : if (expr->symtree->n.sym == sym)
8233 : : return true;
8234 : : else
8235 : : return false;
8236 : : }
8237 : :
8238 : 5700 : if (*f == 2)
8239 : 1730 : *f = 1;
8240 : : return false;
8241 : : }
8242 : :
8243 : :
8244 : : /* Check whether the FORALL index appears in the expression or not.
8245 : : Returns true if SYM is found in EXPR. */
8246 : :
8247 : : bool
8248 : 26366 : find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
8249 : : {
8250 : 26366 : if (gfc_traverse_expr (expr, sym, forall_index, f))
8251 : : return true;
8252 : : else
8253 : : return false;
8254 : : }
8255 : :
8256 : : /* Check compliance with Fortran 2023's C1133 constraint for DO CONCURRENT
8257 : : This constraint specifies rules for variables in locality-specs. */
8258 : :
8259 : : static int
8260 : 573 : do_concur_locality_specs_f2023 (gfc_expr **expr, int *walk_subtrees, void *data)
8261 : : {
8262 : 573 : struct check_default_none_data *dt = (struct check_default_none_data *) data;
8263 : :
8264 : 573 : if ((*expr)->expr_type == EXPR_VARIABLE)
8265 : : {
8266 : 18 : gfc_symbol *sym = (*expr)->symtree->n.sym;
8267 : 18 : for (gfc_expr_list *list = dt->code->ext.concur.locality[LOCALITY_LOCAL];
8268 : 20 : list; list = list->next)
8269 : : {
8270 : 2 : if (list->expr->symtree->n.sym == sym)
8271 : : {
8272 : 0 : gfc_error ("Variable %qs referenced in concurrent-header at %L "
8273 : : "must not appear in LOCAL locality-spec at %L",
8274 : : sym->name, &(*expr)->where, &list->expr->where);
8275 : 0 : *walk_subtrees = 0;
8276 : 0 : return 1;
8277 : : }
8278 : : }
8279 : : }
8280 : :
8281 : 573 : *walk_subtrees = 1;
8282 : 573 : return 0;
8283 : : }
8284 : :
8285 : : static int
8286 : 3761 : check_default_none_expr (gfc_expr **e, int *, void *data)
8287 : : {
8288 : 3761 : struct check_default_none_data *d = (struct check_default_none_data*) data;
8289 : :
8290 : 3761 : if ((*e)->expr_type == EXPR_VARIABLE)
8291 : : {
8292 : 1685 : gfc_symbol *sym = (*e)->symtree->n.sym;
8293 : :
8294 : 1685 : if (d->sym_hash->contains (sym))
8295 : 1262 : sym->mark = 1;
8296 : :
8297 : 423 : else if (d->default_none)
8298 : : {
8299 : 3 : gfc_namespace *ns2 = d->ns;
8300 : 5 : while (ns2)
8301 : : {
8302 : 3 : if (ns2 == sym->ns)
8303 : : break;
8304 : 2 : ns2 = ns2->parent;
8305 : : }
8306 : 3 : if (ns2 != NULL)
8307 : : {
8308 : 1 : gfc_error ("Variable %qs at %L not specified in a locality spec "
8309 : : "of DO CONCURRENT at %L but required due to "
8310 : : "DEFAULT (NONE)",
8311 : 1 : sym->name, &(*e)->where, &d->code->loc);
8312 : 1 : d->sym_hash->add (sym);
8313 : : }
8314 : : }
8315 : : }
8316 : 3761 : return 0;
8317 : : }
8318 : :
8319 : : static void
8320 : 171 : resolve_locality_spec (gfc_code *code, gfc_namespace *ns)
8321 : : {
8322 : 171 : struct check_default_none_data data;
8323 : 171 : data.code = code;
8324 : 171 : data.sym_hash = new hash_set<gfc_symbol *>;
8325 : 171 : data.ns = ns;
8326 : 171 : data.default_none = code->ext.concur.default_none;
8327 : :
8328 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8329 : : {
8330 : 684 : const char *name;
8331 : 684 : switch (locality)
8332 : : {
8333 : : case LOCALITY_LOCAL: name = "LOCAL"; break;
8334 : 171 : case LOCALITY_LOCAL_INIT: name = "LOCAL_INIT"; break;
8335 : 171 : case LOCALITY_SHARED: name = "SHARED"; break;
8336 : 171 : case LOCALITY_REDUCE: name = "REDUCE"; break;
8337 : : default: gcc_unreachable ();
8338 : : }
8339 : :
8340 : 1067 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8341 : 383 : list = list->next)
8342 : : {
8343 : 383 : gfc_expr *expr = list->expr;
8344 : :
8345 : 383 : if (locality == LOCALITY_REDUCE
8346 : 72 : && (expr->expr_type == EXPR_FUNCTION
8347 : 48 : || expr->expr_type == EXPR_OP))
8348 : 35 : continue;
8349 : :
8350 : 359 : if (!gfc_resolve_expr (expr))
8351 : 3 : continue;
8352 : :
8353 : 356 : if (expr->expr_type != EXPR_VARIABLE
8354 : 356 : || expr->symtree->n.sym->attr.flavor != FL_VARIABLE
8355 : 356 : || (expr->ref
8356 : 147 : && (expr->ref->type != REF_ARRAY
8357 : 147 : || expr->ref->u.ar.type != AR_FULL
8358 : 143 : || expr->ref->next)))
8359 : : {
8360 : 4 : gfc_error ("Expected variable name in %s locality spec at %L",
8361 : : name, &expr->where);
8362 : 4 : continue;
8363 : : }
8364 : :
8365 : 352 : gfc_symbol *sym = expr->symtree->n.sym;
8366 : :
8367 : 352 : if (data.sym_hash->contains (sym))
8368 : : {
8369 : 4 : gfc_error ("Variable %qs at %L has already been specified in a "
8370 : : "locality-spec", sym->name, &expr->where);
8371 : 4 : continue;
8372 : : }
8373 : :
8374 : 348 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8375 : 696 : iter; iter = iter->next)
8376 : : {
8377 : 348 : if (iter->var->symtree->n.sym == sym)
8378 : : {
8379 : 0 : gfc_error ("Index variable %qs at %L cannot be specified in a "
8380 : : "locality-spec", sym->name, &expr->where);
8381 : 0 : continue;
8382 : : }
8383 : :
8384 : 348 : data.sym_hash->add (iter->var->symtree->n.sym);
8385 : : }
8386 : :
8387 : 348 : if (locality == LOCALITY_LOCAL
8388 : 348 : || locality == LOCALITY_LOCAL_INIT
8389 : 348 : || locality == LOCALITY_REDUCE)
8390 : : {
8391 : 194 : if (sym->attr.optional)
8392 : 3 : gfc_error ("OPTIONAL attribute not permitted for %qs in %s "
8393 : : "locality-spec at %L",
8394 : : sym->name, name, &expr->where);
8395 : :
8396 : 194 : if (sym->attr.dimension
8397 : 66 : && sym->as
8398 : 66 : && sym->as->type == AS_ASSUMED_SIZE)
8399 : 0 : gfc_error ("Assumed-size array not permitted for %qs in %s "
8400 : : "locality-spec at %L",
8401 : : sym->name, name, &expr->where);
8402 : :
8403 : 194 : gfc_check_vardef_context (expr, false, false, false, name);
8404 : : }
8405 : :
8406 : 194 : if (locality == LOCALITY_LOCAL
8407 : : || locality == LOCALITY_LOCAL_INIT)
8408 : : {
8409 : 177 : symbol_attribute attr = gfc_expr_attr (expr);
8410 : :
8411 : 177 : if (attr.allocatable)
8412 : 2 : gfc_error ("ALLOCATABLE attribute not permitted for %qs in %s "
8413 : : "locality-spec at %L",
8414 : : sym->name, name, &expr->where);
8415 : :
8416 : 175 : else if (expr->ts.type == BT_CLASS && attr.dummy && !attr.pointer)
8417 : 2 : gfc_error ("Nonpointer polymorphic dummy argument not permitted"
8418 : : " for %qs in %s locality-spec at %L",
8419 : : sym->name, name, &expr->where);
8420 : :
8421 : 173 : else if (attr.codimension)
8422 : 0 : gfc_error ("Coarray not permitted for %qs in %s locality-spec "
8423 : : "at %L",
8424 : : sym->name, name, &expr->where);
8425 : :
8426 : 173 : else if (expr->ts.type == BT_DERIVED
8427 : 173 : && gfc_is_finalizable (expr->ts.u.derived, NULL))
8428 : 0 : gfc_error ("Finalizable type not permitted for %qs in %s "
8429 : : "locality-spec at %L",
8430 : : sym->name, name, &expr->where);
8431 : :
8432 : 173 : else if (gfc_has_ultimate_allocatable (expr))
8433 : 4 : gfc_error ("Type with ultimate allocatable component not "
8434 : : "permitted for %qs in %s locality-spec at %L",
8435 : : sym->name, name, &expr->where);
8436 : : }
8437 : :
8438 : 171 : else if (locality == LOCALITY_REDUCE)
8439 : : {
8440 : 17 : if (sym->attr.asynchronous)
8441 : 1 : gfc_error ("ASYNCHRONOUS attribute not permitted for %qs in "
8442 : : "REDUCE locality-spec at %L",
8443 : : sym->name, &expr->where);
8444 : 17 : if (sym->attr.volatile_)
8445 : 1 : gfc_error ("VOLATILE attribute not permitted for %qs in REDUCE "
8446 : : "locality-spec at %L", sym->name, &expr->where);
8447 : : }
8448 : :
8449 : 348 : data.sym_hash->add (sym);
8450 : : }
8451 : :
8452 : 684 : if (locality == LOCALITY_LOCAL)
8453 : : {
8454 : 171 : gcc_assert (locality == 0);
8455 : :
8456 : 171 : for (gfc_forall_iterator *iter = code->ext.concur.forall_iterator;
8457 : 353 : iter; iter = iter->next)
8458 : : {
8459 : 182 : gfc_expr_walker (&iter->start,
8460 : : do_concur_locality_specs_f2023,
8461 : : &data);
8462 : :
8463 : 182 : gfc_expr_walker (&iter->end,
8464 : : do_concur_locality_specs_f2023,
8465 : : &data);
8466 : :
8467 : 182 : gfc_expr_walker (&iter->stride,
8468 : : do_concur_locality_specs_f2023,
8469 : : &data);
8470 : : }
8471 : :
8472 : 171 : if (code->expr1)
8473 : 7 : gfc_expr_walker (&code->expr1,
8474 : : do_concur_locality_specs_f2023,
8475 : : &data);
8476 : : }
8477 : : }
8478 : :
8479 : 171 : gfc_expr *reduce_op = NULL;
8480 : :
8481 : 171 : for (gfc_expr_list *list = code->ext.concur.locality[LOCALITY_REDUCE];
8482 : 219 : list; list = list->next)
8483 : : {
8484 : 48 : gfc_expr *expr = list->expr;
8485 : :
8486 : 48 : if (expr->expr_type != EXPR_VARIABLE)
8487 : : {
8488 : 24 : reduce_op = expr;
8489 : 24 : continue;
8490 : : }
8491 : :
8492 : 24 : if (reduce_op->expr_type == EXPR_OP)
8493 : : {
8494 : 17 : switch (reduce_op->value.op.op)
8495 : : {
8496 : 17 : case INTRINSIC_PLUS:
8497 : 17 : case INTRINSIC_TIMES:
8498 : 17 : if (!gfc_numeric_ts (&expr->ts))
8499 : 3 : gfc_error ("Expected numeric type for %qs in REDUCE at %L, "
8500 : 3 : "got %s", expr->symtree->n.sym->name,
8501 : : &expr->where, gfc_basic_typename (expr->ts.type));
8502 : : break;
8503 : 0 : case INTRINSIC_AND:
8504 : 0 : case INTRINSIC_OR:
8505 : 0 : case INTRINSIC_EQV:
8506 : 0 : case INTRINSIC_NEQV:
8507 : 0 : if (expr->ts.type != BT_LOGICAL)
8508 : 0 : gfc_error ("Expected logical type for %qs in REDUCE at %L, "
8509 : 0 : "got %qs", expr->symtree->n.sym->name,
8510 : : &expr->where, gfc_basic_typename (expr->ts.type));
8511 : : break;
8512 : 0 : default:
8513 : 0 : gcc_unreachable ();
8514 : : }
8515 : : }
8516 : :
8517 : 7 : else if (reduce_op->expr_type == EXPR_FUNCTION)
8518 : : {
8519 : 7 : switch (reduce_op->value.function.isym->id)
8520 : : {
8521 : 6 : case GFC_ISYM_MIN:
8522 : 6 : case GFC_ISYM_MAX:
8523 : 6 : if (expr->ts.type != BT_INTEGER
8524 : : && expr->ts.type != BT_REAL
8525 : : && expr->ts.type != BT_CHARACTER)
8526 : 2 : gfc_error ("Expected INTEGER, REAL or CHARACTER type for %qs "
8527 : : "in REDUCE with MIN/MAX at %L, got %s",
8528 : 2 : expr->symtree->n.sym->name, &expr->where,
8529 : : gfc_basic_typename (expr->ts.type));
8530 : : break;
8531 : 1 : case GFC_ISYM_IAND:
8532 : 1 : case GFC_ISYM_IOR:
8533 : 1 : case GFC_ISYM_IEOR:
8534 : 1 : if (expr->ts.type != BT_INTEGER)
8535 : 1 : gfc_error ("Expected integer type for %qs in REDUCE with "
8536 : : "IAND/IOR/IEOR at %L, got %s",
8537 : 1 : expr->symtree->n.sym->name, &expr->where,
8538 : : gfc_basic_typename (expr->ts.type));
8539 : : break;
8540 : 0 : default:
8541 : 0 : gcc_unreachable ();
8542 : : }
8543 : : }
8544 : :
8545 : : else
8546 : 0 : gcc_unreachable ();
8547 : : }
8548 : :
8549 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8550 : : {
8551 : 1067 : for (gfc_expr_list *list = code->ext.concur.locality[locality]; list;
8552 : 383 : list = list->next)
8553 : : {
8554 : 383 : if (list->expr->expr_type == EXPR_VARIABLE)
8555 : 359 : list->expr->symtree->n.sym->mark = 0;
8556 : : }
8557 : : }
8558 : :
8559 : 171 : gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
8560 : : check_default_none_expr, &data);
8561 : :
8562 : 855 : for (int locality = 0; locality < LOCALITY_NUM; locality++)
8563 : : {
8564 : 684 : gfc_expr_list **plist = &code->ext.concur.locality[locality];
8565 : 1067 : while (*plist)
8566 : : {
8567 : 383 : gfc_expr *expr = (*plist)->expr;
8568 : 383 : if (expr->expr_type == EXPR_VARIABLE)
8569 : : {
8570 : 359 : gfc_symbol *sym = expr->symtree->n.sym;
8571 : 359 : if (sym->mark == 0)
8572 : : {
8573 : 67 : gfc_warning (OPT_Wunused_variable, "Variable %qs in "
8574 : : "locality-spec at %L is not used",
8575 : : sym->name, &expr->where);
8576 : 67 : gfc_expr_list *tmp = *plist;
8577 : 67 : *plist = (*plist)->next;
8578 : 67 : gfc_free_expr (tmp->expr);
8579 : 67 : free (tmp);
8580 : 67 : continue;
8581 : 67 : }
8582 : : }
8583 : 316 : plist = &((*plist)->next);
8584 : : }
8585 : : }
8586 : 171 : }
8587 : :
8588 : : /* Resolve a list of FORALL iterators. The FORALL index-name is constrained
8589 : : to be a scalar INTEGER variable. The subscripts and stride are scalar
8590 : : INTEGERs, and if stride is a constant it must be nonzero.
8591 : : Furthermore "A subscript or stride in a forall-triplet-spec shall
8592 : : not contain a reference to any index-name in the
8593 : : forall-triplet-spec-list in which it appears." (7.5.4.1) */
8594 : :
8595 : : static void
8596 : 2161 : resolve_forall_iterators (gfc_forall_iterator *it)
8597 : : {
8598 : 2161 : gfc_forall_iterator *iter, *iter2;
8599 : :
8600 : 6231 : for (iter = it; iter; iter = iter->next)
8601 : : {
8602 : 4070 : if (gfc_resolve_expr (iter->var)
8603 : 4070 : && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
8604 : 0 : gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
8605 : : &iter->var->where);
8606 : :
8607 : 4070 : if (gfc_resolve_expr (iter->start)
8608 : 4070 : && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
8609 : 0 : gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
8610 : : &iter->start->where);
8611 : 4070 : if (iter->var->ts.kind != iter->start->ts.kind)
8612 : 1 : gfc_convert_type (iter->start, &iter->var->ts, 1);
8613 : :
8614 : 4070 : if (gfc_resolve_expr (iter->end)
8615 : 4070 : && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
8616 : 0 : gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
8617 : : &iter->end->where);
8618 : 4070 : if (iter->var->ts.kind != iter->end->ts.kind)
8619 : 2 : gfc_convert_type (iter->end, &iter->var->ts, 1);
8620 : :
8621 : 4070 : if (gfc_resolve_expr (iter->stride))
8622 : : {
8623 : 4070 : if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
8624 : 0 : gfc_error ("FORALL stride expression at %L must be a scalar %s",
8625 : : &iter->stride->where, "INTEGER");
8626 : :
8627 : 4070 : if (iter->stride->expr_type == EXPR_CONSTANT
8628 : 4067 : && mpz_cmp_ui (iter->stride->value.integer, 0) == 0)
8629 : 1 : gfc_error ("FORALL stride expression at %L cannot be zero",
8630 : : &iter->stride->where);
8631 : : }
8632 : 4070 : if (iter->var->ts.kind != iter->stride->ts.kind)
8633 : 1 : gfc_convert_type (iter->stride, &iter->var->ts, 1);
8634 : : }
8635 : :
8636 : 6231 : for (iter = it; iter; iter = iter->next)
8637 : 10975 : for (iter2 = iter; iter2; iter2 = iter2->next)
8638 : : {
8639 : 6905 : if (find_forall_index (iter2->start, iter->var->symtree->n.sym, 0)
8640 : 6903 : || find_forall_index (iter2->end, iter->var->symtree->n.sym, 0)
8641 : 13806 : || find_forall_index (iter2->stride, iter->var->symtree->n.sym, 0))
8642 : 6 : gfc_error ("FORALL index %qs may not appear in triplet "
8643 : 6 : "specification at %L", iter->var->symtree->name,
8644 : 6 : &iter2->start->where);
8645 : : }
8646 : 2161 : }
8647 : :
8648 : :
8649 : : /* Given a pointer to a symbol that is a derived type, see if it's
8650 : : inaccessible, i.e. if it's defined in another module and the components are
8651 : : PRIVATE. The search is recursive if necessary. Returns zero if no
8652 : : inaccessible components are found, nonzero otherwise. */
8653 : :
8654 : : static bool
8655 : 1311 : derived_inaccessible (gfc_symbol *sym)
8656 : : {
8657 : 1311 : gfc_component *c;
8658 : :
8659 : 1311 : if (sym->attr.use_assoc && sym->attr.private_comp)
8660 : : return 1;
8661 : :
8662 : 3895 : for (c = sym->components; c; c = c->next)
8663 : : {
8664 : : /* Prevent an infinite loop through this function. */
8665 : 2597 : if (c->ts.type == BT_DERIVED
8666 : 282 : && (c->attr.pointer || c->attr.allocatable)
8667 : 72 : && sym == c->ts.u.derived)
8668 : 72 : continue;
8669 : :
8670 : 2525 : if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
8671 : : return 1;
8672 : : }
8673 : :
8674 : : return 0;
8675 : : }
8676 : :
8677 : :
8678 : : /* Resolve the argument of a deallocate expression. The expression must be
8679 : : a pointer or a full array. */
8680 : :
8681 : : static bool
8682 : 7872 : resolve_deallocate_expr (gfc_expr *e)
8683 : : {
8684 : 7872 : symbol_attribute attr;
8685 : 7872 : int allocatable, pointer;
8686 : 7872 : gfc_ref *ref;
8687 : 7872 : gfc_symbol *sym;
8688 : 7872 : gfc_component *c;
8689 : 7872 : bool unlimited;
8690 : :
8691 : 7872 : if (!gfc_resolve_expr (e))
8692 : : return false;
8693 : :
8694 : 7872 : if (e->expr_type != EXPR_VARIABLE)
8695 : 0 : goto bad;
8696 : :
8697 : 7872 : sym = e->symtree->n.sym;
8698 : 7872 : unlimited = UNLIMITED_POLY(sym);
8699 : :
8700 : 7872 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && CLASS_DATA (sym))
8701 : : {
8702 : 1492 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8703 : 1492 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8704 : : }
8705 : : else
8706 : : {
8707 : 6380 : allocatable = sym->attr.allocatable;
8708 : 6380 : pointer = sym->attr.pointer;
8709 : : }
8710 : 15742 : for (ref = e->ref; ref; ref = ref->next)
8711 : : {
8712 : 7870 : switch (ref->type)
8713 : : {
8714 : 5852 : case REF_ARRAY:
8715 : 5852 : if (ref->u.ar.type != AR_FULL
8716 : 6040 : && !(ref->u.ar.type == AR_ELEMENT && ref->u.ar.as->rank == 0
8717 : 188 : && ref->u.ar.codimen && gfc_ref_this_image (ref)))
8718 : : allocatable = 0;
8719 : : break;
8720 : :
8721 : 2018 : case REF_COMPONENT:
8722 : 2018 : c = ref->u.c.component;
8723 : 2018 : if (c->ts.type == BT_CLASS)
8724 : : {
8725 : 272 : allocatable = CLASS_DATA (c)->attr.allocatable;
8726 : 272 : pointer = CLASS_DATA (c)->attr.class_pointer;
8727 : : }
8728 : : else
8729 : : {
8730 : 1746 : allocatable = c->attr.allocatable;
8731 : 1746 : pointer = c->attr.pointer;
8732 : : }
8733 : : break;
8734 : :
8735 : : case REF_SUBSTRING:
8736 : : case REF_INQUIRY:
8737 : 471 : allocatable = 0;
8738 : : break;
8739 : : }
8740 : : }
8741 : :
8742 : 7872 : attr = gfc_expr_attr (e);
8743 : :
8744 : 7872 : if (allocatable == 0 && attr.pointer == 0 && !unlimited)
8745 : : {
8746 : 3 : bad:
8747 : 3 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
8748 : : &e->where);
8749 : 3 : return false;
8750 : : }
8751 : :
8752 : : /* F2008, C644. */
8753 : 7869 : if (gfc_is_coindexed (e))
8754 : : {
8755 : 1 : gfc_error ("Coindexed allocatable object at %L", &e->where);
8756 : 1 : return false;
8757 : : }
8758 : :
8759 : 7868 : if (pointer
8760 : 10150 : && !gfc_check_vardef_context (e, true, true, false,
8761 : 2282 : _("DEALLOCATE object")))
8762 : : return false;
8763 : 7866 : if (!gfc_check_vardef_context (e, false, true, false,
8764 : 7866 : _("DEALLOCATE object")))
8765 : : return false;
8766 : :
8767 : : return true;
8768 : : }
8769 : :
8770 : :
8771 : : /* Returns true if the expression e contains a reference to the symbol sym. */
8772 : : static bool
8773 : 46593 : sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
8774 : : {
8775 : 46593 : if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
8776 : 2081 : return true;
8777 : :
8778 : : return false;
8779 : : }
8780 : :
8781 : : bool
8782 : 20027 : gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
8783 : : {
8784 : 20027 : return gfc_traverse_expr (e, sym, sym_in_expr, 0);
8785 : : }
8786 : :
8787 : : /* Same as gfc_find_sym_in_expr, but do not descend into length type parameter
8788 : : of character expressions. */
8789 : : static bool
8790 : 20181 : gfc_find_var_in_expr (gfc_symbol *sym, gfc_expr *e)
8791 : : {
8792 : 0 : return gfc_traverse_expr (e, sym, sym_in_expr, -1);
8793 : : }
8794 : :
8795 : :
8796 : : /* Given the expression node e for an allocatable/pointer of derived type to be
8797 : : allocated, get the expression node to be initialized afterwards (needed for
8798 : : derived types with default initializers, and derived types with allocatable
8799 : : components that need nullification.) */
8800 : :
8801 : : gfc_expr *
8802 : 5473 : gfc_expr_to_initialize (gfc_expr *e)
8803 : : {
8804 : 5473 : gfc_expr *result;
8805 : 5473 : gfc_ref *ref;
8806 : 5473 : int i;
8807 : :
8808 : 5473 : result = gfc_copy_expr (e);
8809 : :
8810 : : /* Change the last array reference from AR_ELEMENT to AR_FULL. */
8811 : 10797 : for (ref = result->ref; ref; ref = ref->next)
8812 : 8434 : if (ref->type == REF_ARRAY && ref->next == NULL)
8813 : : {
8814 : 3110 : if (ref->u.ar.dimen == 0
8815 : 64 : && ref->u.ar.as && ref->u.ar.as->corank)
8816 : : return result;
8817 : :
8818 : 3046 : ref->u.ar.type = AR_FULL;
8819 : :
8820 : 6885 : for (i = 0; i < ref->u.ar.dimen; i++)
8821 : 3839 : ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
8822 : :
8823 : : break;
8824 : : }
8825 : :
8826 : 5409 : gfc_free_shape (&result->shape, result->rank);
8827 : :
8828 : : /* Recalculate rank, shape, etc. */
8829 : 5409 : gfc_resolve_expr (result);
8830 : 5409 : return result;
8831 : : }
8832 : :
8833 : :
8834 : : /* If the last ref of an expression is an array ref, return a copy of the
8835 : : expression with that one removed. Otherwise, a copy of the original
8836 : : expression. This is used for allocate-expressions and pointer assignment
8837 : : LHS, where there may be an array specification that needs to be stripped
8838 : : off when using gfc_check_vardef_context. */
8839 : :
8840 : : static gfc_expr*
8841 : 26808 : remove_last_array_ref (gfc_expr* e)
8842 : : {
8843 : 26808 : gfc_expr* e2;
8844 : 26808 : gfc_ref** r;
8845 : :
8846 : 26808 : e2 = gfc_copy_expr (e);
8847 : 34248 : for (r = &e2->ref; *r; r = &(*r)->next)
8848 : 23156 : if ((*r)->type == REF_ARRAY && !(*r)->next)
8849 : : {
8850 : 15716 : gfc_free_ref_list (*r);
8851 : 15716 : *r = NULL;
8852 : 15716 : break;
8853 : : }
8854 : :
8855 : 26808 : return e2;
8856 : : }
8857 : :
8858 : :
8859 : : /* Used in resolve_allocate_expr to check that a allocation-object and
8860 : : a source-expr are conformable. This does not catch all possible
8861 : : cases; in particular a runtime checking is needed. */
8862 : :
8863 : : static bool
8864 : 1835 : conformable_arrays (gfc_expr *e1, gfc_expr *e2)
8865 : : {
8866 : 1835 : gfc_ref *tail;
8867 : 1835 : bool scalar;
8868 : :
8869 : 2554 : for (tail = e2->ref; tail && tail->next; tail = tail->next);
8870 : :
8871 : : /* If MOLD= is present and is not scalar, and the allocate-object has an
8872 : : explicit-shape-spec, the ranks need not agree. This may be unintended,
8873 : : so let's emit a warning if -Wsurprising is given. */
8874 : 1835 : scalar = !tail || tail->type == REF_COMPONENT;
8875 : 1835 : if (e1->mold && e1->rank > 0
8876 : 163 : && (scalar || (tail->type == REF_ARRAY && tail->u.ar.type != AR_FULL)))
8877 : : {
8878 : 26 : if (scalar || (tail->u.ar.as && e1->rank != tail->u.ar.as->rank))
8879 : 15 : gfc_warning (OPT_Wsurprising, "Allocate-object at %L has rank %d "
8880 : : "but MOLD= expression at %L has rank %d",
8881 : 6 : &e2->where, scalar ? 0 : tail->u.ar.as->rank,
8882 : : &e1->where, e1->rank);
8883 : 29 : return true;
8884 : : }
8885 : :
8886 : : /* First compare rank. */
8887 : 1806 : if ((tail && (!tail->u.ar.as || e1->rank != tail->u.ar.as->rank))
8888 : 2 : || (!tail && e1->rank != e2->rank))
8889 : : {
8890 : 7 : gfc_error ("Source-expr at %L must be scalar or have the "
8891 : : "same rank as the allocate-object at %L",
8892 : : &e1->where, &e2->where);
8893 : 7 : return false;
8894 : : }
8895 : :
8896 : 1799 : if (e1->shape)
8897 : : {
8898 : 1312 : int i;
8899 : 1312 : mpz_t s;
8900 : :
8901 : 1312 : mpz_init (s);
8902 : :
8903 : 3040 : for (i = 0; i < e1->rank; i++)
8904 : : {
8905 : 1318 : if (tail->u.ar.start[i] == NULL)
8906 : : break;
8907 : :
8908 : 416 : if (tail->u.ar.end[i])
8909 : : {
8910 : 54 : mpz_set (s, tail->u.ar.end[i]->value.integer);
8911 : 54 : mpz_sub (s, s, tail->u.ar.start[i]->value.integer);
8912 : 54 : mpz_add_ui (s, s, 1);
8913 : : }
8914 : : else
8915 : : {
8916 : 362 : mpz_set (s, tail->u.ar.start[i]->value.integer);
8917 : : }
8918 : :
8919 : 416 : if (mpz_cmp (e1->shape[i], s) != 0)
8920 : : {
8921 : 0 : gfc_error ("Source-expr at %L and allocate-object at %L must "
8922 : : "have the same shape", &e1->where, &e2->where);
8923 : 0 : mpz_clear (s);
8924 : 0 : return false;
8925 : : }
8926 : : }
8927 : :
8928 : 1312 : mpz_clear (s);
8929 : : }
8930 : :
8931 : : return true;
8932 : : }
8933 : :
8934 : :
8935 : : /* Resolve the expression in an ALLOCATE statement, doing the additional
8936 : : checks to see whether the expression is OK or not. The expression must
8937 : : have a trailing array reference that gives the size of the array. */
8938 : :
8939 : : static bool
8940 : 16681 : resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
8941 : : {
8942 : 16681 : int i, pointer, allocatable, dimension, is_abstract;
8943 : 16681 : int codimension;
8944 : 16681 : bool coindexed;
8945 : 16681 : bool unlimited;
8946 : 16681 : symbol_attribute attr;
8947 : 16681 : gfc_ref *ref, *ref2;
8948 : 16681 : gfc_expr *e2;
8949 : 16681 : gfc_array_ref *ar;
8950 : 16681 : gfc_symbol *sym = NULL;
8951 : 16681 : gfc_alloc *a;
8952 : 16681 : gfc_component *c;
8953 : 16681 : bool t;
8954 : :
8955 : : /* Mark the utmost array component as being in allocate to allow DIMEN_STAR
8956 : : checking of coarrays. */
8957 : 20919 : for (ref = e->ref; ref; ref = ref->next)
8958 : 16846 : if (ref->next == NULL)
8959 : : break;
8960 : :
8961 : 16681 : if (ref && ref->type == REF_ARRAY)
8962 : 11474 : ref->u.ar.in_allocate = true;
8963 : :
8964 : 16681 : if (!gfc_resolve_expr (e))
8965 : 1 : goto failure;
8966 : :
8967 : : /* Make sure the expression is allocatable or a pointer. If it is
8968 : : pointer, the next-to-last reference must be a pointer. */
8969 : :
8970 : 16680 : ref2 = NULL;
8971 : 16680 : if (e->symtree)
8972 : 16680 : sym = e->symtree->n.sym;
8973 : :
8974 : : /* Check whether ultimate component is abstract and CLASS. */
8975 : 33360 : is_abstract = 0;
8976 : :
8977 : : /* Is the allocate-object unlimited polymorphic? */
8978 : 16680 : unlimited = UNLIMITED_POLY(e);
8979 : :
8980 : 16680 : if (e->expr_type != EXPR_VARIABLE)
8981 : : {
8982 : 0 : allocatable = 0;
8983 : 0 : attr = gfc_expr_attr (e);
8984 : 0 : pointer = attr.pointer;
8985 : 0 : dimension = attr.dimension;
8986 : 0 : codimension = attr.codimension;
8987 : : }
8988 : : else
8989 : : {
8990 : 16680 : if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
8991 : : {
8992 : 3302 : allocatable = CLASS_DATA (sym)->attr.allocatable;
8993 : 3302 : pointer = CLASS_DATA (sym)->attr.class_pointer;
8994 : 3302 : dimension = CLASS_DATA (sym)->attr.dimension;
8995 : 3302 : codimension = CLASS_DATA (sym)->attr.codimension;
8996 : 3302 : is_abstract = CLASS_DATA (sym)->attr.abstract;
8997 : : }
8998 : : else
8999 : : {
9000 : 13378 : allocatable = sym->attr.allocatable;
9001 : 13378 : pointer = sym->attr.pointer;
9002 : 13378 : dimension = sym->attr.dimension;
9003 : 13378 : codimension = sym->attr.codimension;
9004 : : }
9005 : :
9006 : 16680 : coindexed = false;
9007 : :
9008 : 33520 : for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
9009 : : {
9010 : 16842 : switch (ref->type)
9011 : : {
9012 : 12766 : case REF_ARRAY:
9013 : 12766 : if (ref->u.ar.codimen > 0)
9014 : : {
9015 : 659 : int n;
9016 : 940 : for (n = ref->u.ar.dimen;
9017 : 940 : n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
9018 : 696 : if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
9019 : : {
9020 : : coindexed = true;
9021 : : break;
9022 : : }
9023 : : }
9024 : :
9025 : 12766 : if (ref->next != NULL)
9026 : 1294 : pointer = 0;
9027 : : break;
9028 : :
9029 : 4076 : case REF_COMPONENT:
9030 : : /* F2008, C644. */
9031 : 4076 : if (coindexed)
9032 : : {
9033 : 2 : gfc_error ("Coindexed allocatable object at %L",
9034 : : &e->where);
9035 : 2 : goto failure;
9036 : : }
9037 : :
9038 : 4074 : c = ref->u.c.component;
9039 : 4074 : if (c->ts.type == BT_CLASS)
9040 : : {
9041 : 977 : allocatable = CLASS_DATA (c)->attr.allocatable;
9042 : 977 : pointer = CLASS_DATA (c)->attr.class_pointer;
9043 : 977 : dimension = CLASS_DATA (c)->attr.dimension;
9044 : 977 : codimension = CLASS_DATA (c)->attr.codimension;
9045 : 977 : is_abstract = CLASS_DATA (c)->attr.abstract;
9046 : : }
9047 : : else
9048 : : {
9049 : 3097 : allocatable = c->attr.allocatable;
9050 : 3097 : pointer = c->attr.pointer;
9051 : 3097 : dimension = c->attr.dimension;
9052 : 3097 : codimension = c->attr.codimension;
9053 : 3097 : is_abstract = c->attr.abstract;
9054 : : }
9055 : : break;
9056 : :
9057 : 0 : case REF_SUBSTRING:
9058 : 0 : case REF_INQUIRY:
9059 : 0 : allocatable = 0;
9060 : 0 : pointer = 0;
9061 : 0 : break;
9062 : : }
9063 : : }
9064 : : }
9065 : :
9066 : : /* Check for F08:C628 (F2018:C932). Each allocate-object shall be a data
9067 : : pointer or an allocatable variable. */
9068 : 16678 : if (allocatable == 0 && pointer == 0)
9069 : : {
9070 : 4 : gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
9071 : : &e->where);
9072 : 4 : goto failure;
9073 : : }
9074 : :
9075 : : /* Some checks for the SOURCE tag. */
9076 : 16674 : if (code->expr3)
9077 : : {
9078 : : /* Check F03:C632: "The source-expr shall be a scalar or have the same
9079 : : rank as allocate-object". This would require the MOLD argument to
9080 : : NULL() as source-expr for subsequent checking. However, even the
9081 : : resulting disassociated pointer or unallocated array has no shape that
9082 : : could be used for SOURCE= or MOLD=. */
9083 : 3722 : if (code->expr3->expr_type == EXPR_NULL)
9084 : : {
9085 : 4 : gfc_error ("The intrinsic NULL cannot be used as source-expr at %L",
9086 : : &code->expr3->where);
9087 : 4 : goto failure;
9088 : : }
9089 : :
9090 : : /* Check F03:C631. */
9091 : 3718 : if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
9092 : : {
9093 : 10 : gfc_error ("Type of entity at %L is type incompatible with "
9094 : 10 : "source-expr at %L", &e->where, &code->expr3->where);
9095 : 10 : goto failure;
9096 : : }
9097 : :
9098 : : /* Check F03:C632 and restriction following Note 6.18. */
9099 : 3708 : if (code->expr3->rank > 0 && !conformable_arrays (code->expr3, e))
9100 : 7 : goto failure;
9101 : :
9102 : : /* Check F03:C633. */
9103 : 3701 : if (code->expr3->ts.kind != e->ts.kind && !unlimited)
9104 : : {
9105 : 1 : gfc_error ("The allocate-object at %L and the source-expr at %L "
9106 : : "shall have the same kind type parameter",
9107 : : &e->where, &code->expr3->where);
9108 : 1 : goto failure;
9109 : : }
9110 : :
9111 : : /* Check F2008, C642. */
9112 : 3700 : if (code->expr3->ts.type == BT_DERIVED
9113 : 3700 : && ((codimension && gfc_expr_attr (code->expr3).lock_comp)
9114 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9115 : : == INTMOD_ISO_FORTRAN_ENV
9116 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9117 : : == ISOFORTRAN_LOCK_TYPE)))
9118 : : {
9119 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9120 : : "LOCK_TYPE nor have a LOCK_TYPE component if "
9121 : : "allocate-object at %L is a coarray",
9122 : 0 : &code->expr3->where, &e->where);
9123 : 0 : goto failure;
9124 : : }
9125 : :
9126 : : /* Check F2008:C639: "Corresponding kind type parameters of
9127 : : allocate-object and source-expr shall have the same values." */
9128 : 3700 : if (e->ts.type == BT_CHARACTER
9129 : 758 : && !e->ts.deferred
9130 : 138 : && e->ts.u.cl->length
9131 : 138 : && code->expr3->ts.type == BT_CHARACTER
9132 : 3838 : && !gfc_check_same_strlen (e, code->expr3, "ALLOCATE with "
9133 : : "SOURCE= or MOLD= specifier"))
9134 : 17 : goto failure;
9135 : :
9136 : : /* Check TS18508, C702/C703. */
9137 : 3683 : if (code->expr3->ts.type == BT_DERIVED
9138 : 4846 : && ((codimension && gfc_expr_attr (code->expr3).event_comp)
9139 : 1163 : || (code->expr3->ts.u.derived->from_intmod
9140 : : == INTMOD_ISO_FORTRAN_ENV
9141 : 0 : && code->expr3->ts.u.derived->intmod_sym_id
9142 : : == ISOFORTRAN_EVENT_TYPE)))
9143 : : {
9144 : 0 : gfc_error ("The source-expr at %L shall neither be of type "
9145 : : "EVENT_TYPE nor have a EVENT_TYPE component if "
9146 : : "allocate-object at %L is a coarray",
9147 : 0 : &code->expr3->where, &e->where);
9148 : 0 : goto failure;
9149 : : }
9150 : : }
9151 : :
9152 : : /* Check F08:C629. */
9153 : 16635 : if (is_abstract && code->ext.alloc.ts.type == BT_UNKNOWN
9154 : 153 : && !code->expr3)
9155 : : {
9156 : 2 : gcc_assert (e->ts.type == BT_CLASS);
9157 : 2 : gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
9158 : : "type-spec or source-expr", sym->name, &e->where);
9159 : 2 : goto failure;
9160 : : }
9161 : :
9162 : : /* F2003:C626 (R623) A type-param-value in a type-spec shall be an asterisk
9163 : : if and only if each allocate-object is a dummy argument for which the
9164 : : corresponding type parameter is assumed. */
9165 : 16633 : if (code->ext.alloc.ts.type == BT_CHARACTER
9166 : 489 : && code->ext.alloc.ts.u.cl->length != NULL
9167 : 474 : && e->ts.type == BT_CHARACTER && !e->ts.deferred
9168 : 23 : && e->ts.u.cl->length == NULL
9169 : 2 : && e->symtree->n.sym->attr.dummy)
9170 : : {
9171 : 2 : gfc_error ("The type parameter in ALLOCATE statement with type-spec "
9172 : : "shall be an asterisk as allocate object %qs at %L is a "
9173 : : "dummy argument with assumed type parameter",
9174 : : sym->name, &e->where);
9175 : 2 : goto failure;
9176 : : }
9177 : :
9178 : : /* Check F08:C632. */
9179 : 16631 : if (code->ext.alloc.ts.type == BT_CHARACTER && !e->ts.deferred
9180 : 60 : && !UNLIMITED_POLY (e))
9181 : : {
9182 : 36 : int cmp;
9183 : :
9184 : 36 : if (!e->ts.u.cl->length)
9185 : 15 : goto failure;
9186 : :
9187 : 42 : cmp = gfc_dep_compare_expr (e->ts.u.cl->length,
9188 : 21 : code->ext.alloc.ts.u.cl->length);
9189 : 21 : if (cmp == 1 || cmp == -1 || cmp == -3)
9190 : : {
9191 : 2 : gfc_error ("Allocating %s at %L with type-spec requires the same "
9192 : : "character-length parameter as in the declaration",
9193 : : sym->name, &e->where);
9194 : 2 : goto failure;
9195 : : }
9196 : : }
9197 : :
9198 : : /* In the variable definition context checks, gfc_expr_attr is used
9199 : : on the expression. This is fooled by the array specification
9200 : : present in e, thus we have to eliminate that one temporarily. */
9201 : 16614 : e2 = remove_last_array_ref (e);
9202 : 16614 : t = true;
9203 : 16614 : if (t && pointer)
9204 : 3739 : t = gfc_check_vardef_context (e2, true, true, false,
9205 : 3739 : _("ALLOCATE object"));
9206 : 3739 : if (t)
9207 : 16606 : t = gfc_check_vardef_context (e2, false, true, false,
9208 : 16606 : _("ALLOCATE object"));
9209 : 16614 : gfc_free_expr (e2);
9210 : 16614 : if (!t)
9211 : 11 : goto failure;
9212 : :
9213 : 16603 : code->ext.alloc.expr3_not_explicit = 0;
9214 : 16603 : if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
9215 : 1568 : && !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
9216 : : {
9217 : : /* For class arrays, the initialization with SOURCE is done
9218 : : using _copy and trans_call. It is convenient to exploit that
9219 : : when the allocated type is different from the declared type but
9220 : : no SOURCE exists by setting expr3. */
9221 : 284 : code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
9222 : 284 : code->ext.alloc.expr3_not_explicit = 1;
9223 : : }
9224 : 16319 : else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
9225 : 2376 : && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
9226 : 5 : && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
9227 : : {
9228 : : /* We have to zero initialize the integer variable. */
9229 : 1 : code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
9230 : 1 : code->ext.alloc.expr3_not_explicit = 1;
9231 : : }
9232 : :
9233 : 16603 : if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
9234 : : {
9235 : : /* Make sure the vtab symbol is present when
9236 : : the module variables are generated. */
9237 : 2905 : gfc_typespec ts = e->ts;
9238 : 2905 : if (code->expr3)
9239 : 1305 : ts = code->expr3->ts;
9240 : 1600 : else if (code->ext.alloc.ts.type == BT_DERIVED)
9241 : 701 : ts = code->ext.alloc.ts;
9242 : :
9243 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9244 : : statement is necessary. */
9245 : 2905 : gfc_find_derived_vtab (ts.u.derived);
9246 : 2905 : }
9247 : 13698 : else if (unlimited && !UNLIMITED_POLY (code->expr3))
9248 : : {
9249 : : /* Again, make sure the vtab symbol is present when
9250 : : the module variables are generated. */
9251 : 433 : gfc_typespec *ts = NULL;
9252 : 433 : if (code->expr3)
9253 : 347 : ts = &code->expr3->ts;
9254 : : else
9255 : 86 : ts = &code->ext.alloc.ts;
9256 : :
9257 : 433 : gcc_assert (ts);
9258 : :
9259 : : /* Finding the vtab also publishes the type's symbol. Therefore this
9260 : : statement is necessary. */
9261 : 433 : gfc_find_vtab (ts);
9262 : : }
9263 : :
9264 : 16603 : if (dimension == 0 && codimension == 0)
9265 : 5160 : goto success;
9266 : :
9267 : : /* Make sure the last reference node is an array specification. */
9268 : :
9269 : 11443 : if (!ref2 || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL
9270 : 10281 : || (dimension && ref2->u.ar.dimen == 0))
9271 : : {
9272 : : /* F08:C633. */
9273 : 1162 : if (code->expr3)
9274 : : {
9275 : 1161 : if (!gfc_notify_std (GFC_STD_F2008, "Array specification required "
9276 : : "in ALLOCATE statement at %L", &e->where))
9277 : 0 : goto failure;
9278 : 1161 : if (code->expr3->rank != 0)
9279 : 1160 : *array_alloc_wo_spec = true;
9280 : : else
9281 : : {
9282 : 1 : gfc_error ("Array specification or array-valued SOURCE= "
9283 : : "expression required in ALLOCATE statement at %L",
9284 : : &e->where);
9285 : 1 : goto failure;
9286 : : }
9287 : : }
9288 : : else
9289 : : {
9290 : 1 : gfc_error ("Array specification required in ALLOCATE statement "
9291 : : "at %L", &e->where);
9292 : 1 : goto failure;
9293 : : }
9294 : : }
9295 : :
9296 : : /* Make sure that the array section reference makes sense in the
9297 : : context of an ALLOCATE specification. */
9298 : :
9299 : 11441 : ar = &ref2->u.ar;
9300 : :
9301 : 11441 : if (codimension)
9302 : 982 : for (i = ar->dimen; i < ar->dimen + ar->codimen; i++)
9303 : : {
9304 : 580 : switch (ar->dimen_type[i])
9305 : : {
9306 : 2 : case DIMEN_THIS_IMAGE:
9307 : 2 : gfc_error ("Coarray specification required in ALLOCATE statement "
9308 : : "at %L", &e->where);
9309 : 2 : goto failure;
9310 : :
9311 : 83 : case DIMEN_RANGE:
9312 : : /* F2018:R937:
9313 : : * allocate-coshape-spec is [ lower-bound-expr : ] upper-bound-expr
9314 : : */
9315 : 83 : if (ar->start[i] == 0 || ar->end[i] == 0 || ar->stride[i] != NULL)
9316 : : {
9317 : 8 : gfc_error ("Bad coarray specification in ALLOCATE statement "
9318 : : "at %L", &e->where);
9319 : 8 : goto failure;
9320 : : }
9321 : 75 : else if (gfc_dep_compare_expr (ar->start[i], ar->end[i]) == 1)
9322 : : {
9323 : 2 : gfc_error ("Upper cobound is less than lower cobound at %L",
9324 : 2 : &ar->start[i]->where);
9325 : 2 : goto failure;
9326 : : }
9327 : : break;
9328 : :
9329 : 93 : case DIMEN_ELEMENT:
9330 : 93 : if (ar->start[i]->expr_type == EXPR_CONSTANT)
9331 : : {
9332 : 85 : gcc_assert (ar->start[i]->ts.type == BT_INTEGER);
9333 : 85 : if (mpz_cmp_si (ar->start[i]->value.integer, 1) < 0)
9334 : : {
9335 : 1 : gfc_error ("Upper cobound is less than lower cobound "
9336 : : "of 1 at %L", &ar->start[i]->where);
9337 : 1 : goto failure;
9338 : : }
9339 : : }
9340 : : break;
9341 : :
9342 : : case DIMEN_STAR:
9343 : : break;
9344 : :
9345 : 0 : default:
9346 : 0 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9347 : : &e->where);
9348 : 0 : goto failure;
9349 : :
9350 : : }
9351 : : }
9352 : 28193 : for (i = 0; i < ar->dimen; i++)
9353 : : {
9354 : 16769 : if (ar->type == AR_ELEMENT || ar->type == AR_FULL)
9355 : 14115 : goto check_symbols;
9356 : :
9357 : 2654 : switch (ar->dimen_type[i])
9358 : : {
9359 : : case DIMEN_ELEMENT:
9360 : : break;
9361 : :
9362 : 2389 : case DIMEN_RANGE:
9363 : 2389 : if (ar->start[i] != NULL
9364 : 2389 : && ar->end[i] != NULL
9365 : 2388 : && ar->stride[i] == NULL)
9366 : : break;
9367 : :
9368 : : /* Fall through. */
9369 : :
9370 : 1 : case DIMEN_UNKNOWN:
9371 : 1 : case DIMEN_VECTOR:
9372 : 1 : case DIMEN_STAR:
9373 : 1 : case DIMEN_THIS_IMAGE:
9374 : 1 : gfc_error ("Bad array specification in ALLOCATE statement at %L",
9375 : : &e->where);
9376 : 1 : goto failure;
9377 : : }
9378 : :
9379 : 2388 : check_symbols:
9380 : 43878 : for (a = code->ext.alloc.list; a; a = a->next)
9381 : : {
9382 : 27113 : sym = a->expr->symtree->n.sym;
9383 : :
9384 : : /* TODO - check derived type components. */
9385 : 27113 : if (gfc_bt_struct (sym->ts.type) || sym->ts.type == BT_CLASS)
9386 : 8970 : continue;
9387 : :
9388 : 18143 : if ((ar->start[i] != NULL
9389 : 17561 : && gfc_find_var_in_expr (sym, ar->start[i]))
9390 : 35701 : || (ar->end[i] != NULL
9391 : 2620 : && gfc_find_var_in_expr (sym, ar->end[i])))
9392 : : {
9393 : 3 : gfc_error ("%qs must not appear in the array specification at "
9394 : : "%L in the same ALLOCATE statement where it is "
9395 : : "itself allocated", sym->name, &ar->where);
9396 : 3 : goto failure;
9397 : : }
9398 : : }
9399 : : }
9400 : :
9401 : 11588 : for (i = ar->dimen; i < ar->codimen + ar->dimen; i++)
9402 : : {
9403 : 729 : if (ar->dimen_type[i] == DIMEN_ELEMENT
9404 : 565 : || ar->dimen_type[i] == DIMEN_RANGE)
9405 : : {
9406 : 164 : if (i == (ar->dimen + ar->codimen - 1))
9407 : : {
9408 : 0 : gfc_error ("Expected %<*%> in coindex specification in ALLOCATE "
9409 : : "statement at %L", &e->where);
9410 : 0 : goto failure;
9411 : : }
9412 : 164 : continue;
9413 : : }
9414 : :
9415 : 401 : if (ar->dimen_type[i] == DIMEN_STAR && i == (ar->dimen + ar->codimen - 1)
9416 : 401 : && ar->stride[i] == NULL)
9417 : : break;
9418 : :
9419 : 0 : gfc_error ("Bad coarray specification in ALLOCATE statement at %L",
9420 : : &e->where);
9421 : 0 : goto failure;
9422 : : }
9423 : :
9424 : 11424 : success:
9425 : : return true;
9426 : :
9427 : : failure:
9428 : : return false;
9429 : : }
9430 : :
9431 : :
9432 : : static void
9433 : 19400 : resolve_allocate_deallocate (gfc_code *code, const char *fcn)
9434 : : {
9435 : 19400 : gfc_expr *stat, *errmsg, *pe, *qe;
9436 : 19400 : gfc_alloc *a, *p, *q;
9437 : :
9438 : 19400 : stat = code->expr1;
9439 : 19400 : errmsg = code->expr2;
9440 : :
9441 : : /* Check the stat variable. */
9442 : 19400 : if (stat)
9443 : : {
9444 : 619 : if (!gfc_check_vardef_context (stat, false, false, false,
9445 : 619 : _("STAT variable")))
9446 : 8 : goto done_stat;
9447 : :
9448 : 611 : if (stat->ts.type != BT_INTEGER
9449 : 602 : || stat->rank > 0)
9450 : 11 : gfc_error ("Stat-variable at %L must be a scalar INTEGER "
9451 : : "variable", &stat->where);
9452 : :
9453 : 611 : if (stat->expr_type == EXPR_CONSTANT || stat->symtree == NULL)
9454 : 0 : goto done_stat;
9455 : :
9456 : : /* F2018:9.7.4: The stat-variable shall not be allocated or deallocated
9457 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9458 : : */
9459 : 1267 : for (p = code->ext.alloc.list; p; p = p->next)
9460 : 663 : if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
9461 : : {
9462 : 9 : gfc_ref *ref1, *ref2;
9463 : 9 : bool found = true;
9464 : :
9465 : 16 : for (ref1 = p->expr->ref, ref2 = stat->ref; ref1 && ref2;
9466 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9467 : : {
9468 : 9 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9469 : 5 : continue;
9470 : 4 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9471 : : {
9472 : : found = false;
9473 : : break;
9474 : : }
9475 : : }
9476 : :
9477 : 9 : if (found)
9478 : : {
9479 : 7 : gfc_error ("Stat-variable at %L shall not be %sd within "
9480 : : "the same %s statement", &stat->where, fcn, fcn);
9481 : 7 : break;
9482 : : }
9483 : : }
9484 : : }
9485 : :
9486 : 18781 : done_stat:
9487 : :
9488 : : /* Check the errmsg variable. */
9489 : 19400 : if (errmsg)
9490 : : {
9491 : 146 : if (!stat)
9492 : 2 : gfc_warning (0, "ERRMSG at %L is useless without a STAT tag",
9493 : : &errmsg->where);
9494 : :
9495 : 146 : if (!gfc_check_vardef_context (errmsg, false, false, false,
9496 : 146 : _("ERRMSG variable")))
9497 : 6 : goto done_errmsg;
9498 : :
9499 : : /* F18:R928 alloc-opt is ERRMSG = errmsg-variable
9500 : : F18:R930 errmsg-variable is scalar-default-char-variable
9501 : : F18:R906 default-char-variable is variable
9502 : : F18:C906 default-char-variable shall be default character. */
9503 : 140 : if (errmsg->ts.type != BT_CHARACTER
9504 : 138 : || errmsg->rank > 0
9505 : 137 : || errmsg->ts.kind != gfc_default_character_kind)
9506 : 4 : gfc_error ("ERRMSG variable at %L shall be a scalar default CHARACTER "
9507 : : "variable", &errmsg->where);
9508 : :
9509 : 140 : if (errmsg->expr_type == EXPR_CONSTANT || errmsg->symtree == NULL)
9510 : 0 : goto done_errmsg;
9511 : :
9512 : : /* F2018:9.7.5: The errmsg-variable shall not be allocated or deallocated
9513 : : * within the ALLOCATE or DEALLOCATE statement in which it appears ...
9514 : : */
9515 : 278 : for (p = code->ext.alloc.list; p; p = p->next)
9516 : 143 : if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
9517 : : {
9518 : 9 : gfc_ref *ref1, *ref2;
9519 : 9 : bool found = true;
9520 : :
9521 : 16 : for (ref1 = p->expr->ref, ref2 = errmsg->ref; ref1 && ref2;
9522 : 7 : ref1 = ref1->next, ref2 = ref2->next)
9523 : : {
9524 : 11 : if (ref1->type != REF_COMPONENT || ref2->type != REF_COMPONENT)
9525 : 4 : continue;
9526 : 7 : if (ref1->u.c.component->name != ref2->u.c.component->name)
9527 : : {
9528 : : found = false;
9529 : : break;
9530 : : }
9531 : : }
9532 : :
9533 : 9 : if (found)
9534 : : {
9535 : 5 : gfc_error ("Errmsg-variable at %L shall not be %sd within "
9536 : : "the same %s statement", &errmsg->where, fcn, fcn);
9537 : 5 : break;
9538 : : }
9539 : : }
9540 : : }
9541 : :
9542 : 19254 : done_errmsg:
9543 : :
9544 : : /* Check that an allocate-object appears only once in the statement. */
9545 : :
9546 : 43953 : for (p = code->ext.alloc.list; p; p = p->next)
9547 : : {
9548 : 24553 : pe = p->expr;
9549 : 33594 : for (q = p->next; q; q = q->next)
9550 : : {
9551 : 9041 : qe = q->expr;
9552 : 9041 : if (pe->symtree->n.sym->name == qe->symtree->n.sym->name)
9553 : : {
9554 : : /* This is a potential collision. */
9555 : 2093 : gfc_ref *pr = pe->ref;
9556 : 2093 : gfc_ref *qr = qe->ref;
9557 : :
9558 : : /* Follow the references until
9559 : : a) They start to differ, in which case there is no error;
9560 : : you can deallocate a%b and a%c in a single statement
9561 : : b) Both of them stop, which is an error
9562 : : c) One of them stops, which is also an error. */
9563 : 4517 : while (1)
9564 : : {
9565 : 3305 : if (pr == NULL && qr == NULL)
9566 : : {
9567 : 7 : gfc_error ("Allocate-object at %L also appears at %L",
9568 : : &pe->where, &qe->where);
9569 : 7 : break;
9570 : : }
9571 : 3298 : else if (pr != NULL && qr == NULL)
9572 : : {
9573 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9574 : : " object at %L", &pe->where, &qe->where);
9575 : 2 : break;
9576 : : }
9577 : 3296 : else if (pr == NULL && qr != NULL)
9578 : : {
9579 : 2 : gfc_error ("Allocate-object at %L is subobject of"
9580 : : " object at %L", &qe->where, &pe->where);
9581 : 2 : break;
9582 : : }
9583 : : /* Here, pr != NULL && qr != NULL */
9584 : 3294 : gcc_assert(pr->type == qr->type);
9585 : 3294 : if (pr->type == REF_ARRAY)
9586 : : {
9587 : : /* Handle cases like allocate(v(3)%x(3), v(2)%x(3)),
9588 : : which are legal. */
9589 : 1065 : gcc_assert (qr->type == REF_ARRAY);
9590 : :
9591 : 1065 : if (pr->next && qr->next)
9592 : : {
9593 : : int i;
9594 : : gfc_array_ref *par = &(pr->u.ar);
9595 : : gfc_array_ref *qar = &(qr->u.ar);
9596 : :
9597 : 1840 : for (i=0; i<par->dimen; i++)
9598 : : {
9599 : 954 : if ((par->start[i] != NULL
9600 : 0 : || qar->start[i] != NULL)
9601 : 1908 : && gfc_dep_compare_expr (par->start[i],
9602 : 954 : qar->start[i]) != 0)
9603 : 168 : goto break_label;
9604 : : }
9605 : : }
9606 : : }
9607 : : else
9608 : : {
9609 : 2229 : if (pr->u.c.component->name != qr->u.c.component->name)
9610 : : break;
9611 : : }
9612 : :
9613 : 1212 : pr = pr->next;
9614 : 1212 : qr = qr->next;
9615 : 1212 : }
9616 : 9041 : break_label:
9617 : : ;
9618 : : }
9619 : : }
9620 : : }
9621 : :
9622 : 19400 : if (strcmp (fcn, "ALLOCATE") == 0)
9623 : : {
9624 : 13682 : bool arr_alloc_wo_spec = false;
9625 : :
9626 : : /* Resolving the expr3 in the loop over all objects to allocate would
9627 : : execute loop invariant code for each loop item. Therefore do it just
9628 : : once here. */
9629 : 13682 : if (code->expr3 && code->expr3->mold
9630 : 343 : && code->expr3->ts.type == BT_DERIVED)
9631 : : {
9632 : : /* Default initialization via MOLD (non-polymorphic). */
9633 : 20 : gfc_expr *rhs = gfc_default_initializer (&code->expr3->ts);
9634 : 20 : if (rhs != NULL)
9635 : : {
9636 : 7 : gfc_resolve_expr (rhs);
9637 : 7 : gfc_free_expr (code->expr3);
9638 : 7 : code->expr3 = rhs;
9639 : : }
9640 : : }
9641 : 30363 : for (a = code->ext.alloc.list; a; a = a->next)
9642 : 16681 : resolve_allocate_expr (a->expr, code, &arr_alloc_wo_spec);
9643 : :
9644 : 13682 : if (arr_alloc_wo_spec && code->expr3)
9645 : : {
9646 : : /* Mark the allocate to have to take the array specification
9647 : : from the expr3. */
9648 : 1154 : code->ext.alloc.arr_spec_from_expr3 = 1;
9649 : : }
9650 : : }
9651 : : else
9652 : : {
9653 : 13590 : for (a = code->ext.alloc.list; a; a = a->next)
9654 : 7872 : resolve_deallocate_expr (a->expr);
9655 : : }
9656 : 19400 : }
9657 : :
9658 : :
9659 : : /************ SELECT CASE resolution subroutines ************/
9660 : :
9661 : : /* Callback function for our mergesort variant. Determines interval
9662 : : overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
9663 : : op1 > op2. Assumes we're not dealing with the default case.
9664 : : We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
9665 : : There are nine situations to check. */
9666 : :
9667 : : static int
9668 : 1542 : compare_cases (const gfc_case *op1, const gfc_case *op2)
9669 : : {
9670 : 1542 : int retval;
9671 : :
9672 : 1542 : if (op1->low == NULL) /* op1 = (:L) */
9673 : : {
9674 : : /* op2 = (:N), so overlap. */
9675 : 52 : retval = 0;
9676 : : /* op2 = (M:) or (M:N), L < M */
9677 : 52 : if (op2->low != NULL
9678 : 52 : && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9679 : : retval = -1;
9680 : : }
9681 : 1490 : else if (op1->high == NULL) /* op1 = (K:) */
9682 : : {
9683 : : /* op2 = (M:), so overlap. */
9684 : 10 : retval = 0;
9685 : : /* op2 = (:N) or (M:N), K > N */
9686 : 10 : if (op2->high != NULL
9687 : 10 : && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9688 : : retval = 1;
9689 : : }
9690 : : else /* op1 = (K:L) */
9691 : : {
9692 : 1480 : if (op2->low == NULL) /* op2 = (:N), K > N */
9693 : 18 : retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9694 : 18 : ? 1 : 0;
9695 : 1462 : else if (op2->high == NULL) /* op2 = (M:), L < M */
9696 : 14 : retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9697 : 10 : ? -1 : 0;
9698 : : else /* op2 = (M:N) */
9699 : : {
9700 : 1452 : retval = 0;
9701 : : /* L < M */
9702 : 1452 : if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
9703 : : retval = -1;
9704 : : /* K > N */
9705 : 412 : else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
9706 : 438 : retval = 1;
9707 : : }
9708 : : }
9709 : :
9710 : 1542 : return retval;
9711 : : }
9712 : :
9713 : :
9714 : : /* Merge-sort a double linked case list, detecting overlap in the
9715 : : process. LIST is the head of the double linked case list before it
9716 : : is sorted. Returns the head of the sorted list if we don't see any
9717 : : overlap, or NULL otherwise. */
9718 : :
9719 : : static gfc_case *
9720 : 642 : check_case_overlap (gfc_case *list)
9721 : : {
9722 : 642 : gfc_case *p, *q, *e, *tail;
9723 : 642 : int insize, nmerges, psize, qsize, cmp, overlap_seen;
9724 : :
9725 : : /* If the passed list was empty, return immediately. */
9726 : 642 : if (!list)
9727 : : return NULL;
9728 : :
9729 : : overlap_seen = 0;
9730 : : insize = 1;
9731 : :
9732 : : /* Loop unconditionally. The only exit from this loop is a return
9733 : : statement, when we've finished sorting the case list. */
9734 : 1334 : for (;;)
9735 : : {
9736 : 988 : p = list;
9737 : 988 : list = NULL;
9738 : 988 : tail = NULL;
9739 : :
9740 : : /* Count the number of merges we do in this pass. */
9741 : 988 : nmerges = 0;
9742 : :
9743 : : /* Loop while there exists a merge to be done. */
9744 : 2491 : while (p)
9745 : : {
9746 : 1503 : int i;
9747 : :
9748 : : /* Count this merge. */
9749 : 1503 : nmerges++;
9750 : :
9751 : : /* Cut the list in two pieces by stepping INSIZE places
9752 : : forward in the list, starting from P. */
9753 : 1503 : psize = 0;
9754 : 1503 : q = p;
9755 : 3150 : for (i = 0; i < insize; i++)
9756 : : {
9757 : 2206 : psize++;
9758 : 2206 : q = q->right;
9759 : 2206 : if (!q)
9760 : : break;
9761 : : }
9762 : : qsize = insize;
9763 : :
9764 : : /* Now we have two lists. Merge them! */
9765 : 4918 : while (psize > 0 || (qsize > 0 && q != NULL))
9766 : : {
9767 : : /* See from which the next case to merge comes from. */
9768 : 771 : if (psize == 0)
9769 : : {
9770 : : /* P is empty so the next case must come from Q. */
9771 : 771 : e = q;
9772 : 771 : q = q->right;
9773 : 771 : qsize--;
9774 : : }
9775 : 2644 : else if (qsize == 0 || q == NULL)
9776 : : {
9777 : : /* Q is empty. */
9778 : 1102 : e = p;
9779 : 1102 : p = p->right;
9780 : 1102 : psize--;
9781 : : }
9782 : : else
9783 : : {
9784 : 1542 : cmp = compare_cases (p, q);
9785 : 1542 : if (cmp < 0)
9786 : : {
9787 : : /* The whole case range for P is less than the
9788 : : one for Q. */
9789 : 1100 : e = p;
9790 : 1100 : p = p->right;
9791 : 1100 : psize--;
9792 : : }
9793 : 442 : else if (cmp > 0)
9794 : : {
9795 : : /* The whole case range for Q is greater than
9796 : : the case range for P. */
9797 : 438 : e = q;
9798 : 438 : q = q->right;
9799 : 438 : qsize--;
9800 : : }
9801 : : else
9802 : : {
9803 : : /* The cases overlap, or they are the same
9804 : : element in the list. Either way, we must
9805 : : issue an error and get the next case from P. */
9806 : : /* FIXME: Sort P and Q by line number. */
9807 : 4 : gfc_error ("CASE label at %L overlaps with CASE "
9808 : : "label at %L", &p->where, &q->where);
9809 : 4 : overlap_seen = 1;
9810 : 4 : e = p;
9811 : 4 : p = p->right;
9812 : 4 : psize--;
9813 : : }
9814 : : }
9815 : :
9816 : : /* Add the next element to the merged list. */
9817 : 3415 : if (tail)
9818 : 2427 : tail->right = e;
9819 : : else
9820 : : list = e;
9821 : 3415 : e->left = tail;
9822 : 3415 : tail = e;
9823 : : }
9824 : :
9825 : : /* P has now stepped INSIZE places along, and so has Q. So
9826 : : they're the same. */
9827 : : p = q;
9828 : : }
9829 : 988 : tail->right = NULL;
9830 : :
9831 : : /* If we have done only one merge or none at all, we've
9832 : : finished sorting the cases. */
9833 : 988 : if (nmerges <= 1)
9834 : : {
9835 : 642 : if (!overlap_seen)
9836 : : return list;
9837 : : else
9838 : : return NULL;
9839 : : }
9840 : :
9841 : : /* Otherwise repeat, merging lists twice the size. */
9842 : 346 : insize *= 2;
9843 : 346 : }
9844 : : }
9845 : :
9846 : :
9847 : : /* Check to see if an expression is suitable for use in a CASE statement.
9848 : : Makes sure that all case expressions are scalar constants of the same
9849 : : type. Return false if anything is wrong. */
9850 : :
9851 : : static bool
9852 : 3257 : validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
9853 : : {
9854 : 3257 : if (e == NULL) return true;
9855 : :
9856 : 3164 : if (e->ts.type != case_expr->ts.type)
9857 : : {
9858 : 4 : gfc_error ("Expression in CASE statement at %L must be of type %s",
9859 : : &e->where, gfc_basic_typename (case_expr->ts.type));
9860 : 4 : return false;
9861 : : }
9862 : :
9863 : : /* C805 (R808) For a given case-construct, each case-value shall be of
9864 : : the same type as case-expr. For character type, length differences
9865 : : are allowed, but the kind type parameters shall be the same. */
9866 : :
9867 : 3160 : if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
9868 : : {
9869 : 4 : gfc_error ("Expression in CASE statement at %L must be of kind %d",
9870 : : &e->where, case_expr->ts.kind);
9871 : 4 : return false;
9872 : : }
9873 : :
9874 : : /* Convert the case value kind to that of case expression kind,
9875 : : if needed */
9876 : :
9877 : 3156 : if (e->ts.kind != case_expr->ts.kind)
9878 : 14 : gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
9879 : :
9880 : 3156 : if (e->rank != 0)
9881 : : {
9882 : 0 : gfc_error ("Expression in CASE statement at %L must be scalar",
9883 : : &e->where);
9884 : 0 : return false;
9885 : : }
9886 : :
9887 : : return true;
9888 : : }
9889 : :
9890 : :
9891 : : /* Given a completely parsed select statement, we:
9892 : :
9893 : : - Validate all expressions and code within the SELECT.
9894 : : - Make sure that the selection expression is not of the wrong type.
9895 : : - Make sure that no case ranges overlap.
9896 : : - Eliminate unreachable cases and unreachable code resulting from
9897 : : removing case labels.
9898 : :
9899 : : The standard does allow unreachable cases, e.g. CASE (5:3). But
9900 : : they are a hassle for code generation, and to prevent that, we just
9901 : : cut them out here. This is not necessary for overlapping cases
9902 : : because they are illegal and we never even try to generate code.
9903 : :
9904 : : We have the additional caveat that a SELECT construct could have
9905 : : been a computed GOTO in the source code. Fortunately we can fairly
9906 : : easily work around that here: The case_expr for a "real" SELECT CASE
9907 : : is in code->expr1, but for a computed GOTO it is in code->expr2. All
9908 : : we have to do is make sure that the case_expr is a scalar integer
9909 : : expression. */
9910 : :
9911 : : static void
9912 : 683 : resolve_select (gfc_code *code, bool select_type)
9913 : : {
9914 : 683 : gfc_code *body;
9915 : 683 : gfc_expr *case_expr;
9916 : 683 : gfc_case *cp, *default_case, *tail, *head;
9917 : 683 : int seen_unreachable;
9918 : 683 : int seen_logical;
9919 : 683 : int ncases;
9920 : 683 : bt type;
9921 : 683 : bool t;
9922 : :
9923 : 683 : if (code->expr1 == NULL)
9924 : : {
9925 : : /* This was actually a computed GOTO statement. */
9926 : 5 : case_expr = code->expr2;
9927 : 5 : if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
9928 : 3 : gfc_error ("Selection expression in computed GOTO statement "
9929 : : "at %L must be a scalar integer expression",
9930 : : &case_expr->where);
9931 : :
9932 : : /* Further checking is not necessary because this SELECT was built
9933 : : by the compiler, so it should always be OK. Just move the
9934 : : case_expr from expr2 to expr so that we can handle computed
9935 : : GOTOs as normal SELECTs from here on. */
9936 : 5 : code->expr1 = code->expr2;
9937 : 5 : code->expr2 = NULL;
9938 : 5 : return;
9939 : : }
9940 : :
9941 : 678 : case_expr = code->expr1;
9942 : 678 : type = case_expr->ts.type;
9943 : :
9944 : : /* F08:C830. */
9945 : 678 : if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER
9946 : 6 : && (!flag_unsigned || (flag_unsigned && type != BT_UNSIGNED)))
9947 : :
9948 : : {
9949 : 0 : gfc_error ("Argument of SELECT statement at %L cannot be %s",
9950 : : &case_expr->where, gfc_typename (case_expr));
9951 : :
9952 : : /* Punt. Going on here just produce more garbage error messages. */
9953 : 0 : return;
9954 : : }
9955 : :
9956 : : /* F08:R842. */
9957 : 678 : if (!select_type && case_expr->rank != 0)
9958 : : {
9959 : 1 : gfc_error ("Argument of SELECT statement at %L must be a scalar "
9960 : : "expression", &case_expr->where);
9961 : :
9962 : : /* Punt. */
9963 : 1 : return;
9964 : : }
9965 : :
9966 : : /* Raise a warning if an INTEGER case value exceeds the range of
9967 : : the case-expr. Later, all expressions will be promoted to the
9968 : : largest kind of all case-labels. */
9969 : :
9970 : 677 : if (type == BT_INTEGER)
9971 : 1897 : for (body = code->block; body; body = body->block)
9972 : 2800 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
9973 : : {
9974 : 1436 : if (cp->low
9975 : 1436 : && gfc_check_integer_range (cp->low->value.integer,
9976 : : case_expr->ts.kind) != ARITH_OK)
9977 : 6 : gfc_warning (0, "Expression in CASE statement at %L is "
9978 : 6 : "not in the range of %s", &cp->low->where,
9979 : : gfc_typename (case_expr));
9980 : :
9981 : 1436 : if (cp->high
9982 : 1153 : && cp->low != cp->high
9983 : 1544 : && gfc_check_integer_range (cp->high->value.integer,
9984 : : case_expr->ts.kind) != ARITH_OK)
9985 : 0 : gfc_warning (0, "Expression in CASE statement at %L is "
9986 : 0 : "not in the range of %s", &cp->high->where,
9987 : : gfc_typename (case_expr));
9988 : : }
9989 : :
9990 : : /* PR 19168 has a long discussion concerning a mismatch of the kinds
9991 : : of the SELECT CASE expression and its CASE values. Walk the lists
9992 : : of case values, and if we find a mismatch, promote case_expr to
9993 : : the appropriate kind. */
9994 : :
9995 : 677 : if (type == BT_LOGICAL || type == BT_INTEGER)
9996 : : {
9997 : 2083 : for (body = code->block; body; body = body->block)
9998 : : {
9999 : : /* Walk the case label list. */
10000 : 3061 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10001 : : {
10002 : : /* Intercept the DEFAULT case. It does not have a kind. */
10003 : 1571 : if (cp->low == NULL && cp->high == NULL)
10004 : 291 : continue;
10005 : :
10006 : : /* Unreachable case ranges are discarded, so ignore. */
10007 : 1235 : if (cp->low != NULL && cp->high != NULL
10008 : 1187 : && cp->low != cp->high
10009 : 1345 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10010 : 33 : continue;
10011 : :
10012 : 1247 : if (cp->low != NULL
10013 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
10014 : 17 : gfc_convert_type_warn (case_expr, &cp->low->ts, 1, 0);
10015 : :
10016 : 1247 : if (cp->high != NULL
10017 : 1247 : && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
10018 : 4 : gfc_convert_type_warn (case_expr, &cp->high->ts, 1, 0);
10019 : : }
10020 : : }
10021 : : }
10022 : :
10023 : : /* Assume there is no DEFAULT case. */
10024 : 677 : default_case = NULL;
10025 : 677 : head = tail = NULL;
10026 : 677 : ncases = 0;
10027 : 677 : seen_logical = 0;
10028 : :
10029 : 2472 : for (body = code->block; body; body = body->block)
10030 : : {
10031 : : /* Assume the CASE list is OK, and all CASE labels can be matched. */
10032 : 1795 : t = true;
10033 : 1795 : seen_unreachable = 0;
10034 : :
10035 : : /* Walk the case label list, making sure that all case labels
10036 : : are legal. */
10037 : 3777 : for (cp = body->ext.block.case_list; cp; cp = cp->next)
10038 : : {
10039 : : /* Count the number of cases in the whole construct. */
10040 : 1993 : ncases++;
10041 : :
10042 : : /* Intercept the DEFAULT case. */
10043 : 1993 : if (cp->low == NULL && cp->high == NULL)
10044 : : {
10045 : 361 : if (default_case != NULL)
10046 : : {
10047 : 0 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10048 : : "by a second DEFAULT CASE at %L",
10049 : : &default_case->where, &cp->where);
10050 : 0 : t = false;
10051 : 0 : break;
10052 : : }
10053 : : else
10054 : : {
10055 : 361 : default_case = cp;
10056 : 361 : continue;
10057 : : }
10058 : : }
10059 : :
10060 : : /* Deal with single value cases and case ranges. Errors are
10061 : : issued from the validation function. */
10062 : 1632 : if (!validate_case_label_expr (cp->low, case_expr)
10063 : 1632 : || !validate_case_label_expr (cp->high, case_expr))
10064 : : {
10065 : : t = false;
10066 : : break;
10067 : : }
10068 : :
10069 : 1624 : if (type == BT_LOGICAL
10070 : 78 : && ((cp->low == NULL || cp->high == NULL)
10071 : 76 : || cp->low != cp->high))
10072 : : {
10073 : 2 : gfc_error ("Logical range in CASE statement at %L is not "
10074 : : "allowed",
10075 : 1 : cp->low ? &cp->low->where : &cp->high->where);
10076 : 2 : t = false;
10077 : 2 : break;
10078 : : }
10079 : :
10080 : 76 : if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
10081 : : {
10082 : 76 : int value;
10083 : 76 : value = cp->low->value.logical == 0 ? 2 : 1;
10084 : 76 : if (value & seen_logical)
10085 : : {
10086 : 1 : gfc_error ("Constant logical value in CASE statement "
10087 : : "is repeated at %L",
10088 : : &cp->low->where);
10089 : 1 : t = false;
10090 : 1 : break;
10091 : : }
10092 : 75 : seen_logical |= value;
10093 : : }
10094 : :
10095 : 1577 : if (cp->low != NULL && cp->high != NULL
10096 : 1530 : && cp->low != cp->high
10097 : 1733 : && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
10098 : : {
10099 : 35 : if (warn_surprising)
10100 : 1 : gfc_warning (OPT_Wsurprising,
10101 : : "Range specification at %L can never be matched",
10102 : : &cp->where);
10103 : :
10104 : 35 : cp->unreachable = 1;
10105 : 35 : seen_unreachable = 1;
10106 : : }
10107 : : else
10108 : : {
10109 : : /* If the case range can be matched, it can also overlap with
10110 : : other cases. To make sure it does not, we put it in a
10111 : : double linked list here. We sort that with a merge sort
10112 : : later on to detect any overlapping cases. */
10113 : 1586 : if (!head)
10114 : : {
10115 : 642 : head = tail = cp;
10116 : 642 : head->right = head->left = NULL;
10117 : : }
10118 : : else
10119 : : {
10120 : 944 : tail->right = cp;
10121 : 944 : tail->right->left = tail;
10122 : 944 : tail = tail->right;
10123 : 944 : tail->right = NULL;
10124 : : }
10125 : : }
10126 : : }
10127 : :
10128 : : /* It there was a failure in the previous case label, give up
10129 : : for this case label list. Continue with the next block. */
10130 : 1795 : if (!t)
10131 : 11 : continue;
10132 : :
10133 : : /* See if any case labels that are unreachable have been seen.
10134 : : If so, we eliminate them. This is a bit of a kludge because
10135 : : the case lists for a single case statement (label) is a
10136 : : single forward linked lists. */
10137 : 1784 : if (seen_unreachable)
10138 : : {
10139 : : /* Advance until the first case in the list is reachable. */
10140 : 69 : while (body->ext.block.case_list != NULL
10141 : 69 : && body->ext.block.case_list->unreachable)
10142 : : {
10143 : 34 : gfc_case *n = body->ext.block.case_list;
10144 : 34 : body->ext.block.case_list = body->ext.block.case_list->next;
10145 : 34 : n->next = NULL;
10146 : 34 : gfc_free_case_list (n);
10147 : : }
10148 : :
10149 : : /* Strip all other unreachable cases. */
10150 : 35 : if (body->ext.block.case_list)
10151 : : {
10152 : 2 : for (cp = body->ext.block.case_list; cp && cp->next; cp = cp->next)
10153 : : {
10154 : 1 : if (cp->next->unreachable)
10155 : : {
10156 : 1 : gfc_case *n = cp->next;
10157 : 1 : cp->next = cp->next->next;
10158 : 1 : n->next = NULL;
10159 : 1 : gfc_free_case_list (n);
10160 : : }
10161 : : }
10162 : : }
10163 : : }
10164 : : }
10165 : :
10166 : : /* See if there were overlapping cases. If the check returns NULL,
10167 : : there was overlap. In that case we don't do anything. If head
10168 : : is non-NULL, we prepend the DEFAULT case. The sorted list can
10169 : : then used during code generation for SELECT CASE constructs with
10170 : : a case expression of a CHARACTER type. */
10171 : 677 : if (head)
10172 : : {
10173 : 642 : head = check_case_overlap (head);
10174 : :
10175 : : /* Prepend the default_case if it is there. */
10176 : 642 : if (head != NULL && default_case)
10177 : : {
10178 : 344 : default_case->left = NULL;
10179 : 344 : default_case->right = head;
10180 : 344 : head->left = default_case;
10181 : : }
10182 : : }
10183 : :
10184 : : /* Eliminate dead blocks that may be the result if we've seen
10185 : : unreachable case labels for a block. */
10186 : 2438 : for (body = code; body && body->block; body = body->block)
10187 : : {
10188 : 1761 : if (body->block->ext.block.case_list == NULL)
10189 : : {
10190 : : /* Cut the unreachable block from the code chain. */
10191 : 34 : gfc_code *c = body->block;
10192 : 34 : body->block = c->block;
10193 : :
10194 : : /* Kill the dead block, but not the blocks below it. */
10195 : 34 : c->block = NULL;
10196 : 34 : gfc_free_statements (c);
10197 : : }
10198 : : }
10199 : :
10200 : : /* More than two cases is legal but insane for logical selects.
10201 : : Issue a warning for it. */
10202 : 677 : if (warn_surprising && type == BT_LOGICAL && ncases > 2)
10203 : 0 : gfc_warning (OPT_Wsurprising,
10204 : : "Logical SELECT CASE block at %L has more that two cases",
10205 : : &code->loc);
10206 : : }
10207 : :
10208 : :
10209 : : /* Check if a derived type is extensible. */
10210 : :
10211 : : bool
10212 : 22816 : gfc_type_is_extensible (gfc_symbol *sym)
10213 : : {
10214 : 22816 : return !(sym->attr.is_bind_c || sym->attr.sequence
10215 : 22800 : || (sym->attr.is_class
10216 : 2058 : && sym->components->ts.u.derived->attr.unlimited_polymorphic));
10217 : : }
10218 : :
10219 : :
10220 : : static void
10221 : : resolve_types (gfc_namespace *ns);
10222 : :
10223 : : /* Resolve an associate-name: Resolve target and ensure the type-spec is
10224 : : correct as well as possibly the array-spec. */
10225 : :
10226 : : static void
10227 : 12259 : resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
10228 : : {
10229 : 12259 : gfc_expr* target;
10230 : 12259 : bool parentheses = false;
10231 : :
10232 : 12259 : gcc_assert (sym->assoc);
10233 : 12259 : gcc_assert (sym->attr.flavor == FL_VARIABLE);
10234 : :
10235 : : /* If this is for SELECT TYPE, the target may not yet be set. In that
10236 : : case, return. Resolution will be called later manually again when
10237 : : this is done. */
10238 : 12259 : target = sym->assoc->target;
10239 : 12259 : if (!target)
10240 : : return;
10241 : 7257 : gcc_assert (!sym->assoc->dangling);
10242 : :
10243 : 7257 : if (target->expr_type == EXPR_OP
10244 : 198 : && target->value.op.op == INTRINSIC_PARENTHESES
10245 : 42 : && target->value.op.op1->expr_type == EXPR_VARIABLE)
10246 : : {
10247 : 23 : sym->assoc->target = gfc_copy_expr (target->value.op.op1);
10248 : 23 : gfc_free_expr (target);
10249 : 23 : target = sym->assoc->target;
10250 : 23 : parentheses = true;
10251 : : }
10252 : :
10253 : 7257 : if (resolve_target && !gfc_resolve_expr (target))
10254 : : return;
10255 : :
10256 : 7252 : if (sym->assoc->ar)
10257 : : {
10258 : : int dim;
10259 : : gfc_array_ref *ar = sym->assoc->ar;
10260 : 67 : for (dim = 0; dim < sym->assoc->ar->dimen; dim++)
10261 : : {
10262 : 39 : if (!(ar->start[dim] && gfc_resolve_expr (ar->start[dim])
10263 : 39 : && ar->start[dim]->ts.type == BT_INTEGER)
10264 : 78 : || !(ar->end[dim] && gfc_resolve_expr (ar->end[dim])
10265 : 39 : && ar->end[dim]->ts.type == BT_INTEGER))
10266 : 0 : gfc_error ("(F202y)Missing or invalid bound in ASSOCIATE rank "
10267 : : "remapping of associate name %s at %L",
10268 : : sym->name, &sym->declared_at);
10269 : : }
10270 : : }
10271 : :
10272 : : /* For variable targets, we get some attributes from the target. */
10273 : 7252 : if (target->expr_type == EXPR_VARIABLE)
10274 : : {
10275 : 6395 : gfc_symbol *tsym, *dsym;
10276 : :
10277 : 6395 : gcc_assert (target->symtree);
10278 : 6395 : tsym = target->symtree->n.sym;
10279 : :
10280 : 6395 : if (gfc_expr_attr (target).proc_pointer)
10281 : : {
10282 : 0 : gfc_error ("Associating entity %qs at %L is a procedure pointer",
10283 : : tsym->name, &target->where);
10284 : 0 : return;
10285 : : }
10286 : :
10287 : 74 : if (tsym->attr.flavor == FL_PROCEDURE && tsym->generic
10288 : 2 : && (dsym = gfc_find_dt_in_generic (tsym)) != NULL
10289 : 6396 : && dsym->attr.flavor == FL_DERIVED)
10290 : : {
10291 : 1 : gfc_error ("Derived type %qs cannot be used as a variable at %L",
10292 : : tsym->name, &target->where);
10293 : 1 : return;
10294 : : }
10295 : :
10296 : 6394 : if (tsym->attr.flavor == FL_PROCEDURE)
10297 : : {
10298 : 73 : bool is_error = true;
10299 : 73 : if (tsym->attr.function && tsym->result == tsym)
10300 : 141 : for (gfc_namespace *ns = sym->ns; ns; ns = ns->parent)
10301 : 137 : if (tsym == ns->proc_name)
10302 : : {
10303 : : is_error = false;
10304 : : break;
10305 : : }
10306 : 64 : if (is_error)
10307 : : {
10308 : 13 : gfc_error ("Associating entity %qs at %L is a procedure name",
10309 : : tsym->name, &target->where);
10310 : 13 : return;
10311 : : }
10312 : : }
10313 : :
10314 : 6381 : sym->attr.asynchronous = tsym->attr.asynchronous;
10315 : 6381 : sym->attr.volatile_ = tsym->attr.volatile_;
10316 : :
10317 : 12762 : sym->attr.target = tsym->attr.target
10318 : 6381 : || gfc_expr_attr (target).pointer;
10319 : 6381 : if (is_subref_array (target))
10320 : 378 : sym->attr.subref_array_pointer = 1;
10321 : : }
10322 : 857 : else if (target->ts.type == BT_PROCEDURE)
10323 : : {
10324 : 0 : gfc_error ("Associating selector-expression at %L yields a procedure",
10325 : : &target->where);
10326 : 0 : return;
10327 : : }
10328 : :
10329 : 7238 : if (sym->assoc->inferred_type || IS_INFERRED_TYPE (target))
10330 : : {
10331 : : /* By now, the type of the target has been fixed up. */
10332 : 287 : symbol_attribute attr;
10333 : :
10334 : 287 : if (sym->ts.type == BT_DERIVED
10335 : 160 : && target->ts.type == BT_CLASS
10336 : 31 : && !UNLIMITED_POLY (target))
10337 : : {
10338 : : /* Inferred to be derived type but the target has type class. */
10339 : 31 : sym->ts = CLASS_DATA (target)->ts;
10340 : 31 : if (!sym->as)
10341 : 31 : sym->as = gfc_copy_array_spec (CLASS_DATA (target)->as);
10342 : 31 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10343 : 31 : sym->attr.dimension = target->rank ? 1 : 0;
10344 : 31 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10345 : : target->corank);
10346 : 31 : sym->as = NULL;
10347 : : }
10348 : 256 : else if (target->ts.type == BT_DERIVED
10349 : 129 : && target->symtree && target->symtree->n.sym
10350 : 105 : && target->symtree->n.sym->ts.type == BT_CLASS
10351 : 0 : && IS_INFERRED_TYPE (target)
10352 : 0 : && target->ref && target->ref->next
10353 : 0 : && target->ref->next->type == REF_ARRAY
10354 : 0 : && !target->ref->next->next)
10355 : : {
10356 : : /* A inferred type selector whose symbol has been determined to be
10357 : : a class array but which only has an array reference. Change the
10358 : : associate name and the selector to class type. */
10359 : 0 : sym->ts = target->ts;
10360 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10361 : 0 : sym->attr.dimension = target->rank ? 1 : 0;
10362 : 0 : gfc_change_class (&sym->ts, &attr, sym->as, target->rank,
10363 : : target->corank);
10364 : 0 : sym->as = NULL;
10365 : 0 : target->ts = sym->ts;
10366 : : }
10367 : 256 : else if ((target->ts.type == BT_DERIVED)
10368 : 127 : || (sym->ts.type == BT_CLASS && target->ts.type == BT_CLASS
10369 : 61 : && CLASS_DATA (target)->as && !CLASS_DATA (sym)->as))
10370 : : /* Confirmed to be either a derived type or misidentified to be a
10371 : : scalar class object, when the selector is a class array. */
10372 : 135 : sym->ts = target->ts;
10373 : : }
10374 : :
10375 : :
10376 : 7238 : if (target->expr_type == EXPR_NULL)
10377 : : {
10378 : 1 : gfc_error ("Selector at %L cannot be NULL()", &target->where);
10379 : 1 : return;
10380 : : }
10381 : 7237 : else if (target->ts.type == BT_UNKNOWN)
10382 : : {
10383 : 2 : gfc_error ("Selector at %L has no type", &target->where);
10384 : 2 : return;
10385 : : }
10386 : :
10387 : : /* Get type if this was not already set. Note that it can be
10388 : : some other type than the target in case this is a SELECT TYPE
10389 : : selector! So we must not update when the type is already there. */
10390 : 7235 : if (sym->ts.type == BT_UNKNOWN)
10391 : 220 : sym->ts = target->ts;
10392 : :
10393 : 7235 : gcc_assert (sym->ts.type != BT_UNKNOWN);
10394 : :
10395 : : /* See if this is a valid association-to-variable. */
10396 : 14470 : sym->assoc->variable = ((target->expr_type == EXPR_VARIABLE
10397 : 6381 : && !parentheses
10398 : 6360 : && !gfc_has_vector_subscript (target))
10399 : 7283 : || gfc_is_ptr_fcn (target));
10400 : :
10401 : : /* Finally resolve if this is an array or not. */
10402 : 7235 : if (target->expr_type == EXPR_FUNCTION && target->rank == 0
10403 : 131 : && (sym->ts.type == BT_CLASS || sym->ts.type == BT_DERIVED))
10404 : : {
10405 : 86 : gfc_expression_rank (target);
10406 : 86 : if (target->ts.type == BT_DERIVED
10407 : 39 : && !sym->as
10408 : 39 : && target->symtree->n.sym->as)
10409 : : {
10410 : 0 : sym->as = gfc_copy_array_spec (target->symtree->n.sym->as);
10411 : 0 : sym->attr.dimension = 1;
10412 : : }
10413 : 86 : else if (target->ts.type == BT_CLASS
10414 : 47 : && CLASS_DATA (target)->as)
10415 : : {
10416 : 0 : target->rank = CLASS_DATA (target)->as->rank;
10417 : 0 : target->corank = CLASS_DATA (target)->as->corank;
10418 : 0 : if (!(sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
10419 : : {
10420 : 0 : sym->ts = target->ts;
10421 : 0 : sym->attr.dimension = 0;
10422 : : }
10423 : : }
10424 : : }
10425 : :
10426 : :
10427 : 7235 : if (sym->attr.dimension && target->rank == 0)
10428 : : {
10429 : : /* primary.cc makes the assumption that a reference to an associate
10430 : : name followed by a left parenthesis is an array reference. */
10431 : 17 : if (sym->assoc->inferred_type && sym->ts.type != BT_CLASS)
10432 : : {
10433 : 12 : gfc_expression_rank (sym->assoc->target);
10434 : 12 : sym->attr.dimension = sym->assoc->target->rank ? 1 : 0;
10435 : 12 : if (!sym->attr.dimension && sym->as)
10436 : 0 : sym->as = NULL;
10437 : : }
10438 : :
10439 : 17 : if (sym->attr.dimension && target->rank == 0)
10440 : : {
10441 : 5 : if (sym->ts.type != BT_CHARACTER)
10442 : 5 : gfc_error ("Associate-name %qs at %L is used as array",
10443 : : sym->name, &sym->declared_at);
10444 : 5 : sym->attr.dimension = 0;
10445 : 5 : return;
10446 : : }
10447 : : }
10448 : :
10449 : : /* We cannot deal with class selectors that need temporaries. */
10450 : 7230 : if (target->ts.type == BT_CLASS
10451 : 7230 : && gfc_ref_needs_temporary_p (target->ref))
10452 : : {
10453 : 1 : gfc_error ("CLASS selector at %L needs a temporary which is not "
10454 : : "yet implemented", &target->where);
10455 : 1 : return;
10456 : : }
10457 : :
10458 : 7229 : if (target->ts.type == BT_CLASS)
10459 : 2680 : gfc_fix_class_refs (target);
10460 : :
10461 : 7229 : if ((target->rank > 0 || target->corank > 0)
10462 : 2623 : && !sym->attr.select_rank_temporary)
10463 : : {
10464 : 2623 : gfc_array_spec *as;
10465 : : /* The rank may be incorrectly guessed at parsing, therefore make sure
10466 : : it is corrected now. */
10467 : 2623 : if (sym->ts.type != BT_CLASS
10468 : 2068 : && (!sym->as || sym->as->corank != target->corank))
10469 : : {
10470 : 135 : if (!sym->as)
10471 : 128 : sym->as = gfc_get_array_spec ();
10472 : 135 : as = sym->as;
10473 : 135 : as->rank = target->rank;
10474 : 135 : as->type = AS_DEFERRED;
10475 : 135 : as->corank = target->corank;
10476 : 135 : sym->attr.dimension = 1;
10477 : 135 : if (as->corank != 0)
10478 : 7 : sym->attr.codimension = 1;
10479 : : }
10480 : 2488 : else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
10481 : 554 : && (!CLASS_DATA (sym)->as
10482 : 554 : || CLASS_DATA (sym)->as->corank != target->corank))
10483 : : {
10484 : 0 : if (!CLASS_DATA (sym)->as)
10485 : 0 : CLASS_DATA (sym)->as = gfc_get_array_spec ();
10486 : 0 : as = CLASS_DATA (sym)->as;
10487 : 0 : as->rank = target->rank;
10488 : 0 : as->type = AS_DEFERRED;
10489 : 0 : as->corank = target->corank;
10490 : 0 : CLASS_DATA (sym)->attr.dimension = 1;
10491 : 0 : if (as->corank != 0)
10492 : 0 : CLASS_DATA (sym)->attr.codimension = 1;
10493 : : }
10494 : : }
10495 : 4606 : else if (!sym->attr.select_rank_temporary)
10496 : : {
10497 : : /* target's rank is 0, but the type of the sym is still array valued,
10498 : : which has to be corrected. */
10499 : 3223 : if (sym->ts.type == BT_CLASS && sym->ts.u.derived
10500 : 657 : && CLASS_DATA (sym) && CLASS_DATA (sym)->as)
10501 : : {
10502 : 24 : gfc_array_spec *as;
10503 : 24 : symbol_attribute attr;
10504 : : /* The associated variable's type is still the array type
10505 : : correct this now. */
10506 : 24 : gfc_typespec *ts = &target->ts;
10507 : 24 : gfc_ref *ref;
10508 : : /* Internal_ref is true, when this is ref'ing only _data and co-ref.
10509 : : */
10510 : 24 : bool internal_ref = true;
10511 : :
10512 : 72 : for (ref = target->ref; ref != NULL; ref = ref->next)
10513 : : {
10514 : 48 : switch (ref->type)
10515 : : {
10516 : 24 : case REF_COMPONENT:
10517 : 24 : ts = &ref->u.c.component->ts;
10518 : 24 : internal_ref
10519 : 24 : = target->ref == ref && ref->next
10520 : 48 : && strncmp ("_data", ref->u.c.component->name, 5) == 0;
10521 : : break;
10522 : 24 : case REF_ARRAY:
10523 : 24 : if (ts->type == BT_CLASS)
10524 : 0 : ts = &ts->u.derived->components->ts;
10525 : 24 : if (internal_ref && ref->u.ar.codimen > 0)
10526 : 0 : for (int i = ref->u.ar.dimen;
10527 : : internal_ref
10528 : 0 : && i < ref->u.ar.dimen + ref->u.ar.codimen;
10529 : : ++i)
10530 : 0 : internal_ref
10531 : 0 : = ref->u.ar.dimen_type[i] == DIMEN_THIS_IMAGE;
10532 : : break;
10533 : : default:
10534 : : break;
10535 : : }
10536 : : }
10537 : : /* Only rewrite the type of this symbol, when the refs are not the
10538 : : internal ones for class and co-array this-image. */
10539 : 24 : if (!internal_ref)
10540 : : {
10541 : : /* Create a scalar instance of the current class type. Because
10542 : : the rank of a class array goes into its name, the type has to
10543 : : be rebuilt. The alternative of (re-)setting just the
10544 : : attributes and as in the current type, destroys the type also
10545 : : in other places. */
10546 : 0 : as = NULL;
10547 : 0 : sym->ts = *ts;
10548 : 0 : sym->ts.type = BT_CLASS;
10549 : 0 : attr = CLASS_DATA (sym) ? CLASS_DATA (sym)->attr : sym->attr;
10550 : 0 : gfc_change_class (&sym->ts, &attr, as, 0, 0);
10551 : 0 : sym->as = NULL;
10552 : : }
10553 : : }
10554 : : }
10555 : :
10556 : : /* Mark this as an associate variable. */
10557 : 7229 : sym->attr.associate_var = 1;
10558 : :
10559 : : /* Fix up the type-spec for CHARACTER types. */
10560 : 7229 : if (sym->ts.type == BT_CHARACTER && !sym->attr.select_type_temporary)
10561 : : {
10562 : 442 : gfc_ref *ref;
10563 : 709 : for (ref = target->ref; ref; ref = ref->next)
10564 : 293 : if (ref->type == REF_SUBSTRING
10565 : 68 : && (ref->u.ss.start == NULL
10566 : 68 : || ref->u.ss.start->expr_type != EXPR_CONSTANT
10567 : 68 : || ref->u.ss.end == NULL
10568 : 48 : || ref->u.ss.end->expr_type != EXPR_CONSTANT))
10569 : : break;
10570 : :
10571 : 442 : if (!sym->ts.u.cl)
10572 : 122 : sym->ts.u.cl = target->ts.u.cl;
10573 : :
10574 : 442 : if (sym->ts.deferred
10575 : 189 : && sym->ts.u.cl == target->ts.u.cl)
10576 : : {
10577 : 110 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10578 : 110 : sym->ts.deferred = 1;
10579 : : }
10580 : :
10581 : 442 : if (!sym->ts.u.cl->length
10582 : 326 : && !sym->ts.deferred
10583 : 137 : && target->expr_type == EXPR_CONSTANT)
10584 : : {
10585 : 30 : sym->ts.u.cl->length =
10586 : 30 : gfc_get_int_expr (gfc_charlen_int_kind, NULL,
10587 : 30 : target->value.character.length);
10588 : : }
10589 : 412 : else if (((!sym->ts.u.cl->length
10590 : 116 : || sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
10591 : 302 : && target->expr_type != EXPR_VARIABLE)
10592 : 290 : || ref)
10593 : : {
10594 : 148 : if (!sym->ts.deferred)
10595 : : {
10596 : 44 : sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
10597 : 44 : sym->ts.deferred = 1;
10598 : : }
10599 : :
10600 : : /* This is reset in trans-stmt.cc after the assignment
10601 : : of the target expression to the associate name. */
10602 : 148 : if (ref && sym->as)
10603 : 26 : sym->attr.pointer = 1;
10604 : : else
10605 : 122 : sym->attr.allocatable = 1;
10606 : : }
10607 : : }
10608 : :
10609 : 7229 : if (sym->ts.type == BT_CLASS
10610 : 1357 : && IS_INFERRED_TYPE (target)
10611 : 13 : && target->ts.type == BT_DERIVED
10612 : 0 : && CLASS_DATA (sym)->ts.u.derived == target->ts.u.derived
10613 : 0 : && target->ref && target->ref->next && !target->ref->next->next
10614 : 0 : && target->ref->next->type == REF_ARRAY)
10615 : 0 : target->ts = target->symtree->n.sym->ts;
10616 : :
10617 : : /* If the target is a good class object, so is the associate variable. */
10618 : 7229 : if (sym->ts.type == BT_CLASS && gfc_expr_attr (target).class_ok)
10619 : 670 : sym->attr.class_ok = 1;
10620 : : }
10621 : :
10622 : :
10623 : : /* Ensure that SELECT TYPE expressions have the correct rank and a full
10624 : : array reference, where necessary. The symbols are artificial and so
10625 : : the dimension attribute and arrayspec can also be set. In addition,
10626 : : sometimes the expr1 arrives as BT_DERIVED, when the symbol is BT_CLASS.
10627 : : This is corrected here as well.*/
10628 : :
10629 : : static void
10630 : 1644 : fixup_array_ref (gfc_expr **expr1, gfc_expr *expr2, int rank, int corank,
10631 : : gfc_ref *ref)
10632 : : {
10633 : 1644 : gfc_ref *nref = (*expr1)->ref;
10634 : 1644 : gfc_symbol *sym1 = (*expr1)->symtree->n.sym;
10635 : 1644 : gfc_symbol *sym2;
10636 : 1644 : gfc_expr *selector = gfc_copy_expr (expr2);
10637 : :
10638 : 1644 : (*expr1)->rank = rank;
10639 : 1644 : (*expr1)->corank = corank;
10640 : 1644 : if (selector)
10641 : : {
10642 : 309 : gfc_resolve_expr (selector);
10643 : 309 : if (selector->expr_type == EXPR_OP
10644 : 2 : && selector->value.op.op == INTRINSIC_PARENTHESES)
10645 : 2 : sym2 = selector->value.op.op1->symtree->n.sym;
10646 : 307 : else if (selector->expr_type == EXPR_VARIABLE
10647 : 7 : || selector->expr_type == EXPR_FUNCTION)
10648 : 307 : sym2 = selector->symtree->n.sym;
10649 : : else
10650 : 0 : gcc_unreachable ();
10651 : : }
10652 : : else
10653 : : sym2 = NULL;
10654 : :
10655 : 1644 : if (sym1->ts.type == BT_CLASS)
10656 : : {
10657 : 1644 : if ((*expr1)->ts.type != BT_CLASS)
10658 : 13 : (*expr1)->ts = sym1->ts;
10659 : :
10660 : 1644 : CLASS_DATA (sym1)->attr.dimension = rank > 0 ? 1 : 0;
10661 : 1644 : CLASS_DATA (sym1)->attr.codimension = corank > 0 ? 1 : 0;
10662 : 1644 : if (CLASS_DATA (sym1)->as == NULL && sym2)
10663 : 1 : CLASS_DATA (sym1)->as
10664 : 1 : = gfc_copy_array_spec (CLASS_DATA (sym2)->as);
10665 : : }
10666 : : else
10667 : : {
10668 : 0 : sym1->attr.dimension = rank > 0 ? 1 : 0;
10669 : 0 : sym1->attr.codimension = corank > 0 ? 1 : 0;
10670 : 0 : if (sym1->as == NULL && sym2)
10671 : 0 : sym1->as = gfc_copy_array_spec (sym2->as);
10672 : : }
10673 : :
10674 : 2973 : for (; nref; nref = nref->next)
10675 : 2664 : if (nref->next == NULL)
10676 : : break;
10677 : :
10678 : 1644 : if (ref && nref && nref->type != REF_ARRAY)
10679 : 6 : nref->next = gfc_copy_ref (ref);
10680 : 1638 : else if (ref && !nref)
10681 : 300 : (*expr1)->ref = gfc_copy_ref (ref);
10682 : 1338 : else if (ref && nref->u.ar.codimen != corank)
10683 : : {
10684 : 912 : for (int i = nref->u.ar.dimen; i < GFC_MAX_DIMENSIONS; ++i)
10685 : 855 : nref->u.ar.dimen_type[i] = DIMEN_THIS_IMAGE;
10686 : 57 : nref->u.ar.codimen = corank;
10687 : : }
10688 : 1644 : }
10689 : :
10690 : :
10691 : : static gfc_expr *
10692 : 6467 : build_loc_call (gfc_expr *sym_expr)
10693 : : {
10694 : 6467 : gfc_expr *loc_call;
10695 : 6467 : loc_call = gfc_get_expr ();
10696 : 6467 : loc_call->expr_type = EXPR_FUNCTION;
10697 : 6467 : gfc_get_sym_tree ("_loc", gfc_current_ns, &loc_call->symtree, false);
10698 : 6467 : loc_call->symtree->n.sym->attr.flavor = FL_PROCEDURE;
10699 : 6467 : loc_call->symtree->n.sym->attr.intrinsic = 1;
10700 : 6467 : loc_call->symtree->n.sym->result = loc_call->symtree->n.sym;
10701 : 6467 : gfc_commit_symbol (loc_call->symtree->n.sym);
10702 : 6467 : loc_call->ts.type = BT_INTEGER;
10703 : 6467 : loc_call->ts.kind = gfc_index_integer_kind;
10704 : 6467 : loc_call->value.function.isym = gfc_intrinsic_function_by_id (GFC_ISYM_LOC);
10705 : 6467 : loc_call->value.function.actual = gfc_get_actual_arglist ();
10706 : 6467 : loc_call->value.function.actual->expr = sym_expr;
10707 : 6467 : loc_call->where = sym_expr->where;
10708 : 6467 : return loc_call;
10709 : : }
10710 : :
10711 : : /* Resolve a SELECT TYPE statement. */
10712 : :
10713 : : static void
10714 : 2894 : resolve_select_type (gfc_code *code, gfc_namespace *old_ns)
10715 : : {
10716 : 2894 : gfc_symbol *selector_type;
10717 : 2894 : gfc_code *body, *new_st, *if_st, *tail;
10718 : 2894 : gfc_code *class_is = NULL, *default_case = NULL;
10719 : 2894 : gfc_case *c;
10720 : 2894 : gfc_symtree *st;
10721 : 2894 : char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
10722 : 2894 : gfc_namespace *ns;
10723 : 2894 : int error = 0;
10724 : 2894 : int rank = 0, corank = 0;
10725 : 2894 : gfc_ref* ref = NULL;
10726 : 2894 : gfc_expr *selector_expr = NULL;
10727 : 2894 : gfc_code *old_code = code;
10728 : :
10729 : 2894 : ns = code->ext.block.ns;
10730 : 2894 : if (code->expr2)
10731 : : {
10732 : : /* Set this, or coarray checks in resolve will fail. */
10733 : 617 : code->expr1->symtree->n.sym->attr.select_type_temporary = 1;
10734 : : }
10735 : 2894 : gfc_resolve (ns);
10736 : :
10737 : : /* Check for F03:C813. */
10738 : 2894 : if (code->expr1->ts.type != BT_CLASS
10739 : 36 : && !(code->expr2 && code->expr2->ts.type == BT_CLASS))
10740 : : {
10741 : 13 : gfc_error ("Selector shall be polymorphic in SELECT TYPE statement "
10742 : : "at %L", &code->loc);
10743 : 41 : return;
10744 : : }
10745 : :
10746 : : /* Prevent segfault, when class type is not initialized due to previous
10747 : : error. */
10748 : 2881 : if (!code->expr1->symtree->n.sym->attr.class_ok
10749 : 2879 : || (code->expr1->ts.type == BT_CLASS && !code->expr1->ts.u.derived))
10750 : : return;
10751 : :
10752 : 2874 : if (code->expr2)
10753 : : {
10754 : 608 : gfc_ref *ref2 = NULL;
10755 : 1434 : for (ref = code->expr2->ref; ref != NULL; ref = ref->next)
10756 : 826 : if (ref->type == REF_COMPONENT
10757 : 425 : && ref->u.c.component->ts.type == BT_CLASS)
10758 : 826 : ref2 = ref;
10759 : :
10760 : 608 : if (ref2)
10761 : : {
10762 : 333 : if (code->expr1->symtree->n.sym->attr.untyped)
10763 : 1 : code->expr1->symtree->n.sym->ts = ref2->u.c.component->ts;
10764 : 333 : selector_type = CLASS_DATA (ref2->u.c.component)->ts.u.derived;
10765 : : }
10766 : : else
10767 : : {
10768 : 275 : if (code->expr1->symtree->n.sym->attr.untyped)
10769 : 28 : code->expr1->symtree->n.sym->ts = code->expr2->ts;
10770 : : /* Sometimes the selector expression is given the typespec of the
10771 : : '_data' field, which is logical enough but inappropriate here. */
10772 : 275 : if (code->expr2->ts.type == BT_DERIVED
10773 : 80 : && code->expr2->symtree
10774 : 80 : && code->expr2->symtree->n.sym->ts.type == BT_CLASS)
10775 : 80 : code->expr2->ts = code->expr2->symtree->n.sym->ts;
10776 : 275 : selector_type = CLASS_DATA (code->expr2)
10777 : : ? CLASS_DATA (code->expr2)->ts.u.derived : code->expr2->ts.u.derived;
10778 : : }
10779 : :
10780 : 608 : if (code->expr1->ts.type == BT_CLASS && CLASS_DATA (code->expr1)->as)
10781 : : {
10782 : 295 : CLASS_DATA (code->expr1)->as->rank = code->expr2->rank;
10783 : 295 : CLASS_DATA (code->expr1)->as->corank = code->expr2->corank;
10784 : 295 : CLASS_DATA (code->expr1)->as->cotype = AS_DEFERRED;
10785 : : }
10786 : :
10787 : : /* F2008: C803 The selector expression must not be coindexed. */
10788 : 608 : if (gfc_is_coindexed (code->expr2))
10789 : : {
10790 : 3 : gfc_error ("Selector at %L must not be coindexed",
10791 : 3 : &code->expr2->where);
10792 : 3 : return;
10793 : : }
10794 : :
10795 : : }
10796 : : else
10797 : : {
10798 : 2266 : selector_type = CLASS_DATA (code->expr1)->ts.u.derived;
10799 : :
10800 : 2266 : if (gfc_is_coindexed (code->expr1))
10801 : : {
10802 : 0 : gfc_error ("Selector at %L must not be coindexed",
10803 : 0 : &code->expr1->where);
10804 : 0 : return;
10805 : : }
10806 : : }
10807 : :
10808 : : /* Loop over TYPE IS / CLASS IS cases. */
10809 : 8058 : for (body = code->block; body; body = body->block)
10810 : : {
10811 : 5188 : c = body->ext.block.case_list;
10812 : :
10813 : 5188 : if (!error)
10814 : : {
10815 : : /* Check for repeated cases. */
10816 : 8063 : for (tail = code->block; tail; tail = tail->block)
10817 : : {
10818 : 8063 : gfc_case *d = tail->ext.block.case_list;
10819 : 8063 : if (tail == body)
10820 : : break;
10821 : :
10822 : 2884 : if (c->ts.type == d->ts.type
10823 : 516 : && ((c->ts.type == BT_DERIVED
10824 : 418 : && c->ts.u.derived && d->ts.u.derived
10825 : 418 : && !strcmp (c->ts.u.derived->name,
10826 : : d->ts.u.derived->name))
10827 : 515 : || c->ts.type == BT_UNKNOWN
10828 : 515 : || (!(c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10829 : 55 : && c->ts.kind == d->ts.kind)))
10830 : : {
10831 : 1 : gfc_error ("TYPE IS at %L overlaps with TYPE IS at %L",
10832 : : &c->where, &d->where);
10833 : 1 : return;
10834 : : }
10835 : : }
10836 : : }
10837 : :
10838 : : /* Check F03:C815. */
10839 : 3275 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10840 : 2223 : && selector_type
10841 : 2223 : && !selector_type->attr.unlimited_polymorphic
10842 : 7113 : && !gfc_type_is_extensible (c->ts.u.derived))
10843 : : {
10844 : 1 : gfc_error ("Derived type %qs at %L must be extensible",
10845 : 1 : c->ts.u.derived->name, &c->where);
10846 : 1 : error++;
10847 : 1 : continue;
10848 : : }
10849 : :
10850 : : /* Check F03:C816. */
10851 : 5192 : if (c->ts.type != BT_UNKNOWN
10852 : 3612 : && selector_type && !selector_type->attr.unlimited_polymorphic
10853 : 7115 : && ((c->ts.type != BT_DERIVED && c->ts.type != BT_CLASS)
10854 : 1925 : || !gfc_type_is_extension_of (selector_type, c->ts.u.derived)))
10855 : : {
10856 : 6 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10857 : 2 : gfc_error ("Derived type %qs at %L must be an extension of %qs",
10858 : 2 : c->ts.u.derived->name, &c->where, selector_type->name);
10859 : : else
10860 : 4 : gfc_error ("Unexpected intrinsic type %qs at %L",
10861 : : gfc_basic_typename (c->ts.type), &c->where);
10862 : 6 : error++;
10863 : 6 : continue;
10864 : : }
10865 : :
10866 : : /* Check F03:C814. */
10867 : 5180 : if (c->ts.type == BT_CHARACTER
10868 : 730 : && (c->ts.u.cl->length != NULL || c->ts.deferred))
10869 : : {
10870 : 0 : gfc_error ("The type-spec at %L shall specify that each length "
10871 : : "type parameter is assumed", &c->where);
10872 : 0 : error++;
10873 : 0 : continue;
10874 : : }
10875 : :
10876 : : /* Intercept the DEFAULT case. */
10877 : 5180 : if (c->ts.type == BT_UNKNOWN)
10878 : : {
10879 : : /* Check F03:C818. */
10880 : 1574 : if (default_case)
10881 : : {
10882 : 1 : gfc_error ("The DEFAULT CASE at %L cannot be followed "
10883 : : "by a second DEFAULT CASE at %L",
10884 : 1 : &default_case->ext.block.case_list->where, &c->where);
10885 : 1 : error++;
10886 : 1 : continue;
10887 : : }
10888 : :
10889 : : default_case = body;
10890 : : }
10891 : : }
10892 : :
10893 : 2870 : if (error > 0)
10894 : : return;
10895 : :
10896 : : /* Transform SELECT TYPE statement to BLOCK and associate selector to
10897 : : target if present. If there are any EXIT statements referring to the
10898 : : SELECT TYPE construct, this is no problem because the gfc_code
10899 : : reference stays the same and EXIT is equally possible from the BLOCK
10900 : : it is changed to. */
10901 : 2867 : code->op = EXEC_BLOCK;
10902 : 2867 : if (code->expr2)
10903 : : {
10904 : 605 : gfc_association_list* assoc;
10905 : :
10906 : 605 : assoc = gfc_get_association_list ();
10907 : 605 : assoc->st = code->expr1->symtree;
10908 : 605 : assoc->target = gfc_copy_expr (code->expr2);
10909 : 605 : assoc->target->where = code->expr2->where;
10910 : : /* assoc->variable will be set by resolve_assoc_var. */
10911 : :
10912 : 605 : code->ext.block.assoc = assoc;
10913 : 605 : code->expr1->symtree->n.sym->assoc = assoc;
10914 : :
10915 : 605 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
10916 : : }
10917 : : else
10918 : 2262 : code->ext.block.assoc = NULL;
10919 : :
10920 : : /* Ensure that the selector rank and arrayspec are available to
10921 : : correct expressions in which they might be missing. */
10922 : 2867 : if (code->expr2 && (code->expr2->rank || code->expr2->corank))
10923 : : {
10924 : 309 : rank = code->expr2->rank;
10925 : 309 : corank = code->expr2->corank;
10926 : 582 : for (ref = code->expr2->ref; ref; ref = ref->next)
10927 : 573 : if (ref->next == NULL)
10928 : : break;
10929 : 309 : if (ref && ref->type == REF_ARRAY)
10930 : 300 : ref = gfc_copy_ref (ref);
10931 : :
10932 : : /* Fixup expr1 if necessary. */
10933 : 309 : if (rank || corank)
10934 : 309 : fixup_array_ref (&code->expr1, code->expr2, rank, corank, ref);
10935 : : }
10936 : 2558 : else if (code->expr1->rank || code->expr1->corank)
10937 : : {
10938 : 827 : rank = code->expr1->rank;
10939 : 827 : corank = code->expr1->corank;
10940 : 827 : for (ref = code->expr1->ref; ref; ref = ref->next)
10941 : 827 : if (ref->next == NULL)
10942 : : break;
10943 : 827 : if (ref && ref->type == REF_ARRAY)
10944 : 827 : ref = gfc_copy_ref (ref);
10945 : : }
10946 : :
10947 : 2867 : gfc_expr *orig_expr1 = code->expr1;
10948 : :
10949 : : /* Add EXEC_SELECT to switch on type. */
10950 : 2867 : new_st = gfc_get_code (code->op);
10951 : 2867 : new_st->expr1 = code->expr1;
10952 : 2867 : new_st->expr2 = code->expr2;
10953 : 2867 : new_st->block = code->block;
10954 : 2867 : code->expr1 = code->expr2 = NULL;
10955 : 2867 : code->block = NULL;
10956 : 2867 : if (!ns->code)
10957 : 2867 : ns->code = new_st;
10958 : : else
10959 : 0 : ns->code->next = new_st;
10960 : 2867 : code = new_st;
10961 : 2867 : code->op = EXEC_SELECT_TYPE;
10962 : :
10963 : : /* Use the intrinsic LOC function to generate an integer expression
10964 : : for the vtable of the selector. Note that the rank of the selector
10965 : : expression has to be set to zero. */
10966 : 2867 : gfc_add_vptr_component (code->expr1);
10967 : 2867 : code->expr1->rank = 0;
10968 : 2867 : code->expr1->corank = 0;
10969 : 2867 : code->expr1 = build_loc_call (code->expr1);
10970 : 2867 : selector_expr = code->expr1->value.function.actual->expr;
10971 : :
10972 : : /* Loop over TYPE IS / CLASS IS cases. */
10973 : 8039 : for (body = code->block; body; body = body->block)
10974 : : {
10975 : 5172 : gfc_symbol *vtab;
10976 : 5172 : c = body->ext.block.case_list;
10977 : :
10978 : : /* Generate an index integer expression for address of the
10979 : : TYPE/CLASS vtable and store it in c->low. The hash expression
10980 : : is stored in c->high and is used to resolve intrinsic cases. */
10981 : 5172 : if (c->ts.type != BT_UNKNOWN)
10982 : : {
10983 : 3600 : gfc_expr *e;
10984 : 3600 : if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
10985 : : {
10986 : 2214 : vtab = gfc_find_derived_vtab (c->ts.u.derived);
10987 : 2214 : gcc_assert (vtab);
10988 : 2214 : c->high = gfc_get_int_expr (gfc_integer_4_kind, NULL,
10989 : 2214 : c->ts.u.derived->hash_value);
10990 : : }
10991 : : else
10992 : : {
10993 : 1386 : vtab = gfc_find_vtab (&c->ts);
10994 : 1386 : gcc_assert (vtab && CLASS_DATA (vtab)->initializer);
10995 : 1386 : e = CLASS_DATA (vtab)->initializer;
10996 : 1386 : c->high = gfc_copy_expr (e);
10997 : 1386 : if (c->high->ts.kind != gfc_integer_4_kind)
10998 : : {
10999 : 1 : gfc_typespec ts;
11000 : 1 : ts.kind = gfc_integer_4_kind;
11001 : 1 : ts.type = BT_INTEGER;
11002 : 1 : gfc_convert_type_warn (c->high, &ts, 2, 0);
11003 : : }
11004 : : }
11005 : :
11006 : 3600 : e = gfc_lval_expr_from_sym (vtab);
11007 : 3600 : c->low = build_loc_call (e);
11008 : : }
11009 : : else
11010 : 1572 : continue;
11011 : :
11012 : : /* Associate temporary to selector. This should only be done
11013 : : when this case is actually true, so build a new ASSOCIATE
11014 : : that does precisely this here (instead of using the
11015 : : 'global' one). */
11016 : :
11017 : : /* First check the derived type import status. */
11018 : 3600 : if (gfc_current_ns->import_state != IMPORT_NOT_SET
11019 : 6 : && (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS))
11020 : : {
11021 : 12 : st = gfc_find_symtree (gfc_current_ns->sym_root,
11022 : 6 : c->ts.u.derived->name);
11023 : 6 : if (!check_sym_import_status (c->ts.u.derived, st, NULL, old_code,
11024 : : gfc_current_ns))
11025 : 6 : error++;
11026 : : }
11027 : :
11028 : 3600 : const char * var_name = gfc_var_name_for_select_type_temp (orig_expr1);
11029 : 3600 : if (c->ts.type == BT_CLASS)
11030 : 309 : snprintf (name, sizeof (name), "__tmp_class_%s_%s",
11031 : 309 : c->ts.u.derived->name, var_name);
11032 : 3291 : else if (c->ts.type == BT_DERIVED)
11033 : 1905 : snprintf (name, sizeof (name), "__tmp_type_%s_%s",
11034 : 1905 : c->ts.u.derived->name, var_name);
11035 : 1386 : else if (c->ts.type == BT_CHARACTER)
11036 : : {
11037 : 730 : HOST_WIDE_INT charlen = 0;
11038 : 730 : if (c->ts.u.cl && c->ts.u.cl->length
11039 : 0 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11040 : 0 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11041 : 730 : snprintf (name, sizeof (name),
11042 : : "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d_%s",
11043 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind,
11044 : : var_name);
11045 : : }
11046 : : else
11047 : 656 : snprintf (name, sizeof (name), "__tmp_%s_%d_%s",
11048 : : gfc_basic_typename (c->ts.type), c->ts.kind, var_name);
11049 : :
11050 : 3600 : st = gfc_find_symtree (ns->sym_root, name);
11051 : 3600 : gcc_assert (st->n.sym->assoc);
11052 : 3600 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11053 : 3600 : st->n.sym->assoc->target->where = selector_expr->where;
11054 : 3600 : if (c->ts.type != BT_CLASS && c->ts.type != BT_UNKNOWN)
11055 : : {
11056 : 3291 : gfc_add_data_component (st->n.sym->assoc->target);
11057 : : /* Fixup the target expression if necessary. */
11058 : 3291 : if (rank || corank)
11059 : 1335 : fixup_array_ref (&st->n.sym->assoc->target, nullptr, rank, corank,
11060 : : ref);
11061 : : }
11062 : :
11063 : 3600 : new_st = gfc_get_code (EXEC_BLOCK);
11064 : 3600 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11065 : 3600 : new_st->ext.block.ns->code = body->next;
11066 : 3600 : body->next = new_st;
11067 : :
11068 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11069 : : there is a CASE label overlap and this is already used. Just ignore,
11070 : : the error is diagnosed elsewhere. */
11071 : 3600 : if (st->n.sym->assoc->dangling)
11072 : : {
11073 : 3599 : new_st->ext.block.assoc = st->n.sym->assoc;
11074 : 3599 : st->n.sym->assoc->dangling = 0;
11075 : : }
11076 : :
11077 : 3600 : resolve_assoc_var (st->n.sym, false);
11078 : : }
11079 : :
11080 : : /* Take out CLASS IS cases for separate treatment. */
11081 : : body = code;
11082 : 8039 : while (body && body->block)
11083 : : {
11084 : 5172 : if (body->block->ext.block.case_list->ts.type == BT_CLASS)
11085 : : {
11086 : : /* Add to class_is list. */
11087 : 309 : if (class_is == NULL)
11088 : : {
11089 : 278 : class_is = body->block;
11090 : 278 : tail = class_is;
11091 : : }
11092 : : else
11093 : : {
11094 : 43 : for (tail = class_is; tail->block; tail = tail->block) ;
11095 : 31 : tail->block = body->block;
11096 : 31 : tail = tail->block;
11097 : : }
11098 : : /* Remove from EXEC_SELECT list. */
11099 : 309 : body->block = body->block->block;
11100 : 309 : tail->block = NULL;
11101 : : }
11102 : : else
11103 : : body = body->block;
11104 : : }
11105 : :
11106 : 2867 : if (class_is)
11107 : : {
11108 : 278 : gfc_symbol *vtab;
11109 : :
11110 : 278 : if (!default_case)
11111 : : {
11112 : : /* Add a default case to hold the CLASS IS cases. */
11113 : 276 : for (tail = code; tail->block; tail = tail->block) ;
11114 : 168 : tail->block = gfc_get_code (EXEC_SELECT_TYPE);
11115 : 168 : tail = tail->block;
11116 : 168 : tail->ext.block.case_list = gfc_get_case ();
11117 : 168 : tail->ext.block.case_list->ts.type = BT_UNKNOWN;
11118 : 168 : tail->next = NULL;
11119 : 168 : default_case = tail;
11120 : : }
11121 : :
11122 : : /* More than one CLASS IS block? */
11123 : 278 : if (class_is->block)
11124 : : {
11125 : 37 : gfc_code **c1,*c2;
11126 : 37 : bool swapped;
11127 : : /* Sort CLASS IS blocks by extension level. */
11128 : 36 : do
11129 : : {
11130 : 37 : swapped = false;
11131 : 97 : for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
11132 : : {
11133 : 61 : c2 = (*c1)->block;
11134 : : /* F03:C817 (check for doubles). */
11135 : 61 : if ((*c1)->ext.block.case_list->ts.u.derived->hash_value
11136 : 61 : == c2->ext.block.case_list->ts.u.derived->hash_value)
11137 : : {
11138 : 1 : gfc_error ("Double CLASS IS block in SELECT TYPE "
11139 : : "statement at %L",
11140 : : &c2->ext.block.case_list->where);
11141 : 1 : return;
11142 : : }
11143 : 60 : if ((*c1)->ext.block.case_list->ts.u.derived->attr.extension
11144 : 60 : < c2->ext.block.case_list->ts.u.derived->attr.extension)
11145 : : {
11146 : : /* Swap. */
11147 : 24 : (*c1)->block = c2->block;
11148 : 24 : c2->block = *c1;
11149 : 24 : *c1 = c2;
11150 : 24 : swapped = true;
11151 : : }
11152 : : }
11153 : : }
11154 : : while (swapped);
11155 : : }
11156 : :
11157 : : /* Generate IF chain. */
11158 : 277 : if_st = gfc_get_code (EXEC_IF);
11159 : 277 : new_st = if_st;
11160 : 584 : for (body = class_is; body; body = body->block)
11161 : : {
11162 : 307 : new_st->block = gfc_get_code (EXEC_IF);
11163 : 307 : new_st = new_st->block;
11164 : : /* Set up IF condition: Call _gfortran_is_extension_of. */
11165 : 307 : new_st->expr1 = gfc_get_expr ();
11166 : 307 : new_st->expr1->expr_type = EXPR_FUNCTION;
11167 : 307 : new_st->expr1->ts.type = BT_LOGICAL;
11168 : 307 : new_st->expr1->ts.kind = 4;
11169 : 307 : new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
11170 : 307 : new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
11171 : 307 : new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
11172 : : /* Set up arguments. */
11173 : 307 : new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
11174 : 307 : new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (selector_expr->symtree);
11175 : 307 : new_st->expr1->value.function.actual->expr->where = code->loc;
11176 : 307 : new_st->expr1->where = code->loc;
11177 : 307 : gfc_add_vptr_component (new_st->expr1->value.function.actual->expr);
11178 : 307 : vtab = gfc_find_derived_vtab (body->ext.block.case_list->ts.u.derived);
11179 : 307 : st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
11180 : 307 : new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
11181 : 307 : new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
11182 : 307 : new_st->expr1->value.function.actual->next->expr->where = code->loc;
11183 : : /* Set up types in formal arg list. */
11184 : 307 : new_st->expr1->value.function.isym->formal = XCNEW (gfc_intrinsic_arg);
11185 : 307 : new_st->expr1->value.function.isym->formal->ts = new_st->expr1->value.function.actual->expr->ts;
11186 : 307 : new_st->expr1->value.function.isym->formal->next = XCNEW (gfc_intrinsic_arg);
11187 : 307 : new_st->expr1->value.function.isym->formal->next->ts = new_st->expr1->value.function.actual->next->expr->ts;
11188 : :
11189 : 307 : new_st->next = body->next;
11190 : : }
11191 : 277 : if (default_case->next)
11192 : : {
11193 : 110 : new_st->block = gfc_get_code (EXEC_IF);
11194 : 110 : new_st = new_st->block;
11195 : 110 : new_st->next = default_case->next;
11196 : : }
11197 : :
11198 : : /* Replace CLASS DEFAULT code by the IF chain. */
11199 : 277 : default_case->next = if_st;
11200 : : }
11201 : :
11202 : : /* Resolve the internal code. This cannot be done earlier because
11203 : : it requires that the sym->assoc of selectors is set already. */
11204 : 2866 : gfc_current_ns = ns;
11205 : 2866 : gfc_resolve_blocks (code->block, gfc_current_ns);
11206 : 2866 : gfc_current_ns = old_ns;
11207 : :
11208 : 2866 : free (ref);
11209 : : }
11210 : :
11211 : :
11212 : : /* Resolve a SELECT RANK statement. */
11213 : :
11214 : : static void
11215 : 1018 : resolve_select_rank (gfc_code *code, gfc_namespace *old_ns)
11216 : : {
11217 : 1018 : gfc_namespace *ns;
11218 : 1018 : gfc_code *body, *new_st, *tail;
11219 : 1018 : gfc_case *c;
11220 : 1018 : char tname[GFC_MAX_SYMBOL_LEN + 7];
11221 : 1018 : char name[2 * GFC_MAX_SYMBOL_LEN];
11222 : 1018 : gfc_symtree *st;
11223 : 1018 : gfc_expr *selector_expr = NULL;
11224 : 1018 : int case_value;
11225 : 1018 : HOST_WIDE_INT charlen = 0;
11226 : :
11227 : 1018 : ns = code->ext.block.ns;
11228 : 1018 : gfc_resolve (ns);
11229 : :
11230 : 1018 : code->op = EXEC_BLOCK;
11231 : 1018 : if (code->expr2)
11232 : : {
11233 : 42 : gfc_association_list* assoc;
11234 : :
11235 : 42 : assoc = gfc_get_association_list ();
11236 : 42 : assoc->st = code->expr1->symtree;
11237 : 42 : assoc->target = gfc_copy_expr (code->expr2);
11238 : 42 : assoc->target->where = code->expr2->where;
11239 : : /* assoc->variable will be set by resolve_assoc_var. */
11240 : :
11241 : 42 : code->ext.block.assoc = assoc;
11242 : 42 : code->expr1->symtree->n.sym->assoc = assoc;
11243 : :
11244 : 42 : resolve_assoc_var (code->expr1->symtree->n.sym, false);
11245 : : }
11246 : : else
11247 : 976 : code->ext.block.assoc = NULL;
11248 : :
11249 : : /* Loop over RANK cases. Note that returning on the errors causes a
11250 : : cascade of further errors because the case blocks do not compile
11251 : : correctly. */
11252 : 3320 : for (body = code->block; body; body = body->block)
11253 : : {
11254 : 2302 : c = body->ext.block.case_list;
11255 : 2302 : if (c->low)
11256 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11257 : : else
11258 : : case_value = -2;
11259 : :
11260 : : /* Check for repeated cases. */
11261 : 5836 : for (tail = code->block; tail; tail = tail->block)
11262 : : {
11263 : 5836 : gfc_case *d = tail->ext.block.case_list;
11264 : 5836 : int case_value2;
11265 : :
11266 : 5836 : if (tail == body)
11267 : : break;
11268 : :
11269 : : /* Check F2018: C1153. */
11270 : 3534 : if (!c->low && !d->low)
11271 : 1 : gfc_error ("RANK DEFAULT at %L is repeated at %L",
11272 : : &c->where, &d->where);
11273 : :
11274 : 3534 : if (!c->low || !d->low)
11275 : 1253 : continue;
11276 : :
11277 : : /* Check F2018: C1153. */
11278 : 2281 : case_value2 = (int) mpz_get_si (d->low->value.integer);
11279 : 2281 : if ((case_value == case_value2) && case_value == -1)
11280 : 1 : gfc_error ("RANK (*) at %L is repeated at %L",
11281 : : &c->where, &d->where);
11282 : 2280 : else if (case_value == case_value2)
11283 : 1 : gfc_error ("RANK (%i) at %L is repeated at %L",
11284 : : case_value, &c->where, &d->where);
11285 : : }
11286 : :
11287 : 2302 : if (!c->low)
11288 : 919 : continue;
11289 : :
11290 : : /* Check F2018: C1155. */
11291 : 1383 : if (case_value == -1 && (gfc_expr_attr (code->expr1).allocatable
11292 : 1381 : || gfc_expr_attr (code->expr1).pointer))
11293 : 3 : gfc_error ("RANK (*) at %L cannot be used with the pointer or "
11294 : 3 : "allocatable selector at %L", &c->where, &code->expr1->where);
11295 : : }
11296 : :
11297 : : /* Add EXEC_SELECT to switch on rank. */
11298 : 1018 : new_st = gfc_get_code (code->op);
11299 : 1018 : new_st->expr1 = code->expr1;
11300 : 1018 : new_st->expr2 = code->expr2;
11301 : 1018 : new_st->block = code->block;
11302 : 1018 : code->expr1 = code->expr2 = NULL;
11303 : 1018 : code->block = NULL;
11304 : 1018 : if (!ns->code)
11305 : 1018 : ns->code = new_st;
11306 : : else
11307 : 0 : ns->code->next = new_st;
11308 : 1018 : code = new_st;
11309 : 1018 : code->op = EXEC_SELECT_RANK;
11310 : :
11311 : 1018 : selector_expr = code->expr1;
11312 : :
11313 : : /* Loop over SELECT RANK cases. */
11314 : 3320 : for (body = code->block; body; body = body->block)
11315 : : {
11316 : 2302 : c = body->ext.block.case_list;
11317 : 2302 : int case_value;
11318 : :
11319 : : /* Pass on the default case. */
11320 : 2302 : if (c->low == NULL)
11321 : 919 : continue;
11322 : :
11323 : : /* Associate temporary to selector. This should only be done
11324 : : when this case is actually true, so build a new ASSOCIATE
11325 : : that does precisely this here (instead of using the
11326 : : 'global' one). */
11327 : 1383 : if (c->ts.type == BT_CHARACTER && c->ts.u.cl && c->ts.u.cl->length
11328 : 265 : && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
11329 : 186 : charlen = gfc_mpz_get_hwi (c->ts.u.cl->length->value.integer);
11330 : :
11331 : 1383 : if (c->ts.type == BT_CLASS)
11332 : 145 : sprintf (tname, "class_%s", c->ts.u.derived->name);
11333 : 1238 : else if (c->ts.type == BT_DERIVED)
11334 : 110 : sprintf (tname, "type_%s", c->ts.u.derived->name);
11335 : 1128 : else if (c->ts.type != BT_CHARACTER)
11336 : 569 : sprintf (tname, "%s_%d", gfc_basic_typename (c->ts.type), c->ts.kind);
11337 : : else
11338 : 559 : sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
11339 : : gfc_basic_typename (c->ts.type), charlen, c->ts.kind);
11340 : :
11341 : 1383 : case_value = (int) mpz_get_si (c->low->value.integer);
11342 : 1383 : if (case_value >= 0)
11343 : 1350 : sprintf (name, "__tmp_%s_rank_%d", tname, case_value);
11344 : : else
11345 : 33 : sprintf (name, "__tmp_%s_rank_m%d", tname, -case_value);
11346 : :
11347 : 1383 : st = gfc_find_symtree (ns->sym_root, name);
11348 : 1383 : gcc_assert (st->n.sym->assoc);
11349 : :
11350 : 1383 : st->n.sym->assoc->target = gfc_get_variable_expr (selector_expr->symtree);
11351 : 1383 : st->n.sym->assoc->target->where = selector_expr->where;
11352 : :
11353 : 1383 : new_st = gfc_get_code (EXEC_BLOCK);
11354 : 1383 : new_st->ext.block.ns = gfc_build_block_ns (ns);
11355 : 1383 : new_st->ext.block.ns->code = body->next;
11356 : 1383 : body->next = new_st;
11357 : :
11358 : : /* Chain in the new list only if it is marked as dangling. Otherwise
11359 : : there is a CASE label overlap and this is already used. Just ignore,
11360 : : the error is diagnosed elsewhere. */
11361 : 1383 : if (st->n.sym->assoc->dangling)
11362 : : {
11363 : 1381 : new_st->ext.block.assoc = st->n.sym->assoc;
11364 : 1381 : st->n.sym->assoc->dangling = 0;
11365 : : }
11366 : :
11367 : 1383 : resolve_assoc_var (st->n.sym, false);
11368 : : }
11369 : :
11370 : 1018 : gfc_current_ns = ns;
11371 : 1018 : gfc_resolve_blocks (code->block, gfc_current_ns);
11372 : 1018 : gfc_current_ns = old_ns;
11373 : 1018 : }
11374 : :
11375 : :
11376 : : /* Resolve a transfer statement. This is making sure that:
11377 : : -- a derived type being transferred has only non-pointer components
11378 : : -- a derived type being transferred doesn't have private components, unless
11379 : : it's being transferred from the module where the type was defined
11380 : : -- we're not trying to transfer a whole assumed size array. */
11381 : :
11382 : : static void
11383 : 45098 : resolve_transfer (gfc_code *code)
11384 : : {
11385 : 45098 : gfc_symbol *sym, *derived;
11386 : 45098 : gfc_ref *ref;
11387 : 45098 : gfc_expr *exp;
11388 : 45098 : bool write = false;
11389 : 45098 : bool formatted = false;
11390 : 45098 : gfc_dt *dt = code->ext.dt;
11391 : 45098 : gfc_symbol *dtio_sub = NULL;
11392 : :
11393 : 45098 : exp = code->expr1;
11394 : :
11395 : 90202 : while (exp != NULL && exp->expr_type == EXPR_OP
11396 : 45984 : && exp->value.op.op == INTRINSIC_PARENTHESES)
11397 : 6 : exp = exp->value.op.op1;
11398 : :
11399 : 45098 : if (exp && exp->expr_type == EXPR_NULL
11400 : 2 : && code->ext.dt)
11401 : : {
11402 : 2 : gfc_error ("Invalid context for NULL () intrinsic at %L",
11403 : : &exp->where);
11404 : 2 : return;
11405 : : }
11406 : :
11407 : : if (exp == NULL || (exp->expr_type != EXPR_VARIABLE
11408 : : && exp->expr_type != EXPR_FUNCTION
11409 : : && exp->expr_type != EXPR_ARRAY
11410 : : && exp->expr_type != EXPR_STRUCTURE))
11411 : : return;
11412 : :
11413 : : /* If we are reading, the variable will be changed. Note that
11414 : : code->ext.dt may be NULL if the TRANSFER is related to
11415 : : an INQUIRE statement -- but in this case, we are not reading, either. */
11416 : 24505 : if (dt && dt->dt_io_kind->value.iokind == M_READ
11417 : 31871 : && !gfc_check_vardef_context (exp, false, false, false,
11418 : 7218 : _("item in READ")))
11419 : : return;
11420 : :
11421 : 24649 : const gfc_typespec *ts = exp->expr_type == EXPR_STRUCTURE
11422 : 24649 : || exp->expr_type == EXPR_FUNCTION
11423 : 20308 : || exp->expr_type == EXPR_ARRAY
11424 : 44957 : ? &exp->ts : &exp->symtree->n.sym->ts;
11425 : :
11426 : : /* Go to actual component transferred. */
11427 : 31870 : for (ref = exp->ref; ref; ref = ref->next)
11428 : 7221 : if (ref->type == REF_COMPONENT)
11429 : 2126 : ts = &ref->u.c.component->ts;
11430 : :
11431 : 24649 : if (dt && dt->dt_io_kind->value.iokind != M_INQUIRE
11432 : 24501 : && (ts->type == BT_DERIVED || ts->type == BT_CLASS))
11433 : : {
11434 : 686 : derived = ts->u.derived;
11435 : :
11436 : : /* Determine when to use the formatted DTIO procedure. */
11437 : 686 : if (dt && (dt->format_expr || dt->format_label))
11438 : 611 : formatted = true;
11439 : :
11440 : 686 : write = dt->dt_io_kind->value.iokind == M_WRITE
11441 : 686 : || dt->dt_io_kind->value.iokind == M_PRINT;
11442 : 686 : dtio_sub = gfc_find_specific_dtio_proc (derived, write, formatted);
11443 : :
11444 : 686 : if (dtio_sub != NULL && exp->expr_type == EXPR_VARIABLE)
11445 : : {
11446 : 425 : dt->udtio = exp;
11447 : 425 : sym = exp->symtree->n.sym->ns->proc_name;
11448 : : /* Check to see if this is a nested DTIO call, with the
11449 : : dummy as the io-list object. */
11450 : 425 : if (sym && sym == dtio_sub && sym->formal
11451 : 30 : && sym->formal->sym == exp->symtree->n.sym
11452 : 30 : && exp->ref == NULL)
11453 : : {
11454 : 0 : if (!sym->attr.recursive)
11455 : : {
11456 : 0 : gfc_error ("DTIO %s procedure at %L must be recursive",
11457 : : sym->name, &sym->declared_at);
11458 : 0 : return;
11459 : : }
11460 : : }
11461 : : }
11462 : : }
11463 : :
11464 : 24649 : if (ts->type == BT_CLASS && dtio_sub == NULL)
11465 : : {
11466 : 3 : gfc_error ("Data transfer element at %L cannot be polymorphic unless "
11467 : : "it is processed by a defined input/output procedure",
11468 : : &code->loc);
11469 : 3 : return;
11470 : : }
11471 : :
11472 : 24646 : if (ts->type == BT_DERIVED)
11473 : : {
11474 : : /* Check that transferred derived type doesn't contain POINTER
11475 : : components unless it is processed by a defined input/output
11476 : : procedure". */
11477 : 654 : if (ts->u.derived->attr.pointer_comp && dtio_sub == NULL)
11478 : : {
11479 : 2 : gfc_error ("Data transfer element at %L cannot have POINTER "
11480 : : "components unless it is processed by a defined "
11481 : : "input/output procedure", &code->loc);
11482 : 2 : return;
11483 : : }
11484 : :
11485 : : /* F08:C935. */
11486 : 652 : if (ts->u.derived->attr.proc_pointer_comp)
11487 : : {
11488 : 2 : gfc_error ("Data transfer element at %L cannot have "
11489 : : "procedure pointer components", &code->loc);
11490 : 2 : return;
11491 : : }
11492 : :
11493 : 650 : if (ts->u.derived->attr.alloc_comp && dtio_sub == NULL)
11494 : : {
11495 : 6 : gfc_error ("Data transfer element at %L cannot have ALLOCATABLE "
11496 : : "components unless it is processed by a defined "
11497 : : "input/output procedure", &code->loc);
11498 : 6 : return;
11499 : : }
11500 : :
11501 : : /* C_PTR and C_FUNPTR have private components which means they cannot
11502 : : be printed. However, if -std=gnu and not -pedantic, allow
11503 : : the component to be printed to help debugging. */
11504 : 644 : if (ts->u.derived->ts.f90_type == BT_VOID)
11505 : : {
11506 : 4 : gfc_error ("Data transfer element at %L "
11507 : : "cannot have PRIVATE components", &code->loc);
11508 : 4 : return;
11509 : : }
11510 : 640 : else if (derived_inaccessible (ts->u.derived) && dtio_sub == NULL)
11511 : : {
11512 : 4 : gfc_error ("Data transfer element at %L cannot have "
11513 : : "PRIVATE components unless it is processed by "
11514 : : "a defined input/output procedure", &code->loc);
11515 : 4 : return;
11516 : : }
11517 : : }
11518 : :
11519 : 24628 : if (exp->expr_type == EXPR_STRUCTURE)
11520 : : return;
11521 : :
11522 : 24583 : if (exp->expr_type == EXPR_ARRAY)
11523 : : return;
11524 : :
11525 : 24207 : sym = exp->symtree->n.sym;
11526 : :
11527 : 24207 : if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE && exp->ref
11528 : 75 : && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
11529 : : {
11530 : 1 : gfc_error ("Data transfer element at %L cannot be a full reference to "
11531 : : "an assumed-size array", &code->loc);
11532 : 1 : return;
11533 : : }
11534 : : }
11535 : :
11536 : :
11537 : : /*********** Toplevel code resolution subroutines ***********/
11538 : :
11539 : : /* Find the set of labels that are reachable from this block. We also
11540 : : record the last statement in each block. */
11541 : :
11542 : : static void
11543 : 657470 : find_reachable_labels (gfc_code *block)
11544 : : {
11545 : 657470 : gfc_code *c;
11546 : :
11547 : 657470 : if (!block)
11548 : : return;
11549 : :
11550 : 413118 : cs_base->reachable_labels = bitmap_alloc (&labels_obstack);
11551 : :
11552 : : /* Collect labels in this block. We don't keep those corresponding
11553 : : to END {IF|SELECT}, these are checked in resolve_branch by going
11554 : : up through the code_stack. */
11555 : 1515881 : for (c = block; c; c = c->next)
11556 : : {
11557 : 1102763 : if (c->here && c->op != EXEC_END_NESTED_BLOCK)
11558 : 3589 : bitmap_set_bit (cs_base->reachable_labels, c->here->value);
11559 : : }
11560 : :
11561 : : /* Merge with labels from parent block. */
11562 : 413118 : if (cs_base->prev)
11563 : : {
11564 : 339644 : gcc_assert (cs_base->prev->reachable_labels);
11565 : 339644 : bitmap_ior_into (cs_base->reachable_labels,
11566 : : cs_base->prev->reachable_labels);
11567 : : }
11568 : : }
11569 : :
11570 : : static void
11571 : 136 : resolve_lock_unlock_event (gfc_code *code)
11572 : : {
11573 : 136 : if ((code->op == EXEC_LOCK || code->op == EXEC_UNLOCK)
11574 : 136 : && (code->expr1->ts.type != BT_DERIVED
11575 : 95 : || code->expr1->expr_type != EXPR_VARIABLE
11576 : 95 : || code->expr1->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11577 : 94 : || code->expr1->ts.u.derived->intmod_sym_id != ISOFORTRAN_LOCK_TYPE
11578 : 94 : || code->expr1->rank != 0
11579 : 124 : || (!gfc_is_coarray (code->expr1) &&
11580 : 31 : !gfc_is_coindexed (code->expr1))))
11581 : 4 : gfc_error ("Lock variable at %L must be a scalar of type LOCK_TYPE",
11582 : 4 : &code->expr1->where);
11583 : 132 : else if ((code->op == EXEC_EVENT_POST || code->op == EXEC_EVENT_WAIT)
11584 : 39 : && (code->expr1->ts.type != BT_DERIVED
11585 : 39 : || code->expr1->expr_type != EXPR_VARIABLE
11586 : 39 : || code->expr1->ts.u.derived->from_intmod
11587 : : != INTMOD_ISO_FORTRAN_ENV
11588 : 39 : || code->expr1->ts.u.derived->intmod_sym_id
11589 : : != ISOFORTRAN_EVENT_TYPE
11590 : 39 : || code->expr1->rank != 0))
11591 : 0 : gfc_error ("Event variable at %L must be a scalar of type EVENT_TYPE",
11592 : : &code->expr1->where);
11593 : 23 : else if (code->op == EXEC_EVENT_POST && !gfc_is_coarray (code->expr1)
11594 : 143 : && !gfc_is_coindexed (code->expr1))
11595 : 0 : gfc_error ("Event variable argument at %L must be a coarray or coindexed",
11596 : 0 : &code->expr1->where);
11597 : 132 : else if (code->op == EXEC_EVENT_WAIT && !gfc_is_coarray (code->expr1))
11598 : 0 : gfc_error ("Event variable argument at %L must be a coarray but not "
11599 : 0 : "coindexed", &code->expr1->where);
11600 : :
11601 : : /* Check STAT. */
11602 : 136 : if (code->expr2
11603 : 38 : && (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0
11604 : 38 : || code->expr2->expr_type != EXPR_VARIABLE))
11605 : 0 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11606 : : &code->expr2->where);
11607 : :
11608 : 136 : if (code->expr2
11609 : 174 : && !gfc_check_vardef_context (code->expr2, false, false, false,
11610 : 38 : _("STAT variable")))
11611 : : return;
11612 : :
11613 : : /* Check ERRMSG. */
11614 : 136 : if (code->expr3
11615 : 2 : && (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0
11616 : 2 : || code->expr3->expr_type != EXPR_VARIABLE))
11617 : 0 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11618 : : &code->expr3->where);
11619 : :
11620 : 136 : if (code->expr3
11621 : 138 : && !gfc_check_vardef_context (code->expr3, false, false, false,
11622 : 2 : _("ERRMSG variable")))
11623 : : return;
11624 : :
11625 : : /* Check for LOCK the ACQUIRED_LOCK. */
11626 : 136 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11627 : 16 : && (code->expr4->ts.type != BT_LOGICAL || code->expr4->rank != 0
11628 : 16 : || code->expr4->expr_type != EXPR_VARIABLE))
11629 : 0 : gfc_error ("ACQUIRED_LOCK= argument at %L must be a scalar LOGICAL "
11630 : : "variable", &code->expr4->where);
11631 : :
11632 : 120 : if (code->op != EXEC_EVENT_WAIT && code->expr4
11633 : 152 : && !gfc_check_vardef_context (code->expr4, false, false, false,
11634 : 16 : _("ACQUIRED_LOCK variable")))
11635 : : return;
11636 : :
11637 : : /* Check for EVENT WAIT the UNTIL_COUNT. */
11638 : 136 : if (code->op == EXEC_EVENT_WAIT && code->expr4)
11639 : : {
11640 : 24 : if (!gfc_resolve_expr (code->expr4) || code->expr4->ts.type != BT_INTEGER
11641 : 24 : || code->expr4->rank != 0)
11642 : 0 : gfc_error ("UNTIL_COUNT= argument at %L must be a scalar INTEGER "
11643 : 0 : "expression", &code->expr4->where);
11644 : : }
11645 : : }
11646 : :
11647 : : static void
11648 : 195 : resolve_team_argument (gfc_expr *team)
11649 : : {
11650 : 195 : gfc_resolve_expr (team);
11651 : 195 : if (team->rank != 0 || team->ts.type != BT_DERIVED
11652 : 188 : || team->ts.u.derived->from_intmod != INTMOD_ISO_FORTRAN_ENV
11653 : 188 : || team->ts.u.derived->intmod_sym_id != ISOFORTRAN_TEAM_TYPE)
11654 : : {
11655 : 7 : gfc_error ("TEAM argument at %L must be a scalar expression "
11656 : : "of type TEAM_TYPE from the intrinsic module ISO_FORTRAN_ENV",
11657 : : &team->where);
11658 : : }
11659 : 195 : }
11660 : :
11661 : : static void
11662 : 1228 : resolve_scalar_variable_as_arg (const char *name, bt exp_type, int exp_kind,
11663 : : gfc_expr *e)
11664 : : {
11665 : 1228 : gfc_resolve_expr (e);
11666 : 1228 : if (e
11667 : 139 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0
11668 : 124 : || e->expr_type != EXPR_VARIABLE))
11669 : 15 : gfc_error ("%s argument at %L must be a scalar %s variable of at least "
11670 : : "kind %d", name, &e->where, gfc_basic_typename (exp_type),
11671 : : exp_kind);
11672 : 1228 : }
11673 : :
11674 : : void
11675 : 614 : gfc_resolve_sync_stat (struct sync_stat *sync_stat)
11676 : : {
11677 : 614 : resolve_scalar_variable_as_arg ("STAT=", BT_INTEGER, 2, sync_stat->stat);
11678 : 614 : resolve_scalar_variable_as_arg ("ERRMSG=", BT_CHARACTER,
11679 : : gfc_default_character_kind,
11680 : : sync_stat->errmsg);
11681 : 614 : }
11682 : :
11683 : : static void
11684 : 214 : resolve_scalar_argument (const char *name, bt exp_type, int exp_kind,
11685 : : gfc_expr *e)
11686 : : {
11687 : 214 : gfc_resolve_expr (e);
11688 : 214 : if (e
11689 : 140 : && (e->ts.type != exp_type || e->ts.kind < exp_kind || e->rank != 0))
11690 : 3 : gfc_error ("%s argument at %L must be a scalar %s of at least kind %d",
11691 : : name, &e->where, gfc_basic_typename (exp_type), exp_kind);
11692 : 214 : }
11693 : :
11694 : : static void
11695 : 107 : resolve_form_team (gfc_code *code)
11696 : : {
11697 : 107 : resolve_scalar_argument ("TEAM NUMBER", BT_INTEGER, gfc_default_integer_kind,
11698 : : code->expr1);
11699 : 107 : resolve_team_argument (code->expr2);
11700 : 107 : resolve_scalar_argument ("NEW_INDEX=", BT_INTEGER, gfc_default_integer_kind,
11701 : : code->expr3);
11702 : 107 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11703 : 107 : }
11704 : :
11705 : : static void resolve_block_construct (gfc_code *);
11706 : :
11707 : : static void
11708 : 66 : resolve_change_team (gfc_code *code)
11709 : : {
11710 : 66 : resolve_team_argument (code->expr1);
11711 : 66 : gfc_resolve_sync_stat (&code->ext.block.sync_stat);
11712 : 132 : resolve_block_construct (code);
11713 : : /* Map the coarray bounds as selected. */
11714 : 68 : for (gfc_association_list *a = code->ext.block.assoc; a; a = a->next)
11715 : 2 : if (a->ar)
11716 : : {
11717 : 2 : gfc_array_spec *src = a->ar->as, *dst;
11718 : 2 : if (a->st->n.sym->ts.type == BT_CLASS)
11719 : 0 : dst = CLASS_DATA (a->st->n.sym)->as;
11720 : : else
11721 : 2 : dst = a->st->n.sym->as;
11722 : 2 : dst->corank = src->corank;
11723 : 2 : dst->cotype = src->cotype;
11724 : 4 : for (int i = 0; i < src->corank; ++i)
11725 : : {
11726 : 2 : dst->lower[dst->rank + i] = src->lower[i];
11727 : 2 : dst->upper[dst->rank + i] = src->upper[i];
11728 : 2 : src->lower[i] = src->upper[i] = nullptr;
11729 : : }
11730 : 2 : gfc_free_array_spec (src);
11731 : 2 : free (a->ar);
11732 : 2 : a->ar = nullptr;
11733 : 2 : dst->resolved = false;
11734 : 2 : gfc_resolve_array_spec (dst, 0);
11735 : : }
11736 : 66 : }
11737 : :
11738 : : static void
11739 : 22 : resolve_sync_team (gfc_code *code)
11740 : : {
11741 : 22 : resolve_team_argument (code->expr1);
11742 : 22 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11743 : 22 : }
11744 : :
11745 : : static void
11746 : 64 : resolve_end_team (gfc_code *code)
11747 : : {
11748 : 64 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11749 : 64 : }
11750 : :
11751 : : static void
11752 : 50 : resolve_critical (gfc_code *code)
11753 : : {
11754 : 50 : gfc_symtree *symtree;
11755 : 50 : gfc_symbol *lock_type;
11756 : 50 : char name[GFC_MAX_SYMBOL_LEN];
11757 : 50 : static int serial = 0;
11758 : :
11759 : 50 : gfc_resolve_sync_stat (&code->ext.sync_stat);
11760 : :
11761 : 50 : if (flag_coarray != GFC_FCOARRAY_LIB)
11762 : 30 : return;
11763 : :
11764 : 20 : symtree = gfc_find_symtree (gfc_current_ns->sym_root,
11765 : : GFC_PREFIX ("lock_type"));
11766 : 20 : if (symtree)
11767 : 10 : lock_type = symtree->n.sym;
11768 : : else
11769 : : {
11770 : 10 : if (gfc_get_sym_tree (GFC_PREFIX ("lock_type"), gfc_current_ns, &symtree,
11771 : : false) != 0)
11772 : 0 : gcc_unreachable ();
11773 : 10 : lock_type = symtree->n.sym;
11774 : 10 : lock_type->attr.flavor = FL_DERIVED;
11775 : 10 : lock_type->attr.zero_comp = 1;
11776 : 10 : lock_type->from_intmod = INTMOD_ISO_FORTRAN_ENV;
11777 : 10 : lock_type->intmod_sym_id = ISOFORTRAN_LOCK_TYPE;
11778 : : }
11779 : :
11780 : 20 : sprintf(name, GFC_PREFIX ("lock_var") "%d",serial++);
11781 : 20 : if (gfc_get_sym_tree (name, gfc_current_ns, &symtree, false) != 0)
11782 : 0 : gcc_unreachable ();
11783 : :
11784 : 20 : code->resolved_sym = symtree->n.sym;
11785 : 20 : symtree->n.sym->attr.flavor = FL_VARIABLE;
11786 : 20 : symtree->n.sym->attr.referenced = 1;
11787 : 20 : symtree->n.sym->attr.artificial = 1;
11788 : 20 : symtree->n.sym->attr.codimension = 1;
11789 : 20 : symtree->n.sym->ts.type = BT_DERIVED;
11790 : 20 : symtree->n.sym->ts.u.derived = lock_type;
11791 : 20 : symtree->n.sym->as = gfc_get_array_spec ();
11792 : 20 : symtree->n.sym->as->corank = 1;
11793 : 20 : symtree->n.sym->as->type = AS_EXPLICIT;
11794 : 20 : symtree->n.sym->as->cotype = AS_EXPLICIT;
11795 : 20 : symtree->n.sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind,
11796 : : NULL, 1);
11797 : 20 : gfc_commit_symbols();
11798 : : }
11799 : :
11800 : :
11801 : : static void
11802 : 747 : resolve_sync (gfc_code *code)
11803 : : {
11804 : : /* Check imageset. The * case matches expr1 == NULL. */
11805 : 747 : if (code->expr1)
11806 : : {
11807 : 48 : if (code->expr1->ts.type != BT_INTEGER || code->expr1->rank > 1)
11808 : 1 : gfc_error ("Imageset argument at %L must be a scalar or rank-1 "
11809 : : "INTEGER expression", &code->expr1->where);
11810 : 48 : if (code->expr1->expr_type == EXPR_CONSTANT && code->expr1->rank == 0
11811 : 23 : && mpz_cmp_si (code->expr1->value.integer, 1) < 0)
11812 : 1 : gfc_error ("Imageset argument at %L must between 1 and num_images()",
11813 : : &code->expr1->where);
11814 : 47 : else if (code->expr1->expr_type == EXPR_ARRAY
11815 : 47 : && gfc_simplify_expr (code->expr1, 0))
11816 : : {
11817 : 18 : gfc_constructor *cons;
11818 : 18 : cons = gfc_constructor_first (code->expr1->value.constructor);
11819 : 54 : for (; cons; cons = gfc_constructor_next (cons))
11820 : 18 : if (cons->expr->expr_type == EXPR_CONSTANT
11821 : 18 : && mpz_cmp_si (cons->expr->value.integer, 1) < 0)
11822 : 0 : gfc_error ("Imageset argument at %L must between 1 and "
11823 : : "num_images()", &cons->expr->where);
11824 : : }
11825 : : }
11826 : :
11827 : : /* Check STAT. */
11828 : 747 : gfc_resolve_expr (code->expr2);
11829 : 747 : if (code->expr2)
11830 : : {
11831 : 84 : if (code->expr2->ts.type != BT_INTEGER || code->expr2->rank != 0)
11832 : 1 : gfc_error ("STAT= argument at %L must be a scalar INTEGER variable",
11833 : : &code->expr2->where);
11834 : : else
11835 : 83 : gfc_check_vardef_context (code->expr2, false, false, false,
11836 : 83 : _("STAT variable"));
11837 : : }
11838 : :
11839 : : /* Check ERRMSG. */
11840 : 747 : gfc_resolve_expr (code->expr3);
11841 : 747 : if (code->expr3)
11842 : : {
11843 : 75 : if (code->expr3->ts.type != BT_CHARACTER || code->expr3->rank != 0)
11844 : 4 : gfc_error ("ERRMSG= argument at %L must be a scalar CHARACTER variable",
11845 : : &code->expr3->where);
11846 : : else
11847 : 71 : gfc_check_vardef_context (code->expr3, false, false, false,
11848 : 71 : _("ERRMSG variable"));
11849 : : }
11850 : 747 : }
11851 : :
11852 : :
11853 : : /* Given a branch to a label, see if the branch is conforming.
11854 : : The code node describes where the branch is located. */
11855 : :
11856 : : static void
11857 : 105341 : resolve_branch (gfc_st_label *label, gfc_code *code)
11858 : : {
11859 : 105341 : code_stack *stack;
11860 : :
11861 : 105341 : if (label == NULL)
11862 : : return;
11863 : :
11864 : : /* Step one: is this a valid branching target? */
11865 : :
11866 : 2459 : if (label->defined == ST_LABEL_UNKNOWN)
11867 : : {
11868 : 4 : gfc_error ("Label %d referenced at %L is never defined", label->value,
11869 : : &code->loc);
11870 : 4 : return;
11871 : : }
11872 : :
11873 : 2455 : if (label->defined != ST_LABEL_TARGET && label->defined != ST_LABEL_DO_TARGET)
11874 : : {
11875 : 4 : gfc_error ("Statement at %L is not a valid branch target statement "
11876 : : "for the branch statement at %L", &label->where, &code->loc);
11877 : 4 : return;
11878 : : }
11879 : :
11880 : : /* Step two: make sure this branch is not a branch to itself ;-) */
11881 : :
11882 : 2451 : if (code->here == label)
11883 : : {
11884 : 0 : gfc_warning (0, "Branch at %L may result in an infinite loop",
11885 : : &code->loc);
11886 : 0 : return;
11887 : : }
11888 : :
11889 : : /* Step three: See if the label is in the same block as the
11890 : : branching statement. The hard work has been done by setting up
11891 : : the bitmap reachable_labels. */
11892 : :
11893 : 2451 : if (bitmap_bit_p (cs_base->reachable_labels, label->value))
11894 : : {
11895 : : /* Check now whether there is a CRITICAL construct; if so, check
11896 : : whether the label is still visible outside of the CRITICAL block,
11897 : : which is invalid. */
11898 : 6264 : for (stack = cs_base; stack; stack = stack->prev)
11899 : : {
11900 : 3881 : if (stack->current->op == EXEC_CRITICAL
11901 : 3881 : && bitmap_bit_p (stack->reachable_labels, label->value))
11902 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for "
11903 : : "label at %L", &code->loc, &label->where);
11904 : 3879 : else if (stack->current->op == EXEC_DO_CONCURRENT
11905 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
11906 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct "
11907 : : "for label at %L", &code->loc, &label->where);
11908 : 3879 : else if (stack->current->op == EXEC_CHANGE_TEAM
11909 : 3879 : && bitmap_bit_p (stack->reachable_labels, label->value))
11910 : 1 : gfc_error ("GOTO statement at %L leaves CHANGE TEAM construct "
11911 : : "for label at %L", &code->loc, &label->where);
11912 : : }
11913 : :
11914 : : return;
11915 : : }
11916 : :
11917 : : /* Step four: If we haven't found the label in the bitmap, it may
11918 : : still be the label of the END of the enclosing block, in which
11919 : : case we find it by going up the code_stack. */
11920 : :
11921 : 167 : for (stack = cs_base; stack; stack = stack->prev)
11922 : : {
11923 : 131 : if (stack->current->next && stack->current->next->here == label)
11924 : : break;
11925 : 101 : if (stack->current->op == EXEC_CRITICAL)
11926 : : {
11927 : : /* Note: A label at END CRITICAL does not leave the CRITICAL
11928 : : construct as END CRITICAL is still part of it. */
11929 : 2 : gfc_error ("GOTO statement at %L leaves CRITICAL construct for label"
11930 : : " at %L", &code->loc, &label->where);
11931 : 2 : return;
11932 : : }
11933 : 99 : else if (stack->current->op == EXEC_DO_CONCURRENT)
11934 : : {
11935 : 0 : gfc_error ("GOTO statement at %L leaves DO CONCURRENT construct for "
11936 : : "label at %L", &code->loc, &label->where);
11937 : 0 : return;
11938 : : }
11939 : : }
11940 : :
11941 : 66 : if (stack)
11942 : : {
11943 : 30 : gcc_assert (stack->current->next->op == EXEC_END_NESTED_BLOCK);
11944 : : return;
11945 : : }
11946 : :
11947 : : /* The label is not in an enclosing block, so illegal. This was
11948 : : allowed in Fortran 66, so we allow it as extension. No
11949 : : further checks are necessary in this case. */
11950 : 36 : gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
11951 : : "as the GOTO statement at %L", &label->where,
11952 : : &code->loc);
11953 : 36 : return;
11954 : : }
11955 : :
11956 : :
11957 : : /* Check whether EXPR1 has the same shape as EXPR2. */
11958 : :
11959 : : static bool
11960 : 1461 : resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
11961 : : {
11962 : 1461 : mpz_t shape[GFC_MAX_DIMENSIONS];
11963 : 1461 : mpz_t shape2[GFC_MAX_DIMENSIONS];
11964 : 1461 : bool result = false;
11965 : 1461 : int i;
11966 : :
11967 : : /* Compare the rank. */
11968 : 1461 : if (expr1->rank != expr2->rank)
11969 : : return result;
11970 : :
11971 : : /* Compare the size of each dimension. */
11972 : 2795 : for (i=0; i<expr1->rank; i++)
11973 : : {
11974 : 1484 : if (!gfc_array_dimen_size (expr1, i, &shape[i]))
11975 : 150 : goto ignore;
11976 : :
11977 : 1334 : if (!gfc_array_dimen_size (expr2, i, &shape2[i]))
11978 : 0 : goto ignore;
11979 : :
11980 : 1334 : if (mpz_cmp (shape[i], shape2[i]))
11981 : 0 : goto over;
11982 : : }
11983 : :
11984 : : /* When either of the two expression is an assumed size array, we
11985 : : ignore the comparison of dimension sizes. */
11986 : 1311 : ignore:
11987 : : result = true;
11988 : :
11989 : 1461 : over:
11990 : 1461 : gfc_clear_shape (shape, i);
11991 : 1461 : gfc_clear_shape (shape2, i);
11992 : 1461 : return result;
11993 : : }
11994 : :
11995 : :
11996 : : /* Check whether a WHERE assignment target or a WHERE mask expression
11997 : : has the same shape as the outmost WHERE mask expression. */
11998 : :
11999 : : static void
12000 : 506 : resolve_where (gfc_code *code, gfc_expr *mask)
12001 : : {
12002 : 506 : gfc_code *cblock;
12003 : 506 : gfc_code *cnext;
12004 : 506 : gfc_expr *e = NULL;
12005 : :
12006 : 506 : cblock = code->block;
12007 : :
12008 : : /* Store the first WHERE mask-expr of the WHERE statement or construct.
12009 : : In case of nested WHERE, only the outmost one is stored. */
12010 : 506 : if (mask == NULL) /* outmost WHERE */
12011 : 450 : e = cblock->expr1;
12012 : : else /* inner WHERE */
12013 : 506 : e = mask;
12014 : :
12015 : 1381 : while (cblock)
12016 : : {
12017 : 875 : if (cblock->expr1)
12018 : : {
12019 : : /* Check if the mask-expr has a consistent shape with the
12020 : : outmost WHERE mask-expr. */
12021 : 711 : if (!resolve_where_shape (cblock->expr1, e))
12022 : 0 : gfc_error ("WHERE mask at %L has inconsistent shape",
12023 : 0 : &cblock->expr1->where);
12024 : : }
12025 : :
12026 : : /* the assignment statement of a WHERE statement, or the first
12027 : : statement in where-body-construct of a WHERE construct */
12028 : 875 : cnext = cblock->next;
12029 : 1727 : while (cnext)
12030 : : {
12031 : 852 : switch (cnext->op)
12032 : : {
12033 : : /* WHERE assignment statement */
12034 : 750 : case EXEC_ASSIGN:
12035 : :
12036 : : /* Check shape consistent for WHERE assignment target. */
12037 : 750 : if (e && !resolve_where_shape (cnext->expr1, e))
12038 : 0 : gfc_error ("WHERE assignment target at %L has "
12039 : 0 : "inconsistent shape", &cnext->expr1->where);
12040 : :
12041 : 750 : if (cnext->op == EXEC_ASSIGN
12042 : 750 : && gfc_may_be_finalized (cnext->expr1->ts))
12043 : 0 : cnext->expr1->must_finalize = 1;
12044 : :
12045 : : break;
12046 : :
12047 : :
12048 : 46 : case EXEC_ASSIGN_CALL:
12049 : 46 : resolve_call (cnext);
12050 : 46 : if (!cnext->resolved_sym->attr.elemental)
12051 : 2 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12052 : 2 : &cnext->ext.actual->expr->where);
12053 : : break;
12054 : :
12055 : : /* WHERE or WHERE construct is part of a where-body-construct */
12056 : 56 : case EXEC_WHERE:
12057 : 56 : resolve_where (cnext, e);
12058 : 56 : break;
12059 : :
12060 : 0 : default:
12061 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12062 : : &cnext->loc);
12063 : : }
12064 : : /* the next statement within the same where-body-construct */
12065 : 852 : cnext = cnext->next;
12066 : : }
12067 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12068 : 875 : cblock = cblock->block;
12069 : : }
12070 : 506 : }
12071 : :
12072 : :
12073 : : /* Resolve assignment in FORALL construct.
12074 : : NVAR is the number of FORALL index variables, and VAR_EXPR records the
12075 : : FORALL index variables. */
12076 : :
12077 : : static void
12078 : 1944 : gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
12079 : : {
12080 : 1944 : int n;
12081 : :
12082 : 5871 : for (n = 0; n < nvar; n++)
12083 : : {
12084 : 3927 : gfc_symbol *forall_index;
12085 : :
12086 : 3927 : forall_index = var_expr[n]->symtree->n.sym;
12087 : :
12088 : : /* Check whether the assignment target is one of the FORALL index
12089 : : variable. */
12090 : 3927 : if ((code->expr1->expr_type == EXPR_VARIABLE)
12091 : 3927 : && (code->expr1->symtree->n.sym == forall_index))
12092 : 0 : gfc_error ("Assignment to a FORALL index variable at %L",
12093 : : &code->expr1->where);
12094 : : else
12095 : : {
12096 : : /* If one of the FORALL index variables doesn't appear in the
12097 : : assignment variable, then there could be a many-to-one
12098 : : assignment. Emit a warning rather than an error because the
12099 : : mask could be resolving this problem. */
12100 : 3927 : if (!find_forall_index (code->expr1, forall_index, 0))
12101 : 0 : gfc_warning (0, "The FORALL with index %qs is not used on the "
12102 : : "left side of the assignment at %L and so might "
12103 : : "cause multiple assignment to this object",
12104 : 0 : var_expr[n]->symtree->name, &code->expr1->where);
12105 : : }
12106 : : }
12107 : 1944 : }
12108 : :
12109 : :
12110 : : /* Resolve WHERE statement in FORALL construct. */
12111 : :
12112 : : static void
12113 : 46 : gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
12114 : : gfc_expr **var_expr)
12115 : : {
12116 : 46 : gfc_code *cblock;
12117 : 46 : gfc_code *cnext;
12118 : :
12119 : 46 : cblock = code->block;
12120 : 111 : while (cblock)
12121 : : {
12122 : : /* the assignment statement of a WHERE statement, or the first
12123 : : statement in where-body-construct of a WHERE construct */
12124 : 65 : cnext = cblock->next;
12125 : 130 : while (cnext)
12126 : : {
12127 : 65 : switch (cnext->op)
12128 : : {
12129 : : /* WHERE assignment statement */
12130 : 65 : case EXEC_ASSIGN:
12131 : 65 : gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
12132 : :
12133 : 65 : if (cnext->op == EXEC_ASSIGN
12134 : 65 : && gfc_may_be_finalized (cnext->expr1->ts))
12135 : 0 : cnext->expr1->must_finalize = 1;
12136 : :
12137 : : break;
12138 : :
12139 : : /* WHERE operator assignment statement */
12140 : 0 : case EXEC_ASSIGN_CALL:
12141 : 0 : resolve_call (cnext);
12142 : 0 : if (!cnext->resolved_sym->attr.elemental)
12143 : 0 : gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
12144 : 0 : &cnext->ext.actual->expr->where);
12145 : : break;
12146 : :
12147 : : /* WHERE or WHERE construct is part of a where-body-construct */
12148 : 0 : case EXEC_WHERE:
12149 : 0 : gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
12150 : 0 : break;
12151 : :
12152 : 0 : default:
12153 : 0 : gfc_error ("Unsupported statement inside WHERE at %L",
12154 : : &cnext->loc);
12155 : : }
12156 : : /* the next statement within the same where-body-construct */
12157 : 65 : cnext = cnext->next;
12158 : : }
12159 : : /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
12160 : 65 : cblock = cblock->block;
12161 : : }
12162 : 46 : }
12163 : :
12164 : :
12165 : : /* Traverse the FORALL body to check whether the following errors exist:
12166 : : 1. For assignment, check if a many-to-one assignment happens.
12167 : : 2. For WHERE statement, check the WHERE body to see if there is any
12168 : : many-to-one assignment. */
12169 : :
12170 : : static void
12171 : 1990 : gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
12172 : : {
12173 : 1990 : gfc_code *c;
12174 : :
12175 : 1990 : c = code->block->next;
12176 : 3993 : while (c)
12177 : : {
12178 : 2003 : switch (c->op)
12179 : : {
12180 : 1879 : case EXEC_ASSIGN:
12181 : 1879 : case EXEC_POINTER_ASSIGN:
12182 : 1879 : gfc_resolve_assign_in_forall (c, nvar, var_expr);
12183 : :
12184 : 1879 : if (c->op == EXEC_ASSIGN
12185 : 1879 : && gfc_may_be_finalized (c->expr1->ts))
12186 : 0 : c->expr1->must_finalize = 1;
12187 : :
12188 : : break;
12189 : :
12190 : 0 : case EXEC_ASSIGN_CALL:
12191 : 0 : resolve_call (c);
12192 : 0 : break;
12193 : :
12194 : : /* Because the gfc_resolve_blocks() will handle the nested FORALL,
12195 : : there is no need to handle it here. */
12196 : : case EXEC_FORALL:
12197 : : break;
12198 : 46 : case EXEC_WHERE:
12199 : 46 : gfc_resolve_where_code_in_forall(c, nvar, var_expr);
12200 : 46 : break;
12201 : : default:
12202 : : break;
12203 : : }
12204 : : /* The next statement in the FORALL body. */
12205 : 2003 : c = c->next;
12206 : : }
12207 : 1990 : }
12208 : :
12209 : :
12210 : : /* Counts the number of iterators needed inside a forall construct, including
12211 : : nested forall constructs. This is used to allocate the needed memory
12212 : : in gfc_resolve_forall. */
12213 : :
12214 : : static int
12215 : 1990 : gfc_count_forall_iterators (gfc_code *code)
12216 : : {
12217 : 1990 : int max_iters, sub_iters, current_iters;
12218 : 1990 : gfc_forall_iterator *fa;
12219 : :
12220 : 1990 : gcc_assert(code->op == EXEC_FORALL);
12221 : 1990 : max_iters = 0;
12222 : 1990 : current_iters = 0;
12223 : :
12224 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12225 : 3888 : current_iters ++;
12226 : :
12227 : 1990 : code = code->block->next;
12228 : :
12229 : 3993 : while (code)
12230 : : {
12231 : 2003 : if (code->op == EXEC_FORALL)
12232 : : {
12233 : 78 : sub_iters = gfc_count_forall_iterators (code);
12234 : 78 : if (sub_iters > max_iters)
12235 : 2003 : max_iters = sub_iters;
12236 : : }
12237 : 2003 : code = code->next;
12238 : : }
12239 : :
12240 : 1990 : return current_iters + max_iters;
12241 : : }
12242 : :
12243 : :
12244 : : /* Given a FORALL construct, first resolve the FORALL iterator, then call
12245 : : gfc_resolve_forall_body to resolve the FORALL body. */
12246 : :
12247 : : static void
12248 : 1990 : gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
12249 : : {
12250 : 1990 : static gfc_expr **var_expr;
12251 : 1990 : static int total_var = 0;
12252 : 1990 : static int nvar = 0;
12253 : 1990 : int i, old_nvar, tmp;
12254 : 1990 : gfc_forall_iterator *fa;
12255 : :
12256 : 1990 : old_nvar = nvar;
12257 : :
12258 : 1990 : if (!gfc_notify_std (GFC_STD_F2018_OBS, "FORALL construct at %L", &code->loc))
12259 : : return;
12260 : :
12261 : : /* Start to resolve a FORALL construct */
12262 : 1990 : if (forall_save == 0)
12263 : : {
12264 : : /* Count the total number of FORALL indices in the nested FORALL
12265 : : construct in order to allocate the VAR_EXPR with proper size. */
12266 : 1912 : total_var = gfc_count_forall_iterators (code);
12267 : :
12268 : : /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
12269 : 1912 : var_expr = XCNEWVEC (gfc_expr *, total_var);
12270 : : }
12271 : :
12272 : : /* The information about FORALL iterator, including FORALL indices start, end
12273 : : and stride. An outer FORALL indice cannot appear in start, end or stride. */
12274 : 5878 : for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
12275 : : {
12276 : : /* Fortran 20008: C738 (R753). */
12277 : 3888 : if (fa->var->ref && fa->var->ref->type == REF_ARRAY)
12278 : : {
12279 : 2 : gfc_error ("FORALL index-name at %L must be a scalar variable "
12280 : : "of type integer", &fa->var->where);
12281 : 2 : continue;
12282 : : }
12283 : :
12284 : : /* Check if any outer FORALL index name is the same as the current
12285 : : one. */
12286 : 6859 : for (i = 0; i < nvar; i++)
12287 : : {
12288 : 2973 : if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
12289 : 0 : gfc_error ("An outer FORALL construct already has an index "
12290 : : "with this name %L", &fa->var->where);
12291 : : }
12292 : :
12293 : : /* Record the current FORALL index. */
12294 : 3886 : var_expr[nvar] = gfc_copy_expr (fa->var);
12295 : :
12296 : 3886 : nvar++;
12297 : :
12298 : : /* No memory leak. */
12299 : 3886 : gcc_assert (nvar <= total_var);
12300 : : }
12301 : :
12302 : : /* Resolve the FORALL body. */
12303 : 1990 : gfc_resolve_forall_body (code, nvar, var_expr);
12304 : :
12305 : : /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
12306 : 1990 : gfc_resolve_blocks (code->block, ns);
12307 : :
12308 : 1990 : tmp = nvar;
12309 : 1990 : nvar = old_nvar;
12310 : : /* Free only the VAR_EXPRs allocated in this frame. */
12311 : 5876 : for (i = nvar; i < tmp; i++)
12312 : 3886 : gfc_free_expr (var_expr[i]);
12313 : :
12314 : 1990 : if (nvar == 0)
12315 : : {
12316 : : /* We are in the outermost FORALL construct. */
12317 : 1912 : gcc_assert (forall_save == 0);
12318 : :
12319 : : /* VAR_EXPR is not needed any more. */
12320 : 1912 : free (var_expr);
12321 : 1912 : total_var = 0;
12322 : : }
12323 : : }
12324 : :
12325 : :
12326 : : /* Resolve a BLOCK construct statement. */
12327 : :
12328 : : static void
12329 : 7612 : resolve_block_construct (gfc_code* code)
12330 : : {
12331 : 7612 : gfc_namespace *ns = code->ext.block.ns;
12332 : :
12333 : : /* For an ASSOCIATE block, the associations (and their targets) will be
12334 : : resolved by gfc_resolve_symbol, during resolution of the BLOCK's
12335 : : namespace. */
12336 : 7612 : gfc_resolve (ns);
12337 : 0 : }
12338 : :
12339 : :
12340 : : /* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
12341 : : DO code nodes. */
12342 : :
12343 : : void
12344 : 322959 : gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
12345 : : {
12346 : 322959 : bool t;
12347 : :
12348 : 657120 : for (; b; b = b->block)
12349 : : {
12350 : 334161 : t = gfc_resolve_expr (b->expr1);
12351 : 334161 : if (!gfc_resolve_expr (b->expr2))
12352 : 0 : t = false;
12353 : :
12354 : 334161 : switch (b->op)
12355 : : {
12356 : 230641 : case EXEC_IF:
12357 : 230641 : if (t && b->expr1 != NULL
12358 : 226515 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
12359 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
12360 : : &b->expr1->where);
12361 : : break;
12362 : :
12363 : 761 : case EXEC_WHERE:
12364 : 761 : if (t
12365 : 761 : && b->expr1 != NULL
12366 : 628 : && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
12367 : 0 : gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
12368 : : &b->expr1->where);
12369 : : break;
12370 : :
12371 : 76 : case EXEC_GOTO:
12372 : 76 : resolve_branch (b->label1, b);
12373 : 76 : break;
12374 : :
12375 : 0 : case EXEC_BLOCK:
12376 : 0 : resolve_block_construct (b);
12377 : 0 : break;
12378 : :
12379 : : case EXEC_SELECT:
12380 : : case EXEC_SELECT_TYPE:
12381 : : case EXEC_SELECT_RANK:
12382 : : case EXEC_FORALL:
12383 : : case EXEC_DO:
12384 : : case EXEC_DO_WHILE:
12385 : : case EXEC_DO_CONCURRENT:
12386 : : case EXEC_CRITICAL:
12387 : : case EXEC_READ:
12388 : : case EXEC_WRITE:
12389 : : case EXEC_IOLENGTH:
12390 : : case EXEC_WAIT:
12391 : : break;
12392 : :
12393 : 2697 : case EXEC_OMP_ATOMIC:
12394 : 2697 : case EXEC_OACC_ATOMIC:
12395 : 2697 : {
12396 : : /* Verify this before calling gfc_resolve_code, which might
12397 : : change it. */
12398 : 2697 : gcc_assert (b->op == EXEC_OMP_ATOMIC
12399 : : || (b->next && b->next->op == EXEC_ASSIGN));
12400 : : }
12401 : : break;
12402 : :
12403 : : case EXEC_OACC_PARALLEL_LOOP:
12404 : : case EXEC_OACC_PARALLEL:
12405 : : case EXEC_OACC_KERNELS_LOOP:
12406 : : case EXEC_OACC_KERNELS:
12407 : : case EXEC_OACC_SERIAL_LOOP:
12408 : : case EXEC_OACC_SERIAL:
12409 : : case EXEC_OACC_DATA:
12410 : : case EXEC_OACC_HOST_DATA:
12411 : : case EXEC_OACC_LOOP:
12412 : : case EXEC_OACC_UPDATE:
12413 : : case EXEC_OACC_WAIT:
12414 : : case EXEC_OACC_CACHE:
12415 : : case EXEC_OACC_ENTER_DATA:
12416 : : case EXEC_OACC_EXIT_DATA:
12417 : : case EXEC_OACC_ROUTINE:
12418 : : case EXEC_OMP_ALLOCATE:
12419 : : case EXEC_OMP_ALLOCATORS:
12420 : : case EXEC_OMP_ASSUME:
12421 : : case EXEC_OMP_CRITICAL:
12422 : : case EXEC_OMP_DISPATCH:
12423 : : case EXEC_OMP_DISTRIBUTE:
12424 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
12425 : : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
12426 : : case EXEC_OMP_DISTRIBUTE_SIMD:
12427 : : case EXEC_OMP_DO:
12428 : : case EXEC_OMP_DO_SIMD:
12429 : : case EXEC_OMP_ERROR:
12430 : : case EXEC_OMP_LOOP:
12431 : : case EXEC_OMP_MASKED:
12432 : : case EXEC_OMP_MASKED_TASKLOOP:
12433 : : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
12434 : : case EXEC_OMP_MASTER:
12435 : : case EXEC_OMP_MASTER_TASKLOOP:
12436 : : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
12437 : : case EXEC_OMP_ORDERED:
12438 : : case EXEC_OMP_PARALLEL:
12439 : : case EXEC_OMP_PARALLEL_DO:
12440 : : case EXEC_OMP_PARALLEL_DO_SIMD:
12441 : : case EXEC_OMP_PARALLEL_LOOP:
12442 : : case EXEC_OMP_PARALLEL_MASKED:
12443 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
12444 : : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
12445 : : case EXEC_OMP_PARALLEL_MASTER:
12446 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
12447 : : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
12448 : : case EXEC_OMP_PARALLEL_SECTIONS:
12449 : : case EXEC_OMP_PARALLEL_WORKSHARE:
12450 : : case EXEC_OMP_SECTIONS:
12451 : : case EXEC_OMP_SIMD:
12452 : : case EXEC_OMP_SCOPE:
12453 : : case EXEC_OMP_SINGLE:
12454 : : case EXEC_OMP_TARGET:
12455 : : case EXEC_OMP_TARGET_DATA:
12456 : : case EXEC_OMP_TARGET_ENTER_DATA:
12457 : : case EXEC_OMP_TARGET_EXIT_DATA:
12458 : : case EXEC_OMP_TARGET_PARALLEL:
12459 : : case EXEC_OMP_TARGET_PARALLEL_DO:
12460 : : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
12461 : : case EXEC_OMP_TARGET_PARALLEL_LOOP:
12462 : : case EXEC_OMP_TARGET_SIMD:
12463 : : case EXEC_OMP_TARGET_TEAMS:
12464 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
12465 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
12466 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12467 : : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
12468 : : case EXEC_OMP_TARGET_TEAMS_LOOP:
12469 : : case EXEC_OMP_TARGET_UPDATE:
12470 : : case EXEC_OMP_TASK:
12471 : : case EXEC_OMP_TASKGROUP:
12472 : : case EXEC_OMP_TASKLOOP:
12473 : : case EXEC_OMP_TASKLOOP_SIMD:
12474 : : case EXEC_OMP_TASKWAIT:
12475 : : case EXEC_OMP_TASKYIELD:
12476 : : case EXEC_OMP_TEAMS:
12477 : : case EXEC_OMP_TEAMS_DISTRIBUTE:
12478 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
12479 : : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
12480 : : case EXEC_OMP_TEAMS_LOOP:
12481 : : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
12482 : : case EXEC_OMP_TILE:
12483 : : case EXEC_OMP_UNROLL:
12484 : : case EXEC_OMP_WORKSHARE:
12485 : : break;
12486 : :
12487 : 0 : default:
12488 : 0 : gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
12489 : : }
12490 : :
12491 : 334161 : gfc_resolve_code (b->next, ns);
12492 : : }
12493 : 322959 : }
12494 : :
12495 : : bool
12496 : 0 : caf_possible_reallocate (gfc_expr *e)
12497 : : {
12498 : 0 : symbol_attribute caf_attr;
12499 : 0 : gfc_ref *last_arr_ref = nullptr;
12500 : :
12501 : 0 : caf_attr = gfc_caf_attr (e);
12502 : 0 : if (!caf_attr.codimension || !caf_attr.allocatable || !caf_attr.dimension)
12503 : : return false;
12504 : :
12505 : : /* Only full array refs can indicate a needed reallocation. */
12506 : 0 : for (gfc_ref *ref = e->ref; ref; ref = ref->next)
12507 : 0 : if (ref->type == REF_ARRAY && ref->u.ar.dimen)
12508 : 0 : last_arr_ref = ref;
12509 : :
12510 : 0 : return last_arr_ref && last_arr_ref->u.ar.type == AR_FULL;
12511 : : }
12512 : :
12513 : : /* Does everything to resolve an ordinary assignment. Returns true
12514 : : if this is an interface assignment. */
12515 : : static bool
12516 : 279101 : resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
12517 : : {
12518 : 279101 : bool rval = false;
12519 : 279101 : gfc_expr *lhs;
12520 : 279101 : gfc_expr *rhs;
12521 : 279101 : int n;
12522 : 279101 : gfc_ref *ref;
12523 : 279101 : symbol_attribute attr;
12524 : :
12525 : 279101 : if (gfc_extend_assign (code, ns))
12526 : : {
12527 : 752 : gfc_expr** rhsptr;
12528 : :
12529 : 752 : if (code->op == EXEC_ASSIGN_CALL)
12530 : : {
12531 : 336 : lhs = code->ext.actual->expr;
12532 : 336 : rhsptr = &code->ext.actual->next->expr;
12533 : : }
12534 : : else
12535 : : {
12536 : 416 : gfc_actual_arglist* args;
12537 : 416 : gfc_typebound_proc* tbp;
12538 : :
12539 : 416 : gcc_assert (code->op == EXEC_COMPCALL);
12540 : :
12541 : 416 : args = code->expr1->value.compcall.actual;
12542 : 416 : lhs = args->expr;
12543 : 416 : rhsptr = &args->next->expr;
12544 : :
12545 : 416 : tbp = code->expr1->value.compcall.tbp;
12546 : 416 : gcc_assert (!tbp->is_generic);
12547 : : }
12548 : :
12549 : : /* Make a temporary rhs when there is a default initializer
12550 : : and rhs is the same symbol as the lhs. */
12551 : 752 : if ((*rhsptr)->expr_type == EXPR_VARIABLE
12552 : 380 : && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
12553 : 327 : && gfc_has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
12554 : 931 : && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
12555 : 24 : *rhsptr = gfc_get_parentheses (*rhsptr);
12556 : :
12557 : 752 : return true;
12558 : : }
12559 : :
12560 : 278349 : lhs = code->expr1;
12561 : 278349 : rhs = code->expr2;
12562 : :
12563 : 278349 : if ((lhs->symtree->n.sym->ts.type == BT_DERIVED
12564 : 260079 : || lhs->symtree->n.sym->ts.type == BT_CLASS)
12565 : 20635 : && !lhs->symtree->n.sym->attr.proc_pointer
12566 : 298984 : && gfc_expr_attr (lhs).proc_pointer)
12567 : : {
12568 : 1 : gfc_error ("Variable in the ordinary assignment at %L is a procedure "
12569 : : "pointer component",
12570 : : &lhs->where);
12571 : 1 : return false;
12572 : : }
12573 : :
12574 : 326603 : if ((gfc_numeric_ts (&lhs->ts) || lhs->ts.type == BT_LOGICAL)
12575 : 245381 : && rhs->ts.type == BT_CHARACTER
12576 : 278741 : && (rhs->expr_type != EXPR_CONSTANT || !flag_dec_char_conversions))
12577 : : {
12578 : : /* Use of -fdec-char-conversions allows assignment of character data
12579 : : to non-character variables. This not permitted for nonconstant
12580 : : strings. */
12581 : 29 : gfc_error ("Cannot convert %s to %s at %L", gfc_typename (rhs),
12582 : : gfc_typename (lhs), &rhs->where);
12583 : 29 : return false;
12584 : : }
12585 : :
12586 : 278319 : if (flag_unsigned && gfc_invalid_unsigned_ops (lhs, rhs))
12587 : : {
12588 : 0 : gfc_error ("Cannot assign %s to %s at %L", gfc_typename (rhs),
12589 : : gfc_typename (lhs), &rhs->where);
12590 : 0 : return false;
12591 : : }
12592 : :
12593 : : /* Handle the case of a BOZ literal on the RHS. */
12594 : 278319 : if (rhs->ts.type == BT_BOZ)
12595 : : {
12596 : 3 : if (gfc_invalid_boz ("BOZ literal constant at %L is neither a DATA "
12597 : : "statement value nor an actual argument of "
12598 : : "INT/REAL/DBLE/CMPLX intrinsic subprogram",
12599 : : &rhs->where))
12600 : : return false;
12601 : :
12602 : 1 : switch (lhs->ts.type)
12603 : : {
12604 : 0 : case BT_INTEGER:
12605 : 0 : if (!gfc_boz2int (rhs, lhs->ts.kind))
12606 : : return false;
12607 : : break;
12608 : 1 : case BT_REAL:
12609 : 1 : if (!gfc_boz2real (rhs, lhs->ts.kind))
12610 : : return false;
12611 : : break;
12612 : 0 : default:
12613 : 0 : gfc_error ("Invalid use of BOZ literal constant at %L", &rhs->where);
12614 : 0 : return false;
12615 : : }
12616 : : }
12617 : :
12618 : 278317 : if (lhs->ts.type == BT_CHARACTER && warn_character_truncation)
12619 : : {
12620 : 64 : HOST_WIDE_INT llen = 0, rlen = 0;
12621 : 64 : if (lhs->ts.u.cl != NULL
12622 : 64 : && lhs->ts.u.cl->length != NULL
12623 : 53 : && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12624 : 53 : llen = gfc_mpz_get_hwi (lhs->ts.u.cl->length->value.integer);
12625 : :
12626 : 64 : if (rhs->expr_type == EXPR_CONSTANT)
12627 : 26 : rlen = rhs->value.character.length;
12628 : :
12629 : 38 : else if (rhs->ts.u.cl != NULL
12630 : 38 : && rhs->ts.u.cl->length != NULL
12631 : 35 : && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
12632 : 35 : rlen = gfc_mpz_get_hwi (rhs->ts.u.cl->length->value.integer);
12633 : :
12634 : 64 : if (rlen && llen && rlen > llen)
12635 : 28 : gfc_warning_now (OPT_Wcharacter_truncation,
12636 : : "CHARACTER expression will be truncated "
12637 : : "in assignment (%wd/%wd) at %L",
12638 : : llen, rlen, &code->loc);
12639 : : }
12640 : :
12641 : : /* Ensure that a vector index expression for the lvalue is evaluated
12642 : : to a temporary if the lvalue symbol is referenced in it. */
12643 : 278317 : if (lhs->rank)
12644 : : {
12645 : 106465 : for (ref = lhs->ref; ref; ref= ref->next)
12646 : 56391 : if (ref->type == REF_ARRAY)
12647 : : {
12648 : 126770 : for (n = 0; n < ref->u.ar.dimen; n++)
12649 : 75247 : if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
12650 : 75472 : && gfc_find_sym_in_expr (lhs->symtree->n.sym,
12651 : 225 : ref->u.ar.start[n]))
12652 : 14 : ref->u.ar.start[n]
12653 : 14 : = gfc_get_parentheses (ref->u.ar.start[n]);
12654 : : }
12655 : : }
12656 : :
12657 : 278317 : if (gfc_pure (NULL))
12658 : : {
12659 : 3232 : if (lhs->ts.type == BT_DERIVED
12660 : 84 : && lhs->expr_type == EXPR_VARIABLE
12661 : 84 : && lhs->ts.u.derived->attr.pointer_comp
12662 : 4 : && rhs->expr_type == EXPR_VARIABLE
12663 : 3235 : && (gfc_impure_variable (rhs->symtree->n.sym)
12664 : 2 : || gfc_is_coindexed (rhs)))
12665 : : {
12666 : : /* F2008, C1283. */
12667 : 2 : if (gfc_is_coindexed (rhs))
12668 : 1 : gfc_error ("Coindexed expression at %L is assigned to "
12669 : : "a derived type variable with a POINTER "
12670 : : "component in a PURE procedure",
12671 : : &rhs->where);
12672 : : else
12673 : : /* F2008, C1283 (4). */
12674 : 1 : gfc_error ("In a pure subprogram an INTENT(IN) dummy argument "
12675 : : "shall not be used as the expr at %L of an intrinsic "
12676 : : "assignment statement in which the variable is of a "
12677 : : "derived type if the derived type has a pointer "
12678 : : "component at any level of component selection.",
12679 : : &rhs->where);
12680 : 2 : return rval;
12681 : : }
12682 : :
12683 : : /* Fortran 2008, C1283. */
12684 : 3230 : if (gfc_is_coindexed (lhs))
12685 : : {
12686 : 1 : gfc_error ("Assignment to coindexed variable at %L in a PURE "
12687 : : "procedure", &rhs->where);
12688 : 1 : return rval;
12689 : : }
12690 : : }
12691 : :
12692 : 278314 : if (gfc_implicit_pure (NULL))
12693 : : {
12694 : 6930 : if (lhs->expr_type == EXPR_VARIABLE
12695 : 6930 : && lhs->symtree->n.sym != gfc_current_ns->proc_name
12696 : 4973 : && lhs->symtree->n.sym->ns != gfc_current_ns)
12697 : 243 : gfc_unset_implicit_pure (NULL);
12698 : :
12699 : 6930 : if (lhs->ts.type == BT_DERIVED
12700 : 299 : && lhs->expr_type == EXPR_VARIABLE
12701 : 299 : && lhs->ts.u.derived->attr.pointer_comp
12702 : 7 : && rhs->expr_type == EXPR_VARIABLE
12703 : 6937 : && (gfc_impure_variable (rhs->symtree->n.sym)
12704 : 7 : || gfc_is_coindexed (rhs)))
12705 : 0 : gfc_unset_implicit_pure (NULL);
12706 : :
12707 : : /* Fortran 2008, C1283. */
12708 : 6930 : if (gfc_is_coindexed (lhs))
12709 : 0 : gfc_unset_implicit_pure (NULL);
12710 : : }
12711 : :
12712 : : /* F2008, 7.2.1.2. */
12713 : 278314 : attr = gfc_expr_attr (lhs);
12714 : 278314 : if (lhs->ts.type == BT_CLASS && attr.allocatable)
12715 : : {
12716 : 858 : if (attr.codimension)
12717 : : {
12718 : 1 : gfc_error ("Assignment to polymorphic coarray at %L is not "
12719 : : "permitted", &lhs->where);
12720 : 1 : return false;
12721 : : }
12722 : 857 : if (!gfc_notify_std (GFC_STD_F2008, "Assignment to an allocatable "
12723 : : "polymorphic variable at %L", &lhs->where))
12724 : : return false;
12725 : 856 : if (!flag_realloc_lhs)
12726 : : {
12727 : 1 : gfc_error ("Assignment to an allocatable polymorphic variable at %L "
12728 : : "requires %<-frealloc-lhs%>", &lhs->where);
12729 : 1 : return false;
12730 : : }
12731 : : }
12732 : 277456 : else if (lhs->ts.type == BT_CLASS)
12733 : : {
12734 : 9 : gfc_error ("Nonallocatable variable must not be polymorphic in intrinsic "
12735 : : "assignment at %L - check that there is a matching specific "
12736 : : "subroutine for %<=%> operator", &lhs->where);
12737 : 9 : return false;
12738 : : }
12739 : :
12740 : 278302 : bool lhs_coindexed = gfc_is_coindexed (lhs);
12741 : :
12742 : : /* F2008, Section 7.2.1.2. */
12743 : 278302 : if (lhs_coindexed && gfc_has_ultimate_allocatable (lhs))
12744 : : {
12745 : 1 : gfc_error ("Coindexed variable must not have an allocatable ultimate "
12746 : : "component in assignment at %L", &lhs->where);
12747 : 1 : return false;
12748 : : }
12749 : :
12750 : : /* Assign the 'data' of a class object to a derived type. */
12751 : 278301 : if (lhs->ts.type == BT_DERIVED
12752 : 6572 : && rhs->ts.type == BT_CLASS
12753 : 137 : && rhs->expr_type != EXPR_ARRAY)
12754 : 131 : gfc_add_data_component (rhs);
12755 : :
12756 : : /* Make sure there is a vtable and, in particular, a _copy for the
12757 : : rhs type. */
12758 : 278301 : if (lhs->ts.type == BT_CLASS && rhs->ts.type != BT_CLASS)
12759 : 504 : gfc_find_vtab (&rhs->ts);
12760 : :
12761 : 278301 : gfc_check_assign (lhs, rhs, 1);
12762 : :
12763 : 278301 : return false;
12764 : : }
12765 : :
12766 : :
12767 : : /* Add a component reference onto an expression. */
12768 : :
12769 : : static void
12770 : 665 : add_comp_ref (gfc_expr *e, gfc_component *c)
12771 : : {
12772 : 665 : gfc_ref **ref;
12773 : 665 : ref = &(e->ref);
12774 : 889 : while (*ref)
12775 : 224 : ref = &((*ref)->next);
12776 : 665 : *ref = gfc_get_ref ();
12777 : 665 : (*ref)->type = REF_COMPONENT;
12778 : 665 : (*ref)->u.c.sym = e->ts.u.derived;
12779 : 665 : (*ref)->u.c.component = c;
12780 : 665 : e->ts = c->ts;
12781 : :
12782 : : /* Add a full array ref, as necessary. */
12783 : 665 : if (c->as)
12784 : : {
12785 : 84 : gfc_add_full_array_ref (e, c->as);
12786 : 84 : e->rank = c->as->rank;
12787 : 84 : e->corank = c->as->corank;
12788 : : }
12789 : 665 : }
12790 : :
12791 : :
12792 : : /* Build an assignment. Keep the argument 'op' for future use, so that
12793 : : pointer assignments can be made. */
12794 : :
12795 : : static gfc_code *
12796 : 898 : build_assignment (gfc_exec_op op, gfc_expr *expr1, gfc_expr *expr2,
12797 : : gfc_component *comp1, gfc_component *comp2, locus loc)
12798 : : {
12799 : 898 : gfc_code *this_code;
12800 : :
12801 : 898 : this_code = gfc_get_code (op);
12802 : 898 : this_code->next = NULL;
12803 : 898 : this_code->expr1 = gfc_copy_expr (expr1);
12804 : 898 : this_code->expr2 = gfc_copy_expr (expr2);
12805 : 898 : this_code->loc = loc;
12806 : 898 : if (comp1 && comp2)
12807 : : {
12808 : 288 : add_comp_ref (this_code->expr1, comp1);
12809 : 288 : add_comp_ref (this_code->expr2, comp2);
12810 : : }
12811 : :
12812 : 898 : return this_code;
12813 : : }
12814 : :
12815 : :
12816 : : /* Makes a temporary variable expression based on the characteristics of
12817 : : a given variable expression. */
12818 : :
12819 : : static gfc_expr*
12820 : 392 : get_temp_from_expr (gfc_expr *e, gfc_namespace *ns)
12821 : : {
12822 : 392 : static int serial = 0;
12823 : 392 : char name[GFC_MAX_SYMBOL_LEN];
12824 : 392 : gfc_symtree *tmp;
12825 : 392 : gfc_array_spec *as;
12826 : 392 : gfc_array_ref *aref;
12827 : 392 : gfc_ref *ref;
12828 : :
12829 : 392 : sprintf (name, GFC_PREFIX("DA%d"), serial++);
12830 : 392 : gfc_get_sym_tree (name, ns, &tmp, false);
12831 : 392 : gfc_add_type (tmp->n.sym, &e->ts, NULL);
12832 : :
12833 : 392 : if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_CHARACTER)
12834 : 0 : tmp->n.sym->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
12835 : : NULL,
12836 : 0 : e->value.character.length);
12837 : :
12838 : 392 : as = NULL;
12839 : 392 : ref = NULL;
12840 : 392 : aref = NULL;
12841 : :
12842 : : /* Obtain the arrayspec for the temporary. */
12843 : 392 : if (e->rank && e->expr_type != EXPR_ARRAY
12844 : : && e->expr_type != EXPR_FUNCTION
12845 : : && e->expr_type != EXPR_OP)
12846 : : {
12847 : 52 : aref = gfc_find_array_ref (e);
12848 : 52 : if (e->expr_type == EXPR_VARIABLE
12849 : 52 : && e->symtree->n.sym->as == aref->as)
12850 : : as = aref->as;
12851 : : else
12852 : : {
12853 : 0 : for (ref = e->ref; ref; ref = ref->next)
12854 : 0 : if (ref->type == REF_COMPONENT
12855 : 0 : && ref->u.c.component->as == aref->as)
12856 : : {
12857 : : as = aref->as;
12858 : : break;
12859 : : }
12860 : : }
12861 : : }
12862 : :
12863 : : /* Add the attributes and the arrayspec to the temporary. */
12864 : 392 : tmp->n.sym->attr = gfc_expr_attr (e);
12865 : 392 : tmp->n.sym->attr.function = 0;
12866 : 392 : tmp->n.sym->attr.proc_pointer = 0;
12867 : 392 : tmp->n.sym->attr.result = 0;
12868 : 392 : tmp->n.sym->attr.flavor = FL_VARIABLE;
12869 : 392 : tmp->n.sym->attr.dummy = 0;
12870 : 392 : tmp->n.sym->attr.use_assoc = 0;
12871 : 392 : tmp->n.sym->attr.intent = INTENT_UNKNOWN;
12872 : :
12873 : :
12874 : 392 : if (as)
12875 : : {
12876 : 52 : tmp->n.sym->as = gfc_copy_array_spec (as);
12877 : 52 : if (!ref)
12878 : 52 : ref = e->ref;
12879 : 52 : if (as->type == AS_DEFERRED)
12880 : 46 : tmp->n.sym->attr.allocatable = 1;
12881 : : }
12882 : 340 : else if ((e->rank || e->corank)
12883 : 48 : && (e->expr_type == EXPR_ARRAY || e->expr_type == EXPR_FUNCTION
12884 : 0 : || e->expr_type == EXPR_OP))
12885 : : {
12886 : 48 : tmp->n.sym->as = gfc_get_array_spec ();
12887 : 48 : tmp->n.sym->as->type = AS_DEFERRED;
12888 : 48 : tmp->n.sym->as->rank = e->rank;
12889 : 48 : tmp->n.sym->as->corank = e->corank;
12890 : 48 : tmp->n.sym->attr.allocatable = 1;
12891 : 48 : tmp->n.sym->attr.dimension = e->rank ? 1 : 0;
12892 : 96 : tmp->n.sym->attr.codimension = e->corank ? 1 : 0;
12893 : : }
12894 : : else
12895 : 292 : tmp->n.sym->attr.dimension = 0;
12896 : :
12897 : 392 : gfc_set_sym_referenced (tmp->n.sym);
12898 : 392 : gfc_commit_symbol (tmp->n.sym);
12899 : 392 : e = gfc_lval_expr_from_sym (tmp->n.sym);
12900 : :
12901 : : /* Should the lhs be a section, use its array ref for the
12902 : : temporary expression. */
12903 : 392 : if (aref && aref->type != AR_FULL)
12904 : : {
12905 : 6 : gfc_free_ref_list (e->ref);
12906 : 6 : e->ref = gfc_copy_ref (ref);
12907 : : }
12908 : 392 : return e;
12909 : : }
12910 : :
12911 : :
12912 : : /* Add one line of code to the code chain, making sure that 'head' and
12913 : : 'tail' are appropriately updated. */
12914 : :
12915 : : static void
12916 : 656 : add_code_to_chain (gfc_code **this_code, gfc_code **head, gfc_code **tail)
12917 : : {
12918 : 656 : gcc_assert (this_code);
12919 : 656 : if (*head == NULL)
12920 : 308 : *head = *tail = *this_code;
12921 : : else
12922 : 348 : *tail = gfc_append_code (*tail, *this_code);
12923 : 656 : *this_code = NULL;
12924 : 656 : }
12925 : :
12926 : :
12927 : : /* Generate a final call from a variable expression */
12928 : :
12929 : : static void
12930 : 81 : generate_final_call (gfc_expr *tmp_expr, gfc_code **head, gfc_code **tail)
12931 : : {
12932 : 81 : gfc_code *this_code;
12933 : 81 : gfc_expr *final_expr = NULL;
12934 : 81 : gfc_expr *size_expr;
12935 : 81 : gfc_expr *fini_coarray;
12936 : :
12937 : 81 : gcc_assert (tmp_expr->expr_type == EXPR_VARIABLE);
12938 : 81 : if (!gfc_is_finalizable (tmp_expr->ts.u.derived, &final_expr) || !final_expr)
12939 : 75 : return;
12940 : :
12941 : : /* Now generate the finalizer call. */
12942 : 6 : this_code = gfc_get_code (EXEC_CALL);
12943 : 6 : this_code->symtree = final_expr->symtree;
12944 : 6 : this_code->resolved_sym = final_expr->symtree->n.sym;
12945 : :
12946 : : //* Expression to be finalized */
12947 : 6 : this_code->ext.actual = gfc_get_actual_arglist ();
12948 : 6 : this_code->ext.actual->expr = gfc_copy_expr (tmp_expr);
12949 : :
12950 : : /* size_expr = STORAGE_SIZE (...) / NUMERIC_STORAGE_SIZE. */
12951 : 6 : this_code->ext.actual->next = gfc_get_actual_arglist ();
12952 : 6 : size_expr = gfc_get_expr ();
12953 : 6 : size_expr->where = gfc_current_locus;
12954 : 6 : size_expr->expr_type = EXPR_OP;
12955 : 6 : size_expr->value.op.op = INTRINSIC_DIVIDE;
12956 : 6 : size_expr->value.op.op1
12957 : 12 : = gfc_build_intrinsic_call (gfc_current_ns, GFC_ISYM_STORAGE_SIZE,
12958 : : "storage_size", gfc_current_locus, 2,
12959 : 6 : gfc_lval_expr_from_sym (tmp_expr->symtree->n.sym),
12960 : : gfc_get_int_expr (gfc_index_integer_kind,
12961 : : NULL, 0));
12962 : 6 : size_expr->value.op.op2 = gfc_get_int_expr (gfc_index_integer_kind, NULL,
12963 : : gfc_character_storage_size);
12964 : 6 : size_expr->value.op.op1->ts = size_expr->value.op.op2->ts;
12965 : 6 : size_expr->ts = size_expr->value.op.op1->ts;
12966 : 6 : this_code->ext.actual->next->expr = size_expr;
12967 : :
12968 : : /* fini_coarray */
12969 : 6 : this_code->ext.actual->next->next = gfc_get_actual_arglist ();
12970 : 6 : fini_coarray = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind,
12971 : : &tmp_expr->where);
12972 : 6 : fini_coarray->value.logical = (int)gfc_expr_attr (tmp_expr).codimension;
12973 : 6 : this_code->ext.actual->next->next->expr = fini_coarray;
12974 : :
12975 : 6 : add_code_to_chain (&this_code, head, tail);
12976 : :
12977 : : }
12978 : :
12979 : : /* Counts the potential number of part array references that would
12980 : : result from resolution of typebound defined assignments. */
12981 : :
12982 : :
12983 : : static int
12984 : 243 : nonscalar_typebound_assign (gfc_symbol *derived, int depth)
12985 : : {
12986 : 243 : gfc_component *c;
12987 : 243 : int c_depth = 0, t_depth;
12988 : :
12989 : 584 : for (c= derived->components; c; c = c->next)
12990 : : {
12991 : 341 : if ((!gfc_bt_struct (c->ts.type)
12992 : 261 : || c->attr.pointer
12993 : 261 : || c->attr.allocatable
12994 : 260 : || c->attr.proc_pointer_comp
12995 : 260 : || c->attr.class_pointer
12996 : 260 : || c->attr.proc_pointer)
12997 : 81 : && !c->attr.defined_assign_comp)
12998 : 81 : continue;
12999 : :
13000 : 260 : if (c->as && c_depth == 0)
13001 : 260 : c_depth = 1;
13002 : :
13003 : 260 : if (c->ts.u.derived->attr.defined_assign_comp)
13004 : 110 : t_depth = nonscalar_typebound_assign (c->ts.u.derived,
13005 : : c->as ? 1 : 0);
13006 : : else
13007 : : t_depth = 0;
13008 : :
13009 : 260 : c_depth = t_depth > c_depth ? t_depth : c_depth;
13010 : : }
13011 : 243 : return depth + c_depth;
13012 : : }
13013 : :
13014 : :
13015 : : /* Implement 10.2.1.3 paragraph 13 of the F18 standard:
13016 : : "An intrinsic assignment where the variable is of derived type is performed
13017 : : as if each component of the variable were assigned from the corresponding
13018 : : component of expr using pointer assignment (10.2.2) for each pointer
13019 : : component, defined assignment for each nonpointer nonallocatable component
13020 : : of a type that has a type-bound defined assignment consistent with the
13021 : : component, intrinsic assignment for each other nonpointer nonallocatable
13022 : : component, and intrinsic assignment for each allocated coarray component.
13023 : : For unallocated coarray components, the corresponding component of the
13024 : : variable shall be unallocated. For a noncoarray allocatable component the
13025 : : following sequence of operations is applied.
13026 : : (1) If the component of the variable is allocated, it is deallocated.
13027 : : (2) If the component of the value of expr is allocated, the
13028 : : corresponding component of the variable is allocated with the same
13029 : : dynamic type and type parameters as the component of the value of
13030 : : expr. If it is an array, it is allocated with the same bounds. The
13031 : : value of the component of the value of expr is then assigned to the
13032 : : corresponding component of the variable using defined assignment if
13033 : : the declared type of the component has a type-bound defined
13034 : : assignment consistent with the component, and intrinsic assignment
13035 : : for the dynamic type of that component otherwise."
13036 : :
13037 : : The pointer assignments are taken care of by the intrinsic assignment of the
13038 : : structure itself. This function recursively adds defined assignments where
13039 : : required. The recursion is accomplished by calling gfc_resolve_code.
13040 : :
13041 : : When the lhs in a defined assignment has intent INOUT or is intent OUT
13042 : : and the component of 'var' is finalizable, we need a temporary for the
13043 : : lhs. In pseudo-code for an assignment var = expr:
13044 : :
13045 : : ! Confine finalization of temporaries, as far as possible.
13046 : : Enclose the code for the assignment in a block
13047 : : ! Only call function 'expr' once.
13048 : : #if ('expr is not a constant or an variable)
13049 : : temp_expr = expr
13050 : : expr = temp_x
13051 : : ! Do the intrinsic assignment
13052 : : #if typeof ('var') has a typebound final subroutine
13053 : : finalize (var)
13054 : : var = expr
13055 : : ! Now do the component assignments
13056 : : #do over derived type components [%cmp]
13057 : : #if (cmp is a pointer of any kind)
13058 : : continue
13059 : : build the assignment
13060 : : resolve the code
13061 : : #if the code is a typebound assignment
13062 : : #if (arg1 is INOUT or finalizable OUT && !t1)
13063 : : t1 = var
13064 : : arg1 = t1
13065 : : deal with allocatation or not of var and this component
13066 : : #elseif the code is an assignment by itself
13067 : : #if this component does not need finalization
13068 : : delete code and continue
13069 : : #else
13070 : : remove the leading assignment
13071 : : #endif
13072 : : commit the code
13073 : : #if (t1 and (arg1 is INOUT or finalizable OUT))
13074 : : var%cmp = t1%cmp
13075 : : #enddo
13076 : : put all code chunks involving t1 to the top of the generated code
13077 : : insert the generated block in place of the original code
13078 : : */
13079 : :
13080 : : static bool
13081 : 381 : is_finalizable_type (gfc_typespec ts)
13082 : : {
13083 : 381 : gfc_component *c;
13084 : :
13085 : 381 : if (ts.type != BT_DERIVED)
13086 : : return false;
13087 : :
13088 : : /* (1) Check for FINAL subroutines. */
13089 : 381 : if (ts.u.derived->f2k_derived && ts.u.derived->f2k_derived->finalizers)
13090 : : return true;
13091 : :
13092 : : /* (2) Check for components of finalizable type. */
13093 : 809 : for (c = ts.u.derived->components; c; c = c->next)
13094 : 470 : if (c->ts.type == BT_DERIVED
13095 : 243 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable
13096 : 242 : && c->ts.u.derived->f2k_derived
13097 : 242 : && c->ts.u.derived->f2k_derived->finalizers)
13098 : : return true;
13099 : :
13100 : : return false;
13101 : : }
13102 : :
13103 : : /* The temporary assignments have to be put on top of the additional
13104 : : code to avoid the result being changed by the intrinsic assignment.
13105 : : */
13106 : : static int component_assignment_level = 0;
13107 : : static gfc_code *tmp_head = NULL, *tmp_tail = NULL;
13108 : : static bool finalizable_comp;
13109 : :
13110 : : static void
13111 : 188 : generate_component_assignments (gfc_code **code, gfc_namespace *ns)
13112 : : {
13113 : 188 : gfc_component *comp1, *comp2;
13114 : 188 : gfc_code *this_code = NULL, *head = NULL, *tail = NULL;
13115 : 188 : gfc_code *tmp_code = NULL;
13116 : 188 : gfc_expr *t1 = NULL;
13117 : 188 : gfc_expr *tmp_expr = NULL;
13118 : 188 : int error_count, depth;
13119 : 188 : bool finalizable_lhs;
13120 : :
13121 : 188 : gfc_get_errors (NULL, &error_count);
13122 : :
13123 : : /* Filter out continuing processing after an error. */
13124 : 188 : if (error_count
13125 : 188 : || (*code)->expr1->ts.type != BT_DERIVED
13126 : 188 : || (*code)->expr2->ts.type != BT_DERIVED)
13127 : 140 : return;
13128 : :
13129 : : /* TODO: Handle more than one part array reference in assignments. */
13130 : 188 : depth = nonscalar_typebound_assign ((*code)->expr1->ts.u.derived,
13131 : 188 : (*code)->expr1->rank ? 1 : 0);
13132 : 188 : if (depth > 1)
13133 : : {
13134 : 6 : gfc_warning (0, "TODO: type-bound defined assignment(s) at %L not "
13135 : : "done because multiple part array references would "
13136 : : "occur in intermediate expressions.", &(*code)->loc);
13137 : 6 : return;
13138 : : }
13139 : :
13140 : 182 : if (!component_assignment_level)
13141 : 134 : finalizable_comp = true;
13142 : :
13143 : : /* Build a block so that function result temporaries are finalized
13144 : : locally on exiting the rather than enclosing scope. */
13145 : 182 : if (!component_assignment_level)
13146 : : {
13147 : 134 : ns = gfc_build_block_ns (ns);
13148 : 134 : tmp_code = gfc_get_code (EXEC_NOP);
13149 : 134 : *tmp_code = **code;
13150 : 134 : tmp_code->next = NULL;
13151 : 134 : (*code)->op = EXEC_BLOCK;
13152 : 134 : (*code)->ext.block.ns = ns;
13153 : 134 : (*code)->ext.block.assoc = NULL;
13154 : 134 : (*code)->expr1 = (*code)->expr2 = NULL;
13155 : 134 : ns->code = tmp_code;
13156 : 134 : code = &ns->code;
13157 : : }
13158 : :
13159 : 182 : component_assignment_level++;
13160 : :
13161 : 182 : finalizable_lhs = is_finalizable_type ((*code)->expr1->ts);
13162 : :
13163 : : /* Create a temporary so that functions get called only once. */
13164 : 182 : if ((*code)->expr2->expr_type != EXPR_VARIABLE
13165 : 182 : && (*code)->expr2->expr_type != EXPR_CONSTANT)
13166 : : {
13167 : : /* Assign the rhs to the temporary. */
13168 : 81 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13169 : 81 : if (tmp_expr->symtree->n.sym->attr.pointer)
13170 : : {
13171 : : /* Use allocate on assignment for the sake of simplicity. The
13172 : : temporary must not take on the optional attribute. Assume
13173 : : that the assignment is guarded by a PRESENT condition if the
13174 : : lhs is optional. */
13175 : 25 : tmp_expr->symtree->n.sym->attr.pointer = 0;
13176 : 25 : tmp_expr->symtree->n.sym->attr.optional = 0;
13177 : 25 : tmp_expr->symtree->n.sym->attr.allocatable = 1;
13178 : : }
13179 : 162 : this_code = build_assignment (EXEC_ASSIGN,
13180 : : tmp_expr, (*code)->expr2,
13181 : 81 : NULL, NULL, (*code)->loc);
13182 : 81 : this_code->expr2->must_finalize = 1;
13183 : : /* Add the code and substitute the rhs expression. */
13184 : 81 : add_code_to_chain (&this_code, &tmp_head, &tmp_tail);
13185 : 81 : gfc_free_expr ((*code)->expr2);
13186 : 81 : (*code)->expr2 = tmp_expr;
13187 : : }
13188 : :
13189 : : /* Do the intrinsic assignment. This is not needed if the lhs is one
13190 : : of the temporaries generated here, since the intrinsic assignment
13191 : : to the final result already does this. */
13192 : 182 : if ((*code)->expr1->symtree->n.sym->name[2] != '.')
13193 : : {
13194 : 182 : if (finalizable_lhs)
13195 : 18 : (*code)->expr1->must_finalize = 1;
13196 : 182 : this_code = build_assignment (EXEC_ASSIGN,
13197 : : (*code)->expr1, (*code)->expr2,
13198 : : NULL, NULL, (*code)->loc);
13199 : 182 : add_code_to_chain (&this_code, &head, &tail);
13200 : : }
13201 : :
13202 : 182 : comp1 = (*code)->expr1->ts.u.derived->components;
13203 : 182 : comp2 = (*code)->expr2->ts.u.derived->components;
13204 : :
13205 : 449 : for (; comp1; comp1 = comp1->next, comp2 = comp2->next)
13206 : : {
13207 : 267 : bool inout = false;
13208 : 267 : bool finalizable_out = false;
13209 : :
13210 : : /* The intrinsic assignment does the right thing for pointers
13211 : : of all kinds and allocatable components. */
13212 : 267 : if (!gfc_bt_struct (comp1->ts.type)
13213 : 200 : || comp1->attr.pointer
13214 : 200 : || comp1->attr.allocatable
13215 : 199 : || comp1->attr.proc_pointer_comp
13216 : 199 : || comp1->attr.class_pointer
13217 : 199 : || comp1->attr.proc_pointer)
13218 : 68 : continue;
13219 : :
13220 : 398 : finalizable_comp = is_finalizable_type (comp1->ts)
13221 : 199 : && !finalizable_lhs;
13222 : :
13223 : : /* Make an assignment for this component. */
13224 : 398 : this_code = build_assignment (EXEC_ASSIGN,
13225 : : (*code)->expr1, (*code)->expr2,
13226 : 199 : comp1, comp2, (*code)->loc);
13227 : :
13228 : : /* Convert the assignment if there is a defined assignment for
13229 : : this type. Otherwise, using the call from gfc_resolve_code,
13230 : : recurse into its components. */
13231 : 199 : gfc_resolve_code (this_code, ns);
13232 : :
13233 : 199 : if (this_code->op == EXEC_ASSIGN_CALL)
13234 : : {
13235 : 144 : gfc_formal_arglist *dummy_args;
13236 : 144 : gfc_symbol *rsym;
13237 : : /* Check that there is a typebound defined assignment. If not,
13238 : : then this must be a module defined assignment. We cannot
13239 : : use the defined_assign_comp attribute here because it must
13240 : : be this derived type that has the defined assignment and not
13241 : : a parent type. */
13242 : 144 : if (!(comp1->ts.u.derived->f2k_derived
13243 : : && comp1->ts.u.derived->f2k_derived
13244 : 144 : ->tb_op[INTRINSIC_ASSIGN]))
13245 : : {
13246 : 1 : gfc_free_statements (this_code);
13247 : 1 : this_code = NULL;
13248 : 1 : continue;
13249 : : }
13250 : :
13251 : : /* If the first argument of the subroutine has intent INOUT
13252 : : a temporary must be generated and used instead. */
13253 : 143 : rsym = this_code->resolved_sym;
13254 : 143 : dummy_args = gfc_sym_get_dummy_args (rsym);
13255 : 268 : finalizable_out = gfc_may_be_finalized (comp1->ts)
13256 : 18 : && dummy_args
13257 : 161 : && dummy_args->sym->attr.intent == INTENT_OUT;
13258 : 286 : inout = dummy_args
13259 : 268 : && dummy_args->sym->attr.intent == INTENT_INOUT;
13260 : 72 : if ((inout || finalizable_out)
13261 : 89 : && !comp1->attr.allocatable)
13262 : : {
13263 : 89 : gfc_code *temp_code;
13264 : 89 : inout = true;
13265 : :
13266 : : /* Build the temporary required for the assignment and put
13267 : : it at the head of the generated code. */
13268 : 89 : if (!t1)
13269 : : {
13270 : 89 : gfc_namespace *tmp_ns = ns;
13271 : 89 : if (ns->parent && gfc_may_be_finalized (comp1->ts))
13272 : 18 : tmp_ns = (*code)->expr1->symtree->n.sym->ns;
13273 : 89 : t1 = get_temp_from_expr ((*code)->expr1, tmp_ns);
13274 : 89 : t1->symtree->n.sym->attr.artificial = 1;
13275 : 178 : temp_code = build_assignment (EXEC_ASSIGN,
13276 : : t1, (*code)->expr1,
13277 : 89 : NULL, NULL, (*code)->loc);
13278 : :
13279 : : /* For allocatable LHS, check whether it is allocated. Note
13280 : : that allocatable components with defined assignment are
13281 : : not yet support. See PR 57696. */
13282 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable)
13283 : : {
13284 : 24 : gfc_code *block;
13285 : 24 : gfc_expr *e =
13286 : 24 : gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13287 : 24 : block = gfc_get_code (EXEC_IF);
13288 : 24 : block->block = gfc_get_code (EXEC_IF);
13289 : 24 : block->block->expr1
13290 : 48 : = gfc_build_intrinsic_call (ns,
13291 : : GFC_ISYM_ALLOCATED, "allocated",
13292 : 24 : (*code)->loc, 1, e);
13293 : 24 : block->block->next = temp_code;
13294 : 24 : temp_code = block;
13295 : : }
13296 : 89 : add_code_to_chain (&temp_code, &tmp_head, &tmp_tail);
13297 : : }
13298 : :
13299 : : /* Replace the first actual arg with the component of the
13300 : : temporary. */
13301 : 89 : gfc_free_expr (this_code->ext.actual->expr);
13302 : 89 : this_code->ext.actual->expr = gfc_copy_expr (t1);
13303 : 89 : add_comp_ref (this_code->ext.actual->expr, comp1);
13304 : :
13305 : : /* If the LHS variable is allocatable and wasn't allocated and
13306 : : the temporary is allocatable, pointer assign the address of
13307 : : the freshly allocated LHS to the temporary. */
13308 : 89 : if ((*code)->expr1->symtree->n.sym->attr.allocatable
13309 : 89 : && gfc_expr_attr ((*code)->expr1).allocatable)
13310 : : {
13311 : 18 : gfc_code *block;
13312 : 18 : gfc_expr *cond;
13313 : :
13314 : 18 : cond = gfc_get_expr ();
13315 : 18 : cond->ts.type = BT_LOGICAL;
13316 : 18 : cond->ts.kind = gfc_default_logical_kind;
13317 : 18 : cond->expr_type = EXPR_OP;
13318 : 18 : cond->where = (*code)->loc;
13319 : 18 : cond->value.op.op = INTRINSIC_NOT;
13320 : 18 : cond->value.op.op1 = gfc_build_intrinsic_call (ns,
13321 : : GFC_ISYM_ALLOCATED, "allocated",
13322 : 18 : (*code)->loc, 1, gfc_copy_expr (t1));
13323 : 18 : block = gfc_get_code (EXEC_IF);
13324 : 18 : block->block = gfc_get_code (EXEC_IF);
13325 : 18 : block->block->expr1 = cond;
13326 : 36 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13327 : : t1, (*code)->expr1,
13328 : 18 : NULL, NULL, (*code)->loc);
13329 : 18 : add_code_to_chain (&block, &head, &tail);
13330 : : }
13331 : : }
13332 : : }
13333 : 55 : else if (this_code->op == EXEC_ASSIGN && !this_code->next)
13334 : : {
13335 : : /* Don't add intrinsic assignments since they are already
13336 : : effected by the intrinsic assignment of the structure, unless
13337 : : finalization is required. */
13338 : 7 : if (finalizable_comp)
13339 : 0 : this_code->expr1->must_finalize = 1;
13340 : : else
13341 : : {
13342 : 7 : gfc_free_statements (this_code);
13343 : 7 : this_code = NULL;
13344 : 7 : continue;
13345 : : }
13346 : : }
13347 : : else
13348 : : {
13349 : : /* Resolution has expanded an assignment of a derived type with
13350 : : defined assigned components. Remove the redundant, leading
13351 : : assignment. */
13352 : 48 : gcc_assert (this_code->op == EXEC_ASSIGN);
13353 : 48 : gfc_code *tmp = this_code;
13354 : 48 : this_code = this_code->next;
13355 : 48 : tmp->next = NULL;
13356 : 48 : gfc_free_statements (tmp);
13357 : : }
13358 : :
13359 : 191 : add_code_to_chain (&this_code, &head, &tail);
13360 : :
13361 : 191 : if (t1 && (inout || finalizable_out))
13362 : : {
13363 : : /* Transfer the value to the final result. */
13364 : 178 : this_code = build_assignment (EXEC_ASSIGN,
13365 : : (*code)->expr1, t1,
13366 : 89 : comp1, comp2, (*code)->loc);
13367 : 89 : this_code->expr1->must_finalize = 0;
13368 : 89 : add_code_to_chain (&this_code, &head, &tail);
13369 : : }
13370 : : }
13371 : :
13372 : : /* Put the temporary assignments at the top of the generated code. */
13373 : 182 : if (tmp_head && component_assignment_level == 1)
13374 : : {
13375 : 126 : gfc_append_code (tmp_head, head);
13376 : 126 : head = tmp_head;
13377 : 126 : tmp_head = tmp_tail = NULL;
13378 : : }
13379 : :
13380 : : /* If we did a pointer assignment - thus, we need to ensure that the LHS is
13381 : : not accidentally deallocated. Hence, nullify t1. */
13382 : 89 : if (t1 && (*code)->expr1->symtree->n.sym->attr.allocatable
13383 : 271 : && gfc_expr_attr ((*code)->expr1).allocatable)
13384 : : {
13385 : 18 : gfc_code *block;
13386 : 18 : gfc_expr *cond;
13387 : 18 : gfc_expr *e;
13388 : :
13389 : 18 : e = gfc_lval_expr_from_sym ((*code)->expr1->symtree->n.sym);
13390 : 18 : cond = gfc_build_intrinsic_call (ns, GFC_ISYM_ASSOCIATED, "associated",
13391 : 18 : (*code)->loc, 2, gfc_copy_expr (t1), e);
13392 : 18 : block = gfc_get_code (EXEC_IF);
13393 : 18 : block->block = gfc_get_code (EXEC_IF);
13394 : 18 : block->block->expr1 = cond;
13395 : 18 : block->block->next = build_assignment (EXEC_POINTER_ASSIGN,
13396 : : t1, gfc_get_null_expr (&(*code)->loc),
13397 : 18 : NULL, NULL, (*code)->loc);
13398 : 18 : gfc_append_code (tail, block);
13399 : 18 : tail = block;
13400 : : }
13401 : :
13402 : 182 : component_assignment_level--;
13403 : :
13404 : : /* Make an explicit final call for the function result. */
13405 : 182 : if (tmp_expr)
13406 : 81 : generate_final_call (tmp_expr, &head, &tail);
13407 : :
13408 : 182 : if (tmp_code)
13409 : : {
13410 : 134 : ns->code = head;
13411 : 134 : return;
13412 : : }
13413 : :
13414 : : /* Now attach the remaining code chain to the input code. Step on
13415 : : to the end of the new code since resolution is complete. */
13416 : 48 : gcc_assert ((*code)->op == EXEC_ASSIGN);
13417 : 48 : tail->next = (*code)->next;
13418 : : /* Overwrite 'code' because this would place the intrinsic assignment
13419 : : before the temporary for the lhs is created. */
13420 : 48 : gfc_free_expr ((*code)->expr1);
13421 : 48 : gfc_free_expr ((*code)->expr2);
13422 : 48 : **code = *head;
13423 : 48 : if (head != tail)
13424 : 48 : free (head);
13425 : 48 : *code = tail;
13426 : : }
13427 : :
13428 : :
13429 : : /* F2008: Pointer function assignments are of the form:
13430 : : ptr_fcn (args) = expr
13431 : : This function breaks these assignments into two statements:
13432 : : temporary_pointer => ptr_fcn(args)
13433 : : temporary_pointer = expr */
13434 : :
13435 : : static bool
13436 : 279344 : resolve_ptr_fcn_assign (gfc_code **code, gfc_namespace *ns)
13437 : : {
13438 : 279344 : gfc_expr *tmp_ptr_expr;
13439 : 279344 : gfc_code *this_code;
13440 : 279344 : gfc_component *comp;
13441 : 279344 : gfc_symbol *s;
13442 : :
13443 : 279344 : if ((*code)->expr1->expr_type != EXPR_FUNCTION)
13444 : : return false;
13445 : :
13446 : : /* Even if standard does not support this feature, continue to build
13447 : : the two statements to avoid upsetting frontend_passes.c. */
13448 : 205 : gfc_notify_std (GFC_STD_F2008, "Pointer procedure assignment at "
13449 : : "%L", &(*code)->loc);
13450 : :
13451 : 205 : comp = gfc_get_proc_ptr_comp ((*code)->expr1);
13452 : :
13453 : 205 : if (comp)
13454 : 6 : s = comp->ts.interface;
13455 : : else
13456 : 199 : s = (*code)->expr1->symtree->n.sym;
13457 : :
13458 : 205 : if (s == NULL || !s->result->attr.pointer)
13459 : : {
13460 : 5 : gfc_error ("The function result on the lhs of the assignment at "
13461 : : "%L must have the pointer attribute.",
13462 : 5 : &(*code)->expr1->where);
13463 : 5 : (*code)->op = EXEC_NOP;
13464 : 5 : return false;
13465 : : }
13466 : :
13467 : 200 : tmp_ptr_expr = get_temp_from_expr ((*code)->expr1, ns);
13468 : :
13469 : : /* get_temp_from_expression is set up for ordinary assignments. To that
13470 : : end, where array bounds are not known, arrays are made allocatable.
13471 : : Change the temporary to a pointer here. */
13472 : 200 : tmp_ptr_expr->symtree->n.sym->attr.pointer = 1;
13473 : 200 : tmp_ptr_expr->symtree->n.sym->attr.allocatable = 0;
13474 : 200 : tmp_ptr_expr->where = (*code)->loc;
13475 : :
13476 : : /* A new charlen is required to ensure that the variable string length
13477 : : is different to that of the original lhs for deferred results. */
13478 : 200 : if (s->result->ts.deferred && tmp_ptr_expr->ts.type == BT_CHARACTER)
13479 : : {
13480 : 60 : tmp_ptr_expr->ts.u.cl = gfc_get_charlen();
13481 : 60 : tmp_ptr_expr->ts.deferred = 1;
13482 : 60 : tmp_ptr_expr->ts.u.cl->next = gfc_current_ns->cl_list;
13483 : 60 : gfc_current_ns->cl_list = tmp_ptr_expr->ts.u.cl;
13484 : 60 : tmp_ptr_expr->symtree->n.sym->ts.u.cl = tmp_ptr_expr->ts.u.cl;
13485 : : }
13486 : :
13487 : 400 : this_code = build_assignment (EXEC_ASSIGN,
13488 : : tmp_ptr_expr, (*code)->expr2,
13489 : 200 : NULL, NULL, (*code)->loc);
13490 : 200 : this_code->next = (*code)->next;
13491 : 200 : (*code)->next = this_code;
13492 : 200 : (*code)->op = EXEC_POINTER_ASSIGN;
13493 : 200 : (*code)->expr2 = (*code)->expr1;
13494 : 200 : (*code)->expr1 = tmp_ptr_expr;
13495 : :
13496 : 200 : return true;
13497 : : }
13498 : :
13499 : :
13500 : : /* Deferred character length assignments from an operator expression
13501 : : require a temporary because the character length of the lhs can
13502 : : change in the course of the assignment. */
13503 : :
13504 : : static bool
13505 : 278349 : deferred_op_assign (gfc_code **code, gfc_namespace *ns)
13506 : : {
13507 : 278349 : gfc_expr *tmp_expr;
13508 : 278349 : gfc_code *this_code;
13509 : :
13510 : 278349 : if (!((*code)->expr1->ts.type == BT_CHARACTER
13511 : 25526 : && (*code)->expr1->ts.deferred && (*code)->expr1->rank
13512 : 740 : && (*code)->expr2->ts.type == BT_CHARACTER
13513 : 739 : && (*code)->expr2->expr_type == EXPR_OP))
13514 : : return false;
13515 : :
13516 : 34 : if (!gfc_check_dependency ((*code)->expr1, (*code)->expr2, 1))
13517 : : return false;
13518 : :
13519 : 28 : if (gfc_expr_attr ((*code)->expr1).pointer)
13520 : : return false;
13521 : :
13522 : 22 : tmp_expr = get_temp_from_expr ((*code)->expr1, ns);
13523 : 22 : tmp_expr->where = (*code)->loc;
13524 : :
13525 : : /* A new charlen is required to ensure that the variable string
13526 : : length is different to that of the original lhs. */
13527 : 22 : tmp_expr->ts.u.cl = gfc_get_charlen();
13528 : 22 : tmp_expr->symtree->n.sym->ts.u.cl = tmp_expr->ts.u.cl;
13529 : 22 : tmp_expr->ts.u.cl->next = (*code)->expr2->ts.u.cl->next;
13530 : 22 : (*code)->expr2->ts.u.cl->next = tmp_expr->ts.u.cl;
13531 : :
13532 : 22 : tmp_expr->symtree->n.sym->ts.deferred = 1;
13533 : :
13534 : 22 : this_code = build_assignment (EXEC_ASSIGN,
13535 : 22 : (*code)->expr1,
13536 : : gfc_copy_expr (tmp_expr),
13537 : : NULL, NULL, (*code)->loc);
13538 : :
13539 : 22 : (*code)->expr1 = tmp_expr;
13540 : :
13541 : 22 : this_code->next = (*code)->next;
13542 : 22 : (*code)->next = this_code;
13543 : :
13544 : 22 : return true;
13545 : : }
13546 : :
13547 : :
13548 : : /* Given a block of code, recursively resolve everything pointed to by this
13549 : : code block. */
13550 : :
13551 : : void
13552 : 657470 : gfc_resolve_code (gfc_code *code, gfc_namespace *ns)
13553 : : {
13554 : 657470 : int omp_workshare_save;
13555 : 657470 : int forall_save, do_concurrent_save;
13556 : 657470 : code_stack frame;
13557 : 657470 : bool t;
13558 : :
13559 : 657470 : frame.prev = cs_base;
13560 : 657470 : frame.head = code;
13561 : 657470 : cs_base = &frame;
13562 : :
13563 : 657470 : find_reachable_labels (code);
13564 : :
13565 : 1760453 : for (; code; code = code->next)
13566 : : {
13567 : 1102984 : frame.current = code;
13568 : 1102984 : forall_save = forall_flag;
13569 : 1102984 : do_concurrent_save = gfc_do_concurrent_flag;
13570 : :
13571 : 1102984 : if (code->op == EXEC_FORALL)
13572 : : {
13573 : 1990 : forall_flag = 1;
13574 : 1990 : gfc_resolve_forall (code, ns, forall_save);
13575 : 1990 : forall_flag = 2;
13576 : : }
13577 : 1100994 : else if (code->op == EXEC_OMP_METADIRECTIVE)
13578 : 109 : for (gfc_omp_variant *variant
13579 : : = code->ext.omp_variants;
13580 : 359 : variant; variant = variant->next)
13581 : 250 : gfc_resolve_code (variant->code, ns);
13582 : 1100885 : else if (code->block)
13583 : : {
13584 : 320972 : omp_workshare_save = -1;
13585 : 320972 : switch (code->op)
13586 : : {
13587 : 10115 : case EXEC_OACC_PARALLEL_LOOP:
13588 : 10115 : case EXEC_OACC_PARALLEL:
13589 : 10115 : case EXEC_OACC_KERNELS_LOOP:
13590 : 10115 : case EXEC_OACC_KERNELS:
13591 : 10115 : case EXEC_OACC_SERIAL_LOOP:
13592 : 10115 : case EXEC_OACC_SERIAL:
13593 : 10115 : case EXEC_OACC_DATA:
13594 : 10115 : case EXEC_OACC_HOST_DATA:
13595 : 10115 : case EXEC_OACC_LOOP:
13596 : 10115 : gfc_resolve_oacc_blocks (code, ns);
13597 : 10115 : break;
13598 : 54 : case EXEC_OMP_PARALLEL_WORKSHARE:
13599 : 54 : omp_workshare_save = omp_workshare_flag;
13600 : 54 : omp_workshare_flag = 1;
13601 : 54 : gfc_resolve_omp_parallel_blocks (code, ns);
13602 : 54 : break;
13603 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
13604 : 5905 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
13605 : 5905 : case EXEC_OMP_MASKED_TASKLOOP:
13606 : 5905 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
13607 : 5905 : case EXEC_OMP_MASTER_TASKLOOP:
13608 : 5905 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
13609 : 5905 : case EXEC_OMP_PARALLEL:
13610 : 5905 : case EXEC_OMP_PARALLEL_DO:
13611 : 5905 : case EXEC_OMP_PARALLEL_DO_SIMD:
13612 : 5905 : case EXEC_OMP_PARALLEL_LOOP:
13613 : 5905 : case EXEC_OMP_PARALLEL_MASKED:
13614 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
13615 : 5905 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
13616 : 5905 : case EXEC_OMP_PARALLEL_MASTER:
13617 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
13618 : 5905 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
13619 : 5905 : case EXEC_OMP_PARALLEL_SECTIONS:
13620 : 5905 : case EXEC_OMP_TARGET_PARALLEL:
13621 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO:
13622 : 5905 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
13623 : 5905 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
13624 : 5905 : case EXEC_OMP_TARGET_TEAMS:
13625 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
13626 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
13627 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13628 : 5905 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
13629 : 5905 : case EXEC_OMP_TARGET_TEAMS_LOOP:
13630 : 5905 : case EXEC_OMP_TASK:
13631 : 5905 : case EXEC_OMP_TASKLOOP:
13632 : 5905 : case EXEC_OMP_TASKLOOP_SIMD:
13633 : 5905 : case EXEC_OMP_TEAMS:
13634 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE:
13635 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
13636 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
13637 : 5905 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
13638 : 5905 : case EXEC_OMP_TEAMS_LOOP:
13639 : 5905 : omp_workshare_save = omp_workshare_flag;
13640 : 5905 : omp_workshare_flag = 0;
13641 : 5905 : gfc_resolve_omp_parallel_blocks (code, ns);
13642 : 5905 : break;
13643 : 3039 : case EXEC_OMP_DISTRIBUTE:
13644 : 3039 : case EXEC_OMP_DISTRIBUTE_SIMD:
13645 : 3039 : case EXEC_OMP_DO:
13646 : 3039 : case EXEC_OMP_DO_SIMD:
13647 : 3039 : case EXEC_OMP_LOOP:
13648 : 3039 : case EXEC_OMP_SIMD:
13649 : 3039 : case EXEC_OMP_TARGET_SIMD:
13650 : 3039 : case EXEC_OMP_TILE:
13651 : 3039 : case EXEC_OMP_UNROLL:
13652 : 3039 : gfc_resolve_omp_do_blocks (code, ns);
13653 : 3039 : break;
13654 : : case EXEC_SELECT_TYPE:
13655 : : case EXEC_SELECT_RANK:
13656 : : /* Blocks are handled in resolve_select_type/rank because we
13657 : : have to transform the SELECT TYPE into ASSOCIATE first. */
13658 : : break;
13659 : 171 : case EXEC_DO_CONCURRENT:
13660 : 171 : gfc_do_concurrent_flag = 1;
13661 : 171 : gfc_resolve_blocks (code->block, ns);
13662 : 171 : gfc_do_concurrent_flag = 2;
13663 : 171 : break;
13664 : 39 : case EXEC_OMP_WORKSHARE:
13665 : 39 : omp_workshare_save = omp_workshare_flag;
13666 : 39 : omp_workshare_flag = 1;
13667 : : /* FALL THROUGH */
13668 : 297801 : default:
13669 : 297801 : gfc_resolve_blocks (code->block, ns);
13670 : 297801 : break;
13671 : : }
13672 : :
13673 : 317085 : if (omp_workshare_save != -1)
13674 : 5998 : omp_workshare_flag = omp_workshare_save;
13675 : : }
13676 : 779913 : start:
13677 : 1103189 : t = true;
13678 : 1103189 : if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
13679 : 1101807 : t = gfc_resolve_expr (code->expr1);
13680 : :
13681 : 1103189 : forall_flag = forall_save;
13682 : 1103189 : gfc_do_concurrent_flag = do_concurrent_save;
13683 : :
13684 : 1103189 : if (!gfc_resolve_expr (code->expr2))
13685 : 598 : t = false;
13686 : :
13687 : 1103189 : if (code->op == EXEC_ALLOCATE
13688 : 1103189 : && !gfc_resolve_expr (code->expr3))
13689 : : t = false;
13690 : :
13691 : 1103189 : switch (code->op)
13692 : : {
13693 : : case EXEC_NOP:
13694 : : case EXEC_END_BLOCK:
13695 : : case EXEC_END_NESTED_BLOCK:
13696 : : case EXEC_CYCLE:
13697 : : case EXEC_PAUSE:
13698 : : break;
13699 : :
13700 : 211739 : case EXEC_STOP:
13701 : 211739 : case EXEC_ERROR_STOP:
13702 : 211739 : if (code->expr2 != NULL
13703 : 37 : && (code->expr2->ts.type != BT_LOGICAL
13704 : 37 : || code->expr2->rank != 0))
13705 : 0 : gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
13706 : : &code->expr2->where);
13707 : : break;
13708 : :
13709 : : case EXEC_EXIT:
13710 : : case EXEC_CONTINUE:
13711 : : case EXEC_DT_END:
13712 : : case EXEC_ASSIGN_CALL:
13713 : : break;
13714 : :
13715 : 50 : case EXEC_CRITICAL:
13716 : 50 : resolve_critical (code);
13717 : 50 : break;
13718 : :
13719 : 747 : case EXEC_SYNC_ALL:
13720 : 747 : case EXEC_SYNC_IMAGES:
13721 : 747 : case EXEC_SYNC_MEMORY:
13722 : 747 : resolve_sync (code);
13723 : 747 : break;
13724 : :
13725 : 136 : case EXEC_LOCK:
13726 : 136 : case EXEC_UNLOCK:
13727 : 136 : case EXEC_EVENT_POST:
13728 : 136 : case EXEC_EVENT_WAIT:
13729 : 136 : resolve_lock_unlock_event (code);
13730 : 136 : break;
13731 : :
13732 : : case EXEC_FAIL_IMAGE:
13733 : : break;
13734 : :
13735 : 107 : case EXEC_FORM_TEAM:
13736 : 107 : resolve_form_team (code);
13737 : 107 : break;
13738 : :
13739 : 66 : case EXEC_CHANGE_TEAM:
13740 : 66 : resolve_change_team (code);
13741 : 66 : break;
13742 : :
13743 : 64 : case EXEC_END_TEAM:
13744 : 64 : resolve_end_team (code);
13745 : 64 : break;
13746 : :
13747 : 22 : case EXEC_SYNC_TEAM:
13748 : 22 : resolve_sync_team (code);
13749 : 22 : break;
13750 : :
13751 : 1420 : case EXEC_ENTRY:
13752 : : /* Keep track of which entry we are up to. */
13753 : 1420 : current_entry_id = code->ext.entry->id;
13754 : 1420 : break;
13755 : :
13756 : 450 : case EXEC_WHERE:
13757 : 450 : resolve_where (code, NULL);
13758 : 450 : break;
13759 : :
13760 : 1249 : case EXEC_GOTO:
13761 : 1249 : if (code->expr1 != NULL)
13762 : : {
13763 : 78 : if (code->expr1->expr_type != EXPR_VARIABLE
13764 : 76 : || code->expr1->ts.type != BT_INTEGER
13765 : 76 : || (code->expr1->ref
13766 : 1 : && code->expr1->ref->type == REF_ARRAY)
13767 : 75 : || code->expr1->symtree == NULL
13768 : 75 : || (code->expr1->symtree->n.sym
13769 : 75 : && (code->expr1->symtree->n.sym->attr.flavor
13770 : 75 : == FL_PARAMETER)))
13771 : 4 : gfc_error ("ASSIGNED GOTO statement at %L requires a "
13772 : : "scalar INTEGER variable", &code->expr1->where);
13773 : 74 : else if (code->expr1->symtree->n.sym
13774 : 74 : && code->expr1->symtree->n.sym->attr.assign != 1)
13775 : 1 : gfc_error ("Variable %qs has not been assigned a target "
13776 : : "label at %L", code->expr1->symtree->n.sym->name,
13777 : : &code->expr1->where);
13778 : : }
13779 : : else
13780 : 1171 : resolve_branch (code->label1, code);
13781 : : break;
13782 : :
13783 : 3099 : case EXEC_RETURN:
13784 : 3099 : if (code->expr1 != NULL
13785 : 53 : && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
13786 : 1 : gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
13787 : : "INTEGER return specifier", &code->expr1->where);
13788 : : break;
13789 : :
13790 : : case EXEC_INIT_ASSIGN:
13791 : : case EXEC_END_PROCEDURE:
13792 : : break;
13793 : :
13794 : 280370 : case EXEC_ASSIGN:
13795 : 280370 : if (!t)
13796 : : break;
13797 : :
13798 : 279734 : if (flag_coarray == GFC_FCOARRAY_LIB
13799 : 279734 : && gfc_is_coindexed (code->expr1))
13800 : : {
13801 : : /* Insert a GFC_ISYM_CAF_SEND intrinsic, when the LHS is a
13802 : : coindexed variable. */
13803 : 390 : code->op = EXEC_CALL;
13804 : 390 : gfc_get_sym_tree (GFC_PREFIX ("caf_send"), ns, &code->symtree,
13805 : : true);
13806 : 390 : code->resolved_sym = code->symtree->n.sym;
13807 : 390 : code->resolved_sym->attr.flavor = FL_PROCEDURE;
13808 : 390 : code->resolved_sym->attr.intrinsic = 1;
13809 : 390 : code->resolved_sym->attr.subroutine = 1;
13810 : 390 : code->resolved_isym
13811 : 390 : = gfc_intrinsic_subroutine_by_id (GFC_ISYM_CAF_SEND);
13812 : 390 : gfc_commit_symbol (code->resolved_sym);
13813 : 390 : code->ext.actual = gfc_get_actual_arglist ();
13814 : 390 : code->ext.actual->expr = code->expr1;
13815 : 390 : code->ext.actual->next = gfc_get_actual_arglist ();
13816 : 390 : code->ext.actual->next->expr = code->expr2;
13817 : :
13818 : 390 : code->expr1 = NULL;
13819 : 390 : code->expr2 = NULL;
13820 : 390 : break;
13821 : : }
13822 : :
13823 : 279344 : if (code->expr1->ts.type == BT_CLASS)
13824 : 973 : gfc_find_vtab (&code->expr2->ts);
13825 : :
13826 : : /* If this is a pointer function in an lvalue variable context,
13827 : : the new code will have to be resolved afresh. This is also the
13828 : : case with an error, where the code is transformed into NOP to
13829 : : prevent ICEs downstream. */
13830 : 279344 : if (resolve_ptr_fcn_assign (&code, ns)
13831 : 279344 : || code->op == EXEC_NOP)
13832 : 205 : goto start;
13833 : :
13834 : 279139 : if (!gfc_check_vardef_context (code->expr1, false, false, false,
13835 : 279139 : _("assignment")))
13836 : : break;
13837 : :
13838 : 279101 : if (resolve_ordinary_assign (code, ns))
13839 : : {
13840 : 752 : if (omp_workshare_flag)
13841 : : {
13842 : 1 : gfc_error ("Expected intrinsic assignment in OMP WORKSHARE "
13843 : 1 : "at %L", &code->loc);
13844 : 1 : break;
13845 : : }
13846 : 751 : if (code->op == EXEC_COMPCALL)
13847 : 416 : goto compcall;
13848 : : else
13849 : 335 : goto call;
13850 : : }
13851 : :
13852 : : /* Check for dependencies in deferred character length array
13853 : : assignments and generate a temporary, if necessary. */
13854 : 278349 : if (code->op == EXEC_ASSIGN && deferred_op_assign (&code, ns))
13855 : : break;
13856 : :
13857 : : /* F03 7.4.1.3 for non-allocatable, non-pointer components. */
13858 : 278327 : if (code->op != EXEC_CALL && code->expr1->ts.type == BT_DERIVED
13859 : 6575 : && code->expr1->ts.u.derived
13860 : 6575 : && code->expr1->ts.u.derived->attr.defined_assign_comp)
13861 : 188 : generate_component_assignments (&code, ns);
13862 : 278139 : else if (code->op == EXEC_ASSIGN)
13863 : : {
13864 : 278139 : if (gfc_may_be_finalized (code->expr1->ts))
13865 : 1087 : code->expr1->must_finalize = 1;
13866 : 278139 : if (code->expr2->expr_type == EXPR_ARRAY
13867 : 278139 : && gfc_may_be_finalized (code->expr2->ts))
13868 : 43 : code->expr2->must_finalize = 1;
13869 : : }
13870 : :
13871 : : break;
13872 : :
13873 : 126 : case EXEC_LABEL_ASSIGN:
13874 : 126 : if (code->label1->defined == ST_LABEL_UNKNOWN)
13875 : 0 : gfc_error ("Label %d referenced at %L is never defined",
13876 : : code->label1->value, &code->label1->where);
13877 : 126 : if (t
13878 : 126 : && (code->expr1->expr_type != EXPR_VARIABLE
13879 : 126 : || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
13880 : 126 : || code->expr1->symtree->n.sym->ts.kind
13881 : 126 : != gfc_default_integer_kind
13882 : 126 : || code->expr1->symtree->n.sym->attr.flavor == FL_PARAMETER
13883 : 125 : || code->expr1->symtree->n.sym->as != NULL))
13884 : 2 : gfc_error ("ASSIGN statement at %L requires a scalar "
13885 : : "default INTEGER variable", &code->expr1->where);
13886 : : break;
13887 : :
13888 : 10199 : case EXEC_POINTER_ASSIGN:
13889 : 10199 : {
13890 : 10199 : gfc_expr* e;
13891 : :
13892 : 10199 : if (!t)
13893 : : break;
13894 : :
13895 : : /* This is both a variable definition and pointer assignment
13896 : : context, so check both of them. For rank remapping, a final
13897 : : array ref may be present on the LHS and fool gfc_expr_attr
13898 : : used in gfc_check_vardef_context. Remove it. */
13899 : 10194 : e = remove_last_array_ref (code->expr1);
13900 : 20388 : t = gfc_check_vardef_context (e, true, false, false,
13901 : 10194 : _("pointer assignment"));
13902 : 10194 : if (t)
13903 : 10173 : t = gfc_check_vardef_context (e, false, false, false,
13904 : 10173 : _("pointer assignment"));
13905 : 10194 : gfc_free_expr (e);
13906 : :
13907 : 1113043 : t = gfc_check_pointer_assign (code->expr1, code->expr2, !t) && t;
13908 : :
13909 : 10060 : if (!t)
13910 : : break;
13911 : :
13912 : : /* Assigning a class object always is a regular assign. */
13913 : 10060 : if (code->expr2->ts.type == BT_CLASS
13914 : 553 : && code->expr1->ts.type == BT_CLASS
13915 : 468 : && CLASS_DATA (code->expr2)
13916 : 467 : && !CLASS_DATA (code->expr2)->attr.dimension
13917 : 10667 : && !(gfc_expr_attr (code->expr1).proc_pointer
13918 : 54 : && code->expr2->expr_type == EXPR_VARIABLE
13919 : 42 : && code->expr2->symtree->n.sym->attr.flavor
13920 : 42 : == FL_PROCEDURE))
13921 : 324 : code->op = EXEC_ASSIGN;
13922 : : break;
13923 : : }
13924 : :
13925 : 72 : case EXEC_ARITHMETIC_IF:
13926 : 72 : {
13927 : 72 : gfc_expr *e = code->expr1;
13928 : :
13929 : 72 : gfc_resolve_expr (e);
13930 : 72 : if (e->expr_type == EXPR_NULL)
13931 : 1 : gfc_error ("Invalid NULL at %L", &e->where);
13932 : :
13933 : 72 : if (t && (e->rank > 0
13934 : 68 : || !(e->ts.type == BT_REAL || e->ts.type == BT_INTEGER)))
13935 : 5 : gfc_error ("Arithmetic IF statement at %L requires a scalar "
13936 : : "REAL or INTEGER expression", &e->where);
13937 : :
13938 : 72 : resolve_branch (code->label1, code);
13939 : 72 : resolve_branch (code->label2, code);
13940 : 72 : resolve_branch (code->label3, code);
13941 : : }
13942 : 72 : break;
13943 : :
13944 : 224641 : case EXEC_IF:
13945 : 224641 : if (t && code->expr1 != NULL
13946 : 0 : && (code->expr1->ts.type != BT_LOGICAL
13947 : 0 : || code->expr1->rank != 0))
13948 : 0 : gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
13949 : : &code->expr1->where);
13950 : : break;
13951 : :
13952 : 77264 : case EXEC_CALL:
13953 : 77264 : call:
13954 : 77264 : resolve_call (code);
13955 : 77264 : break;
13956 : :
13957 : 1675 : case EXEC_COMPCALL:
13958 : 1675 : compcall:
13959 : 1675 : resolve_typebound_subroutine (code);
13960 : 1675 : break;
13961 : :
13962 : 123 : case EXEC_CALL_PPC:
13963 : 123 : resolve_ppc_call (code);
13964 : 123 : break;
13965 : :
13966 : 683 : case EXEC_SELECT:
13967 : : /* Select is complicated. Also, a SELECT construct could be
13968 : : a transformed computed GOTO. */
13969 : 683 : resolve_select (code, false);
13970 : 683 : break;
13971 : :
13972 : 2894 : case EXEC_SELECT_TYPE:
13973 : 2894 : resolve_select_type (code, ns);
13974 : 2894 : break;
13975 : :
13976 : 1018 : case EXEC_SELECT_RANK:
13977 : 1018 : resolve_select_rank (code, ns);
13978 : 1018 : break;
13979 : :
13980 : 7546 : case EXEC_BLOCK:
13981 : 7546 : resolve_block_construct (code);
13982 : 7546 : break;
13983 : :
13984 : 32289 : case EXEC_DO:
13985 : 32289 : if (code->ext.iterator != NULL)
13986 : : {
13987 : 32289 : gfc_iterator *iter = code->ext.iterator;
13988 : 32289 : if (gfc_resolve_iterator (iter, true, false))
13989 : 32275 : gfc_resolve_do_iterator (code, iter->var->symtree->n.sym,
13990 : : true);
13991 : : }
13992 : : break;
13993 : :
13994 : 525 : case EXEC_DO_WHILE:
13995 : 525 : if (code->expr1 == NULL)
13996 : 0 : gfc_internal_error ("gfc_resolve_code(): No expression on "
13997 : : "DO WHILE");
13998 : 525 : if (t
13999 : 525 : && (code->expr1->rank != 0
14000 : 525 : || code->expr1->ts.type != BT_LOGICAL))
14001 : 0 : gfc_error ("Exit condition of DO WHILE loop at %L must be "
14002 : : "a scalar LOGICAL expression", &code->expr1->where);
14003 : : break;
14004 : :
14005 : 13684 : case EXEC_ALLOCATE:
14006 : 13684 : if (t)
14007 : 13682 : resolve_allocate_deallocate (code, "ALLOCATE");
14008 : :
14009 : : break;
14010 : :
14011 : 5718 : case EXEC_DEALLOCATE:
14012 : 5718 : if (t)
14013 : 5718 : resolve_allocate_deallocate (code, "DEALLOCATE");
14014 : :
14015 : : break;
14016 : :
14017 : 3873 : case EXEC_OPEN:
14018 : 3873 : if (!gfc_resolve_open (code->ext.open, &code->loc))
14019 : : break;
14020 : :
14021 : 3646 : resolve_branch (code->ext.open->err, code);
14022 : 3646 : break;
14023 : :
14024 : 3061 : case EXEC_CLOSE:
14025 : 3061 : if (!gfc_resolve_close (code->ext.close, &code->loc))
14026 : : break;
14027 : :
14028 : 3027 : resolve_branch (code->ext.close->err, code);
14029 : 3027 : break;
14030 : :
14031 : 2773 : case EXEC_BACKSPACE:
14032 : 2773 : case EXEC_ENDFILE:
14033 : 2773 : case EXEC_REWIND:
14034 : 2773 : case EXEC_FLUSH:
14035 : 2773 : if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
14036 : : break;
14037 : :
14038 : 2707 : resolve_branch (code->ext.filepos->err, code);
14039 : 2707 : break;
14040 : :
14041 : 817 : case EXEC_INQUIRE:
14042 : 817 : if (!gfc_resolve_inquire (code->ext.inquire))
14043 : : break;
14044 : :
14045 : 769 : resolve_branch (code->ext.inquire->err, code);
14046 : 769 : break;
14047 : :
14048 : 92 : case EXEC_IOLENGTH:
14049 : 92 : gcc_assert (code->ext.inquire != NULL);
14050 : 92 : if (!gfc_resolve_inquire (code->ext.inquire))
14051 : : break;
14052 : :
14053 : 90 : resolve_branch (code->ext.inquire->err, code);
14054 : 90 : break;
14055 : :
14056 : 89 : case EXEC_WAIT:
14057 : 89 : if (!gfc_resolve_wait (code->ext.wait))
14058 : : break;
14059 : :
14060 : 74 : resolve_branch (code->ext.wait->err, code);
14061 : 74 : resolve_branch (code->ext.wait->end, code);
14062 : 74 : resolve_branch (code->ext.wait->eor, code);
14063 : 74 : break;
14064 : :
14065 : 31448 : case EXEC_READ:
14066 : 31448 : case EXEC_WRITE:
14067 : 31448 : if (!gfc_resolve_dt (code, code->ext.dt, &code->loc))
14068 : : break;
14069 : :
14070 : 31139 : resolve_branch (code->ext.dt->err, code);
14071 : 31139 : resolve_branch (code->ext.dt->end, code);
14072 : 31139 : resolve_branch (code->ext.dt->eor, code);
14073 : 31139 : break;
14074 : :
14075 : 45098 : case EXEC_TRANSFER:
14076 : 45098 : resolve_transfer (code);
14077 : 45098 : break;
14078 : :
14079 : 2161 : case EXEC_DO_CONCURRENT:
14080 : 2161 : case EXEC_FORALL:
14081 : 2161 : resolve_forall_iterators (code->ext.concur.forall_iterator);
14082 : :
14083 : 2161 : if (code->expr1 != NULL
14084 : 730 : && (code->expr1->ts.type != BT_LOGICAL || code->expr1->rank))
14085 : 2 : gfc_error ("FORALL mask clause at %L requires a scalar LOGICAL "
14086 : : "expression", &code->expr1->where);
14087 : :
14088 : 2161 : if (code->op == EXEC_DO_CONCURRENT)
14089 : 171 : resolve_locality_spec (code, ns);
14090 : : break;
14091 : :
14092 : 13144 : case EXEC_OACC_PARALLEL_LOOP:
14093 : 13144 : case EXEC_OACC_PARALLEL:
14094 : 13144 : case EXEC_OACC_KERNELS_LOOP:
14095 : 13144 : case EXEC_OACC_KERNELS:
14096 : 13144 : case EXEC_OACC_SERIAL_LOOP:
14097 : 13144 : case EXEC_OACC_SERIAL:
14098 : 13144 : case EXEC_OACC_DATA:
14099 : 13144 : case EXEC_OACC_HOST_DATA:
14100 : 13144 : case EXEC_OACC_LOOP:
14101 : 13144 : case EXEC_OACC_UPDATE:
14102 : 13144 : case EXEC_OACC_WAIT:
14103 : 13144 : case EXEC_OACC_CACHE:
14104 : 13144 : case EXEC_OACC_ENTER_DATA:
14105 : 13144 : case EXEC_OACC_EXIT_DATA:
14106 : 13144 : case EXEC_OACC_ATOMIC:
14107 : 13144 : case EXEC_OACC_DECLARE:
14108 : 13144 : gfc_resolve_oacc_directive (code, ns);
14109 : 13144 : break;
14110 : :
14111 : 16584 : case EXEC_OMP_ALLOCATE:
14112 : 16584 : case EXEC_OMP_ALLOCATORS:
14113 : 16584 : case EXEC_OMP_ASSUME:
14114 : 16584 : case EXEC_OMP_ATOMIC:
14115 : 16584 : case EXEC_OMP_BARRIER:
14116 : 16584 : case EXEC_OMP_CANCEL:
14117 : 16584 : case EXEC_OMP_CANCELLATION_POINT:
14118 : 16584 : case EXEC_OMP_CRITICAL:
14119 : 16584 : case EXEC_OMP_FLUSH:
14120 : 16584 : case EXEC_OMP_DEPOBJ:
14121 : 16584 : case EXEC_OMP_DISPATCH:
14122 : 16584 : case EXEC_OMP_DISTRIBUTE:
14123 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
14124 : 16584 : case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
14125 : 16584 : case EXEC_OMP_DISTRIBUTE_SIMD:
14126 : 16584 : case EXEC_OMP_DO:
14127 : 16584 : case EXEC_OMP_DO_SIMD:
14128 : 16584 : case EXEC_OMP_ERROR:
14129 : 16584 : case EXEC_OMP_INTEROP:
14130 : 16584 : case EXEC_OMP_LOOP:
14131 : 16584 : case EXEC_OMP_MASTER:
14132 : 16584 : case EXEC_OMP_MASTER_TASKLOOP:
14133 : 16584 : case EXEC_OMP_MASTER_TASKLOOP_SIMD:
14134 : 16584 : case EXEC_OMP_MASKED:
14135 : 16584 : case EXEC_OMP_MASKED_TASKLOOP:
14136 : 16584 : case EXEC_OMP_MASKED_TASKLOOP_SIMD:
14137 : 16584 : case EXEC_OMP_METADIRECTIVE:
14138 : 16584 : case EXEC_OMP_ORDERED:
14139 : 16584 : case EXEC_OMP_SCAN:
14140 : 16584 : case EXEC_OMP_SCOPE:
14141 : 16584 : case EXEC_OMP_SECTIONS:
14142 : 16584 : case EXEC_OMP_SIMD:
14143 : 16584 : case EXEC_OMP_SINGLE:
14144 : 16584 : case EXEC_OMP_TARGET:
14145 : 16584 : case EXEC_OMP_TARGET_DATA:
14146 : 16584 : case EXEC_OMP_TARGET_ENTER_DATA:
14147 : 16584 : case EXEC_OMP_TARGET_EXIT_DATA:
14148 : 16584 : case EXEC_OMP_TARGET_PARALLEL:
14149 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO:
14150 : 16584 : case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
14151 : 16584 : case EXEC_OMP_TARGET_PARALLEL_LOOP:
14152 : 16584 : case EXEC_OMP_TARGET_SIMD:
14153 : 16584 : case EXEC_OMP_TARGET_TEAMS:
14154 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
14155 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
14156 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14157 : 16584 : case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
14158 : 16584 : case EXEC_OMP_TARGET_TEAMS_LOOP:
14159 : 16584 : case EXEC_OMP_TARGET_UPDATE:
14160 : 16584 : case EXEC_OMP_TASK:
14161 : 16584 : case EXEC_OMP_TASKGROUP:
14162 : 16584 : case EXEC_OMP_TASKLOOP:
14163 : 16584 : case EXEC_OMP_TASKLOOP_SIMD:
14164 : 16584 : case EXEC_OMP_TASKWAIT:
14165 : 16584 : case EXEC_OMP_TASKYIELD:
14166 : 16584 : case EXEC_OMP_TEAMS:
14167 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE:
14168 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
14169 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
14170 : 16584 : case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
14171 : 16584 : case EXEC_OMP_TEAMS_LOOP:
14172 : 16584 : case EXEC_OMP_TILE:
14173 : 16584 : case EXEC_OMP_UNROLL:
14174 : 16584 : case EXEC_OMP_WORKSHARE:
14175 : 16584 : gfc_resolve_omp_directive (code, ns);
14176 : 16584 : break;
14177 : :
14178 : 3833 : case EXEC_OMP_PARALLEL:
14179 : 3833 : case EXEC_OMP_PARALLEL_DO:
14180 : 3833 : case EXEC_OMP_PARALLEL_DO_SIMD:
14181 : 3833 : case EXEC_OMP_PARALLEL_LOOP:
14182 : 3833 : case EXEC_OMP_PARALLEL_MASKED:
14183 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
14184 : 3833 : case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
14185 : 3833 : case EXEC_OMP_PARALLEL_MASTER:
14186 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
14187 : 3833 : case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
14188 : 3833 : case EXEC_OMP_PARALLEL_SECTIONS:
14189 : 3833 : case EXEC_OMP_PARALLEL_WORKSHARE:
14190 : 3833 : omp_workshare_save = omp_workshare_flag;
14191 : 3833 : omp_workshare_flag = 0;
14192 : 3833 : gfc_resolve_omp_directive (code, ns);
14193 : 3833 : omp_workshare_flag = omp_workshare_save;
14194 : 3833 : break;
14195 : :
14196 : 0 : default:
14197 : 0 : gfc_internal_error ("gfc_resolve_code(): Bad statement code");
14198 : : }
14199 : : }
14200 : :
14201 : 657469 : cs_base = frame.prev;
14202 : 657469 : }
14203 : :
14204 : :
14205 : : /* Resolve initial values and make sure they are compatible with
14206 : : the variable. */
14207 : :
14208 : : static void
14209 : 1779053 : resolve_values (gfc_symbol *sym)
14210 : : {
14211 : 1779053 : bool t;
14212 : :
14213 : 1779053 : if (sym->value == NULL)
14214 : : return;
14215 : :
14216 : 401579 : if (sym->attr.ext_attr & (1 << EXT_ATTR_DEPRECATED) && sym->attr.referenced)
14217 : 4 : gfc_warning (OPT_Wdeprecated_declarations,
14218 : : "Using parameter %qs declared at %L is deprecated",
14219 : : sym->name, &sym->declared_at);
14220 : :
14221 : 401579 : if (sym->value->expr_type == EXPR_STRUCTURE)
14222 : 37794 : t= resolve_structure_cons (sym->value, 1);
14223 : : else
14224 : 363785 : t = gfc_resolve_expr (sym->value);
14225 : :
14226 : 401579 : if (!t)
14227 : : return;
14228 : :
14229 : 401577 : gfc_check_assign_symbol (sym, NULL, sym->value);
14230 : : }
14231 : :
14232 : :
14233 : : /* Verify any BIND(C) derived types in the namespace so we can report errors
14234 : : for them once, rather than for each variable declared of that type. */
14235 : :
14236 : : static void
14237 : 1752009 : resolve_bind_c_derived_types (gfc_symbol *derived_sym)
14238 : : {
14239 : 1752009 : if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
14240 : 79077 : && derived_sym->attr.is_bind_c == 1)
14241 : 26363 : verify_bind_c_derived_type (derived_sym);
14242 : :
14243 : 1752009 : return;
14244 : : }
14245 : :
14246 : :
14247 : : /* Check the interfaces of DTIO procedures associated with derived
14248 : : type 'sym'. These procedures can either have typebound bindings or
14249 : : can appear in DTIO generic interfaces. */
14250 : :
14251 : : static void
14252 : 1780021 : gfc_verify_DTIO_procedures (gfc_symbol *sym)
14253 : : {
14254 : 1780021 : if (!sym || sym->attr.flavor != FL_DERIVED)
14255 : : return;
14256 : :
14257 : 87716 : gfc_check_dtio_interfaces (sym);
14258 : :
14259 : 87716 : return;
14260 : : }
14261 : :
14262 : : /* Verify that any binding labels used in a given namespace do not collide
14263 : : with the names or binding labels of any global symbols. Multiple INTERFACE
14264 : : for the same procedure are permitted. Abstract interfaces and dummy
14265 : : arguments are not checked. */
14266 : :
14267 : : static void
14268 : 1780021 : gfc_verify_binding_labels (gfc_symbol *sym)
14269 : : {
14270 : 1780021 : gfc_gsymbol *gsym;
14271 : 1780021 : const char *module;
14272 : :
14273 : 1780021 : if (!sym || !sym->attr.is_bind_c || sym->attr.is_iso_c
14274 : 60318 : || sym->attr.flavor == FL_DERIVED || !sym->binding_label
14275 : 33041 : || sym->attr.abstract || sym->attr.dummy)
14276 : : return;
14277 : :
14278 : 32943 : gsym = gfc_find_case_gsymbol (gfc_gsym_root, sym->binding_label);
14279 : :
14280 : 32943 : if (sym->module)
14281 : : module = sym->module;
14282 : 11923 : else if (sym->ns && sym->ns->proc_name
14283 : 11923 : && sym->ns->proc_name->attr.flavor == FL_MODULE)
14284 : 4477 : module = sym->ns->proc_name->name;
14285 : 7446 : else if (sym->ns && sym->ns->parent
14286 : 358 : && sym->ns && sym->ns->parent->proc_name
14287 : 358 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
14288 : 272 : module = sym->ns->parent->proc_name->name;
14289 : : else
14290 : : module = NULL;
14291 : :
14292 : 32943 : if (!gsym
14293 : 11262 : || (!gsym->defined
14294 : 8436 : && (gsym->type == GSYM_FUNCTION || gsym->type == GSYM_SUBROUTINE)))
14295 : : {
14296 : 21681 : if (!gsym)
14297 : 21681 : gsym = gfc_get_gsymbol (sym->binding_label, true);
14298 : 30117 : gsym->where = sym->declared_at;
14299 : 30117 : gsym->sym_name = sym->name;
14300 : 30117 : gsym->binding_label = sym->binding_label;
14301 : 30117 : gsym->ns = sym->ns;
14302 : 30117 : gsym->mod_name = module;
14303 : 30117 : if (sym->attr.function)
14304 : 19358 : gsym->type = GSYM_FUNCTION;
14305 : 10759 : else if (sym->attr.subroutine)
14306 : 10620 : gsym->type = GSYM_SUBROUTINE;
14307 : : /* Mark as variable/procedure as defined, unless its an INTERFACE. */
14308 : 30117 : gsym->defined = sym->attr.if_source != IFSRC_IFBODY;
14309 : 30117 : return;
14310 : : }
14311 : :
14312 : 2826 : if (sym->attr.flavor == FL_VARIABLE && gsym->type != GSYM_UNKNOWN)
14313 : : {
14314 : 1 : gfc_error ("Variable %qs with binding label %qs at %L uses the same global "
14315 : : "identifier as entity at %L", sym->name,
14316 : : sym->binding_label, &sym->declared_at, &gsym->where);
14317 : : /* Clear the binding label to prevent checking multiple times. */
14318 : 1 : sym->binding_label = NULL;
14319 : 1 : return;
14320 : : }
14321 : :
14322 : 2825 : if (sym->attr.flavor == FL_VARIABLE && module
14323 : 37 : && (strcmp (module, gsym->mod_name) != 0
14324 : 35 : || strcmp (sym->name, gsym->sym_name) != 0))
14325 : : {
14326 : : /* This can only happen if the variable is defined in a module - if it
14327 : : isn't the same module, reject it. */
14328 : 3 : gfc_error ("Variable %qs from module %qs with binding label %qs at %L "
14329 : : "uses the same global identifier as entity at %L from module %qs",
14330 : : sym->name, module, sym->binding_label,
14331 : : &sym->declared_at, &gsym->where, gsym->mod_name);
14332 : 3 : sym->binding_label = NULL;
14333 : 3 : return;
14334 : : }
14335 : :
14336 : 2822 : if ((sym->attr.function || sym->attr.subroutine)
14337 : 2786 : && ((gsym->type != GSYM_SUBROUTINE && gsym->type != GSYM_FUNCTION)
14338 : 2784 : || (gsym->defined && sym->attr.if_source != IFSRC_IFBODY))
14339 : 2471 : && (sym != gsym->ns->proc_name && sym->attr.entry == 0)
14340 : 2083 : && (module != gsym->mod_name
14341 : 2079 : || strcmp (gsym->sym_name, sym->name) != 0
14342 : 2079 : || (module && strcmp (module, gsym->mod_name) != 0)))
14343 : : {
14344 : : /* Print an error if the procedure is defined multiple times; we have to
14345 : : exclude references to the same procedure via module association or
14346 : : multiple checks for the same procedure. */
14347 : 4 : gfc_error ("Procedure %qs with binding label %qs at %L uses the same "
14348 : : "global identifier as entity at %L", sym->name,
14349 : : sym->binding_label, &sym->declared_at, &gsym->where);
14350 : 4 : sym->binding_label = NULL;
14351 : : }
14352 : : }
14353 : :
14354 : :
14355 : : /* Resolve an index expression. */
14356 : :
14357 : : static bool
14358 : 260167 : resolve_index_expr (gfc_expr *e)
14359 : : {
14360 : 260167 : if (!gfc_resolve_expr (e))
14361 : : return false;
14362 : :
14363 : 260157 : if (!gfc_simplify_expr (e, 0))
14364 : : return false;
14365 : :
14366 : 260155 : if (!gfc_specification_expr (e))
14367 : : return false;
14368 : :
14369 : : return true;
14370 : : }
14371 : :
14372 : :
14373 : : /* Resolve a charlen structure. */
14374 : :
14375 : : static bool
14376 : 99808 : resolve_charlen (gfc_charlen *cl)
14377 : : {
14378 : 99808 : int k;
14379 : 99808 : bool saved_specification_expr;
14380 : :
14381 : 99808 : if (cl->resolved)
14382 : : return true;
14383 : :
14384 : 91673 : cl->resolved = 1;
14385 : 91673 : saved_specification_expr = specification_expr;
14386 : 91673 : specification_expr = true;
14387 : :
14388 : 91673 : if (cl->length_from_typespec)
14389 : : {
14390 : 1090 : if (!gfc_resolve_expr (cl->length))
14391 : : {
14392 : 1 : specification_expr = saved_specification_expr;
14393 : 1 : return false;
14394 : : }
14395 : :
14396 : 1089 : if (!gfc_simplify_expr (cl->length, 0))
14397 : : {
14398 : 0 : specification_expr = saved_specification_expr;
14399 : 0 : return false;
14400 : : }
14401 : :
14402 : : /* cl->length has been resolved. It should have an integer type. */
14403 : 1089 : if (cl->length
14404 : 1088 : && (cl->length->ts.type != BT_INTEGER || cl->length->rank != 0))
14405 : : {
14406 : 4 : gfc_error ("Scalar INTEGER expression expected at %L",
14407 : : &cl->length->where);
14408 : 4 : return false;
14409 : : }
14410 : : }
14411 : : else
14412 : : {
14413 : 90583 : if (!resolve_index_expr (cl->length))
14414 : : {
14415 : 19 : specification_expr = saved_specification_expr;
14416 : 19 : return false;
14417 : : }
14418 : : }
14419 : :
14420 : : /* F2008, 4.4.3.2: If the character length parameter value evaluates to
14421 : : a negative value, the length of character entities declared is zero. */
14422 : 91649 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14423 : 54712 : && mpz_sgn (cl->length->value.integer) < 0)
14424 : 0 : gfc_replace_expr (cl->length,
14425 : : gfc_get_int_expr (gfc_charlen_int_kind, NULL, 0));
14426 : :
14427 : : /* Check that the character length is not too large. */
14428 : 91649 : k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
14429 : 91649 : if (cl->length && cl->length->expr_type == EXPR_CONSTANT
14430 : 54712 : && cl->length->ts.type == BT_INTEGER
14431 : 54712 : && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
14432 : : {
14433 : 4 : gfc_error ("String length at %L is too large", &cl->length->where);
14434 : 4 : specification_expr = saved_specification_expr;
14435 : 4 : return false;
14436 : : }
14437 : :
14438 : 91645 : specification_expr = saved_specification_expr;
14439 : 91645 : return true;
14440 : : }
14441 : :
14442 : :
14443 : : /* Test for non-constant shape arrays. */
14444 : :
14445 : : static bool
14446 : 114820 : is_non_constant_shape_array (gfc_symbol *sym)
14447 : : {
14448 : 114820 : gfc_expr *e;
14449 : 114820 : int i;
14450 : 114820 : bool not_constant;
14451 : :
14452 : 114820 : not_constant = false;
14453 : 114820 : if (sym->as != NULL)
14454 : : {
14455 : : /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
14456 : : has not been simplified; parameter array references. Do the
14457 : : simplification now. */
14458 : 152392 : for (i = 0; i < sym->as->rank + sym->as->corank; i++)
14459 : : {
14460 : 88055 : if (i == GFC_MAX_DIMENSIONS)
14461 : : break;
14462 : :
14463 : 88053 : e = sym->as->lower[i];
14464 : 88053 : if (e && (!resolve_index_expr(e)
14465 : 85298 : || !gfc_is_constant_expr (e)))
14466 : : not_constant = true;
14467 : 88053 : e = sym->as->upper[i];
14468 : 88053 : if (e && (!resolve_index_expr(e)
14469 : 84258 : || !gfc_is_constant_expr (e)))
14470 : : not_constant = true;
14471 : : }
14472 : : }
14473 : 114820 : return not_constant;
14474 : : }
14475 : :
14476 : : /* Given a symbol and an initialization expression, add code to initialize
14477 : : the symbol to the function entry. */
14478 : : static void
14479 : 1912 : build_init_assign (gfc_symbol *sym, gfc_expr *init)
14480 : : {
14481 : 1912 : gfc_expr *lval;
14482 : 1912 : gfc_code *init_st;
14483 : 1912 : gfc_namespace *ns = sym->ns;
14484 : :
14485 : : /* Search for the function namespace if this is a contained
14486 : : function without an explicit result. */
14487 : 1912 : if (sym->attr.function && sym == sym->result
14488 : 297 : && sym->name != sym->ns->proc_name->name)
14489 : : {
14490 : 296 : ns = ns->contained;
14491 : 1353 : for (;ns; ns = ns->sibling)
14492 : 1287 : if (strcmp (ns->proc_name->name, sym->name) == 0)
14493 : : break;
14494 : : }
14495 : :
14496 : 1912 : if (ns == NULL)
14497 : : {
14498 : 66 : gfc_free_expr (init);
14499 : 66 : return;
14500 : : }
14501 : :
14502 : : /* Build an l-value expression for the result. */
14503 : 1846 : lval = gfc_lval_expr_from_sym (sym);
14504 : :
14505 : : /* Add the code at scope entry. */
14506 : 1846 : init_st = gfc_get_code (EXEC_INIT_ASSIGN);
14507 : 1846 : init_st->next = ns->code;
14508 : 1846 : ns->code = init_st;
14509 : :
14510 : : /* Assign the default initializer to the l-value. */
14511 : 1846 : init_st->loc = sym->declared_at;
14512 : 1846 : init_st->expr1 = lval;
14513 : 1846 : init_st->expr2 = init;
14514 : : }
14515 : :
14516 : :
14517 : : /* Whether or not we can generate a default initializer for a symbol. */
14518 : :
14519 : : static bool
14520 : 28661 : can_generate_init (gfc_symbol *sym)
14521 : : {
14522 : 28661 : symbol_attribute *a;
14523 : 28661 : if (!sym)
14524 : : return false;
14525 : 28661 : a = &sym->attr;
14526 : :
14527 : : /* These symbols should never have a default initialization. */
14528 : 46966 : return !(
14529 : 28661 : a->allocatable
14530 : 28661 : || a->external
14531 : 27516 : || a->pointer
14532 : 27516 : || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
14533 : 5477 : && (CLASS_DATA (sym)->attr.class_pointer
14534 : 3580 : || CLASS_DATA (sym)->attr.proc_pointer))
14535 : 25619 : || a->in_equivalence
14536 : 25498 : || a->in_common
14537 : 25451 : || a->data
14538 : 25273 : || sym->module
14539 : 21540 : || a->cray_pointee
14540 : 21478 : || a->cray_pointer
14541 : 21478 : || sym->assoc
14542 : 18930 : || (!a->referenced && !a->result)
14543 : 18305 : || (a->dummy && (a->intent != INTENT_OUT
14544 : 1049 : || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY))
14545 : 18305 : || (a->function && sym != sym->result)
14546 : : );
14547 : : }
14548 : :
14549 : :
14550 : : /* Assign the default initializer to a derived type variable or result. */
14551 : :
14552 : : static void
14553 : 10804 : apply_default_init (gfc_symbol *sym)
14554 : : {
14555 : 10804 : gfc_expr *init = NULL;
14556 : :
14557 : 10804 : if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14558 : : return;
14559 : :
14560 : 10804 : if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
14561 : 9997 : init = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14562 : :
14563 : 10804 : if (init == NULL && sym->ts.type != BT_CLASS)
14564 : : return;
14565 : :
14566 : 1530 : build_init_assign (sym, init);
14567 : 1530 : sym->attr.referenced = 1;
14568 : : }
14569 : :
14570 : :
14571 : : /* Build an initializer for a local. Returns null if the symbol should not have
14572 : : a default initialization. */
14573 : :
14574 : : static gfc_expr *
14575 : 198820 : build_default_init_expr (gfc_symbol *sym)
14576 : : {
14577 : : /* These symbols should never have a default initialization. */
14578 : 198820 : if (sym->attr.allocatable
14579 : 185576 : || sym->attr.external
14580 : 185576 : || sym->attr.dummy
14581 : 122201 : || sym->attr.pointer
14582 : 114202 : || sym->attr.in_equivalence
14583 : 111826 : || sym->attr.in_common
14584 : 108774 : || sym->attr.data
14585 : 106478 : || sym->module
14586 : 104147 : || sym->attr.cray_pointee
14587 : 103846 : || sym->attr.cray_pointer
14588 : 103544 : || sym->assoc)
14589 : : return NULL;
14590 : :
14591 : : /* Get the appropriate init expression. */
14592 : 99110 : return gfc_build_default_init_expr (&sym->ts, &sym->declared_at);
14593 : : }
14594 : :
14595 : : /* Add an initialization expression to a local variable. */
14596 : : static void
14597 : 198820 : apply_default_init_local (gfc_symbol *sym)
14598 : : {
14599 : 198820 : gfc_expr *init = NULL;
14600 : :
14601 : : /* The symbol should be a variable or a function return value. */
14602 : 198820 : if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
14603 : 198820 : || (sym->attr.function && sym->result != sym))
14604 : : return;
14605 : :
14606 : : /* Try to build the initializer expression. If we can't initialize
14607 : : this symbol, then init will be NULL. */
14608 : 198820 : init = build_default_init_expr (sym);
14609 : 198820 : if (init == NULL)
14610 : : return;
14611 : :
14612 : : /* For saved variables, we don't want to add an initializer at function
14613 : : entry, so we just add a static initializer. Note that automatic variables
14614 : : are stack allocated even with -fno-automatic; we have also to exclude
14615 : : result variable, which are also nonstatic. */
14616 : 419 : if (!sym->attr.automatic
14617 : 419 : && (sym->attr.save || sym->ns->save_all
14618 : 377 : || (flag_max_stack_var_size == 0 && !sym->attr.result
14619 : 27 : && (sym->ns->proc_name && !sym->ns->proc_name->attr.recursive)
14620 : 14 : && (!sym->attr.dimension || !is_non_constant_shape_array (sym)))))
14621 : : {
14622 : : /* Don't clobber an existing initializer! */
14623 : 37 : gcc_assert (sym->value == NULL);
14624 : 37 : sym->value = init;
14625 : 37 : return;
14626 : : }
14627 : :
14628 : 382 : build_init_assign (sym, init);
14629 : : }
14630 : :
14631 : :
14632 : : /* Resolution of common features of flavors variable and procedure. */
14633 : :
14634 : : static bool
14635 : 938783 : resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
14636 : : {
14637 : 938783 : gfc_array_spec *as;
14638 : :
14639 : 938783 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14640 : 18447 : && sym->ts.u.derived && CLASS_DATA (sym))
14641 : 18441 : as = CLASS_DATA (sym)->as;
14642 : : else
14643 : 920342 : as = sym->as;
14644 : :
14645 : : /* Constraints on deferred shape variable. */
14646 : 938783 : if (as == NULL || as->type != AS_DEFERRED)
14647 : : {
14648 : 915242 : bool pointer, allocatable, dimension;
14649 : :
14650 : 915242 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok
14651 : 15374 : && sym->ts.u.derived && CLASS_DATA (sym))
14652 : : {
14653 : 15368 : pointer = CLASS_DATA (sym)->attr.class_pointer;
14654 : 15368 : allocatable = CLASS_DATA (sym)->attr.allocatable;
14655 : 15368 : dimension = CLASS_DATA (sym)->attr.dimension;
14656 : : }
14657 : : else
14658 : : {
14659 : 899874 : pointer = sym->attr.pointer && !sym->attr.select_type_temporary;
14660 : 899874 : allocatable = sym->attr.allocatable;
14661 : 899874 : dimension = sym->attr.dimension;
14662 : : }
14663 : :
14664 : 915242 : if (allocatable)
14665 : : {
14666 : 7805 : if (dimension
14667 : 7805 : && as
14668 : 523 : && as->type != AS_ASSUMED_RANK
14669 : 5 : && !sym->attr.select_rank_temporary)
14670 : : {
14671 : 3 : gfc_error ("Allocatable array %qs at %L must have a deferred "
14672 : : "shape or assumed rank", sym->name, &sym->declared_at);
14673 : 3 : return false;
14674 : : }
14675 : 7802 : else if (!gfc_notify_std (GFC_STD_F2003, "Scalar object "
14676 : : "%qs at %L may not be ALLOCATABLE",
14677 : : sym->name, &sym->declared_at))
14678 : : return false;
14679 : : }
14680 : :
14681 : 915238 : if (pointer && dimension && as->type != AS_ASSUMED_RANK)
14682 : : {
14683 : 4 : gfc_error ("Array pointer %qs at %L must have a deferred shape or "
14684 : : "assumed rank", sym->name, &sym->declared_at);
14685 : 4 : sym->error = 1;
14686 : 4 : return false;
14687 : : }
14688 : : }
14689 : : else
14690 : : {
14691 : 23541 : if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
14692 : 4481 : && sym->ts.type != BT_CLASS && !sym->assoc)
14693 : : {
14694 : 3 : gfc_error ("Array %qs at %L cannot have a deferred shape",
14695 : : sym->name, &sym->declared_at);
14696 : 3 : return false;
14697 : : }
14698 : : }
14699 : :
14700 : : /* Constraints on polymorphic variables. */
14701 : 938772 : if (sym->ts.type == BT_CLASS && !(sym->result && sym->result != sym))
14702 : : {
14703 : : /* F03:C502. */
14704 : 17787 : if (sym->attr.class_ok
14705 : 17731 : && sym->ts.u.derived
14706 : 17726 : && !sym->attr.select_type_temporary
14707 : 16684 : && !UNLIMITED_POLY (sym)
14708 : 14300 : && CLASS_DATA (sym)
14709 : 14299 : && CLASS_DATA (sym)->ts.u.derived
14710 : 32085 : && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
14711 : : {
14712 : 5 : gfc_error ("Type %qs of CLASS variable %qs at %L is not extensible",
14713 : 5 : CLASS_DATA (sym)->ts.u.derived->name, sym->name,
14714 : : &sym->declared_at);
14715 : 5 : return false;
14716 : : }
14717 : :
14718 : : /* F03:C509. */
14719 : : /* Assume that use associated symbols were checked in the module ns.
14720 : : Class-variables that are associate-names are also something special
14721 : : and excepted from the test. */
14722 : 17782 : if (!sym->attr.class_ok && !sym->attr.use_assoc && !sym->assoc
14723 : 54 : && !sym->attr.select_type_temporary
14724 : 54 : && !sym->attr.select_rank_temporary)
14725 : : {
14726 : 54 : gfc_error ("CLASS variable %qs at %L must be dummy, allocatable "
14727 : : "or pointer", sym->name, &sym->declared_at);
14728 : 54 : return false;
14729 : : }
14730 : : }
14731 : :
14732 : : return true;
14733 : : }
14734 : :
14735 : :
14736 : : /* Additional checks for symbols with flavor variable and derived
14737 : : type. To be called from resolve_fl_variable. */
14738 : :
14739 : : static bool
14740 : 78770 : resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
14741 : : {
14742 : 78770 : gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
14743 : :
14744 : : /* Check to see if a derived type is blocked from being host
14745 : : associated by the presence of another class I symbol in the same
14746 : : namespace. 14.6.1.3 of the standard and the discussion on
14747 : : comp.lang.fortran. */
14748 : 78770 : if (sym->ts.u.derived
14749 : 78765 : && sym->ns != sym->ts.u.derived->ns
14750 : 45220 : && !sym->ts.u.derived->attr.use_assoc
14751 : 16459 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
14752 : : {
14753 : 15554 : gfc_symbol *s;
14754 : 15554 : gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
14755 : 15554 : if (s && s->attr.generic)
14756 : 2 : s = gfc_find_dt_in_generic (s);
14757 : 15554 : if (s && !gfc_fl_struct (s->attr.flavor))
14758 : : {
14759 : 2 : gfc_error ("The type %qs cannot be host associated at %L "
14760 : : "because it is blocked by an incompatible object "
14761 : : "of the same name declared at %L",
14762 : 2 : sym->ts.u.derived->name, &sym->declared_at,
14763 : : &s->declared_at);
14764 : 2 : return false;
14765 : : }
14766 : : }
14767 : :
14768 : : /* 4th constraint in section 11.3: "If an object of a type for which
14769 : : component-initialization is specified (R429) appears in the
14770 : : specification-part of a module and does not have the ALLOCATABLE
14771 : : or POINTER attribute, the object shall have the SAVE attribute."
14772 : :
14773 : : The check for initializers is performed with
14774 : : gfc_has_default_initializer because gfc_default_initializer generates
14775 : : a hidden default for allocatable components. */
14776 : 78177 : if (!(sym->value || no_init_flag) && sym->ns->proc_name
14777 : 17435 : && sym->ns->proc_name->attr.flavor == FL_MODULE
14778 : 356 : && !(sym->ns->save_all && !sym->attr.automatic) && !sym->attr.save
14779 : 20 : && !sym->attr.pointer && !sym->attr.allocatable
14780 : 20 : && gfc_has_default_initializer (sym->ts.u.derived)
14781 : 78777 : && !gfc_notify_std (GFC_STD_F2008, "Implied SAVE for module variable "
14782 : : "%qs at %L, needed due to the default "
14783 : : "initialization", sym->name, &sym->declared_at))
14784 : : return false;
14785 : :
14786 : : /* Assign default initializer. */
14787 : 78766 : if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
14788 : 72837 : && (!no_init_flag
14789 : 57172 : || (sym->attr.intent == INTENT_OUT
14790 : 3173 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)))
14791 : 18664 : sym->value = gfc_generate_initializer (&sym->ts, can_generate_init (sym));
14792 : :
14793 : : return true;
14794 : : }
14795 : :
14796 : :
14797 : : /* F2008, C402 (R401): A colon shall not be used as a type-param-value
14798 : : except in the declaration of an entity or component that has the POINTER
14799 : : or ALLOCATABLE attribute. */
14800 : :
14801 : : static bool
14802 : 1458854 : deferred_requirements (gfc_symbol *sym)
14803 : : {
14804 : 1458854 : if (sym->ts.deferred
14805 : 7589 : && !(sym->attr.pointer
14806 : 2272 : || sym->attr.allocatable
14807 : 92 : || sym->attr.associate_var
14808 : 7 : || sym->attr.omp_udr_artificial_var))
14809 : : {
14810 : : /* If a function has a result variable, only check the variable. */
14811 : 7 : if (sym->result && sym->name != sym->result->name)
14812 : : return true;
14813 : :
14814 : 6 : gfc_error ("Entity %qs at %L has a deferred type parameter and "
14815 : : "requires either the POINTER or ALLOCATABLE attribute",
14816 : : sym->name, &sym->declared_at);
14817 : 6 : return false;
14818 : : }
14819 : : return true;
14820 : : }
14821 : :
14822 : :
14823 : : /* Resolve symbols with flavor variable. */
14824 : :
14825 : : static bool
14826 : 628983 : resolve_fl_variable (gfc_symbol *sym, int mp_flag)
14827 : : {
14828 : 628983 : const char *auto_save_msg = G_("Automatic object %qs at %L cannot have the "
14829 : : "SAVE attribute");
14830 : :
14831 : 628983 : if (!resolve_fl_var_and_proc (sym, mp_flag))
14832 : : return false;
14833 : :
14834 : : /* Set this flag to check that variables are parameters of all entries.
14835 : : This check is effected by the call to gfc_resolve_expr through
14836 : : is_non_constant_shape_array. */
14837 : 628923 : bool saved_specification_expr = specification_expr;
14838 : 628923 : specification_expr = true;
14839 : :
14840 : 628923 : if (sym->ns->proc_name
14841 : 628836 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
14842 : 624065 : || sym->ns->proc_name->attr.is_main_program)
14843 : 80149 : && !sym->attr.use_assoc
14844 : 77139 : && !sym->attr.allocatable
14845 : 71707 : && !sym->attr.pointer
14846 : 697046 : && is_non_constant_shape_array (sym))
14847 : : {
14848 : : /* F08:C541. The shape of an array defined in a main program or module
14849 : : * needs to be constant. */
14850 : 3 : gfc_error ("The module or main program array %qs at %L must "
14851 : : "have constant shape", sym->name, &sym->declared_at);
14852 : 3 : specification_expr = saved_specification_expr;
14853 : 3 : return false;
14854 : : }
14855 : :
14856 : : /* Constraints on deferred type parameter. */
14857 : 628920 : if (!deferred_requirements (sym))
14858 : : return false;
14859 : :
14860 : 628916 : if (sym->ts.type == BT_CHARACTER && !sym->attr.associate_var)
14861 : : {
14862 : : /* Make sure that character string variables with assumed length are
14863 : : dummy arguments. */
14864 : 35005 : gfc_expr *e = NULL;
14865 : :
14866 : 35005 : if (sym->ts.u.cl)
14867 : 35005 : e = sym->ts.u.cl->length;
14868 : : else
14869 : : return false;
14870 : :
14871 : 35005 : if (e == NULL && !sym->attr.dummy && !sym->attr.result
14872 : 2465 : && !sym->ts.deferred && !sym->attr.select_type_temporary
14873 : 2 : && !sym->attr.omp_udr_artificial_var)
14874 : : {
14875 : 2 : gfc_error ("Entity with assumed character length at %L must be a "
14876 : : "dummy argument or a PARAMETER", &sym->declared_at);
14877 : 2 : specification_expr = saved_specification_expr;
14878 : 2 : return false;
14879 : : }
14880 : :
14881 : 20291 : if (e && sym->attr.save == SAVE_EXPLICIT && !gfc_is_constant_expr (e))
14882 : : {
14883 : 1 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14884 : 1 : specification_expr = saved_specification_expr;
14885 : 1 : return false;
14886 : : }
14887 : :
14888 : 35002 : if (!gfc_is_constant_expr (e)
14889 : 35002 : && !(e->expr_type == EXPR_VARIABLE
14890 : 1388 : && e->symtree->n.sym->attr.flavor == FL_PARAMETER))
14891 : : {
14892 : 2183 : if (!sym->attr.use_assoc && sym->ns->proc_name
14893 : 1679 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
14894 : 1678 : || sym->ns->proc_name->attr.is_main_program))
14895 : : {
14896 : 3 : gfc_error ("%qs at %L must have constant character length "
14897 : : "in this context", sym->name, &sym->declared_at);
14898 : 3 : specification_expr = saved_specification_expr;
14899 : 3 : return false;
14900 : : }
14901 : 2180 : if (sym->attr.in_common)
14902 : : {
14903 : 1 : gfc_error ("COMMON variable %qs at %L must have constant "
14904 : : "character length", sym->name, &sym->declared_at);
14905 : 1 : specification_expr = saved_specification_expr;
14906 : 1 : return false;
14907 : : }
14908 : : }
14909 : : }
14910 : :
14911 : 628909 : if (sym->value == NULL && sym->attr.referenced
14912 : 200691 : && !(sym->as && sym->as->type == AS_ASSUMED_RANK))
14913 : 198820 : apply_default_init_local (sym); /* Try to apply a default initialization. */
14914 : :
14915 : : /* Determine if the symbol may not have an initializer. */
14916 : 628909 : int no_init_flag = 0, automatic_flag = 0;
14917 : 628909 : if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
14918 : 166465 : || sym->attr.intrinsic || sym->attr.result)
14919 : : no_init_flag = 1;
14920 : 134568 : else if ((sym->attr.dimension || sym->attr.codimension) && !sym->attr.pointer
14921 : 168516 : && is_non_constant_shape_array (sym))
14922 : : {
14923 : 1329 : no_init_flag = automatic_flag = 1;
14924 : :
14925 : : /* Also, they must not have the SAVE attribute.
14926 : : SAVE_IMPLICIT is checked below. */
14927 : 1329 : if (sym->as && sym->attr.codimension)
14928 : : {
14929 : 7 : int corank = sym->as->corank;
14930 : 7 : sym->as->corank = 0;
14931 : 7 : no_init_flag = automatic_flag = is_non_constant_shape_array (sym);
14932 : 7 : sym->as->corank = corank;
14933 : : }
14934 : 1329 : if (automatic_flag && sym->attr.save == SAVE_EXPLICIT)
14935 : : {
14936 : 2 : gfc_error (auto_save_msg, sym->name, &sym->declared_at);
14937 : 2 : specification_expr = saved_specification_expr;
14938 : 2 : return false;
14939 : : }
14940 : : }
14941 : :
14942 : : /* Ensure that any initializer is simplified. */
14943 : 628907 : if (sym->value)
14944 : 7470 : gfc_simplify_expr (sym->value, 1);
14945 : :
14946 : : /* Reject illegal initializers. */
14947 : 628907 : if (!sym->mark && sym->value)
14948 : : {
14949 : 7470 : if (sym->attr.allocatable || (sym->ts.type == BT_CLASS
14950 : 67 : && CLASS_DATA (sym)->attr.allocatable))
14951 : 1 : gfc_error ("Allocatable %qs at %L cannot have an initializer",
14952 : : sym->name, &sym->declared_at);
14953 : 7469 : else if (sym->attr.external)
14954 : 0 : gfc_error ("External %qs at %L cannot have an initializer",
14955 : : sym->name, &sym->declared_at);
14956 : 7469 : else if (sym->attr.dummy)
14957 : 3 : gfc_error ("Dummy %qs at %L cannot have an initializer",
14958 : : sym->name, &sym->declared_at);
14959 : 7466 : else if (sym->attr.intrinsic)
14960 : 0 : gfc_error ("Intrinsic %qs at %L cannot have an initializer",
14961 : : sym->name, &sym->declared_at);
14962 : 7466 : else if (sym->attr.result)
14963 : 1 : gfc_error ("Function result %qs at %L cannot have an initializer",
14964 : : sym->name, &sym->declared_at);
14965 : 7465 : else if (automatic_flag)
14966 : 5 : gfc_error ("Automatic array %qs at %L cannot have an initializer",
14967 : : sym->name, &sym->declared_at);
14968 : : else
14969 : 7460 : goto no_init_error;
14970 : 10 : specification_expr = saved_specification_expr;
14971 : 10 : return false;
14972 : : }
14973 : :
14974 : 621437 : no_init_error:
14975 : 628897 : if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
14976 : : {
14977 : 78770 : bool res = resolve_fl_variable_derived (sym, no_init_flag);
14978 : 78770 : specification_expr = saved_specification_expr;
14979 : 78770 : return res;
14980 : : }
14981 : :
14982 : 550127 : specification_expr = saved_specification_expr;
14983 : 550127 : return true;
14984 : : }
14985 : :
14986 : :
14987 : : /* Compare the dummy characteristics of a module procedure interface
14988 : : declaration with the corresponding declaration in a submodule. */
14989 : : static gfc_formal_arglist *new_formal;
14990 : : static char errmsg[200];
14991 : :
14992 : : static void
14993 : 1146 : compare_fsyms (gfc_symbol *sym)
14994 : : {
14995 : 1146 : gfc_symbol *fsym;
14996 : :
14997 : 1146 : if (sym == NULL || new_formal == NULL)
14998 : : return;
14999 : :
15000 : 1146 : fsym = new_formal->sym;
15001 : :
15002 : 1146 : if (sym == fsym)
15003 : : return;
15004 : :
15005 : 1122 : if (strcmp (sym->name, fsym->name) == 0)
15006 : : {
15007 : 421 : if (!gfc_check_dummy_characteristics (fsym, sym, true, errmsg, 200))
15008 : 2 : gfc_error ("%s at %L", errmsg, &fsym->declared_at);
15009 : : }
15010 : : }
15011 : :
15012 : :
15013 : : /* Resolve a procedure. */
15014 : :
15015 : : static bool
15016 : 459689 : resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
15017 : : {
15018 : 459689 : gfc_formal_arglist *arg;
15019 : 459689 : bool allocatable_or_pointer = false;
15020 : :
15021 : 459689 : if (sym->attr.function
15022 : 459689 : && !resolve_fl_var_and_proc (sym, mp_flag))
15023 : : return false;
15024 : :
15025 : : /* Constraints on deferred type parameter. */
15026 : 459679 : if (!deferred_requirements (sym))
15027 : : return false;
15028 : :
15029 : 459678 : if (sym->ts.type == BT_CHARACTER)
15030 : : {
15031 : 11232 : gfc_charlen *cl = sym->ts.u.cl;
15032 : :
15033 : 7196 : if (cl && cl->length && gfc_is_constant_expr (cl->length)
15034 : 12316 : && !resolve_charlen (cl))
15035 : : return false;
15036 : :
15037 : 11231 : if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
15038 : 10148 : && sym->attr.proc == PROC_ST_FUNCTION)
15039 : : {
15040 : 0 : gfc_error ("Character-valued statement function %qs at %L must "
15041 : : "have constant length", sym->name, &sym->declared_at);
15042 : 0 : return false;
15043 : : }
15044 : : }
15045 : :
15046 : : /* Ensure that derived type for are not of a private type. Internal
15047 : : module procedures are excluded by 2.2.3.3 - i.e., they are not
15048 : : externally accessible and can access all the objects accessible in
15049 : : the host. */
15050 : 106761 : if (!(sym->ns->parent && sym->ns->parent->proc_name
15051 : 106761 : && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
15052 : 543569 : && gfc_check_symbol_access (sym))
15053 : : {
15054 : 430126 : gfc_interface *iface;
15055 : :
15056 : 903743 : for (arg = gfc_sym_get_dummy_args (sym); arg; arg = arg->next)
15057 : : {
15058 : 473618 : if (arg->sym
15059 : 473477 : && arg->sym->ts.type == BT_DERIVED
15060 : 41795 : && arg->sym->ts.u.derived
15061 : 41795 : && !arg->sym->ts.u.derived->attr.use_assoc
15062 : 4191 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15063 : 473627 : && !gfc_notify_std (GFC_STD_F2003, "%qs is of a PRIVATE type "
15064 : : "and cannot be a dummy argument"
15065 : : " of %qs, which is PUBLIC at %L",
15066 : 9 : arg->sym->name, sym->name,
15067 : : &sym->declared_at))
15068 : : {
15069 : : /* Stop this message from recurring. */
15070 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15071 : 1 : return false;
15072 : : }
15073 : : }
15074 : :
15075 : : /* PUBLIC interfaces may expose PRIVATE procedures that take types
15076 : : PRIVATE to the containing module. */
15077 : 612450 : for (iface = sym->generic; iface; iface = iface->next)
15078 : : {
15079 : 424157 : for (arg = gfc_sym_get_dummy_args (iface->sym); arg; arg = arg->next)
15080 : : {
15081 : 241832 : if (arg->sym
15082 : 241800 : && arg->sym->ts.type == BT_DERIVED
15083 : 7876 : && !arg->sym->ts.u.derived->attr.use_assoc
15084 : 208 : && !gfc_check_symbol_access (arg->sym->ts.u.derived)
15085 : 241836 : && !gfc_notify_std (GFC_STD_F2003, "Procedure %qs in "
15086 : : "PUBLIC interface %qs at %L "
15087 : : "takes dummy arguments of %qs which "
15088 : : "is PRIVATE", iface->sym->name,
15089 : 4 : sym->name, &iface->sym->declared_at,
15090 : 4 : gfc_typename(&arg->sym->ts)))
15091 : : {
15092 : : /* Stop this message from recurring. */
15093 : 1 : arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
15094 : 1 : return false;
15095 : : }
15096 : : }
15097 : : }
15098 : : }
15099 : :
15100 : 459675 : if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
15101 : 67 : && !sym->attr.proc_pointer)
15102 : : {
15103 : 2 : gfc_error ("Function %qs at %L cannot have an initializer",
15104 : : sym->name, &sym->declared_at);
15105 : :
15106 : : /* Make sure no second error is issued for this. */
15107 : 2 : sym->value->error = 1;
15108 : 2 : return false;
15109 : : }
15110 : :
15111 : : /* An external symbol may not have an initializer because it is taken to be
15112 : : a procedure. Exception: Procedure Pointers. */
15113 : 459673 : if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
15114 : : {
15115 : 0 : gfc_error ("External object %qs at %L may not have an initializer",
15116 : : sym->name, &sym->declared_at);
15117 : 0 : return false;
15118 : : }
15119 : :
15120 : : /* An elemental function is required to return a scalar 12.7.1 */
15121 : 459673 : if (sym->attr.elemental && sym->attr.function
15122 : 86046 : && (sym->as || (sym->ts.type == BT_CLASS && sym->attr.class_ok
15123 : 2 : && CLASS_DATA (sym)->as)))
15124 : : {
15125 : 3 : gfc_error ("ELEMENTAL function %qs at %L must have a scalar "
15126 : : "result", sym->name, &sym->declared_at);
15127 : : /* Reset so that the error only occurs once. */
15128 : 3 : sym->attr.elemental = 0;
15129 : 3 : return false;
15130 : : }
15131 : :
15132 : 459670 : if (sym->attr.proc == PROC_ST_FUNCTION
15133 : 220 : && (sym->attr.allocatable || sym->attr.pointer))
15134 : : {
15135 : 2 : gfc_error ("Statement function %qs at %L may not have pointer or "
15136 : : "allocatable attribute", sym->name, &sym->declared_at);
15137 : 2 : return false;
15138 : : }
15139 : :
15140 : : /* 5.1.1.5 of the Standard: A function name declared with an asterisk
15141 : : char-len-param shall not be array-valued, pointer-valued, recursive
15142 : : or pure. ....snip... A character value of * may only be used in the
15143 : : following ways: (i) Dummy arg of procedure - dummy associates with
15144 : : actual length; (ii) To declare a named constant; or (iii) External
15145 : : function - but length must be declared in calling scoping unit. */
15146 : 459668 : if (sym->attr.function
15147 : 309781 : && sym->ts.type == BT_CHARACTER && !sym->ts.deferred
15148 : 6409 : && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
15149 : : {
15150 : 178 : if ((sym->as && sym->as->rank) || (sym->attr.pointer)
15151 : 176 : || (sym->attr.recursive) || (sym->attr.pure))
15152 : : {
15153 : 4 : if (sym->as && sym->as->rank)
15154 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15155 : : "array-valued", sym->name, &sym->declared_at);
15156 : :
15157 : 4 : if (sym->attr.pointer)
15158 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15159 : : "pointer-valued", sym->name, &sym->declared_at);
15160 : :
15161 : 4 : if (sym->attr.pure)
15162 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15163 : : "pure", sym->name, &sym->declared_at);
15164 : :
15165 : 4 : if (sym->attr.recursive)
15166 : 1 : gfc_error ("CHARACTER(*) function %qs at %L cannot be "
15167 : : "recursive", sym->name, &sym->declared_at);
15168 : :
15169 : 4 : return false;
15170 : : }
15171 : :
15172 : : /* Appendix B.2 of the standard. Contained functions give an
15173 : : error anyway. Deferred character length is an F2003 feature.
15174 : : Don't warn on intrinsic conversion functions, which start
15175 : : with two underscores. */
15176 : 174 : if (!sym->attr.contained && !sym->ts.deferred
15177 : 170 : && (sym->name[0] != '_' || sym->name[1] != '_'))
15178 : 170 : gfc_notify_std (GFC_STD_F95_OBS,
15179 : : "CHARACTER(*) function %qs at %L",
15180 : : sym->name, &sym->declared_at);
15181 : : }
15182 : :
15183 : : /* F2008, C1218. */
15184 : 459664 : if (sym->attr.elemental)
15185 : : {
15186 : 89210 : if (sym->attr.proc_pointer)
15187 : : {
15188 : 7 : const char* name = (sym->attr.result ? sym->ns->proc_name->name
15189 : : : sym->name);
15190 : 7 : gfc_error ("Procedure pointer %qs at %L shall not be elemental",
15191 : : name, &sym->declared_at);
15192 : 7 : return false;
15193 : : }
15194 : 89203 : if (sym->attr.dummy)
15195 : : {
15196 : 3 : gfc_error ("Dummy procedure %qs at %L shall not be elemental",
15197 : : sym->name, &sym->declared_at);
15198 : 3 : return false;
15199 : : }
15200 : : }
15201 : :
15202 : : /* F2018, C15100: "The result of an elemental function shall be scalar,
15203 : : and shall not have the POINTER or ALLOCATABLE attribute." The scalar
15204 : : pointer is tested and caught elsewhere. */
15205 : 459654 : if (sym->result)
15206 : 260182 : allocatable_or_pointer = sym->result->ts.type == BT_CLASS
15207 : 260182 : && CLASS_DATA (sym->result) ?
15208 : 1641 : (CLASS_DATA (sym->result)->attr.allocatable
15209 : 1641 : || CLASS_DATA (sym->result)->attr.pointer) :
15210 : 258541 : (sym->result->attr.allocatable
15211 : 258541 : || sym->result->attr.pointer);
15212 : :
15213 : 459654 : if (sym->attr.elemental && sym->result
15214 : 85672 : && allocatable_or_pointer)
15215 : : {
15216 : 4 : gfc_error ("Function result variable %qs at %L of elemental "
15217 : : "function %qs shall not have an ALLOCATABLE or POINTER "
15218 : : "attribute", sym->result->name,
15219 : : &sym->result->declared_at, sym->name);
15220 : 4 : return false;
15221 : : }
15222 : :
15223 : 459650 : if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
15224 : : {
15225 : 6561 : gfc_formal_arglist *curr_arg;
15226 : 6561 : int has_non_interop_arg = 0;
15227 : :
15228 : 6561 : if (!verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
15229 : 6561 : sym->common_block))
15230 : : {
15231 : : /* Clear these to prevent looking at them again if there was an
15232 : : error. */
15233 : 2 : sym->attr.is_bind_c = 0;
15234 : 2 : sym->attr.is_c_interop = 0;
15235 : 2 : sym->ts.is_c_interop = 0;
15236 : : }
15237 : : else
15238 : : {
15239 : : /* So far, no errors have been found. */
15240 : 6559 : sym->attr.is_c_interop = 1;
15241 : 6559 : sym->ts.is_c_interop = 1;
15242 : : }
15243 : :
15244 : 6561 : curr_arg = gfc_sym_get_dummy_args (sym);
15245 : 29491 : while (curr_arg != NULL)
15246 : : {
15247 : : /* Skip implicitly typed dummy args here. */
15248 : 16369 : if (curr_arg->sym && curr_arg->sym->attr.implicit_type == 0)
15249 : 16313 : if (!gfc_verify_c_interop_param (curr_arg->sym))
15250 : : /* If something is found to fail, record the fact so we
15251 : : can mark the symbol for the procedure as not being
15252 : : BIND(C) to try and prevent multiple errors being
15253 : : reported. */
15254 : 16369 : has_non_interop_arg = 1;
15255 : :
15256 : 16369 : curr_arg = curr_arg->next;
15257 : : }
15258 : :
15259 : : /* See if any of the arguments were not interoperable and if so, clear
15260 : : the procedure symbol to prevent duplicate error messages. */
15261 : 6561 : if (has_non_interop_arg != 0)
15262 : : {
15263 : 128 : sym->attr.is_c_interop = 0;
15264 : 128 : sym->ts.is_c_interop = 0;
15265 : 128 : sym->attr.is_bind_c = 0;
15266 : : }
15267 : : }
15268 : :
15269 : 459650 : if (!sym->attr.proc_pointer)
15270 : : {
15271 : 458608 : if (sym->attr.save == SAVE_EXPLICIT)
15272 : : {
15273 : 5 : gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
15274 : : "in %qs at %L", sym->name, &sym->declared_at);
15275 : 5 : return false;
15276 : : }
15277 : 458603 : if (sym->attr.intent)
15278 : : {
15279 : 1 : gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
15280 : : "in %qs at %L", sym->name, &sym->declared_at);
15281 : 1 : return false;
15282 : : }
15283 : 458602 : if (sym->attr.subroutine && sym->attr.result)
15284 : : {
15285 : 2 : gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
15286 : 2 : "in %qs at %L", sym->ns->proc_name->name, &sym->declared_at);
15287 : 2 : return false;
15288 : : }
15289 : 458600 : if (sym->attr.external && sym->attr.function && !sym->attr.module_procedure
15290 : 132448 : && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
15291 : 132445 : || sym->attr.contained))
15292 : : {
15293 : 3 : gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
15294 : : "in %qs at %L", sym->name, &sym->declared_at);
15295 : 3 : return false;
15296 : : }
15297 : 458597 : if (strcmp ("ppr@", sym->name) == 0)
15298 : : {
15299 : 0 : gfc_error ("Procedure pointer result %qs at %L "
15300 : : "is missing the pointer attribute",
15301 : 0 : sym->ns->proc_name->name, &sym->declared_at);
15302 : 0 : return false;
15303 : : }
15304 : : }
15305 : :
15306 : : /* Assume that a procedure whose body is not known has references
15307 : : to external arrays. */
15308 : 459639 : if (sym->attr.if_source != IFSRC_DECL)
15309 : 316297 : sym->attr.array_outer_dependency = 1;
15310 : :
15311 : : /* Compare the characteristics of a module procedure with the
15312 : : interface declaration. Ideally this would be done with
15313 : : gfc_compare_interfaces but, at present, the formal interface
15314 : : cannot be copied to the ts.interface. */
15315 : 459639 : if (sym->attr.module_procedure
15316 : 1346 : && sym->attr.if_source == IFSRC_DECL)
15317 : : {
15318 : 574 : gfc_symbol *iface;
15319 : 574 : char name[2*GFC_MAX_SYMBOL_LEN + 1];
15320 : 574 : char *module_name;
15321 : 574 : char *submodule_name;
15322 : 574 : strcpy (name, sym->ns->proc_name->name);
15323 : 574 : module_name = strtok (name, ".");
15324 : 574 : submodule_name = strtok (NULL, ".");
15325 : :
15326 : 574 : iface = sym->tlink;
15327 : 574 : sym->tlink = NULL;
15328 : :
15329 : : /* Make sure that the result uses the correct charlen for deferred
15330 : : length results. */
15331 : 574 : if (iface && sym->result
15332 : 140 : && iface->ts.type == BT_CHARACTER
15333 : 19 : && iface->ts.deferred)
15334 : 6 : sym->result->ts.u.cl = iface->ts.u.cl;
15335 : :
15336 : 6 : if (iface == NULL)
15337 : 193 : goto check_formal;
15338 : :
15339 : : /* Check the procedure characteristics. */
15340 : 381 : if (sym->attr.elemental != iface->attr.elemental)
15341 : : {
15342 : 1 : gfc_error ("Mismatch in ELEMENTAL attribute between MODULE "
15343 : : "PROCEDURE at %L and its interface in %s",
15344 : : &sym->declared_at, module_name);
15345 : 10 : return false;
15346 : : }
15347 : :
15348 : 380 : if (sym->attr.pure != iface->attr.pure)
15349 : : {
15350 : 2 : gfc_error ("Mismatch in PURE attribute between MODULE "
15351 : : "PROCEDURE at %L and its interface in %s",
15352 : : &sym->declared_at, module_name);
15353 : 2 : return false;
15354 : : }
15355 : :
15356 : 378 : if (sym->attr.recursive != iface->attr.recursive)
15357 : : {
15358 : 2 : gfc_error ("Mismatch in RECURSIVE attribute between MODULE "
15359 : : "PROCEDURE at %L and its interface in %s",
15360 : : &sym->declared_at, module_name);
15361 : 2 : return false;
15362 : : }
15363 : :
15364 : : /* Check the result characteristics. */
15365 : 376 : if (!gfc_check_result_characteristics (sym, iface, errmsg, 200))
15366 : : {
15367 : 5 : gfc_error ("%s between the MODULE PROCEDURE declaration "
15368 : : "in MODULE %qs and the declaration at %L in "
15369 : : "(SUB)MODULE %qs",
15370 : : errmsg, module_name, &sym->declared_at,
15371 : : submodule_name ? submodule_name : module_name);
15372 : 5 : return false;
15373 : : }
15374 : :
15375 : 371 : check_formal:
15376 : : /* Check the characteristics of the formal arguments. */
15377 : 564 : if (sym->formal && sym->formal_ns)
15378 : : {
15379 : 1084 : for (arg = sym->formal; arg && arg->sym; arg = arg->next)
15380 : : {
15381 : 619 : new_formal = arg;
15382 : 619 : gfc_traverse_ns (sym->formal_ns, compare_fsyms);
15383 : : }
15384 : : }
15385 : : }
15386 : :
15387 : : /* F2018:15.4.2.2 requires an explicit interface for procedures with the
15388 : : BIND(C) attribute. */
15389 : 459629 : if (sym->attr.is_bind_c && sym->attr.if_source == IFSRC_UNKNOWN)
15390 : : {
15391 : 1 : gfc_error ("Interface of %qs at %L must be explicit",
15392 : : sym->name, &sym->declared_at);
15393 : 1 : return false;
15394 : : }
15395 : :
15396 : : return true;
15397 : : }
15398 : :
15399 : :
15400 : : /* Resolve a list of finalizer procedures. That is, after they have hopefully
15401 : : been defined and we now know their defined arguments, check that they fulfill
15402 : : the requirements of the standard for procedures used as finalizers. */
15403 : :
15404 : : static bool
15405 : 106169 : gfc_resolve_finalizers (gfc_symbol* derived, bool *finalizable)
15406 : : {
15407 : 106169 : gfc_finalizer* list;
15408 : 106169 : gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
15409 : 106169 : bool result = true;
15410 : 106169 : bool seen_scalar = false;
15411 : 106169 : gfc_symbol *vtab;
15412 : 106169 : gfc_component *c;
15413 : 106169 : gfc_symbol *parent = gfc_get_derived_super_type (derived);
15414 : :
15415 : 106169 : if (parent)
15416 : 14705 : gfc_resolve_finalizers (parent, finalizable);
15417 : :
15418 : : /* Ensure that derived-type components have a their finalizers resolved. */
15419 : 106169 : bool has_final = derived->f2k_derived && derived->f2k_derived->finalizers;
15420 : 333463 : for (c = derived->components; c; c = c->next)
15421 : 227294 : if (c->ts.type == BT_DERIVED
15422 : 63632 : && !c->attr.pointer && !c->attr.proc_pointer && !c->attr.allocatable)
15423 : : {
15424 : 7785 : bool has_final2 = false;
15425 : 7785 : if (!gfc_resolve_finalizers (c->ts.u.derived, &has_final2))
15426 : 0 : return false; /* Error. */
15427 : 7785 : has_final = has_final || has_final2;
15428 : : }
15429 : : /* Return early if not finalizable. */
15430 : 106169 : if (!has_final)
15431 : : {
15432 : 103905 : if (finalizable)
15433 : 7743 : *finalizable = false;
15434 : 103905 : return true;
15435 : : }
15436 : :
15437 : : /* Walk over the list of finalizer-procedures, check them, and if any one
15438 : : does not fit in with the standard's definition, print an error and remove
15439 : : it from the list. */
15440 : 2264 : prev_link = &derived->f2k_derived->finalizers;
15441 : 4672 : for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
15442 : : {
15443 : 2408 : gfc_formal_arglist *dummy_args;
15444 : 2408 : gfc_symbol* arg;
15445 : 2408 : gfc_finalizer* i;
15446 : 2408 : int my_rank;
15447 : :
15448 : : /* Skip this finalizer if we already resolved it. */
15449 : 2408 : if (list->proc_tree)
15450 : : {
15451 : 1939 : if (list->proc_tree->n.sym->formal->sym->as == NULL
15452 : 566 : || list->proc_tree->n.sym->formal->sym->as->rank == 0)
15453 : 1373 : seen_scalar = true;
15454 : 1939 : prev_link = &(list->next);
15455 : 1939 : continue;
15456 : : }
15457 : :
15458 : : /* Check this exists and is a SUBROUTINE. */
15459 : 469 : if (!list->proc_sym->attr.subroutine)
15460 : : {
15461 : 3 : gfc_error ("FINAL procedure %qs at %L is not a SUBROUTINE",
15462 : : list->proc_sym->name, &list->where);
15463 : 3 : goto error;
15464 : : }
15465 : :
15466 : : /* We should have exactly one argument. */
15467 : 466 : dummy_args = gfc_sym_get_dummy_args (list->proc_sym);
15468 : 466 : if (!dummy_args || dummy_args->next)
15469 : : {
15470 : 2 : gfc_error ("FINAL procedure at %L must have exactly one argument",
15471 : : &list->where);
15472 : 2 : goto error;
15473 : : }
15474 : 464 : arg = dummy_args->sym;
15475 : :
15476 : 464 : if (!arg)
15477 : : {
15478 : 1 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15479 : 1 : &list->proc_sym->declared_at, derived->name);
15480 : 1 : goto error;
15481 : : }
15482 : :
15483 : 463 : if (arg->as && arg->as->type == AS_ASSUMED_RANK
15484 : 6 : && ((list != derived->f2k_derived->finalizers) || list->next))
15485 : : {
15486 : 0 : gfc_error ("FINAL procedure at %L with assumed rank argument must "
15487 : : "be the only finalizer with the same kind/type "
15488 : : "(F2018: C790)", &list->where);
15489 : 0 : goto error;
15490 : : }
15491 : :
15492 : : /* This argument must be of our type. */
15493 : 463 : if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
15494 : : {
15495 : 2 : gfc_error ("Argument of FINAL procedure at %L must be of type %qs",
15496 : : &arg->declared_at, derived->name);
15497 : 2 : goto error;
15498 : : }
15499 : :
15500 : : /* It must neither be a pointer nor allocatable nor optional. */
15501 : 461 : if (arg->attr.pointer)
15502 : : {
15503 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
15504 : : &arg->declared_at);
15505 : 1 : goto error;
15506 : : }
15507 : 460 : if (arg->attr.allocatable)
15508 : : {
15509 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15510 : : " ALLOCATABLE", &arg->declared_at);
15511 : 1 : goto error;
15512 : : }
15513 : 459 : if (arg->attr.optional)
15514 : : {
15515 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
15516 : : &arg->declared_at);
15517 : 1 : goto error;
15518 : : }
15519 : :
15520 : : /* It must not be INTENT(OUT). */
15521 : 458 : if (arg->attr.intent == INTENT_OUT)
15522 : : {
15523 : 1 : gfc_error ("Argument of FINAL procedure at %L must not be"
15524 : : " INTENT(OUT)", &arg->declared_at);
15525 : 1 : goto error;
15526 : : }
15527 : :
15528 : : /* Warn if the procedure is non-scalar and not assumed shape. */
15529 : 457 : if (warn_surprising && arg->as && arg->as->rank != 0
15530 : 3 : && arg->as->type != AS_ASSUMED_SHAPE)
15531 : 2 : gfc_warning (OPT_Wsurprising,
15532 : : "Non-scalar FINAL procedure at %L should have assumed"
15533 : : " shape argument", &arg->declared_at);
15534 : :
15535 : : /* Check that it does not match in kind and rank with a FINAL procedure
15536 : : defined earlier. To really loop over the *earlier* declarations,
15537 : : we need to walk the tail of the list as new ones were pushed at the
15538 : : front. */
15539 : : /* TODO: Handle kind parameters once they are implemented. */
15540 : 457 : my_rank = (arg->as ? arg->as->rank : 0);
15541 : 546 : for (i = list->next; i; i = i->next)
15542 : : {
15543 : 91 : gfc_formal_arglist *dummy_args;
15544 : :
15545 : : /* Argument list might be empty; that is an error signalled earlier,
15546 : : but we nevertheless continued resolving. */
15547 : 91 : dummy_args = gfc_sym_get_dummy_args (i->proc_sym);
15548 : 91 : if (dummy_args)
15549 : : {
15550 : 89 : gfc_symbol* i_arg = dummy_args->sym;
15551 : 89 : const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
15552 : 89 : if (i_rank == my_rank)
15553 : : {
15554 : 2 : gfc_error ("FINAL procedure %qs declared at %L has the same"
15555 : : " rank (%d) as %qs",
15556 : 2 : list->proc_sym->name, &list->where, my_rank,
15557 : 2 : i->proc_sym->name);
15558 : 2 : goto error;
15559 : : }
15560 : : }
15561 : : }
15562 : :
15563 : : /* Is this the/a scalar finalizer procedure? */
15564 : 455 : if (my_rank == 0)
15565 : 335 : seen_scalar = true;
15566 : :
15567 : : /* Find the symtree for this procedure. */
15568 : 455 : gcc_assert (!list->proc_tree);
15569 : 455 : list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
15570 : :
15571 : 455 : prev_link = &list->next;
15572 : 455 : continue;
15573 : :
15574 : : /* Remove wrong nodes immediately from the list so we don't risk any
15575 : : troubles in the future when they might fail later expectations. */
15576 : 14 : error:
15577 : 14 : i = list;
15578 : 14 : *prev_link = list->next;
15579 : 14 : gfc_free_finalizer (i);
15580 : 14 : result = false;
15581 : 455 : }
15582 : :
15583 : 2264 : if (result == false)
15584 : : return false;
15585 : :
15586 : : /* Warn if we haven't seen a scalar finalizer procedure (but we know there
15587 : : were nodes in the list, must have been for arrays. It is surely a good
15588 : : idea to have a scalar version there if there's something to finalize. */
15589 : 2260 : if (warn_surprising && derived->f2k_derived->finalizers && !seen_scalar)
15590 : 1 : gfc_warning (OPT_Wsurprising,
15591 : : "Only array FINAL procedures declared for derived type %qs"
15592 : : " defined at %L, suggest also scalar one unless an assumed"
15593 : : " rank finalizer has been declared",
15594 : : derived->name, &derived->declared_at);
15595 : :
15596 : 2260 : vtab = gfc_find_derived_vtab (derived);
15597 : 2260 : c = vtab->ts.u.derived->components->next->next->next->next->next;
15598 : 2260 : gfc_set_sym_referenced (c->initializer->symtree->n.sym);
15599 : :
15600 : 2260 : if (finalizable)
15601 : 590 : *finalizable = true;
15602 : :
15603 : : return true;
15604 : : }
15605 : :
15606 : :
15607 : : /* Check if two GENERIC targets are ambiguous and emit an error is they are. */
15608 : :
15609 : : static bool
15610 : 380 : check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
15611 : : const char* generic_name, locus where)
15612 : : {
15613 : 380 : gfc_symbol *sym1, *sym2;
15614 : 380 : const char *pass1, *pass2;
15615 : 380 : gfc_formal_arglist *dummy_args;
15616 : :
15617 : 380 : gcc_assert (t1->specific && t2->specific);
15618 : 380 : gcc_assert (!t1->specific->is_generic);
15619 : 380 : gcc_assert (!t2->specific->is_generic);
15620 : 380 : gcc_assert (t1->is_operator == t2->is_operator);
15621 : :
15622 : 380 : sym1 = t1->specific->u.specific->n.sym;
15623 : 380 : sym2 = t2->specific->u.specific->n.sym;
15624 : :
15625 : 380 : if (sym1 == sym2)
15626 : : return true;
15627 : :
15628 : : /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
15629 : 380 : if (sym1->attr.subroutine != sym2->attr.subroutine
15630 : 378 : || sym1->attr.function != sym2->attr.function)
15631 : : {
15632 : 2 : gfc_error ("%qs and %qs cannot be mixed FUNCTION/SUBROUTINE for"
15633 : : " GENERIC %qs at %L",
15634 : : sym1->name, sym2->name, generic_name, &where);
15635 : 2 : return false;
15636 : : }
15637 : :
15638 : : /* Determine PASS arguments. */
15639 : 378 : if (t1->specific->nopass)
15640 : : pass1 = NULL;
15641 : 327 : else if (t1->specific->pass_arg)
15642 : : pass1 = t1->specific->pass_arg;
15643 : : else
15644 : : {
15645 : 212 : dummy_args = gfc_sym_get_dummy_args (t1->specific->u.specific->n.sym);
15646 : 212 : if (dummy_args)
15647 : 211 : pass1 = dummy_args->sym->name;
15648 : : else
15649 : : pass1 = NULL;
15650 : : }
15651 : 378 : if (t2->specific->nopass)
15652 : : pass2 = NULL;
15653 : 326 : else if (t2->specific->pass_arg)
15654 : : pass2 = t2->specific->pass_arg;
15655 : : else
15656 : : {
15657 : 207 : dummy_args = gfc_sym_get_dummy_args (t2->specific->u.specific->n.sym);
15658 : 207 : if (dummy_args)
15659 : 206 : pass2 = dummy_args->sym->name;
15660 : : else
15661 : : pass2 = NULL;
15662 : : }
15663 : :
15664 : : /* Compare the interfaces. */
15665 : 378 : if (gfc_compare_interfaces (sym1, sym2, sym2->name, !t1->is_operator, 0,
15666 : : NULL, 0, pass1, pass2))
15667 : : {
15668 : 8 : gfc_error ("%qs and %qs for GENERIC %qs at %L are ambiguous",
15669 : : sym1->name, sym2->name, generic_name, &where);
15670 : 8 : return false;
15671 : : }
15672 : :
15673 : : return true;
15674 : : }
15675 : :
15676 : :
15677 : : /* Worker function for resolving a generic procedure binding; this is used to
15678 : : resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
15679 : :
15680 : : The difference between those cases is finding possible inherited bindings
15681 : : that are overridden, as one has to look for them in tb_sym_root,
15682 : : tb_uop_root or tb_op, respectively. Thus the caller must already find
15683 : : the super-type and set p->overridden correctly. */
15684 : :
15685 : : static bool
15686 : 2045 : resolve_tb_generic_targets (gfc_symbol* super_type,
15687 : : gfc_typebound_proc* p, const char* name)
15688 : : {
15689 : 2045 : gfc_tbp_generic* target;
15690 : 2045 : gfc_symtree* first_target;
15691 : 2045 : gfc_symtree* inherited;
15692 : :
15693 : 2045 : gcc_assert (p && p->is_generic);
15694 : :
15695 : : /* Try to find the specific bindings for the symtrees in our target-list. */
15696 : 2045 : gcc_assert (p->u.generic);
15697 : 4334 : for (target = p->u.generic; target; target = target->next)
15698 : 2306 : if (!target->specific)
15699 : : {
15700 : 2168 : gfc_typebound_proc* overridden_tbp;
15701 : 2168 : gfc_tbp_generic* g;
15702 : 2168 : const char* target_name;
15703 : :
15704 : 2168 : target_name = target->specific_st->name;
15705 : :
15706 : : /* Defined for this type directly. */
15707 : 2168 : if (target->specific_st->n.tb && !target->specific_st->n.tb->error)
15708 : : {
15709 : 2159 : target->specific = target->specific_st->n.tb;
15710 : 2159 : goto specific_found;
15711 : : }
15712 : :
15713 : : /* Look for an inherited specific binding. */
15714 : 9 : if (super_type)
15715 : : {
15716 : 5 : inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
15717 : : true, NULL);
15718 : :
15719 : 5 : if (inherited)
15720 : : {
15721 : 5 : gcc_assert (inherited->n.tb);
15722 : 5 : target->specific = inherited->n.tb;
15723 : 5 : goto specific_found;
15724 : : }
15725 : : }
15726 : :
15727 : 4 : gfc_error ("Undefined specific binding %qs as target of GENERIC %qs"
15728 : : " at %L", target_name, name, &p->where);
15729 : 4 : return false;
15730 : :
15731 : : /* Once we've found the specific binding, check it is not ambiguous with
15732 : : other specifics already found or inherited for the same GENERIC. */
15733 : 2164 : specific_found:
15734 : 2164 : gcc_assert (target->specific);
15735 : :
15736 : : /* This must really be a specific binding! */
15737 : 2164 : if (target->specific->is_generic)
15738 : : {
15739 : 3 : gfc_error ("GENERIC %qs at %L must target a specific binding,"
15740 : : " %qs is GENERIC, too", name, &p->where, target_name);
15741 : 3 : return false;
15742 : : }
15743 : :
15744 : : /* Check those already resolved on this type directly. */
15745 : 5032 : for (g = p->u.generic; g; g = g->next)
15746 : 724 : if (g != target && g->specific
15747 : 3240 : && !check_generic_tbp_ambiguity (target, g, name, p->where))
15748 : : return false;
15749 : :
15750 : : /* Check for ambiguity with inherited specific targets. */
15751 : 2170 : for (overridden_tbp = p->overridden; overridden_tbp;
15752 : 16 : overridden_tbp = overridden_tbp->overridden)
15753 : 19 : if (overridden_tbp->is_generic)
15754 : : {
15755 : 33 : for (g = overridden_tbp->u.generic; g; g = g->next)
15756 : : {
15757 : 18 : gcc_assert (g->specific);
15758 : 18 : if (!check_generic_tbp_ambiguity (target, g, name, p->where))
15759 : : return false;
15760 : : }
15761 : : }
15762 : : }
15763 : :
15764 : : /* If we attempt to "overwrite" a specific binding, this is an error. */
15765 : 2028 : if (p->overridden && !p->overridden->is_generic)
15766 : : {
15767 : 1 : gfc_error ("GENERIC %qs at %L cannot overwrite specific binding with"
15768 : : " the same name", name, &p->where);
15769 : 1 : return false;
15770 : : }
15771 : :
15772 : : /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
15773 : : all must have the same attributes here. */
15774 : 2027 : first_target = p->u.generic->specific->u.specific;
15775 : 2027 : gcc_assert (first_target);
15776 : 2027 : p->subroutine = first_target->n.sym->attr.subroutine;
15777 : 2027 : p->function = first_target->n.sym->attr.function;
15778 : :
15779 : 2027 : return true;
15780 : : }
15781 : :
15782 : :
15783 : : /* Resolve a GENERIC procedure binding for a derived type. */
15784 : :
15785 : : static bool
15786 : 1045 : resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
15787 : : {
15788 : 1045 : gfc_symbol* super_type;
15789 : :
15790 : : /* Find the overridden binding if any. */
15791 : 1045 : st->n.tb->overridden = NULL;
15792 : 1045 : super_type = gfc_get_derived_super_type (derived);
15793 : 1045 : if (super_type)
15794 : : {
15795 : 40 : gfc_symtree* overridden;
15796 : 40 : overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
15797 : : true, NULL);
15798 : :
15799 : 40 : if (overridden && overridden->n.tb)
15800 : 21 : st->n.tb->overridden = overridden->n.tb;
15801 : : }
15802 : :
15803 : : /* Resolve using worker function. */
15804 : 1045 : return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
15805 : : }
15806 : :
15807 : :
15808 : : /* Retrieve the target-procedure of an operator binding and do some checks in
15809 : : common for intrinsic and user-defined type-bound operators. */
15810 : :
15811 : : static gfc_symbol*
15812 : 1064 : get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
15813 : : {
15814 : 1064 : gfc_symbol* target_proc;
15815 : :
15816 : 1064 : gcc_assert (target->specific && !target->specific->is_generic);
15817 : 1064 : target_proc = target->specific->u.specific->n.sym;
15818 : 1064 : gcc_assert (target_proc);
15819 : :
15820 : : /* F08:C468. All operator bindings must have a passed-object dummy argument. */
15821 : 1064 : if (target->specific->nopass)
15822 : : {
15823 : 2 : gfc_error ("Type-bound operator at %L cannot be NOPASS", &where);
15824 : 2 : return NULL;
15825 : : }
15826 : :
15827 : : return target_proc;
15828 : : }
15829 : :
15830 : :
15831 : : /* Resolve a type-bound intrinsic operator. */
15832 : :
15833 : : static bool
15834 : 958 : resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
15835 : : gfc_typebound_proc* p)
15836 : : {
15837 : 958 : gfc_symbol* super_type;
15838 : 958 : gfc_tbp_generic* target;
15839 : :
15840 : : /* If there's already an error here, do nothing (but don't fail again). */
15841 : 958 : if (p->error)
15842 : : return true;
15843 : :
15844 : : /* Operators should always be GENERIC bindings. */
15845 : 958 : gcc_assert (p->is_generic);
15846 : :
15847 : : /* Look for an overridden binding. */
15848 : 958 : super_type = gfc_get_derived_super_type (derived);
15849 : 958 : if (super_type && super_type->f2k_derived)
15850 : 1 : p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
15851 : : op, true, NULL);
15852 : : else
15853 : 957 : p->overridden = NULL;
15854 : :
15855 : : /* Resolve general GENERIC properties using worker function. */
15856 : 958 : if (!resolve_tb_generic_targets (super_type, p, gfc_op2string(op)))
15857 : 1 : goto error;
15858 : :
15859 : : /* Check the targets to be procedures of correct interface. */
15860 : 1958 : for (target = p->u.generic; target; target = target->next)
15861 : : {
15862 : 1021 : gfc_symbol* target_proc;
15863 : :
15864 : 1021 : target_proc = get_checked_tb_operator_target (target, p->where);
15865 : 1021 : if (!target_proc)
15866 : 1 : goto error;
15867 : :
15868 : 1020 : if (!gfc_check_operator_interface (target_proc, op, p->where))
15869 : 3 : goto error;
15870 : :
15871 : : /* Add target to non-typebound operator list. */
15872 : 1017 : if (!target->specific->deferred && !derived->attr.use_assoc
15873 : 358 : && p->access != ACCESS_PRIVATE && derived->ns == gfc_current_ns)
15874 : : {
15875 : 356 : gfc_interface *head, *intr;
15876 : :
15877 : : /* Preempt 'gfc_check_new_interface' for submodules, where the
15878 : : mechanism for handling module procedures winds up resolving
15879 : : operator interfaces twice and would otherwise cause an error. */
15880 : 424 : for (intr = derived->ns->op[op]; intr; intr = intr->next)
15881 : 82 : if (intr->sym == target_proc
15882 : 16 : && target_proc->attr.used_in_submodule)
15883 : : return true;
15884 : :
15885 : 342 : if (!gfc_check_new_interface (derived->ns->op[op],
15886 : : target_proc, p->where))
15887 : : return false;
15888 : 340 : head = derived->ns->op[op];
15889 : 340 : intr = gfc_get_interface ();
15890 : 340 : intr->sym = target_proc;
15891 : 340 : intr->where = p->where;
15892 : 340 : intr->next = head;
15893 : 340 : derived->ns->op[op] = intr;
15894 : : }
15895 : : }
15896 : :
15897 : : return true;
15898 : :
15899 : 5 : error:
15900 : 5 : p->error = 1;
15901 : 5 : return false;
15902 : : }
15903 : :
15904 : :
15905 : : /* Resolve a type-bound user operator (tree-walker callback). */
15906 : :
15907 : : static gfc_symbol* resolve_bindings_derived;
15908 : : static bool resolve_bindings_result;
15909 : :
15910 : : static bool check_uop_procedure (gfc_symbol* sym, locus where);
15911 : :
15912 : : static void
15913 : 42 : resolve_typebound_user_op (gfc_symtree* stree)
15914 : : {
15915 : 42 : gfc_symbol* super_type;
15916 : 42 : gfc_tbp_generic* target;
15917 : :
15918 : 42 : gcc_assert (stree && stree->n.tb);
15919 : :
15920 : 42 : if (stree->n.tb->error)
15921 : : return;
15922 : :
15923 : : /* Operators should always be GENERIC bindings. */
15924 : 42 : gcc_assert (stree->n.tb->is_generic);
15925 : :
15926 : : /* Find overridden procedure, if any. */
15927 : 42 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
15928 : 42 : if (super_type && super_type->f2k_derived)
15929 : : {
15930 : 0 : gfc_symtree* overridden;
15931 : 0 : overridden = gfc_find_typebound_user_op (super_type, NULL,
15932 : : stree->name, true, NULL);
15933 : :
15934 : 0 : if (overridden && overridden->n.tb)
15935 : 0 : stree->n.tb->overridden = overridden->n.tb;
15936 : : }
15937 : : else
15938 : 42 : stree->n.tb->overridden = NULL;
15939 : :
15940 : : /* Resolve basically using worker function. */
15941 : 42 : if (!resolve_tb_generic_targets (super_type, stree->n.tb, stree->name))
15942 : 0 : goto error;
15943 : :
15944 : : /* Check the targets to be functions of correct interface. */
15945 : 82 : for (target = stree->n.tb->u.generic; target; target = target->next)
15946 : : {
15947 : 43 : gfc_symbol* target_proc;
15948 : :
15949 : 43 : target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
15950 : 43 : if (!target_proc)
15951 : 1 : goto error;
15952 : :
15953 : 42 : if (!check_uop_procedure (target_proc, stree->n.tb->where))
15954 : 2 : goto error;
15955 : : }
15956 : :
15957 : : return;
15958 : :
15959 : 3 : error:
15960 : 3 : resolve_bindings_result = false;
15961 : 3 : stree->n.tb->error = 1;
15962 : : }
15963 : :
15964 : :
15965 : : /* Resolve the type-bound procedures for a derived type. */
15966 : :
15967 : : static void
15968 : 9086 : resolve_typebound_procedure (gfc_symtree* stree)
15969 : : {
15970 : 9086 : gfc_symbol* proc;
15971 : 9086 : locus where;
15972 : 9086 : gfc_symbol* me_arg;
15973 : 9086 : gfc_symbol* super_type;
15974 : 9086 : gfc_component* comp;
15975 : :
15976 : 9086 : gcc_assert (stree);
15977 : :
15978 : : /* Undefined specific symbol from GENERIC target definition. */
15979 : 9086 : if (!stree->n.tb)
15980 : 9004 : return;
15981 : :
15982 : 9080 : if (stree->n.tb->error)
15983 : : return;
15984 : :
15985 : : /* If this is a GENERIC binding, use that routine. */
15986 : 9064 : if (stree->n.tb->is_generic)
15987 : : {
15988 : 1045 : if (!resolve_typebound_generic (resolve_bindings_derived, stree))
15989 : 17 : goto error;
15990 : : return;
15991 : : }
15992 : :
15993 : : /* Get the target-procedure to check it. */
15994 : 8019 : gcc_assert (!stree->n.tb->is_generic);
15995 : 8019 : gcc_assert (stree->n.tb->u.specific);
15996 : 8019 : proc = stree->n.tb->u.specific->n.sym;
15997 : 8019 : where = stree->n.tb->where;
15998 : :
15999 : : /* Default access should already be resolved from the parser. */
16000 : 8019 : gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
16001 : :
16002 : 8019 : if (stree->n.tb->deferred)
16003 : : {
16004 : 671 : if (!check_proc_interface (proc, &where))
16005 : 5 : goto error;
16006 : : }
16007 : : else
16008 : : {
16009 : : /* If proc has not been resolved at this point, proc->name may
16010 : : actually be a USE associated entity. See PR fortran/89647. */
16011 : 7348 : if (!proc->resolve_symbol_called
16012 : 4986 : && proc->attr.function == 0 && proc->attr.subroutine == 0)
16013 : : {
16014 : 11 : gfc_symbol *tmp;
16015 : 11 : gfc_find_symbol (proc->name, gfc_current_ns->parent, 1, &tmp);
16016 : 11 : if (tmp && tmp->attr.use_assoc)
16017 : : {
16018 : 1 : proc->module = tmp->module;
16019 : 1 : proc->attr.proc = tmp->attr.proc;
16020 : 1 : proc->attr.function = tmp->attr.function;
16021 : 1 : proc->attr.subroutine = tmp->attr.subroutine;
16022 : 1 : proc->attr.use_assoc = tmp->attr.use_assoc;
16023 : 1 : proc->ts = tmp->ts;
16024 : 1 : proc->result = tmp->result;
16025 : : }
16026 : : }
16027 : :
16028 : : /* Check for F08:C465. */
16029 : 7348 : if ((!proc->attr.subroutine && !proc->attr.function)
16030 : 7338 : || (proc->attr.proc != PROC_MODULE
16031 : 21 : && proc->attr.if_source != IFSRC_IFBODY
16032 : 7 : && !proc->attr.module_procedure)
16033 : 7337 : || proc->attr.abstract)
16034 : : {
16035 : 12 : gfc_error ("%qs must be a module procedure or an external "
16036 : : "procedure with an explicit interface at %L",
16037 : : proc->name, &where);
16038 : 12 : goto error;
16039 : : }
16040 : : }
16041 : :
16042 : 8002 : stree->n.tb->subroutine = proc->attr.subroutine;
16043 : 8002 : stree->n.tb->function = proc->attr.function;
16044 : :
16045 : : /* Find the super-type of the current derived type. We could do this once and
16046 : : store in a global if speed is needed, but as long as not I believe this is
16047 : : more readable and clearer. */
16048 : 8002 : super_type = gfc_get_derived_super_type (resolve_bindings_derived);
16049 : :
16050 : : /* If PASS, resolve and check arguments if not already resolved / loaded
16051 : : from a .mod file. */
16052 : 8002 : if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
16053 : : {
16054 : 2656 : gfc_formal_arglist *dummy_args;
16055 : :
16056 : 2656 : dummy_args = gfc_sym_get_dummy_args (proc);
16057 : 2656 : if (stree->n.tb->pass_arg)
16058 : : {
16059 : 453 : gfc_formal_arglist *i;
16060 : :
16061 : : /* If an explicit passing argument name is given, walk the arg-list
16062 : : and look for it. */
16063 : :
16064 : 453 : me_arg = NULL;
16065 : 453 : stree->n.tb->pass_arg_num = 1;
16066 : 573 : for (i = dummy_args; i; i = i->next)
16067 : : {
16068 : 571 : if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
16069 : : {
16070 : : me_arg = i->sym;
16071 : : break;
16072 : : }
16073 : 120 : ++stree->n.tb->pass_arg_num;
16074 : : }
16075 : :
16076 : 453 : if (!me_arg)
16077 : : {
16078 : 2 : gfc_error ("Procedure %qs with PASS(%s) at %L has no"
16079 : : " argument %qs",
16080 : : proc->name, stree->n.tb->pass_arg, &where,
16081 : : stree->n.tb->pass_arg);
16082 : 2 : goto error;
16083 : : }
16084 : : }
16085 : : else
16086 : : {
16087 : : /* Otherwise, take the first one; there should in fact be at least
16088 : : one. */
16089 : 2203 : stree->n.tb->pass_arg_num = 1;
16090 : 2203 : if (!dummy_args)
16091 : : {
16092 : 2 : gfc_error ("Procedure %qs with PASS at %L must have at"
16093 : : " least one argument", proc->name, &where);
16094 : 2 : goto error;
16095 : : }
16096 : 2201 : me_arg = dummy_args->sym;
16097 : : }
16098 : :
16099 : : /* Now check that the argument-type matches and the passed-object
16100 : : dummy argument is generally fine. */
16101 : :
16102 : 2201 : gcc_assert (me_arg);
16103 : :
16104 : 2652 : if (me_arg->ts.type != BT_CLASS)
16105 : : {
16106 : 5 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16107 : : " at %L", proc->name, &where);
16108 : 5 : goto error;
16109 : : }
16110 : :
16111 : : /* The derived type is not a PDT template. Resolve as usual. */
16112 : 2647 : if (!resolve_bindings_derived->attr.pdt_template
16113 : 2632 : && (CLASS_DATA (me_arg)->ts.u.derived != resolve_bindings_derived))
16114 : : {
16115 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16116 : : "the derived-type %qs", me_arg->name, proc->name,
16117 : : me_arg->name, &where, resolve_bindings_derived->name);
16118 : 0 : goto error;
16119 : : }
16120 : :
16121 : 2647 : if (resolve_bindings_derived->attr.pdt_template
16122 : 2662 : && !gfc_pdt_is_instance_of (resolve_bindings_derived,
16123 : 15 : CLASS_DATA (me_arg)->ts.u.derived))
16124 : : {
16125 : 0 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of "
16126 : : "the parametric derived-type %qs", me_arg->name,
16127 : : proc->name, me_arg->name, &where,
16128 : : resolve_bindings_derived->name);
16129 : 0 : goto error;
16130 : : }
16131 : :
16132 : 2647 : if (resolve_bindings_derived->attr.pdt_template
16133 : 15 : && gfc_pdt_is_instance_of (resolve_bindings_derived,
16134 : 15 : CLASS_DATA (me_arg)->ts.u.derived)
16135 : 15 : && (me_arg->param_list != NULL)
16136 : 2662 : && (gfc_spec_list_type (me_arg->param_list,
16137 : 15 : CLASS_DATA(me_arg)->ts.u.derived)
16138 : : != SPEC_ASSUMED))
16139 : : {
16140 : :
16141 : : /* Add a check to verify if there are any LEN parameters in the
16142 : : first place. If there are LEN parameters, throw this error.
16143 : : If there are only KIND parameters, then don't trigger
16144 : : this error. */
16145 : 7 : gfc_component *c;
16146 : 7 : bool seen_len_param = false;
16147 : 7 : gfc_actual_arglist *me_arg_param = me_arg->param_list;
16148 : :
16149 : 8 : for (; me_arg_param; me_arg_param = me_arg_param->next)
16150 : : {
16151 : 7 : c = gfc_find_component (CLASS_DATA(me_arg)->ts.u.derived,
16152 : : me_arg_param->name, true, true, NULL);
16153 : :
16154 : 7 : gcc_assert (c != NULL);
16155 : :
16156 : 7 : if (c->attr.pdt_kind)
16157 : 1 : continue;
16158 : :
16159 : : /* Getting here implies that there is a pdt_len parameter
16160 : : in the list. */
16161 : : seen_len_param = true;
16162 : : break;
16163 : : }
16164 : :
16165 : 7 : if (seen_len_param)
16166 : : {
16167 : 6 : gfc_error ("All LEN type parameters of the passed dummy "
16168 : : "argument %qs of %qs at %L must be ASSUMED.",
16169 : : me_arg->name, proc->name, &where);
16170 : 6 : goto error;
16171 : : }
16172 : : }
16173 : :
16174 : 2641 : gcc_assert (me_arg->ts.type == BT_CLASS);
16175 : 2641 : if (CLASS_DATA (me_arg)->as && CLASS_DATA (me_arg)->as->rank != 0)
16176 : : {
16177 : 1 : gfc_error ("Passed-object dummy argument of %qs at %L must be"
16178 : : " scalar", proc->name, &where);
16179 : 1 : goto error;
16180 : : }
16181 : 2640 : if (CLASS_DATA (me_arg)->attr.allocatable)
16182 : : {
16183 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16184 : : " be ALLOCATABLE", proc->name, &where);
16185 : 2 : goto error;
16186 : : }
16187 : 2638 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16188 : : {
16189 : 2 : gfc_error ("Passed-object dummy argument of %qs at %L must not"
16190 : : " be POINTER", proc->name, &where);
16191 : 2 : goto error;
16192 : : }
16193 : : }
16194 : :
16195 : : /* If we are extending some type, check that we don't override a procedure
16196 : : flagged NON_OVERRIDABLE. */
16197 : 7982 : stree->n.tb->overridden = NULL;
16198 : 7982 : if (super_type)
16199 : : {
16200 : 1480 : gfc_symtree* overridden;
16201 : 1480 : overridden = gfc_find_typebound_proc (super_type, NULL,
16202 : : stree->name, true, NULL);
16203 : :
16204 : 1480 : if (overridden)
16205 : : {
16206 : 1210 : if (overridden->n.tb)
16207 : 1210 : stree->n.tb->overridden = overridden->n.tb;
16208 : :
16209 : 1210 : if (!gfc_check_typebound_override (stree, overridden))
16210 : 26 : goto error;
16211 : : }
16212 : : }
16213 : :
16214 : : /* See if there's a name collision with a component directly in this type. */
16215 : 18987 : for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
16216 : 11032 : if (!strcmp (comp->name, stree->name))
16217 : : {
16218 : 1 : gfc_error ("Procedure %qs at %L has the same name as a component of"
16219 : : " %qs",
16220 : : stree->name, &where, resolve_bindings_derived->name);
16221 : 1 : goto error;
16222 : : }
16223 : :
16224 : : /* Try to find a name collision with an inherited component. */
16225 : 7955 : if (super_type && gfc_find_component (super_type, stree->name, true, true,
16226 : : NULL))
16227 : : {
16228 : 1 : gfc_error ("Procedure %qs at %L has the same name as an inherited"
16229 : : " component of %qs",
16230 : : stree->name, &where, resolve_bindings_derived->name);
16231 : 1 : goto error;
16232 : : }
16233 : :
16234 : 7954 : stree->n.tb->error = 0;
16235 : 7954 : return;
16236 : :
16237 : 82 : error:
16238 : 82 : resolve_bindings_result = false;
16239 : 82 : stree->n.tb->error = 1;
16240 : : }
16241 : :
16242 : :
16243 : : static bool
16244 : 81657 : resolve_typebound_procedures (gfc_symbol* derived)
16245 : : {
16246 : 81657 : int op;
16247 : 81657 : gfc_symbol* super_type;
16248 : :
16249 : 81657 : if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
16250 : : return true;
16251 : :
16252 : 4454 : super_type = gfc_get_derived_super_type (derived);
16253 : 4454 : if (super_type)
16254 : 847 : resolve_symbol (super_type);
16255 : :
16256 : 4454 : resolve_bindings_derived = derived;
16257 : 4454 : resolve_bindings_result = true;
16258 : :
16259 : 4454 : if (derived->f2k_derived->tb_sym_root)
16260 : 4454 : gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
16261 : : &resolve_typebound_procedure);
16262 : :
16263 : 4454 : if (derived->f2k_derived->tb_uop_root)
16264 : 38 : gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
16265 : : &resolve_typebound_user_op);
16266 : :
16267 : 129166 : for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
16268 : : {
16269 : 124712 : gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
16270 : 124712 : if (p && !resolve_typebound_intrinsic_op (derived,
16271 : : (gfc_intrinsic_op)op, p))
16272 : 7 : resolve_bindings_result = false;
16273 : : }
16274 : :
16275 : 4454 : return resolve_bindings_result;
16276 : : }
16277 : :
16278 : :
16279 : : /* Add a derived type to the dt_list. The dt_list is used in trans-types.cc
16280 : : to give all identical derived types the same backend_decl. */
16281 : : static void
16282 : 167610 : add_dt_to_dt_list (gfc_symbol *derived)
16283 : : {
16284 : 167610 : if (!derived->dt_next)
16285 : : {
16286 : 77529 : if (gfc_derived_types)
16287 : : {
16288 : 63666 : derived->dt_next = gfc_derived_types->dt_next;
16289 : 63666 : gfc_derived_types->dt_next = derived;
16290 : : }
16291 : : else
16292 : : {
16293 : 13863 : derived->dt_next = derived;
16294 : : }
16295 : 77529 : gfc_derived_types = derived;
16296 : : }
16297 : 167610 : }
16298 : :
16299 : :
16300 : : /* Ensure that a derived-type is really not abstract, meaning that every
16301 : : inherited DEFERRED binding is overridden by a non-DEFERRED one. */
16302 : :
16303 : : static bool
16304 : 6924 : ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
16305 : : {
16306 : 6924 : if (!st)
16307 : : return true;
16308 : :
16309 : 2712 : if (!ensure_not_abstract_walker (sub, st->left))
16310 : : return false;
16311 : 2712 : if (!ensure_not_abstract_walker (sub, st->right))
16312 : : return false;
16313 : :
16314 : 2711 : if (st->n.tb && st->n.tb->deferred)
16315 : : {
16316 : 1959 : gfc_symtree* overriding;
16317 : 1959 : overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
16318 : 1959 : if (!overriding)
16319 : : return false;
16320 : 1958 : gcc_assert (overriding->n.tb);
16321 : 1958 : if (overriding->n.tb->deferred)
16322 : : {
16323 : 5 : gfc_error ("Derived-type %qs declared at %L must be ABSTRACT because"
16324 : : " %qs is DEFERRED and not overridden",
16325 : : sub->name, &sub->declared_at, st->name);
16326 : 5 : return false;
16327 : : }
16328 : : }
16329 : :
16330 : : return true;
16331 : : }
16332 : :
16333 : : static bool
16334 : 1352 : ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
16335 : : {
16336 : : /* The algorithm used here is to recursively travel up the ancestry of sub
16337 : : and for each ancestor-type, check all bindings. If any of them is
16338 : : DEFERRED, look it up starting from sub and see if the found (overriding)
16339 : : binding is not DEFERRED.
16340 : : This is not the most efficient way to do this, but it should be ok and is
16341 : : clearer than something sophisticated. */
16342 : :
16343 : 1501 : gcc_assert (ancestor && !sub->attr.abstract);
16344 : :
16345 : 1501 : if (!ancestor->attr.abstract)
16346 : : return true;
16347 : :
16348 : : /* Walk bindings of this ancestor. */
16349 : 1500 : if (ancestor->f2k_derived)
16350 : : {
16351 : 1500 : bool t;
16352 : 1500 : t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
16353 : 1500 : if (!t)
16354 : : return false;
16355 : : }
16356 : :
16357 : : /* Find next ancestor type and recurse on it. */
16358 : 1494 : ancestor = gfc_get_derived_super_type (ancestor);
16359 : 1494 : if (ancestor)
16360 : : return ensure_not_abstract (sub, ancestor);
16361 : :
16362 : : return true;
16363 : : }
16364 : :
16365 : :
16366 : : /* This check for typebound defined assignments is done recursively
16367 : : since the order in which derived types are resolved is not always in
16368 : : order of the declarations. */
16369 : :
16370 : : static void
16371 : 171540 : check_defined_assignments (gfc_symbol *derived)
16372 : : {
16373 : 171540 : gfc_component *c;
16374 : :
16375 : 572479 : for (c = derived->components; c; c = c->next)
16376 : : {
16377 : 402491 : if (!gfc_bt_struct (c->ts.type)
16378 : 96710 : || c->attr.pointer
16379 : 18378 : || c->attr.proc_pointer_comp
16380 : 18378 : || c->attr.class_pointer
16381 : 18372 : || c->attr.proc_pointer)
16382 : 384512 : continue;
16383 : :
16384 : 17979 : if (c->ts.u.derived->attr.defined_assign_comp
16385 : 17762 : || (c->ts.u.derived->f2k_derived
16386 : 17193 : && c->ts.u.derived->f2k_derived->tb_op[INTRINSIC_ASSIGN]))
16387 : : {
16388 : 1528 : derived->attr.defined_assign_comp = 1;
16389 : 1528 : return;
16390 : : }
16391 : :
16392 : 16451 : if (c->attr.allocatable)
16393 : 5569 : continue;
16394 : :
16395 : 10882 : check_defined_assignments (c->ts.u.derived);
16396 : 10882 : if (c->ts.u.derived->attr.defined_assign_comp)
16397 : : {
16398 : 24 : derived->attr.defined_assign_comp = 1;
16399 : 24 : return;
16400 : : }
16401 : : }
16402 : : }
16403 : :
16404 : :
16405 : : /* Resolve a single component of a derived type or structure. */
16406 : :
16407 : : static bool
16408 : 384287 : resolve_component (gfc_component *c, gfc_symbol *sym)
16409 : : {
16410 : 384287 : gfc_symbol *super_type;
16411 : 384287 : symbol_attribute *attr;
16412 : :
16413 : 384287 : if (c->attr.artificial)
16414 : : return true;
16415 : :
16416 : : /* Do not allow vtype components to be resolved in nameless namespaces
16417 : : such as block data because the procedure pointers will cause ICEs
16418 : : and vtables are not needed in these contexts. */
16419 : 261950 : if (sym->attr.vtype && sym->attr.use_assoc
16420 : 46434 : && sym->ns->proc_name == NULL)
16421 : : return true;
16422 : :
16423 : : /* F2008, C442. */
16424 : 261941 : if ((!sym->attr.is_class || c != sym->components)
16425 : 261941 : && c->attr.codimension
16426 : 177 : && (!c->attr.allocatable || (c->as && c->as->type != AS_DEFERRED)))
16427 : : {
16428 : 4 : gfc_error ("Coarray component %qs at %L must be allocatable with "
16429 : : "deferred shape", c->name, &c->loc);
16430 : 4 : return false;
16431 : : }
16432 : :
16433 : : /* F2008, C443. */
16434 : 261937 : if (c->attr.codimension && c->ts.type == BT_DERIVED
16435 : 77 : && c->ts.u.derived->ts.is_iso_c)
16436 : : {
16437 : 1 : gfc_error ("Component %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
16438 : : "shall not be a coarray", c->name, &c->loc);
16439 : 1 : return false;
16440 : : }
16441 : :
16442 : : /* F2008, C444. */
16443 : 261936 : if (gfc_bt_struct (c->ts.type) && c->ts.u.derived->attr.coarray_comp
16444 : 22 : && (c->attr.codimension || c->attr.pointer || c->attr.dimension
16445 : 20 : || c->attr.allocatable))
16446 : : {
16447 : 3 : gfc_error ("Component %qs at %L with coarray component "
16448 : : "shall be a nonpointer, nonallocatable scalar",
16449 : : c->name, &c->loc);
16450 : 3 : return false;
16451 : : }
16452 : :
16453 : : /* F2008, C448. */
16454 : 261933 : if (c->ts.type == BT_CLASS)
16455 : : {
16456 : 6579 : if (c->attr.class_ok && CLASS_DATA (c))
16457 : : {
16458 : 6571 : attr = &(CLASS_DATA (c)->attr);
16459 : :
16460 : : /* Fix up contiguous attribute. */
16461 : 6571 : if (c->attr.contiguous)
16462 : 3 : attr->contiguous = 1;
16463 : : }
16464 : : else
16465 : : attr = NULL;
16466 : : }
16467 : : else
16468 : 255354 : attr = &c->attr;
16469 : :
16470 : 261928 : if (attr && attr->contiguous && (!attr->dimension || !attr->pointer))
16471 : : {
16472 : 5 : gfc_error ("Component %qs at %L has the CONTIGUOUS attribute but "
16473 : : "is not an array pointer", c->name, &c->loc);
16474 : 5 : return false;
16475 : : }
16476 : :
16477 : : /* F2003, 15.2.1 - length has to be one. */
16478 : 39775 : if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
16479 : 261947 : && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
16480 : 19 : || !gfc_is_constant_expr (c->ts.u.cl->length)
16481 : 19 : || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
16482 : : {
16483 : 1 : gfc_error ("Component %qs of BIND(C) type at %L must have length one",
16484 : : c->name, &c->loc);
16485 : 1 : return false;
16486 : : }
16487 : :
16488 : 261927 : if (c->attr.proc_pointer && c->ts.interface)
16489 : : {
16490 : 13477 : gfc_symbol *ifc = c->ts.interface;
16491 : :
16492 : 13477 : if (!sym->attr.vtype && !check_proc_interface (ifc, &c->loc))
16493 : : {
16494 : 6 : c->tb->error = 1;
16495 : 6 : return false;
16496 : : }
16497 : :
16498 : 13471 : if (ifc->attr.if_source || ifc->attr.intrinsic)
16499 : : {
16500 : : /* Resolve interface and copy attributes. */
16501 : 13440 : if (ifc->formal && !ifc->formal_ns)
16502 : 2483 : resolve_symbol (ifc);
16503 : 13440 : if (ifc->attr.intrinsic)
16504 : 0 : gfc_resolve_intrinsic (ifc, &ifc->declared_at);
16505 : :
16506 : 13440 : if (ifc->result)
16507 : : {
16508 : 6665 : c->ts = ifc->result->ts;
16509 : 6665 : c->attr.allocatable = ifc->result->attr.allocatable;
16510 : 6665 : c->attr.pointer = ifc->result->attr.pointer;
16511 : 6665 : c->attr.dimension = ifc->result->attr.dimension;
16512 : 6665 : c->as = gfc_copy_array_spec (ifc->result->as);
16513 : 6665 : c->attr.class_ok = ifc->result->attr.class_ok;
16514 : : }
16515 : : else
16516 : : {
16517 : 6775 : c->ts = ifc->ts;
16518 : 6775 : c->attr.allocatable = ifc->attr.allocatable;
16519 : 6775 : c->attr.pointer = ifc->attr.pointer;
16520 : 6775 : c->attr.dimension = ifc->attr.dimension;
16521 : 6775 : c->as = gfc_copy_array_spec (ifc->as);
16522 : 6775 : c->attr.class_ok = ifc->attr.class_ok;
16523 : : }
16524 : 13440 : c->ts.interface = ifc;
16525 : 13440 : c->attr.function = ifc->attr.function;
16526 : 13440 : c->attr.subroutine = ifc->attr.subroutine;
16527 : :
16528 : 13440 : c->attr.pure = ifc->attr.pure;
16529 : 13440 : c->attr.elemental = ifc->attr.elemental;
16530 : 13440 : c->attr.recursive = ifc->attr.recursive;
16531 : 13440 : c->attr.always_explicit = ifc->attr.always_explicit;
16532 : 13440 : c->attr.ext_attr |= ifc->attr.ext_attr;
16533 : : /* Copy char length. */
16534 : 13440 : if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
16535 : : {
16536 : 221 : gfc_charlen *cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
16537 : 184 : if (cl->length && !cl->resolved
16538 : 241 : && !gfc_resolve_expr (cl->length))
16539 : : {
16540 : 0 : c->tb->error = 1;
16541 : 0 : return false;
16542 : : }
16543 : 221 : c->ts.u.cl = cl;
16544 : : }
16545 : : }
16546 : : }
16547 : 248450 : else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
16548 : : {
16549 : : /* Since PPCs are not implicitly typed, a PPC without an explicit
16550 : : interface must be a subroutine. */
16551 : 115 : gfc_add_subroutine (&c->attr, c->name, &c->loc);
16552 : : }
16553 : :
16554 : : /* Procedure pointer components: Check PASS arg. */
16555 : 261921 : if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0
16556 : 717 : && !sym->attr.vtype)
16557 : : {
16558 : 94 : gfc_symbol* me_arg;
16559 : :
16560 : 94 : if (c->tb->pass_arg)
16561 : : {
16562 : 19 : gfc_formal_arglist* i;
16563 : :
16564 : : /* If an explicit passing argument name is given, walk the arg-list
16565 : : and look for it. */
16566 : :
16567 : 19 : me_arg = NULL;
16568 : 19 : c->tb->pass_arg_num = 1;
16569 : 33 : for (i = c->ts.interface->formal; i; i = i->next)
16570 : : {
16571 : 32 : if (!strcmp (i->sym->name, c->tb->pass_arg))
16572 : : {
16573 : : me_arg = i->sym;
16574 : : break;
16575 : : }
16576 : 14 : c->tb->pass_arg_num++;
16577 : : }
16578 : :
16579 : 19 : if (!me_arg)
16580 : : {
16581 : 1 : gfc_error ("Procedure pointer component %qs with PASS(%s) "
16582 : : "at %L has no argument %qs", c->name,
16583 : : c->tb->pass_arg, &c->loc, c->tb->pass_arg);
16584 : 1 : c->tb->error = 1;
16585 : 1 : return false;
16586 : : }
16587 : : }
16588 : : else
16589 : : {
16590 : : /* Otherwise, take the first one; there should in fact be at least
16591 : : one. */
16592 : 75 : c->tb->pass_arg_num = 1;
16593 : 75 : if (!c->ts.interface->formal)
16594 : : {
16595 : 3 : gfc_error ("Procedure pointer component %qs with PASS at %L "
16596 : : "must have at least one argument",
16597 : : c->name, &c->loc);
16598 : 3 : c->tb->error = 1;
16599 : 3 : return false;
16600 : : }
16601 : 72 : me_arg = c->ts.interface->formal->sym;
16602 : : }
16603 : :
16604 : : /* Now check that the argument-type matches. */
16605 : 72 : gcc_assert (me_arg);
16606 : 90 : if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
16607 : 89 : || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
16608 : 89 : || (me_arg->ts.type == BT_CLASS
16609 : 81 : && CLASS_DATA (me_arg)->ts.u.derived != sym))
16610 : : {
16611 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L must be of"
16612 : : " the derived type %qs", me_arg->name, c->name,
16613 : : me_arg->name, &c->loc, sym->name);
16614 : 1 : c->tb->error = 1;
16615 : 1 : return false;
16616 : : }
16617 : :
16618 : : /* Check for F03:C453. */
16619 : 89 : if (CLASS_DATA (me_arg)->attr.dimension)
16620 : : {
16621 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16622 : : "must be scalar", me_arg->name, c->name, me_arg->name,
16623 : : &c->loc);
16624 : 1 : c->tb->error = 1;
16625 : 1 : return false;
16626 : : }
16627 : :
16628 : 88 : if (CLASS_DATA (me_arg)->attr.class_pointer)
16629 : : {
16630 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16631 : : "may not have the POINTER attribute", me_arg->name,
16632 : : c->name, me_arg->name, &c->loc);
16633 : 1 : c->tb->error = 1;
16634 : 1 : return false;
16635 : : }
16636 : :
16637 : 87 : if (CLASS_DATA (me_arg)->attr.allocatable)
16638 : : {
16639 : 1 : gfc_error ("Argument %qs of %qs with PASS(%s) at %L "
16640 : : "may not be ALLOCATABLE", me_arg->name, c->name,
16641 : : me_arg->name, &c->loc);
16642 : 1 : c->tb->error = 1;
16643 : 1 : return false;
16644 : : }
16645 : :
16646 : 86 : if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
16647 : : {
16648 : 2 : gfc_error ("Non-polymorphic passed-object dummy argument of %qs"
16649 : : " at %L", c->name, &c->loc);
16650 : 2 : return false;
16651 : : }
16652 : :
16653 : : }
16654 : :
16655 : : /* Check type-spec if this is not the parent-type component. */
16656 : 261911 : if (((sym->attr.is_class
16657 : 11511 : && (!sym->components->ts.u.derived->attr.extension
16658 : 2318 : || c != CLASS_DATA (sym->components)))
16659 : 251696 : || (!sym->attr.is_class
16660 : 250400 : && (!sym->attr.extension || c != sym->components)))
16661 : 254124 : && !sym->attr.vtype
16662 : 413875 : && !resolve_typespec_used (&c->ts, &c->loc, c->name))
16663 : : return false;
16664 : :
16665 : 261910 : super_type = gfc_get_derived_super_type (sym);
16666 : :
16667 : : /* If this type is an extension, set the accessibility of the parent
16668 : : component. */
16669 : 261910 : if (super_type
16670 : 24125 : && ((sym->attr.is_class
16671 : 11511 : && c == CLASS_DATA (sym->components))
16672 : 16096 : || (!sym->attr.is_class && c == sym->components))
16673 : 14520 : && strcmp (super_type->name, c->name) == 0)
16674 : 6365 : c->attr.access = super_type->attr.access;
16675 : :
16676 : : /* If this type is an extension, see if this component has the same name
16677 : : as an inherited type-bound procedure. */
16678 : 24125 : if (super_type && !sym->attr.is_class
16679 : 12614 : && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
16680 : : {
16681 : 1 : gfc_error ("Component %qs of %qs at %L has the same name as an"
16682 : : " inherited type-bound procedure",
16683 : : c->name, sym->name, &c->loc);
16684 : 1 : return false;
16685 : : }
16686 : :
16687 : 261909 : if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
16688 : 9195 : && !c->ts.deferred)
16689 : : {
16690 : 7044 : if (c->ts.u.cl->length == NULL
16691 : 7038 : || (!resolve_charlen(c->ts.u.cl))
16692 : 14081 : || !gfc_is_constant_expr (c->ts.u.cl->length))
16693 : : {
16694 : 9 : gfc_error ("Character length of component %qs needs to "
16695 : : "be a constant specification expression at %L",
16696 : : c->name,
16697 : 9 : c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
16698 : 9 : return false;
16699 : : }
16700 : :
16701 : 7035 : if (c->ts.u.cl->length && c->ts.u.cl->length->ts.type != BT_INTEGER)
16702 : : {
16703 : 2 : if (!c->ts.u.cl->length->error)
16704 : : {
16705 : 1 : gfc_error ("Character length expression of component %qs at %L "
16706 : : "must be of INTEGER type, found %s",
16707 : 1 : c->name, &c->ts.u.cl->length->where,
16708 : : gfc_basic_typename (c->ts.u.cl->length->ts.type));
16709 : 1 : c->ts.u.cl->length->error = 1;
16710 : : }
16711 : 2 : return false;
16712 : : }
16713 : : }
16714 : :
16715 : 261898 : if (c->ts.type == BT_CHARACTER && c->ts.deferred
16716 : 2187 : && !c->attr.pointer && !c->attr.allocatable)
16717 : : {
16718 : 1 : gfc_error ("Character component %qs of %qs at %L with deferred "
16719 : : "length must be a POINTER or ALLOCATABLE",
16720 : : c->name, sym->name, &c->loc);
16721 : 1 : return false;
16722 : : }
16723 : :
16724 : : /* Add the hidden deferred length field. */
16725 : 261897 : if (c->ts.type == BT_CHARACTER
16726 : 9425 : && (c->ts.deferred || c->attr.pdt_string)
16727 : 2322 : && !c->attr.function
16728 : 2286 : && !sym->attr.is_class)
16729 : : {
16730 : 2139 : char name[GFC_MAX_SYMBOL_LEN+9];
16731 : 2139 : gfc_component *strlen;
16732 : 2139 : sprintf (name, "_%s_length", c->name);
16733 : 2139 : strlen = gfc_find_component (sym, name, true, true, NULL);
16734 : 2139 : if (strlen == NULL)
16735 : : {
16736 : 447 : if (!gfc_add_component (sym, name, &strlen))
16737 : 0 : return false;
16738 : 447 : strlen->ts.type = BT_INTEGER;
16739 : 447 : strlen->ts.kind = gfc_charlen_int_kind;
16740 : 447 : strlen->attr.access = ACCESS_PRIVATE;
16741 : 447 : strlen->attr.artificial = 1;
16742 : : }
16743 : : }
16744 : :
16745 : 261897 : if (c->ts.type == BT_DERIVED
16746 : 48360 : && sym->component_access != ACCESS_PRIVATE
16747 : 47388 : && gfc_check_symbol_access (sym)
16748 : 92868 : && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
16749 : 46382 : && !c->ts.u.derived->attr.use_assoc
16750 : 24668 : && !gfc_check_symbol_access (c->ts.u.derived)
16751 : 262091 : && !gfc_notify_std (GFC_STD_F2003, "the component %qs is a "
16752 : : "PRIVATE type and cannot be a component of "
16753 : : "%qs, which is PUBLIC at %L", c->name,
16754 : : sym->name, &sym->declared_at))
16755 : : return false;
16756 : :
16757 : 261896 : if ((sym->attr.sequence || sym->attr.is_bind_c) && c->ts.type == BT_CLASS)
16758 : : {
16759 : 2 : gfc_error ("Polymorphic component %s at %L in SEQUENCE or BIND(C) "
16760 : : "type %s", c->name, &c->loc, sym->name);
16761 : 2 : return false;
16762 : : }
16763 : :
16764 : 261894 : if (sym->attr.sequence)
16765 : : {
16766 : 2506 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
16767 : : {
16768 : 0 : gfc_error ("Component %s of SEQUENCE type declared at %L does "
16769 : : "not have the SEQUENCE attribute",
16770 : : c->ts.u.derived->name, &sym->declared_at);
16771 : 0 : return false;
16772 : : }
16773 : : }
16774 : :
16775 : 261894 : if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.generic)
16776 : 0 : c->ts.u.derived = gfc_find_dt_in_generic (c->ts.u.derived);
16777 : 261894 : else if (c->ts.type == BT_CLASS && c->attr.class_ok
16778 : 6911 : && CLASS_DATA (c)->ts.u.derived->attr.generic)
16779 : 0 : CLASS_DATA (c)->ts.u.derived
16780 : 0 : = gfc_find_dt_in_generic (CLASS_DATA (c)->ts.u.derived);
16781 : :
16782 : : /* If an allocatable component derived type is of the same type as
16783 : : the enclosing derived type, we need a vtable generating so that
16784 : : the __deallocate procedure is created. */
16785 : 261894 : if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
16786 : 55281 : && c->ts.u.derived == sym && c->attr.allocatable == 1)
16787 : 190 : gfc_find_vtab (&c->ts);
16788 : :
16789 : : /* Ensure that all the derived type components are put on the
16790 : : derived type list; even in formal namespaces, where derived type
16791 : : pointer components might not have been declared. */
16792 : 261894 : if (c->ts.type == BT_DERIVED
16793 : 48359 : && c->ts.u.derived
16794 : 48359 : && c->ts.u.derived->components
16795 : 45221 : && c->attr.pointer
16796 : 31623 : && sym != c->ts.u.derived)
16797 : 4155 : add_dt_to_dt_list (c->ts.u.derived);
16798 : :
16799 : 261894 : if (c->as && c->as->type != AS_DEFERRED
16800 : 5509 : && (c->attr.pointer || c->attr.allocatable))
16801 : : return false;
16802 : :
16803 : 261880 : if (!gfc_resolve_array_spec (c->as,
16804 : 261880 : !(c->attr.pointer || c->attr.proc_pointer
16805 : 213211 : || c->attr.allocatable)))
16806 : : return false;
16807 : :
16808 : 96898 : if (c->initializer && !sym->attr.vtype
16809 : 28146 : && !c->attr.pdt_kind && !c->attr.pdt_len
16810 : 288855 : && !gfc_check_assign_symbol (sym, c, c->initializer))
16811 : : return false;
16812 : :
16813 : : return true;
16814 : : }
16815 : :
16816 : :
16817 : : /* Be nice about the locus for a structure expression - show the locus of the
16818 : : first non-null sub-expression if we can. */
16819 : :
16820 : : static locus *
16821 : 4 : cons_where (gfc_expr *struct_expr)
16822 : : {
16823 : 4 : gfc_constructor *cons;
16824 : :
16825 : 4 : gcc_assert (struct_expr && struct_expr->expr_type == EXPR_STRUCTURE);
16826 : :
16827 : 4 : cons = gfc_constructor_first (struct_expr->value.constructor);
16828 : 12 : for (; cons; cons = gfc_constructor_next (cons))
16829 : : {
16830 : 8 : if (cons->expr && cons->expr->expr_type != EXPR_NULL)
16831 : 4 : return &cons->expr->where;
16832 : : }
16833 : :
16834 : 0 : return &struct_expr->where;
16835 : : }
16836 : :
16837 : : /* Resolve the components of a structure type. Much less work than derived
16838 : : types. */
16839 : :
16840 : : static bool
16841 : 913 : resolve_fl_struct (gfc_symbol *sym)
16842 : : {
16843 : 913 : gfc_component *c;
16844 : 913 : gfc_expr *init = NULL;
16845 : 913 : bool success;
16846 : :
16847 : : /* Make sure UNIONs do not have overlapping initializers. */
16848 : 913 : if (sym->attr.flavor == FL_UNION)
16849 : : {
16850 : 498 : for (c = sym->components; c; c = c->next)
16851 : : {
16852 : 331 : if (init && c->initializer)
16853 : : {
16854 : 2 : gfc_error ("Conflicting initializers in union at %L and %L",
16855 : : cons_where (init), cons_where (c->initializer));
16856 : 2 : gfc_free_expr (c->initializer);
16857 : 2 : c->initializer = NULL;
16858 : : }
16859 : 291 : if (init == NULL)
16860 : 291 : init = c->initializer;
16861 : : }
16862 : : }
16863 : :
16864 : 913 : success = true;
16865 : 2830 : for (c = sym->components; c; c = c->next)
16866 : 1917 : if (!resolve_component (c, sym))
16867 : 0 : success = false;
16868 : :
16869 : 913 : if (!success)
16870 : : return false;
16871 : :
16872 : 913 : if (sym->components)
16873 : 862 : add_dt_to_dt_list (sym);
16874 : :
16875 : : return true;
16876 : : }
16877 : :
16878 : : /* Figure if the derived type is using itself directly in one of its components
16879 : : or through referencing other derived types. The information is required to
16880 : : generate the __deallocate and __final type bound procedures to ensure
16881 : : freeing larger hierarchies of derived types with allocatable objects. */
16882 : :
16883 : : static void
16884 : 130495 : resolve_cyclic_derived_type (gfc_symbol *derived)
16885 : : {
16886 : 130495 : hash_set<gfc_symbol *> seen, to_examin;
16887 : 130495 : gfc_component *c;
16888 : 130495 : seen.add (derived);
16889 : 130495 : to_examin.add (derived);
16890 : 436776 : while (!to_examin.is_empty ())
16891 : : {
16892 : 177713 : gfc_symbol *cand = *to_examin.begin ();
16893 : 177713 : to_examin.remove (cand);
16894 : 475587 : for (c = cand->components; c; c = c->next)
16895 : 299801 : if (c->ts.type == BT_DERIVED)
16896 : : {
16897 : 65748 : if (c->ts.u.derived == derived)
16898 : : {
16899 : 957 : derived->attr.recursive = 1;
16900 : 1927 : return;
16901 : : }
16902 : 64791 : else if (!seen.contains (c->ts.u.derived))
16903 : : {
16904 : 42843 : seen.add (c->ts.u.derived);
16905 : 42843 : to_examin.add (c->ts.u.derived);
16906 : : }
16907 : : }
16908 : 234053 : else if (c->ts.type == BT_CLASS)
16909 : : {
16910 : 8789 : if (!c->attr.class_ok)
16911 : 7 : continue;
16912 : 8782 : if (CLASS_DATA (c)->ts.u.derived == derived)
16913 : : {
16914 : 970 : derived->attr.recursive = 1;
16915 : 970 : return;
16916 : : }
16917 : 7812 : else if (!seen.contains (CLASS_DATA (c)->ts.u.derived))
16918 : : {
16919 : 4596 : seen.add (CLASS_DATA (c)->ts.u.derived);
16920 : 4596 : to_examin.add (CLASS_DATA (c)->ts.u.derived);
16921 : : }
16922 : : }
16923 : : }
16924 : 130495 : }
16925 : :
16926 : : /* Resolve the components of a derived type. This does not have to wait until
16927 : : resolution stage, but can be done as soon as the dt declaration has been
16928 : : parsed. */
16929 : :
16930 : : static bool
16931 : 160742 : resolve_fl_derived0 (gfc_symbol *sym)
16932 : : {
16933 : 160742 : gfc_symbol* super_type;
16934 : 160742 : gfc_component *c;
16935 : 160742 : gfc_formal_arglist *f;
16936 : 160742 : bool success;
16937 : :
16938 : 160742 : if (sym->attr.unlimited_polymorphic)
16939 : : return true;
16940 : :
16941 : 160742 : super_type = gfc_get_derived_super_type (sym);
16942 : :
16943 : : /* F2008, C432. */
16944 : 160742 : if (super_type && sym->attr.coarray_comp && !super_type->attr.coarray_comp)
16945 : : {
16946 : 2 : gfc_error ("As extending type %qs at %L has a coarray component, "
16947 : : "parent type %qs shall also have one", sym->name,
16948 : : &sym->declared_at, super_type->name);
16949 : 2 : return false;
16950 : : }
16951 : :
16952 : : /* Ensure the extended type gets resolved before we do. */
16953 : 16424 : if (super_type && !resolve_fl_derived0 (super_type))
16954 : : return false;
16955 : :
16956 : : /* An ABSTRACT type must be extensible. */
16957 : 160734 : if (sym->attr.abstract && !gfc_type_is_extensible (sym))
16958 : : {
16959 : 2 : gfc_error ("Non-extensible derived-type %qs at %L must not be ABSTRACT",
16960 : : sym->name, &sym->declared_at);
16961 : 2 : return false;
16962 : : }
16963 : :
16964 : : /* Resolving components below, may create vtabs for which the cyclic type
16965 : : information needs to be present. */
16966 : 160732 : if (!sym->attr.vtype)
16967 : 130495 : resolve_cyclic_derived_type (sym);
16968 : :
16969 : 160732 : c = (sym->attr.is_class) ? CLASS_DATA (sym->components)
16970 : : : sym->components;
16971 : :
16972 : : success = true;
16973 : 543102 : for ( ; c != NULL; c = c->next)
16974 : 382370 : if (!resolve_component (c, sym))
16975 : 83 : success = false;
16976 : :
16977 : 160732 : if (!success)
16978 : : return false;
16979 : :
16980 : : /* Now add the caf token field, where needed. */
16981 : 160658 : if (flag_coarray == GFC_FCOARRAY_LIB && !sym->attr.is_class
16982 : 770 : && !sym->attr.vtype)
16983 : : {
16984 : 1883 : for (c = sym->components; c; c = c->next)
16985 : 1266 : if (!c->attr.dimension && !c->attr.codimension
16986 : 703 : && (c->attr.allocatable || c->attr.pointer))
16987 : : {
16988 : 134 : char name[GFC_MAX_SYMBOL_LEN+9];
16989 : 134 : gfc_component *token;
16990 : 134 : sprintf (name, "_caf_%s", c->name);
16991 : 134 : token = gfc_find_component (sym, name, true, true, NULL);
16992 : 134 : if (token == NULL)
16993 : : {
16994 : 75 : if (!gfc_add_component (sym, name, &token))
16995 : 0 : return false;
16996 : 75 : token->ts.type = BT_VOID;
16997 : 75 : token->ts.kind = gfc_default_integer_kind;
16998 : 75 : token->attr.access = ACCESS_PRIVATE;
16999 : 75 : token->attr.artificial = 1;
17000 : 75 : token->attr.caf_token = 1;
17001 : : }
17002 : 134 : c->caf_token = token;
17003 : : }
17004 : : }
17005 : :
17006 : 160658 : check_defined_assignments (sym);
17007 : :
17008 : 160658 : if (!sym->attr.defined_assign_comp && super_type)
17009 : 15594 : sym->attr.defined_assign_comp
17010 : 15594 : = super_type->attr.defined_assign_comp;
17011 : :
17012 : : /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
17013 : : all DEFERRED bindings are overridden. */
17014 : 16417 : if (super_type && super_type->attr.abstract && !sym->attr.abstract
17015 : 1355 : && !sym->attr.is_class
17016 : 2880 : && !ensure_not_abstract (sym, super_type))
17017 : : return false;
17018 : :
17019 : : /* Check that there is a component for every PDT parameter. */
17020 : 160652 : if (sym->attr.pdt_template)
17021 : : {
17022 : 970 : for (f = sym->formal; f; f = f->next)
17023 : : {
17024 : 642 : if (!f->sym)
17025 : 1 : continue;
17026 : 641 : c = gfc_find_component (sym, f->sym->name, true, true, NULL);
17027 : 641 : if (c == NULL)
17028 : : {
17029 : 9 : gfc_error ("Parameterized type %qs does not have a component "
17030 : : "corresponding to parameter %qs at %L", sym->name,
17031 : 9 : f->sym->name, &sym->declared_at);
17032 : 9 : break;
17033 : : }
17034 : : }
17035 : : }
17036 : :
17037 : : /* Add derived type to the derived type list. */
17038 : 160652 : add_dt_to_dt_list (sym);
17039 : :
17040 : 160652 : return true;
17041 : : }
17042 : :
17043 : : /* The following procedure does the full resolution of a derived type,
17044 : : including resolution of all type-bound procedures (if present). In contrast
17045 : : to 'resolve_fl_derived0' this can only be done after the module has been
17046 : : parsed completely. */
17047 : :
17048 : : static bool
17049 : 83696 : resolve_fl_derived (gfc_symbol *sym)
17050 : : {
17051 : 83696 : gfc_symbol *gen_dt = NULL;
17052 : :
17053 : 83696 : if (sym->attr.unlimited_polymorphic)
17054 : : return true;
17055 : :
17056 : 83696 : if (!sym->attr.is_class)
17057 : 71823 : gfc_find_symbol (sym->name, sym->ns, 0, &gen_dt);
17058 : 53517 : if (gen_dt && gen_dt->generic && gen_dt->generic->next
17059 : 2169 : && (!gen_dt->generic->sym->attr.use_assoc
17060 : 2069 : || gen_dt->generic->sym->module != gen_dt->generic->next->sym->module)
17061 : 83829 : && !gfc_notify_std (GFC_STD_F2003, "Generic name %qs of function "
17062 : : "%qs at %L being the same name as derived "
17063 : : "type at %L", sym->name,
17064 : : gen_dt->generic->sym == sym
17065 : 11 : ? gen_dt->generic->next->sym->name
17066 : : : gen_dt->generic->sym->name,
17067 : : gen_dt->generic->sym == sym
17068 : 11 : ? &gen_dt->generic->next->sym->declared_at
17069 : : : &gen_dt->generic->sym->declared_at,
17070 : : &sym->declared_at))
17071 : : return false;
17072 : :
17073 : 83692 : if (sym->components == NULL && !sym->attr.zero_comp && !sym->attr.use_assoc)
17074 : : {
17075 : 13 : gfc_error ("Derived type %qs at %L has not been declared",
17076 : : sym->name, &sym->declared_at);
17077 : 13 : return false;
17078 : : }
17079 : :
17080 : : /* Resolve the finalizer procedures. */
17081 : 83679 : if (!gfc_resolve_finalizers (sym, NULL))
17082 : : return false;
17083 : :
17084 : 83676 : if (sym->attr.is_class && sym->ts.u.derived == NULL)
17085 : : {
17086 : : /* Fix up incomplete CLASS symbols. */
17087 : 11873 : gfc_component *data = gfc_find_component (sym, "_data", true, true, NULL);
17088 : 11873 : gfc_component *vptr = gfc_find_component (sym, "_vptr", true, true, NULL);
17089 : :
17090 : : /* Nothing more to do for unlimited polymorphic entities. */
17091 : 11873 : if (data->ts.u.derived->attr.unlimited_polymorphic)
17092 : : {
17093 : 1941 : add_dt_to_dt_list (sym);
17094 : 1941 : return true;
17095 : : }
17096 : 9932 : else if (vptr->ts.u.derived == NULL)
17097 : : {
17098 : 5968 : gfc_symbol *vtab = gfc_find_derived_vtab (data->ts.u.derived);
17099 : 5968 : gcc_assert (vtab);
17100 : 5968 : vptr->ts.u.derived = vtab->ts.u.derived;
17101 : 5968 : if (!resolve_fl_derived0 (vptr->ts.u.derived))
17102 : : return false;
17103 : : }
17104 : : }
17105 : :
17106 : 81735 : if (!resolve_fl_derived0 (sym))
17107 : : return false;
17108 : :
17109 : : /* Resolve the type-bound procedures. */
17110 : 81657 : if (!resolve_typebound_procedures (sym))
17111 : : return false;
17112 : :
17113 : : /* Generate module vtables subject to their accessibility and their not
17114 : : being vtables or pdt templates. If this is not done class declarations
17115 : : in external procedures wind up with their own version and so SELECT TYPE
17116 : : fails because the vptrs do not have the same address. */
17117 : 81616 : if (gfc_option.allow_std & GFC_STD_F2003 && sym->ns->proc_name
17118 : 81555 : && (sym->ns->proc_name->attr.flavor == FL_MODULE
17119 : 61391 : || (sym->attr.recursive && sym->attr.alloc_comp))
17120 : 20268 : && sym->attr.access != ACCESS_PRIVATE
17121 : 20235 : && !(sym->attr.vtype || sym->attr.pdt_template))
17122 : : {
17123 : 18358 : gfc_symbol *vtab = gfc_find_derived_vtab (sym);
17124 : 18358 : gfc_set_sym_referenced (vtab);
17125 : : }
17126 : :
17127 : : return true;
17128 : : }
17129 : :
17130 : :
17131 : : static bool
17132 : 805 : resolve_fl_namelist (gfc_symbol *sym)
17133 : : {
17134 : 805 : gfc_namelist *nl;
17135 : 805 : gfc_symbol *nlsym;
17136 : :
17137 : 2892 : for (nl = sym->namelist; nl; nl = nl->next)
17138 : : {
17139 : : /* Check again, the check in match only works if NAMELIST comes
17140 : : after the decl. */
17141 : 2092 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SIZE)
17142 : : {
17143 : 1 : gfc_error ("Assumed size array %qs in namelist %qs at %L is not "
17144 : : "allowed", nl->sym->name, sym->name, &sym->declared_at);
17145 : 1 : return false;
17146 : : }
17147 : :
17148 : 652 : if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
17149 : 2099 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17150 : : "with assumed shape in namelist %qs at %L",
17151 : : nl->sym->name, sym->name, &sym->declared_at))
17152 : : return false;
17153 : :
17154 : 2090 : if (is_non_constant_shape_array (nl->sym)
17155 : 2140 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST array object %qs "
17156 : : "with nonconstant shape in namelist %qs at %L",
17157 : 50 : nl->sym->name, sym->name, &sym->declared_at))
17158 : : return false;
17159 : :
17160 : 2089 : if (nl->sym->ts.type == BT_CHARACTER
17161 : 565 : && (nl->sym->ts.u.cl->length == NULL
17162 : 526 : || !gfc_is_constant_expr (nl->sym->ts.u.cl->length))
17163 : 2171 : && !gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs with "
17164 : : "nonconstant character length in "
17165 : 82 : "namelist %qs at %L", nl->sym->name,
17166 : : sym->name, &sym->declared_at))
17167 : : return false;
17168 : :
17169 : : }
17170 : :
17171 : : /* Reject PRIVATE objects in a PUBLIC namelist. */
17172 : 800 : if (gfc_check_symbol_access (sym))
17173 : : {
17174 : 2873 : for (nl = sym->namelist; nl; nl = nl->next)
17175 : : {
17176 : 2086 : if (!nl->sym->attr.use_assoc
17177 : 3948 : && !is_sym_host_assoc (nl->sym, sym->ns)
17178 : 4038 : && !gfc_check_symbol_access (nl->sym))
17179 : : {
17180 : 2 : gfc_error ("NAMELIST object %qs was declared PRIVATE and "
17181 : : "cannot be member of PUBLIC namelist %qs at %L",
17182 : 2 : nl->sym->name, sym->name, &sym->declared_at);
17183 : 2 : return false;
17184 : : }
17185 : :
17186 : 2084 : if (nl->sym->ts.type == BT_DERIVED
17187 : 466 : && (nl->sym->ts.u.derived->attr.alloc_comp
17188 : 464 : || nl->sym->ts.u.derived->attr.pointer_comp))
17189 : : {
17190 : 5 : if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
17191 : : "namelist %qs at %L with ALLOCATABLE "
17192 : : "or POINTER components", nl->sym->name,
17193 : : sym->name, &sym->declared_at))
17194 : : return false;
17195 : : return true;
17196 : : }
17197 : :
17198 : : /* Types with private components that came here by USE-association. */
17199 : 2079 : if (nl->sym->ts.type == BT_DERIVED
17200 : 2079 : && derived_inaccessible (nl->sym->ts.u.derived))
17201 : : {
17202 : 6 : gfc_error ("NAMELIST object %qs has use-associated PRIVATE "
17203 : : "components and cannot be member of namelist %qs at %L",
17204 : : nl->sym->name, sym->name, &sym->declared_at);
17205 : 6 : return false;
17206 : : }
17207 : :
17208 : : /* Types with private components that are defined in the same module. */
17209 : 2073 : if (nl->sym->ts.type == BT_DERIVED
17210 : 910 : && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
17211 : 2351 : && nl->sym->ts.u.derived->attr.private_comp)
17212 : : {
17213 : 0 : gfc_error ("NAMELIST object %qs has PRIVATE components and "
17214 : : "cannot be a member of PUBLIC namelist %qs at %L",
17215 : : nl->sym->name, sym->name, &sym->declared_at);
17216 : 0 : return false;
17217 : : }
17218 : : }
17219 : : }
17220 : :
17221 : :
17222 : : /* 14.1.2 A module or internal procedure represent local entities
17223 : : of the same type as a namelist member and so are not allowed. */
17224 : 2857 : for (nl = sym->namelist; nl; nl = nl->next)
17225 : : {
17226 : 2073 : if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
17227 : 1514 : continue;
17228 : :
17229 : 559 : if (nl->sym->attr.function && nl->sym == nl->sym->result)
17230 : 7 : if ((nl->sym == sym->ns->proc_name)
17231 : 1 : ||
17232 : 1 : (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
17233 : 6 : continue;
17234 : :
17235 : 553 : nlsym = NULL;
17236 : 553 : if (nl->sym->name)
17237 : 553 : gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
17238 : 553 : if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
17239 : : {
17240 : 3 : gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
17241 : : "attribute in %qs at %L", nlsym->name,
17242 : : &sym->declared_at);
17243 : 3 : return false;
17244 : : }
17245 : : }
17246 : :
17247 : : return true;
17248 : : }
17249 : :
17250 : :
17251 : : static bool
17252 : 370272 : resolve_fl_parameter (gfc_symbol *sym)
17253 : : {
17254 : : /* A parameter array's shape needs to be constant. */
17255 : 370272 : if (sym->as != NULL
17256 : 370272 : && (sym->as->type == AS_DEFERRED
17257 : 6080 : || is_non_constant_shape_array (sym)))
17258 : : {
17259 : 17 : gfc_error ("Parameter array %qs at %L cannot be automatic "
17260 : : "or of deferred shape", sym->name, &sym->declared_at);
17261 : 17 : return false;
17262 : : }
17263 : :
17264 : : /* Constraints on deferred type parameter. */
17265 : 370255 : if (!deferred_requirements (sym))
17266 : : return false;
17267 : :
17268 : : /* Make sure a parameter that has been implicitly typed still
17269 : : matches the implicit type, since PARAMETER statements can precede
17270 : : IMPLICIT statements. */
17271 : 370254 : if (sym->attr.implicit_type
17272 : 370963 : && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
17273 : 709 : sym->ns)))
17274 : : {
17275 : 0 : gfc_error ("Implicitly typed PARAMETER %qs at %L doesn't match a "
17276 : : "later IMPLICIT type", sym->name, &sym->declared_at);
17277 : 0 : return false;
17278 : : }
17279 : :
17280 : : /* Make sure the types of derived parameters are consistent. This
17281 : : type checking is deferred until resolution because the type may
17282 : : refer to a derived type from the host. */
17283 : 370254 : if (sym->ts.type == BT_DERIVED
17284 : 370254 : && !gfc_compare_types (&sym->ts, &sym->value->ts))
17285 : : {
17286 : 0 : gfc_error ("Incompatible derived type in PARAMETER at %L",
17287 : 0 : &sym->value->where);
17288 : 0 : return false;
17289 : : }
17290 : :
17291 : : /* F03:C509,C514. */
17292 : 370254 : if (sym->ts.type == BT_CLASS)
17293 : : {
17294 : 0 : gfc_error ("CLASS variable %qs at %L cannot have the PARAMETER attribute",
17295 : : sym->name, &sym->declared_at);
17296 : 0 : return false;
17297 : : }
17298 : :
17299 : : return true;
17300 : : }
17301 : :
17302 : :
17303 : : /* Called by resolve_symbol to check PDTs. */
17304 : :
17305 : : static void
17306 : 522 : resolve_pdt (gfc_symbol* sym)
17307 : : {
17308 : 522 : gfc_symbol *derived = NULL;
17309 : 522 : gfc_actual_arglist *param;
17310 : 522 : gfc_component *c;
17311 : 522 : bool const_len_exprs = true;
17312 : 522 : bool assumed_len_exprs = false;
17313 : 522 : symbol_attribute *attr;
17314 : :
17315 : 522 : if (sym->ts.type == BT_DERIVED)
17316 : : {
17317 : 462 : derived = sym->ts.u.derived;
17318 : 462 : attr = &(sym->attr);
17319 : : }
17320 : 60 : else if (sym->ts.type == BT_CLASS)
17321 : : {
17322 : 60 : derived = CLASS_DATA (sym)->ts.u.derived;
17323 : 60 : attr = &(CLASS_DATA (sym)->attr);
17324 : : }
17325 : : else
17326 : 0 : gcc_unreachable ();
17327 : :
17328 : 522 : gcc_assert (derived->attr.pdt_type);
17329 : :
17330 : 1383 : for (param = sym->param_list; param; param = param->next)
17331 : : {
17332 : 861 : c = gfc_find_component (derived, param->name, false, true, NULL);
17333 : 861 : gcc_assert (c);
17334 : 861 : if (c->attr.pdt_kind)
17335 : 386 : continue;
17336 : :
17337 : 292 : if (param->expr && !gfc_is_constant_expr (param->expr)
17338 : 502 : && c->attr.pdt_len)
17339 : : const_len_exprs = false;
17340 : 448 : else if (param->spec_type == SPEC_ASSUMED)
17341 : 142 : assumed_len_exprs = true;
17342 : :
17343 : 475 : if (param->spec_type == SPEC_DEFERRED && !attr->allocatable
17344 : 10 : && ((sym->ts.type == BT_DERIVED && !attr->pointer)
17345 : 8 : || (sym->ts.type == BT_CLASS && !attr->class_pointer)))
17346 : 3 : gfc_error ("Entity %qs at %L has a deferred LEN "
17347 : : "parameter %qs and requires either the POINTER "
17348 : : "or ALLOCATABLE attribute",
17349 : : sym->name, &sym->declared_at,
17350 : : param->name);
17351 : :
17352 : : }
17353 : :
17354 : 522 : if (!const_len_exprs
17355 : 27 : && (sym->ns->proc_name->attr.is_main_program
17356 : 26 : || sym->ns->proc_name->attr.flavor == FL_MODULE
17357 : 25 : || sym->attr.save != SAVE_NONE))
17358 : 2 : gfc_error ("The AUTOMATIC object %qs at %L must not have the "
17359 : : "SAVE attribute or be a variable declared in the "
17360 : : "main program, a module or a submodule(F08/C513)",
17361 : : sym->name, &sym->declared_at);
17362 : :
17363 : 522 : if (assumed_len_exprs && !(sym->attr.dummy
17364 : 1 : || sym->attr.select_type_temporary || sym->attr.associate_var))
17365 : 1 : gfc_error ("The object %qs at %L with ASSUMED type parameters "
17366 : : "must be a dummy or a SELECT TYPE selector(F08/4.2)",
17367 : : sym->name, &sym->declared_at);
17368 : 522 : }
17369 : :
17370 : :
17371 : : /* Resolve the symbol's array spec. */
17372 : :
17373 : : static bool
17374 : 1639276 : resolve_symbol_array_spec (gfc_symbol *sym, int check_constant)
17375 : : {
17376 : 1639276 : gfc_namespace *orig_current_ns = gfc_current_ns;
17377 : 1639276 : gfc_current_ns = gfc_get_spec_ns (sym);
17378 : :
17379 : 1639276 : bool saved_specification_expr = specification_expr;
17380 : 1639276 : specification_expr = true;
17381 : :
17382 : 1639276 : bool result = gfc_resolve_array_spec (sym->as, check_constant);
17383 : :
17384 : 1639276 : specification_expr = saved_specification_expr;
17385 : 1639276 : gfc_current_ns = orig_current_ns;
17386 : :
17387 : 1639276 : return result;
17388 : : }
17389 : :
17390 : :
17391 : : /* Do anything necessary to resolve a symbol. Right now, we just
17392 : : assume that an otherwise unknown symbol is a variable. This sort
17393 : : of thing commonly happens for symbols in module. */
17394 : :
17395 : : static void
17396 : 1762371 : resolve_symbol (gfc_symbol *sym)
17397 : : {
17398 : 1762371 : int check_constant, mp_flag;
17399 : 1762371 : gfc_symtree *symtree;
17400 : 1762371 : gfc_symtree *this_symtree;
17401 : 1762371 : gfc_namespace *ns;
17402 : 1762371 : gfc_component *c;
17403 : 1762371 : symbol_attribute class_attr;
17404 : 1762371 : gfc_array_spec *as;
17405 : :
17406 : 1762371 : if (sym->resolve_symbol_called >= 1)
17407 : 154198 : return;
17408 : 1695060 : sym->resolve_symbol_called = 1;
17409 : :
17410 : : /* No symbol will ever have union type; only components can be unions.
17411 : : Union type declaration symbols have type BT_UNKNOWN but flavor FL_UNION
17412 : : (just like derived type declaration symbols have flavor FL_DERIVED). */
17413 : 1695060 : gcc_assert (sym->ts.type != BT_UNION);
17414 : :
17415 : : /* Coarrayed polymorphic objects with allocatable or pointer components are
17416 : : yet unsupported for -fcoarray=lib. */
17417 : 1695060 : if (flag_coarray == GFC_FCOARRAY_LIB && sym->ts.type == BT_CLASS
17418 : 86 : && sym->ts.u.derived && CLASS_DATA (sym)
17419 : 86 : && CLASS_DATA (sym)->attr.codimension
17420 : 72 : && CLASS_DATA (sym)->ts.u.derived
17421 : 71 : && (CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp
17422 : 68 : || CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp))
17423 : : {
17424 : 6 : gfc_error ("Sorry, allocatable/pointer components in polymorphic (CLASS) "
17425 : : "type coarrays at %L are unsupported", &sym->declared_at);
17426 : 6 : return;
17427 : : }
17428 : :
17429 : 1695054 : if (sym->attr.artificial)
17430 : : return;
17431 : :
17432 : 1610730 : if (sym->attr.unlimited_polymorphic)
17433 : : return;
17434 : :
17435 : 1609332 : if (UNLIKELY (flag_openmp && strcmp (sym->name, "omp_all_memory") == 0))
17436 : : {
17437 : 4 : gfc_error ("%<omp_all_memory%>, declared at %L, may only be used in "
17438 : : "the OpenMP DEPEND clause", &sym->declared_at);
17439 : 4 : return;
17440 : : }
17441 : :
17442 : 1609328 : if (sym->attr.flavor == FL_UNKNOWN
17443 : 1588744 : || (sym->attr.flavor == FL_PROCEDURE && !sym->attr.intrinsic
17444 : 428974 : && !sym->attr.generic && !sym->attr.external
17445 : 174970 : && sym->attr.if_source == IFSRC_UNKNOWN
17446 : 78354 : && sym->ts.type == BT_UNKNOWN))
17447 : : {
17448 : : /* A symbol in a common block might not have been resolved yet properly.
17449 : : Do not try to find an interface with the same name. */
17450 : 90739 : if (sym->attr.flavor == FL_UNKNOWN && !sym->attr.intrinsic
17451 : 20580 : && !sym->attr.generic && !sym->attr.external
17452 : 20530 : && sym->attr.in_common)
17453 : 2530 : goto skip_interfaces;
17454 : :
17455 : : /* If we find that a flavorless symbol is an interface in one of the
17456 : : parent namespaces, find its symtree in this namespace, free the
17457 : : symbol and set the symtree to point to the interface symbol. */
17458 : 125964 : for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
17459 : : {
17460 : 38396 : symtree = gfc_find_symtree (ns->sym_root, sym->name);
17461 : 38396 : if (symtree && (symtree->n.sym->generic ||
17462 : 680 : (symtree->n.sym->attr.flavor == FL_PROCEDURE
17463 : 591 : && sym->ns->construct_entities)))
17464 : : {
17465 : 649 : this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
17466 : : sym->name);
17467 : 649 : if (this_symtree->n.sym == sym)
17468 : : {
17469 : 641 : symtree->n.sym->refs++;
17470 : 641 : gfc_release_symbol (sym);
17471 : 641 : this_symtree->n.sym = symtree->n.sym;
17472 : 641 : return;
17473 : : }
17474 : : }
17475 : : }
17476 : :
17477 : 87568 : skip_interfaces:
17478 : : /* Otherwise give it a flavor according to such attributes as
17479 : : it has. */
17480 : 90098 : if (sym->attr.flavor == FL_UNKNOWN && sym->attr.external == 0
17481 : 20406 : && sym->attr.intrinsic == 0)
17482 : 20402 : sym->attr.flavor = FL_VARIABLE;
17483 : 69696 : else if (sym->attr.flavor == FL_UNKNOWN)
17484 : : {
17485 : 54 : sym->attr.flavor = FL_PROCEDURE;
17486 : 54 : if (sym->attr.dimension)
17487 : 0 : sym->attr.function = 1;
17488 : : }
17489 : : }
17490 : :
17491 : 1608687 : if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
17492 : 2303 : gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
17493 : :
17494 : 1437 : if (sym->attr.procedure && sym->attr.if_source != IFSRC_DECL
17495 : 1610124 : && !resolve_procedure_interface (sym))
17496 : : return;
17497 : :
17498 : 1608676 : if (sym->attr.is_protected && !sym->attr.proc_pointer
17499 : 130 : && (sym->attr.procedure || sym->attr.external))
17500 : : {
17501 : 0 : if (sym->attr.external)
17502 : 0 : gfc_error ("PROTECTED attribute conflicts with EXTERNAL attribute "
17503 : : "at %L", &sym->declared_at);
17504 : : else
17505 : 0 : gfc_error ("PROCEDURE attribute conflicts with PROTECTED attribute "
17506 : : "at %L", &sym->declared_at);
17507 : :
17508 : 0 : return;
17509 : : }
17510 : :
17511 : : /* Ensure that variables of derived or class type having a finalizer are
17512 : : marked used even when the variable is not used anything else in the scope.
17513 : : This fixes PR118730. */
17514 : 629100 : if (sym->attr.flavor == FL_VARIABLE && !sym->attr.referenced
17515 : 429772 : && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
17516 : 1656622 : && gfc_may_be_finalized (sym->ts))
17517 : 8008 : gfc_set_sym_referenced (sym);
17518 : :
17519 : 1608676 : if (sym->attr.flavor == FL_DERIVED && !resolve_fl_derived (sym))
17520 : : return;
17521 : :
17522 : 1607906 : else if ((sym->attr.flavor == FL_STRUCT || sym->attr.flavor == FL_UNION)
17523 : 1608669 : && !resolve_fl_struct (sym))
17524 : : return;
17525 : :
17526 : : /* Symbols that are module procedures with results (functions) have
17527 : : the types and array specification copied for type checking in
17528 : : procedures that call them, as well as for saving to a module
17529 : : file. These symbols can't stand the scrutiny that their results
17530 : : can. */
17531 : 1608537 : mp_flag = (sym->result != NULL && sym->result != sym);
17532 : :
17533 : : /* Make sure that the intrinsic is consistent with its internal
17534 : : representation. This needs to be done before assigning a default
17535 : : type to avoid spurious warnings. */
17536 : 1575933 : if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
17537 : 1639777 : && !gfc_resolve_intrinsic (sym, &sym->declared_at))
17538 : : return;
17539 : :
17540 : : /* Resolve associate names. */
17541 : 1608501 : if (sym->assoc)
17542 : 6412 : resolve_assoc_var (sym, true);
17543 : :
17544 : : /* Assign default type to symbols that need one and don't have one. */
17545 : 1608501 : if (sym->ts.type == BT_UNKNOWN)
17546 : : {
17547 : 384491 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
17548 : : {
17549 : 11714 : gfc_set_default_type (sym, 1, NULL);
17550 : : }
17551 : :
17552 : 248287 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
17553 : 57676 : && !sym->attr.function && !sym->attr.subroutine
17554 : 386104 : && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
17555 : 564 : gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
17556 : :
17557 : 384491 : if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17558 : : {
17559 : : /* The specific case of an external procedure should emit an error
17560 : : in the case that there is no implicit type. */
17561 : 98400 : if (!mp_flag)
17562 : : {
17563 : 92632 : if (!sym->attr.mixed_entry_master)
17564 : 92526 : gfc_set_default_type (sym, sym->attr.external, NULL);
17565 : : }
17566 : : else
17567 : : {
17568 : : /* Result may be in another namespace. */
17569 : 5768 : resolve_symbol (sym->result);
17570 : :
17571 : 5768 : if (!sym->result->attr.proc_pointer)
17572 : : {
17573 : 5589 : sym->ts = sym->result->ts;
17574 : 5589 : sym->as = gfc_copy_array_spec (sym->result->as);
17575 : 5589 : sym->attr.dimension = sym->result->attr.dimension;
17576 : 5589 : sym->attr.codimension = sym->result->attr.codimension;
17577 : 5589 : sym->attr.pointer = sym->result->attr.pointer;
17578 : 5589 : sym->attr.allocatable = sym->result->attr.allocatable;
17579 : 5589 : sym->attr.contiguous = sym->result->attr.contiguous;
17580 : : }
17581 : : }
17582 : : }
17583 : : }
17584 : 1224010 : else if (mp_flag && sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
17585 : 31100 : resolve_symbol_array_spec (sym->result, false);
17586 : :
17587 : : /* For a CLASS-valued function with a result variable, affirm that it has
17588 : : been resolved also when looking at the symbol 'sym'. */
17589 : 415591 : if (mp_flag && sym->ts.type == BT_CLASS && sym->result->attr.class_ok)
17590 : 713 : sym->attr.class_ok = sym->result->attr.class_ok;
17591 : :
17592 : 1608501 : if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
17593 : 18450 : && CLASS_DATA (sym))
17594 : : {
17595 : 18449 : as = CLASS_DATA (sym)->as;
17596 : 18449 : class_attr = CLASS_DATA (sym)->attr;
17597 : 18449 : class_attr.pointer = class_attr.class_pointer;
17598 : : }
17599 : : else
17600 : : {
17601 : 1590052 : class_attr = sym->attr;
17602 : 1590052 : as = sym->as;
17603 : : }
17604 : :
17605 : : /* F2008, C530. */
17606 : 1608501 : if (sym->attr.contiguous
17607 : 7007 : && (!class_attr.dimension
17608 : 7004 : || (as->type != AS_ASSUMED_SHAPE && as->type != AS_ASSUMED_RANK
17609 : 125 : && !class_attr.pointer)))
17610 : : {
17611 : 7 : gfc_error ("%qs at %L has the CONTIGUOUS attribute but is not an "
17612 : : "array pointer or an assumed-shape or assumed-rank array",
17613 : : sym->name, &sym->declared_at);
17614 : 7 : return;
17615 : : }
17616 : :
17617 : : /* Assumed size arrays and assumed shape arrays must be dummy
17618 : : arguments. Array-spec's of implied-shape should have been resolved to
17619 : : AS_EXPLICIT already. */
17620 : :
17621 : 1601615 : if (as)
17622 : : {
17623 : : /* If AS_IMPLIED_SHAPE makes it to here, it must be a bad
17624 : : specification expression. */
17625 : 140708 : if (as->type == AS_IMPLIED_SHAPE)
17626 : : {
17627 : : int i;
17628 : 1 : for (i=0; i<as->rank; i++)
17629 : : {
17630 : 1 : if (as->lower[i] != NULL && as->upper[i] == NULL)
17631 : : {
17632 : 1 : gfc_error ("Bad specification for assumed size array at %L",
17633 : : &as->lower[i]->where);
17634 : 1 : return;
17635 : : }
17636 : : }
17637 : 0 : gcc_unreachable();
17638 : : }
17639 : :
17640 : 140707 : if (((as->type == AS_ASSUMED_SIZE && !as->cp_was_assumed)
17641 : 109801 : || as->type == AS_ASSUMED_SHAPE)
17642 : 42130 : && !sym->attr.dummy && !sym->attr.select_type_temporary
17643 : 8 : && !sym->attr.associate_var)
17644 : : {
17645 : 7 : if (as->type == AS_ASSUMED_SIZE)
17646 : 7 : gfc_error ("Assumed size array at %L must be a dummy argument",
17647 : : &sym->declared_at);
17648 : : else
17649 : 0 : gfc_error ("Assumed shape array at %L must be a dummy argument",
17650 : : &sym->declared_at);
17651 : 7 : return;
17652 : : }
17653 : : /* TS 29113, C535a. */
17654 : 140700 : if (as->type == AS_ASSUMED_RANK && !sym->attr.dummy
17655 : 60 : && !sym->attr.select_type_temporary
17656 : 60 : && !(cs_base && cs_base->current
17657 : 45 : && (cs_base->current->op == EXEC_SELECT_RANK
17658 : 3 : || ((gfc_option.allow_std & GFC_STD_F202Y)
17659 : 0 : && cs_base->current->op == EXEC_BLOCK))))
17660 : : {
17661 : 18 : gfc_error ("Assumed-rank array at %L must be a dummy argument",
17662 : : &sym->declared_at);
17663 : 18 : return;
17664 : : }
17665 : 140682 : if (as->type == AS_ASSUMED_RANK
17666 : 25510 : && (sym->attr.codimension || sym->attr.value))
17667 : : {
17668 : 2 : gfc_error ("Assumed-rank array at %L may not have the VALUE or "
17669 : : "CODIMENSION attribute", &sym->declared_at);
17670 : 2 : return;
17671 : : }
17672 : : }
17673 : :
17674 : : /* Make sure symbols with known intent or optional are really dummy
17675 : : variable. Because of ENTRY statement, this has to be deferred
17676 : : until resolution time. */
17677 : :
17678 : 1608466 : if (!sym->attr.dummy
17679 : 1156590 : && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
17680 : : {
17681 : 2 : gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
17682 : 2 : return;
17683 : : }
17684 : :
17685 : 1608464 : if (sym->attr.value && !sym->attr.dummy)
17686 : : {
17687 : 2 : gfc_error ("%qs at %L cannot have the VALUE attribute because "
17688 : : "it is not a dummy argument", sym->name, &sym->declared_at);
17689 : 2 : return;
17690 : : }
17691 : :
17692 : 1608462 : if (sym->attr.value && sym->ts.type == BT_CHARACTER)
17693 : : {
17694 : 546 : gfc_charlen *cl = sym->ts.u.cl;
17695 : 546 : if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
17696 : : {
17697 : 2 : gfc_error ("Character dummy variable %qs at %L with VALUE "
17698 : : "attribute must have constant length",
17699 : : sym->name, &sym->declared_at);
17700 : 2 : return;
17701 : : }
17702 : :
17703 : 544 : if (sym->ts.is_c_interop
17704 : 376 : && mpz_cmp_si (cl->length->value.integer, 1) != 0)
17705 : : {
17706 : 1 : gfc_error ("C interoperable character dummy variable %qs at %L "
17707 : : "with VALUE attribute must have length one",
17708 : : sym->name, &sym->declared_at);
17709 : 1 : return;
17710 : : }
17711 : : }
17712 : :
17713 : 1608459 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17714 : 118634 : && sym->ts.u.derived->attr.generic)
17715 : : {
17716 : 20 : sym->ts.u.derived = gfc_find_dt_in_generic (sym->ts.u.derived);
17717 : 20 : if (!sym->ts.u.derived)
17718 : : {
17719 : 0 : gfc_error ("The derived type %qs at %L is of type %qs, "
17720 : : "which has not been defined", sym->name,
17721 : : &sym->declared_at, sym->ts.u.derived->name);
17722 : 0 : sym->ts.type = BT_UNKNOWN;
17723 : 0 : return;
17724 : : }
17725 : : }
17726 : :
17727 : : /* Use the same constraints as TYPE(*), except for the type check
17728 : : and that only scalars and assumed-size arrays are permitted. */
17729 : 1608459 : if (sym->attr.ext_attr & (1 << EXT_ATTR_NO_ARG_CHECK))
17730 : : {
17731 : 11570 : if (!sym->attr.dummy)
17732 : : {
17733 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17734 : : "a dummy argument", sym->name, &sym->declared_at);
17735 : 1 : return;
17736 : : }
17737 : :
17738 : 11569 : if (sym->ts.type != BT_ASSUMED && sym->ts.type != BT_INTEGER
17739 : 8 : && sym->ts.type != BT_REAL && sym->ts.type != BT_LOGICAL
17740 : 0 : && sym->ts.type != BT_COMPLEX)
17741 : : {
17742 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall be "
17743 : : "of type TYPE(*) or of an numeric intrinsic type",
17744 : : sym->name, &sym->declared_at);
17745 : 0 : return;
17746 : : }
17747 : :
17748 : 11569 : if (sym->attr.allocatable || sym->attr.codimension
17749 : 11567 : || sym->attr.pointer || sym->attr.value)
17750 : : {
17751 : 4 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17752 : : "have the ALLOCATABLE, CODIMENSION, POINTER or VALUE "
17753 : : "attribute", sym->name, &sym->declared_at);
17754 : 4 : return;
17755 : : }
17756 : :
17757 : 11565 : if (sym->attr.intent == INTENT_OUT)
17758 : : {
17759 : 0 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute may not "
17760 : : "have the INTENT(OUT) attribute",
17761 : : sym->name, &sym->declared_at);
17762 : 0 : return;
17763 : : }
17764 : 11565 : if (sym->attr.dimension && sym->as->type != AS_ASSUMED_SIZE)
17765 : : {
17766 : 1 : gfc_error ("Variable %s at %L with NO_ARG_CHECK attribute shall "
17767 : : "either be a scalar or an assumed-size array",
17768 : : sym->name, &sym->declared_at);
17769 : 1 : return;
17770 : : }
17771 : :
17772 : : /* Set the type to TYPE(*) and add a dimension(*) to ensure
17773 : : NO_ARG_CHECK is correctly handled in trans*.c, e.g. with
17774 : : packing. */
17775 : 11564 : sym->ts.type = BT_ASSUMED;
17776 : 11564 : sym->as = gfc_get_array_spec ();
17777 : 11564 : sym->as->type = AS_ASSUMED_SIZE;
17778 : 11564 : sym->as->rank = 1;
17779 : 11564 : sym->as->lower[0] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
17780 : : }
17781 : 1596889 : else if (sym->ts.type == BT_ASSUMED)
17782 : : {
17783 : : /* TS 29113, C407a. */
17784 : 10314 : if (!sym->attr.dummy)
17785 : : {
17786 : 7 : gfc_error ("Assumed type of variable %s at %L is only permitted "
17787 : : "for dummy variables", sym->name, &sym->declared_at);
17788 : 7 : return;
17789 : : }
17790 : 10307 : if (sym->attr.allocatable || sym->attr.codimension
17791 : 10303 : || sym->attr.pointer || sym->attr.value)
17792 : : {
17793 : 8 : gfc_error ("Assumed-type variable %s at %L may not have the "
17794 : : "ALLOCATABLE, CODIMENSION, POINTER or VALUE attribute",
17795 : : sym->name, &sym->declared_at);
17796 : 8 : return;
17797 : : }
17798 : 10299 : if (sym->attr.intent == INTENT_OUT)
17799 : : {
17800 : 2 : gfc_error ("Assumed-type variable %s at %L may not have the "
17801 : : "INTENT(OUT) attribute",
17802 : : sym->name, &sym->declared_at);
17803 : 2 : return;
17804 : : }
17805 : 10297 : if (sym->attr.dimension && sym->as->type == AS_EXPLICIT)
17806 : : {
17807 : 3 : gfc_error ("Assumed-type variable %s at %L shall not be an "
17808 : : "explicit-shape array", sym->name, &sym->declared_at);
17809 : 3 : return;
17810 : : }
17811 : : }
17812 : :
17813 : : /* If the symbol is marked as bind(c), that it is declared at module level
17814 : : scope and verify its type and kind. Do not do the latter for symbols
17815 : : that are implicitly typed because that is handled in
17816 : : gfc_set_default_type. Handle dummy arguments and procedure definitions
17817 : : separately. Also, anything that is use associated is not handled here
17818 : : but instead is handled in the module it is declared in. Finally, derived
17819 : : type definitions are allowed to be BIND(C) since that only implies that
17820 : : they're interoperable, and they are checked fully for interoperability
17821 : : when a variable is declared of that type. */
17822 : 1608433 : if (sym->attr.is_bind_c && sym->attr.use_assoc == 0
17823 : 7136 : && sym->attr.dummy == 0 && sym->attr.flavor != FL_PROCEDURE
17824 : 567 : && sym->attr.flavor != FL_DERIVED)
17825 : : {
17826 : 167 : bool t = true;
17827 : :
17828 : : /* First, make sure the variable is declared at the
17829 : : module-level scope (J3/04-007, Section 15.3). */
17830 : 167 : if (!(sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE)
17831 : 7 : && !sym->attr.in_common)
17832 : : {
17833 : 6 : gfc_error ("Variable %qs at %L cannot be BIND(C) because it "
17834 : : "is neither a COMMON block nor declared at the "
17835 : : "module level scope", sym->name, &(sym->declared_at));
17836 : 6 : t = false;
17837 : : }
17838 : 161 : else if (sym->ts.type == BT_CHARACTER
17839 : 161 : && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
17840 : 1 : || !gfc_is_constant_expr (sym->ts.u.cl->length)
17841 : 1 : || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
17842 : : {
17843 : 1 : gfc_error ("BIND(C) Variable %qs at %L must have length one",
17844 : 1 : sym->name, &sym->declared_at);
17845 : 1 : t = false;
17846 : : }
17847 : 160 : else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
17848 : : {
17849 : 1 : t = verify_com_block_vars_c_interop (sym->common_head);
17850 : : }
17851 : 159 : else if (sym->attr.implicit_type == 0)
17852 : : {
17853 : : /* If type() declaration, we need to verify that the components
17854 : : of the given type are all C interoperable, etc. */
17855 : 157 : if (sym->ts.type == BT_DERIVED &&
17856 : 24 : sym->ts.u.derived->attr.is_c_interop != 1)
17857 : : {
17858 : : /* Make sure the user marked the derived type as BIND(C). If
17859 : : not, call the verify routine. This could print an error
17860 : : for the derived type more than once if multiple variables
17861 : : of that type are declared. */
17862 : 14 : if (sym->ts.u.derived->attr.is_bind_c != 1)
17863 : 1 : verify_bind_c_derived_type (sym->ts.u.derived);
17864 : 157 : t = false;
17865 : : }
17866 : :
17867 : : /* Verify the variable itself as C interoperable if it
17868 : : is BIND(C). It is not possible for this to succeed if
17869 : : the verify_bind_c_derived_type failed, so don't have to handle
17870 : : any error returned by verify_bind_c_derived_type. */
17871 : 157 : t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
17872 : 157 : sym->common_block);
17873 : : }
17874 : :
17875 : 165 : if (!t)
17876 : : {
17877 : : /* clear the is_bind_c flag to prevent reporting errors more than
17878 : : once if something failed. */
17879 : 10 : sym->attr.is_bind_c = 0;
17880 : 10 : return;
17881 : : }
17882 : : }
17883 : :
17884 : : /* If a derived type symbol has reached this point, without its
17885 : : type being declared, we have an error. Notice that most
17886 : : conditions that produce undefined derived types have already
17887 : : been dealt with. However, the likes of:
17888 : : implicit type(t) (t) ..... call foo (t) will get us here if
17889 : : the type is not declared in the scope of the implicit
17890 : : statement. Change the type to BT_UNKNOWN, both because it is so
17891 : : and to prevent an ICE. */
17892 : 1608423 : if (sym->ts.type == BT_DERIVED && !sym->attr.is_iso_c
17893 : 118632 : && sym->ts.u.derived->components == NULL
17894 : 1081 : && !sym->ts.u.derived->attr.zero_comp)
17895 : : {
17896 : 3 : gfc_error ("The derived type %qs at %L is of type %qs, "
17897 : : "which has not been defined", sym->name,
17898 : : &sym->declared_at, sym->ts.u.derived->name);
17899 : 3 : sym->ts.type = BT_UNKNOWN;
17900 : 3 : return;
17901 : : }
17902 : :
17903 : : /* Make sure that the derived type has been resolved and that the
17904 : : derived type is visible in the symbol's namespace, if it is a
17905 : : module function and is not PRIVATE. */
17906 : 1608420 : if (sym->ts.type == BT_DERIVED
17907 : 125483 : && sym->ts.u.derived->attr.use_assoc
17908 : 109493 : && sym->ns->proc_name
17909 : 109485 : && sym->ns->proc_name->attr.flavor == FL_MODULE
17910 : 1614210 : && !resolve_fl_derived (sym->ts.u.derived))
17911 : : return;
17912 : :
17913 : : /* Unless the derived-type declaration is use associated, Fortran 95
17914 : : does not allow public entries of private derived types.
17915 : : See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
17916 : : 161 in 95-006r3. */
17917 : 1608420 : if (sym->ts.type == BT_DERIVED
17918 : 125483 : && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
17919 : 7517 : && !sym->ts.u.derived->attr.use_assoc
17920 : 1727 : && gfc_check_symbol_access (sym)
17921 : 1548 : && !gfc_check_symbol_access (sym->ts.u.derived)
17922 : 1608433 : && !gfc_notify_std (GFC_STD_F2003, "PUBLIC %s %qs at %L of PRIVATE "
17923 : : "derived type %qs",
17924 : 13 : (sym->attr.flavor == FL_PARAMETER)
17925 : : ? "parameter" : "variable",
17926 : : sym->name, &sym->declared_at,
17927 : 13 : sym->ts.u.derived->name))
17928 : : return;
17929 : :
17930 : : /* F2008, C1302. */
17931 : 1608413 : if (sym->ts.type == BT_DERIVED
17932 : 125476 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17933 : 118 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
17934 : 125452 : || sym->ts.u.derived->attr.lock_comp)
17935 : 37 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17936 : : {
17937 : 4 : gfc_error ("Variable %s at %L of type LOCK_TYPE or with subcomponent of "
17938 : : "type LOCK_TYPE must be a coarray", sym->name,
17939 : : &sym->declared_at);
17940 : 4 : return;
17941 : : }
17942 : :
17943 : : /* TS18508, C702/C703. */
17944 : 1608409 : if (sym->ts.type == BT_DERIVED
17945 : 125472 : && ((sym->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
17946 : 117 : && sym->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
17947 : 125462 : || sym->ts.u.derived->attr.event_comp)
17948 : 10 : && !sym->attr.codimension && !sym->ts.u.derived->attr.coarray_comp)
17949 : : {
17950 : 1 : gfc_error ("Variable %s at %L of type EVENT_TYPE or with subcomponent of "
17951 : : "type EVENT_TYPE must be a coarray", sym->name,
17952 : : &sym->declared_at);
17953 : 1 : return;
17954 : : }
17955 : :
17956 : : /* An assumed-size array with INTENT(OUT) shall not be of a type for which
17957 : : default initialization is defined (5.1.2.4.4). */
17958 : 1608408 : if (sym->ts.type == BT_DERIVED
17959 : 125471 : && sym->attr.dummy
17960 : 43612 : && sym->attr.intent == INTENT_OUT
17961 : 2348 : && sym->as
17962 : 381 : && sym->as->type == AS_ASSUMED_SIZE)
17963 : : {
17964 : 1 : for (c = sym->ts.u.derived->components; c; c = c->next)
17965 : : {
17966 : 1 : if (c->initializer)
17967 : : {
17968 : 1 : gfc_error ("The INTENT(OUT) dummy argument %qs at %L is "
17969 : : "ASSUMED SIZE and so cannot have a default initializer",
17970 : : sym->name, &sym->declared_at);
17971 : 1 : return;
17972 : : }
17973 : : }
17974 : : }
17975 : :
17976 : : /* F2008, C542. */
17977 : 1608407 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17978 : 43611 : && sym->attr.intent == INTENT_OUT && sym->attr.lock_comp)
17979 : : {
17980 : 0 : gfc_error ("Dummy argument %qs at %L of LOCK_TYPE shall not be "
17981 : : "INTENT(OUT)", sym->name, &sym->declared_at);
17982 : 0 : return;
17983 : : }
17984 : :
17985 : : /* TS18508. */
17986 : 1608407 : if (sym->ts.type == BT_DERIVED && sym->attr.dummy
17987 : 43611 : && sym->attr.intent == INTENT_OUT && sym->attr.event_comp)
17988 : : {
17989 : 0 : gfc_error ("Dummy argument %qs at %L of EVENT_TYPE shall not be "
17990 : : "INTENT(OUT)", sym->name, &sym->declared_at);
17991 : 0 : return;
17992 : : }
17993 : :
17994 : : /* F2008, C525. */
17995 : 1608407 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
17996 : 1608318 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
17997 : 18453 : && sym->ts.u.derived && CLASS_DATA (sym)
17998 : 18447 : && CLASS_DATA (sym)->attr.coarray_comp))
17999 : 1608318 : || class_attr.codimension)
18000 : 1544 : && (sym->attr.result || sym->result == sym))
18001 : : {
18002 : 8 : gfc_error ("Function result %qs at %L shall not be a coarray or have "
18003 : : "a coarray component", sym->name, &sym->declared_at);
18004 : 8 : return;
18005 : : }
18006 : :
18007 : : /* F2008, C524. */
18008 : 1608399 : if (sym->attr.codimension && sym->ts.type == BT_DERIVED
18009 : 370 : && sym->ts.u.derived->ts.is_iso_c)
18010 : : {
18011 : 3 : gfc_error ("Variable %qs at %L of TYPE(C_PTR) or TYPE(C_FUNPTR) "
18012 : : "shall not be a coarray", sym->name, &sym->declared_at);
18013 : 3 : return;
18014 : : }
18015 : :
18016 : : /* F2008, C525. */
18017 : 1608396 : if (((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18018 : 1608310 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18019 : 18452 : && sym->ts.u.derived && CLASS_DATA (sym)
18020 : 18446 : && CLASS_DATA (sym)->attr.coarray_comp))
18021 : 86 : && (class_attr.codimension || class_attr.pointer || class_attr.dimension
18022 : 82 : || class_attr.allocatable))
18023 : : {
18024 : 4 : gfc_error ("Variable %qs at %L with coarray component shall be a "
18025 : : "nonpointer, nonallocatable scalar, which is not a coarray",
18026 : : sym->name, &sym->declared_at);
18027 : 4 : return;
18028 : : }
18029 : :
18030 : : /* F2008, C526. The function-result case was handled above. */
18031 : 1608392 : if (class_attr.codimension
18032 : 1447 : && !(class_attr.allocatable || sym->attr.dummy || sym->attr.save
18033 : 296 : || sym->attr.select_type_temporary
18034 : 225 : || sym->attr.associate_var
18035 : 212 : || (sym->ns->save_all && !sym->attr.automatic)
18036 : 212 : || sym->ns->proc_name->attr.flavor == FL_MODULE
18037 : 212 : || sym->ns->proc_name->attr.is_main_program
18038 : 5 : || sym->attr.function || sym->attr.result || sym->attr.use_assoc))
18039 : : {
18040 : 4 : gfc_error ("Variable %qs at %L is a coarray and is not ALLOCATABLE, SAVE "
18041 : : "nor a dummy argument", sym->name, &sym->declared_at);
18042 : 4 : return;
18043 : : }
18044 : : /* F2008, C528. */
18045 : 1608388 : else if (class_attr.codimension && !sym->attr.select_type_temporary
18046 : 1372 : && !class_attr.allocatable && as && as->cotype == AS_DEFERRED)
18047 : : {
18048 : 6 : gfc_error ("Coarray variable %qs at %L shall not have codimensions with "
18049 : : "deferred shape without allocatable", sym->name,
18050 : : &sym->declared_at);
18051 : 6 : return;
18052 : : }
18053 : 1608382 : else if (class_attr.codimension && class_attr.allocatable && as
18054 : 523 : && (as->cotype != AS_DEFERRED || as->type != AS_DEFERRED))
18055 : : {
18056 : 9 : gfc_error ("Allocatable coarray variable %qs at %L must have "
18057 : : "deferred shape", sym->name, &sym->declared_at);
18058 : 9 : return;
18059 : : }
18060 : :
18061 : : /* F2008, C541. */
18062 : 1608373 : if ((((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.coarray_comp)
18063 : 1608291 : || (sym->ts.type == BT_CLASS && sym->attr.class_ok
18064 : 18447 : && sym->ts.u.derived && CLASS_DATA (sym)
18065 : 18441 : && CLASS_DATA (sym)->attr.coarray_comp))
18066 : 1608291 : || (class_attr.codimension && class_attr.allocatable))
18067 : 596 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT)
18068 : : {
18069 : 3 : gfc_error ("Variable %qs at %L is INTENT(OUT) and can thus not be an "
18070 : : "allocatable coarray or have coarray components",
18071 : : sym->name, &sym->declared_at);
18072 : 3 : return;
18073 : : }
18074 : :
18075 : 1608370 : if (class_attr.codimension && sym->attr.dummy
18076 : 425 : && sym->ns->proc_name && sym->ns->proc_name->attr.is_bind_c)
18077 : : {
18078 : 2 : gfc_error ("Coarray dummy variable %qs at %L not allowed in BIND(C) "
18079 : : "procedure %qs", sym->name, &sym->declared_at,
18080 : : sym->ns->proc_name->name);
18081 : 2 : return;
18082 : : }
18083 : :
18084 : 1608368 : if (sym->ts.type == BT_LOGICAL
18085 : 111198 : && ((sym->attr.function && sym->attr.is_bind_c && sym->result == sym)
18086 : 111195 : || ((sym->attr.dummy || sym->attr.result) && sym->ns->proc_name
18087 : 30765 : && sym->ns->proc_name->attr.is_bind_c)))
18088 : : {
18089 : : int i;
18090 : 200 : for (i = 0; gfc_logical_kinds[i].kind; i++)
18091 : 200 : if (gfc_logical_kinds[i].kind == sym->ts.kind)
18092 : : break;
18093 : 16 : if (!gfc_logical_kinds[i].c_bool && sym->attr.dummy
18094 : 181 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL dummy argument %qs at "
18095 : : "%L with non-C_Bool kind in BIND(C) procedure "
18096 : : "%qs", sym->name, &sym->declared_at,
18097 : 13 : sym->ns->proc_name->name))
18098 : : return;
18099 : 167 : else if (!gfc_logical_kinds[i].c_bool
18100 : 182 : && !gfc_notify_std (GFC_STD_GNU, "LOGICAL result variable "
18101 : : "%qs at %L with non-C_Bool kind in "
18102 : : "BIND(C) procedure %qs", sym->name,
18103 : : &sym->declared_at,
18104 : 15 : sym->attr.function ? sym->name
18105 : 13 : : sym->ns->proc_name->name))
18106 : : return;
18107 : : }
18108 : :
18109 : 1608365 : switch (sym->attr.flavor)
18110 : : {
18111 : 628983 : case FL_VARIABLE:
18112 : 628983 : if (!resolve_fl_variable (sym, mp_flag))
18113 : : return;
18114 : : break;
18115 : :
18116 : 459690 : case FL_PROCEDURE:
18117 : 459690 : if (sym->formal && !sym->formal_ns)
18118 : : {
18119 : : /* Check that none of the arguments are a namelist. */
18120 : : gfc_formal_arglist *formal = sym->formal;
18121 : :
18122 : 102513 : for (; formal; formal = formal->next)
18123 : 69718 : if (formal->sym && formal->sym->attr.flavor == FL_NAMELIST)
18124 : : {
18125 : 1 : gfc_error ("Namelist %qs cannot be an argument to "
18126 : : "subroutine or function at %L",
18127 : : formal->sym->name, &sym->declared_at);
18128 : 1 : return;
18129 : : }
18130 : : }
18131 : :
18132 : 459689 : if (!resolve_fl_procedure (sym, mp_flag))
18133 : : return;
18134 : : break;
18135 : :
18136 : 805 : case FL_NAMELIST:
18137 : 805 : if (!resolve_fl_namelist (sym))
18138 : : return;
18139 : : break;
18140 : :
18141 : 370272 : case FL_PARAMETER:
18142 : 370272 : if (!resolve_fl_parameter (sym))
18143 : : return;
18144 : : break;
18145 : :
18146 : : default:
18147 : : break;
18148 : : }
18149 : :
18150 : : /* Resolve array specifier. Check as well some constraints
18151 : : on COMMON blocks. */
18152 : :
18153 : 1608176 : check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;
18154 : :
18155 : 1608176 : resolve_symbol_array_spec (sym, check_constant);
18156 : :
18157 : : /* Resolve formal namespaces. */
18158 : 1608176 : if (sym->formal_ns && sym->formal_ns != gfc_current_ns
18159 : 251646 : && !sym->attr.contained && !sym->attr.intrinsic)
18160 : 227648 : gfc_resolve (sym->formal_ns);
18161 : :
18162 : : /* Make sure the formal namespace is present. */
18163 : 1608176 : if (sym->formal && !sym->formal_ns)
18164 : : {
18165 : : gfc_formal_arglist *formal = sym->formal;
18166 : 32996 : while (formal && !formal->sym)
18167 : 11 : formal = formal->next;
18168 : :
18169 : 32985 : if (formal)
18170 : : {
18171 : 32974 : sym->formal_ns = formal->sym->ns;
18172 : 32974 : if (sym->formal_ns && sym->ns != formal->sym->ns)
18173 : 24766 : sym->formal_ns->refs++;
18174 : : }
18175 : : }
18176 : :
18177 : : /* Check threadprivate restrictions. */
18178 : 1608176 : if (sym->attr.threadprivate
18179 : 331 : && !(sym->attr.save || sym->attr.data || sym->attr.in_common)
18180 : 32 : && !(sym->ns->save_all && !sym->attr.automatic)
18181 : 31 : && sym->module == NULL
18182 : 16 : && (sym->ns->proc_name == NULL
18183 : 16 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18184 : 3 : && !sym->ns->proc_name->attr.is_main_program)))
18185 : 1 : gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
18186 : :
18187 : : /* Check omp declare target restrictions. */
18188 : 1608176 : if (sym->attr.omp_declare_target
18189 : 1443 : && sym->attr.flavor == FL_VARIABLE
18190 : 604 : && !sym->attr.save
18191 : 195 : && !(sym->ns->save_all && !sym->attr.automatic)
18192 : 195 : && (!sym->attr.in_common
18193 : 182 : && sym->module == NULL
18194 : 92 : && (sym->ns->proc_name == NULL
18195 : 92 : || (sym->ns->proc_name->attr.flavor != FL_MODULE
18196 : 2 : && !sym->ns->proc_name->attr.is_main_program))))
18197 : 1 : gfc_error ("!$OMP DECLARE TARGET variable %qs at %L isn't SAVEd",
18198 : : sym->name, &sym->declared_at);
18199 : :
18200 : : /* If we have come this far we can apply default-initializers, as
18201 : : described in 14.7.5, to those variables that have not already
18202 : : been assigned one. */
18203 : 1608176 : if (sym->ts.type == BT_DERIVED
18204 : 125443 : && !sym->value
18205 : 101324 : && !sym->attr.allocatable
18206 : 98547 : && !sym->attr.alloc_comp)
18207 : : {
18208 : 98547 : symbol_attribute *a = &sym->attr;
18209 : :
18210 : 98547 : if ((!a->save && !a->dummy && !a->pointer
18211 : 53699 : && !a->in_common && !a->use_assoc
18212 : 9496 : && a->referenced
18213 : 7414 : && !((a->function || a->result)
18214 : 1386 : && (!a->dimension
18215 : 124 : || sym->ts.u.derived->attr.alloc_comp
18216 : 83 : || sym->ts.u.derived->attr.pointer_comp))
18217 : 6097 : && !(a->function && sym != sym->result))
18218 : 92470 : || (a->dummy && !a->pointer && a->intent == INTENT_OUT
18219 : 1522 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY))
18220 : 7500 : apply_default_init (sym);
18221 : 91047 : else if (a->function && !a->pointer && !a->allocatable && !a->use_assoc
18222 : 2501 : && sym->result)
18223 : : /* Default initialization for function results. */
18224 : 2497 : apply_default_init (sym->result);
18225 : 88550 : else if (a->function && sym->result && a->access != ACCESS_PRIVATE
18226 : 11115 : && (sym->ts.u.derived->attr.alloc_comp
18227 : 10716 : || sym->ts.u.derived->attr.pointer_comp))
18228 : : /* Mark the result symbol to be referenced, when it has allocatable
18229 : : components. */
18230 : 431 : sym->result->attr.referenced = 1;
18231 : : }
18232 : :
18233 : 1608176 : if (sym->ts.type == BT_CLASS && sym->ns == gfc_current_ns
18234 : 17957 : && sym->attr.dummy && sym->attr.intent == INTENT_OUT
18235 : 1180 : && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY
18236 : 1105 : && !CLASS_DATA (sym)->attr.class_pointer
18237 : 1079 : && !CLASS_DATA (sym)->attr.allocatable)
18238 : 807 : apply_default_init (sym);
18239 : :
18240 : : /* If this symbol has a type-spec, check it. */
18241 : 1608176 : if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
18242 : 609029 : || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
18243 : 1308895 : if (!resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name))
18244 : : return;
18245 : :
18246 : 1608173 : if (sym->param_list)
18247 : 522 : resolve_pdt (sym);
18248 : : }
18249 : :
18250 : :
18251 : : /************* Resolve DATA statements *************/
18252 : :
18253 : : static struct
18254 : : {
18255 : : gfc_data_value *vnode;
18256 : : mpz_t left;
18257 : : }
18258 : : values;
18259 : :
18260 : :
18261 : : /* Advance the values structure to point to the next value in the data list. */
18262 : :
18263 : : static bool
18264 : 10856 : next_data_value (void)
18265 : : {
18266 : 16622 : while (mpz_cmp_ui (values.left, 0) == 0)
18267 : : {
18268 : :
18269 : 8179 : if (values.vnode->next == NULL)
18270 : : return false;
18271 : :
18272 : 5766 : values.vnode = values.vnode->next;
18273 : 5766 : mpz_set (values.left, values.vnode->repeat);
18274 : : }
18275 : :
18276 : : return true;
18277 : : }
18278 : :
18279 : :
18280 : : static bool
18281 : 3537 : check_data_variable (gfc_data_variable *var, locus *where)
18282 : : {
18283 : 3537 : gfc_expr *e;
18284 : 3537 : mpz_t size;
18285 : 3537 : mpz_t offset;
18286 : 3537 : bool t;
18287 : 3537 : ar_type mark = AR_UNKNOWN;
18288 : 3537 : int i;
18289 : 3537 : mpz_t section_index[GFC_MAX_DIMENSIONS];
18290 : 3537 : int vector_offset[GFC_MAX_DIMENSIONS];
18291 : 3537 : gfc_ref *ref;
18292 : 3537 : gfc_array_ref *ar;
18293 : 3537 : gfc_symbol *sym;
18294 : 3537 : int has_pointer;
18295 : :
18296 : 3537 : if (!gfc_resolve_expr (var->expr))
18297 : : return false;
18298 : :
18299 : 3537 : ar = NULL;
18300 : 3537 : e = var->expr;
18301 : :
18302 : 3537 : if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
18303 : 0 : && e->value.function.isym->id == GFC_ISYM_CAF_GET)
18304 : 0 : e = e->value.function.actual->expr;
18305 : :
18306 : 3537 : if (e->expr_type != EXPR_VARIABLE)
18307 : : {
18308 : 0 : gfc_error ("Expecting definable entity near %L", where);
18309 : 0 : return false;
18310 : : }
18311 : :
18312 : 3537 : sym = e->symtree->n.sym;
18313 : :
18314 : 3537 : if (sym->ns->is_block_data && !sym->attr.in_common)
18315 : : {
18316 : 2 : gfc_error ("BLOCK DATA element %qs at %L must be in COMMON",
18317 : : sym->name, &sym->declared_at);
18318 : 2 : return false;
18319 : : }
18320 : :
18321 : 3535 : if (e->ref == NULL && sym->as)
18322 : : {
18323 : 1 : gfc_error ("DATA array %qs at %L must be specified in a previous"
18324 : : " declaration", sym->name, where);
18325 : 1 : return false;
18326 : : }
18327 : :
18328 : 3534 : if (gfc_is_coindexed (e))
18329 : : {
18330 : 5 : gfc_error ("DATA element %qs at %L cannot have a coindex", sym->name,
18331 : : where);
18332 : 5 : return false;
18333 : : }
18334 : :
18335 : 3529 : has_pointer = sym->attr.pointer;
18336 : :
18337 : 5968 : for (ref = e->ref; ref; ref = ref->next)
18338 : : {
18339 : 2443 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
18340 : : has_pointer = 1;
18341 : :
18342 : 2417 : if (has_pointer)
18343 : : {
18344 : 29 : if (ref->type == REF_ARRAY && ref->u.ar.type != AR_FULL)
18345 : : {
18346 : 1 : gfc_error ("DATA element %qs at %L is a pointer and so must "
18347 : : "be a full array", sym->name, where);
18348 : 1 : return false;
18349 : : }
18350 : :
18351 : 28 : if (values.vnode->expr->expr_type == EXPR_CONSTANT)
18352 : : {
18353 : 1 : gfc_error ("DATA object near %L has the pointer attribute "
18354 : : "and the corresponding DATA value is not a valid "
18355 : : "initial-data-target", where);
18356 : 1 : return false;
18357 : : }
18358 : : }
18359 : :
18360 : 2441 : if (ref->type == REF_COMPONENT && ref->u.c.component->attr.allocatable)
18361 : : {
18362 : 1 : gfc_error ("DATA element %qs at %L cannot have the ALLOCATABLE "
18363 : : "attribute", ref->u.c.component->name, &e->where);
18364 : 1 : return false;
18365 : : }
18366 : :
18367 : : /* Reject substrings of strings of non-constant length. */
18368 : 2440 : if (ref->type == REF_SUBSTRING
18369 : 73 : && ref->u.ss.length
18370 : 73 : && ref->u.ss.length->length
18371 : 2513 : && !gfc_is_constant_expr (ref->u.ss.length->length))
18372 : 1 : goto bad_charlen;
18373 : : }
18374 : :
18375 : : /* Reject strings with deferred length or non-constant length. */
18376 : 3525 : if (e->ts.type == BT_CHARACTER
18377 : 3525 : && (e->ts.deferred
18378 : 374 : || (e->ts.u.cl->length
18379 : 323 : && !gfc_is_constant_expr (e->ts.u.cl->length))))
18380 : 5 : goto bad_charlen;
18381 : :
18382 : 3520 : mpz_init_set_si (offset, 0);
18383 : :
18384 : 3520 : if (e->rank == 0 || has_pointer)
18385 : : {
18386 : 2675 : mpz_init_set_ui (size, 1);
18387 : 2675 : ref = NULL;
18388 : : }
18389 : : else
18390 : : {
18391 : 845 : ref = e->ref;
18392 : :
18393 : : /* Find the array section reference. */
18394 : 1028 : for (ref = e->ref; ref; ref = ref->next)
18395 : : {
18396 : 1028 : if (ref->type != REF_ARRAY)
18397 : 92 : continue;
18398 : 936 : if (ref->u.ar.type == AR_ELEMENT)
18399 : 91 : continue;
18400 : : break;
18401 : : }
18402 : 845 : gcc_assert (ref);
18403 : :
18404 : : /* Set marks according to the reference pattern. */
18405 : 845 : switch (ref->u.ar.type)
18406 : : {
18407 : : case AR_FULL:
18408 : : mark = AR_FULL;
18409 : : break;
18410 : :
18411 : 149 : case AR_SECTION:
18412 : 149 : ar = &ref->u.ar;
18413 : : /* Get the start position of array section. */
18414 : 149 : gfc_get_section_index (ar, section_index, &offset, vector_offset);
18415 : 149 : mark = AR_SECTION;
18416 : 149 : break;
18417 : :
18418 : 0 : default:
18419 : 0 : gcc_unreachable ();
18420 : : }
18421 : :
18422 : 845 : if (!gfc_array_size (e, &size))
18423 : : {
18424 : 1 : gfc_error ("Nonconstant array section at %L in DATA statement",
18425 : : where);
18426 : 1 : mpz_clear (offset);
18427 : 1 : return false;
18428 : : }
18429 : : }
18430 : :
18431 : 3519 : t = true;
18432 : :
18433 : 11900 : while (mpz_cmp_ui (size, 0) > 0)
18434 : : {
18435 : 8444 : if (!next_data_value ())
18436 : : {
18437 : 1 : gfc_error ("DATA statement at %L has more variables than values",
18438 : : where);
18439 : 1 : t = false;
18440 : 1 : break;
18441 : : }
18442 : :
18443 : 8443 : t = gfc_check_assign (var->expr, values.vnode->expr, 0);
18444 : 8443 : if (!t)
18445 : : break;
18446 : :
18447 : : /* If we have more than one element left in the repeat count,
18448 : : and we have more than one element left in the target variable,
18449 : : then create a range assignment. */
18450 : : /* FIXME: Only done for full arrays for now, since array sections
18451 : : seem tricky. */
18452 : 8424 : if (mark == AR_FULL && ref && ref->next == NULL
18453 : 5364 : && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
18454 : : {
18455 : 137 : mpz_t range;
18456 : :
18457 : 137 : if (mpz_cmp (size, values.left) >= 0)
18458 : : {
18459 : 126 : mpz_init_set (range, values.left);
18460 : 126 : mpz_sub (size, size, values.left);
18461 : 126 : mpz_set_ui (values.left, 0);
18462 : : }
18463 : : else
18464 : : {
18465 : 11 : mpz_init_set (range, size);
18466 : 11 : mpz_sub (values.left, values.left, size);
18467 : 11 : mpz_set_ui (size, 0);
18468 : : }
18469 : :
18470 : 137 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18471 : : offset, &range);
18472 : :
18473 : 137 : mpz_add (offset, offset, range);
18474 : 137 : mpz_clear (range);
18475 : :
18476 : 137 : if (!t)
18477 : : break;
18478 : 129 : }
18479 : :
18480 : : /* Assign initial value to symbol. */
18481 : : else
18482 : : {
18483 : 8287 : mpz_sub_ui (values.left, values.left, 1);
18484 : 8287 : mpz_sub_ui (size, size, 1);
18485 : :
18486 : 8287 : t = gfc_assign_data_value (var->expr, values.vnode->expr,
18487 : : offset, NULL);
18488 : 8287 : if (!t)
18489 : : break;
18490 : :
18491 : 8252 : if (mark == AR_FULL)
18492 : 5259 : mpz_add_ui (offset, offset, 1);
18493 : :
18494 : : /* Modify the array section indexes and recalculate the offset
18495 : : for next element. */
18496 : 2993 : else if (mark == AR_SECTION)
18497 : 363 : gfc_advance_section (section_index, ar, &offset, vector_offset);
18498 : : }
18499 : : }
18500 : :
18501 : 3519 : if (mark == AR_SECTION)
18502 : : {
18503 : 340 : for (i = 0; i < ar->dimen; i++)
18504 : 192 : mpz_clear (section_index[i]);
18505 : : }
18506 : :
18507 : 3519 : mpz_clear (size);
18508 : 3519 : mpz_clear (offset);
18509 : :
18510 : 3519 : return t;
18511 : :
18512 : 6 : bad_charlen:
18513 : 6 : gfc_error ("Non-constant character length at %L in DATA statement",
18514 : : &e->where);
18515 : 6 : return false;
18516 : : }
18517 : :
18518 : :
18519 : : static bool traverse_data_var (gfc_data_variable *, locus *);
18520 : :
18521 : : /* Iterate over a list of elements in a DATA statement. */
18522 : :
18523 : : static bool
18524 : 236 : traverse_data_list (gfc_data_variable *var, locus *where)
18525 : : {
18526 : 236 : mpz_t trip;
18527 : 236 : iterator_stack frame;
18528 : 236 : gfc_expr *e, *start, *end, *step;
18529 : 236 : bool retval = true;
18530 : :
18531 : 236 : mpz_init (frame.value);
18532 : 236 : mpz_init (trip);
18533 : :
18534 : 236 : start = gfc_copy_expr (var->iter.start);
18535 : 236 : end = gfc_copy_expr (var->iter.end);
18536 : 236 : step = gfc_copy_expr (var->iter.step);
18537 : :
18538 : 236 : if (!gfc_simplify_expr (start, 1)
18539 : 236 : || start->expr_type != EXPR_CONSTANT)
18540 : : {
18541 : 0 : gfc_error ("start of implied-do loop at %L could not be "
18542 : : "simplified to a constant value", &start->where);
18543 : 0 : retval = false;
18544 : 0 : goto cleanup;
18545 : : }
18546 : 236 : if (!gfc_simplify_expr (end, 1)
18547 : 236 : || end->expr_type != EXPR_CONSTANT)
18548 : : {
18549 : 0 : gfc_error ("end of implied-do loop at %L could not be "
18550 : : "simplified to a constant value", &end->where);
18551 : 0 : retval = false;
18552 : 0 : goto cleanup;
18553 : : }
18554 : 236 : if (!gfc_simplify_expr (step, 1)
18555 : 236 : || step->expr_type != EXPR_CONSTANT)
18556 : : {
18557 : 0 : gfc_error ("step of implied-do loop at %L could not be "
18558 : : "simplified to a constant value", &step->where);
18559 : 0 : retval = false;
18560 : 0 : goto cleanup;
18561 : : }
18562 : 236 : if (mpz_cmp_si (step->value.integer, 0) == 0)
18563 : : {
18564 : 1 : gfc_error ("step of implied-do loop at %L shall not be zero",
18565 : : &step->where);
18566 : 1 : retval = false;
18567 : 1 : goto cleanup;
18568 : : }
18569 : :
18570 : 235 : mpz_set (trip, end->value.integer);
18571 : 235 : mpz_sub (trip, trip, start->value.integer);
18572 : 235 : mpz_add (trip, trip, step->value.integer);
18573 : :
18574 : 235 : mpz_div (trip, trip, step->value.integer);
18575 : :
18576 : 235 : mpz_set (frame.value, start->value.integer);
18577 : :
18578 : 235 : frame.prev = iter_stack;
18579 : 235 : frame.variable = var->iter.var->symtree;
18580 : 235 : iter_stack = &frame;
18581 : :
18582 : 1124 : while (mpz_cmp_ui (trip, 0) > 0)
18583 : : {
18584 : 903 : if (!traverse_data_var (var->list, where))
18585 : : {
18586 : 14 : retval = false;
18587 : 14 : goto cleanup;
18588 : : }
18589 : :
18590 : 889 : e = gfc_copy_expr (var->expr);
18591 : 889 : if (!gfc_simplify_expr (e, 1))
18592 : : {
18593 : 0 : gfc_free_expr (e);
18594 : 0 : retval = false;
18595 : 0 : goto cleanup;
18596 : : }
18597 : :
18598 : 889 : mpz_add (frame.value, frame.value, step->value.integer);
18599 : :
18600 : 889 : mpz_sub_ui (trip, trip, 1);
18601 : : }
18602 : :
18603 : 221 : cleanup:
18604 : 236 : mpz_clear (frame.value);
18605 : 236 : mpz_clear (trip);
18606 : :
18607 : 236 : gfc_free_expr (start);
18608 : 236 : gfc_free_expr (end);
18609 : 236 : gfc_free_expr (step);
18610 : :
18611 : 236 : iter_stack = frame.prev;
18612 : 236 : return retval;
18613 : : }
18614 : :
18615 : :
18616 : : /* Type resolve variables in the variable list of a DATA statement. */
18617 : :
18618 : : static bool
18619 : 3397 : traverse_data_var (gfc_data_variable *var, locus *where)
18620 : : {
18621 : 3397 : bool t;
18622 : :
18623 : 7074 : for (; var; var = var->next)
18624 : : {
18625 : 3773 : if (var->expr == NULL)
18626 : 236 : t = traverse_data_list (var, where);
18627 : : else
18628 : 3537 : t = check_data_variable (var, where);
18629 : :
18630 : 3773 : if (!t)
18631 : : return false;
18632 : : }
18633 : :
18634 : : return true;
18635 : : }
18636 : :
18637 : :
18638 : : /* Resolve the expressions and iterators associated with a data statement.
18639 : : This is separate from the assignment checking because data lists should
18640 : : only be resolved once. */
18641 : :
18642 : : static bool
18643 : 2648 : resolve_data_variables (gfc_data_variable *d)
18644 : : {
18645 : 5667 : for (; d; d = d->next)
18646 : : {
18647 : 3024 : if (d->list == NULL)
18648 : : {
18649 : 2872 : if (!gfc_resolve_expr (d->expr))
18650 : : return false;
18651 : : }
18652 : : else
18653 : : {
18654 : 152 : if (!gfc_resolve_iterator (&d->iter, false, true))
18655 : : return false;
18656 : :
18657 : 149 : if (!resolve_data_variables (d->list))
18658 : : return false;
18659 : : }
18660 : : }
18661 : :
18662 : : return true;
18663 : : }
18664 : :
18665 : :
18666 : : /* Resolve a single DATA statement. We implement this by storing a pointer to
18667 : : the value list into static variables, and then recursively traversing the
18668 : : variables list, expanding iterators and such. */
18669 : :
18670 : : static void
18671 : 2499 : resolve_data (gfc_data *d)
18672 : : {
18673 : :
18674 : 2499 : if (!resolve_data_variables (d->var))
18675 : : return;
18676 : :
18677 : 2494 : values.vnode = d->value;
18678 : 2494 : if (d->value == NULL)
18679 : 0 : mpz_set_ui (values.left, 0);
18680 : : else
18681 : 2494 : mpz_set (values.left, d->value->repeat);
18682 : :
18683 : 2494 : if (!traverse_data_var (d->var, &d->where))
18684 : : return;
18685 : :
18686 : : /* At this point, we better not have any values left. */
18687 : :
18688 : 2412 : if (next_data_value ())
18689 : 0 : gfc_error ("DATA statement at %L has more values than variables",
18690 : : &d->where);
18691 : : }
18692 : :
18693 : :
18694 : : /* 12.6 Constraint: In a pure subprogram any variable which is in common or
18695 : : accessed by host or use association, is a dummy argument to a pure function,
18696 : : is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
18697 : : is storage associated with any such variable, shall not be used in the
18698 : : following contexts: (clients of this function). */
18699 : :
18700 : : /* Determines if a variable is not 'pure', i.e., not assignable within a pure
18701 : : procedure. Returns zero if assignment is OK, nonzero if there is a
18702 : : problem. */
18703 : : bool
18704 : 52916 : gfc_impure_variable (gfc_symbol *sym)
18705 : : {
18706 : 52916 : gfc_symbol *proc;
18707 : 52916 : gfc_namespace *ns;
18708 : :
18709 : 52916 : if (sym->attr.use_assoc || sym->attr.in_common)
18710 : : return 1;
18711 : :
18712 : : /* The namespace of a module procedure interface holds the arguments and
18713 : : symbols, and so the symbol namespace can be different to that of the
18714 : : procedure. */
18715 : 52395 : if (sym->ns != gfc_current_ns
18716 : 5543 : && gfc_current_ns->proc_name->abr_modproc_decl
18717 : 36 : && sym->ns->proc_name->attr.function
18718 : 12 : && sym->attr.result
18719 : 12 : && !strcmp (sym->ns->proc_name->name, gfc_current_ns->proc_name->name))
18720 : : return 0;
18721 : :
18722 : : /* Check if the symbol's ns is inside the pure procedure. */
18723 : 56753 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18724 : : {
18725 : 56474 : if (ns == sym->ns)
18726 : : break;
18727 : 5807 : if (ns->proc_name->attr.flavor == FL_PROCEDURE && !sym->attr.function)
18728 : : return 1;
18729 : : }
18730 : :
18731 : 50946 : proc = sym->ns->proc_name;
18732 : 50946 : if (sym->attr.dummy
18733 : 5625 : && !sym->attr.value
18734 : 5503 : && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
18735 : 5308 : || proc->attr.function))
18736 : 674 : return 1;
18737 : :
18738 : : /* TODO: Sort out what can be storage associated, if anything, and include
18739 : : it here. In principle equivalences should be scanned but it does not
18740 : : seem to be possible to storage associate an impure variable this way. */
18741 : : return 0;
18742 : : }
18743 : :
18744 : :
18745 : : /* Test whether a symbol is pure or not. For a NULL pointer, checks if the
18746 : : current namespace is inside a pure procedure. */
18747 : :
18748 : : bool
18749 : 2242647 : gfc_pure (gfc_symbol *sym)
18750 : : {
18751 : 2242647 : symbol_attribute attr;
18752 : 2242647 : gfc_namespace *ns;
18753 : :
18754 : 2242647 : if (sym == NULL)
18755 : : {
18756 : : /* Check if the current namespace or one of its parents
18757 : : belongs to a pure procedure. */
18758 : 3090961 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18759 : : {
18760 : 1825731 : sym = ns->proc_name;
18761 : 1825731 : if (sym == NULL)
18762 : : return 0;
18763 : 1824617 : attr = sym->attr;
18764 : 1824617 : if (attr.flavor == FL_PROCEDURE && attr.pure)
18765 : : return 1;
18766 : : }
18767 : : return 0;
18768 : : }
18769 : :
18770 : 969375 : attr = sym->attr;
18771 : :
18772 : 969375 : return attr.flavor == FL_PROCEDURE && attr.pure;
18773 : : }
18774 : :
18775 : :
18776 : : /* Test whether a symbol is implicitly pure or not. For a NULL pointer,
18777 : : checks if the current namespace is implicitly pure. Note that this
18778 : : function returns false for a PURE procedure. */
18779 : :
18780 : : bool
18781 : 704950 : gfc_implicit_pure (gfc_symbol *sym)
18782 : : {
18783 : 704950 : gfc_namespace *ns;
18784 : :
18785 : 704950 : if (sym == NULL)
18786 : : {
18787 : : /* Check if the current procedure is implicit_pure. Walk up
18788 : : the procedure list until we find a procedure. */
18789 : 969214 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18790 : : {
18791 : 692936 : sym = ns->proc_name;
18792 : 692936 : if (sym == NULL)
18793 : : return 0;
18794 : :
18795 : 692863 : if (sym->attr.flavor == FL_PROCEDURE)
18796 : : break;
18797 : : }
18798 : : }
18799 : :
18800 : 428596 : return sym->attr.flavor == FL_PROCEDURE && sym->attr.implicit_pure
18801 : 730626 : && !sym->attr.pure;
18802 : : }
18803 : :
18804 : :
18805 : : void
18806 : 412439 : gfc_unset_implicit_pure (gfc_symbol *sym)
18807 : : {
18808 : 412439 : gfc_namespace *ns;
18809 : :
18810 : 412439 : if (sym == NULL)
18811 : : {
18812 : : /* Check if the current procedure is implicit_pure. Walk up
18813 : : the procedure list until we find a procedure. */
18814 : 673460 : for (ns = gfc_current_ns; ns; ns = ns->parent)
18815 : : {
18816 : 416674 : sym = ns->proc_name;
18817 : 416674 : if (sym == NULL)
18818 : : return;
18819 : :
18820 : 415868 : if (sym->attr.flavor == FL_PROCEDURE)
18821 : : break;
18822 : : }
18823 : : }
18824 : :
18825 : 411633 : if (sym->attr.flavor == FL_PROCEDURE)
18826 : 147068 : sym->attr.implicit_pure = 0;
18827 : : else
18828 : 264565 : sym->attr.pure = 0;
18829 : : }
18830 : :
18831 : :
18832 : : /* Test whether the current procedure is elemental or not. */
18833 : :
18834 : : bool
18835 : 1299643 : gfc_elemental (gfc_symbol *sym)
18836 : : {
18837 : 1299643 : symbol_attribute attr;
18838 : :
18839 : 1299643 : if (sym == NULL)
18840 : 0 : sym = gfc_current_ns->proc_name;
18841 : 0 : if (sym == NULL)
18842 : : return 0;
18843 : 1299643 : attr = sym->attr;
18844 : :
18845 : 1299643 : return attr.flavor == FL_PROCEDURE && attr.elemental;
18846 : : }
18847 : :
18848 : :
18849 : : /* Warn about unused labels. */
18850 : :
18851 : : static void
18852 : 4469 : warn_unused_fortran_label (gfc_st_label *label)
18853 : : {
18854 : 4472 : if (label == NULL)
18855 : : return;
18856 : :
18857 : 4 : warn_unused_fortran_label (label->left);
18858 : :
18859 : 4 : if (label->defined == ST_LABEL_UNKNOWN)
18860 : : return;
18861 : :
18862 : 3 : switch (label->referenced)
18863 : : {
18864 : 1 : case ST_LABEL_UNKNOWN:
18865 : 1 : gfc_warning (OPT_Wunused_label, "Label %d at %L defined but not used",
18866 : : label->value, &label->where);
18867 : 1 : break;
18868 : :
18869 : 1 : case ST_LABEL_BAD_TARGET:
18870 : 1 : gfc_warning (OPT_Wunused_label,
18871 : : "Label %d at %L defined but cannot be used",
18872 : : label->value, &label->where);
18873 : 1 : break;
18874 : :
18875 : : default:
18876 : : break;
18877 : : }
18878 : :
18879 : 3 : warn_unused_fortran_label (label->right);
18880 : : }
18881 : :
18882 : :
18883 : : /* Returns the sequence type of a symbol or sequence. */
18884 : :
18885 : : static seq_type
18886 : 1076 : sequence_type (gfc_typespec ts)
18887 : : {
18888 : 1076 : seq_type result;
18889 : 1076 : gfc_component *c;
18890 : :
18891 : 1076 : switch (ts.type)
18892 : : {
18893 : 49 : case BT_DERIVED:
18894 : :
18895 : 49 : if (ts.u.derived->components == NULL)
18896 : : return SEQ_NONDEFAULT;
18897 : :
18898 : 49 : result = sequence_type (ts.u.derived->components->ts);
18899 : 103 : for (c = ts.u.derived->components->next; c; c = c->next)
18900 : 67 : if (sequence_type (c->ts) != result)
18901 : : return SEQ_MIXED;
18902 : :
18903 : : return result;
18904 : :
18905 : 129 : case BT_CHARACTER:
18906 : 129 : if (ts.kind != gfc_default_character_kind)
18907 : 0 : return SEQ_NONDEFAULT;
18908 : :
18909 : : return SEQ_CHARACTER;
18910 : :
18911 : 240 : case BT_INTEGER:
18912 : 240 : if (ts.kind != gfc_default_integer_kind)
18913 : 25 : return SEQ_NONDEFAULT;
18914 : :
18915 : : return SEQ_NUMERIC;
18916 : :
18917 : 559 : case BT_REAL:
18918 : 559 : if (!(ts.kind == gfc_default_real_kind
18919 : 269 : || ts.kind == gfc_default_double_kind))
18920 : 0 : return SEQ_NONDEFAULT;
18921 : :
18922 : : return SEQ_NUMERIC;
18923 : :
18924 : 81 : case BT_COMPLEX:
18925 : 81 : if (ts.kind != gfc_default_complex_kind)
18926 : 48 : return SEQ_NONDEFAULT;
18927 : :
18928 : : return SEQ_NUMERIC;
18929 : :
18930 : 17 : case BT_LOGICAL:
18931 : 17 : if (ts.kind != gfc_default_logical_kind)
18932 : 0 : return SEQ_NONDEFAULT;
18933 : :
18934 : : return SEQ_NUMERIC;
18935 : :
18936 : : default:
18937 : : return SEQ_NONDEFAULT;
18938 : : }
18939 : : }
18940 : :
18941 : :
18942 : : /* Resolve derived type EQUIVALENCE object. */
18943 : :
18944 : : static bool
18945 : 80 : resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
18946 : : {
18947 : 80 : gfc_component *c = derived->components;
18948 : :
18949 : 80 : if (!derived)
18950 : : return true;
18951 : :
18952 : : /* Shall not be an object of nonsequence derived type. */
18953 : 80 : if (!derived->attr.sequence)
18954 : : {
18955 : 0 : gfc_error ("Derived type variable %qs at %L must have SEQUENCE "
18956 : : "attribute to be an EQUIVALENCE object", sym->name,
18957 : : &e->where);
18958 : 0 : return false;
18959 : : }
18960 : :
18961 : : /* Shall not have allocatable components. */
18962 : 80 : if (derived->attr.alloc_comp)
18963 : : {
18964 : 1 : gfc_error ("Derived type variable %qs at %L cannot have ALLOCATABLE "
18965 : : "components to be an EQUIVALENCE object",sym->name,
18966 : : &e->where);
18967 : 1 : return false;
18968 : : }
18969 : :
18970 : 79 : if (sym->attr.in_common && gfc_has_default_initializer (sym->ts.u.derived))
18971 : : {
18972 : 1 : gfc_error ("Derived type variable %qs at %L with default "
18973 : : "initialization cannot be in EQUIVALENCE with a variable "
18974 : : "in COMMON", sym->name, &e->where);
18975 : 1 : return false;
18976 : : }
18977 : :
18978 : 245 : for (; c ; c = c->next)
18979 : : {
18980 : 167 : if (gfc_bt_struct (c->ts.type)
18981 : 167 : && (!resolve_equivalence_derived(c->ts.u.derived, sym, e)))
18982 : : return false;
18983 : :
18984 : : /* Shall not be an object of sequence derived type containing a pointer
18985 : : in the structure. */
18986 : 167 : if (c->attr.pointer)
18987 : : {
18988 : 0 : gfc_error ("Derived type variable %qs at %L with pointer "
18989 : : "component(s) cannot be an EQUIVALENCE object",
18990 : : sym->name, &e->where);
18991 : 0 : return false;
18992 : : }
18993 : : }
18994 : : return true;
18995 : : }
18996 : :
18997 : :
18998 : : /* Resolve equivalence object.
18999 : : An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
19000 : : an allocatable array, an object of nonsequence derived type, an object of
19001 : : sequence derived type containing a pointer at any level of component
19002 : : selection, an automatic object, a function name, an entry name, a result
19003 : : name, a named constant, a structure component, or a subobject of any of
19004 : : the preceding objects. A substring shall not have length zero. A
19005 : : derived type shall not have components with default initialization nor
19006 : : shall two objects of an equivalence group be initialized.
19007 : : Either all or none of the objects shall have an protected attribute.
19008 : : The simple constraints are done in symbol.cc(check_conflict) and the rest
19009 : : are implemented here. */
19010 : :
19011 : : static void
19012 : 1565 : resolve_equivalence (gfc_equiv *eq)
19013 : : {
19014 : 1565 : gfc_symbol *sym;
19015 : 1565 : gfc_symbol *first_sym;
19016 : 1565 : gfc_expr *e;
19017 : 1565 : gfc_ref *r;
19018 : 1565 : locus *last_where = NULL;
19019 : 1565 : seq_type eq_type, last_eq_type;
19020 : 1565 : gfc_typespec *last_ts;
19021 : 1565 : int object, cnt_protected;
19022 : 1565 : const char *msg;
19023 : :
19024 : 1565 : last_ts = &eq->expr->symtree->n.sym->ts;
19025 : :
19026 : 1565 : first_sym = eq->expr->symtree->n.sym;
19027 : :
19028 : 1565 : cnt_protected = 0;
19029 : :
19030 : 4727 : for (object = 1; eq; eq = eq->eq, object++)
19031 : : {
19032 : 3171 : e = eq->expr;
19033 : :
19034 : 3171 : e->ts = e->symtree->n.sym->ts;
19035 : : /* match_varspec might not know yet if it is seeing
19036 : : array reference or substring reference, as it doesn't
19037 : : know the types. */
19038 : 3171 : if (e->ref && e->ref->type == REF_ARRAY)
19039 : : {
19040 : 2152 : gfc_ref *ref = e->ref;
19041 : 2152 : sym = e->symtree->n.sym;
19042 : :
19043 : 2152 : if (sym->attr.dimension)
19044 : : {
19045 : 1855 : ref->u.ar.as = sym->as;
19046 : 1855 : ref = ref->next;
19047 : : }
19048 : :
19049 : : /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
19050 : 2152 : if (e->ts.type == BT_CHARACTER
19051 : 592 : && ref
19052 : 371 : && ref->type == REF_ARRAY
19053 : 371 : && ref->u.ar.dimen == 1
19054 : 371 : && ref->u.ar.dimen_type[0] == DIMEN_RANGE
19055 : 371 : && ref->u.ar.stride[0] == NULL)
19056 : : {
19057 : 370 : gfc_expr *start = ref->u.ar.start[0];
19058 : 370 : gfc_expr *end = ref->u.ar.end[0];
19059 : 370 : void *mem = NULL;
19060 : :
19061 : : /* Optimize away the (:) reference. */
19062 : 370 : if (start == NULL && end == NULL)
19063 : : {
19064 : 9 : if (e->ref == ref)
19065 : 0 : e->ref = ref->next;
19066 : : else
19067 : 9 : e->ref->next = ref->next;
19068 : : mem = ref;
19069 : : }
19070 : : else
19071 : : {
19072 : 361 : ref->type = REF_SUBSTRING;
19073 : 361 : if (start == NULL)
19074 : 9 : start = gfc_get_int_expr (gfc_charlen_int_kind,
19075 : : NULL, 1);
19076 : 361 : ref->u.ss.start = start;
19077 : 361 : if (end == NULL && e->ts.u.cl)
19078 : 27 : end = gfc_copy_expr (e->ts.u.cl->length);
19079 : 361 : ref->u.ss.end = end;
19080 : 361 : ref->u.ss.length = e->ts.u.cl;
19081 : 361 : e->ts.u.cl = NULL;
19082 : : }
19083 : 370 : ref = ref->next;
19084 : 370 : free (mem);
19085 : : }
19086 : :
19087 : : /* Any further ref is an error. */
19088 : 1930 : if (ref)
19089 : : {
19090 : 1 : gcc_assert (ref->type == REF_ARRAY);
19091 : 1 : gfc_error ("Syntax error in EQUIVALENCE statement at %L",
19092 : : &ref->u.ar.where);
19093 : 1 : continue;
19094 : : }
19095 : : }
19096 : :
19097 : 3170 : if (!gfc_resolve_expr (e))
19098 : 2 : continue;
19099 : :
19100 : 3168 : sym = e->symtree->n.sym;
19101 : :
19102 : 3168 : if (sym->attr.is_protected)
19103 : 2 : cnt_protected++;
19104 : 3168 : if (cnt_protected > 0 && cnt_protected != object)
19105 : : {
19106 : 2 : gfc_error ("Either all or none of the objects in the "
19107 : : "EQUIVALENCE set at %L shall have the "
19108 : : "PROTECTED attribute",
19109 : : &e->where);
19110 : 2 : break;
19111 : : }
19112 : :
19113 : : /* Shall not equivalence common block variables in a PURE procedure. */
19114 : 3166 : if (sym->ns->proc_name
19115 : 3150 : && sym->ns->proc_name->attr.pure
19116 : 7 : && sym->attr.in_common)
19117 : : {
19118 : : /* Need to check for symbols that may have entered the pure
19119 : : procedure via a USE statement. */
19120 : 7 : bool saw_sym = false;
19121 : 7 : if (sym->ns->use_stmts)
19122 : : {
19123 : 6 : gfc_use_rename *r;
19124 : 10 : for (r = sym->ns->use_stmts->rename; r; r = r->next)
19125 : 4 : if (strcmp(r->use_name, sym->name) == 0) saw_sym = true;
19126 : : }
19127 : : else
19128 : : saw_sym = true;
19129 : :
19130 : 6 : if (saw_sym)
19131 : 3 : gfc_error ("COMMON block member %qs at %L cannot be an "
19132 : : "EQUIVALENCE object in the pure procedure %qs",
19133 : : sym->name, &e->where, sym->ns->proc_name->name);
19134 : : break;
19135 : : }
19136 : :
19137 : : /* Shall not be a named constant. */
19138 : 3159 : if (e->expr_type == EXPR_CONSTANT)
19139 : : {
19140 : 0 : gfc_error ("Named constant %qs at %L cannot be an EQUIVALENCE "
19141 : : "object", sym->name, &e->where);
19142 : 0 : continue;
19143 : : }
19144 : :
19145 : 3161 : if (e->ts.type == BT_DERIVED
19146 : 3159 : && !resolve_equivalence_derived (e->ts.u.derived, sym, e))
19147 : 2 : continue;
19148 : :
19149 : : /* Check that the types correspond correctly:
19150 : : Note 5.28:
19151 : : A numeric sequence structure may be equivalenced to another sequence
19152 : : structure, an object of default integer type, default real type, double
19153 : : precision real type, default logical type such that components of the
19154 : : structure ultimately only become associated to objects of the same
19155 : : kind. A character sequence structure may be equivalenced to an object
19156 : : of default character kind or another character sequence structure.
19157 : : Other objects may be equivalenced only to objects of the same type and
19158 : : kind parameters. */
19159 : :
19160 : : /* Identical types are unconditionally OK. */
19161 : 3157 : if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
19162 : 2677 : goto identical_types;
19163 : :
19164 : 480 : last_eq_type = sequence_type (*last_ts);
19165 : 480 : eq_type = sequence_type (sym->ts);
19166 : :
19167 : : /* Since the pair of objects is not of the same type, mixed or
19168 : : non-default sequences can be rejected. */
19169 : :
19170 : 480 : msg = G_("Sequence %s with mixed components in EQUIVALENCE "
19171 : : "statement at %L with different type objects");
19172 : 481 : if ((object ==2
19173 : 480 : && last_eq_type == SEQ_MIXED
19174 : 7 : && last_where
19175 : 7 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19176 : 486 : || (eq_type == SEQ_MIXED
19177 : 6 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19178 : 1 : continue;
19179 : :
19180 : 479 : msg = G_("Non-default type object or sequence %s in EQUIVALENCE "
19181 : : "statement at %L with objects of different type");
19182 : 483 : if ((object ==2
19183 : 479 : && last_eq_type == SEQ_NONDEFAULT
19184 : 50 : && last_where
19185 : 49 : && !gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where))
19186 : 525 : || (eq_type == SEQ_NONDEFAULT
19187 : 24 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where)))
19188 : 4 : continue;
19189 : :
19190 : 475 : msg = G_("Non-CHARACTER object %qs in default CHARACTER "
19191 : : "EQUIVALENCE statement at %L");
19192 : 479 : if (last_eq_type == SEQ_CHARACTER
19193 : 475 : && eq_type != SEQ_CHARACTER
19194 : 475 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19195 : 4 : continue;
19196 : :
19197 : 471 : msg = G_("Non-NUMERIC object %qs in default NUMERIC "
19198 : : "EQUIVALENCE statement at %L");
19199 : 473 : if (last_eq_type == SEQ_NUMERIC
19200 : 471 : && eq_type != SEQ_NUMERIC
19201 : 471 : && !gfc_notify_std (GFC_STD_GNU, msg, sym->name, &e->where))
19202 : 2 : continue;
19203 : :
19204 : 3146 : identical_types:
19205 : :
19206 : 3146 : last_ts =&sym->ts;
19207 : 3146 : last_where = &e->where;
19208 : :
19209 : 3146 : if (!e->ref)
19210 : 1003 : continue;
19211 : :
19212 : : /* Shall not be an automatic array. */
19213 : 2143 : if (e->ref->type == REF_ARRAY && is_non_constant_shape_array (sym))
19214 : : {
19215 : 3 : gfc_error ("Array %qs at %L with non-constant bounds cannot be "
19216 : : "an EQUIVALENCE object", sym->name, &e->where);
19217 : 3 : continue;
19218 : : }
19219 : :
19220 : 2140 : r = e->ref;
19221 : 4326 : while (r)
19222 : : {
19223 : : /* Shall not be a structure component. */
19224 : 2187 : if (r->type == REF_COMPONENT)
19225 : : {
19226 : 0 : gfc_error ("Structure component %qs at %L cannot be an "
19227 : : "EQUIVALENCE object",
19228 : 0 : r->u.c.component->name, &e->where);
19229 : 0 : break;
19230 : : }
19231 : :
19232 : : /* A substring shall not have length zero. */
19233 : 2187 : if (r->type == REF_SUBSTRING)
19234 : : {
19235 : 341 : if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
19236 : : {
19237 : 1 : gfc_error ("Substring at %L has length zero",
19238 : : &r->u.ss.start->where);
19239 : 1 : break;
19240 : : }
19241 : : }
19242 : 2186 : r = r->next;
19243 : : }
19244 : : }
19245 : 1565 : }
19246 : :
19247 : :
19248 : : /* Function called by resolve_fntype to flag other symbols used in the
19249 : : length type parameter specification of function results. */
19250 : :
19251 : : static bool
19252 : 4081 : flag_fn_result_spec (gfc_expr *expr,
19253 : : gfc_symbol *sym,
19254 : : int *f ATTRIBUTE_UNUSED)
19255 : : {
19256 : 4081 : gfc_namespace *ns;
19257 : 4081 : gfc_symbol *s;
19258 : :
19259 : 4081 : if (expr->expr_type == EXPR_VARIABLE)
19260 : : {
19261 : 1376 : s = expr->symtree->n.sym;
19262 : 2149 : for (ns = s->ns; ns; ns = ns->parent)
19263 : 2149 : if (!ns->parent)
19264 : : break;
19265 : :
19266 : 1376 : if (sym == s)
19267 : : {
19268 : 1 : gfc_error ("Self reference in character length expression "
19269 : : "for %qs at %L", sym->name, &expr->where);
19270 : 1 : return true;
19271 : : }
19272 : :
19273 : 1375 : if (!s->fn_result_spec
19274 : 1375 : && s->attr.flavor == FL_PARAMETER)
19275 : : {
19276 : : /* Function contained in a module.... */
19277 : 63 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_MODULE)
19278 : : {
19279 : 32 : gfc_symtree *st;
19280 : 32 : s->fn_result_spec = 1;
19281 : : /* Make sure that this symbol is translated as a module
19282 : : variable. */
19283 : 32 : st = gfc_get_unique_symtree (ns);
19284 : 32 : st->n.sym = s;
19285 : 32 : s->refs++;
19286 : 32 : }
19287 : : /* ... which is use associated and called. */
19288 : 31 : else if (s->attr.use_assoc || s->attr.used_in_submodule
19289 : 0 : ||
19290 : : /* External function matched with an interface. */
19291 : 0 : (s->ns->proc_name
19292 : 0 : && ((s->ns == ns
19293 : 0 : && s->ns->proc_name->attr.if_source == IFSRC_DECL)
19294 : 0 : || s->ns->proc_name->attr.if_source == IFSRC_IFBODY)
19295 : 0 : && s->ns->proc_name->attr.function))
19296 : 31 : s->fn_result_spec = 1;
19297 : : }
19298 : : }
19299 : : return false;
19300 : : }
19301 : :
19302 : :
19303 : : /* Resolve function and ENTRY types, issue diagnostics if needed. */
19304 : :
19305 : : static void
19306 : 332209 : resolve_fntype (gfc_namespace *ns)
19307 : : {
19308 : 332209 : gfc_entry_list *el;
19309 : 332209 : gfc_symbol *sym;
19310 : :
19311 : 332209 : if (ns->proc_name == NULL || !ns->proc_name->attr.function)
19312 : : return;
19313 : :
19314 : : /* If there are any entries, ns->proc_name is the entry master
19315 : : synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
19316 : 175092 : if (ns->entries)
19317 : 564 : sym = ns->entries->sym;
19318 : : else
19319 : : sym = ns->proc_name;
19320 : 175092 : if (sym->result == sym
19321 : 142634 : && sym->ts.type == BT_UNKNOWN
19322 : 6 : && !gfc_set_default_type (sym, 0, NULL)
19323 : 175096 : && !sym->attr.untyped)
19324 : : {
19325 : 3 : gfc_error ("Function %qs at %L has no IMPLICIT type",
19326 : : sym->name, &sym->declared_at);
19327 : 3 : sym->attr.untyped = 1;
19328 : : }
19329 : :
19330 : 12948 : if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
19331 : 1599 : && !sym->attr.contained
19332 : 274 : && !gfc_check_symbol_access (sym->ts.u.derived)
19333 : 175092 : && gfc_check_symbol_access (sym))
19334 : : {
19335 : 0 : gfc_notify_std (GFC_STD_F2003, "PUBLIC function %qs at "
19336 : : "%L of PRIVATE type %qs", sym->name,
19337 : 0 : &sym->declared_at, sym->ts.u.derived->name);
19338 : : }
19339 : :
19340 : 175092 : if (ns->entries)
19341 : 1189 : for (el = ns->entries->next; el; el = el->next)
19342 : : {
19343 : 625 : if (el->sym->result == el->sym
19344 : 413 : && el->sym->ts.type == BT_UNKNOWN
19345 : 2 : && !gfc_set_default_type (el->sym, 0, NULL)
19346 : 627 : && !el->sym->attr.untyped)
19347 : : {
19348 : 2 : gfc_error ("ENTRY %qs at %L has no IMPLICIT type",
19349 : : el->sym->name, &el->sym->declared_at);
19350 : 2 : el->sym->attr.untyped = 1;
19351 : : }
19352 : : }
19353 : :
19354 : 175092 : if (sym->ts.type == BT_CHARACTER
19355 : 6641 : && sym->ts.u.cl->length
19356 : 1738 : && sym->ts.u.cl->length->ts.type == BT_INTEGER)
19357 : 1733 : gfc_traverse_expr (sym->ts.u.cl->length, sym, flag_fn_result_spec, 0);
19358 : : }
19359 : :
19360 : :
19361 : : /* 12.3.2.1.1 Defined operators. */
19362 : :
19363 : : static bool
19364 : 371 : check_uop_procedure (gfc_symbol *sym, locus where)
19365 : : {
19366 : 371 : gfc_formal_arglist *formal;
19367 : :
19368 : 371 : if (!sym->attr.function)
19369 : : {
19370 : 3 : gfc_error ("User operator procedure %qs at %L must be a FUNCTION",
19371 : : sym->name, &where);
19372 : 3 : return false;
19373 : : }
19374 : :
19375 : 368 : if (sym->ts.type == BT_CHARACTER
19376 : 15 : && !((sym->ts.u.cl && sym->ts.u.cl->length) || sym->ts.deferred)
19377 : 2 : && !(sym->result && ((sym->result->ts.u.cl
19378 : 2 : && sym->result->ts.u.cl->length) || sym->result->ts.deferred)))
19379 : : {
19380 : 2 : gfc_error ("User operator procedure %qs at %L cannot be assumed "
19381 : : "character length", sym->name, &where);
19382 : 2 : return false;
19383 : : }
19384 : :
19385 : 366 : formal = gfc_sym_get_dummy_args (sym);
19386 : 366 : if (!formal || !formal->sym)
19387 : : {
19388 : 1 : gfc_error ("User operator procedure %qs at %L must have at least "
19389 : : "one argument", sym->name, &where);
19390 : 1 : return false;
19391 : : }
19392 : :
19393 : 365 : if (formal->sym->attr.intent != INTENT_IN)
19394 : : {
19395 : 0 : gfc_error ("First argument of operator interface at %L must be "
19396 : : "INTENT(IN)", &where);
19397 : 0 : return false;
19398 : : }
19399 : :
19400 : 365 : if (formal->sym->attr.optional)
19401 : : {
19402 : 0 : gfc_error ("First argument of operator interface at %L cannot be "
19403 : : "optional", &where);
19404 : 0 : return false;
19405 : : }
19406 : :
19407 : 365 : formal = formal->next;
19408 : 365 : if (!formal || !formal->sym)
19409 : : return true;
19410 : :
19411 : 232 : if (formal->sym->attr.intent != INTENT_IN)
19412 : : {
19413 : 0 : gfc_error ("Second argument of operator interface at %L must be "
19414 : : "INTENT(IN)", &where);
19415 : 0 : return false;
19416 : : }
19417 : :
19418 : 232 : if (formal->sym->attr.optional)
19419 : : {
19420 : 1 : gfc_error ("Second argument of operator interface at %L cannot be "
19421 : : "optional", &where);
19422 : 1 : return false;
19423 : : }
19424 : :
19425 : 231 : if (formal->next)
19426 : : {
19427 : 2 : gfc_error ("Operator interface at %L must have, at most, two "
19428 : : "arguments", &where);
19429 : 2 : return false;
19430 : : }
19431 : :
19432 : : return true;
19433 : : }
19434 : :
19435 : : static void
19436 : 332875 : gfc_resolve_uops (gfc_symtree *symtree)
19437 : : {
19438 : 332875 : gfc_interface *itr;
19439 : :
19440 : 332875 : if (symtree == NULL)
19441 : : return;
19442 : :
19443 : 333 : gfc_resolve_uops (symtree->left);
19444 : 333 : gfc_resolve_uops (symtree->right);
19445 : :
19446 : 662 : for (itr = symtree->n.uop->op; itr; itr = itr->next)
19447 : 329 : check_uop_procedure (itr->sym, itr->sym->declared_at);
19448 : : }
19449 : :
19450 : :
19451 : : /* Examine all of the expressions associated with a program unit,
19452 : : assign types to all intermediate expressions, make sure that all
19453 : : assignments are to compatible types and figure out which names
19454 : : refer to which functions or subroutines. It doesn't check code
19455 : : block, which is handled by gfc_resolve_code. */
19456 : :
19457 : : static void
19458 : 334655 : resolve_types (gfc_namespace *ns)
19459 : : {
19460 : 334655 : gfc_namespace *n;
19461 : 334655 : gfc_charlen *cl;
19462 : 334655 : gfc_data *d;
19463 : 334655 : gfc_equiv *eq;
19464 : 334655 : gfc_namespace* old_ns = gfc_current_ns;
19465 : 334655 : bool recursive = ns->proc_name && ns->proc_name->attr.recursive;
19466 : :
19467 : 334655 : if (ns->types_resolved)
19468 : : return;
19469 : :
19470 : : /* Check that all IMPLICIT types are ok. */
19471 : 332210 : if (!ns->seen_implicit_none)
19472 : : {
19473 : : unsigned letter;
19474 : 8362252 : for (letter = 0; letter != GFC_LETTERS; ++letter)
19475 : 8052539 : if (ns->set_flag[letter]
19476 : 8052539 : && !resolve_typespec_used (&ns->default_type[letter],
19477 : : &ns->implicit_loc[letter], NULL))
19478 : : return;
19479 : : }
19480 : :
19481 : 332209 : gfc_current_ns = ns;
19482 : :
19483 : 332209 : resolve_entries (ns);
19484 : :
19485 : 332209 : resolve_common_vars (&ns->blank_common, false);
19486 : 332209 : resolve_common_blocks (ns->common_root);
19487 : :
19488 : 332209 : resolve_contained_functions (ns);
19489 : :
19490 : 332209 : if (ns->proc_name && ns->proc_name->attr.flavor == FL_PROCEDURE
19491 : 284274 : && ns->proc_name->attr.if_source == IFSRC_IFBODY)
19492 : 185724 : gfc_resolve_formal_arglist (ns->proc_name);
19493 : :
19494 : 332209 : gfc_traverse_ns (ns, resolve_bind_c_derived_types);
19495 : :
19496 : 423895 : for (cl = ns->cl_list; cl; cl = cl->next)
19497 : 91686 : resolve_charlen (cl);
19498 : :
19499 : 332209 : gfc_traverse_ns (ns, resolve_symbol);
19500 : :
19501 : 332209 : resolve_fntype (ns);
19502 : :
19503 : 377746 : for (n = ns->contained; n; n = n->sibling)
19504 : : {
19505 : : /* Exclude final wrappers with the test for the artificial attribute. */
19506 : 45537 : if (gfc_pure (ns->proc_name)
19507 : 5 : && !gfc_pure (n->proc_name)
19508 : 45537 : && !n->proc_name->attr.artificial)
19509 : 0 : gfc_error ("Contained procedure %qs at %L of a PURE procedure must "
19510 : : "also be PURE", n->proc_name->name,
19511 : : &n->proc_name->declared_at);
19512 : :
19513 : 45537 : resolve_types (n);
19514 : : }
19515 : :
19516 : 332209 : forall_flag = 0;
19517 : 332209 : gfc_do_concurrent_flag = 0;
19518 : 332209 : gfc_check_interfaces (ns);
19519 : :
19520 : 332209 : gfc_traverse_ns (ns, resolve_values);
19521 : :
19522 : 332209 : if (ns->save_all || (!flag_automatic && !recursive))
19523 : 296 : gfc_save_all (ns);
19524 : :
19525 : 332209 : iter_stack = NULL;
19526 : 334708 : for (d = ns->data; d; d = d->next)
19527 : 2499 : resolve_data (d);
19528 : :
19529 : 332209 : iter_stack = NULL;
19530 : 332209 : gfc_traverse_ns (ns, gfc_formalize_init_value);
19531 : :
19532 : 332209 : gfc_traverse_ns (ns, gfc_verify_binding_labels);
19533 : :
19534 : 333774 : for (eq = ns->equiv; eq; eq = eq->next)
19535 : 1565 : resolve_equivalence (eq);
19536 : :
19537 : : /* Warn about unused labels. */
19538 : 332209 : if (warn_unused_label)
19539 : 4465 : warn_unused_fortran_label (ns->st_labels);
19540 : :
19541 : 332209 : gfc_resolve_uops (ns->uop_root);
19542 : :
19543 : 332209 : gfc_traverse_ns (ns, gfc_verify_DTIO_procedures);
19544 : :
19545 : 332209 : gfc_resolve_omp_declare (ns);
19546 : :
19547 : 332209 : gfc_resolve_omp_udrs (ns->omp_udr_root);
19548 : :
19549 : 332209 : ns->types_resolved = 1;
19550 : :
19551 : 332209 : gfc_current_ns = old_ns;
19552 : : }
19553 : :
19554 : :
19555 : : /* Call gfc_resolve_code recursively. */
19556 : :
19557 : : static void
19558 : 334710 : resolve_codes (gfc_namespace *ns)
19559 : : {
19560 : 334710 : gfc_namespace *n;
19561 : 334710 : bitmap_obstack old_obstack;
19562 : :
19563 : 334710 : if (ns->resolved == 1)
19564 : 12897 : return;
19565 : :
19566 : 367405 : for (n = ns->contained; n; n = n->sibling)
19567 : 45592 : resolve_codes (n);
19568 : :
19569 : 321813 : gfc_current_ns = ns;
19570 : :
19571 : : /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
19572 : 321813 : if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
19573 : 310298 : cs_base = NULL;
19574 : :
19575 : : /* Set to an out of range value. */
19576 : 321813 : current_entry_id = -1;
19577 : :
19578 : 321813 : old_obstack = labels_obstack;
19579 : 321813 : bitmap_obstack_initialize (&labels_obstack);
19580 : :
19581 : 321813 : gfc_resolve_oacc_declare (ns);
19582 : 321813 : gfc_resolve_oacc_routines (ns);
19583 : 321813 : gfc_resolve_omp_local_vars (ns);
19584 : 321813 : if (ns->omp_allocate)
19585 : 55 : gfc_resolve_omp_allocate (ns, ns->omp_allocate);
19586 : 321813 : gfc_resolve_code (ns->code, ns);
19587 : :
19588 : 321812 : bitmap_obstack_release (&labels_obstack);
19589 : 321812 : labels_obstack = old_obstack;
19590 : : }
19591 : :
19592 : :
19593 : : /* This function is called after a complete program unit has been compiled.
19594 : : Its purpose is to examine all of the expressions associated with a program
19595 : : unit, assign types to all intermediate expressions, make sure that all
19596 : : assignments are to compatible types and figure out which names refer to
19597 : : which functions or subroutines. */
19598 : :
19599 : : void
19600 : 293611 : gfc_resolve (gfc_namespace *ns)
19601 : : {
19602 : 293611 : gfc_namespace *old_ns;
19603 : 293611 : code_stack *old_cs_base;
19604 : 293611 : struct gfc_omp_saved_state old_omp_state;
19605 : :
19606 : 293611 : if (ns->resolved)
19607 : 4493 : return;
19608 : :
19609 : 289118 : ns->resolved = -1;
19610 : 289118 : old_ns = gfc_current_ns;
19611 : 289118 : old_cs_base = cs_base;
19612 : :
19613 : : /* As gfc_resolve can be called during resolution of an OpenMP construct
19614 : : body, we should clear any state associated to it, so that say NS's
19615 : : DO loops are not interpreted as OpenMP loops. */
19616 : 289118 : if (!ns->construct_entities)
19617 : 277603 : gfc_omp_save_and_clear_state (&old_omp_state);
19618 : :
19619 : 289118 : resolve_types (ns);
19620 : 289118 : component_assignment_level = 0;
19621 : 289118 : resolve_codes (ns);
19622 : :
19623 : 289117 : if (ns->omp_assumes)
19624 : 13 : gfc_resolve_omp_assumptions (ns->omp_assumes);
19625 : :
19626 : 289117 : gfc_current_ns = old_ns;
19627 : 289117 : cs_base = old_cs_base;
19628 : 289117 : ns->resolved = 1;
19629 : :
19630 : 289117 : gfc_run_passes (ns);
19631 : :
19632 : 289117 : if (!ns->construct_entities)
19633 : 277602 : gfc_omp_restore_state (&old_omp_state);
19634 : : }
|