]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/resolve.c
re PR fortran/42048 ([F03] Erroneous syntax error message on TBP call)
[gcc.git] / gcc / fortran / resolve.c
CommitLineData
df2fba9e 1/* Perform type resolution on the various structures.
9be3684b 2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
edf1eac2 3 Free Software Foundation, Inc.
6de9cd9a
DN
4 Contributed by Andy Vaught
5
9fc4d79b 6This file is part of GCC.
6de9cd9a 7
9fc4d79b
TS
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
d234d788 10Software Foundation; either version 3, or (at your option) any later
9fc4d79b 11version.
6de9cd9a 12
9fc4d79b
TS
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
6de9cd9a
DN
17
18You should have received a copy of the GNU General Public License
d234d788
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
6de9cd9a
DN
21
22#include "config.h"
d22e4895 23#include "system.h"
994c1cc0 24#include "flags.h"
6de9cd9a 25#include "gfortran.h"
0615f923
TS
26#include "obstack.h"
27#include "bitmap.h"
6de9cd9a 28#include "arith.h" /* For gfc_compare_expr(). */
1524f80b 29#include "dependency.h"
ca39e6f2 30#include "data.h"
00a4618b 31#include "target-memory.h" /* for gfc_simplify_transfer */
d22e4895 32
e8ec07e1
PT
33/* Types used in equivalence statements. */
34
35typedef enum seq_type
36{
37 SEQ_NONDEFAULT, SEQ_NUMERIC, SEQ_CHARACTER, SEQ_MIXED
38}
39seq_type;
6de9cd9a 40
0615f923
TS
41/* Stack to keep track of the nesting of blocks as we move through the
42 code. See resolve_branch() and resolve_code(). */
6de9cd9a
DN
43
44typedef struct code_stack
45{
d80c695f 46 struct gfc_code *head, *current;
6de9cd9a 47 struct code_stack *prev;
0615f923
TS
48
49 /* This bitmap keeps track of the targets valid for a branch from
d80c695f
TS
50 inside this block except for END {IF|SELECT}s of enclosing
51 blocks. */
0615f923 52 bitmap reachable_labels;
6de9cd9a
DN
53}
54code_stack;
55
56static code_stack *cs_base = NULL;
57
58
6c7a4dfd 59/* Nonzero if we're inside a FORALL block. */
6de9cd9a
DN
60
61static int forall_flag;
62
6c7a4dfd
JJ
63/* Nonzero if we're inside a OpenMP WORKSHARE or PARALLEL WORKSHARE block. */
64
65static int omp_workshare_flag;
66
4213f93b
PT
67/* Nonzero if we are processing a formal arglist. The corresponding function
68 resets the flag each time that it is read. */
69static int formal_arg_flag = 0;
70
0e9a445b
PT
71/* True if we are resolving a specification expression. */
72static int specification_expr = 0;
73
74/* The id of the last entry seen. */
75static int current_entry_id;
76
0615f923
TS
77/* We use bitmaps to determine if a branch target is valid. */
78static bitmap_obstack labels_obstack;
79
4213f93b
PT
80int
81gfc_is_formal_arg (void)
82{
83 return formal_arg_flag;
84}
85
c867b7b6
PT
86/* Is the symbol host associated? */
87static bool
88is_sym_host_assoc (gfc_symbol *sym, gfc_namespace *ns)
89{
90 for (ns = ns->parent; ns; ns = ns->parent)
91 {
92 if (sym->ns == ns)
93 return true;
94 }
95
96 return false;
97}
52f49934
DK
98
99/* Ensure a typespec used is valid; for instance, TYPE(t) is invalid if t is
100 an ABSTRACT derived-type. If where is not NULL, an error message with that
101 locus is printed, optionally using name. */
102
103static gfc_try
104resolve_typespec_used (gfc_typespec* ts, locus* where, const char* name)
105{
bc21d315 106 if (ts->type == BT_DERIVED && ts->u.derived->attr.abstract)
52f49934
DK
107 {
108 if (where)
109 {
110 if (name)
111 gfc_error ("'%s' at %L is of the ABSTRACT type '%s'",
bc21d315 112 name, where, ts->u.derived->name);
52f49934
DK
113 else
114 gfc_error ("ABSTRACT type '%s' used at %L",
bc21d315 115 ts->u.derived->name, where);
52f49934
DK
116 }
117
118 return FAILURE;
119 }
120
121 return SUCCESS;
122}
123
124
6de9cd9a
DN
125/* Resolve types of formal argument lists. These have to be done early so that
126 the formal argument lists of module procedures can be copied to the
127 containing module before the individual procedures are resolved
128 individually. We also resolve argument lists of procedures in interface
129 blocks because they are self-contained scoping units.
130
131 Since a dummy argument cannot be a non-dummy procedure, the only
132 resort left for untyped names are the IMPLICIT types. */
133
134static void
edf1eac2 135resolve_formal_arglist (gfc_symbol *proc)
6de9cd9a
DN
136{
137 gfc_formal_arglist *f;
138 gfc_symbol *sym;
139 int i;
140
6de9cd9a
DN
141 if (proc->result != NULL)
142 sym = proc->result;
143 else
144 sym = proc;
145
146 if (gfc_elemental (proc)
147 || sym->attr.pointer || sym->attr.allocatable
148 || (sym->as && sym->as->rank > 0))
43e7fd21
FXC
149 {
150 proc->attr.always_explicit = 1;
151 sym->attr.always_explicit = 1;
152 }
6de9cd9a 153
4213f93b
PT
154 formal_arg_flag = 1;
155
6de9cd9a
DN
156 for (f = proc->formal; f; f = f->next)
157 {
158 sym = f->sym;
159
160 if (sym == NULL)
161 {
edf1eac2 162 /* Alternate return placeholder. */
6de9cd9a
DN
163 if (gfc_elemental (proc))
164 gfc_error ("Alternate return specifier in elemental subroutine "
165 "'%s' at %L is not allowed", proc->name,
166 &proc->declared_at);
edf1eac2
SK
167 if (proc->attr.function)
168 gfc_error ("Alternate return specifier in function "
169 "'%s' at %L is not allowed", proc->name,
170 &proc->declared_at);
6de9cd9a
DN
171 continue;
172 }
173
174 if (sym->attr.if_source != IFSRC_UNKNOWN)
175 resolve_formal_arglist (sym);
176
177 if (sym->attr.subroutine || sym->attr.external || sym->attr.intrinsic)
178 {
179 if (gfc_pure (proc) && !gfc_pure (sym))
180 {
edf1eac2
SK
181 gfc_error ("Dummy procedure '%s' of PURE procedure at %L must "
182 "also be PURE", sym->name, &sym->declared_at);
6de9cd9a
DN
183 continue;
184 }
185
186 if (gfc_elemental (proc))
187 {
edf1eac2
SK
188 gfc_error ("Dummy procedure at %L not allowed in ELEMENTAL "
189 "procedure", &sym->declared_at);
6de9cd9a
DN
190 continue;
191 }
192
20a037d5
PT
193 if (sym->attr.function
194 && sym->ts.type == BT_UNKNOWN
195 && sym->attr.intrinsic)
196 {
197 gfc_intrinsic_sym *isym;
198 isym = gfc_find_function (sym->name);
199 if (isym == NULL || !isym->specific)
200 {
201 gfc_error ("Unable to find a specific INTRINSIC procedure "
202 "for the reference '%s' at %L", sym->name,
203 &sym->declared_at);
204 }
205 sym->ts = isym->ts;
206 }
207
6de9cd9a
DN
208 continue;
209 }
210
211 if (sym->ts.type == BT_UNKNOWN)
212 {
213 if (!sym->attr.function || sym->result == sym)
214 gfc_set_default_type (sym, 1, sym->ns);
6de9cd9a
DN
215 }
216
217 gfc_resolve_array_spec (sym->as, 0);
218
219 /* We can't tell if an array with dimension (:) is assumed or deferred
edf1eac2 220 shape until we know if it has the pointer or allocatable attributes.
6de9cd9a
DN
221 */
222 if (sym->as && sym->as->rank > 0 && sym->as->type == AS_DEFERRED
edf1eac2
SK
223 && !(sym->attr.pointer || sym->attr.allocatable))
224 {
225 sym->as->type = AS_ASSUMED_SHAPE;
226 for (i = 0; i < sym->as->rank; i++)
227 sym->as->lower[i] = gfc_int_expr (1);
228 }
6de9cd9a
DN
229
230 if ((sym->as && sym->as->rank > 0 && sym->as->type == AS_ASSUMED_SHAPE)
edf1eac2
SK
231 || sym->attr.pointer || sym->attr.allocatable || sym->attr.target
232 || sym->attr.optional)
43e7fd21
FXC
233 {
234 proc->attr.always_explicit = 1;
235 if (proc->result)
236 proc->result->attr.always_explicit = 1;
237 }
6de9cd9a
DN
238
239 /* If the flavor is unknown at this point, it has to be a variable.
edf1eac2 240 A procedure specification would have already set the type. */
6de9cd9a
DN
241
242 if (sym->attr.flavor == FL_UNKNOWN)
231b2fcc 243 gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, &sym->declared_at);
6de9cd9a 244
c5bfb045 245 if (gfc_pure (proc) && !sym->attr.pointer
edf1eac2 246 && sym->attr.flavor != FL_PROCEDURE)
6de9cd9a 247 {
c5bfb045 248 if (proc->attr.function && sym->attr.intent != INTENT_IN)
6de9cd9a
DN
249 gfc_error ("Argument '%s' of pure function '%s' at %L must be "
250 "INTENT(IN)", sym->name, proc->name,
251 &sym->declared_at);
252
c5bfb045
PT
253 if (proc->attr.subroutine && sym->attr.intent == INTENT_UNKNOWN)
254 gfc_error ("Argument '%s' of pure subroutine '%s' at %L must "
255 "have its INTENT specified", sym->name, proc->name,
256 &sym->declared_at);
6de9cd9a
DN
257 }
258
6de9cd9a
DN
259 if (gfc_elemental (proc))
260 {
261 if (sym->as != NULL)
262 {
edf1eac2
SK
263 gfc_error ("Argument '%s' of elemental procedure at %L must "
264 "be scalar", sym->name, &sym->declared_at);
6de9cd9a
DN
265 continue;
266 }
267
268 if (sym->attr.pointer)
269 {
edf1eac2
SK
270 gfc_error ("Argument '%s' of elemental procedure at %L cannot "
271 "have the POINTER attribute", sym->name,
272 &sym->declared_at);
6de9cd9a
DN
273 continue;
274 }
242633d6
TB
275
276 if (sym->attr.flavor == FL_PROCEDURE)
277 {
278 gfc_error ("Dummy procedure '%s' not allowed in elemental "
279 "procedure '%s' at %L", sym->name, proc->name,
280 &sym->declared_at);
281 continue;
282 }
6de9cd9a
DN
283 }
284
285 /* Each dummy shall be specified to be scalar. */
286 if (proc->attr.proc == PROC_ST_FUNCTION)
edf1eac2
SK
287 {
288 if (sym->as != NULL)
289 {
290 gfc_error ("Argument '%s' of statement function at %L must "
291 "be scalar", sym->name, &sym->declared_at);
292 continue;
293 }
294
295 if (sym->ts.type == BT_CHARACTER)
296 {
bc21d315 297 gfc_charlen *cl = sym->ts.u.cl;
edf1eac2
SK
298 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
299 {
300 gfc_error ("Character-valued argument '%s' of statement "
301 "function at %L must have constant length",
302 sym->name, &sym->declared_at);
303 continue;
304 }
305 }
306 }
6de9cd9a 307 }
4213f93b 308 formal_arg_flag = 0;
6de9cd9a
DN
309}
310
311
312/* Work function called when searching for symbols that have argument lists
313 associated with them. */
314
315static void
edf1eac2 316find_arglists (gfc_symbol *sym)
6de9cd9a 317{
6de9cd9a
DN
318 if (sym->attr.if_source == IFSRC_UNKNOWN || sym->ns != gfc_current_ns)
319 return;
320
321 resolve_formal_arglist (sym);
322}
323
324
325/* Given a namespace, resolve all formal argument lists within the namespace.
326 */
327
328static void
edf1eac2 329resolve_formal_arglists (gfc_namespace *ns)
6de9cd9a 330{
6de9cd9a
DN
331 if (ns == NULL)
332 return;
333
334 gfc_traverse_ns (ns, find_arglists);
335}
336
337
3d79abbd 338static void
edf1eac2 339resolve_contained_fntype (gfc_symbol *sym, gfc_namespace *ns)
3d79abbd 340{
17b1d2a0 341 gfc_try t;
05c1e3a7 342
b5bf3e4d
TB
343 /* If this namespace is not a function or an entry master function,
344 ignore it. */
345 if (! sym || !(sym->attr.function || sym->attr.flavor == FL_VARIABLE)
346 || sym->attr.entry_master)
3d79abbd
PB
347 return;
348
0dd973dd 349 /* Try to find out of what the return type is. */
f9909823 350 if (sym->result->ts.type == BT_UNKNOWN && sym->result->ts.interface == NULL)
3d79abbd 351 {
c2de0c19 352 t = gfc_set_default_type (sym->result, 0, ns);
3d79abbd 353
c2de0c19 354 if (t == FAILURE && !sym->result->attr.untyped)
cf4d246b 355 {
c2de0c19
TB
356 if (sym->result == sym)
357 gfc_error ("Contained function '%s' at %L has no IMPLICIT type",
358 sym->name, &sym->declared_at);
3070bab4 359 else if (!sym->result->attr.proc_pointer)
c2de0c19
TB
360 gfc_error ("Result '%s' of contained function '%s' at %L has "
361 "no IMPLICIT type", sym->result->name, sym->name,
362 &sym->result->declared_at);
363 sym->result->attr.untyped = 1;
cf4d246b 364 }
3d79abbd 365 }
b95605fb 366
edf1eac2
SK
367 /* Fortran 95 Draft Standard, page 51, Section 5.1.1.5, on the Character
368 type, lists the only ways a character length value of * can be used:
369 dummy arguments of procedures, named constants, and function results
6c19d9b5
DK
370 in external functions. Internal function results and results of module
371 procedures are not on this list, ergo, not permitted. */
b95605fb 372
c2de0c19 373 if (sym->result->ts.type == BT_CHARACTER)
b95605fb 374 {
bc21d315 375 gfc_charlen *cl = sym->result->ts.u.cl;
b95605fb 376 if (!cl || !cl->length)
6c19d9b5
DK
377 {
378 /* See if this is a module-procedure and adapt error message
379 accordingly. */
380 bool module_proc;
381 gcc_assert (ns->parent && ns->parent->proc_name);
382 module_proc = (ns->parent->proc_name->attr.flavor == FL_MODULE);
383
384 gfc_error ("Character-valued %s '%s' at %L must not be"
385 " assumed length",
386 module_proc ? _("module procedure")
387 : _("internal function"),
388 sym->name, &sym->declared_at);
389 }
b95605fb 390 }
3d79abbd
PB
391}
392
393
394/* Add NEW_ARGS to the formal argument list of PROC, taking care not to
f7b529fa 395 introduce duplicates. */
3d79abbd
PB
396
397static void
398merge_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
399{
400 gfc_formal_arglist *f, *new_arglist;
401 gfc_symbol *new_sym;
402
403 for (; new_args != NULL; new_args = new_args->next)
404 {
405 new_sym = new_args->sym;
05c1e3a7 406 /* See if this arg is already in the formal argument list. */
3d79abbd
PB
407 for (f = proc->formal; f; f = f->next)
408 {
409 if (new_sym == f->sym)
410 break;
411 }
412
413 if (f)
414 continue;
415
416 /* Add a new argument. Argument order is not important. */
417 new_arglist = gfc_get_formal_arglist ();
418 new_arglist->sym = new_sym;
419 new_arglist->next = proc->formal;
420 proc->formal = new_arglist;
421 }
422}
423
424
54129a64
PT
425/* Flag the arguments that are not present in all entries. */
426
427static void
428check_argument_lists (gfc_symbol *proc, gfc_formal_arglist *new_args)
429{
430 gfc_formal_arglist *f, *head;
431 head = new_args;
432
433 for (f = proc->formal; f; f = f->next)
434 {
435 if (f->sym == NULL)
436 continue;
437
438 for (new_args = head; new_args; new_args = new_args->next)
439 {
440 if (new_args->sym == f->sym)
441 break;
442 }
443
444 if (new_args)
445 continue;
446
447 f->sym->attr.not_always_present = 1;
448 }
449}
450
451
3d79abbd
PB
452/* Resolve alternate entry points. If a symbol has multiple entry points we
453 create a new master symbol for the main routine, and turn the existing
454 symbol into an entry point. */
455
456static void
edf1eac2 457resolve_entries (gfc_namespace *ns)
3d79abbd
PB
458{
459 gfc_namespace *old_ns;
460 gfc_code *c;
461 gfc_symbol *proc;
462 gfc_entry_list *el;
463 char name[GFC_MAX_SYMBOL_LEN + 1];
464 static int master_count = 0;
465
466 if (ns->proc_name == NULL)
467 return;
468
469 /* No need to do anything if this procedure doesn't have alternate entry
470 points. */
471 if (!ns->entries)
472 return;
473
474 /* We may already have resolved alternate entry points. */
475 if (ns->proc_name->attr.entry_master)
476 return;
477
f7b529fa 478 /* If this isn't a procedure something has gone horribly wrong. */
6e45f57b 479 gcc_assert (ns->proc_name->attr.flavor == FL_PROCEDURE);
05c1e3a7 480
3d79abbd
PB
481 /* Remember the current namespace. */
482 old_ns = gfc_current_ns;
483
484 gfc_current_ns = ns;
485
486 /* Add the main entry point to the list of entry points. */
487 el = gfc_get_entry_list ();
488 el->sym = ns->proc_name;
489 el->id = 0;
490 el->next = ns->entries;
491 ns->entries = el;
492 ns->proc_name->attr.entry = 1;
493
1a492601
PT
494 /* If it is a module function, it needs to be in the right namespace
495 so that gfc_get_fake_result_decl can gather up the results. The
496 need for this arose in get_proc_name, where these beasts were
497 left in their own namespace, to keep prior references linked to
498 the entry declaration.*/
499 if (ns->proc_name->attr.function
edf1eac2 500 && ns->parent && ns->parent->proc_name->attr.flavor == FL_MODULE)
1a492601
PT
501 el->sym->ns = ns;
502
08ee9e85
PT
503 /* Do the same for entries where the master is not a module
504 procedure. These are retained in the module namespace because
505 of the module procedure declaration. */
506 for (el = el->next; el; el = el->next)
507 if (el->sym->ns->proc_name->attr.flavor == FL_MODULE
508 && el->sym->attr.mod_proc)
509 el->sym->ns = ns;
510 el = ns->entries;
511
3d79abbd
PB
512 /* Add an entry statement for it. */
513 c = gfc_get_code ();
514 c->op = EXEC_ENTRY;
515 c->ext.entry = el;
516 c->next = ns->code;
517 ns->code = c;
518
519 /* Create a new symbol for the master function. */
520 /* Give the internal function a unique name (within this file).
7be7d41b
TS
521 Also include the function name so the user has some hope of figuring
522 out what is going on. */
3d79abbd
PB
523 snprintf (name, GFC_MAX_SYMBOL_LEN, "master.%d.%s",
524 master_count++, ns->proc_name->name);
3d79abbd 525 gfc_get_ha_symbol (name, &proc);
6e45f57b 526 gcc_assert (proc != NULL);
3d79abbd 527
231b2fcc 528 gfc_add_procedure (&proc->attr, PROC_INTERNAL, proc->name, NULL);
3d79abbd 529 if (ns->proc_name->attr.subroutine)
231b2fcc 530 gfc_add_subroutine (&proc->attr, proc->name, NULL);
3d79abbd
PB
531 else
532 {
d198b59a
JJ
533 gfc_symbol *sym;
534 gfc_typespec *ts, *fts;
5be38273 535 gfc_array_spec *as, *fas;
231b2fcc 536 gfc_add_function (&proc->attr, proc->name, NULL);
d198b59a 537 proc->result = proc;
5be38273
PT
538 fas = ns->entries->sym->as;
539 fas = fas ? fas : ns->entries->sym->result->as;
d198b59a
JJ
540 fts = &ns->entries->sym->result->ts;
541 if (fts->type == BT_UNKNOWN)
713485cc 542 fts = gfc_get_default_type (ns->entries->sym->result->name, NULL);
d198b59a
JJ
543 for (el = ns->entries->next; el; el = el->next)
544 {
545 ts = &el->sym->result->ts;
5be38273
PT
546 as = el->sym->as;
547 as = as ? as : el->sym->result->as;
d198b59a 548 if (ts->type == BT_UNKNOWN)
713485cc 549 ts = gfc_get_default_type (el->sym->result->name, NULL);
5be38273 550
d198b59a
JJ
551 if (! gfc_compare_types (ts, fts)
552 || (el->sym->result->attr.dimension
553 != ns->entries->sym->result->attr.dimension)
554 || (el->sym->result->attr.pointer
555 != ns->entries->sym->result->attr.pointer))
556 break;
f5d67ede
PT
557 else if (as && fas && ns->entries->sym->result != el->sym->result
558 && gfc_compare_array_spec (as, fas) == 0)
107d5ff6 559 gfc_error ("Function %s at %L has entries with mismatched "
5be38273
PT
560 "array specifications", ns->entries->sym->name,
561 &ns->entries->sym->declared_at);
107d5ff6
TB
562 /* The characteristics need to match and thus both need to have
563 the same string length, i.e. both len=*, or both len=4.
564 Having both len=<variable> is also possible, but difficult to
565 check at compile time. */
bc21d315
JW
566 else if (ts->type == BT_CHARACTER && ts->u.cl && fts->u.cl
567 && (((ts->u.cl->length && !fts->u.cl->length)
568 ||(!ts->u.cl->length && fts->u.cl->length))
569 || (ts->u.cl->length
570 && ts->u.cl->length->expr_type
571 != fts->u.cl->length->expr_type)
572 || (ts->u.cl->length
573 && ts->u.cl->length->expr_type == EXPR_CONSTANT
574 && mpz_cmp (ts->u.cl->length->value.integer,
575 fts->u.cl->length->value.integer) != 0)))
107d5ff6
TB
576 gfc_notify_std (GFC_STD_GNU, "Extension: Function %s at %L with "
577 "entries returning variables of different "
578 "string lengths", ns->entries->sym->name,
579 &ns->entries->sym->declared_at);
d198b59a
JJ
580 }
581
582 if (el == NULL)
583 {
584 sym = ns->entries->sym->result;
585 /* All result types the same. */
586 proc->ts = *fts;
587 if (sym->attr.dimension)
588 gfc_set_array_spec (proc, gfc_copy_array_spec (sym->as), NULL);
589 if (sym->attr.pointer)
590 gfc_add_pointer (&proc->attr, NULL);
591 }
592 else
593 {
49de9e73 594 /* Otherwise the result will be passed through a union by
d198b59a
JJ
595 reference. */
596 proc->attr.mixed_entry_master = 1;
597 for (el = ns->entries; el; el = el->next)
598 {
599 sym = el->sym->result;
600 if (sym->attr.dimension)
edf1eac2
SK
601 {
602 if (el == ns->entries)
603 gfc_error ("FUNCTION result %s can't be an array in "
604 "FUNCTION %s at %L", sym->name,
605 ns->entries->sym->name, &sym->declared_at);
606 else
607 gfc_error ("ENTRY result %s can't be an array in "
608 "FUNCTION %s at %L", sym->name,
609 ns->entries->sym->name, &sym->declared_at);
610 }
d198b59a 611 else if (sym->attr.pointer)
edf1eac2
SK
612 {
613 if (el == ns->entries)
614 gfc_error ("FUNCTION result %s can't be a POINTER in "
615 "FUNCTION %s at %L", sym->name,
616 ns->entries->sym->name, &sym->declared_at);
617 else
618 gfc_error ("ENTRY result %s can't be a POINTER in "
619 "FUNCTION %s at %L", sym->name,
620 ns->entries->sym->name, &sym->declared_at);
621 }
d198b59a
JJ
622 else
623 {
624 ts = &sym->ts;
625 if (ts->type == BT_UNKNOWN)
713485cc 626 ts = gfc_get_default_type (sym->name, NULL);
d198b59a
JJ
627 switch (ts->type)
628 {
629 case BT_INTEGER:
630 if (ts->kind == gfc_default_integer_kind)
631 sym = NULL;
632 break;
633 case BT_REAL:
634 if (ts->kind == gfc_default_real_kind
635 || ts->kind == gfc_default_double_kind)
636 sym = NULL;
637 break;
638 case BT_COMPLEX:
639 if (ts->kind == gfc_default_complex_kind)
640 sym = NULL;
641 break;
642 case BT_LOGICAL:
643 if (ts->kind == gfc_default_logical_kind)
644 sym = NULL;
645 break;
cf4d246b
JJ
646 case BT_UNKNOWN:
647 /* We will issue error elsewhere. */
648 sym = NULL;
649 break;
d198b59a
JJ
650 default:
651 break;
652 }
653 if (sym)
edf1eac2
SK
654 {
655 if (el == ns->entries)
656 gfc_error ("FUNCTION result %s can't be of type %s "
657 "in FUNCTION %s at %L", sym->name,
658 gfc_typename (ts), ns->entries->sym->name,
659 &sym->declared_at);
660 else
661 gfc_error ("ENTRY result %s can't be of type %s "
662 "in FUNCTION %s at %L", sym->name,
663 gfc_typename (ts), ns->entries->sym->name,
664 &sym->declared_at);
665 }
d198b59a
JJ
666 }
667 }
668 }
3d79abbd
PB
669 }
670 proc->attr.access = ACCESS_PRIVATE;
671 proc->attr.entry_master = 1;
672
673 /* Merge all the entry point arguments. */
674 for (el = ns->entries; el; el = el->next)
675 merge_argument_lists (proc, el->sym->formal);
676
54129a64
PT
677 /* Check the master formal arguments for any that are not
678 present in all entry points. */
679 for (el = ns->entries; el; el = el->next)
680 check_argument_lists (proc, el->sym->formal);
681
7be7d41b 682 /* Use the master function for the function body. */
3d79abbd
PB
683 ns->proc_name = proc;
684
7be7d41b 685 /* Finalize the new symbols. */
3d79abbd
PB
686 gfc_commit_symbols ();
687
688 /* Restore the original namespace. */
689 gfc_current_ns = old_ns;
690}
691
692
448d2cd2
TS
693static bool
694has_default_initializer (gfc_symbol *der)
695{
696 gfc_component *c;
697
698 gcc_assert (der->attr.flavor == FL_DERIVED);
699 for (c = der->components; c; c = c->next)
700 if ((c->ts.type != BT_DERIVED && c->initializer)
701 || (c->ts.type == BT_DERIVED
bc21d315 702 && (!c->attr.pointer && has_default_initializer (c->ts.u.derived))))
448d2cd2
TS
703 break;
704
705 return c != NULL;
706}
707
346ecba8 708/* Resolve common variables. */
ad22b1ff 709static void
346ecba8 710resolve_common_vars (gfc_symbol *sym, bool named_common)
ad22b1ff 711{
346ecba8 712 gfc_symbol *csym = sym;
ad22b1ff 713
346ecba8 714 for (; csym; csym = csym->common_next)
041cf987 715 {
346ecba8
TB
716 if (csym->value || csym->attr.data)
717 {
718 if (!csym->ns->is_block_data)
719 gfc_notify_std (GFC_STD_GNU, "Variable '%s' at %L is in COMMON "
720 "but only in BLOCK DATA initialization is "
721 "allowed", csym->name, &csym->declared_at);
722 else if (!named_common)
723 gfc_notify_std (GFC_STD_GNU, "Initialized variable '%s' at %L is "
724 "in a blank COMMON but initialization is only "
725 "allowed in named common blocks", csym->name,
726 &csym->declared_at);
727 }
728
448d2cd2
TS
729 if (csym->ts.type != BT_DERIVED)
730 continue;
731
bc21d315
JW
732 if (!(csym->ts.u.derived->attr.sequence
733 || csym->ts.u.derived->attr.is_bind_c))
448d2cd2
TS
734 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
735 "has neither the SEQUENCE nor the BIND(C) "
736 "attribute", csym->name, &csym->declared_at);
bc21d315 737 if (csym->ts.u.derived->attr.alloc_comp)
448d2cd2
TS
738 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
739 "has an ultimate component that is "
740 "allocatable", csym->name, &csym->declared_at);
bc21d315 741 if (has_default_initializer (csym->ts.u.derived))
448d2cd2
TS
742 gfc_error_now ("Derived type variable '%s' in COMMON at %L "
743 "may not have default initializer", csym->name,
744 &csym->declared_at);
6f9c9d6d
TB
745
746 if (csym->attr.flavor == FL_UNKNOWN && !csym->attr.proc_pointer)
747 gfc_add_flavor (&csym->attr, FL_VARIABLE, csym->name, &csym->declared_at);
041cf987 748 }
346ecba8
TB
749}
750
751/* Resolve common blocks. */
752static void
753resolve_common_blocks (gfc_symtree *common_root)
754{
755 gfc_symbol *sym;
756
757 if (common_root == NULL)
758 return;
759
760 if (common_root->left)
761 resolve_common_blocks (common_root->left);
762 if (common_root->right)
763 resolve_common_blocks (common_root->right);
764
765 resolve_common_vars (common_root->n.common->head, true);
ad22b1ff 766
041cf987
TB
767 gfc_find_symbol (common_root->name, gfc_current_ns, 0, &sym);
768 if (sym == NULL)
769 return;
770
771 if (sym->attr.flavor == FL_PARAMETER)
772 gfc_error ("COMMON block '%s' at %L is used as PARAMETER at %L",
773 sym->name, &common_root->n.common->where, &sym->declared_at);
774
775 if (sym->attr.intrinsic)
776 gfc_error ("COMMON block '%s' at %L is also an intrinsic procedure",
777 sym->name, &common_root->n.common->where);
778 else if (sym->attr.result
2d71b918 779 || gfc_is_function_return_value (sym, gfc_current_ns))
041cf987
TB
780 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
781 "that is also a function result", sym->name,
782 &common_root->n.common->where);
783 else if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_INTERNAL
784 && sym->attr.proc != PROC_ST_FUNCTION)
785 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: COMMON block '%s' at %L "
786 "that is also a global procedure", sym->name,
787 &common_root->n.common->where);
ad22b1ff
TB
788}
789
790
6de9cd9a
DN
791/* Resolve contained function types. Because contained functions can call one
792 another, they have to be worked out before any of the contained procedures
793 can be resolved.
794
795 The good news is that if a function doesn't already have a type, the only
796 way it can get one is through an IMPLICIT type or a RESULT variable, because
797 by definition contained functions are contained namespace they're contained
798 in, not in a sibling or parent namespace. */
799
800static void
edf1eac2 801resolve_contained_functions (gfc_namespace *ns)
6de9cd9a 802{
6de9cd9a 803 gfc_namespace *child;
3d79abbd 804 gfc_entry_list *el;
6de9cd9a
DN
805
806 resolve_formal_arglists (ns);
807
808 for (child = ns->contained; child; child = child->sibling)
809 {
3d79abbd 810 /* Resolve alternate entry points first. */
05c1e3a7 811 resolve_entries (child);
6de9cd9a 812
3d79abbd
PB
813 /* Then check function return types. */
814 resolve_contained_fntype (child->proc_name, child);
815 for (el = child->entries; el; el = el->next)
816 resolve_contained_fntype (el->sym, child);
6de9cd9a
DN
817 }
818}
819
820
821/* Resolve all of the elements of a structure constructor and make sure that
f7b529fa 822 the types are correct. */
6de9cd9a 823
17b1d2a0 824static gfc_try
edf1eac2 825resolve_structure_cons (gfc_expr *expr)
6de9cd9a
DN
826{
827 gfc_constructor *cons;
828 gfc_component *comp;
17b1d2a0 829 gfc_try t;
5046aff5 830 symbol_attribute a;
6de9cd9a
DN
831
832 t = SUCCESS;
833 cons = expr->value.constructor;
834 /* A constructor may have references if it is the result of substituting a
835 parameter variable. In this case we just pull out the component we
836 want. */
837 if (expr->ref)
838 comp = expr->ref->u.c.sym->components;
839 else
bc21d315 840 comp = expr->ts.u.derived->components;
6de9cd9a 841
36dcec91
CR
842 /* See if the user is trying to invoke a structure constructor for one of
843 the iso_c_binding derived types. */
a2a0778d
JW
844 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
845 && expr->ts.u.derived->ts.is_iso_c && cons && cons->expr != NULL)
36dcec91
CR
846 {
847 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
bc21d315 848 expr->ts.u.derived->name, &(expr->where));
36dcec91
CR
849 return FAILURE;
850 }
851
6de9cd9a
DN
852 for (; comp; comp = comp->next, cons = cons->next)
853 {
0df50e7a
FXC
854 int rank;
855
edf1eac2 856 if (!cons->expr)
404d8401 857 continue;
6de9cd9a
DN
858
859 if (gfc_resolve_expr (cons->expr) == FAILURE)
860 {
861 t = FAILURE;
862 continue;
863 }
864
0df50e7a
FXC
865 rank = comp->as ? comp->as->rank : 0;
866 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
d4b7d0f0 867 && (comp->attr.allocatable || cons->expr->rank))
5046aff5
PT
868 {
869 gfc_error ("The rank of the element in the derived type "
870 "constructor at %L does not match that of the "
871 "component (%d/%d)", &cons->expr->where,
0df50e7a 872 cons->expr->rank, rank);
5046aff5
PT
873 t = FAILURE;
874 }
875
6de9cd9a
DN
876 /* If we don't have the right type, try to convert it. */
877
e0e85e06
PT
878 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
879 {
880 t = FAILURE;
d4b7d0f0 881 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
e0e85e06
PT
882 gfc_error ("The element in the derived type constructor at %L, "
883 "for pointer component '%s', is %s but should be %s",
884 &cons->expr->where, comp->name,
885 gfc_basic_typename (cons->expr->ts.type),
886 gfc_basic_typename (comp->ts.type));
887 else
888 t = gfc_convert_type (cons->expr, &comp->ts, 1);
889 }
5046aff5 890
c1203a70 891 if (cons->expr->expr_type == EXPR_NULL
713485cc 892 && !(comp->attr.pointer || comp->attr.allocatable
cf2b3c22
TB
893 || comp->attr.proc_pointer
894 || (comp->ts.type == BT_CLASS
895 && (comp->ts.u.derived->components->attr.pointer
896 || comp->ts.u.derived->components->attr.allocatable))))
c1203a70
PT
897 {
898 t = FAILURE;
899 gfc_error ("The NULL in the derived type constructor at %L is "
900 "being applied to component '%s', which is neither "
901 "a POINTER nor ALLOCATABLE", &cons->expr->where,
902 comp->name);
903 }
904
d4b7d0f0 905 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
5046aff5
PT
906 continue;
907
908 a = gfc_expr_attr (cons->expr);
909
910 if (!a.pointer && !a.target)
911 {
912 t = FAILURE;
913 gfc_error ("The element in the derived type constructor at %L, "
914 "for pointer component '%s' should be a POINTER or "
915 "a TARGET", &cons->expr->where, comp->name);
916 }
6de9cd9a
DN
917 }
918
919 return t;
920}
921
922
6de9cd9a
DN
923/****************** Expression name resolution ******************/
924
925/* Returns 0 if a symbol was not declared with a type or
4f613946 926 attribute declaration statement, nonzero otherwise. */
6de9cd9a
DN
927
928static int
edf1eac2 929was_declared (gfc_symbol *sym)
6de9cd9a
DN
930{
931 symbol_attribute a;
932
933 a = sym->attr;
934
935 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
936 return 1;
937
9439ae41 938 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
edf1eac2
SK
939 || a.optional || a.pointer || a.save || a.target || a.volatile_
940 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN)
6de9cd9a
DN
941 return 1;
942
943 return 0;
944}
945
946
947/* Determine if a symbol is generic or not. */
948
949static int
edf1eac2 950generic_sym (gfc_symbol *sym)
6de9cd9a
DN
951{
952 gfc_symbol *s;
953
954 if (sym->attr.generic ||
955 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
956 return 1;
957
958 if (was_declared (sym) || sym->ns->parent == NULL)
959 return 0;
960
961 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
6d023ec5
JD
962
963 if (s != NULL)
964 {
965 if (s == sym)
966 return 0;
967 else
968 return generic_sym (s);
969 }
6de9cd9a 970
6d023ec5 971 return 0;
6de9cd9a
DN
972}
973
974
975/* Determine if a symbol is specific or not. */
976
977static int
edf1eac2 978specific_sym (gfc_symbol *sym)
6de9cd9a
DN
979{
980 gfc_symbol *s;
981
982 if (sym->attr.if_source == IFSRC_IFBODY
983 || sym->attr.proc == PROC_MODULE
984 || sym->attr.proc == PROC_INTERNAL
985 || sym->attr.proc == PROC_ST_FUNCTION
edf1eac2 986 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
6de9cd9a
DN
987 || sym->attr.external)
988 return 1;
989
990 if (was_declared (sym) || sym->ns->parent == NULL)
991 return 0;
992
993 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
994
995 return (s == NULL) ? 0 : specific_sym (s);
996}
997
998
999/* Figure out if the procedure is specific, generic or unknown. */
1000
1001typedef enum
1002{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1003proc_type;
1004
1005static proc_type
edf1eac2 1006procedure_kind (gfc_symbol *sym)
6de9cd9a 1007{
6de9cd9a
DN
1008 if (generic_sym (sym))
1009 return PTYPE_GENERIC;
1010
1011 if (specific_sym (sym))
1012 return PTYPE_SPECIFIC;
1013
1014 return PTYPE_UNKNOWN;
1015}
1016
48474141 1017/* Check references to assumed size arrays. The flag need_full_assumed_size
b82feea5 1018 is nonzero when matching actual arguments. */
48474141
PT
1019
1020static int need_full_assumed_size = 0;
1021
1022static bool
edf1eac2 1023check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
48474141 1024{
edf1eac2 1025 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
48474141
PT
1026 return false;
1027
e0c68ce9
ILT
1028 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1029 What should it be? */
c52938ec
PT
1030 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1031 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
e0c68ce9 1032 && (e->ref->u.ar.type == AR_FULL))
48474141
PT
1033 {
1034 gfc_error ("The upper bound in the last dimension must "
1035 "appear in the reference to the assumed size "
e25a0da3 1036 "array '%s' at %L", sym->name, &e->where);
48474141
PT
1037 return true;
1038 }
1039 return false;
1040}
1041
1042
1043/* Look for bad assumed size array references in argument expressions
1044 of elemental and array valued intrinsic procedures. Since this is
1045 called from procedure resolution functions, it only recurses at
1046 operators. */
1047
1048static bool
1049resolve_assumed_size_actual (gfc_expr *e)
1050{
1051 if (e == NULL)
1052 return false;
1053
1054 switch (e->expr_type)
1055 {
1056 case EXPR_VARIABLE:
edf1eac2 1057 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
48474141
PT
1058 return true;
1059 break;
1060
1061 case EXPR_OP:
1062 if (resolve_assumed_size_actual (e->value.op.op1)
edf1eac2 1063 || resolve_assumed_size_actual (e->value.op.op2))
48474141
PT
1064 return true;
1065 break;
1066
1067 default:
1068 break;
1069 }
1070 return false;
1071}
1072
6de9cd9a 1073
0b4e2af7
PT
1074/* Check a generic procedure, passed as an actual argument, to see if
1075 there is a matching specific name. If none, it is an error, and if
1076 more than one, the reference is ambiguous. */
1077static int
1078count_specific_procs (gfc_expr *e)
1079{
1080 int n;
1081 gfc_interface *p;
1082 gfc_symbol *sym;
1083
1084 n = 0;
1085 sym = e->symtree->n.sym;
1086
1087 for (p = sym->generic; p; p = p->next)
1088 if (strcmp (sym->name, p->sym->name) == 0)
1089 {
1090 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1091 sym->name);
1092 n++;
1093 }
1094
1095 if (n > 1)
1096 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1097 &e->where);
1098
1099 if (n == 0)
1100 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1101 "argument at %L", sym->name, &e->where);
1102
1103 return n;
1104}
1105
a03826d1 1106
1933ba0f
DK
1107/* See if a call to sym could possibly be a not allowed RECURSION because of
1108 a missing RECURIVE declaration. This means that either sym is the current
1109 context itself, or sym is the parent of a contained procedure calling its
1110 non-RECURSIVE containing procedure.
1111 This also works if sym is an ENTRY. */
1112
1113static bool
1114is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1115{
1116 gfc_symbol* proc_sym;
1117 gfc_symbol* context_proc;
9abe5e56 1118 gfc_namespace* real_context;
1933ba0f 1119
6f7e06ce
JD
1120 if (sym->attr.flavor == FL_PROGRAM)
1121 return false;
1122
1933ba0f
DK
1123 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1124
1125 /* If we've got an ENTRY, find real procedure. */
1126 if (sym->attr.entry && sym->ns->entries)
1127 proc_sym = sym->ns->entries->sym;
1128 else
1129 proc_sym = sym;
1130
1131 /* If sym is RECURSIVE, all is well of course. */
1132 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1133 return false;
1134
9abe5e56
DK
1135 /* Find the context procedure's "real" symbol if it has entries.
1136 We look for a procedure symbol, so recurse on the parents if we don't
1137 find one (like in case of a BLOCK construct). */
1138 for (real_context = context; ; real_context = real_context->parent)
1139 {
1140 /* We should find something, eventually! */
1141 gcc_assert (real_context);
1142
1143 context_proc = (real_context->entries ? real_context->entries->sym
1144 : real_context->proc_name);
1145
1146 /* In some special cases, there may not be a proc_name, like for this
1147 invalid code:
1148 real(bad_kind()) function foo () ...
1149 when checking the call to bad_kind ().
1150 In these cases, we simply return here and assume that the
1151 call is ok. */
1152 if (!context_proc)
1153 return false;
1154
1155 if (context_proc->attr.flavor != FL_LABEL)
1156 break;
1157 }
1933ba0f
DK
1158
1159 /* A call from sym's body to itself is recursion, of course. */
1160 if (context_proc == proc_sym)
1161 return true;
1162
1163 /* The same is true if context is a contained procedure and sym the
1164 containing one. */
1165 if (context_proc->attr.contained)
1166 {
1167 gfc_symbol* parent_proc;
1168
1169 gcc_assert (context->parent);
1170 parent_proc = (context->parent->entries ? context->parent->entries->sym
1171 : context->parent->proc_name);
1172
1173 if (parent_proc == proc_sym)
1174 return true;
1175 }
1176
1177 return false;
1178}
1179
1180
c73b6478
JW
1181/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1182 its typespec and formal argument list. */
1183
1184static gfc_try
1185resolve_intrinsic (gfc_symbol *sym, locus *loc)
1186{
f6038131
JW
1187 gfc_intrinsic_sym* isym;
1188 const char* symstd;
1189
1190 if (sym->formal)
1191 return SUCCESS;
1192
1193 /* We already know this one is an intrinsic, so we don't call
1194 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1195 gfc_find_subroutine directly to check whether it is a function or
1196 subroutine. */
1197
1198 if ((isym = gfc_find_function (sym->name)))
c73b6478 1199 {
f6038131
JW
1200 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1201 && !sym->attr.implicit_type)
1202 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1203 " ignored", sym->name, &sym->declared_at);
1204
c73b6478
JW
1205 if (!sym->attr.function &&
1206 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1207 return FAILURE;
f6038131 1208
c73b6478
JW
1209 sym->ts = isym->ts;
1210 }
f6038131 1211 else if ((isym = gfc_find_subroutine (sym->name)))
c73b6478 1212 {
f6038131
JW
1213 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1214 {
1215 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1216 " specifier", sym->name, &sym->declared_at);
1217 return FAILURE;
1218 }
1219
c73b6478
JW
1220 if (!sym->attr.subroutine &&
1221 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1222 return FAILURE;
1223 }
f6038131
JW
1224 else
1225 {
1226 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1227 &sym->declared_at);
1228 return FAILURE;
1229 }
1230
1231 gfc_copy_formal_args_intr (sym, isym);
1232
1233 /* Check it is actually available in the standard settings. */
1234 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1235 == FAILURE)
1236 {
1237 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1238 " available in the current standard settings but %s. Use"
1239 " an appropriate -std=* option or enable -fall-intrinsics"
1240 " in order to use it.",
1241 sym->name, &sym->declared_at, symstd);
1242 return FAILURE;
1243 }
1244
c73b6478
JW
1245 return SUCCESS;
1246}
1247
1248
a03826d1
DK
1249/* Resolve a procedure expression, like passing it to a called procedure or as
1250 RHS for a procedure pointer assignment. */
1251
1252static gfc_try
1253resolve_procedure_expression (gfc_expr* expr)
1254{
1255 gfc_symbol* sym;
1256
1933ba0f 1257 if (expr->expr_type != EXPR_VARIABLE)
a03826d1
DK
1258 return SUCCESS;
1259 gcc_assert (expr->symtree);
1933ba0f 1260
a03826d1 1261 sym = expr->symtree->n.sym;
c73b6478
JW
1262
1263 if (sym->attr.intrinsic)
1264 resolve_intrinsic (sym, &expr->where);
1265
1933ba0f
DK
1266 if (sym->attr.flavor != FL_PROCEDURE
1267 || (sym->attr.function && sym->result == sym))
1268 return SUCCESS;
a03826d1
DK
1269
1270 /* A non-RECURSIVE procedure that is used as procedure expression within its
1271 own body is in danger of being called recursively. */
1933ba0f 1272 if (is_illegal_recursion (sym, gfc_current_ns))
a03826d1
DK
1273 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1274 " itself recursively. Declare it RECURSIVE or use"
1275 " -frecursive", sym->name, &expr->where);
1276
1277 return SUCCESS;
1278}
1279
1280
6de9cd9a
DN
1281/* Resolve an actual argument list. Most of the time, this is just
1282 resolving the expressions in the list.
1283 The exception is that we sometimes have to decide whether arguments
1284 that look like procedure arguments are really simple variable
1285 references. */
1286
17b1d2a0 1287static gfc_try
0b4e2af7
PT
1288resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1289 bool no_formal_args)
6de9cd9a
DN
1290{
1291 gfc_symbol *sym;
1292 gfc_symtree *parent_st;
1293 gfc_expr *e;
5ad6345e 1294 int save_need_full_assumed_size;
713485cc 1295 gfc_component *comp;
0b4e2af7 1296
6de9cd9a
DN
1297 for (; arg; arg = arg->next)
1298 {
6de9cd9a
DN
1299 e = arg->expr;
1300 if (e == NULL)
edf1eac2
SK
1301 {
1302 /* Check the label is a valid branching target. */
1303 if (arg->label)
1304 {
1305 if (arg->label->defined == ST_LABEL_UNKNOWN)
1306 {
1307 gfc_error ("Label %d referenced at %L is never defined",
1308 arg->label->value, &arg->label->where);
1309 return FAILURE;
1310 }
1311 }
1312 continue;
1313 }
6de9cd9a 1314
f64edc8b 1315 if (gfc_is_proc_ptr_comp (e, &comp))
713485cc
JW
1316 {
1317 e->ts = comp->ts;
23878536 1318 if (e->expr_type == EXPR_PPC)
acbdc378
JW
1319 {
1320 if (comp->as != NULL)
1321 e->rank = comp->as->rank;
1322 e->expr_type = EXPR_FUNCTION;
1323 }
6c036626
JW
1324 if (gfc_resolve_expr (e) == FAILURE)
1325 return FAILURE;
713485cc
JW
1326 goto argument_list;
1327 }
1328
67cec813 1329 if (e->expr_type == EXPR_VARIABLE
0b4e2af7
PT
1330 && e->symtree->n.sym->attr.generic
1331 && no_formal_args
1332 && count_specific_procs (e) != 1)
1333 return FAILURE;
27372c38 1334
6de9cd9a
DN
1335 if (e->ts.type != BT_PROCEDURE)
1336 {
5ad6345e 1337 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1338 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1339 need_full_assumed_size = 0;
6de9cd9a
DN
1340 if (gfc_resolve_expr (e) != SUCCESS)
1341 return FAILURE;
5ad6345e 1342 need_full_assumed_size = save_need_full_assumed_size;
7fcafa71 1343 goto argument_list;
6de9cd9a
DN
1344 }
1345
edf1eac2 1346 /* See if the expression node should really be a variable reference. */
6de9cd9a
DN
1347
1348 sym = e->symtree->n.sym;
1349
1350 if (sym->attr.flavor == FL_PROCEDURE
1351 || sym->attr.intrinsic
1352 || sym->attr.external)
1353 {
0e7e7e6e 1354 int actual_ok;
6de9cd9a 1355
d68bd5a8
PT
1356 /* If a procedure is not already determined to be something else
1357 check if it is intrinsic. */
1358 if (!sym->attr.intrinsic
edf1eac2
SK
1359 && !(sym->attr.external || sym->attr.use_assoc
1360 || sym->attr.if_source == IFSRC_IFBODY)
c3005b0f 1361 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
d68bd5a8
PT
1362 sym->attr.intrinsic = 1;
1363
2ed8d224
PT
1364 if (sym->attr.proc == PROC_ST_FUNCTION)
1365 {
1366 gfc_error ("Statement function '%s' at %L is not allowed as an "
1367 "actual argument", sym->name, &e->where);
1368 }
1369
edf1eac2
SK
1370 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1371 sym->attr.subroutine);
0e7e7e6e
FXC
1372 if (sym->attr.intrinsic && actual_ok == 0)
1373 {
1374 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1375 "actual argument", sym->name, &e->where);
1376 }
0e7e7e6e 1377
2ed8d224
PT
1378 if (sym->attr.contained && !sym->attr.use_assoc
1379 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1380 {
1381 gfc_error ("Internal procedure '%s' is not allowed as an "
1382 "actual argument at %L", sym->name, &e->where);
1383 }
1384
1385 if (sym->attr.elemental && !sym->attr.intrinsic)
1386 {
1387 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
edf1eac2 1388 "allowed as an actual argument at %L", sym->name,
2ed8d224
PT
1389 &e->where);
1390 }
781e1004 1391
36d3fb4c
PT
1392 /* Check if a generic interface has a specific procedure
1393 with the same name before emitting an error. */
0b4e2af7
PT
1394 if (sym->attr.generic && count_specific_procs (e) != 1)
1395 return FAILURE;
1396
1397 /* Just in case a specific was found for the expression. */
1398 sym = e->symtree->n.sym;
3e978d30 1399
6de9cd9a
DN
1400 /* If the symbol is the function that names the current (or
1401 parent) scope, then we really have a variable reference. */
1402
2d71b918 1403 if (gfc_is_function_return_value (sym, sym->ns))
6de9cd9a
DN
1404 goto got_variable;
1405
20a037d5 1406 /* If all else fails, see if we have a specific intrinsic. */
26033479 1407 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
20a037d5
PT
1408 {
1409 gfc_intrinsic_sym *isym;
6cc309c9 1410
20a037d5
PT
1411 isym = gfc_find_function (sym->name);
1412 if (isym == NULL || !isym->specific)
1413 {
1414 gfc_error ("Unable to find a specific INTRINSIC procedure "
1415 "for the reference '%s' at %L", sym->name,
1416 &e->where);
26033479 1417 return FAILURE;
20a037d5
PT
1418 }
1419 sym->ts = isym->ts;
6cc309c9 1420 sym->attr.intrinsic = 1;
26033479 1421 sym->attr.function = 1;
20a037d5 1422 }
a03826d1
DK
1423
1424 if (gfc_resolve_expr (e) == FAILURE)
1425 return FAILURE;
7fcafa71 1426 goto argument_list;
6de9cd9a
DN
1427 }
1428
1429 /* See if the name is a module procedure in a parent unit. */
1430
1431 if (was_declared (sym) || sym->ns->parent == NULL)
1432 goto got_variable;
1433
1434 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1435 {
1436 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1437 return FAILURE;
1438 }
1439
1440 if (parent_st == NULL)
1441 goto got_variable;
1442
1443 sym = parent_st->n.sym;
1444 e->symtree = parent_st; /* Point to the right thing. */
1445
1446 if (sym->attr.flavor == FL_PROCEDURE
1447 || sym->attr.intrinsic
1448 || sym->attr.external)
1449 {
a03826d1
DK
1450 if (gfc_resolve_expr (e) == FAILURE)
1451 return FAILURE;
7fcafa71 1452 goto argument_list;
6de9cd9a
DN
1453 }
1454
1455 got_variable:
1456 e->expr_type = EXPR_VARIABLE;
1457 e->ts = sym->ts;
1458 if (sym->as != NULL)
1459 {
1460 e->rank = sym->as->rank;
1461 e->ref = gfc_get_ref ();
1462 e->ref->type = REF_ARRAY;
1463 e->ref->u.ar.type = AR_FULL;
1464 e->ref->u.ar.as = sym->as;
1465 }
7fcafa71 1466
1b35264f
DF
1467 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1468 primary.c (match_actual_arg). If above code determines that it
1469 is a variable instead, it needs to be resolved as it was not
1470 done at the beginning of this function. */
5ad6345e 1471 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1472 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1473 need_full_assumed_size = 0;
1b35264f
DF
1474 if (gfc_resolve_expr (e) != SUCCESS)
1475 return FAILURE;
5ad6345e 1476 need_full_assumed_size = save_need_full_assumed_size;
1b35264f 1477
7fcafa71
PT
1478 argument_list:
1479 /* Check argument list functions %VAL, %LOC and %REF. There is
1480 nothing to do for %REF. */
1481 if (arg->name && arg->name[0] == '%')
1482 {
1483 if (strncmp ("%VAL", arg->name, 4) == 0)
1484 {
1485 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1486 {
1487 gfc_error ("By-value argument at %L is not of numeric "
1488 "type", &e->where);
1489 return FAILURE;
1490 }
1491
1492 if (e->rank)
1493 {
1494 gfc_error ("By-value argument at %L cannot be an array or "
1495 "an array section", &e->where);
1496 return FAILURE;
1497 }
1498
1499 /* Intrinsics are still PROC_UNKNOWN here. However,
1500 since same file external procedures are not resolvable
1501 in gfortran, it is a good deal easier to leave them to
1502 intrinsic.c. */
7193e30a
TB
1503 if (ptype != PROC_UNKNOWN
1504 && ptype != PROC_DUMMY
29ea08da
TB
1505 && ptype != PROC_EXTERNAL
1506 && ptype != PROC_MODULE)
7fcafa71
PT
1507 {
1508 gfc_error ("By-value argument at %L is not allowed "
1509 "in this context", &e->where);
1510 return FAILURE;
1511 }
7fcafa71
PT
1512 }
1513
1514 /* Statement functions have already been excluded above. */
1515 else if (strncmp ("%LOC", arg->name, 4) == 0
edf1eac2 1516 && e->ts.type == BT_PROCEDURE)
7fcafa71
PT
1517 {
1518 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1519 {
1520 gfc_error ("Passing internal procedure at %L by location "
1521 "not allowed", &e->where);
1522 return FAILURE;
1523 }
1524 }
1525 }
6de9cd9a
DN
1526 }
1527
1528 return SUCCESS;
1529}
1530
1531
b8ea6dbc
PT
1532/* Do the checks of the actual argument list that are specific to elemental
1533 procedures. If called with c == NULL, we have a function, otherwise if
1534 expr == NULL, we have a subroutine. */
edf1eac2 1535
17b1d2a0 1536static gfc_try
b8ea6dbc
PT
1537resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1538{
1539 gfc_actual_arglist *arg0;
1540 gfc_actual_arglist *arg;
1541 gfc_symbol *esym = NULL;
1542 gfc_intrinsic_sym *isym = NULL;
1543 gfc_expr *e = NULL;
1544 gfc_intrinsic_arg *iformal = NULL;
1545 gfc_formal_arglist *eformal = NULL;
1546 bool formal_optional = false;
1547 bool set_by_optional = false;
1548 int i;
1549 int rank = 0;
1550
1551 /* Is this an elemental procedure? */
1552 if (expr && expr->value.function.actual != NULL)
1553 {
1554 if (expr->value.function.esym != NULL
edf1eac2 1555 && expr->value.function.esym->attr.elemental)
b8ea6dbc
PT
1556 {
1557 arg0 = expr->value.function.actual;
1558 esym = expr->value.function.esym;
1559 }
1560 else if (expr->value.function.isym != NULL
edf1eac2 1561 && expr->value.function.isym->elemental)
b8ea6dbc
PT
1562 {
1563 arg0 = expr->value.function.actual;
1564 isym = expr->value.function.isym;
1565 }
1566 else
1567 return SUCCESS;
1568 }
dd9315de 1569 else if (c && c->ext.actual != NULL)
b8ea6dbc
PT
1570 {
1571 arg0 = c->ext.actual;
dd9315de
DK
1572
1573 if (c->resolved_sym)
1574 esym = c->resolved_sym;
1575 else
1576 esym = c->symtree->n.sym;
1577 gcc_assert (esym);
1578
1579 if (!esym->attr.elemental)
1580 return SUCCESS;
b8ea6dbc
PT
1581 }
1582 else
1583 return SUCCESS;
1584
1585 /* The rank of an elemental is the rank of its array argument(s). */
1586 for (arg = arg0; arg; arg = arg->next)
1587 {
1588 if (arg->expr != NULL && arg->expr->rank > 0)
1589 {
1590 rank = arg->expr->rank;
1591 if (arg->expr->expr_type == EXPR_VARIABLE
edf1eac2 1592 && arg->expr->symtree->n.sym->attr.optional)
b8ea6dbc
PT
1593 set_by_optional = true;
1594
1595 /* Function specific; set the result rank and shape. */
1596 if (expr)
1597 {
1598 expr->rank = rank;
1599 if (!expr->shape && arg->expr->shape)
1600 {
1601 expr->shape = gfc_get_shape (rank);
1602 for (i = 0; i < rank; i++)
1603 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1604 }
1605 }
1606 break;
1607 }
1608 }
1609
1610 /* If it is an array, it shall not be supplied as an actual argument
1611 to an elemental procedure unless an array of the same rank is supplied
1612 as an actual argument corresponding to a nonoptional dummy argument of
1613 that elemental procedure(12.4.1.5). */
1614 formal_optional = false;
1615 if (isym)
1616 iformal = isym->formal;
1617 else
1618 eformal = esym->formal;
1619
1620 for (arg = arg0; arg; arg = arg->next)
1621 {
1622 if (eformal)
1623 {
1624 if (eformal->sym && eformal->sym->attr.optional)
1625 formal_optional = true;
1626 eformal = eformal->next;
1627 }
1628 else if (isym && iformal)
1629 {
1630 if (iformal->optional)
1631 formal_optional = true;
1632 iformal = iformal->next;
1633 }
1634 else if (isym)
1635 formal_optional = true;
1636
994c1cc0 1637 if (pedantic && arg->expr != NULL
edf1eac2
SK
1638 && arg->expr->expr_type == EXPR_VARIABLE
1639 && arg->expr->symtree->n.sym->attr.optional
1640 && formal_optional
1641 && arg->expr->rank
1642 && (set_by_optional || arg->expr->rank != rank)
cd5ecab6 1643 && !(isym && isym->id == GFC_ISYM_CONVERSION))
b8ea6dbc 1644 {
994c1cc0
SK
1645 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1646 "MISSING, it cannot be the actual argument of an "
edf1eac2 1647 "ELEMENTAL procedure unless there is a non-optional "
994c1cc0
SK
1648 "argument with the same rank (12.4.1.5)",
1649 arg->expr->symtree->n.sym->name, &arg->expr->where);
b8ea6dbc
PT
1650 return FAILURE;
1651 }
1652 }
1653
1654 for (arg = arg0; arg; arg = arg->next)
1655 {
1656 if (arg->expr == NULL || arg->expr->rank == 0)
1657 continue;
1658
1659 /* Being elemental, the last upper bound of an assumed size array
1660 argument must be present. */
1661 if (resolve_assumed_size_actual (arg->expr))
1662 return FAILURE;
1663
3c7b91d3 1664 /* Elemental procedure's array actual arguments must conform. */
b8ea6dbc
PT
1665 if (e != NULL)
1666 {
ca8a8795
DF
1667 if (gfc_check_conformance (arg->expr, e,
1668 "elemental procedure") == FAILURE)
b8ea6dbc
PT
1669 return FAILURE;
1670 }
1671 else
1672 e = arg->expr;
1673 }
1674
4a965827
TB
1675 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1676 is an array, the intent inout/out variable needs to be also an array. */
1677 if (rank > 0 && esym && expr == NULL)
1678 for (eformal = esym->formal, arg = arg0; arg && eformal;
1679 arg = arg->next, eformal = eformal->next)
1680 if ((eformal->sym->attr.intent == INTENT_OUT
1681 || eformal->sym->attr.intent == INTENT_INOUT)
1682 && arg->expr && arg->expr->rank == 0)
1683 {
1684 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1685 "ELEMENTAL subroutine '%s' is a scalar, but another "
1686 "actual argument is an array", &arg->expr->where,
1687 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1688 : "INOUT", eformal->sym->name, esym->name);
1689 return FAILURE;
1690 }
b8ea6dbc
PT
1691 return SUCCESS;
1692}
1693
1694
1524f80b
RS
1695/* Go through each actual argument in ACTUAL and see if it can be
1696 implemented as an inlined, non-copying intrinsic. FNSYM is the
1697 function being called, or NULL if not known. */
1698
1699static void
edf1eac2 1700find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1524f80b
RS
1701{
1702 gfc_actual_arglist *ap;
1703 gfc_expr *expr;
1704
1705 for (ap = actual; ap; ap = ap->next)
1706 if (ap->expr
1707 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
2b0bd714
MM
1708 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1709 NOT_ELEMENTAL))
1524f80b
RS
1710 ap->expr->inline_noncopying_intrinsic = 1;
1711}
1712
edf1eac2 1713
68ea355b
PT
1714/* This function does the checking of references to global procedures
1715 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1716 77 and 95 standards. It checks for a gsymbol for the name, making
1717 one if it does not already exist. If it already exists, then the
1718 reference being resolved must correspond to the type of gsymbol.
05c1e3a7 1719 Otherwise, the new symbol is equipped with the attributes of the
68ea355b 1720 reference. The corresponding code that is called in creating
71a7778c
PT
1721 global entities is parse.c.
1722
1723 In addition, for all but -std=legacy, the gsymbols are used to
1724 check the interfaces of external procedures from the same file.
1725 The namespace of the gsymbol is resolved and then, once this is
1726 done the interface is checked. */
68ea355b 1727
3af8d8cb
PT
1728
1729static bool
1730not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1731{
1732 if (!gsym_ns->proc_name->attr.recursive)
1733 return true;
1734
1735 if (sym->ns == gsym_ns)
1736 return false;
1737
1738 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1739 return false;
1740
1741 return true;
1742}
1743
1744static bool
1745not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1746{
1747 if (gsym_ns->entries)
1748 {
1749 gfc_entry_list *entry = gsym_ns->entries;
1750
1751 for (; entry; entry = entry->next)
1752 {
1753 if (strcmp (sym->name, entry->sym->name) == 0)
1754 {
1755 if (strcmp (gsym_ns->proc_name->name,
1756 sym->ns->proc_name->name) == 0)
1757 return false;
1758
1759 if (sym->ns->parent
1760 && strcmp (gsym_ns->proc_name->name,
1761 sym->ns->parent->proc_name->name) == 0)
1762 return false;
1763 }
1764 }
1765 }
1766 return true;
1767}
1768
ff604888 1769static void
71a7778c
PT
1770resolve_global_procedure (gfc_symbol *sym, locus *where,
1771 gfc_actual_arglist **actual, int sub)
68ea355b
PT
1772{
1773 gfc_gsymbol * gsym;
71a7778c 1774 gfc_namespace *ns;
32e8bb8e 1775 enum gfc_symbol_type type;
68ea355b
PT
1776
1777 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1778
1779 gsym = gfc_get_gsymbol (sym->name);
1780
1781 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
ca39e6f2 1782 gfc_global_used (gsym, where);
68ea355b 1783
71a7778c 1784 if (gfc_option.flag_whole_file
3af8d8cb 1785 && sym->attr.if_source == IFSRC_UNKNOWN
71a7778c
PT
1786 && gsym->type != GSYM_UNKNOWN
1787 && gsym->ns
3af8d8cb
PT
1788 && gsym->ns->resolved != -1
1789 && gsym->ns->proc_name
1790 && not_in_recursive (sym, gsym->ns)
1791 && not_entry_self_reference (sym, gsym->ns))
71a7778c
PT
1792 {
1793 /* Make sure that translation for the gsymbol occurs before
1794 the procedure currently being resolved. */
1795 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1796 for (; ns && ns != gsym->ns; ns = ns->sibling)
1797 {
1798 if (ns->sibling == gsym->ns)
1799 {
1800 ns->sibling = gsym->ns->sibling;
1801 gsym->ns->sibling = gfc_global_ns_list;
1802 gfc_global_ns_list = gsym->ns;
1803 break;
1804 }
1805 }
1806
1807 if (!gsym->ns->resolved)
3af8d8cb
PT
1808 {
1809 gfc_dt_list *old_dt_list;
1810
1811 /* Stash away derived types so that the backend_decls do not
1812 get mixed up. */
1813 old_dt_list = gfc_derived_types;
1814 gfc_derived_types = NULL;
1815
1816 gfc_resolve (gsym->ns);
1817
1818 /* Store the new derived types with the global namespace. */
1819 if (gfc_derived_types)
1820 gsym->ns->derived_types = gfc_derived_types;
1821
1822 /* Restore the derived types of this namespace. */
1823 gfc_derived_types = old_dt_list;
1824 }
1825
1826 if (gsym->ns->proc_name->attr.function
1827 && gsym->ns->proc_name->as
1828 && gsym->ns->proc_name->as->rank
1829 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1830 gfc_error ("The reference to function '%s' at %L either needs an "
1831 "explicit INTERFACE or the rank is incorrect", sym->name,
1832 where);
1833
1834 if (gfc_option.flag_whole_file == 1
1835 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1836 &&
1837 !(gfc_option.warn_std & GFC_STD_GNU)))
1838 gfc_errors_to_warnings (1);
71a7778c
PT
1839
1840 gfc_procedure_use (gsym->ns->proc_name, actual, where);
3af8d8cb
PT
1841
1842 gfc_errors_to_warnings (0);
71a7778c
PT
1843 }
1844
68ea355b
PT
1845 if (gsym->type == GSYM_UNKNOWN)
1846 {
1847 gsym->type = type;
1848 gsym->where = *where;
1849 }
1850
1851 gsym->used = 1;
1852}
1524f80b 1853
edf1eac2 1854
6de9cd9a
DN
1855/************* Function resolution *************/
1856
1857/* Resolve a function call known to be generic.
1858 Section 14.1.2.4.1. */
1859
1860static match
edf1eac2 1861resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
6de9cd9a
DN
1862{
1863 gfc_symbol *s;
1864
1865 if (sym->attr.generic)
1866 {
edf1eac2 1867 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
6de9cd9a
DN
1868 if (s != NULL)
1869 {
1870 expr->value.function.name = s->name;
1871 expr->value.function.esym = s;
f5f701ad
PT
1872
1873 if (s->ts.type != BT_UNKNOWN)
1874 expr->ts = s->ts;
1875 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1876 expr->ts = s->result->ts;
1877
6de9cd9a
DN
1878 if (s->as != NULL)
1879 expr->rank = s->as->rank;
f5f701ad
PT
1880 else if (s->result != NULL && s->result->as != NULL)
1881 expr->rank = s->result->as->rank;
1882
0a164a3c
PT
1883 gfc_set_sym_referenced (expr->value.function.esym);
1884
6de9cd9a
DN
1885 return MATCH_YES;
1886 }
1887
edf1eac2
SK
1888 /* TODO: Need to search for elemental references in generic
1889 interface. */
6de9cd9a
DN
1890 }
1891
1892 if (sym->attr.intrinsic)
1893 return gfc_intrinsic_func_interface (expr, 0);
1894
1895 return MATCH_NO;
1896}
1897
1898
17b1d2a0 1899static gfc_try
edf1eac2 1900resolve_generic_f (gfc_expr *expr)
6de9cd9a
DN
1901{
1902 gfc_symbol *sym;
1903 match m;
1904
1905 sym = expr->symtree->n.sym;
1906
1907 for (;;)
1908 {
1909 m = resolve_generic_f0 (expr, sym);
1910 if (m == MATCH_YES)
1911 return SUCCESS;
1912 else if (m == MATCH_ERROR)
1913 return FAILURE;
1914
1915generic:
1916 if (sym->ns->parent == NULL)
1917 break;
1918 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1919
1920 if (sym == NULL)
1921 break;
1922 if (!generic_sym (sym))
1923 goto generic;
1924 }
1925
71f77fd7
PT
1926 /* Last ditch attempt. See if the reference is to an intrinsic
1927 that possesses a matching interface. 14.1.2.4 */
c3005b0f 1928 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a 1929 {
8c086c9c 1930 gfc_error ("There is no specific function for the generic '%s' at %L",
6de9cd9a
DN
1931 expr->symtree->n.sym->name, &expr->where);
1932 return FAILURE;
1933 }
1934
1935 m = gfc_intrinsic_func_interface (expr, 0);
1936 if (m == MATCH_YES)
1937 return SUCCESS;
1938 if (m == MATCH_NO)
edf1eac2
SK
1939 gfc_error ("Generic function '%s' at %L is not consistent with a "
1940 "specific intrinsic interface", expr->symtree->n.sym->name,
1941 &expr->where);
6de9cd9a
DN
1942
1943 return FAILURE;
1944}
1945
1946
1947/* Resolve a function call known to be specific. */
1948
1949static match
edf1eac2 1950resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
6de9cd9a
DN
1951{
1952 match m;
1953
1954 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1955 {
1956 if (sym->attr.dummy)
1957 {
1958 sym->attr.proc = PROC_DUMMY;
1959 goto found;
1960 }
1961
1962 sym->attr.proc = PROC_EXTERNAL;
1963 goto found;
1964 }
1965
1966 if (sym->attr.proc == PROC_MODULE
1967 || sym->attr.proc == PROC_ST_FUNCTION
1968 || sym->attr.proc == PROC_INTERNAL)
1969 goto found;
1970
1971 if (sym->attr.intrinsic)
1972 {
1973 m = gfc_intrinsic_func_interface (expr, 1);
1974 if (m == MATCH_YES)
1975 return MATCH_YES;
1976 if (m == MATCH_NO)
edf1eac2
SK
1977 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
1978 "with an intrinsic", sym->name, &expr->where);
6de9cd9a
DN
1979
1980 return MATCH_ERROR;
1981 }
1982
1983 return MATCH_NO;
1984
1985found:
1986 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
1987
a7c0b11d
JW
1988 if (sym->result)
1989 expr->ts = sym->result->ts;
1990 else
1991 expr->ts = sym->ts;
6de9cd9a
DN
1992 expr->value.function.name = sym->name;
1993 expr->value.function.esym = sym;
1994 if (sym->as != NULL)
1995 expr->rank = sym->as->rank;
1996
1997 return MATCH_YES;
1998}
1999
2000
17b1d2a0 2001static gfc_try
edf1eac2 2002resolve_specific_f (gfc_expr *expr)
6de9cd9a
DN
2003{
2004 gfc_symbol *sym;
2005 match m;
2006
2007 sym = expr->symtree->n.sym;
2008
2009 for (;;)
2010 {
2011 m = resolve_specific_f0 (sym, expr);
2012 if (m == MATCH_YES)
2013 return SUCCESS;
2014 if (m == MATCH_ERROR)
2015 return FAILURE;
2016
2017 if (sym->ns->parent == NULL)
2018 break;
2019
2020 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2021
2022 if (sym == NULL)
2023 break;
2024 }
2025
2026 gfc_error ("Unable to resolve the specific function '%s' at %L",
2027 expr->symtree->n.sym->name, &expr->where);
2028
2029 return SUCCESS;
2030}
2031
2032
2033/* Resolve a procedure call not known to be generic nor specific. */
2034
17b1d2a0 2035static gfc_try
edf1eac2 2036resolve_unknown_f (gfc_expr *expr)
6de9cd9a
DN
2037{
2038 gfc_symbol *sym;
2039 gfc_typespec *ts;
2040
2041 sym = expr->symtree->n.sym;
2042
2043 if (sym->attr.dummy)
2044 {
2045 sym->attr.proc = PROC_DUMMY;
2046 expr->value.function.name = sym->name;
2047 goto set_type;
2048 }
2049
2050 /* See if we have an intrinsic function reference. */
2051
c3005b0f 2052 if (gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a
DN
2053 {
2054 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2055 return SUCCESS;
2056 return FAILURE;
2057 }
2058
2059 /* The reference is to an external name. */
2060
2061 sym->attr.proc = PROC_EXTERNAL;
2062 expr->value.function.name = sym->name;
2063 expr->value.function.esym = expr->symtree->n.sym;
2064
2065 if (sym->as != NULL)
2066 expr->rank = sym->as->rank;
2067
2068 /* Type of the expression is either the type of the symbol or the
2069 default type of the symbol. */
2070
2071set_type:
2072 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2073
2074 if (sym->ts.type != BT_UNKNOWN)
2075 expr->ts = sym->ts;
2076 else
2077 {
713485cc 2078 ts = gfc_get_default_type (sym->name, sym->ns);
6de9cd9a
DN
2079
2080 if (ts->type == BT_UNKNOWN)
2081 {
cf4d246b 2082 gfc_error ("Function '%s' at %L has no IMPLICIT type",
6de9cd9a
DN
2083 sym->name, &expr->where);
2084 return FAILURE;
2085 }
2086 else
2087 expr->ts = *ts;
2088 }
2089
2090 return SUCCESS;
2091}
2092
2093
e7c8ff56
PT
2094/* Return true, if the symbol is an external procedure. */
2095static bool
2096is_external_proc (gfc_symbol *sym)
2097{
2098 if (!sym->attr.dummy && !sym->attr.contained
2099 && !(sym->attr.intrinsic
c3005b0f 2100 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
e7c8ff56
PT
2101 && sym->attr.proc != PROC_ST_FUNCTION
2102 && !sym->attr.use_assoc
2103 && sym->name)
2104 return true;
c3005b0f
DK
2105
2106 return false;
e7c8ff56
PT
2107}
2108
2109
2054fc29
VR
2110/* Figure out if a function reference is pure or not. Also set the name
2111 of the function for a potential error message. Return nonzero if the
6de9cd9a 2112 function is PURE, zero if not. */
908a2235
PT
2113static int
2114pure_stmt_function (gfc_expr *, gfc_symbol *);
6de9cd9a
DN
2115
2116static int
edf1eac2 2117pure_function (gfc_expr *e, const char **name)
6de9cd9a
DN
2118{
2119 int pure;
2120
36f7dcae
PT
2121 *name = NULL;
2122
9ebe2d22
PT
2123 if (e->symtree != NULL
2124 && e->symtree->n.sym != NULL
2125 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
908a2235 2126 return pure_stmt_function (e, e->symtree->n.sym);
9ebe2d22 2127
6de9cd9a
DN
2128 if (e->value.function.esym)
2129 {
2130 pure = gfc_pure (e->value.function.esym);
2131 *name = e->value.function.esym->name;
2132 }
2133 else if (e->value.function.isym)
2134 {
2135 pure = e->value.function.isym->pure
edf1eac2 2136 || e->value.function.isym->elemental;
6de9cd9a
DN
2137 *name = e->value.function.isym->name;
2138 }
2139 else
2140 {
2141 /* Implicit functions are not pure. */
2142 pure = 0;
2143 *name = e->value.function.name;
2144 }
2145
2146 return pure;
2147}
2148
2149
908a2235
PT
2150static bool
2151impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2152 int *f ATTRIBUTE_UNUSED)
2153{
2154 const char *name;
2155
2156 /* Don't bother recursing into other statement functions
2157 since they will be checked individually for purity. */
2158 if (e->expr_type != EXPR_FUNCTION
2159 || !e->symtree
2160 || e->symtree->n.sym == sym
2161 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2162 return false;
2163
2164 return pure_function (e, &name) ? false : true;
2165}
2166
2167
2168static int
2169pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2170{
2171 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2172}
2173
2174
17b1d2a0 2175static gfc_try
a8b3b0b6
CR
2176is_scalar_expr_ptr (gfc_expr *expr)
2177{
17b1d2a0 2178 gfc_try retval = SUCCESS;
a8b3b0b6
CR
2179 gfc_ref *ref;
2180 int start;
2181 int end;
2182
2183 /* See if we have a gfc_ref, which means we have a substring, array
2184 reference, or a component. */
2185 if (expr->ref != NULL)
2186 {
2187 ref = expr->ref;
2188 while (ref->next != NULL)
2189 ref = ref->next;
2190
2191 switch (ref->type)
2192 {
2193 case REF_SUBSTRING:
2194 if (ref->u.ss.length != NULL
2195 && ref->u.ss.length->length != NULL
2196 && ref->u.ss.start
2197 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2198 && ref->u.ss.end
2199 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2200 {
2201 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2202 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2203 if (end - start + 1 != 1)
2204 retval = FAILURE;
2205 }
2206 else
2207 retval = FAILURE;
2208 break;
2209 case REF_ARRAY:
2210 if (ref->u.ar.type == AR_ELEMENT)
2211 retval = SUCCESS;
2212 else if (ref->u.ar.type == AR_FULL)
2213 {
2214 /* The user can give a full array if the array is of size 1. */
2215 if (ref->u.ar.as != NULL
2216 && ref->u.ar.as->rank == 1
2217 && ref->u.ar.as->type == AS_EXPLICIT
2218 && ref->u.ar.as->lower[0] != NULL
2219 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2220 && ref->u.ar.as->upper[0] != NULL
2221 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2222 {
2223 /* If we have a character string, we need to check if
2224 its length is one. */
2225 if (expr->ts.type == BT_CHARACTER)
2226 {
bc21d315
JW
2227 if (expr->ts.u.cl == NULL
2228 || expr->ts.u.cl->length == NULL
2229 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
a8b3b0b6
CR
2230 != 0)
2231 retval = FAILURE;
2232 }
2233 else
2234 {
3759634f
SK
2235 /* We have constant lower and upper bounds. If the
2236 difference between is 1, it can be considered a
2237 scalar. */
2238 start = (int) mpz_get_si
2239 (ref->u.ar.as->lower[0]->value.integer);
2240 end = (int) mpz_get_si
2241 (ref->u.ar.as->upper[0]->value.integer);
2242 if (end - start + 1 != 1)
2243 retval = FAILURE;
2244 }
a8b3b0b6
CR
2245 }
2246 else
2247 retval = FAILURE;
2248 }
2249 else
2250 retval = FAILURE;
2251 break;
2252 default:
2253 retval = SUCCESS;
2254 break;
2255 }
2256 }
2257 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2258 {
2259 /* Character string. Make sure it's of length 1. */
bc21d315
JW
2260 if (expr->ts.u.cl == NULL
2261 || expr->ts.u.cl->length == NULL
2262 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
a8b3b0b6
CR
2263 retval = FAILURE;
2264 }
2265 else if (expr->rank != 0)
2266 retval = FAILURE;
2267
2268 return retval;
2269}
2270
2271
2272/* Match one of the iso_c_binding functions (c_associated or c_loc)
2273 and, in the case of c_associated, set the binding label based on
2274 the arguments. */
2275
17b1d2a0 2276static gfc_try
a8b3b0b6
CR
2277gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2278 gfc_symbol **new_sym)
2279{
2280 char name[GFC_MAX_SYMBOL_LEN + 1];
2281 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
23f2d017 2282 int optional_arg = 0, is_pointer = 0;
17b1d2a0 2283 gfc_try retval = SUCCESS;
a8b3b0b6 2284 gfc_symbol *args_sym;
15231566 2285 gfc_typespec *arg_ts;
a8b3b0b6 2286
aa5e22f0
CR
2287 if (args->expr->expr_type == EXPR_CONSTANT
2288 || args->expr->expr_type == EXPR_OP
2289 || args->expr->expr_type == EXPR_NULL)
2290 {
2291 gfc_error ("Argument to '%s' at %L is not a variable",
2292 sym->name, &(args->expr->where));
2293 return FAILURE;
2294 }
2295
a8b3b0b6 2296 args_sym = args->expr->symtree->n.sym;
15231566
CR
2297
2298 /* The typespec for the actual arg should be that stored in the expr
2299 and not necessarily that of the expr symbol (args_sym), because
2300 the actual expression could be a part-ref of the expr symbol. */
2301 arg_ts = &(args->expr->ts);
2302
23f2d017
MM
2303 is_pointer = gfc_is_data_pointer (args->expr);
2304
a8b3b0b6
CR
2305 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2306 {
2307 /* If the user gave two args then they are providing something for
2308 the optional arg (the second cptr). Therefore, set the name and
2309 binding label to the c_associated for two cptrs. Otherwise,
2310 set c_associated to expect one cptr. */
2311 if (args->next)
2312 {
2313 /* two args. */
2314 sprintf (name, "%s_2", sym->name);
2315 sprintf (binding_label, "%s_2", sym->binding_label);
2316 optional_arg = 1;
2317 }
2318 else
2319 {
2320 /* one arg. */
2321 sprintf (name, "%s_1", sym->name);
2322 sprintf (binding_label, "%s_1", sym->binding_label);
2323 optional_arg = 0;
2324 }
2325
2326 /* Get a new symbol for the version of c_associated that
2327 will get called. */
2328 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2329 }
2330 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2331 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2332 {
2333 sprintf (name, "%s", sym->name);
2334 sprintf (binding_label, "%s", sym->binding_label);
2335
2336 /* Error check the call. */
2337 if (args->next != NULL)
2338 {
2339 gfc_error_now ("More actual than formal arguments in '%s' "
2340 "call at %L", name, &(args->expr->where));
2341 retval = FAILURE;
2342 }
2343 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2344 {
2345 /* Make sure we have either the target or pointer attribute. */
23f2d017 2346 if (!args_sym->attr.target && !is_pointer)
a8b3b0b6
CR
2347 {
2348 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2349 "a TARGET or an associated pointer",
15231566 2350 args_sym->name,
a8b3b0b6
CR
2351 sym->name, &(args->expr->where));
2352 retval = FAILURE;
2353 }
2354
2355 /* See if we have interoperable type and type param. */
2ec855f1 2356 if (verify_c_interop (arg_ts) == SUCCESS
15231566 2357 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
a8b3b0b6
CR
2358 {
2359 if (args_sym->attr.target == 1)
2360 {
2361 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2362 has the target attribute and is interoperable. */
2363 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2364 allocatable variable that has the TARGET attribute and
2365 is not an array of zero size. */
2366 if (args_sym->attr.allocatable == 1)
2367 {
2368 if (args_sym->attr.dimension != 0
2369 && (args_sym->as && args_sym->as->rank == 0))
2370 {
2371 gfc_error_now ("Allocatable variable '%s' used as a "
2372 "parameter to '%s' at %L must not be "
2373 "an array of zero size",
2374 args_sym->name, sym->name,
2375 &(args->expr->where));
2376 retval = FAILURE;
2377 }
2378 }
2379 else
21a77227
CR
2380 {
2381 /* A non-allocatable target variable with C
2382 interoperable type and type parameters must be
2383 interoperable. */
2384 if (args_sym && args_sym->attr.dimension)
2385 {
2386 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2387 {
2388 gfc_error ("Assumed-shape array '%s' at %L "
2389 "cannot be an argument to the "
2390 "procedure '%s' because "
2391 "it is not C interoperable",
2392 args_sym->name,
2393 &(args->expr->where), sym->name);
2394 retval = FAILURE;
2395 }
2396 else if (args_sym->as->type == AS_DEFERRED)
2397 {
2398 gfc_error ("Deferred-shape array '%s' at %L "
2399 "cannot be an argument to the "
2400 "procedure '%s' because "
2401 "it is not C interoperable",
2402 args_sym->name,
2403 &(args->expr->where), sym->name);
2404 retval = FAILURE;
2405 }
2406 }
2407
a8b3b0b6
CR
2408 /* Make sure it's not a character string. Arrays of
2409 any type should be ok if the variable is of a C
2410 interoperable type. */
15231566 2411 if (arg_ts->type == BT_CHARACTER)
bc21d315
JW
2412 if (arg_ts->u.cl != NULL
2413 && (arg_ts->u.cl->length == NULL
2414 || arg_ts->u.cl->length->expr_type
21a77227
CR
2415 != EXPR_CONSTANT
2416 || mpz_cmp_si
bc21d315 2417 (arg_ts->u.cl->length->value.integer, 1)
21a77227
CR
2418 != 0)
2419 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2420 {
2421 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2422 "at %L must have a length of 1",
2423 args_sym->name, sym->name,
2424 &(args->expr->where));
2425 retval = FAILURE;
2426 }
a8b3b0b6
CR
2427 }
2428 }
23f2d017 2429 else if (is_pointer
15231566 2430 && is_scalar_expr_ptr (args->expr) != SUCCESS)
a8b3b0b6
CR
2431 {
2432 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2433 scalar pointer. */
2434 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2435 "associated scalar POINTER", args_sym->name,
2436 sym->name, &(args->expr->where));
2437 retval = FAILURE;
2438 }
2439 }
2440 else
2441 {
2442 /* The parameter is not required to be C interoperable. If it
2443 is not C interoperable, it must be a nonpolymorphic scalar
2444 with no length type parameters. It still must have either
2445 the pointer or target attribute, and it can be
2446 allocatable (but must be allocated when c_loc is called). */
15231566 2447 if (args->expr->rank != 0
a8b3b0b6
CR
2448 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2449 {
2450 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2451 "scalar", args_sym->name, sym->name,
2452 &(args->expr->where));
2453 retval = FAILURE;
2454 }
15231566 2455 else if (arg_ts->type == BT_CHARACTER
21a77227 2456 && is_scalar_expr_ptr (args->expr) != SUCCESS)
a8b3b0b6 2457 {
21a77227
CR
2458 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2459 "%L must have a length of 1",
a8b3b0b6
CR
2460 args_sym->name, sym->name,
2461 &(args->expr->where));
2462 retval = FAILURE;
2463 }
2464 }
2465 }
2466 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2467 {
15231566 2468 if (args_sym->attr.flavor != FL_PROCEDURE)
a8b3b0b6
CR
2469 {
2470 /* TODO: Update this error message to allow for procedure
2471 pointers once they are implemented. */
2472 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2473 "procedure",
15231566 2474 args_sym->name, sym->name,
a8b3b0b6
CR
2475 &(args->expr->where));
2476 retval = FAILURE;
2477 }
15231566 2478 else if (args_sym->attr.is_bind_c != 1)
089db47d
CR
2479 {
2480 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2481 "BIND(C)",
15231566 2482 args_sym->name, sym->name,
089db47d
CR
2483 &(args->expr->where));
2484 retval = FAILURE;
2485 }
a8b3b0b6
CR
2486 }
2487
2488 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2489 *new_sym = sym;
2490 }
2491 else
2492 {
2493 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2494 "iso_c_binding function: '%s'!\n", sym->name);
2495 }
2496
2497 return retval;
2498}
2499
2500
6de9cd9a
DN
2501/* Resolve a function call, which means resolving the arguments, then figuring
2502 out which entity the name refers to. */
2503/* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2504 to INTENT(OUT) or INTENT(INOUT). */
2505
17b1d2a0 2506static gfc_try
edf1eac2 2507resolve_function (gfc_expr *expr)
6de9cd9a
DN
2508{
2509 gfc_actual_arglist *arg;
edf1eac2 2510 gfc_symbol *sym;
6b25a558 2511 const char *name;
17b1d2a0 2512 gfc_try t;
48474141 2513 int temp;
7fcafa71 2514 procedure_type p = PROC_INTRINSIC;
0b4e2af7 2515 bool no_formal_args;
48474141 2516
20236f90
PT
2517 sym = NULL;
2518 if (expr->symtree)
2519 sym = expr->symtree->n.sym;
2520
6c036626
JW
2521 /* If this is a procedure pointer component, it has already been resolved. */
2522 if (gfc_is_proc_ptr_comp (expr, NULL))
2523 return SUCCESS;
2524
2c68bc89 2525 if (sym && sym->attr.intrinsic
c73b6478
JW
2526 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2527 return FAILURE;
2c68bc89 2528
726d8566 2529 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
20a037d5 2530 {
edf1eac2 2531 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
20a037d5
PT
2532 return FAILURE;
2533 }
2534
8bae6273
JW
2535 /* If this ia a deferred TBP with an abstract interface (which may
2536 of course be referenced), expr->value.function.name will be set. */
2537 if (sym && sym->attr.abstract && !expr->value.function.name)
9e1d712c
TB
2538 {
2539 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2540 sym->name, &expr->where);
2541 return FAILURE;
2542 }
2543
48474141
PT
2544 /* Switch off assumed size checking and do this again for certain kinds
2545 of procedure, once the procedure itself is resolved. */
2546 need_full_assumed_size++;
6de9cd9a 2547
7fcafa71
PT
2548 if (expr->symtree && expr->symtree->n.sym)
2549 p = expr->symtree->n.sym->attr.proc;
2550
0b4e2af7
PT
2551 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2552 if (resolve_actual_arglist (expr->value.function.actual,
2553 p, no_formal_args) == FAILURE)
7fcafa71 2554 return FAILURE;
6de9cd9a 2555
a8b3b0b6
CR
2556 /* Need to setup the call to the correct c_associated, depending on
2557 the number of cptrs to user gives to compare. */
2558 if (sym && sym->attr.is_iso_c == 1)
2559 {
2560 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2561 == FAILURE)
2562 return FAILURE;
2563
2564 /* Get the symtree for the new symbol (resolved func).
2565 the old one will be freed later, when it's no longer used. */
2566 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2567 }
2568
2569 /* Resume assumed_size checking. */
48474141
PT
2570 need_full_assumed_size--;
2571
71a7778c
PT
2572 /* If the procedure is external, check for usage. */
2573 if (sym && is_external_proc (sym))
2574 resolve_global_procedure (sym, &expr->where,
2575 &expr->value.function.actual, 0);
2576
20236f90 2577 if (sym && sym->ts.type == BT_CHARACTER
bc21d315
JW
2578 && sym->ts.u.cl
2579 && sym->ts.u.cl->length == NULL
edf1eac2
SK
2580 && !sym->attr.dummy
2581 && expr->value.function.esym == NULL
2582 && !sym->attr.contained)
20236f90 2583 {
20236f90 2584 /* Internal procedures are taken care of in resolve_contained_fntype. */
0e3e65bc
PT
2585 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2586 "be used at %L since it is not a dummy argument",
2587 sym->name, &expr->where);
2588 return FAILURE;
20236f90
PT
2589 }
2590
edf1eac2 2591 /* See if function is already resolved. */
6de9cd9a
DN
2592
2593 if (expr->value.function.name != NULL)
2594 {
2595 if (expr->ts.type == BT_UNKNOWN)
20236f90 2596 expr->ts = sym->ts;
6de9cd9a
DN
2597 t = SUCCESS;
2598 }
2599 else
2600 {
2601 /* Apply the rules of section 14.1.2. */
2602
20236f90 2603 switch (procedure_kind (sym))
6de9cd9a
DN
2604 {
2605 case PTYPE_GENERIC:
2606 t = resolve_generic_f (expr);
2607 break;
2608
2609 case PTYPE_SPECIFIC:
2610 t = resolve_specific_f (expr);
2611 break;
2612
2613 case PTYPE_UNKNOWN:
2614 t = resolve_unknown_f (expr);
2615 break;
2616
2617 default:
2618 gfc_internal_error ("resolve_function(): bad function type");
2619 }
2620 }
2621
2622 /* If the expression is still a function (it might have simplified),
2623 then we check to see if we are calling an elemental function. */
2624
2625 if (expr->expr_type != EXPR_FUNCTION)
2626 return t;
2627
48474141
PT
2628 temp = need_full_assumed_size;
2629 need_full_assumed_size = 0;
2630
b8ea6dbc
PT
2631 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2632 return FAILURE;
48474141 2633
6c7a4dfd
JJ
2634 if (omp_workshare_flag
2635 && expr->value.function.esym
2636 && ! gfc_elemental (expr->value.function.esym))
2637 {
edf1eac2
SK
2638 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2639 "in WORKSHARE construct", expr->value.function.esym->name,
6c7a4dfd
JJ
2640 &expr->where);
2641 t = FAILURE;
2642 }
6de9cd9a 2643
cd5ecab6 2644#define GENERIC_ID expr->value.function.isym->id
48474141 2645 else if (expr->value.function.actual != NULL
edf1eac2
SK
2646 && expr->value.function.isym != NULL
2647 && GENERIC_ID != GFC_ISYM_LBOUND
2648 && GENERIC_ID != GFC_ISYM_LEN
2649 && GENERIC_ID != GFC_ISYM_LOC
2650 && GENERIC_ID != GFC_ISYM_PRESENT)
48474141 2651 {
fa951694 2652 /* Array intrinsics must also have the last upper bound of an
b82feea5 2653 assumed size array argument. UBOUND and SIZE have to be
48474141
PT
2654 excluded from the check if the second argument is anything
2655 than a constant. */
05c1e3a7 2656
48474141
PT
2657 for (arg = expr->value.function.actual; arg; arg = arg->next)
2658 {
7a687b22
TB
2659 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2660 && arg->next != NULL && arg->next->expr)
9ebe2d22
PT
2661 {
2662 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2663 break;
2664
7a687b22
TB
2665 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2666 break;
2667
9ebe2d22
PT
2668 if ((int)mpz_get_si (arg->next->expr->value.integer)
2669 < arg->expr->rank)
2670 break;
2671 }
05c1e3a7 2672
48474141 2673 if (arg->expr != NULL
edf1eac2
SK
2674 && arg->expr->rank > 0
2675 && resolve_assumed_size_actual (arg->expr))
48474141
PT
2676 return FAILURE;
2677 }
2678 }
4d4074e4 2679#undef GENERIC_ID
48474141
PT
2680
2681 need_full_assumed_size = temp;
36f7dcae 2682 name = NULL;
48474141 2683
5f20c93a 2684 if (!pure_function (expr, &name) && name)
6de9cd9a
DN
2685 {
2686 if (forall_flag)
2687 {
edf1eac2
SK
2688 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2689 "FORALL %s", name, &expr->where,
2690 forall_flag == 2 ? "mask" : "block");
6de9cd9a
DN
2691 t = FAILURE;
2692 }
2693 else if (gfc_pure (NULL))
2694 {
2695 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2696 "procedure within a PURE procedure", name, &expr->where);
2697 t = FAILURE;
2698 }
2699 }
2700
77f131ca
FXC
2701 /* Functions without the RECURSIVE attribution are not allowed to
2702 * call themselves. */
2703 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2704 {
1933ba0f 2705 gfc_symbol *esym;
77f131ca 2706 esym = expr->value.function.esym;
77f131ca 2707
1933ba0f 2708 if (is_illegal_recursion (esym, gfc_current_ns))
77f131ca 2709 {
1933ba0f
DK
2710 if (esym->attr.entry && esym->ns->entries)
2711 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2712 " function '%s' is not RECURSIVE",
2713 esym->name, &expr->where, esym->ns->entries->sym->name);
2714 else
2715 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2716 " is not RECURSIVE", esym->name, &expr->where);
2717
edf1eac2 2718 t = FAILURE;
77f131ca
FXC
2719 }
2720 }
2721
47992a4a
EE
2722 /* Character lengths of use associated functions may contains references to
2723 symbols not referenced from the current program unit otherwise. Make sure
2724 those symbols are marked as referenced. */
2725
05c1e3a7 2726 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
47992a4a
EE
2727 && expr->value.function.esym->attr.use_assoc)
2728 {
bc21d315 2729 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
47992a4a
EE
2730 }
2731
23d1b451
PT
2732 if (t == SUCCESS
2733 && !((expr->value.function.esym
2734 && expr->value.function.esym->attr.elemental)
2735 ||
2736 (expr->value.function.isym
2737 && expr->value.function.isym->elemental)))
1524f80b
RS
2738 find_noncopying_intrinsics (expr->value.function.esym,
2739 expr->value.function.actual);
9ebe2d22
PT
2740
2741 /* Make sure that the expression has a typespec that works. */
2742 if (expr->ts.type == BT_UNKNOWN)
2743 {
2744 if (expr->symtree->n.sym->result
3070bab4
JW
2745 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2746 && !expr->symtree->n.sym->result->attr.proc_pointer)
9ebe2d22 2747 expr->ts = expr->symtree->n.sym->result->ts;
9ebe2d22
PT
2748 }
2749
6de9cd9a
DN
2750 return t;
2751}
2752
2753
2754/************* Subroutine resolution *************/
2755
2756static void
edf1eac2 2757pure_subroutine (gfc_code *c, gfc_symbol *sym)
6de9cd9a 2758{
6de9cd9a
DN
2759 if (gfc_pure (sym))
2760 return;
2761
2762 if (forall_flag)
2763 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2764 sym->name, &c->loc);
2765 else if (gfc_pure (NULL))
2766 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2767 &c->loc);
2768}
2769
2770
2771static match
edf1eac2 2772resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
2773{
2774 gfc_symbol *s;
2775
2776 if (sym->attr.generic)
2777 {
2778 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2779 if (s != NULL)
2780 {
edf1eac2 2781 c->resolved_sym = s;
6de9cd9a
DN
2782 pure_subroutine (c, s);
2783 return MATCH_YES;
2784 }
2785
2786 /* TODO: Need to search for elemental references in generic interface. */
2787 }
2788
2789 if (sym->attr.intrinsic)
2790 return gfc_intrinsic_sub_interface (c, 0);
2791
2792 return MATCH_NO;
2793}
2794
2795
17b1d2a0 2796static gfc_try
edf1eac2 2797resolve_generic_s (gfc_code *c)
6de9cd9a
DN
2798{
2799 gfc_symbol *sym;
2800 match m;
2801
2802 sym = c->symtree->n.sym;
2803
8c086c9c 2804 for (;;)
6de9cd9a 2805 {
8c086c9c
PT
2806 m = resolve_generic_s0 (c, sym);
2807 if (m == MATCH_YES)
2808 return SUCCESS;
2809 else if (m == MATCH_ERROR)
2810 return FAILURE;
2811
2812generic:
2813 if (sym->ns->parent == NULL)
2814 break;
6de9cd9a 2815 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
8c086c9c
PT
2816
2817 if (sym == NULL)
2818 break;
2819 if (!generic_sym (sym))
2820 goto generic;
6de9cd9a
DN
2821 }
2822
71f77fd7
PT
2823 /* Last ditch attempt. See if the reference is to an intrinsic
2824 that possesses a matching interface. 14.1.2.4 */
8c086c9c 2825 sym = c->symtree->n.sym;
71f77fd7 2826
c3005b0f 2827 if (!gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a 2828 {
edf1eac2
SK
2829 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2830 sym->name, &c->loc);
6de9cd9a
DN
2831 return FAILURE;
2832 }
2833
2834 m = gfc_intrinsic_sub_interface (c, 0);
2835 if (m == MATCH_YES)
2836 return SUCCESS;
2837 if (m == MATCH_NO)
2838 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2839 "intrinsic subroutine interface", sym->name, &c->loc);
2840
2841 return FAILURE;
2842}
2843
2844
a8b3b0b6
CR
2845/* Set the name and binding label of the subroutine symbol in the call
2846 expression represented by 'c' to include the type and kind of the
2847 second parameter. This function is for resolving the appropriate
2848 version of c_f_pointer() and c_f_procpointer(). For example, a
2849 call to c_f_pointer() for a default integer pointer could have a
2850 name of c_f_pointer_i4. If no second arg exists, which is an error
2851 for these two functions, it defaults to the generic symbol's name
2852 and binding label. */
2853
2854static void
2855set_name_and_label (gfc_code *c, gfc_symbol *sym,
2856 char *name, char *binding_label)
2857{
2858 gfc_expr *arg = NULL;
2859 char type;
2860 int kind;
2861
2862 /* The second arg of c_f_pointer and c_f_procpointer determines
2863 the type and kind for the procedure name. */
2864 arg = c->ext.actual->next->expr;
2865
2866 if (arg != NULL)
2867 {
2868 /* Set up the name to have the given symbol's name,
2869 plus the type and kind. */
2870 /* a derived type is marked with the type letter 'u' */
2871 if (arg->ts.type == BT_DERIVED)
2872 {
2873 type = 'd';
2874 kind = 0; /* set the kind as 0 for now */
2875 }
2876 else
2877 {
2878 type = gfc_type_letter (arg->ts.type);
2879 kind = arg->ts.kind;
2880 }
6ad5cf72
CR
2881
2882 if (arg->ts.type == BT_CHARACTER)
2883 /* Kind info for character strings not needed. */
2884 kind = 0;
2885
a8b3b0b6
CR
2886 sprintf (name, "%s_%c%d", sym->name, type, kind);
2887 /* Set up the binding label as the given symbol's label plus
2888 the type and kind. */
2889 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2890 }
2891 else
2892 {
2893 /* If the second arg is missing, set the name and label as
2894 was, cause it should at least be found, and the missing
2895 arg error will be caught by compare_parameters(). */
2896 sprintf (name, "%s", sym->name);
2897 sprintf (binding_label, "%s", sym->binding_label);
2898 }
2899
2900 return;
2901}
2902
2903
2904/* Resolve a generic version of the iso_c_binding procedure given
2905 (sym) to the specific one based on the type and kind of the
2906 argument(s). Currently, this function resolves c_f_pointer() and
2907 c_f_procpointer based on the type and kind of the second argument
2908 (FPTR). Other iso_c_binding procedures aren't specially handled.
2909 Upon successfully exiting, c->resolved_sym will hold the resolved
2910 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2911 otherwise. */
2912
2913match
2914gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2915{
2916 gfc_symbol *new_sym;
2917 /* this is fine, since we know the names won't use the max */
2918 char name[GFC_MAX_SYMBOL_LEN + 1];
2919 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2920 /* default to success; will override if find error */
2921 match m = MATCH_YES;
d8fa96e0
CR
2922
2923 /* Make sure the actual arguments are in the necessary order (based on the
2924 formal args) before resolving. */
2925 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2926
a8b3b0b6
CR
2927 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2928 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2929 {
2930 set_name_and_label (c, sym, name, binding_label);
2931
2932 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2933 {
2934 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2935 {
d8fa96e0
CR
2936 /* Make sure we got a third arg if the second arg has non-zero
2937 rank. We must also check that the type and rank are
2938 correct since we short-circuit this check in
2939 gfc_procedure_use() (called above to sort actual args). */
2940 if (c->ext.actual->next->expr->rank != 0)
a8b3b0b6 2941 {
d8fa96e0
CR
2942 if(c->ext.actual->next->next == NULL
2943 || c->ext.actual->next->next->expr == NULL)
2944 {
2945 m = MATCH_ERROR;
2946 gfc_error ("Missing SHAPE parameter for call to %s "
2947 "at %L", sym->name, &(c->loc));
2948 }
2949 else if (c->ext.actual->next->next->expr->ts.type
2950 != BT_INTEGER
2951 || c->ext.actual->next->next->expr->rank != 1)
2952 {
2953 m = MATCH_ERROR;
2954 gfc_error ("SHAPE parameter for call to %s at %L must "
2955 "be a rank 1 INTEGER array", sym->name,
2956 &(c->loc));
2957 }
a8b3b0b6 2958 }
a8b3b0b6
CR
2959 }
2960 }
2961
2962 if (m != MATCH_ERROR)
2963 {
2964 /* the 1 means to add the optional arg to formal list */
2965 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2966
2967 /* for error reporting, say it's declared where the original was */
2968 new_sym->declared_at = sym->declared_at;
2969 }
2970 }
a8b3b0b6
CR
2971 else
2972 {
2973 /* no differences for c_loc or c_funloc */
2974 new_sym = sym;
2975 }
2976
2977 /* set the resolved symbol */
2978 if (m != MATCH_ERROR)
d8fa96e0 2979 c->resolved_sym = new_sym;
a8b3b0b6
CR
2980 else
2981 c->resolved_sym = sym;
2982
2983 return m;
2984}
2985
2986
6de9cd9a
DN
2987/* Resolve a subroutine call known to be specific. */
2988
2989static match
edf1eac2 2990resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
2991{
2992 match m;
2993
a8b3b0b6
CR
2994 if(sym->attr.is_iso_c)
2995 {
2996 m = gfc_iso_c_sub_interface (c,sym);
2997 return m;
2998 }
2999
6de9cd9a
DN
3000 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3001 {
3002 if (sym->attr.dummy)
3003 {
3004 sym->attr.proc = PROC_DUMMY;
3005 goto found;
3006 }
3007
3008 sym->attr.proc = PROC_EXTERNAL;
3009 goto found;
3010 }
3011
3012 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3013 goto found;
3014
3015 if (sym->attr.intrinsic)
3016 {
3017 m = gfc_intrinsic_sub_interface (c, 1);
3018 if (m == MATCH_YES)
3019 return MATCH_YES;
3020 if (m == MATCH_NO)
3021 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3022 "with an intrinsic", sym->name, &c->loc);
3023
3024 return MATCH_ERROR;
3025 }
3026
3027 return MATCH_NO;
3028
3029found:
3030 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3031
3032 c->resolved_sym = sym;
3033 pure_subroutine (c, sym);
3034
3035 return MATCH_YES;
3036}
3037
3038
17b1d2a0 3039static gfc_try
edf1eac2 3040resolve_specific_s (gfc_code *c)
6de9cd9a
DN
3041{
3042 gfc_symbol *sym;
3043 match m;
3044
3045 sym = c->symtree->n.sym;
3046
8c086c9c 3047 for (;;)
6de9cd9a
DN
3048 {
3049 m = resolve_specific_s0 (c, sym);
3050 if (m == MATCH_YES)
3051 return SUCCESS;
3052 if (m == MATCH_ERROR)
3053 return FAILURE;
8c086c9c
PT
3054
3055 if (sym->ns->parent == NULL)
3056 break;
3057
3058 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3059
3060 if (sym == NULL)
3061 break;
6de9cd9a
DN
3062 }
3063
8c086c9c 3064 sym = c->symtree->n.sym;
6de9cd9a
DN
3065 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3066 sym->name, &c->loc);
3067
3068 return FAILURE;
3069}
3070
3071
3072/* Resolve a subroutine call not known to be generic nor specific. */
3073
17b1d2a0 3074static gfc_try
edf1eac2 3075resolve_unknown_s (gfc_code *c)
6de9cd9a
DN
3076{
3077 gfc_symbol *sym;
3078
3079 sym = c->symtree->n.sym;
3080
3081 if (sym->attr.dummy)
3082 {
3083 sym->attr.proc = PROC_DUMMY;
3084 goto found;
3085 }
3086
3087 /* See if we have an intrinsic function reference. */
3088
c3005b0f 3089 if (gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a
DN
3090 {
3091 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3092 return SUCCESS;
3093 return FAILURE;
3094 }
3095
3096 /* The reference is to an external name. */
3097
3098found:
3099 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3100
3101 c->resolved_sym = sym;
3102
3103 pure_subroutine (c, sym);
3104
3105 return SUCCESS;
3106}
3107
3108
3109/* Resolve a subroutine call. Although it was tempting to use the same code
3110 for functions, subroutines and functions are stored differently and this
3111 makes things awkward. */
3112
17b1d2a0 3113static gfc_try
edf1eac2 3114resolve_call (gfc_code *c)
6de9cd9a 3115{
17b1d2a0 3116 gfc_try t;
7fcafa71 3117 procedure_type ptype = PROC_INTRINSIC;
67cec813 3118 gfc_symbol *csym, *sym;
0b4e2af7
PT
3119 bool no_formal_args;
3120
3121 csym = c->symtree ? c->symtree->n.sym : NULL;
6de9cd9a 3122
0b4e2af7 3123 if (csym && csym->ts.type != BT_UNKNOWN)
2ed8d224
PT
3124 {
3125 gfc_error ("'%s' at %L has a type, which is not consistent with "
0b4e2af7 3126 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
2ed8d224
PT
3127 return FAILURE;
3128 }
3129
67cec813
PT
3130 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3131 {
79b1d36c
PT
3132 gfc_symtree *st;
3133 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3134 sym = st ? st->n.sym : NULL;
67cec813
PT
3135 if (sym && csym != sym
3136 && sym->ns == gfc_current_ns
3137 && sym->attr.flavor == FL_PROCEDURE
3138 && sym->attr.contained)
3139 {
3140 sym->refs++;
79b1d36c
PT
3141 if (csym->attr.generic)
3142 c->symtree->n.sym = sym;
3143 else
3144 c->symtree = st;
3145 csym = c->symtree->n.sym;
67cec813
PT
3146 }
3147 }
3148
8bae6273
JW
3149 /* If this ia a deferred TBP with an abstract interface
3150 (which may of course be referenced), c->expr1 will be set. */
3151 if (csym && csym->attr.abstract && !c->expr1)
3152 {
3153 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3154 csym->name, &c->loc);
3155 return FAILURE;
3156 }
3157
77f131ca
FXC
3158 /* Subroutines without the RECURSIVE attribution are not allowed to
3159 * call themselves. */
1933ba0f 3160 if (csym && is_illegal_recursion (csym, gfc_current_ns))
77f131ca 3161 {
1933ba0f
DK
3162 if (csym->attr.entry && csym->ns->entries)
3163 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3164 " subroutine '%s' is not RECURSIVE",
edf1eac2 3165 csym->name, &c->loc, csym->ns->entries->sym->name);
1933ba0f
DK
3166 else
3167 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3168 " is not RECURSIVE", csym->name, &c->loc);
3169
3170 t = FAILURE;
77f131ca
FXC
3171 }
3172
48474141
PT
3173 /* Switch off assumed size checking and do this again for certain kinds
3174 of procedure, once the procedure itself is resolved. */
3175 need_full_assumed_size++;
3176
0b4e2af7
PT
3177 if (csym)
3178 ptype = csym->attr.proc;
7fcafa71 3179
0b4e2af7
PT
3180 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3181 if (resolve_actual_arglist (c->ext.actual, ptype,
3182 no_formal_args) == FAILURE)
6de9cd9a
DN
3183 return FAILURE;
3184
66e4ab31 3185 /* Resume assumed_size checking. */
48474141
PT
3186 need_full_assumed_size--;
3187
71a7778c
PT
3188 /* If external, check for usage. */
3189 if (csym && is_external_proc (csym))
3190 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3191
1524f80b
RS
3192 t = SUCCESS;
3193 if (c->resolved_sym == NULL)
12f681a0
DK
3194 {
3195 c->resolved_isym = NULL;
3196 switch (procedure_kind (csym))
3197 {
3198 case PTYPE_GENERIC:
3199 t = resolve_generic_s (c);
3200 break;
6de9cd9a 3201
12f681a0
DK
3202 case PTYPE_SPECIFIC:
3203 t = resolve_specific_s (c);
3204 break;
6de9cd9a 3205
12f681a0
DK
3206 case PTYPE_UNKNOWN:
3207 t = resolve_unknown_s (c);
3208 break;
6de9cd9a 3209
12f681a0
DK
3210 default:
3211 gfc_internal_error ("resolve_subroutine(): bad function type");
3212 }
3213 }
6de9cd9a 3214
b8ea6dbc
PT
3215 /* Some checks of elemental subroutine actual arguments. */
3216 if (resolve_elemental_actual (NULL, c) == FAILURE)
3217 return FAILURE;
48474141 3218
23d1b451 3219 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
1524f80b 3220 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
6de9cd9a
DN
3221 return t;
3222}
3223
edf1eac2 3224
2c5ed587
SK
3225/* Compare the shapes of two arrays that have non-NULL shapes. If both
3226 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3227 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3228 if their shapes do not match. If either op1->shape or op2->shape is
3229 NULL, return SUCCESS. */
3230
17b1d2a0 3231static gfc_try
edf1eac2 3232compare_shapes (gfc_expr *op1, gfc_expr *op2)
2c5ed587 3233{
17b1d2a0 3234 gfc_try t;
2c5ed587
SK
3235 int i;
3236
3237 t = SUCCESS;
05c1e3a7 3238
2c5ed587
SK
3239 if (op1->shape != NULL && op2->shape != NULL)
3240 {
3241 for (i = 0; i < op1->rank; i++)
3242 {
3243 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3244 {
3245 gfc_error ("Shapes for operands at %L and %L are not conformable",
3246 &op1->where, &op2->where);
3247 t = FAILURE;
3248 break;
3249 }
3250 }
3251 }
3252
3253 return t;
3254}
6de9cd9a 3255
edf1eac2 3256
6de9cd9a
DN
3257/* Resolve an operator expression node. This can involve replacing the
3258 operation with a user defined function call. */
3259
17b1d2a0 3260static gfc_try
edf1eac2 3261resolve_operator (gfc_expr *e)
6de9cd9a
DN
3262{
3263 gfc_expr *op1, *op2;
3264 char msg[200];
27189292 3265 bool dual_locus_error;
17b1d2a0 3266 gfc_try t;
6de9cd9a
DN
3267
3268 /* Resolve all subnodes-- give them types. */
3269
a1ee985f 3270 switch (e->value.op.op)
6de9cd9a
DN
3271 {
3272 default:
58b03ab2 3273 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
6de9cd9a
DN
3274 return FAILURE;
3275
3276 /* Fall through... */
3277
3278 case INTRINSIC_NOT:
3279 case INTRINSIC_UPLUS:
3280 case INTRINSIC_UMINUS:
2414e1d6 3281 case INTRINSIC_PARENTHESES:
58b03ab2 3282 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
6de9cd9a
DN
3283 return FAILURE;
3284 break;
3285 }
3286
3287 /* Typecheck the new node. */
3288
58b03ab2
TS
3289 op1 = e->value.op.op1;
3290 op2 = e->value.op.op2;
27189292 3291 dual_locus_error = false;
6de9cd9a 3292
bb9e683e
TB
3293 if ((op1 && op1->expr_type == EXPR_NULL)
3294 || (op2 && op2->expr_type == EXPR_NULL))
3295 {
3296 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3297 goto bad_op;
3298 }
3299
a1ee985f 3300 switch (e->value.op.op)
6de9cd9a
DN
3301 {
3302 case INTRINSIC_UPLUS:
3303 case INTRINSIC_UMINUS:
3304 if (op1->ts.type == BT_INTEGER
3305 || op1->ts.type == BT_REAL
3306 || op1->ts.type == BT_COMPLEX)
3307 {
3308 e->ts = op1->ts;
3309 break;
3310 }
3311
31043f6c 3312 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
a1ee985f 3313 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
6de9cd9a
DN
3314 goto bad_op;
3315
3316 case INTRINSIC_PLUS:
3317 case INTRINSIC_MINUS:
3318 case INTRINSIC_TIMES:
3319 case INTRINSIC_DIVIDE:
3320 case INTRINSIC_POWER:
3321 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3322 {
3323 gfc_type_convert_binary (e);
3324 break;
3325 }
3326
3327 sprintf (msg,
31043f6c 3328 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
a1ee985f 3329 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3330 gfc_typename (&op2->ts));
3331 goto bad_op;
3332
3333 case INTRINSIC_CONCAT:
d393bbd7
FXC
3334 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3335 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3336 {
3337 e->ts.type = BT_CHARACTER;
3338 e->ts.kind = op1->ts.kind;
3339 break;
3340 }
3341
3342 sprintf (msg,
31043f6c 3343 _("Operands of string concatenation operator at %%L are %s/%s"),
6de9cd9a
DN
3344 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3345 goto bad_op;
3346
3347 case INTRINSIC_AND:
3348 case INTRINSIC_OR:
3349 case INTRINSIC_EQV:
3350 case INTRINSIC_NEQV:
3351 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3352 {
3353 e->ts.type = BT_LOGICAL;
3354 e->ts.kind = gfc_kind_max (op1, op2);
edf1eac2
SK
3355 if (op1->ts.kind < e->ts.kind)
3356 gfc_convert_type (op1, &e->ts, 2);
3357 else if (op2->ts.kind < e->ts.kind)
3358 gfc_convert_type (op2, &e->ts, 2);
6de9cd9a
DN
3359 break;
3360 }
3361
31043f6c 3362 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
a1ee985f 3363 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3364 gfc_typename (&op2->ts));
3365
3366 goto bad_op;
3367
3368 case INTRINSIC_NOT:
3369 if (op1->ts.type == BT_LOGICAL)
3370 {
3371 e->ts.type = BT_LOGICAL;
3372 e->ts.kind = op1->ts.kind;
3373 break;
3374 }
3375
3bed9dd0 3376 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
6de9cd9a
DN
3377 gfc_typename (&op1->ts));
3378 goto bad_op;
3379
3380 case INTRINSIC_GT:
3bed9dd0 3381 case INTRINSIC_GT_OS:
6de9cd9a 3382 case INTRINSIC_GE:
3bed9dd0 3383 case INTRINSIC_GE_OS:
6de9cd9a 3384 case INTRINSIC_LT:
3bed9dd0 3385 case INTRINSIC_LT_OS:
6de9cd9a 3386 case INTRINSIC_LE:
3bed9dd0 3387 case INTRINSIC_LE_OS:
6de9cd9a
DN
3388 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3389 {
31043f6c 3390 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
6de9cd9a
DN
3391 goto bad_op;
3392 }
3393
3394 /* Fall through... */
3395
3396 case INTRINSIC_EQ:
3bed9dd0 3397 case INTRINSIC_EQ_OS:
6de9cd9a 3398 case INTRINSIC_NE:
3bed9dd0 3399 case INTRINSIC_NE_OS:
d393bbd7
FXC
3400 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3401 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3402 {
3403 e->ts.type = BT_LOGICAL;
9d64df18 3404 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3405 break;
3406 }
3407
3408 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3409 {
3410 gfc_type_convert_binary (e);
3411
3412 e->ts.type = BT_LOGICAL;
9d64df18 3413 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3414 break;
3415 }
3416
6a28f513 3417 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
31043f6c 3418 sprintf (msg,
edf1eac2 3419 _("Logicals at %%L must be compared with %s instead of %s"),
a1ee985f
KG
3420 (e->value.op.op == INTRINSIC_EQ
3421 || e->value.op.op == INTRINSIC_EQ_OS)
3422 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
6a28f513 3423 else
31043f6c 3424 sprintf (msg,
edf1eac2 3425 _("Operands of comparison operator '%s' at %%L are %s/%s"),
a1ee985f 3426 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6a28f513 3427 gfc_typename (&op2->ts));
6de9cd9a
DN
3428
3429 goto bad_op;
3430
3431 case INTRINSIC_USER:
a1ee985f 3432 if (e->value.op.uop->op == NULL)
622af87f
DF
3433 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3434 else if (op2 == NULL)
31043f6c 3435 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
58b03ab2 3436 e->value.op.uop->name, gfc_typename (&op1->ts));
6de9cd9a 3437 else
31043f6c 3438 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
58b03ab2 3439 e->value.op.uop->name, gfc_typename (&op1->ts),
6de9cd9a
DN
3440 gfc_typename (&op2->ts));
3441
3442 goto bad_op;
3443
2414e1d6 3444 case INTRINSIC_PARENTHESES:
dcdc83a1
TS
3445 e->ts = op1->ts;
3446 if (e->ts.type == BT_CHARACTER)
bc21d315 3447 e->ts.u.cl = op1->ts.u.cl;
2414e1d6
TS
3448 break;
3449
6de9cd9a
DN
3450 default:
3451 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3452 }
3453
3454 /* Deal with arrayness of an operand through an operator. */
3455
3456 t = SUCCESS;
3457
a1ee985f 3458 switch (e->value.op.op)
6de9cd9a
DN
3459 {
3460 case INTRINSIC_PLUS:
3461 case INTRINSIC_MINUS:
3462 case INTRINSIC_TIMES:
3463 case INTRINSIC_DIVIDE:
3464 case INTRINSIC_POWER:
3465 case INTRINSIC_CONCAT:
3466 case INTRINSIC_AND:
3467 case INTRINSIC_OR:
3468 case INTRINSIC_EQV:
3469 case INTRINSIC_NEQV:
3470 case INTRINSIC_EQ:
3bed9dd0 3471 case INTRINSIC_EQ_OS:
6de9cd9a 3472 case INTRINSIC_NE:
3bed9dd0 3473 case INTRINSIC_NE_OS:
6de9cd9a 3474 case INTRINSIC_GT:
3bed9dd0 3475 case INTRINSIC_GT_OS:
6de9cd9a 3476 case INTRINSIC_GE:
3bed9dd0 3477 case INTRINSIC_GE_OS:
6de9cd9a 3478 case INTRINSIC_LT:
3bed9dd0 3479 case INTRINSIC_LT_OS:
6de9cd9a 3480 case INTRINSIC_LE:
3bed9dd0 3481 case INTRINSIC_LE_OS:
6de9cd9a
DN
3482
3483 if (op1->rank == 0 && op2->rank == 0)
3484 e->rank = 0;
3485
3486 if (op1->rank == 0 && op2->rank != 0)
3487 {
3488 e->rank = op2->rank;
3489
3490 if (e->shape == NULL)
3491 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3492 }
3493
3494 if (op1->rank != 0 && op2->rank == 0)
3495 {
3496 e->rank = op1->rank;
3497
3498 if (e->shape == NULL)
3499 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3500 }
3501
3502 if (op1->rank != 0 && op2->rank != 0)
3503 {
3504 if (op1->rank == op2->rank)
3505 {
3506 e->rank = op1->rank;
6de9cd9a 3507 if (e->shape == NULL)
2c5ed587
SK
3508 {
3509 t = compare_shapes(op1, op2);
3510 if (t == FAILURE)
3511 e->shape = NULL;
3512 else
6de9cd9a 3513 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2c5ed587 3514 }
6de9cd9a
DN
3515 }
3516 else
3517 {
edf1eac2 3518 /* Allow higher level expressions to work. */
6de9cd9a 3519 e->rank = 0;
27189292
FXC
3520
3521 /* Try user-defined operators, and otherwise throw an error. */
3522 dual_locus_error = true;
3523 sprintf (msg,
3524 _("Inconsistent ranks for operator at %%L and %%L"));
3525 goto bad_op;
6de9cd9a
DN
3526 }
3527 }
3528
3529 break;
3530
08113c73 3531 case INTRINSIC_PARENTHESES:
6de9cd9a
DN
3532 case INTRINSIC_NOT:
3533 case INTRINSIC_UPLUS:
3534 case INTRINSIC_UMINUS:
08113c73 3535 /* Simply copy arrayness attribute */
6de9cd9a
DN
3536 e->rank = op1->rank;
3537
3538 if (e->shape == NULL)
3539 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3540
6de9cd9a
DN
3541 break;
3542
3543 default:
3544 break;
3545 }
3546
3547 /* Attempt to simplify the expression. */
3548 if (t == SUCCESS)
dd5ecf41
PT
3549 {
3550 t = gfc_simplify_expr (e, 0);
3551 /* Some calls do not succeed in simplification and return FAILURE
df2fba9e 3552 even though there is no error; e.g. variable references to
dd5ecf41
PT
3553 PARAMETER arrays. */
3554 if (!gfc_is_constant_expr (e))
3555 t = SUCCESS;
3556 }
6de9cd9a
DN
3557 return t;
3558
3559bad_op:
2c5ed587 3560
4a44a72d
DK
3561 {
3562 bool real_error;
3563 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3564 return SUCCESS;
3565
3566 if (real_error)
3567 return FAILURE;
3568 }
6de9cd9a 3569
27189292
FXC
3570 if (dual_locus_error)
3571 gfc_error (msg, &op1->where, &op2->where);
3572 else
3573 gfc_error (msg, &e->where);
2c5ed587 3574
6de9cd9a
DN
3575 return FAILURE;
3576}
3577
3578
3579/************** Array resolution subroutines **************/
3580
6de9cd9a
DN
3581typedef enum
3582{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3583comparison;
3584
3585/* Compare two integer expressions. */
3586
3587static comparison
edf1eac2 3588compare_bound (gfc_expr *a, gfc_expr *b)
6de9cd9a
DN
3589{
3590 int i;
3591
3592 if (a == NULL || a->expr_type != EXPR_CONSTANT
3593 || b == NULL || b->expr_type != EXPR_CONSTANT)
3594 return CMP_UNKNOWN;
3595
df80a455
TK
3596 /* If either of the types isn't INTEGER, we must have
3597 raised an error earlier. */
3598
6de9cd9a 3599 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
df80a455 3600 return CMP_UNKNOWN;
6de9cd9a
DN
3601
3602 i = mpz_cmp (a->value.integer, b->value.integer);
3603
3604 if (i < 0)
3605 return CMP_LT;
3606 if (i > 0)
3607 return CMP_GT;
3608 return CMP_EQ;
3609}
3610
3611
3612/* Compare an integer expression with an integer. */
3613
3614static comparison
edf1eac2 3615compare_bound_int (gfc_expr *a, int b)
6de9cd9a
DN
3616{
3617 int i;
3618
3619 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3620 return CMP_UNKNOWN;
3621
3622 if (a->ts.type != BT_INTEGER)
3623 gfc_internal_error ("compare_bound_int(): Bad expression");
3624
3625 i = mpz_cmp_si (a->value.integer, b);
3626
3627 if (i < 0)
3628 return CMP_LT;
3629 if (i > 0)
3630 return CMP_GT;
3631 return CMP_EQ;
3632}
3633
3634
0094f362
FXC
3635/* Compare an integer expression with a mpz_t. */
3636
3637static comparison
edf1eac2 3638compare_bound_mpz_t (gfc_expr *a, mpz_t b)
0094f362
FXC
3639{
3640 int i;
3641
3642 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3643 return CMP_UNKNOWN;
3644
3645 if (a->ts.type != BT_INTEGER)
3646 gfc_internal_error ("compare_bound_int(): Bad expression");
3647
3648 i = mpz_cmp (a->value.integer, b);
3649
3650 if (i < 0)
3651 return CMP_LT;
3652 if (i > 0)
3653 return CMP_GT;
3654 return CMP_EQ;
3655}
3656
3657
3658/* Compute the last value of a sequence given by a triplet.
3659 Return 0 if it wasn't able to compute the last value, or if the
3660 sequence if empty, and 1 otherwise. */
3661
3662static int
edf1eac2
SK
3663compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3664 gfc_expr *stride, mpz_t last)
0094f362
FXC
3665{
3666 mpz_t rem;
3667
3668 if (start == NULL || start->expr_type != EXPR_CONSTANT
3669 || end == NULL || end->expr_type != EXPR_CONSTANT
3670 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3671 return 0;
3672
3673 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3674 || (stride != NULL && stride->ts.type != BT_INTEGER))
3675 return 0;
3676
3677 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3678 {
3679 if (compare_bound (start, end) == CMP_GT)
3680 return 0;
3681 mpz_set (last, end->value.integer);
3682 return 1;
3683 }
05c1e3a7 3684
0094f362
FXC
3685 if (compare_bound_int (stride, 0) == CMP_GT)
3686 {
3687 /* Stride is positive */
3688 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3689 return 0;
3690 }
3691 else
3692 {
3693 /* Stride is negative */
3694 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3695 return 0;
3696 }
3697
3698 mpz_init (rem);
3699 mpz_sub (rem, end->value.integer, start->value.integer);
3700 mpz_tdiv_r (rem, rem, stride->value.integer);
3701 mpz_sub (last, end->value.integer, rem);
3702 mpz_clear (rem);
3703
3704 return 1;
3705}
3706
3707
6de9cd9a
DN
3708/* Compare a single dimension of an array reference to the array
3709 specification. */
3710
17b1d2a0 3711static gfc_try
edf1eac2 3712check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
6de9cd9a 3713{
0094f362 3714 mpz_t last_value;
6de9cd9a
DN
3715
3716/* Given start, end and stride values, calculate the minimum and
f7b529fa 3717 maximum referenced indexes. */
6de9cd9a 3718
1954a27b 3719 switch (ar->dimen_type[i])
6de9cd9a 3720 {
1954a27b 3721 case DIMEN_VECTOR:
6de9cd9a
DN
3722 break;
3723
1954a27b 3724 case DIMEN_ELEMENT:
6de9cd9a 3725 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1954a27b
TB
3726 {
3727 gfc_warning ("Array reference at %L is out of bounds "
3728 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3729 mpz_get_si (ar->start[i]->value.integer),
3730 mpz_get_si (as->lower[i]->value.integer), i+1);
3731 return SUCCESS;
3732 }
6de9cd9a 3733 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1954a27b
TB
3734 {
3735 gfc_warning ("Array reference at %L is out of bounds "
3736 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3737 mpz_get_si (ar->start[i]->value.integer),
3738 mpz_get_si (as->upper[i]->value.integer), i+1);
3739 return SUCCESS;
3740 }
6de9cd9a
DN
3741
3742 break;
3743
1954a27b 3744 case DIMEN_RANGE:
d912240d 3745 {
0094f362
FXC
3746#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3747#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3748
d912240d 3749 comparison comp_start_end = compare_bound (AR_START, AR_END);
0094f362 3750
d912240d
FXC
3751 /* Check for zero stride, which is not allowed. */
3752 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3753 {
3754 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3755 return FAILURE;
3756 }
3757
3758 /* if start == len || (stride > 0 && start < len)
3759 || (stride < 0 && start > len),
3760 then the array section contains at least one element. In this
3761 case, there is an out-of-bounds access if
3762 (start < lower || start > upper). */
3763 if (compare_bound (AR_START, AR_END) == CMP_EQ
3764 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3765 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3766 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3767 && comp_start_end == CMP_GT))
3768 {
1954a27b
TB
3769 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3770 {
3771 gfc_warning ("Lower array reference at %L is out of bounds "
3772 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3773 mpz_get_si (AR_START->value.integer),
3774 mpz_get_si (as->lower[i]->value.integer), i+1);
3775 return SUCCESS;
3776 }
3777 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3778 {
3779 gfc_warning ("Lower array reference at %L is out of bounds "
3780 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3781 mpz_get_si (AR_START->value.integer),
3782 mpz_get_si (as->upper[i]->value.integer), i+1);
3783 return SUCCESS;
3784 }
d912240d
FXC
3785 }
3786
3787 /* If we can compute the highest index of the array section,
3788 then it also has to be between lower and upper. */
3789 mpz_init (last_value);
3790 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3791 last_value))
3792 {
1954a27b
TB
3793 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3794 {
3795 gfc_warning ("Upper array reference at %L is out of bounds "
3796 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3797 mpz_get_si (last_value),
3798 mpz_get_si (as->lower[i]->value.integer), i+1);
3799 mpz_clear (last_value);
3800 return SUCCESS;
3801 }
3802 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
d912240d 3803 {
1954a27b
TB
3804 gfc_warning ("Upper array reference at %L is out of bounds "
3805 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3806 mpz_get_si (last_value),
3807 mpz_get_si (as->upper[i]->value.integer), i+1);
d912240d 3808 mpz_clear (last_value);
1954a27b 3809 return SUCCESS;
d912240d
FXC
3810 }
3811 }
3812 mpz_clear (last_value);
0094f362
FXC
3813
3814#undef AR_START
3815#undef AR_END
d912240d 3816 }
6de9cd9a
DN
3817 break;
3818
3819 default:
3820 gfc_internal_error ("check_dimension(): Bad array reference");
3821 }
3822
3823 return SUCCESS;
6de9cd9a
DN
3824}
3825
3826
3827/* Compare an array reference with an array specification. */
3828
17b1d2a0 3829static gfc_try
edf1eac2 3830compare_spec_to_ref (gfc_array_ref *ar)
6de9cd9a
DN
3831{
3832 gfc_array_spec *as;
3833 int i;
3834
3835 as = ar->as;
3836 i = as->rank - 1;
3837 /* TODO: Full array sections are only allowed as actual parameters. */
3838 if (as->type == AS_ASSUMED_SIZE
3839 && (/*ar->type == AR_FULL
edf1eac2
SK
3840 ||*/ (ar->type == AR_SECTION
3841 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
6de9cd9a 3842 {
edf1eac2
SK
3843 gfc_error ("Rightmost upper bound of assumed size array section "
3844 "not specified at %L", &ar->where);
6de9cd9a
DN
3845 return FAILURE;
3846 }
3847
3848 if (ar->type == AR_FULL)
3849 return SUCCESS;
3850
3851 if (as->rank != ar->dimen)
3852 {
3853 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3854 &ar->where, ar->dimen, as->rank);
3855 return FAILURE;
3856 }
3857
3858 for (i = 0; i < as->rank; i++)
3859 if (check_dimension (i, ar, as) == FAILURE)
3860 return FAILURE;
3861
3862 return SUCCESS;
3863}
3864
3865
3866/* Resolve one part of an array index. */
3867
17b1d2a0 3868gfc_try
edf1eac2 3869gfc_resolve_index (gfc_expr *index, int check_scalar)
6de9cd9a
DN
3870{
3871 gfc_typespec ts;
3872
3873 if (index == NULL)
3874 return SUCCESS;
3875
3876 if (gfc_resolve_expr (index) == FAILURE)
3877 return FAILURE;
3878
ee943062 3879 if (check_scalar && index->rank != 0)
6de9cd9a 3880 {
ee943062 3881 gfc_error ("Array index at %L must be scalar", &index->where);
6de9cd9a
DN
3882 return FAILURE;
3883 }
3884
ee943062 3885 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
6de9cd9a 3886 {
acb388a0
JD
3887 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3888 &index->where, gfc_basic_typename (index->ts.type));
6de9cd9a
DN
3889 return FAILURE;
3890 }
3891
ee943062 3892 if (index->ts.type == BT_REAL)
7fdf6c69 3893 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
ee943062
TS
3894 &index->where) == FAILURE)
3895 return FAILURE;
3896
3897 if (index->ts.kind != gfc_index_integer_kind
3898 || index->ts.type != BT_INTEGER)
6de9cd9a 3899 {
810306f2 3900 gfc_clear_ts (&ts);
6de9cd9a
DN
3901 ts.type = BT_INTEGER;
3902 ts.kind = gfc_index_integer_kind;
3903
3904 gfc_convert_type_warn (index, &ts, 2, 0);
3905 }
3906
3907 return SUCCESS;
3908}
3909
bf302220
TK
3910/* Resolve a dim argument to an intrinsic function. */
3911
17b1d2a0 3912gfc_try
bf302220
TK
3913gfc_resolve_dim_arg (gfc_expr *dim)
3914{
3915 if (dim == NULL)
3916 return SUCCESS;
3917
3918 if (gfc_resolve_expr (dim) == FAILURE)
3919 return FAILURE;
3920
3921 if (dim->rank != 0)
3922 {
3923 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3924 return FAILURE;
05c1e3a7 3925
bf302220 3926 }
33717d59 3927
bf302220
TK
3928 if (dim->ts.type != BT_INTEGER)
3929 {
3930 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3931 return FAILURE;
3932 }
33717d59 3933
bf302220
TK
3934 if (dim->ts.kind != gfc_index_integer_kind)
3935 {
3936 gfc_typespec ts;
3937
3938 ts.type = BT_INTEGER;
3939 ts.kind = gfc_index_integer_kind;
3940
3941 gfc_convert_type_warn (dim, &ts, 2, 0);
3942 }
3943
3944 return SUCCESS;
3945}
6de9cd9a
DN
3946
3947/* Given an expression that contains array references, update those array
3948 references to point to the right array specifications. While this is
3949 filled in during matching, this information is difficult to save and load
3950 in a module, so we take care of it here.
3951
3952 The idea here is that the original array reference comes from the
3953 base symbol. We traverse the list of reference structures, setting
3954 the stored reference to references. Component references can
3955 provide an additional array specification. */
3956
3957static void
edf1eac2 3958find_array_spec (gfc_expr *e)
6de9cd9a
DN
3959{
3960 gfc_array_spec *as;
3961 gfc_component *c;
014057c5 3962 gfc_symbol *derived;
6de9cd9a
DN
3963 gfc_ref *ref;
3964
cf2b3c22
TB
3965 if (e->symtree->n.sym->ts.type == BT_CLASS)
3966 as = e->symtree->n.sym->ts.u.derived->components->as;
3967 else
3968 as = e->symtree->n.sym->as;
014057c5 3969 derived = NULL;
6de9cd9a
DN
3970
3971 for (ref = e->ref; ref; ref = ref->next)
3972 switch (ref->type)
3973 {
3974 case REF_ARRAY:
3975 if (as == NULL)
3976 gfc_internal_error ("find_array_spec(): Missing spec");
3977
3978 ref->u.ar.as = as;
3979 as = NULL;
3980 break;
3981
3982 case REF_COMPONENT:
014057c5 3983 if (derived == NULL)
bc21d315 3984 derived = e->symtree->n.sym->ts.u.derived;
014057c5
PT
3985
3986 c = derived->components;
3987
3988 for (; c; c = c->next)
6de9cd9a 3989 if (c == ref->u.c.component)
014057c5
PT
3990 {
3991 /* Track the sequence of component references. */
3992 if (c->ts.type == BT_DERIVED)
bc21d315 3993 derived = c->ts.u.derived;
014057c5
PT
3994 break;
3995 }
6de9cd9a
DN
3996
3997 if (c == NULL)
3998 gfc_internal_error ("find_array_spec(): Component not found");
3999
d4b7d0f0 4000 if (c->attr.dimension)
6de9cd9a
DN
4001 {
4002 if (as != NULL)
4003 gfc_internal_error ("find_array_spec(): unused as(1)");
4004 as = c->as;
4005 }
4006
6de9cd9a
DN
4007 break;
4008
4009 case REF_SUBSTRING:
4010 break;
4011 }
4012
4013 if (as != NULL)
4014 gfc_internal_error ("find_array_spec(): unused as(2)");
4015}
4016
4017
4018/* Resolve an array reference. */
4019
17b1d2a0 4020static gfc_try
edf1eac2 4021resolve_array_ref (gfc_array_ref *ar)
6de9cd9a
DN
4022{
4023 int i, check_scalar;
b6398823 4024 gfc_expr *e;
6de9cd9a
DN
4025
4026 for (i = 0; i < ar->dimen; i++)
4027 {
4028 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4029
4030 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4031 return FAILURE;
4032 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4033 return FAILURE;
4034 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4035 return FAILURE;
4036
b6398823
PT
4037 e = ar->start[i];
4038
6de9cd9a 4039 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
b6398823 4040 switch (e->rank)
6de9cd9a
DN
4041 {
4042 case 0:
4043 ar->dimen_type[i] = DIMEN_ELEMENT;
4044 break;
4045
4046 case 1:
4047 ar->dimen_type[i] = DIMEN_VECTOR;
b6398823 4048 if (e->expr_type == EXPR_VARIABLE
edf1eac2 4049 && e->symtree->n.sym->ts.type == BT_DERIVED)
b6398823 4050 ar->start[i] = gfc_get_parentheses (e);
6de9cd9a
DN
4051 break;
4052
4053 default:
4054 gfc_error ("Array index at %L is an array of rank %d",
b6398823 4055 &ar->c_where[i], e->rank);
6de9cd9a
DN
4056 return FAILURE;
4057 }
4058 }
4059
4060 /* If the reference type is unknown, figure out what kind it is. */
4061
4062 if (ar->type == AR_UNKNOWN)
4063 {
4064 ar->type = AR_ELEMENT;
4065 for (i = 0; i < ar->dimen; i++)
4066 if (ar->dimen_type[i] == DIMEN_RANGE
4067 || ar->dimen_type[i] == DIMEN_VECTOR)
4068 {
4069 ar->type = AR_SECTION;
4070 break;
4071 }
4072 }
4073
83d890b9 4074 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
6de9cd9a
DN
4075 return FAILURE;
4076
4077 return SUCCESS;
4078}
4079
4080
17b1d2a0 4081static gfc_try
edf1eac2 4082resolve_substring (gfc_ref *ref)
6de9cd9a 4083{
b0c06816
FXC
4084 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4085
6de9cd9a
DN
4086 if (ref->u.ss.start != NULL)
4087 {
4088 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4089 return FAILURE;
4090
4091 if (ref->u.ss.start->ts.type != BT_INTEGER)
4092 {
4093 gfc_error ("Substring start index at %L must be of type INTEGER",
4094 &ref->u.ss.start->where);
4095 return FAILURE;
4096 }
4097
4098 if (ref->u.ss.start->rank != 0)
4099 {
4100 gfc_error ("Substring start index at %L must be scalar",
4101 &ref->u.ss.start->where);
4102 return FAILURE;
4103 }
4104
97bca513
FXC
4105 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4106 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4107 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a
DN
4108 {
4109 gfc_error ("Substring start index at %L is less than one",
4110 &ref->u.ss.start->where);
4111 return FAILURE;
4112 }
4113 }
4114
4115 if (ref->u.ss.end != NULL)
4116 {
4117 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4118 return FAILURE;
4119
4120 if (ref->u.ss.end->ts.type != BT_INTEGER)
4121 {
4122 gfc_error ("Substring end index at %L must be of type INTEGER",
4123 &ref->u.ss.end->where);
4124 return FAILURE;
4125 }
4126
4127 if (ref->u.ss.end->rank != 0)
4128 {
4129 gfc_error ("Substring end index at %L must be scalar",
4130 &ref->u.ss.end->where);
4131 return FAILURE;
4132 }
4133
4134 if (ref->u.ss.length != NULL
97bca513
FXC
4135 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4136 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4137 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a 4138 {
97bca513 4139 gfc_error ("Substring end index at %L exceeds the string length",
6de9cd9a
DN
4140 &ref->u.ss.start->where);
4141 return FAILURE;
4142 }
b0c06816
FXC
4143
4144 if (compare_bound_mpz_t (ref->u.ss.end,
4145 gfc_integer_kinds[k].huge) == CMP_GT
4146 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4147 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4148 {
4149 gfc_error ("Substring end index at %L is too large",
4150 &ref->u.ss.end->where);
4151 return FAILURE;
4152 }
6de9cd9a
DN
4153 }
4154
4155 return SUCCESS;
4156}
4157
4158
07368af0
PT
4159/* This function supplies missing substring charlens. */
4160
4161void
4162gfc_resolve_substring_charlen (gfc_expr *e)
4163{
4164 gfc_ref *char_ref;
4165 gfc_expr *start, *end;
4166
4167 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4168 if (char_ref->type == REF_SUBSTRING)
4169 break;
4170
4171 if (!char_ref)
4172 return;
4173
4174 gcc_assert (char_ref->next == NULL);
4175
bc21d315 4176 if (e->ts.u.cl)
07368af0 4177 {
bc21d315
JW
4178 if (e->ts.u.cl->length)
4179 gfc_free_expr (e->ts.u.cl->length);
07368af0
PT
4180 else if (e->expr_type == EXPR_VARIABLE
4181 && e->symtree->n.sym->attr.dummy)
4182 return;
4183 }
4184
4185 e->ts.type = BT_CHARACTER;
4186 e->ts.kind = gfc_default_character_kind;
4187
bc21d315 4188 if (!e->ts.u.cl)
b76e28c6 4189 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4190
4191 if (char_ref->u.ss.start)
4192 start = gfc_copy_expr (char_ref->u.ss.start);
4193 else
4194 start = gfc_int_expr (1);
4195
4196 if (char_ref->u.ss.end)
4197 end = gfc_copy_expr (char_ref->u.ss.end);
4198 else if (e->expr_type == EXPR_VARIABLE)
bc21d315 4199 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
07368af0
PT
4200 else
4201 end = NULL;
4202
4203 if (!start || !end)
4204 return;
4205
4206 /* Length = (end - start +1). */
bc21d315
JW
4207 e->ts.u.cl->length = gfc_subtract (end, start);
4208 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
07368af0 4209
bc21d315
JW
4210 e->ts.u.cl->length->ts.type = BT_INTEGER;
4211 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
07368af0
PT
4212
4213 /* Make sure that the length is simplified. */
bc21d315
JW
4214 gfc_simplify_expr (e->ts.u.cl->length, 1);
4215 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4216}
4217
4218
6de9cd9a
DN
4219/* Resolve subtype references. */
4220
17b1d2a0 4221static gfc_try
edf1eac2 4222resolve_ref (gfc_expr *expr)
6de9cd9a
DN
4223{
4224 int current_part_dimension, n_components, seen_part_dimension;
4225 gfc_ref *ref;
4226
4227 for (ref = expr->ref; ref; ref = ref->next)
4228 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4229 {
4230 find_array_spec (expr);
4231 break;
4232 }
4233
4234 for (ref = expr->ref; ref; ref = ref->next)
4235 switch (ref->type)
4236 {
4237 case REF_ARRAY:
4238 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4239 return FAILURE;
4240 break;
4241
4242 case REF_COMPONENT:
4243 break;
4244
4245 case REF_SUBSTRING:
4246 resolve_substring (ref);
4247 break;
4248 }
4249
4250 /* Check constraints on part references. */
4251
4252 current_part_dimension = 0;
4253 seen_part_dimension = 0;
4254 n_components = 0;
4255
4256 for (ref = expr->ref; ref; ref = ref->next)
4257 {
4258 switch (ref->type)
4259 {
4260 case REF_ARRAY:
4261 switch (ref->u.ar.type)
4262 {
4263 case AR_FULL:
4264 case AR_SECTION:
4265 current_part_dimension = 1;
4266 break;
4267
4268 case AR_ELEMENT:
4269 current_part_dimension = 0;
4270 break;
4271
4272 case AR_UNKNOWN:
4273 gfc_internal_error ("resolve_ref(): Bad array reference");
4274 }
4275
4276 break;
4277
4278 case REF_COMPONENT:
51f824b6 4279 if (current_part_dimension || seen_part_dimension)
6de9cd9a 4280 {
ef2bbc8c
JW
4281 /* F03:C614. */
4282 if (ref->u.c.component->attr.pointer
4283 || ref->u.c.component->attr.proc_pointer)
edf1eac2
SK
4284 {
4285 gfc_error ("Component to the right of a part reference "
4286 "with nonzero rank must not have the POINTER "
4287 "attribute at %L", &expr->where);
51f824b6
EE
4288 return FAILURE;
4289 }
d4b7d0f0 4290 else if (ref->u.c.component->attr.allocatable)
edf1eac2
SK
4291 {
4292 gfc_error ("Component to the right of a part reference "
4293 "with nonzero rank must not have the ALLOCATABLE "
4294 "attribute at %L", &expr->where);
51f824b6
EE
4295 return FAILURE;
4296 }
6de9cd9a
DN
4297 }
4298
4299 n_components++;
4300 break;
4301
4302 case REF_SUBSTRING:
4303 break;
4304 }
4305
4306 if (((ref->type == REF_COMPONENT && n_components > 1)
4307 || ref->next == NULL)
edf1eac2 4308 && current_part_dimension
6de9cd9a
DN
4309 && seen_part_dimension)
4310 {
6de9cd9a
DN
4311 gfc_error ("Two or more part references with nonzero rank must "
4312 "not be specified at %L", &expr->where);
4313 return FAILURE;
4314 }
4315
4316 if (ref->type == REF_COMPONENT)
4317 {
4318 if (current_part_dimension)
4319 seen_part_dimension = 1;
4320
edf1eac2 4321 /* reset to make sure */
6de9cd9a
DN
4322 current_part_dimension = 0;
4323 }
4324 }
4325
4326 return SUCCESS;
4327}
4328
4329
4330/* Given an expression, determine its shape. This is easier than it sounds.
f7b529fa 4331 Leaves the shape array NULL if it is not possible to determine the shape. */
6de9cd9a
DN
4332
4333static void
edf1eac2 4334expression_shape (gfc_expr *e)
6de9cd9a
DN
4335{
4336 mpz_t array[GFC_MAX_DIMENSIONS];
4337 int i;
4338
4339 if (e->rank == 0 || e->shape != NULL)
4340 return;
4341
4342 for (i = 0; i < e->rank; i++)
4343 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4344 goto fail;
4345
4346 e->shape = gfc_get_shape (e->rank);
4347
4348 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4349
4350 return;
4351
4352fail:
4353 for (i--; i >= 0; i--)
4354 mpz_clear (array[i]);
4355}
4356
4357
4358/* Given a variable expression node, compute the rank of the expression by
4359 examining the base symbol and any reference structures it may have. */
4360
4361static void
edf1eac2 4362expression_rank (gfc_expr *e)
6de9cd9a
DN
4363{
4364 gfc_ref *ref;
4365 int i, rank;
4366
00ca6640
DK
4367 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4368 could lead to serious confusion... */
4369 gcc_assert (e->expr_type != EXPR_COMPCALL);
4370
6de9cd9a
DN
4371 if (e->ref == NULL)
4372 {
4373 if (e->expr_type == EXPR_ARRAY)
4374 goto done;
f7b529fa 4375 /* Constructors can have a rank different from one via RESHAPE(). */
6de9cd9a
DN
4376
4377 if (e->symtree == NULL)
4378 {
4379 e->rank = 0;
4380 goto done;
4381 }
4382
4383 e->rank = (e->symtree->n.sym->as == NULL)
edf1eac2 4384 ? 0 : e->symtree->n.sym->as->rank;
6de9cd9a
DN
4385 goto done;
4386 }
4387
4388 rank = 0;
4389
4390 for (ref = e->ref; ref; ref = ref->next)
4391 {
4392 if (ref->type != REF_ARRAY)
4393 continue;
4394
4395 if (ref->u.ar.type == AR_FULL)
4396 {
4397 rank = ref->u.ar.as->rank;
4398 break;
4399 }
4400
4401 if (ref->u.ar.type == AR_SECTION)
4402 {
edf1eac2 4403 /* Figure out the rank of the section. */
6de9cd9a
DN
4404 if (rank != 0)
4405 gfc_internal_error ("expression_rank(): Two array specs");
4406
4407 for (i = 0; i < ref->u.ar.dimen; i++)
4408 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4409 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4410 rank++;
4411
4412 break;
4413 }
4414 }
4415
4416 e->rank = rank;
4417
4418done:
4419 expression_shape (e);
4420}
4421
4422
4423/* Resolve a variable expression. */
4424
17b1d2a0 4425static gfc_try
edf1eac2 4426resolve_variable (gfc_expr *e)
6de9cd9a
DN
4427{
4428 gfc_symbol *sym;
17b1d2a0 4429 gfc_try t;
0e9a445b
PT
4430
4431 t = SUCCESS;
6de9cd9a 4432
3e978d30 4433 if (e->symtree == NULL)
6de9cd9a
DN
4434 return FAILURE;
4435
3e978d30 4436 if (e->ref && resolve_ref (e) == FAILURE)
009e94d4
FXC
4437 return FAILURE;
4438
6de9cd9a 4439 sym = e->symtree->n.sym;
3070bab4
JW
4440 if (sym->attr.flavor == FL_PROCEDURE
4441 && (!sym->attr.function
4442 || (sym->attr.function && sym->result
4443 && sym->result->attr.proc_pointer
4444 && !sym->result->attr.function)))
6de9cd9a
DN
4445 {
4446 e->ts.type = BT_PROCEDURE;
a03826d1 4447 goto resolve_procedure;
6de9cd9a
DN
4448 }
4449
4450 if (sym->ts.type != BT_UNKNOWN)
4451 gfc_variable_attr (e, &e->ts);
4452 else
4453 {
4454 /* Must be a simple variable reference. */
9d691ba7 4455 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
6de9cd9a
DN
4456 return FAILURE;
4457 e->ts = sym->ts;
4458 }
4459
48474141
PT
4460 if (check_assumed_size_reference (sym, e))
4461 return FAILURE;
4462
0e9a445b
PT
4463 /* Deal with forward references to entries during resolve_code, to
4464 satisfy, at least partially, 12.5.2.5. */
4465 if (gfc_current_ns->entries
edf1eac2
SK
4466 && current_entry_id == sym->entry_id
4467 && cs_base
4468 && cs_base->current
4469 && cs_base->current->op != EXEC_ENTRY)
0e9a445b
PT
4470 {
4471 gfc_entry_list *entry;
4472 gfc_formal_arglist *formal;
4473 int n;
4474 bool seen;
4475
4476 /* If the symbol is a dummy... */
70365b5c 4477 if (sym->attr.dummy && sym->ns == gfc_current_ns)
0e9a445b
PT
4478 {
4479 entry = gfc_current_ns->entries;
4480 seen = false;
4481
4482 /* ...test if the symbol is a parameter of previous entries. */
4483 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4484 for (formal = entry->sym->formal; formal; formal = formal->next)
4485 {
4486 if (formal->sym && sym->name == formal->sym->name)
4487 seen = true;
4488 }
4489
4490 /* If it has not been seen as a dummy, this is an error. */
4491 if (!seen)
4492 {
4493 if (specification_expr)
70365b5c
TB
4494 gfc_error ("Variable '%s', used in a specification expression"
4495 ", is referenced at %L before the ENTRY statement "
0e9a445b
PT
4496 "in which it is a parameter",
4497 sym->name, &cs_base->current->loc);
4498 else
4499 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4500 "statement in which it is a parameter",
4501 sym->name, &cs_base->current->loc);
4502 t = FAILURE;
4503 }
4504 }
4505
4506 /* Now do the same check on the specification expressions. */
4507 specification_expr = 1;
4508 if (sym->ts.type == BT_CHARACTER
bc21d315 4509 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
0e9a445b
PT
4510 t = FAILURE;
4511
4512 if (sym->as)
4513 for (n = 0; n < sym->as->rank; n++)
4514 {
4515 specification_expr = 1;
4516 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4517 t = FAILURE;
4518 specification_expr = 1;
4519 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4520 t = FAILURE;
4521 }
4522 specification_expr = 0;
4523
4524 if (t == SUCCESS)
4525 /* Update the symbol's entry level. */
4526 sym->entry_id = current_entry_id + 1;
4527 }
4528
a03826d1
DK
4529resolve_procedure:
4530 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4531 t = FAILURE;
4532
0e9a445b 4533 return t;
6de9cd9a
DN
4534}
4535
4536
eb77cddf
PT
4537/* Checks to see that the correct symbol has been host associated.
4538 The only situation where this arises is that in which a twice
4539 contained function is parsed after the host association is made.
5b3b1d09
PT
4540 Therefore, on detecting this, change the symbol in the expression
4541 and convert the array reference into an actual arglist if the old
4542 symbol is a variable. */
eb77cddf
PT
4543static bool
4544check_host_association (gfc_expr *e)
4545{
4546 gfc_symbol *sym, *old_sym;
5b3b1d09 4547 gfc_symtree *st;
eb77cddf 4548 int n;
5b3b1d09 4549 gfc_ref *ref;
e4bf01a4 4550 gfc_actual_arglist *arg, *tail = NULL;
8de10a62 4551 bool retval = e->expr_type == EXPR_FUNCTION;
eb77cddf 4552
a1ab6660
PT
4553 /* If the expression is the result of substitution in
4554 interface.c(gfc_extend_expr) because there is no way in
4555 which the host association can be wrong. */
4556 if (e->symtree == NULL
4557 || e->symtree->n.sym == NULL
4558 || e->user_operator)
8de10a62 4559 return retval;
eb77cddf
PT
4560
4561 old_sym = e->symtree->n.sym;
8de10a62 4562
eb77cddf 4563 if (gfc_current_ns->parent
eb77cddf
PT
4564 && old_sym->ns != gfc_current_ns)
4565 {
5b3b1d09
PT
4566 /* Use the 'USE' name so that renamed module symbols are
4567 correctly handled. */
9be3684b 4568 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5b3b1d09 4569
a944c79a 4570 if (sym && old_sym != sym
67cec813 4571 && sym->ts.type == old_sym->ts.type
a944c79a
PT
4572 && sym->attr.flavor == FL_PROCEDURE
4573 && sym->attr.contained)
eb77cddf 4574 {
5b3b1d09 4575 /* Clear the shape, since it might not be valid. */
eb77cddf
PT
4576 if (e->shape != NULL)
4577 {
4578 for (n = 0; n < e->rank; n++)
4579 mpz_clear (e->shape[n]);
4580
4581 gfc_free (e->shape);
4582 }
4583
1aafbf99
PT
4584 /* Give the expression the right symtree! */
4585 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4586 gcc_assert (st != NULL);
eb77cddf 4587
1aafbf99
PT
4588 if (old_sym->attr.flavor == FL_PROCEDURE
4589 || e->expr_type == EXPR_FUNCTION)
4590 {
5b3b1d09
PT
4591 /* Original was function so point to the new symbol, since
4592 the actual argument list is already attached to the
4593 expression. */
4594 e->value.function.esym = NULL;
4595 e->symtree = st;
4596 }
4597 else
4598 {
4599 /* Original was variable so convert array references into
4600 an actual arglist. This does not need any checking now
4601 since gfc_resolve_function will take care of it. */
4602 e->value.function.actual = NULL;
4603 e->expr_type = EXPR_FUNCTION;
4604 e->symtree = st;
eb77cddf 4605
5b3b1d09
PT
4606 /* Ambiguity will not arise if the array reference is not
4607 the last reference. */
4608 for (ref = e->ref; ref; ref = ref->next)
4609 if (ref->type == REF_ARRAY && ref->next == NULL)
4610 break;
4611
4612 gcc_assert (ref->type == REF_ARRAY);
4613
4614 /* Grab the start expressions from the array ref and
4615 copy them into actual arguments. */
4616 for (n = 0; n < ref->u.ar.dimen; n++)
4617 {
4618 arg = gfc_get_actual_arglist ();
4619 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4620 if (e->value.function.actual == NULL)
4621 tail = e->value.function.actual = arg;
4622 else
4623 {
4624 tail->next = arg;
4625 tail = arg;
4626 }
4627 }
eb77cddf 4628
5b3b1d09
PT
4629 /* Dump the reference list and set the rank. */
4630 gfc_free_ref_list (e->ref);
4631 e->ref = NULL;
4632 e->rank = sym->as ? sym->as->rank : 0;
4633 }
4634
4635 gfc_resolve_expr (e);
4636 sym->refs++;
eb77cddf
PT
4637 }
4638 }
8de10a62 4639 /* This might have changed! */
eb77cddf
PT
4640 return e->expr_type == EXPR_FUNCTION;
4641}
4642
4643
07368af0
PT
4644static void
4645gfc_resolve_character_operator (gfc_expr *e)
4646{
4647 gfc_expr *op1 = e->value.op.op1;
4648 gfc_expr *op2 = e->value.op.op2;
4649 gfc_expr *e1 = NULL;
4650 gfc_expr *e2 = NULL;
4651
a1ee985f 4652 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
07368af0 4653
bc21d315
JW
4654 if (op1->ts.u.cl && op1->ts.u.cl->length)
4655 e1 = gfc_copy_expr (op1->ts.u.cl->length);
07368af0
PT
4656 else if (op1->expr_type == EXPR_CONSTANT)
4657 e1 = gfc_int_expr (op1->value.character.length);
4658
bc21d315
JW
4659 if (op2->ts.u.cl && op2->ts.u.cl->length)
4660 e2 = gfc_copy_expr (op2->ts.u.cl->length);
07368af0
PT
4661 else if (op2->expr_type == EXPR_CONSTANT)
4662 e2 = gfc_int_expr (op2->value.character.length);
4663
b76e28c6 4664 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4665
4666 if (!e1 || !e2)
4667 return;
4668
bc21d315
JW
4669 e->ts.u.cl->length = gfc_add (e1, e2);
4670 e->ts.u.cl->length->ts.type = BT_INTEGER;
4671 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4672 gfc_simplify_expr (e->ts.u.cl->length, 0);
4673 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4674
4675 return;
4676}
4677
4678
4679/* Ensure that an character expression has a charlen and, if possible, a
4680 length expression. */
4681
4682static void
4683fixup_charlen (gfc_expr *e)
4684{
4685 /* The cases fall through so that changes in expression type and the need
4686 for multiple fixes are picked up. In all circumstances, a charlen should
4687 be available for the middle end to hang a backend_decl on. */
4688 switch (e->expr_type)
4689 {
4690 case EXPR_OP:
4691 gfc_resolve_character_operator (e);
4692
4693 case EXPR_ARRAY:
4694 if (e->expr_type == EXPR_ARRAY)
4695 gfc_resolve_character_array_constructor (e);
4696
4697 case EXPR_SUBSTRING:
bc21d315 4698 if (!e->ts.u.cl && e->ref)
07368af0
PT
4699 gfc_resolve_substring_charlen (e);
4700
4701 default:
bc21d315 4702 if (!e->ts.u.cl)
b76e28c6 4703 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4704
4705 break;
4706 }
4707}
4708
4709
8e1f752a
DK
4710/* Update an actual argument to include the passed-object for type-bound
4711 procedures at the right position. */
4712
4713static gfc_actual_arglist*
90661f26
JW
4714update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4715 const char *name)
8e1f752a 4716{
b82657f4
DK
4717 gcc_assert (argpos > 0);
4718
8e1f752a
DK
4719 if (argpos == 1)
4720 {
4721 gfc_actual_arglist* result;
4722
4723 result = gfc_get_actual_arglist ();
4724 result->expr = po;
4725 result->next = lst;
90661f26
JW
4726 if (name)
4727 result->name = name;
8e1f752a
DK
4728
4729 return result;
4730 }
4731
90661f26
JW
4732 if (lst)
4733 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4734 else
4735 lst = update_arglist_pass (NULL, po, argpos - 1, name);
8e1f752a
DK
4736 return lst;
4737}
4738
4739
e157f736 4740/* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
8e1f752a 4741
e157f736
DK
4742static gfc_expr*
4743extract_compcall_passed_object (gfc_expr* e)
8e1f752a
DK
4744{
4745 gfc_expr* po;
8e1f752a 4746
e157f736 4747 gcc_assert (e->expr_type == EXPR_COMPCALL);
8e1f752a 4748
4a44a72d
DK
4749 if (e->value.compcall.base_object)
4750 po = gfc_copy_expr (e->value.compcall.base_object);
4751 else
4752 {
4753 po = gfc_get_expr ();
4754 po->expr_type = EXPR_VARIABLE;
4755 po->symtree = e->symtree;
4756 po->ref = gfc_copy_ref (e->ref);
4757 }
8e1f752a
DK
4758
4759 if (gfc_resolve_expr (po) == FAILURE)
e157f736
DK
4760 return NULL;
4761
4762 return po;
4763}
4764
4765
4766/* Update the arglist of an EXPR_COMPCALL expression to include the
4767 passed-object. */
4768
4769static gfc_try
4770update_compcall_arglist (gfc_expr* e)
4771{
4772 gfc_expr* po;
4773 gfc_typebound_proc* tbp;
4774
4775 tbp = e->value.compcall.tbp;
4776
b82657f4
DK
4777 if (tbp->error)
4778 return FAILURE;
4779
e157f736
DK
4780 po = extract_compcall_passed_object (e);
4781 if (!po)
8e1f752a 4782 return FAILURE;
e157f736 4783
8e1f752a
DK
4784 if (po->rank > 0)
4785 {
4786 gfc_error ("Passed-object at %L must be scalar", &e->where);
4787 return FAILURE;
4788 }
4789
4a44a72d 4790 if (tbp->nopass || e->value.compcall.ignore_pass)
8e1f752a
DK
4791 {
4792 gfc_free_expr (po);
4793 return SUCCESS;
4794 }
4795
4796 gcc_assert (tbp->pass_arg_num > 0);
4797 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
90661f26
JW
4798 tbp->pass_arg_num,
4799 tbp->pass_arg);
4800
4801 return SUCCESS;
4802}
4803
4804
4805/* Extract the passed object from a PPC call (a copy of it). */
4806
4807static gfc_expr*
4808extract_ppc_passed_object (gfc_expr *e)
4809{
4810 gfc_expr *po;
4811 gfc_ref **ref;
4812
4813 po = gfc_get_expr ();
4814 po->expr_type = EXPR_VARIABLE;
4815 po->symtree = e->symtree;
4816 po->ref = gfc_copy_ref (e->ref);
4817
4818 /* Remove PPC reference. */
4819 ref = &po->ref;
4820 while ((*ref)->next)
4821 (*ref) = (*ref)->next;
4822 gfc_free_ref_list (*ref);
4823 *ref = NULL;
4824
4825 if (gfc_resolve_expr (po) == FAILURE)
4826 return NULL;
4827
4828 return po;
4829}
4830
4831
4832/* Update the actual arglist of a procedure pointer component to include the
4833 passed-object. */
4834
4835static gfc_try
4836update_ppc_arglist (gfc_expr* e)
4837{
4838 gfc_expr* po;
4839 gfc_component *ppc;
4840 gfc_typebound_proc* tb;
4841
4842 if (!gfc_is_proc_ptr_comp (e, &ppc))
4843 return FAILURE;
4844
4845 tb = ppc->tb;
4846
4847 if (tb->error)
4848 return FAILURE;
4849 else if (tb->nopass)
4850 return SUCCESS;
4851
4852 po = extract_ppc_passed_object (e);
4853 if (!po)
4854 return FAILURE;
4855
4856 if (po->rank > 0)
4857 {
4858 gfc_error ("Passed-object at %L must be scalar", &e->where);
4859 return FAILURE;
4860 }
4861
4862 gcc_assert (tb->pass_arg_num > 0);
4863 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4864 tb->pass_arg_num,
4865 tb->pass_arg);
8e1f752a
DK
4866
4867 return SUCCESS;
4868}
4869
4870
b0e5fa94
DK
4871/* Check that the object a TBP is called on is valid, i.e. it must not be
4872 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4873
4874static gfc_try
4875check_typebound_baseobject (gfc_expr* e)
4876{
4877 gfc_expr* base;
4878
4879 base = extract_compcall_passed_object (e);
4880 if (!base)
4881 return FAILURE;
4882
cf2b3c22 4883 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
e56817db
TB
4884
4885 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
b0e5fa94
DK
4886 {
4887 gfc_error ("Base object for type-bound procedure call at %L is of"
bc21d315 4888 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
b0e5fa94
DK
4889 return FAILURE;
4890 }
4891
4892 return SUCCESS;
4893}
4894
4895
8e1f752a
DK
4896/* Resolve a call to a type-bound procedure, either function or subroutine,
4897 statically from the data in an EXPR_COMPCALL expression. The adapted
4898 arglist and the target-procedure symtree are returned. */
4899
4900static gfc_try
4901resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4902 gfc_actual_arglist** actual)
4903{
4904 gcc_assert (e->expr_type == EXPR_COMPCALL);
e157f736 4905 gcc_assert (!e->value.compcall.tbp->is_generic);
8e1f752a
DK
4906
4907 /* Update the actual arglist for PASS. */
4908 if (update_compcall_arglist (e) == FAILURE)
4909 return FAILURE;
4910
4911 *actual = e->value.compcall.actual;
e157f736 4912 *target = e->value.compcall.tbp->u.specific;
8e1f752a
DK
4913
4914 gfc_free_ref_list (e->ref);
4915 e->ref = NULL;
4916 e->value.compcall.actual = NULL;
4917
4918 return SUCCESS;
4919}
4920
4921
e157f736
DK
4922/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4923 which of the specific bindings (if any) matches the arglist and transform
4924 the expression into a call of that binding. */
4925
4926static gfc_try
4927resolve_typebound_generic_call (gfc_expr* e)
4928{
4929 gfc_typebound_proc* genproc;
4930 const char* genname;
4931
4932 gcc_assert (e->expr_type == EXPR_COMPCALL);
4933 genname = e->value.compcall.name;
4934 genproc = e->value.compcall.tbp;
4935
4936 if (!genproc->is_generic)
4937 return SUCCESS;
4938
4939 /* Try the bindings on this type and in the inheritance hierarchy. */
4940 for (; genproc; genproc = genproc->overridden)
4941 {
4942 gfc_tbp_generic* g;
4943
4944 gcc_assert (genproc->is_generic);
4945 for (g = genproc->u.generic; g; g = g->next)
4946 {
4947 gfc_symbol* target;
4948 gfc_actual_arglist* args;
4949 bool matches;
4950
4951 gcc_assert (g->specific);
b82657f4
DK
4952
4953 if (g->specific->error)
4954 continue;
4955
e157f736
DK
4956 target = g->specific->u.specific->n.sym;
4957
4958 /* Get the right arglist by handling PASS/NOPASS. */
4959 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4960 if (!g->specific->nopass)
4961 {
4962 gfc_expr* po;
4963 po = extract_compcall_passed_object (e);
4964 if (!po)
4965 return FAILURE;
4966
b82657f4
DK
4967 gcc_assert (g->specific->pass_arg_num > 0);
4968 gcc_assert (!g->specific->error);
90661f26
JW
4969 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
4970 g->specific->pass_arg);
e157f736 4971 }
f0ac18b7
DK
4972 resolve_actual_arglist (args, target->attr.proc,
4973 is_external_proc (target) && !target->formal);
e157f736
DK
4974
4975 /* Check if this arglist matches the formal. */
f0ac18b7 4976 matches = gfc_arglist_matches_symbol (&args, target);
e157f736
DK
4977
4978 /* Clean up and break out of the loop if we've found it. */
4979 gfc_free_actual_arglist (args);
4980 if (matches)
4981 {
4982 e->value.compcall.tbp = g->specific;
4983 goto success;
4984 }
4985 }
4986 }
4987
4988 /* Nothing matching found! */
4989 gfc_error ("Found no matching specific binding for the call to the GENERIC"
4990 " '%s' at %L", genname, &e->where);
4991 return FAILURE;
4992
4993success:
4994 return SUCCESS;
4995}
4996
4997
8e1f752a
DK
4998/* Resolve a call to a type-bound subroutine. */
4999
5000static gfc_try
5001resolve_typebound_call (gfc_code* c)
5002{
5003 gfc_actual_arglist* newactual;
5004 gfc_symtree* target;
5005
e157f736 5006 /* Check that's really a SUBROUTINE. */
a513927a 5007 if (!c->expr1->value.compcall.tbp->subroutine)
e157f736
DK
5008 {
5009 gfc_error ("'%s' at %L should be a SUBROUTINE",
a513927a 5010 c->expr1->value.compcall.name, &c->loc);
e157f736
DK
5011 return FAILURE;
5012 }
5013
a513927a 5014 if (check_typebound_baseobject (c->expr1) == FAILURE)
b0e5fa94
DK
5015 return FAILURE;
5016
a513927a 5017 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
e157f736
DK
5018 return FAILURE;
5019
8e1f752a
DK
5020 /* Transform into an ordinary EXEC_CALL for now. */
5021
a513927a 5022 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
8e1f752a
DK
5023 return FAILURE;
5024
5025 c->ext.actual = newactual;
5026 c->symtree = target;
4a44a72d 5027 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
8e1f752a 5028
a513927a 5029 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7cf078dc 5030
a513927a 5031 gfc_free_expr (c->expr1);
7cf078dc
PT
5032 c->expr1 = gfc_get_expr ();
5033 c->expr1->expr_type = EXPR_FUNCTION;
5034 c->expr1->symtree = target;
5035 c->expr1->where = c->loc;
8e1f752a
DK
5036
5037 return resolve_call (c);
5038}
5039
5040
7cf078dc
PT
5041/* Resolve a component-call expression. This originally was intended
5042 only to see functions. However, it is convenient to use it in
5043 resolving subroutine class methods, since we do not have to add a
5044 gfc_code each time. */
8e1f752a 5045static gfc_try
7cf078dc 5046resolve_compcall (gfc_expr* e, bool fcn)
8e1f752a
DK
5047{
5048 gfc_actual_arglist* newactual;
5049 gfc_symtree* target;
5050
e157f736 5051 /* Check that's really a FUNCTION. */
7cf078dc 5052 if (fcn && !e->value.compcall.tbp->function)
e157f736
DK
5053 {
5054 gfc_error ("'%s' at %L should be a FUNCTION",
5055 e->value.compcall.name, &e->where);
5056 return FAILURE;
5057 }
7cf078dc
PT
5058 else if (!fcn && !e->value.compcall.tbp->subroutine)
5059 {
5060 /* To resolve class member calls, we borrow this bit
5061 of code to select the specific procedures. */
5062 gfc_error ("'%s' at %L should be a SUBROUTINE",
5063 e->value.compcall.name, &e->where);
5064 return FAILURE;
5065 }
e157f736 5066
4a44a72d
DK
5067 /* These must not be assign-calls! */
5068 gcc_assert (!e->value.compcall.assign);
5069
b0e5fa94
DK
5070 if (check_typebound_baseobject (e) == FAILURE)
5071 return FAILURE;
5072
e157f736
DK
5073 if (resolve_typebound_generic_call (e) == FAILURE)
5074 return FAILURE;
00ca6640
DK
5075 gcc_assert (!e->value.compcall.tbp->is_generic);
5076
5077 /* Take the rank from the function's symbol. */
5078 if (e->value.compcall.tbp->u.specific->n.sym->as)
5079 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
e157f736
DK
5080
5081 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
8e1f752a
DK
5082 arglist to the TBP's binding target. */
5083
5084 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5085 return FAILURE;
5086
5087 e->value.function.actual = newactual;
e157f736 5088 e->value.function.name = e->value.compcall.name;
37a40b53 5089 e->value.function.esym = target->n.sym;
7cf078dc 5090 e->value.function.class_esym = NULL;
e157f736 5091 e->value.function.isym = NULL;
8e1f752a 5092 e->symtree = target;
f0ac18b7 5093 e->ts = target->n.sym->ts;
8e1f752a
DK
5094 e->expr_type = EXPR_FUNCTION;
5095
7cf078dc
PT
5096 /* Resolution is not necessary if this is a class subroutine; this
5097 function only has to identify the specific proc. Resolution of
5098 the call will be done next in resolve_typebound_call. */
5099 return fcn ? gfc_resolve_expr (e) : SUCCESS;
5100}
5101
5102
5103/* Resolve a typebound call for the members in a class. This group of
5104 functions implements dynamic dispatch in the provisional version
5105 of f03 OOP. As soon as vtables are in place and contain pointers
5106 to methods, this will no longer be necessary. */
5107static gfc_expr *list_e;
5108static void check_class_members (gfc_symbol *);
5109static gfc_try class_try;
5110static bool fcn_flag;
5111static gfc_symbol *class_object;
5112
5113
5114static void
5115check_members (gfc_symbol *derived)
5116{
5117 if (derived->attr.flavor == FL_DERIVED)
5118 check_class_members (derived);
5119}
5120
5121
5122static void
5123check_class_members (gfc_symbol *derived)
5124{
5125 gfc_symbol* tbp_sym;
5126 gfc_expr *e;
5127 gfc_symtree *tbp;
5128 gfc_class_esym_list *etmp;
5129
5130 e = gfc_copy_expr (list_e);
5131
5132 tbp = gfc_find_typebound_proc (derived, &class_try,
5133 e->value.compcall.name,
5134 false, &e->where);
5135
5136 if (tbp == NULL)
5137 {
5138 gfc_error ("no typebound available procedure named '%s' at %L",
5139 e->value.compcall.name, &e->where);
5140 return;
5141 }
5142
5143 if (tbp->n.tb->is_generic)
5144 {
5145 tbp_sym = NULL;
5146
5147 /* If we have to match a passed class member, force the actual
5148 expression to have the correct type. */
5149 if (!tbp->n.tb->nopass)
5150 {
5151 if (e->value.compcall.base_object == NULL)
5152 e->value.compcall.base_object =
5153 extract_compcall_passed_object (e);
5154
5155 e->value.compcall.base_object->ts.type = BT_DERIVED;
5156 e->value.compcall.base_object->ts.u.derived = derived;
5157 }
5158 }
5159 else
5160 tbp_sym = tbp->n.tb->u.specific->n.sym;
5161
5162 e->value.compcall.tbp = tbp->n.tb;
5163 e->value.compcall.name = tbp->name;
5164
28fccf2c
PT
5165 /* Let the original expresssion catch the assertion in
5166 resolve_compcall, since this flag does not appear to be reset or
5167 copied in some systems. */
5168 e->value.compcall.assign = 0;
5169
7cf078dc
PT
5170 /* Do the renaming, PASSing, generic => specific and other
5171 good things for each class member. */
5172 class_try = (resolve_compcall (e, fcn_flag) == SUCCESS)
5173 ? class_try : FAILURE;
5174
5175 /* Now transfer the found symbol to the esym list. */
5176 if (class_try == SUCCESS)
5177 {
5178 etmp = list_e->value.function.class_esym;
5179 list_e->value.function.class_esym
5180 = gfc_get_class_esym_list();
5181 list_e->value.function.class_esym->next = etmp;
5182 list_e->value.function.class_esym->derived = derived;
7cf078dc
PT
5183 list_e->value.function.class_esym->esym
5184 = e->value.function.esym;
5185 }
5186
5187 gfc_free_expr (e);
5188
5189 /* Burrow down into grandchildren types. */
5190 if (derived->f2k_derived)
5191 gfc_traverse_ns (derived->f2k_derived, check_members);
5192}
5193
5194
5195/* Eliminate esym_lists where all the members point to the
5196 typebound procedure of the declared type; ie. one where
5197 type selection has no effect.. */
5198static void
5199resolve_class_esym (gfc_expr *e)
5200{
5201 gfc_class_esym_list *p, *q;
5202 bool empty = true;
5203
5204 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5205
5206 p = e->value.function.class_esym;
5207 if (p == NULL)
5208 return;
5209
5210 for (; p; p = p->next)
5211 empty = empty && (e->value.function.esym == p->esym);
5212
5213 if (empty)
5214 {
5215 p = e->value.function.class_esym;
5216 for (; p; p = q)
5217 {
5218 q = p->next;
5219 gfc_free (p);
5220 }
5221 e->value.function.class_esym = NULL;
5222 }
5223}
5224
5225
28188747
PT
5226/* Generate an expression for the vindex, given the reference to
5227 the class of the final expression (class_ref), the base of the
5228 full reference list (new_ref), the declared type and the class
5229 object (st). */
5230static gfc_expr*
5231vindex_expr (gfc_ref *class_ref, gfc_ref *new_ref,
5232 gfc_symbol *declared, gfc_symtree *st)
5233{
5234 gfc_expr *vindex;
5235 gfc_ref *ref;
5236
5237 /* Build an expression for the correct vindex; ie. that of the last
5238 CLASS reference. */
5239 ref = gfc_get_ref();
5240 ref->type = REF_COMPONENT;
5241 ref->u.c.component = declared->components->next;
5242 ref->u.c.sym = declared;
5243 ref->next = NULL;
5244 if (class_ref)
5245 {
5246 class_ref->next = ref;
5247 }
5248 else
5249 {
5250 gfc_free_ref_list (new_ref);
5251 new_ref = ref;
5252 }
5253 vindex = gfc_get_expr ();
5254 vindex->expr_type = EXPR_VARIABLE;
5255 vindex->symtree = st;
5256 vindex->symtree->n.sym->refs++;
5257 vindex->ts = ref->u.c.component->ts;
5258 vindex->ref = new_ref;
5259
5260 return vindex;
5261}
5262
5263
5264/* Get the ultimate declared type from an expression. In addition,
5265 return the last class/derived type reference and the copy of the
5266 reference list. */
5267static gfc_symbol*
5268get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5269 gfc_expr *e)
5270{
5271 gfc_symbol *declared;
5272 gfc_ref *ref;
5273
5274 declared = NULL;
5275 *class_ref = NULL;
5276 *new_ref = gfc_copy_ref (e->ref);
5277 for (ref = *new_ref; ref; ref = ref->next)
5278 {
5279 if (ref->type != REF_COMPONENT)
5280 continue;
5281
5282 if (ref->u.c.component->ts.type == BT_CLASS
5283 || ref->u.c.component->ts.type == BT_DERIVED)
5284 {
5285 declared = ref->u.c.component->ts.u.derived;
5286 *class_ref = ref;
5287 }
5288 }
5289
5290 if (declared == NULL)
5291 declared = e->symtree->n.sym->ts.u.derived;
5292
5293 return declared;
5294}
5295
5296
f116b2fc
PT
5297/* Resolve the argument expressions so that any arguments expressions
5298 that include class methods are resolved before the current call.
5299 This is necessary because of the static variables used in CLASS
5300 method resolution. */
5301static void
5302resolve_arg_exprs (gfc_actual_arglist *arg)
5303{
5304 /* Resolve the actual arglist expressions. */
5305 for (; arg; arg = arg->next)
5306 {
5307 if (arg->expr)
5308 gfc_resolve_expr (arg->expr);
5309 }
5310}
5311
5312
7cf078dc
PT
5313/* Resolve a CLASS typebound function, or 'method'. */
5314static gfc_try
5315resolve_class_compcall (gfc_expr* e)
5316{
28188747
PT
5317 gfc_symbol *derived, *declared;
5318 gfc_ref *new_ref;
5319 gfc_ref *class_ref;
5320 gfc_symtree *st;
5321
5322 st = e->symtree;
5323 class_object = st->n.sym;
7cf078dc 5324
28188747
PT
5325 /* Get the CLASS declared type. */
5326 declared = get_declared_from_expr (&class_ref, &new_ref, e);
7cf078dc 5327
28188747
PT
5328 /* Weed out cases of the ultimate component being a derived type. */
5329 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5330 {
5331 gfc_free_ref_list (new_ref);
5332 return resolve_compcall (e, true);
f116b2fc
PT
5333 }
5334
5335 /* Resolve the argument expressions, */
5336 resolve_arg_exprs (e->value.function.actual);
7cf078dc
PT
5337
5338 /* Get the data component, which is of the declared type. */
28188747 5339 derived = declared->components->ts.u.derived;
7cf078dc
PT
5340
5341 /* Resolve the function call for each member of the class. */
5342 class_try = SUCCESS;
5343 fcn_flag = true;
5344 list_e = gfc_copy_expr (e);
5345 check_class_members (derived);
5346
5347 class_try = (resolve_compcall (e, true) == SUCCESS)
5348 ? class_try : FAILURE;
5349
5350 /* Transfer the class list to the original expression. Note that
5351 the class_esym list is cleaned up in trans-expr.c, as the calls
5352 are translated. */
5353 e->value.function.class_esym = list_e->value.function.class_esym;
5354 list_e->value.function.class_esym = NULL;
5355 gfc_free_expr (list_e);
5356
5357 resolve_class_esym (e);
5358
28188747
PT
5359 /* More than one typebound procedure so transmit an expression for
5360 the vindex as the selector. */
5361 if (e->value.function.class_esym != NULL)
5362 e->value.function.class_esym->vindex
5363 = vindex_expr (class_ref, new_ref, declared, st);
5364
7cf078dc
PT
5365 return class_try;
5366}
5367
5368/* Resolve a CLASS typebound subroutine, or 'method'. */
5369static gfc_try
5370resolve_class_typebound_call (gfc_code *code)
5371{
28188747
PT
5372 gfc_symbol *derived, *declared;
5373 gfc_ref *new_ref;
5374 gfc_ref *class_ref;
5375 gfc_symtree *st;
5376
5377 st = code->expr1->symtree;
5378 class_object = st->n.sym;
7cf078dc 5379
28188747
PT
5380 /* Get the CLASS declared type. */
5381 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
7cf078dc 5382
28188747
PT
5383 /* Weed out cases of the ultimate component being a derived type. */
5384 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5385 {
5386 gfc_free_ref_list (new_ref);
5387 return resolve_typebound_call (code);
5388 }
7cf078dc 5389
f116b2fc 5390 /* Resolve the argument expressions, */
aa9aed00 5391 resolve_arg_exprs (code->expr1->value.compcall.actual);
f116b2fc 5392
7cf078dc 5393 /* Get the data component, which is of the declared type. */
28188747 5394 derived = declared->components->ts.u.derived;
7cf078dc
PT
5395
5396 class_try = SUCCESS;
5397 fcn_flag = false;
5398 list_e = gfc_copy_expr (code->expr1);
5399 check_class_members (derived);
5400
5401 class_try = (resolve_typebound_call (code) == SUCCESS)
5402 ? class_try : FAILURE;
5403
5404 /* Transfer the class list to the original expression. Note that
5405 the class_esym list is cleaned up in trans-expr.c, as the calls
5406 are translated. */
5407 code->expr1->value.function.class_esym
5408 = list_e->value.function.class_esym;
5409 list_e->value.function.class_esym = NULL;
5410 gfc_free_expr (list_e);
5411
5412 resolve_class_esym (code->expr1);
5413
28188747
PT
5414 /* More than one typebound procedure so transmit an expression for
5415 the vindex as the selector. */
5416 if (code->expr1->value.function.class_esym != NULL)
5417 code->expr1->value.function.class_esym->vindex
5418 = vindex_expr (class_ref, new_ref, declared, st);
5419
7cf078dc 5420 return class_try;
8e1f752a
DK
5421}
5422
5423
713485cc
JW
5424/* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5425
5426static gfc_try
5427resolve_ppc_call (gfc_code* c)
5428{
5429 gfc_component *comp;
cf2b3c22
TB
5430 bool b;
5431
5432 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5433 gcc_assert (b);
713485cc 5434
a513927a
SK
5435 c->resolved_sym = c->expr1->symtree->n.sym;
5436 c->expr1->expr_type = EXPR_VARIABLE;
713485cc
JW
5437
5438 if (!comp->attr.subroutine)
a513927a 5439 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
713485cc 5440
e35bbb23
JW
5441 if (resolve_ref (c->expr1) == FAILURE)
5442 return FAILURE;
5443
90661f26
JW
5444 if (update_ppc_arglist (c->expr1) == FAILURE)
5445 return FAILURE;
5446
5447 c->ext.actual = c->expr1->value.compcall.actual;
5448
713485cc
JW
5449 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5450 comp->formal == NULL) == FAILURE)
5451 return FAILURE;
5452
7e196f89 5453 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
713485cc
JW
5454
5455 return SUCCESS;
5456}
5457
5458
5459/* Resolve a Function Call to a Procedure Pointer Component (Function). */
5460
5461static gfc_try
5462resolve_expr_ppc (gfc_expr* e)
5463{
5464 gfc_component *comp;
cf2b3c22
TB
5465 bool b;
5466
5467 b = gfc_is_proc_ptr_comp (e, &comp);
5468 gcc_assert (b);
713485cc
JW
5469
5470 /* Convert to EXPR_FUNCTION. */
5471 e->expr_type = EXPR_FUNCTION;
5472 e->value.function.isym = NULL;
5473 e->value.function.actual = e->value.compcall.actual;
5474 e->ts = comp->ts;
c74b74a8
JW
5475 if (comp->as != NULL)
5476 e->rank = comp->as->rank;
713485cc
JW
5477
5478 if (!comp->attr.function)
5479 gfc_add_function (&comp->attr, comp->name, &e->where);
5480
e35bbb23
JW
5481 if (resolve_ref (e) == FAILURE)
5482 return FAILURE;
5483
713485cc
JW
5484 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5485 comp->formal == NULL) == FAILURE)
5486 return FAILURE;
5487
90661f26
JW
5488 if (update_ppc_arglist (e) == FAILURE)
5489 return FAILURE;
5490
7e196f89 5491 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
713485cc
JW
5492
5493 return SUCCESS;
5494}
5495
5496
6de9cd9a
DN
5497/* Resolve an expression. That is, make sure that types of operands agree
5498 with their operators, intrinsic operators are converted to function calls
5499 for overloaded types and unresolved function references are resolved. */
5500
17b1d2a0 5501gfc_try
edf1eac2 5502gfc_resolve_expr (gfc_expr *e)
6de9cd9a 5503{
17b1d2a0 5504 gfc_try t;
6de9cd9a
DN
5505
5506 if (e == NULL)
5507 return SUCCESS;
5508
5509 switch (e->expr_type)
5510 {
5511 case EXPR_OP:
5512 t = resolve_operator (e);
5513 break;
5514
5515 case EXPR_FUNCTION:
6de9cd9a 5516 case EXPR_VARIABLE:
eb77cddf
PT
5517
5518 if (check_host_association (e))
5519 t = resolve_function (e);
5520 else
5521 {
5522 t = resolve_variable (e);
5523 if (t == SUCCESS)
5524 expression_rank (e);
5525 }
07368af0 5526
bc21d315 5527 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
9de88093 5528 && e->ref->type != REF_SUBSTRING)
07368af0
PT
5529 gfc_resolve_substring_charlen (e);
5530
6de9cd9a
DN
5531 break;
5532
8e1f752a 5533 case EXPR_COMPCALL:
7cf078dc
PT
5534 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
5535 t = resolve_class_compcall (e);
5536 else
5537 t = resolve_compcall (e, true);
8e1f752a
DK
5538 break;
5539
6de9cd9a
DN
5540 case EXPR_SUBSTRING:
5541 t = resolve_ref (e);
5542 break;
5543
5544 case EXPR_CONSTANT:
5545 case EXPR_NULL:
5546 t = SUCCESS;
5547 break;
5548
713485cc
JW
5549 case EXPR_PPC:
5550 t = resolve_expr_ppc (e);
5551 break;
5552
6de9cd9a
DN
5553 case EXPR_ARRAY:
5554 t = FAILURE;
5555 if (resolve_ref (e) == FAILURE)
5556 break;
5557
5558 t = gfc_resolve_array_constructor (e);
5559 /* Also try to expand a constructor. */
5560 if (t == SUCCESS)
5561 {
5562 expression_rank (e);
5563 gfc_expand_constructor (e);
5564 }
1855915a 5565
edf1eac2 5566 /* This provides the opportunity for the length of constructors with
86bf520d 5567 character valued function elements to propagate the string length
edf1eac2 5568 to the expression. */
88fec49f
DK
5569 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
5570 t = gfc_resolve_character_array_constructor (e);
6de9cd9a
DN
5571
5572 break;
5573
5574 case EXPR_STRUCTURE:
5575 t = resolve_ref (e);
5576 if (t == FAILURE)
5577 break;
5578
5579 t = resolve_structure_cons (e);
5580 if (t == FAILURE)
5581 break;
5582
5583 t = gfc_simplify_expr (e, 0);
5584 break;
5585
5586 default:
5587 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5588 }
5589
bc21d315 5590 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
07368af0
PT
5591 fixup_charlen (e);
5592
6de9cd9a
DN
5593 return t;
5594}
5595
5596
8d5cfa27
SK
5597/* Resolve an expression from an iterator. They must be scalar and have
5598 INTEGER or (optionally) REAL type. */
6de9cd9a 5599
17b1d2a0 5600static gfc_try
edf1eac2
SK
5601gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5602 const char *name_msgid)
6de9cd9a 5603{
8d5cfa27 5604 if (gfc_resolve_expr (expr) == FAILURE)
6de9cd9a
DN
5605 return FAILURE;
5606
8d5cfa27 5607 if (expr->rank != 0)
6de9cd9a 5608 {
31043f6c 5609 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6de9cd9a
DN
5610 return FAILURE;
5611 }
5612
79e7840d 5613 if (expr->ts.type != BT_INTEGER)
6de9cd9a 5614 {
79e7840d
JD
5615 if (expr->ts.type == BT_REAL)
5616 {
5617 if (real_ok)
5618 return gfc_notify_std (GFC_STD_F95_DEL,
5619 "Deleted feature: %s at %L must be integer",
5620 _(name_msgid), &expr->where);
5621 else
5622 {
5623 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5624 &expr->where);
5625 return FAILURE;
5626 }
5627 }
31043f6c 5628 else
79e7840d
JD
5629 {
5630 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5631 return FAILURE;
5632 }
6de9cd9a 5633 }
8d5cfa27
SK
5634 return SUCCESS;
5635}
5636
5637
5638/* Resolve the expressions in an iterator structure. If REAL_OK is
5639 false allow only INTEGER type iterators, otherwise allow REAL types. */
5640
17b1d2a0 5641gfc_try
edf1eac2 5642gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
8d5cfa27 5643{
8d5cfa27
SK
5644 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5645 == FAILURE)
6de9cd9a
DN
5646 return FAILURE;
5647
8d5cfa27 5648 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
6de9cd9a 5649 {
8d5cfa27
SK
5650 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5651 &iter->var->where);
6de9cd9a
DN
5652 return FAILURE;
5653 }
5654
8d5cfa27
SK
5655 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5656 "Start expression in DO loop") == FAILURE)
6de9cd9a
DN
5657 return FAILURE;
5658
8d5cfa27
SK
5659 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5660 "End expression in DO loop") == FAILURE)
5661 return FAILURE;
6de9cd9a 5662
8d5cfa27
SK
5663 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5664 "Step expression in DO loop") == FAILURE)
6de9cd9a
DN
5665 return FAILURE;
5666
8d5cfa27 5667 if (iter->step->expr_type == EXPR_CONSTANT)
6de9cd9a 5668 {
8d5cfa27
SK
5669 if ((iter->step->ts.type == BT_INTEGER
5670 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5671 || (iter->step->ts.type == BT_REAL
5672 && mpfr_sgn (iter->step->value.real) == 0))
5673 {
5674 gfc_error ("Step expression in DO loop at %L cannot be zero",
5675 &iter->step->where);
5676 return FAILURE;
5677 }
6de9cd9a
DN
5678 }
5679
8d5cfa27
SK
5680 /* Convert start, end, and step to the same type as var. */
5681 if (iter->start->ts.kind != iter->var->ts.kind
5682 || iter->start->ts.type != iter->var->ts.type)
5683 gfc_convert_type (iter->start, &iter->var->ts, 2);
5684
5685 if (iter->end->ts.kind != iter->var->ts.kind
5686 || iter->end->ts.type != iter->var->ts.type)
5687 gfc_convert_type (iter->end, &iter->var->ts, 2);
5688
5689 if (iter->step->ts.kind != iter->var->ts.kind
5690 || iter->step->ts.type != iter->var->ts.type)
5691 gfc_convert_type (iter->step, &iter->var->ts, 2);
6de9cd9a 5692
dc186969
TB
5693 if (iter->start->expr_type == EXPR_CONSTANT
5694 && iter->end->expr_type == EXPR_CONSTANT
5695 && iter->step->expr_type == EXPR_CONSTANT)
5696 {
5697 int sgn, cmp;
5698 if (iter->start->ts.type == BT_INTEGER)
5699 {
5700 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5701 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5702 }
5703 else
5704 {
5705 sgn = mpfr_sgn (iter->step->value.real);
5706 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5707 }
5708 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5709 gfc_warning ("DO loop at %L will be executed zero times",
5710 &iter->step->where);
5711 }
5712
6de9cd9a
DN
5713 return SUCCESS;
5714}
5715
5716
640670c7
PT
5717/* Traversal function for find_forall_index. f == 2 signals that
5718 that variable itself is not to be checked - only the references. */
ac5ba373 5719
640670c7
PT
5720static bool
5721forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
ac5ba373 5722{
908a2235
PT
5723 if (expr->expr_type != EXPR_VARIABLE)
5724 return false;
5725
640670c7
PT
5726 /* A scalar assignment */
5727 if (!expr->ref || *f == 1)
ac5ba373 5728 {
640670c7
PT
5729 if (expr->symtree->n.sym == sym)
5730 return true;
5731 else
5732 return false;
5733 }
ac5ba373 5734
640670c7
PT
5735 if (*f == 2)
5736 *f = 1;
5737 return false;
5738}
ac5ba373 5739
ac5ba373 5740
640670c7
PT
5741/* Check whether the FORALL index appears in the expression or not.
5742 Returns SUCCESS if SYM is found in EXPR. */
ac5ba373 5743
17b1d2a0 5744gfc_try
640670c7
PT
5745find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5746{
5747 if (gfc_traverse_expr (expr, sym, forall_index, f))
5748 return SUCCESS;
5749 else
5750 return FAILURE;
ac5ba373
TS
5751}
5752
5753
1c54741a
SK
5754/* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5755 to be a scalar INTEGER variable. The subscripts and stride are scalar
ac5ba373
TS
5756 INTEGERs, and if stride is a constant it must be nonzero.
5757 Furthermore "A subscript or stride in a forall-triplet-spec shall
5758 not contain a reference to any index-name in the
5759 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6de9cd9a
DN
5760
5761static void
ac5ba373 5762resolve_forall_iterators (gfc_forall_iterator *it)
6de9cd9a 5763{
ac5ba373
TS
5764 gfc_forall_iterator *iter, *iter2;
5765
5766 for (iter = it; iter; iter = iter->next)
6de9cd9a
DN
5767 {
5768 if (gfc_resolve_expr (iter->var) == SUCCESS
1c54741a
SK
5769 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5770 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6de9cd9a
DN
5771 &iter->var->where);
5772
5773 if (gfc_resolve_expr (iter->start) == SUCCESS
1c54741a
SK
5774 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5775 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6de9cd9a
DN
5776 &iter->start->where);
5777 if (iter->var->ts.kind != iter->start->ts.kind)
5778 gfc_convert_type (iter->start, &iter->var->ts, 2);
5779
5780 if (gfc_resolve_expr (iter->end) == SUCCESS
1c54741a
SK
5781 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5782 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6de9cd9a
DN
5783 &iter->end->where);
5784 if (iter->var->ts.kind != iter->end->ts.kind)
5785 gfc_convert_type (iter->end, &iter->var->ts, 2);
5786
1c54741a
SK
5787 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5788 {
5789 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5790 gfc_error ("FORALL stride expression at %L must be a scalar %s",
edf1eac2 5791 &iter->stride->where, "INTEGER");
1c54741a
SK
5792
5793 if (iter->stride->expr_type == EXPR_CONSTANT
5794 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5795 gfc_error ("FORALL stride expression at %L cannot be zero",
5796 &iter->stride->where);
5797 }
6de9cd9a
DN
5798 if (iter->var->ts.kind != iter->stride->ts.kind)
5799 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6de9cd9a 5800 }
ac5ba373
TS
5801
5802 for (iter = it; iter; iter = iter->next)
5803 for (iter2 = iter; iter2; iter2 = iter2->next)
5804 {
5805 if (find_forall_index (iter2->start,
640670c7 5806 iter->var->symtree->n.sym, 0) == SUCCESS
ac5ba373 5807 || find_forall_index (iter2->end,
640670c7 5808 iter->var->symtree->n.sym, 0) == SUCCESS
ac5ba373 5809 || find_forall_index (iter2->stride,
640670c7 5810 iter->var->symtree->n.sym, 0) == SUCCESS)
ac5ba373
TS
5811 gfc_error ("FORALL index '%s' may not appear in triplet "
5812 "specification at %L", iter->var->symtree->name,
5813 &iter2->start->where);
5814 }
6de9cd9a
DN
5815}
5816
5817
8451584a
EE
5818/* Given a pointer to a symbol that is a derived type, see if it's
5819 inaccessible, i.e. if it's defined in another module and the components are
5820 PRIVATE. The search is recursive if necessary. Returns zero if no
5821 inaccessible components are found, nonzero otherwise. */
5822
5823static int
5824derived_inaccessible (gfc_symbol *sym)
5825{
5826 gfc_component *c;
5827
3dbf6538 5828 if (sym->attr.use_assoc && sym->attr.private_comp)
8451584a
EE
5829 return 1;
5830
5831 for (c = sym->components; c; c = c->next)
5832 {
bc21d315 5833 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
edf1eac2 5834 return 1;
8451584a
EE
5835 }
5836
5837 return 0;
5838}
5839
5840
6de9cd9a
DN
5841/* Resolve the argument of a deallocate expression. The expression must be
5842 a pointer or a full array. */
5843
17b1d2a0 5844static gfc_try
edf1eac2 5845resolve_deallocate_expr (gfc_expr *e)
6de9cd9a
DN
5846{
5847 symbol_attribute attr;
f17facac 5848 int allocatable, pointer, check_intent_in;
6de9cd9a 5849 gfc_ref *ref;
cf2b3c22
TB
5850 gfc_symbol *sym;
5851 gfc_component *c;
6de9cd9a 5852
f17facac
TB
5853 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5854 check_intent_in = 1;
5855
6de9cd9a
DN
5856 if (gfc_resolve_expr (e) == FAILURE)
5857 return FAILURE;
5858
6de9cd9a
DN
5859 if (e->expr_type != EXPR_VARIABLE)
5860 goto bad;
5861
cf2b3c22
TB
5862 sym = e->symtree->n.sym;
5863
5864 if (sym->ts.type == BT_CLASS)
5865 {
5866 allocatable = sym->ts.u.derived->components->attr.allocatable;
5867 pointer = sym->ts.u.derived->components->attr.pointer;
5868 }
5869 else
5870 {
5871 allocatable = sym->attr.allocatable;
5872 pointer = sym->attr.pointer;
5873 }
6de9cd9a 5874 for (ref = e->ref; ref; ref = ref->next)
f17facac
TB
5875 {
5876 if (pointer)
edf1eac2 5877 check_intent_in = 0;
6de9cd9a 5878
f17facac 5879 switch (ref->type)
edf1eac2
SK
5880 {
5881 case REF_ARRAY:
f17facac
TB
5882 if (ref->u.ar.type != AR_FULL)
5883 allocatable = 0;
5884 break;
6de9cd9a 5885
edf1eac2 5886 case REF_COMPONENT:
cf2b3c22
TB
5887 c = ref->u.c.component;
5888 if (c->ts.type == BT_CLASS)
5889 {
5890 allocatable = c->ts.u.derived->components->attr.allocatable;
5891 pointer = c->ts.u.derived->components->attr.pointer;
5892 }
5893 else
5894 {
5895 allocatable = c->attr.allocatable;
5896 pointer = c->attr.pointer;
5897 }
f17facac 5898 break;
6de9cd9a 5899
edf1eac2 5900 case REF_SUBSTRING:
f17facac
TB
5901 allocatable = 0;
5902 break;
edf1eac2 5903 }
f17facac
TB
5904 }
5905
5906 attr = gfc_expr_attr (e);
5907
5908 if (allocatable == 0 && attr.pointer == 0)
6de9cd9a
DN
5909 {
5910 bad:
3759634f
SK
5911 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5912 &e->where);
6de9cd9a
DN
5913 }
5914
cf2b3c22 5915 if (check_intent_in && sym->attr.intent == INTENT_IN)
aa08038d 5916 {
f17facac 5917 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
cf2b3c22 5918 sym->name, &e->where);
aa08038d
EE
5919 return FAILURE;
5920 }
5921
cf2b3c22
TB
5922 if (e->ts.type == BT_CLASS)
5923 {
5924 /* Only deallocate the DATA component. */
5925 gfc_add_component_ref (e, "$data");
5926 }
5927
6de9cd9a
DN
5928 return SUCCESS;
5929}
5930
edf1eac2 5931
908a2235 5932/* Returns true if the expression e contains a reference to the symbol sym. */
77726571 5933static bool
908a2235 5934sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
77726571 5935{
908a2235
PT
5936 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5937 return true;
77726571 5938
908a2235
PT
5939 return false;
5940}
77726571 5941
a68ab351
JJ
5942bool
5943gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
908a2235
PT
5944{
5945 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
77726571
PT
5946}
5947
6de9cd9a 5948
68577e56
EE
5949/* Given the expression node e for an allocatable/pointer of derived type to be
5950 allocated, get the expression node to be initialized afterwards (needed for
5046aff5
PT
5951 derived types with default initializers, and derived types with allocatable
5952 components that need nullification.) */
68577e56 5953
cf2b3c22
TB
5954gfc_expr *
5955gfc_expr_to_initialize (gfc_expr *e)
68577e56
EE
5956{
5957 gfc_expr *result;
5958 gfc_ref *ref;
5959 int i;
5960
5961 result = gfc_copy_expr (e);
5962
5963 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
5964 for (ref = result->ref; ref; ref = ref->next)
5965 if (ref->type == REF_ARRAY && ref->next == NULL)
5966 {
edf1eac2 5967 ref->u.ar.type = AR_FULL;
68577e56 5968
edf1eac2
SK
5969 for (i = 0; i < ref->u.ar.dimen; i++)
5970 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
68577e56 5971
edf1eac2
SK
5972 result->rank = ref->u.ar.dimen;
5973 break;
68577e56
EE
5974 }
5975
5976 return result;
5977}
5978
5979
8460475b
JW
5980/* Used in resolve_allocate_expr to check that a allocation-object and
5981 a source-expr are conformable. This does not catch all possible
5982 cases; in particular a runtime checking is needed. */
5983
5984static gfc_try
5985conformable_arrays (gfc_expr *e1, gfc_expr *e2)
5986{
5987 /* First compare rank. */
5988 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
5989 {
5990 gfc_error ("Source-expr at %L must be scalar or have the "
5991 "same rank as the allocate-object at %L",
5992 &e1->where, &e2->where);
5993 return FAILURE;
5994 }
5995
5996 if (e1->shape)
5997 {
5998 int i;
5999 mpz_t s;
6000
6001 mpz_init (s);
6002
6003 for (i = 0; i < e1->rank; i++)
6004 {
6005 if (e2->ref->u.ar.end[i])
6006 {
6007 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6008 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6009 mpz_add_ui (s, s, 1);
6010 }
6011 else
6012 {
6013 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6014 }
6015
6016 if (mpz_cmp (e1->shape[i], s) != 0)
6017 {
6018 gfc_error ("Source-expr at %L and allocate-object at %L must "
6019 "have the same shape", &e1->where, &e2->where);
6020 mpz_clear (s);
6021 return FAILURE;
6022 }
6023 }
6024
6025 mpz_clear (s);
6026 }
6027
6028 return SUCCESS;
6029}
6030
6031
6de9cd9a
DN
6032/* Resolve the expression in an ALLOCATE statement, doing the additional
6033 checks to see whether the expression is OK or not. The expression must
6034 have a trailing array reference that gives the size of the array. */
6035
17b1d2a0 6036static gfc_try
edf1eac2 6037resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6de9cd9a 6038{
d0a9804e 6039 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6de9cd9a
DN
6040 symbol_attribute attr;
6041 gfc_ref *ref, *ref2;
6042 gfc_array_ref *ar;
77726571
PT
6043 gfc_symbol *sym;
6044 gfc_alloc *a;
cf2b3c22 6045 gfc_component *c;
6de9cd9a 6046
f17facac
TB
6047 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6048 check_intent_in = 1;
6049
6de9cd9a
DN
6050 if (gfc_resolve_expr (e) == FAILURE)
6051 return FAILURE;
6052
6053 /* Make sure the expression is allocatable or a pointer. If it is
6054 pointer, the next-to-last reference must be a pointer. */
6055
6056 ref2 = NULL;
cf2b3c22
TB
6057 if (e->symtree)
6058 sym = e->symtree->n.sym;
6de9cd9a 6059
d0a9804e
TB
6060 /* Check whether ultimate component is abstract and CLASS. */
6061 is_abstract = 0;
6062
6de9cd9a
DN
6063 if (e->expr_type != EXPR_VARIABLE)
6064 {
6065 allocatable = 0;
6de9cd9a
DN
6066 attr = gfc_expr_attr (e);
6067 pointer = attr.pointer;
6068 dimension = attr.dimension;
6de9cd9a
DN
6069 }
6070 else
6071 {
cf2b3c22
TB
6072 if (sym->ts.type == BT_CLASS)
6073 {
6074 allocatable = sym->ts.u.derived->components->attr.allocatable;
6075 pointer = sym->ts.u.derived->components->attr.pointer;
6076 dimension = sym->ts.u.derived->components->attr.dimension;
d0a9804e 6077 is_abstract = sym->ts.u.derived->components->attr.abstract;
cf2b3c22
TB
6078 }
6079 else
6080 {
6081 allocatable = sym->attr.allocatable;
6082 pointer = sym->attr.pointer;
6083 dimension = sym->attr.dimension;
6084 }
6de9cd9a
DN
6085
6086 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
edf1eac2 6087 {
f17facac
TB
6088 if (pointer)
6089 check_intent_in = 0;
6de9cd9a 6090
f17facac
TB
6091 switch (ref->type)
6092 {
6093 case REF_ARRAY:
edf1eac2
SK
6094 if (ref->next != NULL)
6095 pointer = 0;
6096 break;
f17facac
TB
6097
6098 case REF_COMPONENT:
cf2b3c22
TB
6099 c = ref->u.c.component;
6100 if (c->ts.type == BT_CLASS)
6101 {
6102 allocatable = c->ts.u.derived->components->attr.allocatable;
6103 pointer = c->ts.u.derived->components->attr.pointer;
6104 dimension = c->ts.u.derived->components->attr.dimension;
d0a9804e 6105 is_abstract = c->ts.u.derived->components->attr.abstract;
cf2b3c22
TB
6106 }
6107 else
6108 {
6109 allocatable = c->attr.allocatable;
6110 pointer = c->attr.pointer;
6111 dimension = c->attr.dimension;
d0a9804e 6112 is_abstract = c->attr.abstract;
cf2b3c22 6113 }
edf1eac2 6114 break;
f17facac
TB
6115
6116 case REF_SUBSTRING:
edf1eac2
SK
6117 allocatable = 0;
6118 pointer = 0;
6119 break;
f17facac 6120 }
8e1f752a 6121 }
6de9cd9a
DN
6122 }
6123
6124 if (allocatable == 0 && pointer == 0)
6125 {
3759634f
SK
6126 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6127 &e->where);
6de9cd9a
DN
6128 return FAILURE;
6129 }
6130
8460475b
JW
6131 /* Some checks for the SOURCE tag. */
6132 if (code->expr3)
6133 {
6134 /* Check F03:C631. */
6135 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6136 {
6137 gfc_error ("Type of entity at %L is type incompatible with "
6138 "source-expr at %L", &e->where, &code->expr3->where);
6139 return FAILURE;
6140 }
6141
6142 /* Check F03:C632 and restriction following Note 6.18. */
6143 if (code->expr3->rank > 0
6144 && conformable_arrays (code->expr3, e) == FAILURE)
6145 return FAILURE;
6146
6147 /* Check F03:C633. */
6148 if (code->expr3->ts.kind != e->ts.kind)
6149 {
6150 gfc_error ("The allocate-object at %L and the source-expr at %L "
6151 "shall have the same kind type parameter",
6152 &e->where, &code->expr3->where);
6153 return FAILURE;
6154 }
6155 }
6156 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
d0a9804e
TB
6157 {
6158 gcc_assert (e->ts.type == BT_CLASS);
6159 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6160 "type-spec or SOURCE=", sym->name, &e->where);
6161 return FAILURE;
6162 }
6163
cf2b3c22 6164 if (check_intent_in && sym->attr.intent == INTENT_IN)
aa08038d 6165 {
f17facac 6166 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
cf2b3c22 6167 sym->name, &e->where);
aa08038d
EE
6168 return FAILURE;
6169 }
6170
2fbd4117 6171 if (pointer || dimension == 0)
6de9cd9a
DN
6172 return SUCCESS;
6173
6174 /* Make sure the next-to-last reference node is an array specification. */
6175
6176 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6177 {
6178 gfc_error ("Array specification required in ALLOCATE statement "
6179 "at %L", &e->where);
6180 return FAILURE;
6181 }
6182
6de9cd9a
DN
6183 /* Make sure that the array section reference makes sense in the
6184 context of an ALLOCATE specification. */
6185
6186 ar = &ref2->u.ar;
6187
6188 for (i = 0; i < ar->dimen; i++)
77726571
PT
6189 {
6190 if (ref2->u.ar.type == AR_ELEMENT)
6191 goto check_symbols;
6de9cd9a 6192
77726571
PT
6193 switch (ar->dimen_type[i])
6194 {
6195 case DIMEN_ELEMENT:
6de9cd9a
DN
6196 break;
6197
77726571
PT
6198 case DIMEN_RANGE:
6199 if (ar->start[i] != NULL
6200 && ar->end[i] != NULL
6201 && ar->stride[i] == NULL)
6202 break;
6de9cd9a 6203
77726571
PT
6204 /* Fall Through... */
6205
6206 case DIMEN_UNKNOWN:
6207 case DIMEN_VECTOR:
6208 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6209 &e->where);
6210 return FAILURE;
6211 }
6212
6213check_symbols:
6214
cf2b3c22 6215 for (a = code->ext.alloc.list; a; a = a->next)
77726571
PT
6216 {
6217 sym = a->expr->symtree->n.sym;
25e8cb2e
PT
6218
6219 /* TODO - check derived type components. */
6168891d 6220 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
25e8cb2e
PT
6221 continue;
6222
a68ab351
JJ
6223 if ((ar->start[i] != NULL
6224 && gfc_find_sym_in_expr (sym, ar->start[i]))
6225 || (ar->end[i] != NULL
6226 && gfc_find_sym_in_expr (sym, ar->end[i])))
77726571 6227 {
df2fba9e 6228 gfc_error ("'%s' must not appear in the array specification at "
77726571
PT
6229 "%L in the same ALLOCATE statement where it is "
6230 "itself allocated", sym->name, &ar->where);
6231 return FAILURE;
6232 }
6233 }
6234 }
6de9cd9a
DN
6235
6236 return SUCCESS;
6237}
6238
b9332b09
PT
6239static void
6240resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6241{
3759634f
SK
6242 gfc_expr *stat, *errmsg, *pe, *qe;
6243 gfc_alloc *a, *p, *q;
6244
a513927a 6245 stat = code->expr1 ? code->expr1 : NULL;
b9332b09 6246
3759634f 6247 errmsg = code->expr2 ? code->expr2 : NULL;
b9332b09 6248
3759634f
SK
6249 /* Check the stat variable. */
6250 if (stat)
b9332b09 6251 {
3759634f
SK
6252 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6253 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6254 stat->symtree->n.sym->name, &stat->where);
6255
6256 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6257 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6258 &stat->where);
b9332b09 6259
6c145259
TK
6260 if ((stat->ts.type != BT_INTEGER
6261 && !(stat->ref && (stat->ref->type == REF_ARRAY
6262 || stat->ref->type == REF_COMPONENT)))
6263 || stat->rank > 0)
3759634f
SK
6264 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6265 "variable", &stat->where);
6266
cf2b3c22 6267 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6268 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6269 gfc_error ("Stat-variable at %L shall not be %sd within "
6270 "the same %s statement", &stat->where, fcn, fcn);
b9332b09
PT
6271 }
6272
3759634f
SK
6273 /* Check the errmsg variable. */
6274 if (errmsg)
6275 {
6276 if (!stat)
6277 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6278 &errmsg->where);
6279
6280 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6281 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6282 errmsg->symtree->n.sym->name, &errmsg->where);
6283
6284 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6285 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6286 &errmsg->where);
6287
6c145259
TK
6288 if ((errmsg->ts.type != BT_CHARACTER
6289 && !(errmsg->ref
6290 && (errmsg->ref->type == REF_ARRAY
6291 || errmsg->ref->type == REF_COMPONENT)))
6292 || errmsg->rank > 0 )
3759634f
SK
6293 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6294 "variable", &errmsg->where);
6295
cf2b3c22 6296 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6297 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6298 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6299 "the same %s statement", &errmsg->where, fcn, fcn);
6300 }
6301
6302 /* Check that an allocate-object appears only once in the statement.
6303 FIXME: Checking derived types is disabled. */
cf2b3c22 6304 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6305 {
6306 pe = p->expr;
6307 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6308 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6309 {
6310 for (q = p->next; q; q = q->next)
6311 {
6312 qe = q->expr;
6313 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6314 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6315 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6316 gfc_error ("Allocate-object at %L also appears at %L",
6317 &pe->where, &qe->where);
6318 }
6319 }
6320 }
b9332b09
PT
6321
6322 if (strcmp (fcn, "ALLOCATE") == 0)
6323 {
cf2b3c22 6324 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
6325 resolve_allocate_expr (a->expr, code);
6326 }
6327 else
6328 {
cf2b3c22 6329 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
6330 resolve_deallocate_expr (a->expr);
6331 }
6332}
6de9cd9a 6333
3759634f 6334
6de9cd9a
DN
6335/************ SELECT CASE resolution subroutines ************/
6336
6337/* Callback function for our mergesort variant. Determines interval
6338 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
c224550f
SK
6339 op1 > op2. Assumes we're not dealing with the default case.
6340 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6341 There are nine situations to check. */
6de9cd9a
DN
6342
6343static int
edf1eac2 6344compare_cases (const gfc_case *op1, const gfc_case *op2)
6de9cd9a 6345{
c224550f 6346 int retval;
6de9cd9a 6347
c224550f 6348 if (op1->low == NULL) /* op1 = (:L) */
6de9cd9a 6349 {
c224550f
SK
6350 /* op2 = (:N), so overlap. */
6351 retval = 0;
6352 /* op2 = (M:) or (M:N), L < M */
6353 if (op2->low != NULL
7b4c5f8b 6354 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 6355 retval = -1;
6de9cd9a 6356 }
c224550f 6357 else if (op1->high == NULL) /* op1 = (K:) */
6de9cd9a 6358 {
c224550f
SK
6359 /* op2 = (M:), so overlap. */
6360 retval = 0;
6361 /* op2 = (:N) or (M:N), K > N */
6362 if (op2->high != NULL
7b4c5f8b 6363 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 6364 retval = 1;
6de9cd9a 6365 }
c224550f 6366 else /* op1 = (K:L) */
6de9cd9a 6367 {
c224550f 6368 if (op2->low == NULL) /* op2 = (:N), K > N */
7b4c5f8b
TB
6369 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6370 ? 1 : 0;
c224550f 6371 else if (op2->high == NULL) /* op2 = (M:), L < M */
7b4c5f8b
TB
6372 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6373 ? -1 : 0;
edf1eac2
SK
6374 else /* op2 = (M:N) */
6375 {
c224550f 6376 retval = 0;
edf1eac2 6377 /* L < M */
7b4c5f8b 6378 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 6379 retval = -1;
edf1eac2 6380 /* K > N */
7b4c5f8b 6381 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 6382 retval = 1;
6de9cd9a
DN
6383 }
6384 }
c224550f
SK
6385
6386 return retval;
6de9cd9a
DN
6387}
6388
6389
6390/* Merge-sort a double linked case list, detecting overlap in the
6391 process. LIST is the head of the double linked case list before it
6392 is sorted. Returns the head of the sorted list if we don't see any
6393 overlap, or NULL otherwise. */
6394
6395static gfc_case *
edf1eac2 6396check_case_overlap (gfc_case *list)
6de9cd9a
DN
6397{
6398 gfc_case *p, *q, *e, *tail;
6399 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6400
6401 /* If the passed list was empty, return immediately. */
6402 if (!list)
6403 return NULL;
6404
6405 overlap_seen = 0;
6406 insize = 1;
6407
6408 /* Loop unconditionally. The only exit from this loop is a return
6409 statement, when we've finished sorting the case list. */
6410 for (;;)
6411 {
6412 p = list;
6413 list = NULL;
6414 tail = NULL;
6415
6416 /* Count the number of merges we do in this pass. */
6417 nmerges = 0;
6418
6419 /* Loop while there exists a merge to be done. */
6420 while (p)
6421 {
6422 int i;
6423
6424 /* Count this merge. */
6425 nmerges++;
6426
5352b89f 6427 /* Cut the list in two pieces by stepping INSIZE places
edf1eac2 6428 forward in the list, starting from P. */
6de9cd9a
DN
6429 psize = 0;
6430 q = p;
6431 for (i = 0; i < insize; i++)
6432 {
6433 psize++;
6434 q = q->right;
6435 if (!q)
6436 break;
6437 }
6438 qsize = insize;
6439
6440 /* Now we have two lists. Merge them! */
6441 while (psize > 0 || (qsize > 0 && q != NULL))
6442 {
6de9cd9a
DN
6443 /* See from which the next case to merge comes from. */
6444 if (psize == 0)
6445 {
6446 /* P is empty so the next case must come from Q. */
6447 e = q;
6448 q = q->right;
6449 qsize--;
6450 }
6451 else if (qsize == 0 || q == NULL)
6452 {
6453 /* Q is empty. */
6454 e = p;
6455 p = p->right;
6456 psize--;
6457 }
6458 else
6459 {
6460 cmp = compare_cases (p, q);
6461 if (cmp < 0)
6462 {
6463 /* The whole case range for P is less than the
edf1eac2 6464 one for Q. */
6de9cd9a
DN
6465 e = p;
6466 p = p->right;
6467 psize--;
6468 }
6469 else if (cmp > 0)
6470 {
6471 /* The whole case range for Q is greater than
edf1eac2 6472 the case range for P. */
6de9cd9a
DN
6473 e = q;
6474 q = q->right;
6475 qsize--;
6476 }
6477 else
6478 {
6479 /* The cases overlap, or they are the same
6480 element in the list. Either way, we must
6481 issue an error and get the next case from P. */
6482 /* FIXME: Sort P and Q by line number. */
6483 gfc_error ("CASE label at %L overlaps with CASE "
6484 "label at %L", &p->where, &q->where);
6485 overlap_seen = 1;
6486 e = p;
6487 p = p->right;
6488 psize--;
6489 }
6490 }
6491
6492 /* Add the next element to the merged list. */
6493 if (tail)
6494 tail->right = e;
6495 else
6496 list = e;
6497 e->left = tail;
6498 tail = e;
6499 }
6500
6501 /* P has now stepped INSIZE places along, and so has Q. So
edf1eac2 6502 they're the same. */
6de9cd9a
DN
6503 p = q;
6504 }
6505 tail->right = NULL;
6506
6507 /* If we have done only one merge or none at all, we've
edf1eac2 6508 finished sorting the cases. */
6de9cd9a 6509 if (nmerges <= 1)
edf1eac2 6510 {
6de9cd9a
DN
6511 if (!overlap_seen)
6512 return list;
6513 else
6514 return NULL;
6515 }
6516
6517 /* Otherwise repeat, merging lists twice the size. */
6518 insize *= 2;
6519 }
6520}
6521
6522
5352b89f
SK
6523/* Check to see if an expression is suitable for use in a CASE statement.
6524 Makes sure that all case expressions are scalar constants of the same
6525 type. Return FAILURE if anything is wrong. */
6de9cd9a 6526
17b1d2a0 6527static gfc_try
edf1eac2 6528validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6de9cd9a 6529{
6de9cd9a
DN
6530 if (e == NULL) return SUCCESS;
6531
5352b89f 6532 if (e->ts.type != case_expr->ts.type)
6de9cd9a
DN
6533 {
6534 gfc_error ("Expression in CASE statement at %L must be of type %s",
5352b89f 6535 &e->where, gfc_basic_typename (case_expr->ts.type));
6de9cd9a
DN
6536 return FAILURE;
6537 }
6538
5352b89f
SK
6539 /* C805 (R808) For a given case-construct, each case-value shall be of
6540 the same type as case-expr. For character type, length differences
6541 are allowed, but the kind type parameters shall be the same. */
6542
6543 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6de9cd9a 6544 {
d393bbd7
FXC
6545 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6546 &e->where, case_expr->ts.kind);
6de9cd9a
DN
6547 return FAILURE;
6548 }
6549
5352b89f
SK
6550 /* Convert the case value kind to that of case expression kind, if needed.
6551 FIXME: Should a warning be issued? */
6552 if (e->ts.kind != case_expr->ts.kind)
6553 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6554
6de9cd9a
DN
6555 if (e->rank != 0)
6556 {
6557 gfc_error ("Expression in CASE statement at %L must be scalar",
6558 &e->where);
6559 return FAILURE;
6560 }
6561
6562 return SUCCESS;
6563}
6564
6565
6566/* Given a completely parsed select statement, we:
6567
6568 - Validate all expressions and code within the SELECT.
6569 - Make sure that the selection expression is not of the wrong type.
6570 - Make sure that no case ranges overlap.
6571 - Eliminate unreachable cases and unreachable code resulting from
6572 removing case labels.
6573
6574 The standard does allow unreachable cases, e.g. CASE (5:3). But
6575 they are a hassle for code generation, and to prevent that, we just
6576 cut them out here. This is not necessary for overlapping cases
6577 because they are illegal and we never even try to generate code.
6578
6579 We have the additional caveat that a SELECT construct could have
1f2959f0 6580 been a computed GOTO in the source code. Fortunately we can fairly
6de9cd9a
DN
6581 easily work around that here: The case_expr for a "real" SELECT CASE
6582 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6583 we have to do is make sure that the case_expr is a scalar integer
6584 expression. */
6585
6586static void
edf1eac2 6587resolve_select (gfc_code *code)
6de9cd9a
DN
6588{
6589 gfc_code *body;
6590 gfc_expr *case_expr;
6591 gfc_case *cp, *default_case, *tail, *head;
6592 int seen_unreachable;
d68bd5a8 6593 int seen_logical;
6de9cd9a
DN
6594 int ncases;
6595 bt type;
17b1d2a0 6596 gfc_try t;
6de9cd9a 6597
a513927a 6598 if (code->expr1 == NULL)
6de9cd9a
DN
6599 {
6600 /* This was actually a computed GOTO statement. */
6601 case_expr = code->expr2;
edf1eac2 6602 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6de9cd9a
DN
6603 gfc_error ("Selection expression in computed GOTO statement "
6604 "at %L must be a scalar integer expression",
6605 &case_expr->where);
6606
6607 /* Further checking is not necessary because this SELECT was built
6608 by the compiler, so it should always be OK. Just move the
6609 case_expr from expr2 to expr so that we can handle computed
6610 GOTOs as normal SELECTs from here on. */
a513927a 6611 code->expr1 = code->expr2;
6de9cd9a
DN
6612 code->expr2 = NULL;
6613 return;
6614 }
6615
a513927a 6616 case_expr = code->expr1;
6de9cd9a
DN
6617
6618 type = case_expr->ts.type;
6619 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6620 {
6621 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6622 &case_expr->where, gfc_typename (&case_expr->ts));
6623
6624 /* Punt. Going on here just produce more garbage error messages. */
6625 return;
6626 }
6627
6628 if (case_expr->rank != 0)
6629 {
6630 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6631 "expression", &case_expr->where);
6632
6633 /* Punt. */
6634 return;
6635 }
6636
5352b89f
SK
6637 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6638 of the SELECT CASE expression and its CASE values. Walk the lists
6639 of case values, and if we find a mismatch, promote case_expr to
6640 the appropriate kind. */
6641
6642 if (type == BT_LOGICAL || type == BT_INTEGER)
6643 {
6644 for (body = code->block; body; body = body->block)
6645 {
6646 /* Walk the case label list. */
6647 for (cp = body->ext.case_list; cp; cp = cp->next)
6648 {
6649 /* Intercept the DEFAULT case. It does not have a kind. */
6650 if (cp->low == NULL && cp->high == NULL)
6651 continue;
6652
05c1e3a7 6653 /* Unreachable case ranges are discarded, so ignore. */
5352b89f
SK
6654 if (cp->low != NULL && cp->high != NULL
6655 && cp->low != cp->high
7b4c5f8b 6656 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5352b89f
SK
6657 continue;
6658
6659 /* FIXME: Should a warning be issued? */
6660 if (cp->low != NULL
6661 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6662 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6663
6664 if (cp->high != NULL
6665 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
05c1e3a7 6666 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5352b89f
SK
6667 }
6668 }
6669 }
6670
6de9cd9a
DN
6671 /* Assume there is no DEFAULT case. */
6672 default_case = NULL;
6673 head = tail = NULL;
6674 ncases = 0;
d68bd5a8 6675 seen_logical = 0;
6de9cd9a
DN
6676
6677 for (body = code->block; body; body = body->block)
6678 {
6679 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6680 t = SUCCESS;
6681 seen_unreachable = 0;
6682
6683 /* Walk the case label list, making sure that all case labels
edf1eac2 6684 are legal. */
6de9cd9a
DN
6685 for (cp = body->ext.case_list; cp; cp = cp->next)
6686 {
6687 /* Count the number of cases in the whole construct. */
6688 ncases++;
6689
6690 /* Intercept the DEFAULT case. */
6691 if (cp->low == NULL && cp->high == NULL)
6692 {
6693 if (default_case != NULL)
edf1eac2 6694 {
6de9cd9a
DN
6695 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6696 "by a second DEFAULT CASE at %L",
6697 &default_case->where, &cp->where);
6698 t = FAILURE;
6699 break;
6700 }
6701 else
6702 {
6703 default_case = cp;
6704 continue;
6705 }
6706 }
6707
6708 /* Deal with single value cases and case ranges. Errors are
edf1eac2 6709 issued from the validation function. */
6de9cd9a
DN
6710 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6711 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6712 {
6713 t = FAILURE;
6714 break;
6715 }
6716
6717 if (type == BT_LOGICAL
6718 && ((cp->low == NULL || cp->high == NULL)
6719 || cp->low != cp->high))
6720 {
edf1eac2
SK
6721 gfc_error ("Logical range in CASE statement at %L is not "
6722 "allowed", &cp->low->where);
6de9cd9a
DN
6723 t = FAILURE;
6724 break;
6725 }
6726
d68bd5a8
PT
6727 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6728 {
6729 int value;
6730 value = cp->low->value.logical == 0 ? 2 : 1;
6731 if (value & seen_logical)
6732 {
6733 gfc_error ("constant logical value in CASE statement "
6734 "is repeated at %L",
6735 &cp->low->where);
6736 t = FAILURE;
6737 break;
6738 }
6739 seen_logical |= value;
6740 }
6741
6de9cd9a
DN
6742 if (cp->low != NULL && cp->high != NULL
6743 && cp->low != cp->high
7b4c5f8b 6744 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6de9cd9a
DN
6745 {
6746 if (gfc_option.warn_surprising)
edf1eac2 6747 gfc_warning ("Range specification at %L can never "
6de9cd9a
DN
6748 "be matched", &cp->where);
6749
6750 cp->unreachable = 1;
6751 seen_unreachable = 1;
6752 }
6753 else
6754 {
6755 /* If the case range can be matched, it can also overlap with
6756 other cases. To make sure it does not, we put it in a
6757 double linked list here. We sort that with a merge sort
6758 later on to detect any overlapping cases. */
6759 if (!head)
edf1eac2 6760 {
6de9cd9a
DN
6761 head = tail = cp;
6762 head->right = head->left = NULL;
6763 }
6764 else
edf1eac2 6765 {
6de9cd9a
DN
6766 tail->right = cp;
6767 tail->right->left = tail;
6768 tail = tail->right;
6769 tail->right = NULL;
6770 }
6771 }
6772 }
6773
6774 /* It there was a failure in the previous case label, give up
6775 for this case label list. Continue with the next block. */
6776 if (t == FAILURE)
6777 continue;
6778
6779 /* See if any case labels that are unreachable have been seen.
6780 If so, we eliminate them. This is a bit of a kludge because
6781 the case lists for a single case statement (label) is a
6782 single forward linked lists. */
6783 if (seen_unreachable)
6784 {
6785 /* Advance until the first case in the list is reachable. */
6786 while (body->ext.case_list != NULL
6787 && body->ext.case_list->unreachable)
6788 {
6789 gfc_case *n = body->ext.case_list;
6790 body->ext.case_list = body->ext.case_list->next;
6791 n->next = NULL;
6792 gfc_free_case_list (n);
6793 }
6794
6795 /* Strip all other unreachable cases. */
6796 if (body->ext.case_list)
6797 {
6798 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6799 {
6800 if (cp->next->unreachable)
6801 {
6802 gfc_case *n = cp->next;
6803 cp->next = cp->next->next;
6804 n->next = NULL;
6805 gfc_free_case_list (n);
6806 }
6807 }
6808 }
6809 }
6810 }
6811
6812 /* See if there were overlapping cases. If the check returns NULL,
6813 there was overlap. In that case we don't do anything. If head
6814 is non-NULL, we prepend the DEFAULT case. The sorted list can
6815 then used during code generation for SELECT CASE constructs with
6816 a case expression of a CHARACTER type. */
6817 if (head)
6818 {
6819 head = check_case_overlap (head);
6820
6821 /* Prepend the default_case if it is there. */
6822 if (head != NULL && default_case)
6823 {
6824 default_case->left = NULL;
6825 default_case->right = head;
6826 head->left = default_case;
6827 }
6828 }
6829
6830 /* Eliminate dead blocks that may be the result if we've seen
6831 unreachable case labels for a block. */
6832 for (body = code; body && body->block; body = body->block)
6833 {
6834 if (body->block->ext.case_list == NULL)
edf1eac2 6835 {
6de9cd9a
DN
6836 /* Cut the unreachable block from the code chain. */
6837 gfc_code *c = body->block;
6838 body->block = c->block;
6839
6840 /* Kill the dead block, but not the blocks below it. */
6841 c->block = NULL;
6842 gfc_free_statements (c);
edf1eac2 6843 }
6de9cd9a
DN
6844 }
6845
6846 /* More than two cases is legal but insane for logical selects.
6847 Issue a warning for it. */
6848 if (gfc_option.warn_surprising && type == BT_LOGICAL
6849 && ncases > 2)
6850 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6851 &code->loc);
6852}
6853
6854
cf2b3c22
TB
6855/* Check if a derived type is extensible. */
6856
6857bool
6858gfc_type_is_extensible (gfc_symbol *sym)
6859{
6860 return !(sym->attr.is_bind_c || sym->attr.sequence);
6861}
6862
6863
6864/* Resolve a SELECT TYPE statement. */
6865
6866static void
6867resolve_select_type (gfc_code *code)
6868{
6869 gfc_symbol *selector_type;
6870 gfc_code *body, *new_st;
6871 gfc_case *c, *default_case;
6872 gfc_symtree *st;
6873 char name[GFC_MAX_SYMBOL_LEN];
93d76687
JW
6874 gfc_namespace *ns;
6875
6876 ns = code->ext.ns;
6877 gfc_resolve (ns);
cf2b3c22 6878
93d76687
JW
6879 if (code->expr2)
6880 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6881 else
6882 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
cf2b3c22
TB
6883
6884 /* Assume there is no DEFAULT case. */
6885 default_case = NULL;
6886
6887 /* Loop over TYPE IS / CLASS IS cases. */
6888 for (body = code->block; body; body = body->block)
6889 {
6890 c = body->ext.case_list;
6891
6892 /* Check F03:C815. */
6893 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6894 && !gfc_type_is_extensible (c->ts.u.derived))
6895 {
6896 gfc_error ("Derived type '%s' at %L must be extensible",
6897 c->ts.u.derived->name, &c->where);
6898 continue;
6899 }
6900
6901 /* Check F03:C816. */
6902 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6903 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
6904 {
6905 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6906 c->ts.u.derived->name, &c->where, selector_type->name);
6907 continue;
6908 }
6909
6910 /* Intercept the DEFAULT case. */
6911 if (c->ts.type == BT_UNKNOWN)
6912 {
6913 /* Check F03:C818. */
6914 if (default_case != NULL)
6915 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6916 "by a second DEFAULT CASE at %L",
6917 &default_case->where, &c->where);
6918 else
6919 default_case = c;
6920 continue;
6921 }
6922 }
6923
93d76687
JW
6924 if (code->expr2)
6925 {
6926 /* Insert assignment for selector variable. */
6927 new_st = gfc_get_code ();
6928 new_st->op = EXEC_ASSIGN;
6929 new_st->expr1 = gfc_copy_expr (code->expr1);
6930 new_st->expr2 = gfc_copy_expr (code->expr2);
6931 ns->code = new_st;
6932 }
6933
6934 /* Put SELECT TYPE statement inside a BLOCK. */
6935 new_st = gfc_get_code ();
6936 new_st->op = code->op;
6937 new_st->expr1 = code->expr1;
6938 new_st->expr2 = code->expr2;
6939 new_st->block = code->block;
6940 if (!ns->code)
6941 ns->code = new_st;
6942 else
6943 ns->code->next = new_st;
6944 code->op = EXEC_BLOCK;
6945 code->expr1 = code->expr2 = NULL;
6946 code->block = NULL;
6947
6948 code = new_st;
6949
cf2b3c22
TB
6950 /* Transform to EXEC_SELECT. */
6951 code->op = EXEC_SELECT;
6952 gfc_add_component_ref (code->expr1, "$vindex");
6953
6954 /* Loop over TYPE IS / CLASS IS cases. */
6955 for (body = code->block; body; body = body->block)
6956 {
6957 c = body->ext.case_list;
6958 if (c->ts.type == BT_DERIVED)
6959 c->low = c->high = gfc_int_expr (c->ts.u.derived->vindex);
6960 else if (c->ts.type == BT_CLASS)
6961 /* Currently IS CLASS blocks are simply ignored.
6962 TODO: Implement IS CLASS. */
6963 c->unreachable = 1;
6964
6965 if (c->ts.type != BT_DERIVED)
6966 continue;
6967 /* Assign temporary to selector. */
6968 sprintf (name, "tmp$%s", c->ts.u.derived->name);
93d76687 6969 st = gfc_find_symtree (ns->sym_root, name);
cf2b3c22
TB
6970 new_st = gfc_get_code ();
6971 new_st->op = EXEC_POINTER_ASSIGN;
6972 new_st->expr1 = gfc_get_variable_expr (st);
6973 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
6974 gfc_add_component_ref (new_st->expr2, "$data");
6975 new_st->next = body->next;
6976 body->next = new_st;
6977 }
6978
6979 /* Eliminate dead blocks. */
6980 for (body = code; body && body->block; body = body->block)
6981 {
6982 if (body->block->ext.case_list->unreachable)
6983 {
6984 /* Cut the unreachable block from the code chain. */
6985 gfc_code *cd = body->block;
6986 body->block = cd->block;
6987 /* Kill the dead block, but not the blocks below it. */
6988 cd->block = NULL;
6989 gfc_free_statements (cd);
6990 }
6991 }
6992
6993 resolve_select (code);
6994
6995}
6996
6997
0e6928d8
TS
6998/* Resolve a transfer statement. This is making sure that:
6999 -- a derived type being transferred has only non-pointer components
8451584a
EE
7000 -- a derived type being transferred doesn't have private components, unless
7001 it's being transferred from the module where the type was defined
0e6928d8
TS
7002 -- we're not trying to transfer a whole assumed size array. */
7003
7004static void
edf1eac2 7005resolve_transfer (gfc_code *code)
0e6928d8
TS
7006{
7007 gfc_typespec *ts;
7008 gfc_symbol *sym;
7009 gfc_ref *ref;
7010 gfc_expr *exp;
7011
a513927a 7012 exp = code->expr1;
0e6928d8 7013
edf1eac2 7014 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
0e6928d8
TS
7015 return;
7016
7017 sym = exp->symtree->n.sym;
7018 ts = &sym->ts;
7019
7020 /* Go to actual component transferred. */
a513927a 7021 for (ref = code->expr1->ref; ref; ref = ref->next)
0e6928d8
TS
7022 if (ref->type == REF_COMPONENT)
7023 ts = &ref->u.c.component->ts;
7024
7025 if (ts->type == BT_DERIVED)
7026 {
7027 /* Check that transferred derived type doesn't contain POINTER
7028 components. */
bc21d315 7029 if (ts->u.derived->attr.pointer_comp)
0e6928d8
TS
7030 {
7031 gfc_error ("Data transfer element at %L cannot have "
7032 "POINTER components", &code->loc);
7033 return;
7034 }
7035
bc21d315 7036 if (ts->u.derived->attr.alloc_comp)
5046aff5
PT
7037 {
7038 gfc_error ("Data transfer element at %L cannot have "
7039 "ALLOCATABLE components", &code->loc);
7040 return;
7041 }
7042
bc21d315 7043 if (derived_inaccessible (ts->u.derived))
0e6928d8
TS
7044 {
7045 gfc_error ("Data transfer element at %L cannot have "
7046 "PRIVATE components",&code->loc);
7047 return;
7048 }
7049 }
7050
7051 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7052 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7053 {
7054 gfc_error ("Data transfer element at %L cannot be a full reference to "
7055 "an assumed-size array", &code->loc);
7056 return;
7057 }
7058}
7059
7060
6de9cd9a
DN
7061/*********** Toplevel code resolution subroutines ***********/
7062
0615f923 7063/* Find the set of labels that are reachable from this block. We also
d80c695f 7064 record the last statement in each block. */
0615f923
TS
7065
7066static void
d80c695f 7067find_reachable_labels (gfc_code *block)
0615f923
TS
7068{
7069 gfc_code *c;
7070
7071 if (!block)
7072 return;
7073
7074 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7075
d80c695f
TS
7076 /* Collect labels in this block. We don't keep those corresponding
7077 to END {IF|SELECT}, these are checked in resolve_branch by going
7078 up through the code_stack. */
0615f923
TS
7079 for (c = block; c; c = c->next)
7080 {
d80c695f 7081 if (c->here && c->op != EXEC_END_BLOCK)
0615f923 7082 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
0615f923
TS
7083 }
7084
7085 /* Merge with labels from parent block. */
7086 if (cs_base->prev)
7087 {
7088 gcc_assert (cs_base->prev->reachable_labels);
7089 bitmap_ior_into (cs_base->reachable_labels,
7090 cs_base->prev->reachable_labels);
7091 }
7092}
7093
d80c695f 7094/* Given a branch to a label, see if the branch is conforming.
0615f923 7095 The code node describes where the branch is located. */
6de9cd9a
DN
7096
7097static void
edf1eac2 7098resolve_branch (gfc_st_label *label, gfc_code *code)
6de9cd9a 7099{
6de9cd9a 7100 code_stack *stack;
6de9cd9a
DN
7101
7102 if (label == NULL)
7103 return;
6de9cd9a
DN
7104
7105 /* Step one: is this a valid branching target? */
7106
0615f923 7107 if (label->defined == ST_LABEL_UNKNOWN)
6de9cd9a 7108 {
0615f923
TS
7109 gfc_error ("Label %d referenced at %L is never defined", label->value,
7110 &label->where);
6de9cd9a
DN
7111 return;
7112 }
7113
0615f923 7114 if (label->defined != ST_LABEL_TARGET)
6de9cd9a
DN
7115 {
7116 gfc_error ("Statement at %L is not a valid branch target statement "
0615f923 7117 "for the branch statement at %L", &label->where, &code->loc);
6de9cd9a
DN
7118 return;
7119 }
7120
7121 /* Step two: make sure this branch is not a branch to itself ;-) */
7122
7123 if (code->here == label)
7124 {
ab551054 7125 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6de9cd9a
DN
7126 return;
7127 }
7128
0615f923
TS
7129 /* Step three: See if the label is in the same block as the
7130 branching statement. The hard work has been done by setting up
7131 the bitmap reachable_labels. */
6de9cd9a 7132
d80c695f
TS
7133 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7134 return;
6de9cd9a 7135
d80c695f
TS
7136 /* Step four: If we haven't found the label in the bitmap, it may
7137 still be the label of the END of the enclosing block, in which
7138 case we find it by going up the code_stack. */
6de9cd9a 7139
0615f923
TS
7140 for (stack = cs_base; stack; stack = stack->prev)
7141 if (stack->current->next && stack->current->next->here == label)
7142 break;
6de9cd9a 7143
d80c695f 7144 if (stack)
0615f923 7145 {
d80c695f
TS
7146 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7147 return;
6de9cd9a 7148 }
0615f923 7149
d80c695f
TS
7150 /* The label is not in an enclosing block, so illegal. This was
7151 allowed in Fortran 66, so we allow it as extension. No
7152 further checks are necessary in this case. */
7153 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7154 "as the GOTO statement at %L", &label->where,
7155 &code->loc);
7156 return;
6de9cd9a
DN
7157}
7158
7159
7160/* Check whether EXPR1 has the same shape as EXPR2. */
7161
17b1d2a0 7162static gfc_try
6de9cd9a
DN
7163resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7164{
7165 mpz_t shape[GFC_MAX_DIMENSIONS];
7166 mpz_t shape2[GFC_MAX_DIMENSIONS];
17b1d2a0 7167 gfc_try result = FAILURE;
6de9cd9a
DN
7168 int i;
7169
7170 /* Compare the rank. */
7171 if (expr1->rank != expr2->rank)
7172 return result;
7173
7174 /* Compare the size of each dimension. */
7175 for (i=0; i<expr1->rank; i++)
7176 {
7177 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
edf1eac2 7178 goto ignore;
6de9cd9a
DN
7179
7180 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
edf1eac2 7181 goto ignore;
6de9cd9a
DN
7182
7183 if (mpz_cmp (shape[i], shape2[i]))
edf1eac2 7184 goto over;
6de9cd9a
DN
7185 }
7186
7187 /* When either of the two expression is an assumed size array, we
7188 ignore the comparison of dimension sizes. */
7189ignore:
7190 result = SUCCESS;
7191
7192over:
edf1eac2 7193 for (i--; i >= 0; i--)
6de9cd9a
DN
7194 {
7195 mpz_clear (shape[i]);
7196 mpz_clear (shape2[i]);
7197 }
7198 return result;
7199}
7200
7201
7202/* Check whether a WHERE assignment target or a WHERE mask expression
7203 has the same shape as the outmost WHERE mask expression. */
7204
7205static void
7206resolve_where (gfc_code *code, gfc_expr *mask)
7207{
7208 gfc_code *cblock;
7209 gfc_code *cnext;
7210 gfc_expr *e = NULL;
7211
7212 cblock = code->block;
7213
7214 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7215 In case of nested WHERE, only the outmost one is stored. */
7216 if (mask == NULL) /* outmost WHERE */
a513927a 7217 e = cblock->expr1;
6de9cd9a
DN
7218 else /* inner WHERE */
7219 e = mask;
7220
7221 while (cblock)
7222 {
a513927a 7223 if (cblock->expr1)
edf1eac2
SK
7224 {
7225 /* Check if the mask-expr has a consistent shape with the
7226 outmost WHERE mask-expr. */
a513927a 7227 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
edf1eac2 7228 gfc_error ("WHERE mask at %L has inconsistent shape",
a513927a 7229 &cblock->expr1->where);
edf1eac2 7230 }
6de9cd9a
DN
7231
7232 /* the assignment statement of a WHERE statement, or the first
edf1eac2 7233 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
7234 cnext = cblock->next;
7235 while (cnext)
edf1eac2
SK
7236 {
7237 switch (cnext->op)
7238 {
7239 /* WHERE assignment statement */
7240 case EXEC_ASSIGN:
7241
7242 /* Check shape consistent for WHERE assignment target. */
a513927a 7243 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
edf1eac2 7244 gfc_error ("WHERE assignment target at %L has "
a513927a 7245 "inconsistent shape", &cnext->expr1->where);
edf1eac2
SK
7246 break;
7247
a00b8d1a
PT
7248
7249 case EXEC_ASSIGN_CALL:
7250 resolve_call (cnext);
42cd23cb 7251 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 7252 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 7253 &cnext->ext.actual->expr->where);
a00b8d1a
PT
7254 break;
7255
edf1eac2
SK
7256 /* WHERE or WHERE construct is part of a where-body-construct */
7257 case EXEC_WHERE:
7258 resolve_where (cnext, e);
7259 break;
7260
7261 default:
7262 gfc_error ("Unsupported statement inside WHERE at %L",
7263 &cnext->loc);
7264 }
7265 /* the next statement within the same where-body-construct */
7266 cnext = cnext->next;
6de9cd9a
DN
7267 }
7268 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7269 cblock = cblock->block;
7270 }
7271}
7272
7273
6de9cd9a
DN
7274/* Resolve assignment in FORALL construct.
7275 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7276 FORALL index variables. */
7277
7278static void
7279gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7280{
7281 int n;
7282
7283 for (n = 0; n < nvar; n++)
7284 {
7285 gfc_symbol *forall_index;
7286
7287 forall_index = var_expr[n]->symtree->n.sym;
7288
7289 /* Check whether the assignment target is one of the FORALL index
edf1eac2 7290 variable. */
a513927a
SK
7291 if ((code->expr1->expr_type == EXPR_VARIABLE)
7292 && (code->expr1->symtree->n.sym == forall_index))
edf1eac2 7293 gfc_error ("Assignment to a FORALL index variable at %L",
a513927a 7294 &code->expr1->where);
6de9cd9a 7295 else
edf1eac2
SK
7296 {
7297 /* If one of the FORALL index variables doesn't appear in the
67cec813
PT
7298 assignment variable, then there could be a many-to-one
7299 assignment. Emit a warning rather than an error because the
7300 mask could be resolving this problem. */
a513927a 7301 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
67cec813
PT
7302 gfc_warning ("The FORALL with index '%s' is not used on the "
7303 "left side of the assignment at %L and so might "
7304 "cause multiple assignment to this object",
a513927a 7305 var_expr[n]->symtree->name, &code->expr1->where);
edf1eac2 7306 }
6de9cd9a
DN
7307 }
7308}
7309
7310
7311/* Resolve WHERE statement in FORALL construct. */
7312
7313static void
edf1eac2
SK
7314gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7315 gfc_expr **var_expr)
7316{
6de9cd9a
DN
7317 gfc_code *cblock;
7318 gfc_code *cnext;
7319
7320 cblock = code->block;
7321 while (cblock)
7322 {
7323 /* the assignment statement of a WHERE statement, or the first
edf1eac2 7324 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
7325 cnext = cblock->next;
7326 while (cnext)
edf1eac2
SK
7327 {
7328 switch (cnext->op)
7329 {
7330 /* WHERE assignment statement */
7331 case EXEC_ASSIGN:
7332 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7333 break;
a00b8d1a
PT
7334
7335 /* WHERE operator assignment statement */
7336 case EXEC_ASSIGN_CALL:
7337 resolve_call (cnext);
42cd23cb 7338 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 7339 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 7340 &cnext->ext.actual->expr->where);
a00b8d1a 7341 break;
edf1eac2
SK
7342
7343 /* WHERE or WHERE construct is part of a where-body-construct */
7344 case EXEC_WHERE:
7345 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7346 break;
7347
7348 default:
7349 gfc_error ("Unsupported statement inside WHERE at %L",
7350 &cnext->loc);
7351 }
7352 /* the next statement within the same where-body-construct */
7353 cnext = cnext->next;
7354 }
6de9cd9a
DN
7355 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7356 cblock = cblock->block;
7357 }
7358}
7359
7360
7361/* Traverse the FORALL body to check whether the following errors exist:
7362 1. For assignment, check if a many-to-one assignment happens.
7363 2. For WHERE statement, check the WHERE body to see if there is any
7364 many-to-one assignment. */
7365
7366static void
7367gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7368{
7369 gfc_code *c;
7370
7371 c = code->block->next;
7372 while (c)
7373 {
7374 switch (c->op)
edf1eac2
SK
7375 {
7376 case EXEC_ASSIGN:
7377 case EXEC_POINTER_ASSIGN:
7378 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7379 break;
7380
a00b8d1a
PT
7381 case EXEC_ASSIGN_CALL:
7382 resolve_call (c);
7383 break;
7384
edf1eac2
SK
7385 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7386 there is no need to handle it here. */
7387 case EXEC_FORALL:
7388 break;
7389 case EXEC_WHERE:
7390 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7391 break;
7392 default:
7393 break;
7394 }
6de9cd9a
DN
7395 /* The next statement in the FORALL body. */
7396 c = c->next;
7397 }
7398}
7399
7400
0e6834af
MM
7401/* Counts the number of iterators needed inside a forall construct, including
7402 nested forall constructs. This is used to allocate the needed memory
7403 in gfc_resolve_forall. */
7404
7405static int
7406gfc_count_forall_iterators (gfc_code *code)
7407{
7408 int max_iters, sub_iters, current_iters;
7409 gfc_forall_iterator *fa;
7410
7411 gcc_assert(code->op == EXEC_FORALL);
7412 max_iters = 0;
7413 current_iters = 0;
7414
7415 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7416 current_iters ++;
7417
7418 code = code->block->next;
7419
7420 while (code)
7421 {
7422 if (code->op == EXEC_FORALL)
7423 {
7424 sub_iters = gfc_count_forall_iterators (code);
7425 if (sub_iters > max_iters)
7426 max_iters = sub_iters;
7427 }
7428 code = code->next;
7429 }
7430
7431 return current_iters + max_iters;
7432}
7433
7434
6de9cd9a
DN
7435/* Given a FORALL construct, first resolve the FORALL iterator, then call
7436 gfc_resolve_forall_body to resolve the FORALL body. */
7437
6de9cd9a
DN
7438static void
7439gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7440{
7441 static gfc_expr **var_expr;
7442 static int total_var = 0;
7443 static int nvar = 0;
0e6834af 7444 int old_nvar, tmp;
6de9cd9a 7445 gfc_forall_iterator *fa;
6de9cd9a
DN
7446 int i;
7447
0e6834af
MM
7448 old_nvar = nvar;
7449
6de9cd9a
DN
7450 /* Start to resolve a FORALL construct */
7451 if (forall_save == 0)
7452 {
7453 /* Count the total number of FORALL index in the nested FORALL
0e6834af
MM
7454 construct in order to allocate the VAR_EXPR with proper size. */
7455 total_var = gfc_count_forall_iterators (code);
6de9cd9a 7456
f7b529fa 7457 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6de9cd9a
DN
7458 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7459 }
7460
7461 /* The information about FORALL iterator, including FORALL index start, end
7462 and stride. The FORALL index can not appear in start, end or stride. */
7463 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7464 {
7465 /* Check if any outer FORALL index name is the same as the current
edf1eac2 7466 one. */
6de9cd9a 7467 for (i = 0; i < nvar; i++)
edf1eac2
SK
7468 {
7469 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7470 {
7471 gfc_error ("An outer FORALL construct already has an index "
7472 "with this name %L", &fa->var->where);
7473 }
7474 }
6de9cd9a
DN
7475
7476 /* Record the current FORALL index. */
7477 var_expr[nvar] = gfc_copy_expr (fa->var);
7478
6de9cd9a 7479 nvar++;
0e6834af
MM
7480
7481 /* No memory leak. */
7482 gcc_assert (nvar <= total_var);
6de9cd9a
DN
7483 }
7484
7485 /* Resolve the FORALL body. */
7486 gfc_resolve_forall_body (code, nvar, var_expr);
7487
7488 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6c7a4dfd 7489 gfc_resolve_blocks (code->block, ns);
6de9cd9a 7490
0e6834af
MM
7491 tmp = nvar;
7492 nvar = old_nvar;
7493 /* Free only the VAR_EXPRs allocated in this frame. */
7494 for (i = nvar; i < tmp; i++)
7495 gfc_free_expr (var_expr[i]);
6de9cd9a 7496
0e6834af
MM
7497 if (nvar == 0)
7498 {
7499 /* We are in the outermost FORALL construct. */
7500 gcc_assert (forall_save == 0);
7501
7502 /* VAR_EXPR is not needed any more. */
7503 gfc_free (var_expr);
7504 total_var = 0;
7505 }
6de9cd9a
DN
7506}
7507
7508
9abe5e56
DK
7509/* Resolve a BLOCK construct statement. */
7510
7511static void
7512resolve_block_construct (gfc_code* code)
7513{
7514 /* Eventually, we may want to do some checks here or handle special stuff.
7515 But so far the only thing we can do is resolving the local namespace. */
7516
7517 gfc_resolve (code->ext.ns);
7518}
7519
7520
7521/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
6de9cd9a
DN
7522 DO code nodes. */
7523
7524static void resolve_code (gfc_code *, gfc_namespace *);
7525
6c7a4dfd 7526void
edf1eac2 7527gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6de9cd9a 7528{
17b1d2a0 7529 gfc_try t;
6de9cd9a
DN
7530
7531 for (; b; b = b->block)
7532 {
a513927a 7533 t = gfc_resolve_expr (b->expr1);
6de9cd9a
DN
7534 if (gfc_resolve_expr (b->expr2) == FAILURE)
7535 t = FAILURE;
7536
7537 switch (b->op)
7538 {
7539 case EXEC_IF:
a513927a
SK
7540 if (t == SUCCESS && b->expr1 != NULL
7541 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
edf1eac2 7542 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 7543 &b->expr1->where);
6de9cd9a
DN
7544 break;
7545
7546 case EXEC_WHERE:
7547 if (t == SUCCESS
a513927a
SK
7548 && b->expr1 != NULL
7549 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
edf1eac2 7550 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
a513927a 7551 &b->expr1->where);
6de9cd9a
DN
7552 break;
7553
edf1eac2 7554 case EXEC_GOTO:
79bd1948 7555 resolve_branch (b->label1, b);
edf1eac2 7556 break;
6de9cd9a 7557
9abe5e56
DK
7558 case EXEC_BLOCK:
7559 resolve_block_construct (b);
7560 break;
7561
6de9cd9a 7562 case EXEC_SELECT:
cf2b3c22 7563 case EXEC_SELECT_TYPE:
6de9cd9a
DN
7564 case EXEC_FORALL:
7565 case EXEC_DO:
7566 case EXEC_DO_WHILE:
5e805e44
JJ
7567 case EXEC_READ:
7568 case EXEC_WRITE:
7569 case EXEC_IOLENGTH:
6f0f0b2e 7570 case EXEC_WAIT:
6de9cd9a
DN
7571 break;
7572
6c7a4dfd
JJ
7573 case EXEC_OMP_ATOMIC:
7574 case EXEC_OMP_CRITICAL:
7575 case EXEC_OMP_DO:
7576 case EXEC_OMP_MASTER:
7577 case EXEC_OMP_ORDERED:
7578 case EXEC_OMP_PARALLEL:
7579 case EXEC_OMP_PARALLEL_DO:
7580 case EXEC_OMP_PARALLEL_SECTIONS:
7581 case EXEC_OMP_PARALLEL_WORKSHARE:
7582 case EXEC_OMP_SECTIONS:
7583 case EXEC_OMP_SINGLE:
a68ab351
JJ
7584 case EXEC_OMP_TASK:
7585 case EXEC_OMP_TASKWAIT:
6c7a4dfd
JJ
7586 case EXEC_OMP_WORKSHARE:
7587 break;
7588
6de9cd9a 7589 default:
9abe5e56 7590 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
6de9cd9a
DN
7591 }
7592
7593 resolve_code (b->next, ns);
7594 }
7595}
7596
7597
c5422462 7598/* Does everything to resolve an ordinary assignment. Returns true
df2fba9e 7599 if this is an interface assignment. */
c5422462
PT
7600static bool
7601resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7602{
7603 bool rval = false;
7604 gfc_expr *lhs;
7605 gfc_expr *rhs;
7606 int llen = 0;
7607 int rlen = 0;
7608 int n;
7609 gfc_ref *ref;
7610
c5422462
PT
7611 if (gfc_extend_assign (code, ns) == SUCCESS)
7612 {
4a44a72d
DK
7613 gfc_symbol* assign_proc;
7614 gfc_expr** rhsptr;
7615
7616 if (code->op == EXEC_ASSIGN_CALL)
c5422462 7617 {
4a44a72d
DK
7618 lhs = code->ext.actual->expr;
7619 rhsptr = &code->ext.actual->next->expr;
7620 assign_proc = code->symtree->n.sym;
7621 }
7622 else
7623 {
7624 gfc_actual_arglist* args;
7625 gfc_typebound_proc* tbp;
7626
7627 gcc_assert (code->op == EXEC_COMPCALL);
7628
7629 args = code->expr1->value.compcall.actual;
7630 lhs = args->expr;
7631 rhsptr = &args->next->expr;
7632
7633 tbp = code->expr1->value.compcall.tbp;
7634 gcc_assert (!tbp->is_generic);
7635 assign_proc = tbp->u.specific->n.sym;
c5422462
PT
7636 }
7637
7638 /* Make a temporary rhs when there is a default initializer
7639 and rhs is the same symbol as the lhs. */
4a44a72d
DK
7640 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7641 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7642 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7643 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7644 *rhsptr = gfc_get_parentheses (*rhsptr);
c5422462
PT
7645
7646 return true;
7647 }
7648
a513927a 7649 lhs = code->expr1;
c5422462
PT
7650 rhs = code->expr2;
7651
00a4618b
TB
7652 if (rhs->is_boz
7653 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
4a44a72d
DK
7654 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7655 &code->loc) == FAILURE)
00a4618b
TB
7656 return false;
7657
7658 /* Handle the case of a BOZ literal on the RHS. */
7659 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7660 {
4956b1f1 7661 int rc;
00a4618b
TB
7662 if (gfc_option.warn_surprising)
7663 gfc_warning ("BOZ literal at %L is bitwise transferred "
7664 "non-integer symbol '%s'", &code->loc,
7665 lhs->symtree->n.sym->name);
7666
c7abc45c
TB
7667 if (!gfc_convert_boz (rhs, &lhs->ts))
7668 return false;
4956b1f1
TB
7669 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7670 {
7671 if (rc == ARITH_UNDERFLOW)
7672 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7673 ". This check can be disabled with the option "
7674 "-fno-range-check", &rhs->where);
7675 else if (rc == ARITH_OVERFLOW)
7676 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7677 ". This check can be disabled with the option "
7678 "-fno-range-check", &rhs->where);
7679 else if (rc == ARITH_NAN)
7680 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7681 ". This check can be disabled with the option "
7682 "-fno-range-check", &rhs->where);
7683 return false;
7684 }
00a4618b
TB
7685 }
7686
7687
c5422462
PT
7688 if (lhs->ts.type == BT_CHARACTER
7689 && gfc_option.warn_character_truncation)
7690 {
bc21d315
JW
7691 if (lhs->ts.u.cl != NULL
7692 && lhs->ts.u.cl->length != NULL
7693 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7694 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
c5422462
PT
7695
7696 if (rhs->expr_type == EXPR_CONSTANT)
7697 rlen = rhs->value.character.length;
7698
bc21d315 7699 else if (rhs->ts.u.cl != NULL
4a44a72d 7700 && rhs->ts.u.cl->length != NULL
bc21d315
JW
7701 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7702 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
c5422462
PT
7703
7704 if (rlen && llen && rlen > llen)
7705 gfc_warning_now ("CHARACTER expression will be truncated "
7706 "in assignment (%d/%d) at %L",
7707 llen, rlen, &code->loc);
7708 }
7709
7710 /* Ensure that a vector index expression for the lvalue is evaluated
908a2235 7711 to a temporary if the lvalue symbol is referenced in it. */
c5422462
PT
7712 if (lhs->rank)
7713 {
7714 for (ref = lhs->ref; ref; ref= ref->next)
7715 if (ref->type == REF_ARRAY)
7716 {
7717 for (n = 0; n < ref->u.ar.dimen; n++)
908a2235 7718 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
a68ab351
JJ
7719 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7720 ref->u.ar.start[n]))
c5422462
PT
7721 ref->u.ar.start[n]
7722 = gfc_get_parentheses (ref->u.ar.start[n]);
7723 }
7724 }
7725
7726 if (gfc_pure (NULL))
7727 {
7728 if (gfc_impure_variable (lhs->symtree->n.sym))
7729 {
7730 gfc_error ("Cannot assign to variable '%s' in PURE "
7731 "procedure at %L",
7732 lhs->symtree->n.sym->name,
7733 &lhs->where);
7734 return rval;
7735 }
7736
7737 if (lhs->ts.type == BT_DERIVED
7738 && lhs->expr_type == EXPR_VARIABLE
bc21d315 7739 && lhs->ts.u.derived->attr.pointer_comp
c5422462
PT
7740 && gfc_impure_variable (rhs->symtree->n.sym))
7741 {
7742 gfc_error ("The impure variable at %L is assigned to "
7743 "a derived type variable with a POINTER "
7744 "component in a PURE procedure (12.6)",
7745 &rhs->where);
7746 return rval;
7747 }
7748 }
7749
0ae278e7
JW
7750 /* F03:7.4.1.2. */
7751 if (lhs->ts.type == BT_CLASS)
7752 {
7753 gfc_error ("Variable must not be polymorphic in assignment at %L",
7754 &lhs->where);
7755 return false;
7756 }
7757
c5422462
PT
7758 gfc_check_assign (lhs, rhs, 1);
7759 return false;
7760}
7761
9abe5e56 7762
6de9cd9a
DN
7763/* Given a block of code, recursively resolve everything pointed to by this
7764 code block. */
7765
7766static void
edf1eac2 7767resolve_code (gfc_code *code, gfc_namespace *ns)
6de9cd9a 7768{
6c7a4dfd 7769 int omp_workshare_save;
d68bd5a8 7770 int forall_save;
6de9cd9a 7771 code_stack frame;
17b1d2a0 7772 gfc_try t;
6de9cd9a
DN
7773
7774 frame.prev = cs_base;
7775 frame.head = code;
7776 cs_base = &frame;
7777
d80c695f 7778 find_reachable_labels (code);
0615f923 7779
6de9cd9a
DN
7780 for (; code; code = code->next)
7781 {
7782 frame.current = code;
d68bd5a8 7783 forall_save = forall_flag;
6de9cd9a
DN
7784
7785 if (code->op == EXEC_FORALL)
7786 {
6de9cd9a 7787 forall_flag = 1;
6c7a4dfd 7788 gfc_resolve_forall (code, ns, forall_save);
d68bd5a8 7789 forall_flag = 2;
6c7a4dfd
JJ
7790 }
7791 else if (code->block)
7792 {
7793 omp_workshare_save = -1;
7794 switch (code->op)
7795 {
7796 case EXEC_OMP_PARALLEL_WORKSHARE:
7797 omp_workshare_save = omp_workshare_flag;
7798 omp_workshare_flag = 1;
7799 gfc_resolve_omp_parallel_blocks (code, ns);
7800 break;
7801 case EXEC_OMP_PARALLEL:
7802 case EXEC_OMP_PARALLEL_DO:
7803 case EXEC_OMP_PARALLEL_SECTIONS:
a68ab351 7804 case EXEC_OMP_TASK:
6c7a4dfd
JJ
7805 omp_workshare_save = omp_workshare_flag;
7806 omp_workshare_flag = 0;
7807 gfc_resolve_omp_parallel_blocks (code, ns);
7808 break;
7809 case EXEC_OMP_DO:
7810 gfc_resolve_omp_do_blocks (code, ns);
7811 break;
7812 case EXEC_OMP_WORKSHARE:
7813 omp_workshare_save = omp_workshare_flag;
7814 omp_workshare_flag = 1;
7815 /* FALLTHROUGH */
7816 default:
7817 gfc_resolve_blocks (code->block, ns);
7818 break;
7819 }
6de9cd9a 7820
6c7a4dfd
JJ
7821 if (omp_workshare_save != -1)
7822 omp_workshare_flag = omp_workshare_save;
7823 }
6de9cd9a 7824
8e1f752a 7825 t = SUCCESS;
713485cc 7826 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
a513927a 7827 t = gfc_resolve_expr (code->expr1);
d68bd5a8
PT
7828 forall_flag = forall_save;
7829
6de9cd9a
DN
7830 if (gfc_resolve_expr (code->expr2) == FAILURE)
7831 t = FAILURE;
7832
8460475b
JW
7833 if (code->op == EXEC_ALLOCATE
7834 && gfc_resolve_expr (code->expr3) == FAILURE)
7835 t = FAILURE;
7836
6de9cd9a
DN
7837 switch (code->op)
7838 {
7839 case EXEC_NOP:
d80c695f 7840 case EXEC_END_BLOCK:
6de9cd9a 7841 case EXEC_CYCLE:
6de9cd9a
DN
7842 case EXEC_PAUSE:
7843 case EXEC_STOP:
7844 case EXEC_EXIT:
7845 case EXEC_CONTINUE:
7846 case EXEC_DT_END:
4a44a72d 7847 case EXEC_ASSIGN_CALL:
0e9a445b
PT
7848 break;
7849
3d79abbd 7850 case EXEC_ENTRY:
0e9a445b
PT
7851 /* Keep track of which entry we are up to. */
7852 current_entry_id = code->ext.entry->id;
6de9cd9a
DN
7853 break;
7854
7855 case EXEC_WHERE:
7856 resolve_where (code, NULL);
7857 break;
7858
7859 case EXEC_GOTO:
a513927a 7860 if (code->expr1 != NULL)
ce2df7c6 7861 {
a513927a 7862 if (code->expr1->ts.type != BT_INTEGER)
edf1eac2 7863 gfc_error ("ASSIGNED GOTO statement at %L requires an "
a513927a
SK
7864 "INTEGER variable", &code->expr1->where);
7865 else if (code->expr1->symtree->n.sym->attr.assign != 1)
edf1eac2 7866 gfc_error ("Variable '%s' has not been assigned a target "
a513927a
SK
7867 "label at %L", code->expr1->symtree->n.sym->name,
7868 &code->expr1->where);
ce2df7c6
FW
7869 }
7870 else
79bd1948 7871 resolve_branch (code->label1, code);
6de9cd9a
DN
7872 break;
7873
7874 case EXEC_RETURN:
a513927a
SK
7875 if (code->expr1 != NULL
7876 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
b6398823 7877 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
a513927a 7878 "INTEGER return specifier", &code->expr1->where);
6de9cd9a
DN
7879 break;
7880
6b591ec0 7881 case EXEC_INIT_ASSIGN:
5c71a5e0 7882 case EXEC_END_PROCEDURE:
6b591ec0
PT
7883 break;
7884
6de9cd9a
DN
7885 case EXEC_ASSIGN:
7886 if (t == FAILURE)
7887 break;
7888
c5422462 7889 if (resolve_ordinary_assign (code, ns))
664e411b
JW
7890 {
7891 if (code->op == EXEC_COMPCALL)
7892 goto compcall;
7893 else
7894 goto call;
7895 }
6de9cd9a
DN
7896 break;
7897
7898 case EXEC_LABEL_ASSIGN:
79bd1948 7899 if (code->label1->defined == ST_LABEL_UNKNOWN)
edf1eac2 7900 gfc_error ("Label %d referenced at %L is never defined",
79bd1948 7901 code->label1->value, &code->label1->where);
edf1eac2 7902 if (t == SUCCESS
a513927a
SK
7903 && (code->expr1->expr_type != EXPR_VARIABLE
7904 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
7905 || code->expr1->symtree->n.sym->ts.kind
edf1eac2 7906 != gfc_default_integer_kind
a513927a 7907 || code->expr1->symtree->n.sym->as != NULL))
40f2165e 7908 gfc_error ("ASSIGN statement at %L requires a scalar "
a513927a 7909 "default INTEGER variable", &code->expr1->where);
6de9cd9a
DN
7910 break;
7911
7912 case EXEC_POINTER_ASSIGN:
7913 if (t == FAILURE)
7914 break;
7915
93d76687 7916 gfc_check_pointer_assign (code->expr1, code->expr2);
6de9cd9a
DN
7917 break;
7918
7919 case EXEC_ARITHMETIC_IF:
7920 if (t == SUCCESS
a513927a
SK
7921 && code->expr1->ts.type != BT_INTEGER
7922 && code->expr1->ts.type != BT_REAL)
6de9cd9a 7923 gfc_error ("Arithmetic IF statement at %L requires a numeric "
a513927a 7924 "expression", &code->expr1->where);
6de9cd9a 7925
79bd1948 7926 resolve_branch (code->label1, code);
6de9cd9a
DN
7927 resolve_branch (code->label2, code);
7928 resolve_branch (code->label3, code);
7929 break;
7930
7931 case EXEC_IF:
a513927a
SK
7932 if (t == SUCCESS && code->expr1 != NULL
7933 && (code->expr1->ts.type != BT_LOGICAL
7934 || code->expr1->rank != 0))
6de9cd9a 7935 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 7936 &code->expr1->where);
6de9cd9a
DN
7937 break;
7938
7939 case EXEC_CALL:
7940 call:
7941 resolve_call (code);
7942 break;
7943
8e1f752a 7944 case EXEC_COMPCALL:
664e411b 7945 compcall:
7cf078dc
PT
7946 if (code->expr1->symtree
7947 && code->expr1->symtree->n.sym->ts.type == BT_CLASS)
7948 resolve_class_typebound_call (code);
7949 else
7950 resolve_typebound_call (code);
8e1f752a
DK
7951 break;
7952
713485cc 7953 case EXEC_CALL_PPC:
9abe5e56 7954 resolve_ppc_call (code);
713485cc
JW
7955 break;
7956
6de9cd9a
DN
7957 case EXEC_SELECT:
7958 /* Select is complicated. Also, a SELECT construct could be
7959 a transformed computed GOTO. */
7960 resolve_select (code);
7961 break;
7962
cf2b3c22
TB
7963 case EXEC_SELECT_TYPE:
7964 resolve_select_type (code);
7965 break;
7966
9abe5e56
DK
7967 case EXEC_BLOCK:
7968 gfc_resolve (code->ext.ns);
7969 break;
7970
6de9cd9a
DN
7971 case EXEC_DO:
7972 if (code->ext.iterator != NULL)
6c7a4dfd
JJ
7973 {
7974 gfc_iterator *iter = code->ext.iterator;
7975 if (gfc_resolve_iterator (iter, true) != FAILURE)
7976 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
7977 }
6de9cd9a
DN
7978 break;
7979
7980 case EXEC_DO_WHILE:
a513927a 7981 if (code->expr1 == NULL)
6de9cd9a
DN
7982 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
7983 if (t == SUCCESS
a513927a
SK
7984 && (code->expr1->rank != 0
7985 || code->expr1->ts.type != BT_LOGICAL))
6de9cd9a 7986 gfc_error ("Exit condition of DO WHILE loop at %L must be "
a513927a 7987 "a scalar LOGICAL expression", &code->expr1->where);
6de9cd9a
DN
7988 break;
7989
7990 case EXEC_ALLOCATE:
b9332b09
PT
7991 if (t == SUCCESS)
7992 resolve_allocate_deallocate (code, "ALLOCATE");
6de9cd9a
DN
7993
7994 break;
7995
7996 case EXEC_DEALLOCATE:
b9332b09
PT
7997 if (t == SUCCESS)
7998 resolve_allocate_deallocate (code, "DEALLOCATE");
6de9cd9a
DN
7999
8000 break;
8001
8002 case EXEC_OPEN:
8003 if (gfc_resolve_open (code->ext.open) == FAILURE)
8004 break;
8005
8006 resolve_branch (code->ext.open->err, code);
8007 break;
8008
8009 case EXEC_CLOSE:
8010 if (gfc_resolve_close (code->ext.close) == FAILURE)
8011 break;
8012
8013 resolve_branch (code->ext.close->err, code);
8014 break;
8015
8016 case EXEC_BACKSPACE:
8017 case EXEC_ENDFILE:
8018 case EXEC_REWIND:
6403ec5f 8019 case EXEC_FLUSH:
6de9cd9a
DN
8020 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8021 break;
8022
8023 resolve_branch (code->ext.filepos->err, code);
8024 break;
8025
8026 case EXEC_INQUIRE:
8750f9cd
JB
8027 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8028 break;
8029
8030 resolve_branch (code->ext.inquire->err, code);
8031 break;
8032
8033 case EXEC_IOLENGTH:
6e45f57b 8034 gcc_assert (code->ext.inquire != NULL);
6de9cd9a
DN
8035 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8036 break;
8037
8038 resolve_branch (code->ext.inquire->err, code);
8039 break;
8040
6f0f0b2e
JD
8041 case EXEC_WAIT:
8042 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8043 break;
8044
8045 resolve_branch (code->ext.wait->err, code);
8046 resolve_branch (code->ext.wait->end, code);
8047 resolve_branch (code->ext.wait->eor, code);
8048 break;
8049
6de9cd9a
DN
8050 case EXEC_READ:
8051 case EXEC_WRITE:
88e18fed 8052 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
6de9cd9a
DN
8053 break;
8054
8055 resolve_branch (code->ext.dt->err, code);
8056 resolve_branch (code->ext.dt->end, code);
8057 resolve_branch (code->ext.dt->eor, code);
8058 break;
8059
0e6928d8
TS
8060 case EXEC_TRANSFER:
8061 resolve_transfer (code);
8062 break;
8063
6de9cd9a
DN
8064 case EXEC_FORALL:
8065 resolve_forall_iterators (code->ext.forall_iterator);
8066
a513927a 8067 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
edf1eac2 8068 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
a513927a 8069 "expression", &code->expr1->where);
6de9cd9a
DN
8070 break;
8071
6c7a4dfd
JJ
8072 case EXEC_OMP_ATOMIC:
8073 case EXEC_OMP_BARRIER:
8074 case EXEC_OMP_CRITICAL:
8075 case EXEC_OMP_FLUSH:
8076 case EXEC_OMP_DO:
8077 case EXEC_OMP_MASTER:
8078 case EXEC_OMP_ORDERED:
8079 case EXEC_OMP_SECTIONS:
8080 case EXEC_OMP_SINGLE:
a68ab351 8081 case EXEC_OMP_TASKWAIT:
6c7a4dfd
JJ
8082 case EXEC_OMP_WORKSHARE:
8083 gfc_resolve_omp_directive (code, ns);
8084 break;
8085
8086 case EXEC_OMP_PARALLEL:
8087 case EXEC_OMP_PARALLEL_DO:
8088 case EXEC_OMP_PARALLEL_SECTIONS:
8089 case EXEC_OMP_PARALLEL_WORKSHARE:
a68ab351 8090 case EXEC_OMP_TASK:
6c7a4dfd
JJ
8091 omp_workshare_save = omp_workshare_flag;
8092 omp_workshare_flag = 0;
8093 gfc_resolve_omp_directive (code, ns);
8094 omp_workshare_flag = omp_workshare_save;
8095 break;
8096
6de9cd9a
DN
8097 default:
8098 gfc_internal_error ("resolve_code(): Bad statement code");
8099 }
8100 }
8101
8102 cs_base = frame.prev;
8103}
8104
8105
8106/* Resolve initial values and make sure they are compatible with
8107 the variable. */
8108
8109static void
edf1eac2 8110resolve_values (gfc_symbol *sym)
6de9cd9a 8111{
6de9cd9a
DN
8112 if (sym->value == NULL)
8113 return;
8114
8115 if (gfc_resolve_expr (sym->value) == FAILURE)
8116 return;
8117
8118 gfc_check_assign_symbol (sym, sym->value);
8119}
8120
8121
a8b3b0b6
CR
8122/* Verify the binding labels for common blocks that are BIND(C). The label
8123 for a BIND(C) common block must be identical in all scoping units in which
8124 the common block is declared. Further, the binding label can not collide
8125 with any other global entity in the program. */
8126
8127static void
8128resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8129{
8130 if (comm_block_tree->n.common->is_bind_c == 1)
8131 {
8132 gfc_gsymbol *binding_label_gsym;
8133 gfc_gsymbol *comm_name_gsym;
8134
8135 /* See if a global symbol exists by the common block's name. It may
8136 be NULL if the common block is use-associated. */
8137 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8138 comm_block_tree->n.common->name);
8139 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8140 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8141 "with the global entity '%s' at %L",
8142 comm_block_tree->n.common->binding_label,
8143 comm_block_tree->n.common->name,
8144 &(comm_block_tree->n.common->where),
8145 comm_name_gsym->name, &(comm_name_gsym->where));
8146 else if (comm_name_gsym != NULL
8147 && strcmp (comm_name_gsym->name,
8148 comm_block_tree->n.common->name) == 0)
8149 {
8150 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8151 as expected. */
8152 if (comm_name_gsym->binding_label == NULL)
8153 /* No binding label for common block stored yet; save this one. */
8154 comm_name_gsym->binding_label =
8155 comm_block_tree->n.common->binding_label;
8156 else
8157 if (strcmp (comm_name_gsym->binding_label,
8158 comm_block_tree->n.common->binding_label) != 0)
8159 {
8160 /* Common block names match but binding labels do not. */
8161 gfc_error ("Binding label '%s' for common block '%s' at %L "
8162 "does not match the binding label '%s' for common "
8163 "block '%s' at %L",
8164 comm_block_tree->n.common->binding_label,
8165 comm_block_tree->n.common->name,
8166 &(comm_block_tree->n.common->where),
8167 comm_name_gsym->binding_label,
8168 comm_name_gsym->name,
8169 &(comm_name_gsym->where));
8170 return;
8171 }
8172 }
8173
8174 /* There is no binding label (NAME="") so we have nothing further to
8175 check and nothing to add as a global symbol for the label. */
8176 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8177 return;
8178
8179 binding_label_gsym =
8180 gfc_find_gsymbol (gfc_gsym_root,
8181 comm_block_tree->n.common->binding_label);
8182 if (binding_label_gsym == NULL)
8183 {
8184 /* Need to make a global symbol for the binding label to prevent
8185 it from colliding with another. */
8186 binding_label_gsym =
8187 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8188 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8189 binding_label_gsym->type = GSYM_COMMON;
8190 }
8191 else
8192 {
8193 /* If comm_name_gsym is NULL, the name common block is use
8194 associated and the name could be colliding. */
8195 if (binding_label_gsym->type != GSYM_COMMON)
8196 gfc_error ("Binding label '%s' for common block '%s' at %L "
8197 "collides with the global entity '%s' at %L",
8198 comm_block_tree->n.common->binding_label,
8199 comm_block_tree->n.common->name,
8200 &(comm_block_tree->n.common->where),
8201 binding_label_gsym->name,
8202 &(binding_label_gsym->where));
8203 else if (comm_name_gsym != NULL
8204 && (strcmp (binding_label_gsym->name,
8205 comm_name_gsym->binding_label) != 0)
8206 && (strcmp (binding_label_gsym->sym_name,
8207 comm_name_gsym->name) != 0))
8208 gfc_error ("Binding label '%s' for common block '%s' at %L "
8209 "collides with global entity '%s' at %L",
8210 binding_label_gsym->name, binding_label_gsym->sym_name,
8211 &(comm_block_tree->n.common->where),
8212 comm_name_gsym->name, &(comm_name_gsym->where));
8213 }
8214 }
8215
8216 return;
8217}
8218
8219
8220/* Verify any BIND(C) derived types in the namespace so we can report errors
8221 for them once, rather than for each variable declared of that type. */
8222
8223static void
8224resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8225{
8226 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8227 && derived_sym->attr.is_bind_c == 1)
8228 verify_bind_c_derived_type (derived_sym);
8229
8230 return;
8231}
8232
8233
8234/* Verify that any binding labels used in a given namespace do not collide
8235 with the names or binding labels of any global symbols. */
8236
8237static void
8238gfc_verify_binding_labels (gfc_symbol *sym)
8239{
8240 int has_error = 0;
8241
8242 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8243 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8244 {
8245 gfc_gsymbol *bind_c_sym;
8246
8247 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8248 if (bind_c_sym != NULL
8249 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8250 {
8251 if (sym->attr.if_source == IFSRC_DECL
8252 && (bind_c_sym->type != GSYM_SUBROUTINE
8253 && bind_c_sym->type != GSYM_FUNCTION)
8254 && ((sym->attr.contained == 1
8255 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8256 || (sym->attr.use_assoc == 1
8257 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8258 {
8259 /* Make sure global procedures don't collide with anything. */
8260 gfc_error ("Binding label '%s' at %L collides with the global "
8261 "entity '%s' at %L", sym->binding_label,
8262 &(sym->declared_at), bind_c_sym->name,
8263 &(bind_c_sym->where));
8264 has_error = 1;
8265 }
8266 else if (sym->attr.contained == 0
8267 && (sym->attr.if_source == IFSRC_IFBODY
8268 && sym->attr.flavor == FL_PROCEDURE)
8269 && (bind_c_sym->sym_name != NULL
8270 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8271 {
8272 /* Make sure procedures in interface bodies don't collide. */
8273 gfc_error ("Binding label '%s' in interface body at %L collides "
8274 "with the global entity '%s' at %L",
8275 sym->binding_label,
8276 &(sym->declared_at), bind_c_sym->name,
8277 &(bind_c_sym->where));
8278 has_error = 1;
8279 }
8280 else if (sym->attr.contained == 0
e7bff0d1
TB
8281 && sym->attr.if_source == IFSRC_UNKNOWN)
8282 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8283 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8284 || sym->attr.use_assoc == 0)
a8b3b0b6
CR
8285 {
8286 gfc_error ("Binding label '%s' at %L collides with global "
8287 "entity '%s' at %L", sym->binding_label,
8288 &(sym->declared_at), bind_c_sym->name,
8289 &(bind_c_sym->where));
8290 has_error = 1;
8291 }
8292
8293 if (has_error != 0)
8294 /* Clear the binding label to prevent checking multiple times. */
8295 sym->binding_label[0] = '\0';
8296 }
8297 else if (bind_c_sym == NULL)
8298 {
8299 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8300 bind_c_sym->where = sym->declared_at;
8301 bind_c_sym->sym_name = sym->name;
8302
8303 if (sym->attr.use_assoc == 1)
8304 bind_c_sym->mod_name = sym->module;
8305 else
8306 if (sym->ns->proc_name != NULL)
8307 bind_c_sym->mod_name = sym->ns->proc_name->name;
8308
8309 if (sym->attr.contained == 0)
8310 {
8311 if (sym->attr.subroutine)
8312 bind_c_sym->type = GSYM_SUBROUTINE;
8313 else if (sym->attr.function)
8314 bind_c_sym->type = GSYM_FUNCTION;
8315 }
8316 }
8317 }
8318 return;
8319}
8320
8321
2ed8d224
PT
8322/* Resolve an index expression. */
8323
17b1d2a0 8324static gfc_try
edf1eac2 8325resolve_index_expr (gfc_expr *e)
2ed8d224 8326{
2ed8d224
PT
8327 if (gfc_resolve_expr (e) == FAILURE)
8328 return FAILURE;
8329
8330 if (gfc_simplify_expr (e, 0) == FAILURE)
8331 return FAILURE;
8332
8333 if (gfc_specification_expr (e) == FAILURE)
8334 return FAILURE;
8335
8336 return SUCCESS;
8337}
8338
110eec24
TS
8339/* Resolve a charlen structure. */
8340
17b1d2a0 8341static gfc_try
110eec24
TS
8342resolve_charlen (gfc_charlen *cl)
8343{
b0c06816 8344 int i, k;
5cd09fac 8345
110eec24
TS
8346 if (cl->resolved)
8347 return SUCCESS;
8348
8349 cl->resolved = 1;
8350
0e9a445b
PT
8351 specification_expr = 1;
8352
2ed8d224 8353 if (resolve_index_expr (cl->length) == FAILURE)
0e9a445b
PT
8354 {
8355 specification_expr = 0;
8356 return FAILURE;
8357 }
110eec24 8358
5cd09fac
TS
8359 /* "If the character length parameter value evaluates to a negative
8360 value, the length of character entities declared is zero." */
815cd406 8361 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
5cd09fac
TS
8362 {
8363 gfc_warning_now ("CHARACTER variable has zero length at %L",
8364 &cl->length->where);
8365 gfc_replace_expr (cl->length, gfc_int_expr (0));
8366 }
8367
b0c06816
FXC
8368 /* Check that the character length is not too large. */
8369 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8370 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8371 && cl->length->ts.type == BT_INTEGER
8372 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8373 {
8374 gfc_error ("String length at %L is too large", &cl->length->where);
8375 return FAILURE;
8376 }
8377
2ed8d224
PT
8378 return SUCCESS;
8379}
8380
8381
66e4ab31 8382/* Test for non-constant shape arrays. */
3e1cf500
PT
8383
8384static bool
8385is_non_constant_shape_array (gfc_symbol *sym)
8386{
8387 gfc_expr *e;
8388 int i;
0e9a445b 8389 bool not_constant;
3e1cf500 8390
0e9a445b 8391 not_constant = false;
3e1cf500
PT
8392 if (sym->as != NULL)
8393 {
8394 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8395 has not been simplified; parameter array references. Do the
8396 simplification now. */
8397 for (i = 0; i < sym->as->rank; i++)
8398 {
8399 e = sym->as->lower[i];
8400 if (e && (resolve_index_expr (e) == FAILURE
edf1eac2 8401 || !gfc_is_constant_expr (e)))
0e9a445b 8402 not_constant = true;
3e1cf500
PT
8403
8404 e = sym->as->upper[i];
8405 if (e && (resolve_index_expr (e) == FAILURE
edf1eac2 8406 || !gfc_is_constant_expr (e)))
0e9a445b 8407 not_constant = true;
3e1cf500
PT
8408 }
8409 }
0e9a445b 8410 return not_constant;
3e1cf500
PT
8411}
8412
51b09ce3
AL
8413/* Given a symbol and an initialization expression, add code to initialize
8414 the symbol to the function entry. */
6b591ec0 8415static void
51b09ce3 8416build_init_assign (gfc_symbol *sym, gfc_expr *init)
6b591ec0
PT
8417{
8418 gfc_expr *lval;
6b591ec0
PT
8419 gfc_code *init_st;
8420 gfc_namespace *ns = sym->ns;
8421
6b591ec0
PT
8422 /* Search for the function namespace if this is a contained
8423 function without an explicit result. */
8424 if (sym->attr.function && sym == sym->result
edf1eac2 8425 && sym->name != sym->ns->proc_name->name)
6b591ec0
PT
8426 {
8427 ns = ns->contained;
8428 for (;ns; ns = ns->sibling)
8429 if (strcmp (ns->proc_name->name, sym->name) == 0)
8430 break;
8431 }
8432
8433 if (ns == NULL)
8434 {
8435 gfc_free_expr (init);
8436 return;
8437 }
8438
8439 /* Build an l-value expression for the result. */
08113c73 8440 lval = gfc_lval_expr_from_sym (sym);
6b591ec0
PT
8441
8442 /* Add the code at scope entry. */
8443 init_st = gfc_get_code ();
8444 init_st->next = ns->code;
8445 ns->code = init_st;
8446
8447 /* Assign the default initializer to the l-value. */
8448 init_st->loc = sym->declared_at;
8449 init_st->op = EXEC_INIT_ASSIGN;
a513927a 8450 init_st->expr1 = lval;
6b591ec0
PT
8451 init_st->expr2 = init;
8452}
8453
51b09ce3
AL
8454/* Assign the default initializer to a derived type variable or result. */
8455
8456static void
8457apply_default_init (gfc_symbol *sym)
8458{
8459 gfc_expr *init = NULL;
8460
8461 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8462 return;
8463
bc21d315 8464 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
51b09ce3
AL
8465 init = gfc_default_initializer (&sym->ts);
8466
8467 if (init == NULL)
8468 return;
8469
8470 build_init_assign (sym, init);
8471}
8472
8473/* Build an initializer for a local integer, real, complex, logical, or
8474 character variable, based on the command line flags finit-local-zero,
8475 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8476 null if the symbol should not have a default initialization. */
8477static gfc_expr *
8478build_default_init_expr (gfc_symbol *sym)
8479{
8480 int char_len;
8481 gfc_expr *init_expr;
8482 int i;
51b09ce3
AL
8483
8484 /* These symbols should never have a default initialization. */
8485 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8486 || sym->attr.external
8487 || sym->attr.dummy
8488 || sym->attr.pointer
8489 || sym->attr.in_equivalence
8490 || sym->attr.in_common
8491 || sym->attr.data
8492 || sym->module
8493 || sym->attr.cray_pointee
8494 || sym->attr.cray_pointer)
8495 return NULL;
8496
8497 /* Now we'll try to build an initializer expression. */
8498 init_expr = gfc_get_expr ();
8499 init_expr->expr_type = EXPR_CONSTANT;
8500 init_expr->ts.type = sym->ts.type;
8501 init_expr->ts.kind = sym->ts.kind;
8502 init_expr->where = sym->declared_at;
8503
8504 /* We will only initialize integers, reals, complex, logicals, and
8505 characters, and only if the corresponding command-line flags
8506 were set. Otherwise, we free init_expr and return null. */
8507 switch (sym->ts.type)
8508 {
8509 case BT_INTEGER:
8510 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8511 mpz_init_set_si (init_expr->value.integer,
8512 gfc_option.flag_init_integer_value);
8513 else
8514 {
8515 gfc_free_expr (init_expr);
8516 init_expr = NULL;
8517 }
8518 break;
8519
8520 case BT_REAL:
8521 mpfr_init (init_expr->value.real);
8522 switch (gfc_option.flag_init_real)
8523 {
346a77d1
TB
8524 case GFC_INIT_REAL_SNAN:
8525 init_expr->is_snan = 1;
8526 /* Fall through. */
51b09ce3
AL
8527 case GFC_INIT_REAL_NAN:
8528 mpfr_set_nan (init_expr->value.real);
8529 break;
8530
8531 case GFC_INIT_REAL_INF:
8532 mpfr_set_inf (init_expr->value.real, 1);
8533 break;
8534
8535 case GFC_INIT_REAL_NEG_INF:
8536 mpfr_set_inf (init_expr->value.real, -1);
8537 break;
8538
8539 case GFC_INIT_REAL_ZERO:
8540 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8541 break;
8542
8543 default:
8544 gfc_free_expr (init_expr);
8545 init_expr = NULL;
8546 break;
8547 }
8548 break;
8549
8550 case BT_COMPLEX:
eb6f9a86
KG
8551#ifdef HAVE_mpc
8552 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
8553#else
51b09ce3
AL
8554 mpfr_init (init_expr->value.complex.r);
8555 mpfr_init (init_expr->value.complex.i);
eb6f9a86 8556#endif
51b09ce3
AL
8557 switch (gfc_option.flag_init_real)
8558 {
346a77d1
TB
8559 case GFC_INIT_REAL_SNAN:
8560 init_expr->is_snan = 1;
8561 /* Fall through. */
51b09ce3 8562 case GFC_INIT_REAL_NAN:
eb6f9a86
KG
8563 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8564 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
51b09ce3
AL
8565 break;
8566
8567 case GFC_INIT_REAL_INF:
eb6f9a86
KG
8568 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8569 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
51b09ce3
AL
8570 break;
8571
8572 case GFC_INIT_REAL_NEG_INF:
eb6f9a86
KG
8573 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8574 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
51b09ce3
AL
8575 break;
8576
8577 case GFC_INIT_REAL_ZERO:
eb6f9a86
KG
8578#ifdef HAVE_mpc
8579 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
8580#else
51b09ce3
AL
8581 mpfr_set_ui (init_expr->value.complex.r, 0.0, GFC_RND_MODE);
8582 mpfr_set_ui (init_expr->value.complex.i, 0.0, GFC_RND_MODE);
eb6f9a86 8583#endif
51b09ce3
AL
8584 break;
8585
8586 default:
8587 gfc_free_expr (init_expr);
8588 init_expr = NULL;
8589 break;
8590 }
8591 break;
8592
8593 case BT_LOGICAL:
8594 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8595 init_expr->value.logical = 0;
8596 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8597 init_expr->value.logical = 1;
8598 else
8599 {
8600 gfc_free_expr (init_expr);
8601 init_expr = NULL;
8602 }
8603 break;
8604
8605 case BT_CHARACTER:
8606 /* For characters, the length must be constant in order to
8607 create a default initializer. */
8608 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
bc21d315
JW
8609 && sym->ts.u.cl->length
8610 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
51b09ce3 8611 {
bc21d315 8612 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
51b09ce3 8613 init_expr->value.character.length = char_len;
00660189 8614 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
51b09ce3 8615 for (i = 0; i < char_len; i++)
00660189
FXC
8616 init_expr->value.character.string[i]
8617 = (unsigned char) gfc_option.flag_init_character_value;
51b09ce3
AL
8618 }
8619 else
8620 {
8621 gfc_free_expr (init_expr);
8622 init_expr = NULL;
8623 }
8624 break;
8625
8626 default:
8627 gfc_free_expr (init_expr);
8628 init_expr = NULL;
8629 }
8630 return init_expr;
8631}
8632
8633/* Add an initialization expression to a local variable. */
8634static void
8635apply_default_init_local (gfc_symbol *sym)
8636{
8637 gfc_expr *init = NULL;
8638
8639 /* The symbol should be a variable or a function return value. */
8640 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8641 || (sym->attr.function && sym->result != sym))
8642 return;
8643
8644 /* Try to build the initializer expression. If we can't initialize
8645 this symbol, then init will be NULL. */
8646 init = build_default_init_expr (sym);
8647 if (init == NULL)
8648 return;
8649
8650 /* For saved variables, we don't want to add an initializer at
8651 function entry, so we just add a static initializer. */
0e8bc11d
JB
8652 if (sym->attr.save || sym->ns->save_all
8653 || gfc_option.flag_max_stack_var_size == 0)
51b09ce3
AL
8654 {
8655 /* Don't clobber an existing initializer! */
8656 gcc_assert (sym->value == NULL);
8657 sym->value = init;
8658 return;
8659 }
8660
8661 build_init_assign (sym, init);
8662}
6b591ec0 8663
66e4ab31 8664/* Resolution of common features of flavors variable and procedure. */
2ed8d224 8665
17b1d2a0 8666static gfc_try
2ed8d224
PT
8667resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8668{
8669 /* Constraints on deferred shape variable. */
8670 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8671 {
8672 if (sym->attr.allocatable)
8673 {
8674 if (sym->attr.dimension)
2fbd4117
JW
8675 {
8676 gfc_error ("Allocatable array '%s' at %L must have "
8677 "a deferred shape", sym->name, &sym->declared_at);
8678 return FAILURE;
8679 }
8680 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8681 "may not be ALLOCATABLE", sym->name,
8682 &sym->declared_at) == FAILURE)
2ed8d224
PT
8683 return FAILURE;
8684 }
8685
8686 if (sym->attr.pointer && sym->attr.dimension)
8687 {
8688 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8689 sym->name, &sym->declared_at);
8690 return FAILURE;
8691 }
8692
8693 }
8694 else
8695 {
cf2b3c22
TB
8696 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8697 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
2ed8d224
PT
8698 {
8699 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8700 sym->name, &sym->declared_at);
8701 return FAILURE;
8702 }
8703 }
8704 return SUCCESS;
8705}
8706
edf1eac2 8707
448d2cd2
TS
8708/* Additional checks for symbols with flavor variable and derived
8709 type. To be called from resolve_fl_variable. */
8710
17b1d2a0 8711static gfc_try
9de88093 8712resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
448d2cd2 8713{
cf2b3c22 8714 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
448d2cd2
TS
8715
8716 /* Check to see if a derived type is blocked from being host
8717 associated by the presence of another class I symbol in the same
8718 namespace. 14.6.1.3 of the standard and the discussion on
8719 comp.lang.fortran. */
bc21d315 8720 if (sym->ns != sym->ts.u.derived->ns
448d2cd2
TS
8721 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8722 {
8723 gfc_symbol *s;
bc21d315 8724 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
334e912a 8725 if (s && s->attr.flavor != FL_DERIVED)
448d2cd2
TS
8726 {
8727 gfc_error ("The type '%s' cannot be host associated at %L "
8728 "because it is blocked by an incompatible object "
8729 "of the same name declared at %L",
bc21d315 8730 sym->ts.u.derived->name, &sym->declared_at,
448d2cd2
TS
8731 &s->declared_at);
8732 return FAILURE;
8733 }
8734 }
8735
8736 /* 4th constraint in section 11.3: "If an object of a type for which
8737 component-initialization is specified (R429) appears in the
8738 specification-part of a module and does not have the ALLOCATABLE
8739 or POINTER attribute, the object shall have the SAVE attribute."
8740
8741 The check for initializers is performed with
8742 has_default_initializer because gfc_default_initializer generates
8743 a hidden default for allocatable components. */
9de88093 8744 if (!(sym->value || no_init_flag) && sym->ns->proc_name
448d2cd2
TS
8745 && sym->ns->proc_name->attr.flavor == FL_MODULE
8746 && !sym->ns->save_all && !sym->attr.save
8747 && !sym->attr.pointer && !sym->attr.allocatable
bc21d315 8748 && has_default_initializer (sym->ts.u.derived))
448d2cd2
TS
8749 {
8750 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8751 "default initialization of a component",
8752 sym->name, &sym->declared_at);
8753 return FAILURE;
8754 }
8755
cf2b3c22 8756 if (sym->ts.type == BT_CLASS)
727e8544
JW
8757 {
8758 /* C502. */
cf2b3c22 8759 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
727e8544
JW
8760 {
8761 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
bc21d315 8762 sym->ts.u.derived->name, sym->name, &sym->declared_at);
727e8544
JW
8763 return FAILURE;
8764 }
8765
8766 /* C509. */
2e23972e
JW
8767 /* Assume that use associated symbols were checked in the module ns. */
8768 if (!sym->attr.class_ok && !sym->attr.use_assoc)
727e8544
JW
8769 {
8770 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8771 "or pointer", sym->name, &sym->declared_at);
8772 return FAILURE;
8773 }
8774 }
8775
448d2cd2
TS
8776 /* Assign default initializer. */
8777 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9de88093 8778 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
448d2cd2
TS
8779 {
8780 sym->value = gfc_default_initializer (&sym->ts);
8781 }
8782
8783 return SUCCESS;
8784}
8785
8786
2ed8d224
PT
8787/* Resolve symbols with flavor variable. */
8788
17b1d2a0 8789static gfc_try
2ed8d224
PT
8790resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8791{
9de88093 8792 int no_init_flag, automatic_flag;
2ed8d224 8793 gfc_expr *e;
edf1eac2 8794 const char *auto_save_msg;
0e9a445b 8795
9de88093 8796 auto_save_msg = "Automatic object '%s' at %L cannot have the "
0e9a445b 8797 "SAVE attribute";
2ed8d224
PT
8798
8799 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
110eec24
TS
8800 return FAILURE;
8801
0e9a445b
PT
8802 /* Set this flag to check that variables are parameters of all entries.
8803 This check is effected by the call to gfc_resolve_expr through
8804 is_non_constant_shape_array. */
8805 specification_expr = 1;
8806
c4d4556f
TS
8807 if (sym->ns->proc_name
8808 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8809 || sym->ns->proc_name->attr.is_main_program)
8810 && !sym->attr.use_assoc
edf1eac2
SK
8811 && !sym->attr.allocatable
8812 && !sym->attr.pointer
8813 && is_non_constant_shape_array (sym))
2ed8d224 8814 {
c4d4556f
TS
8815 /* The shape of a main program or module array needs to be
8816 constant. */
8817 gfc_error ("The module or main program array '%s' at %L must "
8818 "have constant shape", sym->name, &sym->declared_at);
8819 specification_expr = 0;
8820 return FAILURE;
2ed8d224
PT
8821 }
8822
8823 if (sym->ts.type == BT_CHARACTER)
8824 {
8825 /* Make sure that character string variables with assumed length are
8826 dummy arguments. */
bc21d315 8827 e = sym->ts.u.cl->length;
2ed8d224
PT
8828 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8829 {
8830 gfc_error ("Entity with assumed character length at %L must be a "
8831 "dummy argument or a PARAMETER", &sym->declared_at);
8832 return FAILURE;
8833 }
8834
0e9a445b
PT
8835 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8836 {
8837 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8838 return FAILURE;
8839 }
8840
2ed8d224 8841 if (!gfc_is_constant_expr (e)
edf1eac2
SK
8842 && !(e->expr_type == EXPR_VARIABLE
8843 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
8844 && sym->ns->proc_name
8845 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8846 || sym->ns->proc_name->attr.is_main_program)
8847 && !sym->attr.use_assoc)
2ed8d224
PT
8848 {
8849 gfc_error ("'%s' at %L must have constant character length "
8850 "in this context", sym->name, &sym->declared_at);
8851 return FAILURE;
8852 }
8853 }
8854
51b09ce3
AL
8855 if (sym->value == NULL && sym->attr.referenced)
8856 apply_default_init_local (sym); /* Try to apply a default initialization. */
8857
9de88093
TS
8858 /* Determine if the symbol may not have an initializer. */
8859 no_init_flag = automatic_flag = 0;
2ed8d224 8860 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9de88093
TS
8861 || sym->attr.intrinsic || sym->attr.result)
8862 no_init_flag = 1;
8863 else if (sym->attr.dimension && !sym->attr.pointer
8864 && is_non_constant_shape_array (sym))
2ed8d224 8865 {
9de88093 8866 no_init_flag = automatic_flag = 1;
0e9a445b 8867
5349080d
TB
8868 /* Also, they must not have the SAVE attribute.
8869 SAVE_IMPLICIT is checked below. */
9de88093 8870 if (sym->attr.save == SAVE_EXPLICIT)
0e9a445b
PT
8871 {
8872 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8873 return FAILURE;
8874 }
448d2cd2 8875 }
2ed8d224 8876
7a99defe
SK
8877 /* Ensure that any initializer is simplified. */
8878 if (sym->value)
8879 gfc_simplify_expr (sym->value, 1);
8880
2ed8d224 8881 /* Reject illegal initializers. */
9de88093 8882 if (!sym->mark && sym->value)
2ed8d224
PT
8883 {
8884 if (sym->attr.allocatable)
8885 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
8886 sym->name, &sym->declared_at);
8887 else if (sym->attr.external)
8888 gfc_error ("External '%s' at %L cannot have an initializer",
8889 sym->name, &sym->declared_at);
145bdc2c
PT
8890 else if (sym->attr.dummy
8891 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
2ed8d224
PT
8892 gfc_error ("Dummy '%s' at %L cannot have an initializer",
8893 sym->name, &sym->declared_at);
8894 else if (sym->attr.intrinsic)
8895 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
8896 sym->name, &sym->declared_at);
8897 else if (sym->attr.result)
8898 gfc_error ("Function result '%s' at %L cannot have an initializer",
8899 sym->name, &sym->declared_at);
9de88093 8900 else if (automatic_flag)
2ed8d224
PT
8901 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
8902 sym->name, &sym->declared_at);
145bdc2c
PT
8903 else
8904 goto no_init_error;
2ed8d224
PT
8905 return FAILURE;
8906 }
8907
145bdc2c 8908no_init_error:
cf2b3c22 8909 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9de88093 8910 return resolve_fl_variable_derived (sym, no_init_flag);
2ed8d224
PT
8911
8912 return SUCCESS;
8913}
8914
8915
8916/* Resolve a procedure. */
8917
17b1d2a0 8918static gfc_try
2ed8d224
PT
8919resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
8920{
8921 gfc_formal_arglist *arg;
8922
993ef28f
PT
8923 if (sym->attr.ambiguous_interfaces && !sym->attr.referenced)
8924 gfc_warning ("Although not referenced, '%s' at %L has ambiguous "
8925 "interfaces", sym->name, &sym->declared_at);
8926
2ed8d224 8927 if (sym->attr.function
edf1eac2 8928 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
110eec24
TS
8929 return FAILURE;
8930
92c59193 8931 if (sym->ts.type == BT_CHARACTER)
2ed8d224 8932 {
bc21d315 8933 gfc_charlen *cl = sym->ts.u.cl;
8111a921
PT
8934
8935 if (cl && cl->length && gfc_is_constant_expr (cl->length)
8936 && resolve_charlen (cl) == FAILURE)
8937 return FAILURE;
8938
92c59193
PT
8939 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
8940 {
8941 if (sym->attr.proc == PROC_ST_FUNCTION)
8942 {
edf1eac2
SK
8943 gfc_error ("Character-valued statement function '%s' at %L must "
8944 "have constant length", sym->name, &sym->declared_at);
8945 return FAILURE;
8946 }
92c59193
PT
8947
8948 if (sym->attr.external && sym->formal == NULL
edf1eac2
SK
8949 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
8950 {
8951 gfc_error ("Automatic character length function '%s' at %L must "
8952 "have an explicit interface", sym->name,
8953 &sym->declared_at);
8954 return FAILURE;
8955 }
8956 }
2ed8d224
PT
8957 }
8958
37e47ee9 8959 /* Ensure that derived type for are not of a private type. Internal
df2fba9e 8960 module procedures are excluded by 2.2.3.3 - i.e., they are not
b82feea5 8961 externally accessible and can access all the objects accessible in
66e4ab31 8962 the host. */
37e47ee9 8963 if (!(sym->ns->parent
edf1eac2
SK
8964 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
8965 && gfc_check_access(sym->attr.access, sym->ns->default_access))
2ed8d224 8966 {
83b2e4e8
DF
8967 gfc_interface *iface;
8968
2ed8d224
PT
8969 for (arg = sym->formal; arg; arg = arg->next)
8970 {
8971 if (arg->sym
edf1eac2 8972 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
8973 && !arg->sym->ts.u.derived->attr.use_assoc
8974 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8975 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
8976 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
8977 "PRIVATE type and cannot be a dummy argument"
8978 " of '%s', which is PUBLIC at %L",
8979 arg->sym->name, sym->name, &sym->declared_at)
8980 == FAILURE)
2ed8d224 8981 {
2ed8d224 8982 /* Stop this message from recurring. */
bc21d315 8983 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
2ed8d224
PT
8984 return FAILURE;
8985 }
8986 }
83b2e4e8 8987
3bed9dd0
DF
8988 /* PUBLIC interfaces may expose PRIVATE procedures that take types
8989 PRIVATE to the containing module. */
8990 for (iface = sym->generic; iface; iface = iface->next)
8991 {
8992 for (arg = iface->sym->formal; arg; arg = arg->next)
8993 {
8994 if (arg->sym
8995 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
8996 && !arg->sym->ts.u.derived->attr.use_assoc
8997 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
8998 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
8999 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9000 "'%s' in PUBLIC interface '%s' at %L "
9001 "takes dummy arguments of '%s' which is "
9002 "PRIVATE", iface->sym->name, sym->name,
9003 &iface->sym->declared_at,
9004 gfc_typename (&arg->sym->ts)) == FAILURE)
3bed9dd0 9005 {
3bed9dd0 9006 /* Stop this message from recurring. */
bc21d315 9007 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
3bed9dd0
DF
9008 return FAILURE;
9009 }
9010 }
9011 }
9012
83b2e4e8
DF
9013 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9014 PRIVATE to the containing module. */
9015 for (iface = sym->generic; iface; iface = iface->next)
9016 {
9017 for (arg = iface->sym->formal; arg; arg = arg->next)
9018 {
9019 if (arg->sym
9020 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
9021 && !arg->sym->ts.u.derived->attr.use_assoc
9022 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9023 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
9024 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9025 "'%s' in PUBLIC interface '%s' at %L "
9026 "takes dummy arguments of '%s' which is "
9027 "PRIVATE", iface->sym->name, sym->name,
9028 &iface->sym->declared_at,
9029 gfc_typename (&arg->sym->ts)) == FAILURE)
83b2e4e8 9030 {
83b2e4e8 9031 /* Stop this message from recurring. */
bc21d315 9032 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
83b2e4e8
DF
9033 return FAILURE;
9034 }
9035 }
9036 }
2ed8d224
PT
9037 }
9038
8fb74da4
JW
9039 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9040 && !sym->attr.proc_pointer)
f8faa85e
DF
9041 {
9042 gfc_error ("Function '%s' at %L cannot have an initializer",
9043 sym->name, &sym->declared_at);
9044 return FAILURE;
9045 }
9046
e2ae1407 9047 /* An external symbol may not have an initializer because it is taken to be
8fb74da4
JW
9048 a procedure. Exception: Procedure Pointers. */
9049 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
2ed8d224
PT
9050 {
9051 gfc_error ("External object '%s' at %L may not have an initializer",
9052 sym->name, &sym->declared_at);
9053 return FAILURE;
9054 }
9055
d68bd5a8
PT
9056 /* An elemental function is required to return a scalar 12.7.1 */
9057 if (sym->attr.elemental && sym->attr.function && sym->as)
9058 {
9059 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9060 "result", sym->name, &sym->declared_at);
9061 /* Reset so that the error only occurs once. */
9062 sym->attr.elemental = 0;
9063 return FAILURE;
9064 }
9065
2ed8d224
PT
9066 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9067 char-len-param shall not be array-valued, pointer-valued, recursive
9068 or pure. ....snip... A character value of * may only be used in the
9069 following ways: (i) Dummy arg of procedure - dummy associates with
9070 actual length; (ii) To declare a named constant; or (iii) External
9071 function - but length must be declared in calling scoping unit. */
9072 if (sym->attr.function
edf1eac2 9073 && sym->ts.type == BT_CHARACTER
bc21d315 9074 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
2ed8d224
PT
9075 {
9076 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
edf1eac2 9077 || (sym->attr.recursive) || (sym->attr.pure))
2ed8d224
PT
9078 {
9079 if (sym->as && sym->as->rank)
9080 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9081 "array-valued", sym->name, &sym->declared_at);
9082
9083 if (sym->attr.pointer)
9084 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9085 "pointer-valued", sym->name, &sym->declared_at);
9086
9087 if (sym->attr.pure)
9088 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9089 "pure", sym->name, &sym->declared_at);
9090
9091 if (sym->attr.recursive)
9092 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9093 "recursive", sym->name, &sym->declared_at);
9094
9095 return FAILURE;
9096 }
9097
9098 /* Appendix B.2 of the standard. Contained functions give an
9099 error anyway. Fixed-form is likely to be F77/legacy. */
9100 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
e2ab8b09
JW
9101 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9102 "CHARACTER(*) function '%s' at %L",
2ed8d224
PT
9103 sym->name, &sym->declared_at);
9104 }
a8b3b0b6
CR
9105
9106 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9107 {
9108 gfc_formal_arglist *curr_arg;
aa5e22f0 9109 int has_non_interop_arg = 0;
a8b3b0b6
CR
9110
9111 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9112 sym->common_block) == FAILURE)
9113 {
9114 /* Clear these to prevent looking at them again if there was an
9115 error. */
9116 sym->attr.is_bind_c = 0;
9117 sym->attr.is_c_interop = 0;
9118 sym->ts.is_c_interop = 0;
9119 }
9120 else
9121 {
9122 /* So far, no errors have been found. */
9123 sym->attr.is_c_interop = 1;
9124 sym->ts.is_c_interop = 1;
9125 }
9126
9127 curr_arg = sym->formal;
9128 while (curr_arg != NULL)
9129 {
9130 /* Skip implicitly typed dummy args here. */
aa5e22f0
CR
9131 if (curr_arg->sym->attr.implicit_type == 0)
9132 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9133 /* If something is found to fail, record the fact so we
9134 can mark the symbol for the procedure as not being
9135 BIND(C) to try and prevent multiple errors being
9136 reported. */
9137 has_non_interop_arg = 1;
9138
a8b3b0b6
CR
9139 curr_arg = curr_arg->next;
9140 }
aa5e22f0
CR
9141
9142 /* See if any of the arguments were not interoperable and if so, clear
9143 the procedure symbol to prevent duplicate error messages. */
9144 if (has_non_interop_arg != 0)
9145 {
9146 sym->attr.is_c_interop = 0;
9147 sym->ts.is_c_interop = 0;
9148 sym->attr.is_bind_c = 0;
9149 }
a8b3b0b6
CR
9150 }
9151
3070bab4 9152 if (!sym->attr.proc_pointer)
beb4bd6c 9153 {
3070bab4
JW
9154 if (sym->attr.save == SAVE_EXPLICIT)
9155 {
9156 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9157 "in '%s' at %L", sym->name, &sym->declared_at);
9158 return FAILURE;
9159 }
9160 if (sym->attr.intent)
9161 {
9162 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9163 "in '%s' at %L", sym->name, &sym->declared_at);
9164 return FAILURE;
9165 }
9166 if (sym->attr.subroutine && sym->attr.result)
9167 {
9168 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9169 "in '%s' at %L", sym->name, &sym->declared_at);
9170 return FAILURE;
9171 }
9172 if (sym->attr.external && sym->attr.function
9173 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9174 || sym->attr.contained))
9175 {
9176 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9177 "in '%s' at %L", sym->name, &sym->declared_at);
9178 return FAILURE;
9179 }
9180 if (strcmp ("ppr@", sym->name) == 0)
9181 {
9182 gfc_error ("Procedure pointer result '%s' at %L "
9183 "is missing the pointer attribute",
9184 sym->ns->proc_name->name, &sym->declared_at);
9185 return FAILURE;
9186 }
beb4bd6c
JW
9187 }
9188
110eec24
TS
9189 return SUCCESS;
9190}
9191
9192
34523524
DK
9193/* Resolve a list of finalizer procedures. That is, after they have hopefully
9194 been defined and we now know their defined arguments, check that they fulfill
9195 the requirements of the standard for procedures used as finalizers. */
9196
17b1d2a0 9197static gfc_try
34523524
DK
9198gfc_resolve_finalizers (gfc_symbol* derived)
9199{
9200 gfc_finalizer* list;
9201 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
17b1d2a0 9202 gfc_try result = SUCCESS;
34523524
DK
9203 bool seen_scalar = false;
9204
9205 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9206 return SUCCESS;
9207
9208 /* Walk over the list of finalizer-procedures, check them, and if any one
9209 does not fit in with the standard's definition, print an error and remove
9210 it from the list. */
9211 prev_link = &derived->f2k_derived->finalizers;
9212 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9213 {
9214 gfc_symbol* arg;
9215 gfc_finalizer* i;
9216 int my_rank;
9217
f6fad28e
DK
9218 /* Skip this finalizer if we already resolved it. */
9219 if (list->proc_tree)
9220 {
9221 prev_link = &(list->next);
9222 continue;
9223 }
9224
34523524 9225 /* Check this exists and is a SUBROUTINE. */
f6fad28e 9226 if (!list->proc_sym->attr.subroutine)
34523524
DK
9227 {
9228 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
f6fad28e 9229 list->proc_sym->name, &list->where);
34523524
DK
9230 goto error;
9231 }
9232
9233 /* We should have exactly one argument. */
f6fad28e 9234 if (!list->proc_sym->formal || list->proc_sym->formal->next)
34523524
DK
9235 {
9236 gfc_error ("FINAL procedure at %L must have exactly one argument",
9237 &list->where);
9238 goto error;
9239 }
f6fad28e 9240 arg = list->proc_sym->formal->sym;
34523524
DK
9241
9242 /* This argument must be of our type. */
bc21d315 9243 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
34523524
DK
9244 {
9245 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9246 &arg->declared_at, derived->name);
9247 goto error;
9248 }
9249
9250 /* It must neither be a pointer nor allocatable nor optional. */
9251 if (arg->attr.pointer)
9252 {
9253 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9254 &arg->declared_at);
9255 goto error;
9256 }
9257 if (arg->attr.allocatable)
9258 {
9259 gfc_error ("Argument of FINAL procedure at %L must not be"
9260 " ALLOCATABLE", &arg->declared_at);
9261 goto error;
9262 }
9263 if (arg->attr.optional)
9264 {
9265 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9266 &arg->declared_at);
9267 goto error;
9268 }
9269
9270 /* It must not be INTENT(OUT). */
9271 if (arg->attr.intent == INTENT_OUT)
9272 {
9273 gfc_error ("Argument of FINAL procedure at %L must not be"
9274 " INTENT(OUT)", &arg->declared_at);
9275 goto error;
9276 }
9277
9278 /* Warn if the procedure is non-scalar and not assumed shape. */
9279 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9280 && arg->as->type != AS_ASSUMED_SHAPE)
9281 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9282 " shape argument", &arg->declared_at);
9283
9284 /* Check that it does not match in kind and rank with a FINAL procedure
9285 defined earlier. To really loop over the *earlier* declarations,
9286 we need to walk the tail of the list as new ones were pushed at the
9287 front. */
9288 /* TODO: Handle kind parameters once they are implemented. */
9289 my_rank = (arg->as ? arg->as->rank : 0);
9290 for (i = list->next; i; i = i->next)
9291 {
9292 /* Argument list might be empty; that is an error signalled earlier,
9293 but we nevertheless continued resolving. */
f6fad28e 9294 if (i->proc_sym->formal)
34523524 9295 {
f6fad28e 9296 gfc_symbol* i_arg = i->proc_sym->formal->sym;
34523524
DK
9297 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9298 if (i_rank == my_rank)
9299 {
9300 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9301 " rank (%d) as '%s'",
f6fad28e
DK
9302 list->proc_sym->name, &list->where, my_rank,
9303 i->proc_sym->name);
34523524
DK
9304 goto error;
9305 }
9306 }
9307 }
9308
9309 /* Is this the/a scalar finalizer procedure? */
9310 if (!arg->as || arg->as->rank == 0)
9311 seen_scalar = true;
9312
f6fad28e
DK
9313 /* Find the symtree for this procedure. */
9314 gcc_assert (!list->proc_tree);
9315 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9316
34523524
DK
9317 prev_link = &list->next;
9318 continue;
9319
df2fba9e 9320 /* Remove wrong nodes immediately from the list so we don't risk any
34523524
DK
9321 troubles in the future when they might fail later expectations. */
9322error:
9323 result = FAILURE;
9324 i = list;
9325 *prev_link = list->next;
9326 gfc_free_finalizer (i);
9327 }
9328
9329 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9330 were nodes in the list, must have been for arrays. It is surely a good
9331 idea to have a scalar version there if there's something to finalize. */
9332 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9333 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9334 " defined at %L, suggest also scalar one",
9335 derived->name, &derived->declared_at);
9336
9337 /* TODO: Remove this error when finalization is finished. */
f6fad28e
DK
9338 gfc_error ("Finalization at %L is not yet implemented",
9339 &derived->declared_at);
34523524
DK
9340
9341 return result;
9342}
9343
9344
30b608eb
DK
9345/* Check that it is ok for the typebound procedure proc to override the
9346 procedure old. */
9347
9348static gfc_try
9349check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9350{
9351 locus where;
9352 const gfc_symbol* proc_target;
9353 const gfc_symbol* old_target;
9354 unsigned proc_pass_arg, old_pass_arg, argpos;
9355 gfc_formal_arglist* proc_formal;
9356 gfc_formal_arglist* old_formal;
9357
e157f736 9358 /* This procedure should only be called for non-GENERIC proc. */
e34ccb4c 9359 gcc_assert (!proc->n.tb->is_generic);
e157f736
DK
9360
9361 /* If the overwritten procedure is GENERIC, this is an error. */
e34ccb4c 9362 if (old->n.tb->is_generic)
e157f736
DK
9363 {
9364 gfc_error ("Can't overwrite GENERIC '%s' at %L",
e34ccb4c 9365 old->name, &proc->n.tb->where);
e157f736
DK
9366 return FAILURE;
9367 }
9368
e34ccb4c
DK
9369 where = proc->n.tb->where;
9370 proc_target = proc->n.tb->u.specific->n.sym;
9371 old_target = old->n.tb->u.specific->n.sym;
30b608eb
DK
9372
9373 /* Check that overridden binding is not NON_OVERRIDABLE. */
e34ccb4c 9374 if (old->n.tb->non_overridable)
30b608eb
DK
9375 {
9376 gfc_error ("'%s' at %L overrides a procedure binding declared"
9377 " NON_OVERRIDABLE", proc->name, &where);
9378 return FAILURE;
9379 }
9380
b0e5fa94 9381 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
e34ccb4c 9382 if (!old->n.tb->deferred && proc->n.tb->deferred)
b0e5fa94
DK
9383 {
9384 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9385 " non-DEFERRED binding", proc->name, &where);
9386 return FAILURE;
9387 }
9388
30b608eb
DK
9389 /* If the overridden binding is PURE, the overriding must be, too. */
9390 if (old_target->attr.pure && !proc_target->attr.pure)
9391 {
9392 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9393 proc->name, &where);
9394 return FAILURE;
9395 }
9396
9397 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9398 is not, the overriding must not be either. */
9399 if (old_target->attr.elemental && !proc_target->attr.elemental)
9400 {
9401 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9402 " ELEMENTAL", proc->name, &where);
9403 return FAILURE;
9404 }
9405 if (!old_target->attr.elemental && proc_target->attr.elemental)
9406 {
9407 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9408 " be ELEMENTAL, either", proc->name, &where);
9409 return FAILURE;
9410 }
9411
9412 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9413 SUBROUTINE. */
9414 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9415 {
9416 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9417 " SUBROUTINE", proc->name, &where);
9418 return FAILURE;
9419 }
9420
9421 /* If the overridden binding is a FUNCTION, the overriding must also be a
9422 FUNCTION and have the same characteristics. */
9423 if (old_target->attr.function)
9424 {
9425 if (!proc_target->attr.function)
9426 {
9427 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9428 " FUNCTION", proc->name, &where);
9429 return FAILURE;
9430 }
9431
9432 /* FIXME: Do more comprehensive checking (including, for instance, the
9433 rank and array-shape). */
9434 gcc_assert (proc_target->result && old_target->result);
9435 if (!gfc_compare_types (&proc_target->result->ts,
9436 &old_target->result->ts))
9437 {
9438 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9439 " matching result types", proc->name, &where);
9440 return FAILURE;
9441 }
9442 }
9443
9444 /* If the overridden binding is PUBLIC, the overriding one must not be
9445 PRIVATE. */
e34ccb4c
DK
9446 if (old->n.tb->access == ACCESS_PUBLIC
9447 && proc->n.tb->access == ACCESS_PRIVATE)
30b608eb
DK
9448 {
9449 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9450 " PRIVATE", proc->name, &where);
9451 return FAILURE;
9452 }
9453
9454 /* Compare the formal argument lists of both procedures. This is also abused
9455 to find the position of the passed-object dummy arguments of both
9456 bindings as at least the overridden one might not yet be resolved and we
9457 need those positions in the check below. */
9458 proc_pass_arg = old_pass_arg = 0;
e34ccb4c 9459 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
30b608eb 9460 proc_pass_arg = 1;
e34ccb4c 9461 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
30b608eb
DK
9462 old_pass_arg = 1;
9463 argpos = 1;
9464 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9465 proc_formal && old_formal;
9466 proc_formal = proc_formal->next, old_formal = old_formal->next)
9467 {
e34ccb4c
DK
9468 if (proc->n.tb->pass_arg
9469 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
30b608eb 9470 proc_pass_arg = argpos;
e34ccb4c
DK
9471 if (old->n.tb->pass_arg
9472 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
30b608eb
DK
9473 old_pass_arg = argpos;
9474
9475 /* Check that the names correspond. */
9476 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9477 {
9478 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9479 " to match the corresponding argument of the overridden"
9480 " procedure", proc_formal->sym->name, proc->name, &where,
9481 old_formal->sym->name);
9482 return FAILURE;
9483 }
9484
9485 /* Check that the types correspond if neither is the passed-object
9486 argument. */
9487 /* FIXME: Do more comprehensive testing here. */
9488 if (proc_pass_arg != argpos && old_pass_arg != argpos
9489 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9490 {
800cee34
SK
9491 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9492 "in respect to the overridden procedure",
30b608eb
DK
9493 proc_formal->sym->name, proc->name, &where);
9494 return FAILURE;
9495 }
9496
9497 ++argpos;
9498 }
9499 if (proc_formal || old_formal)
9500 {
9501 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9502 " the overridden procedure", proc->name, &where);
9503 return FAILURE;
9504 }
9505
9506 /* If the overridden binding is NOPASS, the overriding one must also be
9507 NOPASS. */
e34ccb4c 9508 if (old->n.tb->nopass && !proc->n.tb->nopass)
30b608eb
DK
9509 {
9510 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9511 " NOPASS", proc->name, &where);
9512 return FAILURE;
9513 }
9514
9515 /* If the overridden binding is PASS(x), the overriding one must also be
9516 PASS and the passed-object dummy arguments must correspond. */
e34ccb4c 9517 if (!old->n.tb->nopass)
30b608eb 9518 {
e34ccb4c 9519 if (proc->n.tb->nopass)
30b608eb
DK
9520 {
9521 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9522 " PASS", proc->name, &where);
9523 return FAILURE;
9524 }
9525
9526 if (proc_pass_arg != old_pass_arg)
9527 {
9528 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9529 " the same position as the passed-object dummy argument of"
9530 " the overridden procedure", proc->name, &where);
9531 return FAILURE;
9532 }
9533 }
9534
9535 return SUCCESS;
9536}
9537
9538
e157f736
DK
9539/* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9540
9541static gfc_try
9542check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9543 const char* generic_name, locus where)
9544{
9545 gfc_symbol* sym1;
9546 gfc_symbol* sym2;
9547
9548 gcc_assert (t1->specific && t2->specific);
9549 gcc_assert (!t1->specific->is_generic);
9550 gcc_assert (!t2->specific->is_generic);
9551
9552 sym1 = t1->specific->u.specific->n.sym;
9553 sym2 = t2->specific->u.specific->n.sym;
9554
cf2b3c22
TB
9555 if (sym1 == sym2)
9556 return SUCCESS;
9557
e157f736
DK
9558 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9559 if (sym1->attr.subroutine != sym2->attr.subroutine
9560 || sym1->attr.function != sym2->attr.function)
9561 {
9562 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9563 " GENERIC '%s' at %L",
9564 sym1->name, sym2->name, generic_name, &where);
9565 return FAILURE;
9566 }
9567
9568 /* Compare the interfaces. */
889dc035 9569 if (gfc_compare_interfaces (sym1, sym2, NULL, 1, 0, NULL, 0))
e157f736
DK
9570 {
9571 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9572 sym1->name, sym2->name, generic_name, &where);
9573 return FAILURE;
9574 }
9575
9576 return SUCCESS;
9577}
9578
9579
94747289
DK
9580/* Worker function for resolving a generic procedure binding; this is used to
9581 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9582
9583 The difference between those cases is finding possible inherited bindings
9584 that are overridden, as one has to look for them in tb_sym_root,
9585 tb_uop_root or tb_op, respectively. Thus the caller must already find
9586 the super-type and set p->overridden correctly. */
e157f736
DK
9587
9588static gfc_try
94747289
DK
9589resolve_tb_generic_targets (gfc_symbol* super_type,
9590 gfc_typebound_proc* p, const char* name)
e157f736
DK
9591{
9592 gfc_tbp_generic* target;
9593 gfc_symtree* first_target;
e157f736 9594 gfc_symtree* inherited;
e157f736 9595
94747289 9596 gcc_assert (p && p->is_generic);
e157f736
DK
9597
9598 /* Try to find the specific bindings for the symtrees in our target-list. */
94747289
DK
9599 gcc_assert (p->u.generic);
9600 for (target = p->u.generic; target; target = target->next)
e157f736
DK
9601 if (!target->specific)
9602 {
9603 gfc_typebound_proc* overridden_tbp;
9604 gfc_tbp_generic* g;
9605 const char* target_name;
9606
9607 target_name = target->specific_st->name;
9608
9609 /* Defined for this type directly. */
e34ccb4c 9610 if (target->specific_st->n.tb)
e157f736 9611 {
e34ccb4c 9612 target->specific = target->specific_st->n.tb;
e157f736
DK
9613 goto specific_found;
9614 }
9615
9616 /* Look for an inherited specific binding. */
9617 if (super_type)
9618 {
4a44a72d
DK
9619 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9620 true, NULL);
e157f736
DK
9621
9622 if (inherited)
9623 {
e34ccb4c
DK
9624 gcc_assert (inherited->n.tb);
9625 target->specific = inherited->n.tb;
e157f736
DK
9626 goto specific_found;
9627 }
9628 }
9629
9630 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
94747289 9631 " at %L", target_name, name, &p->where);
e157f736
DK
9632 return FAILURE;
9633
9634 /* Once we've found the specific binding, check it is not ambiguous with
9635 other specifics already found or inherited for the same GENERIC. */
9636specific_found:
9637 gcc_assert (target->specific);
9638
9639 /* This must really be a specific binding! */
9640 if (target->specific->is_generic)
9641 {
9642 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
94747289 9643 " '%s' is GENERIC, too", name, &p->where, target_name);
e157f736
DK
9644 return FAILURE;
9645 }
9646
9647 /* Check those already resolved on this type directly. */
94747289 9648 for (g = p->u.generic; g; g = g->next)
e157f736 9649 if (g != target && g->specific
94747289 9650 && check_generic_tbp_ambiguity (target, g, name, p->where)
e157f736
DK
9651 == FAILURE)
9652 return FAILURE;
9653
9654 /* Check for ambiguity with inherited specific targets. */
94747289 9655 for (overridden_tbp = p->overridden; overridden_tbp;
e157f736
DK
9656 overridden_tbp = overridden_tbp->overridden)
9657 if (overridden_tbp->is_generic)
9658 {
9659 for (g = overridden_tbp->u.generic; g; g = g->next)
9660 {
9661 gcc_assert (g->specific);
9662 if (check_generic_tbp_ambiguity (target, g,
94747289 9663 name, p->where) == FAILURE)
e157f736
DK
9664 return FAILURE;
9665 }
9666 }
9667 }
9668
9669 /* If we attempt to "overwrite" a specific binding, this is an error. */
94747289 9670 if (p->overridden && !p->overridden->is_generic)
e157f736
DK
9671 {
9672 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
94747289 9673 " the same name", name, &p->where);
e157f736
DK
9674 return FAILURE;
9675 }
9676
9677 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9678 all must have the same attributes here. */
94747289 9679 first_target = p->u.generic->specific->u.specific;
e34ccb4c 9680 gcc_assert (first_target);
94747289
DK
9681 p->subroutine = first_target->n.sym->attr.subroutine;
9682 p->function = first_target->n.sym->attr.function;
e157f736
DK
9683
9684 return SUCCESS;
9685}
9686
9687
94747289
DK
9688/* Resolve a GENERIC procedure binding for a derived type. */
9689
9690static gfc_try
9691resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9692{
9693 gfc_symbol* super_type;
9694
9695 /* Find the overridden binding if any. */
9696 st->n.tb->overridden = NULL;
9697 super_type = gfc_get_derived_super_type (derived);
9698 if (super_type)
9699 {
9700 gfc_symtree* overridden;
4a44a72d
DK
9701 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9702 true, NULL);
94747289
DK
9703
9704 if (overridden && overridden->n.tb)
9705 st->n.tb->overridden = overridden->n.tb;
9706 }
9707
9708 /* Resolve using worker function. */
9709 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9710}
9711
9712
b325faf9
DK
9713/* Retrieve the target-procedure of an operator binding and do some checks in
9714 common for intrinsic and user-defined type-bound operators. */
9715
9716static gfc_symbol*
9717get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9718{
9719 gfc_symbol* target_proc;
9720
9721 gcc_assert (target->specific && !target->specific->is_generic);
9722 target_proc = target->specific->u.specific->n.sym;
9723 gcc_assert (target_proc);
9724
9725 /* All operator bindings must have a passed-object dummy argument. */
9726 if (target->specific->nopass)
9727 {
9728 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9729 return NULL;
9730 }
9731
9732 return target_proc;
9733}
9734
9735
94747289
DK
9736/* Resolve a type-bound intrinsic operator. */
9737
9738static gfc_try
9739resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9740 gfc_typebound_proc* p)
9741{
9742 gfc_symbol* super_type;
9743 gfc_tbp_generic* target;
9744
9745 /* If there's already an error here, do nothing (but don't fail again). */
9746 if (p->error)
9747 return SUCCESS;
9748
9749 /* Operators should always be GENERIC bindings. */
9750 gcc_assert (p->is_generic);
9751
9752 /* Look for an overridden binding. */
9753 super_type = gfc_get_derived_super_type (derived);
9754 if (super_type && super_type->f2k_derived)
9755 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
4a44a72d 9756 op, true, NULL);
94747289
DK
9757 else
9758 p->overridden = NULL;
9759
9760 /* Resolve general GENERIC properties using worker function. */
9761 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9762 goto error;
9763
9764 /* Check the targets to be procedures of correct interface. */
9765 for (target = p->u.generic; target; target = target->next)
9766 {
9767 gfc_symbol* target_proc;
9768
b325faf9
DK
9769 target_proc = get_checked_tb_operator_target (target, p->where);
9770 if (!target_proc)
4a44a72d 9771 goto error;
94747289
DK
9772
9773 if (!gfc_check_operator_interface (target_proc, op, p->where))
4a44a72d 9774 goto error;
94747289
DK
9775 }
9776
9777 return SUCCESS;
9778
9779error:
9780 p->error = 1;
9781 return FAILURE;
9782}
9783
9784
9785/* Resolve a type-bound user operator (tree-walker callback). */
30b608eb
DK
9786
9787static gfc_symbol* resolve_bindings_derived;
9788static gfc_try resolve_bindings_result;
9789
94747289
DK
9790static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9791
9792static void
9793resolve_typebound_user_op (gfc_symtree* stree)
9794{
9795 gfc_symbol* super_type;
9796 gfc_tbp_generic* target;
9797
9798 gcc_assert (stree && stree->n.tb);
9799
9800 if (stree->n.tb->error)
9801 return;
9802
9803 /* Operators should always be GENERIC bindings. */
9804 gcc_assert (stree->n.tb->is_generic);
9805
9806 /* Find overridden procedure, if any. */
9807 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9808 if (super_type && super_type->f2k_derived)
9809 {
9810 gfc_symtree* overridden;
9811 overridden = gfc_find_typebound_user_op (super_type, NULL,
4a44a72d 9812 stree->name, true, NULL);
94747289
DK
9813
9814 if (overridden && overridden->n.tb)
9815 stree->n.tb->overridden = overridden->n.tb;
9816 }
9817 else
9818 stree->n.tb->overridden = NULL;
9819
9820 /* Resolve basically using worker function. */
9821 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9822 == FAILURE)
9823 goto error;
9824
9825 /* Check the targets to be functions of correct interface. */
9826 for (target = stree->n.tb->u.generic; target; target = target->next)
9827 {
9828 gfc_symbol* target_proc;
9829
b325faf9
DK
9830 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9831 if (!target_proc)
9832 goto error;
94747289
DK
9833
9834 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9835 goto error;
9836 }
9837
9838 return;
9839
9840error:
9841 resolve_bindings_result = FAILURE;
9842 stree->n.tb->error = 1;
9843}
9844
9845
9846/* Resolve the type-bound procedures for a derived type. */
9847
30b608eb
DK
9848static void
9849resolve_typebound_procedure (gfc_symtree* stree)
9850{
9851 gfc_symbol* proc;
9852 locus where;
9853 gfc_symbol* me_arg;
9854 gfc_symbol* super_type;
9d1210f4 9855 gfc_component* comp;
30b608eb 9856
e34ccb4c
DK
9857 gcc_assert (stree);
9858
9859 /* Undefined specific symbol from GENERIC target definition. */
9860 if (!stree->n.tb)
9861 return;
9862
9863 if (stree->n.tb->error)
30b608eb
DK
9864 return;
9865
e157f736 9866 /* If this is a GENERIC binding, use that routine. */
e34ccb4c 9867 if (stree->n.tb->is_generic)
e157f736
DK
9868 {
9869 if (resolve_typebound_generic (resolve_bindings_derived, stree)
9870 == FAILURE)
9871 goto error;
9872 return;
9873 }
9874
30b608eb 9875 /* Get the target-procedure to check it. */
e34ccb4c
DK
9876 gcc_assert (!stree->n.tb->is_generic);
9877 gcc_assert (stree->n.tb->u.specific);
9878 proc = stree->n.tb->u.specific->n.sym;
9879 where = stree->n.tb->where;
30b608eb
DK
9880
9881 /* Default access should already be resolved from the parser. */
e34ccb4c 9882 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
30b608eb
DK
9883
9884 /* It should be a module procedure or an external procedure with explicit
b0e5fa94 9885 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
30b608eb
DK
9886 if ((!proc->attr.subroutine && !proc->attr.function)
9887 || (proc->attr.proc != PROC_MODULE
9888 && proc->attr.if_source != IFSRC_IFBODY)
e34ccb4c 9889 || (proc->attr.abstract && !stree->n.tb->deferred))
30b608eb
DK
9890 {
9891 gfc_error ("'%s' must be a module procedure or an external procedure with"
9892 " an explicit interface at %L", proc->name, &where);
9893 goto error;
9894 }
e34ccb4c
DK
9895 stree->n.tb->subroutine = proc->attr.subroutine;
9896 stree->n.tb->function = proc->attr.function;
30b608eb
DK
9897
9898 /* Find the super-type of the current derived type. We could do this once and
9899 store in a global if speed is needed, but as long as not I believe this is
9900 more readable and clearer. */
9901 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9902
e157f736
DK
9903 /* If PASS, resolve and check arguments if not already resolved / loaded
9904 from a .mod file. */
e34ccb4c 9905 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
30b608eb 9906 {
e34ccb4c 9907 if (stree->n.tb->pass_arg)
30b608eb
DK
9908 {
9909 gfc_formal_arglist* i;
9910
9911 /* If an explicit passing argument name is given, walk the arg-list
9912 and look for it. */
9913
9914 me_arg = NULL;
e34ccb4c 9915 stree->n.tb->pass_arg_num = 1;
30b608eb
DK
9916 for (i = proc->formal; i; i = i->next)
9917 {
e34ccb4c 9918 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
30b608eb
DK
9919 {
9920 me_arg = i->sym;
9921 break;
9922 }
e34ccb4c 9923 ++stree->n.tb->pass_arg_num;
30b608eb
DK
9924 }
9925
9926 if (!me_arg)
9927 {
9928 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
9929 " argument '%s'",
e34ccb4c
DK
9930 proc->name, stree->n.tb->pass_arg, &where,
9931 stree->n.tb->pass_arg);
30b608eb
DK
9932 goto error;
9933 }
9934 }
9935 else
9936 {
9937 /* Otherwise, take the first one; there should in fact be at least
9938 one. */
e34ccb4c 9939 stree->n.tb->pass_arg_num = 1;
30b608eb
DK
9940 if (!proc->formal)
9941 {
9942 gfc_error ("Procedure '%s' with PASS at %L must have at"
9943 " least one argument", proc->name, &where);
9944 goto error;
9945 }
9946 me_arg = proc->formal->sym;
9947 }
9948
9949 /* Now check that the argument-type matches. */
9950 gcc_assert (me_arg);
cf2b3c22 9951 if (me_arg->ts.type != BT_CLASS)
30b608eb 9952 {
cf2b3c22
TB
9953 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
9954 " at %L", proc->name, &where);
30b608eb
DK
9955 goto error;
9956 }
8e1f752a 9957
cf2b3c22
TB
9958 if (me_arg->ts.u.derived->components->ts.u.derived
9959 != resolve_bindings_derived)
727e8544 9960 {
cf2b3c22
TB
9961 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
9962 " the derived-type '%s'", me_arg->name, proc->name,
9963 me_arg->name, &where, resolve_bindings_derived->name);
727e8544
JW
9964 goto error;
9965 }
cf2b3c22 9966
30b608eb
DK
9967 }
9968
9969 /* If we are extending some type, check that we don't override a procedure
9970 flagged NON_OVERRIDABLE. */
e34ccb4c 9971 stree->n.tb->overridden = NULL;
30b608eb
DK
9972 if (super_type)
9973 {
9974 gfc_symtree* overridden;
8e1f752a 9975 overridden = gfc_find_typebound_proc (super_type, NULL,
4a44a72d 9976 stree->name, true, NULL);
30b608eb 9977
e34ccb4c
DK
9978 if (overridden && overridden->n.tb)
9979 stree->n.tb->overridden = overridden->n.tb;
e157f736 9980
30b608eb
DK
9981 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
9982 goto error;
9983 }
9984
9d1210f4
DK
9985 /* See if there's a name collision with a component directly in this type. */
9986 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
9987 if (!strcmp (comp->name, stree->name))
9988 {
9989 gfc_error ("Procedure '%s' at %L has the same name as a component of"
9990 " '%s'",
9991 stree->name, &where, resolve_bindings_derived->name);
9992 goto error;
9993 }
9994
9995 /* Try to find a name collision with an inherited component. */
9996 if (super_type && gfc_find_component (super_type, stree->name, true, true))
9997 {
9998 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
9999 " component of '%s'",
10000 stree->name, &where, resolve_bindings_derived->name);
10001 goto error;
10002 }
10003
e34ccb4c 10004 stree->n.tb->error = 0;
30b608eb
DK
10005 return;
10006
10007error:
10008 resolve_bindings_result = FAILURE;
e34ccb4c 10009 stree->n.tb->error = 1;
30b608eb
DK
10010}
10011
10012static gfc_try
10013resolve_typebound_procedures (gfc_symbol* derived)
10014{
94747289 10015 int op;
94747289 10016
e34ccb4c 10017 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
30b608eb
DK
10018 return SUCCESS;
10019
10020 resolve_bindings_derived = derived;
10021 resolve_bindings_result = SUCCESS;
94747289
DK
10022
10023 if (derived->f2k_derived->tb_sym_root)
10024 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10025 &resolve_typebound_procedure);
10026
94747289
DK
10027 if (derived->f2k_derived->tb_uop_root)
10028 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10029 &resolve_typebound_user_op);
10030
10031 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10032 {
10033 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10034 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10035 p) == FAILURE)
10036 resolve_bindings_result = FAILURE;
94747289 10037 }
30b608eb
DK
10038
10039 return resolve_bindings_result;
10040}
10041
10042
9d5c21c1
PT
10043/* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10044 to give all identical derived types the same backend_decl. */
10045static void
10046add_dt_to_dt_list (gfc_symbol *derived)
10047{
10048 gfc_dt_list *dt_list;
10049
10050 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10051 if (derived == dt_list->derived)
10052 break;
10053
10054 if (dt_list == NULL)
10055 {
10056 dt_list = gfc_get_dt_list ();
10057 dt_list->next = gfc_derived_types;
10058 dt_list->derived = derived;
10059 gfc_derived_types = dt_list;
10060 }
10061}
10062
10063
b0e5fa94
DK
10064/* Ensure that a derived-type is really not abstract, meaning that every
10065 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10066
10067static gfc_try
10068ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10069{
10070 if (!st)
10071 return SUCCESS;
10072
10073 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10074 return FAILURE;
10075 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10076 return FAILURE;
10077
e34ccb4c 10078 if (st->n.tb && st->n.tb->deferred)
b0e5fa94
DK
10079 {
10080 gfc_symtree* overriding;
4a44a72d 10081 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
e34ccb4c
DK
10082 gcc_assert (overriding && overriding->n.tb);
10083 if (overriding->n.tb->deferred)
b0e5fa94
DK
10084 {
10085 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10086 " '%s' is DEFERRED and not overridden",
10087 sub->name, &sub->declared_at, st->name);
10088 return FAILURE;
10089 }
10090 }
10091
10092 return SUCCESS;
10093}
10094
10095static gfc_try
10096ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10097{
10098 /* The algorithm used here is to recursively travel up the ancestry of sub
10099 and for each ancestor-type, check all bindings. If any of them is
10100 DEFERRED, look it up starting from sub and see if the found (overriding)
10101 binding is not DEFERRED.
10102 This is not the most efficient way to do this, but it should be ok and is
10103 clearer than something sophisticated. */
10104
10105 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10106
10107 /* Walk bindings of this ancestor. */
10108 if (ancestor->f2k_derived)
10109 {
10110 gfc_try t;
e34ccb4c 10111 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
b0e5fa94
DK
10112 if (t == FAILURE)
10113 return FAILURE;
10114 }
10115
10116 /* Find next ancestor type and recurse on it. */
10117 ancestor = gfc_get_derived_super_type (ancestor);
10118 if (ancestor)
10119 return ensure_not_abstract (sub, ancestor);
10120
10121 return SUCCESS;
10122}
10123
10124
acbdc378
JW
10125static void resolve_symbol (gfc_symbol *sym);
10126
10127
110eec24
TS
10128/* Resolve the components of a derived type. */
10129
17b1d2a0 10130static gfc_try
2ed8d224 10131resolve_fl_derived (gfc_symbol *sym)
110eec24 10132{
9d1210f4 10133 gfc_symbol* super_type;
110eec24 10134 gfc_component *c;
2ed8d224 10135 int i;
110eec24 10136
9d1210f4
DK
10137 super_type = gfc_get_derived_super_type (sym);
10138
e157f736
DK
10139 /* Ensure the extended type gets resolved before we do. */
10140 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10141 return FAILURE;
10142
52f49934 10143 /* An ABSTRACT type must be extensible. */
cf2b3c22 10144 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
52f49934
DK
10145 {
10146 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10147 sym->name, &sym->declared_at);
10148 return FAILURE;
10149 }
10150
110eec24
TS
10151 for (c = sym->components; c != NULL; c = c->next)
10152 {
713485cc
JW
10153 if (c->attr.proc_pointer && c->ts.interface)
10154 {
10155 if (c->ts.interface->attr.procedure)
10156 gfc_error ("Interface '%s', used by procedure pointer component "
10157 "'%s' at %L, is declared in a later PROCEDURE statement",
10158 c->ts.interface->name, c->name, &c->loc);
10159
10160 /* Get the attributes from the interface (now resolved). */
10161 if (c->ts.interface->attr.if_source
10162 || c->ts.interface->attr.intrinsic)
10163 {
10164 gfc_symbol *ifc = c->ts.interface;
10165
acbdc378
JW
10166 if (ifc->formal && !ifc->formal_ns)
10167 resolve_symbol (ifc);
10168
713485cc
JW
10169 if (ifc->attr.intrinsic)
10170 resolve_intrinsic (ifc, &ifc->declared_at);
10171
10172 if (ifc->result)
f64edc8b
JW
10173 {
10174 c->ts = ifc->result->ts;
10175 c->attr.allocatable = ifc->result->attr.allocatable;
10176 c->attr.pointer = ifc->result->attr.pointer;
10177 c->attr.dimension = ifc->result->attr.dimension;
10178 c->as = gfc_copy_array_spec (ifc->result->as);
10179 }
10180 else
10181 {
10182 c->ts = ifc->ts;
10183 c->attr.allocatable = ifc->attr.allocatable;
10184 c->attr.pointer = ifc->attr.pointer;
10185 c->attr.dimension = ifc->attr.dimension;
10186 c->as = gfc_copy_array_spec (ifc->as);
10187 }
713485cc
JW
10188 c->ts.interface = ifc;
10189 c->attr.function = ifc->attr.function;
10190 c->attr.subroutine = ifc->attr.subroutine;
7e196f89 10191 gfc_copy_formal_args_ppc (c, ifc);
713485cc 10192
713485cc
JW
10193 c->attr.pure = ifc->attr.pure;
10194 c->attr.elemental = ifc->attr.elemental;
713485cc
JW
10195 c->attr.recursive = ifc->attr.recursive;
10196 c->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 10197 c->attr.ext_attr |= ifc->attr.ext_attr;
f64edc8b
JW
10198 /* Replace symbols in array spec. */
10199 if (c->as)
713485cc
JW
10200 {
10201 int i;
10202 for (i = 0; i < c->as->rank; i++)
10203 {
f64edc8b
JW
10204 gfc_expr_replace_comp (c->as->lower[i], c);
10205 gfc_expr_replace_comp (c->as->upper[i], c);
713485cc 10206 }
f64edc8b 10207 }
713485cc 10208 /* Copy char length. */
bc21d315 10209 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
713485cc 10210 {
b76e28c6 10211 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
50dbf0b4 10212 gfc_expr_replace_comp (c->ts.u.cl->length, c);
713485cc
JW
10213 }
10214 }
10215 else if (c->ts.interface->name[0] != '\0')
10216 {
10217 gfc_error ("Interface '%s' of procedure pointer component "
10218 "'%s' at %L must be explicit", c->ts.interface->name,
10219 c->name, &c->loc);
10220 return FAILURE;
10221 }
10222 }
10223 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10224 {
6c036626
JW
10225 /* Since PPCs are not implicitly typed, a PPC without an explicit
10226 interface must be a subroutine. */
10227 gfc_add_subroutine (&c->attr, c->name, &c->loc);
713485cc
JW
10228 }
10229
90661f26
JW
10230 /* Procedure pointer components: Check PASS arg. */
10231 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10232 {
10233 gfc_symbol* me_arg;
10234
10235 if (c->tb->pass_arg)
10236 {
10237 gfc_formal_arglist* i;
10238
10239 /* If an explicit passing argument name is given, walk the arg-list
10240 and look for it. */
10241
10242 me_arg = NULL;
10243 c->tb->pass_arg_num = 1;
10244 for (i = c->formal; i; i = i->next)
10245 {
10246 if (!strcmp (i->sym->name, c->tb->pass_arg))
10247 {
10248 me_arg = i->sym;
10249 break;
10250 }
10251 c->tb->pass_arg_num++;
10252 }
10253
10254 if (!me_arg)
10255 {
10256 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10257 "at %L has no argument '%s'", c->name,
10258 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10259 c->tb->error = 1;
10260 return FAILURE;
10261 }
10262 }
10263 else
10264 {
10265 /* Otherwise, take the first one; there should in fact be at least
10266 one. */
10267 c->tb->pass_arg_num = 1;
10268 if (!c->formal)
10269 {
10270 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10271 "must have at least one argument",
10272 c->name, &c->loc);
10273 c->tb->error = 1;
10274 return FAILURE;
10275 }
10276 me_arg = c->formal->sym;
10277 }
10278
10279 /* Now check that the argument-type matches. */
10280 gcc_assert (me_arg);
cf2b3c22
TB
10281 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10282 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10283 || (me_arg->ts.type == BT_CLASS
10284 && me_arg->ts.u.derived->components->ts.u.derived != sym))
90661f26
JW
10285 {
10286 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10287 " the derived type '%s'", me_arg->name, c->name,
10288 me_arg->name, &c->loc, sym->name);
10289 c->tb->error = 1;
10290 return FAILURE;
10291 }
10292
10293 /* Check for C453. */
10294 if (me_arg->attr.dimension)
10295 {
10296 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10297 "must be scalar", me_arg->name, c->name, me_arg->name,
10298 &c->loc);
10299 c->tb->error = 1;
10300 return FAILURE;
10301 }
10302
10303 if (me_arg->attr.pointer)
10304 {
10305 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10306 "may not have the POINTER attribute", me_arg->name,
10307 c->name, me_arg->name, &c->loc);
10308 c->tb->error = 1;
10309 return FAILURE;
10310 }
10311
10312 if (me_arg->attr.allocatable)
10313 {
10314 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10315 "may not be ALLOCATABLE", me_arg->name, c->name,
10316 me_arg->name, &c->loc);
10317 c->tb->error = 1;
10318 return FAILURE;
10319 }
10320
cf2b3c22 10321 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
727e8544 10322 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
cf2b3c22 10323 " at %L", c->name, &c->loc);
90661f26
JW
10324
10325 }
10326
52f49934
DK
10327 /* Check type-spec if this is not the parent-type component. */
10328 if ((!sym->attr.extension || c != sym->components)
10329 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10330 return FAILURE;
10331
9d1210f4
DK
10332 /* If this type is an extension, see if this component has the same name
10333 as an inherited type-bound procedure. */
8e1f752a 10334 if (super_type
4a44a72d 10335 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9d1210f4
DK
10336 {
10337 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10338 " inherited type-bound procedure",
10339 c->name, sym->name, &c->loc);
10340 return FAILURE;
10341 }
10342
50dbf0b4 10343 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
110eec24 10344 {
bc21d315
JW
10345 if (c->ts.u.cl->length == NULL
10346 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10347 || !gfc_is_constant_expr (c->ts.u.cl->length))
110eec24
TS
10348 {
10349 gfc_error ("Character length of component '%s' needs to "
e25a0da3 10350 "be a constant specification expression at %L",
110eec24 10351 c->name,
bc21d315 10352 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
110eec24
TS
10353 return FAILURE;
10354 }
10355 }
10356
2ed8d224 10357 if (c->ts.type == BT_DERIVED
edf1eac2
SK
10358 && sym->component_access != ACCESS_PRIVATE
10359 && gfc_check_access (sym->attr.access, sym->ns->default_access)
bc21d315
JW
10360 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10361 && !c->ts.u.derived->attr.use_assoc
10362 && !gfc_check_access (c->ts.u.derived->attr.access,
10363 c->ts.u.derived->ns->default_access)
cbb9a26e
JW
10364 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10365 "is a PRIVATE type and cannot be a component of "
10366 "'%s', which is PUBLIC at %L", c->name,
10367 sym->name, &sym->declared_at) == FAILURE)
10368 return FAILURE;
2ed8d224 10369
f970c857
PT
10370 if (sym->attr.sequence)
10371 {
bc21d315 10372 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
f970c857
PT
10373 {
10374 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10375 "not have the SEQUENCE attribute",
bc21d315 10376 c->ts.u.derived->name, &sym->declared_at);
f970c857
PT
10377 return FAILURE;
10378 }
10379 }
10380
d4b7d0f0 10381 if (c->ts.type == BT_DERIVED && c->attr.pointer
bc21d315
JW
10382 && c->ts.u.derived->components == NULL
10383 && !c->ts.u.derived->attr.zero_comp)
982186b1
PT
10384 {
10385 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10386 "that has not been declared", c->name, sym->name,
10387 &c->loc);
10388 return FAILURE;
10389 }
10390
727e8544 10391 /* C437. */
cf2b3c22
TB
10392 if (c->ts.type == BT_CLASS
10393 && !(c->ts.u.derived->components->attr.pointer
10394 || c->ts.u.derived->components->attr.allocatable))
727e8544
JW
10395 {
10396 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10397 "or pointer", c->name, &c->loc);
10398 return FAILURE;
10399 }
10400
9d5c21c1
PT
10401 /* Ensure that all the derived type components are put on the
10402 derived type list; even in formal namespaces, where derived type
10403 pointer components might not have been declared. */
10404 if (c->ts.type == BT_DERIVED
bc21d315
JW
10405 && c->ts.u.derived
10406 && c->ts.u.derived->components
d4b7d0f0 10407 && c->attr.pointer
bc21d315
JW
10408 && sym != c->ts.u.derived)
10409 add_dt_to_dt_list (c->ts.u.derived);
9d5c21c1 10410
e35bbb23
JW
10411 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10412 || c->as == NULL)
2ed8d224
PT
10413 continue;
10414
10415 for (i = 0; i < c->as->rank; i++)
10416 {
10417 if (c->as->lower[i] == NULL
edf1eac2 10418 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
bdad0683 10419 || !gfc_is_constant_expr (c->as->lower[i])
edf1eac2
SK
10420 || c->as->upper[i] == NULL
10421 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10422 || !gfc_is_constant_expr (c->as->upper[i]))
2ed8d224
PT
10423 {
10424 gfc_error ("Component '%s' of '%s' at %L must have "
e25a0da3 10425 "constant array bounds",
2ed8d224
PT
10426 c->name, sym->name, &c->loc);
10427 return FAILURE;
10428 }
10429 }
110eec24 10430 }
05c1e3a7 10431
30b608eb
DK
10432 /* Resolve the type-bound procedures. */
10433 if (resolve_typebound_procedures (sym) == FAILURE)
10434 return FAILURE;
10435
34523524
DK
10436 /* Resolve the finalizer procedures. */
10437 if (gfc_resolve_finalizers (sym) == FAILURE)
10438 return FAILURE;
10439
b0e5fa94
DK
10440 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10441 all DEFERRED bindings are overridden. */
10442 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10443 && ensure_not_abstract (sym, super_type) == FAILURE)
10444 return FAILURE;
10445
6b887797 10446 /* Add derived type to the derived type list. */
9d5c21c1 10447 add_dt_to_dt_list (sym);
6b887797 10448
110eec24
TS
10449 return SUCCESS;
10450}
10451
2ed8d224 10452
17b1d2a0 10453static gfc_try
3e1cf500
PT
10454resolve_fl_namelist (gfc_symbol *sym)
10455{
10456 gfc_namelist *nl;
10457 gfc_symbol *nlsym;
10458
10459 /* Reject PRIVATE objects in a PUBLIC namelist. */
10460 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10461 {
10462 for (nl = sym->namelist; nl; nl = nl->next)
10463 {
3dbf6538 10464 if (!nl->sym->attr.use_assoc
c867b7b6 10465 && !is_sym_host_assoc (nl->sym, sym->ns)
3dbf6538 10466 && !gfc_check_access(nl->sym->attr.access,
5cca320d 10467 nl->sym->ns->default_access))
3e1cf500 10468 {
5cca320d
DF
10469 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10470 "cannot be member of PUBLIC namelist '%s' at %L",
10471 nl->sym->name, sym->name, &sym->declared_at);
10472 return FAILURE;
10473 }
10474
3dbf6538
DF
10475 /* Types with private components that came here by USE-association. */
10476 if (nl->sym->ts.type == BT_DERIVED
bc21d315 10477 && derived_inaccessible (nl->sym->ts.u.derived))
3dbf6538
DF
10478 {
10479 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10480 "components and cannot be member of namelist '%s' at %L",
10481 nl->sym->name, sym->name, &sym->declared_at);
10482 return FAILURE;
10483 }
10484
10485 /* Types with private components that are defined in the same module. */
5cca320d 10486 if (nl->sym->ts.type == BT_DERIVED
bc21d315
JW
10487 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10488 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
3dbf6538
DF
10489 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10490 nl->sym->ns->default_access))
5cca320d
DF
10491 {
10492 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10493 "cannot be a member of PUBLIC namelist '%s' at %L",
10494 nl->sym->name, sym->name, &sym->declared_at);
3e1cf500
PT
10495 return FAILURE;
10496 }
10497 }
10498 }
10499
5046aff5
PT
10500 for (nl = sym->namelist; nl; nl = nl->next)
10501 {
5cca320d
DF
10502 /* Reject namelist arrays of assumed shape. */
10503 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10504 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10505 "must not have assumed shape in namelist "
10506 "'%s' at %L", nl->sym->name, sym->name,
10507 &sym->declared_at) == FAILURE)
10508 return FAILURE;
10509
10510 /* Reject namelist arrays that are not constant shape. */
5046aff5
PT
10511 if (is_non_constant_shape_array (nl->sym))
10512 {
5cca320d
DF
10513 gfc_error ("NAMELIST array object '%s' must have constant "
10514 "shape in namelist '%s' at %L", nl->sym->name,
10515 sym->name, &sym->declared_at);
10516 return FAILURE;
10517 }
10518
10519 /* Namelist objects cannot have allocatable or pointer components. */
10520 if (nl->sym->ts.type != BT_DERIVED)
10521 continue;
10522
bc21d315 10523 if (nl->sym->ts.u.derived->attr.alloc_comp)
5cca320d
DF
10524 {
10525 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10526 "have ALLOCATABLE components",
10527 nl->sym->name, sym->name, &sym->declared_at);
5046aff5
PT
10528 return FAILURE;
10529 }
5046aff5 10530
bc21d315 10531 if (nl->sym->ts.u.derived->attr.pointer_comp)
5046aff5 10532 {
5cca320d
DF
10533 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10534 "have POINTER components",
10535 nl->sym->name, sym->name, &sym->declared_at);
5046aff5
PT
10536 return FAILURE;
10537 }
3e1cf500
PT
10538 }
10539
5cca320d 10540
3e1cf500 10541 /* 14.1.2 A module or internal procedure represent local entities
847b053d 10542 of the same type as a namelist member and so are not allowed. */
3e1cf500
PT
10543 for (nl = sym->namelist; nl; nl = nl->next)
10544 {
982186b1
PT
10545 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10546 continue;
847b053d
PT
10547
10548 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10549 if ((nl->sym == sym->ns->proc_name)
10550 ||
10551 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10552 continue;
10553
3e1cf500 10554 nlsym = NULL;
847b053d
PT
10555 if (nl->sym && nl->sym->name)
10556 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
982186b1
PT
10557 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10558 {
10559 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10560 "attribute in '%s' at %L", nlsym->name,
10561 &sym->declared_at);
10562 return FAILURE;
10563 }
3e1cf500
PT
10564 }
10565
10566 return SUCCESS;
10567}
10568
10569
17b1d2a0 10570static gfc_try
2ed8d224
PT
10571resolve_fl_parameter (gfc_symbol *sym)
10572{
10573 /* A parameter array's shape needs to be constant. */
c317bc40
DF
10574 if (sym->as != NULL
10575 && (sym->as->type == AS_DEFERRED
10576 || is_non_constant_shape_array (sym)))
2ed8d224
PT
10577 {
10578 gfc_error ("Parameter array '%s' at %L cannot be automatic "
c317bc40 10579 "or of deferred shape", sym->name, &sym->declared_at);
2ed8d224
PT
10580 return FAILURE;
10581 }
10582
10583 /* Make sure a parameter that has been implicitly typed still
10584 matches the implicit type, since PARAMETER statements can precede
10585 IMPLICIT statements. */
10586 if (sym->attr.implicit_type
713485cc
JW
10587 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10588 sym->ns)))
2ed8d224
PT
10589 {
10590 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10591 "later IMPLICIT type", sym->name, &sym->declared_at);
10592 return FAILURE;
10593 }
10594
10595 /* Make sure the types of derived parameters are consistent. This
10596 type checking is deferred until resolution because the type may
10597 refer to a derived type from the host. */
10598 if (sym->ts.type == BT_DERIVED
edf1eac2 10599 && !gfc_compare_types (&sym->ts, &sym->value->ts))
2ed8d224
PT
10600 {
10601 gfc_error ("Incompatible derived type in PARAMETER at %L",
10602 &sym->value->where);
10603 return FAILURE;
10604 }
10605 return SUCCESS;
10606}
10607
10608
6de9cd9a
DN
10609/* Do anything necessary to resolve a symbol. Right now, we just
10610 assume that an otherwise unknown symbol is a variable. This sort
10611 of thing commonly happens for symbols in module. */
10612
10613static void
edf1eac2 10614resolve_symbol (gfc_symbol *sym)
6de9cd9a 10615{
a34437a1 10616 int check_constant, mp_flag;
219fa8c3
SK
10617 gfc_symtree *symtree;
10618 gfc_symtree *this_symtree;
10619 gfc_namespace *ns;
10620 gfc_component *c;
6de9cd9a
DN
10621
10622 if (sym->attr.flavor == FL_UNKNOWN)
10623 {
24d36d28
PT
10624
10625 /* If we find that a flavorless symbol is an interface in one of the
10626 parent namespaces, find its symtree in this namespace, free the
10627 symbol and set the symtree to point to the interface symbol. */
10628 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10629 {
10630 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10631 if (symtree && symtree->n.sym->generic)
10632 {
10633 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10634 sym->name);
10635 sym->refs--;
10636 if (!sym->refs)
10637 gfc_free_symbol (sym);
10638 symtree->n.sym->refs++;
10639 this_symtree->n.sym = symtree->n.sym;
10640 return;
10641 }
10642 }
10643
10644 /* Otherwise give it a flavor according to such attributes as
10645 it has. */
6de9cd9a
DN
10646 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10647 sym->attr.flavor = FL_VARIABLE;
10648 else
10649 {
10650 sym->attr.flavor = FL_PROCEDURE;
10651 if (sym->attr.dimension)
10652 sym->attr.function = 1;
10653 }
10654 }
10655
c73b6478
JW
10656 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10657 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10658
32d99e68 10659 if (sym->attr.procedure && sym->ts.interface
69773742
JW
10660 && sym->attr.if_source != IFSRC_DECL)
10661 {
d1d919c3
JW
10662 if (sym->ts.interface == sym)
10663 {
10664 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10665 "interface", sym->name, &sym->declared_at);
10666 return;
10667 }
32d99e68 10668 if (sym->ts.interface->attr.procedure)
d1d919c3
JW
10669 {
10670 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10671 " in a later PROCEDURE statement", sym->ts.interface->name,
10672 sym->name,&sym->declared_at);
10673 return;
10674 }
ecf24057 10675
69773742 10676 /* Get the attributes from the interface (now resolved). */
713485cc
JW
10677 if (sym->ts.interface->attr.if_source
10678 || sym->ts.interface->attr.intrinsic)
69773742 10679 {
7db5da56 10680 gfc_symbol *ifc = sym->ts.interface;
c74b74a8 10681 resolve_symbol (ifc);
3afadac3
JW
10682
10683 if (ifc->attr.intrinsic)
c73b6478
JW
10684 resolve_intrinsic (ifc, &ifc->declared_at);
10685
e6a5e544
JW
10686 if (ifc->result)
10687 sym->ts = ifc->result->ts;
10688 else
10689 sym->ts = ifc->ts;
c73b6478
JW
10690 sym->ts.interface = ifc;
10691 sym->attr.function = ifc->attr.function;
10692 sym->attr.subroutine = ifc->attr.subroutine;
10693 gfc_copy_formal_args (sym, ifc);
3afadac3 10694
2d9bbb6b
TB
10695 sym->attr.allocatable = ifc->attr.allocatable;
10696 sym->attr.pointer = ifc->attr.pointer;
10697 sym->attr.pure = ifc->attr.pure;
10698 sym->attr.elemental = ifc->attr.elemental;
10699 sym->attr.dimension = ifc->attr.dimension;
10700 sym->attr.recursive = ifc->attr.recursive;
10701 sym->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 10702 sym->attr.ext_attr |= ifc->attr.ext_attr;
c6acea9d
JW
10703 /* Copy array spec. */
10704 sym->as = gfc_copy_array_spec (ifc->as);
10705 if (sym->as)
10706 {
10707 int i;
10708 for (i = 0; i < sym->as->rank; i++)
10709 {
10710 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10711 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10712 }
10713 }
10714 /* Copy char length. */
bc21d315 10715 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
c6acea9d 10716 {
b76e28c6 10717 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
bc21d315 10718 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
c6acea9d 10719 }
69773742 10720 }
32d99e68 10721 else if (sym->ts.interface->name[0] != '\0')
69773742
JW
10722 {
10723 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
32d99e68 10724 sym->ts.interface->name, sym->name, &sym->declared_at);
69773742
JW
10725 return;
10726 }
10727 }
10728
2ed8d224 10729 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
110eec24
TS
10730 return;
10731
6de9cd9a
DN
10732 /* Symbols that are module procedures with results (functions) have
10733 the types and array specification copied for type checking in
10734 procedures that call them, as well as for saving to a module
10735 file. These symbols can't stand the scrutiny that their results
10736 can. */
10737 mp_flag = (sym->result != NULL && sym->result != sym);
10738
eb2c598d
DF
10739
10740 /* Make sure that the intrinsic is consistent with its internal
10741 representation. This needs to be done before assigning a default
10742 type to avoid spurious warnings. */
f6038131
JW
10743 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10744 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10745 return;
eb2c598d 10746
6de9cd9a
DN
10747 /* Assign default type to symbols that need one and don't have one. */
10748 if (sym->ts.type == BT_UNKNOWN)
10749 {
10750 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
d3fcc995 10751 gfc_set_default_type (sym, 1, NULL);
6de9cd9a 10752
fc9c6e5d
JW
10753 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10754 && !sym->attr.function && !sym->attr.subroutine
10755 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10756 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10757
6de9cd9a
DN
10758 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10759 {
53096259
PT
10760 /* The specific case of an external procedure should emit an error
10761 in the case that there is no implicit type. */
6de9cd9a 10762 if (!mp_flag)
53096259 10763 gfc_set_default_type (sym, sym->attr.external, NULL);
6de9cd9a
DN
10764 else
10765 {
edf1eac2 10766 /* Result may be in another namespace. */
6de9cd9a
DN
10767 resolve_symbol (sym->result);
10768
3070bab4
JW
10769 if (!sym->result->attr.proc_pointer)
10770 {
10771 sym->ts = sym->result->ts;
10772 sym->as = gfc_copy_array_spec (sym->result->as);
10773 sym->attr.dimension = sym->result->attr.dimension;
10774 sym->attr.pointer = sym->result->attr.pointer;
10775 sym->attr.allocatable = sym->result->attr.allocatable;
10776 }
6de9cd9a
DN
10777 }
10778 }
10779 }
10780
f5e440e1 10781 /* Assumed size arrays and assumed shape arrays must be dummy
05c1e3a7 10782 arguments. */
f5e440e1 10783
6de9cd9a
DN
10784 if (sym->as != NULL
10785 && (sym->as->type == AS_ASSUMED_SIZE
10786 || sym->as->type == AS_ASSUMED_SHAPE)
10787 && sym->attr.dummy == 0)
10788 {
31043f6c
FXC
10789 if (sym->as->type == AS_ASSUMED_SIZE)
10790 gfc_error ("Assumed size array at %L must be a dummy argument",
10791 &sym->declared_at);
10792 else
10793 gfc_error ("Assumed shape array at %L must be a dummy argument",
10794 &sym->declared_at);
a4ac5dd3
TS
10795 return;
10796 }
10797
6de9cd9a
DN
10798 /* Make sure symbols with known intent or optional are really dummy
10799 variable. Because of ENTRY statement, this has to be deferred
10800 until resolution time. */
10801
2ed8d224 10802 if (!sym->attr.dummy
edf1eac2 10803 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
6de9cd9a
DN
10804 {
10805 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10806 return;
10807 }
10808
06469efd
PT
10809 if (sym->attr.value && !sym->attr.dummy)
10810 {
10811 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
1084b6b0 10812 "it is not a dummy argument", sym->name, &sym->declared_at);
06469efd
PT
10813 return;
10814 }
10815
1084b6b0
TB
10816 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10817 {
bc21d315 10818 gfc_charlen *cl = sym->ts.u.cl;
1084b6b0
TB
10819 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10820 {
10821 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10822 "attribute must have constant length",
10823 sym->name, &sym->declared_at);
10824 return;
10825 }
a8b3b0b6
CR
10826
10827 if (sym->ts.is_c_interop
10828 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10829 {
10830 gfc_error ("C interoperable character dummy variable '%s' at %L "
10831 "with VALUE attribute must have length one",
10832 sym->name, &sym->declared_at);
10833 return;
10834 }
10835 }
10836
10837 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
10838 do this for something that was implicitly typed because that is handled
10839 in gfc_set_default_type. Handle dummy arguments and procedure
10840 definitions separately. Also, anything that is use associated is not
10841 handled here but instead is handled in the module it is declared in.
10842 Finally, derived type definitions are allowed to be BIND(C) since that
10843 only implies that they're interoperable, and they are checked fully for
10844 interoperability when a variable is declared of that type. */
10845 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
10846 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
10847 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
10848 {
17b1d2a0 10849 gfc_try t = SUCCESS;
a8b3b0b6
CR
10850
10851 /* First, make sure the variable is declared at the
10852 module-level scope (J3/04-007, Section 15.3). */
10853 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
10854 sym->attr.in_common == 0)
10855 {
10856 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
10857 "is neither a COMMON block nor declared at the "
10858 "module level scope", sym->name, &(sym->declared_at));
10859 t = FAILURE;
10860 }
10861 else if (sym->common_head != NULL)
10862 {
10863 t = verify_com_block_vars_c_interop (sym->common_head);
10864 }
10865 else
10866 {
10867 /* If type() declaration, we need to verify that the components
10868 of the given type are all C interoperable, etc. */
10869 if (sym->ts.type == BT_DERIVED &&
bc21d315 10870 sym->ts.u.derived->attr.is_c_interop != 1)
a8b3b0b6
CR
10871 {
10872 /* Make sure the user marked the derived type as BIND(C). If
10873 not, call the verify routine. This could print an error
10874 for the derived type more than once if multiple variables
10875 of that type are declared. */
bc21d315
JW
10876 if (sym->ts.u.derived->attr.is_bind_c != 1)
10877 verify_bind_c_derived_type (sym->ts.u.derived);
a8b3b0b6
CR
10878 t = FAILURE;
10879 }
10880
10881 /* Verify the variable itself as C interoperable if it
10882 is BIND(C). It is not possible for this to succeed if
10883 the verify_bind_c_derived_type failed, so don't have to handle
10884 any error returned by verify_bind_c_derived_type. */
10885 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
10886 sym->common_block);
10887 }
10888
10889 if (t == FAILURE)
10890 {
10891 /* clear the is_bind_c flag to prevent reporting errors more than
10892 once if something failed. */
10893 sym->attr.is_bind_c = 0;
10894 return;
10895 }
1084b6b0
TB
10896 }
10897
976e21f6
PT
10898 /* If a derived type symbol has reached this point, without its
10899 type being declared, we have an error. Notice that most
10900 conditions that produce undefined derived types have already
10901 been dealt with. However, the likes of:
10902 implicit type(t) (t) ..... call foo (t) will get us here if
10903 the type is not declared in the scope of the implicit
10904 statement. Change the type to BT_UNKNOWN, both because it is so
10905 and to prevent an ICE. */
bc21d315
JW
10906 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
10907 && !sym->ts.u.derived->attr.zero_comp)
976e21f6
PT
10908 {
10909 gfc_error ("The derived type '%s' at %L is of type '%s', "
e25a0da3 10910 "which has not been defined", sym->name,
bc21d315 10911 &sym->declared_at, sym->ts.u.derived->name);
976e21f6
PT
10912 sym->ts.type = BT_UNKNOWN;
10913 return;
10914 }
10915
c1203a70
PT
10916 /* Make sure that the derived type has been resolved and that the
10917 derived type is visible in the symbol's namespace, if it is a
10918 module function and is not PRIVATE. */
10919 if (sym->ts.type == BT_DERIVED
bc21d315 10920 && sym->ts.u.derived->attr.use_assoc
96ffc6cd 10921 && sym->ns->proc_name
c1203a70
PT
10922 && sym->ns->proc_name->attr.flavor == FL_MODULE)
10923 {
10924 gfc_symbol *ds;
10925
bc21d315 10926 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
c1203a70
PT
10927 return;
10928
bc21d315 10929 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
c1203a70
PT
10930 if (!ds && sym->attr.function
10931 && gfc_check_access (sym->attr.access, sym->ns->default_access))
10932 {
10933 symtree = gfc_new_symtree (&sym->ns->sym_root,
bc21d315
JW
10934 sym->ts.u.derived->name);
10935 symtree->n.sym = sym->ts.u.derived;
10936 sym->ts.u.derived->refs++;
c1203a70
PT
10937 }
10938 }
10939
a08a5751
TB
10940 /* Unless the derived-type declaration is use associated, Fortran 95
10941 does not allow public entries of private derived types.
10942 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
10943 161 in 95-006r3. */
10944 if (sym->ts.type == BT_DERIVED
72052237 10945 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
bc21d315 10946 && !sym->ts.u.derived->attr.use_assoc
a08a5751 10947 && gfc_check_access (sym->attr.access, sym->ns->default_access)
bc21d315
JW
10948 && !gfc_check_access (sym->ts.u.derived->attr.access,
10949 sym->ts.u.derived->ns->default_access)
a08a5751
TB
10950 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
10951 "of PRIVATE derived type '%s'",
10952 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
10953 : "variable", sym->name, &sym->declared_at,
bc21d315 10954 sym->ts.u.derived->name) == FAILURE)
a08a5751
TB
10955 return;
10956
4213f93b
PT
10957 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
10958 default initialization is defined (5.1.2.4.4). */
10959 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
10960 && sym->attr.dummy
10961 && sym->attr.intent == INTENT_OUT
10962 && sym->as
10963 && sym->as->type == AS_ASSUMED_SIZE)
4213f93b 10964 {
bc21d315 10965 for (c = sym->ts.u.derived->components; c; c = c->next)
4213f93b
PT
10966 {
10967 if (c->initializer)
10968 {
10969 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
10970 "ASSUMED SIZE and so cannot have a default initializer",
10971 sym->name, &sym->declared_at);
10972 return;
10973 }
10974 }
10975 }
10976
af30f793 10977 switch (sym->attr.flavor)
54b4ba60 10978 {
af30f793 10979 case FL_VARIABLE:
2ed8d224
PT
10980 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
10981 return;
10982 break;
219fa8c3 10983
2ed8d224
PT
10984 case FL_PROCEDURE:
10985 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
10986 return;
af30f793
PB
10987 break;
10988
10989 case FL_NAMELIST:
3e1cf500
PT
10990 if (resolve_fl_namelist (sym) == FAILURE)
10991 return;
68ea355b
PT
10992 break;
10993
2ed8d224
PT
10994 case FL_PARAMETER:
10995 if (resolve_fl_parameter (sym) == FAILURE)
10996 return;
e0e85e06
PT
10997 break;
10998
af30f793
PB
10999 default:
11000 break;
54b4ba60
PB
11001 }
11002
6de9cd9a 11003 /* Resolve array specifier. Check as well some constraints
f7b529fa 11004 on COMMON blocks. */
6de9cd9a
DN
11005
11006 check_constant = sym->attr.in_common && !sym->attr.pointer;
98bbe5ee
PT
11007
11008 /* Set the formal_arg_flag so that check_conflict will not throw
11009 an error for host associated variables in the specification
11010 expression for an array_valued function. */
11011 if (sym->attr.function && sym->as)
11012 formal_arg_flag = 1;
11013
6de9cd9a
DN
11014 gfc_resolve_array_spec (sym->as, check_constant);
11015
98bbe5ee
PT
11016 formal_arg_flag = 0;
11017
a34437a1 11018 /* Resolve formal namespaces. */
f6ddbf11 11019 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
e4c1aa19 11020 && !sym->attr.contained && !sym->attr.intrinsic)
a34437a1 11021 gfc_resolve (sym->formal_ns);
6c7a4dfd 11022
acbdc378
JW
11023 /* Make sure the formal namespace is present. */
11024 if (sym->formal && !sym->formal_ns)
11025 {
11026 gfc_formal_arglist *formal = sym->formal;
11027 while (formal && !formal->sym)
11028 formal = formal->next;
11029
11030 if (formal)
11031 {
11032 sym->formal_ns = formal->sym->ns;
11033 sym->formal_ns->refs++;
11034 }
11035 }
11036
6c7a4dfd 11037 /* Check threadprivate restrictions. */
5349080d 11038 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
6c7a4dfd 11039 && (!sym->attr.in_common
edf1eac2
SK
11040 && sym->module == NULL
11041 && (sym->ns->proc_name == NULL
11042 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
6c7a4dfd 11043 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
6b591ec0
PT
11044
11045 /* If we have come this far we can apply default-initializers, as
11046 described in 14.7.5, to those variables that have not already
11047 been assigned one. */
7114edca 11048 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
11049 && sym->attr.referenced
11050 && sym->ns == gfc_current_ns
11051 && !sym->value
11052 && !sym->attr.allocatable
11053 && !sym->attr.alloc_comp)
6b591ec0
PT
11054 {
11055 symbol_attribute *a = &sym->attr;
11056
11057 if ((!a->save && !a->dummy && !a->pointer
edf1eac2
SK
11058 && !a->in_common && !a->use_assoc
11059 && !(a->function && sym != sym->result))
758e12af 11060 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
6b591ec0
PT
11061 apply_default_init (sym);
11062 }
52f49934
DK
11063
11064 /* If this symbol has a type-spec, check it. */
11065 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11066 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11067 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11068 == FAILURE)
11069 return;
6de9cd9a
DN
11070}
11071
11072
6de9cd9a
DN
11073/************* Resolve DATA statements *************/
11074
11075static struct
11076{
11077 gfc_data_value *vnode;
f2112868 11078 mpz_t left;
6de9cd9a
DN
11079}
11080values;
11081
11082
11083/* Advance the values structure to point to the next value in the data list. */
11084
17b1d2a0 11085static gfc_try
6de9cd9a
DN
11086next_data_value (void)
11087{
f2112868 11088 while (mpz_cmp_ui (values.left, 0) == 0)
6de9cd9a 11089 {
abeab938 11090
6de9cd9a
DN
11091 if (values.vnode->next == NULL)
11092 return FAILURE;
11093
11094 values.vnode = values.vnode->next;
f2112868 11095 mpz_set (values.left, values.vnode->repeat);
6de9cd9a
DN
11096 }
11097
6de9cd9a
DN
11098 return SUCCESS;
11099}
11100
11101
17b1d2a0 11102static gfc_try
edf1eac2 11103check_data_variable (gfc_data_variable *var, locus *where)
6de9cd9a
DN
11104{
11105 gfc_expr *e;
11106 mpz_t size;
11107 mpz_t offset;
17b1d2a0 11108 gfc_try t;
f5e440e1 11109 ar_type mark = AR_UNKNOWN;
6de9cd9a
DN
11110 int i;
11111 mpz_t section_index[GFC_MAX_DIMENSIONS];
11112 gfc_ref *ref;
11113 gfc_array_ref *ar;
e49be8f7
PT
11114 gfc_symbol *sym;
11115 int has_pointer;
6de9cd9a
DN
11116
11117 if (gfc_resolve_expr (var->expr) == FAILURE)
11118 return FAILURE;
11119
11120 ar = NULL;
11121 mpz_init_set_si (offset, 0);
11122 e = var->expr;
11123
11124 if (e->expr_type != EXPR_VARIABLE)
11125 gfc_internal_error ("check_data_variable(): Bad expression");
11126
e49be8f7
PT
11127 sym = e->symtree->n.sym;
11128
11129 if (sym->ns->is_block_data && !sym->attr.in_common)
2ed8d224
PT
11130 {
11131 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
e49be8f7 11132 sym->name, &sym->declared_at);
2ed8d224
PT
11133 }
11134
e49be8f7 11135 if (e->ref == NULL && sym->as)
f1607c01
JD
11136 {
11137 gfc_error ("DATA array '%s' at %L must be specified in a previous"
e49be8f7 11138 " declaration", sym->name, where);
f1607c01
JD
11139 return FAILURE;
11140 }
11141
e49be8f7
PT
11142 has_pointer = sym->attr.pointer;
11143
11144 for (ref = e->ref; ref; ref = ref->next)
11145 {
11146 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11147 has_pointer = 1;
11148
11149 if (has_pointer
11150 && ref->type == REF_ARRAY
11151 && ref->u.ar.type != AR_FULL)
11152 {
11153 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11154 "be a full array", sym->name, where);
11155 return FAILURE;
11156 }
11157 }
11158
11159 if (e->rank == 0 || has_pointer)
b8502435
RH
11160 {
11161 mpz_init_set_ui (size, 1);
11162 ref = NULL;
11163 }
6de9cd9a
DN
11164 else
11165 {
11166 ref = e->ref;
11167
11168 /* Find the array section reference. */
11169 for (ref = e->ref; ref; ref = ref->next)
11170 {
11171 if (ref->type != REF_ARRAY)
11172 continue;
11173 if (ref->u.ar.type == AR_ELEMENT)
11174 continue;
11175 break;
11176 }
6e45f57b 11177 gcc_assert (ref);
6de9cd9a 11178
1f2959f0 11179 /* Set marks according to the reference pattern. */
6de9cd9a
DN
11180 switch (ref->u.ar.type)
11181 {
11182 case AR_FULL:
f5e440e1 11183 mark = AR_FULL;
6de9cd9a
DN
11184 break;
11185
11186 case AR_SECTION:
edf1eac2
SK
11187 ar = &ref->u.ar;
11188 /* Get the start position of array section. */
11189 gfc_get_section_index (ar, section_index, &offset);
11190 mark = AR_SECTION;
6de9cd9a
DN
11191 break;
11192
11193 default:
6e45f57b 11194 gcc_unreachable ();
6de9cd9a
DN
11195 }
11196
11197 if (gfc_array_size (e, &size) == FAILURE)
11198 {
11199 gfc_error ("Nonconstant array section at %L in DATA statement",
11200 &e->where);
11201 mpz_clear (offset);
11202 return FAILURE;
11203 }
11204 }
11205
11206 t = SUCCESS;
11207
11208 while (mpz_cmp_ui (size, 0) > 0)
11209 {
11210 if (next_data_value () == FAILURE)
11211 {
11212 gfc_error ("DATA statement at %L has more variables than values",
11213 where);
11214 t = FAILURE;
11215 break;
11216 }
11217
11218 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11219 if (t == FAILURE)
11220 break;
11221
b8502435
RH
11222 /* If we have more than one element left in the repeat count,
11223 and we have more than one element left in the target variable,
11224 then create a range assignment. */
f2112868 11225 /* FIXME: Only done for full arrays for now, since array sections
b8502435
RH
11226 seem tricky. */
11227 if (mark == AR_FULL && ref && ref->next == NULL
f2112868 11228 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
b8502435
RH
11229 {
11230 mpz_t range;
11231
f2112868 11232 if (mpz_cmp (size, values.left) >= 0)
b8502435 11233 {
f2112868
SK
11234 mpz_init_set (range, values.left);
11235 mpz_sub (size, size, values.left);
11236 mpz_set_ui (values.left, 0);
b8502435
RH
11237 }
11238 else
11239 {
11240 mpz_init_set (range, size);
f2112868 11241 mpz_sub (values.left, values.left, size);
b8502435
RH
11242 mpz_set_ui (size, 0);
11243 }
11244
11245 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11246 offset, range);
11247
11248 mpz_add (offset, offset, range);
11249 mpz_clear (range);
11250 }
11251
6de9cd9a 11252 /* Assign initial value to symbol. */
b8502435
RH
11253 else
11254 {
f2112868 11255 mpz_sub_ui (values.left, values.left, 1);
b8502435 11256 mpz_sub_ui (size, size, 1);
6de9cd9a 11257
a24668a3
JD
11258 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11259 if (t == FAILURE)
11260 break;
6de9cd9a 11261
b8502435
RH
11262 if (mark == AR_FULL)
11263 mpz_add_ui (offset, offset, 1);
6de9cd9a 11264
b8502435
RH
11265 /* Modify the array section indexes and recalculate the offset
11266 for next element. */
11267 else if (mark == AR_SECTION)
11268 gfc_advance_section (section_index, ar, &offset);
11269 }
6de9cd9a 11270 }
b8502435 11271
f5e440e1 11272 if (mark == AR_SECTION)
6de9cd9a
DN
11273 {
11274 for (i = 0; i < ar->dimen; i++)
edf1eac2 11275 mpz_clear (section_index[i]);
6de9cd9a
DN
11276 }
11277
11278 mpz_clear (size);
11279 mpz_clear (offset);
11280
11281 return t;
11282}
11283
11284
17b1d2a0 11285static gfc_try traverse_data_var (gfc_data_variable *, locus *);
6de9cd9a
DN
11286
11287/* Iterate over a list of elements in a DATA statement. */
11288
17b1d2a0 11289static gfc_try
edf1eac2 11290traverse_data_list (gfc_data_variable *var, locus *where)
6de9cd9a
DN
11291{
11292 mpz_t trip;
11293 iterator_stack frame;
2220652d 11294 gfc_expr *e, *start, *end, *step;
17b1d2a0 11295 gfc_try retval = SUCCESS;
6de9cd9a
DN
11296
11297 mpz_init (frame.value);
11298
2220652d
PT
11299 start = gfc_copy_expr (var->iter.start);
11300 end = gfc_copy_expr (var->iter.end);
11301 step = gfc_copy_expr (var->iter.step);
11302
11303 if (gfc_simplify_expr (start, 1) == FAILURE
edf1eac2 11304 || start->expr_type != EXPR_CONSTANT)
2220652d 11305 {
edf1eac2 11306 gfc_error ("iterator start at %L does not simplify", &start->where);
2220652d
PT
11307 retval = FAILURE;
11308 goto cleanup;
11309 }
11310 if (gfc_simplify_expr (end, 1) == FAILURE
edf1eac2 11311 || end->expr_type != EXPR_CONSTANT)
2220652d 11312 {
edf1eac2 11313 gfc_error ("iterator end at %L does not simplify", &end->where);
2220652d
PT
11314 retval = FAILURE;
11315 goto cleanup;
11316 }
11317 if (gfc_simplify_expr (step, 1) == FAILURE
edf1eac2 11318 || step->expr_type != EXPR_CONSTANT)
2220652d 11319 {
edf1eac2 11320 gfc_error ("iterator step at %L does not simplify", &step->where);
2220652d
PT
11321 retval = FAILURE;
11322 goto cleanup;
11323 }
11324
11325 mpz_init_set (trip, end->value.integer);
11326 mpz_sub (trip, trip, start->value.integer);
11327 mpz_add (trip, trip, step->value.integer);
6de9cd9a 11328
2220652d 11329 mpz_div (trip, trip, step->value.integer);
6de9cd9a 11330
2220652d 11331 mpz_set (frame.value, start->value.integer);
6de9cd9a
DN
11332
11333 frame.prev = iter_stack;
11334 frame.variable = var->iter.var->symtree;
11335 iter_stack = &frame;
11336
11337 while (mpz_cmp_ui (trip, 0) > 0)
11338 {
11339 if (traverse_data_var (var->list, where) == FAILURE)
11340 {
11341 mpz_clear (trip);
2220652d
PT
11342 retval = FAILURE;
11343 goto cleanup;
6de9cd9a
DN
11344 }
11345
11346 e = gfc_copy_expr (var->expr);
11347 if (gfc_simplify_expr (e, 1) == FAILURE)
2220652d
PT
11348 {
11349 gfc_free_expr (e);
11350 mpz_clear (trip);
11351 retval = FAILURE;
11352 goto cleanup;
11353 }
6de9cd9a 11354
2220652d 11355 mpz_add (frame.value, frame.value, step->value.integer);
6de9cd9a
DN
11356
11357 mpz_sub_ui (trip, trip, 1);
11358 }
11359
11360 mpz_clear (trip);
2220652d 11361cleanup:
6de9cd9a
DN
11362 mpz_clear (frame.value);
11363
2220652d
PT
11364 gfc_free_expr (start);
11365 gfc_free_expr (end);
11366 gfc_free_expr (step);
11367
6de9cd9a 11368 iter_stack = frame.prev;
2220652d 11369 return retval;
6de9cd9a
DN
11370}
11371
11372
11373/* Type resolve variables in the variable list of a DATA statement. */
11374
17b1d2a0 11375static gfc_try
edf1eac2 11376traverse_data_var (gfc_data_variable *var, locus *where)
6de9cd9a 11377{
17b1d2a0 11378 gfc_try t;
6de9cd9a
DN
11379
11380 for (; var; var = var->next)
11381 {
11382 if (var->expr == NULL)
11383 t = traverse_data_list (var, where);
11384 else
11385 t = check_data_variable (var, where);
11386
11387 if (t == FAILURE)
11388 return FAILURE;
11389 }
11390
11391 return SUCCESS;
11392}
11393
11394
11395/* Resolve the expressions and iterators associated with a data statement.
11396 This is separate from the assignment checking because data lists should
11397 only be resolved once. */
11398
17b1d2a0 11399static gfc_try
edf1eac2 11400resolve_data_variables (gfc_data_variable *d)
6de9cd9a 11401{
6de9cd9a
DN
11402 for (; d; d = d->next)
11403 {
11404 if (d->list == NULL)
11405 {
11406 if (gfc_resolve_expr (d->expr) == FAILURE)
11407 return FAILURE;
11408 }
11409 else
11410 {
8d5cfa27 11411 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
6de9cd9a
DN
11412 return FAILURE;
11413
6de9cd9a
DN
11414 if (resolve_data_variables (d->list) == FAILURE)
11415 return FAILURE;
11416 }
11417 }
11418
11419 return SUCCESS;
11420}
11421
11422
11423/* Resolve a single DATA statement. We implement this by storing a pointer to
11424 the value list into static variables, and then recursively traversing the
11425 variables list, expanding iterators and such. */
11426
11427static void
f2112868 11428resolve_data (gfc_data *d)
6de9cd9a 11429{
f2112868 11430
6de9cd9a
DN
11431 if (resolve_data_variables (d->var) == FAILURE)
11432 return;
11433
11434 values.vnode = d->value;
f2112868
SK
11435 if (d->value == NULL)
11436 mpz_set_ui (values.left, 0);
11437 else
11438 mpz_set (values.left, d->value->repeat);
6de9cd9a
DN
11439
11440 if (traverse_data_var (d->var, &d->where) == FAILURE)
11441 return;
11442
11443 /* At this point, we better not have any values left. */
11444
11445 if (next_data_value () == SUCCESS)
11446 gfc_error ("DATA statement at %L has more values than variables",
11447 &d->where);
11448}
11449
11450
d2088bb6
PT
11451/* 12.6 Constraint: In a pure subprogram any variable which is in common or
11452 accessed by host or use association, is a dummy argument to a pure function,
11453 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11454 is storage associated with any such variable, shall not be used in the
11455 following contexts: (clients of this function). */
11456
df2fba9e 11457/* Determines if a variable is not 'pure', i.e., not assignable within a pure
edf1eac2
SK
11458 procedure. Returns zero if assignment is OK, nonzero if there is a
11459 problem. */
6de9cd9a 11460int
edf1eac2 11461gfc_impure_variable (gfc_symbol *sym)
6de9cd9a 11462{
d2088bb6
PT
11463 gfc_symbol *proc;
11464
6de9cd9a
DN
11465 if (sym->attr.use_assoc || sym->attr.in_common)
11466 return 1;
11467
11468 if (sym->ns != gfc_current_ns)
11469 return !sym->attr.function;
11470
d2088bb6
PT
11471 proc = sym->ns->proc_name;
11472 if (sym->attr.dummy && gfc_pure (proc)
11473 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11474 ||
11475 proc->attr.function))
11476 return 1;
6de9cd9a 11477
d2088bb6
PT
11478 /* TODO: Sort out what can be storage associated, if anything, and include
11479 it here. In principle equivalences should be scanned but it does not
11480 seem to be possible to storage associate an impure variable this way. */
6de9cd9a
DN
11481 return 0;
11482}
11483
11484
11485/* Test whether a symbol is pure or not. For a NULL pointer, checks the
11486 symbol of the current procedure. */
11487
11488int
edf1eac2 11489gfc_pure (gfc_symbol *sym)
6de9cd9a
DN
11490{
11491 symbol_attribute attr;
11492
11493 if (sym == NULL)
11494 sym = gfc_current_ns->proc_name;
11495 if (sym == NULL)
11496 return 0;
11497
11498 attr = sym->attr;
11499
11500 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11501}
11502
11503
11504/* Test whether the current procedure is elemental or not. */
11505
11506int
edf1eac2 11507gfc_elemental (gfc_symbol *sym)
6de9cd9a
DN
11508{
11509 symbol_attribute attr;
11510
11511 if (sym == NULL)
11512 sym = gfc_current_ns->proc_name;
11513 if (sym == NULL)
11514 return 0;
11515 attr = sym->attr;
11516
11517 return attr.flavor == FL_PROCEDURE && attr.elemental;
11518}
11519
11520
11521/* Warn about unused labels. */
11522
11523static void
edf1eac2 11524warn_unused_fortran_label (gfc_st_label *label)
6de9cd9a 11525{
5cf54585 11526 if (label == NULL)
6de9cd9a
DN
11527 return;
11528
994c1cc0 11529 warn_unused_fortran_label (label->left);
6de9cd9a 11530
5cf54585
TS
11531 if (label->defined == ST_LABEL_UNKNOWN)
11532 return;
6de9cd9a 11533
5cf54585
TS
11534 switch (label->referenced)
11535 {
11536 case ST_LABEL_UNKNOWN:
11537 gfc_warning ("Label %d at %L defined but not used", label->value,
11538 &label->where);
11539 break;
6de9cd9a 11540
5cf54585
TS
11541 case ST_LABEL_BAD_TARGET:
11542 gfc_warning ("Label %d at %L defined but cannot be used",
11543 label->value, &label->where);
11544 break;
6de9cd9a 11545
5cf54585
TS
11546 default:
11547 break;
6de9cd9a 11548 }
5cf54585 11549
994c1cc0 11550 warn_unused_fortran_label (label->right);
6de9cd9a
DN
11551}
11552
11553
e8ec07e1
PT
11554/* Returns the sequence type of a symbol or sequence. */
11555
11556static seq_type
11557sequence_type (gfc_typespec ts)
11558{
11559 seq_type result;
11560 gfc_component *c;
11561
11562 switch (ts.type)
11563 {
11564 case BT_DERIVED:
11565
bc21d315 11566 if (ts.u.derived->components == NULL)
e8ec07e1
PT
11567 return SEQ_NONDEFAULT;
11568
bc21d315
JW
11569 result = sequence_type (ts.u.derived->components->ts);
11570 for (c = ts.u.derived->components->next; c; c = c->next)
e8ec07e1
PT
11571 if (sequence_type (c->ts) != result)
11572 return SEQ_MIXED;
11573
11574 return result;
11575
11576 case BT_CHARACTER:
11577 if (ts.kind != gfc_default_character_kind)
11578 return SEQ_NONDEFAULT;
11579
11580 return SEQ_CHARACTER;
11581
11582 case BT_INTEGER:
11583 if (ts.kind != gfc_default_integer_kind)
11584 return SEQ_NONDEFAULT;
11585
11586 return SEQ_NUMERIC;
11587
11588 case BT_REAL:
11589 if (!(ts.kind == gfc_default_real_kind
edf1eac2 11590 || ts.kind == gfc_default_double_kind))
e8ec07e1
PT
11591 return SEQ_NONDEFAULT;
11592
11593 return SEQ_NUMERIC;
11594
11595 case BT_COMPLEX:
11596 if (ts.kind != gfc_default_complex_kind)
11597 return SEQ_NONDEFAULT;
11598
11599 return SEQ_NUMERIC;
11600
11601 case BT_LOGICAL:
11602 if (ts.kind != gfc_default_logical_kind)
11603 return SEQ_NONDEFAULT;
11604
11605 return SEQ_NUMERIC;
11606
11607 default:
11608 return SEQ_NONDEFAULT;
11609 }
11610}
11611
11612
6de9cd9a
DN
11613/* Resolve derived type EQUIVALENCE object. */
11614
17b1d2a0 11615static gfc_try
6de9cd9a
DN
11616resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11617{
6de9cd9a
DN
11618 gfc_component *c = derived->components;
11619
11620 if (!derived)
11621 return SUCCESS;
11622
11623 /* Shall not be an object of nonsequence derived type. */
11624 if (!derived->attr.sequence)
11625 {
11626 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
edf1eac2
SK
11627 "attribute to be an EQUIVALENCE object", sym->name,
11628 &e->where);
6de9cd9a
DN
11629 return FAILURE;
11630 }
11631
66e4ab31 11632 /* Shall not have allocatable components. */
5046aff5
PT
11633 if (derived->attr.alloc_comp)
11634 {
11635 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
edf1eac2
SK
11636 "components to be an EQUIVALENCE object",sym->name,
11637 &e->where);
5046aff5
PT
11638 return FAILURE;
11639 }
11640
bc21d315 11641 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
cddcf0d4
TB
11642 {
11643 gfc_error ("Derived type variable '%s' at %L with default "
11644 "initialization cannot be in EQUIVALENCE with a variable "
11645 "in COMMON", sym->name, &e->where);
11646 return FAILURE;
11647 }
11648
6de9cd9a
DN
11649 for (; c ; c = c->next)
11650 {
bc21d315
JW
11651 if (c->ts.type == BT_DERIVED
11652 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
edf1eac2 11653 return FAILURE;
05c1e3a7 11654
6de9cd9a 11655 /* Shall not be an object of sequence derived type containing a pointer
edf1eac2 11656 in the structure. */
d4b7d0f0 11657 if (c->attr.pointer)
edf1eac2
SK
11658 {
11659 gfc_error ("Derived type variable '%s' at %L with pointer "
11660 "component(s) cannot be an EQUIVALENCE object",
11661 sym->name, &e->where);
11662 return FAILURE;
11663 }
6de9cd9a
DN
11664 }
11665 return SUCCESS;
11666}
11667
11668
11669/* Resolve equivalence object.
e8ec07e1
PT
11670 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11671 an allocatable array, an object of nonsequence derived type, an object of
6de9cd9a
DN
11672 sequence derived type containing a pointer at any level of component
11673 selection, an automatic object, a function name, an entry name, a result
11674 name, a named constant, a structure component, or a subobject of any of
e8ec07e1
PT
11675 the preceding objects. A substring shall not have length zero. A
11676 derived type shall not have components with default initialization nor
11677 shall two objects of an equivalence group be initialized.
ee7e677f 11678 Either all or none of the objects shall have an protected attribute.
e8ec07e1
PT
11679 The simple constraints are done in symbol.c(check_conflict) and the rest
11680 are implemented here. */
6de9cd9a
DN
11681
11682static void
11683resolve_equivalence (gfc_equiv *eq)
11684{
11685 gfc_symbol *sym;
e8ec07e1 11686 gfc_symbol *first_sym;
6de9cd9a
DN
11687 gfc_expr *e;
11688 gfc_ref *r;
e8ec07e1
PT
11689 locus *last_where = NULL;
11690 seq_type eq_type, last_eq_type;
11691 gfc_typespec *last_ts;
ee7e677f 11692 int object, cnt_protected;
e8ec07e1
PT
11693 const char *value_name;
11694 const char *msg;
11695
11696 value_name = NULL;
11697 last_ts = &eq->expr->symtree->n.sym->ts;
6de9cd9a 11698
e8ec07e1
PT
11699 first_sym = eq->expr->symtree->n.sym;
11700
ee7e677f
TB
11701 cnt_protected = 0;
11702
e8ec07e1 11703 for (object = 1; eq; eq = eq->eq, object++)
6de9cd9a
DN
11704 {
11705 e = eq->expr;
a8006d09
JJ
11706
11707 e->ts = e->symtree->n.sym->ts;
11708 /* match_varspec might not know yet if it is seeing
11709 array reference or substring reference, as it doesn't
11710 know the types. */
11711 if (e->ref && e->ref->type == REF_ARRAY)
11712 {
11713 gfc_ref *ref = e->ref;
11714 sym = e->symtree->n.sym;
11715
11716 if (sym->attr.dimension)
11717 {
11718 ref->u.ar.as = sym->as;
11719 ref = ref->next;
11720 }
11721
11722 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11723 if (e->ts.type == BT_CHARACTER
11724 && ref
11725 && ref->type == REF_ARRAY
11726 && ref->u.ar.dimen == 1
11727 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11728 && ref->u.ar.stride[0] == NULL)
11729 {
11730 gfc_expr *start = ref->u.ar.start[0];
11731 gfc_expr *end = ref->u.ar.end[0];
11732 void *mem = NULL;
11733
11734 /* Optimize away the (:) reference. */
11735 if (start == NULL && end == NULL)
11736 {
11737 if (e->ref == ref)
11738 e->ref = ref->next;
11739 else
11740 e->ref->next = ref->next;
11741 mem = ref;
11742 }
11743 else
11744 {
11745 ref->type = REF_SUBSTRING;
11746 if (start == NULL)
11747 start = gfc_int_expr (1);
11748 ref->u.ss.start = start;
bc21d315
JW
11749 if (end == NULL && e->ts.u.cl)
11750 end = gfc_copy_expr (e->ts.u.cl->length);
a8006d09 11751 ref->u.ss.end = end;
bc21d315
JW
11752 ref->u.ss.length = e->ts.u.cl;
11753 e->ts.u.cl = NULL;
a8006d09
JJ
11754 }
11755 ref = ref->next;
11756 gfc_free (mem);
11757 }
11758
11759 /* Any further ref is an error. */
11760 if (ref)
11761 {
11762 gcc_assert (ref->type == REF_ARRAY);
11763 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11764 &ref->u.ar.where);
11765 continue;
11766 }
11767 }
11768
6de9cd9a 11769 if (gfc_resolve_expr (e) == FAILURE)
edf1eac2 11770 continue;
6de9cd9a
DN
11771
11772 sym = e->symtree->n.sym;
6de9cd9a 11773
9aa433c2 11774 if (sym->attr.is_protected)
ee7e677f
TB
11775 cnt_protected++;
11776 if (cnt_protected > 0 && cnt_protected != object)
11777 {
11778 gfc_error ("Either all or none of the objects in the "
11779 "EQUIVALENCE set at %L shall have the "
11780 "PROTECTED attribute",
11781 &e->where);
11782 break;
edf1eac2 11783 }
ee7e677f 11784
e8ec07e1 11785 /* Shall not equivalence common block variables in a PURE procedure. */
05c1e3a7 11786 if (sym->ns->proc_name
edf1eac2
SK
11787 && sym->ns->proc_name->attr.pure
11788 && sym->attr.in_common)
11789 {
11790 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
e8ec07e1
PT
11791 "object in the pure procedure '%s'",
11792 sym->name, &e->where, sym->ns->proc_name->name);
edf1eac2
SK
11793 break;
11794 }
05c1e3a7
BF
11795
11796 /* Shall not be a named constant. */
6de9cd9a 11797 if (e->expr_type == EXPR_CONSTANT)
edf1eac2
SK
11798 {
11799 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11800 "object", sym->name, &e->where);
11801 continue;
11802 }
6de9cd9a 11803
bc21d315
JW
11804 if (e->ts.type == BT_DERIVED
11805 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
edf1eac2 11806 continue;
6de9cd9a 11807
e8ec07e1
PT
11808 /* Check that the types correspond correctly:
11809 Note 5.28:
11810 A numeric sequence structure may be equivalenced to another sequence
11811 structure, an object of default integer type, default real type, double
11812 precision real type, default logical type such that components of the
11813 structure ultimately only become associated to objects of the same
11814 kind. A character sequence structure may be equivalenced to an object
11815 of default character kind or another character sequence structure.
11816 Other objects may be equivalenced only to objects of the same type and
11817 kind parameters. */
11818
11819 /* Identical types are unconditionally OK. */
11820 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11821 goto identical_types;
11822
11823 last_eq_type = sequence_type (*last_ts);
11824 eq_type = sequence_type (sym->ts);
11825
11826 /* Since the pair of objects is not of the same type, mixed or
11827 non-default sequences can be rejected. */
11828
11829 msg = "Sequence %s with mixed components in EQUIVALENCE "
11830 "statement at %L with different type objects";
11831 if ((object ==2
edf1eac2
SK
11832 && last_eq_type == SEQ_MIXED
11833 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
11834 == FAILURE)
11835 || (eq_type == SEQ_MIXED
11836 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11837 &e->where) == FAILURE))
e8ec07e1
PT
11838 continue;
11839
11840 msg = "Non-default type object or sequence %s in EQUIVALENCE "
11841 "statement at %L with objects of different type";
11842 if ((object ==2
edf1eac2
SK
11843 && last_eq_type == SEQ_NONDEFAULT
11844 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
11845 last_where) == FAILURE)
11846 || (eq_type == SEQ_NONDEFAULT
11847 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11848 &e->where) == FAILURE))
e8ec07e1
PT
11849 continue;
11850
11851 msg ="Non-CHARACTER object '%s' in default CHARACTER "
11852 "EQUIVALENCE statement at %L";
11853 if (last_eq_type == SEQ_CHARACTER
edf1eac2
SK
11854 && eq_type != SEQ_CHARACTER
11855 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11856 &e->where) == FAILURE)
e8ec07e1
PT
11857 continue;
11858
11859 msg ="Non-NUMERIC object '%s' in default NUMERIC "
11860 "EQUIVALENCE statement at %L";
11861 if (last_eq_type == SEQ_NUMERIC
edf1eac2
SK
11862 && eq_type != SEQ_NUMERIC
11863 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
11864 &e->where) == FAILURE)
e8ec07e1
PT
11865 continue;
11866
11867 identical_types:
11868 last_ts =&sym->ts;
11869 last_where = &e->where;
11870
6de9cd9a 11871 if (!e->ref)
edf1eac2 11872 continue;
6de9cd9a
DN
11873
11874 /* Shall not be an automatic array. */
11875 if (e->ref->type == REF_ARRAY
edf1eac2
SK
11876 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
11877 {
11878 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
11879 "an EQUIVALENCE object", sym->name, &e->where);
11880 continue;
11881 }
6de9cd9a 11882
6de9cd9a
DN
11883 r = e->ref;
11884 while (r)
edf1eac2 11885 {
a8006d09
JJ
11886 /* Shall not be a structure component. */
11887 if (r->type == REF_COMPONENT)
11888 {
11889 gfc_error ("Structure component '%s' at %L cannot be an "
11890 "EQUIVALENCE object",
11891 r->u.c.component->name, &e->where);
11892 break;
11893 }
11894
11895 /* A substring shall not have length zero. */
11896 if (r->type == REF_SUBSTRING)
11897 {
11898 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
11899 {
11900 gfc_error ("Substring at %L has length zero",
11901 &r->u.ss.start->where);
11902 break;
11903 }
11904 }
11905 r = r->next;
11906 }
05c1e3a7
BF
11907 }
11908}
cf4d246b
JJ
11909
11910
66e4ab31 11911/* Resolve function and ENTRY types, issue diagnostics if needed. */
cf4d246b
JJ
11912
11913static void
edf1eac2 11914resolve_fntype (gfc_namespace *ns)
cf4d246b
JJ
11915{
11916 gfc_entry_list *el;
11917 gfc_symbol *sym;
11918
11919 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
11920 return;
11921
11922 /* If there are any entries, ns->proc_name is the entry master
11923 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
11924 if (ns->entries)
11925 sym = ns->entries->sym;
11926 else
11927 sym = ns->proc_name;
11928 if (sym->result == sym
11929 && sym->ts.type == BT_UNKNOWN
11930 && gfc_set_default_type (sym, 0, NULL) == FAILURE
11931 && !sym->attr.untyped)
11932 {
11933 gfc_error ("Function '%s' at %L has no IMPLICIT type",
11934 sym->name, &sym->declared_at);
11935 sym->attr.untyped = 1;
11936 }
11937
bc21d315 11938 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
0d6872cb 11939 && !sym->attr.contained
bc21d315
JW
11940 && !gfc_check_access (sym->ts.u.derived->attr.access,
11941 sym->ts.u.derived->ns->default_access)
3bcc018c
EE
11942 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11943 {
0d6872cb
TB
11944 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
11945 "%L of PRIVATE type '%s'", sym->name,
bc21d315 11946 &sym->declared_at, sym->ts.u.derived->name);
3bcc018c
EE
11947 }
11948
7453378e 11949 if (ns->entries)
cf4d246b
JJ
11950 for (el = ns->entries->next; el; el = el->next)
11951 {
11952 if (el->sym->result == el->sym
11953 && el->sym->ts.type == BT_UNKNOWN
11954 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
11955 && !el->sym->attr.untyped)
11956 {
11957 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
11958 el->sym->name, &el->sym->declared_at);
11959 el->sym->attr.untyped = 1;
11960 }
11961 }
11962}
11963
94747289 11964
0e3e65bc
PT
11965/* 12.3.2.1.1 Defined operators. */
11966
94747289
DK
11967static gfc_try
11968check_uop_procedure (gfc_symbol *sym, locus where)
0e3e65bc 11969{
0e3e65bc
PT
11970 gfc_formal_arglist *formal;
11971
94747289
DK
11972 if (!sym->attr.function)
11973 {
11974 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
11975 sym->name, &where);
11976 return FAILURE;
11977 }
05c1e3a7 11978
94747289 11979 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
11980 && !(sym->ts.u.cl && sym->ts.u.cl->length)
11981 && !(sym->result && sym->result->ts.u.cl
11982 && sym->result->ts.u.cl->length))
94747289
DK
11983 {
11984 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
11985 "character length", sym->name, &where);
11986 return FAILURE;
11987 }
0e3e65bc 11988
94747289
DK
11989 formal = sym->formal;
11990 if (!formal || !formal->sym)
0e3e65bc 11991 {
94747289
DK
11992 gfc_error ("User operator procedure '%s' at %L must have at least "
11993 "one argument", sym->name, &where);
11994 return FAILURE;
11995 }
0e3e65bc 11996
94747289
DK
11997 if (formal->sym->attr.intent != INTENT_IN)
11998 {
11999 gfc_error ("First argument of operator interface at %L must be "
12000 "INTENT(IN)", &where);
12001 return FAILURE;
12002 }
0e3e65bc 12003
94747289
DK
12004 if (formal->sym->attr.optional)
12005 {
12006 gfc_error ("First argument of operator interface at %L cannot be "
12007 "optional", &where);
12008 return FAILURE;
12009 }
0e3e65bc 12010
94747289
DK
12011 formal = formal->next;
12012 if (!formal || !formal->sym)
12013 return SUCCESS;
0e3e65bc 12014
94747289
DK
12015 if (formal->sym->attr.intent != INTENT_IN)
12016 {
12017 gfc_error ("Second argument of operator interface at %L must be "
12018 "INTENT(IN)", &where);
12019 return FAILURE;
12020 }
0e3e65bc 12021
94747289
DK
12022 if (formal->sym->attr.optional)
12023 {
12024 gfc_error ("Second argument of operator interface at %L cannot be "
12025 "optional", &where);
12026 return FAILURE;
12027 }
0e3e65bc 12028
94747289
DK
12029 if (formal->next)
12030 {
12031 gfc_error ("Operator interface at %L must have, at most, two "
12032 "arguments", &where);
12033 return FAILURE;
12034 }
0e3e65bc 12035
94747289
DK
12036 return SUCCESS;
12037}
0e3e65bc 12038
94747289
DK
12039static void
12040gfc_resolve_uops (gfc_symtree *symtree)
12041{
12042 gfc_interface *itr;
12043
12044 if (symtree == NULL)
12045 return;
12046
12047 gfc_resolve_uops (symtree->left);
12048 gfc_resolve_uops (symtree->right);
12049
12050 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12051 check_uop_procedure (itr->sym, itr->sym->declared_at);
0e3e65bc
PT
12052}
12053
cf4d246b 12054
efb0828d
L
12055/* Examine all of the expressions associated with a program unit,
12056 assign types to all intermediate expressions, make sure that all
12057 assignments are to compatible types and figure out which names
12058 refer to which functions or subroutines. It doesn't check code
12059 block, which is handled by resolve_code. */
6de9cd9a 12060
efb0828d 12061static void
edf1eac2 12062resolve_types (gfc_namespace *ns)
6de9cd9a 12063{
efb0828d 12064 gfc_namespace *n;
6de9cd9a
DN
12065 gfc_charlen *cl;
12066 gfc_data *d;
12067 gfc_equiv *eq;
a82f1f2e 12068 gfc_namespace* old_ns = gfc_current_ns;
6de9cd9a 12069
52f49934
DK
12070 /* Check that all IMPLICIT types are ok. */
12071 if (!ns->seen_implicit_none)
12072 {
12073 unsigned letter;
12074 for (letter = 0; letter != GFC_LETTERS; ++letter)
12075 if (ns->set_flag[letter]
12076 && resolve_typespec_used (&ns->default_type[letter],
12077 &ns->implicit_loc[letter],
12078 NULL) == FAILURE)
12079 return;
12080 }
12081
a82f1f2e
DK
12082 gfc_current_ns = ns;
12083
0f3162e3
PT
12084 resolve_entries (ns);
12085
346ecba8 12086 resolve_common_vars (ns->blank_common.head, false);
ad22b1ff
TB
12087 resolve_common_blocks (ns->common_root);
12088
0f3162e3
PT
12089 resolve_contained_functions (ns);
12090
a8b3b0b6
CR
12091 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12092
5cd09fac
TS
12093 for (cl = ns->cl_list; cl; cl = cl->next)
12094 resolve_charlen (cl);
12095
6de9cd9a
DN
12096 gfc_traverse_ns (ns, resolve_symbol);
12097
cf4d246b
JJ
12098 resolve_fntype (ns);
12099
6de9cd9a
DN
12100 for (n = ns->contained; n; n = n->sibling)
12101 {
12102 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12103 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12104 "also be PURE", n->proc_name->name,
12105 &n->proc_name->declared_at);
12106
efb0828d 12107 resolve_types (n);
6de9cd9a
DN
12108 }
12109
12110 forall_flag = 0;
12111 gfc_check_interfaces (ns);
12112
6de9cd9a
DN
12113 gfc_traverse_ns (ns, resolve_values);
12114
d05d9ac7 12115 if (ns->save_all)
6de9cd9a
DN
12116 gfc_save_all (ns);
12117
12118 iter_stack = NULL;
12119 for (d = ns->data; d; d = d->next)
12120 resolve_data (d);
12121
12122 iter_stack = NULL;
12123 gfc_traverse_ns (ns, gfc_formalize_init_value);
12124
a8b3b0b6
CR
12125 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12126
12127 if (ns->common_root != NULL)
12128 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12129
6de9cd9a
DN
12130 for (eq = ns->equiv; eq; eq = eq->next)
12131 resolve_equivalence (eq);
12132
6de9cd9a 12133 /* Warn about unused labels. */
2e5758e8 12134 if (warn_unused_label)
994c1cc0 12135 warn_unused_fortran_label (ns->st_labels);
0e3e65bc
PT
12136
12137 gfc_resolve_uops (ns->uop_root);
a82f1f2e
DK
12138
12139 gfc_current_ns = old_ns;
efb0828d
L
12140}
12141
12142
12143/* Call resolve_code recursively. */
12144
12145static void
edf1eac2 12146resolve_codes (gfc_namespace *ns)
efb0828d
L
12147{
12148 gfc_namespace *n;
71a7778c 12149 bitmap_obstack old_obstack;
efb0828d
L
12150
12151 for (n = ns->contained; n; n = n->sibling)
12152 resolve_codes (n);
12153
12154 gfc_current_ns = ns;
76d02e9f
JW
12155
12156 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12157 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12158 cs_base = NULL;
12159
0e9a445b
PT
12160 /* Set to an out of range value. */
12161 current_entry_id = -1;
0615f923 12162
71a7778c 12163 old_obstack = labels_obstack;
0615f923 12164 bitmap_obstack_initialize (&labels_obstack);
71a7778c 12165
efb0828d 12166 resolve_code (ns->code, ns);
71a7778c 12167
0615f923 12168 bitmap_obstack_release (&labels_obstack);
71a7778c 12169 labels_obstack = old_obstack;
efb0828d
L
12170}
12171
12172
12173/* This function is called after a complete program unit has been compiled.
12174 Its purpose is to examine all of the expressions associated with a program
12175 unit, assign types to all intermediate expressions, make sure that all
12176 assignments are to compatible types and figure out which names refer to
12177 which functions or subroutines. */
12178
12179void
edf1eac2 12180gfc_resolve (gfc_namespace *ns)
efb0828d
L
12181{
12182 gfc_namespace *old_ns;
3af8d8cb 12183 code_stack *old_cs_base;
efb0828d 12184
71a7778c
PT
12185 if (ns->resolved)
12186 return;
12187
3af8d8cb 12188 ns->resolved = -1;
efb0828d 12189 old_ns = gfc_current_ns;
3af8d8cb 12190 old_cs_base = cs_base;
efb0828d
L
12191
12192 resolve_types (ns);
12193 resolve_codes (ns);
6de9cd9a
DN
12194
12195 gfc_current_ns = old_ns;
3af8d8cb 12196 cs_base = old_cs_base;
71a7778c 12197 ns->resolved = 1;
6de9cd9a 12198}
This page took 3.726491 seconds and 5 git commands to generate.