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