]> gcc.gnu.org Git - gcc.git/blame - gcc/fortran/resolve.c
re PR fortran/42677 (Bogus Error: Ambiguous interfaces '...' in intrinsic assignment...
[gcc.git] / gcc / fortran / resolve.c
CommitLineData
df2fba9e 1/* Perform type resolution on the various structures.
3d876aba 2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 844 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
3d876aba
TB
845 && expr->ts.u.derived->ts.is_iso_c && cons
846 && (cons->expr == NULL || cons->expr->expr_type != EXPR_NULL))
36dcec91
CR
847 {
848 gfc_error ("Components of structure constructor '%s' at %L are PRIVATE",
bc21d315 849 expr->ts.u.derived->name, &(expr->where));
36dcec91
CR
850 return FAILURE;
851 }
852
3d876aba
TB
853 /* Return if structure constructor is c_null_(fun)prt. */
854 if (expr->ts.type == BT_DERIVED && expr->ts.u.derived
855 && expr->ts.u.derived->ts.is_iso_c && cons
856 && cons->expr && cons->expr->expr_type == EXPR_NULL)
857 return SUCCESS;
858
6de9cd9a
DN
859 for (; comp; comp = comp->next, cons = cons->next)
860 {
0df50e7a
FXC
861 int rank;
862
edf1eac2 863 if (!cons->expr)
404d8401 864 continue;
6de9cd9a
DN
865
866 if (gfc_resolve_expr (cons->expr) == FAILURE)
867 {
868 t = FAILURE;
869 continue;
870 }
871
0df50e7a
FXC
872 rank = comp->as ? comp->as->rank : 0;
873 if (cons->expr->expr_type != EXPR_NULL && rank != cons->expr->rank
d4b7d0f0 874 && (comp->attr.allocatable || cons->expr->rank))
5046aff5
PT
875 {
876 gfc_error ("The rank of the element in the derived type "
877 "constructor at %L does not match that of the "
878 "component (%d/%d)", &cons->expr->where,
0df50e7a 879 cons->expr->rank, rank);
5046aff5
PT
880 t = FAILURE;
881 }
882
6de9cd9a
DN
883 /* If we don't have the right type, try to convert it. */
884
e0e85e06
PT
885 if (!gfc_compare_types (&cons->expr->ts, &comp->ts))
886 {
887 t = FAILURE;
d4b7d0f0 888 if (comp->attr.pointer && cons->expr->ts.type != BT_UNKNOWN)
e0e85e06
PT
889 gfc_error ("The element in the derived type constructor at %L, "
890 "for pointer component '%s', is %s but should be %s",
891 &cons->expr->where, comp->name,
892 gfc_basic_typename (cons->expr->ts.type),
893 gfc_basic_typename (comp->ts.type));
894 else
895 t = gfc_convert_type (cons->expr, &comp->ts, 1);
896 }
5046aff5 897
c1203a70 898 if (cons->expr->expr_type == EXPR_NULL
713485cc 899 && !(comp->attr.pointer || comp->attr.allocatable
cf2b3c22
TB
900 || comp->attr.proc_pointer
901 || (comp->ts.type == BT_CLASS
902 && (comp->ts.u.derived->components->attr.pointer
903 || comp->ts.u.derived->components->attr.allocatable))))
c1203a70
PT
904 {
905 t = FAILURE;
906 gfc_error ("The NULL in the derived type constructor at %L is "
907 "being applied to component '%s', which is neither "
908 "a POINTER nor ALLOCATABLE", &cons->expr->where,
909 comp->name);
910 }
911
d4b7d0f0 912 if (!comp->attr.pointer || cons->expr->expr_type == EXPR_NULL)
5046aff5
PT
913 continue;
914
915 a = gfc_expr_attr (cons->expr);
916
917 if (!a.pointer && !a.target)
918 {
919 t = FAILURE;
920 gfc_error ("The element in the derived type constructor at %L, "
921 "for pointer component '%s' should be a POINTER or "
922 "a TARGET", &cons->expr->where, comp->name);
923 }
6de9cd9a
DN
924 }
925
926 return t;
927}
928
929
6de9cd9a
DN
930/****************** Expression name resolution ******************/
931
932/* Returns 0 if a symbol was not declared with a type or
4f613946 933 attribute declaration statement, nonzero otherwise. */
6de9cd9a
DN
934
935static int
edf1eac2 936was_declared (gfc_symbol *sym)
6de9cd9a
DN
937{
938 symbol_attribute a;
939
940 a = sym->attr;
941
942 if (!a.implicit_type && sym->ts.type != BT_UNKNOWN)
943 return 1;
944
9439ae41 945 if (a.allocatable || a.dimension || a.dummy || a.external || a.intrinsic
edf1eac2 946 || a.optional || a.pointer || a.save || a.target || a.volatile_
1eee5628
TB
947 || a.value || a.access != ACCESS_UNKNOWN || a.intent != INTENT_UNKNOWN
948 || a.asynchronous)
6de9cd9a
DN
949 return 1;
950
951 return 0;
952}
953
954
955/* Determine if a symbol is generic or not. */
956
957static int
edf1eac2 958generic_sym (gfc_symbol *sym)
6de9cd9a
DN
959{
960 gfc_symbol *s;
961
962 if (sym->attr.generic ||
963 (sym->attr.intrinsic && gfc_generic_intrinsic (sym->name)))
964 return 1;
965
966 if (was_declared (sym) || sym->ns->parent == NULL)
967 return 0;
968
969 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
6d023ec5
JD
970
971 if (s != NULL)
972 {
973 if (s == sym)
974 return 0;
975 else
976 return generic_sym (s);
977 }
6de9cd9a 978
6d023ec5 979 return 0;
6de9cd9a
DN
980}
981
982
983/* Determine if a symbol is specific or not. */
984
985static int
edf1eac2 986specific_sym (gfc_symbol *sym)
6de9cd9a
DN
987{
988 gfc_symbol *s;
989
990 if (sym->attr.if_source == IFSRC_IFBODY
991 || sym->attr.proc == PROC_MODULE
992 || sym->attr.proc == PROC_INTERNAL
993 || sym->attr.proc == PROC_ST_FUNCTION
edf1eac2 994 || (sym->attr.intrinsic && gfc_specific_intrinsic (sym->name))
6de9cd9a
DN
995 || sym->attr.external)
996 return 1;
997
998 if (was_declared (sym) || sym->ns->parent == NULL)
999 return 0;
1000
1001 gfc_find_symbol (sym->name, sym->ns->parent, 1, &s);
1002
1003 return (s == NULL) ? 0 : specific_sym (s);
1004}
1005
1006
1007/* Figure out if the procedure is specific, generic or unknown. */
1008
1009typedef enum
1010{ PTYPE_GENERIC = 1, PTYPE_SPECIFIC, PTYPE_UNKNOWN }
1011proc_type;
1012
1013static proc_type
edf1eac2 1014procedure_kind (gfc_symbol *sym)
6de9cd9a 1015{
6de9cd9a
DN
1016 if (generic_sym (sym))
1017 return PTYPE_GENERIC;
1018
1019 if (specific_sym (sym))
1020 return PTYPE_SPECIFIC;
1021
1022 return PTYPE_UNKNOWN;
1023}
1024
48474141 1025/* Check references to assumed size arrays. The flag need_full_assumed_size
b82feea5 1026 is nonzero when matching actual arguments. */
48474141
PT
1027
1028static int need_full_assumed_size = 0;
1029
1030static bool
edf1eac2 1031check_assumed_size_reference (gfc_symbol *sym, gfc_expr *e)
48474141 1032{
edf1eac2 1033 if (need_full_assumed_size || !(sym->as && sym->as->type == AS_ASSUMED_SIZE))
48474141
PT
1034 return false;
1035
e0c68ce9
ILT
1036 /* FIXME: The comparison "e->ref->u.ar.type == AR_FULL" is wrong.
1037 What should it be? */
c52938ec
PT
1038 if ((e->ref->u.ar.end[e->ref->u.ar.as->rank - 1] == NULL)
1039 && (e->ref->u.ar.as->type == AS_ASSUMED_SIZE)
e0c68ce9 1040 && (e->ref->u.ar.type == AR_FULL))
48474141
PT
1041 {
1042 gfc_error ("The upper bound in the last dimension must "
1043 "appear in the reference to the assumed size "
e25a0da3 1044 "array '%s' at %L", sym->name, &e->where);
48474141
PT
1045 return true;
1046 }
1047 return false;
1048}
1049
1050
1051/* Look for bad assumed size array references in argument expressions
1052 of elemental and array valued intrinsic procedures. Since this is
1053 called from procedure resolution functions, it only recurses at
1054 operators. */
1055
1056static bool
1057resolve_assumed_size_actual (gfc_expr *e)
1058{
1059 if (e == NULL)
1060 return false;
1061
1062 switch (e->expr_type)
1063 {
1064 case EXPR_VARIABLE:
edf1eac2 1065 if (e->symtree && check_assumed_size_reference (e->symtree->n.sym, e))
48474141
PT
1066 return true;
1067 break;
1068
1069 case EXPR_OP:
1070 if (resolve_assumed_size_actual (e->value.op.op1)
edf1eac2 1071 || resolve_assumed_size_actual (e->value.op.op2))
48474141
PT
1072 return true;
1073 break;
1074
1075 default:
1076 break;
1077 }
1078 return false;
1079}
1080
6de9cd9a 1081
0b4e2af7
PT
1082/* Check a generic procedure, passed as an actual argument, to see if
1083 there is a matching specific name. If none, it is an error, and if
1084 more than one, the reference is ambiguous. */
1085static int
1086count_specific_procs (gfc_expr *e)
1087{
1088 int n;
1089 gfc_interface *p;
1090 gfc_symbol *sym;
1091
1092 n = 0;
1093 sym = e->symtree->n.sym;
1094
1095 for (p = sym->generic; p; p = p->next)
1096 if (strcmp (sym->name, p->sym->name) == 0)
1097 {
1098 e->symtree = gfc_find_symtree (p->sym->ns->sym_root,
1099 sym->name);
1100 n++;
1101 }
1102
1103 if (n > 1)
1104 gfc_error ("'%s' at %L is ambiguous", e->symtree->n.sym->name,
1105 &e->where);
1106
1107 if (n == 0)
1108 gfc_error ("GENERIC procedure '%s' is not allowed as an actual "
1109 "argument at %L", sym->name, &e->where);
1110
1111 return n;
1112}
1113
a03826d1 1114
1933ba0f
DK
1115/* See if a call to sym could possibly be a not allowed RECURSION because of
1116 a missing RECURIVE declaration. This means that either sym is the current
1117 context itself, or sym is the parent of a contained procedure calling its
1118 non-RECURSIVE containing procedure.
1119 This also works if sym is an ENTRY. */
1120
1121static bool
1122is_illegal_recursion (gfc_symbol* sym, gfc_namespace* context)
1123{
1124 gfc_symbol* proc_sym;
1125 gfc_symbol* context_proc;
9abe5e56 1126 gfc_namespace* real_context;
1933ba0f 1127
6f7e06ce
JD
1128 if (sym->attr.flavor == FL_PROGRAM)
1129 return false;
1130
1933ba0f
DK
1131 gcc_assert (sym->attr.flavor == FL_PROCEDURE);
1132
1133 /* If we've got an ENTRY, find real procedure. */
1134 if (sym->attr.entry && sym->ns->entries)
1135 proc_sym = sym->ns->entries->sym;
1136 else
1137 proc_sym = sym;
1138
1139 /* If sym is RECURSIVE, all is well of course. */
1140 if (proc_sym->attr.recursive || gfc_option.flag_recursive)
1141 return false;
1142
9abe5e56
DK
1143 /* Find the context procedure's "real" symbol if it has entries.
1144 We look for a procedure symbol, so recurse on the parents if we don't
1145 find one (like in case of a BLOCK construct). */
1146 for (real_context = context; ; real_context = real_context->parent)
1147 {
1148 /* We should find something, eventually! */
1149 gcc_assert (real_context);
1150
1151 context_proc = (real_context->entries ? real_context->entries->sym
1152 : real_context->proc_name);
1153
1154 /* In some special cases, there may not be a proc_name, like for this
1155 invalid code:
1156 real(bad_kind()) function foo () ...
1157 when checking the call to bad_kind ().
1158 In these cases, we simply return here and assume that the
1159 call is ok. */
1160 if (!context_proc)
1161 return false;
1162
1163 if (context_proc->attr.flavor != FL_LABEL)
1164 break;
1165 }
1933ba0f
DK
1166
1167 /* A call from sym's body to itself is recursion, of course. */
1168 if (context_proc == proc_sym)
1169 return true;
1170
1171 /* The same is true if context is a contained procedure and sym the
1172 containing one. */
1173 if (context_proc->attr.contained)
1174 {
1175 gfc_symbol* parent_proc;
1176
1177 gcc_assert (context->parent);
1178 parent_proc = (context->parent->entries ? context->parent->entries->sym
1179 : context->parent->proc_name);
1180
1181 if (parent_proc == proc_sym)
1182 return true;
1183 }
1184
1185 return false;
1186}
1187
1188
c73b6478
JW
1189/* Resolve an intrinsic procedure: Set its function/subroutine attribute,
1190 its typespec and formal argument list. */
1191
1192static gfc_try
1193resolve_intrinsic (gfc_symbol *sym, locus *loc)
1194{
f6038131
JW
1195 gfc_intrinsic_sym* isym;
1196 const char* symstd;
1197
1198 if (sym->formal)
1199 return SUCCESS;
1200
1201 /* We already know this one is an intrinsic, so we don't call
1202 gfc_is_intrinsic for full checking but rather use gfc_find_function and
1203 gfc_find_subroutine directly to check whether it is a function or
1204 subroutine. */
1205
1206 if ((isym = gfc_find_function (sym->name)))
c73b6478 1207 {
f6038131
JW
1208 if (sym->ts.type != BT_UNKNOWN && gfc_option.warn_surprising
1209 && !sym->attr.implicit_type)
1210 gfc_warning ("Type specified for intrinsic function '%s' at %L is"
1211 " ignored", sym->name, &sym->declared_at);
1212
c73b6478
JW
1213 if (!sym->attr.function &&
1214 gfc_add_function (&sym->attr, sym->name, loc) == FAILURE)
1215 return FAILURE;
f6038131 1216
c73b6478
JW
1217 sym->ts = isym->ts;
1218 }
f6038131 1219 else if ((isym = gfc_find_subroutine (sym->name)))
c73b6478 1220 {
f6038131
JW
1221 if (sym->ts.type != BT_UNKNOWN && !sym->attr.implicit_type)
1222 {
1223 gfc_error ("Intrinsic subroutine '%s' at %L shall not have a type"
1224 " specifier", sym->name, &sym->declared_at);
1225 return FAILURE;
1226 }
1227
c73b6478
JW
1228 if (!sym->attr.subroutine &&
1229 gfc_add_subroutine (&sym->attr, sym->name, loc) == FAILURE)
1230 return FAILURE;
1231 }
f6038131
JW
1232 else
1233 {
1234 gfc_error ("'%s' declared INTRINSIC at %L does not exist", sym->name,
1235 &sym->declared_at);
1236 return FAILURE;
1237 }
1238
1239 gfc_copy_formal_args_intr (sym, isym);
1240
1241 /* Check it is actually available in the standard settings. */
1242 if (gfc_check_intrinsic_standard (isym, &symstd, false, sym->declared_at)
1243 == FAILURE)
1244 {
1245 gfc_error ("The intrinsic '%s' declared INTRINSIC at %L is not"
1246 " available in the current standard settings but %s. Use"
1247 " an appropriate -std=* option or enable -fall-intrinsics"
1248 " in order to use it.",
1249 sym->name, &sym->declared_at, symstd);
1250 return FAILURE;
1251 }
1252
c73b6478
JW
1253 return SUCCESS;
1254}
1255
1256
a03826d1
DK
1257/* Resolve a procedure expression, like passing it to a called procedure or as
1258 RHS for a procedure pointer assignment. */
1259
1260static gfc_try
1261resolve_procedure_expression (gfc_expr* expr)
1262{
1263 gfc_symbol* sym;
1264
1933ba0f 1265 if (expr->expr_type != EXPR_VARIABLE)
a03826d1
DK
1266 return SUCCESS;
1267 gcc_assert (expr->symtree);
1933ba0f 1268
a03826d1 1269 sym = expr->symtree->n.sym;
c73b6478
JW
1270
1271 if (sym->attr.intrinsic)
1272 resolve_intrinsic (sym, &expr->where);
1273
1933ba0f
DK
1274 if (sym->attr.flavor != FL_PROCEDURE
1275 || (sym->attr.function && sym->result == sym))
1276 return SUCCESS;
a03826d1
DK
1277
1278 /* A non-RECURSIVE procedure that is used as procedure expression within its
1279 own body is in danger of being called recursively. */
1933ba0f 1280 if (is_illegal_recursion (sym, gfc_current_ns))
a03826d1
DK
1281 gfc_warning ("Non-RECURSIVE procedure '%s' at %L is possibly calling"
1282 " itself recursively. Declare it RECURSIVE or use"
1283 " -frecursive", sym->name, &expr->where);
1284
1285 return SUCCESS;
1286}
1287
1288
6de9cd9a
DN
1289/* Resolve an actual argument list. Most of the time, this is just
1290 resolving the expressions in the list.
1291 The exception is that we sometimes have to decide whether arguments
1292 that look like procedure arguments are really simple variable
1293 references. */
1294
17b1d2a0 1295static gfc_try
0b4e2af7
PT
1296resolve_actual_arglist (gfc_actual_arglist *arg, procedure_type ptype,
1297 bool no_formal_args)
6de9cd9a
DN
1298{
1299 gfc_symbol *sym;
1300 gfc_symtree *parent_st;
1301 gfc_expr *e;
5ad6345e 1302 int save_need_full_assumed_size;
713485cc 1303 gfc_component *comp;
0b4e2af7 1304
6de9cd9a
DN
1305 for (; arg; arg = arg->next)
1306 {
6de9cd9a
DN
1307 e = arg->expr;
1308 if (e == NULL)
edf1eac2
SK
1309 {
1310 /* Check the label is a valid branching target. */
1311 if (arg->label)
1312 {
1313 if (arg->label->defined == ST_LABEL_UNKNOWN)
1314 {
1315 gfc_error ("Label %d referenced at %L is never defined",
1316 arg->label->value, &arg->label->where);
1317 return FAILURE;
1318 }
1319 }
1320 continue;
1321 }
6de9cd9a 1322
f64edc8b 1323 if (gfc_is_proc_ptr_comp (e, &comp))
713485cc
JW
1324 {
1325 e->ts = comp->ts;
23878536 1326 if (e->expr_type == EXPR_PPC)
acbdc378
JW
1327 {
1328 if (comp->as != NULL)
1329 e->rank = comp->as->rank;
1330 e->expr_type = EXPR_FUNCTION;
1331 }
6c036626
JW
1332 if (gfc_resolve_expr (e) == FAILURE)
1333 return FAILURE;
713485cc
JW
1334 goto argument_list;
1335 }
1336
67cec813 1337 if (e->expr_type == EXPR_VARIABLE
0b4e2af7
PT
1338 && e->symtree->n.sym->attr.generic
1339 && no_formal_args
1340 && count_specific_procs (e) != 1)
1341 return FAILURE;
27372c38 1342
6de9cd9a
DN
1343 if (e->ts.type != BT_PROCEDURE)
1344 {
5ad6345e 1345 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1346 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1347 need_full_assumed_size = 0;
6de9cd9a
DN
1348 if (gfc_resolve_expr (e) != SUCCESS)
1349 return FAILURE;
5ad6345e 1350 need_full_assumed_size = save_need_full_assumed_size;
7fcafa71 1351 goto argument_list;
6de9cd9a
DN
1352 }
1353
edf1eac2 1354 /* See if the expression node should really be a variable reference. */
6de9cd9a
DN
1355
1356 sym = e->symtree->n.sym;
1357
1358 if (sym->attr.flavor == FL_PROCEDURE
1359 || sym->attr.intrinsic
1360 || sym->attr.external)
1361 {
0e7e7e6e 1362 int actual_ok;
6de9cd9a 1363
d68bd5a8
PT
1364 /* If a procedure is not already determined to be something else
1365 check if it is intrinsic. */
1366 if (!sym->attr.intrinsic
edf1eac2
SK
1367 && !(sym->attr.external || sym->attr.use_assoc
1368 || sym->attr.if_source == IFSRC_IFBODY)
c3005b0f 1369 && gfc_is_intrinsic (sym, sym->attr.subroutine, e->where))
d68bd5a8
PT
1370 sym->attr.intrinsic = 1;
1371
2ed8d224
PT
1372 if (sym->attr.proc == PROC_ST_FUNCTION)
1373 {
1374 gfc_error ("Statement function '%s' at %L is not allowed as an "
1375 "actual argument", sym->name, &e->where);
1376 }
1377
edf1eac2
SK
1378 actual_ok = gfc_intrinsic_actual_ok (sym->name,
1379 sym->attr.subroutine);
0e7e7e6e
FXC
1380 if (sym->attr.intrinsic && actual_ok == 0)
1381 {
1382 gfc_error ("Intrinsic '%s' at %L is not allowed as an "
1383 "actual argument", sym->name, &e->where);
1384 }
0e7e7e6e 1385
2ed8d224
PT
1386 if (sym->attr.contained && !sym->attr.use_assoc
1387 && sym->ns->proc_name->attr.flavor != FL_MODULE)
1388 {
1389 gfc_error ("Internal procedure '%s' is not allowed as an "
1390 "actual argument at %L", sym->name, &e->where);
1391 }
1392
1393 if (sym->attr.elemental && !sym->attr.intrinsic)
1394 {
1395 gfc_error ("ELEMENTAL non-INTRINSIC procedure '%s' is not "
edf1eac2 1396 "allowed as an actual argument at %L", sym->name,
2ed8d224
PT
1397 &e->where);
1398 }
781e1004 1399
36d3fb4c
PT
1400 /* Check if a generic interface has a specific procedure
1401 with the same name before emitting an error. */
0b4e2af7
PT
1402 if (sym->attr.generic && count_specific_procs (e) != 1)
1403 return FAILURE;
1404
1405 /* Just in case a specific was found for the expression. */
1406 sym = e->symtree->n.sym;
3e978d30 1407
6de9cd9a
DN
1408 /* If the symbol is the function that names the current (or
1409 parent) scope, then we really have a variable reference. */
1410
2d71b918 1411 if (gfc_is_function_return_value (sym, sym->ns))
6de9cd9a
DN
1412 goto got_variable;
1413
20a037d5 1414 /* If all else fails, see if we have a specific intrinsic. */
26033479 1415 if (sym->ts.type == BT_UNKNOWN && sym->attr.intrinsic)
20a037d5
PT
1416 {
1417 gfc_intrinsic_sym *isym;
6cc309c9 1418
20a037d5
PT
1419 isym = gfc_find_function (sym->name);
1420 if (isym == NULL || !isym->specific)
1421 {
1422 gfc_error ("Unable to find a specific INTRINSIC procedure "
1423 "for the reference '%s' at %L", sym->name,
1424 &e->where);
26033479 1425 return FAILURE;
20a037d5
PT
1426 }
1427 sym->ts = isym->ts;
6cc309c9 1428 sym->attr.intrinsic = 1;
26033479 1429 sym->attr.function = 1;
20a037d5 1430 }
a03826d1
DK
1431
1432 if (gfc_resolve_expr (e) == FAILURE)
1433 return FAILURE;
7fcafa71 1434 goto argument_list;
6de9cd9a
DN
1435 }
1436
1437 /* See if the name is a module procedure in a parent unit. */
1438
1439 if (was_declared (sym) || sym->ns->parent == NULL)
1440 goto got_variable;
1441
1442 if (gfc_find_sym_tree (sym->name, sym->ns->parent, 1, &parent_st))
1443 {
1444 gfc_error ("Symbol '%s' at %L is ambiguous", sym->name, &e->where);
1445 return FAILURE;
1446 }
1447
1448 if (parent_st == NULL)
1449 goto got_variable;
1450
1451 sym = parent_st->n.sym;
1452 e->symtree = parent_st; /* Point to the right thing. */
1453
1454 if (sym->attr.flavor == FL_PROCEDURE
1455 || sym->attr.intrinsic
1456 || sym->attr.external)
1457 {
a03826d1
DK
1458 if (gfc_resolve_expr (e) == FAILURE)
1459 return FAILURE;
7fcafa71 1460 goto argument_list;
6de9cd9a
DN
1461 }
1462
1463 got_variable:
1464 e->expr_type = EXPR_VARIABLE;
1465 e->ts = sym->ts;
1466 if (sym->as != NULL)
1467 {
1468 e->rank = sym->as->rank;
1469 e->ref = gfc_get_ref ();
1470 e->ref->type = REF_ARRAY;
1471 e->ref->u.ar.type = AR_FULL;
1472 e->ref->u.ar.as = sym->as;
1473 }
7fcafa71 1474
1b35264f
DF
1475 /* Expressions are assigned a default ts.type of BT_PROCEDURE in
1476 primary.c (match_actual_arg). If above code determines that it
1477 is a variable instead, it needs to be resolved as it was not
1478 done at the beginning of this function. */
5ad6345e 1479 save_need_full_assumed_size = need_full_assumed_size;
e0c68ce9 1480 if (e->expr_type != EXPR_VARIABLE)
5ad6345e 1481 need_full_assumed_size = 0;
1b35264f
DF
1482 if (gfc_resolve_expr (e) != SUCCESS)
1483 return FAILURE;
5ad6345e 1484 need_full_assumed_size = save_need_full_assumed_size;
1b35264f 1485
7fcafa71
PT
1486 argument_list:
1487 /* Check argument list functions %VAL, %LOC and %REF. There is
1488 nothing to do for %REF. */
1489 if (arg->name && arg->name[0] == '%')
1490 {
1491 if (strncmp ("%VAL", arg->name, 4) == 0)
1492 {
1493 if (e->ts.type == BT_CHARACTER || e->ts.type == BT_DERIVED)
1494 {
1495 gfc_error ("By-value argument at %L is not of numeric "
1496 "type", &e->where);
1497 return FAILURE;
1498 }
1499
1500 if (e->rank)
1501 {
1502 gfc_error ("By-value argument at %L cannot be an array or "
1503 "an array section", &e->where);
1504 return FAILURE;
1505 }
1506
1507 /* Intrinsics are still PROC_UNKNOWN here. However,
1508 since same file external procedures are not resolvable
1509 in gfortran, it is a good deal easier to leave them to
1510 intrinsic.c. */
7193e30a
TB
1511 if (ptype != PROC_UNKNOWN
1512 && ptype != PROC_DUMMY
29ea08da
TB
1513 && ptype != PROC_EXTERNAL
1514 && ptype != PROC_MODULE)
7fcafa71
PT
1515 {
1516 gfc_error ("By-value argument at %L is not allowed "
1517 "in this context", &e->where);
1518 return FAILURE;
1519 }
7fcafa71
PT
1520 }
1521
1522 /* Statement functions have already been excluded above. */
1523 else if (strncmp ("%LOC", arg->name, 4) == 0
edf1eac2 1524 && e->ts.type == BT_PROCEDURE)
7fcafa71
PT
1525 {
1526 if (e->symtree->n.sym->attr.proc == PROC_INTERNAL)
1527 {
1528 gfc_error ("Passing internal procedure at %L by location "
1529 "not allowed", &e->where);
1530 return FAILURE;
1531 }
1532 }
1533 }
6de9cd9a
DN
1534 }
1535
1536 return SUCCESS;
1537}
1538
1539
b8ea6dbc
PT
1540/* Do the checks of the actual argument list that are specific to elemental
1541 procedures. If called with c == NULL, we have a function, otherwise if
1542 expr == NULL, we have a subroutine. */
edf1eac2 1543
17b1d2a0 1544static gfc_try
b8ea6dbc
PT
1545resolve_elemental_actual (gfc_expr *expr, gfc_code *c)
1546{
1547 gfc_actual_arglist *arg0;
1548 gfc_actual_arglist *arg;
1549 gfc_symbol *esym = NULL;
1550 gfc_intrinsic_sym *isym = NULL;
1551 gfc_expr *e = NULL;
1552 gfc_intrinsic_arg *iformal = NULL;
1553 gfc_formal_arglist *eformal = NULL;
1554 bool formal_optional = false;
1555 bool set_by_optional = false;
1556 int i;
1557 int rank = 0;
1558
1559 /* Is this an elemental procedure? */
1560 if (expr && expr->value.function.actual != NULL)
1561 {
1562 if (expr->value.function.esym != NULL
edf1eac2 1563 && expr->value.function.esym->attr.elemental)
b8ea6dbc
PT
1564 {
1565 arg0 = expr->value.function.actual;
1566 esym = expr->value.function.esym;
1567 }
1568 else if (expr->value.function.isym != NULL
edf1eac2 1569 && expr->value.function.isym->elemental)
b8ea6dbc
PT
1570 {
1571 arg0 = expr->value.function.actual;
1572 isym = expr->value.function.isym;
1573 }
1574 else
1575 return SUCCESS;
1576 }
dd9315de 1577 else if (c && c->ext.actual != NULL)
b8ea6dbc
PT
1578 {
1579 arg0 = c->ext.actual;
dd9315de
DK
1580
1581 if (c->resolved_sym)
1582 esym = c->resolved_sym;
1583 else
1584 esym = c->symtree->n.sym;
1585 gcc_assert (esym);
1586
1587 if (!esym->attr.elemental)
1588 return SUCCESS;
b8ea6dbc
PT
1589 }
1590 else
1591 return SUCCESS;
1592
1593 /* The rank of an elemental is the rank of its array argument(s). */
1594 for (arg = arg0; arg; arg = arg->next)
1595 {
1596 if (arg->expr != NULL && arg->expr->rank > 0)
1597 {
1598 rank = arg->expr->rank;
1599 if (arg->expr->expr_type == EXPR_VARIABLE
edf1eac2 1600 && arg->expr->symtree->n.sym->attr.optional)
b8ea6dbc
PT
1601 set_by_optional = true;
1602
1603 /* Function specific; set the result rank and shape. */
1604 if (expr)
1605 {
1606 expr->rank = rank;
1607 if (!expr->shape && arg->expr->shape)
1608 {
1609 expr->shape = gfc_get_shape (rank);
1610 for (i = 0; i < rank; i++)
1611 mpz_init_set (expr->shape[i], arg->expr->shape[i]);
1612 }
1613 }
1614 break;
1615 }
1616 }
1617
1618 /* If it is an array, it shall not be supplied as an actual argument
1619 to an elemental procedure unless an array of the same rank is supplied
1620 as an actual argument corresponding to a nonoptional dummy argument of
1621 that elemental procedure(12.4.1.5). */
1622 formal_optional = false;
1623 if (isym)
1624 iformal = isym->formal;
1625 else
1626 eformal = esym->formal;
1627
1628 for (arg = arg0; arg; arg = arg->next)
1629 {
1630 if (eformal)
1631 {
1632 if (eformal->sym && eformal->sym->attr.optional)
1633 formal_optional = true;
1634 eformal = eformal->next;
1635 }
1636 else if (isym && iformal)
1637 {
1638 if (iformal->optional)
1639 formal_optional = true;
1640 iformal = iformal->next;
1641 }
1642 else if (isym)
1643 formal_optional = true;
1644
994c1cc0 1645 if (pedantic && arg->expr != NULL
edf1eac2
SK
1646 && arg->expr->expr_type == EXPR_VARIABLE
1647 && arg->expr->symtree->n.sym->attr.optional
1648 && formal_optional
1649 && arg->expr->rank
1650 && (set_by_optional || arg->expr->rank != rank)
cd5ecab6 1651 && !(isym && isym->id == GFC_ISYM_CONVERSION))
b8ea6dbc 1652 {
994c1cc0
SK
1653 gfc_warning ("'%s' at %L is an array and OPTIONAL; IF IT IS "
1654 "MISSING, it cannot be the actual argument of an "
edf1eac2 1655 "ELEMENTAL procedure unless there is a non-optional "
994c1cc0
SK
1656 "argument with the same rank (12.4.1.5)",
1657 arg->expr->symtree->n.sym->name, &arg->expr->where);
b8ea6dbc
PT
1658 return FAILURE;
1659 }
1660 }
1661
1662 for (arg = arg0; arg; arg = arg->next)
1663 {
1664 if (arg->expr == NULL || arg->expr->rank == 0)
1665 continue;
1666
1667 /* Being elemental, the last upper bound of an assumed size array
1668 argument must be present. */
1669 if (resolve_assumed_size_actual (arg->expr))
1670 return FAILURE;
1671
3c7b91d3 1672 /* Elemental procedure's array actual arguments must conform. */
b8ea6dbc
PT
1673 if (e != NULL)
1674 {
ca8a8795
DF
1675 if (gfc_check_conformance (arg->expr, e,
1676 "elemental procedure") == FAILURE)
b8ea6dbc
PT
1677 return FAILURE;
1678 }
1679 else
1680 e = arg->expr;
1681 }
1682
4a965827
TB
1683 /* INTENT(OUT) is only allowed for subroutines; if any actual argument
1684 is an array, the intent inout/out variable needs to be also an array. */
1685 if (rank > 0 && esym && expr == NULL)
1686 for (eformal = esym->formal, arg = arg0; arg && eformal;
1687 arg = arg->next, eformal = eformal->next)
1688 if ((eformal->sym->attr.intent == INTENT_OUT
1689 || eformal->sym->attr.intent == INTENT_INOUT)
1690 && arg->expr && arg->expr->rank == 0)
1691 {
1692 gfc_error ("Actual argument at %L for INTENT(%s) dummy '%s' of "
1693 "ELEMENTAL subroutine '%s' is a scalar, but another "
1694 "actual argument is an array", &arg->expr->where,
1695 (eformal->sym->attr.intent == INTENT_OUT) ? "OUT"
1696 : "INOUT", eformal->sym->name, esym->name);
1697 return FAILURE;
1698 }
b8ea6dbc
PT
1699 return SUCCESS;
1700}
1701
1702
1524f80b
RS
1703/* Go through each actual argument in ACTUAL and see if it can be
1704 implemented as an inlined, non-copying intrinsic. FNSYM is the
1705 function being called, or NULL if not known. */
1706
1707static void
edf1eac2 1708find_noncopying_intrinsics (gfc_symbol *fnsym, gfc_actual_arglist *actual)
1524f80b
RS
1709{
1710 gfc_actual_arglist *ap;
1711 gfc_expr *expr;
1712
1713 for (ap = actual; ap; ap = ap->next)
1714 if (ap->expr
1715 && (expr = gfc_get_noncopying_intrinsic_argument (ap->expr))
2b0bd714
MM
1716 && !gfc_check_fncall_dependency (expr, INTENT_IN, fnsym, actual,
1717 NOT_ELEMENTAL))
1524f80b
RS
1718 ap->expr->inline_noncopying_intrinsic = 1;
1719}
1720
edf1eac2 1721
68ea355b
PT
1722/* This function does the checking of references to global procedures
1723 as defined in sections 18.1 and 14.1, respectively, of the Fortran
1724 77 and 95 standards. It checks for a gsymbol for the name, making
1725 one if it does not already exist. If it already exists, then the
1726 reference being resolved must correspond to the type of gsymbol.
05c1e3a7 1727 Otherwise, the new symbol is equipped with the attributes of the
68ea355b 1728 reference. The corresponding code that is called in creating
71a7778c
PT
1729 global entities is parse.c.
1730
1731 In addition, for all but -std=legacy, the gsymbols are used to
1732 check the interfaces of external procedures from the same file.
1733 The namespace of the gsymbol is resolved and then, once this is
1734 done the interface is checked. */
68ea355b 1735
3af8d8cb
PT
1736
1737static bool
1738not_in_recursive (gfc_symbol *sym, gfc_namespace *gsym_ns)
1739{
1740 if (!gsym_ns->proc_name->attr.recursive)
1741 return true;
1742
1743 if (sym->ns == gsym_ns)
1744 return false;
1745
1746 if (sym->ns->parent && sym->ns->parent == gsym_ns)
1747 return false;
1748
1749 return true;
1750}
1751
1752static bool
1753not_entry_self_reference (gfc_symbol *sym, gfc_namespace *gsym_ns)
1754{
1755 if (gsym_ns->entries)
1756 {
1757 gfc_entry_list *entry = gsym_ns->entries;
1758
1759 for (; entry; entry = entry->next)
1760 {
1761 if (strcmp (sym->name, entry->sym->name) == 0)
1762 {
1763 if (strcmp (gsym_ns->proc_name->name,
1764 sym->ns->proc_name->name) == 0)
1765 return false;
1766
1767 if (sym->ns->parent
1768 && strcmp (gsym_ns->proc_name->name,
1769 sym->ns->parent->proc_name->name) == 0)
1770 return false;
1771 }
1772 }
1773 }
1774 return true;
1775}
1776
ff604888 1777static void
71a7778c
PT
1778resolve_global_procedure (gfc_symbol *sym, locus *where,
1779 gfc_actual_arglist **actual, int sub)
68ea355b
PT
1780{
1781 gfc_gsymbol * gsym;
71a7778c 1782 gfc_namespace *ns;
32e8bb8e 1783 enum gfc_symbol_type type;
68ea355b
PT
1784
1785 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
1786
1787 gsym = gfc_get_gsymbol (sym->name);
1788
1789 if ((gsym->type != GSYM_UNKNOWN && gsym->type != type))
ca39e6f2 1790 gfc_global_used (gsym, where);
68ea355b 1791
71a7778c 1792 if (gfc_option.flag_whole_file
3af8d8cb 1793 && sym->attr.if_source == IFSRC_UNKNOWN
71a7778c
PT
1794 && gsym->type != GSYM_UNKNOWN
1795 && gsym->ns
3af8d8cb
PT
1796 && gsym->ns->resolved != -1
1797 && gsym->ns->proc_name
1798 && not_in_recursive (sym, gsym->ns)
1799 && not_entry_self_reference (sym, gsym->ns))
71a7778c
PT
1800 {
1801 /* Make sure that translation for the gsymbol occurs before
1802 the procedure currently being resolved. */
1803 ns = gsym->ns->resolved ? NULL : gfc_global_ns_list;
1804 for (; ns && ns != gsym->ns; ns = ns->sibling)
1805 {
1806 if (ns->sibling == gsym->ns)
1807 {
1808 ns->sibling = gsym->ns->sibling;
1809 gsym->ns->sibling = gfc_global_ns_list;
1810 gfc_global_ns_list = gsym->ns;
1811 break;
1812 }
1813 }
1814
1815 if (!gsym->ns->resolved)
3af8d8cb
PT
1816 {
1817 gfc_dt_list *old_dt_list;
1818
1819 /* Stash away derived types so that the backend_decls do not
1820 get mixed up. */
1821 old_dt_list = gfc_derived_types;
1822 gfc_derived_types = NULL;
1823
1824 gfc_resolve (gsym->ns);
1825
1826 /* Store the new derived types with the global namespace. */
1827 if (gfc_derived_types)
1828 gsym->ns->derived_types = gfc_derived_types;
1829
1830 /* Restore the derived types of this namespace. */
1831 gfc_derived_types = old_dt_list;
1832 }
1833
1834 if (gsym->ns->proc_name->attr.function
1835 && gsym->ns->proc_name->as
1836 && gsym->ns->proc_name->as->rank
1837 && (!sym->as || sym->as->rank != gsym->ns->proc_name->as->rank))
1838 gfc_error ("The reference to function '%s' at %L either needs an "
1839 "explicit INTERFACE or the rank is incorrect", sym->name,
1840 where);
d94be5e0
TB
1841
1842 /* Non-assumed length character functions. */
1843 if (sym->attr.function && sym->ts.type == BT_CHARACTER
1844 && gsym->ns->proc_name->ts.u.cl->length != NULL)
1845 {
1846 gfc_charlen *cl = sym->ts.u.cl;
1847
1848 if (!sym->attr.entry_master && sym->attr.if_source == IFSRC_UNKNOWN
1849 && cl && cl->length && cl->length->expr_type != EXPR_CONSTANT)
1850 {
1851 gfc_error ("Nonconstant character-length function '%s' at %L "
1852 "must have an explicit interface", sym->name,
1853 &sym->declared_at);
1854 }
1855 }
3af8d8cb
PT
1856
1857 if (gfc_option.flag_whole_file == 1
1858 || ((gfc_option.warn_std & GFC_STD_LEGACY)
1859 &&
1860 !(gfc_option.warn_std & GFC_STD_GNU)))
1861 gfc_errors_to_warnings (1);
71a7778c
PT
1862
1863 gfc_procedure_use (gsym->ns->proc_name, actual, where);
3af8d8cb
PT
1864
1865 gfc_errors_to_warnings (0);
71a7778c
PT
1866 }
1867
68ea355b
PT
1868 if (gsym->type == GSYM_UNKNOWN)
1869 {
1870 gsym->type = type;
1871 gsym->where = *where;
1872 }
1873
1874 gsym->used = 1;
1875}
1524f80b 1876
edf1eac2 1877
6de9cd9a
DN
1878/************* Function resolution *************/
1879
1880/* Resolve a function call known to be generic.
1881 Section 14.1.2.4.1. */
1882
1883static match
edf1eac2 1884resolve_generic_f0 (gfc_expr *expr, gfc_symbol *sym)
6de9cd9a
DN
1885{
1886 gfc_symbol *s;
1887
1888 if (sym->attr.generic)
1889 {
edf1eac2 1890 s = gfc_search_interface (sym->generic, 0, &expr->value.function.actual);
6de9cd9a
DN
1891 if (s != NULL)
1892 {
1893 expr->value.function.name = s->name;
1894 expr->value.function.esym = s;
f5f701ad
PT
1895
1896 if (s->ts.type != BT_UNKNOWN)
1897 expr->ts = s->ts;
1898 else if (s->result != NULL && s->result->ts.type != BT_UNKNOWN)
1899 expr->ts = s->result->ts;
1900
6de9cd9a
DN
1901 if (s->as != NULL)
1902 expr->rank = s->as->rank;
f5f701ad
PT
1903 else if (s->result != NULL && s->result->as != NULL)
1904 expr->rank = s->result->as->rank;
1905
0a164a3c
PT
1906 gfc_set_sym_referenced (expr->value.function.esym);
1907
6de9cd9a
DN
1908 return MATCH_YES;
1909 }
1910
edf1eac2
SK
1911 /* TODO: Need to search for elemental references in generic
1912 interface. */
6de9cd9a
DN
1913 }
1914
1915 if (sym->attr.intrinsic)
1916 return gfc_intrinsic_func_interface (expr, 0);
1917
1918 return MATCH_NO;
1919}
1920
1921
17b1d2a0 1922static gfc_try
edf1eac2 1923resolve_generic_f (gfc_expr *expr)
6de9cd9a
DN
1924{
1925 gfc_symbol *sym;
1926 match m;
1927
1928 sym = expr->symtree->n.sym;
1929
1930 for (;;)
1931 {
1932 m = resolve_generic_f0 (expr, sym);
1933 if (m == MATCH_YES)
1934 return SUCCESS;
1935 else if (m == MATCH_ERROR)
1936 return FAILURE;
1937
1938generic:
1939 if (sym->ns->parent == NULL)
1940 break;
1941 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
1942
1943 if (sym == NULL)
1944 break;
1945 if (!generic_sym (sym))
1946 goto generic;
1947 }
1948
71f77fd7
PT
1949 /* Last ditch attempt. See if the reference is to an intrinsic
1950 that possesses a matching interface. 14.1.2.4 */
c3005b0f 1951 if (sym && !gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a 1952 {
8c086c9c 1953 gfc_error ("There is no specific function for the generic '%s' at %L",
6de9cd9a
DN
1954 expr->symtree->n.sym->name, &expr->where);
1955 return FAILURE;
1956 }
1957
1958 m = gfc_intrinsic_func_interface (expr, 0);
1959 if (m == MATCH_YES)
1960 return SUCCESS;
1961 if (m == MATCH_NO)
edf1eac2
SK
1962 gfc_error ("Generic function '%s' at %L is not consistent with a "
1963 "specific intrinsic interface", expr->symtree->n.sym->name,
1964 &expr->where);
6de9cd9a
DN
1965
1966 return FAILURE;
1967}
1968
1969
1970/* Resolve a function call known to be specific. */
1971
1972static match
edf1eac2 1973resolve_specific_f0 (gfc_symbol *sym, gfc_expr *expr)
6de9cd9a
DN
1974{
1975 match m;
1976
1977 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
1978 {
1979 if (sym->attr.dummy)
1980 {
1981 sym->attr.proc = PROC_DUMMY;
1982 goto found;
1983 }
1984
1985 sym->attr.proc = PROC_EXTERNAL;
1986 goto found;
1987 }
1988
1989 if (sym->attr.proc == PROC_MODULE
1990 || sym->attr.proc == PROC_ST_FUNCTION
1991 || sym->attr.proc == PROC_INTERNAL)
1992 goto found;
1993
1994 if (sym->attr.intrinsic)
1995 {
1996 m = gfc_intrinsic_func_interface (expr, 1);
1997 if (m == MATCH_YES)
1998 return MATCH_YES;
1999 if (m == MATCH_NO)
edf1eac2
SK
2000 gfc_error ("Function '%s' at %L is INTRINSIC but is not compatible "
2001 "with an intrinsic", sym->name, &expr->where);
6de9cd9a
DN
2002
2003 return MATCH_ERROR;
2004 }
2005
2006 return MATCH_NO;
2007
2008found:
2009 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2010
a7c0b11d
JW
2011 if (sym->result)
2012 expr->ts = sym->result->ts;
2013 else
2014 expr->ts = sym->ts;
6de9cd9a
DN
2015 expr->value.function.name = sym->name;
2016 expr->value.function.esym = sym;
2017 if (sym->as != NULL)
2018 expr->rank = sym->as->rank;
2019
2020 return MATCH_YES;
2021}
2022
2023
17b1d2a0 2024static gfc_try
edf1eac2 2025resolve_specific_f (gfc_expr *expr)
6de9cd9a
DN
2026{
2027 gfc_symbol *sym;
2028 match m;
2029
2030 sym = expr->symtree->n.sym;
2031
2032 for (;;)
2033 {
2034 m = resolve_specific_f0 (sym, expr);
2035 if (m == MATCH_YES)
2036 return SUCCESS;
2037 if (m == MATCH_ERROR)
2038 return FAILURE;
2039
2040 if (sym->ns->parent == NULL)
2041 break;
2042
2043 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
2044
2045 if (sym == NULL)
2046 break;
2047 }
2048
2049 gfc_error ("Unable to resolve the specific function '%s' at %L",
2050 expr->symtree->n.sym->name, &expr->where);
2051
2052 return SUCCESS;
2053}
2054
2055
2056/* Resolve a procedure call not known to be generic nor specific. */
2057
17b1d2a0 2058static gfc_try
edf1eac2 2059resolve_unknown_f (gfc_expr *expr)
6de9cd9a
DN
2060{
2061 gfc_symbol *sym;
2062 gfc_typespec *ts;
2063
2064 sym = expr->symtree->n.sym;
2065
2066 if (sym->attr.dummy)
2067 {
2068 sym->attr.proc = PROC_DUMMY;
2069 expr->value.function.name = sym->name;
2070 goto set_type;
2071 }
2072
2073 /* See if we have an intrinsic function reference. */
2074
c3005b0f 2075 if (gfc_is_intrinsic (sym, 0, expr->where))
6de9cd9a
DN
2076 {
2077 if (gfc_intrinsic_func_interface (expr, 1) == MATCH_YES)
2078 return SUCCESS;
2079 return FAILURE;
2080 }
2081
2082 /* The reference is to an external name. */
2083
2084 sym->attr.proc = PROC_EXTERNAL;
2085 expr->value.function.name = sym->name;
2086 expr->value.function.esym = expr->symtree->n.sym;
2087
2088 if (sym->as != NULL)
2089 expr->rank = sym->as->rank;
2090
2091 /* Type of the expression is either the type of the symbol or the
2092 default type of the symbol. */
2093
2094set_type:
2095 gfc_procedure_use (sym, &expr->value.function.actual, &expr->where);
2096
2097 if (sym->ts.type != BT_UNKNOWN)
2098 expr->ts = sym->ts;
2099 else
2100 {
713485cc 2101 ts = gfc_get_default_type (sym->name, sym->ns);
6de9cd9a
DN
2102
2103 if (ts->type == BT_UNKNOWN)
2104 {
cf4d246b 2105 gfc_error ("Function '%s' at %L has no IMPLICIT type",
6de9cd9a
DN
2106 sym->name, &expr->where);
2107 return FAILURE;
2108 }
2109 else
2110 expr->ts = *ts;
2111 }
2112
2113 return SUCCESS;
2114}
2115
2116
e7c8ff56
PT
2117/* Return true, if the symbol is an external procedure. */
2118static bool
2119is_external_proc (gfc_symbol *sym)
2120{
2121 if (!sym->attr.dummy && !sym->attr.contained
2122 && !(sym->attr.intrinsic
c3005b0f 2123 || gfc_is_intrinsic (sym, sym->attr.subroutine, sym->declared_at))
e7c8ff56
PT
2124 && sym->attr.proc != PROC_ST_FUNCTION
2125 && !sym->attr.use_assoc
2126 && sym->name)
2127 return true;
c3005b0f
DK
2128
2129 return false;
e7c8ff56
PT
2130}
2131
2132
2054fc29
VR
2133/* Figure out if a function reference is pure or not. Also set the name
2134 of the function for a potential error message. Return nonzero if the
6de9cd9a 2135 function is PURE, zero if not. */
908a2235
PT
2136static int
2137pure_stmt_function (gfc_expr *, gfc_symbol *);
6de9cd9a
DN
2138
2139static int
edf1eac2 2140pure_function (gfc_expr *e, const char **name)
6de9cd9a
DN
2141{
2142 int pure;
2143
36f7dcae
PT
2144 *name = NULL;
2145
9ebe2d22
PT
2146 if (e->symtree != NULL
2147 && e->symtree->n.sym != NULL
2148 && e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
908a2235 2149 return pure_stmt_function (e, e->symtree->n.sym);
9ebe2d22 2150
6de9cd9a
DN
2151 if (e->value.function.esym)
2152 {
2153 pure = gfc_pure (e->value.function.esym);
2154 *name = e->value.function.esym->name;
2155 }
2156 else if (e->value.function.isym)
2157 {
2158 pure = e->value.function.isym->pure
edf1eac2 2159 || e->value.function.isym->elemental;
6de9cd9a
DN
2160 *name = e->value.function.isym->name;
2161 }
2162 else
2163 {
2164 /* Implicit functions are not pure. */
2165 pure = 0;
2166 *name = e->value.function.name;
2167 }
2168
2169 return pure;
2170}
2171
2172
908a2235
PT
2173static bool
2174impure_stmt_fcn (gfc_expr *e, gfc_symbol *sym,
2175 int *f ATTRIBUTE_UNUSED)
2176{
2177 const char *name;
2178
2179 /* Don't bother recursing into other statement functions
2180 since they will be checked individually for purity. */
2181 if (e->expr_type != EXPR_FUNCTION
2182 || !e->symtree
2183 || e->symtree->n.sym == sym
2184 || e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION)
2185 return false;
2186
2187 return pure_function (e, &name) ? false : true;
2188}
2189
2190
2191static int
2192pure_stmt_function (gfc_expr *e, gfc_symbol *sym)
2193{
2194 return gfc_traverse_expr (e, sym, impure_stmt_fcn, 0) ? 0 : 1;
2195}
2196
2197
17b1d2a0 2198static gfc_try
a8b3b0b6
CR
2199is_scalar_expr_ptr (gfc_expr *expr)
2200{
17b1d2a0 2201 gfc_try retval = SUCCESS;
a8b3b0b6
CR
2202 gfc_ref *ref;
2203 int start;
2204 int end;
2205
2206 /* See if we have a gfc_ref, which means we have a substring, array
2207 reference, or a component. */
2208 if (expr->ref != NULL)
2209 {
2210 ref = expr->ref;
2211 while (ref->next != NULL)
2212 ref = ref->next;
2213
2214 switch (ref->type)
2215 {
2216 case REF_SUBSTRING:
2217 if (ref->u.ss.length != NULL
2218 && ref->u.ss.length->length != NULL
2219 && ref->u.ss.start
2220 && ref->u.ss.start->expr_type == EXPR_CONSTANT
2221 && ref->u.ss.end
2222 && ref->u.ss.end->expr_type == EXPR_CONSTANT)
2223 {
2224 start = (int) mpz_get_si (ref->u.ss.start->value.integer);
2225 end = (int) mpz_get_si (ref->u.ss.end->value.integer);
2226 if (end - start + 1 != 1)
2227 retval = FAILURE;
2228 }
2229 else
2230 retval = FAILURE;
2231 break;
2232 case REF_ARRAY:
2233 if (ref->u.ar.type == AR_ELEMENT)
2234 retval = SUCCESS;
2235 else if (ref->u.ar.type == AR_FULL)
2236 {
2237 /* The user can give a full array if the array is of size 1. */
2238 if (ref->u.ar.as != NULL
2239 && ref->u.ar.as->rank == 1
2240 && ref->u.ar.as->type == AS_EXPLICIT
2241 && ref->u.ar.as->lower[0] != NULL
2242 && ref->u.ar.as->lower[0]->expr_type == EXPR_CONSTANT
2243 && ref->u.ar.as->upper[0] != NULL
2244 && ref->u.ar.as->upper[0]->expr_type == EXPR_CONSTANT)
2245 {
2246 /* If we have a character string, we need to check if
2247 its length is one. */
2248 if (expr->ts.type == BT_CHARACTER)
2249 {
bc21d315
JW
2250 if (expr->ts.u.cl == NULL
2251 || expr->ts.u.cl->length == NULL
2252 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1)
a8b3b0b6
CR
2253 != 0)
2254 retval = FAILURE;
2255 }
2256 else
2257 {
3759634f
SK
2258 /* We have constant lower and upper bounds. If the
2259 difference between is 1, it can be considered a
2260 scalar. */
2261 start = (int) mpz_get_si
2262 (ref->u.ar.as->lower[0]->value.integer);
2263 end = (int) mpz_get_si
2264 (ref->u.ar.as->upper[0]->value.integer);
2265 if (end - start + 1 != 1)
2266 retval = FAILURE;
2267 }
a8b3b0b6
CR
2268 }
2269 else
2270 retval = FAILURE;
2271 }
2272 else
2273 retval = FAILURE;
2274 break;
2275 default:
2276 retval = SUCCESS;
2277 break;
2278 }
2279 }
2280 else if (expr->ts.type == BT_CHARACTER && expr->rank == 0)
2281 {
2282 /* Character string. Make sure it's of length 1. */
bc21d315
JW
2283 if (expr->ts.u.cl == NULL
2284 || expr->ts.u.cl->length == NULL
2285 || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)
a8b3b0b6
CR
2286 retval = FAILURE;
2287 }
2288 else if (expr->rank != 0)
2289 retval = FAILURE;
2290
2291 return retval;
2292}
2293
2294
2295/* Match one of the iso_c_binding functions (c_associated or c_loc)
2296 and, in the case of c_associated, set the binding label based on
2297 the arguments. */
2298
17b1d2a0 2299static gfc_try
a8b3b0b6
CR
2300gfc_iso_c_func_interface (gfc_symbol *sym, gfc_actual_arglist *args,
2301 gfc_symbol **new_sym)
2302{
2303 char name[GFC_MAX_SYMBOL_LEN + 1];
2304 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
23f2d017 2305 int optional_arg = 0, is_pointer = 0;
17b1d2a0 2306 gfc_try retval = SUCCESS;
a8b3b0b6 2307 gfc_symbol *args_sym;
15231566 2308 gfc_typespec *arg_ts;
a8b3b0b6 2309
aa5e22f0
CR
2310 if (args->expr->expr_type == EXPR_CONSTANT
2311 || args->expr->expr_type == EXPR_OP
2312 || args->expr->expr_type == EXPR_NULL)
2313 {
2314 gfc_error ("Argument to '%s' at %L is not a variable",
2315 sym->name, &(args->expr->where));
2316 return FAILURE;
2317 }
2318
a8b3b0b6 2319 args_sym = args->expr->symtree->n.sym;
15231566
CR
2320
2321 /* The typespec for the actual arg should be that stored in the expr
2322 and not necessarily that of the expr symbol (args_sym), because
2323 the actual expression could be a part-ref of the expr symbol. */
2324 arg_ts = &(args->expr->ts);
2325
23f2d017
MM
2326 is_pointer = gfc_is_data_pointer (args->expr);
2327
a8b3b0b6
CR
2328 if (sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)
2329 {
2330 /* If the user gave two args then they are providing something for
2331 the optional arg (the second cptr). Therefore, set the name and
2332 binding label to the c_associated for two cptrs. Otherwise,
2333 set c_associated to expect one cptr. */
2334 if (args->next)
2335 {
2336 /* two args. */
2337 sprintf (name, "%s_2", sym->name);
2338 sprintf (binding_label, "%s_2", sym->binding_label);
2339 optional_arg = 1;
2340 }
2341 else
2342 {
2343 /* one arg. */
2344 sprintf (name, "%s_1", sym->name);
2345 sprintf (binding_label, "%s_1", sym->binding_label);
2346 optional_arg = 0;
2347 }
2348
2349 /* Get a new symbol for the version of c_associated that
2350 will get called. */
2351 *new_sym = get_iso_c_sym (sym, name, binding_label, optional_arg);
2352 }
2353 else if (sym->intmod_sym_id == ISOCBINDING_LOC
2354 || sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2355 {
2356 sprintf (name, "%s", sym->name);
2357 sprintf (binding_label, "%s", sym->binding_label);
2358
2359 /* Error check the call. */
2360 if (args->next != NULL)
2361 {
2362 gfc_error_now ("More actual than formal arguments in '%s' "
2363 "call at %L", name, &(args->expr->where));
2364 retval = FAILURE;
2365 }
2366 else if (sym->intmod_sym_id == ISOCBINDING_LOC)
2367 {
2368 /* Make sure we have either the target or pointer attribute. */
23f2d017 2369 if (!args_sym->attr.target && !is_pointer)
a8b3b0b6
CR
2370 {
2371 gfc_error_now ("Parameter '%s' to '%s' at %L must be either "
2372 "a TARGET or an associated pointer",
15231566 2373 args_sym->name,
a8b3b0b6
CR
2374 sym->name, &(args->expr->where));
2375 retval = FAILURE;
2376 }
2377
2378 /* See if we have interoperable type and type param. */
2ec855f1 2379 if (verify_c_interop (arg_ts) == SUCCESS
15231566 2380 || gfc_check_any_c_kind (arg_ts) == SUCCESS)
a8b3b0b6
CR
2381 {
2382 if (args_sym->attr.target == 1)
2383 {
2384 /* Case 1a, section 15.1.2.5, J3/04-007: variable that
2385 has the target attribute and is interoperable. */
2386 /* Case 1b, section 15.1.2.5, J3/04-007: allocated
2387 allocatable variable that has the TARGET attribute and
2388 is not an array of zero size. */
2389 if (args_sym->attr.allocatable == 1)
2390 {
2391 if (args_sym->attr.dimension != 0
2392 && (args_sym->as && args_sym->as->rank == 0))
2393 {
2394 gfc_error_now ("Allocatable variable '%s' used as a "
2395 "parameter to '%s' at %L must not be "
2396 "an array of zero size",
2397 args_sym->name, sym->name,
2398 &(args->expr->where));
2399 retval = FAILURE;
2400 }
2401 }
2402 else
21a77227
CR
2403 {
2404 /* A non-allocatable target variable with C
2405 interoperable type and type parameters must be
2406 interoperable. */
2407 if (args_sym && args_sym->attr.dimension)
2408 {
2409 if (args_sym->as->type == AS_ASSUMED_SHAPE)
2410 {
2411 gfc_error ("Assumed-shape array '%s' at %L "
2412 "cannot be an argument to the "
2413 "procedure '%s' because "
2414 "it is not C interoperable",
2415 args_sym->name,
2416 &(args->expr->where), sym->name);
2417 retval = FAILURE;
2418 }
2419 else if (args_sym->as->type == AS_DEFERRED)
2420 {
2421 gfc_error ("Deferred-shape array '%s' at %L "
2422 "cannot be an argument to the "
2423 "procedure '%s' because "
2424 "it is not C interoperable",
2425 args_sym->name,
2426 &(args->expr->where), sym->name);
2427 retval = FAILURE;
2428 }
2429 }
2430
a8b3b0b6
CR
2431 /* Make sure it's not a character string. Arrays of
2432 any type should be ok if the variable is of a C
2433 interoperable type. */
15231566 2434 if (arg_ts->type == BT_CHARACTER)
bc21d315
JW
2435 if (arg_ts->u.cl != NULL
2436 && (arg_ts->u.cl->length == NULL
2437 || arg_ts->u.cl->length->expr_type
21a77227
CR
2438 != EXPR_CONSTANT
2439 || mpz_cmp_si
bc21d315 2440 (arg_ts->u.cl->length->value.integer, 1)
21a77227
CR
2441 != 0)
2442 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2443 {
2444 gfc_error_now ("CHARACTER argument '%s' to '%s' "
2445 "at %L must have a length of 1",
2446 args_sym->name, sym->name,
2447 &(args->expr->where));
2448 retval = FAILURE;
2449 }
a8b3b0b6
CR
2450 }
2451 }
23f2d017 2452 else if (is_pointer
15231566 2453 && is_scalar_expr_ptr (args->expr) != SUCCESS)
a8b3b0b6
CR
2454 {
2455 /* Case 1c, section 15.1.2.5, J3/04-007: an associated
2456 scalar pointer. */
2457 gfc_error_now ("Argument '%s' to '%s' at %L must be an "
2458 "associated scalar POINTER", args_sym->name,
2459 sym->name, &(args->expr->where));
2460 retval = FAILURE;
2461 }
2462 }
2463 else
2464 {
2465 /* The parameter is not required to be C interoperable. If it
2466 is not C interoperable, it must be a nonpolymorphic scalar
2467 with no length type parameters. It still must have either
2468 the pointer or target attribute, and it can be
2469 allocatable (but must be allocated when c_loc is called). */
15231566 2470 if (args->expr->rank != 0
a8b3b0b6
CR
2471 && is_scalar_expr_ptr (args->expr) != SUCCESS)
2472 {
2473 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2474 "scalar", args_sym->name, sym->name,
2475 &(args->expr->where));
2476 retval = FAILURE;
2477 }
15231566 2478 else if (arg_ts->type == BT_CHARACTER
21a77227 2479 && is_scalar_expr_ptr (args->expr) != SUCCESS)
a8b3b0b6 2480 {
21a77227
CR
2481 gfc_error_now ("CHARACTER argument '%s' to '%s' at "
2482 "%L must have a length of 1",
a8b3b0b6
CR
2483 args_sym->name, sym->name,
2484 &(args->expr->where));
2485 retval = FAILURE;
2486 }
2487 }
2488 }
2489 else if (sym->intmod_sym_id == ISOCBINDING_FUNLOC)
2490 {
15231566 2491 if (args_sym->attr.flavor != FL_PROCEDURE)
a8b3b0b6
CR
2492 {
2493 /* TODO: Update this error message to allow for procedure
2494 pointers once they are implemented. */
2495 gfc_error_now ("Parameter '%s' to '%s' at %L must be a "
2496 "procedure",
15231566 2497 args_sym->name, sym->name,
a8b3b0b6
CR
2498 &(args->expr->where));
2499 retval = FAILURE;
2500 }
15231566 2501 else if (args_sym->attr.is_bind_c != 1)
089db47d
CR
2502 {
2503 gfc_error_now ("Parameter '%s' to '%s' at %L must be "
2504 "BIND(C)",
15231566 2505 args_sym->name, sym->name,
089db47d
CR
2506 &(args->expr->where));
2507 retval = FAILURE;
2508 }
a8b3b0b6
CR
2509 }
2510
2511 /* for c_loc/c_funloc, the new symbol is the same as the old one */
2512 *new_sym = sym;
2513 }
2514 else
2515 {
2516 gfc_internal_error ("gfc_iso_c_func_interface(): Unhandled "
2517 "iso_c_binding function: '%s'!\n", sym->name);
2518 }
2519
2520 return retval;
2521}
2522
2523
6de9cd9a
DN
2524/* Resolve a function call, which means resolving the arguments, then figuring
2525 out which entity the name refers to. */
2526/* TODO: Check procedure arguments so that an INTENT(IN) isn't passed
2527 to INTENT(OUT) or INTENT(INOUT). */
2528
17b1d2a0 2529static gfc_try
edf1eac2 2530resolve_function (gfc_expr *expr)
6de9cd9a
DN
2531{
2532 gfc_actual_arglist *arg;
edf1eac2 2533 gfc_symbol *sym;
6b25a558 2534 const char *name;
17b1d2a0 2535 gfc_try t;
48474141 2536 int temp;
7fcafa71 2537 procedure_type p = PROC_INTRINSIC;
0b4e2af7 2538 bool no_formal_args;
48474141 2539
20236f90
PT
2540 sym = NULL;
2541 if (expr->symtree)
2542 sym = expr->symtree->n.sym;
2543
6c036626
JW
2544 /* If this is a procedure pointer component, it has already been resolved. */
2545 if (gfc_is_proc_ptr_comp (expr, NULL))
2546 return SUCCESS;
2547
2c68bc89 2548 if (sym && sym->attr.intrinsic
c73b6478
JW
2549 && resolve_intrinsic (sym, &expr->where) == FAILURE)
2550 return FAILURE;
2c68bc89 2551
726d8566 2552 if (sym && (sym->attr.flavor == FL_VARIABLE || sym->attr.subroutine))
20a037d5 2553 {
edf1eac2 2554 gfc_error ("'%s' at %L is not a function", sym->name, &expr->where);
20a037d5
PT
2555 return FAILURE;
2556 }
2557
8bae6273
JW
2558 /* If this ia a deferred TBP with an abstract interface (which may
2559 of course be referenced), expr->value.function.name will be set. */
2560 if (sym && sym->attr.abstract && !expr->value.function.name)
9e1d712c
TB
2561 {
2562 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
2563 sym->name, &expr->where);
2564 return FAILURE;
2565 }
2566
48474141
PT
2567 /* Switch off assumed size checking and do this again for certain kinds
2568 of procedure, once the procedure itself is resolved. */
2569 need_full_assumed_size++;
6de9cd9a 2570
7fcafa71
PT
2571 if (expr->symtree && expr->symtree->n.sym)
2572 p = expr->symtree->n.sym->attr.proc;
2573
0b4e2af7
PT
2574 no_formal_args = sym && is_external_proc (sym) && sym->formal == NULL;
2575 if (resolve_actual_arglist (expr->value.function.actual,
2576 p, no_formal_args) == FAILURE)
7fcafa71 2577 return FAILURE;
6de9cd9a 2578
a8b3b0b6
CR
2579 /* Need to setup the call to the correct c_associated, depending on
2580 the number of cptrs to user gives to compare. */
2581 if (sym && sym->attr.is_iso_c == 1)
2582 {
2583 if (gfc_iso_c_func_interface (sym, expr->value.function.actual, &sym)
2584 == FAILURE)
2585 return FAILURE;
2586
2587 /* Get the symtree for the new symbol (resolved func).
2588 the old one will be freed later, when it's no longer used. */
2589 gfc_find_sym_tree (sym->name, sym->ns, 1, &(expr->symtree));
2590 }
2591
2592 /* Resume assumed_size checking. */
48474141
PT
2593 need_full_assumed_size--;
2594
71a7778c
PT
2595 /* If the procedure is external, check for usage. */
2596 if (sym && is_external_proc (sym))
2597 resolve_global_procedure (sym, &expr->where,
2598 &expr->value.function.actual, 0);
2599
20236f90 2600 if (sym && sym->ts.type == BT_CHARACTER
bc21d315
JW
2601 && sym->ts.u.cl
2602 && sym->ts.u.cl->length == NULL
edf1eac2
SK
2603 && !sym->attr.dummy
2604 && expr->value.function.esym == NULL
2605 && !sym->attr.contained)
20236f90 2606 {
20236f90 2607 /* Internal procedures are taken care of in resolve_contained_fntype. */
0e3e65bc
PT
2608 gfc_error ("Function '%s' is declared CHARACTER(*) and cannot "
2609 "be used at %L since it is not a dummy argument",
2610 sym->name, &expr->where);
2611 return FAILURE;
20236f90
PT
2612 }
2613
edf1eac2 2614 /* See if function is already resolved. */
6de9cd9a
DN
2615
2616 if (expr->value.function.name != NULL)
2617 {
2618 if (expr->ts.type == BT_UNKNOWN)
20236f90 2619 expr->ts = sym->ts;
6de9cd9a
DN
2620 t = SUCCESS;
2621 }
2622 else
2623 {
2624 /* Apply the rules of section 14.1.2. */
2625
20236f90 2626 switch (procedure_kind (sym))
6de9cd9a
DN
2627 {
2628 case PTYPE_GENERIC:
2629 t = resolve_generic_f (expr);
2630 break;
2631
2632 case PTYPE_SPECIFIC:
2633 t = resolve_specific_f (expr);
2634 break;
2635
2636 case PTYPE_UNKNOWN:
2637 t = resolve_unknown_f (expr);
2638 break;
2639
2640 default:
2641 gfc_internal_error ("resolve_function(): bad function type");
2642 }
2643 }
2644
2645 /* If the expression is still a function (it might have simplified),
2646 then we check to see if we are calling an elemental function. */
2647
2648 if (expr->expr_type != EXPR_FUNCTION)
2649 return t;
2650
48474141
PT
2651 temp = need_full_assumed_size;
2652 need_full_assumed_size = 0;
2653
b8ea6dbc
PT
2654 if (resolve_elemental_actual (expr, NULL) == FAILURE)
2655 return FAILURE;
48474141 2656
6c7a4dfd
JJ
2657 if (omp_workshare_flag
2658 && expr->value.function.esym
2659 && ! gfc_elemental (expr->value.function.esym))
2660 {
edf1eac2
SK
2661 gfc_error ("User defined non-ELEMENTAL function '%s' at %L not allowed "
2662 "in WORKSHARE construct", expr->value.function.esym->name,
6c7a4dfd
JJ
2663 &expr->where);
2664 t = FAILURE;
2665 }
6de9cd9a 2666
cd5ecab6 2667#define GENERIC_ID expr->value.function.isym->id
48474141 2668 else if (expr->value.function.actual != NULL
edf1eac2
SK
2669 && expr->value.function.isym != NULL
2670 && GENERIC_ID != GFC_ISYM_LBOUND
2671 && GENERIC_ID != GFC_ISYM_LEN
2672 && GENERIC_ID != GFC_ISYM_LOC
2673 && GENERIC_ID != GFC_ISYM_PRESENT)
48474141 2674 {
fa951694 2675 /* Array intrinsics must also have the last upper bound of an
b82feea5 2676 assumed size array argument. UBOUND and SIZE have to be
48474141
PT
2677 excluded from the check if the second argument is anything
2678 than a constant. */
05c1e3a7 2679
48474141
PT
2680 for (arg = expr->value.function.actual; arg; arg = arg->next)
2681 {
7a687b22
TB
2682 if ((GENERIC_ID == GFC_ISYM_UBOUND || GENERIC_ID == GFC_ISYM_SIZE)
2683 && arg->next != NULL && arg->next->expr)
9ebe2d22
PT
2684 {
2685 if (arg->next->expr->expr_type != EXPR_CONSTANT)
2686 break;
2687
7a687b22
TB
2688 if (arg->next->name && strncmp(arg->next->name, "kind", 4) == 0)
2689 break;
2690
9ebe2d22
PT
2691 if ((int)mpz_get_si (arg->next->expr->value.integer)
2692 < arg->expr->rank)
2693 break;
2694 }
05c1e3a7 2695
48474141 2696 if (arg->expr != NULL
edf1eac2
SK
2697 && arg->expr->rank > 0
2698 && resolve_assumed_size_actual (arg->expr))
48474141
PT
2699 return FAILURE;
2700 }
2701 }
4d4074e4 2702#undef GENERIC_ID
48474141
PT
2703
2704 need_full_assumed_size = temp;
36f7dcae 2705 name = NULL;
48474141 2706
5f20c93a 2707 if (!pure_function (expr, &name) && name)
6de9cd9a
DN
2708 {
2709 if (forall_flag)
2710 {
edf1eac2
SK
2711 gfc_error ("reference to non-PURE function '%s' at %L inside a "
2712 "FORALL %s", name, &expr->where,
2713 forall_flag == 2 ? "mask" : "block");
6de9cd9a
DN
2714 t = FAILURE;
2715 }
2716 else if (gfc_pure (NULL))
2717 {
2718 gfc_error ("Function reference to '%s' at %L is to a non-PURE "
2719 "procedure within a PURE procedure", name, &expr->where);
2720 t = FAILURE;
2721 }
2722 }
2723
77f131ca
FXC
2724 /* Functions without the RECURSIVE attribution are not allowed to
2725 * call themselves. */
2726 if (expr->value.function.esym && !expr->value.function.esym->attr.recursive)
2727 {
1933ba0f 2728 gfc_symbol *esym;
77f131ca 2729 esym = expr->value.function.esym;
77f131ca 2730
1933ba0f 2731 if (is_illegal_recursion (esym, gfc_current_ns))
77f131ca 2732 {
1933ba0f
DK
2733 if (esym->attr.entry && esym->ns->entries)
2734 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
2735 " function '%s' is not RECURSIVE",
2736 esym->name, &expr->where, esym->ns->entries->sym->name);
2737 else
2738 gfc_error ("Function '%s' at %L cannot be called recursively, as it"
2739 " is not RECURSIVE", esym->name, &expr->where);
2740
edf1eac2 2741 t = FAILURE;
77f131ca
FXC
2742 }
2743 }
2744
47992a4a
EE
2745 /* Character lengths of use associated functions may contains references to
2746 symbols not referenced from the current program unit otherwise. Make sure
2747 those symbols are marked as referenced. */
2748
05c1e3a7 2749 if (expr->ts.type == BT_CHARACTER && expr->value.function.esym
47992a4a
EE
2750 && expr->value.function.esym->attr.use_assoc)
2751 {
bc21d315 2752 gfc_expr_set_symbols_referenced (expr->ts.u.cl->length);
47992a4a
EE
2753 }
2754
23d1b451
PT
2755 if (t == SUCCESS
2756 && !((expr->value.function.esym
2757 && expr->value.function.esym->attr.elemental)
2758 ||
2759 (expr->value.function.isym
2760 && expr->value.function.isym->elemental)))
1524f80b
RS
2761 find_noncopying_intrinsics (expr->value.function.esym,
2762 expr->value.function.actual);
9ebe2d22
PT
2763
2764 /* Make sure that the expression has a typespec that works. */
2765 if (expr->ts.type == BT_UNKNOWN)
2766 {
2767 if (expr->symtree->n.sym->result
3070bab4
JW
2768 && expr->symtree->n.sym->result->ts.type != BT_UNKNOWN
2769 && !expr->symtree->n.sym->result->attr.proc_pointer)
9ebe2d22 2770 expr->ts = expr->symtree->n.sym->result->ts;
9ebe2d22
PT
2771 }
2772
6de9cd9a
DN
2773 return t;
2774}
2775
2776
2777/************* Subroutine resolution *************/
2778
2779static void
edf1eac2 2780pure_subroutine (gfc_code *c, gfc_symbol *sym)
6de9cd9a 2781{
6de9cd9a
DN
2782 if (gfc_pure (sym))
2783 return;
2784
2785 if (forall_flag)
2786 gfc_error ("Subroutine call to '%s' in FORALL block at %L is not PURE",
2787 sym->name, &c->loc);
2788 else if (gfc_pure (NULL))
2789 gfc_error ("Subroutine call to '%s' at %L is not PURE", sym->name,
2790 &c->loc);
2791}
2792
2793
2794static match
edf1eac2 2795resolve_generic_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
2796{
2797 gfc_symbol *s;
2798
2799 if (sym->attr.generic)
2800 {
2801 s = gfc_search_interface (sym->generic, 1, &c->ext.actual);
2802 if (s != NULL)
2803 {
edf1eac2 2804 c->resolved_sym = s;
6de9cd9a
DN
2805 pure_subroutine (c, s);
2806 return MATCH_YES;
2807 }
2808
2809 /* TODO: Need to search for elemental references in generic interface. */
2810 }
2811
2812 if (sym->attr.intrinsic)
2813 return gfc_intrinsic_sub_interface (c, 0);
2814
2815 return MATCH_NO;
2816}
2817
2818
17b1d2a0 2819static gfc_try
edf1eac2 2820resolve_generic_s (gfc_code *c)
6de9cd9a
DN
2821{
2822 gfc_symbol *sym;
2823 match m;
2824
2825 sym = c->symtree->n.sym;
2826
8c086c9c 2827 for (;;)
6de9cd9a 2828 {
8c086c9c
PT
2829 m = resolve_generic_s0 (c, sym);
2830 if (m == MATCH_YES)
2831 return SUCCESS;
2832 else if (m == MATCH_ERROR)
2833 return FAILURE;
2834
2835generic:
2836 if (sym->ns->parent == NULL)
2837 break;
6de9cd9a 2838 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
8c086c9c
PT
2839
2840 if (sym == NULL)
2841 break;
2842 if (!generic_sym (sym))
2843 goto generic;
6de9cd9a
DN
2844 }
2845
71f77fd7
PT
2846 /* Last ditch attempt. See if the reference is to an intrinsic
2847 that possesses a matching interface. 14.1.2.4 */
8c086c9c 2848 sym = c->symtree->n.sym;
71f77fd7 2849
c3005b0f 2850 if (!gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a 2851 {
edf1eac2
SK
2852 gfc_error ("There is no specific subroutine for the generic '%s' at %L",
2853 sym->name, &c->loc);
6de9cd9a
DN
2854 return FAILURE;
2855 }
2856
2857 m = gfc_intrinsic_sub_interface (c, 0);
2858 if (m == MATCH_YES)
2859 return SUCCESS;
2860 if (m == MATCH_NO)
2861 gfc_error ("Generic subroutine '%s' at %L is not consistent with an "
2862 "intrinsic subroutine interface", sym->name, &c->loc);
2863
2864 return FAILURE;
2865}
2866
2867
a8b3b0b6
CR
2868/* Set the name and binding label of the subroutine symbol in the call
2869 expression represented by 'c' to include the type and kind of the
2870 second parameter. This function is for resolving the appropriate
2871 version of c_f_pointer() and c_f_procpointer(). For example, a
2872 call to c_f_pointer() for a default integer pointer could have a
2873 name of c_f_pointer_i4. If no second arg exists, which is an error
2874 for these two functions, it defaults to the generic symbol's name
2875 and binding label. */
2876
2877static void
2878set_name_and_label (gfc_code *c, gfc_symbol *sym,
2879 char *name, char *binding_label)
2880{
2881 gfc_expr *arg = NULL;
2882 char type;
2883 int kind;
2884
2885 /* The second arg of c_f_pointer and c_f_procpointer determines
2886 the type and kind for the procedure name. */
2887 arg = c->ext.actual->next->expr;
2888
2889 if (arg != NULL)
2890 {
2891 /* Set up the name to have the given symbol's name,
2892 plus the type and kind. */
2893 /* a derived type is marked with the type letter 'u' */
2894 if (arg->ts.type == BT_DERIVED)
2895 {
2896 type = 'd';
2897 kind = 0; /* set the kind as 0 for now */
2898 }
2899 else
2900 {
2901 type = gfc_type_letter (arg->ts.type);
2902 kind = arg->ts.kind;
2903 }
6ad5cf72
CR
2904
2905 if (arg->ts.type == BT_CHARACTER)
2906 /* Kind info for character strings not needed. */
2907 kind = 0;
2908
a8b3b0b6
CR
2909 sprintf (name, "%s_%c%d", sym->name, type, kind);
2910 /* Set up the binding label as the given symbol's label plus
2911 the type and kind. */
2912 sprintf (binding_label, "%s_%c%d", sym->binding_label, type, kind);
2913 }
2914 else
2915 {
2916 /* If the second arg is missing, set the name and label as
2917 was, cause it should at least be found, and the missing
2918 arg error will be caught by compare_parameters(). */
2919 sprintf (name, "%s", sym->name);
2920 sprintf (binding_label, "%s", sym->binding_label);
2921 }
2922
2923 return;
2924}
2925
2926
2927/* Resolve a generic version of the iso_c_binding procedure given
2928 (sym) to the specific one based on the type and kind of the
2929 argument(s). Currently, this function resolves c_f_pointer() and
2930 c_f_procpointer based on the type and kind of the second argument
2931 (FPTR). Other iso_c_binding procedures aren't specially handled.
2932 Upon successfully exiting, c->resolved_sym will hold the resolved
2933 symbol. Returns MATCH_ERROR if an error occurred; MATCH_YES
2934 otherwise. */
2935
2936match
2937gfc_iso_c_sub_interface (gfc_code *c, gfc_symbol *sym)
2938{
2939 gfc_symbol *new_sym;
2940 /* this is fine, since we know the names won't use the max */
2941 char name[GFC_MAX_SYMBOL_LEN + 1];
2942 char binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
2943 /* default to success; will override if find error */
2944 match m = MATCH_YES;
d8fa96e0
CR
2945
2946 /* Make sure the actual arguments are in the necessary order (based on the
2947 formal args) before resolving. */
2948 gfc_procedure_use (sym, &c->ext.actual, &(c->loc));
2949
a8b3b0b6
CR
2950 if ((sym->intmod_sym_id == ISOCBINDING_F_POINTER) ||
2951 (sym->intmod_sym_id == ISOCBINDING_F_PROCPOINTER))
2952 {
2953 set_name_and_label (c, sym, name, binding_label);
2954
2955 if (sym->intmod_sym_id == ISOCBINDING_F_POINTER)
2956 {
2957 if (c->ext.actual != NULL && c->ext.actual->next != NULL)
2958 {
d8fa96e0
CR
2959 /* Make sure we got a third arg if the second arg has non-zero
2960 rank. We must also check that the type and rank are
2961 correct since we short-circuit this check in
2962 gfc_procedure_use() (called above to sort actual args). */
2963 if (c->ext.actual->next->expr->rank != 0)
a8b3b0b6 2964 {
d8fa96e0
CR
2965 if(c->ext.actual->next->next == NULL
2966 || c->ext.actual->next->next->expr == NULL)
2967 {
2968 m = MATCH_ERROR;
2969 gfc_error ("Missing SHAPE parameter for call to %s "
2970 "at %L", sym->name, &(c->loc));
2971 }
2972 else if (c->ext.actual->next->next->expr->ts.type
2973 != BT_INTEGER
2974 || c->ext.actual->next->next->expr->rank != 1)
2975 {
2976 m = MATCH_ERROR;
2977 gfc_error ("SHAPE parameter for call to %s at %L must "
2978 "be a rank 1 INTEGER array", sym->name,
2979 &(c->loc));
2980 }
a8b3b0b6 2981 }
a8b3b0b6
CR
2982 }
2983 }
2984
2985 if (m != MATCH_ERROR)
2986 {
2987 /* the 1 means to add the optional arg to formal list */
2988 new_sym = get_iso_c_sym (sym, name, binding_label, 1);
2989
2990 /* for error reporting, say it's declared where the original was */
2991 new_sym->declared_at = sym->declared_at;
2992 }
2993 }
a8b3b0b6
CR
2994 else
2995 {
2996 /* no differences for c_loc or c_funloc */
2997 new_sym = sym;
2998 }
2999
3000 /* set the resolved symbol */
3001 if (m != MATCH_ERROR)
d8fa96e0 3002 c->resolved_sym = new_sym;
a8b3b0b6
CR
3003 else
3004 c->resolved_sym = sym;
3005
3006 return m;
3007}
3008
3009
6de9cd9a
DN
3010/* Resolve a subroutine call known to be specific. */
3011
3012static match
edf1eac2 3013resolve_specific_s0 (gfc_code *c, gfc_symbol *sym)
6de9cd9a
DN
3014{
3015 match m;
3016
a8b3b0b6
CR
3017 if(sym->attr.is_iso_c)
3018 {
3019 m = gfc_iso_c_sub_interface (c,sym);
3020 return m;
3021 }
3022
6de9cd9a
DN
3023 if (sym->attr.external || sym->attr.if_source == IFSRC_IFBODY)
3024 {
3025 if (sym->attr.dummy)
3026 {
3027 sym->attr.proc = PROC_DUMMY;
3028 goto found;
3029 }
3030
3031 sym->attr.proc = PROC_EXTERNAL;
3032 goto found;
3033 }
3034
3035 if (sym->attr.proc == PROC_MODULE || sym->attr.proc == PROC_INTERNAL)
3036 goto found;
3037
3038 if (sym->attr.intrinsic)
3039 {
3040 m = gfc_intrinsic_sub_interface (c, 1);
3041 if (m == MATCH_YES)
3042 return MATCH_YES;
3043 if (m == MATCH_NO)
3044 gfc_error ("Subroutine '%s' at %L is INTRINSIC but is not compatible "
3045 "with an intrinsic", sym->name, &c->loc);
3046
3047 return MATCH_ERROR;
3048 }
3049
3050 return MATCH_NO;
3051
3052found:
3053 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3054
3055 c->resolved_sym = sym;
3056 pure_subroutine (c, sym);
3057
3058 return MATCH_YES;
3059}
3060
3061
17b1d2a0 3062static gfc_try
edf1eac2 3063resolve_specific_s (gfc_code *c)
6de9cd9a
DN
3064{
3065 gfc_symbol *sym;
3066 match m;
3067
3068 sym = c->symtree->n.sym;
3069
8c086c9c 3070 for (;;)
6de9cd9a
DN
3071 {
3072 m = resolve_specific_s0 (c, sym);
3073 if (m == MATCH_YES)
3074 return SUCCESS;
3075 if (m == MATCH_ERROR)
3076 return FAILURE;
8c086c9c
PT
3077
3078 if (sym->ns->parent == NULL)
3079 break;
3080
3081 gfc_find_symbol (sym->name, sym->ns->parent, 1, &sym);
3082
3083 if (sym == NULL)
3084 break;
6de9cd9a
DN
3085 }
3086
8c086c9c 3087 sym = c->symtree->n.sym;
6de9cd9a
DN
3088 gfc_error ("Unable to resolve the specific subroutine '%s' at %L",
3089 sym->name, &c->loc);
3090
3091 return FAILURE;
3092}
3093
3094
3095/* Resolve a subroutine call not known to be generic nor specific. */
3096
17b1d2a0 3097static gfc_try
edf1eac2 3098resolve_unknown_s (gfc_code *c)
6de9cd9a
DN
3099{
3100 gfc_symbol *sym;
3101
3102 sym = c->symtree->n.sym;
3103
3104 if (sym->attr.dummy)
3105 {
3106 sym->attr.proc = PROC_DUMMY;
3107 goto found;
3108 }
3109
3110 /* See if we have an intrinsic function reference. */
3111
c3005b0f 3112 if (gfc_is_intrinsic (sym, 1, c->loc))
6de9cd9a
DN
3113 {
3114 if (gfc_intrinsic_sub_interface (c, 1) == MATCH_YES)
3115 return SUCCESS;
3116 return FAILURE;
3117 }
3118
3119 /* The reference is to an external name. */
3120
3121found:
3122 gfc_procedure_use (sym, &c->ext.actual, &c->loc);
3123
3124 c->resolved_sym = sym;
3125
3126 pure_subroutine (c, sym);
3127
3128 return SUCCESS;
3129}
3130
3131
3132/* Resolve a subroutine call. Although it was tempting to use the same code
3133 for functions, subroutines and functions are stored differently and this
3134 makes things awkward. */
3135
17b1d2a0 3136static gfc_try
edf1eac2 3137resolve_call (gfc_code *c)
6de9cd9a 3138{
17b1d2a0 3139 gfc_try t;
7fcafa71 3140 procedure_type ptype = PROC_INTRINSIC;
67cec813 3141 gfc_symbol *csym, *sym;
0b4e2af7
PT
3142 bool no_formal_args;
3143
3144 csym = c->symtree ? c->symtree->n.sym : NULL;
6de9cd9a 3145
0b4e2af7 3146 if (csym && csym->ts.type != BT_UNKNOWN)
2ed8d224
PT
3147 {
3148 gfc_error ("'%s' at %L has a type, which is not consistent with "
0b4e2af7 3149 "the CALL at %L", csym->name, &csym->declared_at, &c->loc);
2ed8d224
PT
3150 return FAILURE;
3151 }
3152
67cec813
PT
3153 if (csym && gfc_current_ns->parent && csym->ns != gfc_current_ns)
3154 {
79b1d36c
PT
3155 gfc_symtree *st;
3156 gfc_find_sym_tree (csym->name, gfc_current_ns, 1, &st);
3157 sym = st ? st->n.sym : NULL;
67cec813
PT
3158 if (sym && csym != sym
3159 && sym->ns == gfc_current_ns
3160 && sym->attr.flavor == FL_PROCEDURE
3161 && sym->attr.contained)
3162 {
3163 sym->refs++;
79b1d36c
PT
3164 if (csym->attr.generic)
3165 c->symtree->n.sym = sym;
3166 else
3167 c->symtree = st;
3168 csym = c->symtree->n.sym;
67cec813
PT
3169 }
3170 }
3171
8bae6273
JW
3172 /* If this ia a deferred TBP with an abstract interface
3173 (which may of course be referenced), c->expr1 will be set. */
3174 if (csym && csym->attr.abstract && !c->expr1)
3175 {
3176 gfc_error ("ABSTRACT INTERFACE '%s' must not be referenced at %L",
3177 csym->name, &c->loc);
3178 return FAILURE;
3179 }
3180
77f131ca
FXC
3181 /* Subroutines without the RECURSIVE attribution are not allowed to
3182 * call themselves. */
1933ba0f 3183 if (csym && is_illegal_recursion (csym, gfc_current_ns))
77f131ca 3184 {
1933ba0f
DK
3185 if (csym->attr.entry && csym->ns->entries)
3186 gfc_error ("ENTRY '%s' at %L cannot be called recursively, as"
3187 " subroutine '%s' is not RECURSIVE",
edf1eac2 3188 csym->name, &c->loc, csym->ns->entries->sym->name);
1933ba0f
DK
3189 else
3190 gfc_error ("SUBROUTINE '%s' at %L cannot be called recursively, as it"
3191 " is not RECURSIVE", csym->name, &c->loc);
3192
3193 t = FAILURE;
77f131ca
FXC
3194 }
3195
48474141
PT
3196 /* Switch off assumed size checking and do this again for certain kinds
3197 of procedure, once the procedure itself is resolved. */
3198 need_full_assumed_size++;
3199
0b4e2af7
PT
3200 if (csym)
3201 ptype = csym->attr.proc;
7fcafa71 3202
0b4e2af7
PT
3203 no_formal_args = csym && is_external_proc (csym) && csym->formal == NULL;
3204 if (resolve_actual_arglist (c->ext.actual, ptype,
3205 no_formal_args) == FAILURE)
6de9cd9a
DN
3206 return FAILURE;
3207
66e4ab31 3208 /* Resume assumed_size checking. */
48474141
PT
3209 need_full_assumed_size--;
3210
71a7778c
PT
3211 /* If external, check for usage. */
3212 if (csym && is_external_proc (csym))
3213 resolve_global_procedure (csym, &c->loc, &c->ext.actual, 1);
3214
1524f80b
RS
3215 t = SUCCESS;
3216 if (c->resolved_sym == NULL)
12f681a0
DK
3217 {
3218 c->resolved_isym = NULL;
3219 switch (procedure_kind (csym))
3220 {
3221 case PTYPE_GENERIC:
3222 t = resolve_generic_s (c);
3223 break;
6de9cd9a 3224
12f681a0
DK
3225 case PTYPE_SPECIFIC:
3226 t = resolve_specific_s (c);
3227 break;
6de9cd9a 3228
12f681a0
DK
3229 case PTYPE_UNKNOWN:
3230 t = resolve_unknown_s (c);
3231 break;
6de9cd9a 3232
12f681a0
DK
3233 default:
3234 gfc_internal_error ("resolve_subroutine(): bad function type");
3235 }
3236 }
6de9cd9a 3237
b8ea6dbc
PT
3238 /* Some checks of elemental subroutine actual arguments. */
3239 if (resolve_elemental_actual (NULL, c) == FAILURE)
3240 return FAILURE;
48474141 3241
23d1b451 3242 if (t == SUCCESS && !(c->resolved_sym && c->resolved_sym->attr.elemental))
1524f80b 3243 find_noncopying_intrinsics (c->resolved_sym, c->ext.actual);
6de9cd9a
DN
3244 return t;
3245}
3246
edf1eac2 3247
2c5ed587
SK
3248/* Compare the shapes of two arrays that have non-NULL shapes. If both
3249 op1->shape and op2->shape are non-NULL return SUCCESS if their shapes
3250 match. If both op1->shape and op2->shape are non-NULL return FAILURE
3251 if their shapes do not match. If either op1->shape or op2->shape is
3252 NULL, return SUCCESS. */
3253
17b1d2a0 3254static gfc_try
edf1eac2 3255compare_shapes (gfc_expr *op1, gfc_expr *op2)
2c5ed587 3256{
17b1d2a0 3257 gfc_try t;
2c5ed587
SK
3258 int i;
3259
3260 t = SUCCESS;
05c1e3a7 3261
2c5ed587
SK
3262 if (op1->shape != NULL && op2->shape != NULL)
3263 {
3264 for (i = 0; i < op1->rank; i++)
3265 {
3266 if (mpz_cmp (op1->shape[i], op2->shape[i]) != 0)
3267 {
3268 gfc_error ("Shapes for operands at %L and %L are not conformable",
3269 &op1->where, &op2->where);
3270 t = FAILURE;
3271 break;
3272 }
3273 }
3274 }
3275
3276 return t;
3277}
6de9cd9a 3278
edf1eac2 3279
6de9cd9a
DN
3280/* Resolve an operator expression node. This can involve replacing the
3281 operation with a user defined function call. */
3282
17b1d2a0 3283static gfc_try
edf1eac2 3284resolve_operator (gfc_expr *e)
6de9cd9a
DN
3285{
3286 gfc_expr *op1, *op2;
3287 char msg[200];
27189292 3288 bool dual_locus_error;
17b1d2a0 3289 gfc_try t;
6de9cd9a
DN
3290
3291 /* Resolve all subnodes-- give them types. */
3292
a1ee985f 3293 switch (e->value.op.op)
6de9cd9a
DN
3294 {
3295 default:
58b03ab2 3296 if (gfc_resolve_expr (e->value.op.op2) == FAILURE)
6de9cd9a
DN
3297 return FAILURE;
3298
3299 /* Fall through... */
3300
3301 case INTRINSIC_NOT:
3302 case INTRINSIC_UPLUS:
3303 case INTRINSIC_UMINUS:
2414e1d6 3304 case INTRINSIC_PARENTHESES:
58b03ab2 3305 if (gfc_resolve_expr (e->value.op.op1) == FAILURE)
6de9cd9a
DN
3306 return FAILURE;
3307 break;
3308 }
3309
3310 /* Typecheck the new node. */
3311
58b03ab2
TS
3312 op1 = e->value.op.op1;
3313 op2 = e->value.op.op2;
27189292 3314 dual_locus_error = false;
6de9cd9a 3315
bb9e683e
TB
3316 if ((op1 && op1->expr_type == EXPR_NULL)
3317 || (op2 && op2->expr_type == EXPR_NULL))
3318 {
3319 sprintf (msg, _("Invalid context for NULL() pointer at %%L"));
3320 goto bad_op;
3321 }
3322
a1ee985f 3323 switch (e->value.op.op)
6de9cd9a
DN
3324 {
3325 case INTRINSIC_UPLUS:
3326 case INTRINSIC_UMINUS:
3327 if (op1->ts.type == BT_INTEGER
3328 || op1->ts.type == BT_REAL
3329 || op1->ts.type == BT_COMPLEX)
3330 {
3331 e->ts = op1->ts;
3332 break;
3333 }
3334
31043f6c 3335 sprintf (msg, _("Operand of unary numeric operator '%s' at %%L is %s"),
a1ee985f 3336 gfc_op2string (e->value.op.op), gfc_typename (&e->ts));
6de9cd9a
DN
3337 goto bad_op;
3338
3339 case INTRINSIC_PLUS:
3340 case INTRINSIC_MINUS:
3341 case INTRINSIC_TIMES:
3342 case INTRINSIC_DIVIDE:
3343 case INTRINSIC_POWER:
3344 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3345 {
dcea1b2f 3346 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3347 break;
3348 }
3349
3350 sprintf (msg,
31043f6c 3351 _("Operands of binary numeric operator '%s' at %%L are %s/%s"),
a1ee985f 3352 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3353 gfc_typename (&op2->ts));
3354 goto bad_op;
3355
3356 case INTRINSIC_CONCAT:
d393bbd7
FXC
3357 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3358 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3359 {
3360 e->ts.type = BT_CHARACTER;
3361 e->ts.kind = op1->ts.kind;
3362 break;
3363 }
3364
3365 sprintf (msg,
31043f6c 3366 _("Operands of string concatenation operator at %%L are %s/%s"),
6de9cd9a
DN
3367 gfc_typename (&op1->ts), gfc_typename (&op2->ts));
3368 goto bad_op;
3369
3370 case INTRINSIC_AND:
3371 case INTRINSIC_OR:
3372 case INTRINSIC_EQV:
3373 case INTRINSIC_NEQV:
3374 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
3375 {
3376 e->ts.type = BT_LOGICAL;
3377 e->ts.kind = gfc_kind_max (op1, op2);
edf1eac2
SK
3378 if (op1->ts.kind < e->ts.kind)
3379 gfc_convert_type (op1, &e->ts, 2);
3380 else if (op2->ts.kind < e->ts.kind)
3381 gfc_convert_type (op2, &e->ts, 2);
6de9cd9a
DN
3382 break;
3383 }
3384
31043f6c 3385 sprintf (msg, _("Operands of logical operator '%s' at %%L are %s/%s"),
a1ee985f 3386 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6de9cd9a
DN
3387 gfc_typename (&op2->ts));
3388
3389 goto bad_op;
3390
3391 case INTRINSIC_NOT:
3392 if (op1->ts.type == BT_LOGICAL)
3393 {
3394 e->ts.type = BT_LOGICAL;
3395 e->ts.kind = op1->ts.kind;
3396 break;
3397 }
3398
3bed9dd0 3399 sprintf (msg, _("Operand of .not. operator at %%L is %s"),
6de9cd9a
DN
3400 gfc_typename (&op1->ts));
3401 goto bad_op;
3402
3403 case INTRINSIC_GT:
3bed9dd0 3404 case INTRINSIC_GT_OS:
6de9cd9a 3405 case INTRINSIC_GE:
3bed9dd0 3406 case INTRINSIC_GE_OS:
6de9cd9a 3407 case INTRINSIC_LT:
3bed9dd0 3408 case INTRINSIC_LT_OS:
6de9cd9a 3409 case INTRINSIC_LE:
3bed9dd0 3410 case INTRINSIC_LE_OS:
6de9cd9a
DN
3411 if (op1->ts.type == BT_COMPLEX || op2->ts.type == BT_COMPLEX)
3412 {
31043f6c 3413 strcpy (msg, _("COMPLEX quantities cannot be compared at %L"));
6de9cd9a
DN
3414 goto bad_op;
3415 }
3416
3417 /* Fall through... */
3418
3419 case INTRINSIC_EQ:
3bed9dd0 3420 case INTRINSIC_EQ_OS:
6de9cd9a 3421 case INTRINSIC_NE:
3bed9dd0 3422 case INTRINSIC_NE_OS:
d393bbd7
FXC
3423 if (op1->ts.type == BT_CHARACTER && op2->ts.type == BT_CHARACTER
3424 && op1->ts.kind == op2->ts.kind)
6de9cd9a
DN
3425 {
3426 e->ts.type = BT_LOGICAL;
9d64df18 3427 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3428 break;
3429 }
3430
3431 if (gfc_numeric_ts (&op1->ts) && gfc_numeric_ts (&op2->ts))
3432 {
dcea1b2f 3433 gfc_type_convert_binary (e, 1);
6de9cd9a
DN
3434
3435 e->ts.type = BT_LOGICAL;
9d64df18 3436 e->ts.kind = gfc_default_logical_kind;
6de9cd9a
DN
3437 break;
3438 }
3439
6a28f513 3440 if (op1->ts.type == BT_LOGICAL && op2->ts.type == BT_LOGICAL)
31043f6c 3441 sprintf (msg,
edf1eac2 3442 _("Logicals at %%L must be compared with %s instead of %s"),
a1ee985f
KG
3443 (e->value.op.op == INTRINSIC_EQ
3444 || e->value.op.op == INTRINSIC_EQ_OS)
3445 ? ".eqv." : ".neqv.", gfc_op2string (e->value.op.op));
6a28f513 3446 else
31043f6c 3447 sprintf (msg,
edf1eac2 3448 _("Operands of comparison operator '%s' at %%L are %s/%s"),
a1ee985f 3449 gfc_op2string (e->value.op.op), gfc_typename (&op1->ts),
6a28f513 3450 gfc_typename (&op2->ts));
6de9cd9a
DN
3451
3452 goto bad_op;
3453
3454 case INTRINSIC_USER:
a1ee985f 3455 if (e->value.op.uop->op == NULL)
622af87f
DF
3456 sprintf (msg, _("Unknown operator '%s' at %%L"), e->value.op.uop->name);
3457 else if (op2 == NULL)
31043f6c 3458 sprintf (msg, _("Operand of user operator '%s' at %%L is %s"),
58b03ab2 3459 e->value.op.uop->name, gfc_typename (&op1->ts));
6de9cd9a 3460 else
31043f6c 3461 sprintf (msg, _("Operands of user operator '%s' at %%L are %s/%s"),
58b03ab2 3462 e->value.op.uop->name, gfc_typename (&op1->ts),
6de9cd9a
DN
3463 gfc_typename (&op2->ts));
3464
3465 goto bad_op;
3466
2414e1d6 3467 case INTRINSIC_PARENTHESES:
dcdc83a1
TS
3468 e->ts = op1->ts;
3469 if (e->ts.type == BT_CHARACTER)
bc21d315 3470 e->ts.u.cl = op1->ts.u.cl;
2414e1d6
TS
3471 break;
3472
6de9cd9a
DN
3473 default:
3474 gfc_internal_error ("resolve_operator(): Bad intrinsic");
3475 }
3476
3477 /* Deal with arrayness of an operand through an operator. */
3478
3479 t = SUCCESS;
3480
a1ee985f 3481 switch (e->value.op.op)
6de9cd9a
DN
3482 {
3483 case INTRINSIC_PLUS:
3484 case INTRINSIC_MINUS:
3485 case INTRINSIC_TIMES:
3486 case INTRINSIC_DIVIDE:
3487 case INTRINSIC_POWER:
3488 case INTRINSIC_CONCAT:
3489 case INTRINSIC_AND:
3490 case INTRINSIC_OR:
3491 case INTRINSIC_EQV:
3492 case INTRINSIC_NEQV:
3493 case INTRINSIC_EQ:
3bed9dd0 3494 case INTRINSIC_EQ_OS:
6de9cd9a 3495 case INTRINSIC_NE:
3bed9dd0 3496 case INTRINSIC_NE_OS:
6de9cd9a 3497 case INTRINSIC_GT:
3bed9dd0 3498 case INTRINSIC_GT_OS:
6de9cd9a 3499 case INTRINSIC_GE:
3bed9dd0 3500 case INTRINSIC_GE_OS:
6de9cd9a 3501 case INTRINSIC_LT:
3bed9dd0 3502 case INTRINSIC_LT_OS:
6de9cd9a 3503 case INTRINSIC_LE:
3bed9dd0 3504 case INTRINSIC_LE_OS:
6de9cd9a
DN
3505
3506 if (op1->rank == 0 && op2->rank == 0)
3507 e->rank = 0;
3508
3509 if (op1->rank == 0 && op2->rank != 0)
3510 {
3511 e->rank = op2->rank;
3512
3513 if (e->shape == NULL)
3514 e->shape = gfc_copy_shape (op2->shape, op2->rank);
3515 }
3516
3517 if (op1->rank != 0 && op2->rank == 0)
3518 {
3519 e->rank = op1->rank;
3520
3521 if (e->shape == NULL)
3522 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3523 }
3524
3525 if (op1->rank != 0 && op2->rank != 0)
3526 {
3527 if (op1->rank == op2->rank)
3528 {
3529 e->rank = op1->rank;
6de9cd9a 3530 if (e->shape == NULL)
2c5ed587
SK
3531 {
3532 t = compare_shapes(op1, op2);
3533 if (t == FAILURE)
3534 e->shape = NULL;
3535 else
6de9cd9a 3536 e->shape = gfc_copy_shape (op1->shape, op1->rank);
2c5ed587 3537 }
6de9cd9a
DN
3538 }
3539 else
3540 {
edf1eac2 3541 /* Allow higher level expressions to work. */
6de9cd9a 3542 e->rank = 0;
27189292
FXC
3543
3544 /* Try user-defined operators, and otherwise throw an error. */
3545 dual_locus_error = true;
3546 sprintf (msg,
3547 _("Inconsistent ranks for operator at %%L and %%L"));
3548 goto bad_op;
6de9cd9a
DN
3549 }
3550 }
3551
3552 break;
3553
08113c73 3554 case INTRINSIC_PARENTHESES:
6de9cd9a
DN
3555 case INTRINSIC_NOT:
3556 case INTRINSIC_UPLUS:
3557 case INTRINSIC_UMINUS:
08113c73 3558 /* Simply copy arrayness attribute */
6de9cd9a
DN
3559 e->rank = op1->rank;
3560
3561 if (e->shape == NULL)
3562 e->shape = gfc_copy_shape (op1->shape, op1->rank);
3563
6de9cd9a
DN
3564 break;
3565
3566 default:
3567 break;
3568 }
3569
3570 /* Attempt to simplify the expression. */
3571 if (t == SUCCESS)
dd5ecf41
PT
3572 {
3573 t = gfc_simplify_expr (e, 0);
3574 /* Some calls do not succeed in simplification and return FAILURE
df2fba9e 3575 even though there is no error; e.g. variable references to
dd5ecf41
PT
3576 PARAMETER arrays. */
3577 if (!gfc_is_constant_expr (e))
3578 t = SUCCESS;
3579 }
6de9cd9a
DN
3580 return t;
3581
3582bad_op:
2c5ed587 3583
4a44a72d
DK
3584 {
3585 bool real_error;
3586 if (gfc_extend_expr (e, &real_error) == SUCCESS)
3587 return SUCCESS;
3588
3589 if (real_error)
3590 return FAILURE;
3591 }
6de9cd9a 3592
27189292
FXC
3593 if (dual_locus_error)
3594 gfc_error (msg, &op1->where, &op2->where);
3595 else
3596 gfc_error (msg, &e->where);
2c5ed587 3597
6de9cd9a
DN
3598 return FAILURE;
3599}
3600
3601
3602/************** Array resolution subroutines **************/
3603
6de9cd9a
DN
3604typedef enum
3605{ CMP_LT, CMP_EQ, CMP_GT, CMP_UNKNOWN }
3606comparison;
3607
3608/* Compare two integer expressions. */
3609
3610static comparison
edf1eac2 3611compare_bound (gfc_expr *a, gfc_expr *b)
6de9cd9a
DN
3612{
3613 int i;
3614
3615 if (a == NULL || a->expr_type != EXPR_CONSTANT
3616 || b == NULL || b->expr_type != EXPR_CONSTANT)
3617 return CMP_UNKNOWN;
3618
df80a455
TK
3619 /* If either of the types isn't INTEGER, we must have
3620 raised an error earlier. */
3621
6de9cd9a 3622 if (a->ts.type != BT_INTEGER || b->ts.type != BT_INTEGER)
df80a455 3623 return CMP_UNKNOWN;
6de9cd9a
DN
3624
3625 i = mpz_cmp (a->value.integer, b->value.integer);
3626
3627 if (i < 0)
3628 return CMP_LT;
3629 if (i > 0)
3630 return CMP_GT;
3631 return CMP_EQ;
3632}
3633
3634
3635/* Compare an integer expression with an integer. */
3636
3637static comparison
edf1eac2 3638compare_bound_int (gfc_expr *a, int b)
6de9cd9a
DN
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_si (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
0094f362
FXC
3658/* Compare an integer expression with a mpz_t. */
3659
3660static comparison
edf1eac2 3661compare_bound_mpz_t (gfc_expr *a, mpz_t b)
0094f362
FXC
3662{
3663 int i;
3664
3665 if (a == NULL || a->expr_type != EXPR_CONSTANT)
3666 return CMP_UNKNOWN;
3667
3668 if (a->ts.type != BT_INTEGER)
3669 gfc_internal_error ("compare_bound_int(): Bad expression");
3670
3671 i = mpz_cmp (a->value.integer, b);
3672
3673 if (i < 0)
3674 return CMP_LT;
3675 if (i > 0)
3676 return CMP_GT;
3677 return CMP_EQ;
3678}
3679
3680
3681/* Compute the last value of a sequence given by a triplet.
3682 Return 0 if it wasn't able to compute the last value, or if the
3683 sequence if empty, and 1 otherwise. */
3684
3685static int
edf1eac2
SK
3686compute_last_value_for_triplet (gfc_expr *start, gfc_expr *end,
3687 gfc_expr *stride, mpz_t last)
0094f362
FXC
3688{
3689 mpz_t rem;
3690
3691 if (start == NULL || start->expr_type != EXPR_CONSTANT
3692 || end == NULL || end->expr_type != EXPR_CONSTANT
3693 || (stride != NULL && stride->expr_type != EXPR_CONSTANT))
3694 return 0;
3695
3696 if (start->ts.type != BT_INTEGER || end->ts.type != BT_INTEGER
3697 || (stride != NULL && stride->ts.type != BT_INTEGER))
3698 return 0;
3699
3700 if (stride == NULL || compare_bound_int(stride, 1) == CMP_EQ)
3701 {
3702 if (compare_bound (start, end) == CMP_GT)
3703 return 0;
3704 mpz_set (last, end->value.integer);
3705 return 1;
3706 }
05c1e3a7 3707
0094f362
FXC
3708 if (compare_bound_int (stride, 0) == CMP_GT)
3709 {
3710 /* Stride is positive */
3711 if (mpz_cmp (start->value.integer, end->value.integer) > 0)
3712 return 0;
3713 }
3714 else
3715 {
3716 /* Stride is negative */
3717 if (mpz_cmp (start->value.integer, end->value.integer) < 0)
3718 return 0;
3719 }
3720
3721 mpz_init (rem);
3722 mpz_sub (rem, end->value.integer, start->value.integer);
3723 mpz_tdiv_r (rem, rem, stride->value.integer);
3724 mpz_sub (last, end->value.integer, rem);
3725 mpz_clear (rem);
3726
3727 return 1;
3728}
3729
3730
6de9cd9a
DN
3731/* Compare a single dimension of an array reference to the array
3732 specification. */
3733
17b1d2a0 3734static gfc_try
edf1eac2 3735check_dimension (int i, gfc_array_ref *ar, gfc_array_spec *as)
6de9cd9a 3736{
0094f362 3737 mpz_t last_value;
6de9cd9a
DN
3738
3739/* Given start, end and stride values, calculate the minimum and
f7b529fa 3740 maximum referenced indexes. */
6de9cd9a 3741
1954a27b 3742 switch (ar->dimen_type[i])
6de9cd9a 3743 {
1954a27b 3744 case DIMEN_VECTOR:
6de9cd9a
DN
3745 break;
3746
1954a27b 3747 case DIMEN_ELEMENT:
6de9cd9a 3748 if (compare_bound (ar->start[i], as->lower[i]) == CMP_LT)
1954a27b
TB
3749 {
3750 gfc_warning ("Array reference at %L is out of bounds "
3751 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3752 mpz_get_si (ar->start[i]->value.integer),
3753 mpz_get_si (as->lower[i]->value.integer), i+1);
3754 return SUCCESS;
3755 }
6de9cd9a 3756 if (compare_bound (ar->start[i], as->upper[i]) == CMP_GT)
1954a27b
TB
3757 {
3758 gfc_warning ("Array reference at %L is out of bounds "
3759 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3760 mpz_get_si (ar->start[i]->value.integer),
3761 mpz_get_si (as->upper[i]->value.integer), i+1);
3762 return SUCCESS;
3763 }
6de9cd9a
DN
3764
3765 break;
3766
1954a27b 3767 case DIMEN_RANGE:
d912240d 3768 {
0094f362
FXC
3769#define AR_START (ar->start[i] ? ar->start[i] : as->lower[i])
3770#define AR_END (ar->end[i] ? ar->end[i] : as->upper[i])
3771
d912240d 3772 comparison comp_start_end = compare_bound (AR_START, AR_END);
0094f362 3773
d912240d
FXC
3774 /* Check for zero stride, which is not allowed. */
3775 if (compare_bound_int (ar->stride[i], 0) == CMP_EQ)
3776 {
3777 gfc_error ("Illegal stride of zero at %L", &ar->c_where[i]);
3778 return FAILURE;
3779 }
3780
3781 /* if start == len || (stride > 0 && start < len)
3782 || (stride < 0 && start > len),
3783 then the array section contains at least one element. In this
3784 case, there is an out-of-bounds access if
3785 (start < lower || start > upper). */
3786 if (compare_bound (AR_START, AR_END) == CMP_EQ
3787 || ((compare_bound_int (ar->stride[i], 0) == CMP_GT
3788 || ar->stride[i] == NULL) && comp_start_end == CMP_LT)
3789 || (compare_bound_int (ar->stride[i], 0) == CMP_LT
3790 && comp_start_end == CMP_GT))
3791 {
1954a27b
TB
3792 if (compare_bound (AR_START, as->lower[i]) == CMP_LT)
3793 {
3794 gfc_warning ("Lower array reference at %L is out of bounds "
3795 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3796 mpz_get_si (AR_START->value.integer),
3797 mpz_get_si (as->lower[i]->value.integer), i+1);
3798 return SUCCESS;
3799 }
3800 if (compare_bound (AR_START, as->upper[i]) == CMP_GT)
3801 {
3802 gfc_warning ("Lower array reference at %L is out of bounds "
3803 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3804 mpz_get_si (AR_START->value.integer),
3805 mpz_get_si (as->upper[i]->value.integer), i+1);
3806 return SUCCESS;
3807 }
d912240d
FXC
3808 }
3809
3810 /* If we can compute the highest index of the array section,
3811 then it also has to be between lower and upper. */
3812 mpz_init (last_value);
3813 if (compute_last_value_for_triplet (AR_START, AR_END, ar->stride[i],
3814 last_value))
3815 {
1954a27b
TB
3816 if (compare_bound_mpz_t (as->lower[i], last_value) == CMP_GT)
3817 {
3818 gfc_warning ("Upper array reference at %L is out of bounds "
3819 "(%ld < %ld) in dimension %d", &ar->c_where[i],
3820 mpz_get_si (last_value),
3821 mpz_get_si (as->lower[i]->value.integer), i+1);
3822 mpz_clear (last_value);
3823 return SUCCESS;
3824 }
3825 if (compare_bound_mpz_t (as->upper[i], last_value) == CMP_LT)
d912240d 3826 {
1954a27b
TB
3827 gfc_warning ("Upper array reference at %L is out of bounds "
3828 "(%ld > %ld) in dimension %d", &ar->c_where[i],
3829 mpz_get_si (last_value),
3830 mpz_get_si (as->upper[i]->value.integer), i+1);
d912240d 3831 mpz_clear (last_value);
1954a27b 3832 return SUCCESS;
d912240d
FXC
3833 }
3834 }
3835 mpz_clear (last_value);
0094f362
FXC
3836
3837#undef AR_START
3838#undef AR_END
d912240d 3839 }
6de9cd9a
DN
3840 break;
3841
3842 default:
3843 gfc_internal_error ("check_dimension(): Bad array reference");
3844 }
3845
3846 return SUCCESS;
6de9cd9a
DN
3847}
3848
3849
3850/* Compare an array reference with an array specification. */
3851
17b1d2a0 3852static gfc_try
edf1eac2 3853compare_spec_to_ref (gfc_array_ref *ar)
6de9cd9a
DN
3854{
3855 gfc_array_spec *as;
3856 int i;
3857
3858 as = ar->as;
3859 i = as->rank - 1;
3860 /* TODO: Full array sections are only allowed as actual parameters. */
3861 if (as->type == AS_ASSUMED_SIZE
3862 && (/*ar->type == AR_FULL
edf1eac2
SK
3863 ||*/ (ar->type == AR_SECTION
3864 && ar->dimen_type[i] == DIMEN_RANGE && ar->end[i] == NULL)))
6de9cd9a 3865 {
edf1eac2
SK
3866 gfc_error ("Rightmost upper bound of assumed size array section "
3867 "not specified at %L", &ar->where);
6de9cd9a
DN
3868 return FAILURE;
3869 }
3870
3871 if (ar->type == AR_FULL)
3872 return SUCCESS;
3873
3874 if (as->rank != ar->dimen)
3875 {
3876 gfc_error ("Rank mismatch in array reference at %L (%d/%d)",
3877 &ar->where, ar->dimen, as->rank);
3878 return FAILURE;
3879 }
3880
3881 for (i = 0; i < as->rank; i++)
3882 if (check_dimension (i, ar, as) == FAILURE)
3883 return FAILURE;
3884
3885 return SUCCESS;
3886}
3887
3888
3889/* Resolve one part of an array index. */
3890
17b1d2a0 3891gfc_try
edf1eac2 3892gfc_resolve_index (gfc_expr *index, int check_scalar)
6de9cd9a
DN
3893{
3894 gfc_typespec ts;
3895
3896 if (index == NULL)
3897 return SUCCESS;
3898
3899 if (gfc_resolve_expr (index) == FAILURE)
3900 return FAILURE;
3901
ee943062 3902 if (check_scalar && index->rank != 0)
6de9cd9a 3903 {
ee943062 3904 gfc_error ("Array index at %L must be scalar", &index->where);
6de9cd9a
DN
3905 return FAILURE;
3906 }
3907
ee943062 3908 if (index->ts.type != BT_INTEGER && index->ts.type != BT_REAL)
6de9cd9a 3909 {
acb388a0
JD
3910 gfc_error ("Array index at %L must be of INTEGER type, found %s",
3911 &index->where, gfc_basic_typename (index->ts.type));
6de9cd9a
DN
3912 return FAILURE;
3913 }
3914
ee943062 3915 if (index->ts.type == BT_REAL)
7fdf6c69 3916 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: REAL array index at %L",
ee943062
TS
3917 &index->where) == FAILURE)
3918 return FAILURE;
3919
3920 if (index->ts.kind != gfc_index_integer_kind
3921 || index->ts.type != BT_INTEGER)
6de9cd9a 3922 {
810306f2 3923 gfc_clear_ts (&ts);
6de9cd9a
DN
3924 ts.type = BT_INTEGER;
3925 ts.kind = gfc_index_integer_kind;
3926
3927 gfc_convert_type_warn (index, &ts, 2, 0);
3928 }
3929
3930 return SUCCESS;
3931}
3932
bf302220
TK
3933/* Resolve a dim argument to an intrinsic function. */
3934
17b1d2a0 3935gfc_try
bf302220
TK
3936gfc_resolve_dim_arg (gfc_expr *dim)
3937{
3938 if (dim == NULL)
3939 return SUCCESS;
3940
3941 if (gfc_resolve_expr (dim) == FAILURE)
3942 return FAILURE;
3943
3944 if (dim->rank != 0)
3945 {
3946 gfc_error ("Argument dim at %L must be scalar", &dim->where);
3947 return FAILURE;
05c1e3a7 3948
bf302220 3949 }
33717d59 3950
bf302220
TK
3951 if (dim->ts.type != BT_INTEGER)
3952 {
3953 gfc_error ("Argument dim at %L must be of INTEGER type", &dim->where);
3954 return FAILURE;
3955 }
33717d59 3956
bf302220
TK
3957 if (dim->ts.kind != gfc_index_integer_kind)
3958 {
3959 gfc_typespec ts;
3960
3961 ts.type = BT_INTEGER;
3962 ts.kind = gfc_index_integer_kind;
3963
3964 gfc_convert_type_warn (dim, &ts, 2, 0);
3965 }
3966
3967 return SUCCESS;
3968}
6de9cd9a
DN
3969
3970/* Given an expression that contains array references, update those array
3971 references to point to the right array specifications. While this is
3972 filled in during matching, this information is difficult to save and load
3973 in a module, so we take care of it here.
3974
3975 The idea here is that the original array reference comes from the
3976 base symbol. We traverse the list of reference structures, setting
3977 the stored reference to references. Component references can
3978 provide an additional array specification. */
3979
3980static void
edf1eac2 3981find_array_spec (gfc_expr *e)
6de9cd9a
DN
3982{
3983 gfc_array_spec *as;
3984 gfc_component *c;
014057c5 3985 gfc_symbol *derived;
6de9cd9a
DN
3986 gfc_ref *ref;
3987
cf2b3c22
TB
3988 if (e->symtree->n.sym->ts.type == BT_CLASS)
3989 as = e->symtree->n.sym->ts.u.derived->components->as;
3990 else
3991 as = e->symtree->n.sym->as;
014057c5 3992 derived = NULL;
6de9cd9a
DN
3993
3994 for (ref = e->ref; ref; ref = ref->next)
3995 switch (ref->type)
3996 {
3997 case REF_ARRAY:
3998 if (as == NULL)
3999 gfc_internal_error ("find_array_spec(): Missing spec");
4000
4001 ref->u.ar.as = as;
4002 as = NULL;
4003 break;
4004
4005 case REF_COMPONENT:
014057c5 4006 if (derived == NULL)
bc21d315 4007 derived = e->symtree->n.sym->ts.u.derived;
014057c5
PT
4008
4009 c = derived->components;
4010
4011 for (; c; c = c->next)
6de9cd9a 4012 if (c == ref->u.c.component)
014057c5
PT
4013 {
4014 /* Track the sequence of component references. */
4015 if (c->ts.type == BT_DERIVED)
bc21d315 4016 derived = c->ts.u.derived;
014057c5
PT
4017 break;
4018 }
6de9cd9a
DN
4019
4020 if (c == NULL)
4021 gfc_internal_error ("find_array_spec(): Component not found");
4022
d4b7d0f0 4023 if (c->attr.dimension)
6de9cd9a
DN
4024 {
4025 if (as != NULL)
4026 gfc_internal_error ("find_array_spec(): unused as(1)");
4027 as = c->as;
4028 }
4029
6de9cd9a
DN
4030 break;
4031
4032 case REF_SUBSTRING:
4033 break;
4034 }
4035
4036 if (as != NULL)
4037 gfc_internal_error ("find_array_spec(): unused as(2)");
4038}
4039
4040
4041/* Resolve an array reference. */
4042
17b1d2a0 4043static gfc_try
edf1eac2 4044resolve_array_ref (gfc_array_ref *ar)
6de9cd9a
DN
4045{
4046 int i, check_scalar;
b6398823 4047 gfc_expr *e;
6de9cd9a
DN
4048
4049 for (i = 0; i < ar->dimen; i++)
4050 {
4051 check_scalar = ar->dimen_type[i] == DIMEN_RANGE;
4052
4053 if (gfc_resolve_index (ar->start[i], check_scalar) == FAILURE)
4054 return FAILURE;
4055 if (gfc_resolve_index (ar->end[i], check_scalar) == FAILURE)
4056 return FAILURE;
4057 if (gfc_resolve_index (ar->stride[i], check_scalar) == FAILURE)
4058 return FAILURE;
4059
b6398823
PT
4060 e = ar->start[i];
4061
6de9cd9a 4062 if (ar->dimen_type[i] == DIMEN_UNKNOWN)
b6398823 4063 switch (e->rank)
6de9cd9a
DN
4064 {
4065 case 0:
4066 ar->dimen_type[i] = DIMEN_ELEMENT;
4067 break;
4068
4069 case 1:
4070 ar->dimen_type[i] = DIMEN_VECTOR;
b6398823 4071 if (e->expr_type == EXPR_VARIABLE
edf1eac2 4072 && e->symtree->n.sym->ts.type == BT_DERIVED)
b6398823 4073 ar->start[i] = gfc_get_parentheses (e);
6de9cd9a
DN
4074 break;
4075
4076 default:
4077 gfc_error ("Array index at %L is an array of rank %d",
b6398823 4078 &ar->c_where[i], e->rank);
6de9cd9a
DN
4079 return FAILURE;
4080 }
4081 }
4082
4083 /* If the reference type is unknown, figure out what kind it is. */
4084
4085 if (ar->type == AR_UNKNOWN)
4086 {
4087 ar->type = AR_ELEMENT;
4088 for (i = 0; i < ar->dimen; i++)
4089 if (ar->dimen_type[i] == DIMEN_RANGE
4090 || ar->dimen_type[i] == DIMEN_VECTOR)
4091 {
4092 ar->type = AR_SECTION;
4093 break;
4094 }
4095 }
4096
83d890b9 4097 if (!ar->as->cray_pointee && compare_spec_to_ref (ar) == FAILURE)
6de9cd9a
DN
4098 return FAILURE;
4099
4100 return SUCCESS;
4101}
4102
4103
17b1d2a0 4104static gfc_try
edf1eac2 4105resolve_substring (gfc_ref *ref)
6de9cd9a 4106{
b0c06816
FXC
4107 int k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
4108
6de9cd9a
DN
4109 if (ref->u.ss.start != NULL)
4110 {
4111 if (gfc_resolve_expr (ref->u.ss.start) == FAILURE)
4112 return FAILURE;
4113
4114 if (ref->u.ss.start->ts.type != BT_INTEGER)
4115 {
4116 gfc_error ("Substring start index at %L must be of type INTEGER",
4117 &ref->u.ss.start->where);
4118 return FAILURE;
4119 }
4120
4121 if (ref->u.ss.start->rank != 0)
4122 {
4123 gfc_error ("Substring start index at %L must be scalar",
4124 &ref->u.ss.start->where);
4125 return FAILURE;
4126 }
4127
97bca513
FXC
4128 if (compare_bound_int (ref->u.ss.start, 1) == CMP_LT
4129 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4130 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a
DN
4131 {
4132 gfc_error ("Substring start index at %L is less than one",
4133 &ref->u.ss.start->where);
4134 return FAILURE;
4135 }
4136 }
4137
4138 if (ref->u.ss.end != NULL)
4139 {
4140 if (gfc_resolve_expr (ref->u.ss.end) == FAILURE)
4141 return FAILURE;
4142
4143 if (ref->u.ss.end->ts.type != BT_INTEGER)
4144 {
4145 gfc_error ("Substring end index at %L must be of type INTEGER",
4146 &ref->u.ss.end->where);
4147 return FAILURE;
4148 }
4149
4150 if (ref->u.ss.end->rank != 0)
4151 {
4152 gfc_error ("Substring end index at %L must be scalar",
4153 &ref->u.ss.end->where);
4154 return FAILURE;
4155 }
4156
4157 if (ref->u.ss.length != NULL
97bca513
FXC
4158 && compare_bound (ref->u.ss.end, ref->u.ss.length->length) == CMP_GT
4159 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4160 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
6de9cd9a 4161 {
97bca513 4162 gfc_error ("Substring end index at %L exceeds the string length",
6de9cd9a
DN
4163 &ref->u.ss.start->where);
4164 return FAILURE;
4165 }
b0c06816
FXC
4166
4167 if (compare_bound_mpz_t (ref->u.ss.end,
4168 gfc_integer_kinds[k].huge) == CMP_GT
4169 && (compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_EQ
4170 || compare_bound (ref->u.ss.end, ref->u.ss.start) == CMP_GT))
4171 {
4172 gfc_error ("Substring end index at %L is too large",
4173 &ref->u.ss.end->where);
4174 return FAILURE;
4175 }
6de9cd9a
DN
4176 }
4177
4178 return SUCCESS;
4179}
4180
4181
07368af0
PT
4182/* This function supplies missing substring charlens. */
4183
4184void
4185gfc_resolve_substring_charlen (gfc_expr *e)
4186{
4187 gfc_ref *char_ref;
4188 gfc_expr *start, *end;
4189
4190 for (char_ref = e->ref; char_ref; char_ref = char_ref->next)
4191 if (char_ref->type == REF_SUBSTRING)
4192 break;
4193
4194 if (!char_ref)
4195 return;
4196
4197 gcc_assert (char_ref->next == NULL);
4198
bc21d315 4199 if (e->ts.u.cl)
07368af0 4200 {
bc21d315
JW
4201 if (e->ts.u.cl->length)
4202 gfc_free_expr (e->ts.u.cl->length);
07368af0
PT
4203 else if (e->expr_type == EXPR_VARIABLE
4204 && e->symtree->n.sym->attr.dummy)
4205 return;
4206 }
4207
4208 e->ts.type = BT_CHARACTER;
4209 e->ts.kind = gfc_default_character_kind;
4210
bc21d315 4211 if (!e->ts.u.cl)
b76e28c6 4212 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4213
4214 if (char_ref->u.ss.start)
4215 start = gfc_copy_expr (char_ref->u.ss.start);
4216 else
4217 start = gfc_int_expr (1);
4218
4219 if (char_ref->u.ss.end)
4220 end = gfc_copy_expr (char_ref->u.ss.end);
4221 else if (e->expr_type == EXPR_VARIABLE)
bc21d315 4222 end = gfc_copy_expr (e->symtree->n.sym->ts.u.cl->length);
07368af0
PT
4223 else
4224 end = NULL;
4225
4226 if (!start || !end)
4227 return;
4228
4229 /* Length = (end - start +1). */
bc21d315
JW
4230 e->ts.u.cl->length = gfc_subtract (end, start);
4231 e->ts.u.cl->length = gfc_add (e->ts.u.cl->length, gfc_int_expr (1));
07368af0 4232
bc21d315
JW
4233 e->ts.u.cl->length->ts.type = BT_INTEGER;
4234 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
07368af0
PT
4235
4236 /* Make sure that the length is simplified. */
bc21d315
JW
4237 gfc_simplify_expr (e->ts.u.cl->length, 1);
4238 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4239}
4240
4241
6de9cd9a
DN
4242/* Resolve subtype references. */
4243
17b1d2a0 4244static gfc_try
edf1eac2 4245resolve_ref (gfc_expr *expr)
6de9cd9a
DN
4246{
4247 int current_part_dimension, n_components, seen_part_dimension;
4248 gfc_ref *ref;
4249
4250 for (ref = expr->ref; ref; ref = ref->next)
4251 if (ref->type == REF_ARRAY && ref->u.ar.as == NULL)
4252 {
4253 find_array_spec (expr);
4254 break;
4255 }
4256
4257 for (ref = expr->ref; ref; ref = ref->next)
4258 switch (ref->type)
4259 {
4260 case REF_ARRAY:
4261 if (resolve_array_ref (&ref->u.ar) == FAILURE)
4262 return FAILURE;
4263 break;
4264
4265 case REF_COMPONENT:
4266 break;
4267
4268 case REF_SUBSTRING:
4269 resolve_substring (ref);
4270 break;
4271 }
4272
4273 /* Check constraints on part references. */
4274
4275 current_part_dimension = 0;
4276 seen_part_dimension = 0;
4277 n_components = 0;
4278
4279 for (ref = expr->ref; ref; ref = ref->next)
4280 {
4281 switch (ref->type)
4282 {
4283 case REF_ARRAY:
4284 switch (ref->u.ar.type)
4285 {
4286 case AR_FULL:
4287 case AR_SECTION:
4288 current_part_dimension = 1;
4289 break;
4290
4291 case AR_ELEMENT:
4292 current_part_dimension = 0;
4293 break;
4294
4295 case AR_UNKNOWN:
4296 gfc_internal_error ("resolve_ref(): Bad array reference");
4297 }
4298
4299 break;
4300
4301 case REF_COMPONENT:
51f824b6 4302 if (current_part_dimension || seen_part_dimension)
6de9cd9a 4303 {
ef2bbc8c
JW
4304 /* F03:C614. */
4305 if (ref->u.c.component->attr.pointer
4306 || ref->u.c.component->attr.proc_pointer)
edf1eac2
SK
4307 {
4308 gfc_error ("Component to the right of a part reference "
4309 "with nonzero rank must not have the POINTER "
4310 "attribute at %L", &expr->where);
51f824b6
EE
4311 return FAILURE;
4312 }
d4b7d0f0 4313 else if (ref->u.c.component->attr.allocatable)
edf1eac2
SK
4314 {
4315 gfc_error ("Component to the right of a part reference "
4316 "with nonzero rank must not have the ALLOCATABLE "
4317 "attribute at %L", &expr->where);
51f824b6
EE
4318 return FAILURE;
4319 }
6de9cd9a
DN
4320 }
4321
4322 n_components++;
4323 break;
4324
4325 case REF_SUBSTRING:
4326 break;
4327 }
4328
4329 if (((ref->type == REF_COMPONENT && n_components > 1)
4330 || ref->next == NULL)
edf1eac2 4331 && current_part_dimension
6de9cd9a
DN
4332 && seen_part_dimension)
4333 {
6de9cd9a
DN
4334 gfc_error ("Two or more part references with nonzero rank must "
4335 "not be specified at %L", &expr->where);
4336 return FAILURE;
4337 }
4338
4339 if (ref->type == REF_COMPONENT)
4340 {
4341 if (current_part_dimension)
4342 seen_part_dimension = 1;
4343
edf1eac2 4344 /* reset to make sure */
6de9cd9a
DN
4345 current_part_dimension = 0;
4346 }
4347 }
4348
4349 return SUCCESS;
4350}
4351
4352
4353/* Given an expression, determine its shape. This is easier than it sounds.
f7b529fa 4354 Leaves the shape array NULL if it is not possible to determine the shape. */
6de9cd9a
DN
4355
4356static void
edf1eac2 4357expression_shape (gfc_expr *e)
6de9cd9a
DN
4358{
4359 mpz_t array[GFC_MAX_DIMENSIONS];
4360 int i;
4361
4362 if (e->rank == 0 || e->shape != NULL)
4363 return;
4364
4365 for (i = 0; i < e->rank; i++)
4366 if (gfc_array_dimen_size (e, i, &array[i]) == FAILURE)
4367 goto fail;
4368
4369 e->shape = gfc_get_shape (e->rank);
4370
4371 memcpy (e->shape, array, e->rank * sizeof (mpz_t));
4372
4373 return;
4374
4375fail:
4376 for (i--; i >= 0; i--)
4377 mpz_clear (array[i]);
4378}
4379
4380
4381/* Given a variable expression node, compute the rank of the expression by
4382 examining the base symbol and any reference structures it may have. */
4383
4384static void
edf1eac2 4385expression_rank (gfc_expr *e)
6de9cd9a
DN
4386{
4387 gfc_ref *ref;
4388 int i, rank;
4389
00ca6640
DK
4390 /* Just to make sure, because EXPR_COMPCALL's also have an e->ref and that
4391 could lead to serious confusion... */
4392 gcc_assert (e->expr_type != EXPR_COMPCALL);
4393
6de9cd9a
DN
4394 if (e->ref == NULL)
4395 {
4396 if (e->expr_type == EXPR_ARRAY)
4397 goto done;
f7b529fa 4398 /* Constructors can have a rank different from one via RESHAPE(). */
6de9cd9a
DN
4399
4400 if (e->symtree == NULL)
4401 {
4402 e->rank = 0;
4403 goto done;
4404 }
4405
4406 e->rank = (e->symtree->n.sym->as == NULL)
edf1eac2 4407 ? 0 : e->symtree->n.sym->as->rank;
6de9cd9a
DN
4408 goto done;
4409 }
4410
4411 rank = 0;
4412
4413 for (ref = e->ref; ref; ref = ref->next)
4414 {
4415 if (ref->type != REF_ARRAY)
4416 continue;
4417
4418 if (ref->u.ar.type == AR_FULL)
4419 {
4420 rank = ref->u.ar.as->rank;
4421 break;
4422 }
4423
4424 if (ref->u.ar.type == AR_SECTION)
4425 {
edf1eac2 4426 /* Figure out the rank of the section. */
6de9cd9a
DN
4427 if (rank != 0)
4428 gfc_internal_error ("expression_rank(): Two array specs");
4429
4430 for (i = 0; i < ref->u.ar.dimen; i++)
4431 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
4432 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
4433 rank++;
4434
4435 break;
4436 }
4437 }
4438
4439 e->rank = rank;
4440
4441done:
4442 expression_shape (e);
4443}
4444
4445
4446/* Resolve a variable expression. */
4447
17b1d2a0 4448static gfc_try
edf1eac2 4449resolve_variable (gfc_expr *e)
6de9cd9a
DN
4450{
4451 gfc_symbol *sym;
17b1d2a0 4452 gfc_try t;
0e9a445b
PT
4453
4454 t = SUCCESS;
6de9cd9a 4455
3e978d30 4456 if (e->symtree == NULL)
6de9cd9a
DN
4457 return FAILURE;
4458
3e978d30 4459 if (e->ref && resolve_ref (e) == FAILURE)
009e94d4
FXC
4460 return FAILURE;
4461
6de9cd9a 4462 sym = e->symtree->n.sym;
3070bab4
JW
4463 if (sym->attr.flavor == FL_PROCEDURE
4464 && (!sym->attr.function
4465 || (sym->attr.function && sym->result
4466 && sym->result->attr.proc_pointer
4467 && !sym->result->attr.function)))
6de9cd9a
DN
4468 {
4469 e->ts.type = BT_PROCEDURE;
a03826d1 4470 goto resolve_procedure;
6de9cd9a
DN
4471 }
4472
4473 if (sym->ts.type != BT_UNKNOWN)
4474 gfc_variable_attr (e, &e->ts);
4475 else
4476 {
4477 /* Must be a simple variable reference. */
9d691ba7 4478 if (gfc_set_default_type (sym, 1, sym->ns) == FAILURE)
6de9cd9a
DN
4479 return FAILURE;
4480 e->ts = sym->ts;
4481 }
4482
48474141
PT
4483 if (check_assumed_size_reference (sym, e))
4484 return FAILURE;
4485
0e9a445b
PT
4486 /* Deal with forward references to entries during resolve_code, to
4487 satisfy, at least partially, 12.5.2.5. */
4488 if (gfc_current_ns->entries
edf1eac2
SK
4489 && current_entry_id == sym->entry_id
4490 && cs_base
4491 && cs_base->current
4492 && cs_base->current->op != EXEC_ENTRY)
0e9a445b
PT
4493 {
4494 gfc_entry_list *entry;
4495 gfc_formal_arglist *formal;
4496 int n;
4497 bool seen;
4498
4499 /* If the symbol is a dummy... */
70365b5c 4500 if (sym->attr.dummy && sym->ns == gfc_current_ns)
0e9a445b
PT
4501 {
4502 entry = gfc_current_ns->entries;
4503 seen = false;
4504
4505 /* ...test if the symbol is a parameter of previous entries. */
4506 for (; entry && entry->id <= current_entry_id; entry = entry->next)
4507 for (formal = entry->sym->formal; formal; formal = formal->next)
4508 {
4509 if (formal->sym && sym->name == formal->sym->name)
4510 seen = true;
4511 }
4512
4513 /* If it has not been seen as a dummy, this is an error. */
4514 if (!seen)
4515 {
4516 if (specification_expr)
70365b5c
TB
4517 gfc_error ("Variable '%s', used in a specification expression"
4518 ", is referenced at %L before the ENTRY statement "
0e9a445b
PT
4519 "in which it is a parameter",
4520 sym->name, &cs_base->current->loc);
4521 else
4522 gfc_error ("Variable '%s' is used at %L before the ENTRY "
4523 "statement in which it is a parameter",
4524 sym->name, &cs_base->current->loc);
4525 t = FAILURE;
4526 }
4527 }
4528
4529 /* Now do the same check on the specification expressions. */
4530 specification_expr = 1;
4531 if (sym->ts.type == BT_CHARACTER
bc21d315 4532 && gfc_resolve_expr (sym->ts.u.cl->length) == FAILURE)
0e9a445b
PT
4533 t = FAILURE;
4534
4535 if (sym->as)
4536 for (n = 0; n < sym->as->rank; n++)
4537 {
4538 specification_expr = 1;
4539 if (gfc_resolve_expr (sym->as->lower[n]) == FAILURE)
4540 t = FAILURE;
4541 specification_expr = 1;
4542 if (gfc_resolve_expr (sym->as->upper[n]) == FAILURE)
4543 t = FAILURE;
4544 }
4545 specification_expr = 0;
4546
4547 if (t == SUCCESS)
4548 /* Update the symbol's entry level. */
4549 sym->entry_id = current_entry_id + 1;
4550 }
4551
a03826d1
DK
4552resolve_procedure:
4553 if (t == SUCCESS && resolve_procedure_expression (e) == FAILURE)
4554 t = FAILURE;
4555
0e9a445b 4556 return t;
6de9cd9a
DN
4557}
4558
4559
eb77cddf
PT
4560/* Checks to see that the correct symbol has been host associated.
4561 The only situation where this arises is that in which a twice
4562 contained function is parsed after the host association is made.
5b3b1d09
PT
4563 Therefore, on detecting this, change the symbol in the expression
4564 and convert the array reference into an actual arglist if the old
4565 symbol is a variable. */
eb77cddf
PT
4566static bool
4567check_host_association (gfc_expr *e)
4568{
4569 gfc_symbol *sym, *old_sym;
5b3b1d09 4570 gfc_symtree *st;
eb77cddf 4571 int n;
5b3b1d09 4572 gfc_ref *ref;
e4bf01a4 4573 gfc_actual_arglist *arg, *tail = NULL;
8de10a62 4574 bool retval = e->expr_type == EXPR_FUNCTION;
eb77cddf 4575
a1ab6660
PT
4576 /* If the expression is the result of substitution in
4577 interface.c(gfc_extend_expr) because there is no way in
4578 which the host association can be wrong. */
4579 if (e->symtree == NULL
4580 || e->symtree->n.sym == NULL
4581 || e->user_operator)
8de10a62 4582 return retval;
eb77cddf
PT
4583
4584 old_sym = e->symtree->n.sym;
8de10a62 4585
eb77cddf 4586 if (gfc_current_ns->parent
eb77cddf
PT
4587 && old_sym->ns != gfc_current_ns)
4588 {
5b3b1d09
PT
4589 /* Use the 'USE' name so that renamed module symbols are
4590 correctly handled. */
9be3684b 4591 gfc_find_symbol (e->symtree->name, gfc_current_ns, 1, &sym);
5b3b1d09 4592
a944c79a 4593 if (sym && old_sym != sym
67cec813 4594 && sym->ts.type == old_sym->ts.type
a944c79a
PT
4595 && sym->attr.flavor == FL_PROCEDURE
4596 && sym->attr.contained)
eb77cddf 4597 {
5b3b1d09 4598 /* Clear the shape, since it might not be valid. */
eb77cddf
PT
4599 if (e->shape != NULL)
4600 {
4601 for (n = 0; n < e->rank; n++)
4602 mpz_clear (e->shape[n]);
4603
4604 gfc_free (e->shape);
4605 }
4606
1aafbf99
PT
4607 /* Give the expression the right symtree! */
4608 gfc_find_sym_tree (e->symtree->name, NULL, 1, &st);
4609 gcc_assert (st != NULL);
eb77cddf 4610
1aafbf99
PT
4611 if (old_sym->attr.flavor == FL_PROCEDURE
4612 || e->expr_type == EXPR_FUNCTION)
4613 {
5b3b1d09
PT
4614 /* Original was function so point to the new symbol, since
4615 the actual argument list is already attached to the
4616 expression. */
4617 e->value.function.esym = NULL;
4618 e->symtree = st;
4619 }
4620 else
4621 {
4622 /* Original was variable so convert array references into
4623 an actual arglist. This does not need any checking now
4624 since gfc_resolve_function will take care of it. */
4625 e->value.function.actual = NULL;
4626 e->expr_type = EXPR_FUNCTION;
4627 e->symtree = st;
eb77cddf 4628
5b3b1d09
PT
4629 /* Ambiguity will not arise if the array reference is not
4630 the last reference. */
4631 for (ref = e->ref; ref; ref = ref->next)
4632 if (ref->type == REF_ARRAY && ref->next == NULL)
4633 break;
4634
4635 gcc_assert (ref->type == REF_ARRAY);
4636
4637 /* Grab the start expressions from the array ref and
4638 copy them into actual arguments. */
4639 for (n = 0; n < ref->u.ar.dimen; n++)
4640 {
4641 arg = gfc_get_actual_arglist ();
4642 arg->expr = gfc_copy_expr (ref->u.ar.start[n]);
4643 if (e->value.function.actual == NULL)
4644 tail = e->value.function.actual = arg;
4645 else
4646 {
4647 tail->next = arg;
4648 tail = arg;
4649 }
4650 }
eb77cddf 4651
5b3b1d09
PT
4652 /* Dump the reference list and set the rank. */
4653 gfc_free_ref_list (e->ref);
4654 e->ref = NULL;
4655 e->rank = sym->as ? sym->as->rank : 0;
4656 }
4657
4658 gfc_resolve_expr (e);
4659 sym->refs++;
eb77cddf
PT
4660 }
4661 }
8de10a62 4662 /* This might have changed! */
eb77cddf
PT
4663 return e->expr_type == EXPR_FUNCTION;
4664}
4665
4666
07368af0
PT
4667static void
4668gfc_resolve_character_operator (gfc_expr *e)
4669{
4670 gfc_expr *op1 = e->value.op.op1;
4671 gfc_expr *op2 = e->value.op.op2;
4672 gfc_expr *e1 = NULL;
4673 gfc_expr *e2 = NULL;
4674
a1ee985f 4675 gcc_assert (e->value.op.op == INTRINSIC_CONCAT);
07368af0 4676
bc21d315
JW
4677 if (op1->ts.u.cl && op1->ts.u.cl->length)
4678 e1 = gfc_copy_expr (op1->ts.u.cl->length);
07368af0
PT
4679 else if (op1->expr_type == EXPR_CONSTANT)
4680 e1 = gfc_int_expr (op1->value.character.length);
4681
bc21d315
JW
4682 if (op2->ts.u.cl && op2->ts.u.cl->length)
4683 e2 = gfc_copy_expr (op2->ts.u.cl->length);
07368af0
PT
4684 else if (op2->expr_type == EXPR_CONSTANT)
4685 e2 = gfc_int_expr (op2->value.character.length);
4686
b76e28c6 4687 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4688
4689 if (!e1 || !e2)
4690 return;
4691
bc21d315
JW
4692 e->ts.u.cl->length = gfc_add (e1, e2);
4693 e->ts.u.cl->length->ts.type = BT_INTEGER;
4694 e->ts.u.cl->length->ts.kind = gfc_charlen_int_kind;
4695 gfc_simplify_expr (e->ts.u.cl->length, 0);
4696 gfc_resolve_expr (e->ts.u.cl->length);
07368af0
PT
4697
4698 return;
4699}
4700
4701
4702/* Ensure that an character expression has a charlen and, if possible, a
4703 length expression. */
4704
4705static void
4706fixup_charlen (gfc_expr *e)
4707{
4708 /* The cases fall through so that changes in expression type and the need
4709 for multiple fixes are picked up. In all circumstances, a charlen should
4710 be available for the middle end to hang a backend_decl on. */
4711 switch (e->expr_type)
4712 {
4713 case EXPR_OP:
4714 gfc_resolve_character_operator (e);
4715
4716 case EXPR_ARRAY:
4717 if (e->expr_type == EXPR_ARRAY)
4718 gfc_resolve_character_array_constructor (e);
4719
4720 case EXPR_SUBSTRING:
bc21d315 4721 if (!e->ts.u.cl && e->ref)
07368af0
PT
4722 gfc_resolve_substring_charlen (e);
4723
4724 default:
bc21d315 4725 if (!e->ts.u.cl)
b76e28c6 4726 e->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
07368af0
PT
4727
4728 break;
4729 }
4730}
4731
4732
8e1f752a
DK
4733/* Update an actual argument to include the passed-object for type-bound
4734 procedures at the right position. */
4735
4736static gfc_actual_arglist*
90661f26
JW
4737update_arglist_pass (gfc_actual_arglist* lst, gfc_expr* po, unsigned argpos,
4738 const char *name)
8e1f752a 4739{
b82657f4
DK
4740 gcc_assert (argpos > 0);
4741
8e1f752a
DK
4742 if (argpos == 1)
4743 {
4744 gfc_actual_arglist* result;
4745
4746 result = gfc_get_actual_arglist ();
4747 result->expr = po;
4748 result->next = lst;
90661f26
JW
4749 if (name)
4750 result->name = name;
8e1f752a
DK
4751
4752 return result;
4753 }
4754
90661f26
JW
4755 if (lst)
4756 lst->next = update_arglist_pass (lst->next, po, argpos - 1, name);
4757 else
4758 lst = update_arglist_pass (NULL, po, argpos - 1, name);
8e1f752a
DK
4759 return lst;
4760}
4761
4762
e157f736 4763/* Extract the passed-object from an EXPR_COMPCALL (a copy of it). */
8e1f752a 4764
e157f736
DK
4765static gfc_expr*
4766extract_compcall_passed_object (gfc_expr* e)
8e1f752a
DK
4767{
4768 gfc_expr* po;
8e1f752a 4769
e157f736 4770 gcc_assert (e->expr_type == EXPR_COMPCALL);
8e1f752a 4771
4a44a72d
DK
4772 if (e->value.compcall.base_object)
4773 po = gfc_copy_expr (e->value.compcall.base_object);
4774 else
4775 {
4776 po = gfc_get_expr ();
4777 po->expr_type = EXPR_VARIABLE;
4778 po->symtree = e->symtree;
4779 po->ref = gfc_copy_ref (e->ref);
4780 }
8e1f752a
DK
4781
4782 if (gfc_resolve_expr (po) == FAILURE)
e157f736
DK
4783 return NULL;
4784
4785 return po;
4786}
4787
4788
4789/* Update the arglist of an EXPR_COMPCALL expression to include the
4790 passed-object. */
4791
4792static gfc_try
4793update_compcall_arglist (gfc_expr* e)
4794{
4795 gfc_expr* po;
4796 gfc_typebound_proc* tbp;
4797
4798 tbp = e->value.compcall.tbp;
4799
b82657f4
DK
4800 if (tbp->error)
4801 return FAILURE;
4802
e157f736
DK
4803 po = extract_compcall_passed_object (e);
4804 if (!po)
8e1f752a 4805 return FAILURE;
e157f736 4806
4a44a72d 4807 if (tbp->nopass || e->value.compcall.ignore_pass)
8e1f752a
DK
4808 {
4809 gfc_free_expr (po);
4810 return SUCCESS;
4811 }
4812
4813 gcc_assert (tbp->pass_arg_num > 0);
4814 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
90661f26
JW
4815 tbp->pass_arg_num,
4816 tbp->pass_arg);
4817
4818 return SUCCESS;
4819}
4820
4821
4822/* Extract the passed object from a PPC call (a copy of it). */
4823
4824static gfc_expr*
4825extract_ppc_passed_object (gfc_expr *e)
4826{
4827 gfc_expr *po;
4828 gfc_ref **ref;
4829
4830 po = gfc_get_expr ();
4831 po->expr_type = EXPR_VARIABLE;
4832 po->symtree = e->symtree;
4833 po->ref = gfc_copy_ref (e->ref);
4834
4835 /* Remove PPC reference. */
4836 ref = &po->ref;
4837 while ((*ref)->next)
4838 (*ref) = (*ref)->next;
4839 gfc_free_ref_list (*ref);
4840 *ref = NULL;
4841
4842 if (gfc_resolve_expr (po) == FAILURE)
4843 return NULL;
4844
4845 return po;
4846}
4847
4848
4849/* Update the actual arglist of a procedure pointer component to include the
4850 passed-object. */
4851
4852static gfc_try
4853update_ppc_arglist (gfc_expr* e)
4854{
4855 gfc_expr* po;
4856 gfc_component *ppc;
4857 gfc_typebound_proc* tb;
4858
4859 if (!gfc_is_proc_ptr_comp (e, &ppc))
4860 return FAILURE;
4861
4862 tb = ppc->tb;
4863
4864 if (tb->error)
4865 return FAILURE;
4866 else if (tb->nopass)
4867 return SUCCESS;
4868
4869 po = extract_ppc_passed_object (e);
4870 if (!po)
4871 return FAILURE;
4872
4873 if (po->rank > 0)
4874 {
4875 gfc_error ("Passed-object at %L must be scalar", &e->where);
4876 return FAILURE;
4877 }
4878
4879 gcc_assert (tb->pass_arg_num > 0);
4880 e->value.compcall.actual = update_arglist_pass (e->value.compcall.actual, po,
4881 tb->pass_arg_num,
4882 tb->pass_arg);
8e1f752a
DK
4883
4884 return SUCCESS;
4885}
4886
4887
b0e5fa94
DK
4888/* Check that the object a TBP is called on is valid, i.e. it must not be
4889 of ABSTRACT type (as in subobject%abstract_parent%tbp()). */
4890
4891static gfc_try
4892check_typebound_baseobject (gfc_expr* e)
4893{
4894 gfc_expr* base;
4895
4896 base = extract_compcall_passed_object (e);
4897 if (!base)
4898 return FAILURE;
4899
cf2b3c22 4900 gcc_assert (base->ts.type == BT_DERIVED || base->ts.type == BT_CLASS);
e56817db
TB
4901
4902 if (base->ts.type == BT_DERIVED && base->ts.u.derived->attr.abstract)
b0e5fa94
DK
4903 {
4904 gfc_error ("Base object for type-bound procedure call at %L is of"
bc21d315 4905 " ABSTRACT type '%s'", &e->where, base->ts.u.derived->name);
b0e5fa94
DK
4906 return FAILURE;
4907 }
4908
41a394bb
DK
4909 /* If the procedure called is NOPASS, the base object must be scalar. */
4910 if (e->value.compcall.tbp->nopass && base->rank > 0)
4911 {
4912 gfc_error ("Base object for NOPASS type-bound procedure call at %L must"
4913 " be scalar", &e->where);
4914 return FAILURE;
4915 }
4916
4917 /* FIXME: Remove once PR 41177 (this problem) is fixed completely. */
4918 if (base->rank > 0)
4919 {
4920 gfc_error ("Non-scalar base object at %L currently not implemented",
4921 &e->where);
4922 return FAILURE;
4923 }
4924
b0e5fa94
DK
4925 return SUCCESS;
4926}
4927
4928
8e1f752a
DK
4929/* Resolve a call to a type-bound procedure, either function or subroutine,
4930 statically from the data in an EXPR_COMPCALL expression. The adapted
4931 arglist and the target-procedure symtree are returned. */
4932
4933static gfc_try
4934resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
4935 gfc_actual_arglist** actual)
4936{
4937 gcc_assert (e->expr_type == EXPR_COMPCALL);
e157f736 4938 gcc_assert (!e->value.compcall.tbp->is_generic);
8e1f752a
DK
4939
4940 /* Update the actual arglist for PASS. */
4941 if (update_compcall_arglist (e) == FAILURE)
4942 return FAILURE;
4943
4944 *actual = e->value.compcall.actual;
e157f736 4945 *target = e->value.compcall.tbp->u.specific;
8e1f752a
DK
4946
4947 gfc_free_ref_list (e->ref);
4948 e->ref = NULL;
4949 e->value.compcall.actual = NULL;
4950
4951 return SUCCESS;
4952}
4953
4954
e157f736
DK
4955/* Given an EXPR_COMPCALL calling a GENERIC typebound procedure, figure out
4956 which of the specific bindings (if any) matches the arglist and transform
4957 the expression into a call of that binding. */
4958
4959static gfc_try
4960resolve_typebound_generic_call (gfc_expr* e)
4961{
4962 gfc_typebound_proc* genproc;
4963 const char* genname;
4964
4965 gcc_assert (e->expr_type == EXPR_COMPCALL);
4966 genname = e->value.compcall.name;
4967 genproc = e->value.compcall.tbp;
4968
4969 if (!genproc->is_generic)
4970 return SUCCESS;
4971
4972 /* Try the bindings on this type and in the inheritance hierarchy. */
4973 for (; genproc; genproc = genproc->overridden)
4974 {
4975 gfc_tbp_generic* g;
4976
4977 gcc_assert (genproc->is_generic);
4978 for (g = genproc->u.generic; g; g = g->next)
4979 {
4980 gfc_symbol* target;
4981 gfc_actual_arglist* args;
4982 bool matches;
4983
4984 gcc_assert (g->specific);
b82657f4
DK
4985
4986 if (g->specific->error)
4987 continue;
4988
e157f736
DK
4989 target = g->specific->u.specific->n.sym;
4990
4991 /* Get the right arglist by handling PASS/NOPASS. */
4992 args = gfc_copy_actual_arglist (e->value.compcall.actual);
4993 if (!g->specific->nopass)
4994 {
4995 gfc_expr* po;
4996 po = extract_compcall_passed_object (e);
4997 if (!po)
4998 return FAILURE;
4999
b82657f4
DK
5000 gcc_assert (g->specific->pass_arg_num > 0);
5001 gcc_assert (!g->specific->error);
90661f26
JW
5002 args = update_arglist_pass (args, po, g->specific->pass_arg_num,
5003 g->specific->pass_arg);
e157f736 5004 }
f0ac18b7
DK
5005 resolve_actual_arglist (args, target->attr.proc,
5006 is_external_proc (target) && !target->formal);
e157f736
DK
5007
5008 /* Check if this arglist matches the formal. */
f0ac18b7 5009 matches = gfc_arglist_matches_symbol (&args, target);
e157f736
DK
5010
5011 /* Clean up and break out of the loop if we've found it. */
5012 gfc_free_actual_arglist (args);
5013 if (matches)
5014 {
5015 e->value.compcall.tbp = g->specific;
5016 goto success;
5017 }
5018 }
5019 }
5020
5021 /* Nothing matching found! */
5022 gfc_error ("Found no matching specific binding for the call to the GENERIC"
5023 " '%s' at %L", genname, &e->where);
5024 return FAILURE;
5025
5026success:
5027 return SUCCESS;
5028}
5029
5030
8e1f752a
DK
5031/* Resolve a call to a type-bound subroutine. */
5032
5033static gfc_try
5034resolve_typebound_call (gfc_code* c)
5035{
5036 gfc_actual_arglist* newactual;
5037 gfc_symtree* target;
5038
e157f736 5039 /* Check that's really a SUBROUTINE. */
a513927a 5040 if (!c->expr1->value.compcall.tbp->subroutine)
e157f736
DK
5041 {
5042 gfc_error ("'%s' at %L should be a SUBROUTINE",
a513927a 5043 c->expr1->value.compcall.name, &c->loc);
e157f736
DK
5044 return FAILURE;
5045 }
5046
a513927a 5047 if (check_typebound_baseobject (c->expr1) == FAILURE)
b0e5fa94
DK
5048 return FAILURE;
5049
a513927a 5050 if (resolve_typebound_generic_call (c->expr1) == FAILURE)
e157f736
DK
5051 return FAILURE;
5052
8e1f752a
DK
5053 /* Transform into an ordinary EXEC_CALL for now. */
5054
a513927a 5055 if (resolve_typebound_static (c->expr1, &target, &newactual) == FAILURE)
8e1f752a
DK
5056 return FAILURE;
5057
5058 c->ext.actual = newactual;
5059 c->symtree = target;
4a44a72d 5060 c->op = (c->expr1->value.compcall.assign ? EXEC_ASSIGN_CALL : EXEC_CALL);
8e1f752a 5061
a513927a 5062 gcc_assert (!c->expr1->ref && !c->expr1->value.compcall.actual);
7cf078dc 5063
a513927a 5064 gfc_free_expr (c->expr1);
7cf078dc
PT
5065 c->expr1 = gfc_get_expr ();
5066 c->expr1->expr_type = EXPR_FUNCTION;
5067 c->expr1->symtree = target;
5068 c->expr1->where = c->loc;
8e1f752a
DK
5069
5070 return resolve_call (c);
5071}
5072
5073
7cf078dc
PT
5074/* Resolve a component-call expression. This originally was intended
5075 only to see functions. However, it is convenient to use it in
5076 resolving subroutine class methods, since we do not have to add a
5077 gfc_code each time. */
8e1f752a 5078static gfc_try
7cf078dc 5079resolve_compcall (gfc_expr* e, bool fcn)
8e1f752a
DK
5080{
5081 gfc_actual_arglist* newactual;
5082 gfc_symtree* target;
5083
e157f736 5084 /* Check that's really a FUNCTION. */
7cf078dc 5085 if (fcn && !e->value.compcall.tbp->function)
e157f736
DK
5086 {
5087 gfc_error ("'%s' at %L should be a FUNCTION",
5088 e->value.compcall.name, &e->where);
5089 return FAILURE;
5090 }
7cf078dc
PT
5091 else if (!fcn && !e->value.compcall.tbp->subroutine)
5092 {
5093 /* To resolve class member calls, we borrow this bit
5094 of code to select the specific procedures. */
5095 gfc_error ("'%s' at %L should be a SUBROUTINE",
5096 e->value.compcall.name, &e->where);
5097 return FAILURE;
5098 }
e157f736 5099
4a44a72d
DK
5100 /* These must not be assign-calls! */
5101 gcc_assert (!e->value.compcall.assign);
5102
b0e5fa94
DK
5103 if (check_typebound_baseobject (e) == FAILURE)
5104 return FAILURE;
5105
e157f736
DK
5106 if (resolve_typebound_generic_call (e) == FAILURE)
5107 return FAILURE;
00ca6640
DK
5108 gcc_assert (!e->value.compcall.tbp->is_generic);
5109
5110 /* Take the rank from the function's symbol. */
5111 if (e->value.compcall.tbp->u.specific->n.sym->as)
5112 e->rank = e->value.compcall.tbp->u.specific->n.sym->as->rank;
e157f736
DK
5113
5114 /* For now, we simply transform it into an EXPR_FUNCTION call with the same
8e1f752a
DK
5115 arglist to the TBP's binding target. */
5116
5117 if (resolve_typebound_static (e, &target, &newactual) == FAILURE)
5118 return FAILURE;
5119
5120 e->value.function.actual = newactual;
e157f736 5121 e->value.function.name = e->value.compcall.name;
37a40b53 5122 e->value.function.esym = target->n.sym;
7cf078dc 5123 e->value.function.class_esym = NULL;
e157f736 5124 e->value.function.isym = NULL;
8e1f752a 5125 e->symtree = target;
f0ac18b7 5126 e->ts = target->n.sym->ts;
8e1f752a
DK
5127 e->expr_type = EXPR_FUNCTION;
5128
7cf078dc
PT
5129 /* Resolution is not necessary if this is a class subroutine; this
5130 function only has to identify the specific proc. Resolution of
5131 the call will be done next in resolve_typebound_call. */
5132 return fcn ? gfc_resolve_expr (e) : SUCCESS;
5133}
5134
5135
5136/* Resolve a typebound call for the members in a class. This group of
5137 functions implements dynamic dispatch in the provisional version
5138 of f03 OOP. As soon as vtables are in place and contain pointers
5139 to methods, this will no longer be necessary. */
5140static gfc_expr *list_e;
5141static void check_class_members (gfc_symbol *);
5142static gfc_try class_try;
5143static bool fcn_flag;
5144static gfc_symbol *class_object;
5145
5146
5147static void
5148check_members (gfc_symbol *derived)
5149{
5150 if (derived->attr.flavor == FL_DERIVED)
5151 check_class_members (derived);
5152}
5153
5154
5155static void
5156check_class_members (gfc_symbol *derived)
5157{
7cf078dc
PT
5158 gfc_expr *e;
5159 gfc_symtree *tbp;
5160 gfc_class_esym_list *etmp;
5161
5162 e = gfc_copy_expr (list_e);
5163
5164 tbp = gfc_find_typebound_proc (derived, &class_try,
5165 e->value.compcall.name,
5166 false, &e->where);
5167
5168 if (tbp == NULL)
5169 {
5170 gfc_error ("no typebound available procedure named '%s' at %L",
5171 e->value.compcall.name, &e->where);
5172 return;
5173 }
5174
5175 if (tbp->n.tb->is_generic)
5176 {
7cf078dc
PT
5177 /* If we have to match a passed class member, force the actual
5178 expression to have the correct type. */
5179 if (!tbp->n.tb->nopass)
5180 {
5181 if (e->value.compcall.base_object == NULL)
5182 e->value.compcall.base_object =
5183 extract_compcall_passed_object (e);
5184
5185 e->value.compcall.base_object->ts.type = BT_DERIVED;
5186 e->value.compcall.base_object->ts.u.derived = derived;
5187 }
5188 }
7cf078dc
PT
5189
5190 e->value.compcall.tbp = tbp->n.tb;
5191 e->value.compcall.name = tbp->name;
5192
28fccf2c
PT
5193 /* Let the original expresssion catch the assertion in
5194 resolve_compcall, since this flag does not appear to be reset or
5195 copied in some systems. */
5196 e->value.compcall.assign = 0;
5197
7cf078dc
PT
5198 /* Do the renaming, PASSing, generic => specific and other
5199 good things for each class member. */
5200 class_try = (resolve_compcall (e, fcn_flag) == SUCCESS)
5201 ? class_try : FAILURE;
5202
5203 /* Now transfer the found symbol to the esym list. */
5204 if (class_try == SUCCESS)
5205 {
5206 etmp = list_e->value.function.class_esym;
5207 list_e->value.function.class_esym
5208 = gfc_get_class_esym_list();
5209 list_e->value.function.class_esym->next = etmp;
5210 list_e->value.function.class_esym->derived = derived;
7cf078dc
PT
5211 list_e->value.function.class_esym->esym
5212 = e->value.function.esym;
5213 }
5214
5215 gfc_free_expr (e);
5216
5217 /* Burrow down into grandchildren types. */
5218 if (derived->f2k_derived)
5219 gfc_traverse_ns (derived->f2k_derived, check_members);
5220}
5221
5222
5223/* Eliminate esym_lists where all the members point to the
5224 typebound procedure of the declared type; ie. one where
5225 type selection has no effect.. */
5226static void
5227resolve_class_esym (gfc_expr *e)
5228{
5229 gfc_class_esym_list *p, *q;
5230 bool empty = true;
5231
5232 gcc_assert (e && e->expr_type == EXPR_FUNCTION);
5233
5234 p = e->value.function.class_esym;
5235 if (p == NULL)
5236 return;
5237
5238 for (; p; p = p->next)
5239 empty = empty && (e->value.function.esym == p->esym);
5240
5241 if (empty)
5242 {
5243 p = e->value.function.class_esym;
5244 for (; p; p = q)
5245 {
5246 q = p->next;
5247 gfc_free (p);
5248 }
5249 e->value.function.class_esym = NULL;
5250 }
5251}
5252
5253
7c1dab0d 5254/* Generate an expression for the hash value, given the reference to
28188747
PT
5255 the class of the final expression (class_ref), the base of the
5256 full reference list (new_ref), the declared type and the class
5257 object (st). */
5258static gfc_expr*
7c1dab0d 5259hash_value_expr (gfc_ref *class_ref, gfc_ref *new_ref, gfc_symtree *st)
28188747 5260{
7c1dab0d 5261 gfc_expr *hash_value;
28188747 5262
7c1dab0d 5263 /* Build an expression for the correct hash_value; ie. that of the last
28188747 5264 CLASS reference. */
28188747
PT
5265 if (class_ref)
5266 {
7c1dab0d 5267 class_ref->next = NULL;
28188747
PT
5268 }
5269 else
5270 {
5271 gfc_free_ref_list (new_ref);
7c1dab0d 5272 new_ref = NULL;
28188747 5273 }
7c1dab0d
JW
5274 hash_value = gfc_get_expr ();
5275 hash_value->expr_type = EXPR_VARIABLE;
5276 hash_value->symtree = st;
5277 hash_value->symtree->n.sym->refs++;
5278 hash_value->ref = new_ref;
5279 gfc_add_component_ref (hash_value, "$vptr");
5280 gfc_add_component_ref (hash_value, "$hash");
28188747 5281
7c1dab0d 5282 return hash_value;
28188747
PT
5283}
5284
5285
5286/* Get the ultimate declared type from an expression. In addition,
5287 return the last class/derived type reference and the copy of the
5288 reference list. */
5289static gfc_symbol*
5290get_declared_from_expr (gfc_ref **class_ref, gfc_ref **new_ref,
5291 gfc_expr *e)
5292{
5293 gfc_symbol *declared;
5294 gfc_ref *ref;
5295
5296 declared = NULL;
5297 *class_ref = NULL;
5298 *new_ref = gfc_copy_ref (e->ref);
5299 for (ref = *new_ref; ref; ref = ref->next)
5300 {
5301 if (ref->type != REF_COMPONENT)
5302 continue;
5303
5304 if (ref->u.c.component->ts.type == BT_CLASS
5305 || ref->u.c.component->ts.type == BT_DERIVED)
5306 {
5307 declared = ref->u.c.component->ts.u.derived;
5308 *class_ref = ref;
5309 }
5310 }
5311
5312 if (declared == NULL)
5313 declared = e->symtree->n.sym->ts.u.derived;
5314
5315 return declared;
5316}
5317
5318
f116b2fc
PT
5319/* Resolve the argument expressions so that any arguments expressions
5320 that include class methods are resolved before the current call.
5321 This is necessary because of the static variables used in CLASS
5322 method resolution. */
5323static void
5324resolve_arg_exprs (gfc_actual_arglist *arg)
5325{
5326 /* Resolve the actual arglist expressions. */
5327 for (; arg; arg = arg->next)
5328 {
5329 if (arg->expr)
5330 gfc_resolve_expr (arg->expr);
5331 }
5332}
5333
5334
7cf078dc
PT
5335/* Resolve a CLASS typebound function, or 'method'. */
5336static gfc_try
5337resolve_class_compcall (gfc_expr* e)
5338{
28188747
PT
5339 gfc_symbol *derived, *declared;
5340 gfc_ref *new_ref;
5341 gfc_ref *class_ref;
5342 gfc_symtree *st;
5343
5344 st = e->symtree;
5345 class_object = st->n.sym;
7cf078dc 5346
28188747
PT
5347 /* Get the CLASS declared type. */
5348 declared = get_declared_from_expr (&class_ref, &new_ref, e);
7cf078dc 5349
28188747
PT
5350 /* Weed out cases of the ultimate component being a derived type. */
5351 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5352 {
5353 gfc_free_ref_list (new_ref);
5354 return resolve_compcall (e, true);
f116b2fc
PT
5355 }
5356
5357 /* Resolve the argument expressions, */
5358 resolve_arg_exprs (e->value.function.actual);
7cf078dc
PT
5359
5360 /* Get the data component, which is of the declared type. */
28188747 5361 derived = declared->components->ts.u.derived;
7cf078dc
PT
5362
5363 /* Resolve the function call for each member of the class. */
5364 class_try = SUCCESS;
5365 fcn_flag = true;
5366 list_e = gfc_copy_expr (e);
5367 check_class_members (derived);
5368
5369 class_try = (resolve_compcall (e, true) == SUCCESS)
5370 ? class_try : FAILURE;
5371
5372 /* Transfer the class list to the original expression. Note that
5373 the class_esym list is cleaned up in trans-expr.c, as the calls
5374 are translated. */
5375 e->value.function.class_esym = list_e->value.function.class_esym;
5376 list_e->value.function.class_esym = NULL;
5377 gfc_free_expr (list_e);
5378
5379 resolve_class_esym (e);
5380
28188747 5381 /* More than one typebound procedure so transmit an expression for
7c1dab0d 5382 the hash_value as the selector. */
28188747 5383 if (e->value.function.class_esym != NULL)
7c1dab0d
JW
5384 e->value.function.class_esym->hash_value
5385 = hash_value_expr (class_ref, new_ref, st);
28188747 5386
7cf078dc
PT
5387 return class_try;
5388}
5389
5390/* Resolve a CLASS typebound subroutine, or 'method'. */
5391static gfc_try
5392resolve_class_typebound_call (gfc_code *code)
5393{
28188747
PT
5394 gfc_symbol *derived, *declared;
5395 gfc_ref *new_ref;
5396 gfc_ref *class_ref;
5397 gfc_symtree *st;
5398
5399 st = code->expr1->symtree;
5400 class_object = st->n.sym;
7cf078dc 5401
28188747
PT
5402 /* Get the CLASS declared type. */
5403 declared = get_declared_from_expr (&class_ref, &new_ref, code->expr1);
7cf078dc 5404
28188747
PT
5405 /* Weed out cases of the ultimate component being a derived type. */
5406 if (class_ref && class_ref->u.c.component->ts.type == BT_DERIVED)
5407 {
5408 gfc_free_ref_list (new_ref);
5409 return resolve_typebound_call (code);
5410 }
7cf078dc 5411
f116b2fc 5412 /* Resolve the argument expressions, */
aa9aed00 5413 resolve_arg_exprs (code->expr1->value.compcall.actual);
f116b2fc 5414
7cf078dc 5415 /* Get the data component, which is of the declared type. */
28188747 5416 derived = declared->components->ts.u.derived;
7cf078dc
PT
5417
5418 class_try = SUCCESS;
5419 fcn_flag = false;
5420 list_e = gfc_copy_expr (code->expr1);
5421 check_class_members (derived);
5422
5423 class_try = (resolve_typebound_call (code) == SUCCESS)
5424 ? class_try : FAILURE;
5425
5426 /* Transfer the class list to the original expression. Note that
5427 the class_esym list is cleaned up in trans-expr.c, as the calls
5428 are translated. */
5429 code->expr1->value.function.class_esym
5430 = list_e->value.function.class_esym;
5431 list_e->value.function.class_esym = NULL;
5432 gfc_free_expr (list_e);
5433
5434 resolve_class_esym (code->expr1);
5435
28188747 5436 /* More than one typebound procedure so transmit an expression for
7c1dab0d 5437 the hash_value as the selector. */
28188747 5438 if (code->expr1->value.function.class_esym != NULL)
7c1dab0d
JW
5439 code->expr1->value.function.class_esym->hash_value
5440 = hash_value_expr (class_ref, new_ref, st);
28188747 5441
7cf078dc 5442 return class_try;
8e1f752a
DK
5443}
5444
5445
713485cc
JW
5446/* Resolve a CALL to a Procedure Pointer Component (Subroutine). */
5447
5448static gfc_try
5449resolve_ppc_call (gfc_code* c)
5450{
5451 gfc_component *comp;
cf2b3c22
TB
5452 bool b;
5453
5454 b = gfc_is_proc_ptr_comp (c->expr1, &comp);
5455 gcc_assert (b);
713485cc 5456
a513927a
SK
5457 c->resolved_sym = c->expr1->symtree->n.sym;
5458 c->expr1->expr_type = EXPR_VARIABLE;
713485cc
JW
5459
5460 if (!comp->attr.subroutine)
a513927a 5461 gfc_add_subroutine (&comp->attr, comp->name, &c->expr1->where);
713485cc 5462
e35bbb23
JW
5463 if (resolve_ref (c->expr1) == FAILURE)
5464 return FAILURE;
5465
90661f26
JW
5466 if (update_ppc_arglist (c->expr1) == FAILURE)
5467 return FAILURE;
5468
5469 c->ext.actual = c->expr1->value.compcall.actual;
5470
713485cc
JW
5471 if (resolve_actual_arglist (c->ext.actual, comp->attr.proc,
5472 comp->formal == NULL) == FAILURE)
5473 return FAILURE;
5474
7e196f89 5475 gfc_ppc_use (comp, &c->expr1->value.compcall.actual, &c->expr1->where);
713485cc
JW
5476
5477 return SUCCESS;
5478}
5479
5480
5481/* Resolve a Function Call to a Procedure Pointer Component (Function). */
5482
5483static gfc_try
5484resolve_expr_ppc (gfc_expr* e)
5485{
5486 gfc_component *comp;
cf2b3c22
TB
5487 bool b;
5488
5489 b = gfc_is_proc_ptr_comp (e, &comp);
5490 gcc_assert (b);
713485cc
JW
5491
5492 /* Convert to EXPR_FUNCTION. */
5493 e->expr_type = EXPR_FUNCTION;
5494 e->value.function.isym = NULL;
5495 e->value.function.actual = e->value.compcall.actual;
5496 e->ts = comp->ts;
c74b74a8
JW
5497 if (comp->as != NULL)
5498 e->rank = comp->as->rank;
713485cc
JW
5499
5500 if (!comp->attr.function)
5501 gfc_add_function (&comp->attr, comp->name, &e->where);
5502
e35bbb23
JW
5503 if (resolve_ref (e) == FAILURE)
5504 return FAILURE;
5505
713485cc
JW
5506 if (resolve_actual_arglist (e->value.function.actual, comp->attr.proc,
5507 comp->formal == NULL) == FAILURE)
5508 return FAILURE;
5509
90661f26
JW
5510 if (update_ppc_arglist (e) == FAILURE)
5511 return FAILURE;
5512
7e196f89 5513 gfc_ppc_use (comp, &e->value.compcall.actual, &e->where);
713485cc
JW
5514
5515 return SUCCESS;
5516}
5517
5518
f2ff577a
JD
5519static bool
5520gfc_is_expandable_expr (gfc_expr *e)
5521{
5522 gfc_constructor *con;
5523
5524 if (e->expr_type == EXPR_ARRAY)
5525 {
5526 /* Traverse the constructor looking for variables that are flavor
5527 parameter. Parameters must be expanded since they are fully used at
5528 compile time. */
5529 for (con = e->value.constructor; con; con = con->next)
5530 {
5531 if (con->expr->expr_type == EXPR_VARIABLE
5532 && con->expr->symtree
5533 && (con->expr->symtree->n.sym->attr.flavor == FL_PARAMETER
5534 || con->expr->symtree->n.sym->attr.flavor == FL_VARIABLE))
5535 return true;
5536 if (con->expr->expr_type == EXPR_ARRAY
5537 && gfc_is_expandable_expr (con->expr))
5538 return true;
5539 }
5540 }
5541
5542 return false;
5543}
5544
6de9cd9a
DN
5545/* Resolve an expression. That is, make sure that types of operands agree
5546 with their operators, intrinsic operators are converted to function calls
5547 for overloaded types and unresolved function references are resolved. */
5548
17b1d2a0 5549gfc_try
edf1eac2 5550gfc_resolve_expr (gfc_expr *e)
6de9cd9a 5551{
17b1d2a0 5552 gfc_try t;
6de9cd9a
DN
5553
5554 if (e == NULL)
5555 return SUCCESS;
5556
5557 switch (e->expr_type)
5558 {
5559 case EXPR_OP:
5560 t = resolve_operator (e);
5561 break;
5562
5563 case EXPR_FUNCTION:
6de9cd9a 5564 case EXPR_VARIABLE:
eb77cddf
PT
5565
5566 if (check_host_association (e))
5567 t = resolve_function (e);
5568 else
5569 {
5570 t = resolve_variable (e);
5571 if (t == SUCCESS)
5572 expression_rank (e);
5573 }
07368af0 5574
bc21d315 5575 if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL && e->ref
9de88093 5576 && e->ref->type != REF_SUBSTRING)
07368af0
PT
5577 gfc_resolve_substring_charlen (e);
5578
6de9cd9a
DN
5579 break;
5580
8e1f752a 5581 case EXPR_COMPCALL:
7cf078dc
PT
5582 if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
5583 t = resolve_class_compcall (e);
5584 else
5585 t = resolve_compcall (e, true);
8e1f752a
DK
5586 break;
5587
6de9cd9a
DN
5588 case EXPR_SUBSTRING:
5589 t = resolve_ref (e);
5590 break;
5591
5592 case EXPR_CONSTANT:
5593 case EXPR_NULL:
5594 t = SUCCESS;
5595 break;
5596
713485cc
JW
5597 case EXPR_PPC:
5598 t = resolve_expr_ppc (e);
5599 break;
5600
6de9cd9a
DN
5601 case EXPR_ARRAY:
5602 t = FAILURE;
5603 if (resolve_ref (e) == FAILURE)
5604 break;
5605
5606 t = gfc_resolve_array_constructor (e);
5607 /* Also try to expand a constructor. */
5608 if (t == SUCCESS)
5609 {
5610 expression_rank (e);
f2ff577a
JD
5611 if (gfc_is_constant_expr (e) || gfc_is_expandable_expr (e))
5612 gfc_expand_constructor (e);
6de9cd9a 5613 }
1855915a 5614
edf1eac2 5615 /* This provides the opportunity for the length of constructors with
86bf520d 5616 character valued function elements to propagate the string length
edf1eac2 5617 to the expression. */
88fec49f 5618 if (t == SUCCESS && e->ts.type == BT_CHARACTER)
f2ff577a
JD
5619 {
5620 /* For efficiency, we call gfc_expand_constructor for BT_CHARACTER
5621 here rather then add a duplicate test for it above. */
5622 gfc_expand_constructor (e);
5623 t = gfc_resolve_character_array_constructor (e);
5624 }
6de9cd9a
DN
5625
5626 break;
5627
5628 case EXPR_STRUCTURE:
5629 t = resolve_ref (e);
5630 if (t == FAILURE)
5631 break;
5632
5633 t = resolve_structure_cons (e);
5634 if (t == FAILURE)
5635 break;
5636
5637 t = gfc_simplify_expr (e, 0);
5638 break;
5639
5640 default:
5641 gfc_internal_error ("gfc_resolve_expr(): Bad expression type");
5642 }
5643
bc21d315 5644 if (e->ts.type == BT_CHARACTER && t == SUCCESS && !e->ts.u.cl)
07368af0
PT
5645 fixup_charlen (e);
5646
6de9cd9a
DN
5647 return t;
5648}
5649
5650
8d5cfa27
SK
5651/* Resolve an expression from an iterator. They must be scalar and have
5652 INTEGER or (optionally) REAL type. */
6de9cd9a 5653
17b1d2a0 5654static gfc_try
edf1eac2
SK
5655gfc_resolve_iterator_expr (gfc_expr *expr, bool real_ok,
5656 const char *name_msgid)
6de9cd9a 5657{
8d5cfa27 5658 if (gfc_resolve_expr (expr) == FAILURE)
6de9cd9a
DN
5659 return FAILURE;
5660
8d5cfa27 5661 if (expr->rank != 0)
6de9cd9a 5662 {
31043f6c 5663 gfc_error ("%s at %L must be a scalar", _(name_msgid), &expr->where);
6de9cd9a
DN
5664 return FAILURE;
5665 }
5666
79e7840d 5667 if (expr->ts.type != BT_INTEGER)
6de9cd9a 5668 {
79e7840d
JD
5669 if (expr->ts.type == BT_REAL)
5670 {
5671 if (real_ok)
5672 return gfc_notify_std (GFC_STD_F95_DEL,
5673 "Deleted feature: %s at %L must be integer",
5674 _(name_msgid), &expr->where);
5675 else
5676 {
5677 gfc_error ("%s at %L must be INTEGER", _(name_msgid),
5678 &expr->where);
5679 return FAILURE;
5680 }
5681 }
31043f6c 5682 else
79e7840d
JD
5683 {
5684 gfc_error ("%s at %L must be INTEGER", _(name_msgid), &expr->where);
5685 return FAILURE;
5686 }
6de9cd9a 5687 }
8d5cfa27
SK
5688 return SUCCESS;
5689}
5690
5691
5692/* Resolve the expressions in an iterator structure. If REAL_OK is
5693 false allow only INTEGER type iterators, otherwise allow REAL types. */
5694
17b1d2a0 5695gfc_try
edf1eac2 5696gfc_resolve_iterator (gfc_iterator *iter, bool real_ok)
8d5cfa27 5697{
8d5cfa27
SK
5698 if (gfc_resolve_iterator_expr (iter->var, real_ok, "Loop variable")
5699 == FAILURE)
6de9cd9a
DN
5700 return FAILURE;
5701
8d5cfa27 5702 if (gfc_pure (NULL) && gfc_impure_variable (iter->var->symtree->n.sym))
6de9cd9a 5703 {
8d5cfa27
SK
5704 gfc_error ("Cannot assign to loop variable in PURE procedure at %L",
5705 &iter->var->where);
6de9cd9a
DN
5706 return FAILURE;
5707 }
5708
8d5cfa27
SK
5709 if (gfc_resolve_iterator_expr (iter->start, real_ok,
5710 "Start expression in DO loop") == FAILURE)
6de9cd9a
DN
5711 return FAILURE;
5712
8d5cfa27
SK
5713 if (gfc_resolve_iterator_expr (iter->end, real_ok,
5714 "End expression in DO loop") == FAILURE)
5715 return FAILURE;
6de9cd9a 5716
8d5cfa27
SK
5717 if (gfc_resolve_iterator_expr (iter->step, real_ok,
5718 "Step expression in DO loop") == FAILURE)
6de9cd9a
DN
5719 return FAILURE;
5720
8d5cfa27 5721 if (iter->step->expr_type == EXPR_CONSTANT)
6de9cd9a 5722 {
8d5cfa27
SK
5723 if ((iter->step->ts.type == BT_INTEGER
5724 && mpz_cmp_ui (iter->step->value.integer, 0) == 0)
5725 || (iter->step->ts.type == BT_REAL
5726 && mpfr_sgn (iter->step->value.real) == 0))
5727 {
5728 gfc_error ("Step expression in DO loop at %L cannot be zero",
5729 &iter->step->where);
5730 return FAILURE;
5731 }
6de9cd9a
DN
5732 }
5733
8d5cfa27
SK
5734 /* Convert start, end, and step to the same type as var. */
5735 if (iter->start->ts.kind != iter->var->ts.kind
5736 || iter->start->ts.type != iter->var->ts.type)
5737 gfc_convert_type (iter->start, &iter->var->ts, 2);
5738
5739 if (iter->end->ts.kind != iter->var->ts.kind
5740 || iter->end->ts.type != iter->var->ts.type)
5741 gfc_convert_type (iter->end, &iter->var->ts, 2);
5742
5743 if (iter->step->ts.kind != iter->var->ts.kind
5744 || iter->step->ts.type != iter->var->ts.type)
5745 gfc_convert_type (iter->step, &iter->var->ts, 2);
6de9cd9a 5746
dc186969
TB
5747 if (iter->start->expr_type == EXPR_CONSTANT
5748 && iter->end->expr_type == EXPR_CONSTANT
5749 && iter->step->expr_type == EXPR_CONSTANT)
5750 {
5751 int sgn, cmp;
5752 if (iter->start->ts.type == BT_INTEGER)
5753 {
5754 sgn = mpz_cmp_ui (iter->step->value.integer, 0);
5755 cmp = mpz_cmp (iter->end->value.integer, iter->start->value.integer);
5756 }
5757 else
5758 {
5759 sgn = mpfr_sgn (iter->step->value.real);
5760 cmp = mpfr_cmp (iter->end->value.real, iter->start->value.real);
5761 }
5762 if ((sgn > 0 && cmp < 0) || (sgn < 0 && cmp > 0))
5763 gfc_warning ("DO loop at %L will be executed zero times",
5764 &iter->step->where);
5765 }
5766
6de9cd9a
DN
5767 return SUCCESS;
5768}
5769
5770
640670c7
PT
5771/* Traversal function for find_forall_index. f == 2 signals that
5772 that variable itself is not to be checked - only the references. */
ac5ba373 5773
640670c7
PT
5774static bool
5775forall_index (gfc_expr *expr, gfc_symbol *sym, int *f)
ac5ba373 5776{
908a2235
PT
5777 if (expr->expr_type != EXPR_VARIABLE)
5778 return false;
5779
640670c7
PT
5780 /* A scalar assignment */
5781 if (!expr->ref || *f == 1)
ac5ba373 5782 {
640670c7
PT
5783 if (expr->symtree->n.sym == sym)
5784 return true;
5785 else
5786 return false;
5787 }
ac5ba373 5788
640670c7
PT
5789 if (*f == 2)
5790 *f = 1;
5791 return false;
5792}
ac5ba373 5793
ac5ba373 5794
640670c7
PT
5795/* Check whether the FORALL index appears in the expression or not.
5796 Returns SUCCESS if SYM is found in EXPR. */
ac5ba373 5797
17b1d2a0 5798gfc_try
640670c7
PT
5799find_forall_index (gfc_expr *expr, gfc_symbol *sym, int f)
5800{
5801 if (gfc_traverse_expr (expr, sym, forall_index, f))
5802 return SUCCESS;
5803 else
5804 return FAILURE;
ac5ba373
TS
5805}
5806
5807
1c54741a
SK
5808/* Resolve a list of FORALL iterators. The FORALL index-name is constrained
5809 to be a scalar INTEGER variable. The subscripts and stride are scalar
ac5ba373
TS
5810 INTEGERs, and if stride is a constant it must be nonzero.
5811 Furthermore "A subscript or stride in a forall-triplet-spec shall
5812 not contain a reference to any index-name in the
5813 forall-triplet-spec-list in which it appears." (7.5.4.1) */
6de9cd9a
DN
5814
5815static void
ac5ba373 5816resolve_forall_iterators (gfc_forall_iterator *it)
6de9cd9a 5817{
ac5ba373
TS
5818 gfc_forall_iterator *iter, *iter2;
5819
5820 for (iter = it; iter; iter = iter->next)
6de9cd9a
DN
5821 {
5822 if (gfc_resolve_expr (iter->var) == SUCCESS
1c54741a
SK
5823 && (iter->var->ts.type != BT_INTEGER || iter->var->rank != 0))
5824 gfc_error ("FORALL index-name at %L must be a scalar INTEGER",
6de9cd9a
DN
5825 &iter->var->where);
5826
5827 if (gfc_resolve_expr (iter->start) == SUCCESS
1c54741a
SK
5828 && (iter->start->ts.type != BT_INTEGER || iter->start->rank != 0))
5829 gfc_error ("FORALL start expression at %L must be a scalar INTEGER",
6de9cd9a
DN
5830 &iter->start->where);
5831 if (iter->var->ts.kind != iter->start->ts.kind)
5832 gfc_convert_type (iter->start, &iter->var->ts, 2);
5833
5834 if (gfc_resolve_expr (iter->end) == SUCCESS
1c54741a
SK
5835 && (iter->end->ts.type != BT_INTEGER || iter->end->rank != 0))
5836 gfc_error ("FORALL end expression at %L must be a scalar INTEGER",
6de9cd9a
DN
5837 &iter->end->where);
5838 if (iter->var->ts.kind != iter->end->ts.kind)
5839 gfc_convert_type (iter->end, &iter->var->ts, 2);
5840
1c54741a
SK
5841 if (gfc_resolve_expr (iter->stride) == SUCCESS)
5842 {
5843 if (iter->stride->ts.type != BT_INTEGER || iter->stride->rank != 0)
5844 gfc_error ("FORALL stride expression at %L must be a scalar %s",
edf1eac2 5845 &iter->stride->where, "INTEGER");
1c54741a
SK
5846
5847 if (iter->stride->expr_type == EXPR_CONSTANT
5848 && mpz_cmp_ui(iter->stride->value.integer, 0) == 0)
5849 gfc_error ("FORALL stride expression at %L cannot be zero",
5850 &iter->stride->where);
5851 }
6de9cd9a
DN
5852 if (iter->var->ts.kind != iter->stride->ts.kind)
5853 gfc_convert_type (iter->stride, &iter->var->ts, 2);
6de9cd9a 5854 }
ac5ba373
TS
5855
5856 for (iter = it; iter; iter = iter->next)
5857 for (iter2 = iter; iter2; iter2 = iter2->next)
5858 {
5859 if (find_forall_index (iter2->start,
640670c7 5860 iter->var->symtree->n.sym, 0) == SUCCESS
ac5ba373 5861 || find_forall_index (iter2->end,
640670c7 5862 iter->var->symtree->n.sym, 0) == SUCCESS
ac5ba373 5863 || find_forall_index (iter2->stride,
640670c7 5864 iter->var->symtree->n.sym, 0) == SUCCESS)
ac5ba373
TS
5865 gfc_error ("FORALL index '%s' may not appear in triplet "
5866 "specification at %L", iter->var->symtree->name,
5867 &iter2->start->where);
5868 }
6de9cd9a
DN
5869}
5870
5871
8451584a
EE
5872/* Given a pointer to a symbol that is a derived type, see if it's
5873 inaccessible, i.e. if it's defined in another module and the components are
5874 PRIVATE. The search is recursive if necessary. Returns zero if no
5875 inaccessible components are found, nonzero otherwise. */
5876
5877static int
5878derived_inaccessible (gfc_symbol *sym)
5879{
5880 gfc_component *c;
5881
3dbf6538 5882 if (sym->attr.use_assoc && sym->attr.private_comp)
8451584a
EE
5883 return 1;
5884
5885 for (c = sym->components; c; c = c->next)
5886 {
bc21d315 5887 if (c->ts.type == BT_DERIVED && derived_inaccessible (c->ts.u.derived))
edf1eac2 5888 return 1;
8451584a
EE
5889 }
5890
5891 return 0;
5892}
5893
5894
6de9cd9a
DN
5895/* Resolve the argument of a deallocate expression. The expression must be
5896 a pointer or a full array. */
5897
17b1d2a0 5898static gfc_try
edf1eac2 5899resolve_deallocate_expr (gfc_expr *e)
6de9cd9a
DN
5900{
5901 symbol_attribute attr;
f17facac 5902 int allocatable, pointer, check_intent_in;
6de9cd9a 5903 gfc_ref *ref;
cf2b3c22
TB
5904 gfc_symbol *sym;
5905 gfc_component *c;
6de9cd9a 5906
f17facac
TB
5907 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
5908 check_intent_in = 1;
5909
6de9cd9a
DN
5910 if (gfc_resolve_expr (e) == FAILURE)
5911 return FAILURE;
5912
6de9cd9a
DN
5913 if (e->expr_type != EXPR_VARIABLE)
5914 goto bad;
5915
cf2b3c22
TB
5916 sym = e->symtree->n.sym;
5917
5918 if (sym->ts.type == BT_CLASS)
5919 {
5920 allocatable = sym->ts.u.derived->components->attr.allocatable;
5921 pointer = sym->ts.u.derived->components->attr.pointer;
5922 }
5923 else
5924 {
5925 allocatable = sym->attr.allocatable;
5926 pointer = sym->attr.pointer;
5927 }
6de9cd9a 5928 for (ref = e->ref; ref; ref = ref->next)
f17facac
TB
5929 {
5930 if (pointer)
edf1eac2 5931 check_intent_in = 0;
6de9cd9a 5932
f17facac 5933 switch (ref->type)
edf1eac2
SK
5934 {
5935 case REF_ARRAY:
f17facac
TB
5936 if (ref->u.ar.type != AR_FULL)
5937 allocatable = 0;
5938 break;
6de9cd9a 5939
edf1eac2 5940 case REF_COMPONENT:
cf2b3c22
TB
5941 c = ref->u.c.component;
5942 if (c->ts.type == BT_CLASS)
5943 {
5944 allocatable = c->ts.u.derived->components->attr.allocatable;
5945 pointer = c->ts.u.derived->components->attr.pointer;
5946 }
5947 else
5948 {
5949 allocatable = c->attr.allocatable;
5950 pointer = c->attr.pointer;
5951 }
f17facac 5952 break;
6de9cd9a 5953
edf1eac2 5954 case REF_SUBSTRING:
f17facac
TB
5955 allocatable = 0;
5956 break;
edf1eac2 5957 }
f17facac
TB
5958 }
5959
5960 attr = gfc_expr_attr (e);
5961
5962 if (allocatable == 0 && attr.pointer == 0)
6de9cd9a
DN
5963 {
5964 bad:
3759634f
SK
5965 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
5966 &e->where);
6de9cd9a
DN
5967 }
5968
cf2b3c22 5969 if (check_intent_in && sym->attr.intent == INTENT_IN)
aa08038d 5970 {
f17facac 5971 gfc_error ("Cannot deallocate INTENT(IN) variable '%s' at %L",
cf2b3c22 5972 sym->name, &e->where);
aa08038d
EE
5973 return FAILURE;
5974 }
5975
cf2b3c22
TB
5976 if (e->ts.type == BT_CLASS)
5977 {
5978 /* Only deallocate the DATA component. */
5979 gfc_add_component_ref (e, "$data");
5980 }
5981
6de9cd9a
DN
5982 return SUCCESS;
5983}
5984
edf1eac2 5985
908a2235 5986/* Returns true if the expression e contains a reference to the symbol sym. */
77726571 5987static bool
908a2235 5988sym_in_expr (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
77726571 5989{
908a2235
PT
5990 if (e->expr_type == EXPR_VARIABLE && e->symtree->n.sym == sym)
5991 return true;
77726571 5992
908a2235
PT
5993 return false;
5994}
77726571 5995
a68ab351
JJ
5996bool
5997gfc_find_sym_in_expr (gfc_symbol *sym, gfc_expr *e)
908a2235
PT
5998{
5999 return gfc_traverse_expr (e, sym, sym_in_expr, 0);
77726571
PT
6000}
6001
6de9cd9a 6002
68577e56
EE
6003/* Given the expression node e for an allocatable/pointer of derived type to be
6004 allocated, get the expression node to be initialized afterwards (needed for
5046aff5
PT
6005 derived types with default initializers, and derived types with allocatable
6006 components that need nullification.) */
68577e56 6007
cf2b3c22
TB
6008gfc_expr *
6009gfc_expr_to_initialize (gfc_expr *e)
68577e56
EE
6010{
6011 gfc_expr *result;
6012 gfc_ref *ref;
6013 int i;
6014
6015 result = gfc_copy_expr (e);
6016
6017 /* Change the last array reference from AR_ELEMENT to AR_FULL. */
6018 for (ref = result->ref; ref; ref = ref->next)
6019 if (ref->type == REF_ARRAY && ref->next == NULL)
6020 {
edf1eac2 6021 ref->u.ar.type = AR_FULL;
68577e56 6022
edf1eac2
SK
6023 for (i = 0; i < ref->u.ar.dimen; i++)
6024 ref->u.ar.start[i] = ref->u.ar.end[i] = ref->u.ar.stride[i] = NULL;
68577e56 6025
edf1eac2
SK
6026 result->rank = ref->u.ar.dimen;
6027 break;
68577e56
EE
6028 }
6029
6030 return result;
6031}
6032
6033
8460475b
JW
6034/* Used in resolve_allocate_expr to check that a allocation-object and
6035 a source-expr are conformable. This does not catch all possible
6036 cases; in particular a runtime checking is needed. */
6037
6038static gfc_try
6039conformable_arrays (gfc_expr *e1, gfc_expr *e2)
6040{
6041 /* First compare rank. */
6042 if (e2->ref && e1->rank != e2->ref->u.ar.as->rank)
6043 {
6044 gfc_error ("Source-expr at %L must be scalar or have the "
6045 "same rank as the allocate-object at %L",
6046 &e1->where, &e2->where);
6047 return FAILURE;
6048 }
6049
6050 if (e1->shape)
6051 {
6052 int i;
6053 mpz_t s;
6054
6055 mpz_init (s);
6056
6057 for (i = 0; i < e1->rank; i++)
6058 {
6059 if (e2->ref->u.ar.end[i])
6060 {
6061 mpz_set (s, e2->ref->u.ar.end[i]->value.integer);
6062 mpz_sub (s, s, e2->ref->u.ar.start[i]->value.integer);
6063 mpz_add_ui (s, s, 1);
6064 }
6065 else
6066 {
6067 mpz_set (s, e2->ref->u.ar.start[i]->value.integer);
6068 }
6069
6070 if (mpz_cmp (e1->shape[i], s) != 0)
6071 {
6072 gfc_error ("Source-expr at %L and allocate-object at %L must "
6073 "have the same shape", &e1->where, &e2->where);
6074 mpz_clear (s);
6075 return FAILURE;
6076 }
6077 }
6078
6079 mpz_clear (s);
6080 }
6081
6082 return SUCCESS;
6083}
6084
6085
6de9cd9a
DN
6086/* Resolve the expression in an ALLOCATE statement, doing the additional
6087 checks to see whether the expression is OK or not. The expression must
6088 have a trailing array reference that gives the size of the array. */
6089
17b1d2a0 6090static gfc_try
edf1eac2 6091resolve_allocate_expr (gfc_expr *e, gfc_code *code)
6de9cd9a 6092{
d0a9804e 6093 int i, pointer, allocatable, dimension, check_intent_in, is_abstract;
6de9cd9a
DN
6094 symbol_attribute attr;
6095 gfc_ref *ref, *ref2;
6096 gfc_array_ref *ar;
77726571
PT
6097 gfc_symbol *sym;
6098 gfc_alloc *a;
cf2b3c22 6099 gfc_component *c;
6de9cd9a 6100
f17facac
TB
6101 /* Check INTENT(IN), unless the object is a sub-component of a pointer. */
6102 check_intent_in = 1;
6103
6de9cd9a
DN
6104 if (gfc_resolve_expr (e) == FAILURE)
6105 return FAILURE;
6106
6107 /* Make sure the expression is allocatable or a pointer. If it is
6108 pointer, the next-to-last reference must be a pointer. */
6109
6110 ref2 = NULL;
cf2b3c22
TB
6111 if (e->symtree)
6112 sym = e->symtree->n.sym;
6de9cd9a 6113
d0a9804e
TB
6114 /* Check whether ultimate component is abstract and CLASS. */
6115 is_abstract = 0;
6116
6de9cd9a
DN
6117 if (e->expr_type != EXPR_VARIABLE)
6118 {
6119 allocatable = 0;
6de9cd9a
DN
6120 attr = gfc_expr_attr (e);
6121 pointer = attr.pointer;
6122 dimension = attr.dimension;
6de9cd9a
DN
6123 }
6124 else
6125 {
cf2b3c22
TB
6126 if (sym->ts.type == BT_CLASS)
6127 {
6128 allocatable = sym->ts.u.derived->components->attr.allocatable;
6129 pointer = sym->ts.u.derived->components->attr.pointer;
6130 dimension = sym->ts.u.derived->components->attr.dimension;
d0a9804e 6131 is_abstract = sym->ts.u.derived->components->attr.abstract;
cf2b3c22
TB
6132 }
6133 else
6134 {
6135 allocatable = sym->attr.allocatable;
6136 pointer = sym->attr.pointer;
6137 dimension = sym->attr.dimension;
6138 }
6de9cd9a
DN
6139
6140 for (ref = e->ref; ref; ref2 = ref, ref = ref->next)
edf1eac2 6141 {
f17facac
TB
6142 if (pointer)
6143 check_intent_in = 0;
6de9cd9a 6144
f17facac
TB
6145 switch (ref->type)
6146 {
6147 case REF_ARRAY:
edf1eac2
SK
6148 if (ref->next != NULL)
6149 pointer = 0;
6150 break;
f17facac
TB
6151
6152 case REF_COMPONENT:
cf2b3c22
TB
6153 c = ref->u.c.component;
6154 if (c->ts.type == BT_CLASS)
6155 {
6156 allocatable = c->ts.u.derived->components->attr.allocatable;
6157 pointer = c->ts.u.derived->components->attr.pointer;
6158 dimension = c->ts.u.derived->components->attr.dimension;
d0a9804e 6159 is_abstract = c->ts.u.derived->components->attr.abstract;
cf2b3c22
TB
6160 }
6161 else
6162 {
6163 allocatable = c->attr.allocatable;
6164 pointer = c->attr.pointer;
6165 dimension = c->attr.dimension;
d0a9804e 6166 is_abstract = c->attr.abstract;
cf2b3c22 6167 }
edf1eac2 6168 break;
f17facac
TB
6169
6170 case REF_SUBSTRING:
edf1eac2
SK
6171 allocatable = 0;
6172 pointer = 0;
6173 break;
f17facac 6174 }
8e1f752a 6175 }
6de9cd9a
DN
6176 }
6177
6178 if (allocatable == 0 && pointer == 0)
6179 {
3759634f
SK
6180 gfc_error ("Allocate-object at %L must be ALLOCATABLE or a POINTER",
6181 &e->where);
6de9cd9a
DN
6182 return FAILURE;
6183 }
6184
8460475b
JW
6185 /* Some checks for the SOURCE tag. */
6186 if (code->expr3)
6187 {
6188 /* Check F03:C631. */
6189 if (!gfc_type_compatible (&e->ts, &code->expr3->ts))
6190 {
6191 gfc_error ("Type of entity at %L is type incompatible with "
6192 "source-expr at %L", &e->where, &code->expr3->where);
6193 return FAILURE;
6194 }
6195
6196 /* Check F03:C632 and restriction following Note 6.18. */
6197 if (code->expr3->rank > 0
6198 && conformable_arrays (code->expr3, e) == FAILURE)
6199 return FAILURE;
6200
6201 /* Check F03:C633. */
6202 if (code->expr3->ts.kind != e->ts.kind)
6203 {
6204 gfc_error ("The allocate-object at %L and the source-expr at %L "
6205 "shall have the same kind type parameter",
6206 &e->where, &code->expr3->where);
6207 return FAILURE;
6208 }
6209 }
6210 else if (is_abstract&& code->ext.alloc.ts.type == BT_UNKNOWN)
d0a9804e
TB
6211 {
6212 gcc_assert (e->ts.type == BT_CLASS);
6213 gfc_error ("Allocating %s of ABSTRACT base type at %L requires a "
6214 "type-spec or SOURCE=", sym->name, &e->where);
6215 return FAILURE;
6216 }
6217
cf2b3c22 6218 if (check_intent_in && sym->attr.intent == INTENT_IN)
aa08038d 6219 {
f17facac 6220 gfc_error ("Cannot allocate INTENT(IN) variable '%s' at %L",
cf2b3c22 6221 sym->name, &e->where);
aa08038d
EE
6222 return FAILURE;
6223 }
6224
2fbd4117 6225 if (pointer || dimension == 0)
6de9cd9a
DN
6226 return SUCCESS;
6227
6228 /* Make sure the next-to-last reference node is an array specification. */
6229
6230 if (ref2 == NULL || ref2->type != REF_ARRAY || ref2->u.ar.type == AR_FULL)
6231 {
6232 gfc_error ("Array specification required in ALLOCATE statement "
6233 "at %L", &e->where);
6234 return FAILURE;
6235 }
6236
6de9cd9a
DN
6237 /* Make sure that the array section reference makes sense in the
6238 context of an ALLOCATE specification. */
6239
6240 ar = &ref2->u.ar;
6241
6242 for (i = 0; i < ar->dimen; i++)
77726571
PT
6243 {
6244 if (ref2->u.ar.type == AR_ELEMENT)
6245 goto check_symbols;
6de9cd9a 6246
77726571
PT
6247 switch (ar->dimen_type[i])
6248 {
6249 case DIMEN_ELEMENT:
6de9cd9a
DN
6250 break;
6251
77726571
PT
6252 case DIMEN_RANGE:
6253 if (ar->start[i] != NULL
6254 && ar->end[i] != NULL
6255 && ar->stride[i] == NULL)
6256 break;
6de9cd9a 6257
77726571
PT
6258 /* Fall Through... */
6259
6260 case DIMEN_UNKNOWN:
6261 case DIMEN_VECTOR:
6262 gfc_error ("Bad array specification in ALLOCATE statement at %L",
6263 &e->where);
6264 return FAILURE;
6265 }
6266
6267check_symbols:
6268
cf2b3c22 6269 for (a = code->ext.alloc.list; a; a = a->next)
77726571
PT
6270 {
6271 sym = a->expr->symtree->n.sym;
25e8cb2e
PT
6272
6273 /* TODO - check derived type components. */
6168891d 6274 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
25e8cb2e
PT
6275 continue;
6276
a68ab351
JJ
6277 if ((ar->start[i] != NULL
6278 && gfc_find_sym_in_expr (sym, ar->start[i]))
6279 || (ar->end[i] != NULL
6280 && gfc_find_sym_in_expr (sym, ar->end[i])))
77726571 6281 {
df2fba9e 6282 gfc_error ("'%s' must not appear in the array specification at "
77726571
PT
6283 "%L in the same ALLOCATE statement where it is "
6284 "itself allocated", sym->name, &ar->where);
6285 return FAILURE;
6286 }
6287 }
6288 }
6de9cd9a
DN
6289
6290 return SUCCESS;
6291}
6292
b9332b09
PT
6293static void
6294resolve_allocate_deallocate (gfc_code *code, const char *fcn)
6295{
3759634f
SK
6296 gfc_expr *stat, *errmsg, *pe, *qe;
6297 gfc_alloc *a, *p, *q;
6298
a513927a 6299 stat = code->expr1 ? code->expr1 : NULL;
b9332b09 6300
3759634f 6301 errmsg = code->expr2 ? code->expr2 : NULL;
b9332b09 6302
3759634f
SK
6303 /* Check the stat variable. */
6304 if (stat)
b9332b09 6305 {
3759634f
SK
6306 if (stat->symtree->n.sym->attr.intent == INTENT_IN)
6307 gfc_error ("Stat-variable '%s' at %L cannot be INTENT(IN)",
6308 stat->symtree->n.sym->name, &stat->where);
6309
6310 if (gfc_pure (NULL) && gfc_impure_variable (stat->symtree->n.sym))
6311 gfc_error ("Illegal stat-variable at %L for a PURE procedure",
6312 &stat->where);
b9332b09 6313
6c145259
TK
6314 if ((stat->ts.type != BT_INTEGER
6315 && !(stat->ref && (stat->ref->type == REF_ARRAY
6316 || stat->ref->type == REF_COMPONENT)))
6317 || stat->rank > 0)
3759634f
SK
6318 gfc_error ("Stat-variable at %L must be a scalar INTEGER "
6319 "variable", &stat->where);
6320
cf2b3c22 6321 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6322 if (p->expr->symtree->n.sym->name == stat->symtree->n.sym->name)
6323 gfc_error ("Stat-variable at %L shall not be %sd within "
6324 "the same %s statement", &stat->where, fcn, fcn);
b9332b09
PT
6325 }
6326
3759634f
SK
6327 /* Check the errmsg variable. */
6328 if (errmsg)
6329 {
6330 if (!stat)
6331 gfc_warning ("ERRMSG at %L is useless without a STAT tag",
6332 &errmsg->where);
6333
6334 if (errmsg->symtree->n.sym->attr.intent == INTENT_IN)
6335 gfc_error ("Errmsg-variable '%s' at %L cannot be INTENT(IN)",
6336 errmsg->symtree->n.sym->name, &errmsg->where);
6337
6338 if (gfc_pure (NULL) && gfc_impure_variable (errmsg->symtree->n.sym))
6339 gfc_error ("Illegal errmsg-variable at %L for a PURE procedure",
6340 &errmsg->where);
6341
6c145259
TK
6342 if ((errmsg->ts.type != BT_CHARACTER
6343 && !(errmsg->ref
6344 && (errmsg->ref->type == REF_ARRAY
6345 || errmsg->ref->type == REF_COMPONENT)))
6346 || errmsg->rank > 0 )
3759634f
SK
6347 gfc_error ("Errmsg-variable at %L must be a scalar CHARACTER "
6348 "variable", &errmsg->where);
6349
cf2b3c22 6350 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6351 if (p->expr->symtree->n.sym->name == errmsg->symtree->n.sym->name)
6352 gfc_error ("Errmsg-variable at %L shall not be %sd within "
6353 "the same %s statement", &errmsg->where, fcn, fcn);
6354 }
6355
6356 /* Check that an allocate-object appears only once in the statement.
6357 FIXME: Checking derived types is disabled. */
cf2b3c22 6358 for (p = code->ext.alloc.list; p; p = p->next)
3759634f
SK
6359 {
6360 pe = p->expr;
6361 if ((pe->ref && pe->ref->type != REF_COMPONENT)
6362 && (pe->symtree->n.sym->ts.type != BT_DERIVED))
6363 {
6364 for (q = p->next; q; q = q->next)
6365 {
6366 qe = q->expr;
6367 if ((qe->ref && qe->ref->type != REF_COMPONENT)
6368 && (qe->symtree->n.sym->ts.type != BT_DERIVED)
6369 && (pe->symtree->n.sym->name == qe->symtree->n.sym->name))
6370 gfc_error ("Allocate-object at %L also appears at %L",
6371 &pe->where, &qe->where);
6372 }
6373 }
6374 }
b9332b09
PT
6375
6376 if (strcmp (fcn, "ALLOCATE") == 0)
6377 {
cf2b3c22 6378 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
6379 resolve_allocate_expr (a->expr, code);
6380 }
6381 else
6382 {
cf2b3c22 6383 for (a = code->ext.alloc.list; a; a = a->next)
b9332b09
PT
6384 resolve_deallocate_expr (a->expr);
6385 }
6386}
6de9cd9a 6387
3759634f 6388
6de9cd9a
DN
6389/************ SELECT CASE resolution subroutines ************/
6390
6391/* Callback function for our mergesort variant. Determines interval
6392 overlaps for CASEs. Return <0 if op1 < op2, 0 for overlap, >0 for
c224550f
SK
6393 op1 > op2. Assumes we're not dealing with the default case.
6394 We have op1 = (:L), (K:L) or (K:) and op2 = (:N), (M:N) or (M:).
6395 There are nine situations to check. */
6de9cd9a
DN
6396
6397static int
edf1eac2 6398compare_cases (const gfc_case *op1, const gfc_case *op2)
6de9cd9a 6399{
c224550f 6400 int retval;
6de9cd9a 6401
c224550f 6402 if (op1->low == NULL) /* op1 = (:L) */
6de9cd9a 6403 {
c224550f
SK
6404 /* op2 = (:N), so overlap. */
6405 retval = 0;
6406 /* op2 = (M:) or (M:N), L < M */
6407 if (op2->low != NULL
7b4c5f8b 6408 && gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 6409 retval = -1;
6de9cd9a 6410 }
c224550f 6411 else if (op1->high == NULL) /* op1 = (K:) */
6de9cd9a 6412 {
c224550f
SK
6413 /* op2 = (M:), so overlap. */
6414 retval = 0;
6415 /* op2 = (:N) or (M:N), K > N */
6416 if (op2->high != NULL
7b4c5f8b 6417 && gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 6418 retval = 1;
6de9cd9a 6419 }
c224550f 6420 else /* op1 = (K:L) */
6de9cd9a 6421 {
c224550f 6422 if (op2->low == NULL) /* op2 = (:N), K > N */
7b4c5f8b
TB
6423 retval = (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
6424 ? 1 : 0;
c224550f 6425 else if (op2->high == NULL) /* op2 = (M:), L < M */
7b4c5f8b
TB
6426 retval = (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
6427 ? -1 : 0;
edf1eac2
SK
6428 else /* op2 = (M:N) */
6429 {
c224550f 6430 retval = 0;
edf1eac2 6431 /* L < M */
7b4c5f8b 6432 if (gfc_compare_expr (op1->high, op2->low, INTRINSIC_LT) < 0)
c224550f 6433 retval = -1;
edf1eac2 6434 /* K > N */
7b4c5f8b 6435 else if (gfc_compare_expr (op1->low, op2->high, INTRINSIC_GT) > 0)
c224550f 6436 retval = 1;
6de9cd9a
DN
6437 }
6438 }
c224550f
SK
6439
6440 return retval;
6de9cd9a
DN
6441}
6442
6443
6444/* Merge-sort a double linked case list, detecting overlap in the
6445 process. LIST is the head of the double linked case list before it
6446 is sorted. Returns the head of the sorted list if we don't see any
6447 overlap, or NULL otherwise. */
6448
6449static gfc_case *
edf1eac2 6450check_case_overlap (gfc_case *list)
6de9cd9a
DN
6451{
6452 gfc_case *p, *q, *e, *tail;
6453 int insize, nmerges, psize, qsize, cmp, overlap_seen;
6454
6455 /* If the passed list was empty, return immediately. */
6456 if (!list)
6457 return NULL;
6458
6459 overlap_seen = 0;
6460 insize = 1;
6461
6462 /* Loop unconditionally. The only exit from this loop is a return
6463 statement, when we've finished sorting the case list. */
6464 for (;;)
6465 {
6466 p = list;
6467 list = NULL;
6468 tail = NULL;
6469
6470 /* Count the number of merges we do in this pass. */
6471 nmerges = 0;
6472
6473 /* Loop while there exists a merge to be done. */
6474 while (p)
6475 {
6476 int i;
6477
6478 /* Count this merge. */
6479 nmerges++;
6480
5352b89f 6481 /* Cut the list in two pieces by stepping INSIZE places
edf1eac2 6482 forward in the list, starting from P. */
6de9cd9a
DN
6483 psize = 0;
6484 q = p;
6485 for (i = 0; i < insize; i++)
6486 {
6487 psize++;
6488 q = q->right;
6489 if (!q)
6490 break;
6491 }
6492 qsize = insize;
6493
6494 /* Now we have two lists. Merge them! */
6495 while (psize > 0 || (qsize > 0 && q != NULL))
6496 {
6de9cd9a
DN
6497 /* See from which the next case to merge comes from. */
6498 if (psize == 0)
6499 {
6500 /* P is empty so the next case must come from Q. */
6501 e = q;
6502 q = q->right;
6503 qsize--;
6504 }
6505 else if (qsize == 0 || q == NULL)
6506 {
6507 /* Q is empty. */
6508 e = p;
6509 p = p->right;
6510 psize--;
6511 }
6512 else
6513 {
6514 cmp = compare_cases (p, q);
6515 if (cmp < 0)
6516 {
6517 /* The whole case range for P is less than the
edf1eac2 6518 one for Q. */
6de9cd9a
DN
6519 e = p;
6520 p = p->right;
6521 psize--;
6522 }
6523 else if (cmp > 0)
6524 {
6525 /* The whole case range for Q is greater than
edf1eac2 6526 the case range for P. */
6de9cd9a
DN
6527 e = q;
6528 q = q->right;
6529 qsize--;
6530 }
6531 else
6532 {
6533 /* The cases overlap, or they are the same
6534 element in the list. Either way, we must
6535 issue an error and get the next case from P. */
6536 /* FIXME: Sort P and Q by line number. */
6537 gfc_error ("CASE label at %L overlaps with CASE "
6538 "label at %L", &p->where, &q->where);
6539 overlap_seen = 1;
6540 e = p;
6541 p = p->right;
6542 psize--;
6543 }
6544 }
6545
6546 /* Add the next element to the merged list. */
6547 if (tail)
6548 tail->right = e;
6549 else
6550 list = e;
6551 e->left = tail;
6552 tail = e;
6553 }
6554
6555 /* P has now stepped INSIZE places along, and so has Q. So
edf1eac2 6556 they're the same. */
6de9cd9a
DN
6557 p = q;
6558 }
6559 tail->right = NULL;
6560
6561 /* If we have done only one merge or none at all, we've
edf1eac2 6562 finished sorting the cases. */
6de9cd9a 6563 if (nmerges <= 1)
edf1eac2 6564 {
6de9cd9a
DN
6565 if (!overlap_seen)
6566 return list;
6567 else
6568 return NULL;
6569 }
6570
6571 /* Otherwise repeat, merging lists twice the size. */
6572 insize *= 2;
6573 }
6574}
6575
6576
5352b89f
SK
6577/* Check to see if an expression is suitable for use in a CASE statement.
6578 Makes sure that all case expressions are scalar constants of the same
6579 type. Return FAILURE if anything is wrong. */
6de9cd9a 6580
17b1d2a0 6581static gfc_try
edf1eac2 6582validate_case_label_expr (gfc_expr *e, gfc_expr *case_expr)
6de9cd9a 6583{
6de9cd9a
DN
6584 if (e == NULL) return SUCCESS;
6585
5352b89f 6586 if (e->ts.type != case_expr->ts.type)
6de9cd9a
DN
6587 {
6588 gfc_error ("Expression in CASE statement at %L must be of type %s",
5352b89f 6589 &e->where, gfc_basic_typename (case_expr->ts.type));
6de9cd9a
DN
6590 return FAILURE;
6591 }
6592
5352b89f
SK
6593 /* C805 (R808) For a given case-construct, each case-value shall be of
6594 the same type as case-expr. For character type, length differences
6595 are allowed, but the kind type parameters shall be the same. */
6596
6597 if (case_expr->ts.type == BT_CHARACTER && e->ts.kind != case_expr->ts.kind)
6de9cd9a 6598 {
d393bbd7
FXC
6599 gfc_error ("Expression in CASE statement at %L must be of kind %d",
6600 &e->where, case_expr->ts.kind);
6de9cd9a
DN
6601 return FAILURE;
6602 }
6603
5352b89f
SK
6604 /* Convert the case value kind to that of case expression kind, if needed.
6605 FIXME: Should a warning be issued? */
6606 if (e->ts.kind != case_expr->ts.kind)
6607 gfc_convert_type_warn (e, &case_expr->ts, 2, 0);
6608
6de9cd9a
DN
6609 if (e->rank != 0)
6610 {
6611 gfc_error ("Expression in CASE statement at %L must be scalar",
6612 &e->where);
6613 return FAILURE;
6614 }
6615
6616 return SUCCESS;
6617}
6618
6619
6620/* Given a completely parsed select statement, we:
6621
6622 - Validate all expressions and code within the SELECT.
6623 - Make sure that the selection expression is not of the wrong type.
6624 - Make sure that no case ranges overlap.
6625 - Eliminate unreachable cases and unreachable code resulting from
6626 removing case labels.
6627
6628 The standard does allow unreachable cases, e.g. CASE (5:3). But
6629 they are a hassle for code generation, and to prevent that, we just
6630 cut them out here. This is not necessary for overlapping cases
6631 because they are illegal and we never even try to generate code.
6632
6633 We have the additional caveat that a SELECT construct could have
1f2959f0 6634 been a computed GOTO in the source code. Fortunately we can fairly
6de9cd9a
DN
6635 easily work around that here: The case_expr for a "real" SELECT CASE
6636 is in code->expr1, but for a computed GOTO it is in code->expr2. All
6637 we have to do is make sure that the case_expr is a scalar integer
6638 expression. */
6639
6640static void
edf1eac2 6641resolve_select (gfc_code *code)
6de9cd9a
DN
6642{
6643 gfc_code *body;
6644 gfc_expr *case_expr;
6645 gfc_case *cp, *default_case, *tail, *head;
6646 int seen_unreachable;
d68bd5a8 6647 int seen_logical;
6de9cd9a
DN
6648 int ncases;
6649 bt type;
17b1d2a0 6650 gfc_try t;
6de9cd9a 6651
a513927a 6652 if (code->expr1 == NULL)
6de9cd9a
DN
6653 {
6654 /* This was actually a computed GOTO statement. */
6655 case_expr = code->expr2;
edf1eac2 6656 if (case_expr->ts.type != BT_INTEGER|| case_expr->rank != 0)
6de9cd9a
DN
6657 gfc_error ("Selection expression in computed GOTO statement "
6658 "at %L must be a scalar integer expression",
6659 &case_expr->where);
6660
6661 /* Further checking is not necessary because this SELECT was built
6662 by the compiler, so it should always be OK. Just move the
6663 case_expr from expr2 to expr so that we can handle computed
6664 GOTOs as normal SELECTs from here on. */
a513927a 6665 code->expr1 = code->expr2;
6de9cd9a
DN
6666 code->expr2 = NULL;
6667 return;
6668 }
6669
a513927a 6670 case_expr = code->expr1;
6de9cd9a
DN
6671
6672 type = case_expr->ts.type;
6673 if (type != BT_LOGICAL && type != BT_INTEGER && type != BT_CHARACTER)
6674 {
6675 gfc_error ("Argument of SELECT statement at %L cannot be %s",
6676 &case_expr->where, gfc_typename (&case_expr->ts));
6677
6678 /* Punt. Going on here just produce more garbage error messages. */
6679 return;
6680 }
6681
6682 if (case_expr->rank != 0)
6683 {
6684 gfc_error ("Argument of SELECT statement at %L must be a scalar "
6685 "expression", &case_expr->where);
6686
6687 /* Punt. */
6688 return;
6689 }
6690
5352b89f
SK
6691 /* PR 19168 has a long discussion concerning a mismatch of the kinds
6692 of the SELECT CASE expression and its CASE values. Walk the lists
6693 of case values, and if we find a mismatch, promote case_expr to
6694 the appropriate kind. */
6695
6696 if (type == BT_LOGICAL || type == BT_INTEGER)
6697 {
6698 for (body = code->block; body; body = body->block)
6699 {
6700 /* Walk the case label list. */
6701 for (cp = body->ext.case_list; cp; cp = cp->next)
6702 {
6703 /* Intercept the DEFAULT case. It does not have a kind. */
6704 if (cp->low == NULL && cp->high == NULL)
6705 continue;
6706
05c1e3a7 6707 /* Unreachable case ranges are discarded, so ignore. */
5352b89f
SK
6708 if (cp->low != NULL && cp->high != NULL
6709 && cp->low != cp->high
7b4c5f8b 6710 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
5352b89f
SK
6711 continue;
6712
6713 /* FIXME: Should a warning be issued? */
6714 if (cp->low != NULL
6715 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->low))
6716 gfc_convert_type_warn (case_expr, &cp->low->ts, 2, 0);
6717
6718 if (cp->high != NULL
6719 && case_expr->ts.kind != gfc_kind_max(case_expr, cp->high))
05c1e3a7 6720 gfc_convert_type_warn (case_expr, &cp->high->ts, 2, 0);
5352b89f
SK
6721 }
6722 }
6723 }
6724
6de9cd9a
DN
6725 /* Assume there is no DEFAULT case. */
6726 default_case = NULL;
6727 head = tail = NULL;
6728 ncases = 0;
d68bd5a8 6729 seen_logical = 0;
6de9cd9a
DN
6730
6731 for (body = code->block; body; body = body->block)
6732 {
6733 /* Assume the CASE list is OK, and all CASE labels can be matched. */
6734 t = SUCCESS;
6735 seen_unreachable = 0;
6736
6737 /* Walk the case label list, making sure that all case labels
edf1eac2 6738 are legal. */
6de9cd9a
DN
6739 for (cp = body->ext.case_list; cp; cp = cp->next)
6740 {
6741 /* Count the number of cases in the whole construct. */
6742 ncases++;
6743
6744 /* Intercept the DEFAULT case. */
6745 if (cp->low == NULL && cp->high == NULL)
6746 {
6747 if (default_case != NULL)
edf1eac2 6748 {
6de9cd9a
DN
6749 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6750 "by a second DEFAULT CASE at %L",
6751 &default_case->where, &cp->where);
6752 t = FAILURE;
6753 break;
6754 }
6755 else
6756 {
6757 default_case = cp;
6758 continue;
6759 }
6760 }
6761
6762 /* Deal with single value cases and case ranges. Errors are
edf1eac2 6763 issued from the validation function. */
6de9cd9a
DN
6764 if(validate_case_label_expr (cp->low, case_expr) != SUCCESS
6765 || validate_case_label_expr (cp->high, case_expr) != SUCCESS)
6766 {
6767 t = FAILURE;
6768 break;
6769 }
6770
6771 if (type == BT_LOGICAL
6772 && ((cp->low == NULL || cp->high == NULL)
6773 || cp->low != cp->high))
6774 {
edf1eac2
SK
6775 gfc_error ("Logical range in CASE statement at %L is not "
6776 "allowed", &cp->low->where);
6de9cd9a
DN
6777 t = FAILURE;
6778 break;
6779 }
6780
d68bd5a8
PT
6781 if (type == BT_LOGICAL && cp->low->expr_type == EXPR_CONSTANT)
6782 {
6783 int value;
6784 value = cp->low->value.logical == 0 ? 2 : 1;
6785 if (value & seen_logical)
6786 {
6787 gfc_error ("constant logical value in CASE statement "
6788 "is repeated at %L",
6789 &cp->low->where);
6790 t = FAILURE;
6791 break;
6792 }
6793 seen_logical |= value;
6794 }
6795
6de9cd9a
DN
6796 if (cp->low != NULL && cp->high != NULL
6797 && cp->low != cp->high
7b4c5f8b 6798 && gfc_compare_expr (cp->low, cp->high, INTRINSIC_GT) > 0)
6de9cd9a
DN
6799 {
6800 if (gfc_option.warn_surprising)
edf1eac2 6801 gfc_warning ("Range specification at %L can never "
6de9cd9a
DN
6802 "be matched", &cp->where);
6803
6804 cp->unreachable = 1;
6805 seen_unreachable = 1;
6806 }
6807 else
6808 {
6809 /* If the case range can be matched, it can also overlap with
6810 other cases. To make sure it does not, we put it in a
6811 double linked list here. We sort that with a merge sort
6812 later on to detect any overlapping cases. */
6813 if (!head)
edf1eac2 6814 {
6de9cd9a
DN
6815 head = tail = cp;
6816 head->right = head->left = NULL;
6817 }
6818 else
edf1eac2 6819 {
6de9cd9a
DN
6820 tail->right = cp;
6821 tail->right->left = tail;
6822 tail = tail->right;
6823 tail->right = NULL;
6824 }
6825 }
6826 }
6827
6828 /* It there was a failure in the previous case label, give up
6829 for this case label list. Continue with the next block. */
6830 if (t == FAILURE)
6831 continue;
6832
6833 /* See if any case labels that are unreachable have been seen.
6834 If so, we eliminate them. This is a bit of a kludge because
6835 the case lists for a single case statement (label) is a
6836 single forward linked lists. */
6837 if (seen_unreachable)
6838 {
6839 /* Advance until the first case in the list is reachable. */
6840 while (body->ext.case_list != NULL
6841 && body->ext.case_list->unreachable)
6842 {
6843 gfc_case *n = body->ext.case_list;
6844 body->ext.case_list = body->ext.case_list->next;
6845 n->next = NULL;
6846 gfc_free_case_list (n);
6847 }
6848
6849 /* Strip all other unreachable cases. */
6850 if (body->ext.case_list)
6851 {
6852 for (cp = body->ext.case_list; cp->next; cp = cp->next)
6853 {
6854 if (cp->next->unreachable)
6855 {
6856 gfc_case *n = cp->next;
6857 cp->next = cp->next->next;
6858 n->next = NULL;
6859 gfc_free_case_list (n);
6860 }
6861 }
6862 }
6863 }
6864 }
6865
6866 /* See if there were overlapping cases. If the check returns NULL,
6867 there was overlap. In that case we don't do anything. If head
6868 is non-NULL, we prepend the DEFAULT case. The sorted list can
6869 then used during code generation for SELECT CASE constructs with
6870 a case expression of a CHARACTER type. */
6871 if (head)
6872 {
6873 head = check_case_overlap (head);
6874
6875 /* Prepend the default_case if it is there. */
6876 if (head != NULL && default_case)
6877 {
6878 default_case->left = NULL;
6879 default_case->right = head;
6880 head->left = default_case;
6881 }
6882 }
6883
6884 /* Eliminate dead blocks that may be the result if we've seen
6885 unreachable case labels for a block. */
6886 for (body = code; body && body->block; body = body->block)
6887 {
6888 if (body->block->ext.case_list == NULL)
edf1eac2 6889 {
6de9cd9a
DN
6890 /* Cut the unreachable block from the code chain. */
6891 gfc_code *c = body->block;
6892 body->block = c->block;
6893
6894 /* Kill the dead block, but not the blocks below it. */
6895 c->block = NULL;
6896 gfc_free_statements (c);
edf1eac2 6897 }
6de9cd9a
DN
6898 }
6899
6900 /* More than two cases is legal but insane for logical selects.
6901 Issue a warning for it. */
6902 if (gfc_option.warn_surprising && type == BT_LOGICAL
6903 && ncases > 2)
6904 gfc_warning ("Logical SELECT CASE block at %L has more that two cases",
6905 &code->loc);
6906}
6907
6908
cf2b3c22
TB
6909/* Check if a derived type is extensible. */
6910
6911bool
6912gfc_type_is_extensible (gfc_symbol *sym)
6913{
6914 return !(sym->attr.is_bind_c || sym->attr.sequence);
6915}
6916
6917
6918/* Resolve a SELECT TYPE statement. */
6919
6920static void
6921resolve_select_type (gfc_code *code)
6922{
6923 gfc_symbol *selector_type;
7c1dab0d
JW
6924 gfc_code *body, *new_st, *if_st, *tail;
6925 gfc_code *class_is = NULL, *default_case = NULL;
6926 gfc_case *c;
cf2b3c22
TB
6927 gfc_symtree *st;
6928 char name[GFC_MAX_SYMBOL_LEN];
93d76687 6929 gfc_namespace *ns;
7c1dab0d 6930 int error = 0;
93d76687
JW
6931
6932 ns = code->ext.ns;
6933 gfc_resolve (ns);
cf2b3c22 6934
93d76687
JW
6935 if (code->expr2)
6936 selector_type = code->expr2->ts.u.derived->components->ts.u.derived;
6937 else
6938 selector_type = code->expr1->ts.u.derived->components->ts.u.derived;
cf2b3c22 6939
cf2b3c22
TB
6940 /* Loop over TYPE IS / CLASS IS cases. */
6941 for (body = code->block; body; body = body->block)
6942 {
6943 c = body->ext.case_list;
6944
6945 /* Check F03:C815. */
6946 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6947 && !gfc_type_is_extensible (c->ts.u.derived))
6948 {
6949 gfc_error ("Derived type '%s' at %L must be extensible",
6950 c->ts.u.derived->name, &c->where);
7c1dab0d 6951 error++;
cf2b3c22
TB
6952 continue;
6953 }
6954
6955 /* Check F03:C816. */
6956 if ((c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
6957 && !gfc_type_is_extension_of (selector_type, c->ts.u.derived))
6958 {
6959 gfc_error ("Derived type '%s' at %L must be an extension of '%s'",
6960 c->ts.u.derived->name, &c->where, selector_type->name);
7c1dab0d 6961 error++;
cf2b3c22
TB
6962 continue;
6963 }
6964
6965 /* Intercept the DEFAULT case. */
6966 if (c->ts.type == BT_UNKNOWN)
6967 {
6968 /* Check F03:C818. */
7c1dab0d
JW
6969 if (default_case)
6970 {
6971 gfc_error ("The DEFAULT CASE at %L cannot be followed "
6972 "by a second DEFAULT CASE at %L",
6973 &default_case->ext.case_list->where, &c->where);
6974 error++;
6975 continue;
6976 }
cf2b3c22 6977 else
7c1dab0d 6978 default_case = body;
cf2b3c22
TB
6979 }
6980 }
7c1dab0d
JW
6981
6982 if (error>0)
6983 return;
cf2b3c22 6984
93d76687
JW
6985 if (code->expr2)
6986 {
6987 /* Insert assignment for selector variable. */
6988 new_st = gfc_get_code ();
6989 new_st->op = EXEC_ASSIGN;
6990 new_st->expr1 = gfc_copy_expr (code->expr1);
6991 new_st->expr2 = gfc_copy_expr (code->expr2);
6992 ns->code = new_st;
6993 }
6994
6995 /* Put SELECT TYPE statement inside a BLOCK. */
6996 new_st = gfc_get_code ();
6997 new_st->op = code->op;
6998 new_st->expr1 = code->expr1;
6999 new_st->expr2 = code->expr2;
7000 new_st->block = code->block;
7001 if (!ns->code)
7002 ns->code = new_st;
7003 else
7004 ns->code->next = new_st;
7005 code->op = EXEC_BLOCK;
7006 code->expr1 = code->expr2 = NULL;
7007 code->block = NULL;
7008
7009 code = new_st;
7010
cf2b3c22
TB
7011 /* Transform to EXEC_SELECT. */
7012 code->op = EXEC_SELECT;
7c1dab0d
JW
7013 gfc_add_component_ref (code->expr1, "$vptr");
7014 gfc_add_component_ref (code->expr1, "$hash");
cf2b3c22
TB
7015
7016 /* Loop over TYPE IS / CLASS IS cases. */
7017 for (body = code->block; body; body = body->block)
7018 {
7019 c = body->ext.case_list;
7c1dab0d 7020
cf2b3c22 7021 if (c->ts.type == BT_DERIVED)
7c1dab0d
JW
7022 c->low = c->high = gfc_int_expr (c->ts.u.derived->hash_value);
7023 else if (c->ts.type == BT_UNKNOWN)
cf2b3c22 7024 continue;
7c1dab0d 7025
cf2b3c22 7026 /* Assign temporary to selector. */
7c1dab0d
JW
7027 if (c->ts.type == BT_CLASS)
7028 sprintf (name, "tmp$class$%s", c->ts.u.derived->name);
7029 else
7030 sprintf (name, "tmp$type$%s", c->ts.u.derived->name);
93d76687 7031 st = gfc_find_symtree (ns->sym_root, name);
cf2b3c22 7032 new_st = gfc_get_code ();
cf2b3c22
TB
7033 new_st->expr1 = gfc_get_variable_expr (st);
7034 new_st->expr2 = gfc_get_variable_expr (code->expr1->symtree);
7c1dab0d
JW
7035 if (c->ts.type == BT_DERIVED)
7036 {
7037 new_st->op = EXEC_POINTER_ASSIGN;
7038 gfc_add_component_ref (new_st->expr2, "$data");
7039 }
7040 else
7041 new_st->op = EXEC_POINTER_ASSIGN;
cf2b3c22
TB
7042 new_st->next = body->next;
7043 body->next = new_st;
7044 }
7c1dab0d
JW
7045
7046 /* Take out CLASS IS cases for separate treatment. */
7047 body = code;
7048 while (body && body->block)
7049 {
7050 if (body->block->ext.case_list->ts.type == BT_CLASS)
7051 {
7052 /* Add to class_is list. */
7053 if (class_is == NULL)
7054 {
7055 class_is = body->block;
7056 tail = class_is;
7057 }
7058 else
7059 {
7060 for (tail = class_is; tail->block; tail = tail->block) ;
7061 tail->block = body->block;
7062 tail = tail->block;
7063 }
7064 /* Remove from EXEC_SELECT list. */
7065 body->block = body->block->block;
7066 tail->block = NULL;
7067 }
7068 else
7069 body = body->block;
7070 }
cf2b3c22 7071
7c1dab0d 7072 if (class_is)
cf2b3c22 7073 {
7c1dab0d
JW
7074 gfc_symbol *vtab;
7075
7076 if (!default_case)
7077 {
7078 /* Add a default case to hold the CLASS IS cases. */
7079 for (tail = code; tail->block; tail = tail->block) ;
7080 tail->block = gfc_get_code ();
7081 tail = tail->block;
7082 tail->op = EXEC_SELECT_TYPE;
7083 tail->ext.case_list = gfc_get_case ();
7084 tail->ext.case_list->ts.type = BT_UNKNOWN;
7085 tail->next = NULL;
7086 default_case = tail;
7087 }
7088
7089 /* More than one CLASS IS block? */
7090 if (class_is->block)
cf2b3c22 7091 {
7c1dab0d
JW
7092 gfc_code **c1,*c2;
7093 bool swapped;
7094 /* Sort CLASS IS blocks by extension level. */
7095 do
7096 {
7097 swapped = false;
7098 for (c1 = &class_is; (*c1) && (*c1)->block; c1 = &((*c1)->block))
7099 {
7100 c2 = (*c1)->block;
7101 /* F03:C817 (check for doubles). */
7102 if ((*c1)->ext.case_list->ts.u.derived->hash_value
7103 == c2->ext.case_list->ts.u.derived->hash_value)
7104 {
7105 gfc_error ("Double CLASS IS block in SELECT TYPE "
7106 "statement at %L", &c2->ext.case_list->where);
7107 return;
7108 }
7109 if ((*c1)->ext.case_list->ts.u.derived->attr.extension
7110 < c2->ext.case_list->ts.u.derived->attr.extension)
7111 {
7112 /* Swap. */
7113 (*c1)->block = c2->block;
7114 c2->block = *c1;
7115 *c1 = c2;
7116 swapped = true;
7117 }
7118 }
7119 }
7120 while (swapped);
cf2b3c22 7121 }
7c1dab0d
JW
7122
7123 /* Generate IF chain. */
7124 if_st = gfc_get_code ();
7125 if_st->op = EXEC_IF;
7126 new_st = if_st;
7127 for (body = class_is; body; body = body->block)
7128 {
7129 new_st->block = gfc_get_code ();
7130 new_st = new_st->block;
7131 new_st->op = EXEC_IF;
7132 /* Set up IF condition: Call _gfortran_is_extension_of. */
7133 new_st->expr1 = gfc_get_expr ();
7134 new_st->expr1->expr_type = EXPR_FUNCTION;
7135 new_st->expr1->ts.type = BT_LOGICAL;
7136 new_st->expr1->ts.kind = 4;
7137 new_st->expr1->value.function.name = gfc_get_string (PREFIX ("is_extension_of"));
7138 new_st->expr1->value.function.isym = XCNEW (gfc_intrinsic_sym);
7139 new_st->expr1->value.function.isym->id = GFC_ISYM_EXTENDS_TYPE_OF;
7140 /* Set up arguments. */
7141 new_st->expr1->value.function.actual = gfc_get_actual_arglist ();
7142 new_st->expr1->value.function.actual->expr = gfc_get_variable_expr (code->expr1->symtree);
7143 gfc_add_component_ref (new_st->expr1->value.function.actual->expr, "$vptr");
7144 vtab = gfc_find_derived_vtab (body->ext.case_list->ts.u.derived);
7145 st = gfc_find_symtree (vtab->ns->sym_root, vtab->name);
7146 new_st->expr1->value.function.actual->next = gfc_get_actual_arglist ();
7147 new_st->expr1->value.function.actual->next->expr = gfc_get_variable_expr (st);
7148 new_st->next = body->next;
7149 }
7150 if (default_case->next)
7151 {
7152 new_st->block = gfc_get_code ();
7153 new_st = new_st->block;
7154 new_st->op = EXEC_IF;
7155 new_st->next = default_case->next;
7156 }
7157
7158 /* Replace CLASS DEFAULT code by the IF chain. */
7159 default_case->next = if_st;
cf2b3c22
TB
7160 }
7161
7162 resolve_select (code);
7163
7164}
7165
7166
0e6928d8
TS
7167/* Resolve a transfer statement. This is making sure that:
7168 -- a derived type being transferred has only non-pointer components
8451584a
EE
7169 -- a derived type being transferred doesn't have private components, unless
7170 it's being transferred from the module where the type was defined
0e6928d8
TS
7171 -- we're not trying to transfer a whole assumed size array. */
7172
7173static void
edf1eac2 7174resolve_transfer (gfc_code *code)
0e6928d8
TS
7175{
7176 gfc_typespec *ts;
7177 gfc_symbol *sym;
7178 gfc_ref *ref;
7179 gfc_expr *exp;
7180
a513927a 7181 exp = code->expr1;
0e6928d8 7182
edf1eac2 7183 if (exp->expr_type != EXPR_VARIABLE && exp->expr_type != EXPR_FUNCTION)
0e6928d8
TS
7184 return;
7185
7186 sym = exp->symtree->n.sym;
7187 ts = &sym->ts;
7188
7189 /* Go to actual component transferred. */
a513927a 7190 for (ref = code->expr1->ref; ref; ref = ref->next)
0e6928d8
TS
7191 if (ref->type == REF_COMPONENT)
7192 ts = &ref->u.c.component->ts;
7193
7194 if (ts->type == BT_DERIVED)
7195 {
7196 /* Check that transferred derived type doesn't contain POINTER
7197 components. */
bc21d315 7198 if (ts->u.derived->attr.pointer_comp)
0e6928d8
TS
7199 {
7200 gfc_error ("Data transfer element at %L cannot have "
7201 "POINTER components", &code->loc);
7202 return;
7203 }
7204
bc21d315 7205 if (ts->u.derived->attr.alloc_comp)
5046aff5
PT
7206 {
7207 gfc_error ("Data transfer element at %L cannot have "
7208 "ALLOCATABLE components", &code->loc);
7209 return;
7210 }
7211
bc21d315 7212 if (derived_inaccessible (ts->u.derived))
0e6928d8
TS
7213 {
7214 gfc_error ("Data transfer element at %L cannot have "
7215 "PRIVATE components",&code->loc);
7216 return;
7217 }
7218 }
7219
7220 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SIZE
7221 && exp->ref->type == REF_ARRAY && exp->ref->u.ar.type == AR_FULL)
7222 {
7223 gfc_error ("Data transfer element at %L cannot be a full reference to "
7224 "an assumed-size array", &code->loc);
7225 return;
7226 }
7227}
7228
7229
6de9cd9a
DN
7230/*********** Toplevel code resolution subroutines ***********/
7231
0615f923 7232/* Find the set of labels that are reachable from this block. We also
d80c695f 7233 record the last statement in each block. */
0615f923
TS
7234
7235static void
d80c695f 7236find_reachable_labels (gfc_code *block)
0615f923
TS
7237{
7238 gfc_code *c;
7239
7240 if (!block)
7241 return;
7242
7243 cs_base->reachable_labels = bitmap_obstack_alloc (&labels_obstack);
7244
d80c695f
TS
7245 /* Collect labels in this block. We don't keep those corresponding
7246 to END {IF|SELECT}, these are checked in resolve_branch by going
7247 up through the code_stack. */
0615f923
TS
7248 for (c = block; c; c = c->next)
7249 {
d80c695f 7250 if (c->here && c->op != EXEC_END_BLOCK)
0615f923 7251 bitmap_set_bit (cs_base->reachable_labels, c->here->value);
0615f923
TS
7252 }
7253
7254 /* Merge with labels from parent block. */
7255 if (cs_base->prev)
7256 {
7257 gcc_assert (cs_base->prev->reachable_labels);
7258 bitmap_ior_into (cs_base->reachable_labels,
7259 cs_base->prev->reachable_labels);
7260 }
7261}
7262
d80c695f 7263/* Given a branch to a label, see if the branch is conforming.
0615f923 7264 The code node describes where the branch is located. */
6de9cd9a
DN
7265
7266static void
edf1eac2 7267resolve_branch (gfc_st_label *label, gfc_code *code)
6de9cd9a 7268{
6de9cd9a 7269 code_stack *stack;
6de9cd9a
DN
7270
7271 if (label == NULL)
7272 return;
6de9cd9a
DN
7273
7274 /* Step one: is this a valid branching target? */
7275
0615f923 7276 if (label->defined == ST_LABEL_UNKNOWN)
6de9cd9a 7277 {
0615f923
TS
7278 gfc_error ("Label %d referenced at %L is never defined", label->value,
7279 &label->where);
6de9cd9a
DN
7280 return;
7281 }
7282
0615f923 7283 if (label->defined != ST_LABEL_TARGET)
6de9cd9a
DN
7284 {
7285 gfc_error ("Statement at %L is not a valid branch target statement "
0615f923 7286 "for the branch statement at %L", &label->where, &code->loc);
6de9cd9a
DN
7287 return;
7288 }
7289
7290 /* Step two: make sure this branch is not a branch to itself ;-) */
7291
7292 if (code->here == label)
7293 {
ab551054 7294 gfc_warning ("Branch at %L may result in an infinite loop", &code->loc);
6de9cd9a
DN
7295 return;
7296 }
7297
0615f923
TS
7298 /* Step three: See if the label is in the same block as the
7299 branching statement. The hard work has been done by setting up
7300 the bitmap reachable_labels. */
6de9cd9a 7301
d80c695f
TS
7302 if (bitmap_bit_p (cs_base->reachable_labels, label->value))
7303 return;
6de9cd9a 7304
d80c695f
TS
7305 /* Step four: If we haven't found the label in the bitmap, it may
7306 still be the label of the END of the enclosing block, in which
7307 case we find it by going up the code_stack. */
6de9cd9a 7308
0615f923
TS
7309 for (stack = cs_base; stack; stack = stack->prev)
7310 if (stack->current->next && stack->current->next->here == label)
7311 break;
6de9cd9a 7312
d80c695f 7313 if (stack)
0615f923 7314 {
d80c695f
TS
7315 gcc_assert (stack->current->next->op == EXEC_END_BLOCK);
7316 return;
6de9cd9a 7317 }
0615f923 7318
d80c695f
TS
7319 /* The label is not in an enclosing block, so illegal. This was
7320 allowed in Fortran 66, so we allow it as extension. No
7321 further checks are necessary in this case. */
7322 gfc_notify_std (GFC_STD_LEGACY, "Label at %L is not in the same block "
7323 "as the GOTO statement at %L", &label->where,
7324 &code->loc);
7325 return;
6de9cd9a
DN
7326}
7327
7328
7329/* Check whether EXPR1 has the same shape as EXPR2. */
7330
17b1d2a0 7331static gfc_try
6de9cd9a
DN
7332resolve_where_shape (gfc_expr *expr1, gfc_expr *expr2)
7333{
7334 mpz_t shape[GFC_MAX_DIMENSIONS];
7335 mpz_t shape2[GFC_MAX_DIMENSIONS];
17b1d2a0 7336 gfc_try result = FAILURE;
6de9cd9a
DN
7337 int i;
7338
7339 /* Compare the rank. */
7340 if (expr1->rank != expr2->rank)
7341 return result;
7342
7343 /* Compare the size of each dimension. */
7344 for (i=0; i<expr1->rank; i++)
7345 {
7346 if (gfc_array_dimen_size (expr1, i, &shape[i]) == FAILURE)
edf1eac2 7347 goto ignore;
6de9cd9a
DN
7348
7349 if (gfc_array_dimen_size (expr2, i, &shape2[i]) == FAILURE)
edf1eac2 7350 goto ignore;
6de9cd9a
DN
7351
7352 if (mpz_cmp (shape[i], shape2[i]))
edf1eac2 7353 goto over;
6de9cd9a
DN
7354 }
7355
7356 /* When either of the two expression is an assumed size array, we
7357 ignore the comparison of dimension sizes. */
7358ignore:
7359 result = SUCCESS;
7360
7361over:
edf1eac2 7362 for (i--; i >= 0; i--)
6de9cd9a
DN
7363 {
7364 mpz_clear (shape[i]);
7365 mpz_clear (shape2[i]);
7366 }
7367 return result;
7368}
7369
7370
7371/* Check whether a WHERE assignment target or a WHERE mask expression
7372 has the same shape as the outmost WHERE mask expression. */
7373
7374static void
7375resolve_where (gfc_code *code, gfc_expr *mask)
7376{
7377 gfc_code *cblock;
7378 gfc_code *cnext;
7379 gfc_expr *e = NULL;
7380
7381 cblock = code->block;
7382
7383 /* Store the first WHERE mask-expr of the WHERE statement or construct.
7384 In case of nested WHERE, only the outmost one is stored. */
7385 if (mask == NULL) /* outmost WHERE */
a513927a 7386 e = cblock->expr1;
6de9cd9a
DN
7387 else /* inner WHERE */
7388 e = mask;
7389
7390 while (cblock)
7391 {
a513927a 7392 if (cblock->expr1)
edf1eac2
SK
7393 {
7394 /* Check if the mask-expr has a consistent shape with the
7395 outmost WHERE mask-expr. */
a513927a 7396 if (resolve_where_shape (cblock->expr1, e) == FAILURE)
edf1eac2 7397 gfc_error ("WHERE mask at %L has inconsistent shape",
a513927a 7398 &cblock->expr1->where);
edf1eac2 7399 }
6de9cd9a
DN
7400
7401 /* the assignment statement of a WHERE statement, or the first
edf1eac2 7402 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
7403 cnext = cblock->next;
7404 while (cnext)
edf1eac2
SK
7405 {
7406 switch (cnext->op)
7407 {
7408 /* WHERE assignment statement */
7409 case EXEC_ASSIGN:
7410
7411 /* Check shape consistent for WHERE assignment target. */
a513927a 7412 if (e && resolve_where_shape (cnext->expr1, e) == FAILURE)
edf1eac2 7413 gfc_error ("WHERE assignment target at %L has "
a513927a 7414 "inconsistent shape", &cnext->expr1->where);
edf1eac2
SK
7415 break;
7416
a00b8d1a
PT
7417
7418 case EXEC_ASSIGN_CALL:
7419 resolve_call (cnext);
42cd23cb 7420 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 7421 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 7422 &cnext->ext.actual->expr->where);
a00b8d1a
PT
7423 break;
7424
edf1eac2
SK
7425 /* WHERE or WHERE construct is part of a where-body-construct */
7426 case EXEC_WHERE:
7427 resolve_where (cnext, e);
7428 break;
7429
7430 default:
7431 gfc_error ("Unsupported statement inside WHERE at %L",
7432 &cnext->loc);
7433 }
7434 /* the next statement within the same where-body-construct */
7435 cnext = cnext->next;
6de9cd9a
DN
7436 }
7437 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7438 cblock = cblock->block;
7439 }
7440}
7441
7442
6de9cd9a
DN
7443/* Resolve assignment in FORALL construct.
7444 NVAR is the number of FORALL index variables, and VAR_EXPR records the
7445 FORALL index variables. */
7446
7447static void
7448gfc_resolve_assign_in_forall (gfc_code *code, int nvar, gfc_expr **var_expr)
7449{
7450 int n;
7451
7452 for (n = 0; n < nvar; n++)
7453 {
7454 gfc_symbol *forall_index;
7455
7456 forall_index = var_expr[n]->symtree->n.sym;
7457
7458 /* Check whether the assignment target is one of the FORALL index
edf1eac2 7459 variable. */
a513927a
SK
7460 if ((code->expr1->expr_type == EXPR_VARIABLE)
7461 && (code->expr1->symtree->n.sym == forall_index))
edf1eac2 7462 gfc_error ("Assignment to a FORALL index variable at %L",
a513927a 7463 &code->expr1->where);
6de9cd9a 7464 else
edf1eac2
SK
7465 {
7466 /* If one of the FORALL index variables doesn't appear in the
67cec813
PT
7467 assignment variable, then there could be a many-to-one
7468 assignment. Emit a warning rather than an error because the
7469 mask could be resolving this problem. */
a513927a 7470 if (find_forall_index (code->expr1, forall_index, 0) == FAILURE)
67cec813
PT
7471 gfc_warning ("The FORALL with index '%s' is not used on the "
7472 "left side of the assignment at %L and so might "
7473 "cause multiple assignment to this object",
a513927a 7474 var_expr[n]->symtree->name, &code->expr1->where);
edf1eac2 7475 }
6de9cd9a
DN
7476 }
7477}
7478
7479
7480/* Resolve WHERE statement in FORALL construct. */
7481
7482static void
edf1eac2
SK
7483gfc_resolve_where_code_in_forall (gfc_code *code, int nvar,
7484 gfc_expr **var_expr)
7485{
6de9cd9a
DN
7486 gfc_code *cblock;
7487 gfc_code *cnext;
7488
7489 cblock = code->block;
7490 while (cblock)
7491 {
7492 /* the assignment statement of a WHERE statement, or the first
edf1eac2 7493 statement in where-body-construct of a WHERE construct */
6de9cd9a
DN
7494 cnext = cblock->next;
7495 while (cnext)
edf1eac2
SK
7496 {
7497 switch (cnext->op)
7498 {
7499 /* WHERE assignment statement */
7500 case EXEC_ASSIGN:
7501 gfc_resolve_assign_in_forall (cnext, nvar, var_expr);
7502 break;
a00b8d1a
PT
7503
7504 /* WHERE operator assignment statement */
7505 case EXEC_ASSIGN_CALL:
7506 resolve_call (cnext);
42cd23cb 7507 if (!cnext->resolved_sym->attr.elemental)
ba6e57ba 7508 gfc_error("Non-ELEMENTAL user-defined assignment in WHERE at %L",
42cd23cb 7509 &cnext->ext.actual->expr->where);
a00b8d1a 7510 break;
edf1eac2
SK
7511
7512 /* WHERE or WHERE construct is part of a where-body-construct */
7513 case EXEC_WHERE:
7514 gfc_resolve_where_code_in_forall (cnext, nvar, var_expr);
7515 break;
7516
7517 default:
7518 gfc_error ("Unsupported statement inside WHERE at %L",
7519 &cnext->loc);
7520 }
7521 /* the next statement within the same where-body-construct */
7522 cnext = cnext->next;
7523 }
6de9cd9a
DN
7524 /* the next masked-elsewhere-stmt, elsewhere-stmt, or end-where-stmt */
7525 cblock = cblock->block;
7526 }
7527}
7528
7529
7530/* Traverse the FORALL body to check whether the following errors exist:
7531 1. For assignment, check if a many-to-one assignment happens.
7532 2. For WHERE statement, check the WHERE body to see if there is any
7533 many-to-one assignment. */
7534
7535static void
7536gfc_resolve_forall_body (gfc_code *code, int nvar, gfc_expr **var_expr)
7537{
7538 gfc_code *c;
7539
7540 c = code->block->next;
7541 while (c)
7542 {
7543 switch (c->op)
edf1eac2
SK
7544 {
7545 case EXEC_ASSIGN:
7546 case EXEC_POINTER_ASSIGN:
7547 gfc_resolve_assign_in_forall (c, nvar, var_expr);
7548 break;
7549
a00b8d1a
PT
7550 case EXEC_ASSIGN_CALL:
7551 resolve_call (c);
7552 break;
7553
edf1eac2
SK
7554 /* Because the gfc_resolve_blocks() will handle the nested FORALL,
7555 there is no need to handle it here. */
7556 case EXEC_FORALL:
7557 break;
7558 case EXEC_WHERE:
7559 gfc_resolve_where_code_in_forall(c, nvar, var_expr);
7560 break;
7561 default:
7562 break;
7563 }
6de9cd9a
DN
7564 /* The next statement in the FORALL body. */
7565 c = c->next;
7566 }
7567}
7568
7569
0e6834af
MM
7570/* Counts the number of iterators needed inside a forall construct, including
7571 nested forall constructs. This is used to allocate the needed memory
7572 in gfc_resolve_forall. */
7573
7574static int
7575gfc_count_forall_iterators (gfc_code *code)
7576{
7577 int max_iters, sub_iters, current_iters;
7578 gfc_forall_iterator *fa;
7579
7580 gcc_assert(code->op == EXEC_FORALL);
7581 max_iters = 0;
7582 current_iters = 0;
7583
7584 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7585 current_iters ++;
7586
7587 code = code->block->next;
7588
7589 while (code)
7590 {
7591 if (code->op == EXEC_FORALL)
7592 {
7593 sub_iters = gfc_count_forall_iterators (code);
7594 if (sub_iters > max_iters)
7595 max_iters = sub_iters;
7596 }
7597 code = code->next;
7598 }
7599
7600 return current_iters + max_iters;
7601}
7602
7603
6de9cd9a
DN
7604/* Given a FORALL construct, first resolve the FORALL iterator, then call
7605 gfc_resolve_forall_body to resolve the FORALL body. */
7606
6de9cd9a
DN
7607static void
7608gfc_resolve_forall (gfc_code *code, gfc_namespace *ns, int forall_save)
7609{
7610 static gfc_expr **var_expr;
7611 static int total_var = 0;
7612 static int nvar = 0;
0e6834af 7613 int old_nvar, tmp;
6de9cd9a 7614 gfc_forall_iterator *fa;
6de9cd9a
DN
7615 int i;
7616
0e6834af
MM
7617 old_nvar = nvar;
7618
6de9cd9a
DN
7619 /* Start to resolve a FORALL construct */
7620 if (forall_save == 0)
7621 {
7622 /* Count the total number of FORALL index in the nested FORALL
0e6834af
MM
7623 construct in order to allocate the VAR_EXPR with proper size. */
7624 total_var = gfc_count_forall_iterators (code);
6de9cd9a 7625
f7b529fa 7626 /* Allocate VAR_EXPR with NUMBER_OF_FORALL_INDEX elements. */
6de9cd9a
DN
7627 var_expr = (gfc_expr **) gfc_getmem (total_var * sizeof (gfc_expr *));
7628 }
7629
7630 /* The information about FORALL iterator, including FORALL index start, end
7631 and stride. The FORALL index can not appear in start, end or stride. */
7632 for (fa = code->ext.forall_iterator; fa; fa = fa->next)
7633 {
7634 /* Check if any outer FORALL index name is the same as the current
edf1eac2 7635 one. */
6de9cd9a 7636 for (i = 0; i < nvar; i++)
edf1eac2
SK
7637 {
7638 if (fa->var->symtree->n.sym == var_expr[i]->symtree->n.sym)
7639 {
7640 gfc_error ("An outer FORALL construct already has an index "
7641 "with this name %L", &fa->var->where);
7642 }
7643 }
6de9cd9a
DN
7644
7645 /* Record the current FORALL index. */
7646 var_expr[nvar] = gfc_copy_expr (fa->var);
7647
6de9cd9a 7648 nvar++;
0e6834af
MM
7649
7650 /* No memory leak. */
7651 gcc_assert (nvar <= total_var);
6de9cd9a
DN
7652 }
7653
7654 /* Resolve the FORALL body. */
7655 gfc_resolve_forall_body (code, nvar, var_expr);
7656
7657 /* May call gfc_resolve_forall to resolve the inner FORALL loop. */
6c7a4dfd 7658 gfc_resolve_blocks (code->block, ns);
6de9cd9a 7659
0e6834af
MM
7660 tmp = nvar;
7661 nvar = old_nvar;
7662 /* Free only the VAR_EXPRs allocated in this frame. */
7663 for (i = nvar; i < tmp; i++)
7664 gfc_free_expr (var_expr[i]);
6de9cd9a 7665
0e6834af
MM
7666 if (nvar == 0)
7667 {
7668 /* We are in the outermost FORALL construct. */
7669 gcc_assert (forall_save == 0);
7670
7671 /* VAR_EXPR is not needed any more. */
7672 gfc_free (var_expr);
7673 total_var = 0;
7674 }
6de9cd9a
DN
7675}
7676
7677
9abe5e56
DK
7678/* Resolve a BLOCK construct statement. */
7679
7680static void
7681resolve_block_construct (gfc_code* code)
7682{
7683 /* Eventually, we may want to do some checks here or handle special stuff.
7684 But so far the only thing we can do is resolving the local namespace. */
7685
7686 gfc_resolve (code->ext.ns);
7687}
7688
7689
7690/* Resolve lists of blocks found in IF, SELECT CASE, WHERE, FORALL, GOTO and
6de9cd9a
DN
7691 DO code nodes. */
7692
7693static void resolve_code (gfc_code *, gfc_namespace *);
7694
6c7a4dfd 7695void
edf1eac2 7696gfc_resolve_blocks (gfc_code *b, gfc_namespace *ns)
6de9cd9a 7697{
17b1d2a0 7698 gfc_try t;
6de9cd9a
DN
7699
7700 for (; b; b = b->block)
7701 {
a513927a 7702 t = gfc_resolve_expr (b->expr1);
6de9cd9a
DN
7703 if (gfc_resolve_expr (b->expr2) == FAILURE)
7704 t = FAILURE;
7705
7706 switch (b->op)
7707 {
7708 case EXEC_IF:
a513927a
SK
7709 if (t == SUCCESS && b->expr1 != NULL
7710 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank != 0))
edf1eac2 7711 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 7712 &b->expr1->where);
6de9cd9a
DN
7713 break;
7714
7715 case EXEC_WHERE:
7716 if (t == SUCCESS
a513927a
SK
7717 && b->expr1 != NULL
7718 && (b->expr1->ts.type != BT_LOGICAL || b->expr1->rank == 0))
edf1eac2 7719 gfc_error ("WHERE/ELSEWHERE clause at %L requires a LOGICAL array",
a513927a 7720 &b->expr1->where);
6de9cd9a
DN
7721 break;
7722
edf1eac2 7723 case EXEC_GOTO:
79bd1948 7724 resolve_branch (b->label1, b);
edf1eac2 7725 break;
6de9cd9a 7726
9abe5e56
DK
7727 case EXEC_BLOCK:
7728 resolve_block_construct (b);
7729 break;
7730
6de9cd9a 7731 case EXEC_SELECT:
cf2b3c22 7732 case EXEC_SELECT_TYPE:
6de9cd9a
DN
7733 case EXEC_FORALL:
7734 case EXEC_DO:
7735 case EXEC_DO_WHILE:
5e805e44
JJ
7736 case EXEC_READ:
7737 case EXEC_WRITE:
7738 case EXEC_IOLENGTH:
6f0f0b2e 7739 case EXEC_WAIT:
6de9cd9a
DN
7740 break;
7741
6c7a4dfd
JJ
7742 case EXEC_OMP_ATOMIC:
7743 case EXEC_OMP_CRITICAL:
7744 case EXEC_OMP_DO:
7745 case EXEC_OMP_MASTER:
7746 case EXEC_OMP_ORDERED:
7747 case EXEC_OMP_PARALLEL:
7748 case EXEC_OMP_PARALLEL_DO:
7749 case EXEC_OMP_PARALLEL_SECTIONS:
7750 case EXEC_OMP_PARALLEL_WORKSHARE:
7751 case EXEC_OMP_SECTIONS:
7752 case EXEC_OMP_SINGLE:
a68ab351
JJ
7753 case EXEC_OMP_TASK:
7754 case EXEC_OMP_TASKWAIT:
6c7a4dfd
JJ
7755 case EXEC_OMP_WORKSHARE:
7756 break;
7757
6de9cd9a 7758 default:
9abe5e56 7759 gfc_internal_error ("gfc_resolve_blocks(): Bad block type");
6de9cd9a
DN
7760 }
7761
7762 resolve_code (b->next, ns);
7763 }
7764}
7765
7766
c5422462 7767/* Does everything to resolve an ordinary assignment. Returns true
df2fba9e 7768 if this is an interface assignment. */
c5422462
PT
7769static bool
7770resolve_ordinary_assign (gfc_code *code, gfc_namespace *ns)
7771{
7772 bool rval = false;
7773 gfc_expr *lhs;
7774 gfc_expr *rhs;
7775 int llen = 0;
7776 int rlen = 0;
7777 int n;
7778 gfc_ref *ref;
7779
c5422462
PT
7780 if (gfc_extend_assign (code, ns) == SUCCESS)
7781 {
4a44a72d
DK
7782 gfc_expr** rhsptr;
7783
7784 if (code->op == EXEC_ASSIGN_CALL)
c5422462 7785 {
4a44a72d
DK
7786 lhs = code->ext.actual->expr;
7787 rhsptr = &code->ext.actual->next->expr;
4a44a72d
DK
7788 }
7789 else
7790 {
7791 gfc_actual_arglist* args;
7792 gfc_typebound_proc* tbp;
7793
7794 gcc_assert (code->op == EXEC_COMPCALL);
7795
7796 args = code->expr1->value.compcall.actual;
7797 lhs = args->expr;
7798 rhsptr = &args->next->expr;
7799
7800 tbp = code->expr1->value.compcall.tbp;
7801 gcc_assert (!tbp->is_generic);
c5422462
PT
7802 }
7803
7804 /* Make a temporary rhs when there is a default initializer
7805 and rhs is the same symbol as the lhs. */
4a44a72d
DK
7806 if ((*rhsptr)->expr_type == EXPR_VARIABLE
7807 && (*rhsptr)->symtree->n.sym->ts.type == BT_DERIVED
7808 && has_default_initializer ((*rhsptr)->symtree->n.sym->ts.u.derived)
7809 && (lhs->symtree->n.sym == (*rhsptr)->symtree->n.sym))
7810 *rhsptr = gfc_get_parentheses (*rhsptr);
c5422462
PT
7811
7812 return true;
7813 }
7814
a513927a 7815 lhs = code->expr1;
c5422462
PT
7816 rhs = code->expr2;
7817
00a4618b
TB
7818 if (rhs->is_boz
7819 && gfc_notify_std (GFC_STD_GNU, "Extension: BOZ literal at %L outside "
4a44a72d
DK
7820 "a DATA statement and outside INT/REAL/DBLE/CMPLX",
7821 &code->loc) == FAILURE)
00a4618b
TB
7822 return false;
7823
7824 /* Handle the case of a BOZ literal on the RHS. */
7825 if (rhs->is_boz && lhs->ts.type != BT_INTEGER)
7826 {
4956b1f1 7827 int rc;
00a4618b
TB
7828 if (gfc_option.warn_surprising)
7829 gfc_warning ("BOZ literal at %L is bitwise transferred "
7830 "non-integer symbol '%s'", &code->loc,
7831 lhs->symtree->n.sym->name);
7832
c7abc45c
TB
7833 if (!gfc_convert_boz (rhs, &lhs->ts))
7834 return false;
4956b1f1
TB
7835 if ((rc = gfc_range_check (rhs)) != ARITH_OK)
7836 {
7837 if (rc == ARITH_UNDERFLOW)
7838 gfc_error ("Arithmetic underflow of bit-wise transferred BOZ at %L"
7839 ". This check can be disabled with the option "
7840 "-fno-range-check", &rhs->where);
7841 else if (rc == ARITH_OVERFLOW)
7842 gfc_error ("Arithmetic overflow of bit-wise transferred BOZ at %L"
7843 ". This check can be disabled with the option "
7844 "-fno-range-check", &rhs->where);
7845 else if (rc == ARITH_NAN)
7846 gfc_error ("Arithmetic NaN of bit-wise transferred BOZ at %L"
7847 ". This check can be disabled with the option "
7848 "-fno-range-check", &rhs->where);
7849 return false;
7850 }
00a4618b
TB
7851 }
7852
7853
c5422462
PT
7854 if (lhs->ts.type == BT_CHARACTER
7855 && gfc_option.warn_character_truncation)
7856 {
bc21d315
JW
7857 if (lhs->ts.u.cl != NULL
7858 && lhs->ts.u.cl->length != NULL
7859 && lhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7860 llen = mpz_get_si (lhs->ts.u.cl->length->value.integer);
c5422462
PT
7861
7862 if (rhs->expr_type == EXPR_CONSTANT)
7863 rlen = rhs->value.character.length;
7864
bc21d315 7865 else if (rhs->ts.u.cl != NULL
4a44a72d 7866 && rhs->ts.u.cl->length != NULL
bc21d315
JW
7867 && rhs->ts.u.cl->length->expr_type == EXPR_CONSTANT)
7868 rlen = mpz_get_si (rhs->ts.u.cl->length->value.integer);
c5422462
PT
7869
7870 if (rlen && llen && rlen > llen)
7871 gfc_warning_now ("CHARACTER expression will be truncated "
7872 "in assignment (%d/%d) at %L",
7873 llen, rlen, &code->loc);
7874 }
7875
7876 /* Ensure that a vector index expression for the lvalue is evaluated
908a2235 7877 to a temporary if the lvalue symbol is referenced in it. */
c5422462
PT
7878 if (lhs->rank)
7879 {
7880 for (ref = lhs->ref; ref; ref= ref->next)
7881 if (ref->type == REF_ARRAY)
7882 {
7883 for (n = 0; n < ref->u.ar.dimen; n++)
908a2235 7884 if (ref->u.ar.dimen_type[n] == DIMEN_VECTOR
a68ab351
JJ
7885 && gfc_find_sym_in_expr (lhs->symtree->n.sym,
7886 ref->u.ar.start[n]))
c5422462
PT
7887 ref->u.ar.start[n]
7888 = gfc_get_parentheses (ref->u.ar.start[n]);
7889 }
7890 }
7891
7892 if (gfc_pure (NULL))
7893 {
7894 if (gfc_impure_variable (lhs->symtree->n.sym))
7895 {
7896 gfc_error ("Cannot assign to variable '%s' in PURE "
7897 "procedure at %L",
7898 lhs->symtree->n.sym->name,
7899 &lhs->where);
7900 return rval;
7901 }
7902
7903 if (lhs->ts.type == BT_DERIVED
7904 && lhs->expr_type == EXPR_VARIABLE
bc21d315 7905 && lhs->ts.u.derived->attr.pointer_comp
c5422462
PT
7906 && gfc_impure_variable (rhs->symtree->n.sym))
7907 {
7908 gfc_error ("The impure variable at %L is assigned to "
7909 "a derived type variable with a POINTER "
7910 "component in a PURE procedure (12.6)",
7911 &rhs->where);
7912 return rval;
7913 }
7914 }
7915
0ae278e7
JW
7916 /* F03:7.4.1.2. */
7917 if (lhs->ts.type == BT_CLASS)
7918 {
7919 gfc_error ("Variable must not be polymorphic in assignment at %L",
7920 &lhs->where);
7921 return false;
7922 }
7923
c5422462
PT
7924 gfc_check_assign (lhs, rhs, 1);
7925 return false;
7926}
7927
9abe5e56 7928
6de9cd9a
DN
7929/* Given a block of code, recursively resolve everything pointed to by this
7930 code block. */
7931
7932static void
edf1eac2 7933resolve_code (gfc_code *code, gfc_namespace *ns)
6de9cd9a 7934{
6c7a4dfd 7935 int omp_workshare_save;
d68bd5a8 7936 int forall_save;
6de9cd9a 7937 code_stack frame;
17b1d2a0 7938 gfc_try t;
6de9cd9a
DN
7939
7940 frame.prev = cs_base;
7941 frame.head = code;
7942 cs_base = &frame;
7943
d80c695f 7944 find_reachable_labels (code);
0615f923 7945
6de9cd9a
DN
7946 for (; code; code = code->next)
7947 {
7948 frame.current = code;
d68bd5a8 7949 forall_save = forall_flag;
6de9cd9a
DN
7950
7951 if (code->op == EXEC_FORALL)
7952 {
6de9cd9a 7953 forall_flag = 1;
6c7a4dfd 7954 gfc_resolve_forall (code, ns, forall_save);
d68bd5a8 7955 forall_flag = 2;
6c7a4dfd
JJ
7956 }
7957 else if (code->block)
7958 {
7959 omp_workshare_save = -1;
7960 switch (code->op)
7961 {
7962 case EXEC_OMP_PARALLEL_WORKSHARE:
7963 omp_workshare_save = omp_workshare_flag;
7964 omp_workshare_flag = 1;
7965 gfc_resolve_omp_parallel_blocks (code, ns);
7966 break;
7967 case EXEC_OMP_PARALLEL:
7968 case EXEC_OMP_PARALLEL_DO:
7969 case EXEC_OMP_PARALLEL_SECTIONS:
a68ab351 7970 case EXEC_OMP_TASK:
6c7a4dfd
JJ
7971 omp_workshare_save = omp_workshare_flag;
7972 omp_workshare_flag = 0;
7973 gfc_resolve_omp_parallel_blocks (code, ns);
7974 break;
7975 case EXEC_OMP_DO:
7976 gfc_resolve_omp_do_blocks (code, ns);
7977 break;
7978 case EXEC_OMP_WORKSHARE:
7979 omp_workshare_save = omp_workshare_flag;
7980 omp_workshare_flag = 1;
7981 /* FALLTHROUGH */
7982 default:
7983 gfc_resolve_blocks (code->block, ns);
7984 break;
7985 }
6de9cd9a 7986
6c7a4dfd
JJ
7987 if (omp_workshare_save != -1)
7988 omp_workshare_flag = omp_workshare_save;
7989 }
6de9cd9a 7990
8e1f752a 7991 t = SUCCESS;
713485cc 7992 if (code->op != EXEC_COMPCALL && code->op != EXEC_CALL_PPC)
a513927a 7993 t = gfc_resolve_expr (code->expr1);
d68bd5a8
PT
7994 forall_flag = forall_save;
7995
6de9cd9a
DN
7996 if (gfc_resolve_expr (code->expr2) == FAILURE)
7997 t = FAILURE;
7998
8460475b
JW
7999 if (code->op == EXEC_ALLOCATE
8000 && gfc_resolve_expr (code->expr3) == FAILURE)
8001 t = FAILURE;
8002
6de9cd9a
DN
8003 switch (code->op)
8004 {
8005 case EXEC_NOP:
d80c695f 8006 case EXEC_END_BLOCK:
6de9cd9a 8007 case EXEC_CYCLE:
6de9cd9a
DN
8008 case EXEC_PAUSE:
8009 case EXEC_STOP:
8010 case EXEC_EXIT:
8011 case EXEC_CONTINUE:
8012 case EXEC_DT_END:
4a44a72d 8013 case EXEC_ASSIGN_CALL:
0e9a445b
PT
8014 break;
8015
3d79abbd 8016 case EXEC_ENTRY:
0e9a445b
PT
8017 /* Keep track of which entry we are up to. */
8018 current_entry_id = code->ext.entry->id;
6de9cd9a
DN
8019 break;
8020
8021 case EXEC_WHERE:
8022 resolve_where (code, NULL);
8023 break;
8024
8025 case EXEC_GOTO:
a513927a 8026 if (code->expr1 != NULL)
ce2df7c6 8027 {
a513927a 8028 if (code->expr1->ts.type != BT_INTEGER)
edf1eac2 8029 gfc_error ("ASSIGNED GOTO statement at %L requires an "
a513927a
SK
8030 "INTEGER variable", &code->expr1->where);
8031 else if (code->expr1->symtree->n.sym->attr.assign != 1)
edf1eac2 8032 gfc_error ("Variable '%s' has not been assigned a target "
a513927a
SK
8033 "label at %L", code->expr1->symtree->n.sym->name,
8034 &code->expr1->where);
ce2df7c6
FW
8035 }
8036 else
79bd1948 8037 resolve_branch (code->label1, code);
6de9cd9a
DN
8038 break;
8039
8040 case EXEC_RETURN:
a513927a
SK
8041 if (code->expr1 != NULL
8042 && (code->expr1->ts.type != BT_INTEGER || code->expr1->rank))
b6398823 8043 gfc_error ("Alternate RETURN statement at %L requires a SCALAR-"
a513927a 8044 "INTEGER return specifier", &code->expr1->where);
6de9cd9a
DN
8045 break;
8046
6b591ec0 8047 case EXEC_INIT_ASSIGN:
5c71a5e0 8048 case EXEC_END_PROCEDURE:
6b591ec0
PT
8049 break;
8050
6de9cd9a
DN
8051 case EXEC_ASSIGN:
8052 if (t == FAILURE)
8053 break;
8054
c5422462 8055 if (resolve_ordinary_assign (code, ns))
664e411b
JW
8056 {
8057 if (code->op == EXEC_COMPCALL)
8058 goto compcall;
8059 else
8060 goto call;
8061 }
6de9cd9a
DN
8062 break;
8063
8064 case EXEC_LABEL_ASSIGN:
79bd1948 8065 if (code->label1->defined == ST_LABEL_UNKNOWN)
edf1eac2 8066 gfc_error ("Label %d referenced at %L is never defined",
79bd1948 8067 code->label1->value, &code->label1->where);
edf1eac2 8068 if (t == SUCCESS
a513927a
SK
8069 && (code->expr1->expr_type != EXPR_VARIABLE
8070 || code->expr1->symtree->n.sym->ts.type != BT_INTEGER
8071 || code->expr1->symtree->n.sym->ts.kind
edf1eac2 8072 != gfc_default_integer_kind
a513927a 8073 || code->expr1->symtree->n.sym->as != NULL))
40f2165e 8074 gfc_error ("ASSIGN statement at %L requires a scalar "
a513927a 8075 "default INTEGER variable", &code->expr1->where);
6de9cd9a
DN
8076 break;
8077
8078 case EXEC_POINTER_ASSIGN:
8079 if (t == FAILURE)
8080 break;
8081
93d76687 8082 gfc_check_pointer_assign (code->expr1, code->expr2);
6de9cd9a
DN
8083 break;
8084
8085 case EXEC_ARITHMETIC_IF:
8086 if (t == SUCCESS
a513927a
SK
8087 && code->expr1->ts.type != BT_INTEGER
8088 && code->expr1->ts.type != BT_REAL)
6de9cd9a 8089 gfc_error ("Arithmetic IF statement at %L requires a numeric "
a513927a 8090 "expression", &code->expr1->where);
6de9cd9a 8091
79bd1948 8092 resolve_branch (code->label1, code);
6de9cd9a
DN
8093 resolve_branch (code->label2, code);
8094 resolve_branch (code->label3, code);
8095 break;
8096
8097 case EXEC_IF:
a513927a
SK
8098 if (t == SUCCESS && code->expr1 != NULL
8099 && (code->expr1->ts.type != BT_LOGICAL
8100 || code->expr1->rank != 0))
6de9cd9a 8101 gfc_error ("IF clause at %L requires a scalar LOGICAL expression",
a513927a 8102 &code->expr1->where);
6de9cd9a
DN
8103 break;
8104
8105 case EXEC_CALL:
8106 call:
8107 resolve_call (code);
8108 break;
8109
8e1f752a 8110 case EXEC_COMPCALL:
664e411b 8111 compcall:
7cf078dc
PT
8112 if (code->expr1->symtree
8113 && code->expr1->symtree->n.sym->ts.type == BT_CLASS)
8114 resolve_class_typebound_call (code);
8115 else
8116 resolve_typebound_call (code);
8e1f752a
DK
8117 break;
8118
713485cc 8119 case EXEC_CALL_PPC:
9abe5e56 8120 resolve_ppc_call (code);
713485cc
JW
8121 break;
8122
6de9cd9a
DN
8123 case EXEC_SELECT:
8124 /* Select is complicated. Also, a SELECT construct could be
8125 a transformed computed GOTO. */
8126 resolve_select (code);
8127 break;
8128
cf2b3c22
TB
8129 case EXEC_SELECT_TYPE:
8130 resolve_select_type (code);
8131 break;
8132
9abe5e56
DK
8133 case EXEC_BLOCK:
8134 gfc_resolve (code->ext.ns);
8135 break;
8136
6de9cd9a
DN
8137 case EXEC_DO:
8138 if (code->ext.iterator != NULL)
6c7a4dfd
JJ
8139 {
8140 gfc_iterator *iter = code->ext.iterator;
8141 if (gfc_resolve_iterator (iter, true) != FAILURE)
8142 gfc_resolve_do_iterator (code, iter->var->symtree->n.sym);
8143 }
6de9cd9a
DN
8144 break;
8145
8146 case EXEC_DO_WHILE:
a513927a 8147 if (code->expr1 == NULL)
6de9cd9a
DN
8148 gfc_internal_error ("resolve_code(): No expression on DO WHILE");
8149 if (t == SUCCESS
a513927a
SK
8150 && (code->expr1->rank != 0
8151 || code->expr1->ts.type != BT_LOGICAL))
6de9cd9a 8152 gfc_error ("Exit condition of DO WHILE loop at %L must be "
a513927a 8153 "a scalar LOGICAL expression", &code->expr1->where);
6de9cd9a
DN
8154 break;
8155
8156 case EXEC_ALLOCATE:
b9332b09
PT
8157 if (t == SUCCESS)
8158 resolve_allocate_deallocate (code, "ALLOCATE");
6de9cd9a
DN
8159
8160 break;
8161
8162 case EXEC_DEALLOCATE:
b9332b09
PT
8163 if (t == SUCCESS)
8164 resolve_allocate_deallocate (code, "DEALLOCATE");
6de9cd9a
DN
8165
8166 break;
8167
8168 case EXEC_OPEN:
8169 if (gfc_resolve_open (code->ext.open) == FAILURE)
8170 break;
8171
8172 resolve_branch (code->ext.open->err, code);
8173 break;
8174
8175 case EXEC_CLOSE:
8176 if (gfc_resolve_close (code->ext.close) == FAILURE)
8177 break;
8178
8179 resolve_branch (code->ext.close->err, code);
8180 break;
8181
8182 case EXEC_BACKSPACE:
8183 case EXEC_ENDFILE:
8184 case EXEC_REWIND:
6403ec5f 8185 case EXEC_FLUSH:
6de9cd9a
DN
8186 if (gfc_resolve_filepos (code->ext.filepos) == FAILURE)
8187 break;
8188
8189 resolve_branch (code->ext.filepos->err, code);
8190 break;
8191
8192 case EXEC_INQUIRE:
8750f9cd
JB
8193 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8194 break;
8195
8196 resolve_branch (code->ext.inquire->err, code);
8197 break;
8198
8199 case EXEC_IOLENGTH:
6e45f57b 8200 gcc_assert (code->ext.inquire != NULL);
6de9cd9a
DN
8201 if (gfc_resolve_inquire (code->ext.inquire) == FAILURE)
8202 break;
8203
8204 resolve_branch (code->ext.inquire->err, code);
8205 break;
8206
6f0f0b2e
JD
8207 case EXEC_WAIT:
8208 if (gfc_resolve_wait (code->ext.wait) == FAILURE)
8209 break;
8210
8211 resolve_branch (code->ext.wait->err, code);
8212 resolve_branch (code->ext.wait->end, code);
8213 resolve_branch (code->ext.wait->eor, code);
8214 break;
8215
6de9cd9a
DN
8216 case EXEC_READ:
8217 case EXEC_WRITE:
88e18fed 8218 if (gfc_resolve_dt (code->ext.dt, &code->loc) == FAILURE)
6de9cd9a
DN
8219 break;
8220
8221 resolve_branch (code->ext.dt->err, code);
8222 resolve_branch (code->ext.dt->end, code);
8223 resolve_branch (code->ext.dt->eor, code);
8224 break;
8225
0e6928d8
TS
8226 case EXEC_TRANSFER:
8227 resolve_transfer (code);
8228 break;
8229
6de9cd9a
DN
8230 case EXEC_FORALL:
8231 resolve_forall_iterators (code->ext.forall_iterator);
8232
a513927a 8233 if (code->expr1 != NULL && code->expr1->ts.type != BT_LOGICAL)
edf1eac2 8234 gfc_error ("FORALL mask clause at %L requires a LOGICAL "
a513927a 8235 "expression", &code->expr1->where);
6de9cd9a
DN
8236 break;
8237
6c7a4dfd
JJ
8238 case EXEC_OMP_ATOMIC:
8239 case EXEC_OMP_BARRIER:
8240 case EXEC_OMP_CRITICAL:
8241 case EXEC_OMP_FLUSH:
8242 case EXEC_OMP_DO:
8243 case EXEC_OMP_MASTER:
8244 case EXEC_OMP_ORDERED:
8245 case EXEC_OMP_SECTIONS:
8246 case EXEC_OMP_SINGLE:
a68ab351 8247 case EXEC_OMP_TASKWAIT:
6c7a4dfd
JJ
8248 case EXEC_OMP_WORKSHARE:
8249 gfc_resolve_omp_directive (code, ns);
8250 break;
8251
8252 case EXEC_OMP_PARALLEL:
8253 case EXEC_OMP_PARALLEL_DO:
8254 case EXEC_OMP_PARALLEL_SECTIONS:
8255 case EXEC_OMP_PARALLEL_WORKSHARE:
a68ab351 8256 case EXEC_OMP_TASK:
6c7a4dfd
JJ
8257 omp_workshare_save = omp_workshare_flag;
8258 omp_workshare_flag = 0;
8259 gfc_resolve_omp_directive (code, ns);
8260 omp_workshare_flag = omp_workshare_save;
8261 break;
8262
6de9cd9a
DN
8263 default:
8264 gfc_internal_error ("resolve_code(): Bad statement code");
8265 }
8266 }
8267
8268 cs_base = frame.prev;
8269}
8270
8271
8272/* Resolve initial values and make sure they are compatible with
8273 the variable. */
8274
8275static void
edf1eac2 8276resolve_values (gfc_symbol *sym)
6de9cd9a 8277{
6de9cd9a
DN
8278 if (sym->value == NULL)
8279 return;
8280
8281 if (gfc_resolve_expr (sym->value) == FAILURE)
8282 return;
8283
8284 gfc_check_assign_symbol (sym, sym->value);
8285}
8286
8287
a8b3b0b6
CR
8288/* Verify the binding labels for common blocks that are BIND(C). The label
8289 for a BIND(C) common block must be identical in all scoping units in which
8290 the common block is declared. Further, the binding label can not collide
8291 with any other global entity in the program. */
8292
8293static void
8294resolve_bind_c_comms (gfc_symtree *comm_block_tree)
8295{
8296 if (comm_block_tree->n.common->is_bind_c == 1)
8297 {
8298 gfc_gsymbol *binding_label_gsym;
8299 gfc_gsymbol *comm_name_gsym;
8300
8301 /* See if a global symbol exists by the common block's name. It may
8302 be NULL if the common block is use-associated. */
8303 comm_name_gsym = gfc_find_gsymbol (gfc_gsym_root,
8304 comm_block_tree->n.common->name);
8305 if (comm_name_gsym != NULL && comm_name_gsym->type != GSYM_COMMON)
8306 gfc_error ("Binding label '%s' for common block '%s' at %L collides "
8307 "with the global entity '%s' at %L",
8308 comm_block_tree->n.common->binding_label,
8309 comm_block_tree->n.common->name,
8310 &(comm_block_tree->n.common->where),
8311 comm_name_gsym->name, &(comm_name_gsym->where));
8312 else if (comm_name_gsym != NULL
8313 && strcmp (comm_name_gsym->name,
8314 comm_block_tree->n.common->name) == 0)
8315 {
8316 /* TODO: Need to make sure the fields of gfc_gsymbol are initialized
8317 as expected. */
8318 if (comm_name_gsym->binding_label == NULL)
8319 /* No binding label for common block stored yet; save this one. */
8320 comm_name_gsym->binding_label =
8321 comm_block_tree->n.common->binding_label;
8322 else
8323 if (strcmp (comm_name_gsym->binding_label,
8324 comm_block_tree->n.common->binding_label) != 0)
8325 {
8326 /* Common block names match but binding labels do not. */
8327 gfc_error ("Binding label '%s' for common block '%s' at %L "
8328 "does not match the binding label '%s' for common "
8329 "block '%s' at %L",
8330 comm_block_tree->n.common->binding_label,
8331 comm_block_tree->n.common->name,
8332 &(comm_block_tree->n.common->where),
8333 comm_name_gsym->binding_label,
8334 comm_name_gsym->name,
8335 &(comm_name_gsym->where));
8336 return;
8337 }
8338 }
8339
8340 /* There is no binding label (NAME="") so we have nothing further to
8341 check and nothing to add as a global symbol for the label. */
8342 if (comm_block_tree->n.common->binding_label[0] == '\0' )
8343 return;
8344
8345 binding_label_gsym =
8346 gfc_find_gsymbol (gfc_gsym_root,
8347 comm_block_tree->n.common->binding_label);
8348 if (binding_label_gsym == NULL)
8349 {
8350 /* Need to make a global symbol for the binding label to prevent
8351 it from colliding with another. */
8352 binding_label_gsym =
8353 gfc_get_gsymbol (comm_block_tree->n.common->binding_label);
8354 binding_label_gsym->sym_name = comm_block_tree->n.common->name;
8355 binding_label_gsym->type = GSYM_COMMON;
8356 }
8357 else
8358 {
8359 /* If comm_name_gsym is NULL, the name common block is use
8360 associated and the name could be colliding. */
8361 if (binding_label_gsym->type != GSYM_COMMON)
8362 gfc_error ("Binding label '%s' for common block '%s' at %L "
8363 "collides with the global entity '%s' at %L",
8364 comm_block_tree->n.common->binding_label,
8365 comm_block_tree->n.common->name,
8366 &(comm_block_tree->n.common->where),
8367 binding_label_gsym->name,
8368 &(binding_label_gsym->where));
8369 else if (comm_name_gsym != NULL
8370 && (strcmp (binding_label_gsym->name,
8371 comm_name_gsym->binding_label) != 0)
8372 && (strcmp (binding_label_gsym->sym_name,
8373 comm_name_gsym->name) != 0))
8374 gfc_error ("Binding label '%s' for common block '%s' at %L "
8375 "collides with global entity '%s' at %L",
8376 binding_label_gsym->name, binding_label_gsym->sym_name,
8377 &(comm_block_tree->n.common->where),
8378 comm_name_gsym->name, &(comm_name_gsym->where));
8379 }
8380 }
8381
8382 return;
8383}
8384
8385
8386/* Verify any BIND(C) derived types in the namespace so we can report errors
8387 for them once, rather than for each variable declared of that type. */
8388
8389static void
8390resolve_bind_c_derived_types (gfc_symbol *derived_sym)
8391{
8392 if (derived_sym != NULL && derived_sym->attr.flavor == FL_DERIVED
8393 && derived_sym->attr.is_bind_c == 1)
8394 verify_bind_c_derived_type (derived_sym);
8395
8396 return;
8397}
8398
8399
8400/* Verify that any binding labels used in a given namespace do not collide
8401 with the names or binding labels of any global symbols. */
8402
8403static void
8404gfc_verify_binding_labels (gfc_symbol *sym)
8405{
8406 int has_error = 0;
8407
8408 if (sym != NULL && sym->attr.is_bind_c && sym->attr.is_iso_c == 0
8409 && sym->attr.flavor != FL_DERIVED && sym->binding_label[0] != '\0')
8410 {
8411 gfc_gsymbol *bind_c_sym;
8412
8413 bind_c_sym = gfc_find_gsymbol (gfc_gsym_root, sym->binding_label);
8414 if (bind_c_sym != NULL
8415 && strcmp (bind_c_sym->name, sym->binding_label) == 0)
8416 {
8417 if (sym->attr.if_source == IFSRC_DECL
8418 && (bind_c_sym->type != GSYM_SUBROUTINE
8419 && bind_c_sym->type != GSYM_FUNCTION)
8420 && ((sym->attr.contained == 1
8421 && strcmp (bind_c_sym->sym_name, sym->name) != 0)
8422 || (sym->attr.use_assoc == 1
8423 && (strcmp (bind_c_sym->mod_name, sym->module) != 0))))
8424 {
8425 /* Make sure global procedures don't collide with anything. */
8426 gfc_error ("Binding label '%s' at %L collides with the global "
8427 "entity '%s' at %L", sym->binding_label,
8428 &(sym->declared_at), bind_c_sym->name,
8429 &(bind_c_sym->where));
8430 has_error = 1;
8431 }
8432 else if (sym->attr.contained == 0
8433 && (sym->attr.if_source == IFSRC_IFBODY
8434 && sym->attr.flavor == FL_PROCEDURE)
8435 && (bind_c_sym->sym_name != NULL
8436 && strcmp (bind_c_sym->sym_name, sym->name) != 0))
8437 {
8438 /* Make sure procedures in interface bodies don't collide. */
8439 gfc_error ("Binding label '%s' in interface body at %L collides "
8440 "with the global entity '%s' at %L",
8441 sym->binding_label,
8442 &(sym->declared_at), bind_c_sym->name,
8443 &(bind_c_sym->where));
8444 has_error = 1;
8445 }
8446 else if (sym->attr.contained == 0
e7bff0d1
TB
8447 && sym->attr.if_source == IFSRC_UNKNOWN)
8448 if ((sym->attr.use_assoc && bind_c_sym->mod_name
8449 && strcmp (bind_c_sym->mod_name, sym->module) != 0)
8450 || sym->attr.use_assoc == 0)
a8b3b0b6
CR
8451 {
8452 gfc_error ("Binding label '%s' at %L collides with global "
8453 "entity '%s' at %L", sym->binding_label,
8454 &(sym->declared_at), bind_c_sym->name,
8455 &(bind_c_sym->where));
8456 has_error = 1;
8457 }
8458
8459 if (has_error != 0)
8460 /* Clear the binding label to prevent checking multiple times. */
8461 sym->binding_label[0] = '\0';
8462 }
8463 else if (bind_c_sym == NULL)
8464 {
8465 bind_c_sym = gfc_get_gsymbol (sym->binding_label);
8466 bind_c_sym->where = sym->declared_at;
8467 bind_c_sym->sym_name = sym->name;
8468
8469 if (sym->attr.use_assoc == 1)
8470 bind_c_sym->mod_name = sym->module;
8471 else
8472 if (sym->ns->proc_name != NULL)
8473 bind_c_sym->mod_name = sym->ns->proc_name->name;
8474
8475 if (sym->attr.contained == 0)
8476 {
8477 if (sym->attr.subroutine)
8478 bind_c_sym->type = GSYM_SUBROUTINE;
8479 else if (sym->attr.function)
8480 bind_c_sym->type = GSYM_FUNCTION;
8481 }
8482 }
8483 }
8484 return;
8485}
8486
8487
2ed8d224
PT
8488/* Resolve an index expression. */
8489
17b1d2a0 8490static gfc_try
edf1eac2 8491resolve_index_expr (gfc_expr *e)
2ed8d224 8492{
2ed8d224
PT
8493 if (gfc_resolve_expr (e) == FAILURE)
8494 return FAILURE;
8495
8496 if (gfc_simplify_expr (e, 0) == FAILURE)
8497 return FAILURE;
8498
8499 if (gfc_specification_expr (e) == FAILURE)
8500 return FAILURE;
8501
8502 return SUCCESS;
8503}
8504
110eec24
TS
8505/* Resolve a charlen structure. */
8506
17b1d2a0 8507static gfc_try
110eec24
TS
8508resolve_charlen (gfc_charlen *cl)
8509{
b0c06816 8510 int i, k;
5cd09fac 8511
110eec24
TS
8512 if (cl->resolved)
8513 return SUCCESS;
8514
8515 cl->resolved = 1;
8516
0e9a445b
PT
8517 specification_expr = 1;
8518
2ed8d224 8519 if (resolve_index_expr (cl->length) == FAILURE)
0e9a445b
PT
8520 {
8521 specification_expr = 0;
8522 return FAILURE;
8523 }
110eec24 8524
5cd09fac
TS
8525 /* "If the character length parameter value evaluates to a negative
8526 value, the length of character entities declared is zero." */
815cd406 8527 if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
5cd09fac
TS
8528 {
8529 gfc_warning_now ("CHARACTER variable has zero length at %L",
8530 &cl->length->where);
8531 gfc_replace_expr (cl->length, gfc_int_expr (0));
8532 }
8533
b0c06816
FXC
8534 /* Check that the character length is not too large. */
8535 k = gfc_validate_kind (BT_INTEGER, gfc_charlen_int_kind, false);
8536 if (cl->length && cl->length->expr_type == EXPR_CONSTANT
8537 && cl->length->ts.type == BT_INTEGER
8538 && mpz_cmp (cl->length->value.integer, gfc_integer_kinds[k].huge) > 0)
8539 {
8540 gfc_error ("String length at %L is too large", &cl->length->where);
8541 return FAILURE;
8542 }
8543
2ed8d224
PT
8544 return SUCCESS;
8545}
8546
8547
66e4ab31 8548/* Test for non-constant shape arrays. */
3e1cf500
PT
8549
8550static bool
8551is_non_constant_shape_array (gfc_symbol *sym)
8552{
8553 gfc_expr *e;
8554 int i;
0e9a445b 8555 bool not_constant;
3e1cf500 8556
0e9a445b 8557 not_constant = false;
3e1cf500
PT
8558 if (sym->as != NULL)
8559 {
8560 /* Unfortunately, !gfc_is_compile_time_shape hits a legal case that
8561 has not been simplified; parameter array references. Do the
8562 simplification now. */
8563 for (i = 0; i < sym->as->rank; i++)
8564 {
8565 e = sym->as->lower[i];
8566 if (e && (resolve_index_expr (e) == FAILURE
edf1eac2 8567 || !gfc_is_constant_expr (e)))
0e9a445b 8568 not_constant = true;
3e1cf500
PT
8569
8570 e = sym->as->upper[i];
8571 if (e && (resolve_index_expr (e) == FAILURE
edf1eac2 8572 || !gfc_is_constant_expr (e)))
0e9a445b 8573 not_constant = true;
3e1cf500
PT
8574 }
8575 }
0e9a445b 8576 return not_constant;
3e1cf500
PT
8577}
8578
51b09ce3
AL
8579/* Given a symbol and an initialization expression, add code to initialize
8580 the symbol to the function entry. */
6b591ec0 8581static void
51b09ce3 8582build_init_assign (gfc_symbol *sym, gfc_expr *init)
6b591ec0
PT
8583{
8584 gfc_expr *lval;
6b591ec0
PT
8585 gfc_code *init_st;
8586 gfc_namespace *ns = sym->ns;
8587
6b591ec0
PT
8588 /* Search for the function namespace if this is a contained
8589 function without an explicit result. */
8590 if (sym->attr.function && sym == sym->result
edf1eac2 8591 && sym->name != sym->ns->proc_name->name)
6b591ec0
PT
8592 {
8593 ns = ns->contained;
8594 for (;ns; ns = ns->sibling)
8595 if (strcmp (ns->proc_name->name, sym->name) == 0)
8596 break;
8597 }
8598
8599 if (ns == NULL)
8600 {
8601 gfc_free_expr (init);
8602 return;
8603 }
8604
8605 /* Build an l-value expression for the result. */
08113c73 8606 lval = gfc_lval_expr_from_sym (sym);
6b591ec0
PT
8607
8608 /* Add the code at scope entry. */
8609 init_st = gfc_get_code ();
8610 init_st->next = ns->code;
8611 ns->code = init_st;
8612
8613 /* Assign the default initializer to the l-value. */
8614 init_st->loc = sym->declared_at;
8615 init_st->op = EXEC_INIT_ASSIGN;
a513927a 8616 init_st->expr1 = lval;
6b591ec0
PT
8617 init_st->expr2 = init;
8618}
8619
51b09ce3
AL
8620/* Assign the default initializer to a derived type variable or result. */
8621
8622static void
8623apply_default_init (gfc_symbol *sym)
8624{
8625 gfc_expr *init = NULL;
8626
8627 if (sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8628 return;
8629
bc21d315 8630 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived)
51b09ce3
AL
8631 init = gfc_default_initializer (&sym->ts);
8632
8633 if (init == NULL)
8634 return;
8635
8636 build_init_assign (sym, init);
8637}
8638
8639/* Build an initializer for a local integer, real, complex, logical, or
8640 character variable, based on the command line flags finit-local-zero,
8641 finit-integer=, finit-real=, finit-logical=, and finit-runtime. Returns
8642 null if the symbol should not have a default initialization. */
8643static gfc_expr *
8644build_default_init_expr (gfc_symbol *sym)
8645{
8646 int char_len;
8647 gfc_expr *init_expr;
8648 int i;
51b09ce3
AL
8649
8650 /* These symbols should never have a default initialization. */
8651 if ((sym->attr.dimension && !gfc_is_compile_time_shape (sym->as))
8652 || sym->attr.external
8653 || sym->attr.dummy
8654 || sym->attr.pointer
8655 || sym->attr.in_equivalence
8656 || sym->attr.in_common
8657 || sym->attr.data
8658 || sym->module
8659 || sym->attr.cray_pointee
8660 || sym->attr.cray_pointer)
8661 return NULL;
8662
8663 /* Now we'll try to build an initializer expression. */
8664 init_expr = gfc_get_expr ();
8665 init_expr->expr_type = EXPR_CONSTANT;
8666 init_expr->ts.type = sym->ts.type;
8667 init_expr->ts.kind = sym->ts.kind;
8668 init_expr->where = sym->declared_at;
8669
8670 /* We will only initialize integers, reals, complex, logicals, and
8671 characters, and only if the corresponding command-line flags
8672 were set. Otherwise, we free init_expr and return null. */
8673 switch (sym->ts.type)
8674 {
8675 case BT_INTEGER:
8676 if (gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
8677 mpz_init_set_si (init_expr->value.integer,
8678 gfc_option.flag_init_integer_value);
8679 else
8680 {
8681 gfc_free_expr (init_expr);
8682 init_expr = NULL;
8683 }
8684 break;
8685
8686 case BT_REAL:
8687 mpfr_init (init_expr->value.real);
8688 switch (gfc_option.flag_init_real)
8689 {
346a77d1
TB
8690 case GFC_INIT_REAL_SNAN:
8691 init_expr->is_snan = 1;
8692 /* Fall through. */
51b09ce3
AL
8693 case GFC_INIT_REAL_NAN:
8694 mpfr_set_nan (init_expr->value.real);
8695 break;
8696
8697 case GFC_INIT_REAL_INF:
8698 mpfr_set_inf (init_expr->value.real, 1);
8699 break;
8700
8701 case GFC_INIT_REAL_NEG_INF:
8702 mpfr_set_inf (init_expr->value.real, -1);
8703 break;
8704
8705 case GFC_INIT_REAL_ZERO:
8706 mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
8707 break;
8708
8709 default:
8710 gfc_free_expr (init_expr);
8711 init_expr = NULL;
8712 break;
8713 }
8714 break;
8715
8716 case BT_COMPLEX:
eb6f9a86 8717 mpc_init2 (init_expr->value.complex, mpfr_get_default_prec());
51b09ce3
AL
8718 switch (gfc_option.flag_init_real)
8719 {
346a77d1
TB
8720 case GFC_INIT_REAL_SNAN:
8721 init_expr->is_snan = 1;
8722 /* Fall through. */
51b09ce3 8723 case GFC_INIT_REAL_NAN:
eb6f9a86
KG
8724 mpfr_set_nan (mpc_realref (init_expr->value.complex));
8725 mpfr_set_nan (mpc_imagref (init_expr->value.complex));
51b09ce3
AL
8726 break;
8727
8728 case GFC_INIT_REAL_INF:
eb6f9a86
KG
8729 mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
8730 mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
51b09ce3
AL
8731 break;
8732
8733 case GFC_INIT_REAL_NEG_INF:
eb6f9a86
KG
8734 mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
8735 mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
51b09ce3
AL
8736 break;
8737
8738 case GFC_INIT_REAL_ZERO:
eb6f9a86 8739 mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
51b09ce3
AL
8740 break;
8741
8742 default:
8743 gfc_free_expr (init_expr);
8744 init_expr = NULL;
8745 break;
8746 }
8747 break;
8748
8749 case BT_LOGICAL:
8750 if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_FALSE)
8751 init_expr->value.logical = 0;
8752 else if (gfc_option.flag_init_logical == GFC_INIT_LOGICAL_TRUE)
8753 init_expr->value.logical = 1;
8754 else
8755 {
8756 gfc_free_expr (init_expr);
8757 init_expr = NULL;
8758 }
8759 break;
8760
8761 case BT_CHARACTER:
8762 /* For characters, the length must be constant in order to
8763 create a default initializer. */
8764 if (gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON
bc21d315
JW
8765 && sym->ts.u.cl->length
8766 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
51b09ce3 8767 {
bc21d315 8768 char_len = mpz_get_si (sym->ts.u.cl->length->value.integer);
51b09ce3 8769 init_expr->value.character.length = char_len;
00660189 8770 init_expr->value.character.string = gfc_get_wide_string (char_len+1);
51b09ce3 8771 for (i = 0; i < char_len; i++)
00660189
FXC
8772 init_expr->value.character.string[i]
8773 = (unsigned char) gfc_option.flag_init_character_value;
51b09ce3
AL
8774 }
8775 else
8776 {
8777 gfc_free_expr (init_expr);
8778 init_expr = NULL;
8779 }
8780 break;
8781
8782 default:
8783 gfc_free_expr (init_expr);
8784 init_expr = NULL;
8785 }
8786 return init_expr;
8787}
8788
8789/* Add an initialization expression to a local variable. */
8790static void
8791apply_default_init_local (gfc_symbol *sym)
8792{
8793 gfc_expr *init = NULL;
8794
8795 /* The symbol should be a variable or a function return value. */
8796 if ((sym->attr.flavor != FL_VARIABLE && !sym->attr.function)
8797 || (sym->attr.function && sym->result != sym))
8798 return;
8799
8800 /* Try to build the initializer expression. If we can't initialize
8801 this symbol, then init will be NULL. */
8802 init = build_default_init_expr (sym);
8803 if (init == NULL)
8804 return;
8805
8806 /* For saved variables, we don't want to add an initializer at
8807 function entry, so we just add a static initializer. */
0e8bc11d
JB
8808 if (sym->attr.save || sym->ns->save_all
8809 || gfc_option.flag_max_stack_var_size == 0)
51b09ce3
AL
8810 {
8811 /* Don't clobber an existing initializer! */
8812 gcc_assert (sym->value == NULL);
8813 sym->value = init;
8814 return;
8815 }
8816
8817 build_init_assign (sym, init);
8818}
6b591ec0 8819
66e4ab31 8820/* Resolution of common features of flavors variable and procedure. */
2ed8d224 8821
17b1d2a0 8822static gfc_try
2ed8d224
PT
8823resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
8824{
8825 /* Constraints on deferred shape variable. */
8826 if (sym->as == NULL || sym->as->type != AS_DEFERRED)
8827 {
8828 if (sym->attr.allocatable)
8829 {
8830 if (sym->attr.dimension)
2fbd4117
JW
8831 {
8832 gfc_error ("Allocatable array '%s' at %L must have "
8833 "a deferred shape", sym->name, &sym->declared_at);
8834 return FAILURE;
8835 }
8836 else if (gfc_notify_std (GFC_STD_F2003, "Scalar object '%s' at %L "
8837 "may not be ALLOCATABLE", sym->name,
8838 &sym->declared_at) == FAILURE)
2ed8d224
PT
8839 return FAILURE;
8840 }
8841
8842 if (sym->attr.pointer && sym->attr.dimension)
8843 {
8844 gfc_error ("Array pointer '%s' at %L must have a deferred shape",
8845 sym->name, &sym->declared_at);
8846 return FAILURE;
8847 }
8848
8849 }
8850 else
8851 {
cf2b3c22
TB
8852 if (!mp_flag && !sym->attr.allocatable && !sym->attr.pointer
8853 && !sym->attr.dummy && sym->ts.type != BT_CLASS)
2ed8d224
PT
8854 {
8855 gfc_error ("Array '%s' at %L cannot have a deferred shape",
8856 sym->name, &sym->declared_at);
8857 return FAILURE;
8858 }
8859 }
8860 return SUCCESS;
8861}
8862
edf1eac2 8863
448d2cd2
TS
8864/* Additional checks for symbols with flavor variable and derived
8865 type. To be called from resolve_fl_variable. */
8866
17b1d2a0 8867static gfc_try
9de88093 8868resolve_fl_variable_derived (gfc_symbol *sym, int no_init_flag)
448d2cd2 8869{
cf2b3c22 8870 gcc_assert (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS);
448d2cd2
TS
8871
8872 /* Check to see if a derived type is blocked from being host
8873 associated by the presence of another class I symbol in the same
8874 namespace. 14.6.1.3 of the standard and the discussion on
8875 comp.lang.fortran. */
bc21d315 8876 if (sym->ns != sym->ts.u.derived->ns
448d2cd2
TS
8877 && sym->ns->proc_name->attr.if_source != IFSRC_IFBODY)
8878 {
8879 gfc_symbol *s;
bc21d315 8880 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 0, &s);
334e912a 8881 if (s && s->attr.flavor != FL_DERIVED)
448d2cd2
TS
8882 {
8883 gfc_error ("The type '%s' cannot be host associated at %L "
8884 "because it is blocked by an incompatible object "
8885 "of the same name declared at %L",
bc21d315 8886 sym->ts.u.derived->name, &sym->declared_at,
448d2cd2
TS
8887 &s->declared_at);
8888 return FAILURE;
8889 }
8890 }
8891
8892 /* 4th constraint in section 11.3: "If an object of a type for which
8893 component-initialization is specified (R429) appears in the
8894 specification-part of a module and does not have the ALLOCATABLE
8895 or POINTER attribute, the object shall have the SAVE attribute."
8896
8897 The check for initializers is performed with
8898 has_default_initializer because gfc_default_initializer generates
8899 a hidden default for allocatable components. */
9de88093 8900 if (!(sym->value || no_init_flag) && sym->ns->proc_name
448d2cd2
TS
8901 && sym->ns->proc_name->attr.flavor == FL_MODULE
8902 && !sym->ns->save_all && !sym->attr.save
8903 && !sym->attr.pointer && !sym->attr.allocatable
bc21d315 8904 && has_default_initializer (sym->ts.u.derived))
448d2cd2
TS
8905 {
8906 gfc_error("Object '%s' at %L must have the SAVE attribute for "
8907 "default initialization of a component",
8908 sym->name, &sym->declared_at);
8909 return FAILURE;
8910 }
8911
cf2b3c22 8912 if (sym->ts.type == BT_CLASS)
727e8544
JW
8913 {
8914 /* C502. */
cf2b3c22 8915 if (!gfc_type_is_extensible (sym->ts.u.derived->components->ts.u.derived))
727e8544
JW
8916 {
8917 gfc_error ("Type '%s' of CLASS variable '%s' at %L is not extensible",
7c1dab0d
JW
8918 sym->ts.u.derived->components->ts.u.derived->name,
8919 sym->name, &sym->declared_at);
727e8544
JW
8920 return FAILURE;
8921 }
8922
8923 /* C509. */
2e23972e
JW
8924 /* Assume that use associated symbols were checked in the module ns. */
8925 if (!sym->attr.class_ok && !sym->attr.use_assoc)
727e8544
JW
8926 {
8927 gfc_error ("CLASS variable '%s' at %L must be dummy, allocatable "
8928 "or pointer", sym->name, &sym->declared_at);
8929 return FAILURE;
8930 }
8931 }
8932
448d2cd2
TS
8933 /* Assign default initializer. */
8934 if (!(sym->value || sym->attr.pointer || sym->attr.allocatable)
9de88093 8935 && (!no_init_flag || sym->attr.intent == INTENT_OUT))
448d2cd2
TS
8936 {
8937 sym->value = gfc_default_initializer (&sym->ts);
8938 }
8939
8940 return SUCCESS;
8941}
8942
8943
2ed8d224
PT
8944/* Resolve symbols with flavor variable. */
8945
17b1d2a0 8946static gfc_try
2ed8d224
PT
8947resolve_fl_variable (gfc_symbol *sym, int mp_flag)
8948{
9de88093 8949 int no_init_flag, automatic_flag;
2ed8d224 8950 gfc_expr *e;
edf1eac2 8951 const char *auto_save_msg;
0e9a445b 8952
9de88093 8953 auto_save_msg = "Automatic object '%s' at %L cannot have the "
0e9a445b 8954 "SAVE attribute";
2ed8d224
PT
8955
8956 if (resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
110eec24
TS
8957 return FAILURE;
8958
0e9a445b
PT
8959 /* Set this flag to check that variables are parameters of all entries.
8960 This check is effected by the call to gfc_resolve_expr through
8961 is_non_constant_shape_array. */
8962 specification_expr = 1;
8963
c4d4556f
TS
8964 if (sym->ns->proc_name
8965 && (sym->ns->proc_name->attr.flavor == FL_MODULE
8966 || sym->ns->proc_name->attr.is_main_program)
8967 && !sym->attr.use_assoc
edf1eac2
SK
8968 && !sym->attr.allocatable
8969 && !sym->attr.pointer
8970 && is_non_constant_shape_array (sym))
2ed8d224 8971 {
c4d4556f
TS
8972 /* The shape of a main program or module array needs to be
8973 constant. */
8974 gfc_error ("The module or main program array '%s' at %L must "
8975 "have constant shape", sym->name, &sym->declared_at);
8976 specification_expr = 0;
8977 return FAILURE;
2ed8d224
PT
8978 }
8979
8980 if (sym->ts.type == BT_CHARACTER)
8981 {
8982 /* Make sure that character string variables with assumed length are
8983 dummy arguments. */
bc21d315 8984 e = sym->ts.u.cl->length;
2ed8d224
PT
8985 if (e == NULL && !sym->attr.dummy && !sym->attr.result)
8986 {
8987 gfc_error ("Entity with assumed character length at %L must be a "
8988 "dummy argument or a PARAMETER", &sym->declared_at);
8989 return FAILURE;
8990 }
8991
0e9a445b
PT
8992 if (e && sym->attr.save && !gfc_is_constant_expr (e))
8993 {
8994 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
8995 return FAILURE;
8996 }
8997
2ed8d224 8998 if (!gfc_is_constant_expr (e)
edf1eac2
SK
8999 && !(e->expr_type == EXPR_VARIABLE
9000 && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
9001 && sym->ns->proc_name
9002 && (sym->ns->proc_name->attr.flavor == FL_MODULE
9003 || sym->ns->proc_name->attr.is_main_program)
9004 && !sym->attr.use_assoc)
2ed8d224
PT
9005 {
9006 gfc_error ("'%s' at %L must have constant character length "
9007 "in this context", sym->name, &sym->declared_at);
9008 return FAILURE;
9009 }
9010 }
9011
51b09ce3
AL
9012 if (sym->value == NULL && sym->attr.referenced)
9013 apply_default_init_local (sym); /* Try to apply a default initialization. */
9014
9de88093
TS
9015 /* Determine if the symbol may not have an initializer. */
9016 no_init_flag = automatic_flag = 0;
2ed8d224 9017 if (sym->attr.allocatable || sym->attr.external || sym->attr.dummy
9de88093
TS
9018 || sym->attr.intrinsic || sym->attr.result)
9019 no_init_flag = 1;
9020 else if (sym->attr.dimension && !sym->attr.pointer
9021 && is_non_constant_shape_array (sym))
2ed8d224 9022 {
9de88093 9023 no_init_flag = automatic_flag = 1;
0e9a445b 9024
5349080d
TB
9025 /* Also, they must not have the SAVE attribute.
9026 SAVE_IMPLICIT is checked below. */
9de88093 9027 if (sym->attr.save == SAVE_EXPLICIT)
0e9a445b
PT
9028 {
9029 gfc_error (auto_save_msg, sym->name, &sym->declared_at);
9030 return FAILURE;
9031 }
448d2cd2 9032 }
2ed8d224 9033
7a99defe
SK
9034 /* Ensure that any initializer is simplified. */
9035 if (sym->value)
9036 gfc_simplify_expr (sym->value, 1);
9037
2ed8d224 9038 /* Reject illegal initializers. */
9de88093 9039 if (!sym->mark && sym->value)
2ed8d224
PT
9040 {
9041 if (sym->attr.allocatable)
9042 gfc_error ("Allocatable '%s' at %L cannot have an initializer",
9043 sym->name, &sym->declared_at);
9044 else if (sym->attr.external)
9045 gfc_error ("External '%s' at %L cannot have an initializer",
9046 sym->name, &sym->declared_at);
145bdc2c
PT
9047 else if (sym->attr.dummy
9048 && !(sym->ts.type == BT_DERIVED && sym->attr.intent == INTENT_OUT))
2ed8d224
PT
9049 gfc_error ("Dummy '%s' at %L cannot have an initializer",
9050 sym->name, &sym->declared_at);
9051 else if (sym->attr.intrinsic)
9052 gfc_error ("Intrinsic '%s' at %L cannot have an initializer",
9053 sym->name, &sym->declared_at);
9054 else if (sym->attr.result)
9055 gfc_error ("Function result '%s' at %L cannot have an initializer",
9056 sym->name, &sym->declared_at);
9de88093 9057 else if (automatic_flag)
2ed8d224
PT
9058 gfc_error ("Automatic array '%s' at %L cannot have an initializer",
9059 sym->name, &sym->declared_at);
145bdc2c
PT
9060 else
9061 goto no_init_error;
2ed8d224
PT
9062 return FAILURE;
9063 }
9064
145bdc2c 9065no_init_error:
cf2b3c22 9066 if (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS)
9de88093 9067 return resolve_fl_variable_derived (sym, no_init_flag);
2ed8d224
PT
9068
9069 return SUCCESS;
9070}
9071
9072
9073/* Resolve a procedure. */
9074
17b1d2a0 9075static gfc_try
2ed8d224
PT
9076resolve_fl_procedure (gfc_symbol *sym, int mp_flag)
9077{
9078 gfc_formal_arglist *arg;
9079
9080 if (sym->attr.function
edf1eac2 9081 && resolve_fl_var_and_proc (sym, mp_flag) == FAILURE)
110eec24
TS
9082 return FAILURE;
9083
92c59193 9084 if (sym->ts.type == BT_CHARACTER)
2ed8d224 9085 {
bc21d315 9086 gfc_charlen *cl = sym->ts.u.cl;
8111a921
PT
9087
9088 if (cl && cl->length && gfc_is_constant_expr (cl->length)
9089 && resolve_charlen (cl) == FAILURE)
9090 return FAILURE;
9091
d94be5e0
TB
9092 if ((!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
9093 && sym->attr.proc == PROC_ST_FUNCTION)
92c59193 9094 {
d94be5e0
TB
9095 gfc_error ("Character-valued statement function '%s' at %L must "
9096 "have constant length", sym->name, &sym->declared_at);
9097 return FAILURE;
edf1eac2 9098 }
2ed8d224
PT
9099 }
9100
37e47ee9 9101 /* Ensure that derived type for are not of a private type. Internal
df2fba9e 9102 module procedures are excluded by 2.2.3.3 - i.e., they are not
b82feea5 9103 externally accessible and can access all the objects accessible in
66e4ab31 9104 the host. */
37e47ee9 9105 if (!(sym->ns->parent
edf1eac2
SK
9106 && sym->ns->parent->proc_name->attr.flavor == FL_MODULE)
9107 && gfc_check_access(sym->attr.access, sym->ns->default_access))
2ed8d224 9108 {
83b2e4e8
DF
9109 gfc_interface *iface;
9110
2ed8d224
PT
9111 for (arg = sym->formal; arg; arg = arg->next)
9112 {
9113 if (arg->sym
edf1eac2 9114 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
9115 && !arg->sym->ts.u.derived->attr.use_assoc
9116 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9117 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
9118 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: '%s' is of a "
9119 "PRIVATE type and cannot be a dummy argument"
9120 " of '%s', which is PUBLIC at %L",
9121 arg->sym->name, sym->name, &sym->declared_at)
9122 == FAILURE)
2ed8d224 9123 {
2ed8d224 9124 /* Stop this message from recurring. */
bc21d315 9125 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
2ed8d224
PT
9126 return FAILURE;
9127 }
9128 }
83b2e4e8 9129
3bed9dd0
DF
9130 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9131 PRIVATE to the containing module. */
9132 for (iface = sym->generic; iface; iface = iface->next)
9133 {
9134 for (arg = iface->sym->formal; arg; arg = arg->next)
9135 {
9136 if (arg->sym
9137 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
9138 && !arg->sym->ts.u.derived->attr.use_assoc
9139 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9140 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
9141 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9142 "'%s' in PUBLIC interface '%s' at %L "
9143 "takes dummy arguments of '%s' which is "
9144 "PRIVATE", iface->sym->name, sym->name,
9145 &iface->sym->declared_at,
9146 gfc_typename (&arg->sym->ts)) == FAILURE)
3bed9dd0 9147 {
3bed9dd0 9148 /* Stop this message from recurring. */
bc21d315 9149 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
3bed9dd0
DF
9150 return FAILURE;
9151 }
9152 }
9153 }
9154
83b2e4e8
DF
9155 /* PUBLIC interfaces may expose PRIVATE procedures that take types
9156 PRIVATE to the containing module. */
9157 for (iface = sym->generic; iface; iface = iface->next)
9158 {
9159 for (arg = iface->sym->formal; arg; arg = arg->next)
9160 {
9161 if (arg->sym
9162 && arg->sym->ts.type == BT_DERIVED
bc21d315
JW
9163 && !arg->sym->ts.u.derived->attr.use_assoc
9164 && !gfc_check_access (arg->sym->ts.u.derived->attr.access,
9165 arg->sym->ts.u.derived->ns->default_access)
0ab7816b
TB
9166 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure "
9167 "'%s' in PUBLIC interface '%s' at %L "
9168 "takes dummy arguments of '%s' which is "
9169 "PRIVATE", iface->sym->name, sym->name,
9170 &iface->sym->declared_at,
9171 gfc_typename (&arg->sym->ts)) == FAILURE)
83b2e4e8 9172 {
83b2e4e8 9173 /* Stop this message from recurring. */
bc21d315 9174 arg->sym->ts.u.derived->attr.access = ACCESS_PUBLIC;
83b2e4e8
DF
9175 return FAILURE;
9176 }
9177 }
9178 }
2ed8d224
PT
9179 }
9180
8fb74da4
JW
9181 if (sym->attr.function && sym->value && sym->attr.proc != PROC_ST_FUNCTION
9182 && !sym->attr.proc_pointer)
f8faa85e
DF
9183 {
9184 gfc_error ("Function '%s' at %L cannot have an initializer",
9185 sym->name, &sym->declared_at);
9186 return FAILURE;
9187 }
9188
e2ae1407 9189 /* An external symbol may not have an initializer because it is taken to be
8fb74da4
JW
9190 a procedure. Exception: Procedure Pointers. */
9191 if (sym->attr.external && sym->value && !sym->attr.proc_pointer)
2ed8d224
PT
9192 {
9193 gfc_error ("External object '%s' at %L may not have an initializer",
9194 sym->name, &sym->declared_at);
9195 return FAILURE;
9196 }
9197
d68bd5a8
PT
9198 /* An elemental function is required to return a scalar 12.7.1 */
9199 if (sym->attr.elemental && sym->attr.function && sym->as)
9200 {
9201 gfc_error ("ELEMENTAL function '%s' at %L must have a scalar "
9202 "result", sym->name, &sym->declared_at);
9203 /* Reset so that the error only occurs once. */
9204 sym->attr.elemental = 0;
9205 return FAILURE;
9206 }
9207
2ed8d224
PT
9208 /* 5.1.1.5 of the Standard: A function name declared with an asterisk
9209 char-len-param shall not be array-valued, pointer-valued, recursive
9210 or pure. ....snip... A character value of * may only be used in the
9211 following ways: (i) Dummy arg of procedure - dummy associates with
9212 actual length; (ii) To declare a named constant; or (iii) External
9213 function - but length must be declared in calling scoping unit. */
9214 if (sym->attr.function
edf1eac2 9215 && sym->ts.type == BT_CHARACTER
bc21d315 9216 && sym->ts.u.cl && sym->ts.u.cl->length == NULL)
2ed8d224
PT
9217 {
9218 if ((sym->as && sym->as->rank) || (sym->attr.pointer)
edf1eac2 9219 || (sym->attr.recursive) || (sym->attr.pure))
2ed8d224
PT
9220 {
9221 if (sym->as && sym->as->rank)
9222 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9223 "array-valued", sym->name, &sym->declared_at);
9224
9225 if (sym->attr.pointer)
9226 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9227 "pointer-valued", sym->name, &sym->declared_at);
9228
9229 if (sym->attr.pure)
9230 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9231 "pure", sym->name, &sym->declared_at);
9232
9233 if (sym->attr.recursive)
9234 gfc_error ("CHARACTER(*) function '%s' at %L cannot be "
9235 "recursive", sym->name, &sym->declared_at);
9236
9237 return FAILURE;
9238 }
9239
9240 /* Appendix B.2 of the standard. Contained functions give an
9241 error anyway. Fixed-form is likely to be F77/legacy. */
9242 if (!sym->attr.contained && gfc_current_form != FORM_FIXED)
e2ab8b09
JW
9243 gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
9244 "CHARACTER(*) function '%s' at %L",
2ed8d224
PT
9245 sym->name, &sym->declared_at);
9246 }
a8b3b0b6
CR
9247
9248 if (sym->attr.is_bind_c && sym->attr.is_c_interop != 1)
9249 {
9250 gfc_formal_arglist *curr_arg;
aa5e22f0 9251 int has_non_interop_arg = 0;
a8b3b0b6
CR
9252
9253 if (verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
9254 sym->common_block) == FAILURE)
9255 {
9256 /* Clear these to prevent looking at them again if there was an
9257 error. */
9258 sym->attr.is_bind_c = 0;
9259 sym->attr.is_c_interop = 0;
9260 sym->ts.is_c_interop = 0;
9261 }
9262 else
9263 {
9264 /* So far, no errors have been found. */
9265 sym->attr.is_c_interop = 1;
9266 sym->ts.is_c_interop = 1;
9267 }
9268
9269 curr_arg = sym->formal;
9270 while (curr_arg != NULL)
9271 {
9272 /* Skip implicitly typed dummy args here. */
aa5e22f0
CR
9273 if (curr_arg->sym->attr.implicit_type == 0)
9274 if (verify_c_interop_param (curr_arg->sym) == FAILURE)
9275 /* If something is found to fail, record the fact so we
9276 can mark the symbol for the procedure as not being
9277 BIND(C) to try and prevent multiple errors being
9278 reported. */
9279 has_non_interop_arg = 1;
9280
a8b3b0b6
CR
9281 curr_arg = curr_arg->next;
9282 }
aa5e22f0
CR
9283
9284 /* See if any of the arguments were not interoperable and if so, clear
9285 the procedure symbol to prevent duplicate error messages. */
9286 if (has_non_interop_arg != 0)
9287 {
9288 sym->attr.is_c_interop = 0;
9289 sym->ts.is_c_interop = 0;
9290 sym->attr.is_bind_c = 0;
9291 }
a8b3b0b6
CR
9292 }
9293
3070bab4 9294 if (!sym->attr.proc_pointer)
beb4bd6c 9295 {
3070bab4
JW
9296 if (sym->attr.save == SAVE_EXPLICIT)
9297 {
9298 gfc_error ("PROCEDURE attribute conflicts with SAVE attribute "
9299 "in '%s' at %L", sym->name, &sym->declared_at);
9300 return FAILURE;
9301 }
9302 if (sym->attr.intent)
9303 {
9304 gfc_error ("PROCEDURE attribute conflicts with INTENT attribute "
9305 "in '%s' at %L", sym->name, &sym->declared_at);
9306 return FAILURE;
9307 }
9308 if (sym->attr.subroutine && sym->attr.result)
9309 {
9310 gfc_error ("PROCEDURE attribute conflicts with RESULT attribute "
9311 "in '%s' at %L", sym->name, &sym->declared_at);
9312 return FAILURE;
9313 }
9314 if (sym->attr.external && sym->attr.function
9315 && ((sym->attr.if_source == IFSRC_DECL && !sym->attr.procedure)
9316 || sym->attr.contained))
9317 {
9318 gfc_error ("EXTERNAL attribute conflicts with FUNCTION attribute "
9319 "in '%s' at %L", sym->name, &sym->declared_at);
9320 return FAILURE;
9321 }
9322 if (strcmp ("ppr@", sym->name) == 0)
9323 {
9324 gfc_error ("Procedure pointer result '%s' at %L "
9325 "is missing the pointer attribute",
9326 sym->ns->proc_name->name, &sym->declared_at);
9327 return FAILURE;
9328 }
beb4bd6c
JW
9329 }
9330
110eec24
TS
9331 return SUCCESS;
9332}
9333
9334
34523524
DK
9335/* Resolve a list of finalizer procedures. That is, after they have hopefully
9336 been defined and we now know their defined arguments, check that they fulfill
9337 the requirements of the standard for procedures used as finalizers. */
9338
17b1d2a0 9339static gfc_try
34523524
DK
9340gfc_resolve_finalizers (gfc_symbol* derived)
9341{
9342 gfc_finalizer* list;
9343 gfc_finalizer** prev_link; /* For removing wrong entries from the list. */
17b1d2a0 9344 gfc_try result = SUCCESS;
34523524
DK
9345 bool seen_scalar = false;
9346
9347 if (!derived->f2k_derived || !derived->f2k_derived->finalizers)
9348 return SUCCESS;
9349
9350 /* Walk over the list of finalizer-procedures, check them, and if any one
9351 does not fit in with the standard's definition, print an error and remove
9352 it from the list. */
9353 prev_link = &derived->f2k_derived->finalizers;
9354 for (list = derived->f2k_derived->finalizers; list; list = *prev_link)
9355 {
9356 gfc_symbol* arg;
9357 gfc_finalizer* i;
9358 int my_rank;
9359
f6fad28e
DK
9360 /* Skip this finalizer if we already resolved it. */
9361 if (list->proc_tree)
9362 {
9363 prev_link = &(list->next);
9364 continue;
9365 }
9366
34523524 9367 /* Check this exists and is a SUBROUTINE. */
f6fad28e 9368 if (!list->proc_sym->attr.subroutine)
34523524
DK
9369 {
9370 gfc_error ("FINAL procedure '%s' at %L is not a SUBROUTINE",
f6fad28e 9371 list->proc_sym->name, &list->where);
34523524
DK
9372 goto error;
9373 }
9374
9375 /* We should have exactly one argument. */
f6fad28e 9376 if (!list->proc_sym->formal || list->proc_sym->formal->next)
34523524
DK
9377 {
9378 gfc_error ("FINAL procedure at %L must have exactly one argument",
9379 &list->where);
9380 goto error;
9381 }
f6fad28e 9382 arg = list->proc_sym->formal->sym;
34523524
DK
9383
9384 /* This argument must be of our type. */
bc21d315 9385 if (arg->ts.type != BT_DERIVED || arg->ts.u.derived != derived)
34523524
DK
9386 {
9387 gfc_error ("Argument of FINAL procedure at %L must be of type '%s'",
9388 &arg->declared_at, derived->name);
9389 goto error;
9390 }
9391
9392 /* It must neither be a pointer nor allocatable nor optional. */
9393 if (arg->attr.pointer)
9394 {
9395 gfc_error ("Argument of FINAL procedure at %L must not be a POINTER",
9396 &arg->declared_at);
9397 goto error;
9398 }
9399 if (arg->attr.allocatable)
9400 {
9401 gfc_error ("Argument of FINAL procedure at %L must not be"
9402 " ALLOCATABLE", &arg->declared_at);
9403 goto error;
9404 }
9405 if (arg->attr.optional)
9406 {
9407 gfc_error ("Argument of FINAL procedure at %L must not be OPTIONAL",
9408 &arg->declared_at);
9409 goto error;
9410 }
9411
9412 /* It must not be INTENT(OUT). */
9413 if (arg->attr.intent == INTENT_OUT)
9414 {
9415 gfc_error ("Argument of FINAL procedure at %L must not be"
9416 " INTENT(OUT)", &arg->declared_at);
9417 goto error;
9418 }
9419
9420 /* Warn if the procedure is non-scalar and not assumed shape. */
9421 if (gfc_option.warn_surprising && arg->as && arg->as->rank > 0
9422 && arg->as->type != AS_ASSUMED_SHAPE)
9423 gfc_warning ("Non-scalar FINAL procedure at %L should have assumed"
9424 " shape argument", &arg->declared_at);
9425
9426 /* Check that it does not match in kind and rank with a FINAL procedure
9427 defined earlier. To really loop over the *earlier* declarations,
9428 we need to walk the tail of the list as new ones were pushed at the
9429 front. */
9430 /* TODO: Handle kind parameters once they are implemented. */
9431 my_rank = (arg->as ? arg->as->rank : 0);
9432 for (i = list->next; i; i = i->next)
9433 {
9434 /* Argument list might be empty; that is an error signalled earlier,
9435 but we nevertheless continued resolving. */
f6fad28e 9436 if (i->proc_sym->formal)
34523524 9437 {
f6fad28e 9438 gfc_symbol* i_arg = i->proc_sym->formal->sym;
34523524
DK
9439 const int i_rank = (i_arg->as ? i_arg->as->rank : 0);
9440 if (i_rank == my_rank)
9441 {
9442 gfc_error ("FINAL procedure '%s' declared at %L has the same"
9443 " rank (%d) as '%s'",
f6fad28e
DK
9444 list->proc_sym->name, &list->where, my_rank,
9445 i->proc_sym->name);
34523524
DK
9446 goto error;
9447 }
9448 }
9449 }
9450
9451 /* Is this the/a scalar finalizer procedure? */
9452 if (!arg->as || arg->as->rank == 0)
9453 seen_scalar = true;
9454
f6fad28e
DK
9455 /* Find the symtree for this procedure. */
9456 gcc_assert (!list->proc_tree);
9457 list->proc_tree = gfc_find_sym_in_symtree (list->proc_sym);
9458
34523524
DK
9459 prev_link = &list->next;
9460 continue;
9461
df2fba9e 9462 /* Remove wrong nodes immediately from the list so we don't risk any
34523524
DK
9463 troubles in the future when they might fail later expectations. */
9464error:
9465 result = FAILURE;
9466 i = list;
9467 *prev_link = list->next;
9468 gfc_free_finalizer (i);
9469 }
9470
9471 /* Warn if we haven't seen a scalar finalizer procedure (but we know there
9472 were nodes in the list, must have been for arrays. It is surely a good
9473 idea to have a scalar version there if there's something to finalize. */
9474 if (gfc_option.warn_surprising && result == SUCCESS && !seen_scalar)
9475 gfc_warning ("Only array FINAL procedures declared for derived type '%s'"
9476 " defined at %L, suggest also scalar one",
9477 derived->name, &derived->declared_at);
9478
9479 /* TODO: Remove this error when finalization is finished. */
f6fad28e
DK
9480 gfc_error ("Finalization at %L is not yet implemented",
9481 &derived->declared_at);
34523524
DK
9482
9483 return result;
9484}
9485
9486
30b608eb
DK
9487/* Check that it is ok for the typebound procedure proc to override the
9488 procedure old. */
9489
9490static gfc_try
9491check_typebound_override (gfc_symtree* proc, gfc_symtree* old)
9492{
9493 locus where;
9494 const gfc_symbol* proc_target;
9495 const gfc_symbol* old_target;
9496 unsigned proc_pass_arg, old_pass_arg, argpos;
9497 gfc_formal_arglist* proc_formal;
9498 gfc_formal_arglist* old_formal;
9499
e157f736 9500 /* This procedure should only be called for non-GENERIC proc. */
e34ccb4c 9501 gcc_assert (!proc->n.tb->is_generic);
e157f736
DK
9502
9503 /* If the overwritten procedure is GENERIC, this is an error. */
e34ccb4c 9504 if (old->n.tb->is_generic)
e157f736
DK
9505 {
9506 gfc_error ("Can't overwrite GENERIC '%s' at %L",
e34ccb4c 9507 old->name, &proc->n.tb->where);
e157f736
DK
9508 return FAILURE;
9509 }
9510
e34ccb4c
DK
9511 where = proc->n.tb->where;
9512 proc_target = proc->n.tb->u.specific->n.sym;
9513 old_target = old->n.tb->u.specific->n.sym;
30b608eb
DK
9514
9515 /* Check that overridden binding is not NON_OVERRIDABLE. */
e34ccb4c 9516 if (old->n.tb->non_overridable)
30b608eb
DK
9517 {
9518 gfc_error ("'%s' at %L overrides a procedure binding declared"
9519 " NON_OVERRIDABLE", proc->name, &where);
9520 return FAILURE;
9521 }
9522
b0e5fa94 9523 /* It's an error to override a non-DEFERRED procedure with a DEFERRED one. */
e34ccb4c 9524 if (!old->n.tb->deferred && proc->n.tb->deferred)
b0e5fa94
DK
9525 {
9526 gfc_error ("'%s' at %L must not be DEFERRED as it overrides a"
9527 " non-DEFERRED binding", proc->name, &where);
9528 return FAILURE;
9529 }
9530
30b608eb
DK
9531 /* If the overridden binding is PURE, the overriding must be, too. */
9532 if (old_target->attr.pure && !proc_target->attr.pure)
9533 {
9534 gfc_error ("'%s' at %L overrides a PURE procedure and must also be PURE",
9535 proc->name, &where);
9536 return FAILURE;
9537 }
9538
9539 /* If the overridden binding is ELEMENTAL, the overriding must be, too. If it
9540 is not, the overriding must not be either. */
9541 if (old_target->attr.elemental && !proc_target->attr.elemental)
9542 {
9543 gfc_error ("'%s' at %L overrides an ELEMENTAL procedure and must also be"
9544 " ELEMENTAL", proc->name, &where);
9545 return FAILURE;
9546 }
9547 if (!old_target->attr.elemental && proc_target->attr.elemental)
9548 {
9549 gfc_error ("'%s' at %L overrides a non-ELEMENTAL procedure and must not"
9550 " be ELEMENTAL, either", proc->name, &where);
9551 return FAILURE;
9552 }
9553
9554 /* If the overridden binding is a SUBROUTINE, the overriding must also be a
9555 SUBROUTINE. */
9556 if (old_target->attr.subroutine && !proc_target->attr.subroutine)
9557 {
9558 gfc_error ("'%s' at %L overrides a SUBROUTINE and must also be a"
9559 " SUBROUTINE", proc->name, &where);
9560 return FAILURE;
9561 }
9562
9563 /* If the overridden binding is a FUNCTION, the overriding must also be a
9564 FUNCTION and have the same characteristics. */
9565 if (old_target->attr.function)
9566 {
9567 if (!proc_target->attr.function)
9568 {
9569 gfc_error ("'%s' at %L overrides a FUNCTION and must also be a"
9570 " FUNCTION", proc->name, &where);
9571 return FAILURE;
9572 }
9573
9574 /* FIXME: Do more comprehensive checking (including, for instance, the
9575 rank and array-shape). */
9576 gcc_assert (proc_target->result && old_target->result);
9577 if (!gfc_compare_types (&proc_target->result->ts,
9578 &old_target->result->ts))
9579 {
9580 gfc_error ("'%s' at %L and the overridden FUNCTION should have"
9581 " matching result types", proc->name, &where);
9582 return FAILURE;
9583 }
9584 }
9585
9586 /* If the overridden binding is PUBLIC, the overriding one must not be
9587 PRIVATE. */
e34ccb4c
DK
9588 if (old->n.tb->access == ACCESS_PUBLIC
9589 && proc->n.tb->access == ACCESS_PRIVATE)
30b608eb
DK
9590 {
9591 gfc_error ("'%s' at %L overrides a PUBLIC procedure and must not be"
9592 " PRIVATE", proc->name, &where);
9593 return FAILURE;
9594 }
9595
9596 /* Compare the formal argument lists of both procedures. This is also abused
9597 to find the position of the passed-object dummy arguments of both
9598 bindings as at least the overridden one might not yet be resolved and we
9599 need those positions in the check below. */
9600 proc_pass_arg = old_pass_arg = 0;
e34ccb4c 9601 if (!proc->n.tb->nopass && !proc->n.tb->pass_arg)
30b608eb 9602 proc_pass_arg = 1;
e34ccb4c 9603 if (!old->n.tb->nopass && !old->n.tb->pass_arg)
30b608eb
DK
9604 old_pass_arg = 1;
9605 argpos = 1;
9606 for (proc_formal = proc_target->formal, old_formal = old_target->formal;
9607 proc_formal && old_formal;
9608 proc_formal = proc_formal->next, old_formal = old_formal->next)
9609 {
e34ccb4c
DK
9610 if (proc->n.tb->pass_arg
9611 && !strcmp (proc->n.tb->pass_arg, proc_formal->sym->name))
30b608eb 9612 proc_pass_arg = argpos;
e34ccb4c
DK
9613 if (old->n.tb->pass_arg
9614 && !strcmp (old->n.tb->pass_arg, old_formal->sym->name))
30b608eb
DK
9615 old_pass_arg = argpos;
9616
9617 /* Check that the names correspond. */
9618 if (strcmp (proc_formal->sym->name, old_formal->sym->name))
9619 {
9620 gfc_error ("Dummy argument '%s' of '%s' at %L should be named '%s' as"
9621 " to match the corresponding argument of the overridden"
9622 " procedure", proc_formal->sym->name, proc->name, &where,
9623 old_formal->sym->name);
9624 return FAILURE;
9625 }
9626
9627 /* Check that the types correspond if neither is the passed-object
9628 argument. */
9629 /* FIXME: Do more comprehensive testing here. */
9630 if (proc_pass_arg != argpos && old_pass_arg != argpos
9631 && !gfc_compare_types (&proc_formal->sym->ts, &old_formal->sym->ts))
9632 {
800cee34
SK
9633 gfc_error ("Types mismatch for dummy argument '%s' of '%s' %L "
9634 "in respect to the overridden procedure",
30b608eb
DK
9635 proc_formal->sym->name, proc->name, &where);
9636 return FAILURE;
9637 }
9638
9639 ++argpos;
9640 }
9641 if (proc_formal || old_formal)
9642 {
9643 gfc_error ("'%s' at %L must have the same number of formal arguments as"
9644 " the overridden procedure", proc->name, &where);
9645 return FAILURE;
9646 }
9647
9648 /* If the overridden binding is NOPASS, the overriding one must also be
9649 NOPASS. */
e34ccb4c 9650 if (old->n.tb->nopass && !proc->n.tb->nopass)
30b608eb
DK
9651 {
9652 gfc_error ("'%s' at %L overrides a NOPASS binding and must also be"
9653 " NOPASS", proc->name, &where);
9654 return FAILURE;
9655 }
9656
9657 /* If the overridden binding is PASS(x), the overriding one must also be
9658 PASS and the passed-object dummy arguments must correspond. */
e34ccb4c 9659 if (!old->n.tb->nopass)
30b608eb 9660 {
e34ccb4c 9661 if (proc->n.tb->nopass)
30b608eb
DK
9662 {
9663 gfc_error ("'%s' at %L overrides a binding with PASS and must also be"
9664 " PASS", proc->name, &where);
9665 return FAILURE;
9666 }
9667
9668 if (proc_pass_arg != old_pass_arg)
9669 {
9670 gfc_error ("Passed-object dummy argument of '%s' at %L must be at"
9671 " the same position as the passed-object dummy argument of"
9672 " the overridden procedure", proc->name, &where);
9673 return FAILURE;
9674 }
9675 }
9676
9677 return SUCCESS;
9678}
9679
9680
e157f736
DK
9681/* Check if two GENERIC targets are ambiguous and emit an error is they are. */
9682
9683static gfc_try
9684check_generic_tbp_ambiguity (gfc_tbp_generic* t1, gfc_tbp_generic* t2,
9685 const char* generic_name, locus where)
9686{
9687 gfc_symbol* sym1;
9688 gfc_symbol* sym2;
9689
9690 gcc_assert (t1->specific && t2->specific);
9691 gcc_assert (!t1->specific->is_generic);
9692 gcc_assert (!t2->specific->is_generic);
9693
9694 sym1 = t1->specific->u.specific->n.sym;
9695 sym2 = t2->specific->u.specific->n.sym;
9696
cf2b3c22
TB
9697 if (sym1 == sym2)
9698 return SUCCESS;
9699
e157f736
DK
9700 /* Both must be SUBROUTINEs or both must be FUNCTIONs. */
9701 if (sym1->attr.subroutine != sym2->attr.subroutine
9702 || sym1->attr.function != sym2->attr.function)
9703 {
9704 gfc_error ("'%s' and '%s' can't be mixed FUNCTION/SUBROUTINE for"
9705 " GENERIC '%s' at %L",
9706 sym1->name, sym2->name, generic_name, &where);
9707 return FAILURE;
9708 }
9709
9710 /* Compare the interfaces. */
0175478d 9711 if (gfc_compare_interfaces (sym1, sym2, sym2->name, 1, 0, NULL, 0))
e157f736
DK
9712 {
9713 gfc_error ("'%s' and '%s' for GENERIC '%s' at %L are ambiguous",
9714 sym1->name, sym2->name, generic_name, &where);
9715 return FAILURE;
9716 }
9717
9718 return SUCCESS;
9719}
9720
9721
94747289
DK
9722/* Worker function for resolving a generic procedure binding; this is used to
9723 resolve GENERIC as well as user and intrinsic OPERATOR typebound procedures.
9724
9725 The difference between those cases is finding possible inherited bindings
9726 that are overridden, as one has to look for them in tb_sym_root,
9727 tb_uop_root or tb_op, respectively. Thus the caller must already find
9728 the super-type and set p->overridden correctly. */
e157f736
DK
9729
9730static gfc_try
94747289
DK
9731resolve_tb_generic_targets (gfc_symbol* super_type,
9732 gfc_typebound_proc* p, const char* name)
e157f736
DK
9733{
9734 gfc_tbp_generic* target;
9735 gfc_symtree* first_target;
e157f736 9736 gfc_symtree* inherited;
e157f736 9737
94747289 9738 gcc_assert (p && p->is_generic);
e157f736
DK
9739
9740 /* Try to find the specific bindings for the symtrees in our target-list. */
94747289
DK
9741 gcc_assert (p->u.generic);
9742 for (target = p->u.generic; target; target = target->next)
e157f736
DK
9743 if (!target->specific)
9744 {
9745 gfc_typebound_proc* overridden_tbp;
9746 gfc_tbp_generic* g;
9747 const char* target_name;
9748
9749 target_name = target->specific_st->name;
9750
9751 /* Defined for this type directly. */
e34ccb4c 9752 if (target->specific_st->n.tb)
e157f736 9753 {
e34ccb4c 9754 target->specific = target->specific_st->n.tb;
e157f736
DK
9755 goto specific_found;
9756 }
9757
9758 /* Look for an inherited specific binding. */
9759 if (super_type)
9760 {
4a44a72d
DK
9761 inherited = gfc_find_typebound_proc (super_type, NULL, target_name,
9762 true, NULL);
e157f736
DK
9763
9764 if (inherited)
9765 {
e34ccb4c
DK
9766 gcc_assert (inherited->n.tb);
9767 target->specific = inherited->n.tb;
e157f736
DK
9768 goto specific_found;
9769 }
9770 }
9771
9772 gfc_error ("Undefined specific binding '%s' as target of GENERIC '%s'"
94747289 9773 " at %L", target_name, name, &p->where);
e157f736
DK
9774 return FAILURE;
9775
9776 /* Once we've found the specific binding, check it is not ambiguous with
9777 other specifics already found or inherited for the same GENERIC. */
9778specific_found:
9779 gcc_assert (target->specific);
9780
9781 /* This must really be a specific binding! */
9782 if (target->specific->is_generic)
9783 {
9784 gfc_error ("GENERIC '%s' at %L must target a specific binding,"
94747289 9785 " '%s' is GENERIC, too", name, &p->where, target_name);
e157f736
DK
9786 return FAILURE;
9787 }
9788
9789 /* Check those already resolved on this type directly. */
94747289 9790 for (g = p->u.generic; g; g = g->next)
e157f736 9791 if (g != target && g->specific
94747289 9792 && check_generic_tbp_ambiguity (target, g, name, p->where)
e157f736
DK
9793 == FAILURE)
9794 return FAILURE;
9795
9796 /* Check for ambiguity with inherited specific targets. */
94747289 9797 for (overridden_tbp = p->overridden; overridden_tbp;
e157f736
DK
9798 overridden_tbp = overridden_tbp->overridden)
9799 if (overridden_tbp->is_generic)
9800 {
9801 for (g = overridden_tbp->u.generic; g; g = g->next)
9802 {
9803 gcc_assert (g->specific);
9804 if (check_generic_tbp_ambiguity (target, g,
94747289 9805 name, p->where) == FAILURE)
e157f736
DK
9806 return FAILURE;
9807 }
9808 }
9809 }
9810
9811 /* If we attempt to "overwrite" a specific binding, this is an error. */
94747289 9812 if (p->overridden && !p->overridden->is_generic)
e157f736
DK
9813 {
9814 gfc_error ("GENERIC '%s' at %L can't overwrite specific binding with"
94747289 9815 " the same name", name, &p->where);
e157f736
DK
9816 return FAILURE;
9817 }
9818
9819 /* Take the SUBROUTINE/FUNCTION attributes of the first specific target, as
9820 all must have the same attributes here. */
94747289 9821 first_target = p->u.generic->specific->u.specific;
e34ccb4c 9822 gcc_assert (first_target);
94747289
DK
9823 p->subroutine = first_target->n.sym->attr.subroutine;
9824 p->function = first_target->n.sym->attr.function;
e157f736
DK
9825
9826 return SUCCESS;
9827}
9828
9829
94747289
DK
9830/* Resolve a GENERIC procedure binding for a derived type. */
9831
9832static gfc_try
9833resolve_typebound_generic (gfc_symbol* derived, gfc_symtree* st)
9834{
9835 gfc_symbol* super_type;
9836
9837 /* Find the overridden binding if any. */
9838 st->n.tb->overridden = NULL;
9839 super_type = gfc_get_derived_super_type (derived);
9840 if (super_type)
9841 {
9842 gfc_symtree* overridden;
4a44a72d
DK
9843 overridden = gfc_find_typebound_proc (super_type, NULL, st->name,
9844 true, NULL);
94747289
DK
9845
9846 if (overridden && overridden->n.tb)
9847 st->n.tb->overridden = overridden->n.tb;
9848 }
9849
9850 /* Resolve using worker function. */
9851 return resolve_tb_generic_targets (super_type, st->n.tb, st->name);
9852}
9853
9854
b325faf9
DK
9855/* Retrieve the target-procedure of an operator binding and do some checks in
9856 common for intrinsic and user-defined type-bound operators. */
9857
9858static gfc_symbol*
9859get_checked_tb_operator_target (gfc_tbp_generic* target, locus where)
9860{
9861 gfc_symbol* target_proc;
9862
9863 gcc_assert (target->specific && !target->specific->is_generic);
9864 target_proc = target->specific->u.specific->n.sym;
9865 gcc_assert (target_proc);
9866
9867 /* All operator bindings must have a passed-object dummy argument. */
9868 if (target->specific->nopass)
9869 {
9870 gfc_error ("Type-bound operator at %L can't be NOPASS", &where);
9871 return NULL;
9872 }
9873
9874 return target_proc;
9875}
9876
9877
94747289
DK
9878/* Resolve a type-bound intrinsic operator. */
9879
9880static gfc_try
9881resolve_typebound_intrinsic_op (gfc_symbol* derived, gfc_intrinsic_op op,
9882 gfc_typebound_proc* p)
9883{
9884 gfc_symbol* super_type;
9885 gfc_tbp_generic* target;
9886
9887 /* If there's already an error here, do nothing (but don't fail again). */
9888 if (p->error)
9889 return SUCCESS;
9890
9891 /* Operators should always be GENERIC bindings. */
9892 gcc_assert (p->is_generic);
9893
9894 /* Look for an overridden binding. */
9895 super_type = gfc_get_derived_super_type (derived);
9896 if (super_type && super_type->f2k_derived)
9897 p->overridden = gfc_find_typebound_intrinsic_op (super_type, NULL,
4a44a72d 9898 op, true, NULL);
94747289
DK
9899 else
9900 p->overridden = NULL;
9901
9902 /* Resolve general GENERIC properties using worker function. */
9903 if (resolve_tb_generic_targets (super_type, p, gfc_op2string (op)) == FAILURE)
9904 goto error;
9905
9906 /* Check the targets to be procedures of correct interface. */
9907 for (target = p->u.generic; target; target = target->next)
9908 {
9909 gfc_symbol* target_proc;
9910
b325faf9
DK
9911 target_proc = get_checked_tb_operator_target (target, p->where);
9912 if (!target_proc)
4a44a72d 9913 goto error;
94747289
DK
9914
9915 if (!gfc_check_operator_interface (target_proc, op, p->where))
4a44a72d 9916 goto error;
94747289
DK
9917 }
9918
9919 return SUCCESS;
9920
9921error:
9922 p->error = 1;
9923 return FAILURE;
9924}
9925
9926
9927/* Resolve a type-bound user operator (tree-walker callback). */
30b608eb
DK
9928
9929static gfc_symbol* resolve_bindings_derived;
9930static gfc_try resolve_bindings_result;
9931
94747289
DK
9932static gfc_try check_uop_procedure (gfc_symbol* sym, locus where);
9933
9934static void
9935resolve_typebound_user_op (gfc_symtree* stree)
9936{
9937 gfc_symbol* super_type;
9938 gfc_tbp_generic* target;
9939
9940 gcc_assert (stree && stree->n.tb);
9941
9942 if (stree->n.tb->error)
9943 return;
9944
9945 /* Operators should always be GENERIC bindings. */
9946 gcc_assert (stree->n.tb->is_generic);
9947
9948 /* Find overridden procedure, if any. */
9949 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
9950 if (super_type && super_type->f2k_derived)
9951 {
9952 gfc_symtree* overridden;
9953 overridden = gfc_find_typebound_user_op (super_type, NULL,
4a44a72d 9954 stree->name, true, NULL);
94747289
DK
9955
9956 if (overridden && overridden->n.tb)
9957 stree->n.tb->overridden = overridden->n.tb;
9958 }
9959 else
9960 stree->n.tb->overridden = NULL;
9961
9962 /* Resolve basically using worker function. */
9963 if (resolve_tb_generic_targets (super_type, stree->n.tb, stree->name)
9964 == FAILURE)
9965 goto error;
9966
9967 /* Check the targets to be functions of correct interface. */
9968 for (target = stree->n.tb->u.generic; target; target = target->next)
9969 {
9970 gfc_symbol* target_proc;
9971
b325faf9
DK
9972 target_proc = get_checked_tb_operator_target (target, stree->n.tb->where);
9973 if (!target_proc)
9974 goto error;
94747289
DK
9975
9976 if (check_uop_procedure (target_proc, stree->n.tb->where) == FAILURE)
9977 goto error;
9978 }
9979
9980 return;
9981
9982error:
9983 resolve_bindings_result = FAILURE;
9984 stree->n.tb->error = 1;
9985}
9986
9987
9988/* Resolve the type-bound procedures for a derived type. */
9989
30b608eb
DK
9990static void
9991resolve_typebound_procedure (gfc_symtree* stree)
9992{
9993 gfc_symbol* proc;
9994 locus where;
9995 gfc_symbol* me_arg;
9996 gfc_symbol* super_type;
9d1210f4 9997 gfc_component* comp;
30b608eb 9998
e34ccb4c
DK
9999 gcc_assert (stree);
10000
10001 /* Undefined specific symbol from GENERIC target definition. */
10002 if (!stree->n.tb)
10003 return;
10004
10005 if (stree->n.tb->error)
30b608eb
DK
10006 return;
10007
e157f736 10008 /* If this is a GENERIC binding, use that routine. */
e34ccb4c 10009 if (stree->n.tb->is_generic)
e157f736
DK
10010 {
10011 if (resolve_typebound_generic (resolve_bindings_derived, stree)
10012 == FAILURE)
10013 goto error;
10014 return;
10015 }
10016
30b608eb 10017 /* Get the target-procedure to check it. */
e34ccb4c
DK
10018 gcc_assert (!stree->n.tb->is_generic);
10019 gcc_assert (stree->n.tb->u.specific);
10020 proc = stree->n.tb->u.specific->n.sym;
10021 where = stree->n.tb->where;
30b608eb
DK
10022
10023 /* Default access should already be resolved from the parser. */
e34ccb4c 10024 gcc_assert (stree->n.tb->access != ACCESS_UNKNOWN);
30b608eb
DK
10025
10026 /* It should be a module procedure or an external procedure with explicit
b0e5fa94 10027 interface. For DEFERRED bindings, abstract interfaces are ok as well. */
30b608eb
DK
10028 if ((!proc->attr.subroutine && !proc->attr.function)
10029 || (proc->attr.proc != PROC_MODULE
10030 && proc->attr.if_source != IFSRC_IFBODY)
e34ccb4c 10031 || (proc->attr.abstract && !stree->n.tb->deferred))
30b608eb
DK
10032 {
10033 gfc_error ("'%s' must be a module procedure or an external procedure with"
10034 " an explicit interface at %L", proc->name, &where);
10035 goto error;
10036 }
e34ccb4c
DK
10037 stree->n.tb->subroutine = proc->attr.subroutine;
10038 stree->n.tb->function = proc->attr.function;
30b608eb
DK
10039
10040 /* Find the super-type of the current derived type. We could do this once and
10041 store in a global if speed is needed, but as long as not I believe this is
10042 more readable and clearer. */
10043 super_type = gfc_get_derived_super_type (resolve_bindings_derived);
10044
e157f736
DK
10045 /* If PASS, resolve and check arguments if not already resolved / loaded
10046 from a .mod file. */
e34ccb4c 10047 if (!stree->n.tb->nopass && stree->n.tb->pass_arg_num == 0)
30b608eb 10048 {
e34ccb4c 10049 if (stree->n.tb->pass_arg)
30b608eb
DK
10050 {
10051 gfc_formal_arglist* i;
10052
10053 /* If an explicit passing argument name is given, walk the arg-list
10054 and look for it. */
10055
10056 me_arg = NULL;
e34ccb4c 10057 stree->n.tb->pass_arg_num = 1;
30b608eb
DK
10058 for (i = proc->formal; i; i = i->next)
10059 {
e34ccb4c 10060 if (!strcmp (i->sym->name, stree->n.tb->pass_arg))
30b608eb
DK
10061 {
10062 me_arg = i->sym;
10063 break;
10064 }
e34ccb4c 10065 ++stree->n.tb->pass_arg_num;
30b608eb
DK
10066 }
10067
10068 if (!me_arg)
10069 {
10070 gfc_error ("Procedure '%s' with PASS(%s) at %L has no"
10071 " argument '%s'",
e34ccb4c
DK
10072 proc->name, stree->n.tb->pass_arg, &where,
10073 stree->n.tb->pass_arg);
30b608eb
DK
10074 goto error;
10075 }
10076 }
10077 else
10078 {
10079 /* Otherwise, take the first one; there should in fact be at least
10080 one. */
e34ccb4c 10081 stree->n.tb->pass_arg_num = 1;
30b608eb
DK
10082 if (!proc->formal)
10083 {
10084 gfc_error ("Procedure '%s' with PASS at %L must have at"
10085 " least one argument", proc->name, &where);
10086 goto error;
10087 }
10088 me_arg = proc->formal->sym;
10089 }
10090
41a394bb
DK
10091 /* Now check that the argument-type matches and the passed-object
10092 dummy argument is generally fine. */
10093
30b608eb 10094 gcc_assert (me_arg);
41a394bb 10095
cf2b3c22 10096 if (me_arg->ts.type != BT_CLASS)
30b608eb 10097 {
cf2b3c22
TB
10098 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
10099 " at %L", proc->name, &where);
30b608eb
DK
10100 goto error;
10101 }
8e1f752a 10102
cf2b3c22
TB
10103 if (me_arg->ts.u.derived->components->ts.u.derived
10104 != resolve_bindings_derived)
727e8544 10105 {
cf2b3c22
TB
10106 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10107 " the derived-type '%s'", me_arg->name, proc->name,
10108 me_arg->name, &where, resolve_bindings_derived->name);
727e8544
JW
10109 goto error;
10110 }
41a394bb
DK
10111
10112 gcc_assert (me_arg->ts.type == BT_CLASS);
10113 if (me_arg->ts.u.derived->components->as
10114 && me_arg->ts.u.derived->components->as->rank > 0)
10115 {
10116 gfc_error ("Passed-object dummy argument of '%s' at %L must be"
10117 " scalar", proc->name, &where);
10118 goto error;
10119 }
10120 if (me_arg->ts.u.derived->components->attr.allocatable)
10121 {
10122 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10123 " be ALLOCATABLE", proc->name, &where);
10124 goto error;
10125 }
10126 if (me_arg->ts.u.derived->components->attr.class_pointer)
10127 {
10128 gfc_error ("Passed-object dummy argument of '%s' at %L must not"
10129 " be POINTER", proc->name, &where);
10130 goto error;
10131 }
30b608eb
DK
10132 }
10133
10134 /* If we are extending some type, check that we don't override a procedure
10135 flagged NON_OVERRIDABLE. */
e34ccb4c 10136 stree->n.tb->overridden = NULL;
30b608eb
DK
10137 if (super_type)
10138 {
10139 gfc_symtree* overridden;
8e1f752a 10140 overridden = gfc_find_typebound_proc (super_type, NULL,
4a44a72d 10141 stree->name, true, NULL);
30b608eb 10142
e34ccb4c
DK
10143 if (overridden && overridden->n.tb)
10144 stree->n.tb->overridden = overridden->n.tb;
e157f736 10145
30b608eb
DK
10146 if (overridden && check_typebound_override (stree, overridden) == FAILURE)
10147 goto error;
10148 }
10149
9d1210f4
DK
10150 /* See if there's a name collision with a component directly in this type. */
10151 for (comp = resolve_bindings_derived->components; comp; comp = comp->next)
10152 if (!strcmp (comp->name, stree->name))
10153 {
10154 gfc_error ("Procedure '%s' at %L has the same name as a component of"
10155 " '%s'",
10156 stree->name, &where, resolve_bindings_derived->name);
10157 goto error;
10158 }
10159
10160 /* Try to find a name collision with an inherited component. */
10161 if (super_type && gfc_find_component (super_type, stree->name, true, true))
10162 {
10163 gfc_error ("Procedure '%s' at %L has the same name as an inherited"
10164 " component of '%s'",
10165 stree->name, &where, resolve_bindings_derived->name);
10166 goto error;
10167 }
10168
e34ccb4c 10169 stree->n.tb->error = 0;
30b608eb
DK
10170 return;
10171
10172error:
10173 resolve_bindings_result = FAILURE;
e34ccb4c 10174 stree->n.tb->error = 1;
30b608eb
DK
10175}
10176
10177static gfc_try
10178resolve_typebound_procedures (gfc_symbol* derived)
10179{
94747289 10180 int op;
94747289 10181
e34ccb4c 10182 if (!derived->f2k_derived || !derived->f2k_derived->tb_sym_root)
30b608eb
DK
10183 return SUCCESS;
10184
10185 resolve_bindings_derived = derived;
10186 resolve_bindings_result = SUCCESS;
94747289
DK
10187
10188 if (derived->f2k_derived->tb_sym_root)
10189 gfc_traverse_symtree (derived->f2k_derived->tb_sym_root,
10190 &resolve_typebound_procedure);
10191
94747289
DK
10192 if (derived->f2k_derived->tb_uop_root)
10193 gfc_traverse_symtree (derived->f2k_derived->tb_uop_root,
10194 &resolve_typebound_user_op);
10195
10196 for (op = 0; op != GFC_INTRINSIC_OPS; ++op)
10197 {
10198 gfc_typebound_proc* p = derived->f2k_derived->tb_op[op];
10199 if (p && resolve_typebound_intrinsic_op (derived, (gfc_intrinsic_op) op,
10200 p) == FAILURE)
10201 resolve_bindings_result = FAILURE;
94747289 10202 }
30b608eb
DK
10203
10204 return resolve_bindings_result;
10205}
10206
10207
9d5c21c1
PT
10208/* Add a derived type to the dt_list. The dt_list is used in trans-types.c
10209 to give all identical derived types the same backend_decl. */
10210static void
10211add_dt_to_dt_list (gfc_symbol *derived)
10212{
10213 gfc_dt_list *dt_list;
10214
10215 for (dt_list = gfc_derived_types; dt_list; dt_list = dt_list->next)
10216 if (derived == dt_list->derived)
10217 break;
10218
10219 if (dt_list == NULL)
10220 {
10221 dt_list = gfc_get_dt_list ();
10222 dt_list->next = gfc_derived_types;
10223 dt_list->derived = derived;
10224 gfc_derived_types = dt_list;
10225 }
10226}
10227
10228
b0e5fa94
DK
10229/* Ensure that a derived-type is really not abstract, meaning that every
10230 inherited DEFERRED binding is overridden by a non-DEFERRED one. */
10231
10232static gfc_try
10233ensure_not_abstract_walker (gfc_symbol* sub, gfc_symtree* st)
10234{
10235 if (!st)
10236 return SUCCESS;
10237
10238 if (ensure_not_abstract_walker (sub, st->left) == FAILURE)
10239 return FAILURE;
10240 if (ensure_not_abstract_walker (sub, st->right) == FAILURE)
10241 return FAILURE;
10242
e34ccb4c 10243 if (st->n.tb && st->n.tb->deferred)
b0e5fa94
DK
10244 {
10245 gfc_symtree* overriding;
4a44a72d 10246 overriding = gfc_find_typebound_proc (sub, NULL, st->name, true, NULL);
e34ccb4c
DK
10247 gcc_assert (overriding && overriding->n.tb);
10248 if (overriding->n.tb->deferred)
b0e5fa94
DK
10249 {
10250 gfc_error ("Derived-type '%s' declared at %L must be ABSTRACT because"
10251 " '%s' is DEFERRED and not overridden",
10252 sub->name, &sub->declared_at, st->name);
10253 return FAILURE;
10254 }
10255 }
10256
10257 return SUCCESS;
10258}
10259
10260static gfc_try
10261ensure_not_abstract (gfc_symbol* sub, gfc_symbol* ancestor)
10262{
10263 /* The algorithm used here is to recursively travel up the ancestry of sub
10264 and for each ancestor-type, check all bindings. If any of them is
10265 DEFERRED, look it up starting from sub and see if the found (overriding)
10266 binding is not DEFERRED.
10267 This is not the most efficient way to do this, but it should be ok and is
10268 clearer than something sophisticated. */
10269
10270 gcc_assert (ancestor && ancestor->attr.abstract && !sub->attr.abstract);
10271
10272 /* Walk bindings of this ancestor. */
10273 if (ancestor->f2k_derived)
10274 {
10275 gfc_try t;
e34ccb4c 10276 t = ensure_not_abstract_walker (sub, ancestor->f2k_derived->tb_sym_root);
b0e5fa94
DK
10277 if (t == FAILURE)
10278 return FAILURE;
10279 }
10280
10281 /* Find next ancestor type and recurse on it. */
10282 ancestor = gfc_get_derived_super_type (ancestor);
10283 if (ancestor)
10284 return ensure_not_abstract (sub, ancestor);
10285
10286 return SUCCESS;
10287}
10288
10289
acbdc378
JW
10290static void resolve_symbol (gfc_symbol *sym);
10291
10292
110eec24
TS
10293/* Resolve the components of a derived type. */
10294
17b1d2a0 10295static gfc_try
2ed8d224 10296resolve_fl_derived (gfc_symbol *sym)
110eec24 10297{
9d1210f4 10298 gfc_symbol* super_type;
110eec24 10299 gfc_component *c;
2ed8d224 10300 int i;
110eec24 10301
9d1210f4
DK
10302 super_type = gfc_get_derived_super_type (sym);
10303
e157f736
DK
10304 /* Ensure the extended type gets resolved before we do. */
10305 if (super_type && resolve_fl_derived (super_type) == FAILURE)
10306 return FAILURE;
10307
52f49934 10308 /* An ABSTRACT type must be extensible. */
cf2b3c22 10309 if (sym->attr.abstract && !gfc_type_is_extensible (sym))
52f49934
DK
10310 {
10311 gfc_error ("Non-extensible derived-type '%s' at %L must not be ABSTRACT",
10312 sym->name, &sym->declared_at);
10313 return FAILURE;
10314 }
10315
110eec24
TS
10316 for (c = sym->components; c != NULL; c = c->next)
10317 {
713485cc
JW
10318 if (c->attr.proc_pointer && c->ts.interface)
10319 {
10320 if (c->ts.interface->attr.procedure)
10321 gfc_error ("Interface '%s', used by procedure pointer component "
10322 "'%s' at %L, is declared in a later PROCEDURE statement",
10323 c->ts.interface->name, c->name, &c->loc);
10324
10325 /* Get the attributes from the interface (now resolved). */
10326 if (c->ts.interface->attr.if_source
10327 || c->ts.interface->attr.intrinsic)
10328 {
10329 gfc_symbol *ifc = c->ts.interface;
10330
acbdc378
JW
10331 if (ifc->formal && !ifc->formal_ns)
10332 resolve_symbol (ifc);
10333
713485cc
JW
10334 if (ifc->attr.intrinsic)
10335 resolve_intrinsic (ifc, &ifc->declared_at);
10336
10337 if (ifc->result)
f64edc8b
JW
10338 {
10339 c->ts = ifc->result->ts;
10340 c->attr.allocatable = ifc->result->attr.allocatable;
10341 c->attr.pointer = ifc->result->attr.pointer;
10342 c->attr.dimension = ifc->result->attr.dimension;
10343 c->as = gfc_copy_array_spec (ifc->result->as);
10344 }
10345 else
10346 {
10347 c->ts = ifc->ts;
10348 c->attr.allocatable = ifc->attr.allocatable;
10349 c->attr.pointer = ifc->attr.pointer;
10350 c->attr.dimension = ifc->attr.dimension;
10351 c->as = gfc_copy_array_spec (ifc->as);
10352 }
713485cc
JW
10353 c->ts.interface = ifc;
10354 c->attr.function = ifc->attr.function;
10355 c->attr.subroutine = ifc->attr.subroutine;
7e196f89 10356 gfc_copy_formal_args_ppc (c, ifc);
713485cc 10357
713485cc
JW
10358 c->attr.pure = ifc->attr.pure;
10359 c->attr.elemental = ifc->attr.elemental;
713485cc
JW
10360 c->attr.recursive = ifc->attr.recursive;
10361 c->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 10362 c->attr.ext_attr |= ifc->attr.ext_attr;
f64edc8b
JW
10363 /* Replace symbols in array spec. */
10364 if (c->as)
713485cc
JW
10365 {
10366 int i;
10367 for (i = 0; i < c->as->rank; i++)
10368 {
f64edc8b
JW
10369 gfc_expr_replace_comp (c->as->lower[i], c);
10370 gfc_expr_replace_comp (c->as->upper[i], c);
713485cc 10371 }
f64edc8b 10372 }
713485cc 10373 /* Copy char length. */
bc21d315 10374 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
713485cc 10375 {
b76e28c6 10376 c->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
50dbf0b4 10377 gfc_expr_replace_comp (c->ts.u.cl->length, c);
713485cc
JW
10378 }
10379 }
10380 else if (c->ts.interface->name[0] != '\0')
10381 {
10382 gfc_error ("Interface '%s' of procedure pointer component "
10383 "'%s' at %L must be explicit", c->ts.interface->name,
10384 c->name, &c->loc);
10385 return FAILURE;
10386 }
10387 }
10388 else if (c->attr.proc_pointer && c->ts.type == BT_UNKNOWN)
10389 {
6c036626
JW
10390 /* Since PPCs are not implicitly typed, a PPC without an explicit
10391 interface must be a subroutine. */
10392 gfc_add_subroutine (&c->attr, c->name, &c->loc);
713485cc
JW
10393 }
10394
90661f26
JW
10395 /* Procedure pointer components: Check PASS arg. */
10396 if (c->attr.proc_pointer && !c->tb->nopass && c->tb->pass_arg_num == 0)
10397 {
10398 gfc_symbol* me_arg;
10399
10400 if (c->tb->pass_arg)
10401 {
10402 gfc_formal_arglist* i;
10403
10404 /* If an explicit passing argument name is given, walk the arg-list
10405 and look for it. */
10406
10407 me_arg = NULL;
10408 c->tb->pass_arg_num = 1;
10409 for (i = c->formal; i; i = i->next)
10410 {
10411 if (!strcmp (i->sym->name, c->tb->pass_arg))
10412 {
10413 me_arg = i->sym;
10414 break;
10415 }
10416 c->tb->pass_arg_num++;
10417 }
10418
10419 if (!me_arg)
10420 {
10421 gfc_error ("Procedure pointer component '%s' with PASS(%s) "
10422 "at %L has no argument '%s'", c->name,
10423 c->tb->pass_arg, &c->loc, c->tb->pass_arg);
10424 c->tb->error = 1;
10425 return FAILURE;
10426 }
10427 }
10428 else
10429 {
10430 /* Otherwise, take the first one; there should in fact be at least
10431 one. */
10432 c->tb->pass_arg_num = 1;
10433 if (!c->formal)
10434 {
10435 gfc_error ("Procedure pointer component '%s' with PASS at %L "
10436 "must have at least one argument",
10437 c->name, &c->loc);
10438 c->tb->error = 1;
10439 return FAILURE;
10440 }
10441 me_arg = c->formal->sym;
10442 }
10443
10444 /* Now check that the argument-type matches. */
10445 gcc_assert (me_arg);
cf2b3c22
TB
10446 if ((me_arg->ts.type != BT_DERIVED && me_arg->ts.type != BT_CLASS)
10447 || (me_arg->ts.type == BT_DERIVED && me_arg->ts.u.derived != sym)
10448 || (me_arg->ts.type == BT_CLASS
10449 && me_arg->ts.u.derived->components->ts.u.derived != sym))
90661f26
JW
10450 {
10451 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L must be of"
10452 " the derived type '%s'", me_arg->name, c->name,
10453 me_arg->name, &c->loc, sym->name);
10454 c->tb->error = 1;
10455 return FAILURE;
10456 }
10457
10458 /* Check for C453. */
10459 if (me_arg->attr.dimension)
10460 {
10461 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10462 "must be scalar", me_arg->name, c->name, me_arg->name,
10463 &c->loc);
10464 c->tb->error = 1;
10465 return FAILURE;
10466 }
10467
10468 if (me_arg->attr.pointer)
10469 {
10470 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10471 "may not have the POINTER attribute", me_arg->name,
10472 c->name, me_arg->name, &c->loc);
10473 c->tb->error = 1;
10474 return FAILURE;
10475 }
10476
10477 if (me_arg->attr.allocatable)
10478 {
10479 gfc_error ("Argument '%s' of '%s' with PASS(%s) at %L "
10480 "may not be ALLOCATABLE", me_arg->name, c->name,
10481 me_arg->name, &c->loc);
10482 c->tb->error = 1;
10483 return FAILURE;
10484 }
10485
cf2b3c22 10486 if (gfc_type_is_extensible (sym) && me_arg->ts.type != BT_CLASS)
727e8544 10487 gfc_error ("Non-polymorphic passed-object dummy argument of '%s'"
cf2b3c22 10488 " at %L", c->name, &c->loc);
90661f26
JW
10489
10490 }
10491
52f49934
DK
10492 /* Check type-spec if this is not the parent-type component. */
10493 if ((!sym->attr.extension || c != sym->components)
10494 && resolve_typespec_used (&c->ts, &c->loc, c->name) == FAILURE)
10495 return FAILURE;
10496
9d1210f4
DK
10497 /* If this type is an extension, see if this component has the same name
10498 as an inherited type-bound procedure. */
8e1f752a 10499 if (super_type
4a44a72d 10500 && gfc_find_typebound_proc (super_type, NULL, c->name, true, NULL))
9d1210f4
DK
10501 {
10502 gfc_error ("Component '%s' of '%s' at %L has the same name as an"
10503 " inherited type-bound procedure",
10504 c->name, sym->name, &c->loc);
10505 return FAILURE;
10506 }
10507
50dbf0b4 10508 if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer)
110eec24 10509 {
bc21d315
JW
10510 if (c->ts.u.cl->length == NULL
10511 || (resolve_charlen (c->ts.u.cl) == FAILURE)
10512 || !gfc_is_constant_expr (c->ts.u.cl->length))
110eec24
TS
10513 {
10514 gfc_error ("Character length of component '%s' needs to "
e25a0da3 10515 "be a constant specification expression at %L",
110eec24 10516 c->name,
bc21d315 10517 c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
110eec24
TS
10518 return FAILURE;
10519 }
10520 }
10521
2ed8d224 10522 if (c->ts.type == BT_DERIVED
edf1eac2
SK
10523 && sym->component_access != ACCESS_PRIVATE
10524 && gfc_check_access (sym->attr.access, sym->ns->default_access)
bc21d315
JW
10525 && !is_sym_host_assoc (c->ts.u.derived, sym->ns)
10526 && !c->ts.u.derived->attr.use_assoc
10527 && !gfc_check_access (c->ts.u.derived->attr.access,
10528 c->ts.u.derived->ns->default_access)
cbb9a26e
JW
10529 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: the component '%s' "
10530 "is a PRIVATE type and cannot be a component of "
10531 "'%s', which is PUBLIC at %L", c->name,
10532 sym->name, &sym->declared_at) == FAILURE)
10533 return FAILURE;
2ed8d224 10534
f970c857
PT
10535 if (sym->attr.sequence)
10536 {
bc21d315 10537 if (c->ts.type == BT_DERIVED && c->ts.u.derived->attr.sequence == 0)
f970c857
PT
10538 {
10539 gfc_error ("Component %s of SEQUENCE type declared at %L does "
10540 "not have the SEQUENCE attribute",
bc21d315 10541 c->ts.u.derived->name, &sym->declared_at);
f970c857
PT
10542 return FAILURE;
10543 }
10544 }
10545
d4b7d0f0 10546 if (c->ts.type == BT_DERIVED && c->attr.pointer
bc21d315
JW
10547 && c->ts.u.derived->components == NULL
10548 && !c->ts.u.derived->attr.zero_comp)
982186b1
PT
10549 {
10550 gfc_error ("The pointer component '%s' of '%s' at %L is a type "
10551 "that has not been declared", c->name, sym->name,
10552 &c->loc);
10553 return FAILURE;
10554 }
10555
727e8544 10556 /* C437. */
cf2b3c22
TB
10557 if (c->ts.type == BT_CLASS
10558 && !(c->ts.u.derived->components->attr.pointer
10559 || c->ts.u.derived->components->attr.allocatable))
727e8544
JW
10560 {
10561 gfc_error ("Component '%s' with CLASS at %L must be allocatable "
10562 "or pointer", c->name, &c->loc);
10563 return FAILURE;
10564 }
10565
9d5c21c1
PT
10566 /* Ensure that all the derived type components are put on the
10567 derived type list; even in formal namespaces, where derived type
10568 pointer components might not have been declared. */
10569 if (c->ts.type == BT_DERIVED
bc21d315
JW
10570 && c->ts.u.derived
10571 && c->ts.u.derived->components
d4b7d0f0 10572 && c->attr.pointer
bc21d315
JW
10573 && sym != c->ts.u.derived)
10574 add_dt_to_dt_list (c->ts.u.derived);
9d5c21c1 10575
e35bbb23
JW
10576 if (c->attr.pointer || c->attr.proc_pointer || c->attr.allocatable
10577 || c->as == NULL)
2ed8d224
PT
10578 continue;
10579
10580 for (i = 0; i < c->as->rank; i++)
10581 {
10582 if (c->as->lower[i] == NULL
edf1eac2 10583 || (resolve_index_expr (c->as->lower[i]) == FAILURE)
bdad0683 10584 || !gfc_is_constant_expr (c->as->lower[i])
edf1eac2
SK
10585 || c->as->upper[i] == NULL
10586 || (resolve_index_expr (c->as->upper[i]) == FAILURE)
10587 || !gfc_is_constant_expr (c->as->upper[i]))
2ed8d224
PT
10588 {
10589 gfc_error ("Component '%s' of '%s' at %L must have "
e25a0da3 10590 "constant array bounds",
2ed8d224
PT
10591 c->name, sym->name, &c->loc);
10592 return FAILURE;
10593 }
10594 }
110eec24 10595 }
05c1e3a7 10596
30b608eb
DK
10597 /* Resolve the type-bound procedures. */
10598 if (resolve_typebound_procedures (sym) == FAILURE)
10599 return FAILURE;
10600
34523524
DK
10601 /* Resolve the finalizer procedures. */
10602 if (gfc_resolve_finalizers (sym) == FAILURE)
10603 return FAILURE;
10604
b0e5fa94
DK
10605 /* If this is a non-ABSTRACT type extending an ABSTRACT one, ensure that
10606 all DEFERRED bindings are overridden. */
10607 if (super_type && super_type->attr.abstract && !sym->attr.abstract
10608 && ensure_not_abstract (sym, super_type) == FAILURE)
10609 return FAILURE;
10610
6b887797 10611 /* Add derived type to the derived type list. */
9d5c21c1 10612 add_dt_to_dt_list (sym);
6b887797 10613
110eec24
TS
10614 return SUCCESS;
10615}
10616
2ed8d224 10617
17b1d2a0 10618static gfc_try
3e1cf500
PT
10619resolve_fl_namelist (gfc_symbol *sym)
10620{
10621 gfc_namelist *nl;
10622 gfc_symbol *nlsym;
10623
10624 /* Reject PRIVATE objects in a PUBLIC namelist. */
10625 if (gfc_check_access(sym->attr.access, sym->ns->default_access))
10626 {
10627 for (nl = sym->namelist; nl; nl = nl->next)
10628 {
3dbf6538 10629 if (!nl->sym->attr.use_assoc
c867b7b6 10630 && !is_sym_host_assoc (nl->sym, sym->ns)
3dbf6538 10631 && !gfc_check_access(nl->sym->attr.access,
5cca320d 10632 nl->sym->ns->default_access))
3e1cf500 10633 {
5cca320d
DF
10634 gfc_error ("NAMELIST object '%s' was declared PRIVATE and "
10635 "cannot be member of PUBLIC namelist '%s' at %L",
10636 nl->sym->name, sym->name, &sym->declared_at);
10637 return FAILURE;
10638 }
10639
3dbf6538
DF
10640 /* Types with private components that came here by USE-association. */
10641 if (nl->sym->ts.type == BT_DERIVED
bc21d315 10642 && derived_inaccessible (nl->sym->ts.u.derived))
3dbf6538
DF
10643 {
10644 gfc_error ("NAMELIST object '%s' has use-associated PRIVATE "
10645 "components and cannot be member of namelist '%s' at %L",
10646 nl->sym->name, sym->name, &sym->declared_at);
10647 return FAILURE;
10648 }
10649
10650 /* Types with private components that are defined in the same module. */
5cca320d 10651 if (nl->sym->ts.type == BT_DERIVED
bc21d315
JW
10652 && !is_sym_host_assoc (nl->sym->ts.u.derived, sym->ns)
10653 && !gfc_check_access (nl->sym->ts.u.derived->attr.private_comp
3dbf6538
DF
10654 ? ACCESS_PRIVATE : ACCESS_UNKNOWN,
10655 nl->sym->ns->default_access))
5cca320d
DF
10656 {
10657 gfc_error ("NAMELIST object '%s' has PRIVATE components and "
10658 "cannot be a member of PUBLIC namelist '%s' at %L",
10659 nl->sym->name, sym->name, &sym->declared_at);
3e1cf500
PT
10660 return FAILURE;
10661 }
10662 }
10663 }
10664
5046aff5
PT
10665 for (nl = sym->namelist; nl; nl = nl->next)
10666 {
5cca320d
DF
10667 /* Reject namelist arrays of assumed shape. */
10668 if (nl->sym->as && nl->sym->as->type == AS_ASSUMED_SHAPE
10669 && gfc_notify_std (GFC_STD_F2003, "NAMELIST array object '%s' "
10670 "must not have assumed shape in namelist "
10671 "'%s' at %L", nl->sym->name, sym->name,
10672 &sym->declared_at) == FAILURE)
10673 return FAILURE;
10674
10675 /* Reject namelist arrays that are not constant shape. */
5046aff5
PT
10676 if (is_non_constant_shape_array (nl->sym))
10677 {
5cca320d
DF
10678 gfc_error ("NAMELIST array object '%s' must have constant "
10679 "shape in namelist '%s' at %L", nl->sym->name,
10680 sym->name, &sym->declared_at);
10681 return FAILURE;
10682 }
10683
10684 /* Namelist objects cannot have allocatable or pointer components. */
10685 if (nl->sym->ts.type != BT_DERIVED)
10686 continue;
10687
bc21d315 10688 if (nl->sym->ts.u.derived->attr.alloc_comp)
5cca320d
DF
10689 {
10690 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10691 "have ALLOCATABLE components",
10692 nl->sym->name, sym->name, &sym->declared_at);
5046aff5
PT
10693 return FAILURE;
10694 }
5046aff5 10695
bc21d315 10696 if (nl->sym->ts.u.derived->attr.pointer_comp)
5046aff5 10697 {
5cca320d
DF
10698 gfc_error ("NAMELIST object '%s' in namelist '%s' at %L cannot "
10699 "have POINTER components",
10700 nl->sym->name, sym->name, &sym->declared_at);
5046aff5
PT
10701 return FAILURE;
10702 }
3e1cf500
PT
10703 }
10704
5cca320d 10705
3e1cf500 10706 /* 14.1.2 A module or internal procedure represent local entities
847b053d 10707 of the same type as a namelist member and so are not allowed. */
3e1cf500
PT
10708 for (nl = sym->namelist; nl; nl = nl->next)
10709 {
982186b1
PT
10710 if (nl->sym->ts.kind != 0 && nl->sym->attr.flavor == FL_VARIABLE)
10711 continue;
847b053d
PT
10712
10713 if (nl->sym->attr.function && nl->sym == nl->sym->result)
10714 if ((nl->sym == sym->ns->proc_name)
10715 ||
10716 (sym->ns->parent && nl->sym == sym->ns->parent->proc_name))
10717 continue;
10718
3e1cf500 10719 nlsym = NULL;
847b053d
PT
10720 if (nl->sym && nl->sym->name)
10721 gfc_find_symbol (nl->sym->name, sym->ns, 1, &nlsym);
982186b1
PT
10722 if (nlsym && nlsym->attr.flavor == FL_PROCEDURE)
10723 {
10724 gfc_error ("PROCEDURE attribute conflicts with NAMELIST "
10725 "attribute in '%s' at %L", nlsym->name,
10726 &sym->declared_at);
10727 return FAILURE;
10728 }
3e1cf500
PT
10729 }
10730
10731 return SUCCESS;
10732}
10733
10734
17b1d2a0 10735static gfc_try
2ed8d224
PT
10736resolve_fl_parameter (gfc_symbol *sym)
10737{
10738 /* A parameter array's shape needs to be constant. */
c317bc40
DF
10739 if (sym->as != NULL
10740 && (sym->as->type == AS_DEFERRED
10741 || is_non_constant_shape_array (sym)))
2ed8d224
PT
10742 {
10743 gfc_error ("Parameter array '%s' at %L cannot be automatic "
c317bc40 10744 "or of deferred shape", sym->name, &sym->declared_at);
2ed8d224
PT
10745 return FAILURE;
10746 }
10747
10748 /* Make sure a parameter that has been implicitly typed still
10749 matches the implicit type, since PARAMETER statements can precede
10750 IMPLICIT statements. */
10751 if (sym->attr.implicit_type
713485cc
JW
10752 && !gfc_compare_types (&sym->ts, gfc_get_default_type (sym->name,
10753 sym->ns)))
2ed8d224
PT
10754 {
10755 gfc_error ("Implicitly typed PARAMETER '%s' at %L doesn't match a "
10756 "later IMPLICIT type", sym->name, &sym->declared_at);
10757 return FAILURE;
10758 }
10759
10760 /* Make sure the types of derived parameters are consistent. This
10761 type checking is deferred until resolution because the type may
10762 refer to a derived type from the host. */
10763 if (sym->ts.type == BT_DERIVED
edf1eac2 10764 && !gfc_compare_types (&sym->ts, &sym->value->ts))
2ed8d224
PT
10765 {
10766 gfc_error ("Incompatible derived type in PARAMETER at %L",
10767 &sym->value->where);
10768 return FAILURE;
10769 }
10770 return SUCCESS;
10771}
10772
10773
6de9cd9a
DN
10774/* Do anything necessary to resolve a symbol. Right now, we just
10775 assume that an otherwise unknown symbol is a variable. This sort
10776 of thing commonly happens for symbols in module. */
10777
10778static void
edf1eac2 10779resolve_symbol (gfc_symbol *sym)
6de9cd9a 10780{
a34437a1 10781 int check_constant, mp_flag;
219fa8c3
SK
10782 gfc_symtree *symtree;
10783 gfc_symtree *this_symtree;
10784 gfc_namespace *ns;
10785 gfc_component *c;
6de9cd9a
DN
10786
10787 if (sym->attr.flavor == FL_UNKNOWN)
10788 {
24d36d28
PT
10789
10790 /* If we find that a flavorless symbol is an interface in one of the
10791 parent namespaces, find its symtree in this namespace, free the
10792 symbol and set the symtree to point to the interface symbol. */
10793 for (ns = gfc_current_ns->parent; ns; ns = ns->parent)
10794 {
10795 symtree = gfc_find_symtree (ns->sym_root, sym->name);
10796 if (symtree && symtree->n.sym->generic)
10797 {
10798 this_symtree = gfc_find_symtree (gfc_current_ns->sym_root,
10799 sym->name);
10800 sym->refs--;
10801 if (!sym->refs)
10802 gfc_free_symbol (sym);
10803 symtree->n.sym->refs++;
10804 this_symtree->n.sym = symtree->n.sym;
10805 return;
10806 }
10807 }
10808
10809 /* Otherwise give it a flavor according to such attributes as
10810 it has. */
6de9cd9a
DN
10811 if (sym->attr.external == 0 && sym->attr.intrinsic == 0)
10812 sym->attr.flavor = FL_VARIABLE;
10813 else
10814 {
10815 sym->attr.flavor = FL_PROCEDURE;
10816 if (sym->attr.dimension)
10817 sym->attr.function = 1;
10818 }
10819 }
10820
c73b6478
JW
10821 if (sym->attr.external && sym->ts.type != BT_UNKNOWN && !sym->attr.function)
10822 gfc_add_function (&sym->attr, sym->name, &sym->declared_at);
10823
32d99e68 10824 if (sym->attr.procedure && sym->ts.interface
69773742
JW
10825 && sym->attr.if_source != IFSRC_DECL)
10826 {
d1d919c3
JW
10827 if (sym->ts.interface == sym)
10828 {
10829 gfc_error ("PROCEDURE '%s' at %L may not be used as its own "
10830 "interface", sym->name, &sym->declared_at);
10831 return;
10832 }
32d99e68 10833 if (sym->ts.interface->attr.procedure)
d1d919c3
JW
10834 {
10835 gfc_error ("Interface '%s', used by procedure '%s' at %L, is declared"
10836 " in a later PROCEDURE statement", sym->ts.interface->name,
10837 sym->name,&sym->declared_at);
10838 return;
10839 }
ecf24057 10840
69773742 10841 /* Get the attributes from the interface (now resolved). */
713485cc
JW
10842 if (sym->ts.interface->attr.if_source
10843 || sym->ts.interface->attr.intrinsic)
69773742 10844 {
7db5da56 10845 gfc_symbol *ifc = sym->ts.interface;
c74b74a8 10846 resolve_symbol (ifc);
3afadac3
JW
10847
10848 if (ifc->attr.intrinsic)
c73b6478
JW
10849 resolve_intrinsic (ifc, &ifc->declared_at);
10850
e6a5e544
JW
10851 if (ifc->result)
10852 sym->ts = ifc->result->ts;
10853 else
10854 sym->ts = ifc->ts;
c73b6478
JW
10855 sym->ts.interface = ifc;
10856 sym->attr.function = ifc->attr.function;
10857 sym->attr.subroutine = ifc->attr.subroutine;
10858 gfc_copy_formal_args (sym, ifc);
3afadac3 10859
2d9bbb6b
TB
10860 sym->attr.allocatable = ifc->attr.allocatable;
10861 sym->attr.pointer = ifc->attr.pointer;
10862 sym->attr.pure = ifc->attr.pure;
10863 sym->attr.elemental = ifc->attr.elemental;
10864 sym->attr.dimension = ifc->attr.dimension;
10865 sym->attr.recursive = ifc->attr.recursive;
10866 sym->attr.always_explicit = ifc->attr.always_explicit;
2b374f55 10867 sym->attr.ext_attr |= ifc->attr.ext_attr;
c6acea9d
JW
10868 /* Copy array spec. */
10869 sym->as = gfc_copy_array_spec (ifc->as);
10870 if (sym->as)
10871 {
10872 int i;
10873 for (i = 0; i < sym->as->rank; i++)
10874 {
10875 gfc_expr_replace_symbols (sym->as->lower[i], sym);
10876 gfc_expr_replace_symbols (sym->as->upper[i], sym);
10877 }
10878 }
10879 /* Copy char length. */
bc21d315 10880 if (ifc->ts.type == BT_CHARACTER && ifc->ts.u.cl)
c6acea9d 10881 {
b76e28c6 10882 sym->ts.u.cl = gfc_new_charlen (sym->ns, ifc->ts.u.cl);
bc21d315 10883 gfc_expr_replace_symbols (sym->ts.u.cl->length, sym);
c6acea9d 10884 }
69773742 10885 }
32d99e68 10886 else if (sym->ts.interface->name[0] != '\0')
69773742
JW
10887 {
10888 gfc_error ("Interface '%s' of procedure '%s' at %L must be explicit",
32d99e68 10889 sym->ts.interface->name, sym->name, &sym->declared_at);
69773742
JW
10890 return;
10891 }
10892 }
10893
2ed8d224 10894 if (sym->attr.flavor == FL_DERIVED && resolve_fl_derived (sym) == FAILURE)
110eec24
TS
10895 return;
10896
6de9cd9a
DN
10897 /* Symbols that are module procedures with results (functions) have
10898 the types and array specification copied for type checking in
10899 procedures that call them, as well as for saving to a module
10900 file. These symbols can't stand the scrutiny that their results
10901 can. */
10902 mp_flag = (sym->result != NULL && sym->result != sym);
10903
eb2c598d
DF
10904
10905 /* Make sure that the intrinsic is consistent with its internal
10906 representation. This needs to be done before assigning a default
10907 type to avoid spurious warnings. */
f6038131
JW
10908 if (sym->attr.flavor != FL_MODULE && sym->attr.intrinsic
10909 && resolve_intrinsic (sym, &sym->declared_at) == FAILURE)
10910 return;
eb2c598d 10911
6de9cd9a
DN
10912 /* Assign default type to symbols that need one and don't have one. */
10913 if (sym->ts.type == BT_UNKNOWN)
10914 {
10915 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER)
d3fcc995 10916 gfc_set_default_type (sym, 1, NULL);
6de9cd9a 10917
fc9c6e5d
JW
10918 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
10919 && !sym->attr.function && !sym->attr.subroutine
10920 && gfc_get_default_type (sym->name, sym->ns)->type == BT_UNKNOWN)
10921 gfc_add_subroutine (&sym->attr, sym->name, &sym->declared_at);
10922
6de9cd9a
DN
10923 if (sym->attr.flavor == FL_PROCEDURE && sym->attr.function)
10924 {
53096259
PT
10925 /* The specific case of an external procedure should emit an error
10926 in the case that there is no implicit type. */
6de9cd9a 10927 if (!mp_flag)
53096259 10928 gfc_set_default_type (sym, sym->attr.external, NULL);
6de9cd9a
DN
10929 else
10930 {
edf1eac2 10931 /* Result may be in another namespace. */
6de9cd9a
DN
10932 resolve_symbol (sym->result);
10933
3070bab4
JW
10934 if (!sym->result->attr.proc_pointer)
10935 {
10936 sym->ts = sym->result->ts;
10937 sym->as = gfc_copy_array_spec (sym->result->as);
10938 sym->attr.dimension = sym->result->attr.dimension;
10939 sym->attr.pointer = sym->result->attr.pointer;
10940 sym->attr.allocatable = sym->result->attr.allocatable;
10941 }
6de9cd9a
DN
10942 }
10943 }
10944 }
10945
f5e440e1 10946 /* Assumed size arrays and assumed shape arrays must be dummy
05c1e3a7 10947 arguments. */
f5e440e1 10948
6de9cd9a
DN
10949 if (sym->as != NULL
10950 && (sym->as->type == AS_ASSUMED_SIZE
10951 || sym->as->type == AS_ASSUMED_SHAPE)
10952 && sym->attr.dummy == 0)
10953 {
31043f6c
FXC
10954 if (sym->as->type == AS_ASSUMED_SIZE)
10955 gfc_error ("Assumed size array at %L must be a dummy argument",
10956 &sym->declared_at);
10957 else
10958 gfc_error ("Assumed shape array at %L must be a dummy argument",
10959 &sym->declared_at);
a4ac5dd3
TS
10960 return;
10961 }
10962
6de9cd9a
DN
10963 /* Make sure symbols with known intent or optional are really dummy
10964 variable. Because of ENTRY statement, this has to be deferred
10965 until resolution time. */
10966
2ed8d224 10967 if (!sym->attr.dummy
edf1eac2 10968 && (sym->attr.optional || sym->attr.intent != INTENT_UNKNOWN))
6de9cd9a
DN
10969 {
10970 gfc_error ("Symbol at %L is not a DUMMY variable", &sym->declared_at);
10971 return;
10972 }
10973
06469efd
PT
10974 if (sym->attr.value && !sym->attr.dummy)
10975 {
10976 gfc_error ("'%s' at %L cannot have the VALUE attribute because "
1084b6b0 10977 "it is not a dummy argument", sym->name, &sym->declared_at);
06469efd
PT
10978 return;
10979 }
10980
1084b6b0
TB
10981 if (sym->attr.value && sym->ts.type == BT_CHARACTER)
10982 {
bc21d315 10983 gfc_charlen *cl = sym->ts.u.cl;
1084b6b0
TB
10984 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT)
10985 {
10986 gfc_error ("Character dummy variable '%s' at %L with VALUE "
10987 "attribute must have constant length",
10988 sym->name, &sym->declared_at);
10989 return;
10990 }
a8b3b0b6
CR
10991
10992 if (sym->ts.is_c_interop
10993 && mpz_cmp_si (cl->length->value.integer, 1) != 0)
10994 {
10995 gfc_error ("C interoperable character dummy variable '%s' at %L "
10996 "with VALUE attribute must have length one",
10997 sym->name, &sym->declared_at);
10998 return;
10999 }
11000 }
11001
11002 /* If the symbol is marked as bind(c), verify it's type and kind. Do not
11003 do this for something that was implicitly typed because that is handled
11004 in gfc_set_default_type. Handle dummy arguments and procedure
11005 definitions separately. Also, anything that is use associated is not
11006 handled here but instead is handled in the module it is declared in.
11007 Finally, derived type definitions are allowed to be BIND(C) since that
11008 only implies that they're interoperable, and they are checked fully for
11009 interoperability when a variable is declared of that type. */
11010 if (sym->attr.is_bind_c && sym->attr.implicit_type == 0 &&
11011 sym->attr.use_assoc == 0 && sym->attr.dummy == 0 &&
11012 sym->attr.flavor != FL_PROCEDURE && sym->attr.flavor != FL_DERIVED)
11013 {
17b1d2a0 11014 gfc_try t = SUCCESS;
a8b3b0b6
CR
11015
11016 /* First, make sure the variable is declared at the
11017 module-level scope (J3/04-007, Section 15.3). */
11018 if (sym->ns->proc_name->attr.flavor != FL_MODULE &&
11019 sym->attr.in_common == 0)
11020 {
11021 gfc_error ("Variable '%s' at %L cannot be BIND(C) because it "
11022 "is neither a COMMON block nor declared at the "
11023 "module level scope", sym->name, &(sym->declared_at));
11024 t = FAILURE;
11025 }
11026 else if (sym->common_head != NULL)
11027 {
11028 t = verify_com_block_vars_c_interop (sym->common_head);
11029 }
11030 else
11031 {
11032 /* If type() declaration, we need to verify that the components
11033 of the given type are all C interoperable, etc. */
11034 if (sym->ts.type == BT_DERIVED &&
bc21d315 11035 sym->ts.u.derived->attr.is_c_interop != 1)
a8b3b0b6
CR
11036 {
11037 /* Make sure the user marked the derived type as BIND(C). If
11038 not, call the verify routine. This could print an error
11039 for the derived type more than once if multiple variables
11040 of that type are declared. */
bc21d315
JW
11041 if (sym->ts.u.derived->attr.is_bind_c != 1)
11042 verify_bind_c_derived_type (sym->ts.u.derived);
a8b3b0b6
CR
11043 t = FAILURE;
11044 }
11045
11046 /* Verify the variable itself as C interoperable if it
11047 is BIND(C). It is not possible for this to succeed if
11048 the verify_bind_c_derived_type failed, so don't have to handle
11049 any error returned by verify_bind_c_derived_type. */
11050 t = verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
11051 sym->common_block);
11052 }
11053
11054 if (t == FAILURE)
11055 {
11056 /* clear the is_bind_c flag to prevent reporting errors more than
11057 once if something failed. */
11058 sym->attr.is_bind_c = 0;
11059 return;
11060 }
1084b6b0
TB
11061 }
11062
976e21f6
PT
11063 /* If a derived type symbol has reached this point, without its
11064 type being declared, we have an error. Notice that most
11065 conditions that produce undefined derived types have already
11066 been dealt with. However, the likes of:
11067 implicit type(t) (t) ..... call foo (t) will get us here if
11068 the type is not declared in the scope of the implicit
11069 statement. Change the type to BT_UNKNOWN, both because it is so
11070 and to prevent an ICE. */
bc21d315
JW
11071 if (sym->ts.type == BT_DERIVED && sym->ts.u.derived->components == NULL
11072 && !sym->ts.u.derived->attr.zero_comp)
976e21f6
PT
11073 {
11074 gfc_error ("The derived type '%s' at %L is of type '%s', "
e25a0da3 11075 "which has not been defined", sym->name,
bc21d315 11076 &sym->declared_at, sym->ts.u.derived->name);
976e21f6
PT
11077 sym->ts.type = BT_UNKNOWN;
11078 return;
11079 }
11080
c1203a70
PT
11081 /* Make sure that the derived type has been resolved and that the
11082 derived type is visible in the symbol's namespace, if it is a
11083 module function and is not PRIVATE. */
11084 if (sym->ts.type == BT_DERIVED
bc21d315 11085 && sym->ts.u.derived->attr.use_assoc
96ffc6cd 11086 && sym->ns->proc_name
c1203a70
PT
11087 && sym->ns->proc_name->attr.flavor == FL_MODULE)
11088 {
11089 gfc_symbol *ds;
11090
bc21d315 11091 if (resolve_fl_derived (sym->ts.u.derived) == FAILURE)
c1203a70
PT
11092 return;
11093
bc21d315 11094 gfc_find_symbol (sym->ts.u.derived->name, sym->ns, 1, &ds);
c1203a70
PT
11095 if (!ds && sym->attr.function
11096 && gfc_check_access (sym->attr.access, sym->ns->default_access))
11097 {
11098 symtree = gfc_new_symtree (&sym->ns->sym_root,
bc21d315
JW
11099 sym->ts.u.derived->name);
11100 symtree->n.sym = sym->ts.u.derived;
11101 sym->ts.u.derived->refs++;
c1203a70
PT
11102 }
11103 }
11104
a08a5751
TB
11105 /* Unless the derived-type declaration is use associated, Fortran 95
11106 does not allow public entries of private derived types.
11107 See 4.4.1 (F95) and 4.5.1.1 (F2003); and related interpretation
11108 161 in 95-006r3. */
11109 if (sym->ts.type == BT_DERIVED
72052237 11110 && sym->ns->proc_name && sym->ns->proc_name->attr.flavor == FL_MODULE
bc21d315 11111 && !sym->ts.u.derived->attr.use_assoc
a08a5751 11112 && gfc_check_access (sym->attr.access, sym->ns->default_access)
bc21d315
JW
11113 && !gfc_check_access (sym->ts.u.derived->attr.access,
11114 sym->ts.u.derived->ns->default_access)
a08a5751
TB
11115 && gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC %s '%s' at %L "
11116 "of PRIVATE derived type '%s'",
11117 (sym->attr.flavor == FL_PARAMETER) ? "parameter"
11118 : "variable", sym->name, &sym->declared_at,
bc21d315 11119 sym->ts.u.derived->name) == FAILURE)
a08a5751
TB
11120 return;
11121
4213f93b
PT
11122 /* An assumed-size array with INTENT(OUT) shall not be of a type for which
11123 default initialization is defined (5.1.2.4.4). */
11124 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
11125 && sym->attr.dummy
11126 && sym->attr.intent == INTENT_OUT
11127 && sym->as
11128 && sym->as->type == AS_ASSUMED_SIZE)
4213f93b 11129 {
bc21d315 11130 for (c = sym->ts.u.derived->components; c; c = c->next)
4213f93b
PT
11131 {
11132 if (c->initializer)
11133 {
11134 gfc_error ("The INTENT(OUT) dummy argument '%s' at %L is "
11135 "ASSUMED SIZE and so cannot have a default initializer",
11136 sym->name, &sym->declared_at);
11137 return;
11138 }
11139 }
11140 }
11141
af30f793 11142 switch (sym->attr.flavor)
54b4ba60 11143 {
af30f793 11144 case FL_VARIABLE:
2ed8d224
PT
11145 if (resolve_fl_variable (sym, mp_flag) == FAILURE)
11146 return;
11147 break;
219fa8c3 11148
2ed8d224
PT
11149 case FL_PROCEDURE:
11150 if (resolve_fl_procedure (sym, mp_flag) == FAILURE)
11151 return;
af30f793
PB
11152 break;
11153
11154 case FL_NAMELIST:
3e1cf500
PT
11155 if (resolve_fl_namelist (sym) == FAILURE)
11156 return;
68ea355b
PT
11157 break;
11158
2ed8d224
PT
11159 case FL_PARAMETER:
11160 if (resolve_fl_parameter (sym) == FAILURE)
11161 return;
e0e85e06
PT
11162 break;
11163
af30f793
PB
11164 default:
11165 break;
54b4ba60
PB
11166 }
11167
6de9cd9a 11168 /* Resolve array specifier. Check as well some constraints
f7b529fa 11169 on COMMON blocks. */
6de9cd9a
DN
11170
11171 check_constant = sym->attr.in_common && !sym->attr.pointer;
98bbe5ee
PT
11172
11173 /* Set the formal_arg_flag so that check_conflict will not throw
11174 an error for host associated variables in the specification
11175 expression for an array_valued function. */
11176 if (sym->attr.function && sym->as)
11177 formal_arg_flag = 1;
11178
6de9cd9a
DN
11179 gfc_resolve_array_spec (sym->as, check_constant);
11180
98bbe5ee
PT
11181 formal_arg_flag = 0;
11182
a34437a1 11183 /* Resolve formal namespaces. */
f6ddbf11 11184 if (sym->formal_ns && sym->formal_ns != gfc_current_ns
e4c1aa19 11185 && !sym->attr.contained && !sym->attr.intrinsic)
a34437a1 11186 gfc_resolve (sym->formal_ns);
6c7a4dfd 11187
acbdc378
JW
11188 /* Make sure the formal namespace is present. */
11189 if (sym->formal && !sym->formal_ns)
11190 {
11191 gfc_formal_arglist *formal = sym->formal;
11192 while (formal && !formal->sym)
11193 formal = formal->next;
11194
11195 if (formal)
11196 {
11197 sym->formal_ns = formal->sym->ns;
11198 sym->formal_ns->refs++;
11199 }
11200 }
11201
6c7a4dfd 11202 /* Check threadprivate restrictions. */
5349080d 11203 if (sym->attr.threadprivate && !sym->attr.save && !sym->ns->save_all
6c7a4dfd 11204 && (!sym->attr.in_common
edf1eac2
SK
11205 && sym->module == NULL
11206 && (sym->ns->proc_name == NULL
11207 || sym->ns->proc_name->attr.flavor != FL_MODULE)))
6c7a4dfd 11208 gfc_error ("Threadprivate at %L isn't SAVEd", &sym->declared_at);
6b591ec0
PT
11209
11210 /* If we have come this far we can apply default-initializers, as
11211 described in 14.7.5, to those variables that have not already
11212 been assigned one. */
7114edca 11213 if (sym->ts.type == BT_DERIVED
edf1eac2
SK
11214 && sym->attr.referenced
11215 && sym->ns == gfc_current_ns
11216 && !sym->value
11217 && !sym->attr.allocatable
11218 && !sym->attr.alloc_comp)
6b591ec0
PT
11219 {
11220 symbol_attribute *a = &sym->attr;
11221
11222 if ((!a->save && !a->dummy && !a->pointer
edf1eac2
SK
11223 && !a->in_common && !a->use_assoc
11224 && !(a->function && sym != sym->result))
758e12af 11225 || (a->dummy && a->intent == INTENT_OUT && !a->pointer))
6b591ec0
PT
11226 apply_default_init (sym);
11227 }
52f49934
DK
11228
11229 /* If this symbol has a type-spec, check it. */
11230 if (sym->attr.flavor == FL_VARIABLE || sym->attr.flavor == FL_PARAMETER
11231 || (sym->attr.flavor == FL_PROCEDURE && sym->attr.function))
11232 if (resolve_typespec_used (&sym->ts, &sym->declared_at, sym->name)
11233 == FAILURE)
11234 return;
6de9cd9a
DN
11235}
11236
11237
6de9cd9a
DN
11238/************* Resolve DATA statements *************/
11239
11240static struct
11241{
11242 gfc_data_value *vnode;
f2112868 11243 mpz_t left;
6de9cd9a
DN
11244}
11245values;
11246
11247
11248/* Advance the values structure to point to the next value in the data list. */
11249
17b1d2a0 11250static gfc_try
6de9cd9a
DN
11251next_data_value (void)
11252{
f2112868 11253 while (mpz_cmp_ui (values.left, 0) == 0)
6de9cd9a 11254 {
abeab938 11255
6de9cd9a
DN
11256 if (values.vnode->next == NULL)
11257 return FAILURE;
11258
11259 values.vnode = values.vnode->next;
f2112868 11260 mpz_set (values.left, values.vnode->repeat);
6de9cd9a
DN
11261 }
11262
6de9cd9a
DN
11263 return SUCCESS;
11264}
11265
11266
17b1d2a0 11267static gfc_try
edf1eac2 11268check_data_variable (gfc_data_variable *var, locus *where)
6de9cd9a
DN
11269{
11270 gfc_expr *e;
11271 mpz_t size;
11272 mpz_t offset;
17b1d2a0 11273 gfc_try t;
f5e440e1 11274 ar_type mark = AR_UNKNOWN;
6de9cd9a
DN
11275 int i;
11276 mpz_t section_index[GFC_MAX_DIMENSIONS];
11277 gfc_ref *ref;
11278 gfc_array_ref *ar;
e49be8f7
PT
11279 gfc_symbol *sym;
11280 int has_pointer;
6de9cd9a
DN
11281
11282 if (gfc_resolve_expr (var->expr) == FAILURE)
11283 return FAILURE;
11284
11285 ar = NULL;
11286 mpz_init_set_si (offset, 0);
11287 e = var->expr;
11288
11289 if (e->expr_type != EXPR_VARIABLE)
11290 gfc_internal_error ("check_data_variable(): Bad expression");
11291
e49be8f7
PT
11292 sym = e->symtree->n.sym;
11293
11294 if (sym->ns->is_block_data && !sym->attr.in_common)
2ed8d224
PT
11295 {
11296 gfc_error ("BLOCK DATA element '%s' at %L must be in COMMON",
e49be8f7 11297 sym->name, &sym->declared_at);
2ed8d224
PT
11298 }
11299
e49be8f7 11300 if (e->ref == NULL && sym->as)
f1607c01
JD
11301 {
11302 gfc_error ("DATA array '%s' at %L must be specified in a previous"
e49be8f7 11303 " declaration", sym->name, where);
f1607c01
JD
11304 return FAILURE;
11305 }
11306
e49be8f7
PT
11307 has_pointer = sym->attr.pointer;
11308
11309 for (ref = e->ref; ref; ref = ref->next)
11310 {
11311 if (ref->type == REF_COMPONENT && ref->u.c.component->attr.pointer)
11312 has_pointer = 1;
11313
11314 if (has_pointer
11315 && ref->type == REF_ARRAY
11316 && ref->u.ar.type != AR_FULL)
11317 {
11318 gfc_error ("DATA element '%s' at %L is a pointer and so must "
11319 "be a full array", sym->name, where);
11320 return FAILURE;
11321 }
11322 }
11323
11324 if (e->rank == 0 || has_pointer)
b8502435
RH
11325 {
11326 mpz_init_set_ui (size, 1);
11327 ref = NULL;
11328 }
6de9cd9a
DN
11329 else
11330 {
11331 ref = e->ref;
11332
11333 /* Find the array section reference. */
11334 for (ref = e->ref; ref; ref = ref->next)
11335 {
11336 if (ref->type != REF_ARRAY)
11337 continue;
11338 if (ref->u.ar.type == AR_ELEMENT)
11339 continue;
11340 break;
11341 }
6e45f57b 11342 gcc_assert (ref);
6de9cd9a 11343
1f2959f0 11344 /* Set marks according to the reference pattern. */
6de9cd9a
DN
11345 switch (ref->u.ar.type)
11346 {
11347 case AR_FULL:
f5e440e1 11348 mark = AR_FULL;
6de9cd9a
DN
11349 break;
11350
11351 case AR_SECTION:
edf1eac2
SK
11352 ar = &ref->u.ar;
11353 /* Get the start position of array section. */
11354 gfc_get_section_index (ar, section_index, &offset);
11355 mark = AR_SECTION;
6de9cd9a
DN
11356 break;
11357
11358 default:
6e45f57b 11359 gcc_unreachable ();
6de9cd9a
DN
11360 }
11361
11362 if (gfc_array_size (e, &size) == FAILURE)
11363 {
11364 gfc_error ("Nonconstant array section at %L in DATA statement",
11365 &e->where);
11366 mpz_clear (offset);
11367 return FAILURE;
11368 }
11369 }
11370
11371 t = SUCCESS;
11372
11373 while (mpz_cmp_ui (size, 0) > 0)
11374 {
11375 if (next_data_value () == FAILURE)
11376 {
11377 gfc_error ("DATA statement at %L has more variables than values",
11378 where);
11379 t = FAILURE;
11380 break;
11381 }
11382
11383 t = gfc_check_assign (var->expr, values.vnode->expr, 0);
11384 if (t == FAILURE)
11385 break;
11386
b8502435
RH
11387 /* If we have more than one element left in the repeat count,
11388 and we have more than one element left in the target variable,
11389 then create a range assignment. */
f2112868 11390 /* FIXME: Only done for full arrays for now, since array sections
b8502435
RH
11391 seem tricky. */
11392 if (mark == AR_FULL && ref && ref->next == NULL
f2112868 11393 && mpz_cmp_ui (values.left, 1) > 0 && mpz_cmp_ui (size, 1) > 0)
b8502435
RH
11394 {
11395 mpz_t range;
11396
f2112868 11397 if (mpz_cmp (size, values.left) >= 0)
b8502435 11398 {
f2112868
SK
11399 mpz_init_set (range, values.left);
11400 mpz_sub (size, size, values.left);
11401 mpz_set_ui (values.left, 0);
b8502435
RH
11402 }
11403 else
11404 {
11405 mpz_init_set (range, size);
f2112868 11406 mpz_sub (values.left, values.left, size);
b8502435
RH
11407 mpz_set_ui (size, 0);
11408 }
11409
11410 gfc_assign_data_value_range (var->expr, values.vnode->expr,
11411 offset, range);
11412
11413 mpz_add (offset, offset, range);
11414 mpz_clear (range);
11415 }
11416
6de9cd9a 11417 /* Assign initial value to symbol. */
b8502435
RH
11418 else
11419 {
f2112868 11420 mpz_sub_ui (values.left, values.left, 1);
b8502435 11421 mpz_sub_ui (size, size, 1);
6de9cd9a 11422
a24668a3
JD
11423 t = gfc_assign_data_value (var->expr, values.vnode->expr, offset);
11424 if (t == FAILURE)
11425 break;
6de9cd9a 11426
b8502435
RH
11427 if (mark == AR_FULL)
11428 mpz_add_ui (offset, offset, 1);
6de9cd9a 11429
b8502435
RH
11430 /* Modify the array section indexes and recalculate the offset
11431 for next element. */
11432 else if (mark == AR_SECTION)
11433 gfc_advance_section (section_index, ar, &offset);
11434 }
6de9cd9a 11435 }
b8502435 11436
f5e440e1 11437 if (mark == AR_SECTION)
6de9cd9a
DN
11438 {
11439 for (i = 0; i < ar->dimen; i++)
edf1eac2 11440 mpz_clear (section_index[i]);
6de9cd9a
DN
11441 }
11442
11443 mpz_clear (size);
11444 mpz_clear (offset);
11445
11446 return t;
11447}
11448
11449
17b1d2a0 11450static gfc_try traverse_data_var (gfc_data_variable *, locus *);
6de9cd9a
DN
11451
11452/* Iterate over a list of elements in a DATA statement. */
11453
17b1d2a0 11454static gfc_try
edf1eac2 11455traverse_data_list (gfc_data_variable *var, locus *where)
6de9cd9a
DN
11456{
11457 mpz_t trip;
11458 iterator_stack frame;
2220652d 11459 gfc_expr *e, *start, *end, *step;
17b1d2a0 11460 gfc_try retval = SUCCESS;
6de9cd9a
DN
11461
11462 mpz_init (frame.value);
11463
2220652d
PT
11464 start = gfc_copy_expr (var->iter.start);
11465 end = gfc_copy_expr (var->iter.end);
11466 step = gfc_copy_expr (var->iter.step);
11467
11468 if (gfc_simplify_expr (start, 1) == FAILURE
edf1eac2 11469 || start->expr_type != EXPR_CONSTANT)
2220652d 11470 {
edf1eac2 11471 gfc_error ("iterator start at %L does not simplify", &start->where);
2220652d
PT
11472 retval = FAILURE;
11473 goto cleanup;
11474 }
11475 if (gfc_simplify_expr (end, 1) == FAILURE
edf1eac2 11476 || end->expr_type != EXPR_CONSTANT)
2220652d 11477 {
edf1eac2 11478 gfc_error ("iterator end at %L does not simplify", &end->where);
2220652d
PT
11479 retval = FAILURE;
11480 goto cleanup;
11481 }
11482 if (gfc_simplify_expr (step, 1) == FAILURE
edf1eac2 11483 || step->expr_type != EXPR_CONSTANT)
2220652d 11484 {
edf1eac2 11485 gfc_error ("iterator step at %L does not simplify", &step->where);
2220652d
PT
11486 retval = FAILURE;
11487 goto cleanup;
11488 }
11489
11490 mpz_init_set (trip, end->value.integer);
11491 mpz_sub (trip, trip, start->value.integer);
11492 mpz_add (trip, trip, step->value.integer);
6de9cd9a 11493
2220652d 11494 mpz_div (trip, trip, step->value.integer);
6de9cd9a 11495
2220652d 11496 mpz_set (frame.value, start->value.integer);
6de9cd9a
DN
11497
11498 frame.prev = iter_stack;
11499 frame.variable = var->iter.var->symtree;
11500 iter_stack = &frame;
11501
11502 while (mpz_cmp_ui (trip, 0) > 0)
11503 {
11504 if (traverse_data_var (var->list, where) == FAILURE)
11505 {
11506 mpz_clear (trip);
2220652d
PT
11507 retval = FAILURE;
11508 goto cleanup;
6de9cd9a
DN
11509 }
11510
11511 e = gfc_copy_expr (var->expr);
11512 if (gfc_simplify_expr (e, 1) == FAILURE)
2220652d
PT
11513 {
11514 gfc_free_expr (e);
11515 mpz_clear (trip);
11516 retval = FAILURE;
11517 goto cleanup;
11518 }
6de9cd9a 11519
2220652d 11520 mpz_add (frame.value, frame.value, step->value.integer);
6de9cd9a
DN
11521
11522 mpz_sub_ui (trip, trip, 1);
11523 }
11524
11525 mpz_clear (trip);
2220652d 11526cleanup:
6de9cd9a
DN
11527 mpz_clear (frame.value);
11528
2220652d
PT
11529 gfc_free_expr (start);
11530 gfc_free_expr (end);
11531 gfc_free_expr (step);
11532
6de9cd9a 11533 iter_stack = frame.prev;
2220652d 11534 return retval;
6de9cd9a
DN
11535}
11536
11537
11538/* Type resolve variables in the variable list of a DATA statement. */
11539
17b1d2a0 11540static gfc_try
edf1eac2 11541traverse_data_var (gfc_data_variable *var, locus *where)
6de9cd9a 11542{
17b1d2a0 11543 gfc_try t;
6de9cd9a
DN
11544
11545 for (; var; var = var->next)
11546 {
11547 if (var->expr == NULL)
11548 t = traverse_data_list (var, where);
11549 else
11550 t = check_data_variable (var, where);
11551
11552 if (t == FAILURE)
11553 return FAILURE;
11554 }
11555
11556 return SUCCESS;
11557}
11558
11559
11560/* Resolve the expressions and iterators associated with a data statement.
11561 This is separate from the assignment checking because data lists should
11562 only be resolved once. */
11563
17b1d2a0 11564static gfc_try
edf1eac2 11565resolve_data_variables (gfc_data_variable *d)
6de9cd9a 11566{
6de9cd9a
DN
11567 for (; d; d = d->next)
11568 {
11569 if (d->list == NULL)
11570 {
11571 if (gfc_resolve_expr (d->expr) == FAILURE)
11572 return FAILURE;
11573 }
11574 else
11575 {
8d5cfa27 11576 if (gfc_resolve_iterator (&d->iter, false) == FAILURE)
6de9cd9a
DN
11577 return FAILURE;
11578
6de9cd9a
DN
11579 if (resolve_data_variables (d->list) == FAILURE)
11580 return FAILURE;
11581 }
11582 }
11583
11584 return SUCCESS;
11585}
11586
11587
11588/* Resolve a single DATA statement. We implement this by storing a pointer to
11589 the value list into static variables, and then recursively traversing the
11590 variables list, expanding iterators and such. */
11591
11592static void
f2112868 11593resolve_data (gfc_data *d)
6de9cd9a 11594{
f2112868 11595
6de9cd9a
DN
11596 if (resolve_data_variables (d->var) == FAILURE)
11597 return;
11598
11599 values.vnode = d->value;
f2112868
SK
11600 if (d->value == NULL)
11601 mpz_set_ui (values.left, 0);
11602 else
11603 mpz_set (values.left, d->value->repeat);
6de9cd9a
DN
11604
11605 if (traverse_data_var (d->var, &d->where) == FAILURE)
11606 return;
11607
11608 /* At this point, we better not have any values left. */
11609
11610 if (next_data_value () == SUCCESS)
11611 gfc_error ("DATA statement at %L has more values than variables",
11612 &d->where);
11613}
11614
11615
d2088bb6
PT
11616/* 12.6 Constraint: In a pure subprogram any variable which is in common or
11617 accessed by host or use association, is a dummy argument to a pure function,
11618 is a dummy argument with INTENT (IN) to a pure subroutine, or an object that
11619 is storage associated with any such variable, shall not be used in the
11620 following contexts: (clients of this function). */
11621
df2fba9e 11622/* Determines if a variable is not 'pure', i.e., not assignable within a pure
edf1eac2
SK
11623 procedure. Returns zero if assignment is OK, nonzero if there is a
11624 problem. */
6de9cd9a 11625int
edf1eac2 11626gfc_impure_variable (gfc_symbol *sym)
6de9cd9a 11627{
d2088bb6
PT
11628 gfc_symbol *proc;
11629
6de9cd9a
DN
11630 if (sym->attr.use_assoc || sym->attr.in_common)
11631 return 1;
11632
11633 if (sym->ns != gfc_current_ns)
11634 return !sym->attr.function;
11635
d2088bb6
PT
11636 proc = sym->ns->proc_name;
11637 if (sym->attr.dummy && gfc_pure (proc)
11638 && ((proc->attr.subroutine && sym->attr.intent == INTENT_IN)
11639 ||
11640 proc->attr.function))
11641 return 1;
6de9cd9a 11642
d2088bb6
PT
11643 /* TODO: Sort out what can be storage associated, if anything, and include
11644 it here. In principle equivalences should be scanned but it does not
11645 seem to be possible to storage associate an impure variable this way. */
6de9cd9a
DN
11646 return 0;
11647}
11648
11649
11650/* Test whether a symbol is pure or not. For a NULL pointer, checks the
11651 symbol of the current procedure. */
11652
11653int
edf1eac2 11654gfc_pure (gfc_symbol *sym)
6de9cd9a
DN
11655{
11656 symbol_attribute attr;
11657
11658 if (sym == NULL)
11659 sym = gfc_current_ns->proc_name;
11660 if (sym == NULL)
11661 return 0;
11662
11663 attr = sym->attr;
11664
11665 return attr.flavor == FL_PROCEDURE && (attr.pure || attr.elemental);
11666}
11667
11668
11669/* Test whether the current procedure is elemental or not. */
11670
11671int
edf1eac2 11672gfc_elemental (gfc_symbol *sym)
6de9cd9a
DN
11673{
11674 symbol_attribute attr;
11675
11676 if (sym == NULL)
11677 sym = gfc_current_ns->proc_name;
11678 if (sym == NULL)
11679 return 0;
11680 attr = sym->attr;
11681
11682 return attr.flavor == FL_PROCEDURE && attr.elemental;
11683}
11684
11685
11686/* Warn about unused labels. */
11687
11688static void
edf1eac2 11689warn_unused_fortran_label (gfc_st_label *label)
6de9cd9a 11690{
5cf54585 11691 if (label == NULL)
6de9cd9a
DN
11692 return;
11693
994c1cc0 11694 warn_unused_fortran_label (label->left);
6de9cd9a 11695
5cf54585
TS
11696 if (label->defined == ST_LABEL_UNKNOWN)
11697 return;
6de9cd9a 11698
5cf54585
TS
11699 switch (label->referenced)
11700 {
11701 case ST_LABEL_UNKNOWN:
11702 gfc_warning ("Label %d at %L defined but not used", label->value,
11703 &label->where);
11704 break;
6de9cd9a 11705
5cf54585
TS
11706 case ST_LABEL_BAD_TARGET:
11707 gfc_warning ("Label %d at %L defined but cannot be used",
11708 label->value, &label->where);
11709 break;
6de9cd9a 11710
5cf54585
TS
11711 default:
11712 break;
6de9cd9a 11713 }
5cf54585 11714
994c1cc0 11715 warn_unused_fortran_label (label->right);
6de9cd9a
DN
11716}
11717
11718
e8ec07e1
PT
11719/* Returns the sequence type of a symbol or sequence. */
11720
11721static seq_type
11722sequence_type (gfc_typespec ts)
11723{
11724 seq_type result;
11725 gfc_component *c;
11726
11727 switch (ts.type)
11728 {
11729 case BT_DERIVED:
11730
bc21d315 11731 if (ts.u.derived->components == NULL)
e8ec07e1
PT
11732 return SEQ_NONDEFAULT;
11733
bc21d315
JW
11734 result = sequence_type (ts.u.derived->components->ts);
11735 for (c = ts.u.derived->components->next; c; c = c->next)
e8ec07e1
PT
11736 if (sequence_type (c->ts) != result)
11737 return SEQ_MIXED;
11738
11739 return result;
11740
11741 case BT_CHARACTER:
11742 if (ts.kind != gfc_default_character_kind)
11743 return SEQ_NONDEFAULT;
11744
11745 return SEQ_CHARACTER;
11746
11747 case BT_INTEGER:
11748 if (ts.kind != gfc_default_integer_kind)
11749 return SEQ_NONDEFAULT;
11750
11751 return SEQ_NUMERIC;
11752
11753 case BT_REAL:
11754 if (!(ts.kind == gfc_default_real_kind
edf1eac2 11755 || ts.kind == gfc_default_double_kind))
e8ec07e1
PT
11756 return SEQ_NONDEFAULT;
11757
11758 return SEQ_NUMERIC;
11759
11760 case BT_COMPLEX:
11761 if (ts.kind != gfc_default_complex_kind)
11762 return SEQ_NONDEFAULT;
11763
11764 return SEQ_NUMERIC;
11765
11766 case BT_LOGICAL:
11767 if (ts.kind != gfc_default_logical_kind)
11768 return SEQ_NONDEFAULT;
11769
11770 return SEQ_NUMERIC;
11771
11772 default:
11773 return SEQ_NONDEFAULT;
11774 }
11775}
11776
11777
6de9cd9a
DN
11778/* Resolve derived type EQUIVALENCE object. */
11779
17b1d2a0 11780static gfc_try
6de9cd9a
DN
11781resolve_equivalence_derived (gfc_symbol *derived, gfc_symbol *sym, gfc_expr *e)
11782{
6de9cd9a
DN
11783 gfc_component *c = derived->components;
11784
11785 if (!derived)
11786 return SUCCESS;
11787
11788 /* Shall not be an object of nonsequence derived type. */
11789 if (!derived->attr.sequence)
11790 {
11791 gfc_error ("Derived type variable '%s' at %L must have SEQUENCE "
edf1eac2
SK
11792 "attribute to be an EQUIVALENCE object", sym->name,
11793 &e->where);
6de9cd9a
DN
11794 return FAILURE;
11795 }
11796
66e4ab31 11797 /* Shall not have allocatable components. */
5046aff5
PT
11798 if (derived->attr.alloc_comp)
11799 {
11800 gfc_error ("Derived type variable '%s' at %L cannot have ALLOCATABLE "
edf1eac2
SK
11801 "components to be an EQUIVALENCE object",sym->name,
11802 &e->where);
5046aff5
PT
11803 return FAILURE;
11804 }
11805
bc21d315 11806 if (sym->attr.in_common && has_default_initializer (sym->ts.u.derived))
cddcf0d4
TB
11807 {
11808 gfc_error ("Derived type variable '%s' at %L with default "
11809 "initialization cannot be in EQUIVALENCE with a variable "
11810 "in COMMON", sym->name, &e->where);
11811 return FAILURE;
11812 }
11813
6de9cd9a
DN
11814 for (; c ; c = c->next)
11815 {
bc21d315
JW
11816 if (c->ts.type == BT_DERIVED
11817 && (resolve_equivalence_derived (c->ts.u.derived, sym, e) == FAILURE))
edf1eac2 11818 return FAILURE;
05c1e3a7 11819
6de9cd9a 11820 /* Shall not be an object of sequence derived type containing a pointer
edf1eac2 11821 in the structure. */
d4b7d0f0 11822 if (c->attr.pointer)
edf1eac2
SK
11823 {
11824 gfc_error ("Derived type variable '%s' at %L with pointer "
11825 "component(s) cannot be an EQUIVALENCE object",
11826 sym->name, &e->where);
11827 return FAILURE;
11828 }
6de9cd9a
DN
11829 }
11830 return SUCCESS;
11831}
11832
11833
11834/* Resolve equivalence object.
e8ec07e1
PT
11835 An EQUIVALENCE object shall not be a dummy argument, a pointer, a target,
11836 an allocatable array, an object of nonsequence derived type, an object of
6de9cd9a
DN
11837 sequence derived type containing a pointer at any level of component
11838 selection, an automatic object, a function name, an entry name, a result
11839 name, a named constant, a structure component, or a subobject of any of
e8ec07e1
PT
11840 the preceding objects. A substring shall not have length zero. A
11841 derived type shall not have components with default initialization nor
11842 shall two objects of an equivalence group be initialized.
ee7e677f 11843 Either all or none of the objects shall have an protected attribute.
e8ec07e1
PT
11844 The simple constraints are done in symbol.c(check_conflict) and the rest
11845 are implemented here. */
6de9cd9a
DN
11846
11847static void
11848resolve_equivalence (gfc_equiv *eq)
11849{
11850 gfc_symbol *sym;
e8ec07e1 11851 gfc_symbol *first_sym;
6de9cd9a
DN
11852 gfc_expr *e;
11853 gfc_ref *r;
e8ec07e1
PT
11854 locus *last_where = NULL;
11855 seq_type eq_type, last_eq_type;
11856 gfc_typespec *last_ts;
ee7e677f 11857 int object, cnt_protected;
e8ec07e1
PT
11858 const char *msg;
11859
e8ec07e1 11860 last_ts = &eq->expr->symtree->n.sym->ts;
6de9cd9a 11861
e8ec07e1
PT
11862 first_sym = eq->expr->symtree->n.sym;
11863
ee7e677f
TB
11864 cnt_protected = 0;
11865
e8ec07e1 11866 for (object = 1; eq; eq = eq->eq, object++)
6de9cd9a
DN
11867 {
11868 e = eq->expr;
a8006d09
JJ
11869
11870 e->ts = e->symtree->n.sym->ts;
11871 /* match_varspec might not know yet if it is seeing
11872 array reference or substring reference, as it doesn't
11873 know the types. */
11874 if (e->ref && e->ref->type == REF_ARRAY)
11875 {
11876 gfc_ref *ref = e->ref;
11877 sym = e->symtree->n.sym;
11878
11879 if (sym->attr.dimension)
11880 {
11881 ref->u.ar.as = sym->as;
11882 ref = ref->next;
11883 }
11884
11885 /* For substrings, convert REF_ARRAY into REF_SUBSTRING. */
11886 if (e->ts.type == BT_CHARACTER
11887 && ref
11888 && ref->type == REF_ARRAY
11889 && ref->u.ar.dimen == 1
11890 && ref->u.ar.dimen_type[0] == DIMEN_RANGE
11891 && ref->u.ar.stride[0] == NULL)
11892 {
11893 gfc_expr *start = ref->u.ar.start[0];
11894 gfc_expr *end = ref->u.ar.end[0];
11895 void *mem = NULL;
11896
11897 /* Optimize away the (:) reference. */
11898 if (start == NULL && end == NULL)
11899 {
11900 if (e->ref == ref)
11901 e->ref = ref->next;
11902 else
11903 e->ref->next = ref->next;
11904 mem = ref;
11905 }
11906 else
11907 {
11908 ref->type = REF_SUBSTRING;
11909 if (start == NULL)
11910 start = gfc_int_expr (1);
11911 ref->u.ss.start = start;
bc21d315
JW
11912 if (end == NULL && e->ts.u.cl)
11913 end = gfc_copy_expr (e->ts.u.cl->length);
a8006d09 11914 ref->u.ss.end = end;
bc21d315
JW
11915 ref->u.ss.length = e->ts.u.cl;
11916 e->ts.u.cl = NULL;
a8006d09
JJ
11917 }
11918 ref = ref->next;
11919 gfc_free (mem);
11920 }
11921
11922 /* Any further ref is an error. */
11923 if (ref)
11924 {
11925 gcc_assert (ref->type == REF_ARRAY);
11926 gfc_error ("Syntax error in EQUIVALENCE statement at %L",
11927 &ref->u.ar.where);
11928 continue;
11929 }
11930 }
11931
6de9cd9a 11932 if (gfc_resolve_expr (e) == FAILURE)
edf1eac2 11933 continue;
6de9cd9a
DN
11934
11935 sym = e->symtree->n.sym;
6de9cd9a 11936
9aa433c2 11937 if (sym->attr.is_protected)
ee7e677f
TB
11938 cnt_protected++;
11939 if (cnt_protected > 0 && cnt_protected != object)
11940 {
11941 gfc_error ("Either all or none of the objects in the "
11942 "EQUIVALENCE set at %L shall have the "
11943 "PROTECTED attribute",
11944 &e->where);
11945 break;
edf1eac2 11946 }
ee7e677f 11947
e8ec07e1 11948 /* Shall not equivalence common block variables in a PURE procedure. */
05c1e3a7 11949 if (sym->ns->proc_name
edf1eac2
SK
11950 && sym->ns->proc_name->attr.pure
11951 && sym->attr.in_common)
11952 {
11953 gfc_error ("Common block member '%s' at %L cannot be an EQUIVALENCE "
e8ec07e1
PT
11954 "object in the pure procedure '%s'",
11955 sym->name, &e->where, sym->ns->proc_name->name);
edf1eac2
SK
11956 break;
11957 }
05c1e3a7
BF
11958
11959 /* Shall not be a named constant. */
6de9cd9a 11960 if (e->expr_type == EXPR_CONSTANT)
edf1eac2
SK
11961 {
11962 gfc_error ("Named constant '%s' at %L cannot be an EQUIVALENCE "
11963 "object", sym->name, &e->where);
11964 continue;
11965 }
6de9cd9a 11966
bc21d315
JW
11967 if (e->ts.type == BT_DERIVED
11968 && resolve_equivalence_derived (e->ts.u.derived, sym, e) == FAILURE)
edf1eac2 11969 continue;
6de9cd9a 11970
e8ec07e1
PT
11971 /* Check that the types correspond correctly:
11972 Note 5.28:
11973 A numeric sequence structure may be equivalenced to another sequence
11974 structure, an object of default integer type, default real type, double
11975 precision real type, default logical type such that components of the
11976 structure ultimately only become associated to objects of the same
11977 kind. A character sequence structure may be equivalenced to an object
11978 of default character kind or another character sequence structure.
11979 Other objects may be equivalenced only to objects of the same type and
11980 kind parameters. */
11981
11982 /* Identical types are unconditionally OK. */
11983 if (object == 1 || gfc_compare_types (last_ts, &sym->ts))
11984 goto identical_types;
11985
11986 last_eq_type = sequence_type (*last_ts);
11987 eq_type = sequence_type (sym->ts);
11988
11989 /* Since the pair of objects is not of the same type, mixed or
11990 non-default sequences can be rejected. */
11991
11992 msg = "Sequence %s with mixed components in EQUIVALENCE "
11993 "statement at %L with different type objects";
11994 if ((object ==2
edf1eac2
SK
11995 && last_eq_type == SEQ_MIXED
11996 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name, last_where)
11997 == FAILURE)
11998 || (eq_type == SEQ_MIXED
11999 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12000 &e->where) == FAILURE))
e8ec07e1
PT
12001 continue;
12002
12003 msg = "Non-default type object or sequence %s in EQUIVALENCE "
12004 "statement at %L with objects of different type";
12005 if ((object ==2
edf1eac2
SK
12006 && last_eq_type == SEQ_NONDEFAULT
12007 && gfc_notify_std (GFC_STD_GNU, msg, first_sym->name,
12008 last_where) == FAILURE)
12009 || (eq_type == SEQ_NONDEFAULT
12010 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12011 &e->where) == FAILURE))
e8ec07e1
PT
12012 continue;
12013
12014 msg ="Non-CHARACTER object '%s' in default CHARACTER "
12015 "EQUIVALENCE statement at %L";
12016 if (last_eq_type == SEQ_CHARACTER
edf1eac2
SK
12017 && eq_type != SEQ_CHARACTER
12018 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12019 &e->where) == FAILURE)
e8ec07e1
PT
12020 continue;
12021
12022 msg ="Non-NUMERIC object '%s' in default NUMERIC "
12023 "EQUIVALENCE statement at %L";
12024 if (last_eq_type == SEQ_NUMERIC
edf1eac2
SK
12025 && eq_type != SEQ_NUMERIC
12026 && gfc_notify_std (GFC_STD_GNU, msg, sym->name,
12027 &e->where) == FAILURE)
e8ec07e1
PT
12028 continue;
12029
12030 identical_types:
12031 last_ts =&sym->ts;
12032 last_where = &e->where;
12033
6de9cd9a 12034 if (!e->ref)
edf1eac2 12035 continue;
6de9cd9a
DN
12036
12037 /* Shall not be an automatic array. */
12038 if (e->ref->type == REF_ARRAY
edf1eac2
SK
12039 && gfc_resolve_array_spec (e->ref->u.ar.as, 1) == FAILURE)
12040 {
12041 gfc_error ("Array '%s' at %L with non-constant bounds cannot be "
12042 "an EQUIVALENCE object", sym->name, &e->where);
12043 continue;
12044 }
6de9cd9a 12045
6de9cd9a
DN
12046 r = e->ref;
12047 while (r)
edf1eac2 12048 {
a8006d09
JJ
12049 /* Shall not be a structure component. */
12050 if (r->type == REF_COMPONENT)
12051 {
12052 gfc_error ("Structure component '%s' at %L cannot be an "
12053 "EQUIVALENCE object",
12054 r->u.c.component->name, &e->where);
12055 break;
12056 }
12057
12058 /* A substring shall not have length zero. */
12059 if (r->type == REF_SUBSTRING)
12060 {
12061 if (compare_bound (r->u.ss.start, r->u.ss.end) == CMP_GT)
12062 {
12063 gfc_error ("Substring at %L has length zero",
12064 &r->u.ss.start->where);
12065 break;
12066 }
12067 }
12068 r = r->next;
12069 }
05c1e3a7
BF
12070 }
12071}
cf4d246b
JJ
12072
12073
66e4ab31 12074/* Resolve function and ENTRY types, issue diagnostics if needed. */
cf4d246b
JJ
12075
12076static void
edf1eac2 12077resolve_fntype (gfc_namespace *ns)
cf4d246b
JJ
12078{
12079 gfc_entry_list *el;
12080 gfc_symbol *sym;
12081
12082 if (ns->proc_name == NULL || !ns->proc_name->attr.function)
12083 return;
12084
12085 /* If there are any entries, ns->proc_name is the entry master
12086 synthetic symbol and ns->entries->sym actual FUNCTION symbol. */
12087 if (ns->entries)
12088 sym = ns->entries->sym;
12089 else
12090 sym = ns->proc_name;
12091 if (sym->result == sym
12092 && sym->ts.type == BT_UNKNOWN
12093 && gfc_set_default_type (sym, 0, NULL) == FAILURE
12094 && !sym->attr.untyped)
12095 {
12096 gfc_error ("Function '%s' at %L has no IMPLICIT type",
12097 sym->name, &sym->declared_at);
12098 sym->attr.untyped = 1;
12099 }
12100
bc21d315 12101 if (sym->ts.type == BT_DERIVED && !sym->ts.u.derived->attr.use_assoc
0d6872cb 12102 && !sym->attr.contained
bc21d315
JW
12103 && !gfc_check_access (sym->ts.u.derived->attr.access,
12104 sym->ts.u.derived->ns->default_access)
3bcc018c
EE
12105 && gfc_check_access (sym->attr.access, sym->ns->default_access))
12106 {
0d6872cb
TB
12107 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PUBLIC function '%s' at "
12108 "%L of PRIVATE type '%s'", sym->name,
bc21d315 12109 &sym->declared_at, sym->ts.u.derived->name);
3bcc018c
EE
12110 }
12111
7453378e 12112 if (ns->entries)
cf4d246b
JJ
12113 for (el = ns->entries->next; el; el = el->next)
12114 {
12115 if (el->sym->result == el->sym
12116 && el->sym->ts.type == BT_UNKNOWN
12117 && gfc_set_default_type (el->sym, 0, NULL) == FAILURE
12118 && !el->sym->attr.untyped)
12119 {
12120 gfc_error ("ENTRY '%s' at %L has no IMPLICIT type",
12121 el->sym->name, &el->sym->declared_at);
12122 el->sym->attr.untyped = 1;
12123 }
12124 }
12125}
12126
94747289 12127
0e3e65bc
PT
12128/* 12.3.2.1.1 Defined operators. */
12129
94747289
DK
12130static gfc_try
12131check_uop_procedure (gfc_symbol *sym, locus where)
0e3e65bc 12132{
0e3e65bc
PT
12133 gfc_formal_arglist *formal;
12134
94747289
DK
12135 if (!sym->attr.function)
12136 {
12137 gfc_error ("User operator procedure '%s' at %L must be a FUNCTION",
12138 sym->name, &where);
12139 return FAILURE;
12140 }
05c1e3a7 12141
94747289 12142 if (sym->ts.type == BT_CHARACTER
bc21d315
JW
12143 && !(sym->ts.u.cl && sym->ts.u.cl->length)
12144 && !(sym->result && sym->result->ts.u.cl
12145 && sym->result->ts.u.cl->length))
94747289
DK
12146 {
12147 gfc_error ("User operator procedure '%s' at %L cannot be assumed "
12148 "character length", sym->name, &where);
12149 return FAILURE;
12150 }
0e3e65bc 12151
94747289
DK
12152 formal = sym->formal;
12153 if (!formal || !formal->sym)
0e3e65bc 12154 {
94747289
DK
12155 gfc_error ("User operator procedure '%s' at %L must have at least "
12156 "one argument", sym->name, &where);
12157 return FAILURE;
12158 }
0e3e65bc 12159
94747289
DK
12160 if (formal->sym->attr.intent != INTENT_IN)
12161 {
12162 gfc_error ("First argument of operator interface at %L must be "
12163 "INTENT(IN)", &where);
12164 return FAILURE;
12165 }
0e3e65bc 12166
94747289
DK
12167 if (formal->sym->attr.optional)
12168 {
12169 gfc_error ("First argument of operator interface at %L cannot be "
12170 "optional", &where);
12171 return FAILURE;
12172 }
0e3e65bc 12173
94747289
DK
12174 formal = formal->next;
12175 if (!formal || !formal->sym)
12176 return SUCCESS;
0e3e65bc 12177
94747289
DK
12178 if (formal->sym->attr.intent != INTENT_IN)
12179 {
12180 gfc_error ("Second argument of operator interface at %L must be "
12181 "INTENT(IN)", &where);
12182 return FAILURE;
12183 }
0e3e65bc 12184
94747289
DK
12185 if (formal->sym->attr.optional)
12186 {
12187 gfc_error ("Second argument of operator interface at %L cannot be "
12188 "optional", &where);
12189 return FAILURE;
12190 }
0e3e65bc 12191
94747289
DK
12192 if (formal->next)
12193 {
12194 gfc_error ("Operator interface at %L must have, at most, two "
12195 "arguments", &where);
12196 return FAILURE;
12197 }
0e3e65bc 12198
94747289
DK
12199 return SUCCESS;
12200}
0e3e65bc 12201
94747289
DK
12202static void
12203gfc_resolve_uops (gfc_symtree *symtree)
12204{
12205 gfc_interface *itr;
12206
12207 if (symtree == NULL)
12208 return;
12209
12210 gfc_resolve_uops (symtree->left);
12211 gfc_resolve_uops (symtree->right);
12212
12213 for (itr = symtree->n.uop->op; itr; itr = itr->next)
12214 check_uop_procedure (itr->sym, itr->sym->declared_at);
0e3e65bc
PT
12215}
12216
cf4d246b 12217
efb0828d
L
12218/* Examine all of the expressions associated with a program unit,
12219 assign types to all intermediate expressions, make sure that all
12220 assignments are to compatible types and figure out which names
12221 refer to which functions or subroutines. It doesn't check code
12222 block, which is handled by resolve_code. */
6de9cd9a 12223
efb0828d 12224static void
edf1eac2 12225resolve_types (gfc_namespace *ns)
6de9cd9a 12226{
efb0828d 12227 gfc_namespace *n;
6de9cd9a
DN
12228 gfc_charlen *cl;
12229 gfc_data *d;
12230 gfc_equiv *eq;
a82f1f2e 12231 gfc_namespace* old_ns = gfc_current_ns;
6de9cd9a 12232
52f49934
DK
12233 /* Check that all IMPLICIT types are ok. */
12234 if (!ns->seen_implicit_none)
12235 {
12236 unsigned letter;
12237 for (letter = 0; letter != GFC_LETTERS; ++letter)
12238 if (ns->set_flag[letter]
12239 && resolve_typespec_used (&ns->default_type[letter],
12240 &ns->implicit_loc[letter],
12241 NULL) == FAILURE)
12242 return;
12243 }
12244
a82f1f2e
DK
12245 gfc_current_ns = ns;
12246
0f3162e3
PT
12247 resolve_entries (ns);
12248
346ecba8 12249 resolve_common_vars (ns->blank_common.head, false);
ad22b1ff
TB
12250 resolve_common_blocks (ns->common_root);
12251
0f3162e3
PT
12252 resolve_contained_functions (ns);
12253
a8b3b0b6
CR
12254 gfc_traverse_ns (ns, resolve_bind_c_derived_types);
12255
5cd09fac
TS
12256 for (cl = ns->cl_list; cl; cl = cl->next)
12257 resolve_charlen (cl);
12258
6de9cd9a
DN
12259 gfc_traverse_ns (ns, resolve_symbol);
12260
cf4d246b
JJ
12261 resolve_fntype (ns);
12262
6de9cd9a
DN
12263 for (n = ns->contained; n; n = n->sibling)
12264 {
12265 if (gfc_pure (ns->proc_name) && !gfc_pure (n->proc_name))
12266 gfc_error ("Contained procedure '%s' at %L of a PURE procedure must "
12267 "also be PURE", n->proc_name->name,
12268 &n->proc_name->declared_at);
12269
efb0828d 12270 resolve_types (n);
6de9cd9a
DN
12271 }
12272
12273 forall_flag = 0;
12274 gfc_check_interfaces (ns);
12275
6de9cd9a
DN
12276 gfc_traverse_ns (ns, resolve_values);
12277
d05d9ac7 12278 if (ns->save_all)
6de9cd9a
DN
12279 gfc_save_all (ns);
12280
12281 iter_stack = NULL;
12282 for (d = ns->data; d; d = d->next)
12283 resolve_data (d);
12284
12285 iter_stack = NULL;
12286 gfc_traverse_ns (ns, gfc_formalize_init_value);
12287
a8b3b0b6
CR
12288 gfc_traverse_ns (ns, gfc_verify_binding_labels);
12289
12290 if (ns->common_root != NULL)
12291 gfc_traverse_symtree (ns->common_root, resolve_bind_c_comms);
12292
6de9cd9a
DN
12293 for (eq = ns->equiv; eq; eq = eq->next)
12294 resolve_equivalence (eq);
12295
6de9cd9a 12296 /* Warn about unused labels. */
2e5758e8 12297 if (warn_unused_label)
994c1cc0 12298 warn_unused_fortran_label (ns->st_labels);
0e3e65bc
PT
12299
12300 gfc_resolve_uops (ns->uop_root);
a82f1f2e
DK
12301
12302 gfc_current_ns = old_ns;
efb0828d
L
12303}
12304
12305
12306/* Call resolve_code recursively. */
12307
12308static void
edf1eac2 12309resolve_codes (gfc_namespace *ns)
efb0828d
L
12310{
12311 gfc_namespace *n;
71a7778c 12312 bitmap_obstack old_obstack;
efb0828d
L
12313
12314 for (n = ns->contained; n; n = n->sibling)
12315 resolve_codes (n);
12316
12317 gfc_current_ns = ns;
76d02e9f
JW
12318
12319 /* Don't clear 'cs_base' if this is the namespace of a BLOCK construct. */
12320 if (!(ns->proc_name && ns->proc_name->attr.flavor == FL_LABEL))
12321 cs_base = NULL;
12322
0e9a445b
PT
12323 /* Set to an out of range value. */
12324 current_entry_id = -1;
0615f923 12325
71a7778c 12326 old_obstack = labels_obstack;
0615f923 12327 bitmap_obstack_initialize (&labels_obstack);
71a7778c 12328
efb0828d 12329 resolve_code (ns->code, ns);
71a7778c 12330
0615f923 12331 bitmap_obstack_release (&labels_obstack);
71a7778c 12332 labels_obstack = old_obstack;
efb0828d
L
12333}
12334
12335
12336/* This function is called after a complete program unit has been compiled.
12337 Its purpose is to examine all of the expressions associated with a program
12338 unit, assign types to all intermediate expressions, make sure that all
12339 assignments are to compatible types and figure out which names refer to
12340 which functions or subroutines. */
12341
12342void
edf1eac2 12343gfc_resolve (gfc_namespace *ns)
efb0828d
L
12344{
12345 gfc_namespace *old_ns;
3af8d8cb 12346 code_stack *old_cs_base;
efb0828d 12347
71a7778c
PT
12348 if (ns->resolved)
12349 return;
12350
3af8d8cb 12351 ns->resolved = -1;
efb0828d 12352 old_ns = gfc_current_ns;
3af8d8cb 12353 old_cs_base = cs_base;
efb0828d
L
12354
12355 resolve_types (ns);
12356 resolve_codes (ns);
6de9cd9a
DN
12357
12358 gfc_current_ns = old_ns;
3af8d8cb 12359 cs_base = old_cs_base;
71a7778c 12360 ns->resolved = 1;
6de9cd9a 12361}
This page took 3.614569 seconds and 5 git commands to generate.